]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/setup/networking.c
00dfc4b5a7d3a8aa02344d2966a60db6f7776007
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.
13 #define _(x) dgettext("setup", x)
19 #define DEFAULT_GATEWAY 2
20 #define DNSGATEWAY_TOTAL 3
28 #define HAS_RED (configtype == 1 || configtype == 2 || configtype == 3 || configtype == 4)
29 #define HAS_ORANGE (configtype == 2 || configtype == 4)
30 #define HAS_BLUE (configtype == 3 || configtype == 4)
31 #define RED_IS_NOT_ETH (configtype == 0)
33 extern struct nic nics
[];
34 extern struct knic knics
[];
36 char *configtypenames
[] = {
38 "GREEN + RED + ORANGE",
40 "GREEN + RED + ORANGE + BLUE",
42 int configtypecards
[] = {
44 3, // "GREEN + RED + ORANGE",
45 3, // "GREEN + RED + BLUE",
46 4 // "GREEN + RED + ORANGE + BLUE",
54 int configtypemenu(void);
56 int changedrivers(void);
57 int greenaddressmenu(void);
58 int addressesmenu(void);
59 int dnsgatewaymenu(void);
61 int handlenetworking(void)
69 found
= scan_network_cards();
96 if (oktoleave()) done
= 1;
106 /* Restart networking! */
107 if (netaddresschange
)
109 runcommandwithstatus("/etc/rc.d/init.d/network stop",
110 _("Networking"), _("Stopping network..."), NULL
);
114 runcommandwithstatus("/etc/rc.d/init.d/network start",
115 _("Networking"), _("Restarting network..."), NULL
);
116 runcommandwithstatus("/etc/rc.d/init.d/unbound restart",
117 _("Networking"), _("Restarting unbound..."), NULL
);
127 struct keyvalue
*kv
= initkeyvalues();
128 char temp
[STRING_SIZE
];
132 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
135 errorbox(_("Unable to open settings file"));
139 strcpy(temp
, "1"); findkey(kv
, "CONFIG_TYPE", temp
); configtype
= atol(temp
);
140 if (configtype
< 1 || configtype
> 4) configtype
= 1;
144 strcpy(temp
, ""); findkey(kv
, "GREEN_DEV", temp
);
147 errorbox(_("No GREEN interface assigned."));
151 if (!(interfacecheck(kv
, "GREEN")))
153 errorbox(_("Missing an IP address on GREEN."));
161 strcpy(temp
, ""); findkey(kv
, "RED_DEV", temp
);
164 rc
= newtWinChoice(_("Error"), _("OK"), _("Ignore"),
165 _("No RED interface assigned."));
166 if (rc
== 0 || rc
== 1)
172 if (!(interfacecheck(kv
, "RED")))
174 errorbox(_("Missing an IP address on RED."));
181 strcpy(temp
, ""); findkey(kv
, "ORANGE_DEV", temp
);
184 errorbox(_("No ORANGE interface assigned."));
188 if (!(interfacecheck(kv
, "ORANGE")))
190 errorbox(_("Missing an IP address on ORANGE."));
197 strcpy(temp
, ""); findkey(kv
, "BLUE_DEV", temp
);
200 errorbox(_("No BLUE interface assigned."));
204 if (!(interfacecheck(kv
, "BLUE")))
206 errorbox(_("Missing an IP address on BLUE."));
212 strcpy(temp
, ""); findkey(kv
, "RED_TYPE", temp
);
213 if ((configtype
== 0) || (strcmp(temp
, "STATIC") == 0))
215 strcpy(temp
, ""); findkey(kv
, "DNS1", temp
);
218 errorbox(_("Missing DNS."));
222 strcpy(temp
, ""); findkey(kv
, "DEFAULT_GATEWAY", temp
);
225 errorbox(_("Missing Default Gateway."));
234 /* Shows the main menu and a summary of the current settings. */
238 _("Network configuration type"),
239 _("Drivers and card assignments"),
240 _("Address settings"),
241 _("DNS and Gateway settings"),
245 static int choice
= 0;
246 struct keyvalue
*kv
= initkeyvalues();
248 char temp
[STRING_SIZE
] = "1";
251 char networkrestart
[STRING_SIZE
] = "";
253 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
256 errorbox(_("Unable to open settings file"));
260 if (netaddresschange
)
261 strcpy(networkrestart
, _("When configuration is complete, a network restart will be required."));
263 strcpy(temp
, ""); findkey(kv
, "CONFIG_TYPE", temp
);
266 if (x
< 0 || x
> 4) x
= 0;
267 /* Format heading bit. */
268 snprintf(message
, 1000, _("Current config: %s\n\n%s"), configtypenames
[x
], networkrestart
);
269 rc
= newtWinMenu(_("Network configuration menu"), message
, 50, 5, 5, 6,
270 sections
, &choice
, _("OK"), _("Done"), NULL
);
272 if (rc
== 0 || rc
== 1)
280 /* Here they choose general network config, number of nics etc. */
281 int configtypemenu(void)
283 struct keyvalue
*kv
= initkeyvalues();
284 char temp
[STRING_SIZE
] = "1";
289 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
292 errorbox(_("Unable to open settings file"));
296 found
= scan_network_cards();
298 findkey(kv
, "CONFIG_TYPE", temp
); choise
= atol(temp
);
301 sprintf(message
, _("Select the network configuration for %s. "
302 "The following configuration types list those interfaces which have ethernet attached. "
303 "If you change this setting, a network restart will be required, and you will have to "
304 "reconfigure the network driver assignments."), NAME
);
305 rc
= newtWinMenu(_("Network configuration type"), message
, 50, 5, 5,
306 6, configtypenames
, &choise
, _("OK"), _("Cancel"), NULL
);
307 if ( configtypecards
[choise
] > found
) {
308 sprintf(message
, _("Not enough netcards for your choice.\n\nNeeded: %d - Available: %d\n"),
309 configtypecards
[choise
], found
);
313 if (rc
== 0 || rc
== 1)
316 sprintf(temp
, "%d", choise
);
317 replacekeyvalue(kv
, "CONFIG_TYPE", temp
);
318 configtype
= atol(temp
);
320 clear_card_entry(_RED_CARD_
);
322 clear_card_entry(_ORANGE_CARD_
);
324 clear_card_entry(_BLUE_CARD_
);
326 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
327 netaddresschange
= 1;
334 /* Driver menu. Choose drivers.. */
337 struct keyvalue
*kv
= initkeyvalues();
338 char message
[STRING_SIZE
];
339 char temp
[STRING_SIZE
] = "1";
342 int i
, rc
, kcount
= 0, neednics
;
344 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
347 errorbox(_("Unable to open settings file"));
351 if (findkey(kv
, "CONFIG_TYPE", temp
))
352 configtype
= atol(temp
);
354 fprintf(flog
,"setting CONFIG_TYPE = %s\n",temp
);
355 configtype
= atol(temp
);
356 replacekeyvalue(kv
, "CONFIG_TYPE", temp
);
357 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
360 strcpy(message
, _("Configure network drivers, and which interface each card is assigned to. "
361 "The current configuration is as follows:\n\n"));
366 sprintf(temp
, "%-6s: %s\n", "GREEN", knics
[_GREEN_CARD_
].description
);
367 strcat(message
, temp
);
368 if (strlen(knics
[_GREEN_CARD_
].macaddr
) ) {
369 sprintf(temp
, "%-6s: (%s)\n", "GREEN", knics
[_GREEN_CARD_
].macaddr
);
370 strcat(message
, temp
);
375 sprintf(temp
, "%-6s: %s\n", "RED", knics
[_RED_CARD_
].description
);
376 strcat(message
, temp
);
377 if (strlen(knics
[_RED_CARD_
].macaddr
) ) {
378 sprintf(temp
, "%-6s: (%s)\n", "RED", knics
[_RED_CARD_
].macaddr
);
379 strcat(message
, temp
);
384 sprintf(temp
, "%-6s: %s\n", "ORANGE", knics
[_ORANGE_CARD_
].description
);
385 strcat(message
, temp
);
386 if ( strlen(knics
[_ORANGE_CARD_
].macaddr
) ) {
387 sprintf(temp
, "%-6s: (%s)\n", "ORANGE", knics
[_ORANGE_CARD_
].macaddr
);
388 strcat(message
, temp
);
393 sprintf(temp
, "%-6s: %s\n", "BLUE", knics
[_BLUE_CARD_
].description
);
394 strcat(message
, temp
);
395 if (strlen(knics
[_BLUE_CARD_
].macaddr
)) {
396 sprintf(temp
, "%-6s: (%s)\n", "BLUE", knics
[_BLUE_CARD_
].macaddr
);
397 strcat(message
, temp
);
402 for ( i
=0 ; i
<4; i
++)
403 if (strcmp(knics
[i
].macaddr
, ""))
406 if (neednics
= kcount
)
408 strcat(message
, "\n");
409 strcat(message
, _("Do you wish to change these settings?"));
410 rc
= newtWinChoice(_("Drivers and card assignments"), _("OK"),
411 _("Cancel"), message
);
412 if (rc
== 0 || rc
== 1)
424 int set_menu_entry_for(int *nr
, int *card
)
429 int changedrivers(void)
431 struct keyvalue
*kv
= initkeyvalues();
432 char temp
[STRING_SIZE
], message
[STRING_SIZE
];
434 int green
= 0, red
= 0, blue
= 0, orange
= 0;
435 char MenuInhalt
[10][180];
436 char *pMenuInhalt
[10];
437 int count
= 0, choise
= 0, rc
;
440 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
443 errorbox(_("Unable to open settings file"));
447 runcommandwithstatus("/etc/rc.d/init.d/network stop red blue orange",
448 _("Networking"), _("Restarting non-local network..."), NULL
);
450 findkey(kv
, "CONFIG_TYPE", temp
); configtype
= atol(temp
);
452 { green
= 1; red
= 1; }
453 else if (configtype
== 2)
454 { green
= 1; red
= 1; orange
= 1; }
455 else if (configtype
== 3)
456 { green
= 1; red
= 1; blue
= 1; }
457 else if (configtype
== 4)
458 { green
= 1; red
=1; orange
=1; blue
= 1; }
459 else if (configtype
== "")
460 { green
= 1; red
= 1; }
465 strcpy(message
, _("Please choose the interface you wish to change.\n\n"));
468 strcpy(MenuInhalt
[count
], "GREEN");
469 pMenuInhalt
[count
] = MenuInhalt
[count
];
470 NicEntry
[_GREEN_CARD_
] = count
;
471 sprintf(temp
, "%-6s: %s\n", "GREEN", knics
[_GREEN_CARD_
].description
);
472 strcat(message
, temp
);
473 if ( strlen(knics
[_GREEN_CARD_
].macaddr
) ) {
474 sprintf(temp
, "%-6s: (%s)\n", "GREEN", knics
[_GREEN_CARD_
].macaddr
);
475 strcat(message
, temp
);
481 strcpy(MenuInhalt
[count
], "RED");
482 pMenuInhalt
[count
] = MenuInhalt
[count
];
483 NicEntry
[_RED_CARD_
] = count
;
484 sprintf(temp
, "%-6s: %s\n", "RED", knics
[_RED_CARD_
].description
);
485 strcat(message
, temp
);
486 if ( strlen(knics
[_RED_CARD_
].macaddr
) ) {
487 sprintf(temp
, "%-6s: (%s)\n", "RED", knics
[_RED_CARD_
].macaddr
);
488 strcat(message
, temp
);
494 strcpy(MenuInhalt
[count
], "ORANGE");
495 pMenuInhalt
[count
] = MenuInhalt
[count
];
496 NicEntry
[_ORANGE_CARD_
] = count
;
497 sprintf(temp
, "%-6s: %s\n", "ORANGE", knics
[_ORANGE_CARD_
].description
);
498 strcat(message
, temp
);
499 if ( strlen(knics
[_ORANGE_CARD_
].macaddr
) ) {
500 sprintf(temp
, "%-6s: (%s)\n", "ORANGE", knics
[_ORANGE_CARD_
].macaddr
);
501 strcat(message
, temp
);
507 strcpy(MenuInhalt
[count
], "BLUE");
508 pMenuInhalt
[count
] = MenuInhalt
[count
];
509 NicEntry
[_BLUE_CARD_
] = count
;
510 sprintf(temp
, "%-6s: %s\n", "BLUE", knics
[_BLUE_CARD_
].description
);
511 strcat(message
, temp
);
512 if ( strlen(knics
[_BLUE_CARD_
].macaddr
) ) {
513 sprintf(temp
, "%-6s: (%s)\n", "BLUE", knics
[_BLUE_CARD_
].macaddr
);
514 strcat(message
, temp
);
518 pMenuInhalt
[count
] = NULL
;
520 rc
= newtWinMenu(_("Assigned Cards"), message
, 70, 5, 5, 6, pMenuInhalt
,
521 &choise
, _("Select"), _("Remove"), _("Done"), NULL
);
523 if ( rc
== 0 || rc
== 1) {
524 if ((green
) && ( choise
== NicEntry
[0])) nicmenu(_GREEN_CARD_
);
525 if ((red
) && ( choise
== NicEntry
[1])) nicmenu(_RED_CARD_
);
526 if ((orange
) && ( choise
== NicEntry
[2])) nicmenu(_ORANGE_CARD_
);
527 if ((blue
) && ( choise
== NicEntry
[3])) nicmenu(_BLUE_CARD_
);
528 netaddresschange
= 1;
529 } else if (rc
== 2) {
530 if ((green
) && ( choise
== NicEntry
[0])) ask_clear_card_entry(_GREEN_CARD_
);
531 if ((red
) && ( choise
== NicEntry
[1])) ask_clear_card_entry(_RED_CARD_
);
532 if ((orange
) && ( choise
== NicEntry
[2])) ask_clear_card_entry(_ORANGE_CARD_
);
533 if ((blue
) && ( choise
== NicEntry
[3])) ask_clear_card_entry(_BLUE_CARD_
);
534 netaddresschange
= 1;
543 // Let user change GREEN address.
544 int greenaddressmenu(void)
546 struct keyvalue
*kv
= initkeyvalues();
550 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
553 errorbox(_("Unable to open settings file"));
557 sprintf(message
, _("If you change this IP address, and you are logged in remotely, "
558 "your connection to the %s machine will be broken, and you will have to reconnect "
559 "on the new IP. This is a risky operation, and should only be attempted if you "
560 "have physical access to the machine, should something go wrong."), NAME
);
561 rc
= newtWinChoice(_("Warning"), _("OK"), _("Cancel"), message
);
563 if (rc
== 0 || rc
== 1)
565 if (changeaddress(kv
, "GREEN", 0, ""))
567 netaddresschange
= 1;
568 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
578 // They can change BLUE, ORANGE and GREEN too :)
579 int addressesmenu(void)
581 struct keyvalue
*kv
= initkeyvalues();
582 struct keyvalue
*mainkv
= initkeyvalues();
585 char *green
= "GREEN";
586 char *orange
= "ORANGE";
590 char greenaddress
[STRING_SIZE
];
591 char oldgreenaddress
[STRING_SIZE
];
592 char temp
[STRING_SIZE
];
593 char temp2
[STRING_SIZE
];
598 char hostname
[STRING_SIZE
];
600 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
603 freekeyvalues(mainkv
);
604 errorbox(_("Unable to open settings file"));
607 if (!(readkeyvalues(mainkv
, CONFIG_ROOT
"/main/settings")))
610 freekeyvalues(mainkv
);
611 errorbox(_("Unable to open settings file"));
615 strcpy(temp
, "0"); findkey(kv
, "CONFIG_TYPE", temp
);
616 configtype
= atol(temp
);
627 sections
[c
] = orange
;
641 rc
= newtWinMenu(_("Address settings"),
642 _("Select the interface you wish to reconfigure."), 50, 5,
643 5, 6, sections
, &choice
, _("OK"), _("Done"), NULL
);
645 if (rc
== 0 || rc
== 1)
647 if (strcmp(sections
[choice
], "GREEN") == 0)
649 findkey(kv
, "GREEN_ADDRESS", oldgreenaddress
);
650 sprintf(message
, _("If you change this IP address, and you are logged in remotely, "
651 "your connection to the %s machine will be broken, and you will have to reconnect "
652 "on the new IP. This is a risky operation, and should only be attempted if you "
653 "have physical access to the machine, should something go wrong."), NAME
);
654 rc
= newtWinChoice(_("Warning"), _("OK"), _("Cancel"), message
);
655 if (rc
== 0 || rc
== 1)
657 if (changeaddress(kv
, "GREEN", 0, ""))
659 netaddresschange
= 1;
660 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
662 findkey(kv
, "GREEN_ADDRESS", greenaddress
);
663 snprintf(temp
, STRING_SIZE
-1, "option routers %s", oldgreenaddress
);
664 snprintf(temp2
, STRING_SIZE
-1, "option routers %s", greenaddress
);
665 replace (CONFIG_ROOT
"/dhcp/dhcpd.conf", temp
, temp2
);
666 chown (CONFIG_ROOT
"/dhcp/dhcpd.conf", 99, 99);
670 if (strcmp(sections
[choice
], "BLUE") == 0)
672 if (changeaddress(kv
, "BLUE", 0, ""))
673 netaddresschange
= 1;
675 if (strcmp(sections
[choice
], "ORANGE") == 0)
677 if (changeaddress(kv
, "ORANGE", 0, ""))
678 netaddresschange
= 1;
680 if (strcmp(sections
[choice
], "RED") == 0)
682 strcpy(hostname
, "");
683 findkey(mainkv
, "HOSTNAME", hostname
);
684 if (changeaddress(kv
, "RED", 1, hostname
))
685 netaddresschange
= 1;
692 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
694 freekeyvalues(mainkv
);
699 /* DNS and default gateway.... */
700 int dnsgatewaymenu(void)
702 struct keyvalue
*kv
= initkeyvalues();
704 char temp
[STRING_SIZE
] = "0";
705 struct newtWinEntry entries
[DNSGATEWAY_TOTAL
+1];
706 char *values
[DNSGATEWAY_TOTAL
]; /* pointers for the values. */
711 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
714 errorbox(_("Unable to open settings file"));
718 entries
[DNS1
].text
= _("Primary DNS:");
719 strcpy(temp
, ""); findkey(kv
, "DNS1", temp
);
720 values
[DNS1
] = strdup(temp
);
721 entries
[DNS1
].value
= &values
[DNS1
];
722 entries
[DNS1
].flags
= 0;
724 entries
[DNS2
].text
= _("Secondary DNS:");
725 strcpy(temp
, ""); findkey(kv
, "DNS2", temp
);
726 values
[DNS2
] = strdup(temp
);
727 entries
[DNS2
].value
= &values
[DNS2
];
728 entries
[DNS2
].flags
= 0;
730 entries
[DEFAULT_GATEWAY
].text
= _("Default gateway:");
731 strcpy(temp
, ""); findkey(kv
, "DEFAULT_GATEWAY", temp
);
732 values
[DEFAULT_GATEWAY
] = strdup(temp
);
733 entries
[DEFAULT_GATEWAY
].value
= &values
[DEFAULT_GATEWAY
];
734 entries
[DEFAULT_GATEWAY
].flags
= 0;
736 entries
[DNSGATEWAY_TOTAL
].text
= NULL
;
737 entries
[DNSGATEWAY_TOTAL
].value
= NULL
;
738 entries
[DNSGATEWAY_TOTAL
].flags
= 0;
744 rc
= newtWinEntries(_("DNS and Gateway settings"),
745 _("Enter the DNS and gateway information. "
746 "These settings are used only with Static IP (and DHCP if DNS set) on the RED interface."),
747 50, 5, 5, 18, entries
, _("OK"), _("Cancel"), NULL
);
748 if (rc
== 0 || rc
== 1)
750 strcpy(message
, _("The following fields are invalid:"));
751 strcpy(message
, "\n\n");
752 if (strlen(values
[DNS1
]))
754 if (inet_addr(values
[DNS1
]) == INADDR_NONE
)
756 strcat(message
, _("Primary DNS"));
757 strcat(message
, "\n");
761 if (strlen(values
[DNS2
]))
763 if (inet_addr(values
[DNS2
]) == INADDR_NONE
)
765 strcat(message
, _("Secondary DNS"));
766 strcat(message
, "\n");
770 if (strlen(values
[DEFAULT_GATEWAY
]))
772 if (inet_addr(values
[DEFAULT_GATEWAY
]) == INADDR_NONE
)
774 strcat(message
, _("Default gateway"));
775 strcat(message
, "\n");
779 if (!strlen(values
[DNS1
]) && strlen(values
[DNS2
]))
781 strcpy(message
, _("Secondary DNS specified without a Primary DNS"));
782 strcat(message
, "\n");
790 replacekeyvalue(kv
, "DNS1", values
[DNS1
]);
791 replacekeyvalue(kv
, "DNS2", values
[DNS2
]);
792 replacekeyvalue(kv
, "DEFAULT_GATEWAY", values
[DEFAULT_GATEWAY
]);
793 netaddresschange
= 1;
796 free(values
[DEFAULT_GATEWAY
]);
797 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");