-
Notifications
You must be signed in to change notification settings - Fork 7
/
rbldnsd.c
1636 lines (1438 loc) · 40.9 KB
/
rbldnsd.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* rbldnsd: main program
*/
#define _LARGEFILE64_SOURCE /* to define O_LARGEFILE if supported */
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h> /* for bool */
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <syslog.h>
#include <time.h>
#include <sys/time.h> /* some systems can't include time.h and sys/time.h */
#include <fcntl.h>
#include <sys/wait.h>
#include "ev.h"
#include "rbldnsd.h"
#include "sds/sds.h"
#ifndef NO_SELECT_H
# include <sys/select.h>
#endif
#ifndef NO_POLL
# include <poll.h>
#endif
#ifdef WITH_JEMALLOC
#include <jemalloc/jemalloc.h>
#else
#ifndef NO_MEMINFO
#if defined(__APPLE__)
#include <malloc/malloc.h>
#endif
#if defined(__linux__)
#include <malloc.h>
#endif
#if defined(__FreeBSD__)
#include <malloc_np.h>
#endif
#endif
#endif
#ifndef NO_TIMES
# include <sys/times.h>
#endif
#ifndef NO_STDINT_H
/* if system have stdint.h, assume it have inttypes.h too */
# include <inttypes.h>
#endif
#ifndef NO_STATS
# ifndef NO_IOVEC
# include <sys/uio.h>
# define STATS_IPC_IOVEC 1
# endif
#endif
#ifndef NO_DSO
# include <dlfcn.h>
#endif
#ifndef NI_MAXHOST
# define NI_MAXHOST 1025
#endif
#ifndef NI_MAXSERV
# define NI_MAXSERV 32
#endif
#ifndef O_LARGEFILE
# define O_LARGEFILE 0
#endif
const char *version = VERSION;
const char *show_version = "rbldnsd " VERSION;
/* version to show in version.bind CH TXT reply */
char *progname; /* limited to 32 chars */
int logto;
void error(int errnum, const char *fmt, ...) {
char buf[256];
int l, pl;
va_list ap;
l = pl = ssprintf(buf, sizeof(buf), "%.30s: ", progname);
va_start(ap, fmt);
l += vssprintf(buf + l, sizeof(buf) - l, fmt, ap);
if (errnum)
l += ssprintf(buf + l, sizeof(buf) - l, ": %.50s", strerror(errnum));
if (logto & LOGTO_SYSLOG) {
fmt = buf + pl;
syslog(LOG_ERR, strchr(fmt, '%') ? "%s" : fmt, fmt);
}
buf[l++] = '\n';
write(2, buf, l);
_exit(1);
}
static unsigned recheck = 60; /* interval between checks for reload */
static int initialized; /* 1 when initialized */
static char *logfile; /* log file name */
#ifndef NO_STATS
static char *statsfile; /* statistics file */
static int stats_relative; /* dump relative, not absolute, stats */
#endif
int accept_in_cidr; /* accept 127.0.0.1/8-"style" CIDRs */
int nouncompress; /* disable on-the-fly decompression */
unsigned def_ttl = 35*60; /* default record TTL 35m */
unsigned min_ttl, max_ttl; /* TTL constraints */
const char def_rr[5] = "\177\0\0\2\0"; /* default A RR */
#define MAXSOCK 20 /* maximum # of supported sockets */
static int sock[MAXSOCK]; /* array of active sockets */
static int update_sock = -1; /* used for dynamic updates */
static int numsock; /* number of active sockets in sock[] */
static FILE *flog; /* log file */
static int flushlog; /* flush log after each line */
static struct zone *zonelist; /* list of zones we're authoritative for */
static int numzones; /* number of zones in zonelist */
int lazy; /* don't return AUTH section by default */
static int fork_on_reload; /* >0 - perform fork on reloads, <0 - this is a child of reloading parent */
static int can_reload; /* block reload when another reload is there */
static int pending_reload = 0;
static ev_signal ev_hup, ev_usr1, ev_usr2, ev_term, ev_int;
#if STATS_IPC_IOVEC
static struct iovec *stats_iov;
#endif
#ifndef NO_DSO
hook_reload_check_t hook_reload_check;
hook_reload_t hook_reload;
hook_query_access_t hook_query_access;
hook_query_result_t hook_query_result;
#endif
/* a list of zonetypes. */
const struct dstype *ds_types[] = {
dstype(ip4set),
dstype(ip4tset),
dstype(ip4trie),
dstype(ip6tset),
dstype(ip6trie),
dstype(dnset),
dstype(dnhash),
dstype(dnhash_fixed),
dstype(combined),
dstype(generic),
dstype(acl),
dstype(aclkey),
NULL
};
static int do_reload(int do_fork, struct ev_loop *loop);
static int satoi(const char *s) {
int n = 0;
if (*s < '0' || *s > '9') return -1;
do n = n * 10 + (*s++ - '0');
while (*s >= '0' && *s <= '9');
return *s ? -1 : n;
}
static void NORETURN usage(int exitcode) {
const struct dstype **dstp;
printf(
"%s: rbl dns daemon version %s\n"
"Usage is: %s options zonespec...\n"
"where options are:\n"
" -u user[:group] - run as this user:group (rbldns)\n"
" -r rootdir - chroot to this directory\n"
" -w workdir - working directory with zone files\n"
" -b address[/port] - bind to (listen on) this address (required)\n"
#ifndef NO_IPv6
" -4 - use IPv4 socket type\n"
" -6 - use IPv6 socket type\n"
#endif
" -t ttl - default TTL value to set in answers (35m)\n"
" -v - hide version information in replies to version.bind CH TXT\n"
" (second -v makes rbldnsd to refuse such requests completely)\n"
" -e - enable CIDR ranges where prefix is not on the range boundary\n"
" (by default ranges such 127.0.0.1/8 will be rejected)\n"
" -c check - time interval to check for data file updates (1m)\n"
" -p pidfile - write pid to specified file\n"
" -n - do not become a daemon\n"
" -f - fork a child process while reloading zones, to process requests\n"
" during reload (may double memory requiriments)\n"
" -q - quickstart, load zones after backgrounding\n"
" -l [+]logfile - log queries and answers to this file (+ for unbuffered)\n"
" -U address/port or unix socket - socket credentials for updates\n"
#ifndef NO_STATS
" -s [+]statsfile - write a line with short statistics summary into this\n"
" file every `check' (-c) secounds, for rrdtool-like applications\n"
" (+ to log relative, not absolute, statistics counters)\n"
#endif
" -a - omit AUTH section from regular replies, do not return list of\n"
" nameservers, but only return NS info when explicitly asked.\n"
" This is an equivalent of bind9 \"minimal-answers\" setting.\n"
" In future versions this mode will be the default.\n"
" -A - put AUTH section in every reply.\n"
" -F facility - Log facility for syslog. Default is 'daemon'.\n"
#ifndef NO_ZLIB
" -C - disable on-the-fly decompression of dataset files\n"
#endif
#ifndef NO_DZO
" -x extension - load given extension module (.so file)\n"
" -X extarg - pass extarg to extension init routine\n"
#endif
" -d - dump all zones in BIND format to standard output and exit\n"
"each zone specified using `name:type:file,file...'\n"
"syntax, repeated names constitute the same zone.\n"
"Available dataset types:\n"
, progname, version, progname);
for(dstp = ds_types; *dstp; ++dstp)
printf(" %s - %s\n", (*dstp)->dst_name, (*dstp)->dst_descr);
exit(exitcode);
}
static inline int sockaddr_in_equal(const struct sockaddr_in *addr1,
const struct sockaddr_in *addr2)
{
return (addr1->sin_port == addr2->sin_port
&& addr1->sin_addr.s_addr == addr2->sin_addr.s_addr);
}
#ifndef NO_IPv6
static inline int sockaddr_in6_equal(const struct sockaddr_in6 *addr1,
const struct sockaddr_in6 *addr2)
{
if (memcmp(addr1->sin6_addr.s6_addr, addr2->sin6_addr.s6_addr, 16) != 0)
return 0;
return (addr1->sin6_port == addr2->sin6_port
&& addr1->sin6_flowinfo == addr2->sin6_flowinfo
&& addr1->sin6_scope_id == addr2->sin6_scope_id);
}
#endif
static inline int sockaddr_equal(const struct sockaddr *addr1,
const struct sockaddr *addr2)
{
if (addr1->sa_family != addr2->sa_family)
return 0;
switch (addr1->sa_family) {
case AF_INET:
return sockaddr_in_equal((const struct sockaddr_in *)addr1,
(const struct sockaddr_in *)addr2);
#ifndef NO_IPv6
case AF_INET6:
return sockaddr_in6_equal((const struct sockaddr_in6 *)addr1,
(const struct sockaddr_in6 *)addr2);
#endif
default:
error(0, "unknown address family (%d)", addr1->sa_family);
}
}
/* already_bound(addr, addrlen)
*
* Determine whether we've already bound to a particular address.
* This is here mostly to deal with the fact that on certain systems,
* gethostbyname()/getaddrinfo() can return a duplicate 127.0.0.1
* for 'localhost'. See
* - http://sourceware.org/bugzilla/show_bug.cgi?id=4980
* - https://bugzilla.redhat.com/show_bug.cgi?id=496300
*/
static int already_bound(const struct sockaddr *addr, socklen_t addrlen) {
#ifdef NO_IPv6
struct sockaddr_in addr_buf;
#else
struct sockaddr_in6 addr_buf;
#endif
struct sockaddr *boundaddr = (struct sockaddr *)&addr_buf;
socklen_t buflen;
int i;
for (i = 0; i < numsock; i++) {
buflen = sizeof(addr_buf);
if (getsockname(sock[i], boundaddr, &buflen) < 0)
error(errno, "getsockname failed");
if (buflen == addrlen && sockaddr_equal(boundaddr, addr))
return 1;
}
return 0;
}
static int newsocket(struct addrinfo *ai) {
int fd;
char host[NI_MAXHOST], serv[NI_MAXSERV];
if (already_bound(ai->ai_addr, ai->ai_addrlen))
return -1;
if (numsock >= MAXSOCK)
error(0, "too many listening sockets (%d max)", MAXSOCK);
fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (fd < 0) {
if (errno == EAFNOSUPPORT) return 0;
error(errno, "unable to create socket");
}
getnameinfo(ai->ai_addr, ai->ai_addrlen,
host, sizeof(host), serv, sizeof(serv),
NI_NUMERICHOST|NI_NUMERICSERV);
if (bind(fd, ai->ai_addr, ai->ai_addrlen) < 0)
error(errno, "unable to bind to %s/%s", host, serv);
dslog(LOG_INFO, 0, "listening on %s/%s", host, serv);
return fd;
}
static int newsocket_unix(const char *path)
{
int nelts, i, fd;
sds *elts = sdssplitlen(path, strlen(path), ":", 1, &nelts);
struct sockaddr_un un;
unsigned mode = 00644;
size_t pwlen;
char *pwbuf, *p;
struct passwd pw, *ppw;
struct group gr, *pgr;
uid_t owner = (uid_t)-1;
gid_t group = (gid_t)-1;
int has_group = false;
if (nelts < 1) {
error(0, "invalid path: %s", path);
}
un.sun_family = AF_UNIX;
estrlcpy(un.sun_path, elts[0], sizeof (un.sun_path));
#if defined(FREEBSD) || defined(__APPLE__)
un.sun_len = SUN_LEN (&un);
#endif
#ifdef _SC_GETPW_R_SIZE_MAX
pwlen = sysconf (_SC_GETPW_R_SIZE_MAX);
if (pwlen <= 0) {
pwlen = 8192;
}
#else
pwlen = 8192;
#endif
pwbuf = emalloc(pwlen);
for (i = 1; i < nelts; i ++) {
if (strncmp(elts[i], "mode=", sizeof ("mode=") - 1) == 0) {
p = strchr(elts[i], '=');
/* XXX: add error check */
mode = strtoul (p + 1, NULL, 8);
if (mode == 0) {
error(0, "bad mode: %s", p + 1);
}
}
else if (strncmp(elts[i], "owner=", sizeof ("owner=") - 1) == 0) {
p = strchr(elts[i], '=');
if (getpwnam_r(p + 1, &pw, pwbuf, pwlen, &ppw) != 0 || ppw == NULL) {
error(errno, "bad user: %s", p + 1);
}
owner = pw.pw_uid;
if (!has_group) {
group = pw.pw_gid;
}
}
else if (strncmp(elts[i], "group=", sizeof ("group=") - 1) == 0) {
p = strchr(elts[i], '=');
if (getgrnam_r(p + 1, &gr, pwbuf, pwlen, &pgr) != 0 || pgr == NULL) {
error(errno, "bad group: %s", p + 1);
}
has_group = true;
group = gr.gr_gid;
}
}
sdsfreesplitres(elts, nelts);
free(pwbuf);
fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (fd == -1) {
error(errno, "unable to create socket");
}
if (bind(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
if (errno == EADDRINUSE) {
/* Try to unlink */
dslog(LOG_INFO, 0, "unlinking stale socket %s", un.sun_path);
unlink(un.sun_path);
if (bind(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
error(errno, "unable to bind to unix socket: %s", path);
}
}
else {
error(errno, "unable to bind to unix socket: %s", path);
}
}
if (owner != (uid_t)-1 || group != (gid_t)-1) {
if (chown(un.sun_path, owner, group) == -1) {
error(errno, "cannot change owner for %s to %d:%d: %s",
un.sun_path, (int)owner, (int)group,
strerror(errno));
}
}
if (chmod(un.sun_path, mode) == -1) {
error(errno, "cannot change mode for %s to %od %s",
un.sun_path, mode, strerror(errno));
}
dslog(LOG_INFO, 0, "listening on %s", un.sun_path);
return fd;
}
static int
initsockets(const char *bindaddr[MAXSOCK], int *dest, int nba, int UNUSED family) {
int i, x = 0;
char *host, *serv;
const char *ba;
struct addrinfo hints, *aires, *ai;
memset(&hints, 0, sizeof(hints));
hints.ai_family = family;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
for (i = 0; i < nba; ++i) {
ba = bindaddr[i];
if (ba[0] == '/' || ba[0] == '.') {
int nfd = newsocket_unix(ba);
if (nfd != -1) {
dest[numsock++] = nfd;
}
}
else {
host = estrdup(ba);
serv = strchr(host, '/');
if (serv) {
*serv++ = '\0';
if (!*host)
error(0, "missing host part in bind address `%.60s'", ba);
}
if (!serv || !*serv)
serv = "domain";
x = getaddrinfo(host, serv, &hints, &aires);
if (x != 0)
error(0, "%.60s: %s", ba, gai_strerror(x));
for (ai = aires, x = 0; ai; ai = ai->ai_next) {
int nfd = newsocket(ai);
if (nfd != -1) {
++x;
dest[numsock++] = nfd;
}
}
if (!x)
error(0, "%.60s: no available protocols", ba);
freeaddrinfo(aires);
free(host);
}
}
endservent();
endhostent();
for (i = 0; i < numsock; ++i) {
x = 65536;
do {
if (setsockopt(dest[i], SOL_SOCKET, SO_RCVBUF, (void *) &x, sizeof x) == 0) {
break;
}
} while ((x -= (x >> 5)) >= 1024);
}
return numsock;
}
static struct {
int facility;
const char *name;
} facility_names[] = {
{ LOG_AUTH, "auth" },
{ LOG_AUTHPRIV, "authpriv" },
{ LOG_CRON, "cron" },
{ LOG_DAEMON, "daemon" },
{ LOG_FTP, "ftp" },
{ LOG_KERN, "kern" },
{ LOG_LOCAL0, "local0" },
{ LOG_LOCAL1, "local1" },
{ LOG_LOCAL2, "local2" },
{ LOG_LOCAL3, "local3" },
{ LOG_LOCAL4, "local4" },
{ LOG_LOCAL5, "local5" },
{ LOG_LOCAL6, "local6" },
{ LOG_LOCAL7, "local7" },
{ LOG_LPR, "lpr" },
{ LOG_MAIL, "mail" },
{ LOG_NEWS, "news" },
{ LOG_SYSLOG, "syslog" },
{ LOG_USER, "user" },
{ LOG_UUCP, "uucp" },
};
static int logfacility_lookup(const char *facility, int *logfacility) {
unsigned int t;
if ( logfacility == NULL ) {
return 0;
}
for ( t=0; t < sizeof(facility_names) / sizeof(facility_names[0]); t++ ) {
if ( !strncmp(facility_names[t].name, facility, strlen(facility_names[t].name)+1) ) {
*logfacility = facility_names[t].facility;
return 1;
}
}
*logfacility = LOG_DAEMON;
return 0;
}
static void init(int argc, char **argv, struct ev_loop *loop) {
int c;
char *p;
const char *user = NULL;
const char *rootdir = NULL, *workdir = NULL, *pidfile = NULL, *facility = NULL;
const char *bindaddr[MAXSOCK];
const char *update_addr = NULL;
int logfacility;
int nba = 0;
uid_t uid = 0;
gid_t gid = 0;
int nodaemon = 0, quickstart = 0, dump = 0, nover = 0, forkon = 0, dry_run = 0;
#ifdef NO_IPv6
int family = AF_INET;
#else
int family = AF_UNSPEC;
#endif
int cfd = -1;
const struct zone *z;
#ifndef NO_DSO
char *ext = NULL, *extarg = NULL;
int (*extinit)(const char *arg, struct zone *zonelist) = NULL;
#endif
if ((progname = strrchr(argv[0], '/')) != NULL)
argv[0] = ++progname;
else
progname = argv[0];
if (argc <= 1) usage(1);
while((c = getopt(argc, argv, "u:r:b:w:t:c:p:nel:qs:h46dvaAfF:Cx:X:DU:")) != EOF)
switch(c) {
case 'u': user = optarg; break;
case 'r': rootdir = optarg; break;
case 'b':
if (nba >= MAXSOCK)
error(0, "too many addresses to listen on (%d max)", MAXSOCK);
bindaddr[nba++] = optarg;
break;
case 'U':
update_addr = optarg;
break;
#ifndef NO_IPv6
case '4': family = AF_INET; break;
case '6': family = AF_INET6; break;
#else
case '4': break;
case '6': error(0, "IPv6 support isn't compiled in");
#endif
case 'w': workdir = optarg; break;
case 'p': pidfile = optarg; break;
case 't':
p = optarg;
if (*p == ':') ++p;
else {
if (!(p = parse_time(p, &def_ttl)) || !def_ttl ||
(*p && *p++ != ':'))
error(0, "invalid ttl (-t) value `%.50s'", optarg);
}
if (*p == ':') ++p;
else if (*p) {
if (!(p = parse_time(p, &min_ttl)) || (*p && *p++ != ':'))
error(0, "invalid minttl (-t) value `%.50s'", optarg);
}
if (*p == ':') ++p;
else if (*p) {
if (!(p = parse_time(p, &max_ttl)) || (*p && *p++ != ':'))
error(0, "invalid maxttl (-t) value `%.50s'", optarg);
}
if (*p)
error(0, "invalid value for -t (ttl) option: `%.50s'", optarg);
if ((min_ttl && max_ttl && min_ttl > max_ttl) ||
(min_ttl && def_ttl < min_ttl) ||
(max_ttl && def_ttl > max_ttl))
error(0, "inconsistent def:min:max ttl: %u:%u:%u",
def_ttl, min_ttl, max_ttl);
break;
case 'c':
if (!(p = parse_time(optarg, &recheck)) || *p)
error(0, "invalid check interval (-c) value `%.50s'", optarg);
break;
case 'n': nodaemon = 1; break;
case 'e': accept_in_cidr = 1; break;
case 'l':
logfile = optarg;
if (*logfile != '+') {
flushlog = 0;
}
else {
++logfile;
flushlog = 1;
}
if (!*logfile) {
logfile = NULL;
flushlog = 0;
}
else if (logfile[0] == '-' && logfile[1] == '\0') {
/* No need to reopen stdout */
logfile = NULL;
flog = stdout;
}
break;
break;
case 's':
#ifdef NO_STATS
fprintf(stderr,
"%s: warning: no statistics counters support is compiled in\n",
progname);
#else
statsfile = optarg;
if (*statsfile != '+') stats_relative = 0;
else ++statsfile, stats_relative = 1;
if (!*statsfile) statsfile = NULL;
#endif
break;
case 'q': quickstart = 1; break;
case 'd':
dump = 1;
break;
case 'D': dry_run = 1; break;
case 'v': show_version = nover++ ? NULL : "rbldnsd"; break;
case 'a': lazy = 1; break;
case 'A': lazy = 0; break;
case 'f': forkon = 1; break;
case 'F': facility = optarg; break;
case 'C': nouncompress = 1; break;
#ifndef NO_DSO
case 'x': ext = optarg; break;
case 'X': extarg = optarg; break;
#else
case 'x':
case 'X':
error(0, "extension support is not compiled in");
#endif
case 'h': usage(0);
default: error(0, "type `%.50s -h' for help", progname);
}
/* options switch end */
if (!(argc -= optind))
error(0, "no zone(s) to service specified (-h for help)");
argv += optind;
if (dump || dry_run) {
time_t now;
logto = LOGTO_STDERR;
for(c = 0; c < argc; ++c)
zonelist = addzone(zonelist, argv[c]);
init_zones_caches(zonelist);
if (rootdir && (chdir(rootdir) < 0 || chroot(rootdir) < 0))
error(errno, "unable to chroot to %.50s", rootdir);
if (workdir && chdir(workdir) < 0)
error(errno, "unable to chdir to %.50s", workdir);
if (!do_reload(0, loop))
error(0, "zone loading errors, aborting");
if (dump) {
now = time(NULL);
printf("; zone dump made %s", ctime(&now));
printf("; rbldnsd version %s\n", version);
for (z = zonelist; z; z = z->z_next)
dumpzone(z, stdout);
fflush(stdout);
exit(ferror(stdout) ? 1 : 0);
}
else {
/* Dry run */
printf("zones loaded successfully\n");
fflush(stdout);
exit(ferror(stdout) ? 1 : 0);
}
}
if (!nba)
error(0, "no address to listen on (-b option) specified");
if ( facility == NULL ) {
logfacility = LOG_DAEMON;
}
else {
if ( logfacility_lookup(facility, &logfacility) == 0 ) {
error(0, "log facility %s is not valid", facility);
}
}
tzset();
if (nodaemon)
logto = LOGTO_STDOUT|LOGTO_STDERR;
else
{
/* fork early so that logging will be from right pid */
int pfd[2];
if (pipe(pfd) < 0) error(errno, "pipe() failed");
c = fork();
if (c < 0) error(errno, "fork() failed");
if (c > 0) {
close(pfd[1]);
if (read(pfd[0], &c, 1) < 1) exit(1);
else exit(0);
}
/* Forked process */
ev_loop_fork(loop);
cfd = pfd[1];
close(pfd[0]);
openlog(progname, LOG_PID|LOG_NDELAY, logfacility);
logto = LOGTO_STDERR|LOGTO_SYSLOG;
if (!quickstart && !flog) logto |= LOGTO_STDOUT;
}
initsockets(bindaddr, sock, nba, family);
if (update_addr) {
if (initsockets(&update_addr, &update_sock, 1, family) != 1) {
error(0, "unable to listen for updates on `%s'", update_addr);
}
}
#ifndef NO_DSO
if (ext) {
void *handle = dlopen(ext, RTLD_NOW);
if (!handle)
error(0, "unable to load extension `%s': %s", ext, dlerror());
extinit = dlsym(handle, "rbldnsd_extension_init");
if (!extinit)
error(0, "unable to find extension init routine in `%s'", ext);
}
#endif
if (!user && !(uid = getuid()))
user = "rbldns";
if (!user)
p = NULL;
else {
if ((p = strchr(user, ':')) != NULL)
*p++ = '\0';
if ((c = satoi(user)) >= 0)
uid = c, gid = c;
else {
struct passwd *pw = getpwnam(user);
if (!pw)
error(0, "unknown user `%s'", user);
uid = pw->pw_uid;
gid = pw->pw_gid;
endpwent();
}
}
if (!uid)
error(0, "daemon should not run as root, specify -u option");
if (p) {
if ((c = satoi(p)) >= 0)
gid = c;
else {
struct group *gr = getgrnam(p);
if (!gr)
error(0, "unknown group `%s'", p);
gid = gr->gr_gid;
endgrent();
}
p[-1] = ':';
}
if (pidfile) {
int fdpid;
char buf[40];
c = sprintf(buf, "%ld\n", (long)getpid());
fdpid = open(pidfile, O_CREAT|O_WRONLY|O_TRUNC, 0644);
if (fdpid < 0 || write(fdpid, buf, c) < c)
error(errno, "unable to write pidfile");
close(fdpid);
}
if (rootdir && (chdir(rootdir) < 0 || chroot(rootdir) < 0))
error(errno, "unable to chroot to %.50s", rootdir);
if (workdir && chdir(workdir) < 0)
error(errno, "unable to chdir to %.50s", workdir);
if (user)
if (setgroups(1, &gid) < 0 || setgid(gid) < 0 || setuid(uid) < 0)
error(errno, "unable to setuid(%d:%d)", (int)uid, (int)gid);
for(c = 0; c < argc; ++c)
zonelist = addzone(zonelist, argv[c]);
init_zones_caches(zonelist);
#ifndef NO_DSO
if (extinit && extinit(extarg, zonelist) != 0)
error(0, "unable to iniitialize extension `%s'", ext);
#endif
if (!quickstart && !do_reload(0, loop))
error(0, "zone loading errors, aborting");
/* count number of zones */
for(c = 0, z = zonelist; z; z = z->z_next)
++c;
numzones = c;
#if STATS_IPC_IOVEC
stats_iov = (struct iovec *)emalloc(numzones * sizeof(struct iovec));
for(c = 0, z = zonelist; z; z = z->z_next, ++c) {
stats_iov[c].iov_base = (char*)&z->z_stats;
stats_iov[c].iov_len = sizeof(z->z_stats);
}
#endif
dslog(LOG_INFO, 0, "rbldnsd version %s started (%d socket(s), %d zone(s))",
version, numsock, numzones);
initialized = 1;
if (cfd >= 0) {
write(cfd, "", 1);
close(cfd);
close(0); close(2);
if (!flog) close(1);
setsid();
logto = LOGTO_SYSLOG;
}
if (quickstart)
do_reload(0, loop);
/* only set "main" fork_on_reload after first reload */
fork_on_reload = forkon;
}
#ifndef NO_STATS
struct dnsstats gstats;
static struct dnsstats gptot;
static time_t stats_time;
static void dumpstats(void) {
struct dnsstats tot;
char name[DNS_MAXDOMAIN+1];
FILE *f;
struct zone *z;
f = fopen(statsfile, "a");
if (f)
fprintf(f, "%ld", (long)time(NULL));
#define C ":%" PRI_DNSCNT
tot = gstats;
for(z = zonelist; z; z = z->z_next) {
#define add(x) tot.x += z->z_stats.x
add(b_in); add(b_out);
add(q_ok); add(q_nxd); add(q_err);
#undef add
if (f) {
dns_dntop(z->z_dn, name, sizeof(name));
#define delta(x) z->z_stats.x - z->z_pstats.x
fprintf(f, " %s" C C C C C,
name,
delta(q_ok) + delta(q_nxd) + delta(q_err),
delta(q_ok), delta(q_nxd),
delta(b_in), delta(b_out));
#undef delta
}
if (stats_relative)
z->z_pstats = z->z_stats;
}
if (f) {
#define delta(x) tot.x - gptot.x
fprintf(f, " *" C C C C C "\n",
delta(q_ok) + delta(q_nxd) + delta(q_err),
delta(q_ok), delta(q_nxd),
delta(b_in), delta(b_out));
#undef delta
fclose(f);
}
if (stats_relative)
gptot = tot;
#undef C
}
static void dumpstats_z(void) {
FILE *f = fopen(statsfile, "a");
if (f) {
fprintf(f, "%ld\n", (long)time(NULL));
fclose(f);
}
}
static void logstats(int reset) {
time_t t = time(NULL);
time_t d = t - stats_time;
struct dnsstats tot = gstats;
char name[DNS_MAXDOMAIN+1];
struct zone *z;
#define C(x) " " #x "=%" PRI_DNSCNT
for(z = zonelist; z; z = z->z_next) {
#define add(x) tot.x += z->z_stats.x
add(b_in); add(b_out);
add(q_ok); add(q_nxd); add(q_err);
#undef add
dns_dntop(z->z_dn, name, sizeof(name));
dslog(LOG_INFO, 0,
"stats for %ldsecs zone %.60s:" C(tot) C(ok) C(nxd) C(err) C(in) C(out),
(long)d, name,
z->z_stats.q_ok + z->z_stats.q_nxd + z->z_stats.q_err,
z->z_stats.q_ok, z->z_stats.q_nxd, z->z_stats.q_err,
z->z_stats.b_in, z->z_stats.b_out);
}
dslog(LOG_INFO, 0,
"stats for %ldsec:" C(tot) C(ok) C(nxd) C(err) C(in) C(out),
(long)d,
tot.q_ok + tot.q_nxd + tot.q_err,
tot.q_ok, tot.q_nxd, tot.q_err,
tot.b_in, tot.b_out);
#undef C
if (reset) {
for(z = zonelist; z; z = z->z_next) {
memset(&z->z_stats, 0, sizeof(z->z_stats));
memset(&z->z_pstats, 0, sizeof(z->z_pstats));
}
memset(&gstats, 0, sizeof(gstats));
memset(&gptot, 0, sizeof(gptot));
stats_time = t;
}
}
#if STATS_IPC_IOVEC
# define ipc_read_stats(fd) readv(fd, stats_iov, numzones)
# define ipc_write_stats(fd) writev(fd, stats_iov, numzones)
#else
static void ipc_read_stats(int fd) {
struct zone *z;
for(z = zonelist; z; z = z->z_next)
if (read(fd, &z->z_stats, sizeof(z->z_stats)) <= 0)
break;
}
static void ipc_write_stats(int fd) {
const struct zone *z;
for(z = zonelist; z; z = z->z_next)
if (write(fd, &z->z_stats, sizeof(z->z_stats)) <= 0)
break;
}
#endif
#else
# define ipc_read_stats(fd)
# define ipc_write_stats(fd)
#endif
static void reopenlog(void) {
if (logfile) {
int fd;
if (flog) fclose(flog);
fd = open(logfile, O_WRONLY|O_APPEND|O_CREAT|O_NONBLOCK|O_LARGEFILE, 0644);
if (fd < 0 || (flog = fdopen(fd, "a")) == NULL) {
dslog(LOG_WARNING, 0, "error (re)opening logfile `%.50s': %s",