1
/* dnsmasq is Copyright (c) 2000 - 2005 Simon Kelley
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 dated June, 1991.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
13
/* Author's email: simon@thekelleys.org.uk */
24
#define OPTSTRING "yZDNLERKzowefnbvhdkqr:m:p:c:l:s:i:t:u:g:a:x:S:C:A:T:H:Q:I:B:F:G:O:M:X:V:U:j:P:J:W:Y:"
26
static struct myoption opts[] = {
27
{"version", 0, 0, 'v'},
28
{"no-hosts", 0, 0, 'h'},
29
{"no-poll", 0, 0, 'n'},
31
{"no-daemon", 0, 0, 'd'},
32
{"log-queries", 0, 0, 'q'},
35
{"resolv-file", 1, 0, 'r'},
36
{"mx-host", 1, 0, 'm'},
37
{"mx-target", 1, 0, 't'},
38
{"cache-size", 1, 0, 'c'},
40
{"dhcp-leasefile", 1, 0, 'l'},
41
{"dhcp-lease", 1, 0, 'l' },
42
{"dhcp-host", 1, 0, 'G'},
43
{"dhcp-range", 1, 0, 'F'},
44
{"dhcp-option", 1, 0, 'O'},
45
{"dhcp-boot", 1, 0, 'M'},
46
{"domain", 1, 0, 's'},
47
{"domain-suffix", 1, 0, 's'},
48
{"interface", 1, 0, 'i'},
49
{"listen-address", 1, 0, 'a'},
50
{"bogus-priv", 0, 0, 'b'},
51
{"bogus-nxdomain", 1, 0, 'B'},
52
{"selfmx", 0, 0, 'e'},
53
{"filterwin2k", 0, 0, 'f'},
54
{"pid-file", 1, 0, 'x'},
55
{"strict-order", 0, 0, 'o'},
56
{"server", 1, 0, 'S'},
57
{"local", 1, 0, 'S' },
58
{"address", 1, 0, 'A' },
59
{"conf-file", 1, 0, 'C'},
60
{"no-resolv", 0, 0, 'R'},
61
{"expand-hosts", 0, 0, 'E'},
62
{"localmx", 0, 0, 'L'},
63
{"local-ttl", 1, 0, 'T'},
64
{"no-negcache", 0, 0, 'N'},
65
{"addn-hosts", 1, 0, 'H'},
66
{"query-port", 1, 0, 'Q'},
67
{"except-interface", 1, 0, 'I'},
68
{"domain-needed", 0, 0, 'D'},
69
{"dhcp-lease-max", 1, 0, 'X' },
70
{"bind-interfaces", 0, 0, 'z'},
71
{"read-ethers", 0, 0, 'Z' },
72
{"alias", 1, 0, 'V' },
73
{"dhcp-vendorclass", 1, 0, 'U'},
74
{"dhcp-userclass", 1, 0, 'j'},
75
{"dhcp-ignore", 1, 0, 'J'},
76
{"edns-packet-max", 1, 0, 'P'},
77
{"keep-in-foreground", 0, 0, 'k'},
78
{"dhcp-authoritative", 0, 0, 'K'},
79
{"srv-host", 1, 0, 'W'},
80
{"localise-queries", 0, 0, 'y'},
81
{"txt-record", 1, 0, 'Y'},
90
static struct optflags optmap[] = {
91
{ 'b', OPT_BOGUSPRIV },
95
{ 'h', OPT_NO_HOSTS },
99
{ 'K', OPT_AUTHORITATIVE },
101
{ 'R', OPT_NO_RESOLV },
103
{ 'L', OPT_LOCALMX },
105
{ 'D', OPT_NODOTS_LOCAL },
108
{ 'y', OPT_LOCALISE },
115
"Usage: dnsmasq [options]\n\n"
116
#ifndef HAVE_GETOPT_LONG
117
"Use short options only on the command line.\n"
119
"Valid options are :\n"
120
"-a, --listen-address=ipaddr Specify local address(es) to listen on.\n"
121
"-A, --address=/domain/ipaddr Return ipaddr for all hosts in specified domains.\n"
122
"-b, --bogus-priv Fake reverse lookups for RFC1918 private address ranges.\n"
123
"-B, --bogus-nxdomain=ipaddr Treat ipaddr as NXDOMAIN (defeats Verisign wildcard).\n"
124
"-c, --cache-size=cachesize Specify the size of the cache in entries (defaults to %d).\n"
125
"-C, --conf-file=path Specify configuration file (defaults to " CONFFILE ").\n"
126
"-d, --no-daemon Do NOT fork into the background: run in debug mode.\n"
127
"-D, --domain-needed Do NOT forward queries with no domain part.\n"
128
"-e, --selfmx Return self-pointing MX records for local hosts.\n"
129
"-E, --expand-hosts Expand simple names in /etc/hosts with domain-suffix.\n"
130
"-f, --filterwin2k Don't forward spurious DNS requests from Windows hosts.\n"
131
"-F, --dhcp-range=ipaddr,ipaddr,time Enable DHCP in the range given with lease duration.\n"
132
"-g, --group=groupname Change to this group after startup (defaults to " CHGRP ").\n"
133
"-G, --dhcp-host=<hostspec> Set address or hostname for a specified machine.\n"
134
"-h, --no-hosts Do NOT load " HOSTSFILE " file.\n"
135
"-H, --addn-hosts=path Specify a hosts file to be read in addition to " HOSTSFILE ".\n"
136
"-i, --interface=interface Specify interface(s) to listen on.\n"
137
"-I, --except-interface=int Specify interface(s) NOT to listen on.\n"
138
"-j, --dhcp-userclass=<id>,<class> Map DHCP user class to option set.\n"
139
"-J, --dhcp-ignore=<id> Don't do DHCP for hosts in option set.\n"
140
"-k, --keep-in-foreground Do NOT fork into the background, do NOT run in debug mode.\n"
141
"-K, --dhcp-authoritative Assume we are the only DHCP server on the local network.\n"
142
"-l, --dhcp-leasefile=path Specify where to store DHCP leases (defaults to " LEASEFILE ").\n"
143
"-L, --localmx Return MX records for local hosts.\n"
144
"-m, --mx-host=host_name,target,pref Specify an MX record.\n"
145
"-M, --dhcp-boot=<bootp opts> Specify BOOTP options to DHCP server.\n"
146
"-n, --no-poll Do NOT poll " RESOLVFILE " file, reload only on SIGHUP.\n"
147
"-N, --no-negcache Do NOT cache failed search results.\n"
148
"-o, --strict-order Use nameservers strictly in the order given in " RESOLVFILE ".\n"
149
"-O, --dhcp-option=<optspec> Set extra options to be set to DHCP clients.\n"
150
"-p, --port=number Specify port to listen for DNS requests on (defaults to 53).\n"
151
"-P, --edns-packet-max=<size> Maximum supported UDP packet size for EDNS.0 (defaults to %d).\n"
152
"-q, --log-queries Log queries.\n"
153
"-Q, --query-port=number Force the originating port for upstream queries.\n"
154
"-R, --no-resolv Do NOT read resolv.conf.\n"
155
"-r, --resolv-file=path Specify path to resolv.conf (defaults to " RESOLVFILE ").\n"
156
"-S, --server=/domain/ipaddr Specify address(es) of upstream servers with optional domains.\n"
157
" --local=/domain/ Never forward queries to specified domains.\n"
158
"-s, --domain=domain Specify the domain to be assigned in DHCP leases.\n"
159
"-t, --mx-target=host_name Specify default target in an MX record.\n"
160
"-T, --local-ttl=time Specify time-to-live in seconds for replies from /etc/hosts.\n"
161
"-u, --user=username Change to this user after startup. (defaults to " CHUSER ").\n"
162
"-U, --dhcp-vendorclass=<id>,<class> Map DHCP vendor class to option set.\n"
163
"-v, --version Display dnsmasq version and copyright information.\n"
164
"-V, --alias=addr,addr,mask Translate IPv4 addresses from upstream servers.\n"
165
"-W, --srv-host=name,target,... Specify a SRV record.\n"
166
"-w, --help Display this message.\n"
167
"-x, --pid-file=path Specify path of PID file. (defaults to " RUNFILE ").\n"
168
"-X, --dhcp-lease-max=number Specify maximum number of DHCP leases (defaults to %d).\n"
169
"-y, --localise-queries Answer DNS queries based on the interface a query was sent to.\n"
170
"-Y --txt-record=name,txt.... Specify TXT DNS record.\n"
171
"-z, --bind-interfaces Bind only to interfaces in use.\n"
172
"-Z, --read-ethers Read DHCP static host information from " ETHERSFILE ".\n"
175
static void add_txt(struct daemon *daemon, char *name, char *txt)
177
size_t len = strlen(txt);
178
struct txt_record *r = safe_malloc(sizeof(struct txt_record));
180
r->name = safe_string_alloc(name);
181
r->next = daemon->txt;
184
r->txt = safe_malloc(len+1);
187
memcpy((r->txt)+1, txt, len);
190
/* filenames are OK with unquoted commas, restore them here. */
191
static char *safe_filename_alloc(char *filename)
193
char *p, *ret = safe_string_alloc(filename);
196
for (p = ret; *p; p++)
203
struct daemon *read_opts (int argc, char **argv)
205
struct daemon *daemon = safe_malloc(sizeof(struct daemon));
206
char *problem = NULL, *buff = safe_malloc(MAXDNAME);
208
FILE *file_save = NULL, *f = NULL;
209
char *p, *arg, *comma, *file_name_save = NULL, *conffile = CONFFILE;
210
int hosts_index = 1, conffile_set = 0;
211
int line_save = 0, lineno = 0;
214
memset(daemon, 0, sizeof(struct daemon));
215
daemon->namebuff = buff;
217
/* Set defaults - everything else is zero or NULL */
218
daemon->min_leasetime = UINT_MAX;
219
daemon->cachesize = CACHESIZ;
220
daemon->port = NAMESERVER_PORT;
221
daemon->default_resolv.is_default = 1;
222
daemon->default_resolv.name = RESOLVFILE;
223
daemon->resolv_files = &daemon->default_resolv;
224
daemon->username = CHUSER;
225
daemon->groupname = CHGRP;
226
daemon->runfile = RUNFILE;
227
daemon->dhcp_max = MAXLEASES;
228
daemon->edns_pktsz = EDNS_PKTSZ;
229
add_txt(daemon, "version.bind", "dnsmasq-" VERSION );
230
add_txt(daemon, "authors.bind", "Simon Kelley");
231
add_txt(daemon, "copyright.bind", COPYRIGHT);
240
#ifdef HAVE_GETOPT_LONG
241
option = getopt_long(argc, argv, OPTSTRING, (struct option *)opts, NULL);
243
option = getopt(argc, argv, OPTSTRING);
245
/* Copy optarg so that argv doesn't get changed */
248
strncpy(buff, optarg, MAXDNAME);
249
buff[MAXDNAME-1] = 0;
251
for (p = arg; *p; p++)
259
{ /* f non-NULL, reading from conffile. */
261
if (!fgets(buff, MAXDNAME, f))
263
/* At end of file, all done */
268
conffile = file_name_save;
281
/* Implement quotes, inside quotes we allow \\ \" \n and \t
282
unquoted commas get changed to \001 also strip comments */
284
for (white = 1, p = buff; *p; p++)
288
memmove(p, p+1, strlen(p+1)+1);
289
for(; *p && *p != '"'; p++)
291
(p[1] == '\\' || p[1] == '"' || p[1] == 'n' || p[1] == 't'))
295
else if (p[1] == 'n')
297
memmove(p, p+1, strlen(p+1)+1);
300
memmove(p, p+1, strlen(p+1)+1);
302
complain("missing \"", lineno, conffile);
305
if (white && *p == '#')
315
/* fgets gets end of line char too. */
316
while (strlen(buff) > 0 && isspace(buff[strlen(buff)-1]))
317
buff[strlen(buff)-1] = 0;
320
if ((p=strchr(buff, '=')))
329
for (i=0; opts[i].name; i++)
330
if (strcmp(opts[i].name, buff) == 0)
331
option = opts[i].val;
334
complain("bad option", lineno, conffile);
341
{ /* end of command line args, start reading conffile. */
343
break; /* "confile=" option disables */
346
if (!(f = fopen(conffile, "r")))
348
if (errno == ENOENT && !conffile_set)
349
break; /* No conffile, all done. */
351
die("cannot read %s: %s", conffile);
355
if (!f && option == 'w')
357
printf (usage, CACHESIZ, EDNS_PKTSZ, MAXLEASES);
361
if (!f && option == 'v')
363
printf("Dnsmasq version %s %s\n\n", VERSION, COPYRIGHT);
364
printf("This software comes with ABSOLUTELY NO WARRANTY.\n");
365
printf("Dnsmasq is free software, and you are welcome to redistribute it\n");
366
printf("under the terms of the GNU General Public License, version 2.\n");
370
for (i=0; optmap[i].c; i++)
371
if (option == optmap[i].c)
373
daemon->options |= optmap[i].flag;
376
complain("extraneous parameter", lineno, conffile);
380
if (option && option != '?')
384
complain("missing parameter", lineno, conffile);
393
conffile = safe_filename_alloc(arg);
398
/* nest conffiles one deep */
401
complain("nested includes not allowed", lineno, conffile);
404
file_name_save = conffile;
407
conffile = safe_filename_alloc(arg);
413
daemon->runfile = safe_filename_alloc(arg);
418
char *name = safe_filename_alloc(arg);
419
struct resolvc *new, *list = daemon->resolv_files;
421
if (list && list->is_default)
423
/* replace default resolv file - possibly with nothing */
426
list->is_default = 0;
434
new = safe_malloc(sizeof(struct resolvc));
442
daemon->resolv_files = list;
449
struct mx_srv_record *new;
451
if ((comma = strchr(arg, '\001')))
455
if ((prefstr=strchr(comma, '\001')))
458
if (!atoi_check(prefstr, &pref))
461
problem = "bad MX preference";
467
if (!canonicalise(arg) || (comma && !canonicalise(comma)))
470
problem = "bad MX name";
474
new = safe_malloc(sizeof(struct mx_srv_record));
475
new->next = daemon->mxnames;
476
daemon->mxnames = new;
478
new->name = safe_string_alloc(arg);
479
new->target = safe_string_alloc(comma); /* may be NULL */
485
if (!canonicalise(arg))
488
problem = "bad MX target";
491
daemon->mxtarget = safe_string_alloc(arg);
495
daemon->lease_file = safe_filename_alloc(arg);
500
struct hostsfile *new = safe_malloc(sizeof(struct hostsfile));
501
new->fname = safe_filename_alloc(arg);
502
new->index = hosts_index++;
503
new->next = daemon->addn_hosts;
504
daemon->addn_hosts = new;
509
if (strcmp (arg, "#") == 0)
510
daemon->options |= OPT_RESOLV_DOMAIN;
511
else if (!canonicalise(arg))
514
daemon->domain_suffix = safe_string_alloc(arg);
518
daemon->username = safe_string_alloc(arg);
522
daemon->groupname = safe_string_alloc(arg);
527
struct iname *new = safe_malloc(sizeof(struct iname));
528
if ((comma = strchr(arg, '\001')))
530
new->next = daemon->if_names;
531
daemon->if_names = new;
532
/* new->name may be NULL if someone does
533
"interface=" to disable all interfaces except loop. */
534
new->name = safe_string_alloc(arg);
535
new->isloop = new->used = 0;
536
if (strchr(arg, ':'))
537
daemon->options |= OPT_NOWILD;
544
struct iname *new = safe_malloc(sizeof(struct iname));
545
if ((comma = strchr(arg, '\001')))
547
new->next = daemon->if_except;
548
daemon->if_except = new;
549
new->name = safe_string_alloc(arg);
550
if (strchr(arg, ':'))
551
daemon->options |= OPT_NOWILD;
559
if ((addr.s_addr = inet_addr(arg)) != (in_addr_t)-1)
561
struct bogus_addr *baddr = safe_malloc(sizeof(struct bogus_addr));
562
baddr->next = daemon->bogus_addr;
563
daemon->bogus_addr = baddr;
567
option = '?'; /* error */
573
struct iname *new = safe_malloc(sizeof(struct iname));
574
if ((comma = strchr(arg, '\001')))
576
new->next = daemon->if_addrs;
578
if (inet_pton(AF_INET, arg, &new->addr.in.sin_addr))
580
new->addr.sa.sa_family = AF_INET;
581
#ifdef HAVE_SOCKADDR_SA_LEN
582
new->addr.in.sin_len = sizeof(struct sockaddr_in);
585
else if (inet_pton(AF_INET6, arg, &new->addr.in6.sin6_addr))
587
new->addr.sa.sa_family = AF_INET6;
588
new->addr.in6.sin6_flowinfo = htonl(0);
589
#ifdef HAVE_SOCKADDR_SA_LEN
590
new->addr.in6.sin6_len = sizeof(struct sockaddr_in6);
594
if ((new->addr.in.sin_addr.s_addr = inet_addr(arg)) != (in_addr_t)-1)
596
new->addr.sa.sa_family = AF_INET;
597
#ifdef HAVE_SOCKADDR_SA_LEN
598
new->addr.in.sin_len = sizeof(struct sockaddr_in);
604
option = '?'; /* error */
609
daemon->if_addrs = new;
617
struct server *serv, *newlist = NULL;
623
while ((end = strchr(arg, '/')))
627
/* # matches everything and becomes a zero length domain string */
628
if (strcmp(arg, "#") == 0)
630
else if (!canonicalise(arg) && strlen(arg) != 0)
633
domain = safe_string_alloc(arg); /* NULL if strlen is zero */
634
serv = safe_malloc(sizeof(struct server));
635
serv->next = newlist;
638
serv->domain = domain;
639
serv->flags = domain ? SERV_HAS_DOMAIN : SERV_FOR_NODOTS;
651
newlist = safe_malloc(sizeof(struct server));
652
newlist->next = NULL;
655
newlist->domain = NULL;
660
newlist->flags |= SERV_LITERAL_ADDRESS;
661
if (!(newlist->flags & SERV_TYPE))
667
newlist->flags |= SERV_NO_ADDR; /* no server */
668
if (newlist->flags & SERV_LITERAL_ADDRESS)
673
int source_port = 0, serv_port = NAMESERVER_PORT;
674
char *portno, *source;
676
if ((source = strchr(arg, '@'))) /* is there a source. */
679
if ((portno = strchr(source+1, '#')))
682
if (!atoi_check(portno+1, &source_port))
685
problem = "bad port";
690
if ((portno = strchr(arg, '#'))) /* is there a port no. */
693
if (!atoi_check(portno+1, &serv_port))
696
problem = "bad port";
701
if (inet_pton(AF_INET, arg, &newlist->addr.in.sin_addr))
703
if ((newlist->addr.in.sin_addr.s_addr = inet_addr(arg)) != (in_addr_t) -1)
706
newlist->addr.in.sin_port = htons(serv_port);
707
newlist->source_addr.in.sin_port = htons(source_port);
708
newlist->addr.sa.sa_family = newlist->source_addr.sa.sa_family = AF_INET;
709
#ifdef HAVE_SOCKADDR_SA_LEN
710
newlist->source_addr.in.sin_len = newlist->addr.in.sin_len = sizeof(struct sockaddr_in);
715
if (inet_pton(AF_INET, source+1, &newlist->source_addr.in.sin_addr))
717
if ((newlist->source_addr.in.sin_addr.s_addr = inet_addr(source+1)) != (in_addr_t) -1)
719
newlist->flags |= SERV_HAS_SOURCE;
721
option = '?'; /* error */
724
newlist->source_addr.in.sin_addr.s_addr = INADDR_ANY;
727
else if (inet_pton(AF_INET6, arg, &newlist->addr.in6.sin6_addr))
729
newlist->addr.in6.sin6_port = htons(serv_port);
730
newlist->source_addr.in6.sin6_port = htons(source_port);
731
newlist->addr.sa.sa_family = newlist->source_addr.sa.sa_family = AF_INET6;
732
newlist->addr.in6.sin6_flowinfo = newlist->source_addr.in6.sin6_flowinfo = htonl(0);
733
#ifdef HAVE_SOCKADDR_SA_LEN
734
newlist->addr.in6.sin6_len = newlist->source_addr.in6.sin6_len = sizeof(struct sockaddr_in6);
738
if (inet_pton(AF_INET6, source+1, &newlist->source_addr.in6.sin6_addr))
739
newlist->flags |= SERV_HAS_SOURCE;
741
option = '?'; /* error */
744
newlist->source_addr.in6.sin6_addr = in6addr_any;
748
option = '?'; /* error */
756
newlist = newlist->next;
764
serv->next->flags = serv->flags;
765
serv->next->addr = serv->addr;
766
serv->next->source_addr = serv->source_addr;
769
serv->next = daemon->servers;
770
daemon->servers = newlist;
778
if (!atoi_check(arg, &size))
782
/* zero is OK, and means no caching. */
786
else if (size > 10000)
789
daemon->cachesize = size;
795
if (!atoi_check(arg, &daemon->port))
802
if (!atoi_check(arg, &i))
804
daemon->edns_pktsz = (unsigned short)i;
809
if (!atoi_check(arg, &daemon->query_port))
816
if (!atoi_check(arg, &ttl))
819
daemon->local_ttl = (unsigned long)ttl;
824
if (!atoi_check(arg, &daemon->dhcp_max))
831
char *cp, *a[5] = { NULL, NULL, NULL, NULL, NULL };
832
struct dhcp_context *new = safe_malloc(sizeof(struct dhcp_context));
834
new->next = daemon->dhcp;
835
new->lease_time = DEFLEASE;
837
new->netmask.s_addr = 0;
838
new->broadcast.s_addr = 0;
839
new->router.s_addr = 0;
840
new->netid.net = NULL;
843
problem = "bad dhcp-range";
845
for (cp = arg; *cp; cp++)
846
if (!(*cp == ' ' || *cp == '.' || (*cp >='0' && *cp <= '9')))
849
if (*cp != '\001' && (comma = strchr(arg, '\001')))
852
if (strstr(arg, "net:") == arg)
854
new->netid.net = safe_string_alloc(arg+4);
855
new->netid.next = NULL;
856
new->flags |= CONTEXT_FILTER;
859
new->netid.net = safe_string_alloc(arg);
866
for (k = 1; k < 5; k++)
868
if (!(a[k] = strchr(a[k-1], '\001')))
873
if ((k < 2) || ((new->start.s_addr = inet_addr(a[0])) == (in_addr_t)-1))
875
else if (strcmp(a[1], "static") == 0)
877
new->end = new->start;
878
new->flags |= CONTEXT_STATIC;
880
else if ((new->end.s_addr = inet_addr(a[1])) == (in_addr_t)-1)
883
if (ntohl(new->start.s_addr) > ntohl(new->end.s_addr))
885
struct in_addr tmp = new->start;
886
new->start = new->end;
890
if (option != '?' && k >= 3 && strchr(a[2], '.') &&
891
((new->netmask.s_addr = inet_addr(a[2])) != (in_addr_t)-1))
893
new->flags |= CONTEXT_NETMASK;
895
if (!is_same_net(new->start, new->end, new->netmask))
897
problem = "inconsistent DHCP range";
910
if (k >= 4 && strchr(a[3], '.') &&
911
((new->broadcast.s_addr = inet_addr(a[3])) != (in_addr_t)-1))
913
new->flags |= CONTEXT_BRDCAST;
919
if (strcmp(a[leasepos], "infinite") == 0)
920
new->lease_time = 0xffffffff;
924
if (strlen(a[leasepos]) > 0)
926
switch (a[leasepos][strlen(a[leasepos]) - 1])
938
a[leasepos][strlen(a[leasepos]) - 1] = 0;
941
new->lease_time = atoi(a[leasepos]) * fac;
942
/* Leases of a minute or less confuse
943
some clients, notably Apple's */
944
if (new->lease_time < 120)
945
new->lease_time = 120;
950
if (new->lease_time < daemon->min_leasetime)
951
daemon->min_leasetime = new->lease_time;
958
char *a[6] = { NULL, NULL, NULL, NULL, NULL, NULL };
959
struct dhcp_config *new = safe_malloc(sizeof(struct dhcp_config));
962
new->next = daemon->dhcp_conf;
967
for (k = 1; k < 6; k++)
969
if (!(a[k] = strchr(a[k-1], '\001')))
974
for(j = 0; j < k; j++)
975
if (strchr(a[j], ':')) /* ethernet address, netid or binary CLID */
979
if ((arg[0] == 'i' || arg[0] == 'I') &&
980
(arg[1] == 'd' || arg[1] == 'D') &&
984
new->flags |= CONFIG_NOCLID;
988
arg += 3; /* dump id: */
989
if (strchr(arg, ':'))
990
len = parse_hex(arg, arg, -1, NULL);
992
len = (int) strlen(arg);
994
new->flags |= CONFIG_CLID;
996
new->clid = safe_malloc(len);
997
memcpy(new->clid, arg, len);
1000
else if (strstr(arg, "net:") == arg)
1002
new->flags |= CONFIG_NETID;
1003
new->netid.net = safe_string_alloc(arg+4);
1005
else if (parse_hex(a[j], new->hwaddr, 6, &new->wildcard_mask) == 6)
1006
new->flags |= CONFIG_HWADDR;
1010
else if (strchr(a[j], '.') && (in.s_addr = inet_addr(a[j])) != (in_addr_t)-1)
1013
new->flags |= CONFIG_ADDR;
1017
char *cp, *lastp = NULL, last = 0;
1020
if (strlen(a[j]) > 1)
1022
lastp = a[j] + strlen(a[j]) - 1;
1040
for (cp = a[j]; *cp; cp++)
1041
if (!isdigit(*cp) && *cp != ' ')
1048
if (strcmp(a[j], "infinite") == 0)
1050
new->lease_time = 0xffffffff;
1051
new->flags |= CONFIG_TIME;
1053
else if (strcmp(a[j], "ignore") == 0)
1054
new->flags |= CONFIG_DISABLE;
1057
new->hostname = safe_string_alloc(a[j]);
1058
new->flags |= CONFIG_NAME;
1063
new->lease_time = atoi(a[j]) * fac;
1064
/* Leases of a minute or less confuse
1065
some clients, notably Apple's */
1066
if (new->lease_time < 120)
1067
new->lease_time = 120;
1068
new->flags |= CONFIG_TIME;
1074
problem = "bad dhcp-host";
1075
if (new->flags & CONFIG_NAME)
1076
free(new->hostname);
1077
if (new->flags & CONFIG_CLID)
1079
if (new->flags & CONFIG_NETID)
1080
free(new->netid.net);
1085
if ((new->flags & CONFIG_TIME) && new->lease_time < daemon->min_leasetime)
1086
daemon->min_leasetime = new->lease_time;
1087
daemon->dhcp_conf = new;
1094
struct dhcp_opt *new = safe_malloc(sizeof(struct dhcp_opt));
1095
char lenchar = 0, *cp;
1096
int addrs, digs, is_addr, is_hex, is_dec;
1102
new->vendor_class = NULL;
1104
if ((comma = strchr(arg, '\001')))
1106
struct dhcp_netid *np = NULL;
1110
for (cp = arg; *cp; cp++)
1111
if (!(*cp == ' ' || (*cp >='0' && *cp <= '9')))
1116
if (strstr(arg, "vendor:") == arg)
1117
new->vendor_class = safe_string_alloc(arg+7);
1120
new->netid = safe_malloc(sizeof (struct dhcp_netid));
1121
new->netid->net = safe_string_alloc(arg);
1122
new->netid->next = np;
1126
if ((comma = strchr(arg, '\001')))
1131
if (!arg || (new->opt = atoi(arg)) == 0)
1134
problem = "bad dhcp-option";
1136
else if (comma && new->opt == 119 && !new->vendor_class)
1138
/* dns search, RFC 3397 */
1139
unsigned char *q, *r, *tail;
1140
unsigned char *p = NULL;
1141
size_t newlen, len = 0;
1144
if ((comma = strchr(arg, '\001')))
1149
if (!canonicalise(arg))
1152
problem = "bad domain in dhcp-option";
1156
if (!(p = realloc(p, len + strlen(arg) + 2)))
1157
die("could not get memory", NULL);
1160
/* add string on the end in RFC1035 format */
1165
for (j = 0; *arg && (*arg != '.'); arg++, j++)
1173
/* Now tail-compress using earlier names. */
1175
for (tail = p + len; *tail; tail += (*tail) + 1)
1176
for (r = p; r - p < (int)len; r += (*r) + 1)
1177
if (strcmp(r, tail) == 0)
1179
PUTSHORT((r - p) | 0xc000, tail);
1187
if (arg && (comma = strchr(arg, '\001')))
1191
new->len = (int) len;
1196
/* not option 119 */
1197
/* characterise the value */
1198
is_addr = is_hex = is_dec = 1;
1200
for (cp = comma; *cp; cp++)
1204
is_dec = is_hex = 0;
1206
else if (*cp == ':')
1209
is_dec = is_addr = 0;
1211
else if (*cp == '.')
1212
is_dec = is_hex = 0;
1213
else if (!((*cp >='0' && *cp <= '9') || *cp == '-'))
1216
if (cp[1] == 0 && is_dec &&
1217
(*cp == 'b' || *cp == 's' || *cp == 'i'))
1224
if (!((*cp >='A' && *cp <= 'F') ||
1225
(*cp >='a' && *cp <= 'f')))
1229
if (is_hex && digs > 1)
1232
new->val = safe_malloc(new->len);
1233
parse_hex(comma, new->val, digs, NULL);
1237
int i, val = atoi(comma);
1238
/* assume numeric arg is 1 byte except for
1239
options where it is known otherwise.
1240
For vendor class option, we have to hack. */
1244
else if (lenchar == 's')
1246
else if (lenchar == 'i')
1248
else if (new->vendor_class)
1250
if (val & 0xffff0000)
1252
else if (val & 0xff00)
1258
case 13: case 22: case 25: case 26:
1261
case 2: case 24: case 35: case 38:
1265
new->val = safe_malloc(new->len);
1266
for (i=0; i<new->len; i++)
1267
new->val[i] = val>>((new->len - i - 1)*8);
1273
new->len = INADDRSZ * addrs;
1274
new->val = op = safe_malloc(new->len);
1279
if ((comma = strchr(cp, '\001')))
1281
in.s_addr = inet_addr(cp);
1282
memcpy(op, &in, INADDRSZ);
1289
new->len = strlen(comma);
1290
/* keep terminating zero on string */
1291
new->val = safe_string_alloc(comma);
1298
problem = "dhcp-option too long";
1307
if (new->vendor_class)
1308
free(new->vendor_class);
1311
else if (new->vendor_class)
1313
new->next = daemon->vendor_opts;
1314
daemon->vendor_opts = new;
1318
new->next = daemon->dhcp_opts;
1319
daemon->dhcp_opts = new;
1326
struct dhcp_netid *id = NULL;
1327
while (arg && strstr(arg, "net:") == arg)
1329
struct dhcp_netid *newid = safe_malloc(sizeof(struct dhcp_netid));
1332
if ((comma = strchr(arg, '\001')))
1334
newid->net = safe_string_alloc(arg+4);
1342
char *dhcp_file, *dhcp_sname = NULL;
1343
struct in_addr dhcp_next_server;
1344
if ((comma = strchr(arg, '\001')))
1346
dhcp_file = safe_string_alloc(arg);
1347
dhcp_next_server.s_addr = 0;
1351
if ((comma = strchr(arg, '\001')))
1353
dhcp_sname = safe_string_alloc(arg);
1354
if (comma && (dhcp_next_server.s_addr = inet_addr(comma)) == (in_addr_t)-1)
1359
struct dhcp_boot *new = safe_malloc(sizeof(struct dhcp_boot));
1360
new->file = dhcp_file;
1361
new->sname = dhcp_sname;
1362
new->next_server = dhcp_next_server;
1364
new->next = daemon->boot_config;
1365
daemon->boot_config = new;
1371
struct dhcp_netid *tmp;
1372
for (; id; id = tmp)
1384
if (!(comma = strchr(arg, '\001')))
1388
struct dhcp_vendor *new = safe_malloc(sizeof(struct dhcp_vendor));
1390
new->netid.net = safe_string_alloc(arg);
1391
new->len = strlen(comma+1);
1392
new->data = safe_malloc(new->len);
1393
memcpy(new->data, comma+1, new->len);
1394
new->is_vendor = (option == 'U');
1395
new->next = daemon->dhcp_vendors;
1396
daemon->dhcp_vendors = new;
1403
struct dhcp_netid_list *new = safe_malloc(sizeof(struct dhcp_netid_list));
1404
struct dhcp_netid *list = NULL;
1405
new->next = daemon->dhcp_ignore;
1406
daemon->dhcp_ignore = new;
1408
struct dhcp_netid *member = safe_malloc(sizeof(struct dhcp_netid));
1409
if ((comma = strchr(arg, '\001')))
1411
member->next = list;
1413
member->net = safe_string_alloc(arg);
1423
char *a[3] = { NULL, NULL, NULL };
1425
struct in_addr in, out, mask;
1428
mask.s_addr = 0xffffffff;
1431
for (k = 1; k < 4; k++)
1433
if (!(a[k] = strchr(a[k-1], '\001')))
1439
((in.s_addr = inet_addr(a[0])) == (in_addr_t)-1) ||
1440
((out.s_addr = inet_addr(a[1])) == (in_addr_t)-1))
1447
mask.s_addr = inet_addr(a[2]);
1449
new = safe_malloc(sizeof(struct doctor));
1453
new->next = daemon->doctors;
1454
daemon->doctors = new;
1461
struct txt_record *new;
1462
unsigned char *p, *q;
1464
if ((comma = strchr(arg, '\001')))
1467
if (!canonicalise(arg))
1470
problem = "bad TXT record";
1478
if ((p = strchr(q+1, '\001')))
1480
if ((len = p - q - 1) > 255)
1490
if ((len = strlen(q+1)) > 255)
1499
problem = "TXT record string too long";
1503
new = safe_malloc(sizeof(struct txt_record));
1504
new->next = daemon->txt;
1509
new->len = q - ((unsigned char *)comma) + *q + 1;
1510
new->txt = safe_malloc(new->len);
1511
memcpy(new->txt, comma, new->len);
1515
static char empty[] = "";
1522
new->name = safe_string_alloc(arg);
1528
int port = 1, priority = 0, weight = 0;
1529
char *name, *target = NULL;
1530
struct mx_srv_record *new;
1532
if ((comma = strchr(arg, '\001')))
1535
if (!canonicalise(arg))
1538
problem = "bad SRV record";
1541
name = safe_string_alloc(arg);
1546
if ((comma = strchr(arg, '\001')))
1548
if (!canonicalise(arg))
1551
problem = "bad SRV target";
1554
target = safe_string_alloc(arg);
1558
if ((comma = strchr(arg, '\001')))
1560
if (!atoi_check(arg, &port))
1563
problem = "invalid port number";
1569
if ((comma = strchr(arg, '\001')))
1571
if (!atoi_check(arg, &priority))
1574
problem = "invalid priority";
1580
if ((comma = strchr(arg, '\001')))
1582
if (!atoi_check(arg, &weight))
1585
problem = "invalid weight";
1593
new = safe_malloc(sizeof(struct mx_srv_record));
1594
new->next = daemon->mxnames;
1595
daemon->mxnames = new;
1598
new->target = target;
1599
new->srvport = port;
1600
new->priority = priority;
1601
new->weight = weight;
1610
complain( problem ? problem : "error", lineno, conffile);
1612
#ifdef HAVE_GETOPT_LONG
1613
die("bad command line options: %s.", problem ? problem : "try --help");
1615
die("bad command line options: %s.", problem ? problem : "try -w");
1620
/* port might no be known when the address is parsed - fill in here */
1621
if (daemon->servers)
1624
for (tmp = daemon->servers; tmp; tmp = tmp->next)
1625
if (!(tmp->flags & SERV_HAS_SOURCE))
1627
if (tmp->source_addr.sa.sa_family == AF_INET)
1628
tmp->source_addr.in.sin_port = htons(daemon->query_port);
1630
else if (tmp->source_addr.sa.sa_family == AF_INET6)
1631
tmp->source_addr.in6.sin6_port = htons(daemon->query_port);
1636
if (daemon->if_addrs)
1639
for(tmp = daemon->if_addrs; tmp; tmp = tmp->next)
1640
if (tmp->addr.sa.sa_family == AF_INET)
1641
tmp->addr.in.sin_port = htons(daemon->port);
1643
else if (tmp->addr.sa.sa_family == AF_INET6)
1644
tmp->addr.in6.sin6_port = htons(daemon->port);
1648
/* only one of these need be specified: the other defaults to the host-name */
1649
if ((daemon->options & OPT_LOCALMX) || daemon->mxnames || daemon->mxtarget)
1651
struct mx_srv_record *mx;
1653
if (gethostname(buff, MAXDNAME) == -1)
1654
die("cannot get host-name: %s", NULL);
1656
for (mx = daemon->mxnames; mx; mx = mx->next)
1657
if (!mx->issrv && hostname_isequal(mx->name, buff))
1660
if ((daemon->mxtarget || (daemon->options & OPT_LOCALMX)) && !mx)
1662
mx = safe_malloc(sizeof(struct mx_srv_record));
1663
mx->next = daemon->mxnames;
1666
mx->name = safe_string_alloc(buff);
1667
daemon->mxnames = mx;
1670
if (!daemon->mxtarget)
1671
daemon->mxtarget = safe_string_alloc(buff);
1673
for (mx = daemon->mxnames; mx; mx = mx->next)
1674
if (!mx->issrv && !mx->target)
1675
mx->target = daemon->mxtarget;
1678
if (daemon->domain_suffix)
1680
/* add domain for any srv record without one. */
1681
struct mx_srv_record *srv;
1683
for (srv = daemon->mxnames; srv; srv = srv->next)
1685
strchr(srv->name, '.') &&
1686
strchr(srv->name, '.') == strrchr(srv->name, '.'))
1688
strcpy(buff, srv->name);
1690
strcat(buff, daemon->domain_suffix);
1692
srv->name = safe_string_alloc(buff);
1696
if (daemon->options & OPT_NO_RESOLV)
1697
daemon->resolv_files = 0;
1698
else if (daemon->resolv_files && (daemon->resolv_files)->next && (daemon->options & OPT_NO_POLL))
1699
die("only one resolv.conf file allowed in no-poll mode.", NULL);
1701
if (daemon->options & OPT_RESOLV_DOMAIN)
1705
if (!daemon->resolv_files || (daemon->resolv_files)->next)
1706
die("must have exactly one resolv.conf to read domain from.", NULL);
1708
if (!(f = fopen((daemon->resolv_files)->name, "r")))
1709
die("failed to read %s: %m", (daemon->resolv_files)->name);
1711
while ((line = fgets(buff, MAXDNAME, f)))
1713
char *token = strtok(line, " \t\n\r");
1715
if (!token || strcmp(token, "search") != 0)
1718
if ((token = strtok(NULL, " \t\n\r")) &&
1719
canonicalise(token) &&
1720
(daemon->domain_suffix = safe_string_alloc(token)))
1726
if (!daemon->domain_suffix)
1727
die("no search directive found in %s", (daemon->resolv_files)->name);