1 /* SmoothWall setup program.
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
6 * (c) Lawrence Manning, 2001
7 * The big one: networking.
15 #define DEFAULT_GATEWAY 2
16 #define DNSGATEWAY_TOTAL 3
26 #define HAS_RED (configtype == 1 || configtype == 2 || configtype == 3 || configtype == 4)
27 #define HAS_ORANGE (configtype == 2 || configtype == 4)
28 #define HAS_BLUE (configtype == 3 || configtype == 4)
29 #define RED_IS_NOT_ETH (configtype == 0)
31 extern struct nic nics
[];
32 extern struct knic knics
[];
34 char *configtypenames
[] = {
36 "GREEN + RED + ORANGE",
38 "GREEN + RED + ORANGE + BLUE",
40 int configtypecards
[] = {
42 3, // "GREEN + RED + ORANGE",
43 3, // "GREEN + RED + BLUE",
44 4 // "GREEN + RED + ORANGE + BLUE",
52 int configtypemenu(void);
54 int changedrivers(void);
55 int greenaddressmenu(void);
56 int addressesmenu(void);
57 int dnsgatewaymenu(void);
59 int handlenetworking(void)
67 found
= scan_network_cards();
94 if (oktoleave()) done
= 1;
104 /* Restart networking! */
105 if (netaddresschange
)
107 runcommandwithstatus("/etc/rc.d/init.d/network stop",
108 ctr
[TR_PUSHING_NETWORK_DOWN
]);
112 runcommandwithstatus("/etc/rc.d/init.d/network start",
113 ctr
[TR_PULLING_NETWORK_UP
]);
122 struct keyvalue
*kv
= initkeyvalues();
123 char temp
[STRING_SIZE
];
127 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
130 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
134 strcpy(temp
, "1"); findkey(kv
, "CONFIG_TYPE", temp
); configtype
= atol(temp
);
135 if (configtype
< 1 || configtype
> 4) configtype
= 1;
139 strcpy(temp
, ""); findkey(kv
, "GREEN_DEV", temp
);
142 errorbox(ctr
[TR_NO_GREEN_INTERFACE
]);
146 if (!(interfacecheck(kv
, "GREEN")))
148 errorbox(ctr
[TR_MISSING_GREEN_IP
]);
156 strcpy(temp
, ""); findkey(kv
, "RED_DEV", temp
);
159 rc
= newtWinChoice(ctr
[TR_ERROR
], ctr
[TR_OK
], ctr
[TR_IGNORE
], ctr
[TR_NO_RED_INTERFACE
]);
160 if (rc
== 0 || rc
== 1)
166 if (!(interfacecheck(kv
, "RED")))
168 errorbox(ctr
[TR_MISSING_RED_IP
]);
175 strcpy(temp
, ""); findkey(kv
, "ORANGE_DEV", temp
);
178 errorbox(ctr
[TR_NO_ORANGE_INTERFACE
]);
182 if (!(interfacecheck(kv
, "ORANGE")))
184 errorbox(ctr
[TR_MISSING_ORANGE_IP
]);
191 strcpy(temp
, ""); findkey(kv
, "BLUE_DEV", temp
);
194 errorbox(ctr
[TR_NO_BLUE_INTERFACE
]);
198 if (!(interfacecheck(kv
, "BLUE")))
200 errorbox(ctr
[TR_MISSING_BLUE_IP
]);
206 strcpy(temp
, ""); findkey(kv
, "RED_TYPE", temp
);
207 if ((configtype
== 0) || (strcmp(temp
, "STATIC") == 0))
209 strcpy(temp
, ""); findkey(kv
, "DNS1", temp
);
212 errorbox(ctr
[TR_MISSING_DNS
]);
216 strcpy(temp
, ""); findkey(kv
, "DEFAULT_GATEWAY", temp
);
219 errorbox(ctr
[TR_MISSING_DEFAULT
]);
228 /* Shows the main menu and a summary of the current settings. */
231 char *sections
[] = { ctr
[TR_NETWORK_CONFIGURATION_TYPE
],
232 ctr
[TR_DRIVERS_AND_CARD_ASSIGNMENTS
],
233 ctr
[TR_ADDRESS_SETTINGS
],
234 ctr
[TR_DNS_AND_GATEWAY_SETTINGS
], NULL
};
236 static int choice
= 0;
237 struct keyvalue
*kv
= initkeyvalues();
239 char temp
[STRING_SIZE
] = "1";
242 char networkrestart
[STRING_SIZE
] = "";
244 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
247 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
251 if (netaddresschange
)
252 strcpy(networkrestart
, ctr
[TR_RESTART_REQUIRED
]);
254 strcpy(temp
, ""); findkey(kv
, "CONFIG_TYPE", temp
);
257 if (x
< 0 || x
> 4) x
= 0;
258 /* Format heading bit. */
259 snprintf(message
, 1000, ctr
[TR_CURRENT_CONFIG
], configtypenames
[x
],
261 rc
= newtWinMenu(ctr
[TR_NETWORK_CONFIGURATION_MENU
], message
, 50, 5, 5, 6,
262 sections
, &choice
, ctr
[TR_OK
], ctr
[TR_DONE
], NULL
);
264 if (rc
== 0 || rc
== 1)
272 /* Here they choose general network config, number of nics etc. */
273 int configtypemenu(void)
275 struct keyvalue
*kv
= initkeyvalues();
276 char temp
[STRING_SIZE
] = "1";
281 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
284 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
288 found
= scan_network_cards();
290 findkey(kv
, "CONFIG_TYPE", temp
); choise
= atol(temp
);
293 sprintf(message
, ctr
[TR_NETWORK_CONFIGURATION_TYPE_LONG
], NAME
);
294 rc
= newtWinMenu(ctr
[TR_NETWORK_CONFIGURATION_TYPE
], message
, 50, 5, 5,
295 6, configtypenames
, &choise
, ctr
[TR_OK
], ctr
[TR_CANCEL
], NULL
);
296 if ( configtypecards
[choise
] > found
) {
297 sprintf(message
, ctr
[TR_NOT_ENOUGH_INTERFACES
] , configtypecards
[choise
], found
);
301 if (rc
== 0 || rc
== 1)
304 sprintf(temp
, "%d", choise
);
305 replacekeyvalue(kv
, "CONFIG_TYPE", temp
);
306 configtype
= atol(temp
);
308 clear_card_entry(_RED_CARD_
);
310 clear_card_entry(_ORANGE_CARD_
);
312 clear_card_entry(_BLUE_CARD_
);
314 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
315 netaddresschange
= 1;
322 /* Driver menu. Choose drivers.. */
325 struct keyvalue
*kv
= initkeyvalues();
326 char message
[STRING_SIZE
];
327 char temp
[STRING_SIZE
] = "1";
330 int i
, rc
, kcount
= 0, neednics
;
332 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
335 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
339 if (findkey(kv
, "CONFIG_TYPE", temp
))
340 configtype
= atol(temp
);
342 fprintf(flog
,"setting CONFIG_TYPE = %s\n",temp
);
343 configtype
= atol(temp
);
344 replacekeyvalue(kv
, "CONFIG_TYPE", temp
);
345 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
348 strcpy(message
, ctr
[TR_CONFIGURE_NETWORK_DRIVERS
]);
353 sprintf(temp
, "GREEN: %s\n", knics
[_GREEN_CARD_
].description
);
354 strcat(message
, temp
);
355 if (strlen(knics
[_GREEN_CARD_
].macaddr
) ) {
356 sprintf(temp
, "GREEN: (%s) %s green0\n", knics
[_GREEN_CARD_
].macaddr
, ctr
[TR_AS
]);
357 strcat(message
, temp
);
362 sprintf(temp
, "RED: %s\n", knics
[_RED_CARD_
].description
);
363 strcat(message
, temp
);
364 if (strlen(knics
[_RED_CARD_
].macaddr
) ) {
365 sprintf(temp
, "RED: (%s) %s red0\n", knics
[_RED_CARD_
].macaddr
, ctr
[TR_AS
]);
366 strcat(message
, temp
);
371 sprintf(temp
, "ORANGE: %s\n", knics
[_ORANGE_CARD_
].description
);
372 strcat(message
, temp
);
373 if ( strlen(knics
[_ORANGE_CARD_
].macaddr
) ) {
374 sprintf(temp
, "ORANGE: (%s) %s orange0\n", knics
[_ORANGE_CARD_
].macaddr
, ctr
[TR_AS
]);
375 strcat(message
, temp
);
380 sprintf(temp
, "BLUE: %s\n", knics
[_BLUE_CARD_
].description
);
381 strcat(message
, temp
);
382 if (strlen(knics
[_BLUE_CARD_
].macaddr
)) {
383 sprintf(temp
, "BLUE: (%s) %s blue0\n", knics
[_BLUE_CARD_
].macaddr
, ctr
[TR_AS
]);
384 strcat(message
, temp
);
389 for ( i
=0 ; i
<4; i
++)
390 if (strcmp(knics
[i
].macaddr
, ""))
393 if (neednics
= kcount
)
395 strcat(message
, ctr
[TR_DO_YOU_WISH_TO_CHANGE_THESE_SETTINGS
]);
396 rc
= newtWinChoice(ctr
[TR_DRIVERS_AND_CARD_ASSIGNMENTS
], ctr
[TR_OK
],
397 ctr
[TR_CANCEL
], message
);
398 if (rc
== 0 || rc
== 1)
410 int set_menu_entry_for(int *nr
, int *card
)
415 int changedrivers(void)
417 struct keyvalue
*kv
= initkeyvalues();
418 char temp
[STRING_SIZE
], message
[STRING_SIZE
];
420 int green
= 0, red
= 0, blue
= 0, orange
= 0;
421 char MenuInhalt
[10][180];
422 char *pMenuInhalt
[10];
423 int count
= 0, choise
= 0, rc
;
426 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
429 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
433 runcommandwithstatus("/etc/rc.d/init.d/network stop red blue orange",
434 ctr
[TR_PUSHING_NON_LOCAL_NETWORK_DOWN
]);
436 findkey(kv
, "CONFIG_TYPE", temp
); configtype
= atol(temp
);
438 { green
= 1; red
= 1; }
439 else if (configtype
== 2)
440 { green
= 1; red
= 1; orange
= 1; }
441 else if (configtype
== 3)
442 { green
= 1; red
= 1; blue
= 1; }
443 else if (configtype
== 4)
444 { green
= 1; red
=1; orange
=1; blue
= 1; }
445 else if (configtype
== "")
446 { green
= 1; red
= 1; }
451 strcpy(message
, ctr
[TR_INTERFACE_CHANGE
]);
454 strcpy(MenuInhalt
[count
], "GREEN");
455 pMenuInhalt
[count
] = MenuInhalt
[count
];
456 NicEntry
[_GREEN_CARD_
] = count
;
457 sprintf(temp
, "GREEN: %s\n", knics
[_GREEN_CARD_
].description
);
458 strcat(message
, temp
);
459 if ( strlen(knics
[_GREEN_CARD_
].macaddr
) ) {
460 sprintf(temp
, "GREEN: (%s) %s green0\n", knics
[_GREEN_CARD_
].macaddr
, ctr
[TR_AS
]);
461 strcat(message
, temp
);
467 strcpy(MenuInhalt
[count
], "RED");
468 pMenuInhalt
[count
] = MenuInhalt
[count
];
469 NicEntry
[_RED_CARD_
] = count
;
470 sprintf(temp
, "RED: %s\n", knics
[_RED_CARD_
].description
);
471 strcat(message
, temp
);
472 if ( strlen(knics
[_RED_CARD_
].macaddr
) ) {
473 sprintf(temp
, "RED: (%s) %s red0\n", knics
[_RED_CARD_
].macaddr
, ctr
[TR_AS
]);
474 strcat(message
, temp
);
480 strcpy(MenuInhalt
[count
], "ORANGE");
481 pMenuInhalt
[count
] = MenuInhalt
[count
];
482 NicEntry
[_ORANGE_CARD_
] = count
;
483 sprintf(temp
, "ORANGE: %s\n", knics
[_ORANGE_CARD_
].description
);
484 strcat(message
, temp
);
485 if ( strlen(knics
[_ORANGE_CARD_
].macaddr
) ) {
486 sprintf(temp
, "ORANGE: (%s) %s orange0\n", knics
[_ORANGE_CARD_
].macaddr
, ctr
[TR_AS
]);
487 strcat(message
, temp
);
493 strcpy(MenuInhalt
[count
], "BLUE");
494 pMenuInhalt
[count
] = MenuInhalt
[count
];
495 NicEntry
[_BLUE_CARD_
] = count
;
496 sprintf(temp
, "BLUE: %s\n", knics
[_BLUE_CARD_
].description
);
497 strcat(message
, temp
);
498 if ( strlen(knics
[_BLUE_CARD_
].macaddr
) ) {
499 sprintf(temp
, "BLUE: (%s) %s blue0\n", knics
[_BLUE_CARD_
].macaddr
, ctr
[TR_AS
]);
500 strcat(message
, temp
);
504 pMenuInhalt
[count
] = NULL
;
506 rc
= newtWinMenu( ctr
[TR_NETCARD_COLOR
], message
, 70, 5, 5, 6, pMenuInhalt
, &choise
, ctr
[TR_SELECT
], ctr
[TR_REMOVE
], ctr
[TR_DONE
], NULL
);
508 if ( rc
== 0 || rc
== 1) {
509 if ((green
) && ( choise
== NicEntry
[0])) nicmenu(_GREEN_CARD_
);
510 if ((red
) && ( choise
== NicEntry
[1])) nicmenu(_RED_CARD_
);
511 if ((orange
) && ( choise
== NicEntry
[2])) nicmenu(_ORANGE_CARD_
);
512 if ((blue
) && ( choise
== NicEntry
[3])) nicmenu(_BLUE_CARD_
);
513 netaddresschange
= 1;
514 } else if (rc
== 2) {
515 if ((green
) && ( choise
== NicEntry
[0])) ask_clear_card_entry(_GREEN_CARD_
);
516 if ((red
) && ( choise
== NicEntry
[1])) ask_clear_card_entry(_RED_CARD_
);
517 if ((orange
) && ( choise
== NicEntry
[2])) ask_clear_card_entry(_ORANGE_CARD_
);
518 if ((blue
) && ( choise
== NicEntry
[3])) ask_clear_card_entry(_BLUE_CARD_
);
519 netaddresschange
= 1;
528 // Let user change GREEN address.
529 int greenaddressmenu(void)
531 struct keyvalue
*kv
= initkeyvalues();
535 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
538 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
542 sprintf(message
, ctr
[TR_WARNING_LONG
], NAME
);
543 rc
= newtWinChoice(ctr
[TR_WARNING
], ctr
[TR_OK
], ctr
[TR_CANCEL
], message
);
545 if (rc
== 0 || rc
== 1)
547 if (changeaddress(kv
, "GREEN", 0, ""))
549 netaddresschange
= 1;
550 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
560 // They can change BLUE, ORANGE and GREEN too :)
561 int addressesmenu(void)
563 struct keyvalue
*kv
= initkeyvalues();
564 struct keyvalue
*mainkv
= initkeyvalues();
567 char *green
= "GREEN";
568 char *orange
= "ORANGE";
572 char greenaddress
[STRING_SIZE
];
573 char oldgreenaddress
[STRING_SIZE
];
574 char temp
[STRING_SIZE
];
575 char temp2
[STRING_SIZE
];
580 char hostname
[STRING_SIZE
];
582 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
585 freekeyvalues(mainkv
);
586 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
589 if (!(readkeyvalues(mainkv
, CONFIG_ROOT
"/main/settings")))
592 freekeyvalues(mainkv
);
593 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
597 strcpy(temp
, "0"); findkey(kv
, "CONFIG_TYPE", temp
);
598 configtype
= atol(temp
);
609 sections
[c
] = orange
;
623 rc
= newtWinMenu(ctr
[TR_ADDRESS_SETTINGS
],
624 ctr
[TR_SELECT_THE_INTERFACE_YOU_WISH_TO_RECONFIGURE
], 50, 5,
625 5, 6, sections
, &choice
, ctr
[TR_OK
], ctr
[TR_DONE
], NULL
);
627 if (rc
== 0 || rc
== 1)
629 if (strcmp(sections
[choice
], "GREEN") == 0)
631 findkey(kv
, "GREEN_ADDRESS", oldgreenaddress
);
632 sprintf(message
, ctr
[TR_WARNING_LONG
], NAME
);
633 rc
= newtWinChoice(ctr
[TR_WARNING
], ctr
[TR_OK
], ctr
[TR_CANCEL
],
635 if (rc
== 0 || rc
== 1)
637 if (changeaddress(kv
, "GREEN", 0, ""))
639 netaddresschange
= 1;
640 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
642 findkey(kv
, "GREEN_ADDRESS", greenaddress
);
643 snprintf(temp
, STRING_SIZE
-1, "option routers %s", oldgreenaddress
);
644 snprintf(temp2
, STRING_SIZE
-1, "option routers %s", greenaddress
);
645 replace (CONFIG_ROOT
"/dhcp/dhcpd.conf", temp
, temp2
);
646 chown (CONFIG_ROOT
"/dhcp/dhcpd.conf", 99, 99);
650 if (strcmp(sections
[choice
], "BLUE") == 0)
652 if (changeaddress(kv
, "BLUE", 0, ""))
653 netaddresschange
= 1;
655 if (strcmp(sections
[choice
], "ORANGE") == 0)
657 if (changeaddress(kv
, "ORANGE", 0, ""))
658 netaddresschange
= 1;
660 if (strcmp(sections
[choice
], "RED") == 0)
662 strcpy(hostname
, "");
663 findkey(mainkv
, "HOSTNAME", hostname
);
664 if (changeaddress(kv
, "RED", 1, hostname
))
665 netaddresschange
= 1;
672 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
674 freekeyvalues(mainkv
);
679 /* DNS and default gateway.... */
680 int dnsgatewaymenu(void)
682 struct keyvalue
*kv
= initkeyvalues();
684 char temp
[STRING_SIZE
] = "0";
685 struct newtWinEntry entries
[DNSGATEWAY_TOTAL
+1];
686 char *values
[DNSGATEWAY_TOTAL
]; /* pointers for the values. */
691 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
694 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
698 entries
[DNS1
].text
= ctr
[TR_PRIMARY_DNS
];
699 strcpy(temp
, ""); findkey(kv
, "DNS1", temp
);
700 values
[DNS1
] = strdup(temp
);
701 entries
[DNS1
].value
= &values
[DNS1
];
702 entries
[DNS1
].flags
= 0;
704 entries
[DNS2
].text
= ctr
[TR_SECONDARY_DNS
];
705 strcpy(temp
, ""); findkey(kv
, "DNS2", temp
);
706 values
[DNS2
] = strdup(temp
);
707 entries
[DNS2
].value
= &values
[DNS2
];
708 entries
[DNS2
].flags
= 0;
710 entries
[DEFAULT_GATEWAY
].text
= ctr
[TR_DEFAULT_GATEWAY
];
711 strcpy(temp
, ""); findkey(kv
, "DEFAULT_GATEWAY", temp
);
712 values
[DEFAULT_GATEWAY
] = strdup(temp
);
713 entries
[DEFAULT_GATEWAY
].value
= &values
[DEFAULT_GATEWAY
];
714 entries
[DEFAULT_GATEWAY
].flags
= 0;
716 entries
[DNSGATEWAY_TOTAL
].text
= NULL
;
717 entries
[DNSGATEWAY_TOTAL
].value
= NULL
;
718 entries
[DNSGATEWAY_TOTAL
].flags
= 0;
724 rc
= newtWinEntries(ctr
[TR_DNS_AND_GATEWAY_SETTINGS
],
725 ctr
[TR_DNS_AND_GATEWAY_SETTINGS_LONG
], 50, 5, 5, 18, entries
,
726 ctr
[TR_OK
], ctr
[TR_CANCEL
], NULL
);
727 if (rc
== 0 || rc
== 1)
729 strcpy(message
, ctr
[TR_INVALID_FIELDS
]);
730 if (strlen(values
[DNS1
]))
732 if (inet_addr(values
[DNS1
]) == INADDR_NONE
)
734 strcat(message
, ctr
[TR_PRIMARY_DNS_CR
]);
738 if (strlen(values
[DNS2
]))
740 if (inet_addr(values
[DNS2
]) == INADDR_NONE
)
742 strcat(message
, ctr
[TR_SECONDARY_DNS_CR
]);
746 if (strlen(values
[DEFAULT_GATEWAY
]))
748 if (inet_addr(values
[DEFAULT_GATEWAY
]) == INADDR_NONE
)
750 strcat(message
, ctr
[TR_DEFAULT_GATEWAY_CR
]);
754 if (!strlen(values
[DNS1
]) && strlen(values
[DNS2
]))
756 strcpy(message
, ctr
[TR_SECONDARY_WITHOUT_PRIMARY_DNS
]);
764 replacekeyvalue(kv
, "DNS1", values
[DNS1
]);
765 replacekeyvalue(kv
, "DNS2", values
[DNS2
]);
766 replacekeyvalue(kv
, "DEFAULT_GATEWAY", values
[DEFAULT_GATEWAY
]);
767 netaddresschange
= 1;
770 free(values
[DEFAULT_GATEWAY
]);
771 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");