d65e29e613388cb845b4edf1c12685b8931b29cb
[dragonfly.git] / sbin / dhclient / dhclient.c
1 /*      $OpenBSD: src/sbin/dhclient/dhclient.c,v 1.127 2009/05/25 00:17:40 stevesk Exp $        */
2
3 /*
4  * Copyright 2004 Henning Brauer <henning@openbsd.org>
5  * Copyright (c) 1995, 1996, 1997, 1998, 1999
6  * The Internet Software Consortium.    All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of The Internet Software Consortium nor the names
18  *    of its contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
22  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * This software has been written for the Internet Software Consortium
36  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
37  * Enterprises.  To learn more about the Internet Software Consortium,
38  * see ``http://www.vix.com/isc''.  To learn more about Vixie
39  * Enterprises, see ``http://www.vix.com''.
40  *
41  * This client was substantially modified and enhanced by Elliot Poger
42  * for use on Linux while he was working on the MosquitoNet project at
43  * Stanford.
44  *
45  * The current version owes much to Elliot's Linux enhancements, but
46  * was substantially reorganized and partially rewritten by Ted Lemon
47  * so as to use the same networking framework that the Internet Software
48  * Consortium DHCP server uses.   Much system-specific configuration code
49  * was moved into a shell script so that as support for more operating
50  * systems is added, it will not be necessary to port and maintain
51  * system-specific configuration code to these operating systems - instead,
52  * the shell script can invoke the native tools to accomplish the same
53  * purpose.
54  */
55
56 #include <ctype.h>
57 #include <poll.h>
58 #include <pwd.h>
59 #include <unistd.h>
60
61 #include "dhcpd.h"
62 #include "privsep.h"
63
64 #define CLIENT_PATH             "PATH=/usr/bin:/usr/sbin:/bin:/sbin"
65 #define DEFAULT_LEASE_TIME      43200   /* 12 hours... */
66 #define TIME_MAX                2147483647
67 #define POLL_FAILURES           10
68 #define POLL_FAILURE_WAIT       1       /* Back off multiplier (seconds) */
69
70 time_t cur_time;
71
72 char *path_dhclient_conf = _PATH_DHCLIENT_CONF;
73 char *path_dhclient_db = NULL;
74
75 int log_perror = 1;
76 int privfd;
77 int nullfd = -1;
78 int no_daemon;
79 int unknown_ok = 1;
80 int routefd = -1;
81
82 struct iaddr iaddr_broadcast = { 4, { 255, 255, 255, 255 } };
83 struct in_addr inaddr_any;
84 struct sockaddr_in sockaddr_broadcast;
85
86 struct interface_info *ifi;
87 struct client_state *client;
88 struct client_config *config;
89
90 int              findproto(char *, int);
91 struct sockaddr *get_ifa(char *, int);
92 void             usage(void);
93 int              check_option(struct client_lease *l, int option);
94 int              ipv4addrs(char * buf);
95 int              res_hnok(const char *dn);
96 char            *option_as_string(unsigned int code, unsigned char *data, int len);
97 int              fork_privchld(int, int);
98
99 #define ROUNDUP(a) \
100             ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
101 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
102
103 time_t  scripttime;
104 static FILE *leaseFile;
105
106 int
107 findproto(char *cp, int n)
108 {
109         struct sockaddr *sa;
110         int i;
111
112         if (n == 0)
113                 return -1;
114         for (i = 1; i; i <<= 1) {
115                 if (i & n) {
116                         sa = (struct sockaddr *)cp;
117                         switch (i) {
118                         case RTA_IFA:
119                         case RTA_DST:
120                         case RTA_GATEWAY:
121                         case RTA_NETMASK:
122                                 if (sa->sa_family == AF_INET)
123                                         return AF_INET;
124                                 if (sa->sa_family == AF_INET6)
125                                         return AF_INET6;
126                                 break;
127                         case RTA_IFP:
128                                 break;
129                         }
130                         ADVANCE(cp, sa);
131                 }
132         }
133         return (-1);
134 }
135
136 struct sockaddr *
137 get_ifa(char *cp, int n)
138 {
139         struct sockaddr *sa;
140         int i;
141
142         if (n == 0)
143                 return (NULL);
144         for (i = 1; i; i <<= 1)
145                 if (i & n) {
146                         sa = (struct sockaddr *)cp;
147                         if (i == RTA_IFA)
148                                 return (sa);
149                         ADVANCE(cp, sa);
150                 }
151
152         return (NULL);
153 }
154 struct iaddr defaddr = { .len = 4 }; /* NULL is for silence warnings */
155
156 /* ARGSUSED */
157 void
158 routehandler(void)
159 {
160         int linkstat;
161         char msg[2048];
162         struct rt_msghdr *rtm;
163         struct if_msghdr *ifm;
164         struct ifa_msghdr *ifam;
165         struct if_announcemsghdr *ifan;
166         struct client_lease *l;
167         time_t t = time(NULL);
168         struct sockaddr *sa;
169         struct iaddr a;
170         ssize_t n;
171         char *errmsg, buf[64];
172
173         do {
174                 n = read(routefd, &msg, sizeof(msg));
175         } while (n == -1 && errno == EINTR);
176
177         rtm = (struct rt_msghdr *)msg;
178         if (n < sizeof(rtm->rtm_msglen) || n < rtm->rtm_msglen ||
179             rtm->rtm_version != RTM_VERSION)
180                 return;
181
182         switch (rtm->rtm_type) {
183         case RTM_NEWADDR:
184                 ifam = (struct ifa_msghdr *)rtm;
185                 if (ifam->ifam_index != ifi->index)
186                         break;
187                 if (findproto((char *)(ifam + 1), ifam->ifam_addrs) != AF_INET)
188                         break;
189                 sa = get_ifa((char *)(ifam + 1), ifam->ifam_addrs);
190                 if (sa == NULL) {
191                         errmsg = "sa == NULL";
192                         goto die;
193                 }
194
195                 if ((a.len = sizeof(struct in_addr)) > sizeof(a.iabuf))
196                         error("king bula sez: len mismatch");
197                 memcpy(a.iabuf, &((struct sockaddr_in *)sa)->sin_addr, a.len);
198                 if (addr_eq(a, defaddr))
199                         break;
200
201                 for (l = client->active; l != NULL; l = l->next)
202                         if (addr_eq(a, l->address))
203                                 break;
204
205                 if (l != NULL || (client->alias &&
206                     addr_eq(a, client->alias->address)))
207                         /* new addr is the one we set */
208                         break;
209                 snprintf(buf, sizeof(buf), "%s: %s",
210                     "new address not one we set", piaddr(a));
211                 errmsg = buf;
212                 goto die;
213         case RTM_DELADDR:
214                 ifam = (struct ifa_msghdr *)rtm;
215                 if (ifam->ifam_index != ifi->index)
216                         break;
217                 if (findproto((char *)(ifam + 1), ifam->ifam_addrs) != AF_INET)
218                         break;
219                 if (scripttime == 0 || t < scripttime + 10)
220                         break;
221                 errmsg = "interface address deleted";
222                 goto die;
223         case RTM_IFINFO:
224                 ifm = (struct if_msghdr *)rtm;
225                 if (ifm->ifm_index != ifi->index)
226                         break;
227                 if ((rtm->rtm_flags & RTF_UP) == 0) {
228                         errmsg = "interface down";
229                         goto die;
230                 }
231
232                 linkstat =
233                     LINK_STATE_IS_UP(ifm->ifm_data.ifi_link_state) ? 1 : 0;
234                 if (linkstat != ifi->linkstat) {
235                         debug("link state %s -> %s",
236                             ifi->linkstat ? "up" : "down",
237                             linkstat ? "up" : "down");
238                         ifi->linkstat = interface_link_status(ifi->name);
239                         if (ifi->linkstat) {
240                                 client->state = S_INIT;
241                                 state_reboot();
242                         }
243                 }
244                 break;
245         case RTM_IFANNOUNCE:
246                 ifan = (struct if_announcemsghdr *)rtm;
247                 if (ifan->ifan_what == IFAN_DEPARTURE &&
248                     ifan->ifan_index == ifi->index) {
249                         errmsg = "interface departure";
250                         goto die;
251                 }
252                 break;
253         default:
254                 break;
255         }
256         return;
257
258 die:
259         script_init("FAIL", NULL);
260         if (client->alias)
261                 script_write_params("alias_", client->alias);
262         script_go();
263         error("routehandler: %s", errmsg);
264 }
265
266 int
267 main(int argc, char *argv[])
268 {
269         int      ch, fd, quiet = 0, i = 0, pipe_fd[2];
270         struct passwd *pw;
271
272         /* Initially, log errors to stderr as well as to syslogd. */
273         openlog(getprogname(), LOG_PID | LOG_NDELAY, DHCPD_LOG_FACILITY);
274         setlogmask(LOG_UPTO(LOG_INFO));
275
276         while ((ch = getopt(argc, argv, "c:dl:qu")) != -1)
277                 switch (ch) {
278                 case 'c':
279                         path_dhclient_conf = optarg;
280                         break;
281                 case 'd':
282                         no_daemon = 1;
283                         break;
284                 case 'l':
285                         path_dhclient_db = optarg;
286                         break;
287                 case 'q':
288                         quiet = 1;
289                         break;
290                 case 'u':
291                         unknown_ok = 0;
292                         break;
293                 default:
294                         usage();
295                 }
296
297         argc -= optind;
298         argv += optind;
299
300         if (argc != 1)
301                 usage();
302
303         ifi = calloc(1, sizeof(*ifi));
304         if (ifi == NULL)
305                 error("ifi calloc");
306         client = calloc(1, sizeof(*client));
307         if (client == NULL)
308                 error("client calloc");
309         config = calloc(1, sizeof(*config));
310         if (config == NULL)
311                 error("config calloc");
312
313         if (strlcpy(ifi->name, argv[0], IFNAMSIZ) >= IFNAMSIZ)
314                 error("Interface name too long");
315         if (path_dhclient_db == NULL && asprintf(&path_dhclient_db, "%s.%s",
316             _PATH_DHCLIENT_DB, ifi->name) == -1)
317                 error("asprintf");
318
319         if (quiet)
320                 log_perror = 0;
321
322         tzset();
323         time(&cur_time);
324
325         memset(&sockaddr_broadcast, 0, sizeof(sockaddr_broadcast));
326         sockaddr_broadcast.sin_family = AF_INET;
327         sockaddr_broadcast.sin_port = htons(REMOTE_PORT);
328         sockaddr_broadcast.sin_addr.s_addr = INADDR_BROADCAST;
329         sockaddr_broadcast.sin_len = sizeof(sockaddr_broadcast);
330         inaddr_any.s_addr = INADDR_ANY;
331
332         read_client_conf();
333
334         if (interface_status(ifi->name) == 0) {
335                 interface_link_forceup(ifi->name);
336                 /* Give it up to 4 seconds of silent grace to find link */
337                 i = -4;
338         } else
339                 i = 0;
340
341         while (!(ifi->linkstat = interface_link_status(ifi->name))) {
342                 if (i == 0)
343                         fprintf(stderr, "%s: no link ...", ifi->name);
344                 else if (i > 0)
345                         fprintf(stderr, ".");
346                 fflush(stderr);
347                 if (++i > config->link_timeout) {
348                         fprintf(stderr, " sleeping\n");
349                         goto dispatch;
350                 }
351                 sleep(1);
352         }
353         if (i > 0)
354                 fprintf(stderr, " got link\n");
355
356  dispatch:
357         if ((nullfd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1)
358                 error("cannot open %s: %m", _PATH_DEVNULL);
359
360         if ((pw = getpwnam("_dhcp")) == NULL)
361                 error("no such user: _dhcp");
362
363         if (pipe(pipe_fd) == -1)
364                 error("pipe");
365
366         fork_privchld(pipe_fd[0], pipe_fd[1]);
367
368         close(pipe_fd[0]);
369         privfd = pipe_fd[1];
370
371         if ((fd = open(path_dhclient_db, O_RDONLY|O_EXLOCK|O_CREAT, 0)) == -1)
372                 error("can't open and lock %s: %m", path_dhclient_db);
373         read_client_leases();
374         if ((leaseFile = fopen(path_dhclient_db, "w")) == NULL)
375                 error("can't open %s: %m", path_dhclient_db);
376         rewrite_client_leases();
377         close(fd);
378
379         priv_script_init("PREINIT", NULL);
380         if (client->alias)
381                 priv_script_write_params("alias_", client->alias);
382         priv_script_go();
383
384         if ((routefd = socket(PF_ROUTE, SOCK_RAW, 0)) == -1)
385                 error("socket(PF_ROUTE, SOCK_RAW): %m");
386
387         /* set up the interface */
388         discover_interface();
389
390         if (chroot(_PATH_VAREMPTY) == -1)
391                 error("chroot");
392         if (chdir("/") == -1)
393                 error("chdir(\"/\")");
394
395         if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1)
396                 error("setresgid");
397         if (setgroups(1, &pw->pw_gid) == -1)
398                 error("setgroups");
399         if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
400                 error("setresuid");
401
402         endpwent();
403
404         setproctitle("%s", ifi->name);
405
406         if (ifi->linkstat) {
407                 client->state = S_INIT;
408                 state_reboot();
409         } else
410                 go_daemon();
411
412         dispatch();
413
414         /* not reached */
415         return (0);
416 }
417
418 void
419 usage(void)
420 {
421         fprintf(stderr, "usage: %s [-dqu] [-c file] [-l file] interface\n",
422             getprogname());
423         exit(1);
424 }
425
426 /*
427  * Individual States:
428  *
429  * Each routine is called from the dhclient_state_machine() in one of
430  * these conditions:
431  * -> entering INIT state
432  * -> recvpacket_flag == 0: timeout in this state
433  * -> otherwise: received a packet in this state
434  *
435  * Return conditions as handled by dhclient_state_machine():
436  * Returns 1, sendpacket_flag = 1: send packet, reset timer.
437  * Returns 1, sendpacket_flag = 0: just reset the timer (wait for a milestone).
438  * Returns 0: finish the nap which was interrupted for no good reason.
439  *
440  * Several per-interface variables are used to keep track of the process:
441  *   active_lease: the lease that is being used on the interface
442  *                 (null pointer if not configured yet).
443  *   offered_leases: leases corresponding to DHCPOFFER messages that have
444  *                   been sent to us by DHCP servers.
445  *   acked_leases: leases corresponding to DHCPACK messages that have been
446  *                 sent to us by DHCP servers.
447  *   sendpacket: DHCP packet we're trying to send.
448  *   destination: IP address to send sendpacket to
449  * In addition, there are several relevant per-lease variables.
450  *   T1_expiry, T2_expiry, lease_expiry: lease milestones
451  * In the active lease, these control the process of renewing the lease;
452  * In leases on the acked_leases list, this simply determines when we
453  * can no longer legitimately use the lease.
454  */
455 void
456 state_reboot(void)
457 {
458         /* If we don't remember an active lease, go straight to INIT. */
459         if (!client->active || client->active->is_bootp) {
460                 state_init();
461                 return;
462         }
463
464         /* We are in the rebooting state. */
465         client->state = S_REBOOTING;
466
467         /* make_request doesn't initialize xid because it normally comes
468            from the DHCPDISCOVER, but we haven't sent a DHCPDISCOVER,
469            so pick an xid now. */
470         client->xid = arc4random();
471
472         /* Make a DHCPREQUEST packet, and set appropriate per-interface
473            flags. */
474         make_request(client->active);
475         client->destination = iaddr_broadcast;
476         client->first_sending = cur_time;
477         client->interval = config->initial_interval;
478
479         /* Zap the medium list... */
480         client->medium = NULL;
481
482         /* Send out the first DHCPREQUEST packet. */
483         send_request();
484 }
485
486 /*
487  * Called when a lease has completely expired and we've
488  * been unable to renew it.
489  */
490 void
491 state_init(void)
492 {
493         /* Make a DHCPDISCOVER packet, and set appropriate per-interface
494            flags. */
495         make_discover(client->active);
496         client->xid = client->packet.xid;
497         client->destination = iaddr_broadcast;
498         client->state = S_SELECTING;
499         client->first_sending = cur_time;
500         client->interval = config->initial_interval;
501
502         /* Add an immediate timeout to cause the first DHCPDISCOVER packet
503            to go out. */
504         send_discover();
505 }
506
507 /*
508  * state_selecting is called when one or more DHCPOFFER packets
509  * have been received and a configurable period of time has passed.
510  */
511 void
512 state_selecting(void)
513 {
514         struct client_lease *lp, *next, *picked;
515
516         /* Cancel state_selecting and send_discover timeouts, since either
517            one could have got us here. */
518         cancel_timeout(state_selecting);
519         cancel_timeout(send_discover);
520
521         /* We have received one or more DHCPOFFER packets.   Currently,
522            the only criterion by which we judge leases is whether or
523            not we get a response when we arp for them. */
524         picked = NULL;
525         for (lp = client->offered_leases; lp; lp = next) {
526                 next = lp->next;
527
528                 /* Check to see if we got an ARPREPLY for the address
529                    in this particular lease. */
530                 if (!picked) {
531                         script_init("ARPCHECK", lp->medium);
532                         script_write_params("check_", lp);
533
534                         /* If the ARPCHECK code detects another
535                            machine using the offered address, it exits
536                            nonzero.  We need to send a DHCPDECLINE and
537                            toss the lease. */
538                         if (script_go()) {
539                                 make_decline(lp);
540                                 send_decline();
541                                 goto freeit;
542                         }
543                         picked = lp;
544                         picked->next = NULL;
545                 } else {
546 freeit:
547                         free_client_lease(lp);
548                 }
549         }
550         client->offered_leases = NULL;
551
552         /* If we just tossed all the leases we were offered, go back
553            to square one. */
554         if (!picked) {
555                 client->state = S_INIT;
556                 state_init();
557                 return;
558         }
559
560         /* If it was a BOOTREPLY, we can just take the address right now. */
561         if (!picked->options[DHO_DHCP_MESSAGE_TYPE].len) {
562                 client->new = picked;
563
564                 /* Make up some lease expiry times
565                    XXX these should be configurable. */
566                 client->new->expiry = cur_time + 12000;
567                 client->new->renewal += cur_time + 8000;
568                 client->new->rebind += cur_time + 10000;
569
570                 client->state = S_REQUESTING;
571
572                 /* Bind to the address we received. */
573                 bind_lease();
574                 return;
575         }
576
577         /* Go to the REQUESTING state. */
578         client->destination = iaddr_broadcast;
579         client->state = S_REQUESTING;
580         client->first_sending = cur_time;
581         client->interval = config->initial_interval;
582
583         /* Make a DHCPREQUEST packet from the lease we picked. */
584         make_request(picked);
585         client->xid = client->packet.xid;
586
587         /* Toss the lease we picked - we'll get it back in a DHCPACK. */
588         free_client_lease(picked);
589
590         /* Add an immediate timeout to send the first DHCPREQUEST packet. */
591         send_request();
592 }
593
594 void
595 dhcpack(struct iaddr client_addr, struct option_data *options)
596 {
597         struct client_lease *lease;
598
599
600         if (client->state != S_REBOOTING &&
601             client->state != S_REQUESTING &&
602             client->state != S_RENEWING &&
603             client->state != S_REBINDING)
604                 return;
605
606
607         lease = packet_to_lease(options);
608         if (!lease) {
609                 note("packet_to_lease failed.");
610                 return;
611         }
612
613         client->new = lease;
614
615         /* Stop resending DHCPREQUEST. */
616         cancel_timeout(send_request);
617
618         /* Figure out the lease time. */
619         if (client->new->options[DHO_DHCP_LEASE_TIME].data)
620                 client->new->expiry =
621                     getULong(client->new->options[DHO_DHCP_LEASE_TIME].data);
622         else
623                 client->new->expiry = DEFAULT_LEASE_TIME;
624         /* A number that looks negative here is really just very large,
625            because the lease expiry offset is unsigned. */
626         if (client->new->expiry < 0)
627                 client->new->expiry = TIME_MAX;
628         /* XXX should be fixed by resetting the client state */
629         if (client->new->expiry < 60)
630                 client->new->expiry = 60;
631
632         /* Take the server-provided renewal time if there is one;
633            otherwise figure it out according to the spec. */
634         if (client->new->options[DHO_DHCP_RENEWAL_TIME].len)
635                 client->new->renewal =
636                     getULong(client->new->options[DHO_DHCP_RENEWAL_TIME].data);
637         else
638                 client->new->renewal = client->new->expiry / 2;
639
640         /* Same deal with the rebind time. */
641         if (client->new->options[DHO_DHCP_REBINDING_TIME].len)
642                 client->new->rebind =
643                     getULong(client->new->options[DHO_DHCP_REBINDING_TIME].data);
644         else
645                 client->new->rebind = client->new->renewal +
646                     client->new->renewal / 2 + client->new->renewal / 4;
647
648         client->new->expiry += cur_time;
649         /* Lease lengths can never be negative. */
650         if (client->new->expiry < cur_time)
651                 client->new->expiry = TIME_MAX;
652         client->new->renewal += cur_time;
653         if (client->new->renewal < cur_time)
654                 client->new->renewal = TIME_MAX;
655         client->new->rebind += cur_time;
656         if (client->new->rebind < cur_time)
657                 client->new->rebind = TIME_MAX;
658
659         bind_lease();
660 }
661
662 void
663 bind_lease(void)
664 {
665         /* Remember the medium. */
666         client->new->medium = client->medium;
667
668         /* Write out the new lease. */
669         write_client_lease(client->new, 0);
670
671         /* Run the client script with the new parameters. */
672         script_init((client->state == S_REQUESTING ? "BOUND" :
673             (client->state == S_RENEWING ? "RENEW" :
674             (client->state == S_REBOOTING ? "REBOOT" : "REBIND"))),
675             client->new->medium);
676         if (client->active && client->state != S_REBOOTING)
677                 script_write_params("old_", client->active);
678         script_write_params("new_", client->new);
679         if (client->alias)
680                 script_write_params("alias_", client->alias);
681         script_go();
682
683         /* Replace the old active lease with the new one. */
684         if (client->active)
685                 free_client_lease(client->active);
686         client->active = client->new;
687         client->new = NULL;
688
689         /* Set up a timeout to start the renewal process. */
690         add_timeout(client->active->renewal, state_bound);
691
692         note("bound to %s -- renewal in %ld seconds.",
693             piaddr(client->active->address),
694             client->active->renewal - cur_time);
695         client->state = S_BOUND;
696         reinitialize_interface();
697         go_daemon();
698 }
699
700 /*
701  * state_bound is called when we've successfully bound to a particular
702  * lease, but the renewal time on that lease has expired.   We are
703  * expected to unicast a DHCPREQUEST to the server that gave us our
704  * original lease.
705  */
706 void
707 state_bound(void)
708 {
709         /* T1 has expired. */
710         make_request(client->active);
711         client->xid = client->packet.xid;
712
713         if (client->active->options[DHO_DHCP_SERVER_IDENTIFIER].len == 4) {
714                 memcpy(client->destination.iabuf,
715                     client->active->options[DHO_DHCP_SERVER_IDENTIFIER].data,
716                     4);
717                 client->destination.len = 4;
718         } else
719                 client->destination = iaddr_broadcast;
720
721         client->first_sending = cur_time;
722         client->interval = config->initial_interval;
723         client->state = S_RENEWING;
724
725         /* Send the first packet immediately. */
726         send_request();
727 }
728
729 void
730 dhcpoffer(struct iaddr client_addr, struct option_data *options)
731 {
732         struct client_lease *lease, *lp;
733         int i;
734         int arp_timeout_needed, stop_selecting;
735         char *name = options[DHO_DHCP_MESSAGE_TYPE].len ? "DHCPOFFER" :
736             "BOOTREPLY";
737
738
739         if (client->state != S_SELECTING)
740                 return;
741
742
743         /* If this lease doesn't supply the minimum required parameters,
744            blow it off. */
745         for (i = 0; config->required_options[i]; i++) {
746                 if (!options[config->required_options[i]].len) {
747                         note("%s isn't satisfactory.", name);
748                         return;
749                 }
750         }
751
752         /* If we've already seen this lease, don't record it again. */
753         for (lease = client->offered_leases;
754             lease; lease = lease->next) {
755                 if (lease->address.len == sizeof(client->packet.yiaddr) &&
756                     !memcmp(lease->address.iabuf,
757                     &client->packet.yiaddr, lease->address.len)) {
758                         debug("%s already seen.", name);
759                         return;
760                 }
761         }
762
763         lease = packet_to_lease(options);
764         if (!lease) {
765                 note("packet_to_lease failed.");
766                 return;
767         }
768
769         /* If this lease was acquired through a BOOTREPLY, record that
770            fact. */
771         if (!options[DHO_DHCP_MESSAGE_TYPE].len)
772                 lease->is_bootp = 1;
773
774         /* Record the medium under which this lease was offered. */
775         lease->medium = client->medium;
776
777         /* Send out an ARP Request for the offered IP address. */
778         script_init("ARPSEND", lease->medium);
779         script_write_params("check_", lease);
780         /* If the script can't send an ARP request without waiting,
781            we'll be waiting when we do the ARPCHECK, so don't wait now. */
782         if (script_go())
783                 arp_timeout_needed = 0;
784         else
785                 arp_timeout_needed = 2;
786
787         /* Figure out when we're supposed to stop selecting. */
788         stop_selecting = client->first_sending + config->select_interval;
789
790         /* If this is the lease we asked for, put it at the head of the
791            list, and don't mess with the arp request timeout. */
792         if (lease->address.len == client->requested_address.len &&
793             !memcmp(lease->address.iabuf,
794             client->requested_address.iabuf,
795             client->requested_address.len)) {
796                 lease->next = client->offered_leases;
797                 client->offered_leases = lease;
798         } else {
799                 /* If we already have an offer, and arping for this
800                    offer would take us past the selection timeout,
801                    then don't extend the timeout - just hope for the
802                    best. */
803                 if (client->offered_leases &&
804                     (cur_time + arp_timeout_needed) > stop_selecting)
805                         arp_timeout_needed = 0;
806
807                 /* Put the lease at the end of the list. */
808                 lease->next = NULL;
809                 if (!client->offered_leases)
810                         client->offered_leases = lease;
811                 else {
812                         for (lp = client->offered_leases; lp->next;
813                             lp = lp->next)
814                                 ;       /* nothing */
815                         lp->next = lease;
816                 }
817         }
818
819         /* If we're supposed to stop selecting before we've had time
820            to wait for the ARPREPLY, add some delay to wait for
821            the ARPREPLY. */
822         if (stop_selecting - cur_time < arp_timeout_needed)
823                 stop_selecting = cur_time + arp_timeout_needed;
824
825         /* If the selecting interval has expired, go immediately to
826            state_selecting().  Otherwise, time out into
827            state_selecting at the select interval. */
828         if (stop_selecting <= 0)
829                 state_selecting();
830         else {
831                 add_timeout(stop_selecting, state_selecting);
832                 cancel_timeout(send_discover);
833         }
834 }
835
836 /*
837  * Allocate a client_lease structure and initialize it from the
838  * parameters in the specified packet.
839  */
840 struct client_lease *
841 packet_to_lease(struct option_data *options)
842 {
843         struct client_lease *lease;
844         int i;
845
846         lease = malloc(sizeof(struct client_lease));
847
848         if (!lease) {
849                 warning("dhcpoffer: no memory to record lease.");
850                 return (NULL);
851         }
852
853         memset(lease, 0, sizeof(*lease));
854
855         /* Copy the lease options. */
856         for (i = 0; i < 256; i++) {
857                 if (options[i].len) {
858                         lease->options[i] = options[i];
859                         options[i].data = NULL;
860                         options[i].len = 0;
861                         if (!check_option(lease, i)) {
862                                 warning("Invalid lease option - ignoring offer");
863                                 free_client_lease(lease);
864                                 return (NULL);
865                         }
866                 }
867         }
868
869         lease->address.len = sizeof(client->packet.yiaddr);
870         memcpy(lease->address.iabuf, &client->packet.yiaddr,
871             lease->address.len);
872
873         /* If the server name was filled out, copy it. */
874         if ((!lease->options[DHO_DHCP_OPTION_OVERLOAD].len ||
875             !(lease->options[DHO_DHCP_OPTION_OVERLOAD].data[0] & 2)) &&
876             client->packet.sname[0]) {
877                 lease->server_name = malloc(DHCP_SNAME_LEN + 1);
878                 if (!lease->server_name) {
879                         warning("dhcpoffer: no memory for server name.");
880                         free_client_lease(lease);
881                         return (NULL);
882                 }
883                 memcpy(lease->server_name, client->packet.sname,
884                     DHCP_SNAME_LEN);
885                 lease->server_name[DHCP_SNAME_LEN] = '\0';
886                 if (!res_hnok(lease->server_name)) {
887                         warning("Bogus server name %s", lease->server_name);
888                         free(lease->server_name);
889                         lease->server_name = NULL;
890                 }
891         }
892
893         /* Ditto for the filename. */
894         if ((!lease->options[DHO_DHCP_OPTION_OVERLOAD].len ||
895             !(lease->options[DHO_DHCP_OPTION_OVERLOAD].data[0] & 1)) &&
896             client->packet.file[0]) {
897                 /* Don't count on the NUL terminator. */
898                 lease->filename = malloc(DHCP_FILE_LEN + 1);
899                 if (!lease->filename) {
900                         warning("dhcpoffer: no memory for filename.");
901                         free_client_lease(lease);
902                         return (NULL);
903                 }
904                 memcpy(lease->filename, client->packet.file, DHCP_FILE_LEN);
905                 lease->filename[DHCP_FILE_LEN] = '\0';
906         }
907         return lease;
908 }
909
910 void
911 dhcpnak(struct iaddr client_addr, struct option_data *options)
912 {
913
914         if (client->state != S_REBOOTING &&
915             client->state != S_REQUESTING &&
916             client->state != S_RENEWING &&
917             client->state != S_REBINDING)
918                 return;
919
920
921         if (!client->active) {
922                 note("DHCPNAK with no active lease.");
923                 return;
924         }
925
926         free_client_lease(client->active);
927         client->active = NULL;
928
929         /* Stop sending DHCPREQUEST packets... */
930         cancel_timeout(send_request);
931
932         client->state = S_INIT;
933         state_init();
934 }
935
936 /*
937  * Send out a DHCPDISCOVER packet, and set a timeout to send out another
938  * one after the right interval has expired.  If we don't get an offer by
939  * the time we reach the panic interval, call the panic function.
940  */
941 void
942 send_discover(void)
943 {
944         int interval, increase = 1;
945
946         /* Figure out how long it's been since we started transmitting. */
947         interval = cur_time - client->first_sending;
948
949         /* If we're past the panic timeout, call the script and tell it
950            we haven't found anything for this interface yet. */
951         if (interval > config->timeout) {
952                 state_panic();
953                 return;
954         }
955
956         /* If we're selecting media, try the whole list before doing
957            the exponential backoff, but if we've already received an
958            offer, stop looping, because we obviously have it right. */
959         if (!client->offered_leases && config->media) {
960                 int fail = 0;
961 again:
962                 if (client->medium) {
963                         client->medium = client->medium->next;
964                         increase = 0;
965                 }
966                 if (!client->medium) {
967                         if (fail)
968                                 error("No valid media types for %s!", ifi->name);
969                         client->medium = config->media;
970                         increase = 1;
971                 }
972
973                 note("Trying medium \"%s\" %d", client->medium->string,
974                     increase);
975                 script_init("MEDIUM", client->medium);
976                 if (script_go())
977                         goto again;
978         }
979
980         /*
981          * If we're supposed to increase the interval, do so.  If it's
982          * currently zero (i.e., we haven't sent any packets yet), set
983          * it to initial_interval; otherwise, add to it a random
984          * number between zero and two times itself.  On average, this
985          * means that it will double with every transmission.
986          */
987         if (increase) {
988                 if (!client->interval)
989                         client->interval = config->initial_interval;
990                 else {
991                         client->interval += (arc4random() >> 2) %
992                             (2 * client->interval);
993                 }
994
995                 /* Don't backoff past cutoff. */
996                 if (client->interval > config->backoff_cutoff)
997                         client->interval = ((config->backoff_cutoff / 2)
998                                  + ((arc4random() >> 2) %
999                                     config->backoff_cutoff));
1000         } else if (!client->interval)
1001                 client->interval = config->initial_interval;
1002
1003         /* If the backoff would take us to the panic timeout, just use that
1004            as the interval. */
1005         if (cur_time + client->interval >
1006             client->first_sending + config->timeout)
1007                 client->interval = (client->first_sending +
1008                          config->timeout) - cur_time + 1;
1009
1010         /* Record the number of seconds since we started sending. */
1011         if (interval < 65536)
1012                 client->packet.secs = htons(interval);
1013         else
1014                 client->packet.secs = htons(65535);
1015         client->secs = client->packet.secs;
1016
1017         note("DHCPDISCOVER on %s to %s port %d interval %ld",
1018             ifi->name, inet_ntoa(sockaddr_broadcast.sin_addr),
1019             ntohs(sockaddr_broadcast.sin_port), client->interval);
1020
1021         /* Send out a packet. */
1022         send_packet(inaddr_any, &sockaddr_broadcast, NULL);
1023
1024         add_timeout(cur_time + client->interval, send_discover);
1025 }
1026
1027 /*
1028  * state_panic gets called if we haven't received any offers in a preset
1029  * amount of time.   When this happens, we try to use existing leases
1030  * that haven't yet expired, and failing that, we call the client script
1031  * and hope it can do something.
1032  */
1033 void
1034 state_panic(void)
1035 {
1036         struct client_lease *loop = client->active;
1037         struct client_lease *lp;
1038
1039         note("No DHCPOFFERS received.");
1040
1041         /* We may not have an active lease, but we may have some
1042            predefined leases that we can try. */
1043         if (!client->active && client->leases)
1044                 goto activate_next;
1045
1046         /* Run through the list of leases and see if one can be used. */
1047         while (client->active) {
1048                 if (client->active->expiry > cur_time) {
1049                         note("Trying recorded lease %s",
1050                             piaddr(client->active->address));
1051                         /* Run the client script with the existing
1052                            parameters. */
1053                         script_init("TIMEOUT",
1054                             client->active->medium);
1055                         script_write_params("new_", client->active);
1056                         if (client->alias)
1057                                 script_write_params("alias_",
1058                                     client->alias);
1059
1060                         /* If the old lease is still good and doesn't
1061                            yet need renewal, go into BOUND state and
1062                            timeout at the renewal time. */
1063                         if (!script_go()) {
1064                                 if (cur_time <
1065                                     client->active->renewal) {
1066                                         client->state = S_BOUND;
1067                                         note("bound: renewal in %ld seconds.",
1068                                             client->active->renewal -
1069                                             cur_time);
1070                                         add_timeout(client->active->renewal,
1071                                             state_bound);
1072                                 } else {
1073                                         client->state = S_BOUND;
1074                                         note("bound: immediate renewal.");
1075                                         state_bound();
1076                                 }
1077                                 reinitialize_interface();
1078                                 go_daemon();
1079                                 return;
1080                         }
1081                 }
1082
1083                 /* If there are no other leases, give up. */
1084                 if (!client->leases) {
1085                         client->leases = client->active;
1086                         client->active = NULL;
1087                         break;
1088                 }
1089
1090 activate_next:
1091                 /* Otherwise, put the active lease at the end of the
1092                    lease list, and try another lease.. */
1093                 for (lp = client->leases; lp->next; lp = lp->next)
1094                         ;
1095                 lp->next = client->active;
1096                 if (lp->next)
1097                         lp->next->next = NULL;
1098                 client->active = client->leases;
1099                 client->leases = client->leases->next;
1100
1101                 /* If we already tried this lease, we've exhausted the
1102                    set of leases, so we might as well give up for
1103                    now. */
1104                 if (client->active == loop)
1105                         break;
1106                 else if (!loop)
1107                         loop = client->active;
1108         }
1109
1110         /* No leases were available, or what was available didn't work, so
1111            tell the shell script that we failed to allocate an address,
1112            and try again later. */
1113         note("No working leases in persistent database - sleeping.");
1114         script_init("FAIL", NULL);
1115         if (client->alias)
1116                 script_write_params("alias_", client->alias);
1117         script_go();
1118         client->state = S_INIT;
1119         add_timeout(cur_time + config->retry_interval, state_init);
1120         go_daemon();
1121 }
1122
1123 void
1124 send_request(void)
1125 {
1126         struct sockaddr_in destination;
1127         struct in_addr from;
1128         int interval;
1129
1130         /* Figure out how long it's been since we started transmitting. */
1131         interval = cur_time - client->first_sending;
1132
1133         /* If we're in the INIT-REBOOT or REQUESTING state and we're
1134            past the reboot timeout, go to INIT and see if we can
1135            DISCOVER an address... */
1136         /* XXX In the INIT-REBOOT state, if we don't get an ACK, it
1137            means either that we're on a network with no DHCP server,
1138            or that our server is down.  In the latter case, assuming
1139            that there is a backup DHCP server, DHCPDISCOVER will get
1140            us a new address, but we could also have successfully
1141            reused our old address.  In the former case, we're hosed
1142            anyway.  This is not a win-prone situation. */
1143         if ((client->state == S_REBOOTING ||
1144             client->state == S_REQUESTING) &&
1145             interval > config->reboot_timeout) {
1146 cancel:
1147                 client->state = S_INIT;
1148                 cancel_timeout(send_request);
1149                 state_init();
1150                 return;
1151         }
1152
1153         /* If we're in the reboot state, make sure the media is set up
1154            correctly. */
1155         if (client->state == S_REBOOTING &&
1156             !client->medium &&
1157             client->active->medium) {
1158                 script_init("MEDIUM", client->active->medium);
1159
1160                 /* If the medium we chose won't fly, go to INIT state. */
1161                 if (script_go())
1162                         goto cancel;
1163
1164                 /* Record the medium. */
1165                 client->medium = client->active->medium;
1166         }
1167
1168         /* If the lease has expired, relinquish the address and go back
1169            to the INIT state. */
1170         if (client->state != S_REQUESTING &&
1171             cur_time > client->active->expiry) {
1172                 /* Run the client script with the new parameters. */
1173                 script_init("EXPIRE", NULL);
1174                 script_write_params("old_", client->active);
1175                 if (client->alias)
1176                         script_write_params("alias_", client->alias);
1177                 script_go();
1178
1179                 /* Now do a preinit on the interface so that we can
1180                    discover a new address. */
1181                 script_init("PREINIT", NULL);
1182                 if (client->alias)
1183                         script_write_params("alias_", client->alias);
1184                 script_go();
1185
1186                 client->state = S_INIT;
1187                 state_init();
1188                 return;
1189         }
1190
1191         /* Do the exponential backoff... */
1192         if (!client->interval)
1193                 client->interval = config->initial_interval;
1194         else
1195                 client->interval += ((arc4random() >> 2) %
1196                     (2 * client->interval));
1197
1198         /* Don't backoff past cutoff. */
1199         if (client->interval > config->backoff_cutoff)
1200                 client->interval = ((config->backoff_cutoff / 2) +
1201                     ((arc4random() >> 2) % client->interval));
1202
1203         /* If the backoff would take us to the expiry time, just set the
1204            timeout to the expiry time. */
1205         if (client->state != S_REQUESTING && cur_time + client->interval >
1206             client->active->expiry)
1207                 client->interval = client->active->expiry - cur_time + 1;
1208
1209         /* If the lease T2 time has elapsed, or if we're not yet bound,
1210            broadcast the DHCPREQUEST rather than unicasting. */
1211         memset(&destination, 0, sizeof(destination));
1212         if (client->state == S_REQUESTING ||
1213             client->state == S_REBOOTING ||
1214             cur_time > client->active->rebind)
1215                 destination.sin_addr.s_addr = INADDR_BROADCAST;
1216         else
1217                 memcpy(&destination.sin_addr.s_addr, client->destination.iabuf,
1218                     sizeof(destination.sin_addr.s_addr));
1219         destination.sin_port = htons(REMOTE_PORT);
1220         destination.sin_family = AF_INET;
1221         destination.sin_len = sizeof(destination);
1222
1223         if (client->state != S_REQUESTING)
1224                 memcpy(&from, client->active->address.iabuf, sizeof(from));
1225         else
1226                 from.s_addr = INADDR_ANY;
1227
1228         /* Record the number of seconds since we started sending. */
1229         if (client->state == S_REQUESTING)
1230                 client->packet.secs = client->secs;
1231         else {
1232                 if (interval < 65536)
1233                         client->packet.secs = htons(interval);
1234                 else
1235                         client->packet.secs = htons(65535);
1236         }
1237
1238         note("DHCPREQUEST on %s to %s port %d", ifi->name,
1239             inet_ntoa(destination.sin_addr), ntohs(destination.sin_port));
1240
1241         /* Send out a packet. */
1242         send_packet(from, &destination, NULL);
1243
1244         add_timeout(cur_time + client->interval, send_request);
1245 }
1246
1247 void
1248 send_decline(void)
1249 {
1250         note("DHCPDECLINE on %s to %s port %d", ifi->name,
1251             inet_ntoa(sockaddr_broadcast.sin_addr),
1252             ntohs(sockaddr_broadcast.sin_port));
1253
1254         /* Send out a packet. */
1255         send_packet(inaddr_any, &sockaddr_broadcast, NULL);
1256 }
1257
1258 void
1259 make_discover(struct client_lease *lease)
1260 {
1261         unsigned char discover = DHCPDISCOVER;
1262         struct option_data options[256];
1263         int i;
1264
1265         memset(options, 0, sizeof(options));
1266         memset(&client->packet, 0, sizeof(client->packet));
1267
1268         /* Set DHCP_MESSAGE_TYPE to DHCPDISCOVER */
1269         i = DHO_DHCP_MESSAGE_TYPE;
1270         options[i].data = &discover;
1271         options[i].len = sizeof(discover);
1272
1273         /* Request the options we want */
1274         i  = DHO_DHCP_PARAMETER_REQUEST_LIST;
1275         options[i].data = config->requested_options;
1276         options[i].len = config->requested_option_count;
1277
1278         /* If we had an address, try to get it again. */
1279         if (lease) {
1280                 client->requested_address = lease->address;
1281                 i = DHO_DHCP_REQUESTED_ADDRESS;
1282                 options[i].data = lease->address.iabuf;
1283                 options[i].len = lease->address.len;
1284         } else
1285                 client->requested_address.len = 0;
1286
1287         /* Send any options requested in the config file. */
1288         for (i = 0; i < 256; i++)
1289                 if (!options[i].data &&
1290                     config->send_options[i].data) {
1291                         options[i].data = config->send_options[i].data;
1292                         options[i].len = config->send_options[i].len;
1293                 }
1294
1295         /* Set up the option buffer to fit in a minimal UDP packet. */
1296         i = cons_options(client->packet.options, 576 - DHCP_FIXED_LEN,
1297             options);
1298         if (i == -1 || client->packet.options[i] != DHO_END)
1299                 error("options do not fit in DHCPDISCOVER packet.");
1300         client->packet_length = DHCP_FIXED_NON_UDP+i+1;
1301         if (client->packet_length < BOOTP_MIN_LEN)
1302                 client->packet_length = BOOTP_MIN_LEN;
1303
1304         client->packet.op = BOOTREQUEST;
1305         client->packet.htype = ifi->hw_address.htype;
1306         client->packet.hlen = ifi->hw_address.hlen;
1307         client->packet.hops = 0;
1308         client->packet.xid = arc4random();
1309         client->packet.secs = 0; /* filled in by send_discover. */
1310         client->packet.flags = 0;
1311
1312         memset(&client->packet.ciaddr, 0, sizeof(client->packet.ciaddr));
1313         memset(&client->packet.yiaddr, 0, sizeof(client->packet.yiaddr));
1314         memset(&client->packet.siaddr, 0, sizeof(client->packet.siaddr));
1315         memset(&client->packet.giaddr, 0, sizeof(client->packet.giaddr));
1316         memcpy(client->packet.chaddr, ifi->hw_address.haddr,
1317             ifi->hw_address.hlen);
1318 }
1319
1320 void
1321 make_request(struct client_lease * lease)
1322 {
1323         unsigned char request = DHCPREQUEST;
1324         struct option_data options[256];
1325         int i;
1326
1327         memset(options, 0, sizeof(options));
1328         memset(&client->packet, 0, sizeof(client->packet));
1329
1330         /* Set DHCP_MESSAGE_TYPE to DHCPREQUEST */
1331         i = DHO_DHCP_MESSAGE_TYPE;
1332         options[i].data = &request;
1333         options[i].len = sizeof(request);
1334
1335         /* Request the options we want */
1336         i = DHO_DHCP_PARAMETER_REQUEST_LIST;
1337         options[i].data = config->requested_options;
1338         options[i].len = config->requested_option_count;
1339
1340         /* If we are requesting an address that hasn't yet been assigned
1341            to us, use the DHCP Requested Address option. */
1342         if (client->state == S_REQUESTING) {
1343                 /* Send back the server identifier... */
1344                 i = DHO_DHCP_SERVER_IDENTIFIER;
1345                 options[i].data = lease->options[i].data;
1346                 options[i].len = lease->options[i].len;
1347         }
1348         if (client->state == S_REQUESTING ||
1349             client->state == S_REBOOTING) {
1350                 client->requested_address = lease->address;
1351                 i = DHO_DHCP_REQUESTED_ADDRESS;
1352                 options[i].data = lease->address.iabuf;
1353                 options[i].len = lease->address.len;
1354         } else
1355                 client->requested_address.len = 0;
1356
1357         /* Send any options requested in the config file. */
1358         for (i = 0; i < 256; i++)
1359                 if (!options[i].data && config->send_options[i].data) {
1360                         options[i].data = config->send_options[i].data;
1361                         options[i].len = config->send_options[i].len;
1362                 }
1363
1364         /* Set up the option buffer to fit in a minimal UDP packet. */
1365         i = cons_options(client->packet.options, 576 - DHCP_FIXED_LEN,
1366             options);
1367         if (i == -1 || client->packet.options[i] != DHO_END)
1368                 error("options do not fit in DHCPREQUEST packet.");
1369         client->packet_length = DHCP_FIXED_NON_UDP+i+1;
1370         if (client->packet_length < BOOTP_MIN_LEN)
1371                 client->packet_length = BOOTP_MIN_LEN;
1372
1373         client->packet.op = BOOTREQUEST;
1374         client->packet.htype = ifi->hw_address.htype;
1375         client->packet.hlen = ifi->hw_address.hlen;
1376         client->packet.hops = 0;
1377         client->packet.xid = client->xid;
1378         client->packet.secs = 0; /* Filled in by send_request. */
1379         client->packet.flags = 0;
1380
1381         /* If we own the address we're requesting, put it in ciaddr;
1382            otherwise set ciaddr to zero. */
1383         if (client->state == S_BOUND ||
1384             client->state == S_RENEWING ||
1385             client->state == S_REBINDING) {
1386                 memcpy(&client->packet.ciaddr,
1387                     lease->address.iabuf, lease->address.len);
1388         } else {
1389                 memset(&client->packet.ciaddr, 0,
1390                     sizeof(client->packet.ciaddr));
1391         }
1392
1393         memset(&client->packet.yiaddr, 0, sizeof(client->packet.yiaddr));
1394         memset(&client->packet.siaddr, 0, sizeof(client->packet.siaddr));
1395         memset(&client->packet.giaddr, 0, sizeof(client->packet.giaddr));
1396         memcpy(client->packet.chaddr, ifi->hw_address.haddr,
1397             ifi->hw_address.hlen);
1398 }
1399
1400 void
1401 make_decline(struct client_lease *lease)
1402 {
1403         struct option_data options[256];
1404         unsigned char decline = DHCPDECLINE;
1405         int i;
1406
1407         memset(options, 0, sizeof(options));
1408         memset(&client->packet, 0, sizeof(client->packet));
1409
1410         /* Set DHCP_MESSAGE_TYPE to DHCPDECLINE */
1411         i = DHO_DHCP_MESSAGE_TYPE;
1412         options[i].data = &decline;
1413         options[i].len = sizeof(decline);
1414
1415         /* Send back the server identifier... */
1416         i = DHO_DHCP_SERVER_IDENTIFIER;
1417         options[i].data = lease->options[i].data;
1418         options[i].len = lease->options[i].len;
1419
1420         /* Send back the address we're declining. */
1421         i = DHO_DHCP_REQUESTED_ADDRESS;
1422         options[i].data = lease->address.iabuf;
1423         options[i].len = lease->address.len;
1424
1425         /* Send the uid if the user supplied one. */
1426         i = DHO_DHCP_CLIENT_IDENTIFIER;
1427         if (config->send_options[i].len) {
1428                 options[i].data = config->send_options[i].data;
1429                 options[i].len = config->send_options[i].len;
1430         }
1431
1432         /* Set up the option buffer to fit in a minimal UDP packet. */
1433         i = cons_options(client->packet.options, 576 - DHCP_FIXED_LEN,
1434             options);
1435         if (i == -1 || client->packet.options[i] != DHO_END)
1436                 error("options do not fit in DHCPDECLINE packet.");
1437         client->packet_length = DHCP_FIXED_NON_UDP+i+1;
1438         if (client->packet_length < BOOTP_MIN_LEN)
1439                 client->packet_length = BOOTP_MIN_LEN;
1440
1441         client->packet.op = BOOTREQUEST;
1442         client->packet.htype = ifi->hw_address.htype;
1443         client->packet.hlen = ifi->hw_address.hlen;
1444         client->packet.hops = 0;
1445         client->packet.xid = client->xid;
1446         client->packet.secs = 0; /* Filled in by send_request. */
1447         client->packet.flags = 0;
1448
1449         /* ciaddr must always be zero. */
1450         memset(&client->packet.ciaddr, 0, sizeof(client->packet.ciaddr));
1451         memset(&client->packet.yiaddr, 0, sizeof(client->packet.yiaddr));
1452         memset(&client->packet.siaddr, 0, sizeof(client->packet.siaddr));
1453         memset(&client->packet.giaddr, 0, sizeof(client->packet.giaddr));
1454         memcpy(client->packet.chaddr, ifi->hw_address.haddr,
1455             ifi->hw_address.hlen);
1456 }
1457
1458 void
1459 free_client_lease(struct client_lease *lease)
1460 {
1461         int i;
1462
1463         if (lease->server_name)
1464                 free(lease->server_name);
1465         if (lease->filename)
1466                 free(lease->filename);
1467         for (i = 0; i < 256; i++) {
1468                 if (lease->options[i].len)
1469                         free(lease->options[i].data);
1470         }
1471         free(lease);
1472 }
1473
1474 void
1475 rewrite_client_leases(void)
1476 {
1477         struct client_lease *lp;
1478
1479         if (!leaseFile) /* XXX */
1480                 error("lease file not open");
1481
1482         fflush(leaseFile);
1483         rewind(leaseFile);
1484
1485         for (lp = client->leases; lp; lp = lp->next)
1486                 write_client_lease(lp, 1);
1487         if (client->active)
1488                 write_client_lease(client->active, 1);
1489
1490         fflush(leaseFile);
1491         ftruncate(fileno(leaseFile), ftello(leaseFile));
1492         fsync(fileno(leaseFile));
1493 }
1494
1495 void
1496 write_client_lease(struct client_lease *lease, int rewrite)
1497 {
1498         static int leases_written;
1499         struct tm *t;
1500         int i;
1501
1502         if (!rewrite) {
1503                 if (leases_written++ > 20) {
1504                         rewrite_client_leases();
1505                         leases_written = 0;
1506                 }
1507         }
1508
1509         /* If the lease came from the config file, we don't need to stash
1510            a copy in the lease database. */
1511         if (lease->is_static)
1512                 return;
1513
1514         if (!leaseFile) /* XXX */
1515                 error("lease file not open");
1516
1517         fprintf(leaseFile, "lease {\n");
1518         if (lease->is_bootp)
1519                 fprintf(leaseFile, "  bootp;\n");
1520         fprintf(leaseFile, "  interface \"%s\";\n", ifi->name);
1521         fprintf(leaseFile, "  fixed-address %s;\n", piaddr(lease->address));
1522         if (lease->filename)
1523                 fprintf(leaseFile, "  filename \"%s\";\n", lease->filename);
1524         if (lease->server_name)
1525                 fprintf(leaseFile, "  server-name \"%s\";\n",
1526                     lease->server_name);
1527         if (lease->medium)
1528                 fprintf(leaseFile, "  medium \"%s\";\n", lease->medium->string);
1529         for (i = 0; i < 256; i++)
1530                 if (lease->options[i].len)
1531                         fprintf(leaseFile, "  option %s %s;\n",
1532                             dhcp_options[i].name,
1533                             pretty_print_option(i, lease->options[i].data,
1534                             lease->options[i].len, 1, 1));
1535
1536         t = gmtime(&lease->renewal);
1537         fprintf(leaseFile, "  renew %d %d/%d/%d %02d:%02d:%02d;\n",
1538             t->tm_wday, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
1539             t->tm_hour, t->tm_min, t->tm_sec);
1540         t = gmtime(&lease->rebind);
1541         fprintf(leaseFile, "  rebind %d %d/%d/%d %02d:%02d:%02d;\n",
1542             t->tm_wday, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
1543             t->tm_hour, t->tm_min, t->tm_sec);
1544         t = gmtime(&lease->expiry);
1545         fprintf(leaseFile, "  expire %d %d/%d/%d %02d:%02d:%02d;\n",
1546             t->tm_wday, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
1547             t->tm_hour, t->tm_min, t->tm_sec);
1548         fprintf(leaseFile, "}\n");
1549         fflush(leaseFile);
1550 }
1551
1552 void
1553 script_init(char *reason, struct string_list *medium)
1554 {
1555         size_t           len, mediumlen = 0;
1556         struct imsg_hdr  hdr;
1557         struct buf      *buf;
1558
1559         if (medium != NULL && medium->string != NULL)
1560                 mediumlen = strlen(medium->string);
1561
1562         hdr.code = IMSG_SCRIPT_INIT;
1563         hdr.len = sizeof(struct imsg_hdr) +
1564             sizeof(size_t) + mediumlen +
1565             sizeof(size_t) + strlen(reason);
1566
1567         buf = buf_open(hdr.len);
1568
1569         buf_add(buf, &hdr, sizeof(hdr));
1570         buf_add(buf, &mediumlen, sizeof(mediumlen));
1571         if (mediumlen > 0)
1572                 buf_add(buf, medium->string, mediumlen);
1573         len = strlen(reason);
1574         buf_add(buf, &len, sizeof(len));
1575         buf_add(buf, reason, len);
1576
1577         buf_close(privfd, buf);
1578 }
1579
1580 void
1581 priv_script_init(char *reason, char *medium)
1582 {
1583         client->scriptEnvsize = 100;
1584         if (client->scriptEnv == NULL)
1585                 client->scriptEnv =
1586                     calloc(client->scriptEnvsize, sizeof(char *));
1587         if (client->scriptEnv == NULL)
1588                 error("script_init: no memory for environment");
1589
1590         client->scriptEnv[0] = strdup(CLIENT_PATH);
1591         if (client->scriptEnv[0] == NULL)
1592                 error("script_init: no memory for environment");
1593
1594         client->scriptEnv[1] = NULL;
1595
1596         script_set_env("", "interface", ifi->name);
1597
1598         if (medium)
1599                 script_set_env("", "medium", medium);
1600
1601         script_set_env("", "reason", reason);
1602 }
1603
1604 void
1605 priv_script_write_params(char *prefix, struct client_lease *lease)
1606 {
1607         u_int8_t dbuf[1500];
1608         int i, len = 0;
1609         char tbuf[128];
1610
1611         script_set_env(prefix, "ip_address", piaddr(lease->address));
1612
1613         if (lease->options[DHO_SUBNET_MASK].len &&
1614             (lease->options[DHO_SUBNET_MASK].len <
1615             sizeof(lease->address.iabuf))) {
1616                 struct iaddr netmask, subnet, broadcast;
1617
1618                 memcpy(netmask.iabuf, lease->options[DHO_SUBNET_MASK].data,
1619                     lease->options[DHO_SUBNET_MASK].len);
1620                 netmask.len = lease->options[DHO_SUBNET_MASK].len;
1621
1622                 subnet = subnet_number(lease->address, netmask);
1623                 if (subnet.len) {
1624                         script_set_env(prefix, "network_number",
1625                             piaddr(subnet));
1626                         if (!lease->options[DHO_BROADCAST_ADDRESS].len) {
1627                                 broadcast = broadcast_addr(subnet, netmask);
1628                                 if (broadcast.len)
1629                                         script_set_env(prefix,
1630                                             "broadcast_address",
1631                                             piaddr(broadcast));
1632                         }
1633                 }
1634         }
1635
1636         if (lease->filename)
1637                 script_set_env(prefix, "filename", lease->filename);
1638         if (lease->server_name)
1639                 script_set_env(prefix, "server_name",
1640                     lease->server_name);
1641         for (i = 0; i < 256; i++) {
1642                 u_int8_t *dp = NULL;
1643
1644                 if (config->defaults[i].len) {
1645                         if (lease->options[i].len) {
1646                                 switch (config->default_actions[i]) {
1647                                 case ACTION_DEFAULT:
1648                                         dp = lease->options[i].data;
1649                                         len = lease->options[i].len;
1650                                         break;
1651                                 case ACTION_SUPERSEDE:
1652 supersede:
1653                                         dp = config->defaults[i].data;
1654                                         len = config->defaults[i].len;
1655                                         break;
1656                                 case ACTION_PREPEND:
1657                                         len = config->defaults[i].len +
1658                                             lease->options[i].len;
1659                                         if (len > sizeof(dbuf)) {
1660                                                 warning("no space to %s %s",
1661                                                     "prepend option",
1662                                                     dhcp_options[i].name);
1663                                                 goto supersede;
1664                                         }
1665                                         dp = dbuf;
1666                                         memcpy(dp,
1667                                             config->defaults[i].data,
1668                                             config->defaults[i].len);
1669                                         memcpy(dp +
1670                                             config->defaults[i].len,
1671                                             lease->options[i].data,
1672                                             lease->options[i].len);
1673                                         dp[len] = '\0';
1674                                         break;
1675                                 case ACTION_APPEND:
1676                                         len = config->defaults[i].len +
1677                                             lease->options[i].len;
1678                                         if (len > sizeof(dbuf)) {
1679                                                 warning("no space to %s %s",
1680                                                     "append option",
1681                                                     dhcp_options[i].name);
1682                                                 goto supersede;
1683                                         }
1684                                         dp = dbuf;
1685                                         memcpy(dp, lease->options[i].data,
1686                                             lease->options[i].len);
1687                                         memcpy(dp + lease->options[i].len,
1688                                             config->defaults[i].data,
1689                                             config->defaults[i].len);
1690                                         dp[len] = '\0';
1691                                 }
1692                         } else {
1693                                 dp = config->defaults[i].data;
1694                                 len = config->defaults[i].len;
1695                         }
1696                 } else if (lease->options[i].len) {
1697                         len = lease->options[i].len;
1698                         dp = lease->options[i].data;
1699                 } else {
1700                         len = 0;
1701                 }
1702                 if (len) {
1703                         char name[256];
1704
1705                         if (dhcp_option_ev_name(name, sizeof(name),
1706                             &dhcp_options[i]))
1707                                 script_set_env(prefix, name,
1708                                     pretty_print_option(i, dp, len, 0, 0));
1709                 }
1710         }
1711         snprintf(tbuf, sizeof(tbuf), "%d", (int)lease->expiry);
1712         script_set_env(prefix, "expiry", tbuf);
1713 }
1714
1715 void
1716 script_write_params(char *prefix, struct client_lease *lease)
1717 {
1718         size_t           fn_len = 0, sn_len = 0, pr_len = 0;
1719         struct imsg_hdr  hdr;
1720         struct buf      *buf;
1721         int              i;
1722
1723         if (lease->filename != NULL)
1724                 fn_len = strlen(lease->filename);
1725         if (lease->server_name != NULL)
1726                 sn_len = strlen(lease->server_name);
1727         if (prefix != NULL)
1728                 pr_len = strlen(prefix);
1729
1730         hdr.code = IMSG_SCRIPT_WRITE_PARAMS;
1731         hdr.len = sizeof(hdr) + sizeof(struct client_lease) +
1732             sizeof(size_t) + fn_len + sizeof(size_t) + sn_len +
1733             sizeof(size_t) + pr_len;
1734
1735         for (i = 0; i < 256; i++)
1736                 hdr.len += sizeof(int) + lease->options[i].len;
1737
1738         scripttime = time(NULL);
1739
1740         buf = buf_open(hdr.len);
1741
1742         buf_add(buf, &hdr, sizeof(hdr));
1743         buf_add(buf, lease, sizeof(struct client_lease));
1744         buf_add(buf, &fn_len, sizeof(fn_len));
1745         buf_add(buf, lease->filename, fn_len);
1746         buf_add(buf, &sn_len, sizeof(sn_len));
1747         buf_add(buf, lease->server_name, sn_len);
1748         buf_add(buf, &pr_len, sizeof(pr_len));
1749         buf_add(buf, prefix, pr_len);
1750
1751         for (i = 0; i < 256; i++) {
1752                 buf_add(buf, &lease->options[i].len,
1753                     sizeof(lease->options[i].len));
1754                 buf_add(buf, lease->options[i].data,
1755                     lease->options[i].len);
1756         }
1757
1758         buf_close(privfd, buf);
1759 }
1760
1761 int
1762 script_go(void)
1763 {
1764         struct imsg_hdr  hdr;
1765         struct buf      *buf;
1766         int              ret;
1767
1768         scripttime = time(NULL);
1769
1770         hdr.code = IMSG_SCRIPT_GO;
1771         hdr.len = sizeof(struct imsg_hdr);
1772
1773         buf = buf_open(hdr.len);
1774
1775         buf_add(buf, &hdr, sizeof(hdr));
1776         buf_close(privfd, buf);
1777
1778         bzero(&hdr, sizeof(hdr));
1779         buf_read(privfd, &hdr, sizeof(hdr));
1780         if (hdr.code != IMSG_SCRIPT_GO_RET)
1781                 error("unexpected msg type %u", hdr.code);
1782         if (hdr.len != sizeof(hdr) + sizeof(int))
1783                 error("received corrupted message");
1784         buf_read(privfd, &ret, sizeof(ret));
1785
1786         return (ret);
1787 }
1788
1789 int
1790 priv_script_go(void)
1791 {
1792         char *scriptName, *argv[2], **envp;
1793         int pid, wpid, wstatus;
1794
1795         scripttime = time(NULL);
1796
1797         scriptName = config->script_name;
1798         envp = client->scriptEnv;
1799
1800         argv[0] = scriptName;
1801         argv[1] = NULL;
1802
1803         pid = fork();
1804         if (pid < 0) {
1805                 error("fork: %m");
1806                 wstatus = 0;
1807         } else if (pid) {
1808                 do {
1809                         wpid = wait(&wstatus);
1810                 } while (wpid != pid && wpid > 0);
1811                 if (wpid < 0) {
1812                         error("wait: %m");
1813                         wstatus = 0;
1814                 }
1815         } else {
1816                 execve(scriptName, argv, envp);
1817                 error("execve (%s, ...): %m", scriptName);
1818         }
1819
1820         script_flush_env();
1821
1822         return (WEXITSTATUS(wstatus));
1823 }
1824
1825 void
1826 script_set_env(const char *prefix, const char *name, const char *value)
1827 {
1828         int i, j, namelen;
1829
1830         namelen = strlen(name);
1831
1832         for (i = 0; client->scriptEnv[i]; i++)
1833                 if (strncmp(client->scriptEnv[i], name, namelen) == 0 &&
1834                     client->scriptEnv[i][namelen] == '=')
1835                         break;
1836
1837         if (client->scriptEnv[i])
1838                 /* Reuse the slot. */
1839                 free(client->scriptEnv[i]);
1840         else {
1841                 /* New variable.  Expand if necessary. */
1842                 if (i >= client->scriptEnvsize - 1) {
1843                         char **newscriptEnv;
1844                         int newscriptEnvsize = client->scriptEnvsize + 50;
1845
1846                         newscriptEnv = realloc(client->scriptEnv,
1847                             newscriptEnvsize);
1848                         if (newscriptEnv == NULL) {
1849                                 free(client->scriptEnv);
1850                                 client->scriptEnv = NULL;
1851                                 client->scriptEnvsize = 0;
1852                                 error("script_set_env: no memory for variable");
1853                         }
1854                         client->scriptEnv = newscriptEnv;
1855                         client->scriptEnvsize = newscriptEnvsize;
1856                 }
1857                 /* need to set the NULL pointer at end of array beyond
1858                    the new slot. */
1859                 client->scriptEnv[i + 1] = NULL;
1860         }
1861         /* Allocate space and format the variable in the appropriate slot. */
1862         client->scriptEnv[i] = malloc(strlen(prefix) + strlen(name) + 1 +
1863             strlen(value) + 1);
1864         if (client->scriptEnv[i] == NULL)
1865                 error("script_set_env: no memory for variable assignment");
1866
1867         /* No `` or $() command substitution allowed in environment values! */
1868         for (j = 0; j < strlen(value); j++)
1869                 switch (value[j]) {
1870                 case '`':
1871                 case '$':
1872                         error("illegal character (%c) in value '%s'", value[j],
1873                             value);
1874                         /* not reached */
1875                 }
1876         snprintf(client->scriptEnv[i], strlen(prefix) + strlen(name) +
1877             1 + strlen(value) + 1, "%s%s=%s", prefix, name, value);
1878 }
1879
1880 void
1881 script_flush_env(void)
1882 {
1883         int i;
1884
1885         for (i = 0; client->scriptEnv[i]; i++) {
1886                 free(client->scriptEnv[i]);
1887                 client->scriptEnv[i] = NULL;
1888         }
1889         client->scriptEnvsize = 0;
1890 }
1891
1892 int
1893 dhcp_option_ev_name(char *buf, size_t buflen, const struct option *option)
1894 {
1895         size_t i;
1896
1897         for (i = 0; option->name[i]; i++) {
1898                 if (i + 1 == buflen)
1899                         return 0;
1900                 if (option->name[i] == '-')
1901                         buf[i] = '_';
1902                 else
1903                         buf[i] = option->name[i];
1904         }
1905
1906         buf[i] = 0;
1907         return 1;
1908 }
1909
1910 void
1911 go_daemon(void)
1912 {
1913         static int state = 0;
1914
1915         if (no_daemon || state)
1916                 return;
1917
1918         state = 1;
1919
1920         /* Stop logging to stderr... */
1921         log_perror = 0;
1922
1923         if (daemon(1, 0) == -1)
1924                 error("daemon");
1925
1926         /* we are chrooted, daemon(3) fails to open /dev/null */
1927         if (nullfd != -1) {
1928                 dup2(nullfd, STDIN_FILENO);
1929                 dup2(nullfd, STDOUT_FILENO);
1930                 dup2(nullfd, STDERR_FILENO);
1931                 close(nullfd);
1932                 nullfd = -1;
1933         }
1934 }
1935
1936 int
1937 check_option(struct client_lease *l, int option)
1938 {
1939         char *opbuf;
1940         char *sbuf;
1941
1942         /* we use this, since this is what gets passed to dhclient-script */
1943
1944         opbuf = pretty_print_option(option, l->options[option].data,
1945             l->options[option].len, 0, 0);
1946
1947         sbuf = option_as_string(option, l->options[option].data,
1948             l->options[option].len);
1949
1950         switch (option) {
1951         case DHO_SUBNET_MASK:
1952         case DHO_TIME_SERVERS:
1953         case DHO_NAME_SERVERS:
1954         case DHO_ROUTERS:
1955         case DHO_DOMAIN_NAME_SERVERS:
1956         case DHO_LOG_SERVERS:
1957         case DHO_COOKIE_SERVERS:
1958         case DHO_LPR_SERVERS:
1959         case DHO_IMPRESS_SERVERS:
1960         case DHO_RESOURCE_LOCATION_SERVERS:
1961         case DHO_SWAP_SERVER:
1962         case DHO_BROADCAST_ADDRESS:
1963         case DHO_NIS_SERVERS:
1964         case DHO_NTP_SERVERS:
1965         case DHO_NETBIOS_NAME_SERVERS:
1966         case DHO_NETBIOS_DD_SERVER:
1967         case DHO_FONT_SERVERS:
1968         case DHO_DHCP_SERVER_IDENTIFIER:
1969                 if (!ipv4addrs(opbuf)) {
1970                         warning("Invalid IP address in option: %s", opbuf);
1971                         return (0);
1972                 }
1973                 return (1);
1974         case DHO_HOST_NAME:
1975         case DHO_DOMAIN_NAME:
1976         case DHO_NIS_DOMAIN:
1977                 if (!res_hnok(sbuf)) {
1978                         warning("Bogus Host Name option %d: %s (%s)", option,
1979                             sbuf, opbuf);
1980                         l->options[option].len = 0;
1981                         free(l->options[option].data);
1982                 }
1983                 return (1);
1984         case DHO_PAD:
1985         case DHO_TIME_OFFSET:
1986         case DHO_BOOT_SIZE:
1987         case DHO_MERIT_DUMP:
1988         case DHO_ROOT_PATH:
1989         case DHO_EXTENSIONS_PATH:
1990         case DHO_IP_FORWARDING:
1991         case DHO_NON_LOCAL_SOURCE_ROUTING:
1992         case DHO_POLICY_FILTER:
1993         case DHO_MAX_DGRAM_REASSEMBLY:
1994         case DHO_DEFAULT_IP_TTL:
1995         case DHO_PATH_MTU_AGING_TIMEOUT:
1996         case DHO_PATH_MTU_PLATEAU_TABLE:
1997         case DHO_INTERFACE_MTU:
1998         case DHO_ALL_SUBNETS_LOCAL:
1999         case DHO_PERFORM_MASK_DISCOVERY:
2000         case DHO_MASK_SUPPLIER:
2001         case DHO_ROUTER_DISCOVERY:
2002         case DHO_ROUTER_SOLICITATION_ADDRESS:
2003         case DHO_STATIC_ROUTES:
2004         case DHO_TRAILER_ENCAPSULATION:
2005         case DHO_ARP_CACHE_TIMEOUT:
2006         case DHO_IEEE802_3_ENCAPSULATION:
2007         case DHO_DEFAULT_TCP_TTL:
2008         case DHO_TCP_KEEPALIVE_INTERVAL:
2009         case DHO_TCP_KEEPALIVE_GARBAGE:
2010         case DHO_VENDOR_ENCAPSULATED_OPTIONS:
2011         case DHO_NETBIOS_NODE_TYPE:
2012         case DHO_NETBIOS_SCOPE:
2013         case DHO_X_DISPLAY_MANAGER:
2014         case DHO_DHCP_REQUESTED_ADDRESS:
2015         case DHO_DHCP_LEASE_TIME:
2016         case DHO_DHCP_OPTION_OVERLOAD:
2017         case DHO_DHCP_MESSAGE_TYPE:
2018         case DHO_DHCP_PARAMETER_REQUEST_LIST:
2019         case DHO_DHCP_MESSAGE:
2020         case DHO_DHCP_MAX_MESSAGE_SIZE:
2021         case DHO_DHCP_RENEWAL_TIME:
2022         case DHO_DHCP_REBINDING_TIME:
2023         case DHO_DHCP_CLASS_IDENTIFIER:
2024         case DHO_DHCP_CLIENT_IDENTIFIER:
2025         case DHO_DHCP_USER_CLASS_ID:
2026         case DHO_END:
2027                 return (1);
2028         default:
2029                 warning("unknown dhcp option value 0x%x", option);
2030                 return (unknown_ok);
2031         }
2032 }
2033
2034 int
2035 res_hnok(const char *name)
2036 {
2037         const char *dn = name;
2038         int pch = '.', ch = *dn++;
2039         int warn = 0;
2040
2041         while (ch != '\0') {
2042                 int nch = *dn++;
2043
2044                 if (ch == '.') {
2045                         ;
2046                 } else if (pch == '.' || nch == '.' || nch == '\0') {
2047                         if (!isalnum(ch))
2048                                 return (0);
2049                 } else if (!isalnum(ch) && ch != '-' && ch != '_')
2050                                 return (0);
2051                 else if (ch == '_' && warn == 0) {
2052                         warning("warning: hostname %s contains an "
2053                             "underscore which violates RFC 952", name);
2054                         warn++;
2055                 }
2056                 pch = ch, ch = nch;
2057         }
2058         return (1);
2059 }
2060
2061 /* Does buf consist only of dotted decimal ipv4 addrs?
2062  * return how many if so,
2063  * otherwise, return 0
2064  */
2065 int
2066 ipv4addrs(char * buf)
2067 {
2068         struct in_addr jnk;
2069         int count = 0;
2070
2071         while (inet_aton(buf, &jnk) == 1){
2072                 count++;
2073                 while (*buf == '.' || isdigit(*buf))
2074                         buf++;
2075                 if (*buf == '\0')
2076                         return (count);
2077                 while (*buf ==  ' ')
2078                         buf++;
2079         }
2080         return (0);
2081 }
2082
2083 char *
2084 option_as_string(unsigned int code, unsigned char *data, int len)
2085 {
2086         static char optbuf[32768]; /* XXX */
2087         char *op = optbuf;
2088         int opleft = sizeof(optbuf);
2089         unsigned char *dp = data;
2090
2091         if (code > 255)
2092                 error("option_as_string: bad code %d", code);
2093
2094         for (; dp < data + len; dp++) {
2095                 if (!isascii(*dp) || !isprint(*dp)) {
2096                         if (dp + 1 != data + len || *dp != 0) {
2097                                 size_t oplen;
2098                                 snprintf(op, opleft, "\\%03o", *dp);
2099                                 oplen = strlen(op);
2100                                 op += oplen;
2101                                 opleft -= oplen;
2102                         }
2103                 } else if (*dp == '"' || *dp == '\'' || *dp == '$' ||
2104                     *dp == '`' || *dp == '\\') {
2105                         *op++ = '\\';
2106                         *op++ = *dp;
2107                         opleft -= 2;
2108                 } else {
2109                         *op++ = *dp;
2110                         opleft--;
2111                 }
2112         }
2113         if (opleft < 1)
2114                 goto toobig;
2115         *op = 0;
2116         return optbuf;
2117 toobig:
2118         warning("dhcp option too large");
2119         return "<error>";
2120 }
2121
2122 int
2123 fork_privchld(int fd, int fd2)
2124 {
2125         struct pollfd pfd[1];
2126         int nfds, pfail = 0;
2127
2128         switch (fork()) {
2129         case -1:
2130                 error("cannot fork");
2131                 break;
2132         case 0:
2133                 break;
2134         default:
2135                 return (0);
2136         }
2137
2138         if (chdir("/") == -1)
2139                 error("chdir(\"/\")");
2140
2141         setproctitle("%s [priv]", ifi->name);
2142
2143         dup2(nullfd, STDIN_FILENO);
2144         dup2(nullfd, STDOUT_FILENO);
2145         dup2(nullfd, STDERR_FILENO);
2146         close(nullfd);
2147         close(fd2);
2148
2149         for (;;) {
2150                 pfd[0].fd = fd;
2151                 pfd[0].events = POLLIN;
2152                 if ((nfds = poll(pfd, 1, INFTIM)) == -1)
2153                         if (errno != EINTR)
2154                                 error("poll error");
2155
2156                 /*
2157                  * Handle temporary errors, but bail if they persist.
2158                  */
2159                 if (nfds == 0 || !(pfd[0].revents & POLLIN)) {
2160                         if (pfail > POLL_FAILURES)
2161                                 error("poll failed > %d times", POLL_FAILURES);
2162                         sleep(pfail * POLL_FAILURE_WAIT);
2163                         pfail++;
2164                         continue;
2165                 }
2166
2167                 dispatch_imsg(fd);
2168         }
2169 }