blob: a2841d869ce2cc2f829a75bf4e54b58d6b71ed38 [file] [log] [blame]
Simon Kelley59546082012-01-06 20:02:04 +00001/* dnsmasq is Copyright (c) 2000-2012 Simon Kelley
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002
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
Simon Kelley824af852008-02-12 20:43:05 +00005 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
Simon Kelley9e4abcb2004-01-22 19:47:41 +00008 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
Simon Kelley824af852008-02-12 20:43:05 +000012
Simon Kelley73a08a22009-02-05 20:28:08 +000013 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
Simon Kelley9e4abcb2004-01-22 19:47:41 +000015*/
16
Simon Kelley849a8352006-06-09 21:02:31 +010017/* define this to get facilitynames */
18#define SYSLOG_NAMES
Simon Kelley9e4abcb2004-01-22 19:47:41 +000019#include "dnsmasq.h"
Simon Kelley824af852008-02-12 20:43:05 +000020#include <setjmp.h>
21
Simon Kelley7622fc02009-06-04 20:32:05 +010022static volatile int mem_recover = 0;
23static jmp_buf mem_jmp;
Simon Kelley28866e92011-02-14 20:19:14 +000024static void one_file(char *file, int hard_opt);
Simon Kelley7622fc02009-06-04 20:32:05 +010025
Simon Kelley824af852008-02-12 20:43:05 +000026/* Solaris headers don't have facility names. */
27#ifdef HAVE_SOLARIS_NETWORK
28static const struct {
29 char *c_name;
30 unsigned int c_val;
31} facilitynames[] = {
32 { "kern", LOG_KERN },
33 { "user", LOG_USER },
34 { "mail", LOG_MAIL },
35 { "daemon", LOG_DAEMON },
36 { "auth", LOG_AUTH },
37 { "syslog", LOG_SYSLOG },
38 { "lpr", LOG_LPR },
39 { "news", LOG_NEWS },
40 { "uucp", LOG_UUCP },
Simon Kelley824af852008-02-12 20:43:05 +000041 { "audit", LOG_AUDIT },
Simon Kelley824af852008-02-12 20:43:05 +000042 { "cron", LOG_CRON },
43 { "local0", LOG_LOCAL0 },
44 { "local1", LOG_LOCAL1 },
45 { "local2", LOG_LOCAL2 },
46 { "local3", LOG_LOCAL3 },
47 { "local4", LOG_LOCAL4 },
48 { "local5", LOG_LOCAL5 },
49 { "local6", LOG_LOCAL6 },
50 { "local7", LOG_LOCAL7 },
51 { NULL, 0 }
52};
53#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +000054
Simon Kelley849a8352006-06-09 21:02:31 +010055#ifndef HAVE_GETOPT_LONG
Simon Kelley9e4abcb2004-01-22 19:47:41 +000056struct myoption {
57 const char *name;
58 int has_arg;
59 int *flag;
60 int val;
61};
Simon Kelley849a8352006-06-09 21:02:31 +010062#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +000063
Simon Kelley9009d742008-11-14 20:04:27 +000064#define OPTSTRING "951yZDNLERKzowefnbvhdkqr: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:2:4:6:7:8:0:3:"
Simon Kelley9e4abcb2004-01-22 19:47:41 +000065
Simon Kelley16972692006-10-16 20:04:18 +010066/* options which don't have a one-char version */
Simon Kelley832af0b2007-01-21 20:01:28 +000067#define LOPT_RELOAD 256
68#define LOPT_NO_NAMES 257
69#define LOPT_TFTP 258
70#define LOPT_SECURE 259
71#define LOPT_PREFIX 260
72#define LOPT_PTR 261
73#define LOPT_BRIDGE 262
74#define LOPT_TFTP_MAX 263
Simon Kelley6b010842007-02-12 20:32:07 +000075#define LOPT_FORCE 264
76#define LOPT_NOBLOCK 265
Simon Kelleyf2621c72007-04-29 19:47:21 +010077#define LOPT_LOG_OPTS 266
78#define LOPT_MAX_LOGS 267
79#define LOPT_CIRCUIT 268
80#define LOPT_REMOTE 269
81#define LOPT_SUBSCR 270
82#define LOPT_INTNAME 271
Simon Kelley5aabfc72007-08-29 11:24:47 +010083#define LOPT_BANK 272
84#define LOPT_DHCP_HOST 273
85#define LOPT_APREF 274
Simon Kelley824af852008-02-12 20:43:05 +000086#define LOPT_OVERRIDE 275
87#define LOPT_TFTPPORTS 276
88#define LOPT_REBIND 277
89#define LOPT_NOLAST 278
90#define LOPT_OPTS 279
91#define LOPT_DHCP_OPTS 280
92#define LOPT_MATCH 281
93#define LOPT_BROADCAST 282
94#define LOPT_NEGTTL 283
Simon Kelley9e038942008-05-30 20:06:34 +010095#define LOPT_ALTPORT 284
96#define LOPT_SCRIPTUSR 285
Simon Kelley1a6bca82008-07-11 11:11:42 +010097#define LOPT_LOCAL 286
98#define LOPT_NAPTR 287
99#define LOPT_MINPORT 288
Simon Kelley9009d742008-11-14 20:04:27 +0000100#define LOPT_DHCP_FQDN 289
101#define LOPT_CNAME 290
Simon Kelley7622fc02009-06-04 20:32:05 +0100102#define LOPT_PXE_PROMT 291
103#define LOPT_PXE_SERV 292
104#define LOPT_TEST 293
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100105#define LOPT_TAG_IF 294
106#define LOPT_PROXY 295
107#define LOPT_GEN_NAMES 296
108#define LOPT_MAXTTL 297
109#define LOPT_NO_REBIND 298
110#define LOPT_LOC_REBND 299
Simon Kelley28866e92011-02-14 20:19:14 +0000111#define LOPT_ADD_MAC 300
112#define LOPT_DNSSEC 301
Simon Kelley7de060b2011-08-26 17:24:52 +0100113#define LOPT_INCR_ADDR 302
114#define LOPT_CONNTRACK 303
Simon Kelleyc72daea2012-01-05 21:33:27 +0000115#define LOPT_FQDN 304
116#define LOPT_LUASCRIPT 305
Simon Kelley16972692006-10-16 20:04:18 +0100117
Simon Kelley849a8352006-06-09 21:02:31 +0100118#ifdef HAVE_GETOPT_LONG
119static const struct option opts[] =
120#else
121static const struct myoption opts[] =
122#endif
123 {
Simon Kelley7622fc02009-06-04 20:32:05 +0100124 { "version", 0, 0, 'v' },
125 { "no-hosts", 0, 0, 'h' },
126 { "no-poll", 0, 0, 'n' },
127 { "help", 0, 0, 'w' },
128 { "no-daemon", 0, 0, 'd' },
129 { "log-queries", 0, 0, 'q' },
130 { "user", 2, 0, 'u' },
131 { "group", 2, 0, 'g' },
132 { "resolv-file", 2, 0, 'r' },
133 { "mx-host", 1, 0, 'm' },
134 { "mx-target", 1, 0, 't' },
135 { "cache-size", 2, 0, 'c' },
136 { "port", 1, 0, 'p' },
137 { "dhcp-leasefile", 2, 0, 'l' },
138 { "dhcp-lease", 1, 0, 'l' },
139 { "dhcp-host", 1, 0, 'G' },
140 { "dhcp-range", 1, 0, 'F' },
141 { "dhcp-option", 1, 0, 'O' },
142 { "dhcp-boot", 1, 0, 'M' },
143 { "domain", 1, 0, 's' },
144 { "domain-suffix", 1, 0, 's' },
145 { "interface", 1, 0, 'i' },
146 { "listen-address", 1, 0, 'a' },
147 { "bogus-priv", 0, 0, 'b' },
148 { "bogus-nxdomain", 1, 0, 'B' },
149 { "selfmx", 0, 0, 'e' },
150 { "filterwin2k", 0, 0, 'f' },
151 { "pid-file", 2, 0, 'x' },
152 { "strict-order", 0, 0, 'o' },
153 { "server", 1, 0, 'S' },
154 { "local", 1, 0, LOPT_LOCAL },
155 { "address", 1, 0, 'A' },
156 { "conf-file", 2, 0, 'C' },
157 { "no-resolv", 0, 0, 'R' },
158 { "expand-hosts", 0, 0, 'E' },
159 { "localmx", 0, 0, 'L' },
160 { "local-ttl", 1, 0, 'T' },
161 { "no-negcache", 0, 0, 'N' },
162 { "addn-hosts", 1, 0, 'H' },
163 { "query-port", 1, 0, 'Q' },
164 { "except-interface", 1, 0, 'I' },
165 { "no-dhcp-interface", 1, 0, '2' },
166 { "domain-needed", 0, 0, 'D' },
167 { "dhcp-lease-max", 1, 0, 'X' },
168 { "bind-interfaces", 0, 0, 'z' },
169 { "read-ethers", 0, 0, 'Z' },
170 { "alias", 1, 0, 'V' },
171 { "dhcp-vendorclass", 1, 0, 'U' },
172 { "dhcp-userclass", 1, 0, 'j' },
173 { "dhcp-ignore", 1, 0, 'J' },
174 { "edns-packet-max", 1, 0, 'P' },
175 { "keep-in-foreground", 0, 0, 'k' },
176 { "dhcp-authoritative", 0, 0, 'K' },
177 { "srv-host", 1, 0, 'W' },
178 { "localise-queries", 0, 0, 'y' },
179 { "txt-record", 1, 0, 'Y' },
180 { "enable-dbus", 0, 0, '1' },
181 { "bootp-dynamic", 2, 0, '3' },
182 { "dhcp-mac", 1, 0, '4' },
183 { "no-ping", 0, 0, '5' },
184 { "dhcp-script", 1, 0, '6' },
185 { "conf-dir", 1, 0, '7' },
186 { "log-facility", 1, 0 ,'8' },
187 { "leasefile-ro", 0, 0, '9' },
188 { "dns-forward-max", 1, 0, '0' },
189 { "clear-on-reload", 0, 0, LOPT_RELOAD },
190 { "dhcp-ignore-names", 2, 0, LOPT_NO_NAMES },
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100191 { "enable-tftp", 2, 0, LOPT_TFTP },
Simon Kelley7622fc02009-06-04 20:32:05 +0100192 { "tftp-secure", 0, 0, LOPT_SECURE },
193 { "tftp-unique-root", 0, 0, LOPT_APREF },
194 { "tftp-root", 1, 0, LOPT_PREFIX },
195 { "tftp-max", 1, 0, LOPT_TFTP_MAX },
196 { "ptr-record", 1, 0, LOPT_PTR },
197 { "naptr-record", 1, 0, LOPT_NAPTR },
198 { "bridge-interface", 1, 0 , LOPT_BRIDGE },
199 { "dhcp-option-force", 1, 0, LOPT_FORCE },
200 { "tftp-no-blocksize", 0, 0, LOPT_NOBLOCK },
201 { "log-dhcp", 0, 0, LOPT_LOG_OPTS },
202 { "log-async", 2, 0, LOPT_MAX_LOGS },
203 { "dhcp-circuitid", 1, 0, LOPT_CIRCUIT },
204 { "dhcp-remoteid", 1, 0, LOPT_REMOTE },
205 { "dhcp-subscrid", 1, 0, LOPT_SUBSCR },
206 { "interface-name", 1, 0, LOPT_INTNAME },
207 { "dhcp-hostsfile", 1, 0, LOPT_DHCP_HOST },
208 { "dhcp-optsfile", 1, 0, LOPT_DHCP_OPTS },
209 { "dhcp-no-override", 0, 0, LOPT_OVERRIDE },
210 { "tftp-port-range", 1, 0, LOPT_TFTPPORTS },
211 { "stop-dns-rebind", 0, 0, LOPT_REBIND },
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100212 { "rebind-domain-ok", 1, 0, LOPT_NO_REBIND },
Simon Kelley7622fc02009-06-04 20:32:05 +0100213 { "all-servers", 0, 0, LOPT_NOLAST },
214 { "dhcp-match", 1, 0, LOPT_MATCH },
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100215 { "dhcp-broadcast", 2, 0, LOPT_BROADCAST },
Simon Kelley7622fc02009-06-04 20:32:05 +0100216 { "neg-ttl", 1, 0, LOPT_NEGTTL },
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100217 { "max-ttl", 1, 0, LOPT_MAXTTL },
Simon Kelley7622fc02009-06-04 20:32:05 +0100218 { "dhcp-alternate-port", 2, 0, LOPT_ALTPORT },
219 { "dhcp-scriptuser", 1, 0, LOPT_SCRIPTUSR },
220 { "min-port", 1, 0, LOPT_MINPORT },
221 { "dhcp-fqdn", 0, 0, LOPT_DHCP_FQDN },
222 { "cname", 1, 0, LOPT_CNAME },
223 { "pxe-prompt", 1, 0, LOPT_PXE_PROMT },
224 { "pxe-service", 1, 0, LOPT_PXE_SERV },
225 { "test", 0, 0, LOPT_TEST },
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100226 { "tag-if", 1, 0, LOPT_TAG_IF },
227 { "dhcp-proxy", 2, 0, LOPT_PROXY },
228 { "dhcp-generate-names", 2, 0, LOPT_GEN_NAMES },
229 { "rebind-localhost-ok", 0, 0, LOPT_LOC_REBND },
Simon Kelley28866e92011-02-14 20:19:14 +0000230 { "add-mac", 0, 0, LOPT_ADD_MAC },
231 { "proxy-dnssec", 0, 0, LOPT_DNSSEC },
Simon Kelley7de060b2011-08-26 17:24:52 +0100232 { "dhcp-sequential-ip", 0, 0, LOPT_INCR_ADDR },
233 { "conntrack", 0, 0, LOPT_CONNTRACK },
Simon Kelleyc72daea2012-01-05 21:33:27 +0000234 { "dhcp-client-update", 0, 0, LOPT_FQDN },
235 { "dhcp-luascript", 1, 0, LOPT_LUASCRIPT },
Simon Kelley849a8352006-06-09 21:02:31 +0100236 { NULL, 0, 0, 0 }
237 };
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000238
Simon Kelley28866e92011-02-14 20:19:14 +0000239
240#define ARG_DUP OPT_LAST
241#define ARG_ONE OPT_LAST + 1
242#define ARG_USED_CL OPT_LAST + 2
243#define ARG_USED_FILE OPT_LAST + 3
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000244
Simon Kelley1a6bca82008-07-11 11:11:42 +0100245static struct {
246 int opt;
247 unsigned int rept;
248 char * const flagdesc;
Simon Kelleyb8187c82005-11-26 21:46:27 +0000249 char * const desc;
250 char * const arg;
251} usage[] = {
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000252 { 'a', ARG_DUP, "<ipaddr>", gettext_noop("Specify local address(es) to listen on."), NULL },
253 { 'A', ARG_DUP, "/<domain>/<ipaddr>", gettext_noop("Return ipaddr for all hosts in specified domains."), NULL },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100254 { 'b', OPT_BOGUSPRIV, NULL, gettext_noop("Fake reverse lookups for RFC1918 private address ranges."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000255 { 'B', ARG_DUP, "<ipaddr>", gettext_noop("Treat ipaddr as NXDOMAIN (defeats Verisign wildcard)."), NULL },
256 { 'c', ARG_ONE, "<integer>", gettext_noop("Specify the size of the cache in entries (defaults to %s)."), "$" },
257 { 'C', ARG_DUP, "<path>", gettext_noop("Specify configuration file (defaults to %s)."), CONFFILE },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100258 { 'd', OPT_DEBUG, NULL, gettext_noop("Do NOT fork into the background: run in debug mode."), NULL },
259 { 'D', OPT_NODOTS_LOCAL, NULL, gettext_noop("Do NOT forward queries with no domain part."), NULL },
260 { 'e', OPT_SELFMX, NULL, gettext_noop("Return self-pointing MX records for local hosts."), NULL },
261 { 'E', OPT_EXPAND, NULL, gettext_noop("Expand simple names in /etc/hosts with domain-suffix."), NULL },
262 { 'f', OPT_FILTER, NULL, gettext_noop("Don't forward spurious DNS requests from Windows hosts."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000263 { 'F', ARG_DUP, "<ipaddr>,...", gettext_noop("Enable DHCP in the range given with lease duration."), NULL },
264 { 'g', ARG_ONE, "<groupname>", gettext_noop("Change to this group after startup (defaults to %s)."), CHGRP },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100265 { 'G', ARG_DUP, "<hostspec>", gettext_noop("Set address or hostname for a specified machine."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000266 { LOPT_DHCP_HOST, ARG_DUP, "<path>", gettext_noop("Read DHCP host specs from file."), NULL },
267 { LOPT_DHCP_OPTS, ARG_DUP, "<path>", gettext_noop("Read DHCP option specs from file."), NULL },
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100268 { LOPT_TAG_IF, ARG_DUP, "tag-expression", gettext_noop("Evaluate conditional tag expression."), NULL },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100269 { 'h', OPT_NO_HOSTS, NULL, gettext_noop("Do NOT load %s file."), HOSTSFILE },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000270 { 'H', ARG_DUP, "<path>", gettext_noop("Specify a hosts file to be read in addition to %s."), HOSTSFILE },
271 { 'i', ARG_DUP, "<interface>", gettext_noop("Specify interface(s) to listen on."), NULL },
272 { 'I', ARG_DUP, "<interface>", gettext_noop("Specify interface(s) NOT to listen on.") , NULL },
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100273 { 'j', ARG_DUP, "set:<tag>,<class>", gettext_noop("Map DHCP user class to tag."), NULL },
274 { LOPT_CIRCUIT, ARG_DUP, "set:<tag>,<circuit>", gettext_noop("Map RFC3046 circuit-id to tag."), NULL },
275 { LOPT_REMOTE, ARG_DUP, "set:<tag>,<remote>", gettext_noop("Map RFC3046 remote-id to tag."), NULL },
276 { LOPT_SUBSCR, ARG_DUP, "set:<tag>,<remote>", gettext_noop("Map RFC3993 subscriber-id to tag."), NULL },
277 { 'J', ARG_DUP, "tag:<tag>...", gettext_noop("Don't do DHCP for hosts with tag set."), NULL },
278 { LOPT_BROADCAST, ARG_DUP, "[=tag:<tag>...]", gettext_noop("Force broadcast replies for hosts with tag set."), NULL },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100279 { 'k', OPT_NO_FORK, NULL, gettext_noop("Do NOT fork into the background, do NOT run in debug mode."), NULL },
280 { 'K', OPT_AUTHORITATIVE, NULL, gettext_noop("Assume we are the only DHCP server on the local network."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000281 { 'l', ARG_ONE, "<path>", gettext_noop("Specify where to store DHCP leases (defaults to %s)."), LEASEFILE },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100282 { 'L', OPT_LOCALMX, NULL, gettext_noop("Return MX records for local hosts."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000283 { 'm', ARG_DUP, "<host_name>,<target>,<pref>", gettext_noop("Specify an MX record."), NULL },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100284 { 'M', ARG_DUP, "<bootp opts>", gettext_noop("Specify BOOTP options to DHCP server."), NULL },
285 { 'n', OPT_NO_POLL, NULL, gettext_noop("Do NOT poll %s file, reload only on SIGHUP."), RESOLVFILE },
286 { 'N', OPT_NO_NEG, NULL, gettext_noop("Do NOT cache failed search results."), NULL },
287 { 'o', OPT_ORDER, NULL, gettext_noop("Use nameservers strictly in the order given in %s."), RESOLVFILE },
288 { 'O', ARG_DUP, "<optspec>", gettext_noop("Specify options to be sent to DHCP clients."), NULL },
289 { LOPT_FORCE, ARG_DUP, "<optspec>", gettext_noop("DHCP option sent even if the client does not request it."), NULL},
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000290 { 'p', ARG_ONE, "<integer>", gettext_noop("Specify port to listen for DNS requests on (defaults to 53)."), NULL },
291 { 'P', ARG_ONE, "<integer>", gettext_noop("Maximum supported UDP packet size for EDNS.0 (defaults to %s)."), "*" },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100292 { 'q', OPT_LOG, NULL, gettext_noop("Log DNS queries."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000293 { 'Q', ARG_ONE, "<integer>", gettext_noop("Force the originating port for upstream DNS queries."), NULL },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100294 { 'R', OPT_NO_RESOLV, NULL, gettext_noop("Do NOT read resolv.conf."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000295 { 'r', ARG_DUP, "<path>", gettext_noop("Specify path to resolv.conf (defaults to %s)."), RESOLVFILE },
296 { 'S', ARG_DUP, "/<domain>/<ipaddr>", gettext_noop("Specify address(es) of upstream servers with optional domains."), NULL },
297 { LOPT_LOCAL, ARG_DUP, "/<domain>/", gettext_noop("Never forward queries to specified domains."), NULL },
Simon Kelley9009d742008-11-14 20:04:27 +0000298 { 's', ARG_DUP, "<domain>[,<range>]", gettext_noop("Specify the domain to be assigned in DHCP leases."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000299 { 't', ARG_ONE, "<host_name>", gettext_noop("Specify default target in an MX record."), NULL },
300 { 'T', ARG_ONE, "<integer>", gettext_noop("Specify time-to-live in seconds for replies from /etc/hosts."), NULL },
301 { LOPT_NEGTTL, ARG_ONE, "<integer>", gettext_noop("Specify time-to-live in seconds for negative caching."), NULL },
302 { LOPT_MAXTTL, ARG_ONE, "<integer>", gettext_noop("Specify time-to-live in seconds for maximum TTL to send to clients."), NULL },
303 { 'u', ARG_ONE, "<username>", gettext_noop("Change to this user after startup. (defaults to %s)."), CHUSER },
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100304 { 'U', ARG_DUP, "set:<tag>,<class>", gettext_noop("Map DHCP vendor class to tag."), NULL },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100305 { 'v', 0, NULL, gettext_noop("Display dnsmasq version and copyright information."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000306 { 'V', ARG_DUP, "<ipaddr>,<ipaddr>,<netmask>", gettext_noop("Translate IPv4 addresses from upstream servers."), NULL },
307 { 'W', ARG_DUP, "<name>,<target>,...", gettext_noop("Specify a SRV record."), NULL },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100308 { 'w', 0, NULL, gettext_noop("Display this message. Use --help dhcp for known DHCP options."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000309 { 'x', ARG_ONE, "<path>", gettext_noop("Specify path of PID file (defaults to %s)."), RUNFILE },
310 { 'X', ARG_ONE, "<integer>", gettext_noop("Specify maximum number of DHCP leases (defaults to %s)."), "&" },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100311 { 'y', OPT_LOCALISE, NULL, gettext_noop("Answer DNS queries based on the interface a query was sent to."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000312 { 'Y', ARG_DUP, "<name>,<txt>[,<txt]", gettext_noop("Specify TXT DNS record."), NULL },
313 { LOPT_PTR, ARG_DUP, "<name>,<target>", gettext_noop("Specify PTR DNS record."), NULL },
314 { LOPT_INTNAME, ARG_DUP, "<name>,<interface>", gettext_noop("Give DNS name to IPv4 address of interface."), NULL },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100315 { 'z', OPT_NOWILD, NULL, gettext_noop("Bind only to interfaces in use."), NULL },
316 { 'Z', OPT_ETHERS, NULL, gettext_noop("Read DHCP static host information from %s."), ETHERSFILE },
317 { '1', OPT_DBUS, NULL, gettext_noop("Enable the DBus interface for setting upstream servers, etc."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000318 { '2', ARG_DUP, "<interface>", gettext_noop("Do not provide DHCP on this interface, only provide DNS."), NULL },
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100319 { '3', ARG_DUP, "[=tag:<tag>]...", gettext_noop("Enable dynamic address allocation for bootp."), NULL },
320 { '4', ARG_DUP, "set:<tag>,<mac address>", gettext_noop("Map MAC address (with wildcards) to option set."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000321 { LOPT_BRIDGE, ARG_DUP, "<iface>,<alias>..", gettext_noop("Treat DHCP requests on aliases as arriving from interface."), NULL },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100322 { '5', OPT_NO_PING, NULL, gettext_noop("Disable ICMP echo address checking in the DHCP server."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000323 { '6', ARG_ONE, "<path>", gettext_noop("Shell script to run on DHCP lease creation and destruction."), NULL },
324 { LOPT_LUASCRIPT, ARG_DUP, "path", gettext_noop("Lua script to run on DHCP lease creation and destruction."), NULL },
325 { LOPT_SCRIPTUSR, ARG_ONE, "<username>", gettext_noop("Run lease-change scripts as this user."), NULL },
326 { '7', ARG_DUP, "<path>", gettext_noop("Read configuration from all the files in this directory."), NULL },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100327 { '8', ARG_ONE, "<facilty>|<file>", gettext_noop("Log to this syslog facility or file. (defaults to DAEMON)"), NULL },
328 { '9', OPT_LEASE_RO, NULL, gettext_noop("Do not use leasefile."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000329 { '0', ARG_ONE, "<integer>", gettext_noop("Maximum number of concurrent DNS queries. (defaults to %s)"), "!" },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100330 { LOPT_RELOAD, OPT_RELOAD, NULL, gettext_noop("Clear DNS cache when reloading %s."), RESOLVFILE },
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100331 { LOPT_NO_NAMES, ARG_DUP, "[=tag:<tag>]...", gettext_noop("Ignore hostnames provided by DHCP clients."), NULL },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100332 { LOPT_OVERRIDE, OPT_NO_OVERRIDE, NULL, gettext_noop("Do NOT reuse filename and server fields for extra DHCP options."), NULL },
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100333 { LOPT_TFTP, ARG_DUP, "[=<interface>]", gettext_noop("Enable integrated read-only TFTP server."), NULL },
334 { LOPT_PREFIX, ARG_ONE, "<dir>[,<iface>]", gettext_noop("Export files by TFTP only from the specified subtree."), NULL },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100335 { LOPT_APREF, OPT_TFTP_APREF, NULL, gettext_noop("Add client IP address to tftp-root."), NULL },
336 { LOPT_SECURE, OPT_TFTP_SECURE, NULL, gettext_noop("Allow access only to files owned by the user running dnsmasq."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000337 { LOPT_TFTP_MAX, ARG_ONE, "<integer>", gettext_noop("Maximum number of conncurrent TFTP transfers (defaults to %s)."), "#" },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100338 { LOPT_NOBLOCK, OPT_TFTP_NOBLOCK, NULL, gettext_noop("Disable the TFTP blocksize extension."), NULL },
339 { LOPT_TFTPPORTS, ARG_ONE, "<start>,<end>", gettext_noop("Ephemeral port range for use by TFTP transfers."), NULL },
340 { LOPT_LOG_OPTS, OPT_LOG_OPTS, NULL, gettext_noop("Extra logging for DHCP."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000341 { LOPT_MAX_LOGS, ARG_ONE, "[=<integer>]", gettext_noop("Enable async. logging; optionally set queue length."), NULL },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100342 { LOPT_REBIND, OPT_NO_REBIND, NULL, gettext_noop("Stop DNS rebinding. Filter private IP ranges when resolving."), NULL },
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100343 { LOPT_LOC_REBND, OPT_LOCAL_REBIND, NULL, gettext_noop("Allow rebinding of 127.0.0.0/8, for RBL servers."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000344 { LOPT_NO_REBIND, ARG_DUP, "/<domain>/", gettext_noop("Inhibit DNS-rebind protection on this domain."), NULL },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100345 { LOPT_NOLAST, OPT_ALL_SERVERS, NULL, gettext_noop("Always perform DNS queries to all servers."), NULL },
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100346 { LOPT_MATCH, ARG_DUP, "set:<tag>,<optspec>", gettext_noop("Set tag if client includes matching option in request."), NULL },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100347 { LOPT_ALTPORT, ARG_ONE, "[=<ports>]", gettext_noop("Use alternative ports for DHCP."), NULL },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100348 { LOPT_NAPTR, ARG_DUP, "<name>,<naptr>", gettext_noop("Specify NAPTR DNS record."), NULL },
349 { LOPT_MINPORT, ARG_ONE, "<port>", gettext_noop("Specify lowest port available for DNS query transmission."), NULL },
Simon Kelley9009d742008-11-14 20:04:27 +0000350 { LOPT_DHCP_FQDN, OPT_DHCP_FQDN, NULL, gettext_noop("Use only fully qualified domain names for DHCP clients."), NULL },
Simon Kelley8ecfaa42012-01-07 15:29:48 +0000351 { LOPT_GEN_NAMES, ARG_DUP, "[=tag:<tag>]", gettext_noop("Generate hostnames based on MAC address for nameless clients."), NULL},
352 { LOPT_PROXY, ARG_DUP, "[=<ipaddr>]...", gettext_noop("Use these DHCP relays as full proxies."), NULL },
Simon Kelley9009d742008-11-14 20:04:27 +0000353 { LOPT_CNAME, ARG_DUP, "<alias>,<target>", gettext_noop("Specify alias name for LOCAL DNS name."), NULL },
Simon Kelley7622fc02009-06-04 20:32:05 +0100354 { LOPT_PXE_PROMT, ARG_DUP, "<prompt>,[<timeout>]", gettext_noop("Prompt to send to PXE clients."), NULL },
355 { LOPT_PXE_SERV, ARG_DUP, "<service>", gettext_noop("Boot service for PXE menu."), NULL },
356 { LOPT_TEST, 0, NULL, gettext_noop("Check configuration syntax."), NULL },
Simon Kelley7de060b2011-08-26 17:24:52 +0100357 { LOPT_ADD_MAC, OPT_ADD_MAC, NULL, gettext_noop("Add requestor's MAC address to forwarded DNS queries."), NULL },
358 { LOPT_DNSSEC, OPT_DNSSEC, NULL, gettext_noop("Proxy DNSSEC validation results from upstream nameservers."), NULL },
359 { LOPT_INCR_ADDR, OPT_CONSEC_ADDR, NULL, gettext_noop("Attempt to allocate sequential IP addresses to DHCP clients."), NULL },
360 { LOPT_CONNTRACK, OPT_CONNTRACK, NULL, gettext_noop("Copy connection-track mark from queries to upstream connections."), NULL },
Simon Kelleyc72daea2012-01-05 21:33:27 +0000361 { LOPT_FQDN, OPT_FQDN_UPDATE, NULL, gettext_noop("Allow DHCP clients to do their own DDNS updates."), NULL },
Simon Kelley1a6bca82008-07-11 11:11:42 +0100362 { 0, 0, NULL, NULL, NULL }
Simon Kelleyb8187c82005-11-26 21:46:27 +0000363};
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000364
Simon Kelley7622fc02009-06-04 20:32:05 +0100365#ifdef HAVE_DHCP
Simon Kelley28866e92011-02-14 20:19:14 +0000366
Simon Kelley4cb1b322012-02-06 14:30:41 +0000367#define OT_ADDR_LIST 0x8000
368#define OT_RFC1035_NAME 0x4000
369#define OT_INTERNAL 0x2000
370#define OT_NAME 0x1000
371#define OT_CSTRING 0x0800
Simon Kelleyf2621c72007-04-29 19:47:21 +0100372
Simon Kelley4cb1b322012-02-06 14:30:41 +0000373static const struct opttab_t {
Simon Kelleyf2621c72007-04-29 19:47:21 +0100374 char *name;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000375 u16 val, size;
Simon Kelleyf2621c72007-04-29 19:47:21 +0100376} opttab[] = {
377 { "netmask", 1, OT_ADDR_LIST },
378 { "time-offset", 2, 4 },
379 { "router", 3, OT_ADDR_LIST },
380 { "dns-server", 6, OT_ADDR_LIST },
381 { "log-server", 7, OT_ADDR_LIST },
382 { "lpr-server", 9, OT_ADDR_LIST },
Simon Kelley7622fc02009-06-04 20:32:05 +0100383 { "hostname", 12, OT_INTERNAL | OT_NAME },
Simon Kelleyf2621c72007-04-29 19:47:21 +0100384 { "boot-file-size", 13, 2 },
Simon Kelley7622fc02009-06-04 20:32:05 +0100385 { "domain-name", 15, OT_NAME },
Simon Kelleyf2621c72007-04-29 19:47:21 +0100386 { "swap-server", 16, OT_ADDR_LIST },
Simon Kelley28866e92011-02-14 20:19:14 +0000387 { "root-path", 17, OT_NAME },
388 { "extension-path", 18, OT_NAME },
Simon Kelleyf2621c72007-04-29 19:47:21 +0100389 { "ip-forward-enable", 19, 1 },
390 { "non-local-source-routing", 20, 1 },
391 { "policy-filter", 21, OT_ADDR_LIST },
392 { "max-datagram-reassembly", 22, 2 },
393 { "default-ttl", 23, 1 },
394 { "mtu", 26, 2 },
395 { "all-subnets-local", 27, 1 },
Simon Kelley7622fc02009-06-04 20:32:05 +0100396 { "broadcast", 28, OT_INTERNAL | OT_ADDR_LIST },
Simon Kelleyf2621c72007-04-29 19:47:21 +0100397 { "router-discovery", 31, 1 },
398 { "router-solicitation", 32, OT_ADDR_LIST },
399 { "static-route", 33, OT_ADDR_LIST },
400 { "trailer-encapsulation", 34, 1 },
401 { "arp-timeout", 35, 4 },
402 { "ethernet-encap", 36, 1 },
403 { "tcp-ttl", 37, 1 },
404 { "tcp-keepalive", 38, 4 },
Simon Kelley28866e92011-02-14 20:19:14 +0000405 { "nis-domain", 40, OT_NAME },
Simon Kelleyf2621c72007-04-29 19:47:21 +0100406 { "nis-server", 41, OT_ADDR_LIST },
407 { "ntp-server", 42, OT_ADDR_LIST },
408 { "vendor-encap", 43, OT_INTERNAL },
409 { "netbios-ns", 44, OT_ADDR_LIST },
410 { "netbios-dd", 45, OT_ADDR_LIST },
411 { "netbios-nodetype", 46, 1 },
412 { "netbios-scope", 47, 0 },
413 { "x-windows-fs", 48, OT_ADDR_LIST },
414 { "x-windows-dm", 49, OT_ADDR_LIST },
Simon Kelley7622fc02009-06-04 20:32:05 +0100415 { "requested-address", 50, OT_INTERNAL | OT_ADDR_LIST },
Simon Kelleyf2621c72007-04-29 19:47:21 +0100416 { "lease-time", 51, OT_INTERNAL },
417 { "option-overload", 52, OT_INTERNAL },
418 { "message-type", 53, OT_INTERNAL, },
Simon Kelley7622fc02009-06-04 20:32:05 +0100419 { "server-identifier", 54, OT_INTERNAL | OT_ADDR_LIST },
Simon Kelleyf2621c72007-04-29 19:47:21 +0100420 { "parameter-request", 55, OT_INTERNAL },
421 { "message", 56, OT_INTERNAL },
422 { "max-message-size", 57, OT_INTERNAL },
423 { "T1", 58, OT_INTERNAL },
424 { "T2", 59, OT_INTERNAL },
425 { "vendor-class", 60, 0 },
Simon Kelley4cb1b322012-02-06 14:30:41 +0000426 { "client-id", 61, OT_INTERNAL },
Simon Kelley28866e92011-02-14 20:19:14 +0000427 { "nis+-domain", 64, OT_NAME },
Simon Kelley9009d742008-11-14 20:04:27 +0000428 { "nis+-server", 65, OT_ADDR_LIST },
Simon Kelley28866e92011-02-14 20:19:14 +0000429 { "tftp-server", 66, OT_NAME },
430 { "bootfile-name", 67, OT_NAME },
Simon Kelleyf2621c72007-04-29 19:47:21 +0100431 { "mobile-ip-home", 68, OT_ADDR_LIST },
432 { "smtp-server", 69, OT_ADDR_LIST },
433 { "pop3-server", 70, OT_ADDR_LIST },
434 { "nntp-server", 71, OT_ADDR_LIST },
435 { "irc-server", 74, OT_ADDR_LIST },
436 { "user-class", 77, 0 },
437 { "FQDN", 81, OT_INTERNAL },
438 { "agent-id", 82, OT_INTERNAL },
Simon Kelley73a08a22009-02-05 20:28:08 +0000439 { "client-arch", 93, 2 },
440 { "client-interface-id", 94, 0 },
441 { "client-machine-id", 97, 0 },
Simon Kelleyf2621c72007-04-29 19:47:21 +0100442 { "subnet-select", 118, OT_INTERNAL },
Simon Kelley28866e92011-02-14 20:19:14 +0000443 { "domain-search", 119, OT_RFC1035_NAME },
Simon Kelleyf2621c72007-04-29 19:47:21 +0100444 { "sip-server", 120, 0 },
445 { "classless-static-route", 121, 0 },
Simon Kelley316e2732010-01-22 20:16:09 +0000446 { "vendor-id-encap", 125, 0 },
Simon Kelley1f15b812009-10-13 17:49:32 +0100447 { "server-ip-address", 255, OT_ADDR_LIST }, /* special, internal only, sets siaddr */
Simon Kelleyf2621c72007-04-29 19:47:21 +0100448 { NULL, 0, 0 }
449};
450
Simon Kelley4cb1b322012-02-06 14:30:41 +0000451#ifdef HAVE_DHCP6
452static const struct opttab_t opttab6[] = {
453 { "client-id", 1, OT_INTERNAL },
454 { "server-id", 2, OT_INTERNAL },
455 { "ia-na", 3, OT_INTERNAL },
456 { "ia-ta", 4, OT_INTERNAL },
457 { "iaaddr", 5, OT_INTERNAL },
458 { "oro", 6, OT_INTERNAL },
459 { "preference", 7, OT_INTERNAL },
460 { "unicast", 12, OT_INTERNAL },
461 { "status-code", 13, OT_INTERNAL },
462 { "rapid-commit", 14, OT_INTERNAL },
463 { "user-class", 15, OT_INTERNAL | OT_CSTRING },
464 { "vendor-class", 16, OT_INTERNAL | OT_CSTRING },
465 { "vendor-opts", 17, OT_INTERNAL },
466 { "sip-server-domain", 21, OT_RFC1035_NAME },
467 { "sip-server", 22, OT_ADDR_LIST },
468 { "dns-server", 23, OT_ADDR_LIST },
469 { "domain-search", 24, OT_RFC1035_NAME },
470 { "nis-server", 27, OT_ADDR_LIST },
471 { "nis+-server", 28, OT_ADDR_LIST },
472 { "nis-domain", 29, OT_RFC1035_NAME },
473 { "nis+-domain", 30, OT_RFC1035_NAME },
474 { "sntp-server", 31, OT_ADDR_LIST },
475 { "FQDN", 39, OT_INTERNAL | OT_RFC1035_NAME },
476 { "ntp-server", 56, OT_ADDR_LIST },
477 { "bootfile-url", 59, OT_NAME },
478 { "bootfile-param", 60, OT_CSTRING },
479 { NULL, 0, 0 }
480};
481#endif
Simon Kelleyf2621c72007-04-29 19:47:21 +0100482
Simon Kelley4cb1b322012-02-06 14:30:41 +0000483
484char *option_string(int prot, unsigned int opt, unsigned char *val, int opt_len, char *buf, int buf_len)
485{
486 int o, i, j, nodecode = 0;
487 const struct opttab_t *ot = opttab;
488
489#ifdef HAVE_DHCP6
490 if (prot == AF_INET6)
491 ot = opttab6;
492#endif
493
494 for (o = 0; ot[o].name; o++)
495 if (ot[o].val == opt)
Simon Kelley7622fc02009-06-04 20:32:05 +0100496 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000497 if (buf)
498 {
499 memset(buf, 0, buf_len);
500
501 if (ot[o].size & OT_ADDR_LIST)
502 {
503 struct all_addr addr;
504 int addr_len = INADDRSZ;
505
506#ifdef HAVE_DHCP6
507 if (prot == AF_INET6)
508 addr_len = IN6ADDRSZ;
509#endif
510 for (buf[0]= 0, i = 0; i <= opt_len - addr_len; i += addr_len)
511 {
512 if (i != 0)
513 strncat(buf, ", ", buf_len - strlen(buf));
514 /* align */
515 memcpy(&addr, &val[i], addr_len);
516 inet_ntop(prot, &val[i], daemon->addrbuff, ADDRSTRLEN);
517 strncat(buf, daemon->addrbuff, buf_len - strlen(buf));
518 }
519 }
520 else if (ot[o].size & OT_NAME)
521 for (i = 0, j = 0; i < opt_len && j < buf_len ; i++)
522 {
523 char c = val[i];
524 if (isprint((int)c))
525 buf[j++] = c;
526 }
527#ifdef HAVE_DHCP6
528 /* We don't handle compressed rfc1035 names, so no good in IPv4 land */
529 else if ((ot[o].size & OT_RFC1035_NAME) && prot == AF_INET6)
530 {
531 i = 0, j = 0;
532 while (i < opt_len && val[i] != 0)
533 {
534 int k, l = i + val[i] + 1;
535 for (k = i + 1; k < opt_len && k < l && j < buf_len ; k++)
536 {
537 char c = val[k];
538 if (isprint((int)c))
539 buf[j++] = c;
540 }
541 i = l;
542 if (val[i] != 0 && j < buf_len)
543 buf[j++] = '.';
544 }
545 }
Simon Kelleyd74942a2012-02-07 20:51:56 +0000546 else if ((ot[o].size & OT_CSTRING))
547 {
548 int k, len;
549 unsigned char *p;
550
551 i = 0, j = 0;
552 while (1)
553 {
554 p = &val[i];
555 GETSHORT(len, p);
556 for (k = 0; k < len && j < buf_len; k++)
557 {
558 char c = *p++;
559 if (isprint((int)c))
560 buf[j++] = c;
561 }
562 i += len +2;
563 if (i >= opt_len)
564 break;
565
566 if (j < buf_len)
567 buf[j++] = ',';
568 }
569 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000570#endif
571 else
572 nodecode = 1;
573 }
574 break;
Simon Kelley7622fc02009-06-04 20:32:05 +0100575 }
Simon Kelleyf2621c72007-04-29 19:47:21 +0100576
Simon Kelley4cb1b322012-02-06 14:30:41 +0000577 if (buf && (!ot[o].name || nodecode))
578 {
579 int trunc = 0;
580 if (opt_len > 13)
581 {
582 trunc = 1;
583 opt_len = 13;
584 }
585 print_mac(buf, val, opt_len);
586 if (trunc)
587 strncat(buf, "...", buf_len - strlen(buf));
588
589
590 }
591
592 return ot[o].name ? ot[o].name : "";
593
Simon Kelleyf2621c72007-04-29 19:47:21 +0100594}
595
Simon Kelley7622fc02009-06-04 20:32:05 +0100596#endif
597
Simon Kelley3d8df262005-08-29 12:19:27 +0100598/* We hide metacharaters in quoted strings by mapping them into the ASCII control
Simon Kelleyf2621c72007-04-29 19:47:21 +0100599 character space. Note that the \0, \t \b \r \033 and \n characters are carefully placed in the
Simon Kelley3d8df262005-08-29 12:19:27 +0100600 following sequence so that they map to themselves: it is therefore possible to call
601 unhide_metas repeatedly on string without breaking things.
Simon Kelley824af852008-02-12 20:43:05 +0000602 The transformation gets undone by opt_canonicalise, atoi_check and opt_string_alloc, and a
Simon Kelleyf2621c72007-04-29 19:47:21 +0100603 couple of other places.
604 Note that space is included here so that
605 --dhcp-option=3, string
606 has five characters, whilst
607 --dhcp-option=3," string"
608 has six.
609*/
Simon Kelley3d8df262005-08-29 12:19:27 +0100610
Simon Kelleyf2621c72007-04-29 19:47:21 +0100611static const char meta[] = "\000123456 \b\t\n78\r90abcdefABCDE\033F:,.";
Simon Kelley3d8df262005-08-29 12:19:27 +0100612
613static char hide_meta(char c)
614{
615 unsigned int i;
616
617 for (i = 0; i < (sizeof(meta) - 1); i++)
618 if (c == meta[i])
619 return (char)i;
620
621 return c;
622}
623
624static char unhide_meta(char cr)
625{
626 unsigned int c = cr;
627
628 if (c < (sizeof(meta) - 1))
629 cr = meta[c];
630
631 return cr;
632}
633
634static void unhide_metas(char *cp)
635{
636 if (cp)
637 for(; *cp; cp++)
638 *cp = unhide_meta(*cp);
639}
640
Simon Kelley824af852008-02-12 20:43:05 +0000641static void *opt_malloc(size_t size)
642{
643 void *ret;
644
645 if (mem_recover)
646 {
647 ret = whine_malloc(size);
648 if (!ret)
649 longjmp(mem_jmp, 1);
650 }
651 else
652 ret = safe_malloc(size);
653
654 return ret;
655}
656
657static char *opt_string_alloc(char *cp)
Simon Kelley3d8df262005-08-29 12:19:27 +0100658{
659 char *ret = NULL;
660
661 if (cp && strlen(cp) != 0)
662 {
Simon Kelley824af852008-02-12 20:43:05 +0000663 ret = opt_malloc(strlen(cp)+1);
Simon Kelley3d8df262005-08-29 12:19:27 +0100664 strcpy(ret, cp);
665
666 /* restore hidden metachars */
667 unhide_metas(ret);
668 }
669
670 return ret;
671}
672
Simon Kelley3d8df262005-08-29 12:19:27 +0100673
Simon Kelleyf2621c72007-04-29 19:47:21 +0100674/* find next comma, split string with zero and eliminate spaces.
675 return start of string following comma */
Simon Kelley73a08a22009-02-05 20:28:08 +0000676
677static char *split_chr(char *s, char c)
Simon Kelleyf2621c72007-04-29 19:47:21 +0100678{
679 char *comma, *p;
680
Simon Kelley73a08a22009-02-05 20:28:08 +0000681 if (!s || !(comma = strchr(s, c)))
Simon Kelleyf2621c72007-04-29 19:47:21 +0100682 return NULL;
683
684 p = comma;
685 *comma = ' ';
686
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100687 for (; *comma == ' '; comma++);
Simon Kelleyf2621c72007-04-29 19:47:21 +0100688
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100689 for (; (p >= s) && *p == ' '; p--)
Simon Kelleyf2621c72007-04-29 19:47:21 +0100690 *p = 0;
691
692 return comma;
Simon Kelley3d8df262005-08-29 12:19:27 +0100693}
694
Simon Kelley73a08a22009-02-05 20:28:08 +0000695static char *split(char *s)
696{
697 return split_chr(s, ',');
698}
699
Simon Kelley1f15b812009-10-13 17:49:32 +0100700static char *canonicalise_opt(char *s)
Simon Kelley3d8df262005-08-29 12:19:27 +0100701{
Simon Kelley1f15b812009-10-13 17:49:32 +0100702 char *ret;
703 int nomem;
704
Simon Kelley3d8df262005-08-29 12:19:27 +0100705 if (!s)
706 return 0;
707
708 unhide_metas(s);
Simon Kelley1f15b812009-10-13 17:49:32 +0100709 if (!(ret = canonicalise(s, &nomem)) && nomem)
710 {
711 if (mem_recover)
712 longjmp(mem_jmp, 1);
713 else
714 die(_("could not get memory"), NULL, EC_NOMEM);
715 }
716
717 return ret;
Simon Kelley3d8df262005-08-29 12:19:27 +0100718}
719
720static int atoi_check(char *a, int *res)
721{
722 char *p;
723
724 if (!a)
725 return 0;
726
727 unhide_metas(a);
728
729 for (p = a; *p; p++)
730 if (*p < '0' || *p > '9')
731 return 0;
732
733 *res = atoi(a);
734 return 1;
735}
736
Simon Kelley1ad24ae2008-07-20 20:22:50 +0100737static int atoi_check16(char *a, int *res)
738{
739 if (!(atoi_check(a, res)) ||
740 *res < 0 ||
741 *res > 0xffff)
742 return 0;
743
744 return 1;
745}
746
Simon Kelley5aabfc72007-08-29 11:24:47 +0100747static void add_txt(char *name, char *txt)
Simon Kelley0a852542005-03-23 20:28:59 +0000748{
749 size_t len = strlen(txt);
Simon Kelley824af852008-02-12 20:43:05 +0000750 struct txt_record *r = opt_malloc(sizeof(struct txt_record));
Simon Kelley0a852542005-03-23 20:28:59 +0000751
Simon Kelley824af852008-02-12 20:43:05 +0000752 r->name = opt_string_alloc(name);
Simon Kelley0a852542005-03-23 20:28:59 +0000753 r->next = daemon->txt;
754 daemon->txt = r;
755 r->class = C_CHAOS;
Simon Kelley824af852008-02-12 20:43:05 +0000756 r->txt = opt_malloc(len+1);
Simon Kelley0a852542005-03-23 20:28:59 +0000757 r->len = len+1;
758 *(r->txt) = len;
759 memcpy((r->txt)+1, txt, len);
760}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000761
Simon Kelley849a8352006-06-09 21:02:31 +0100762static void do_usage(void)
763{
764 char buff[100];
Simon Kelley832af0b2007-01-21 20:01:28 +0000765 int i, j;
766
767 struct {
768 char handle;
769 int val;
770 } tab[] = {
771 { '$', CACHESIZ },
772 { '*', EDNS_PKTSZ },
773 { '&', MAXLEASES },
774 { '!', FTABSIZ },
775 { '#', TFTP_MAX_CONNECTIONS },
776 { '\0', 0 }
777 };
Simon Kelley849a8352006-06-09 21:02:31 +0100778
779 printf(_("Usage: dnsmasq [options]\n\n"));
780#ifndef HAVE_GETOPT_LONG
781 printf(_("Use short options only on the command line.\n"));
782#endif
Simon Kelley1a6bca82008-07-11 11:11:42 +0100783 printf(_("Valid options are:\n"));
Simon Kelley849a8352006-06-09 21:02:31 +0100784
Simon Kelley1a6bca82008-07-11 11:11:42 +0100785 for (i = 0; usage[i].opt != 0; i++)
Simon Kelley849a8352006-06-09 21:02:31 +0100786 {
Simon Kelley1a6bca82008-07-11 11:11:42 +0100787 char *desc = usage[i].flagdesc;
788 char *eq = "=";
789
790 if (!desc || *desc == '[')
791 eq = "";
792
793 if (!desc)
794 desc = "";
795
796 for ( j = 0; opts[j].name; j++)
797 if (opts[j].val == usage[i].opt)
798 break;
799 if (usage[i].opt < 256)
800 sprintf(buff, "-%c, ", usage[i].opt);
801 else
802 sprintf(buff, " ");
803
804 sprintf(buff+4, "--%s%s%s", opts[j].name, eq, desc);
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100805 printf("%-40.40s", buff);
Simon Kelley1a6bca82008-07-11 11:11:42 +0100806
Simon Kelley849a8352006-06-09 21:02:31 +0100807 if (usage[i].arg)
808 {
Simon Kelley832af0b2007-01-21 20:01:28 +0000809 strcpy(buff, usage[i].arg);
810 for (j = 0; tab[j].handle; j++)
811 if (tab[j].handle == *(usage[i].arg))
812 sprintf(buff, "%d", tab[j].val);
Simon Kelley849a8352006-06-09 21:02:31 +0100813 }
Simon Kelley849a8352006-06-09 21:02:31 +0100814 printf(_(usage[i].desc), buff);
815 printf("\n");
816 }
817}
818
Simon Kelley7622fc02009-06-04 20:32:05 +0100819#ifdef HAVE_DHCP
Simon Kelleyf2621c72007-04-29 19:47:21 +0100820static void display_opts(void)
821{
822 int i;
823
824 printf(_("Known DHCP options:\n"));
825
826 for (i = 0; opttab[i].name; i++)
Simon Kelley1f15b812009-10-13 17:49:32 +0100827 if (!(opttab[i].size & OT_INTERNAL))
Simon Kelleyf2621c72007-04-29 19:47:21 +0100828 printf("%3d %s\n", opttab[i].val, opttab[i].name);
829}
830
Simon Kelley4cb1b322012-02-06 14:30:41 +0000831#ifdef HAVE_DHCP6
832static void display_opts6(void)
833{
834 int i;
835 printf(_("Known DHCPv6 options:\n"));
836
837 for (i = 0; opttab6[i].name; i++)
838 if (!(opttab6[i].size & OT_INTERNAL))
839 printf("%3d %s\n", opttab6[i].val, opttab6[i].name);
840}
841#endif
842
843
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100844static int is_tag_prefix(char *arg)
845{
846 if (arg && (strstr(arg, "net:") == arg || strstr(arg, "tag:") == arg))
847 return 1;
848
849 return 0;
850}
851
852static char *set_prefix(char *arg)
853{
854 if (strstr(arg, "set:") == arg)
855 return arg+4;
856
857 return arg;
858}
859
Simon Kelley832af0b2007-01-21 20:01:28 +0000860/* This is too insanely large to keep in-line in the switch */
Simon Kelley824af852008-02-12 20:43:05 +0000861static char *parse_dhcp_opt(char *arg, int flags)
Simon Kelley832af0b2007-01-21 20:01:28 +0000862{
Simon Kelley824af852008-02-12 20:43:05 +0000863 struct dhcp_opt *new = opt_malloc(sizeof(struct dhcp_opt));
Simon Kelley832af0b2007-01-21 20:01:28 +0000864 char lenchar = 0, *cp;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000865 int i, addrs, digs, is_addr, is_addr6, is_hex, is_dec, is_string, dots;
Simon Kelleyf2621c72007-04-29 19:47:21 +0100866 char *comma = NULL, *problem = NULL;
867 struct dhcp_netid *np = NULL;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000868 u16 opt_len = 0;
869 int is6 = 0;
Simon Kelley832af0b2007-01-21 20:01:28 +0000870
871 new->len = 0;
Simon Kelley824af852008-02-12 20:43:05 +0000872 new->flags = flags;
Simon Kelley832af0b2007-01-21 20:01:28 +0000873 new->netid = NULL;
874 new->val = NULL;
Simon Kelleyf2621c72007-04-29 19:47:21 +0100875 new->opt = 0;
Simon Kelley832af0b2007-01-21 20:01:28 +0000876
Simon Kelleyf2621c72007-04-29 19:47:21 +0100877 while (arg)
Simon Kelley832af0b2007-01-21 20:01:28 +0000878 {
Simon Kelleyf2621c72007-04-29 19:47:21 +0100879 comma = split(arg);
880
881 for (cp = arg; *cp; cp++)
882 if (*cp < '0' || *cp > '9')
Simon Kelley832af0b2007-01-21 20:01:28 +0000883 break;
Simon Kelleyf2621c72007-04-29 19:47:21 +0100884
885 if (!*cp)
886 {
887 new->opt = atoi(arg);
888 opt_len = 0;
889 break;
890 }
891
892 if (strstr(arg, "option:") == arg)
893 {
894 for (i = 0; opttab[i].name; i++)
Simon Kelley1f15b812009-10-13 17:49:32 +0100895 if (!(opttab[i].size & OT_INTERNAL) &&
Simon Kelleyf2621c72007-04-29 19:47:21 +0100896 strcasecmp(opttab[i].name, arg+7) == 0)
897 {
898 new->opt = opttab[i].val;
899 opt_len = opttab[i].size;
900 break;
901 }
902 /* option:<optname> must follow tag and vendor string. */
903 break;
904 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000905#ifdef HAVE_DHCP6
906 else if (strstr(arg, "option6:") == arg)
907 {
908 for (cp = arg+8; *cp; cp++)
909 if (*cp < '0' || *cp > '9')
910 break;
911
912 if (!*cp)
913 {
914 new->opt = atoi(arg+8);
915 opt_len = 0;
916 }
917 else
918 for (i = 0; opttab6[i].name; i++)
919 if (!(opttab6[i].size & OT_INTERNAL) &&
920 strcasecmp(opttab6[i].name, arg+8) == 0)
921 {
922 new->opt = opttab6[i].val;
923 opt_len = opttab6[i].size;
924 break;
925 }
926 /* option6:<opt>|<optname> must follow tag and vendor string. */
927 is6 = 1;
928 break;
929 }
930#endif
Simon Kelleyf2621c72007-04-29 19:47:21 +0100931 else if (strstr(arg, "vendor:") == arg)
932 {
Simon Kelley73a08a22009-02-05 20:28:08 +0000933 new->u.vendor_class = (unsigned char *)opt_string_alloc(arg+7);
934 new->flags |= DHOPT_VENDOR;
935 }
936 else if (strstr(arg, "encap:") == arg)
937 {
938 new->u.encap = atoi(arg+6);
Simon Kelleyf2621c72007-04-29 19:47:21 +0100939 new->flags |= DHOPT_ENCAPSULATE;
940 }
Simon Kelley316e2732010-01-22 20:16:09 +0000941 else if (strstr(arg, "vi-encap:") == arg)
942 {
943 new->u.encap = atoi(arg+9);
944 new->flags |= DHOPT_RFC3925;
945 if (flags == DHOPT_MATCH)
946 {
947 new->opt = 1; /* avoid error below */
948 break;
949 }
950 }
Simon Kelleyf2621c72007-04-29 19:47:21 +0100951 else
952 {
Simon Kelley824af852008-02-12 20:43:05 +0000953 new->netid = opt_malloc(sizeof (struct dhcp_netid));
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100954 /* allow optional "net:" or "tag:" for consistency */
955 if (is_tag_prefix(arg))
Simon Kelley824af852008-02-12 20:43:05 +0000956 new->netid->net = opt_string_alloc(arg+4);
Simon Kelleyf2621c72007-04-29 19:47:21 +0100957 else
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100958 new->netid->net = opt_string_alloc(set_prefix(arg));
Simon Kelleyf2621c72007-04-29 19:47:21 +0100959 new->netid->next = np;
960 np = new->netid;
961 }
962
963 arg = comma;
Simon Kelley832af0b2007-01-21 20:01:28 +0000964 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000965
966#ifdef HAVE_DHCP6
967 if (is6)
968 {
969 if (new->flags & (DHOPT_VENDOR | DHOPT_ENCAPSULATE))
970 problem = _("unsupported encapsulation for IPv6 option");
971
972 if (opt_len == 0 &&
973 !(new->flags & DHOPT_RFC3925))
974 for (i = 0; opttab6[i].name; i++)
975 if (new->opt == opttab6[i].val)
976 {
977 opt_len = opttab6[i].size;
978 if (opt_len & OT_INTERNAL)
979 opt_len = 0;
980 break;
981 }
982 }
983 else
984#endif
985 if (opt_len == 0 &&
986 !(new->flags & (DHOPT_VENDOR | DHOPT_ENCAPSULATE | DHOPT_RFC3925)))
987 for (i = 0; opttab[i].name; i++)
988 if (new->opt == opttab[i].val)
989 {
990 opt_len = opttab[i].size;
991 if (opt_len & OT_INTERNAL)
992 opt_len = 0;
993 break;
994 }
Simon Kelley28866e92011-02-14 20:19:14 +0000995
Simon Kelley316e2732010-01-22 20:16:09 +0000996 /* option may be missing with rfc3925 match */
Simon Kelleyf2621c72007-04-29 19:47:21 +0100997 if (new->opt == 0)
Simon Kelley832af0b2007-01-21 20:01:28 +0000998 problem = _("bad dhcp-option");
999 else if (comma)
1000 {
1001 /* characterise the value */
Simon Kelleyf2621c72007-04-29 19:47:21 +01001002 char c;
Simon Kelley28866e92011-02-14 20:19:14 +00001003 int found_dig = 0;
Simon Kelley4cb1b322012-02-06 14:30:41 +00001004 is_addr = is_addr6 = is_hex = is_dec = is_string = 1;
Simon Kelley832af0b2007-01-21 20:01:28 +00001005 addrs = digs = 1;
Simon Kelleyf2621c72007-04-29 19:47:21 +01001006 dots = 0;
1007 for (cp = comma; (c = *cp); cp++)
1008 if (c == ',')
Simon Kelley832af0b2007-01-21 20:01:28 +00001009 {
1010 addrs++;
1011 is_dec = is_hex = 0;
1012 }
Simon Kelleyf2621c72007-04-29 19:47:21 +01001013 else if (c == ':')
Simon Kelley832af0b2007-01-21 20:01:28 +00001014 {
1015 digs++;
1016 is_dec = is_addr = 0;
1017 }
Simon Kelleyf2621c72007-04-29 19:47:21 +01001018 else if (c == '/')
Simon Kelley832af0b2007-01-21 20:01:28 +00001019 {
Simon Kelley4cb1b322012-02-06 14:30:41 +00001020 is_addr6 = is_dec = is_hex = 0;
Simon Kelley832af0b2007-01-21 20:01:28 +00001021 if (cp == comma) /* leading / means a pathname */
1022 is_addr = 0;
1023 }
Simon Kelleyf2621c72007-04-29 19:47:21 +01001024 else if (c == '.')
1025 {
Simon Kelley4cb1b322012-02-06 14:30:41 +00001026 is_addr6 =is_dec = is_hex = 0;
Simon Kelleyf2621c72007-04-29 19:47:21 +01001027 dots++;
1028 }
1029 else if (c == '-')
Simon Kelley4cb1b322012-02-06 14:30:41 +00001030 is_hex = is_addr = is_addr6 = 0;
Simon Kelleyf2621c72007-04-29 19:47:21 +01001031 else if (c == ' ')
Simon Kelley832af0b2007-01-21 20:01:28 +00001032 is_dec = is_hex = 0;
Simon Kelleyf2621c72007-04-29 19:47:21 +01001033 else if (!(c >='0' && c <= '9'))
Simon Kelley832af0b2007-01-21 20:01:28 +00001034 {
1035 is_addr = 0;
1036 if (cp[1] == 0 && is_dec &&
Simon Kelleyf2621c72007-04-29 19:47:21 +01001037 (c == 'b' || c == 's' || c == 'i'))
Simon Kelley832af0b2007-01-21 20:01:28 +00001038 {
Simon Kelleyf2621c72007-04-29 19:47:21 +01001039 lenchar = c;
Simon Kelley832af0b2007-01-21 20:01:28 +00001040 *cp = 0;
1041 }
1042 else
1043 is_dec = 0;
Simon Kelleyf2621c72007-04-29 19:47:21 +01001044 if (!((c >='A' && c <= 'F') ||
Simon Kelley73a08a22009-02-05 20:28:08 +00001045 (c >='a' && c <= 'f') ||
1046 (c == '*' && (flags & DHOPT_MATCH))))
Simon Kelley4cb1b322012-02-06 14:30:41 +00001047 {
1048 is_hex = 0;
1049 if (c != '[' && c != ']')
1050 is_addr6 = 0;
1051 }
Simon Kelley832af0b2007-01-21 20:01:28 +00001052 }
Simon Kelley28866e92011-02-14 20:19:14 +00001053 else
1054 found_dig = 1;
Simon Kelleyf2621c72007-04-29 19:47:21 +01001055
Simon Kelley28866e92011-02-14 20:19:14 +00001056 if (!found_dig)
1057 is_dec = is_addr = 0;
Simon Kelley4cb1b322012-02-06 14:30:41 +00001058
Simon Kelleyf2621c72007-04-29 19:47:21 +01001059 /* We know that some options take addresses */
Simon Kelley7622fc02009-06-04 20:32:05 +01001060 if (opt_len & OT_ADDR_LIST)
Simon Kelleyf2621c72007-04-29 19:47:21 +01001061 {
1062 is_string = is_dec = is_hex = 0;
Simon Kelley4cb1b322012-02-06 14:30:41 +00001063
1064 if (!is6 && (!is_addr || dots == 0))
Simon Kelleyf2621c72007-04-29 19:47:21 +01001065 problem = _("bad IP address");
Simon Kelley4cb1b322012-02-06 14:30:41 +00001066
1067 if (is6 && !is_addr6)
1068 problem = _("bad IPv6 address");
Simon Kelleyf2621c72007-04-29 19:47:21 +01001069 }
Simon Kelley28866e92011-02-14 20:19:14 +00001070 /* or names */
Simon Kelley4cb1b322012-02-06 14:30:41 +00001071 else if (opt_len & (OT_NAME | OT_RFC1035_NAME | OT_CSTRING))
1072 is_addr6 = is_addr = is_dec = is_hex = 0;
1073
Simon Kelley832af0b2007-01-21 20:01:28 +00001074 if (is_hex && digs > 1)
1075 {
1076 new->len = digs;
Simon Kelley824af852008-02-12 20:43:05 +00001077 new->val = opt_malloc(new->len);
Simon Kelley73a08a22009-02-05 20:28:08 +00001078 parse_hex(comma, new->val, digs, (flags & DHOPT_MATCH) ? &new->u.wildcard_mask : NULL, NULL);
1079 new->flags |= DHOPT_HEX;
Simon Kelley832af0b2007-01-21 20:01:28 +00001080 }
1081 else if (is_dec)
1082 {
1083 int i, val = atoi(comma);
1084 /* assume numeric arg is 1 byte except for
1085 options where it is known otherwise.
1086 For vendor class option, we have to hack. */
Simon Kelleyf2621c72007-04-29 19:47:21 +01001087 if (opt_len != 0)
1088 new->len = opt_len;
1089 else if (val & 0xffff0000)
1090 new->len = 4;
1091 else if (val & 0xff00)
1092 new->len = 2;
1093 else
1094 new->len = 1;
1095
Simon Kelley832af0b2007-01-21 20:01:28 +00001096 if (lenchar == 'b')
1097 new->len = 1;
1098 else if (lenchar == 's')
1099 new->len = 2;
1100 else if (lenchar == 'i')
1101 new->len = 4;
Simon Kelleyf2621c72007-04-29 19:47:21 +01001102
Simon Kelley824af852008-02-12 20:43:05 +00001103 new->val = opt_malloc(new->len);
Simon Kelley832af0b2007-01-21 20:01:28 +00001104 for (i=0; i<new->len; i++)
1105 new->val[i] = val>>((new->len - i - 1)*8);
1106 }
Simon Kelley4cb1b322012-02-06 14:30:41 +00001107 else if (is_addr && !is6)
Simon Kelley832af0b2007-01-21 20:01:28 +00001108 {
1109 struct in_addr in;
1110 unsigned char *op;
1111 char *slash;
1112 /* max length of address/subnet descriptor is five bytes,
1113 add one for the option 120 enc byte too */
Simon Kelley824af852008-02-12 20:43:05 +00001114 new->val = op = opt_malloc((5 * addrs) + 1);
Simon Kelley6b010842007-02-12 20:32:07 +00001115 new->flags |= DHOPT_ADDR;
1116
Simon Kelley572b41e2011-02-18 18:11:18 +00001117 if (!(new->flags & (DHOPT_ENCAPSULATE | DHOPT_VENDOR | DHOPT_RFC3925)) &&
1118 new->opt == OPTION_SIP_SERVER)
Simon Kelley832af0b2007-01-21 20:01:28 +00001119 {
Simon Kelley6b010842007-02-12 20:32:07 +00001120 *(op++) = 1; /* RFC 3361 "enc byte" */
1121 new->flags &= ~DHOPT_ADDR;
Simon Kelley832af0b2007-01-21 20:01:28 +00001122 }
1123 while (addrs--)
1124 {
1125 cp = comma;
Simon Kelleyf2621c72007-04-29 19:47:21 +01001126 comma = split(cp);
Simon Kelley73a08a22009-02-05 20:28:08 +00001127 slash = split_chr(cp, '/');
Simon Kelley832af0b2007-01-21 20:01:28 +00001128 in.s_addr = inet_addr(cp);
1129 if (!slash)
1130 {
1131 memcpy(op, &in, INADDRSZ);
1132 op += INADDRSZ;
1133 }
1134 else
1135 {
1136 unsigned char *p = (unsigned char *)&in;
1137 int netsize = atoi(slash);
1138 *op++ = netsize;
1139 if (netsize > 0)
1140 *op++ = *p++;
1141 if (netsize > 8)
1142 *op++ = *p++;
1143 if (netsize > 16)
1144 *op++ = *p++;
1145 if (netsize > 24)
1146 *op++ = *p++;
1147 new->flags &= ~DHOPT_ADDR; /* cannot re-write descriptor format */
1148 }
1149 }
1150 new->len = op - new->val;
1151 }
Simon Kelley4cb1b322012-02-06 14:30:41 +00001152 else if (is_addr6 && is6)
1153 {
1154 unsigned char *op;
1155 new->val = op = opt_malloc(16 * addrs);
1156 new->flags |= DHOPT_ADDR6;
1157 while (addrs--)
1158 {
1159 cp = comma;
1160 comma = split(cp);
1161
1162 /* check for [1234::7] */
1163 if (*cp == '[')
1164 cp++;
1165 if (strlen(cp) > 1 && cp[strlen(cp)-1] == ']')
1166 cp[strlen(cp)-1] = 0;
1167
1168 if (inet_pton(AF_INET6, cp, op))
1169 {
1170 op += IN6ADDRSZ;
1171 continue;
1172 }
1173
1174 problem = _("bad IPv6 address");
1175 }
1176 new->len = op - new->val;
1177 }
Simon Kelleyf2621c72007-04-29 19:47:21 +01001178 else if (is_string)
Simon Kelley832af0b2007-01-21 20:01:28 +00001179 {
Simon Kelley4cb1b322012-02-06 14:30:41 +00001180 /* text arg */
Simon Kelley572b41e2011-02-18 18:11:18 +00001181 if ((new->opt == OPTION_DOMAIN_SEARCH || new->opt == OPTION_SIP_SERVER) &&
Simon Kelley4cb1b322012-02-06 14:30:41 +00001182 !is6 && !(new->flags & (DHOPT_ENCAPSULATE | DHOPT_VENDOR | DHOPT_RFC3925)))
Simon Kelley832af0b2007-01-21 20:01:28 +00001183 {
1184 /* dns search, RFC 3397, or SIP, RFC 3361 */
1185 unsigned char *q, *r, *tail;
Simon Kelley824af852008-02-12 20:43:05 +00001186 unsigned char *p, *m = NULL, *newp;
Simon Kelley832af0b2007-01-21 20:01:28 +00001187 size_t newlen, len = 0;
Simon Kelley572b41e2011-02-18 18:11:18 +00001188 int header_size = (new->opt == OPTION_DOMAIN_SEARCH) ? 0 : 1;
Simon Kelley832af0b2007-01-21 20:01:28 +00001189
1190 arg = comma;
Simon Kelleyf2621c72007-04-29 19:47:21 +01001191 comma = split(arg);
Simon Kelley832af0b2007-01-21 20:01:28 +00001192
1193 while (arg && *arg)
1194 {
Simon Kelleyc52e1892010-06-07 22:01:39 +01001195 char *in, *dom = NULL;
1196 size_t domlen = 1;
1197 /* Allow "." as an empty domain */
1198 if (strcmp (arg, ".") != 0)
Simon Kelley832af0b2007-01-21 20:01:28 +00001199 {
Simon Kelleyc52e1892010-06-07 22:01:39 +01001200 if (!(dom = canonicalise_opt(arg)))
1201 {
1202 problem = _("bad domain in dhcp-option");
1203 break;
1204 }
1205 domlen = strlen(dom) + 2;
Simon Kelley832af0b2007-01-21 20:01:28 +00001206 }
Simon Kelleyc52e1892010-06-07 22:01:39 +01001207
1208 newp = opt_malloc(len + domlen + header_size);
Simon Kelley824af852008-02-12 20:43:05 +00001209 if (m)
Simon Kelleyc52e1892010-06-07 22:01:39 +01001210 {
1211 memcpy(newp, m, header_size + len);
1212 free(m);
1213 }
Simon Kelley824af852008-02-12 20:43:05 +00001214 m = newp;
Simon Kelley832af0b2007-01-21 20:01:28 +00001215 p = m + header_size;
1216 q = p + len;
1217
1218 /* add string on the end in RFC1035 format */
Simon Kelleyc52e1892010-06-07 22:01:39 +01001219 for (in = dom; in && *in;)
Simon Kelley832af0b2007-01-21 20:01:28 +00001220 {
1221 unsigned char *cp = q++;
1222 int j;
Simon Kelleyc52e1892010-06-07 22:01:39 +01001223 for (j = 0; *in && (*in != '.'); in++, j++)
1224 *q++ = *in;
Simon Kelley832af0b2007-01-21 20:01:28 +00001225 *cp = j;
Simon Kelleyc52e1892010-06-07 22:01:39 +01001226 if (*in)
1227 in++;
Simon Kelley832af0b2007-01-21 20:01:28 +00001228 }
1229 *q++ = 0;
Simon Kelley1f15b812009-10-13 17:49:32 +01001230 free(dom);
Simon Kelleyc52e1892010-06-07 22:01:39 +01001231
Simon Kelley832af0b2007-01-21 20:01:28 +00001232 /* Now tail-compress using earlier names. */
1233 newlen = q - p;
1234 for (tail = p + len; *tail; tail += (*tail) + 1)
1235 for (r = p; r - p < (int)len; r += (*r) + 1)
1236 if (strcmp((char *)r, (char *)tail) == 0)
1237 {
1238 PUTSHORT((r - p) | 0xc000, tail);
1239 newlen = tail - p;
1240 goto end;
1241 }
1242 end:
1243 len = newlen;
1244
1245 arg = comma;
Simon Kelleyf2621c72007-04-29 19:47:21 +01001246 comma = split(arg);
Simon Kelley832af0b2007-01-21 20:01:28 +00001247 }
1248
1249 /* RFC 3361, enc byte is zero for names */
Simon Kelley572b41e2011-02-18 18:11:18 +00001250 if (new->opt == OPTION_SIP_SERVER)
Simon Kelley832af0b2007-01-21 20:01:28 +00001251 m[0] = 0;
1252 new->len = (int) len + header_size;
1253 new->val = m;
1254 }
Simon Kelley4cb1b322012-02-06 14:30:41 +00001255#ifdef HAVE_DHCP6
1256 else if (comma && (opt_len & OT_CSTRING))
1257 {
1258 /* length fields are two bytes so need 16 bits for each string */
1259 int commas = 1;
1260 unsigned char *p, *newp;
1261
1262 for(i = 0; comma[i]; i++)
1263 if (comma[i] == ',')
1264 commas++;
1265
1266 newp = opt_malloc(strlen(comma)+(2*commas));
1267 p = newp;
1268 arg = comma;
1269 comma = split(arg);
1270
1271 while (arg && *arg)
1272 {
1273 u16 len = strlen(arg);
1274 PUTSHORT(len, p);
1275 memcpy(p, arg, len);
1276 p += len;
1277
1278 arg = comma;
1279 comma = split(arg);
1280 }
1281
1282 new->val = newp;
1283 new->len = p - newp;
1284 }
1285 else if (comma && (opt_len & OT_RFC1035_NAME))
1286 {
1287 int commas = 1;
1288 unsigned char *p, *newp;
1289
1290 for(i = 0; comma[i]; i++)
1291 if (comma[i] == ',')
1292 commas++;
1293
1294 newp = opt_malloc(strlen(comma)+(2*commas));
1295 p = newp;
1296 arg = comma;
1297 comma = split(arg);
1298
1299 while (arg && *arg)
1300 {
1301 p = do_rfc1035_name(p, arg);
1302 *p++ = 0;
1303
1304 arg = comma;
1305 comma = split(arg);
1306 }
1307
1308 new->val = newp;
1309 new->len = p - newp;
1310 }
1311#endif
Simon Kelley832af0b2007-01-21 20:01:28 +00001312 else
1313 {
1314 new->len = strlen(comma);
1315 /* keep terminating zero on string */
Simon Kelley824af852008-02-12 20:43:05 +00001316 new->val = (unsigned char *)opt_string_alloc(comma);
Simon Kelley832af0b2007-01-21 20:01:28 +00001317 new->flags |= DHOPT_STRING;
1318 }
1319 }
1320 }
1321
Simon Kelley4cb1b322012-02-06 14:30:41 +00001322 if (!is6 &&
1323 ((new->len > 255) ||
Simon Kelley316e2732010-01-22 20:16:09 +00001324 (new->len > 253 && (new->flags & (DHOPT_VENDOR | DHOPT_ENCAPSULATE))) ||
Simon Kelley4cb1b322012-02-06 14:30:41 +00001325 (new->len > 250 && (new->flags & DHOPT_RFC3925))))
Simon Kelley832af0b2007-01-21 20:01:28 +00001326 problem = _("dhcp-option too long");
1327
Simon Kelley824af852008-02-12 20:43:05 +00001328 if (!problem)
1329 {
Simon Kelley73a08a22009-02-05 20:28:08 +00001330 if (flags == DHOPT_MATCH)
1331 {
1332 if ((new->flags & (DHOPT_ENCAPSULATE | DHOPT_VENDOR)) ||
1333 !new->netid ||
1334 new->netid->next)
1335 problem = _("illegal dhcp-match");
Simon Kelley3634c542012-02-08 14:22:37 +00001336 else if (is6)
1337 {
1338 new->next = daemon->dhcp_match6;
1339 daemon->dhcp_match6 = new;
1340 }
Simon Kelley73a08a22009-02-05 20:28:08 +00001341 else
1342 {
1343 new->next = daemon->dhcp_match;
1344 daemon->dhcp_match = new;
1345 }
1346 }
Simon Kelley4cb1b322012-02-06 14:30:41 +00001347 else if (is6)
1348 {
1349 new->next = daemon->dhcp_opts6;
1350 daemon->dhcp_opts6 = new;
1351 }
1352 else
Simon Kelley73a08a22009-02-05 20:28:08 +00001353 {
1354 new->next = daemon->dhcp_opts;
1355 daemon->dhcp_opts = new;
1356 }
Simon Kelley824af852008-02-12 20:43:05 +00001357 }
1358
Simon Kelley832af0b2007-01-21 20:01:28 +00001359 return problem;
1360}
1361
Simon Kelley7622fc02009-06-04 20:32:05 +01001362#endif
Simon Kelley832af0b2007-01-21 20:01:28 +00001363
Simon Kelley28866e92011-02-14 20:19:14 +00001364void set_option_bool(unsigned int opt)
1365{
1366 if (opt < 32)
1367 daemon->options |= 1u << opt;
1368 else
1369 daemon->options2 |= 1u << (opt - 32);
1370}
1371
1372static char *one_opt(int option, char *arg, char *gen_prob, int command_line)
Simon Kelley849a8352006-06-09 21:02:31 +01001373{
1374 int i;
Simon Kelley824af852008-02-12 20:43:05 +00001375 char *comma, *problem = NULL;;
Simon Kelley849a8352006-06-09 21:02:31 +01001376
Simon Kelley832af0b2007-01-21 20:01:28 +00001377 if (option == '?')
Simon Kelley824af852008-02-12 20:43:05 +00001378 return gen_prob;
Simon Kelley832af0b2007-01-21 20:01:28 +00001379
Simon Kelley1a6bca82008-07-11 11:11:42 +01001380 for (i=0; usage[i].opt != 0; i++)
1381 if (usage[i].opt == option)
Simon Kelley849a8352006-06-09 21:02:31 +01001382 {
Simon Kelley1a6bca82008-07-11 11:11:42 +01001383 int rept = usage[i].rept;
1384
Simon Kelley28866e92011-02-14 20:19:14 +00001385 if (command_line)
Simon Kelley1a6bca82008-07-11 11:11:42 +01001386 {
1387 /* command line */
1388 if (rept == ARG_USED_CL)
1389 return _("illegal repeated flag");
1390 if (rept == ARG_ONE)
1391 usage[i].rept = ARG_USED_CL;
1392 }
1393 else
1394 {
1395 /* allow file to override command line */
1396 if (rept == ARG_USED_FILE)
1397 return _("illegal repeated keyword");
1398 if (rept == ARG_USED_CL || rept == ARG_ONE)
1399 usage[i].rept = ARG_USED_FILE;
1400 }
1401
1402 if (rept != ARG_DUP && rept != ARG_ONE && rept != ARG_USED_CL)
1403 {
Simon Kelley28866e92011-02-14 20:19:14 +00001404 set_option_bool(rept);
Simon Kelley1a6bca82008-07-11 11:11:42 +01001405 return NULL;
1406 }
1407
1408 break;
Simon Kelley849a8352006-06-09 21:02:31 +01001409 }
Simon Kelley1a6bca82008-07-11 11:11:42 +01001410
Simon Kelley849a8352006-06-09 21:02:31 +01001411 switch (option)
1412 {
Simon Kelleyf2621c72007-04-29 19:47:21 +01001413 case 'C': /* --conf-file */
Simon Kelley849a8352006-06-09 21:02:31 +01001414 {
Simon Kelley824af852008-02-12 20:43:05 +00001415 char *file = opt_string_alloc(arg);
Simon Kelley849a8352006-06-09 21:02:31 +01001416 if (file)
Simon Kelley9009d742008-11-14 20:04:27 +00001417 {
Simon Kelley28866e92011-02-14 20:19:14 +00001418 one_file(file, 0);
Simon Kelley9009d742008-11-14 20:04:27 +00001419 free(file);
1420 }
Simon Kelley849a8352006-06-09 21:02:31 +01001421 break;
1422 }
1423
Simon Kelleyf2621c72007-04-29 19:47:21 +01001424 case '7': /* --conf-dir */
Simon Kelley849a8352006-06-09 21:02:31 +01001425 {
1426 DIR *dir_stream;
1427 struct dirent *ent;
1428 char *directory, *path;
Simon Kelley1f15b812009-10-13 17:49:32 +01001429 struct list {
1430 char *suffix;
1431 struct list *next;
1432 } *ignore_suffix = NULL, *li;
Simon Kelley849a8352006-06-09 21:02:31 +01001433
Simon Kelley1f15b812009-10-13 17:49:32 +01001434 comma = split(arg);
Simon Kelley824af852008-02-12 20:43:05 +00001435 if (!(directory = opt_string_alloc(arg)))
Simon Kelley849a8352006-06-09 21:02:31 +01001436 break;
1437
Simon Kelley1f15b812009-10-13 17:49:32 +01001438 for (arg = comma; arg; arg = comma)
1439 {
1440 comma = split(arg);
1441 li = opt_malloc(sizeof(struct list));
1442 li->next = ignore_suffix;
1443 ignore_suffix = li;
1444 /* Have to copy: buffer is overwritten */
1445 li->suffix = opt_string_alloc(arg);
1446 };
1447
Simon Kelley849a8352006-06-09 21:02:31 +01001448 if (!(dir_stream = opendir(directory)))
Simon Kelley5aabfc72007-08-29 11:24:47 +01001449 die(_("cannot access directory %s: %s"), directory, EC_FILE);
Simon Kelley1f15b812009-10-13 17:49:32 +01001450
Simon Kelley849a8352006-06-09 21:02:31 +01001451 while ((ent = readdir(dir_stream)))
1452 {
Simon Kelley7622fc02009-06-04 20:32:05 +01001453 size_t len = strlen(ent->d_name);
Simon Kelley849a8352006-06-09 21:02:31 +01001454 struct stat buf;
Simon Kelley1f15b812009-10-13 17:49:32 +01001455
1456 /* ignore emacs backups and dotfiles */
Simon Kelley7622fc02009-06-04 20:32:05 +01001457 if (len == 0 ||
1458 ent->d_name[len - 1] == '~' ||
Simon Kelley849a8352006-06-09 21:02:31 +01001459 (ent->d_name[0] == '#' && ent->d_name[len - 1] == '#') ||
1460 ent->d_name[0] == '.')
1461 continue;
Simon Kelley7622fc02009-06-04 20:32:05 +01001462
Simon Kelley1f15b812009-10-13 17:49:32 +01001463 for (li = ignore_suffix; li; li = li->next)
1464 {
1465 /* check for proscribed suffices */
1466 size_t ls = strlen(li->suffix);
1467 if (len > ls &&
1468 strcmp(li->suffix, &ent->d_name[len - ls]) == 0)
1469 break;
1470 }
1471 if (li)
1472 continue;
1473
Simon Kelley824af852008-02-12 20:43:05 +00001474 path = opt_malloc(strlen(directory) + len + 2);
Simon Kelley849a8352006-06-09 21:02:31 +01001475 strcpy(path, directory);
1476 strcat(path, "/");
1477 strcat(path, ent->d_name);
Simon Kelley7622fc02009-06-04 20:32:05 +01001478
Simon Kelley849a8352006-06-09 21:02:31 +01001479 if (stat(path, &buf) == -1)
Simon Kelley5aabfc72007-08-29 11:24:47 +01001480 die(_("cannot access %s: %s"), path, EC_FILE);
Simon Kelley849a8352006-06-09 21:02:31 +01001481 /* only reg files allowed. */
1482 if (!S_ISREG(buf.st_mode))
1483 continue;
1484
Simon Kelley28866e92011-02-14 20:19:14 +00001485 /* files must be readable */
1486 one_file(path, 0);
Simon Kelley849a8352006-06-09 21:02:31 +01001487 free(path);
1488 }
1489
1490 closedir(dir_stream);
Simon Kelley9009d742008-11-14 20:04:27 +00001491 free(directory);
Simon Kelley1f15b812009-10-13 17:49:32 +01001492 for(; ignore_suffix; ignore_suffix = li)
1493 {
1494 li = ignore_suffix->next;
1495 free(ignore_suffix->suffix);
1496 free(ignore_suffix);
1497 }
1498
Simon Kelley849a8352006-06-09 21:02:31 +01001499 break;
1500 }
1501
Simon Kelleyf2621c72007-04-29 19:47:21 +01001502 case '8': /* --log-facility */
1503 /* may be a filename */
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001504 if (strchr(arg, '/') || strcmp (arg, "-") == 0)
Simon Kelley824af852008-02-12 20:43:05 +00001505 daemon->log_file = opt_string_alloc(arg);
Simon Kelley849a8352006-06-09 21:02:31 +01001506 else
Simon Kelleyf2621c72007-04-29 19:47:21 +01001507 {
Simon Kelley572b41e2011-02-18 18:11:18 +00001508#ifdef __ANDROID__
1509 problem = _("setting log facility is not possible under Android");
1510#else
Simon Kelleyf2621c72007-04-29 19:47:21 +01001511 for (i = 0; facilitynames[i].c_name; i++)
1512 if (hostname_isequal((char *)facilitynames[i].c_name, arg))
1513 break;
1514
1515 if (facilitynames[i].c_name)
1516 daemon->log_fac = facilitynames[i].c_val;
1517 else
Simon Kelley572b41e2011-02-18 18:11:18 +00001518 problem = _("bad log facility");
1519#endif
Simon Kelley849a8352006-06-09 21:02:31 +01001520 }
1521 break;
1522
Simon Kelleyf2621c72007-04-29 19:47:21 +01001523 case 'x': /* --pid-file */
Simon Kelley824af852008-02-12 20:43:05 +00001524 daemon->runfile = opt_string_alloc(arg);
Simon Kelley849a8352006-06-09 21:02:31 +01001525 break;
Simon Kelley5aabfc72007-08-29 11:24:47 +01001526
Simon Kelleyf2621c72007-04-29 19:47:21 +01001527 case 'r': /* --resolv-file */
Simon Kelley849a8352006-06-09 21:02:31 +01001528 {
Simon Kelley824af852008-02-12 20:43:05 +00001529 char *name = opt_string_alloc(arg);
Simon Kelley849a8352006-06-09 21:02:31 +01001530 struct resolvc *new, *list = daemon->resolv_files;
1531
1532 if (list && list->is_default)
1533 {
1534 /* replace default resolv file - possibly with nothing */
1535 if (name)
1536 {
1537 list->is_default = 0;
1538 list->name = name;
1539 }
1540 else
1541 list = NULL;
1542 }
1543 else if (name)
1544 {
Simon Kelley824af852008-02-12 20:43:05 +00001545 new = opt_malloc(sizeof(struct resolvc));
Simon Kelley849a8352006-06-09 21:02:31 +01001546 new->next = list;
1547 new->name = name;
1548 new->is_default = 0;
1549 new->mtime = 0;
1550 new->logged = 0;
1551 list = new;
1552 }
1553 daemon->resolv_files = list;
1554 break;
1555 }
1556
Simon Kelleyf2621c72007-04-29 19:47:21 +01001557 case 'm': /* --mx-host */
Simon Kelley849a8352006-06-09 21:02:31 +01001558 {
1559 int pref = 1;
1560 struct mx_srv_record *new;
Simon Kelley1f15b812009-10-13 17:49:32 +01001561 char *name, *target = NULL;
1562
Simon Kelleyf2621c72007-04-29 19:47:21 +01001563 if ((comma = split(arg)))
Simon Kelley849a8352006-06-09 21:02:31 +01001564 {
1565 char *prefstr;
Simon Kelley1f15b812009-10-13 17:49:32 +01001566 if ((prefstr = split(comma)) && !atoi_check16(prefstr, &pref))
Simon Kelley824af852008-02-12 20:43:05 +00001567 problem = _("bad MX preference");
Simon Kelley849a8352006-06-09 21:02:31 +01001568 }
1569
Simon Kelley1f15b812009-10-13 17:49:32 +01001570 if (!(name = canonicalise_opt(arg)) ||
1571 (comma && !(target = canonicalise_opt(comma))))
Simon Kelley824af852008-02-12 20:43:05 +00001572 problem = _("bad MX name");
Simon Kelley1f15b812009-10-13 17:49:32 +01001573
Simon Kelley824af852008-02-12 20:43:05 +00001574 new = opt_malloc(sizeof(struct mx_srv_record));
Simon Kelley849a8352006-06-09 21:02:31 +01001575 new->next = daemon->mxnames;
1576 daemon->mxnames = new;
1577 new->issrv = 0;
Simon Kelley1f15b812009-10-13 17:49:32 +01001578 new->name = name;
1579 new->target = target; /* may be NULL */
Simon Kelley849a8352006-06-09 21:02:31 +01001580 new->weight = pref;
1581 break;
1582 }
1583
Simon Kelleyf2621c72007-04-29 19:47:21 +01001584 case 't': /* --mx-target */
Simon Kelley1f15b812009-10-13 17:49:32 +01001585 if (!(daemon->mxtarget = canonicalise_opt(arg)))
Simon Kelley824af852008-02-12 20:43:05 +00001586 problem = _("bad MX target");
Simon Kelley849a8352006-06-09 21:02:31 +01001587 break;
Simon Kelley7622fc02009-06-04 20:32:05 +01001588
1589#ifdef HAVE_DHCP
Simon Kelleyf2621c72007-04-29 19:47:21 +01001590 case 'l': /* --dhcp-leasefile */
Simon Kelley824af852008-02-12 20:43:05 +00001591 daemon->lease_file = opt_string_alloc(arg);
Simon Kelley849a8352006-06-09 21:02:31 +01001592 break;
1593
Simon Kelleyc72daea2012-01-05 21:33:27 +00001594 /* Sorry about the gross pre-processor abuse */
1595 case '6': /* --dhcp-script */
1596 case LOPT_LUASCRIPT: /* --dhcp-luascript */
Simon Kelley1f15b812009-10-13 17:49:32 +01001597# if defined(NO_FORK)
Simon Kelley849a8352006-06-09 21:02:31 +01001598 problem = _("cannot run scripts under uClinux");
Simon Kelley1f15b812009-10-13 17:49:32 +01001599# elif !defined(HAVE_SCRIPT)
1600 problem = _("recompile with HAVE_SCRIPT defined to enable lease-change scripts");
Simon Kelley7622fc02009-06-04 20:32:05 +01001601# else
Simon Kelleyc72daea2012-01-05 21:33:27 +00001602 if (option == LOPT_LUASCRIPT)
1603# if !defined(HAVE_LUASCRIPT)
1604 problem = _("recompile with HAVE_LUASCRIPT defined to enable Lua scripts");
1605# else
1606 daemon->luascript = opt_string_alloc(arg);
1607# endif
1608 else
1609 daemon->lease_change_command = opt_string_alloc(arg);
Simon Kelley7622fc02009-06-04 20:32:05 +01001610# endif
Simon Kelley849a8352006-06-09 21:02:31 +01001611 break;
Simon Kelleyc72daea2012-01-05 21:33:27 +00001612#endif /* HAVE_DHCP */
Simon Kelley7622fc02009-06-04 20:32:05 +01001613
Simon Kelley28866e92011-02-14 20:19:14 +00001614 case LOPT_DHCP_HOST: /* --dhcp-hostfile */
1615 case LOPT_DHCP_OPTS: /* --dhcp-optsfile */
Simon Kelleyf2621c72007-04-29 19:47:21 +01001616 case 'H': /* --addn-hosts */
Simon Kelley849a8352006-06-09 21:02:31 +01001617 {
Simon Kelley824af852008-02-12 20:43:05 +00001618 struct hostsfile *new = opt_malloc(sizeof(struct hostsfile));
Simon Kelley849a8352006-06-09 21:02:31 +01001619 static int hosts_index = 1;
Simon Kelley824af852008-02-12 20:43:05 +00001620 new->fname = opt_string_alloc(arg);
Simon Kelley849a8352006-06-09 21:02:31 +01001621 new->index = hosts_index++;
Simon Kelley7622fc02009-06-04 20:32:05 +01001622 new->flags = 0;
Simon Kelley28866e92011-02-14 20:19:14 +00001623 if (option == 'H')
1624 {
1625 new->next = daemon->addn_hosts;
1626 daemon->addn_hosts = new;
1627 }
1628 else if (option == LOPT_DHCP_HOST)
1629 {
1630 new->next = daemon->dhcp_hosts_file;
1631 daemon->dhcp_hosts_file = new;
1632 }
1633 else if (option == LOPT_DHCP_OPTS)
1634 {
1635 new->next = daemon->dhcp_opts_file;
1636 daemon->dhcp_opts_file = new;
1637 }
Simon Kelley849a8352006-06-09 21:02:31 +01001638 break;
1639 }
1640
Simon Kelleyf2621c72007-04-29 19:47:21 +01001641 case 's': /* --domain */
Simon Kelley849a8352006-06-09 21:02:31 +01001642 if (strcmp (arg, "#") == 0)
Simon Kelley28866e92011-02-14 20:19:14 +00001643 set_option_bool(OPT_RESOLV_DOMAIN);
Simon Kelley849a8352006-06-09 21:02:31 +01001644 else
Simon Kelley9009d742008-11-14 20:04:27 +00001645 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001646 char *d;
Simon Kelley9009d742008-11-14 20:04:27 +00001647 comma = split(arg);
Simon Kelley1f15b812009-10-13 17:49:32 +01001648 if (!(d = canonicalise_opt(arg)))
Simon Kelley9009d742008-11-14 20:04:27 +00001649 option = '?';
1650 else
1651 {
Simon Kelley9009d742008-11-14 20:04:27 +00001652 if (comma)
1653 {
1654 struct cond_domain *new = safe_malloc(sizeof(struct cond_domain));
Simon Kelley28866e92011-02-14 20:19:14 +00001655 char *netpart;
1656
Simon Kelley9009d742008-11-14 20:04:27 +00001657 unhide_metas(comma);
Simon Kelley28866e92011-02-14 20:19:14 +00001658 if ((netpart = split_chr(comma, '/')))
Simon Kelley9009d742008-11-14 20:04:27 +00001659 {
Simon Kelleyd74942a2012-02-07 20:51:56 +00001660 int msize;
1661
Simon Kelley28866e92011-02-14 20:19:14 +00001662 arg = split(netpart);
Simon Kelleyd74942a2012-02-07 20:51:56 +00001663 if (!atoi_check(netpart, &msize))
Simon Kelley9009d742008-11-14 20:04:27 +00001664 option = '?';
Simon Kelleyd74942a2012-02-07 20:51:56 +00001665 else if (inet_pton(AF_INET, comma, &new->start))
Simon Kelley9009d742008-11-14 20:04:27 +00001666 {
Simon Kelleyd74942a2012-02-07 20:51:56 +00001667 int mask = (1 << (32 - msize)) - 1;
1668 new->is6 = 0;
Simon Kelley9009d742008-11-14 20:04:27 +00001669 new->start.s_addr = ntohl(htonl(new->start.s_addr) & ~mask);
1670 new->end.s_addr = new->start.s_addr | htonl(mask);
Simon Kelley28866e92011-02-14 20:19:14 +00001671 if (arg)
1672 {
1673 /* generate the equivalent of
1674 local=/<domain>/
1675 local=/xxx.yyy.zzz.in-addr.arpa/ */
1676
1677 if (strcmp(arg, "local") != 0 ||
1678 (msize != 8 && msize != 16 && msize != 24))
1679 option = '?';
1680 else
1681 {
1682 struct server *serv = opt_malloc(sizeof(struct server));
1683 in_addr_t a = ntohl(new->start.s_addr) >> 8;
1684 char *p;
1685
1686 memset(serv, 0, sizeof(struct server));
1687 serv->domain = d;
1688 serv->flags = SERV_HAS_DOMAIN | SERV_NO_ADDR;
1689 serv->next = daemon->servers;
1690 daemon->servers = serv;
1691
1692 serv = opt_malloc(sizeof(struct server));
1693 memset(serv, 0, sizeof(struct server));
1694 p = serv->domain = opt_malloc(25); /* strlen("xxx.yyy.zzz.in-addr.arpa")+1 */
1695
1696 if (msize == 24)
1697 p += sprintf(p, "%d.", a & 0xff);
1698 a = a >> 8;
1699 if (msize != 8)
1700 p += sprintf(p, "%d.", a & 0xff);
1701 a = a >> 8;
1702 p += sprintf(p, "%d.in-addr.arpa", a & 0xff);
1703
1704 serv->flags = SERV_HAS_DOMAIN | SERV_NO_ADDR;
1705 serv->next = daemon->servers;
1706 daemon->servers = serv;
1707 }
1708 }
Simon Kelley9009d742008-11-14 20:04:27 +00001709 }
Simon Kelleyd74942a2012-02-07 20:51:56 +00001710#ifdef HAVE_IPV6
1711 else if (inet_pton(AF_INET6, comma, &new->start6))
1712 {
1713 u64 mask = (1LLU << (128 - msize)) - 1LLU;
1714 u64 addrpart = addr6part(&new->start6);
1715 new->is6 = 1;
1716
1717 /* prefix==64 overflows the mask calculation above */
1718 if (msize == 64)
1719 mask = (u64)-1LL;
1720
1721 new->end6 = new->start6;
1722 setaddr6part(&new->start6, addrpart & ~mask);
1723 setaddr6part(&new->end6, addrpart | mask);
1724
1725 if (msize < 64)
1726 option = '?';
1727 else if (arg)
1728 {
1729 /* generate the equivalent of
1730 local=/<domain>/
1731 local=/xxx.yyy.zzz.ip6.arpa/ */
1732
1733 if (strcmp(arg, "local") != 0 || (msize & 4 != 0))
1734 option = '?';
1735 else
1736 {
1737 struct server *serv = opt_malloc(sizeof(struct server));
1738 in_addr_t a = ntohl(new->start.s_addr) >> 8;
1739 char *p;
1740
1741 memset(serv, 0, sizeof(struct server));
1742 serv->domain = d;
1743 serv->flags = SERV_HAS_DOMAIN | SERV_NO_ADDR;
1744 serv->next = daemon->servers;
1745 daemon->servers = serv;
1746
1747 serv = opt_malloc(sizeof(struct server));
1748 memset(serv, 0, sizeof(struct server));
1749 p = serv->domain = opt_malloc(73); /* strlen("32*<n.>ip6.arpa")+1 */
1750
1751 for (i = msize-1; i >= 0; i -= 4)
1752 {
1753 int dig = ((unsigned char *)&new->start6)[i>>3];
1754 p += sprintf(p, "%.1x.", (i>>2) & 1 ? dig & 15 : dig >> 4);
1755 }
1756 p += sprintf(p, "ip6.arpa");
1757
1758 serv->flags = SERV_HAS_DOMAIN | SERV_NO_ADDR;
1759 serv->next = daemon->servers;
1760 daemon->servers = serv;
1761 }
1762 }
1763 }
1764#endif
1765 else
Simon Kelley9009d742008-11-14 20:04:27 +00001766 option = '?';
1767 }
Simon Kelleyd74942a2012-02-07 20:51:56 +00001768 else
1769 {
1770 arg = split(comma);
1771 if (inet_pton(AF_INET, comma, &new->start))
1772 {
1773 new->is6 = 0;
1774 if (!arg)
1775 new->end.s_addr = new->start.s_addr;
1776 else if (!inet_pton(AF_INET, arg, &new->end))
1777 option = '?';
1778 }
1779#ifdef HAVE_IPV6
1780 else if (inet_pton(AF_INET6, comma, &new->start6))
1781 {
1782 new->is6 = 1;
1783 if (!arg)
1784 memcpy(&new->end6, &new->start6, IN6ADDRSZ);
1785 else if (!inet_pton(AF_INET6, arg, &new->end6))
1786 option = '?';
1787 }
1788#endif
1789 else
1790 option = '?';
Simon Kelley9009d742008-11-14 20:04:27 +00001791
Simon Kelleyd74942a2012-02-07 20:51:56 +00001792 new->domain = d;
1793 new->next = daemon->cond_domain;
1794 daemon->cond_domain = new;
1795
1796 }
Simon Kelley9009d742008-11-14 20:04:27 +00001797 }
1798 else
1799 daemon->domain_suffix = d;
1800 }
1801 }
Simon Kelley849a8352006-06-09 21:02:31 +01001802 break;
1803
Simon Kelleyf2621c72007-04-29 19:47:21 +01001804 case 'u': /* --user */
Simon Kelley824af852008-02-12 20:43:05 +00001805 daemon->username = opt_string_alloc(arg);
Simon Kelley849a8352006-06-09 21:02:31 +01001806 break;
1807
Simon Kelleyf2621c72007-04-29 19:47:21 +01001808 case 'g': /* --group */
Simon Kelley824af852008-02-12 20:43:05 +00001809 daemon->groupname = opt_string_alloc(arg);
Simon Kelley1a6bca82008-07-11 11:11:42 +01001810 daemon->group_set = 1;
Simon Kelley849a8352006-06-09 21:02:31 +01001811 break;
Simon Kelley9e038942008-05-30 20:06:34 +01001812
Simon Kelley7622fc02009-06-04 20:32:05 +01001813#ifdef HAVE_DHCP
Simon Kelley9e038942008-05-30 20:06:34 +01001814 case LOPT_SCRIPTUSR: /* --scriptuser */
1815 daemon->scriptuser = opt_string_alloc(arg);
1816 break;
Simon Kelley7622fc02009-06-04 20:32:05 +01001817#endif
Simon Kelley849a8352006-06-09 21:02:31 +01001818
Simon Kelleyf2621c72007-04-29 19:47:21 +01001819 case 'i': /* --interface */
Simon Kelley849a8352006-06-09 21:02:31 +01001820 do {
Simon Kelley824af852008-02-12 20:43:05 +00001821 struct iname *new = opt_malloc(sizeof(struct iname));
Simon Kelleyf2621c72007-04-29 19:47:21 +01001822 comma = split(arg);
Simon Kelley849a8352006-06-09 21:02:31 +01001823 new->next = daemon->if_names;
1824 daemon->if_names = new;
1825 /* new->name may be NULL if someone does
1826 "interface=" to disable all interfaces except loop. */
Simon Kelley824af852008-02-12 20:43:05 +00001827 new->name = opt_string_alloc(arg);
Simon Kelley849a8352006-06-09 21:02:31 +01001828 new->isloop = new->used = 0;
1829 arg = comma;
1830 } while (arg);
1831 break;
1832
Simon Kelleyf2621c72007-04-29 19:47:21 +01001833 case 'I': /* --except-interface */
1834 case '2': /* --no-dhcp-interface */
Simon Kelley849a8352006-06-09 21:02:31 +01001835 do {
Simon Kelley824af852008-02-12 20:43:05 +00001836 struct iname *new = opt_malloc(sizeof(struct iname));
Simon Kelleyf2621c72007-04-29 19:47:21 +01001837 comma = split(arg);
Simon Kelley824af852008-02-12 20:43:05 +00001838 new->name = opt_string_alloc(arg);
Simon Kelley849a8352006-06-09 21:02:31 +01001839 if (option == 'I')
1840 {
1841 new->next = daemon->if_except;
1842 daemon->if_except = new;
1843 }
1844 else
1845 {
1846 new->next = daemon->dhcp_except;
1847 daemon->dhcp_except = new;
1848 }
1849 arg = comma;
1850 } while (arg);
1851 break;
1852
Simon Kelleyf2621c72007-04-29 19:47:21 +01001853 case 'B': /* --bogus-nxdomain */
Simon Kelley849a8352006-06-09 21:02:31 +01001854 {
1855 struct in_addr addr;
1856 unhide_metas(arg);
1857 if (arg && (addr.s_addr = inet_addr(arg)) != (in_addr_t)-1)
1858 {
Simon Kelley824af852008-02-12 20:43:05 +00001859 struct bogus_addr *baddr = opt_malloc(sizeof(struct bogus_addr));
Simon Kelley849a8352006-06-09 21:02:31 +01001860 baddr->next = daemon->bogus_addr;
1861 daemon->bogus_addr = baddr;
1862 baddr->addr = addr;
1863 }
1864 else
1865 option = '?'; /* error */
1866 break;
1867 }
1868
Simon Kelleyf2621c72007-04-29 19:47:21 +01001869 case 'a': /* --listen-address */
Simon Kelley849a8352006-06-09 21:02:31 +01001870 do {
Simon Kelley824af852008-02-12 20:43:05 +00001871 struct iname *new = opt_malloc(sizeof(struct iname));
Simon Kelleyf2621c72007-04-29 19:47:21 +01001872 comma = split(arg);
Simon Kelley849a8352006-06-09 21:02:31 +01001873 unhide_metas(arg);
1874 new->next = daemon->if_addrs;
1875 if (arg && (new->addr.in.sin_addr.s_addr = inet_addr(arg)) != (in_addr_t)-1)
1876 {
1877 new->addr.sa.sa_family = AF_INET;
1878#ifdef HAVE_SOCKADDR_SA_LEN
1879 new->addr.in.sin_len = sizeof(new->addr.in);
1880#endif
1881 }
1882#ifdef HAVE_IPV6
1883 else if (arg && inet_pton(AF_INET6, arg, &new->addr.in6.sin6_addr) > 0)
1884 {
1885 new->addr.sa.sa_family = AF_INET6;
1886 new->addr.in6.sin6_flowinfo = 0;
1887 new->addr.in6.sin6_scope_id = 0;
1888#ifdef HAVE_SOCKADDR_SA_LEN
1889 new->addr.in6.sin6_len = sizeof(new->addr.in6);
1890#endif
1891 }
1892#endif
1893 else
1894 {
1895 option = '?'; /* error */
Simon Kelley849a8352006-06-09 21:02:31 +01001896 break;
1897 }
1898
1899 daemon->if_addrs = new;
1900 arg = comma;
1901 } while (arg);
1902 break;
1903
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001904 case 'S': /* --server */
1905 case LOPT_LOCAL: /* --local */
1906 case 'A': /* --address */
1907 case LOPT_NO_REBIND: /* --rebind-domain-ok */
Simon Kelley849a8352006-06-09 21:02:31 +01001908 {
1909 struct server *serv, *newlist = NULL;
1910
1911 unhide_metas(arg);
1912
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001913 if (arg && (*arg == '/' || option == LOPT_NO_REBIND))
Simon Kelley849a8352006-06-09 21:02:31 +01001914 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001915 int rebind = !(*arg == '/');
1916 char *end = NULL;
1917 if (!rebind)
1918 arg++;
1919 while (rebind || (end = split_chr(arg, '/')))
Simon Kelley849a8352006-06-09 21:02:31 +01001920 {
1921 char *domain = NULL;
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001922 /* elide leading dots - they are implied in the search algorithm */
1923 while (*arg == '.') arg++;
Simon Kelley849a8352006-06-09 21:02:31 +01001924 /* # matches everything and becomes a zero length domain string */
1925 if (strcmp(arg, "#") == 0)
1926 domain = "";
Simon Kelley1f15b812009-10-13 17:49:32 +01001927 else if (strlen (arg) != 0 && !(domain = canonicalise_opt(arg)))
Simon Kelley849a8352006-06-09 21:02:31 +01001928 option = '?';
Simon Kelley824af852008-02-12 20:43:05 +00001929 serv = opt_malloc(sizeof(struct server));
1930 memset(serv, 0, sizeof(struct server));
Simon Kelley849a8352006-06-09 21:02:31 +01001931 serv->next = newlist;
1932 newlist = serv;
Simon Kelley849a8352006-06-09 21:02:31 +01001933 serv->domain = domain;
1934 serv->flags = domain ? SERV_HAS_DOMAIN : SERV_FOR_NODOTS;
Simon Kelley73a08a22009-02-05 20:28:08 +00001935 arg = end;
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001936 if (rebind)
1937 break;
Simon Kelley849a8352006-06-09 21:02:31 +01001938 }
1939 if (!newlist)
1940 {
1941 option = '?';
1942 break;
1943 }
1944
1945 }
1946 else
1947 {
Simon Kelley824af852008-02-12 20:43:05 +00001948 newlist = opt_malloc(sizeof(struct server));
1949 memset(newlist, 0, sizeof(struct server));
Simon Kelley849a8352006-06-09 21:02:31 +01001950 }
1951
1952 if (option == 'A')
1953 {
1954 newlist->flags |= SERV_LITERAL_ADDRESS;
1955 if (!(newlist->flags & SERV_TYPE))
1956 option = '?';
1957 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001958 else if (option == LOPT_NO_REBIND)
1959 newlist->flags |= SERV_NO_REBIND;
Simon Kelley849a8352006-06-09 21:02:31 +01001960
1961 if (!arg || !*arg)
1962 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001963 if (!(newlist->flags & SERV_NO_REBIND))
1964 newlist->flags |= SERV_NO_ADDR; /* no server */
1965 if (newlist->flags & SERV_LITERAL_ADDRESS)
1966 option = '?';
1967 }
1968
1969 else if (strcmp(arg, "#") == 0)
1970 {
1971 newlist->flags |= SERV_USE_RESOLV; /* treat in ordinary way */
Simon Kelley849a8352006-06-09 21:02:31 +01001972 if (newlist->flags & SERV_LITERAL_ADDRESS)
1973 option = '?';
1974 }
1975 else
1976 {
1977 int source_port = 0, serv_port = NAMESERVER_PORT;
1978 char *portno, *source;
Simon Kelley7de060b2011-08-26 17:24:52 +01001979#ifdef HAVE_IPV6
1980 int scope_index = 0;
1981 char *scope_id;
1982#endif
Simon Kelley849a8352006-06-09 21:02:31 +01001983
Simon Kelley73a08a22009-02-05 20:28:08 +00001984 if ((source = split_chr(arg, '@')) && /* is there a source. */
1985 (portno = split_chr(source, '#')) &&
1986 !atoi_check16(portno, &source_port))
1987 problem = _("bad port");
1988
1989 if ((portno = split_chr(arg, '#')) && /* is there a port no. */
1990 !atoi_check16(portno, &serv_port))
1991 problem = _("bad port");
Simon Kelley849a8352006-06-09 21:02:31 +01001992
Simon Kelley7de060b2011-08-26 17:24:52 +01001993#ifdef HAVE_IPV6
1994 scope_id = split_chr(arg, '%');
1995#endif
1996
Simon Kelley849a8352006-06-09 21:02:31 +01001997 if ((newlist->addr.in.sin_addr.s_addr = inet_addr(arg)) != (in_addr_t) -1)
1998 {
1999 newlist->addr.in.sin_port = htons(serv_port);
2000 newlist->source_addr.in.sin_port = htons(source_port);
2001 newlist->addr.sa.sa_family = newlist->source_addr.sa.sa_family = AF_INET;
2002#ifdef HAVE_SOCKADDR_SA_LEN
2003 newlist->source_addr.in.sin_len = newlist->addr.in.sin_len = sizeof(struct sockaddr_in);
2004#endif
2005 if (source)
2006 {
Simon Kelley824af852008-02-12 20:43:05 +00002007 newlist->flags |= SERV_HAS_SOURCE;
Simon Kelley73a08a22009-02-05 20:28:08 +00002008 if ((newlist->source_addr.in.sin_addr.s_addr = inet_addr(source)) == (in_addr_t) -1)
Simon Kelley824af852008-02-12 20:43:05 +00002009 {
2010#if defined(SO_BINDTODEVICE)
2011 newlist->source_addr.in.sin_addr.s_addr = INADDR_ANY;
Simon Kelley316e2732010-01-22 20:16:09 +00002012 strncpy(newlist->interface, source, IF_NAMESIZE - 1);
Simon Kelley824af852008-02-12 20:43:05 +00002013#else
2014 problem = _("interface binding not supported");
2015#endif
2016 }
Simon Kelley849a8352006-06-09 21:02:31 +01002017 }
2018 else
2019 newlist->source_addr.in.sin_addr.s_addr = INADDR_ANY;
Simon Kelley5aabfc72007-08-29 11:24:47 +01002020 }
Simon Kelley849a8352006-06-09 21:02:31 +01002021#ifdef HAVE_IPV6
2022 else if (inet_pton(AF_INET6, arg, &newlist->addr.in6.sin6_addr) > 0)
2023 {
Simon Kelley7de060b2011-08-26 17:24:52 +01002024 if (scope_id && (scope_index = if_nametoindex(scope_id)) == 0)
2025 problem = _("bad interface name");
2026
Simon Kelley849a8352006-06-09 21:02:31 +01002027 newlist->addr.in6.sin6_port = htons(serv_port);
Simon Kelley7de060b2011-08-26 17:24:52 +01002028 newlist->addr.in6.sin6_scope_id = scope_index;
Simon Kelley849a8352006-06-09 21:02:31 +01002029 newlist->source_addr.in6.sin6_port = htons(source_port);
Simon Kelley7de060b2011-08-26 17:24:52 +01002030 newlist->source_addr.in6.sin6_scope_id = 0;
Simon Kelley849a8352006-06-09 21:02:31 +01002031 newlist->addr.sa.sa_family = newlist->source_addr.sa.sa_family = AF_INET6;
Simon Kelley7de060b2011-08-26 17:24:52 +01002032 newlist->addr.in6.sin6_flowinfo = newlist->source_addr.in6.sin6_flowinfo = 0;
Simon Kelley849a8352006-06-09 21:02:31 +01002033#ifdef HAVE_SOCKADDR_SA_LEN
2034 newlist->addr.in6.sin6_len = newlist->source_addr.in6.sin6_len = sizeof(newlist->addr.in6);
2035#endif
2036 if (source)
2037 {
Simon Kelley7de060b2011-08-26 17:24:52 +01002038 newlist->flags |= SERV_HAS_SOURCE;
2039 if (inet_pton(AF_INET6, source, &newlist->source_addr.in6.sin6_addr) == 0)
Simon Kelley824af852008-02-12 20:43:05 +00002040 {
Simon Kelley73a08a22009-02-05 20:28:08 +00002041#if defined(SO_BINDTODEVICE)
Simon Kelley824af852008-02-12 20:43:05 +00002042 newlist->source_addr.in6.sin6_addr = in6addr_any;
Simon Kelley316e2732010-01-22 20:16:09 +00002043 strncpy(newlist->interface, source, IF_NAMESIZE - 1);
Simon Kelley824af852008-02-12 20:43:05 +00002044#else
2045 problem = _("interface binding not supported");
2046#endif
2047 }
Simon Kelley849a8352006-06-09 21:02:31 +01002048 }
2049 else
2050 newlist->source_addr.in6.sin6_addr = in6addr_any;
2051 }
2052#endif
2053 else
2054 option = '?'; /* error */
Simon Kelley849a8352006-06-09 21:02:31 +01002055 }
2056
Simon Kelleyf2621c72007-04-29 19:47:21 +01002057 serv = newlist;
2058 while (serv->next)
Simon Kelley849a8352006-06-09 21:02:31 +01002059 {
Simon Kelleyf2621c72007-04-29 19:47:21 +01002060 serv->next->flags = serv->flags;
2061 serv->next->addr = serv->addr;
2062 serv->next->source_addr = serv->source_addr;
2063 serv = serv->next;
Simon Kelley849a8352006-06-09 21:02:31 +01002064 }
Simon Kelleyf2621c72007-04-29 19:47:21 +01002065 serv->next = daemon->servers;
2066 daemon->servers = newlist;
Simon Kelley849a8352006-06-09 21:02:31 +01002067 break;
2068 }
2069
Simon Kelleyf2621c72007-04-29 19:47:21 +01002070 case 'c': /* --cache-size */
Simon Kelley849a8352006-06-09 21:02:31 +01002071 {
2072 int size;
2073
2074 if (!atoi_check(arg, &size))
2075 option = '?';
2076 else
2077 {
2078 /* zero is OK, and means no caching. */
2079
2080 if (size < 0)
2081 size = 0;
2082 else if (size > 10000)
2083 size = 10000;
2084
2085 daemon->cachesize = size;
2086 }
2087 break;
2088 }
2089
Simon Kelleyf2621c72007-04-29 19:47:21 +01002090 case 'p': /* --port */
Simon Kelley1ad24ae2008-07-20 20:22:50 +01002091 if (!atoi_check16(arg, &daemon->port))
Simon Kelley849a8352006-06-09 21:02:31 +01002092 option = '?';
2093 break;
Simon Kelley208b65c2006-08-05 21:41:37 +01002094
Simon Kelley1a6bca82008-07-11 11:11:42 +01002095 case LOPT_MINPORT: /* --min-port */
Simon Kelley1ad24ae2008-07-20 20:22:50 +01002096 if (!atoi_check16(arg, &daemon->min_port))
Simon Kelley73a08a22009-02-05 20:28:08 +00002097 option = '?';
Simon Kelley1a6bca82008-07-11 11:11:42 +01002098 break;
2099
Simon Kelleyf2621c72007-04-29 19:47:21 +01002100 case '0': /* --dns-forward-max */
Simon Kelley208b65c2006-08-05 21:41:37 +01002101 if (!atoi_check(arg, &daemon->ftabsize))
2102 option = '?';
2103 break;
2104
Simon Kelleyf2621c72007-04-29 19:47:21 +01002105 case LOPT_MAX_LOGS: /* --log-async */
2106 daemon->max_logs = LOG_MAX; /* default */
2107 if (arg && !atoi_check(arg, &daemon->max_logs))
2108 option = '?';
2109 else if (daemon->max_logs > 100)
2110 daemon->max_logs = 100;
2111 break;
2112
2113 case 'P': /* --edns-packet-max */
Simon Kelley849a8352006-06-09 21:02:31 +01002114 {
2115 int i;
2116 if (!atoi_check(arg, &i))
2117 option = '?';
2118 daemon->edns_pktsz = (unsigned short)i;
2119 break;
2120 }
2121
Simon Kelleyf2621c72007-04-29 19:47:21 +01002122 case 'Q': /* --query-port */
Simon Kelley1ad24ae2008-07-20 20:22:50 +01002123 if (!atoi_check16(arg, &daemon->query_port))
Simon Kelley849a8352006-06-09 21:02:31 +01002124 option = '?';
Simon Kelley1a6bca82008-07-11 11:11:42 +01002125 /* if explicitly set to zero, use single OS ephemeral port
2126 and disable random ports */
2127 if (daemon->query_port == 0)
2128 daemon->osport = 1;
Simon Kelley849a8352006-06-09 21:02:31 +01002129 break;
2130
Simon Kelley824af852008-02-12 20:43:05 +00002131 case 'T': /* --local-ttl */
2132 case LOPT_NEGTTL: /* --neg-ttl */
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002133 case LOPT_MAXTTL: /* --max-ttl */
Simon Kelley849a8352006-06-09 21:02:31 +01002134 {
2135 int ttl;
2136 if (!atoi_check(arg, &ttl))
2137 option = '?';
Simon Kelley824af852008-02-12 20:43:05 +00002138 else if (option == LOPT_NEGTTL)
2139 daemon->neg_ttl = (unsigned long)ttl;
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002140 else if (option == LOPT_MAXTTL)
2141 daemon->max_ttl = (unsigned long)ttl;
Simon Kelley849a8352006-06-09 21:02:31 +01002142 else
2143 daemon->local_ttl = (unsigned long)ttl;
2144 break;
2145 }
2146
Simon Kelley7622fc02009-06-04 20:32:05 +01002147#ifdef HAVE_DHCP
Simon Kelleyf2621c72007-04-29 19:47:21 +01002148 case 'X': /* --dhcp-lease-max */
Simon Kelley849a8352006-06-09 21:02:31 +01002149 if (!atoi_check(arg, &daemon->dhcp_max))
2150 option = '?';
2151 break;
Simon Kelley7622fc02009-06-04 20:32:05 +01002152#endif
Simon Kelley849a8352006-06-09 21:02:31 +01002153
Simon Kelley7622fc02009-06-04 20:32:05 +01002154#ifdef HAVE_TFTP
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002155 case LOPT_TFTP: /* --enable-tftp */
2156 if (arg)
2157 {
2158 struct interface_list *new = opt_malloc(sizeof(struct interface_list));
2159 new->interface = opt_string_alloc(arg);
2160 new->next = daemon->tftp_interfaces;
2161 daemon->tftp_interfaces = new;
2162 }
2163 else
2164 daemon->tftp_unlimited = 1;
2165 break;
2166
Simon Kelleyf2621c72007-04-29 19:47:21 +01002167 case LOPT_TFTP_MAX: /* --tftp-max */
Simon Kelley832af0b2007-01-21 20:01:28 +00002168 if (!atoi_check(arg, &daemon->tftp_max))
2169 option = '?';
2170 break;
2171
Simon Kelley824af852008-02-12 20:43:05 +00002172 case LOPT_PREFIX: /* --tftp-prefix */
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002173 comma = split(arg);
2174 if (comma)
2175 {
2176 struct tftp_prefix *new = opt_malloc(sizeof(struct tftp_prefix));
2177 new->interface = opt_string_alloc(comma);
2178 new->prefix = opt_string_alloc(arg);
2179 new->next = daemon->if_prefix;
2180 daemon->if_prefix = new;
2181 }
2182 else
2183 daemon->tftp_prefix = opt_string_alloc(arg);
Simon Kelley832af0b2007-01-21 20:01:28 +00002184 break;
2185
Simon Kelley824af852008-02-12 20:43:05 +00002186 case LOPT_TFTPPORTS: /* --tftp-port-range */
2187 if (!(comma = split(arg)) ||
Simon Kelley1ad24ae2008-07-20 20:22:50 +01002188 !atoi_check16(arg, &daemon->start_tftp_port) ||
2189 !atoi_check16(comma, &daemon->end_tftp_port))
Simon Kelley824af852008-02-12 20:43:05 +00002190 problem = _("bad port range");
2191
2192 if (daemon->start_tftp_port > daemon->end_tftp_port)
2193 {
2194 int tmp = daemon->start_tftp_port;
2195 daemon->start_tftp_port = daemon->end_tftp_port;
2196 daemon->end_tftp_port = tmp;
2197 }
2198
2199 break;
Simon Kelley7622fc02009-06-04 20:32:05 +01002200#endif
Simon Kelley824af852008-02-12 20:43:05 +00002201
Simon Kelleyf2621c72007-04-29 19:47:21 +01002202 case LOPT_BRIDGE: /* --bridge-interface */
Simon Kelley832af0b2007-01-21 20:01:28 +00002203 {
Simon Kelley824af852008-02-12 20:43:05 +00002204 struct dhcp_bridge *new = opt_malloc(sizeof(struct dhcp_bridge));
Simon Kelley316e2732010-01-22 20:16:09 +00002205 if (!(comma = split(arg)) || strlen(arg) > IF_NAMESIZE - 1 )
Simon Kelley832af0b2007-01-21 20:01:28 +00002206 {
2207 problem = _("bad bridge-interface");
Simon Kelley832af0b2007-01-21 20:01:28 +00002208 break;
2209 }
2210
Simon Kelley316e2732010-01-22 20:16:09 +00002211 strcpy(new->iface, arg);
Simon Kelley832af0b2007-01-21 20:01:28 +00002212 new->alias = NULL;
2213 new->next = daemon->bridges;
2214 daemon->bridges = new;
2215
2216 do {
Simon Kelleyf2621c72007-04-29 19:47:21 +01002217 arg = comma;
2218 comma = split(arg);
Simon Kelley316e2732010-01-22 20:16:09 +00002219 if (strlen(arg) != 0 && strlen(arg) <= IF_NAMESIZE - 1)
Simon Kelley832af0b2007-01-21 20:01:28 +00002220 {
Simon Kelley824af852008-02-12 20:43:05 +00002221 struct dhcp_bridge *b = opt_malloc(sizeof(struct dhcp_bridge));
Simon Kelley832af0b2007-01-21 20:01:28 +00002222 b->next = new->alias;
2223 new->alias = b;
Simon Kelley316e2732010-01-22 20:16:09 +00002224 strcpy(b->iface, arg);
Simon Kelley832af0b2007-01-21 20:01:28 +00002225 }
2226 } while (comma);
2227
2228 break;
2229 }
Simon Kelley832af0b2007-01-21 20:01:28 +00002230
Simon Kelley7622fc02009-06-04 20:32:05 +01002231#ifdef HAVE_DHCP
Simon Kelleyf2621c72007-04-29 19:47:21 +01002232 case 'F': /* --dhcp-range */
Simon Kelley849a8352006-06-09 21:02:31 +01002233 {
2234 int k, leasepos = 2;
2235 char *cp, *a[5] = { NULL, NULL, NULL, NULL, NULL };
Simon Kelley824af852008-02-12 20:43:05 +00002236 struct dhcp_context *new = opt_malloc(sizeof(struct dhcp_context));
Simon Kelley849a8352006-06-09 21:02:31 +01002237
Simon Kelley52b92f42012-01-22 16:05:15 +00002238 memset (new, 0, sizeof(*new));
Simon Kelley849a8352006-06-09 21:02:31 +01002239 new->lease_time = DEFLEASE;
Simon Kelley52b92f42012-01-22 16:05:15 +00002240
Simon Kelley824af852008-02-12 20:43:05 +00002241 gen_prob = _("bad dhcp-range");
Simon Kelley849a8352006-06-09 21:02:31 +01002242
2243 if (!arg)
2244 {
2245 option = '?';
2246 break;
2247 }
2248
2249 while(1)
2250 {
2251 for (cp = arg; *cp; cp++)
Simon Kelley52b92f42012-01-22 16:05:15 +00002252 if (!(*cp == ' ' || *cp == '.' || *cp == ':' ||
2253 (*cp >= 'a' && *cp <= 'f') || (*cp >= 'A' && *cp <= 'F') ||
2254 (*cp >='0' && *cp <= '9')))
Simon Kelley849a8352006-06-09 21:02:31 +01002255 break;
2256
Simon Kelleyf2621c72007-04-29 19:47:21 +01002257 if (*cp != ',' && (comma = split(arg)))
Simon Kelley849a8352006-06-09 21:02:31 +01002258 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002259 if (strstr(arg, "interface:") == arg)
2260 new->interface = opt_string_alloc(arg+10);
2261 else if (is_tag_prefix(arg))
Simon Kelley849a8352006-06-09 21:02:31 +01002262 {
Simon Kelley824af852008-02-12 20:43:05 +00002263 struct dhcp_netid *tt = opt_malloc(sizeof (struct dhcp_netid));
2264 tt->net = opt_string_alloc(arg+4);
Simon Kelley849a8352006-06-09 21:02:31 +01002265 tt->next = new->filter;
2266 new->filter = tt;
2267 }
2268 else
2269 {
2270 if (new->netid.net)
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002271 problem = _("only one tag allowed");
2272 else if (strstr(arg, "set:") == arg)
2273 new->netid.net = opt_string_alloc(arg+4);
Simon Kelley849a8352006-06-09 21:02:31 +01002274 else
Simon Kelley824af852008-02-12 20:43:05 +00002275 new->netid.net = opt_string_alloc(arg);
Simon Kelley849a8352006-06-09 21:02:31 +01002276 }
Simon Kelleyf2621c72007-04-29 19:47:21 +01002277 arg = comma;
Simon Kelley849a8352006-06-09 21:02:31 +01002278 }
2279 else
2280 {
2281 a[0] = arg;
2282 break;
2283 }
2284 }
2285
2286 for (k = 1; k < 5; k++)
Simon Kelleyf2621c72007-04-29 19:47:21 +01002287 if (!(a[k] = split(a[k-1])))
2288 break;
Simon Kelley849a8352006-06-09 21:02:31 +01002289
Simon Kelley52b92f42012-01-22 16:05:15 +00002290 if (k < 2)
Simon Kelley849a8352006-06-09 21:02:31 +01002291 option = '?';
Simon Kelley52b92f42012-01-22 16:05:15 +00002292 else if (inet_pton(AF_INET, a[0], &new->start))
Simon Kelley849a8352006-06-09 21:02:31 +01002293 {
Simon Kelley52b92f42012-01-22 16:05:15 +00002294 new->next = daemon->dhcp;
2295 daemon->dhcp = new;
2296 if (strcmp(a[1], "static") == 0)
2297 {
2298 new->end = new->start;
2299 new->flags |= CONTEXT_STATIC;
2300 }
2301 else if (strcmp(a[1], "proxy") == 0)
2302 {
2303 new->end = new->start;
2304 new->flags |= CONTEXT_PROXY;
2305 }
2306 else if ((new->end.s_addr = inet_addr(a[1])) == (in_addr_t)-1)
2307 option = '?';
2308
2309 if (ntohl(new->start.s_addr) > ntohl(new->end.s_addr))
2310 {
2311 struct in_addr tmp = new->start;
2312 new->start = new->end;
2313 new->end = tmp;
2314 }
2315
2316 if (option != '?' && k >= 3 && strchr(a[2], '.') &&
2317 ((new->netmask.s_addr = inet_addr(a[2])) != (in_addr_t)-1))
2318 {
2319 new->flags |= CONTEXT_NETMASK;
2320 leasepos = 3;
2321 if (!is_same_net(new->start, new->end, new->netmask))
2322 problem = _("inconsistent DHCP range");
2323 }
2324
2325 if (k >= 4 && strchr(a[3], '.') &&
2326 ((new->broadcast.s_addr = inet_addr(a[3])) != (in_addr_t)-1))
2327 {
2328 new->flags |= CONTEXT_BRDCAST;
2329 leasepos = 4;
2330 }
Simon Kelley849a8352006-06-09 21:02:31 +01002331 }
Simon Kelley52b92f42012-01-22 16:05:15 +00002332#ifdef HAVE_DHCP6
2333 else if (inet_pton(AF_INET6, a[0], &new->start6))
Simon Kelley7622fc02009-06-04 20:32:05 +01002334 {
Simon Kelley52b92f42012-01-22 16:05:15 +00002335 new->next = daemon->dhcp6;
2336 new->prefix = 64; /* default */
2337 daemon->dhcp6 = new;
2338 if (strcmp(a[1], "static") == 0)
2339 {
2340 new->end = new->start;
2341 new->flags |= CONTEXT_STATIC;
2342 }
2343 else if (!inet_pton(AF_INET6, a[1], &new->end6))
2344 option = '?';
2345
2346 /* bare integer < 128 is prefix value */
2347 if (option != '?' && k >= 3)
2348 {
2349 int pref;
2350 for (cp = a[2]; *cp; cp++)
2351 if (!(*cp >= '0' && *cp <= '9'))
2352 break;
2353 if (!*cp && (pref = atoi(a[2])) <= 128)
2354 {
2355 new->prefix = pref;
2356 leasepos = 3;
Simon Kelley4cb1b322012-02-06 14:30:41 +00002357 if (new->prefix < 64)
2358 problem = _("prefix must be at least 64");
Simon Kelley52b92f42012-01-22 16:05:15 +00002359 }
2360 }
2361 if (!is_same_net6(&new->start6, &new->end6, new->prefix))
Simon Kelley824af852008-02-12 20:43:05 +00002362 problem = _("inconsistent DHCP range");
Simon Kelley52b92f42012-01-22 16:05:15 +00002363
2364 if (addr6part(&new->start6) > addr6part(&new->end6))
2365 {
2366 struct in6_addr tmp = new->start6;
2367 new->start6 = new->end6;
2368 new->end6 = tmp;
2369 }
Simon Kelley849a8352006-06-09 21:02:31 +01002370 }
Simon Kelley52b92f42012-01-22 16:05:15 +00002371#endif
Simon Kelley849a8352006-06-09 21:02:31 +01002372
2373 if (k >= leasepos+1)
2374 {
2375 if (strcmp(a[leasepos], "infinite") == 0)
2376 new->lease_time = 0xffffffff;
2377 else
2378 {
2379 int fac = 1;
2380 if (strlen(a[leasepos]) > 0)
2381 {
2382 switch (a[leasepos][strlen(a[leasepos]) - 1])
2383 {
2384 case 'd':
2385 case 'D':
2386 fac *= 24;
2387 /* fall though */
2388 case 'h':
2389 case 'H':
2390 fac *= 60;
2391 /* fall through */
2392 case 'm':
2393 case 'M':
2394 fac *= 60;
2395 /* fall through */
2396 case 's':
2397 case 'S':
Simon Kelleyf2621c72007-04-29 19:47:21 +01002398 a[leasepos][strlen(a[leasepos]) - 1] = 0;
Simon Kelley849a8352006-06-09 21:02:31 +01002399 }
2400
2401 new->lease_time = atoi(a[leasepos]) * fac;
2402 /* Leases of a minute or less confuse
2403 some clients, notably Apple's */
2404 if (new->lease_time < 120)
2405 new->lease_time = 120;
2406 }
2407 }
2408 }
2409 break;
2410 }
Simon Kelley5aabfc72007-08-29 11:24:47 +01002411
Simon Kelley5aabfc72007-08-29 11:24:47 +01002412 case LOPT_BANK:
Simon Kelleyf2621c72007-04-29 19:47:21 +01002413 case 'G': /* --dhcp-host */
Simon Kelley849a8352006-06-09 21:02:31 +01002414 {
Simon Kelleyf2621c72007-04-29 19:47:21 +01002415 int j, k = 0;
Simon Kelley849a8352006-06-09 21:02:31 +01002416 char *a[6] = { NULL, NULL, NULL, NULL, NULL, NULL };
Simon Kelley5aabfc72007-08-29 11:24:47 +01002417 struct dhcp_config *new;
Simon Kelley849a8352006-06-09 21:02:31 +01002418 struct in_addr in;
2419
Simon Kelley824af852008-02-12 20:43:05 +00002420 new = opt_malloc(sizeof(struct dhcp_config));
2421
Simon Kelley849a8352006-06-09 21:02:31 +01002422 new->next = daemon->dhcp_conf;
Simon Kelley9009d742008-11-14 20:04:27 +00002423 new->flags = (option == LOPT_BANK) ? CONFIG_BANK : 0;
2424 new->hwaddr = NULL;
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002425 new->netid = NULL;
2426
Simon Kelley849a8352006-06-09 21:02:31 +01002427 if ((a[0] = arg))
2428 for (k = 1; k < 6; k++)
Simon Kelleyf2621c72007-04-29 19:47:21 +01002429 if (!(a[k] = split(a[k-1])))
2430 break;
Simon Kelley849a8352006-06-09 21:02:31 +01002431
2432 for (j = 0; j < k; j++)
2433 if (strchr(a[j], ':')) /* ethernet address, netid or binary CLID */
2434 {
2435 char *arg = a[j];
2436
2437 if ((arg[0] == 'i' || arg[0] == 'I') &&
2438 (arg[1] == 'd' || arg[1] == 'D') &&
2439 arg[2] == ':')
2440 {
2441 if (arg[3] == '*')
2442 new->flags |= CONFIG_NOCLID;
2443 else
2444 {
2445 int len;
2446 arg += 3; /* dump id: */
2447 if (strchr(arg, ':'))
2448 len = parse_hex(arg, (unsigned char *)arg, -1, NULL, NULL);
2449 else
Simon Kelley5aabfc72007-08-29 11:24:47 +01002450 {
2451 unhide_metas(arg);
2452 len = (int) strlen(arg);
2453 }
2454
Simon Kelley28866e92011-02-14 20:19:14 +00002455 if (len == -1)
2456 problem = _("bad hex constant");
2457 else if ((new->clid = opt_malloc(len)))
Simon Kelley5aabfc72007-08-29 11:24:47 +01002458 {
2459 new->flags |= CONFIG_CLID;
2460 new->clid_len = len;
2461 memcpy(new->clid, arg, len);
2462 }
Simon Kelley849a8352006-06-09 21:02:31 +01002463 }
2464 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002465 /* dhcp-host has strange backwards-compat needs. */
2466 else if (strstr(arg, "net:") == arg || strstr(arg, "set:") == arg)
Simon Kelley849a8352006-06-09 21:02:31 +01002467 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002468 struct dhcp_netid *newtag = opt_malloc(sizeof(struct dhcp_netid));
2469 struct dhcp_netid_list *newlist = opt_malloc(sizeof(struct dhcp_netid_list));
2470 newtag->net = opt_malloc(strlen(arg + 4) + 1);
2471 newlist->next = new->netid;
2472 new->netid = newlist;
2473 newlist->list = newtag;
2474 strcpy(newtag->net, arg+4);
2475 unhide_metas(newtag->net);
Simon Kelley849a8352006-06-09 21:02:31 +01002476 }
Simon Kelley7de060b2011-08-26 17:24:52 +01002477 else if (strstr(arg, "tag:") == arg)
2478 problem = _("cannot match tags in --dhcp-host");
Simon Kelley4cb1b322012-02-06 14:30:41 +00002479#ifdef HAVE_DHCP6
2480 else if (arg[0] == '[' && arg[strlen(arg)-1] == ']')
2481 {
2482 arg[strlen(arg)-1] = 0;
2483 arg++;
2484
2485 if (!inet_pton(AF_INET6, arg, &new->addr6))
2486 problem = _("bad IPv6 address");
2487
2488 new->flags |= CONFIG_ADDR6;
2489 }
2490#endif
Simon Kelley7de060b2011-08-26 17:24:52 +01002491 else
Simon Kelley849a8352006-06-09 21:02:31 +01002492 {
Simon Kelley9009d742008-11-14 20:04:27 +00002493 struct hwaddr_config *newhw = opt_malloc(sizeof(struct hwaddr_config));
Simon Kelley28866e92011-02-14 20:19:14 +00002494 if ((newhw->hwaddr_len = parse_hex(a[j], newhw->hwaddr, DHCP_CHADDR_MAX,
2495 &newhw->wildcard_mask, &newhw->hwaddr_type)) == -1)
2496 problem = _("bad hex constant");
2497 else
2498 {
2499
2500 newhw->next = new->hwaddr;
2501 new->hwaddr = newhw;
2502 }
Simon Kelley849a8352006-06-09 21:02:31 +01002503 }
2504 }
2505 else if (strchr(a[j], '.') && (in.s_addr = inet_addr(a[j])) != (in_addr_t)-1)
2506 {
2507 new->addr = in;
2508 new->flags |= CONFIG_ADDR;
2509 }
2510 else
2511 {
2512 char *cp, *lastp = NULL, last = 0;
2513 int fac = 1;
2514
2515 if (strlen(a[j]) > 1)
2516 {
2517 lastp = a[j] + strlen(a[j]) - 1;
2518 last = *lastp;
2519 switch (last)
2520 {
2521 case 'd':
2522 case 'D':
2523 fac *= 24;
2524 /* fall through */
2525 case 'h':
2526 case 'H':
2527 fac *= 60;
2528 /* fall through */
2529 case 'm':
2530 case 'M':
2531 fac *= 60;
2532 /* fall through */
2533 case 's':
2534 case 'S':
2535 *lastp = 0;
2536 }
2537 }
2538
2539 for (cp = a[j]; *cp; cp++)
Simon Kelley572b41e2011-02-18 18:11:18 +00002540 if (!isdigit((unsigned char)*cp) && *cp != ' ')
Simon Kelley849a8352006-06-09 21:02:31 +01002541 break;
2542
2543 if (*cp)
2544 {
2545 if (lastp)
2546 *lastp = last;
2547 if (strcmp(a[j], "infinite") == 0)
2548 {
2549 new->lease_time = 0xffffffff;
2550 new->flags |= CONFIG_TIME;
2551 }
2552 else if (strcmp(a[j], "ignore") == 0)
2553 new->flags |= CONFIG_DISABLE;
2554 else
2555 {
Simon Kelley1f15b812009-10-13 17:49:32 +01002556 if (!(new->hostname = canonicalise_opt(a[j])) ||
2557 !legal_hostname(new->hostname))
Simon Kelley824af852008-02-12 20:43:05 +00002558 problem = _("bad DHCP host name");
Simon Kelley1f15b812009-10-13 17:49:32 +01002559 else
2560 new->flags |= CONFIG_NAME;
2561 new->domain = NULL;
Simon Kelley849a8352006-06-09 21:02:31 +01002562 }
2563 }
2564 else
2565 {
2566 new->lease_time = atoi(a[j]) * fac;
2567 /* Leases of a minute or less confuse
2568 some clients, notably Apple's */
2569 if (new->lease_time < 120)
2570 new->lease_time = 120;
2571 new->flags |= CONFIG_TIME;
2572 }
2573 }
2574
Simon Kelley5aabfc72007-08-29 11:24:47 +01002575 daemon->dhcp_conf = new;
Simon Kelley849a8352006-06-09 21:02:31 +01002576 break;
2577 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002578
2579 case LOPT_TAG_IF: /* --tag-if */
2580 {
2581 struct tag_if *new = opt_malloc(sizeof(struct tag_if));
2582
2583 new->tag = NULL;
2584 new->set = NULL;
2585 new->next = NULL;
2586
2587 /* preserve order */
2588 if (!daemon->tag_if)
2589 daemon->tag_if = new;
2590 else
2591 {
2592 struct tag_if *tmp;
2593 for (tmp = daemon->tag_if; tmp->next; tmp = tmp->next);
2594 tmp->next = new;
2595 }
2596
2597 while (arg)
2598 {
2599 size_t len;
2600
2601 comma = split(arg);
2602 len = strlen(arg);
2603
2604 if (len < 5)
2605 {
2606 new->set = NULL;
2607 break;
2608 }
2609 else
2610 {
2611 struct dhcp_netid *newtag = opt_malloc(sizeof(struct dhcp_netid));
2612 newtag->net = opt_malloc(len - 3);
2613 strcpy(newtag->net, arg+4);
2614 unhide_metas(newtag->net);
2615
2616 if (strstr(arg, "set:") == arg)
2617 {
2618 struct dhcp_netid_list *newlist = opt_malloc(sizeof(struct dhcp_netid_list));
2619 newlist->next = new->set;
2620 new->set = newlist;
2621 newlist->list = newtag;
2622 }
2623 else if (strstr(arg, "tag:") == arg)
2624 {
2625 newtag->next = new->tag;
2626 new->tag = newtag;
2627 }
2628 else
2629 {
2630 new->set = NULL;
2631 break;
2632 }
2633 }
2634
2635 arg = comma;
2636 }
2637
2638 if (!new->set)
2639 problem = _("bad tag-if");
2640
2641 break;
2642 }
2643
Simon Kelley849a8352006-06-09 21:02:31 +01002644
Simon Kelley73a08a22009-02-05 20:28:08 +00002645 case 'O': /* --dhcp-option */
2646 case LOPT_FORCE: /* --dhcp-option-force */
Simon Kelley824af852008-02-12 20:43:05 +00002647 case LOPT_OPTS:
Simon Kelley73a08a22009-02-05 20:28:08 +00002648 case LOPT_MATCH: /* --dhcp-match */
Simon Kelley824af852008-02-12 20:43:05 +00002649 problem = parse_dhcp_opt(arg,
2650 option == LOPT_FORCE ? DHOPT_FORCE :
Simon Kelley73a08a22009-02-05 20:28:08 +00002651 (option == LOPT_MATCH ? DHOPT_MATCH :
2652 (option == LOPT_OPTS ? DHOPT_BANK : 0)));
Simon Kelley832af0b2007-01-21 20:01:28 +00002653 break;
Simon Kelley849a8352006-06-09 21:02:31 +01002654
Simon Kelleyf2621c72007-04-29 19:47:21 +01002655 case 'M': /* --dhcp-boot */
Simon Kelley849a8352006-06-09 21:02:31 +01002656 {
2657 struct dhcp_netid *id = NULL;
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002658 while (is_tag_prefix(arg))
Simon Kelley849a8352006-06-09 21:02:31 +01002659 {
Simon Kelley824af852008-02-12 20:43:05 +00002660 struct dhcp_netid *newid = opt_malloc(sizeof(struct dhcp_netid));
Simon Kelley849a8352006-06-09 21:02:31 +01002661 newid->next = id;
2662 id = newid;
Simon Kelleyf2621c72007-04-29 19:47:21 +01002663 comma = split(arg);
Simon Kelley824af852008-02-12 20:43:05 +00002664 newid->net = opt_string_alloc(arg+4);
Simon Kelley849a8352006-06-09 21:02:31 +01002665 arg = comma;
2666 };
2667
2668 if (!arg)
2669 option = '?';
2670 else
2671 {
Simon Kelley7de060b2011-08-26 17:24:52 +01002672 char *dhcp_file, *dhcp_sname = NULL, *tftp_sname = NULL;
Simon Kelley849a8352006-06-09 21:02:31 +01002673 struct in_addr dhcp_next_server;
Simon Kelleyf2621c72007-04-29 19:47:21 +01002674 comma = split(arg);
Simon Kelley824af852008-02-12 20:43:05 +00002675 dhcp_file = opt_string_alloc(arg);
Simon Kelley849a8352006-06-09 21:02:31 +01002676 dhcp_next_server.s_addr = 0;
2677 if (comma)
2678 {
2679 arg = comma;
Simon Kelleyf2621c72007-04-29 19:47:21 +01002680 comma = split(arg);
Simon Kelley824af852008-02-12 20:43:05 +00002681 dhcp_sname = opt_string_alloc(arg);
Simon Kelley849a8352006-06-09 21:02:31 +01002682 if (comma)
2683 {
2684 unhide_metas(comma);
Simon Kelley7de060b2011-08-26 17:24:52 +01002685 if ((dhcp_next_server.s_addr = inet_addr(comma)) == (in_addr_t)-1) {
2686
2687 /*
2688 * The user may have specified the tftp hostname here.
2689 * save it so that it can be resolved/looked up during
2690 * actual dhcp_reply().
2691 */
2692
2693 tftp_sname = opt_string_alloc(comma);
2694 dhcp_next_server.s_addr = 0;
2695 }
Simon Kelley849a8352006-06-09 21:02:31 +01002696 }
2697 }
2698 if (option != '?')
2699 {
Simon Kelley824af852008-02-12 20:43:05 +00002700 struct dhcp_boot *new = opt_malloc(sizeof(struct dhcp_boot));
Simon Kelley849a8352006-06-09 21:02:31 +01002701 new->file = dhcp_file;
2702 new->sname = dhcp_sname;
Simon Kelley7de060b2011-08-26 17:24:52 +01002703 new->tftp_sname = tftp_sname;
Simon Kelley849a8352006-06-09 21:02:31 +01002704 new->next_server = dhcp_next_server;
2705 new->netid = id;
2706 new->next = daemon->boot_config;
2707 daemon->boot_config = new;
2708 }
2709 }
2710
Simon Kelley849a8352006-06-09 21:02:31 +01002711 break;
2712 }
Simon Kelley7622fc02009-06-04 20:32:05 +01002713
2714 case LOPT_PXE_PROMT: /* --pxe-prompt */
2715 {
2716 struct dhcp_opt *new = opt_malloc(sizeof(struct dhcp_opt));
2717 int timeout;
2718
2719 new->netid = NULL;
2720 new->opt = 10; /* PXE_MENU_PROMPT */
2721
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002722 while (is_tag_prefix(arg))
2723 {
Simon Kelley7622fc02009-06-04 20:32:05 +01002724 struct dhcp_netid *nn = opt_malloc(sizeof (struct dhcp_netid));
2725 comma = split(arg);
2726 nn->next = new->netid;
2727 new->netid = nn;
2728 nn->net = opt_string_alloc(arg+4);
2729 arg = comma;
2730 }
2731
2732 if (!arg)
2733 option = '?';
2734 else
2735 {
2736 comma = split(arg);
2737 unhide_metas(arg);
2738 new->len = strlen(arg) + 1;
2739 new->val = opt_malloc(new->len);
2740 memcpy(new->val + 1, arg, new->len - 1);
2741
2742 new->u.vendor_class = (unsigned char *)"PXEClient";
2743 new->flags = DHOPT_VENDOR;
2744
2745 if (comma && atoi_check(comma, &timeout))
2746 *(new->val) = timeout;
2747 else
2748 *(new->val) = 255;
2749
2750 new->next = daemon->dhcp_opts;
2751 daemon->dhcp_opts = new;
Simon Kelley1f15b812009-10-13 17:49:32 +01002752 daemon->enable_pxe = 1;
Simon Kelley7622fc02009-06-04 20:32:05 +01002753 }
2754
2755 break;
2756 }
2757
2758 case LOPT_PXE_SERV: /* --pxe-service */
2759 {
2760 struct pxe_service *new = opt_malloc(sizeof(struct pxe_service));
2761 char *CSA[] = { "x86PC", "PC98", "IA64_EFI", "Alpha", "Arc_x86", "Intel_Lean_Client",
2762 "IA32_EFI", "BC_EFI", "Xscale_EFI", "x86-64_EFI", NULL };
2763 static int boottype = 32768;
2764
2765 new->netid = NULL;
2766 new->server.s_addr = 0;
2767
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002768 while (is_tag_prefix(arg))
Simon Kelley7622fc02009-06-04 20:32:05 +01002769 {
2770 struct dhcp_netid *nn = opt_malloc(sizeof (struct dhcp_netid));
2771 comma = split(arg);
2772 nn->next = new->netid;
2773 new->netid = nn;
2774 nn->net = opt_string_alloc(arg+4);
2775 arg = comma;
2776 }
2777
2778 if (arg && (comma = split(arg)))
2779 {
2780 for (i = 0; CSA[i]; i++)
2781 if (strcasecmp(CSA[i], arg) == 0)
2782 break;
2783
2784 if (CSA[i] || atoi_check(arg, &i))
2785 {
2786 arg = comma;
2787 comma = split(arg);
2788
2789 new->CSA = i;
2790 new->menu = opt_string_alloc(arg);
2791
Simon Kelley316e2732010-01-22 20:16:09 +00002792 if (!comma)
2793 {
2794 new->type = 0; /* local boot */
2795 new->basename = NULL;
2796 }
2797 else
Simon Kelley7622fc02009-06-04 20:32:05 +01002798 {
2799 arg = comma;
2800 comma = split(arg);
2801 if (atoi_check(arg, &i))
2802 {
2803 new->type = i;
2804 new->basename = NULL;
2805 }
2806 else
2807 {
2808 new->type = boottype++;
2809 new->basename = opt_string_alloc(arg);
2810 }
2811
2812 if (comma && (new->server.s_addr = inet_addr(comma)) == (in_addr_t)-1)
2813 option = '?';
Simon Kelley7622fc02009-06-04 20:32:05 +01002814 }
Simon Kelley316e2732010-01-22 20:16:09 +00002815
2816 /* Order matters */
2817 new->next = NULL;
2818 if (!daemon->pxe_services)
2819 daemon->pxe_services = new;
2820 else
2821 {
2822 struct pxe_service *s;
2823 for (s = daemon->pxe_services; s->next; s = s->next);
2824 s->next = new;
2825 }
2826
2827 daemon->enable_pxe = 1;
2828 break;
2829
Simon Kelley7622fc02009-06-04 20:32:05 +01002830 }
2831 }
2832
2833 option = '?';
2834 break;
2835 }
2836
Simon Kelleyf2621c72007-04-29 19:47:21 +01002837 case '4': /* --dhcp-mac */
Simon Kelley849a8352006-06-09 21:02:31 +01002838 {
Simon Kelleyf2621c72007-04-29 19:47:21 +01002839 if (!(comma = split(arg)))
Simon Kelley849a8352006-06-09 21:02:31 +01002840 option = '?';
2841 else
2842 {
Simon Kelley824af852008-02-12 20:43:05 +00002843 struct dhcp_mac *new = opt_malloc(sizeof(struct dhcp_mac));
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002844 new->netid.net = opt_string_alloc(set_prefix(arg));
Simon Kelleyf2621c72007-04-29 19:47:21 +01002845 unhide_metas(comma);
2846 new->hwaddr_len = parse_hex(comma, new->hwaddr, DHCP_CHADDR_MAX, &new->mask, &new->hwaddr_type);
Simon Kelley28866e92011-02-14 20:19:14 +00002847 if (new->hwaddr_len == -1)
2848 option = '?';
2849 else
2850 {
2851 new->next = daemon->dhcp_macs;
2852 daemon->dhcp_macs = new;
2853 }
Simon Kelley849a8352006-06-09 21:02:31 +01002854 }
2855 }
2856 break;
2857
Simon Kelleyf2621c72007-04-29 19:47:21 +01002858 case 'U': /* --dhcp-vendorclass */
2859 case 'j': /* --dhcp-userclass */
2860 case LOPT_CIRCUIT: /* --dhcp-circuitid */
2861 case LOPT_REMOTE: /* --dhcp-remoteid */
2862 case LOPT_SUBSCR: /* --dhcp-subscrid */
Simon Kelley849a8352006-06-09 21:02:31 +01002863 {
Simon Kelleyf2621c72007-04-29 19:47:21 +01002864 if (!(comma = split(arg)))
Simon Kelley849a8352006-06-09 21:02:31 +01002865 option = '?';
2866 else
2867 {
Simon Kelley572b41e2011-02-18 18:11:18 +00002868 unsigned char *p;
Simon Kelleyf2621c72007-04-29 19:47:21 +01002869 int dig = 0;
Simon Kelley824af852008-02-12 20:43:05 +00002870 struct dhcp_vendor *new = opt_malloc(sizeof(struct dhcp_vendor));
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002871 new->netid.net = opt_string_alloc(set_prefix(arg));
Simon Kelleyf2621c72007-04-29 19:47:21 +01002872 /* check for hex string - must digits may include : must not have nothing else,
2873 only allowed for agent-options. */
Simon Kelley572b41e2011-02-18 18:11:18 +00002874 for (p = (unsigned char *)comma; *p; p++)
2875 if (isxdigit(*p))
Simon Kelleyf2621c72007-04-29 19:47:21 +01002876 dig = 1;
2877 else if (*p != ':')
2878 break;
2879 unhide_metas(comma);
Simon Kelley73a08a22009-02-05 20:28:08 +00002880 if (option == 'U' || option == 'j' || *p || !dig)
Simon Kelley824af852008-02-12 20:43:05 +00002881 {
2882 new->len = strlen(comma);
2883 new->data = opt_malloc(new->len);
2884 memcpy(new->data, comma, new->len);
2885 }
Simon Kelleyf2621c72007-04-29 19:47:21 +01002886 else
Simon Kelley824af852008-02-12 20:43:05 +00002887 {
2888 new->len = parse_hex(comma, (unsigned char *)comma, strlen(comma), NULL, NULL);
2889 new->data = opt_malloc(new->len);
2890 memcpy(new->data, comma, new->len);
2891 }
2892
Simon Kelleyf2621c72007-04-29 19:47:21 +01002893 switch (option)
2894 {
2895 case 'j':
2896 new->match_type = MATCH_USER;
2897 break;
2898 case 'U':
2899 new->match_type = MATCH_VENDOR;
2900 break;
2901 case LOPT_CIRCUIT:
2902 new->match_type = MATCH_CIRCUIT;
2903 break;
2904 case LOPT_REMOTE:
2905 new->match_type = MATCH_REMOTE;
2906 break;
2907 case LOPT_SUBSCR:
2908 new->match_type = MATCH_SUBSCRIBER;
2909 break;
2910 }
Simon Kelley849a8352006-06-09 21:02:31 +01002911 new->next = daemon->dhcp_vendors;
2912 daemon->dhcp_vendors = new;
2913 }
2914 break;
2915 }
2916
Simon Kelley9e038942008-05-30 20:06:34 +01002917 case LOPT_ALTPORT: /* --dhcp-alternate-port */
2918 if (!arg)
2919 {
2920 daemon->dhcp_server_port = DHCP_SERVER_ALTPORT;
2921 daemon->dhcp_client_port = DHCP_CLIENT_ALTPORT;
2922 }
2923 else
2924 {
2925 comma = split(arg);
Simon Kelley1ad24ae2008-07-20 20:22:50 +01002926 if (!atoi_check16(arg, &daemon->dhcp_server_port) ||
2927 (comma && !atoi_check16(comma, &daemon->dhcp_client_port)))
Simon Kelley9e038942008-05-30 20:06:34 +01002928 problem = _("invalid port number");
2929 if (!comma)
2930 daemon->dhcp_client_port = daemon->dhcp_server_port+1;
2931 }
2932 break;
2933
Simon Kelley824af852008-02-12 20:43:05 +00002934 case 'J': /* --dhcp-ignore */
2935 case LOPT_NO_NAMES: /* --dhcp-ignore-names */
2936 case LOPT_BROADCAST: /* --dhcp-broadcast */
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002937 case '3': /* --bootp-dynamic */
2938 case LOPT_GEN_NAMES: /* --dhcp-generate-names */
Simon Kelley849a8352006-06-09 21:02:31 +01002939 {
Simon Kelley824af852008-02-12 20:43:05 +00002940 struct dhcp_netid_list *new = opt_malloc(sizeof(struct dhcp_netid_list));
Simon Kelley849a8352006-06-09 21:02:31 +01002941 struct dhcp_netid *list = NULL;
Simon Kelley832af0b2007-01-21 20:01:28 +00002942 if (option == 'J')
2943 {
2944 new->next = daemon->dhcp_ignore;
2945 daemon->dhcp_ignore = new;
2946 }
Simon Kelley824af852008-02-12 20:43:05 +00002947 else if (option == LOPT_BROADCAST)
2948 {
2949 new->next = daemon->force_broadcast;
2950 daemon->force_broadcast = new;
2951 }
Simon Kelley9009d742008-11-14 20:04:27 +00002952 else if (option == '3')
2953 {
2954 new->next = daemon->bootp_dynamic;
2955 daemon->bootp_dynamic = new;
2956 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002957 else if (option == LOPT_GEN_NAMES)
2958 {
2959 new->next = daemon->dhcp_gen_names;
2960 daemon->dhcp_gen_names = new;
2961 }
Simon Kelley832af0b2007-01-21 20:01:28 +00002962 else
2963 {
2964 new->next = daemon->dhcp_ignore_names;
2965 daemon->dhcp_ignore_names = new;
2966 }
2967
2968 while (arg) {
Simon Kelley824af852008-02-12 20:43:05 +00002969 struct dhcp_netid *member = opt_malloc(sizeof(struct dhcp_netid));
Simon Kelleyf2621c72007-04-29 19:47:21 +01002970 comma = split(arg);
Simon Kelley849a8352006-06-09 21:02:31 +01002971 member->next = list;
2972 list = member;
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002973 if (is_tag_prefix(arg))
Simon Kelley9009d742008-11-14 20:04:27 +00002974 member->net = opt_string_alloc(arg+4);
2975 else
2976 member->net = opt_string_alloc(arg);
Simon Kelley849a8352006-06-09 21:02:31 +01002977 arg = comma;
Simon Kelley832af0b2007-01-21 20:01:28 +00002978 }
Simon Kelley849a8352006-06-09 21:02:31 +01002979
2980 new->list = list;
2981 break;
2982 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +01002983
2984 case LOPT_PROXY: /* --dhcp-proxy */
2985 daemon->override = 1;
2986 while (arg) {
2987 struct addr_list *new = opt_malloc(sizeof(struct addr_list));
2988 comma = split(arg);
2989 if ((new->addr.s_addr = inet_addr(arg)) == (in_addr_t)-1)
2990 problem = _("bad dhcp-proxy address");
2991 new->next = daemon->override_relays;
2992 daemon->override_relays = new;
2993 arg = comma;
2994 }
2995 break;
Simon Kelley7622fc02009-06-04 20:32:05 +01002996#endif
Simon Kelley849a8352006-06-09 21:02:31 +01002997
Simon Kelleyf2621c72007-04-29 19:47:21 +01002998 case 'V': /* --alias */
Simon Kelley849a8352006-06-09 21:02:31 +01002999 {
Simon Kelley73a08a22009-02-05 20:28:08 +00003000 char *dash, *a[3] = { NULL, NULL, NULL };
Simon Kelleyf2621c72007-04-29 19:47:21 +01003001 int k = 0;
Simon Kelley73a08a22009-02-05 20:28:08 +00003002 struct doctor *new = opt_malloc(sizeof(struct doctor));
3003 new->next = daemon->doctors;
3004 daemon->doctors = new;
3005 new->mask.s_addr = 0xffffffff;
3006 new->end.s_addr = 0;
3007
Simon Kelley849a8352006-06-09 21:02:31 +01003008 if ((a[0] = arg))
3009 for (k = 1; k < 3; k++)
3010 {
Simon Kelleyf2621c72007-04-29 19:47:21 +01003011 if (!(a[k] = split(a[k-1])))
Simon Kelley849a8352006-06-09 21:02:31 +01003012 break;
Simon Kelley849a8352006-06-09 21:02:31 +01003013 unhide_metas(a[k]);
3014 }
Simon Kelley849a8352006-06-09 21:02:31 +01003015
Simon Kelley73a08a22009-02-05 20:28:08 +00003016 dash = split_chr(a[0], '-');
3017
Simon Kelley849a8352006-06-09 21:02:31 +01003018 if ((k < 2) ||
Simon Kelley73a08a22009-02-05 20:28:08 +00003019 ((new->in.s_addr = inet_addr(a[0])) == (in_addr_t)-1) ||
3020 ((new->out.s_addr = inet_addr(a[1])) == (in_addr_t)-1))
3021 option = '?';
Simon Kelley849a8352006-06-09 21:02:31 +01003022
3023 if (k == 3)
Simon Kelley73a08a22009-02-05 20:28:08 +00003024 new->mask.s_addr = inet_addr(a[2]);
Simon Kelley849a8352006-06-09 21:02:31 +01003025
Simon Kelley73a08a22009-02-05 20:28:08 +00003026 if (dash &&
3027 ((new->end.s_addr = inet_addr(dash)) == (in_addr_t)-1 ||
3028 !is_same_net(new->in, new->end, new->mask) ||
3029 ntohl(new->in.s_addr) > ntohl(new->end.s_addr)))
3030 problem = _("invalid alias range");
Simon Kelley849a8352006-06-09 21:02:31 +01003031
3032 break;
3033 }
3034
Simon Kelleyf2621c72007-04-29 19:47:21 +01003035 case LOPT_INTNAME: /* --interface-name */
3036 {
3037 struct interface_name *new, **up;
Simon Kelley1f15b812009-10-13 17:49:32 +01003038 char *domain = NULL;
3039
Simon Kelleyf2621c72007-04-29 19:47:21 +01003040 comma = split(arg);
3041
Simon Kelley1f15b812009-10-13 17:49:32 +01003042 if (!comma || !(domain = canonicalise_opt(arg)))
Simon Kelley824af852008-02-12 20:43:05 +00003043 problem = _("bad interface name");
Simon Kelley1f15b812009-10-13 17:49:32 +01003044
Simon Kelley824af852008-02-12 20:43:05 +00003045 new = opt_malloc(sizeof(struct interface_name));
Simon Kelleyf2621c72007-04-29 19:47:21 +01003046 new->next = NULL;
3047 /* Add to the end of the list, so that first name
3048 of an interface is used for PTR lookups. */
Simon Kelley824af852008-02-12 20:43:05 +00003049 for (up = &daemon->int_names; *up; up = &((*up)->next));
Simon Kelleyf2621c72007-04-29 19:47:21 +01003050 *up = new;
Simon Kelley1f15b812009-10-13 17:49:32 +01003051 new->name = domain;
Simon Kelley824af852008-02-12 20:43:05 +00003052 new->intr = opt_string_alloc(comma);
Simon Kelleyf2621c72007-04-29 19:47:21 +01003053 break;
3054 }
Simon Kelley9009d742008-11-14 20:04:27 +00003055
3056 case LOPT_CNAME: /* --cname */
3057 {
3058 struct cname *new;
3059
3060 if (!(comma = split(arg)))
3061 option = '?';
3062 else
3063 {
Simon Kelley1f15b812009-10-13 17:49:32 +01003064 char *alias = canonicalise_opt(arg);
3065 char *target = canonicalise_opt(comma);
3066
3067 if (!alias || !target)
3068 problem = _("bad CNAME");
3069 else
3070 {
3071 for (new = daemon->cnames; new; new = new->next)
3072 if (hostname_isequal(new->alias, arg))
3073 problem = _("duplicate CNAME");
3074 new = opt_malloc(sizeof(struct cname));
3075 new->next = daemon->cnames;
3076 daemon->cnames = new;
3077 new->alias = alias;
3078 new->target = target;
3079 }
Simon Kelley9009d742008-11-14 20:04:27 +00003080 }
3081 break;
3082 }
Simon Kelleyf2621c72007-04-29 19:47:21 +01003083
3084 case LOPT_PTR: /* --ptr-record */
Simon Kelley832af0b2007-01-21 20:01:28 +00003085 {
3086 struct ptr_record *new;
Simon Kelley1f15b812009-10-13 17:49:32 +01003087 char *dom, *target = NULL;
3088
Simon Kelleyf2621c72007-04-29 19:47:21 +01003089 comma = split(arg);
3090
Simon Kelley1f15b812009-10-13 17:49:32 +01003091 if (!(dom = canonicalise_opt(arg)) ||
3092 (comma && !(target = canonicalise_opt(comma))))
Simon Kelley824af852008-02-12 20:43:05 +00003093 problem = _("bad PTR record");
Simon Kelley1f15b812009-10-13 17:49:32 +01003094 else
3095 {
3096 new = opt_malloc(sizeof(struct ptr_record));
3097 new->next = daemon->ptr;
3098 daemon->ptr = new;
3099 new->name = dom;
3100 new->ptr = target;
3101 }
Simon Kelley832af0b2007-01-21 20:01:28 +00003102 break;
3103 }
3104
Simon Kelley1a6bca82008-07-11 11:11:42 +01003105 case LOPT_NAPTR: /* --naptr-record */
3106 {
3107 char *a[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
3108 int k = 0;
3109 struct naptr *new;
3110 int order, pref;
Simon Kelley1f15b812009-10-13 17:49:32 +01003111 char *name, *replace = NULL;
Simon Kelley1a6bca82008-07-11 11:11:42 +01003112
3113 if ((a[0] = arg))
3114 for (k = 1; k < 7; k++)
3115 if (!(a[k] = split(a[k-1])))
3116 break;
3117
3118
3119 if (k < 6 ||
Simon Kelley1f15b812009-10-13 17:49:32 +01003120 !(name = canonicalise_opt(a[0])) ||
Simon Kelley1ad24ae2008-07-20 20:22:50 +01003121 !atoi_check16(a[1], &order) ||
3122 !atoi_check16(a[2], &pref) ||
Simon Kelley1f15b812009-10-13 17:49:32 +01003123 (k == 7 && !(replace = canonicalise_opt(a[6]))))
Simon Kelley1a6bca82008-07-11 11:11:42 +01003124 problem = _("bad NAPTR record");
3125 else
3126 {
3127 new = opt_malloc(sizeof(struct naptr));
3128 new->next = daemon->naptr;
3129 daemon->naptr = new;
Simon Kelley1f15b812009-10-13 17:49:32 +01003130 new->name = name;
Simon Kelley1a6bca82008-07-11 11:11:42 +01003131 new->flags = opt_string_alloc(a[3]);
3132 new->services = opt_string_alloc(a[4]);
3133 new->regexp = opt_string_alloc(a[5]);
Simon Kelley1f15b812009-10-13 17:49:32 +01003134 new->replace = replace;
Simon Kelley1a6bca82008-07-11 11:11:42 +01003135 new->order = order;
3136 new->pref = pref;
3137 }
3138 break;
3139 }
3140
Simon Kelleyf2621c72007-04-29 19:47:21 +01003141 case 'Y': /* --txt-record */
Simon Kelley849a8352006-06-09 21:02:31 +01003142 {
3143 struct txt_record *new;
Simon Kelley28866e92011-02-14 20:19:14 +00003144 unsigned char *p, *cnt;
3145 size_t len;
3146
3147 comma = split(arg);
3148
Simon Kelley824af852008-02-12 20:43:05 +00003149 new = opt_malloc(sizeof(struct txt_record));
Simon Kelley849a8352006-06-09 21:02:31 +01003150 new->next = daemon->txt;
3151 daemon->txt = new;
3152 new->class = C_IN;
Simon Kelley849a8352006-06-09 21:02:31 +01003153
Simon Kelley1f15b812009-10-13 17:49:32 +01003154 if (!(new->name = canonicalise_opt(arg)))
3155 {
3156 problem = _("bad TXT record");
3157 break;
3158 }
3159
Simon Kelley28866e92011-02-14 20:19:14 +00003160 len = comma ? strlen(comma) : 0;
3161 len += (len/255) + 1; /* room for extra counts */
3162 new->txt = p = opt_malloc(len);
3163
3164 cnt = p++;
3165 *cnt = 0;
3166
3167 while (comma && *comma)
3168 {
3169 unsigned char c = (unsigned char)*comma++;
3170
3171 if (c == ',' || *cnt == 255)
3172 {
3173 if (c != ',')
3174 comma--;
3175 cnt = p++;
3176 *cnt = 0;
3177 }
3178 else
3179 {
3180 *p++ = unhide_meta(c);
3181 (*cnt)++;
3182 }
3183 }
3184
3185 new->len = p - new->txt;
3186
Simon Kelley849a8352006-06-09 21:02:31 +01003187 break;
3188 }
3189
Simon Kelleyf2621c72007-04-29 19:47:21 +01003190 case 'W': /* --srv-host */
Simon Kelley849a8352006-06-09 21:02:31 +01003191 {
3192 int port = 1, priority = 0, weight = 0;
3193 char *name, *target = NULL;
3194 struct mx_srv_record *new;
3195
Simon Kelleyf2621c72007-04-29 19:47:21 +01003196 comma = split(arg);
Simon Kelley849a8352006-06-09 21:02:31 +01003197
Simon Kelley1f15b812009-10-13 17:49:32 +01003198 if (!(name = canonicalise_opt(arg)))
Simon Kelley824af852008-02-12 20:43:05 +00003199 problem = _("bad SRV record");
3200
Simon Kelley849a8352006-06-09 21:02:31 +01003201 if (comma)
3202 {
3203 arg = comma;
Simon Kelleyf2621c72007-04-29 19:47:21 +01003204 comma = split(arg);
Simon Kelley1f15b812009-10-13 17:49:32 +01003205 if (!(target = canonicalise_opt(arg))
3206) problem = _("bad SRV target");
Simon Kelley824af852008-02-12 20:43:05 +00003207
Simon Kelley849a8352006-06-09 21:02:31 +01003208 if (comma)
3209 {
3210 arg = comma;
Simon Kelleyf2621c72007-04-29 19:47:21 +01003211 comma = split(arg);
Simon Kelley1ad24ae2008-07-20 20:22:50 +01003212 if (!atoi_check16(arg, &port))
Simon Kelley824af852008-02-12 20:43:05 +00003213 problem = _("invalid port number");
3214
Simon Kelley849a8352006-06-09 21:02:31 +01003215 if (comma)
3216 {
3217 arg = comma;
Simon Kelleyf2621c72007-04-29 19:47:21 +01003218 comma = split(arg);
Simon Kelley1ad24ae2008-07-20 20:22:50 +01003219 if (!atoi_check16(arg, &priority))
Simon Kelley824af852008-02-12 20:43:05 +00003220 problem = _("invalid priority");
3221
Simon Kelley849a8352006-06-09 21:02:31 +01003222 if (comma)
3223 {
3224 arg = comma;
Simon Kelleyf2621c72007-04-29 19:47:21 +01003225 comma = split(arg);
Simon Kelley1ad24ae2008-07-20 20:22:50 +01003226 if (!atoi_check16(arg, &weight))
Simon Kelley824af852008-02-12 20:43:05 +00003227 problem = _("invalid weight");
Simon Kelley849a8352006-06-09 21:02:31 +01003228 }
3229 }
3230 }
3231 }
3232
Simon Kelley824af852008-02-12 20:43:05 +00003233 new = opt_malloc(sizeof(struct mx_srv_record));
Simon Kelley849a8352006-06-09 21:02:31 +01003234 new->next = daemon->mxnames;
3235 daemon->mxnames = new;
3236 new->issrv = 1;
3237 new->name = name;
3238 new->target = target;
3239 new->srvport = port;
3240 new->priority = priority;
3241 new->weight = weight;
3242 break;
3243 }
Simon Kelley7622fc02009-06-04 20:32:05 +01003244
3245 default:
3246 return _("unsupported option (check that dnsmasq was compiled with DHCP/TFTP/DBus support)");
3247
Simon Kelley849a8352006-06-09 21:02:31 +01003248 }
3249
Simon Kelley824af852008-02-12 20:43:05 +00003250 if (problem)
3251 return problem;
3252
3253 if (option == '?')
3254 return gen_prob;
3255
3256 return NULL;
Simon Kelley849a8352006-06-09 21:02:31 +01003257}
3258
Simon Kelley28866e92011-02-14 20:19:14 +00003259static void read_file(char *file, FILE *f, int hard_opt)
Simon Kelley849a8352006-06-09 21:02:31 +01003260{
Simon Kelley824af852008-02-12 20:43:05 +00003261 volatile int lineno = 0;
Simon Kelley8ef5ada2010-06-03 19:42:45 +01003262 char *buff = daemon->namebuff;
Simon Kelley849a8352006-06-09 21:02:31 +01003263
3264 while (fgets(buff, MAXDNAME, f))
3265 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +01003266 int white, i, option; ;
3267 char *errmess, *p, *arg, *start;
3268 size_t len;
Simon Kelley832af0b2007-01-21 20:01:28 +00003269
Simon Kelley824af852008-02-12 20:43:05 +00003270 /* Memory allocation failure longjmps here if mem_recover == 1 */
3271 if (hard_opt)
3272 {
3273 if (setjmp(mem_jmp))
3274 continue;
3275 mem_recover = 1;
3276 }
3277
Simon Kelley849a8352006-06-09 21:02:31 +01003278 lineno++;
Simon Kelley824af852008-02-12 20:43:05 +00003279 errmess = NULL;
3280
Simon Kelley849a8352006-06-09 21:02:31 +01003281 /* Implement quotes, inside quotes we allow \\ \" \n and \t
3282 metacharacters get hidden also strip comments */
Simon Kelley8ef5ada2010-06-03 19:42:45 +01003283 for (white = 1, p = buff; *p; p++)
Simon Kelley849a8352006-06-09 21:02:31 +01003284 {
3285 if (*p == '"')
3286 {
3287 memmove(p, p+1, strlen(p+1)+1);
Simon Kelley8ef5ada2010-06-03 19:42:45 +01003288
Simon Kelley849a8352006-06-09 21:02:31 +01003289 for(; *p && *p != '"'; p++)
3290 {
Simon Kelley5aabfc72007-08-29 11:24:47 +01003291 if (*p == '\\' && strchr("\"tnebr\\", p[1]))
Simon Kelley849a8352006-06-09 21:02:31 +01003292 {
3293 if (p[1] == 't')
3294 p[1] = '\t';
3295 else if (p[1] == 'n')
3296 p[1] = '\n';
Simon Kelley849a8352006-06-09 21:02:31 +01003297 else if (p[1] == 'b')
3298 p[1] = '\b';
3299 else if (p[1] == 'r')
3300 p[1] = '\r';
Simon Kelley6b010842007-02-12 20:32:07 +00003301 else if (p[1] == 'e') /* escape */
3302 p[1] = '\033';
Simon Kelley849a8352006-06-09 21:02:31 +01003303 memmove(p, p+1, strlen(p+1)+1);
3304 }
3305 *p = hide_meta(*p);
3306 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +01003307
3308 if (*p == 0)
Simon Kelleyf2621c72007-04-29 19:47:21 +01003309 {
3310 errmess = _("missing \"");
3311 goto oops;
3312 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +01003313
3314 memmove(p, p+1, strlen(p+1)+1);
Simon Kelley849a8352006-06-09 21:02:31 +01003315 }
Simon Kelleyf2621c72007-04-29 19:47:21 +01003316
Simon Kelley8ef5ada2010-06-03 19:42:45 +01003317 if (isspace(*p))
3318 {
3319 *p = ' ';
3320 white = 1;
Simon Kelley849a8352006-06-09 21:02:31 +01003321 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +01003322 else
3323 {
3324 if (white && *p == '#')
3325 {
3326 *p = 0;
3327 break;
3328 }
3329 white = 0;
3330 }
Simon Kelley849a8352006-06-09 21:02:31 +01003331 }
3332
Simon Kelley8ef5ada2010-06-03 19:42:45 +01003333
3334 /* strip leading spaces */
3335 for (start = buff; *start && *start == ' '; start++);
3336
3337 /* strip trailing spaces */
3338 for (len = strlen(start); (len != 0) && (start[len-1] == ' '); len--);
3339
3340 if (len == 0)
Simon Kelley849a8352006-06-09 21:02:31 +01003341 continue;
Simon Kelley8ef5ada2010-06-03 19:42:45 +01003342 else
3343 start[len] = 0;
3344
Simon Kelley824af852008-02-12 20:43:05 +00003345 if (hard_opt != 0)
Simon Kelley8ef5ada2010-06-03 19:42:45 +01003346 arg = start;
3347 else if ((p=strchr(start, '=')))
Simon Kelley849a8352006-06-09 21:02:31 +01003348 {
3349 /* allow spaces around "=" */
Simon Kelley8ef5ada2010-06-03 19:42:45 +01003350 for (arg = p+1; *arg == ' '; arg++);
3351 for (; p >= start && (*p == ' ' || *p == '='); p--)
Simon Kelley849a8352006-06-09 21:02:31 +01003352 *p = 0;
3353 }
3354 else
3355 arg = NULL;
Simon Kelley832af0b2007-01-21 20:01:28 +00003356
Simon Kelley824af852008-02-12 20:43:05 +00003357 if (hard_opt != 0)
3358 option = hard_opt;
Simon Kelley849a8352006-06-09 21:02:31 +01003359 else
Simon Kelley5aabfc72007-08-29 11:24:47 +01003360 {
Simon Kelley5aabfc72007-08-29 11:24:47 +01003361 for (option = 0, i = 0; opts[i].name; i++)
3362 if (strcmp(opts[i].name, start) == 0)
3363 {
3364 option = opts[i].val;
3365 break;
3366 }
3367
3368 if (!option)
3369 errmess = _("bad option");
3370 else if (opts[i].has_arg == 0 && arg)
3371 errmess = _("extraneous parameter");
3372 else if (opts[i].has_arg == 1 && !arg)
3373 errmess = _("missing parameter");
3374 }
Simon Kelley824af852008-02-12 20:43:05 +00003375
Simon Kelley5aabfc72007-08-29 11:24:47 +01003376 if (!errmess)
Simon Kelley28866e92011-02-14 20:19:14 +00003377 errmess = one_opt(option, arg, _("error"), 0);
Simon Kelley832af0b2007-01-21 20:01:28 +00003378
3379 if (errmess)
Simon Kelleyf2621c72007-04-29 19:47:21 +01003380 {
3381 oops:
3382 sprintf(buff, _("%s at line %d of %%s"), errmess, lineno);
Simon Kelley824af852008-02-12 20:43:05 +00003383 if (hard_opt != 0)
Simon Kelley5aabfc72007-08-29 11:24:47 +01003384 my_syslog(LOG_ERR, buff, file);
3385 else
3386 die(buff, file, EC_BADCONF);
Simon Kelleyf2621c72007-04-29 19:47:21 +01003387 }
Simon Kelley849a8352006-06-09 21:02:31 +01003388 }
3389
Simon Kelley8ef5ada2010-06-03 19:42:45 +01003390 mem_recover = 0;
Simon Kelley849a8352006-06-09 21:02:31 +01003391 fclose(f);
3392}
3393
Simon Kelley28866e92011-02-14 20:19:14 +00003394static void one_file(char *file, int hard_opt)
3395{
3396 FILE *f;
3397 int nofile_ok = 0;
3398 static int read_stdin = 0;
3399 static struct fileread {
3400 dev_t dev;
3401 ino_t ino;
3402 struct fileread *next;
3403 } *filesread = NULL;
3404
3405 if (hard_opt == '7')
3406 {
3407 /* default conf-file reading */
3408 hard_opt = 0;
3409 nofile_ok = 1;
3410 }
3411
3412 if (hard_opt == 0 && strcmp(file, "-") == 0)
3413 {
3414 if (read_stdin == 1)
3415 return;
3416 read_stdin = 1;
3417 file = "stdin";
3418 f = stdin;
3419 }
3420 else
3421 {
3422 /* ignore repeated files. */
3423 struct stat statbuf;
3424
3425 if (hard_opt == 0 && stat(file, &statbuf) == 0)
3426 {
3427 struct fileread *r;
3428
3429 for (r = filesread; r; r = r->next)
3430 if (r->dev == statbuf.st_dev && r->ino == statbuf.st_ino)
3431 return;
3432
3433 r = safe_malloc(sizeof(struct fileread));
3434 r->next = filesread;
3435 filesread = r;
3436 r->dev = statbuf.st_dev;
3437 r->ino = statbuf.st_ino;
3438 }
3439
3440 if (!(f = fopen(file, "r")))
3441 {
3442 if (errno == ENOENT && nofile_ok)
3443 return; /* No conffile, all done. */
3444 else
3445 {
3446 char *str = _("cannot read %s: %s");
3447 if (hard_opt != 0)
3448 {
3449 my_syslog(LOG_ERR, str, file, strerror(errno));
3450 return;
3451 }
3452 else
3453 die(str, file, EC_FILE);
3454 }
3455 }
3456 }
3457
3458 read_file(file, f, hard_opt);
3459}
3460
3461/* expand any name which is a directory */
3462struct hostsfile *expand_filelist(struct hostsfile *list)
3463{
3464 int i;
3465 struct hostsfile *ah;
3466
3467 for (i = 0, ah = list; ah; ah = ah->next)
3468 {
3469 if (i <= ah->index)
3470 i = ah->index + 1;
3471
3472 if (ah->flags & AH_DIR)
3473 ah->flags |= AH_INACTIVE;
3474 else
3475 ah->flags &= ~AH_INACTIVE;
3476 }
3477
3478 for (ah = list; ah; ah = ah->next)
3479 if (!(ah->flags & AH_INACTIVE))
3480 {
3481 struct stat buf;
3482 if (stat(ah->fname, &buf) != -1 && S_ISDIR(buf.st_mode))
3483 {
3484 DIR *dir_stream;
3485 struct dirent *ent;
3486
3487 /* don't read this as a file */
3488 ah->flags |= AH_INACTIVE;
3489
3490 if (!(dir_stream = opendir(ah->fname)))
3491 my_syslog(LOG_ERR, _("cannot access directory %s: %s"),
3492 ah->fname, strerror(errno));
3493 else
3494 {
3495 while ((ent = readdir(dir_stream)))
3496 {
3497 size_t lendir = strlen(ah->fname);
3498 size_t lenfile = strlen(ent->d_name);
3499 struct hostsfile *ah1;
3500 char *path;
3501
3502 /* ignore emacs backups and dotfiles */
3503 if (lenfile == 0 ||
3504 ent->d_name[lenfile - 1] == '~' ||
3505 (ent->d_name[0] == '#' && ent->d_name[lenfile - 1] == '#') ||
3506 ent->d_name[0] == '.')
3507 continue;
3508
3509 /* see if we have an existing record.
3510 dir is ah->fname
3511 file is ent->d_name
3512 path to match is ah1->fname */
3513
3514 for (ah1 = list; ah1; ah1 = ah1->next)
3515 {
3516 if (lendir < strlen(ah1->fname) &&
3517 strstr(ah1->fname, ah->fname) == ah1->fname &&
3518 ah1->fname[lendir] == '/' &&
3519 strcmp(ah1->fname + lendir + 1, ent->d_name) == 0)
3520 {
3521 ah1->flags &= ~AH_INACTIVE;
3522 break;
3523 }
3524 }
3525
3526 /* make new record */
3527 if (!ah1)
3528 {
3529 if (!(ah1 = whine_malloc(sizeof(struct hostsfile))))
3530 continue;
3531
3532 if (!(path = whine_malloc(lendir + lenfile + 2)))
3533 {
3534 free(ah1);
3535 continue;
3536 }
3537
3538 strcpy(path, ah->fname);
3539 strcat(path, "/");
3540 strcat(path, ent->d_name);
3541 ah1->fname = path;
3542 ah1->index = i++;
3543 ah1->flags = AH_DIR;
3544 ah1->next = list;
3545 list = ah1;
3546 }
3547
3548 /* inactivate record if not regular file */
3549 if ((ah1->flags & AH_DIR) && stat(ah1->fname, &buf) != -1 && !S_ISREG(buf.st_mode))
3550 ah1->flags |= AH_INACTIVE;
3551
3552 }
3553 closedir(dir_stream);
3554 }
3555 }
3556 }
3557
3558 return list;
3559}
3560
3561
Simon Kelley7622fc02009-06-04 20:32:05 +01003562#ifdef HAVE_DHCP
Simon Kelley824af852008-02-12 20:43:05 +00003563void reread_dhcp(void)
3564{
Simon Kelley28866e92011-02-14 20:19:14 +00003565 struct hostsfile *hf;
3566
Simon Kelley824af852008-02-12 20:43:05 +00003567 if (daemon->dhcp_hosts_file)
3568 {
3569 struct dhcp_config *configs, *cp, **up;
Simon Kelley28866e92011-02-14 20:19:14 +00003570
Simon Kelley824af852008-02-12 20:43:05 +00003571 /* remove existing... */
3572 for (up = &daemon->dhcp_conf, configs = daemon->dhcp_conf; configs; configs = cp)
3573 {
3574 cp = configs->next;
3575
3576 if (configs->flags & CONFIG_BANK)
3577 {
Simon Kelley9009d742008-11-14 20:04:27 +00003578 struct hwaddr_config *mac, *tmp;
Simon Kelley8ef5ada2010-06-03 19:42:45 +01003579 struct dhcp_netid_list *list, *tmplist;
Simon Kelley9009d742008-11-14 20:04:27 +00003580
3581 for (mac = configs->hwaddr; mac; mac = tmp)
3582 {
3583 tmp = mac->next;
3584 free(mac);
3585 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +01003586
Simon Kelley824af852008-02-12 20:43:05 +00003587 if (configs->flags & CONFIG_CLID)
3588 free(configs->clid);
Simon Kelley8ef5ada2010-06-03 19:42:45 +01003589
3590 for (list = configs->netid; list; list = tmplist)
3591 {
3592 free(list->list);
3593 tmplist = list->next;
3594 free(list);
3595 }
3596
Simon Kelley824af852008-02-12 20:43:05 +00003597 if (configs->flags & CONFIG_NAME)
3598 free(configs->hostname);
3599
3600 *up = configs->next;
3601 free(configs);
3602 }
3603 else
3604 up = &configs->next;
3605 }
3606
Simon Kelley28866e92011-02-14 20:19:14 +00003607 daemon->dhcp_hosts_file = expand_filelist(daemon->dhcp_hosts_file);
3608 for (hf = daemon->dhcp_hosts_file; hf; hf = hf->next)
3609 if (!(hf->flags & AH_INACTIVE))
3610 {
3611 one_file(hf->fname, LOPT_BANK);
3612 my_syslog(MS_DHCP | LOG_INFO, _("read %s"), hf->fname);
3613 }
Simon Kelley824af852008-02-12 20:43:05 +00003614 }
3615
3616 if (daemon->dhcp_opts_file)
3617 {
3618 struct dhcp_opt *opts, *cp, **up;
3619 struct dhcp_netid *id, *next;
3620
3621 for (up = &daemon->dhcp_opts, opts = daemon->dhcp_opts; opts; opts = cp)
3622 {
3623 cp = opts->next;
3624
3625 if (opts->flags & DHOPT_BANK)
3626 {
Simon Kelley73a08a22009-02-05 20:28:08 +00003627 if ((opts->flags & DHOPT_VENDOR))
3628 free(opts->u.vendor_class);
Simon Kelley824af852008-02-12 20:43:05 +00003629 free(opts->val);
3630 for (id = opts->netid; id; id = next)
3631 {
3632 next = id->next;
3633 free(id->net);
3634 free(id);
3635 }
3636 *up = opts->next;
3637 free(opts);
3638 }
3639 else
3640 up = &opts->next;
3641 }
3642
Simon Kelley28866e92011-02-14 20:19:14 +00003643 daemon->dhcp_opts_file = expand_filelist(daemon->dhcp_opts_file);
3644 for (hf = daemon->dhcp_opts_file; hf; hf = hf->next)
3645 if (!(hf->flags & AH_INACTIVE))
3646 {
3647 one_file(hf->fname, LOPT_OPTS);
3648 my_syslog(MS_DHCP | LOG_INFO, _("read %s"), hf->fname);
3649 }
Simon Kelley824af852008-02-12 20:43:05 +00003650 }
3651}
Simon Kelley7622fc02009-06-04 20:32:05 +01003652#endif
Simon Kelley824af852008-02-12 20:43:05 +00003653
Simon Kelley5aabfc72007-08-29 11:24:47 +01003654void read_opts(int argc, char **argv, char *compile_opts)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003655{
Simon Kelley824af852008-02-12 20:43:05 +00003656 char *buff = opt_malloc(MAXDNAME);
Simon Kelley28866e92011-02-14 20:19:14 +00003657 int option, conffile_opt = '7', testmode = 0;
Simon Kelley849a8352006-06-09 21:02:31 +01003658 char *errmess, *arg, *conffile = CONFFILE;
3659
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003660 opterr = 0;
Simon Kelley5aabfc72007-08-29 11:24:47 +01003661
Simon Kelley824af852008-02-12 20:43:05 +00003662 daemon = opt_malloc(sizeof(struct daemon));
Simon Kelley3be34542004-09-11 19:12:13 +01003663 memset(daemon, 0, sizeof(struct daemon));
3664 daemon->namebuff = buff;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003665
Simon Kelley3be34542004-09-11 19:12:13 +01003666 /* Set defaults - everything else is zero or NULL */
Simon Kelley3be34542004-09-11 19:12:13 +01003667 daemon->cachesize = CACHESIZ;
Simon Kelley208b65c2006-08-05 21:41:37 +01003668 daemon->ftabsize = FTABSIZ;
Simon Kelley3be34542004-09-11 19:12:13 +01003669 daemon->port = NAMESERVER_PORT;
Simon Kelley9e038942008-05-30 20:06:34 +01003670 daemon->dhcp_client_port = DHCP_CLIENT_PORT;
3671 daemon->dhcp_server_port = DHCP_SERVER_PORT;
Simon Kelley3be34542004-09-11 19:12:13 +01003672 daemon->default_resolv.is_default = 1;
3673 daemon->default_resolv.name = RESOLVFILE;
3674 daemon->resolv_files = &daemon->default_resolv;
3675 daemon->username = CHUSER;
Simon Kelley3be34542004-09-11 19:12:13 +01003676 daemon->runfile = RUNFILE;
3677 daemon->dhcp_max = MAXLEASES;
Simon Kelley832af0b2007-01-21 20:01:28 +00003678 daemon->tftp_max = TFTP_MAX_CONNECTIONS;
Simon Kelley3be34542004-09-11 19:12:13 +01003679 daemon->edns_pktsz = EDNS_PKTSZ;
Simon Kelley849a8352006-06-09 21:02:31 +01003680 daemon->log_fac = -1;
Simon Kelley5aabfc72007-08-29 11:24:47 +01003681 add_txt("version.bind", "dnsmasq-" VERSION );
3682 add_txt("authors.bind", "Simon Kelley");
3683 add_txt("copyright.bind", COPYRIGHT);
Simon Kelley0a852542005-03-23 20:28:59 +00003684
Simon Kelley849a8352006-06-09 21:02:31 +01003685 while (1)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003686 {
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003687#ifdef HAVE_GETOPT_LONG
Simon Kelley849a8352006-06-09 21:02:31 +01003688 option = getopt_long(argc, argv, OPTSTRING, opts, NULL);
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003689#else
Simon Kelley849a8352006-06-09 21:02:31 +01003690 option = getopt(argc, argv, OPTSTRING);
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003691#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003692
3693 if (option == -1)
Simon Kelley28866e92011-02-14 20:19:14 +00003694 {
Simon Kelley572b41e2011-02-18 18:11:18 +00003695 for (; optind < argc; optind++)
3696 {
3697 unsigned char *c = (unsigned char *)argv[optind];
3698 for (; *c != 0; c++)
3699 if (!isspace(*c))
3700 die(_("junk found in command line"), NULL, EC_BADCONF);
3701 }
Simon Kelley28866e92011-02-14 20:19:14 +00003702 break;
3703 }
3704
Simon Kelley849a8352006-06-09 21:02:31 +01003705 /* Copy optarg so that argv doesn't get changed */
3706 if (optarg)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003707 {
Simon Kelley849a8352006-06-09 21:02:31 +01003708 strncpy(buff, optarg, MAXDNAME);
3709 buff[MAXDNAME-1] = 0;
3710 arg = buff;
3711 }
3712 else
3713 arg = NULL;
3714
3715 /* command-line only stuff */
Simon Kelley7622fc02009-06-04 20:32:05 +01003716 if (option == LOPT_TEST)
3717 testmode = 1;
3718 else if (option == 'w')
Simon Kelley849a8352006-06-09 21:02:31 +01003719 {
Simon Kelley7622fc02009-06-04 20:32:05 +01003720#ifdef HAVE_DHCP
Simon Kelley4cb1b322012-02-06 14:30:41 +00003721 if (argc == 3 && strcmp(argv[2], "dhcp") == 0)
Simon Kelley7622fc02009-06-04 20:32:05 +01003722 display_opts();
Simon Kelley4cb1b322012-02-06 14:30:41 +00003723#ifdef HAVE_DHCP6
3724 else if (argc == 3 && strcmp(argv[2], "dhcp6") == 0)
3725 display_opts6();
Simon Kelley7622fc02009-06-04 20:32:05 +01003726#endif
Simon Kelley4cb1b322012-02-06 14:30:41 +00003727 else
3728#endif
3729 do_usage();
3730
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003731 exit(0);
3732 }
Simon Kelley849a8352006-06-09 21:02:31 +01003733 else if (option == 'v')
3734 {
3735 printf(_("Dnsmasq version %s %s\n"), VERSION, COPYRIGHT);
Simon Kelleyc72daea2012-01-05 21:33:27 +00003736 printf(_("Compile time options: %s\n\n"), compile_opts);
Simon Kelleyb8187c82005-11-26 21:46:27 +00003737 printf(_("This software comes with ABSOLUTELY NO WARRANTY.\n"));
3738 printf(_("Dnsmasq is free software, and you are welcome to redistribute it\n"));
Simon Kelley824af852008-02-12 20:43:05 +00003739 printf(_("under the terms of the GNU General Public License, version 2 or 3.\n"));
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003740 exit(0);
3741 }
Simon Kelley849a8352006-06-09 21:02:31 +01003742 else if (option == 'C')
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003743 {
Simon Kelley28866e92011-02-14 20:19:14 +00003744 conffile_opt = 0; /* file must exist */
Simon Kelley824af852008-02-12 20:43:05 +00003745 conffile = opt_string_alloc(arg);
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003746 }
Simon Kelley849a8352006-06-09 21:02:31 +01003747 else
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003748 {
Simon Kelley26128d22004-11-14 16:43:54 +00003749#ifdef HAVE_GETOPT_LONG
Simon Kelley28866e92011-02-14 20:19:14 +00003750 errmess = one_opt(option, arg, _("try --help"), 1);
Simon Kelley849a8352006-06-09 21:02:31 +01003751#else
Simon Kelley28866e92011-02-14 20:19:14 +00003752 errmess = one_opt(option, arg, _("try -w"), 1);
Simon Kelley849a8352006-06-09 21:02:31 +01003753#endif
3754 if (errmess)
Simon Kelley5aabfc72007-08-29 11:24:47 +01003755 die(_("bad command line options: %s"), errmess, EC_BADCONF);
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003756 }
3757 }
Simon Kelley849a8352006-06-09 21:02:31 +01003758
3759 if (conffile)
Simon Kelley28866e92011-02-14 20:19:14 +00003760 one_file(conffile, conffile_opt);
Simon Kelley849a8352006-06-09 21:02:31 +01003761
Simon Kelley1a6bca82008-07-11 11:11:42 +01003762 /* port might not be known when the address is parsed - fill in here */
Simon Kelley3be34542004-09-11 19:12:13 +01003763 if (daemon->servers)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003764 {
3765 struct server *tmp;
Simon Kelley3be34542004-09-11 19:12:13 +01003766 for (tmp = daemon->servers; tmp; tmp = tmp->next)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003767 if (!(tmp->flags & SERV_HAS_SOURCE))
3768 {
3769 if (tmp->source_addr.sa.sa_family == AF_INET)
Simon Kelley3be34542004-09-11 19:12:13 +01003770 tmp->source_addr.in.sin_port = htons(daemon->query_port);
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003771#ifdef HAVE_IPV6
3772 else if (tmp->source_addr.sa.sa_family == AF_INET6)
Simon Kelley3be34542004-09-11 19:12:13 +01003773 tmp->source_addr.in6.sin6_port = htons(daemon->query_port);
Simon Kelley5aabfc72007-08-29 11:24:47 +01003774#endif
3775 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003776 }
3777
Simon Kelley3be34542004-09-11 19:12:13 +01003778 if (daemon->if_addrs)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003779 {
3780 struct iname *tmp;
Simon Kelley3be34542004-09-11 19:12:13 +01003781 for(tmp = daemon->if_addrs; tmp; tmp = tmp->next)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003782 if (tmp->addr.sa.sa_family == AF_INET)
Simon Kelley3be34542004-09-11 19:12:13 +01003783 tmp->addr.in.sin_port = htons(daemon->port);
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003784#ifdef HAVE_IPV6
3785 else if (tmp->addr.sa.sa_family == AF_INET6)
Simon Kelley3be34542004-09-11 19:12:13 +01003786 tmp->addr.in6.sin6_port = htons(daemon->port);
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003787#endif /* IPv6 */
3788 }
3789
Simon Kelleyf6b7dc42005-01-23 12:06:08 +00003790 /* only one of these need be specified: the other defaults to the host-name */
Simon Kelley28866e92011-02-14 20:19:14 +00003791 if (option_bool(OPT_LOCALMX) || daemon->mxnames || daemon->mxtarget)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003792 {
Simon Kelley0a852542005-03-23 20:28:59 +00003793 struct mx_srv_record *mx;
3794
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003795 if (gethostname(buff, MAXDNAME) == -1)
Simon Kelley5aabfc72007-08-29 11:24:47 +01003796 die(_("cannot get host-name: %s"), NULL, EC_MISC);
Simon Kelleyf6b7dc42005-01-23 12:06:08 +00003797
Simon Kelley0a852542005-03-23 20:28:59 +00003798 for (mx = daemon->mxnames; mx; mx = mx->next)
3799 if (!mx->issrv && hostname_isequal(mx->name, buff))
3800 break;
3801
Simon Kelley28866e92011-02-14 20:19:14 +00003802 if ((daemon->mxtarget || option_bool(OPT_LOCALMX)) && !mx)
Simon Kelleyde379512004-06-22 20:23:33 +01003803 {
Simon Kelley824af852008-02-12 20:43:05 +00003804 mx = opt_malloc(sizeof(struct mx_srv_record));
Simon Kelley91dccd02005-03-31 17:48:32 +01003805 mx->next = daemon->mxnames;
3806 mx->issrv = 0;
3807 mx->target = NULL;
Simon Kelley824af852008-02-12 20:43:05 +00003808 mx->name = opt_string_alloc(buff);
Simon Kelley91dccd02005-03-31 17:48:32 +01003809 daemon->mxnames = mx;
Simon Kelleyf6b7dc42005-01-23 12:06:08 +00003810 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003811
Simon Kelley3be34542004-09-11 19:12:13 +01003812 if (!daemon->mxtarget)
Simon Kelley824af852008-02-12 20:43:05 +00003813 daemon->mxtarget = opt_string_alloc(buff);
Simon Kelley0a852542005-03-23 20:28:59 +00003814
3815 for (mx = daemon->mxnames; mx; mx = mx->next)
3816 if (!mx->issrv && !mx->target)
3817 mx->target = daemon->mxtarget;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00003818 }
Simon Kelleyf6b7dc42005-01-23 12:06:08 +00003819
Simon Kelley28866e92011-02-14 20:19:14 +00003820 if (!option_bool(OPT_NO_RESOLV) &&
Simon Kelley208b65c2006-08-05 21:41:37 +01003821 daemon->resolv_files &&
3822 daemon->resolv_files->next &&
Simon Kelley28866e92011-02-14 20:19:14 +00003823 option_bool(OPT_NO_POLL))
Simon Kelley5aabfc72007-08-29 11:24:47 +01003824 die(_("only one resolv.conf file allowed in no-poll mode."), NULL, EC_BADCONF);
Simon Kelleyde379512004-06-22 20:23:33 +01003825
Simon Kelley28866e92011-02-14 20:19:14 +00003826 if (option_bool(OPT_RESOLV_DOMAIN))
Simon Kelleyde379512004-06-22 20:23:33 +01003827 {
3828 char *line;
Simon Kelley849a8352006-06-09 21:02:31 +01003829 FILE *f;
3830
Simon Kelley28866e92011-02-14 20:19:14 +00003831 if (option_bool(OPT_NO_RESOLV) ||
Simon Kelley208b65c2006-08-05 21:41:37 +01003832 !daemon->resolv_files ||
3833 (daemon->resolv_files)->next)
Simon Kelley5aabfc72007-08-29 11:24:47 +01003834 die(_("must have exactly one resolv.conf to read domain from."), NULL, EC_BADCONF);
Simon Kelleyde379512004-06-22 20:23:33 +01003835
Simon Kelley3be34542004-09-11 19:12:13 +01003836 if (!(f = fopen((daemon->resolv_files)->name, "r")))
Simon Kelley5aabfc72007-08-29 11:24:47 +01003837 die(_("failed to read %s: %s"), (daemon->resolv_files)->name, EC_FILE);
Simon Kelleyde379512004-06-22 20:23:33 +01003838
3839 while ((line = fgets(buff, MAXDNAME, f)))
3840 {
3841 char *token = strtok(line, " \t\n\r");
3842
3843 if (!token || strcmp(token, "search") != 0)
3844 continue;
3845
3846 if ((token = strtok(NULL, " \t\n\r")) &&
Simon Kelley1f15b812009-10-13 17:49:32 +01003847 (daemon->domain_suffix = canonicalise_opt(token)))
Simon Kelleyde379512004-06-22 20:23:33 +01003848 break;
3849 }
Simon Kelley3be34542004-09-11 19:12:13 +01003850
Simon Kelleyde379512004-06-22 20:23:33 +01003851 fclose(f);
Simon Kelley8a911cc2004-03-16 18:35:52 +00003852
Simon Kelley3be34542004-09-11 19:12:13 +01003853 if (!daemon->domain_suffix)
Simon Kelley5aabfc72007-08-29 11:24:47 +01003854 die(_("no search directive found in %s"), (daemon->resolv_files)->name, EC_MISC);
Simon Kelleyde379512004-06-22 20:23:33 +01003855 }
Simon Kelley3d8df262005-08-29 12:19:27 +01003856
3857 if (daemon->domain_suffix)
3858 {
3859 /* add domain for any srv record without one. */
3860 struct mx_srv_record *srv;
Simon Kelleyde379512004-06-22 20:23:33 +01003861
Simon Kelley3d8df262005-08-29 12:19:27 +01003862 for (srv = daemon->mxnames; srv; srv = srv->next)
3863 if (srv->issrv &&
3864 strchr(srv->name, '.') &&
3865 strchr(srv->name, '.') == strrchr(srv->name, '.'))
3866 {
3867 strcpy(buff, srv->name);
3868 strcat(buff, ".");
3869 strcat(buff, daemon->domain_suffix);
3870 free(srv->name);
Simon Kelley824af852008-02-12 20:43:05 +00003871 srv->name = opt_string_alloc(buff);
Simon Kelley3d8df262005-08-29 12:19:27 +01003872 }
3873 }
Simon Kelley28866e92011-02-14 20:19:14 +00003874 else if (option_bool(OPT_DHCP_FQDN))
Simon Kelley9009d742008-11-14 20:04:27 +00003875 die(_("there must be a default domain when --dhcp-fqdn is set"), NULL, EC_BADCONF);
Simon Kelley7622fc02009-06-04 20:32:05 +01003876
3877 if (testmode)
3878 {
3879 fprintf(stderr, "dnsmasq: %s.\n", _("syntax check OK"));
3880 exit(0);
3881 }
Simon Kelley849a8352006-06-09 21:02:31 +01003882}