Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 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 | * Remove checks for kernel version, assume kernel version 2.2.0 or better |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/utsname.h> |
| 25 | #include <sys/wait.h> |
| 26 | |
| 27 | #include <ctype.h> |
| 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <fnmatch.h> |
| 31 | #include <getopt.h> |
| 32 | #include <stdarg.h> |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <unistd.h> |
| 37 | |
| 38 | #include "busybox.h" |
| 39 | #include "config.h" |
| 40 | |
| 41 | #define IFUPDOWN_VERSION "0.6.4" |
| 42 | |
| 43 | typedef struct interface_defn interface_defn; |
| 44 | |
| 45 | typedef int (execfn)(char *command); |
| 46 | typedef int (command_set)(interface_defn *ifd, execfn *e); |
| 47 | |
| 48 | typedef struct method { |
| 49 | char *name; |
| 50 | command_set *up; |
| 51 | command_set *down; |
| 52 | } method; |
| 53 | |
| 54 | typedef struct address_family { |
| 55 | char *name; |
| 56 | int n_methods; |
| 57 | method *method; |
| 58 | } address_family; |
| 59 | |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 60 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 61 | typedef struct mapping_defn { |
| 62 | struct mapping_defn *next; |
| 63 | |
| 64 | int max_matches; |
| 65 | int n_matches; |
| 66 | char **match; |
| 67 | |
| 68 | char *script; |
| 69 | |
| 70 | int max_mappings; |
| 71 | int n_mappings; |
| 72 | char **mapping; |
| 73 | } mapping_defn; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 74 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 75 | |
| 76 | typedef struct variable { |
| 77 | char *name; |
| 78 | char *value; |
| 79 | } variable; |
| 80 | |
| 81 | struct interface_defn { |
| 82 | struct interface_defn *next; |
| 83 | |
| 84 | char *iface; |
| 85 | address_family *address_family; |
| 86 | method *method; |
| 87 | |
| 88 | int automatic; |
| 89 | |
| 90 | int max_options; |
| 91 | int n_options; |
| 92 | variable *option; |
| 93 | }; |
| 94 | |
| 95 | typedef struct interfaces_file { |
| 96 | int max_autointerfaces; |
| 97 | int n_autointerfaces; |
| 98 | char **autointerfaces; |
| 99 | |
| 100 | interface_defn *ifaces; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 101 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 102 | mapping_defn *mappings; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 103 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 104 | } interfaces_file; |
| 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 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 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 *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 *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 *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 *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 *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 *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 *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 methods_ipx[] = { |
| 302 | { "dynamic", dynamic_up, dynamic_down, }, |
| 303 | { "static", static_up_ipx, static_down_ipx, }, |
| 304 | }; |
| 305 | |
| 306 | address_family addr_ipx = { |
| 307 | "ipx", |
| 308 | sizeof(methods_ipx) / sizeof(struct method), |
| 309 | methods_ipx |
| 310 | }; |
| 311 | #endif /* IFUP_FEATURE_IPX */ |
| 312 | |
| 313 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6 |
| 314 | static int loopback_up6(interface_defn *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 *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 *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 *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 *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 * 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 methods6[] = { |
| 378 | { "v4tunnel", v4tunnel_up, v4tunnel_down, }, |
| 379 | { "static", static_up6, static_down6, }, |
| 380 | { "loopback", loopback_up6, loopback_down6, }, |
| 381 | }; |
| 382 | |
| 383 | address_family addr_inet6 = { |
| 384 | "inet6", |
| 385 | sizeof(methods6) / sizeof(struct method), |
| 386 | methods6 |
| 387 | }; |
| 388 | #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */ |
| 389 | |
| 390 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 |
| 391 | static int loopback_up(interface_defn *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 *ifd, execfn *exec) |
| 400 | { |
| 401 | if (!execute("ifconfig %iface% down", ifd, exec)) { |
| 402 | return(0); |
| 403 | } |
| 404 | return(1); |
| 405 | } |
| 406 | |
| 407 | static int static_up(interface_defn *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 *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 | |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 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 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 441 | static int dhcp_up(interface_defn *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 *ifd, execfn *exec) |
| 464 | { |
| 465 | if (execable("/sbin/dhclient")) { |
| 466 | if (!execute("cat /var/run/dhclient.%iface%.pid | xargs -i kill -TERM {}", 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("cat /var/run/udhcpc.%iface%.pid | xargs -i kill -TERM {}", 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 *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 *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 *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 *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 *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 *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 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 addr_inet = { |
| 546 | "inet", |
| 547 | sizeof(methods) / sizeof(struct method), |
| 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 *get_address_family(address_family *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 *get_method(address_family *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 *ifa, interface_defn *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 int get_line(char **result, size_t * result_len, FILE * f, int *line) |
| 612 | { |
| 613 | size_t pos; |
| 614 | |
| 615 | do { |
| 616 | pos = 0; |
| 617 | do { |
| 618 | if (*result_len - pos < 10) { |
| 619 | char *newstr = xrealloc(*result, *result_len * 2 + 80); |
| 620 | *result = newstr; |
| 621 | *result_len = *result_len * 2 + 80; |
| 622 | } |
| 623 | |
| 624 | if (!fgets(*result + pos, *result_len - pos, f)) { |
| 625 | if (ferror(f) == 0 && pos == 0) |
| 626 | return 0; |
| 627 | if (ferror(f) != 0) |
| 628 | return 0; |
| 629 | } |
| 630 | pos += xstrlen(*result + pos); |
| 631 | } while (pos == *result_len - 1 && (*result)[pos - 1] != '\n'); |
| 632 | |
| 633 | if (pos != 0 && (*result)[pos - 1] == '\n') { |
| 634 | (*result)[--pos] = '\0'; |
| 635 | } |
| 636 | |
| 637 | (*line)++; |
| 638 | { |
| 639 | int first = 0; |
| 640 | |
| 641 | while (isspace((*result)[first]) && (*result)[first]) { |
| 642 | first++; |
| 643 | } |
| 644 | |
| 645 | memmove(*result, *result + first, pos - first + 1); |
| 646 | pos -= first; |
| 647 | } |
| 648 | } while ((*result)[0] == '#'); |
| 649 | |
| 650 | while ((*result)[pos - 1] == '\\') { |
| 651 | (*result)[--pos] = '\0'; |
| 652 | do { |
| 653 | if (*result_len - pos < 10) { |
| 654 | char *newstr = xrealloc(*result, *result_len * 2 + 80); |
| 655 | *result = newstr; |
| 656 | *result_len = *result_len * 2 + 80; |
| 657 | } |
| 658 | |
| 659 | if (!fgets(*result + pos, *result_len - pos, f)) { |
| 660 | if (ferror(f) == 0 && pos == 0) |
| 661 | return 0; |
| 662 | if (ferror(f) != 0) |
| 663 | return 0; |
| 664 | } |
| 665 | pos += xstrlen(*result + pos); |
| 666 | } while (pos == *result_len - 1 && (*result)[pos - 1] != '\n'); |
| 667 | |
| 668 | if (pos != 0 && (*result)[pos - 1] == '\n') { |
| 669 | (*result)[--pos] = '\0'; |
| 670 | } |
| 671 | (*line)++; |
| 672 | } |
| 673 | |
| 674 | while (isspace((*result)[pos - 1])) { /* remove trailing whitespace */ |
| 675 | pos--; |
| 676 | } |
| 677 | (*result)[pos] = '\0'; |
| 678 | |
| 679 | return 1; |
| 680 | } |
| 681 | |
| 682 | static interfaces_file *read_interfaces(char *filename) |
| 683 | { |
| 684 | interface_defn *currif = NULL; |
| 685 | interfaces_file *defn; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 686 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 687 | mapping_defn *currmap = NULL; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 688 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 689 | FILE *f; |
| 690 | char firstword[80]; |
| 691 | char *buf = NULL; |
| 692 | char *rest; |
| 693 | int line; |
| 694 | size_t buf_len = 0; |
| 695 | |
| 696 | enum { NONE, IFACE, MAPPING } currently_processing = NONE; |
| 697 | |
| 698 | defn = xmalloc(sizeof(interfaces_file)); |
| 699 | defn->max_autointerfaces = defn->n_autointerfaces = 0; |
| 700 | defn->autointerfaces = NULL; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 701 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 702 | defn->mappings = NULL; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 703 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 704 | defn->ifaces = NULL; |
| 705 | f = fopen(filename, "r"); |
| 706 | if (f == NULL) { |
| 707 | return NULL; |
| 708 | } |
| 709 | line = 0; |
| 710 | |
| 711 | while (get_line(&buf, &buf_len, f, &line)) { |
| 712 | rest = next_word(buf, firstword, 80); |
| 713 | if (rest == NULL) { |
| 714 | continue; /* blank line */ |
| 715 | } |
| 716 | |
| 717 | if (strcmp(firstword, "mapping") == 0) { |
Glenn L McGrath | eebe31d | 2002-11-10 13:20:35 +0000 | [diff] [blame] | 718 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 719 | currmap = xmalloc(sizeof(mapping_defn)); |
| 720 | currmap->max_matches = 0; |
| 721 | currmap->n_matches = 0; |
| 722 | currmap->match = NULL; |
| 723 | |
| 724 | while ((rest = next_word(rest, firstword, 80))) { |
| 725 | if (currmap->max_matches == currmap->n_matches) { |
| 726 | currmap->max_matches = currmap->max_matches * 2 + 1; |
| 727 | currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches); |
| 728 | } |
| 729 | |
| 730 | currmap->match[currmap->n_matches++] = xstrdup(firstword); |
| 731 | } |
| 732 | currmap->max_mappings = 0; |
| 733 | currmap->n_mappings = 0; |
| 734 | currmap->mapping = NULL; |
| 735 | currmap->script = NULL; |
| 736 | { |
| 737 | mapping_defn **where = &defn->mappings; |
| 738 | while (*where != NULL) { |
| 739 | where = &(*where)->next; |
| 740 | } |
| 741 | *where = currmap; |
| 742 | currmap->next = NULL; |
| 743 | } |
| 744 | currently_processing = MAPPING; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 745 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 746 | } else if (strcmp(firstword, "iface") == 0) { |
| 747 | { |
| 748 | char iface_name[80]; |
| 749 | char address_family_name[80]; |
| 750 | char method_name[80]; |
| 751 | address_family *addr_fams[] = { |
| 752 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 |
| 753 | &addr_inet, |
| 754 | #endif |
| 755 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6 |
| 756 | &addr_inet6, |
| 757 | #endif |
| 758 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPX |
| 759 | &addr_ipx, |
| 760 | #endif |
| 761 | NULL |
| 762 | }; |
| 763 | |
| 764 | currif = xmalloc(sizeof(interface_defn)); |
| 765 | |
| 766 | rest = next_word(rest, iface_name, 80); |
| 767 | rest = next_word(rest, address_family_name, 80); |
| 768 | rest = next_word(rest, method_name, 80); |
| 769 | |
| 770 | if (rest == NULL) { |
| 771 | error_msg("%s:%d: too few parameters for iface line", filename, line); |
| 772 | return NULL; |
| 773 | } |
| 774 | |
| 775 | if (rest[0] != '\0') { |
| 776 | error_msg("%s:%d: too many parameters for iface line", filename, line); |
| 777 | return NULL; |
| 778 | } |
| 779 | |
| 780 | currif->iface = xstrdup(iface_name); |
| 781 | |
| 782 | currif->address_family = get_address_family(addr_fams, address_family_name); |
| 783 | if (!currif->address_family) { |
| 784 | error_msg("%s:%d: unknown address type", filename, line); |
| 785 | return NULL; |
| 786 | } |
| 787 | |
| 788 | currif->method = get_method(currif->address_family, method_name); |
| 789 | if (!currif->method) { |
| 790 | error_msg("%s:%d: unknown method", filename, line); |
| 791 | return NULL; |
| 792 | } |
| 793 | |
| 794 | currif->automatic = 1; |
| 795 | currif->max_options = 0; |
| 796 | currif->n_options = 0; |
| 797 | currif->option = NULL; |
| 798 | |
| 799 | |
| 800 | { |
| 801 | interface_defn **where = &defn->ifaces; |
| 802 | |
| 803 | while (*where != NULL) { |
| 804 | if (duplicate_if(*where, currif)) { |
| 805 | error_msg("%s:%d: duplicate interface", filename, line); |
| 806 | return NULL; |
| 807 | } |
| 808 | where = &(*where)->next; |
| 809 | } |
| 810 | |
| 811 | *where = currif; |
| 812 | currif->next = NULL; |
| 813 | } |
| 814 | } |
| 815 | currently_processing = IFACE; |
| 816 | } else if (strcmp(firstword, "auto") == 0) { |
| 817 | while ((rest = next_word(rest, firstword, 80))) { |
| 818 | int i; |
| 819 | |
| 820 | for (i = 0; i < defn->n_autointerfaces; i++) { |
| 821 | if (strcmp(firstword, defn->autointerfaces[i]) == 0) { |
| 822 | perror_msg("%s:%d: interface declared auto twice", filename, line); |
| 823 | return NULL; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | if (defn->n_autointerfaces == defn->max_autointerfaces) { |
| 828 | char **tmp; |
| 829 | |
| 830 | defn->max_autointerfaces *= 2; |
| 831 | defn->max_autointerfaces++; |
| 832 | tmp = xrealloc(defn->autointerfaces, sizeof(*tmp) * defn->max_autointerfaces); |
| 833 | defn->autointerfaces = tmp; |
| 834 | } |
| 835 | |
| 836 | defn->autointerfaces[defn->n_autointerfaces] = xstrdup(firstword); |
| 837 | defn->n_autointerfaces++; |
| 838 | } |
| 839 | currently_processing = NONE; |
| 840 | } else { |
| 841 | switch (currently_processing) { |
| 842 | case IFACE: |
| 843 | { |
| 844 | int i; |
| 845 | |
| 846 | if (xstrlen(rest) == 0) { |
| 847 | error_msg("%s:%d: option with empty value", filename, line); |
| 848 | return NULL; |
| 849 | } |
| 850 | |
| 851 | if (strcmp(firstword, "up") != 0 |
| 852 | && strcmp(firstword, "down") != 0 |
| 853 | && strcmp(firstword, "pre-up") != 0 |
| 854 | && strcmp(firstword, "post-down") != 0) { |
| 855 | for (i = 0; i < currif->n_options; i++) { |
| 856 | if (strcmp(currif->option[i].name, firstword) == 0) { |
| 857 | error_msg("%s:%d: duplicate option", filename, line); |
| 858 | return NULL; |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | if (currif->n_options >= currif->max_options) { |
| 864 | variable *opt; |
| 865 | |
| 866 | currif->max_options = currif->max_options + 10; |
| 867 | opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options); |
| 868 | currif->option = opt; |
| 869 | } |
| 870 | currif->option[currif->n_options].name = xstrdup(firstword); |
| 871 | currif->option[currif->n_options].value = xstrdup(rest); |
| 872 | if (!currif->option[currif->n_options].name) { |
| 873 | perror(filename); |
| 874 | return NULL; |
| 875 | } |
| 876 | if (!currif->option[currif->n_options].value) { |
| 877 | perror(filename); |
| 878 | return NULL; |
| 879 | } |
| 880 | currif->n_options++; |
| 881 | break; |
| 882 | case MAPPING: |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 883 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 884 | if (strcmp(firstword, "script") == 0) { |
| 885 | if (currmap->script != NULL) { |
| 886 | error_msg("%s:%d: duplicate script in mapping", filename, line); |
| 887 | return NULL; |
| 888 | } else { |
| 889 | currmap->script = xstrdup(rest); |
| 890 | } |
| 891 | } else if (strcmp(firstword, "map") == 0) { |
| 892 | if (currmap->max_mappings == currmap->n_mappings) { |
| 893 | currmap->max_mappings = currmap->max_mappings * 2 + 1; |
| 894 | currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings); |
| 895 | } |
| 896 | currmap->mapping[currmap->n_mappings] = xstrdup(rest); |
| 897 | currmap->n_mappings++; |
| 898 | } else { |
| 899 | error_msg("%s:%d: misplaced option", filename, line); |
| 900 | return NULL; |
| 901 | } |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 902 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 903 | break; |
| 904 | case NONE: |
| 905 | default: |
| 906 | error_msg("%s:%d: misplaced option", filename, line); |
| 907 | return NULL; |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | if (ferror(f) != 0) { |
| 912 | perror_msg("%s", filename); |
| 913 | return NULL; |
| 914 | } |
| 915 | fclose(f); |
| 916 | line = -1; |
| 917 | |
| 918 | return defn; |
| 919 | } |
| 920 | |
| 921 | static int check(char *str) |
| 922 | { |
| 923 | return (str != NULL); |
| 924 | } |
| 925 | |
| 926 | static char *setlocalenv(char *format, char *name, char *value) |
| 927 | { |
| 928 | char *result; |
| 929 | char *here; |
| 930 | char *there; |
| 931 | |
| 932 | result = xmalloc(xstrlen(format) + xstrlen(name) + xstrlen(value) + 1); |
| 933 | |
| 934 | sprintf(result, format, name, value); |
| 935 | |
| 936 | for (here = there = result; *there != '=' && *there; there++) { |
| 937 | if (*there == '-') |
| 938 | *there = '_'; |
| 939 | if (isalpha(*there)) |
| 940 | *there = toupper(*there); |
| 941 | |
| 942 | if (isalnum(*there) || *there == '_') { |
| 943 | *here = *there; |
| 944 | here++; |
| 945 | } |
| 946 | } |
| 947 | memmove(here, there, xstrlen(there) + 1); |
| 948 | |
| 949 | return result; |
| 950 | } |
| 951 | |
| 952 | static void set_environ(interface_defn *iface, char *mode) |
| 953 | { |
| 954 | char **environend; |
| 955 | int i; |
| 956 | const int n_env_entries = iface->n_options + 5; |
| 957 | char **ppch; |
| 958 | |
| 959 | if (environ != NULL) { |
| 960 | for (ppch = environ; *ppch; ppch++) { |
| 961 | free(*ppch); |
| 962 | *ppch = NULL; |
| 963 | } |
| 964 | free(environ); |
| 965 | environ = NULL; |
| 966 | } |
| 967 | environ = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ )); |
| 968 | environend = environ; |
| 969 | *environend = NULL; |
| 970 | |
| 971 | for (i = 0; i < iface->n_options; i++) { |
| 972 | if (strcmp(iface->option[i].name, "up") == 0 |
| 973 | || strcmp(iface->option[i].name, "down") == 0 |
| 974 | || strcmp(iface->option[i].name, "pre-up") == 0 |
| 975 | || strcmp(iface->option[i].name, "post-down") == 0) { |
| 976 | continue; |
| 977 | } |
| 978 | *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value); |
| 979 | *environend = NULL; |
| 980 | } |
| 981 | |
| 982 | *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface); |
| 983 | *environend = NULL; |
| 984 | *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name); |
| 985 | *environend = NULL; |
| 986 | *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name); |
| 987 | *environend = NULL; |
| 988 | *(environend++) = setlocalenv("%s=%s", "MODE", mode); |
| 989 | *environend = NULL; |
| 990 | *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"); |
| 991 | *environend = NULL; |
| 992 | } |
| 993 | |
| 994 | static int doit(char *str) |
| 995 | { |
| 996 | if (verbose || no_act) { |
| 997 | error_msg("%s", str); |
| 998 | } |
| 999 | if (!no_act) { |
| 1000 | pid_t child; |
| 1001 | int status; |
| 1002 | |
| 1003 | fflush(NULL); |
| 1004 | switch (child = fork()) { |
| 1005 | case -1: /* failure */ |
| 1006 | return 0; |
| 1007 | case 0: /* child */ |
| 1008 | execle("/bin/sh", "/bin/sh", "-c", str, NULL, environ); |
| 1009 | exit(127); |
| 1010 | default: /* parent */ |
| 1011 | } |
| 1012 | waitpid(child, &status, 0); |
| 1013 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 1014 | return 0; |
| 1015 | } |
| 1016 | } |
| 1017 | return (1); |
| 1018 | } |
| 1019 | |
| 1020 | static int execute_all(interface_defn *ifd, execfn *exec, char *opt) |
| 1021 | { |
| 1022 | int i; |
| 1023 | char buf[100]; |
| 1024 | |
| 1025 | for (i = 0; i < ifd->n_options; i++) { |
| 1026 | if (strcmp(ifd->option[i].name, opt) == 0) { |
| 1027 | if (!(*exec) (ifd->option[i].value)) { |
| 1028 | return 0; |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | sprintf(buf, "run-parts /etc/network/if-%s.d", opt); |
| 1034 | (*exec) (buf); |
| 1035 | |
| 1036 | return (1); |
| 1037 | } |
| 1038 | |
| 1039 | static int iface_up(interface_defn *iface) |
| 1040 | { |
| 1041 | if (!iface->method->up(iface, check)) { |
| 1042 | return (-1); |
| 1043 | } |
| 1044 | |
| 1045 | set_environ(iface, "start"); |
| 1046 | if (!execute_all(iface, doit, "pre-up")) { |
| 1047 | return (0); |
| 1048 | } |
| 1049 | if (!iface->method->up(iface, doit)) { |
| 1050 | return (0); |
| 1051 | } |
| 1052 | if (!execute_all(iface, doit, "up")) { |
| 1053 | return (0); |
| 1054 | } |
| 1055 | |
| 1056 | return (1); |
| 1057 | } |
| 1058 | |
| 1059 | static int iface_down(interface_defn *iface) |
| 1060 | { |
| 1061 | if (!iface->method->down(iface, check)) { |
| 1062 | return (-1); |
| 1063 | } |
| 1064 | set_environ(iface, "stop"); |
| 1065 | if (!execute_all(iface, doit, "down")) { |
| 1066 | return (0); |
| 1067 | } |
| 1068 | if (!iface->method->down(iface, doit)) { |
| 1069 | return (0); |
| 1070 | } |
| 1071 | if (!execute_all(iface, doit, "post-down")) { |
| 1072 | return (0); |
| 1073 | } |
| 1074 | return (1); |
| 1075 | } |
| 1076 | |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1077 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1078 | static int popen2(FILE **in, FILE **out, char *command, ...) |
| 1079 | { |
| 1080 | va_list ap; |
| 1081 | char *argv[11] = { command }; |
| 1082 | int argc; |
| 1083 | int infd[2], outfd[2]; |
| 1084 | pid_t pid; |
| 1085 | |
| 1086 | argc = 1; |
| 1087 | va_start(ap, command); |
| 1088 | while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) { |
| 1089 | argc++; |
| 1090 | } |
| 1091 | argv[argc] = NULL; /* make sure */ |
| 1092 | va_end(ap); |
| 1093 | |
| 1094 | if (pipe(infd) != 0) { |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
| 1098 | if (pipe(outfd) != 0) { |
| 1099 | close(infd[0]); |
| 1100 | close(infd[1]); |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | fflush(NULL); |
| 1105 | switch (pid = fork()) { |
| 1106 | case -1: /* failure */ |
| 1107 | close(infd[0]); |
| 1108 | close(infd[1]); |
| 1109 | close(outfd[0]); |
| 1110 | close(outfd[1]); |
| 1111 | return 0; |
| 1112 | case 0: /* child */ |
| 1113 | dup2(infd[0], 0); |
| 1114 | dup2(outfd[1], 1); |
| 1115 | close(infd[0]); |
| 1116 | close(infd[1]); |
| 1117 | close(outfd[0]); |
| 1118 | close(outfd[1]); |
| 1119 | execvp(command, argv); |
| 1120 | exit(127); |
| 1121 | default: /* parent */ |
| 1122 | *in = fdopen(infd[1], "w"); |
| 1123 | *out = fdopen(outfd[0], "r"); |
| 1124 | close(infd[0]); |
| 1125 | close(outfd[1]); |
| 1126 | return pid; |
| 1127 | } |
| 1128 | /* unreached */ |
| 1129 | } |
| 1130 | |
| 1131 | static int run_mapping(char *physical, char *logical, int len, mapping_defn * map) |
| 1132 | { |
| 1133 | FILE *in, *out; |
| 1134 | int i, status; |
| 1135 | pid_t pid; |
| 1136 | |
| 1137 | |
| 1138 | pid = popen2(&in, &out, map->script, physical, NULL); |
| 1139 | if (pid == 0) { |
| 1140 | return 0; |
| 1141 | } |
| 1142 | for (i = 0; i < map->n_mappings; i++) { |
| 1143 | fprintf(in, "%s\n", map->mapping[i]); |
| 1144 | } |
| 1145 | fclose(in); |
| 1146 | waitpid(pid, &status, 0); |
| 1147 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
| 1148 | if (fgets(logical, len, out)) { |
| 1149 | char *pch = logical + xstrlen(logical) - 1; |
| 1150 | |
| 1151 | while (pch >= logical && isspace(*pch)) |
| 1152 | *(pch--) = '\0'; |
| 1153 | } |
| 1154 | } |
| 1155 | fclose(out); |
| 1156 | |
| 1157 | return 1; |
| 1158 | } |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1159 | #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */ |
| 1160 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1161 | |
| 1162 | static int lookfor_iface(char **ifaces, int n_ifaces, char *iface) |
| 1163 | { |
| 1164 | int i; |
| 1165 | |
| 1166 | for (i = 0; i < n_ifaces; i++) { |
| 1167 | if (strncmp(iface, ifaces[i], xstrlen(iface)) == 0) { |
| 1168 | if (ifaces[i][xstrlen(iface)] == '=') { |
| 1169 | return i; |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | return(-1); |
| 1175 | } |
| 1176 | |
| 1177 | static void add_to_state(char ***ifaces, int *n_ifaces, int *max_ifaces, char *new_iface) |
| 1178 | { |
| 1179 | if (*max_ifaces == *n_ifaces) { |
| 1180 | *max_ifaces = (*max_ifaces * 2) + 1; |
| 1181 | *ifaces = xrealloc(*ifaces, sizeof(**ifaces) * *max_ifaces); |
| 1182 | } |
| 1183 | |
| 1184 | (*ifaces)[(*n_ifaces)++] = new_iface; |
| 1185 | } |
| 1186 | |
| 1187 | extern int ifupdown_main(int argc, char **argv) |
| 1188 | { |
| 1189 | int (*cmds) (interface_defn *) = NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1190 | interfaces_file *defn; |
| 1191 | FILE *state_fp = NULL; |
| 1192 | char **target_iface = NULL; |
| 1193 | char **state = NULL; /* list of iface=liface */ |
| 1194 | char *interfaces = "/etc/network/interfaces"; |
| 1195 | char *statefile = "/etc/network/ifstate"; |
| 1196 | |
| 1197 | int do_all = 0; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1198 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1199 | int run_mappings = 1; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1200 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1201 | int force = 0; |
| 1202 | int n_target_ifaces = 0; |
| 1203 | int n_state = 0; |
| 1204 | int max_state = 0; |
| 1205 | int i; |
| 1206 | |
| 1207 | if (applet_name[2] == 'u') { |
| 1208 | /* ifup command */ |
| 1209 | cmds = iface_up; |
| 1210 | } else { |
| 1211 | /* ifdown command */ |
| 1212 | cmds = iface_down; |
| 1213 | } |
| 1214 | |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1215 | while ((i = getopt(argc, argv, "i:hvnamf")) != -1) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1216 | switch (i) { |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1217 | case 'i': /* interfaces */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1218 | interfaces = xstrdup(optarg); |
| 1219 | break; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1220 | case 'v': /* verbose */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1221 | verbose = 1; |
| 1222 | break; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1223 | case 'a': /* all */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1224 | do_all = 1; |
| 1225 | break; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1226 | case 'n': /* no-act */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1227 | no_act = 1; |
| 1228 | break; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1229 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
| 1230 | case 'm': /* no-mappings */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1231 | run_mappings = 0; |
| 1232 | break; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1233 | #endif |
| 1234 | case 'f': /* force */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1235 | force = 1; |
| 1236 | break; |
| 1237 | default: |
| 1238 | show_usage(); |
| 1239 | break; |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | if (argc - optind > 0) { |
| 1244 | if (do_all) { |
| 1245 | show_usage(); |
| 1246 | } |
| 1247 | } else { |
| 1248 | if (!do_all) { |
| 1249 | show_usage(); |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | defn = read_interfaces(interfaces); |
| 1254 | if (!defn) { |
| 1255 | error_msg_and_die("couldn't read interfaces file \"%s\"", interfaces); |
| 1256 | } |
| 1257 | |
| 1258 | state_fp = fopen(statefile, no_act ? "r" : "a+"); |
| 1259 | if (state_fp == NULL && !no_act) { |
| 1260 | perror_msg_and_die("failed to open statefile %s", statefile); |
| 1261 | } |
| 1262 | |
| 1263 | if (state_fp != NULL) { |
| 1264 | char buf[80]; |
| 1265 | char *p; |
| 1266 | |
| 1267 | if (!no_act) { |
| 1268 | int flags; |
| 1269 | struct flock lock; |
| 1270 | const int state_fd = fileno(state_fp); |
| 1271 | |
| 1272 | flags = fcntl(state_fd, F_GETFD); |
| 1273 | if ((flags < 0) || (fcntl(state_fd, F_SETFD, flags | FD_CLOEXEC) < 0)) { |
| 1274 | perror_msg_and_die("failed to set FD_CLOEXEC on statefile %s", statefile); |
| 1275 | } |
| 1276 | |
| 1277 | lock.l_type = F_WRLCK; |
| 1278 | lock.l_whence = SEEK_SET; |
| 1279 | lock.l_start = 0; |
| 1280 | lock.l_len = 0; |
| 1281 | |
| 1282 | if (fcntl(state_fd, F_SETLKW, &lock) < 0) { |
| 1283 | perror_msg_and_die("failed to lock statefile %s", statefile); |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | rewind(state_fp); |
| 1288 | while ((p = fgets(buf, sizeof buf, state_fp)) != NULL) { |
| 1289 | char *pch; |
| 1290 | |
| 1291 | pch = buf + xstrlen(buf) - 1; |
| 1292 | while (pch > buf && isspace(*pch)) { |
| 1293 | pch--; |
| 1294 | } |
| 1295 | *(pch + 1) = '\0'; |
| 1296 | |
| 1297 | pch = buf; |
| 1298 | while (isspace(*pch)) { |
| 1299 | pch++; |
| 1300 | } |
| 1301 | |
| 1302 | add_to_state(&state, &n_state, &max_state, xstrdup(pch)); |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | if (do_all) { |
| 1307 | if (cmds == iface_up) { |
| 1308 | target_iface = defn->autointerfaces; |
| 1309 | n_target_ifaces = defn->n_autointerfaces; |
| 1310 | } else if (cmds == iface_down) { |
| 1311 | target_iface = state; |
| 1312 | n_target_ifaces = n_state; |
| 1313 | } |
| 1314 | } else { |
| 1315 | target_iface = argv + optind; |
| 1316 | n_target_ifaces = argc - optind; |
| 1317 | } |
| 1318 | |
| 1319 | |
| 1320 | for (i = 0; i < n_target_ifaces; i++) { |
| 1321 | interface_defn *currif; |
| 1322 | char iface[80]; |
| 1323 | char liface[80]; |
| 1324 | char *pch; |
| 1325 | int okay = 0; |
| 1326 | |
| 1327 | strncpy(iface, target_iface[i], sizeof(iface)); |
| 1328 | iface[sizeof(iface) - 1] = '\0'; |
| 1329 | |
| 1330 | if ((pch = strchr(iface, '='))) { |
| 1331 | *pch = '\0'; |
| 1332 | strncpy(liface, pch + 1, sizeof(liface)); |
| 1333 | liface[sizeof(liface) - 1] = '\0'; |
| 1334 | } else { |
| 1335 | strncpy(liface, iface, sizeof(liface)); |
| 1336 | liface[sizeof(liface) - 1] = '\0'; |
| 1337 | } |
| 1338 | if (!force) { |
| 1339 | int already_up = lookfor_iface(state, n_state, iface);; |
| 1340 | |
| 1341 | if (cmds == iface_up) { |
| 1342 | /* ifup */ |
| 1343 | if (already_up != -1) { |
| 1344 | error_msg("interface %s already configured", iface); |
| 1345 | continue; |
| 1346 | } |
| 1347 | } else { |
| 1348 | /* ifdown */ |
| 1349 | if (already_up == -1) { |
| 1350 | error_msg("interface %s not configured", iface); |
| 1351 | continue; |
| 1352 | } |
| 1353 | strncpy(liface, strchr(state[already_up], '=') + 1, 80); |
| 1354 | liface[79] = 0; |
| 1355 | } |
| 1356 | } |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1357 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1358 | if ((cmds == iface_up) && run_mappings) { |
| 1359 | mapping_defn *currmap; |
| 1360 | |
| 1361 | for (currmap = defn->mappings; currmap; currmap = currmap->next) { |
| 1362 | |
| 1363 | for (i = 0; i < currmap->n_matches; i++) { |
| 1364 | if (fnmatch(currmap->match[i], liface, 0) != 0) |
| 1365 | continue; |
| 1366 | if (verbose) { |
| 1367 | error_msg("Running mapping script %s on %s", currmap->script, liface); |
| 1368 | } |
| 1369 | run_mapping(iface, liface, sizeof(liface), currmap); |
| 1370 | break; |
| 1371 | } |
| 1372 | } |
| 1373 | } |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1374 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1375 | |
| 1376 | for (currif = defn->ifaces; currif; currif = currif->next) { |
| 1377 | if (strcmp(liface, currif->iface) == 0) { |
| 1378 | char *oldiface = currif->iface; |
| 1379 | |
| 1380 | okay = 1; |
| 1381 | |
| 1382 | currif->iface = iface; |
| 1383 | |
| 1384 | if (verbose) { |
| 1385 | error_msg("Configuring interface %s=%s (%s)", iface, liface, currif->address_family->name); |
| 1386 | } |
| 1387 | |
| 1388 | switch (cmds(currif)) { |
| 1389 | case -1: |
| 1390 | printf |
| 1391 | ("Don't seem to be have all the variables for %s/%s.\n", |
| 1392 | liface, currif->address_family->name); |
| 1393 | break; |
| 1394 | case 0: |
| 1395 | /* this wasn't entirely successful, should it be added to |
| 1396 | * the state file? |
| 1397 | */ |
| 1398 | case 1: |
| 1399 | /* successful */ |
| 1400 | } |
| 1401 | currif->iface = oldiface; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | if (!okay && !force) { |
| 1406 | error_msg("Ignoring unknown interface %s=%s.", iface, liface); |
| 1407 | } else { |
| 1408 | int already_up = lookfor_iface(state, n_state, iface); |
| 1409 | |
| 1410 | if (cmds == iface_up) { |
| 1411 | char *newiface = xmalloc(xstrlen(iface) + 1 + xstrlen(liface) + 1); |
| 1412 | sprintf(newiface, "%s=%s", iface, liface); |
| 1413 | if (already_up == -1) { |
| 1414 | add_to_state(&state, &n_state, &max_state, newiface); |
| 1415 | } else { |
| 1416 | free(state[already_up]); |
| 1417 | state[already_up] = newiface; |
| 1418 | } |
| 1419 | } else if (cmds == iface_down) { |
| 1420 | if (already_up != -1) { |
| 1421 | state[already_up] = state[--n_state]; |
| 1422 | } |
| 1423 | } |
| 1424 | } |
| 1425 | if (state_fp != NULL && !no_act) { |
| 1426 | if (ftruncate(fileno(state_fp), 0) < 0) { |
| 1427 | error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno)); |
| 1428 | } |
| 1429 | |
| 1430 | rewind(state_fp); |
| 1431 | for (i = 0; i < n_state; i++) { |
| 1432 | fputs(state[i], state_fp); |
| 1433 | fputc('\n', state_fp); |
| 1434 | } |
| 1435 | fflush(state_fp); |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | if (state_fp != NULL) { |
| 1440 | fclose(state_fp); |
| 1441 | state_fp = NULL; |
| 1442 | } |
| 1443 | |
| 1444 | return 0; |
| 1445 | } |