3e18df19efe49d9b28fa2c9e21a1053d2391ee51
[oweals/busybox.git] / networking / ifupdown.c
1 /*
2  *  ifupdown for busybox
3  *  Based on ifupdown by Anthony Towns
4  *  Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au>
5  *
6  *  Changes to upstream version
7  *  Remove checks for kernel version, assume kernel version 2.2.0 or better
8  *  Lines in the interfaces file cannot wrap.
9  *  The default state file is moved to /var/run/ifstate
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 #include <sys/stat.h>
27 #include <sys/utsname.h>
28 #include <sys/wait.h>
29
30 #include <ctype.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <fnmatch.h>
34 #include <getopt.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #include "libbb.h"
42 //#include "busybox.h"
43 //#include "config.h"
44
45 #define IFUPDOWN_VERSION "0.6.4"
46
47 typedef struct interface_defn_s interface_defn_t;
48
49 typedef int (execfn)(char *command);
50 typedef int (command_set)(interface_defn_t *ifd, execfn *e);
51
52 typedef struct method_s {
53         char *name;
54         command_set *up;
55         command_set *down;
56 } method_t;
57
58 typedef struct address_family_s {
59         char *name;
60         int n_methods;
61         method_t *method;
62 } address_family_t;
63
64 typedef struct mapping_defn_s {
65         struct mapping_defn_s *next;
66
67         int max_matches;
68         int n_matches;
69         char **match;
70
71         char *script;
72
73         int max_mappings;
74         int n_mappings;
75         char **mapping;
76 } mapping_defn_t;
77
78 typedef struct variable_s {
79         char *name;
80         char *value;
81 } variable_t;
82
83 struct interface_defn_s {
84         struct interface_defn_s *next;
85
86         char *iface;
87         address_family_t *address_family;
88         method_t *method;
89
90         int automatic;
91
92         int max_options;
93         int n_options;
94         variable_t *option;
95 };
96
97 typedef struct interfaces_file_s {
98         int max_autointerfaces;
99         int n_autointerfaces;
100         char **autointerfaces;
101
102         interface_defn_t *ifaces;
103         mapping_defn_t *mappings;
104 } interfaces_file_t;
105
106 #define MAX_OPT_DEPTH 10
107 #define EUNBALBRACK 10001
108 #define EUNDEFVAR   10002
109 #define MAX_VARNAME    32
110 #define EUNBALPER   10000
111
112 static int no_act = 0;
113 static int verbose = 0;
114 static char **environ = NULL;
115
116 static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
117 {
118         if (*pos + str_length >= *len) {
119                 char *newbuf;
120
121                 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
122                 *buf = newbuf;
123                 *len = *len * 2 + str_length + 1;
124         }
125
126         while (str_length-- >= 1) {
127                 (*buf)[(*pos)++] = *str;
128                 str++;
129         }
130         (*buf)[*pos] = '\0';
131 }
132
133 static int strncmpz(char *l, char *r, size_t llen)
134 {
135         int i = strncmp(l, r, llen);
136
137         if (i == 0) {
138                 return(-r[llen]);
139         } else {
140                 return(i);
141         }
142 }
143
144 static char *get_var(char *id, size_t idlen, interface_defn_t *ifd)
145 {
146         int i;
147
148         if (strncmpz(id, "iface", idlen) == 0) {
149                 return (ifd->iface);
150         } else {
151                 for (i = 0; i < ifd->n_options; i++) {
152                         if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
153                                 return (ifd->option[i].value);
154                         }
155                 }
156         }
157
158         return(NULL);
159 }
160
161 static char *parse(char *command, interface_defn_t *ifd)
162 {
163
164         char *result = NULL;
165         size_t pos = 0, len = 0;
166         size_t old_pos[MAX_OPT_DEPTH] = { 0 };
167         int okay[MAX_OPT_DEPTH] = { 1 };
168         int opt_depth = 1;
169
170         while (*command) {
171                 switch (*command) {
172
173                 default:
174                         addstr(&result, &len, &pos, command, 1);
175                         command++;
176                         break;
177                 case '\\':
178                         if (command[1]) {
179                                 addstr(&result, &len, &pos, command + 1, 1);
180                                 command += 2;
181                         } else {
182                                 addstr(&result, &len, &pos, command, 1);
183                                 command++;
184                         }
185                         break;
186                 case '[':
187                         if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
188                                 old_pos[opt_depth] = pos;
189                                 okay[opt_depth] = 1;
190                                 opt_depth++;
191                                 command += 2;
192                         } else {
193                                 addstr(&result, &len, &pos, "[", 1);
194                                 command++;
195                         }
196                         break;
197                 case ']':
198                         if (command[1] == ']' && opt_depth > 1) {
199                                 opt_depth--;
200                                 if (!okay[opt_depth]) {
201                                         pos = old_pos[opt_depth];
202                                         result[pos] = '\0';
203                                 }
204                                 command += 2;
205                         } else {
206                                 addstr(&result, &len, &pos, "]", 1);
207                                 command++;
208                         }
209                         break;
210                 case '%':
211                 {
212                         char *nextpercent;
213                         char *varvalue;
214
215                         command++;
216                         nextpercent = strchr(command, '%');
217                         if (!nextpercent) {
218                                 errno = EUNBALPER;
219                                 free(result);
220                                 return (NULL);
221                         }
222
223                         varvalue = get_var(command, nextpercent - command, ifd);
224
225                         if (varvalue) {
226                                 addstr(&result, &len, &pos, varvalue, xstrlen(varvalue));
227                         } else {
228                                 okay[opt_depth - 1] = 0;
229                         }
230
231                         command = nextpercent + 1;
232                 }
233                         break;
234                 }
235         }
236
237         if (opt_depth > 1) {
238                 errno = EUNBALBRACK;
239                 free(result);
240                 return(NULL);
241         }
242
243         if (!okay[0]) {
244                 errno = EUNDEFVAR;
245                 free(result);
246                 return(NULL);
247         }
248
249         return(result);
250 }
251
252 static int execute(char *command, interface_defn_t *ifd, execfn *exec)
253 {
254         char *out;
255         int ret;
256
257         out = parse(command, ifd);
258         if (!out) {
259                 return(0);
260         }
261
262         ret = (*exec) (out);
263
264         free(out);
265         return(ret);
266 }
267
268 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
269 static int static_up_ipx(interface_defn_t *ifd, execfn *exec)
270 {
271         if (!execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec)) {
272                 return(0);
273         }
274         return(1);
275 }
276
277 static int static_down_ipx(interface_defn_t *ifd, execfn *exec)
278 {
279         if (!execute("ipx_interface del %iface% %frame%", ifd, exec)) {
280                 return(0);
281         }
282         return(1);
283 }
284
285 static int dynamic_up(interface_defn_t *ifd, execfn *exec)
286 {
287         if (!execute("ipx_interface add %iface% %frame%", ifd, exec)) {
288                 return(0);
289         }
290         return(1);
291 }
292
293 static int dynamic_down(interface_defn_t *ifd, execfn *exec)
294 {
295         if (!execute("ipx_interface del %iface% %frame%", ifd, exec)) {
296                 return(0);
297         }
298         return(1);
299 }
300
301 static method_t methods_ipx[] = {
302         { "dynamic", dynamic_up, dynamic_down, },
303         { "static", static_up_ipx, static_down_ipx, },
304 };
305
306 address_family_t addr_ipx = {
307         "ipx",
308         sizeof(methods_ipx) / sizeof(method_t),
309         methods_ipx
310 };
311 #endif /* IFUP_FEATURE_IPX */
312
313 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
314 static int loopback_up6(interface_defn_t *ifd, execfn *exec)
315 {
316         if (!execute("ifconfig %iface% add ::1", ifd, exec)) {
317                 return(0);
318         }
319         return(1);
320 }
321
322 static int loopback_down6(interface_defn_t *ifd, execfn *exec)
323 {
324         if (!execute("ifconfig %iface% del ::1", ifd, exec)) {
325                 return(0);
326         }
327         return(1);
328 }
329
330 static int static_up6(interface_defn_t *ifd, execfn *exec)
331 {
332         if (!execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec)) {
333                 return(0);
334         }
335         if (!execute("ifconfig %iface% add %address%/%netmask%", ifd, exec)) {
336                 return(0);
337         }
338         if (!execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec)) {
339                 return(0);
340         }
341         return(1);
342 }
343
344 static int static_down6(interface_defn_t *ifd, execfn *exec)
345 {
346         if (!execute("ifconfig %iface% down", ifd, exec)) {
347                 return(0);
348         }
349         return(1);
350 }
351
352 static int v4tunnel_up(interface_defn_t *ifd, execfn *exec)
353 {
354         if (!execute("ip tunnel add %iface% mode sit remote %endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec)) {
355                 return(0);
356         }
357         if (!execute("ip link set %iface% up", ifd, exec)) {
358                 return(0);
359         }
360         if (!execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec)) {
361                 return(0);
362         }
363         if (!execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec)) {
364                 return(0);
365         }
366         return(1);
367 }
368
369 static int v4tunnel_down(interface_defn_t * ifd, execfn * exec)
370 {
371         if (!execute("ip tunnel del %iface%", ifd, exec)) {
372                 return(0);
373         }
374         return(1);
375 }
376
377 static method_t methods6[] = {
378         { "v4tunnel", v4tunnel_up, v4tunnel_down, },
379         { "static", static_up6, static_down6, },
380         { "loopback", loopback_up6, loopback_down6, },
381 };
382
383 address_family_t addr_inet6 = {
384         "inet6",
385         sizeof(methods6) / sizeof(method_t),
386         methods6
387 };
388 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
389
390 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
391 static int loopback_up(interface_defn_t *ifd, execfn *exec)
392 {
393         if (!execute("ifconfig %iface% 127.0.0.1 up", ifd, exec)) {
394                 return(0);
395         }
396         return(1);
397 }
398
399 static int loopback_down(interface_defn_t *ifd, execfn *exec)
400 {
401         if (!execute("ifconfig %iface% 127.0.0.1 down", ifd, exec)) {
402                 return(0);
403         }
404         return(1);
405 }
406
407 static int static_up(interface_defn_t *ifd, execfn *exec)
408 {
409         if (!execute("ifconfig %iface% %address% netmask %netmask% [[broadcast %broadcast%]]    [[pointopoint %pointopoint%]] [[media %media%]] [[mtu %mtu%]]   [[hw %hwaddress%]] up",
410                  ifd, exec)) {
411                 return(0);
412         }
413         if (!execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec)) {
414                 return(0);
415         }
416         return(1);
417 }
418
419 static int static_down(interface_defn_t *ifd, execfn *exec)
420 {
421         if (!execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec)) {
422                 return(0);
423         }
424         if (!execute("ifconfig %iface% down", ifd, exec)) {
425                 return(0);
426         }
427         return(1);
428 }
429
430 static int execable(char *program)
431 {
432         struct stat buf;
433         if (0 == stat(program, &buf)) {
434                 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
435                         return(1);
436                 }
437         }
438         return(0);
439 }
440
441 static int dhcp_up(interface_defn_t *ifd, execfn *exec)
442 {
443         if (execable("/sbin/dhclient")) {
444                 if (!execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec)) {
445                         return(0);
446                 }
447         } else if (execable("/sbin/pump")) {
448                 if (!execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec)) {
449                         return(0);
450                 }
451         } else if (execable("/sbin/udhcpc")) {
452                 if (!execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i %iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec)) {
453                         return 0;
454                 }
455         } else if (execable("/sbin/dhcpcd")) {
456                 if (!execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] [[-l %leasetime%]] %iface%", ifd, exec)) {
457                         return(0);
458                 }
459         }
460         return(1);
461 }
462
463 static int dhcp_down(interface_defn_t *ifd, execfn *exec)
464 {
465         if (execable("/sbin/dhclient")) {
466                 if (!execute("kill -9 `cat /var/run/udhcpc.%iface%.pid`", ifd, exec)) {
467                         return(0);
468                 }
469         } else if (execable("/sbin/pump")) {
470                 if (!execute("pump -i %iface% -k", ifd, exec)) {
471                         return(0);
472                 }
473         } else if (execable("/sbin/udhcpc")) {
474                 if (!execute("kill -9 `cat /var/run/udhcpc.%iface%.pid`", ifd, exec)) {
475                         return(0);
476                 }
477         } else if (execable("/sbin/dhcpcd")) {
478                 if (!execute("dhcpcd -k %iface%", ifd, exec)) {
479                         return(0);
480                 }
481         }
482         if (!execute("ifconfig %iface% down", ifd, exec)) {
483                 return(0);
484         }
485         return(1);
486 }
487
488 static int bootp_up(interface_defn_t *ifd, execfn *exec)
489 {
490         if (!execute("bootpc [[--bootfile %bootfile%]] --dev %iface% [[--server %server%]]            [[--hwaddr %hwaddr%]] --returniffail --serverbcast", ifd, exec)) {
491                 return 0;
492         }
493         return 1;
494 }
495
496 static int bootp_down(interface_defn_t *ifd, execfn *exec)
497 {
498         if (!execute("ifconfig down %iface%", ifd, exec)) {
499                 return 0;
500         }
501         return 1;
502 }
503
504 static int ppp_up(interface_defn_t *ifd, execfn *exec)
505 {
506         if (!execute("pon [[%provider%]]", ifd, exec)) {
507                 return 0;
508         }
509         return 1;
510 }
511
512 static int ppp_down(interface_defn_t *ifd, execfn *exec)
513 {
514         if (!execute("poff [[%provider%]]", ifd, exec)) {
515                 return 0;
516         }
517         return 1;
518 }
519
520 static int wvdial_up(interface_defn_t *ifd, execfn *exec)
521 {
522         if (!execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial -p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec)) {
523                 return 0;
524         }
525         return 1;
526 }
527
528 static int wvdial_down(interface_defn_t *ifd, execfn *exec)
529 {
530         if (!execute ("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial -p /var/run/wvdial.%iface% -s 2", ifd, exec)) {
531                 return 0;
532         }
533         return 1;
534 }
535
536 static method_t methods[] = {
537         { "wvdial", wvdial_up, wvdial_down, },
538         { "ppp", ppp_up, ppp_down, },
539         { "static", static_up, static_down, },
540         { "bootp", bootp_up, bootp_down, },
541         { "dhcp", dhcp_up, dhcp_down, },
542         { "loopback", loopback_up, loopback_down, },
543 };
544
545 address_family_t addr_inet = {
546         "inet",
547         sizeof(methods) / sizeof(method_t),
548         methods
549 };
550
551 #endif  /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
552
553 static char *next_word(char *buf, char *word, int maxlen)
554 {
555         if (!buf)
556                 return NULL;
557         if (!*buf)
558                 return NULL;
559
560         while (!isspace(*buf) && *buf) {
561                 if (maxlen-- > 1)
562                         *word++ = *buf;
563                 buf++;
564         }
565         if (maxlen > 0) {
566                 *word = '\0';
567         }
568
569         while (isspace(*buf) && *buf) {
570                 buf++;
571         }
572
573         return buf;
574 }
575
576 static address_family_t *get_address_family(address_family_t *af[], char *name)
577 {
578         int i;
579
580         for (i = 0; af[i]; i++) {
581                 if (strcmp(af[i]->name, name) == 0) {
582                         return af[i];
583                 }
584         }
585         return NULL;
586 }
587
588 static method_t *get_method(address_family_t *af, char *name)
589 {
590         int i;
591
592         for (i = 0; i < af->n_methods; i++) {
593                 if (strcmp(af->method[i].name, name) == 0) {
594                         return &af->method[i];
595                 }
596         }
597         return(NULL);
598 }
599
600 static int duplicate_if(interface_defn_t *ifa, interface_defn_t *ifb)
601 {
602         if (strcmp(ifa->iface, ifb->iface) != 0) {
603                 return(0);
604         }
605         if (ifa->address_family != ifb->address_family) {
606                 return(0);
607         }
608         return(1);
609 }
610
611 static interfaces_file_t *read_interfaces(char *filename)
612 {
613         interface_defn_t *currif = NULL;
614         interfaces_file_t *defn;
615 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
616         mapping_defn_t *currmap = NULL;
617 #endif
618         FILE *f;
619         char firstword[80];
620         char *buf = NULL;
621         char *rest;
622 //      int line;
623
624         enum { NONE, IFACE, MAPPING } currently_processing = NONE;
625
626         defn = xmalloc(sizeof(interfaces_file_t));
627         defn->max_autointerfaces = defn->n_autointerfaces = 0;
628         defn->autointerfaces = NULL;
629         defn->mappings = NULL;
630         defn->ifaces = NULL;
631         f = fopen(filename, "r");
632         if (f == NULL) {
633                 return NULL;
634         }
635
636         while ((buf = get_line_from_file(f)) != NULL) {
637                 char *end;
638
639                 if (buf[0] == '#') {
640                         continue;
641                 }
642                 end = last_char_is(buf, '\n');
643                 if (end) {
644                         *end = '\0';
645                 }
646                 while ((end = last_char_is(buf, ' ')) != NULL) {
647                         *end = '\0';
648                 }
649                 rest = next_word(buf, firstword, 80);
650                 if (rest == NULL) {
651                         continue;       /* blank line */
652                 }
653
654                 if (strcmp(firstword, "mapping") == 0) {
655 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
656                         currmap = xmalloc(sizeof(mapping_defn_t));
657                         currmap->max_matches = 0;
658                         currmap->n_matches = 0;
659                         currmap->match = NULL;
660
661                         while ((rest = next_word(rest, firstword, 80))) {
662                                 if (currmap->max_matches == currmap->n_matches) {
663                                         currmap->max_matches = currmap->max_matches * 2 + 1;
664                                         currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
665                                 }
666
667                                 currmap->match[currmap->n_matches++] = xstrdup(firstword);
668                         }
669                         currmap->max_mappings = 0;
670                         currmap->n_mappings = 0;
671                         currmap->mapping = NULL;
672                         currmap->script = NULL;
673                         {
674                                 mapping_defn_t **where = &defn->mappings;
675                                 while (*where != NULL) {
676                                         where = &(*where)->next;
677                                 }
678                                 *where = currmap;
679                                 currmap->next = NULL;
680                         }
681 #endif
682                         currently_processing = MAPPING;
683                 } else if (strcmp(firstword, "iface") == 0) {
684                         {
685                                 char iface_name[80];
686                                 char address_family_name[80];
687                                 char method_name[80];
688                                 address_family_t *addr_fams[] = {
689 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
690                                         &addr_inet,
691 #endif
692 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
693                                         &addr_inet6,
694 #endif
695 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
696                                         &addr_ipx,
697 #endif
698                                         NULL
699                                 };
700
701                                 currif = xmalloc(sizeof(interface_defn_t));
702
703                                 rest = next_word(rest, iface_name, 80);
704                                 rest = next_word(rest, address_family_name, 80);
705                                 rest = next_word(rest, method_name, 80);
706
707                                 if (rest == NULL) {
708                                         error_msg("too few parameters for line \"%s\"", buf);
709                                         return NULL;
710                                 }
711
712                                 if (rest[0] != '\0') {
713                                         error_msg("too many parameters \"%s\"", buf);
714                                         return NULL;
715                                 }
716
717                                 currif->iface = xstrdup(iface_name);
718
719                                 currif->address_family = get_address_family(addr_fams, address_family_name);
720                                 if (!currif->address_family) {
721                                         error_msg("unknown address type \"%s\"", buf);
722                                         return NULL;
723                                 }
724
725                                 currif->method = get_method(currif->address_family, method_name);
726                                 if (!currif->method) {
727                                         error_msg("unknown method \"%s\"", buf);
728                                         return NULL;
729                                 }
730
731                                 currif->automatic = 1;
732                                 currif->max_options = 0;
733                                 currif->n_options = 0;
734                                 currif->option = NULL;
735
736
737                                 {
738                                         interface_defn_t **where = &defn->ifaces;
739
740                                         while (*where != NULL) {
741                                                 if (duplicate_if(*where, currif)) {
742                                                         error_msg("duplicate interface \"%s\"", buf);
743                                                         return NULL;
744                                                 }
745                                                 where = &(*where)->next;
746                                         }
747
748                                         *where = currif;
749                                         currif->next = NULL;
750                                 }
751                         }
752                         currently_processing = IFACE;
753                 } else if (strcmp(firstword, "auto") == 0) {
754                         while ((rest = next_word(rest, firstword, 80))) {
755                                 int i;
756
757                                 for (i = 0; i < defn->n_autointerfaces; i++) {
758                                         if (strcmp(firstword, defn->autointerfaces[i]) == 0) {
759                                                 perror_msg("interface declared auto twice \"%s\"", buf);
760                                                 return NULL;
761                                         }
762                                 }
763
764                                 if (defn->n_autointerfaces == defn->max_autointerfaces) {
765                                         char **tmp;
766
767                                         defn->max_autointerfaces *= 2;
768                                         defn->max_autointerfaces++;
769                                         tmp = xrealloc(defn->autointerfaces, sizeof(*tmp) * defn->max_autointerfaces);
770                                         defn->autointerfaces = tmp;
771                                 }
772
773                                 defn->autointerfaces[defn->n_autointerfaces] = xstrdup(firstword);
774                                 defn->n_autointerfaces++;
775                         }
776                         currently_processing = NONE;
777                 } else {
778                         switch (currently_processing) {
779                         case IFACE:
780                         {
781                                 int i;
782
783                                 if (xstrlen(rest) == 0) {
784                                         error_msg("option with empty value \"%s\"", buf);
785                                         return NULL;
786                                 }
787
788                                 if (strcmp(firstword, "up") != 0
789                                         && strcmp(firstword, "down") != 0
790                                         && strcmp(firstword, "pre-up") != 0
791                                         && strcmp(firstword, "post-down") != 0) {
792                                         for (i = 0; i < currif->n_options; i++) {
793                                                 if (strcmp(currif->option[i].name, firstword) == 0) {
794                                                         error_msg("duplicate option \"%s\"", buf);
795                                                         return NULL;
796                                                 }
797                                         }
798                                 }
799                         }
800                                 if (currif->n_options >= currif->max_options) {
801                                         variable_t *opt;
802
803                                         currif->max_options = currif->max_options + 10;
804                                         opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
805                                         currif->option = opt;
806                                 }
807                                 currif->option[currif->n_options].name = xstrdup(firstword);
808                                 currif->option[currif->n_options].value = xstrdup(rest);
809                                 if (!currif->option[currif->n_options].name) {
810                                         perror(filename);
811                                         return NULL;
812                                 }
813                                 if (!currif->option[currif->n_options].value) {
814                                         perror(filename);
815                                         return NULL;
816                                 }
817                                 currif->n_options++;
818                                 break;
819                         case MAPPING:
820 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
821                                 if (strcmp(firstword, "script") == 0) {
822                                         if (currmap->script != NULL) {
823                                                 error_msg("duplicate script in mapping \"%s\"", buf);
824                                                 return NULL;
825                                         } else {
826                                                 currmap->script = xstrdup(rest);
827                                         }
828                                 } else if (strcmp(firstword, "map") == 0) {
829                                         if (currmap->max_mappings == currmap->n_mappings) {
830                                                 currmap->max_mappings = currmap->max_mappings * 2 + 1;
831                                                 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
832                                         }
833                                         currmap->mapping[currmap->n_mappings] = xstrdup(rest);
834                                         currmap->n_mappings++;
835                                 } else {
836                                         error_msg("misplaced option \"%s\"", buf);
837                                         return NULL;
838                                 }
839 #endif
840                                 break;
841                         case NONE:
842                         default:
843                                 error_msg("misplaced option \"%s\"", buf);
844                                 return NULL;
845                         }
846                 }
847         }
848         if (ferror(f) != 0) {
849                 perror_msg("%s", filename);
850                 return NULL;
851         }
852         fclose(f);
853
854         return defn;
855 }
856
857 static int check(char *str)
858 {
859         return (str != NULL);
860 }
861
862 static char *setlocalenv(char *format, char *name, char *value)
863 {
864         char *result;
865         char *here;
866         char *there;
867
868         result = xmalloc(xstrlen(format) + xstrlen(name) + xstrlen(value) + 1);
869
870         sprintf(result, format, name, value);
871
872         for (here = there = result; *there != '=' && *there; there++) {
873                 if (*there == '-')
874                         *there = '_';
875                 if (isalpha(*there))
876                         *there = toupper(*there);
877
878                 if (isalnum(*there) || *there == '_') {
879                         *here = *there;
880                         here++;
881                 }
882         }
883         memmove(here, there, xstrlen(there) + 1);
884
885         return result;
886 }
887
888 static void set_environ(interface_defn_t *iface, char *mode)
889 {
890         char **environend;
891         int i;
892         const int n_env_entries = iface->n_options + 5;
893         char **ppch;
894
895         if (environ != NULL) {
896                 for (ppch = environ; *ppch; ppch++) {
897                         free(*ppch);
898                         *ppch = NULL;
899                 }
900                 free(environ);
901                 environ = NULL;
902         }
903         environ = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
904         environend = environ;
905         *environend = NULL;
906
907         for (i = 0; i < iface->n_options; i++) {
908                 if (strcmp(iface->option[i].name, "up") == 0
909                         || strcmp(iface->option[i].name, "down") == 0
910                         || strcmp(iface->option[i].name, "pre-up") == 0
911                         || strcmp(iface->option[i].name, "post-down") == 0) {
912                         continue;
913                 }
914                 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
915                 *environend = NULL;
916         }
917
918         *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
919         *environend = NULL;
920         *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
921         *environend = NULL;
922         *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
923         *environend = NULL;
924         *(environend++) = setlocalenv("%s=%s", "MODE", mode);
925         *environend = NULL;
926         *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
927         *environend = NULL;
928 }
929
930 static int doit(char *str)
931 {
932         if (verbose || no_act) {
933                 error_msg("%s", str);
934         }
935         if (!no_act) {
936                 pid_t child;
937                 int status;
938
939                 fflush(NULL);
940                 switch (child = fork()) {
941                 case -1:                /* failure */
942                         return 0;
943                 case 0:         /* child */
944                         execle("/bin/sh", "/bin/sh", "-c", str, NULL, environ);
945                         exit(127);
946                 default:                /* parent */
947                 }
948                 waitpid(child, &status, 0);
949                 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
950                         return 0;
951                 }
952         }
953         return (1);
954 }
955
956 static int execute_all(interface_defn_t *ifd, execfn *exec, const char *opt)
957 {
958         int i;
959         char *buf;
960
961         for (i = 0; i < ifd->n_options; i++) {
962                 if (strcmp(ifd->option[i].name, opt) == 0) {
963                         if (!(*exec) (ifd->option[i].value)) {
964                                 return 0;
965                         }
966                 }
967         }
968
969         buf = xmalloc(xstrlen(opt) + 19);
970         sprintf(buf, "/etc/network/if-%s.d", opt);
971         run_parts(&buf, 0);
972         free(buf);
973         return (1);
974 }
975
976 static int iface_up(interface_defn_t *iface)
977 {
978         if (!iface->method->up(iface, check)) {
979                 return (-1);
980         }
981
982         set_environ(iface, "start");
983         if (!execute_all(iface, doit, "pre-up")) {
984                 return (0);
985         }
986         if (!iface->method->up(iface, doit)) {
987                 return (0);
988         }
989         if (!execute_all(iface, doit, "up")) {
990                 return (0);
991         }
992
993         return (1);
994 }
995
996 static int iface_down(interface_defn_t *iface)
997 {
998         if (!iface->method->down(iface, check)) {
999                 return (-1);
1000         }
1001         set_environ(iface, "stop");
1002         if (!execute_all(iface, doit, "down")) {
1003                 return (0);
1004         }
1005         if (!iface->method->down(iface, doit)) {
1006                 return (0);
1007         }
1008         if (!execute_all(iface, doit, "post-down")) {
1009                 return (0);
1010         }
1011         return (1);
1012 }
1013
1014 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1015 static int popen2(FILE **in, FILE **out, char *command, ...)
1016 {
1017         va_list ap;
1018         char *argv[11] = { command };
1019         int argc;
1020         int infd[2], outfd[2];
1021         pid_t pid;
1022
1023         argc = 1;
1024         va_start(ap, command);
1025         while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1026                 argc++;
1027         }
1028         argv[argc] = NULL;      /* make sure */
1029         va_end(ap);
1030
1031         if (pipe(infd) != 0) {
1032                 return 0;
1033         }
1034
1035         if (pipe(outfd) != 0) {
1036                 close(infd[0]);
1037                 close(infd[1]);
1038                 return 0;
1039         }
1040
1041         fflush(NULL);
1042         switch (pid = fork()) {
1043         case -1:                        /* failure */
1044                 close(infd[0]);
1045                 close(infd[1]);
1046                 close(outfd[0]);
1047                 close(outfd[1]);
1048                 return 0;
1049         case 0:                 /* child */
1050                 dup2(infd[0], 0);
1051                 dup2(outfd[1], 1);
1052                 close(infd[0]);
1053                 close(infd[1]);
1054                 close(outfd[0]);
1055                 close(outfd[1]);
1056                 execvp(command, argv);
1057                 exit(127);
1058         default:                        /* parent */
1059                 *in = fdopen(infd[1], "w");
1060                 *out = fdopen(outfd[0], "r");
1061                 close(infd[0]);
1062                 close(outfd[1]);
1063                 return pid;
1064         }
1065         /* unreached */
1066 }
1067
1068 static int run_mapping(char *physical, char *logical, int len, mapping_defn_t * map)
1069 {
1070         FILE *in, *out;
1071         int i, status;
1072         pid_t pid;
1073
1074
1075         pid = popen2(&in, &out, map->script, physical, NULL);
1076         if (pid == 0) {
1077                 return 0;
1078         }
1079         for (i = 0; i < map->n_mappings; i++) {
1080                 fprintf(in, "%s\n", map->mapping[i]);
1081         }
1082         fclose(in);
1083         waitpid(pid, &status, 0);
1084         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1085                 if (fgets(logical, len, out)) {
1086                         char *pch = logical + xstrlen(logical) - 1;
1087
1088                         while (pch >= logical && isspace(*pch))
1089                                 *(pch--) = '\0';
1090                 }
1091         }
1092         fclose(out);
1093
1094         return 1;
1095 }
1096 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
1097
1098 static int lookfor_iface(char **ifaces, int n_ifaces, char *iface)
1099 {
1100         int i;
1101
1102         for (i = 0; i < n_ifaces; i++) {
1103                 if (strncmp(iface, ifaces[i], xstrlen(iface)) == 0) {
1104                         if (ifaces[i][xstrlen(iface)] == '=') {
1105                                 return i;
1106                         }
1107                 }
1108         }
1109
1110         return(-1);
1111 }
1112
1113 static void add_to_state(char ***ifaces, int *n_ifaces, int *max_ifaces, char *new_iface)
1114 {
1115         if (*max_ifaces == *n_ifaces) {
1116                 *max_ifaces = (*max_ifaces * 2) + 1;
1117                 *ifaces = xrealloc(*ifaces, sizeof(**ifaces) * *max_ifaces);
1118         }
1119
1120         (*ifaces)[(*n_ifaces)++] = new_iface;
1121 }
1122
1123 extern int ifupdown_main(int argc, char **argv)
1124 {
1125         int (*cmds) (interface_defn_t *) = NULL;
1126         interfaces_file_t *defn;
1127         FILE *state_fp = NULL;
1128         char **target_iface = NULL;
1129         char **state = NULL;    /* list of iface=liface */
1130         char *interfaces = "/etc/network/interfaces";
1131         char *statefile = "/var/run/ifstate";
1132
1133 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1134         int run_mappings = 1;
1135 #endif
1136         int do_all = 0;
1137         int force = 0;
1138         int n_target_ifaces = 0;
1139         int n_state = 0;
1140         int max_state = 0;
1141         int i;
1142
1143         if (applet_name[2] == 'u') {
1144                 /* ifup command */
1145                 cmds = iface_up;
1146         } else {
1147                 /* ifdown command */
1148                 cmds = iface_down;
1149         }
1150
1151 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1152         while ((i = getopt(argc, argv, "i:hvnamf")) != -1) {
1153 #else
1154         while ((i = getopt(argc, argv, "i:hvnaf")) != -1) {
1155 #endif
1156                 switch (i) {
1157                 case 'i':       /* interfaces */
1158                         interfaces = xstrdup(optarg);
1159                         break;
1160                 case 'v':       /* verbose */
1161                         verbose = 1;
1162                         break;
1163                 case 'a':       /* all */
1164                         do_all = 1;
1165                         break;
1166                 case 'n':       /* no-act */
1167                         no_act = 1;
1168                         break;
1169 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1170                 case 'm':       /* no-mappings */
1171                         run_mappings = 0;
1172                         break;
1173 #endif
1174                 case 'f':       /* force */
1175                         force = 1;
1176                         break;
1177                 default:
1178                         show_usage();
1179                         break;
1180                 }
1181         }
1182
1183         if (argc - optind > 0) {
1184                 if (do_all) {
1185                         show_usage();
1186                 }
1187         } else {
1188                 if (!do_all) {
1189                         show_usage();
1190                 }
1191         }                       
1192
1193         defn = read_interfaces(interfaces);
1194         if (!defn) {
1195                 error_msg_and_die("couldn't read interfaces file \"%s\"", interfaces);
1196         }
1197
1198         state_fp = fopen(statefile, no_act ? "r" : "a+");
1199         if (state_fp == NULL && !no_act) {
1200                 perror_msg_and_die("failed to open statefile %s", statefile);
1201         }
1202
1203         if (state_fp != NULL) {
1204                 char *start;
1205                 while ((start = get_line_from_file(state_fp)) != NULL) {
1206                         char *end_ptr;
1207                         /* We should only need to check for a single character */
1208                         end_ptr = start + strcspn(start, " \t\n");
1209                         *end_ptr = '\0';
1210                         add_to_state(&state, &n_state, &max_state, start);
1211                 }
1212         }
1213
1214         if (do_all) {
1215                 if (cmds == iface_up) {
1216                         target_iface = defn->autointerfaces;
1217                         n_target_ifaces = defn->n_autointerfaces;
1218                 } else if (cmds == iface_down) {
1219                         target_iface = state;
1220                         n_target_ifaces = n_state;
1221                 }
1222         } else {
1223                 target_iface = argv + optind;
1224                 n_target_ifaces = argc - optind;
1225         }
1226
1227
1228         for (i = 0; i < n_target_ifaces; i++) {
1229                 interface_defn_t *currif;
1230                 char iface[80];
1231                 char liface[80];
1232                 char *pch;
1233                 int okay = 0;
1234
1235                 strncpy(iface, target_iface[i], sizeof(iface));
1236                 iface[sizeof(iface) - 1] = '\0';
1237
1238                 if ((pch = strchr(iface, '='))) {
1239                         *pch = '\0';
1240                         strncpy(liface, pch + 1, sizeof(liface));
1241                         liface[sizeof(liface) - 1] = '\0';
1242                 } else {
1243                         strncpy(liface, iface, sizeof(liface));
1244                         liface[sizeof(liface) - 1] = '\0';
1245                 }
1246                 if (!force) {
1247                         int already_up = lookfor_iface(state, n_state, iface);;
1248
1249                         if (cmds == iface_up) {
1250                                 /* ifup */
1251                                 if (already_up != -1) {
1252                                         error_msg("interface %s already configured", iface);
1253                                         continue;
1254                                 }
1255                         } else {
1256                                 /* ifdown */
1257                                 if (already_up == -1) {
1258                                         error_msg("interface %s not configured", iface);
1259                                         continue;
1260                                 }
1261                                 strncpy(liface, strchr(state[already_up], '=') + 1, 80);
1262                                 liface[79] = 0;
1263                         }
1264                 }
1265 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1266                 if ((cmds == iface_up) && run_mappings) {
1267                         mapping_defn_t *currmap;
1268
1269                         for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1270
1271                                 for (i = 0; i < currmap->n_matches; i++) {
1272                                         if (fnmatch(currmap->match[i], liface, 0) != 0)
1273                                                 continue;
1274                                         if (verbose) {
1275                                                 error_msg("Running mapping script %s on %s", currmap->script, liface);
1276                                         }
1277                                         run_mapping(iface, liface, sizeof(liface), currmap);
1278                                         break;
1279                                 }
1280                         }
1281                 }
1282 #endif
1283
1284                 for (currif = defn->ifaces; currif; currif = currif->next) {
1285                         if (strcmp(liface, currif->iface) == 0) {
1286                                 char *oldiface = currif->iface;
1287
1288                                 okay = 1;
1289
1290                                 currif->iface = iface;
1291
1292                                 if (verbose) {
1293                                         error_msg("Configuring interface %s=%s (%s)", iface, liface, currif->address_family->name);
1294                                 }
1295
1296                                 if (cmds(currif) == -1) {
1297                                         printf
1298                                                 ("Don't seem to be have all the variables for %s/%s.\n",
1299                                                  liface, currif->address_family->name);
1300                                 }
1301
1302                                 currif->iface = oldiface;
1303                         }
1304                 }
1305
1306                 if (!okay && !force) {
1307                         error_msg("Ignoring unknown interface %s=%s.", iface, liface);
1308                 } else {
1309                         int already_up = lookfor_iface(state, n_state, iface);
1310
1311                         if (cmds == iface_up) {
1312                                 char *newiface = xmalloc(xstrlen(iface) + 1 + xstrlen(liface) + 1);
1313                                 sprintf(newiface, "%s=%s", iface, liface);
1314                                 if (already_up == -1) {
1315                                         add_to_state(&state, &n_state, &max_state, newiface);
1316                                 } else {
1317                                         free(state[already_up]);
1318                                         state[already_up] = newiface;
1319                                 }
1320                         } else if (cmds == iface_down) {
1321                                 if (already_up != -1) {
1322                                         state[already_up] = state[--n_state];
1323                                 }
1324                         }
1325                 }
1326                 if (state_fp != NULL && !no_act) {
1327                         unsigned short j;
1328
1329                         if (ftruncate(fileno(state_fp), 0) < 0) {
1330                                 error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
1331                         }
1332
1333                         rewind(state_fp);
1334                         for (j = 0; j < n_state; j++) {
1335                                 fputs(state[i], state_fp);
1336                                 fputc('\n', state_fp);
1337                         }
1338                         fflush(state_fp);
1339                 }
1340         }
1341
1342         if (state_fp != NULL) {
1343                 fclose(state_fp);
1344                 state_fp = NULL;
1345         }
1346
1347         return 0;
1348 }