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",
50 int oktoleave(char *errormessage
);
52 int configtypemenu(void);
54 int changedrivers(void);
55 int greenaddressmenu(void);
56 int addressesmenu(void);
57 int dnsgatewaymenu(void);
59 int handlenetworking(void)
64 char errormessage
[STRING_SIZE
];
68 found
= scan_network_cards();
95 if (oktoleave(errormessage
))
98 errorbox(errormessage
);
108 /* Restart networking! */
109 if (netaddresschange
)
111 runcommandwithstatus("/etc/rc.d/init.d/network stop",
112 ctr
[TR_PUSHING_NETWORK_DOWN
]);
116 runcommandwithstatus("/etc/rc.d/init.d/network start",
117 ctr
[TR_PULLING_NETWORK_UP
]);
124 int oktoleave(char *errormessage
)
126 struct keyvalue
*kv
= initkeyvalues();
127 char temp
[STRING_SIZE
];
130 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
133 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
137 strcpy(temp
, "1"); findkey(kv
, "CONFIG_TYPE", temp
); configtype
= atol(temp
);
138 if (configtype
< 1 || configtype
> 4) configtype
= 1;
142 strcpy(temp
, ""); findkey(kv
, "GREEN_DEV", temp
);
145 strcpy(errormessage
, ctr
[TR_NO_GREEN_INTERFACE
]);
148 if (!(interfacecheck(kv
, "GREEN")))
150 strcpy(errormessage
, ctr
[TR_MISSING_GREEN_IP
]);
156 strcpy(temp
, ""); findkey(kv
, "RED_DEV", temp
);
159 strcpy(errormessage
, ctr
[TR_NO_RED_INTERFACE
]);
162 if (!(interfacecheck(kv
, "RED")))
164 strcpy(errormessage
, ctr
[TR_MISSING_RED_IP
]);
170 strcpy(temp
, ""); findkey(kv
, "ORANGE_DEV", temp
);
173 strcpy(errormessage
, ctr
[TR_NO_ORANGE_INTERFACE
]);
176 if (!(interfacecheck(kv
, "ORANGE")))
178 strcpy(errormessage
, ctr
[TR_MISSING_ORANGE_IP
]);
184 strcpy(temp
, ""); findkey(kv
, "BLUE_DEV", temp
);
187 strcpy(errormessage
, ctr
[TR_NO_BLUE_INTERFACE
]);
190 if (!(interfacecheck(kv
, "BLUE")))
192 strcpy(errormessage
, ctr
[TR_MISSING_BLUE_IP
]);
198 strcpy(temp
, ""); findkey(kv
, "DNS1", temp
);
201 strcpy(errormessage
, ctr
[TR_MISSING_DNS
]);
204 strcpy(temp
, ""); findkey(kv
, "DEFAULT_GATEWAY", temp
);
207 strcpy(errormessage
, ctr
[TR_MISSING_DEFAULT
]);
211 strcpy(errormessage
, "");
215 if (strlen(errormessage
))
222 /* Shows the main menu and a summary of the current settings. */
225 char *sections
[] = { ctr
[TR_NETWORK_CONFIGURATION_TYPE
],
226 ctr
[TR_DRIVERS_AND_CARD_ASSIGNMENTS
],
227 ctr
[TR_ADDRESS_SETTINGS
],
228 ctr
[TR_DNS_AND_GATEWAY_SETTINGS
], NULL
};
230 static int choice
= 0;
231 struct keyvalue
*kv
= initkeyvalues();
233 char temp
[STRING_SIZE
] = "1";
236 char networkrestart
[STRING_SIZE
] = "";
238 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
241 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
245 if (netaddresschange
)
246 strcpy(networkrestart
, ctr
[TR_RESTART_REQUIRED
]);
248 strcpy(temp
, ""); findkey(kv
, "CONFIG_TYPE", temp
);
251 if (x
< 0 || x
> 4) x
= 0;
252 /* Format heading bit. */
253 snprintf(message
, 1000, ctr
[TR_CURRENT_CONFIG
], configtypenames
[x
],
255 rc
= newtWinMenu(ctr
[TR_NETWORK_CONFIGURATION_MENU
], message
, 50, 5, 5, 6,
256 sections
, &choice
, ctr
[TR_OK
], ctr
[TR_DONE
], NULL
);
258 if (rc
== 0 || rc
== 1)
266 /* Here they choose general network config, number of nics etc. */
267 int configtypemenu(void)
269 struct keyvalue
*kv
= initkeyvalues();
270 char temp
[STRING_SIZE
] = "1";
275 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
278 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
282 found
= scan_network_cards();
284 findkey(kv
, "CONFIG_TYPE", temp
); choise
= atol(temp
);
289 sprintf(message
, ctr
[TR_NETWORK_CONFIGURATION_TYPE_LONG
], NAME
);
290 rc
= newtWinMenu(ctr
[TR_NETWORK_CONFIGURATION_TYPE
], message
, 50, 5, 5,
291 6, configtypenames
, &choise
, ctr
[TR_OK
], ctr
[TR_CANCEL
], NULL
);
292 if ( configtypecards
[choise
] > found
) {
293 sprintf(message
, ctr
[TR_NOT_ENOUGH_INTERFACES
] , configtypecards
[choise
], found
);
297 while ( 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 findkey(kv
, "CONFIG_TYPE", temp
);
338 configtype
= atol(temp
);
340 strcpy(message
, ctr
[TR_CONFIGURE_NETWORK_DRIVERS
]);
345 sprintf(temp
, "GREEN: %s\n", knics
[_GREEN_CARD_
].description
);
346 strcat(message
, temp
);
347 if (strlen(knics
[_GREEN_CARD_
].macaddr
) ) {
348 sprintf(temp
, "GREEN: (%s) %s green0\n", knics
[_GREEN_CARD_
].macaddr
, ctr
[TR_AS
]);
349 strcat(message
, temp
);
354 sprintf(temp
, "RED: %s\n", knics
[_RED_CARD_
].description
);
355 strcat(message
, temp
);
356 if (strlen(knics
[_RED_CARD_
].macaddr
) ) {
357 sprintf(temp
, "RED: (%s) %s red0\n", knics
[_RED_CARD_
].macaddr
, ctr
[TR_AS
]);
358 strcat(message
, temp
);
363 sprintf(temp
, "ORANGE: %s\n", knics
[_ORANGE_CARD_
].description
);
364 strcat(message
, temp
);
365 if ( strlen(knics
[_ORANGE_CARD_
].macaddr
) ) {
366 sprintf(temp
, "ORANGE: (%s) %s orange0\n", knics
[_ORANGE_CARD_
].macaddr
, ctr
[TR_AS
]);
367 strcat(message
, temp
);
372 sprintf(temp
, "BLUE: %s\n", knics
[_BLUE_CARD_
].description
);
373 strcat(message
, temp
);
374 if (strlen(knics
[_BLUE_CARD_
].macaddr
)) {
375 sprintf(temp
, "BLUE: (%s) %s blue0\n", knics
[_BLUE_CARD_
].macaddr
, ctr
[TR_AS
]);
376 strcat(message
, temp
);
381 for ( i
=0 ; i
<4; i
++)
382 if (strcmp(knics
[i
].macaddr
, ""))
385 if (neednics
= kcount
)
387 strcat(message
, ctr
[TR_DO_YOU_WISH_TO_CHANGE_THESE_SETTINGS
]);
388 rc
= newtWinChoice(ctr
[TR_DRIVERS_AND_CARD_ASSIGNMENTS
], ctr
[TR_OK
],
389 ctr
[TR_CANCEL
], message
);
390 if (rc
== 0 || rc
== 1)
402 int set_menu_entry_for(int *nr
, int *card
)
407 int changedrivers(void)
409 struct keyvalue
*kv
= initkeyvalues();
410 char temp
[STRING_SIZE
], message
[STRING_SIZE
];
412 int green
= 0, red
= 0, blue
= 0, orange
= 0;
413 char MenuInhalt
[10][180];
414 char *pMenuInhalt
[10];
415 int count
= 0, choise
= 0, rc
;
418 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
421 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
425 runcommandwithstatus("/etc/rc.d/init.d/network stop red blue orange",
426 ctr
[TR_PUSHING_NON_LOCAL_NETWORK_DOWN
]);
428 findkey(kv
, "CONFIG_TYPE", temp
); configtype
= atol(temp
);
430 { green
= 1; red
= 1; }
431 else if (configtype
== 2)
432 { green
= 1; red
= 1; orange
= 1; }
433 else if (configtype
== 3)
434 { green
= 1; red
= 1; blue
= 1; }
435 else if (configtype
== 4)
436 { green
= 1; red
=1; orange
=1; blue
= 1; }
441 strcpy(message
, ctr
[TR_INTERFACE_CHANGE
]);
444 strcpy(MenuInhalt
[count
], "GREEN");
445 pMenuInhalt
[count
] = MenuInhalt
[count
];
446 NicEntry
[_GREEN_CARD_
] = count
;
447 sprintf(temp
, "GREEN: %s\n", knics
[_GREEN_CARD_
].description
);
448 strcat(message
, temp
);
449 if ( strlen(knics
[_GREEN_CARD_
].macaddr
) ) {
450 sprintf(temp
, "GREEN: (%s) %s green0\n", knics
[_GREEN_CARD_
].macaddr
, ctr
[TR_AS
]);
451 strcat(message
, temp
);
457 strcpy(MenuInhalt
[count
], "RED");
458 pMenuInhalt
[count
] = MenuInhalt
[count
];
459 NicEntry
[_RED_CARD_
] = count
;
460 sprintf(temp
, "RED: %s\n", knics
[_RED_CARD_
].description
);
461 strcat(message
, temp
);
462 if ( strlen(knics
[_RED_CARD_
].macaddr
) ) {
463 sprintf(temp
, "RED: (%s) %s red0\n", knics
[_RED_CARD_
].macaddr
, ctr
[TR_AS
]);
464 strcat(message
, temp
);
470 strcpy(MenuInhalt
[count
], "ORANGE");
471 pMenuInhalt
[count
] = MenuInhalt
[count
];
472 NicEntry
[_ORANGE_CARD_
] = count
;
473 sprintf(temp
, "ORANGE: %s\n", knics
[_ORANGE_CARD_
].description
);
474 strcat(message
, temp
);
475 if ( strlen(knics
[_ORANGE_CARD_
].macaddr
) ) {
476 sprintf(temp
, "ORANGE: (%s) %s orange0\n", knics
[_ORANGE_CARD_
].macaddr
, ctr
[TR_AS
]);
477 strcat(message
, temp
);
483 strcpy(MenuInhalt
[count
], "BLUE");
484 pMenuInhalt
[count
] = MenuInhalt
[count
];
485 NicEntry
[_BLUE_CARD_
] = count
;
486 sprintf(temp
, "BLUE: %s\n", knics
[_BLUE_CARD_
].description
);
487 strcat(message
, temp
);
488 if ( strlen(knics
[_BLUE_CARD_
].macaddr
) ) {
489 sprintf(temp
, "BLUE: (%s) %s blue0\n", knics
[_BLUE_CARD_
].macaddr
, ctr
[TR_AS
]);
490 strcat(message
, temp
);
494 pMenuInhalt
[count
] = NULL
;
496 rc
= newtWinMenu( ctr
[TR_NETCARD_COLOR
], message
, 70, 5, 5, 6, pMenuInhalt
, &choise
, ctr
[TR_SELECT
], ctr
[TR_REMOVE
], ctr
[TR_DONE
], NULL
);
498 if ( rc
== 0 || rc
== 1) {
499 if ((green
) && ( choise
== NicEntry
[0])) nicmenu(_GREEN_CARD_
);
500 if ((red
) && ( choise
== NicEntry
[1])) nicmenu(_RED_CARD_
);
501 if ((orange
) && ( choise
== NicEntry
[2])) nicmenu(_ORANGE_CARD_
);
502 if ((blue
) && ( choise
== NicEntry
[3])) nicmenu(_BLUE_CARD_
);
503 netaddresschange
= 1;
504 } else if (rc
== 2) {
505 if ((green
) && ( choise
== NicEntry
[0])) ask_clear_card_entry(_GREEN_CARD_
);
506 if ((red
) && ( choise
== NicEntry
[1])) ask_clear_card_entry(_RED_CARD_
);
507 if ((orange
) && ( choise
== NicEntry
[2])) ask_clear_card_entry(_ORANGE_CARD_
);
508 if ((blue
) && ( choise
== NicEntry
[3])) ask_clear_card_entry(_BLUE_CARD_
);
509 netaddresschange
= 1;
518 // Let user change GREEN address.
519 int greenaddressmenu(void)
521 struct keyvalue
*kv
= initkeyvalues();
525 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
528 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
532 sprintf(message
, ctr
[TR_WARNING_LONG
], NAME
);
533 rc
= newtWinChoice(ctr
[TR_WARNING
], ctr
[TR_OK
], ctr
[TR_CANCEL
], message
);
535 if (rc
== 0 || rc
== 1)
537 if (changeaddress(kv
, "GREEN", 0, ""))
539 netaddresschange
= 1;
540 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
550 // They can change BLUE, ORANGE and GREEN too :)
551 int addressesmenu(void)
553 struct keyvalue
*kv
= initkeyvalues();
554 struct keyvalue
*mainkv
= initkeyvalues();
557 char *green
= "GREEN";
558 char *orange
= "ORANGE";
562 char greenaddress
[STRING_SIZE
];
563 char oldgreenaddress
[STRING_SIZE
];
564 char temp
[STRING_SIZE
];
565 char temp2
[STRING_SIZE
];
570 char hostname
[STRING_SIZE
];
572 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
575 freekeyvalues(mainkv
);
576 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
579 if (!(readkeyvalues(mainkv
, CONFIG_ROOT
"/main/settings")))
582 freekeyvalues(mainkv
);
583 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
587 strcpy(temp
, "0"); findkey(kv
, "CONFIG_TYPE", temp
);
588 configtype
= atol(temp
);
599 sections
[c
] = orange
;
613 rc
= newtWinMenu(ctr
[TR_ADDRESS_SETTINGS
],
614 ctr
[TR_SELECT_THE_INTERFACE_YOU_WISH_TO_RECONFIGURE
], 50, 5,
615 5, 6, sections
, &choice
, ctr
[TR_OK
], ctr
[TR_DONE
], NULL
);
617 if (rc
== 0 || rc
== 1)
619 if (strcmp(sections
[choice
], "GREEN") == 0)
621 findkey(kv
, "GREEN_ADDRESS", oldgreenaddress
);
622 sprintf(message
, ctr
[TR_WARNING_LONG
], NAME
);
623 rc
= newtWinChoice(ctr
[TR_WARNING
], ctr
[TR_OK
], ctr
[TR_CANCEL
],
625 if (rc
== 0 || rc
== 1)
627 if (changeaddress(kv
, "GREEN", 0, ""))
629 netaddresschange
= 1;
630 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
632 findkey(kv
, "GREEN_ADDRESS", greenaddress
);
633 snprintf(temp
, STRING_SIZE
-1, "option routers %s", oldgreenaddress
);
634 snprintf(temp2
, STRING_SIZE
-1, "option routers %s", greenaddress
);
635 replace (CONFIG_ROOT
"/dhcp/dhcpd.conf", temp
, temp2
);
636 chown (CONFIG_ROOT
"/dhcp/dhcpd.conf", 99, 99);
640 if (strcmp(sections
[choice
], "BLUE") == 0)
642 if (changeaddress(kv
, "BLUE", 0, ""))
643 netaddresschange
= 1;
645 if (strcmp(sections
[choice
], "ORANGE") == 0)
647 if (changeaddress(kv
, "ORANGE", 0, ""))
648 netaddresschange
= 1;
650 if (strcmp(sections
[choice
], "RED") == 0)
652 strcpy(hostname
, "");
653 findkey(mainkv
, "HOSTNAME", hostname
);
654 if (changeaddress(kv
, "RED", 1, hostname
))
655 netaddresschange
= 1;
662 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
664 freekeyvalues(mainkv
);
669 /* DNS and default gateway.... */
670 int dnsgatewaymenu(void)
672 struct keyvalue
*kv
= initkeyvalues();
674 char temp
[STRING_SIZE
] = "0";
675 struct newtWinEntry entries
[DNSGATEWAY_TOTAL
+1];
676 char *values
[DNSGATEWAY_TOTAL
]; /* pointers for the values. */
681 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
684 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
688 entries
[DNS1
].text
= ctr
[TR_PRIMARY_DNS
];
689 strcpy(temp
, ""); findkey(kv
, "DNS1", temp
);
690 values
[DNS1
] = strdup(temp
);
691 entries
[DNS1
].value
= &values
[DNS1
];
692 entries
[DNS1
].flags
= 0;
694 entries
[DNS2
].text
= ctr
[TR_SECONDARY_DNS
];
695 strcpy(temp
, ""); findkey(kv
, "DNS2", temp
);
696 values
[DNS2
] = strdup(temp
);
697 entries
[DNS2
].value
= &values
[DNS2
];
698 entries
[DNS2
].flags
= 0;
700 entries
[DEFAULT_GATEWAY
].text
= ctr
[TR_DEFAULT_GATEWAY
];
701 strcpy(temp
, ""); findkey(kv
, "DEFAULT_GATEWAY", temp
);
702 values
[DEFAULT_GATEWAY
] = strdup(temp
);
703 entries
[DEFAULT_GATEWAY
].value
= &values
[DEFAULT_GATEWAY
];
704 entries
[DEFAULT_GATEWAY
].flags
= 0;
706 entries
[DNSGATEWAY_TOTAL
].text
= NULL
;
707 entries
[DNSGATEWAY_TOTAL
].value
= NULL
;
708 entries
[DNSGATEWAY_TOTAL
].flags
= 0;
714 rc
= newtWinEntries(ctr
[TR_DNS_AND_GATEWAY_SETTINGS
],
715 ctr
[TR_DNS_AND_GATEWAY_SETTINGS_LONG
], 50, 5, 5, 18, entries
,
716 ctr
[TR_OK
], ctr
[TR_CANCEL
], NULL
);
717 if (rc
== 0 || rc
== 1)
719 strcpy(message
, ctr
[TR_INVALID_FIELDS
]);
720 if (strlen(values
[DNS1
]))
722 if (inet_addr(values
[DNS1
]) == INADDR_NONE
)
724 strcat(message
, ctr
[TR_PRIMARY_DNS_CR
]);
728 if (strlen(values
[DNS2
]))
730 if (inet_addr(values
[DNS2
]) == INADDR_NONE
)
732 strcat(message
, ctr
[TR_SECONDARY_DNS_CR
]);
736 if (strlen(values
[DEFAULT_GATEWAY
]))
738 if (inet_addr(values
[DEFAULT_GATEWAY
]) == INADDR_NONE
)
740 strcat(message
, ctr
[TR_DEFAULT_GATEWAY_CR
]);
744 if (!strlen(values
[DNS1
]) && strlen(values
[DNS2
]))
746 strcpy(message
, ctr
[TR_SECONDARY_WITHOUT_PRIMARY_DNS
]);
754 replacekeyvalue(kv
, "DNS1", values
[DNS1
]);
755 replacekeyvalue(kv
, "DNS2", values
[DNS2
]);
756 replacekeyvalue(kv
, "DEFAULT_GATEWAY", values
[DEFAULT_GATEWAY
]);
757 netaddresschange
= 1;
760 free(values
[DEFAULT_GATEWAY
]);
761 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");