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