-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcommon.c
323 lines (269 loc) · 7.67 KB
/
common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* common.c: This file is part of the `djbdns' project, originally written
* by Dr. D J Bernstein and later released under public-domain since late
* December 2007 (http://cr.yp.to/distributors.html).
*
* Copyright (C) 2009 - 2015 Prasad J Pandit
*
* This program is a free software; you can redistribute it and/or modify
* it under the terms of GNU General Public License as published by Free
* Software Foundation; either version 2 of the license or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* of FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <err.h>
#include <time.h>
#include <ctype.h>
#include <stdio.h>
#include <fcntl.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <netinet/in.h>
#include "taia.h"
#include "uint32.h"
#define free(ptr) free ((ptr)); (ptr) = NULL
extern short mode, debug_level;
uint32 seed[32];
int seedpos = 0;
void
seed_adduint32 (uint32 u)
{
int i = 0;
seed[seedpos] += u;
if (++seedpos == 32)
{
for (i = 0; i < 32; ++i)
{
u = ((u ^ seed[i]) + 0x9e3779b9) ^ (u << 7) ^ (u >> 25);
seed[i] = u;
}
seedpos = 0;
}
}
void
seed_addtime (void)
{
int i = 0;
struct taia t;
char tpack[TAIA_PACK];
taia_now (&t);
taia_pack (tpack, &t);
for (i = 0; i < TAIA_PACK; ++i)
seed_adduint32 (tpack[i]);
}
/*
* strtrim: removes leading & trailing white spaces(space, tab, new-line, etc)
* from a given character string and returns a pointer to the new string.
* Do free(3) it later.
*/
char *
strtrim (const char *s)
{
if (s == NULL)
return NULL;
const char *e = &s[strlen(s) - 1];
while (*s)
{
if (isspace (*s))
s++;
else
break;
}
while (*e)
{
if (isspace (*e))
e--;
else
break;
}
e++;
return strndup (s, e - s);
}
/* checks if the given variable is valid & used by dnscache. */
int
check_variable (const char *var)
{
assert (var != NULL);
int i = 0, l = 0;
const char *known_variable[] = \
{
"AXFR", "DATALIMIT", "CACHESIZE", "IP", "IPSEND",
"UID", "GID", "ROOT", "HIDETTL", "FORWARDONLY",
"MERGEQUERIES", "DEBUG_LEVEL", "BASE", "TCPREMOTEIP",
"TCPREMOTEPORT"
};
l = sizeof (known_variable) / sizeof (*known_variable);
for (i = 0; i < l; i++)
{
if (strlen (var) != strlen (known_variable[i]))
continue;
if (!memcmp (var, known_variable[i], strlen (var)))
return 1;
}
return 0;
}
void
read_conf (const char *file)
{
assert (file != NULL);
int lcount = 0;
FILE *fp = NULL;
size_t l = 0, n = 0;
char *line = NULL, *key = NULL, *val = NULL;
if (!(fp = fopen (file, "r")))
err (-1, "could not open file `%s'", file);
while ((signed)(n = getline (&line, &l, fp)) != -1)
{
lcount++;
line[n - 1] = '\0';
char *s = strtrim (line);
if (*s && *s != '#' && *s != '\n')
{
key = strtrim (strtok (s, "="));
if (!check_variable (key))
errx (-1, "%s: %d: unknown variable `%s'", file, lcount, key);
val = strtrim (strtok (NULL, "="));
if (debug_level)
warnx ("%s: %s", key, val);
if (val)
{
setenv (key, val, 1);
free (val);
}
free (s);
free (key);
free (line);
}
seed_addtime ();
}
if (line)
free (line);
fclose (fp);
}
/*
* redirect stdout & stderr to a log file. flag parameter decides which
* descriptors to redirect; It is an 'OR' of STDOUT_FILENO & STDERR_FILENO.
*/
void
redirect_to_log (const char *logfile, unsigned char flag)
{
assert (logfile != NULL);
int fd = 0, perm = S_IRUSR | S_IWUSR;
if ((fd = open (logfile, O_CREAT | O_WRONLY | O_APPEND, perm)) == -1)
err (-1, "could not open logfile `%s'", logfile);
if (flag & STDOUT_FILENO && dup2 (fd, STDOUT_FILENO) == -1)
err (-1, "could not duplicate stdout");
if (flag & STDERR_FILENO && dup2 (fd, STDERR_FILENO) == -1)
err (-1, "could not duplicate stderr");
}
/*
* wirets pid to a file under /var/run directory, which will be used by
* /sbin/service to shut down the dns daemon.
*/
void
write_pid (const char *pidfile)
{
int n = 0, fd = 0, perm = 0;
char *pid = strdup (pidfile);
perm = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
if ((fd = open (pid, O_CREAT | O_WRONLY | O_TRUNC, perm)) == -1)
err (-1, "could not open file: `%s'", pid);
memset (pid, '\0', strlen (pid));
n = sprintf (pid, "%d\n", getpid ());
write (fd, pid, n);
close (fd);
free (pid);
}
void
handle_term (int n)
{
warnx ("going down with signal: %d ---\n", n);
exit (0);
}
/* gettimezone: reads local timezone definition from '/etc/localtime'
* and returns a pointer to a POSIX TZ environment variable string or
* NULL in case of an error; See: tzfile(5), tzset(3).
*
* std offset dst [offset],start[/time],end[/time]
*
* Ex: TZ="NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0"
*/
char *
gettimezone (void)
{
#define TZ_VERSION '2'
#define TZ_MAGIC "TZif"
#define TZ_FILE "/etc/localtime"
int32_t fd = 0;
char *tz = NULL;
struct stat st;
char *tzbuf = NULL;
if ((fd = open (TZ_FILE, O_RDONLY | O_NDELAY)) < 0)
{
warn ("could not access timezone: %s", TZ_FILE);
return tz;
}
if (fstat (fd, &st) < 0)
err (-1, "could not get file status: %s", TZ_FILE);
tzbuf = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (tzbuf == MAP_FAILED)
err (-1, "could not mmap(2) file: %s", TZ_FILE);
if (memcmp (tzbuf, TZ_MAGIC, 4))
err (-1, "invalid timezone file: %s, see tzfile(5)", TZ_FILE);
if (TZ_VERSION == *(tzbuf + 4) || TZ_VERSION == *(tzbuf + 4) - 1)
{
char *p1 = NULL, *p2 = NULL;
p1 = p2 = tzbuf + st.st_size - 1;
while (*--p1 != '\n');
if (!(tz = calloc (abs (p2 - p1), sizeof (char))))
err (-1, "could not allocate memory for tz");
memcpy (tz, p1 + 1, abs (p2 - p1) - 1);
}
munmap (tzbuf, st.st_size);
close (fd);
return tz;
}
/*
* set_timezone: set `TZ' environment variable to appropriate time zone value.
* `TZ' environment variable is used by numerous - <time.h> - functions to
* perform local time conversions. TZ: NAME[+-]HH:MM:SS[DST]
* ex: IST-5:30:00, EST+5:00:00EDT etc.
*/
void
set_timezone (void)
{
char *tzone = getenv ("TZ");
if (tzone)
return;
tzone = gettimezone ();
if (!tzone)
{
time_t t;
struct tm *tt = NULL;
char hh = 0, mm = 0, ss = 0;
t = time (NULL);
tt = localtime (&t);
hh = timezone / (60 * 60);
mm = abs (timezone % (60 * 60) / 60);
ss = abs (timezone % (60 * 60) % 60);
if (!(tzone = calloc (22, sizeof (char))))
err (-1, "could not allocate memory for tzone");
snprintf (tzone, 22, "%s%+02d:%02d:%02d%s", tzname[0],
hh, mm, ss, (tt->tm_isdst > 0) ? tzname[1] : "");
}
setenv ("TZ", tzone, 1);
free (tzone);
}