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
]);
207 strcpy(temp
, ""); findkey(kv
, "DNS1", temp
);
210 errorbox(ctr
[TR_MISSING_DNS
]);
214 strcpy(temp
, ""); findkey(kv
, "DEFAULT_GATEWAY", temp
);
217 errorbox(ctr
[TR_MISSING_DEFAULT
]);
226 /* Shows the main menu and a summary of the current settings. */
229 char *sections
[] = { ctr
[TR_NETWORK_CONFIGURATION_TYPE
],
230 ctr
[TR_DRIVERS_AND_CARD_ASSIGNMENTS
],
231 ctr
[TR_ADDRESS_SETTINGS
],
232 ctr
[TR_DNS_AND_GATEWAY_SETTINGS
], NULL
};
234 static int choice
= 0;
235 struct keyvalue
*kv
= initkeyvalues();
237 char temp
[STRING_SIZE
] = "1";
240 char networkrestart
[STRING_SIZE
] = "";
242 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
245 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
249 if (netaddresschange
)
250 strcpy(networkrestart
, ctr
[TR_RESTART_REQUIRED
]);
252 strcpy(temp
, ""); findkey(kv
, "CONFIG_TYPE", temp
);
255 if (x
< 0 || x
> 4) x
= 0;
256 /* Format heading bit. */
257 snprintf(message
, 1000, ctr
[TR_CURRENT_CONFIG
], configtypenames
[x
],
259 rc
= newtWinMenu(ctr
[TR_NETWORK_CONFIGURATION_MENU
], message
, 50, 5, 5, 6,
260 sections
, &choice
, ctr
[TR_OK
], ctr
[TR_DONE
], NULL
);
262 if (rc
== 0 || rc
== 1)
270 /* Here they choose general network config, number of nics etc. */
271 int configtypemenu(void)
273 struct keyvalue
*kv
= initkeyvalues();
274 char temp
[STRING_SIZE
] = "1";
279 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
282 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
286 found
= scan_network_cards();
288 findkey(kv
, "CONFIG_TYPE", temp
); choise
= atol(temp
);
291 sprintf(message
, ctr
[TR_NETWORK_CONFIGURATION_TYPE_LONG
], NAME
);
292 rc
= newtWinMenu(ctr
[TR_NETWORK_CONFIGURATION_TYPE
], message
, 50, 5, 5,
293 6, configtypenames
, &choise
, ctr
[TR_OK
], ctr
[TR_CANCEL
], NULL
);
294 if ( configtypecards
[choise
] > found
) {
295 sprintf(message
, ctr
[TR_NOT_ENOUGH_INTERFACES
] , configtypecards
[choise
], found
);
299 if (rc
== 0 || rc
== 1)
302 sprintf(temp
, "%d", choise
);
303 replacekeyvalue(kv
, "CONFIG_TYPE", temp
);
304 configtype
= atol(temp
);
306 clear_card_entry(_RED_CARD_
);
308 clear_card_entry(_ORANGE_CARD_
);
310 clear_card_entry(_BLUE_CARD_
);
312 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
313 netaddresschange
= 1;
320 /* Driver menu. Choose drivers.. */
323 struct keyvalue
*kv
= initkeyvalues();
324 char message
[STRING_SIZE
];
325 char temp
[STRING_SIZE
] = "1";
328 int i
, rc
, kcount
= 0, neednics
;
330 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
333 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
337 if (findkey(kv
, "CONFIG_TYPE", temp
))
338 configtype
= atol(temp
);
340 fprintf(flog
,"setting CONFIG_TYPE = %s\n",temp
);
341 configtype
= atol(temp
);
342 replacekeyvalue(kv
, "CONFIG_TYPE", temp
);
343 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
346 strcpy(message
, ctr
[TR_CONFIGURE_NETWORK_DRIVERS
]);
351 sprintf(temp
, "GREEN: %s\n", knics
[_GREEN_CARD_
].description
);
352 strcat(message
, temp
);
353 if (strlen(knics
[_GREEN_CARD_
].macaddr
) ) {
354 sprintf(temp
, "GREEN: (%s) %s green0\n", knics
[_GREEN_CARD_
].macaddr
, ctr
[TR_AS
]);
355 strcat(message
, temp
);
360 sprintf(temp
, "RED: %s\n", knics
[_RED_CARD_
].description
);
361 strcat(message
, temp
);
362 if (strlen(knics
[_RED_CARD_
].macaddr
) ) {
363 sprintf(temp
, "RED: (%s) %s red0\n", knics
[_RED_CARD_
].macaddr
, ctr
[TR_AS
]);
364 strcat(message
, temp
);
369 sprintf(temp
, "ORANGE: %s\n", knics
[_ORANGE_CARD_
].description
);
370 strcat(message
, temp
);
371 if ( strlen(knics
[_ORANGE_CARD_
].macaddr
) ) {
372 sprintf(temp
, "ORANGE: (%s) %s orange0\n", knics
[_ORANGE_CARD_
].macaddr
, ctr
[TR_AS
]);
373 strcat(message
, temp
);
378 sprintf(temp
, "BLUE: %s\n", knics
[_BLUE_CARD_
].description
);
379 strcat(message
, temp
);
380 if (strlen(knics
[_BLUE_CARD_
].macaddr
)) {
381 sprintf(temp
, "BLUE: (%s) %s blue0\n", knics
[_BLUE_CARD_
].macaddr
, ctr
[TR_AS
]);
382 strcat(message
, temp
);
387 for ( i
=0 ; i
<4; i
++)
388 if (strcmp(knics
[i
].macaddr
, ""))
391 if (neednics
= kcount
)
393 strcat(message
, ctr
[TR_DO_YOU_WISH_TO_CHANGE_THESE_SETTINGS
]);
394 rc
= newtWinChoice(ctr
[TR_DRIVERS_AND_CARD_ASSIGNMENTS
], ctr
[TR_OK
],
395 ctr
[TR_CANCEL
], message
);
396 if (rc
== 0 || rc
== 1)
408 int set_menu_entry_for(int *nr
, int *card
)
413 int changedrivers(void)
415 struct keyvalue
*kv
= initkeyvalues();
416 char temp
[STRING_SIZE
], message
[STRING_SIZE
];
418 int green
= 0, red
= 0, blue
= 0, orange
= 0;
419 char MenuInhalt
[10][180];
420 char *pMenuInhalt
[10];
421 int count
= 0, choise
= 0, rc
;
424 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
427 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
431 runcommandwithstatus("/etc/rc.d/init.d/network stop red blue orange",
432 ctr
[TR_PUSHING_NON_LOCAL_NETWORK_DOWN
]);
434 findkey(kv
, "CONFIG_TYPE", temp
); configtype
= atol(temp
);
436 { green
= 1; red
= 1; }
437 else if (configtype
== 2)
438 { green
= 1; red
= 1; orange
= 1; }
439 else if (configtype
== 3)
440 { green
= 1; red
= 1; blue
= 1; }
441 else if (configtype
== 4)
442 { green
= 1; red
=1; orange
=1; blue
= 1; }
443 else if (configtype
== "")
444 { green
= 1; red
= 1; }
449 strcpy(message
, ctr
[TR_INTERFACE_CHANGE
]);
452 strcpy(MenuInhalt
[count
], "GREEN");
453 pMenuInhalt
[count
] = MenuInhalt
[count
];
454 NicEntry
[_GREEN_CARD_
] = count
;
455 sprintf(temp
, "GREEN: %s\n", knics
[_GREEN_CARD_
].description
);
456 strcat(message
, temp
);
457 if ( strlen(knics
[_GREEN_CARD_
].macaddr
) ) {
458 sprintf(temp
, "GREEN: (%s) %s green0\n", knics
[_GREEN_CARD_
].macaddr
, ctr
[TR_AS
]);
459 strcat(message
, temp
);
465 strcpy(MenuInhalt
[count
], "RED");
466 pMenuInhalt
[count
] = MenuInhalt
[count
];
467 NicEntry
[_RED_CARD_
] = count
;
468 sprintf(temp
, "RED: %s\n", knics
[_RED_CARD_
].description
);
469 strcat(message
, temp
);
470 if ( strlen(knics
[_RED_CARD_
].macaddr
) ) {
471 sprintf(temp
, "RED: (%s) %s red0\n", knics
[_RED_CARD_
].macaddr
, ctr
[TR_AS
]);
472 strcat(message
, temp
);
478 strcpy(MenuInhalt
[count
], "ORANGE");
479 pMenuInhalt
[count
] = MenuInhalt
[count
];
480 NicEntry
[_ORANGE_CARD_
] = count
;
481 sprintf(temp
, "ORANGE: %s\n", knics
[_ORANGE_CARD_
].description
);
482 strcat(message
, temp
);
483 if ( strlen(knics
[_ORANGE_CARD_
].macaddr
) ) {
484 sprintf(temp
, "ORANGE: (%s) %s orange0\n", knics
[_ORANGE_CARD_
].macaddr
, ctr
[TR_AS
]);
485 strcat(message
, temp
);
491 strcpy(MenuInhalt
[count
], "BLUE");
492 pMenuInhalt
[count
] = MenuInhalt
[count
];
493 NicEntry
[_BLUE_CARD_
] = count
;
494 sprintf(temp
, "BLUE: %s\n", knics
[_BLUE_CARD_
].description
);
495 strcat(message
, temp
);
496 if ( strlen(knics
[_BLUE_CARD_
].macaddr
) ) {
497 sprintf(temp
, "BLUE: (%s) %s blue0\n", knics
[_BLUE_CARD_
].macaddr
, ctr
[TR_AS
]);
498 strcat(message
, temp
);
502 pMenuInhalt
[count
] = NULL
;
504 rc
= newtWinMenu( ctr
[TR_NETCARD_COLOR
], message
, 70, 5, 5, 6, pMenuInhalt
, &choise
, ctr
[TR_SELECT
], ctr
[TR_REMOVE
], ctr
[TR_DONE
], NULL
);
506 if ( rc
== 0 || rc
== 1) {
507 if ((green
) && ( choise
== NicEntry
[0])) nicmenu(_GREEN_CARD_
);
508 if ((red
) && ( choise
== NicEntry
[1])) nicmenu(_RED_CARD_
);
509 if ((orange
) && ( choise
== NicEntry
[2])) nicmenu(_ORANGE_CARD_
);
510 if ((blue
) && ( choise
== NicEntry
[3])) nicmenu(_BLUE_CARD_
);
511 netaddresschange
= 1;
512 } else if (rc
== 2) {
513 if ((green
) && ( choise
== NicEntry
[0])) ask_clear_card_entry(_GREEN_CARD_
);
514 if ((red
) && ( choise
== NicEntry
[1])) ask_clear_card_entry(_RED_CARD_
);
515 if ((orange
) && ( choise
== NicEntry
[2])) ask_clear_card_entry(_ORANGE_CARD_
);
516 if ((blue
) && ( choise
== NicEntry
[3])) ask_clear_card_entry(_BLUE_CARD_
);
517 netaddresschange
= 1;
526 // Let user change GREEN address.
527 int greenaddressmenu(void)
529 struct keyvalue
*kv
= initkeyvalues();
533 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
536 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
540 sprintf(message
, ctr
[TR_WARNING_LONG
], NAME
);
541 rc
= newtWinChoice(ctr
[TR_WARNING
], ctr
[TR_OK
], ctr
[TR_CANCEL
], message
);
543 if (rc
== 0 || rc
== 1)
545 if (changeaddress(kv
, "GREEN", 0, ""))
547 netaddresschange
= 1;
548 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
558 // They can change BLUE, ORANGE and GREEN too :)
559 int addressesmenu(void)
561 struct keyvalue
*kv
= initkeyvalues();
562 struct keyvalue
*mainkv
= initkeyvalues();
565 char *green
= "GREEN";
566 char *orange
= "ORANGE";
570 char greenaddress
[STRING_SIZE
];
571 char oldgreenaddress
[STRING_SIZE
];
572 char temp
[STRING_SIZE
];
573 char temp2
[STRING_SIZE
];
578 char hostname
[STRING_SIZE
];
580 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
583 freekeyvalues(mainkv
);
584 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
587 if (!(readkeyvalues(mainkv
, CONFIG_ROOT
"/main/settings")))
590 freekeyvalues(mainkv
);
591 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
595 strcpy(temp
, "0"); findkey(kv
, "CONFIG_TYPE", temp
);
596 configtype
= atol(temp
);
607 sections
[c
] = orange
;
621 rc
= newtWinMenu(ctr
[TR_ADDRESS_SETTINGS
],
622 ctr
[TR_SELECT_THE_INTERFACE_YOU_WISH_TO_RECONFIGURE
], 50, 5,
623 5, 6, sections
, &choice
, ctr
[TR_OK
], ctr
[TR_DONE
], NULL
);
625 if (rc
== 0 || rc
== 1)
627 if (strcmp(sections
[choice
], "GREEN") == 0)
629 findkey(kv
, "GREEN_ADDRESS", oldgreenaddress
);
630 sprintf(message
, ctr
[TR_WARNING_LONG
], NAME
);
631 rc
= newtWinChoice(ctr
[TR_WARNING
], ctr
[TR_OK
], ctr
[TR_CANCEL
],
633 if (rc
== 0 || rc
== 1)
635 if (changeaddress(kv
, "GREEN", 0, ""))
637 netaddresschange
= 1;
638 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
640 findkey(kv
, "GREEN_ADDRESS", greenaddress
);
641 snprintf(temp
, STRING_SIZE
-1, "option routers %s", oldgreenaddress
);
642 snprintf(temp2
, STRING_SIZE
-1, "option routers %s", greenaddress
);
643 replace (CONFIG_ROOT
"/dhcp/dhcpd.conf", temp
, temp2
);
644 chown (CONFIG_ROOT
"/dhcp/dhcpd.conf", 99, 99);
648 if (strcmp(sections
[choice
], "BLUE") == 0)
650 if (changeaddress(kv
, "BLUE", 0, ""))
651 netaddresschange
= 1;
653 if (strcmp(sections
[choice
], "ORANGE") == 0)
655 if (changeaddress(kv
, "ORANGE", 0, ""))
656 netaddresschange
= 1;
658 if (strcmp(sections
[choice
], "RED") == 0)
660 strcpy(hostname
, "");
661 findkey(mainkv
, "HOSTNAME", hostname
);
662 if (changeaddress(kv
, "RED", 1, hostname
))
663 netaddresschange
= 1;
670 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
672 freekeyvalues(mainkv
);
677 /* DNS and default gateway.... */
678 int dnsgatewaymenu(void)
680 struct keyvalue
*kv
= initkeyvalues();
682 char temp
[STRING_SIZE
] = "0";
683 struct newtWinEntry entries
[DNSGATEWAY_TOTAL
+1];
684 char *values
[DNSGATEWAY_TOTAL
]; /* pointers for the values. */
689 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
692 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
696 entries
[DNS1
].text
= ctr
[TR_PRIMARY_DNS
];
697 strcpy(temp
, ""); findkey(kv
, "DNS1", temp
);
698 values
[DNS1
] = strdup(temp
);
699 entries
[DNS1
].value
= &values
[DNS1
];
700 entries
[DNS1
].flags
= 0;
702 entries
[DNS2
].text
= ctr
[TR_SECONDARY_DNS
];
703 strcpy(temp
, ""); findkey(kv
, "DNS2", temp
);
704 values
[DNS2
] = strdup(temp
);
705 entries
[DNS2
].value
= &values
[DNS2
];
706 entries
[DNS2
].flags
= 0;
708 entries
[DEFAULT_GATEWAY
].text
= ctr
[TR_DEFAULT_GATEWAY
];
709 strcpy(temp
, ""); findkey(kv
, "DEFAULT_GATEWAY", temp
);
710 values
[DEFAULT_GATEWAY
] = strdup(temp
);
711 entries
[DEFAULT_GATEWAY
].value
= &values
[DEFAULT_GATEWAY
];
712 entries
[DEFAULT_GATEWAY
].flags
= 0;
714 entries
[DNSGATEWAY_TOTAL
].text
= NULL
;
715 entries
[DNSGATEWAY_TOTAL
].value
= NULL
;
716 entries
[DNSGATEWAY_TOTAL
].flags
= 0;
722 rc
= newtWinEntries(ctr
[TR_DNS_AND_GATEWAY_SETTINGS
],
723 ctr
[TR_DNS_AND_GATEWAY_SETTINGS_LONG
], 50, 5, 5, 18, entries
,
724 ctr
[TR_OK
], ctr
[TR_CANCEL
], NULL
);
725 if (rc
== 0 || rc
== 1)
727 strcpy(message
, ctr
[TR_INVALID_FIELDS
]);
728 if (strlen(values
[DNS1
]))
730 if (inet_addr(values
[DNS1
]) == INADDR_NONE
)
732 strcat(message
, ctr
[TR_PRIMARY_DNS_CR
]);
736 if (strlen(values
[DNS2
]))
738 if (inet_addr(values
[DNS2
]) == INADDR_NONE
)
740 strcat(message
, ctr
[TR_SECONDARY_DNS_CR
]);
744 if (strlen(values
[DEFAULT_GATEWAY
]))
746 if (inet_addr(values
[DEFAULT_GATEWAY
]) == INADDR_NONE
)
748 strcat(message
, ctr
[TR_DEFAULT_GATEWAY_CR
]);
752 if (!strlen(values
[DNS1
]) && strlen(values
[DNS2
]))
754 strcpy(message
, ctr
[TR_SECONDARY_WITHOUT_PRIMARY_DNS
]);
762 replacekeyvalue(kv
, "DNS1", values
[DNS1
]);
763 replacekeyvalue(kv
, "DNS2", values
[DNS2
]);
764 replacekeyvalue(kv
, "DEFAULT_GATEWAY", values
[DEFAULT_GATEWAY
]);
765 netaddresschange
= 1;
768 free(values
[DEFAULT_GATEWAY
]);
769 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");