]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/setup/networking.c
d6cd30cff0219b7b92958174ce286e2afeaf004d
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 rc
= newtWinChoice(_("Error"), _("OK"), _("Ignore"),
148 _("No GREEN interface assigned."));
149 if (rc
== 0 || rc
== 1)
155 if (!(interfacecheck(kv
, "GREEN")))
157 errorbox(_("Missing an IP address on GREEN."));
165 strcpy(temp
, ""); findkey(kv
, "RED_DEV", temp
);
168 rc
= newtWinChoice(_("Error"), _("OK"), _("Ignore"),
169 _("No RED interface assigned."));
170 if (rc
== 0 || rc
== 1)
176 if (!(interfacecheck(kv
, "RED")))
178 errorbox(_("Missing an IP address on RED."));
185 strcpy(temp
, ""); findkey(kv
, "ORANGE_DEV", temp
);
188 rc
= newtWinChoice(_("Error"), _("OK"), _("Ignore"),
189 _("No ORANGE interface assigned."));
190 if (rc
== 0 || rc
== 1)
196 if (!(interfacecheck(kv
, "ORANGE")))
198 errorbox(_("Missing an IP address on ORANGE."));
205 strcpy(temp
, ""); findkey(kv
, "BLUE_DEV", temp
);
208 rc
= newtWinChoice(_("Error"), _("OK"), _("Ignore"),
209 _("No BLUE interface assigned."));
210 if (rc
== 0 || rc
== 1)
216 if (!(interfacecheck(kv
, "BLUE")))
218 errorbox(_("Missing an IP address on BLUE."));
224 strcpy(temp
, ""); findkey(kv
, "RED_TYPE", temp
);
225 if ((configtype
== 0) || (strcmp(temp
, "STATIC") == 0))
227 strcpy(temp
, ""); findkey(kv
, "DNS1", temp
);
230 errorbox(_("Missing DNS."));
234 strcpy(temp
, ""); findkey(kv
, "DEFAULT_GATEWAY", temp
);
237 errorbox(_("Missing Default Gateway."));
246 /* Shows the main menu and a summary of the current settings. */
250 _("Network configuration type"),
251 _("Drivers and card assignments"),
252 _("Address settings"),
253 _("DNS and Gateway settings"),
257 static int choice
= 0;
258 struct keyvalue
*kv
= initkeyvalues();
260 char temp
[STRING_SIZE
] = "1";
263 char networkrestart
[STRING_SIZE
] = "";
265 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
268 errorbox(_("Unable to open settings file"));
272 if (netaddresschange
)
273 strcpy(networkrestart
, _("When configuration is complete, a network restart will be required."));
275 strcpy(temp
, ""); findkey(kv
, "CONFIG_TYPE", temp
);
278 if (x
< 0 || x
> 4) x
= 0;
279 /* Format heading bit. */
280 snprintf(message
, 1000, _("Current config: %s\n\n%s"), configtypenames
[x
], networkrestart
);
281 rc
= newtWinMenu(_("Network configuration menu"), message
, 50, 5, 5, 6,
282 sections
, &choice
, _("OK"), _("Done"), NULL
);
284 if (rc
== 0 || rc
== 1)
292 /* Here they choose general network config, number of nics etc. */
293 int configtypemenu(void)
295 struct keyvalue
*kv
= initkeyvalues();
296 char temp
[STRING_SIZE
] = "1";
301 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
304 errorbox(_("Unable to open settings file"));
308 found
= scan_network_cards();
310 findkey(kv
, "CONFIG_TYPE", temp
); choise
= atol(temp
);
313 sprintf(message
, _("Select the network configuration for %s. "
314 "The following configuration types list those interfaces which have ethernet attached. "
315 "If you change this setting, a network restart will be required, and you will have to "
316 "reconfigure the network driver assignments."), NAME
);
317 rc
= newtWinMenu(_("Network configuration type"), message
, 50, 5, 5,
318 6, configtypenames
, &choise
, _("OK"), _("Cancel"), NULL
);
319 if ( configtypecards
[choise
] > found
) {
320 sprintf(message
, _("Not enough netcards for your choice.\n\nNeeded: %d - Available: %d\n"),
321 configtypecards
[choise
], found
);
325 if (rc
== 0 || rc
== 1)
328 sprintf(temp
, "%d", choise
);
329 replacekeyvalue(kv
, "CONFIG_TYPE", temp
);
330 configtype
= atol(temp
);
332 clear_card_entry(_RED_CARD_
);
334 clear_card_entry(_ORANGE_CARD_
);
336 clear_card_entry(_BLUE_CARD_
);
338 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
339 netaddresschange
= 1;
346 /* Driver menu. Choose drivers.. */
349 struct keyvalue
*kv
= initkeyvalues();
350 char message
[STRING_SIZE
];
351 char temp
[STRING_SIZE
] = "1";
354 int i
, rc
, kcount
= 0, neednics
;
356 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
359 errorbox(_("Unable to open settings file"));
363 if (findkey(kv
, "CONFIG_TYPE", temp
))
364 configtype
= atol(temp
);
366 fprintf(flog
,"setting CONFIG_TYPE = %s\n",temp
);
367 configtype
= atol(temp
);
368 replacekeyvalue(kv
, "CONFIG_TYPE", temp
);
369 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
372 strcpy(message
, _("Configure network drivers, and which interface each card is assigned to. "
373 "The current configuration is as follows:\n\n"));
378 sprintf(temp
, "%-6s: %s\n", "GREEN", knics
[_GREEN_CARD_
].description
);
379 strcat(message
, temp
);
380 if (strlen(knics
[_GREEN_CARD_
].macaddr
) ) {
381 sprintf(temp
, "%-6s: (%s)\n", "GREEN", knics
[_GREEN_CARD_
].macaddr
);
382 strcat(message
, temp
);
387 sprintf(temp
, "%-6s: %s\n", "RED", knics
[_RED_CARD_
].description
);
388 strcat(message
, temp
);
389 if (strlen(knics
[_RED_CARD_
].macaddr
) ) {
390 sprintf(temp
, "%-6s: (%s)\n", "RED", knics
[_RED_CARD_
].macaddr
);
391 strcat(message
, temp
);
396 sprintf(temp
, "%-6s: %s\n", "ORANGE", knics
[_ORANGE_CARD_
].description
);
397 strcat(message
, temp
);
398 if ( strlen(knics
[_ORANGE_CARD_
].macaddr
) ) {
399 sprintf(temp
, "%-6s: (%s)\n", "ORANGE", knics
[_ORANGE_CARD_
].macaddr
);
400 strcat(message
, temp
);
405 sprintf(temp
, "%-6s: %s\n", "BLUE", knics
[_BLUE_CARD_
].description
);
406 strcat(message
, temp
);
407 if (strlen(knics
[_BLUE_CARD_
].macaddr
)) {
408 sprintf(temp
, "%-6s: (%s)\n", "BLUE", knics
[_BLUE_CARD_
].macaddr
);
409 strcat(message
, temp
);
414 for ( i
=0 ; i
<4; i
++)
415 if (strcmp(knics
[i
].macaddr
, ""))
418 if (neednics
= kcount
)
420 strcat(message
, "\n");
421 strcat(message
, _("Do you wish to change these settings?"));
422 rc
= newtWinChoice(_("Drivers and card assignments"), _("OK"),
423 _("Cancel"), message
);
424 if (rc
== 0 || rc
== 1)
436 int set_menu_entry_for(int *nr
, int *card
)
441 int changedrivers(void)
443 struct keyvalue
*kv
= initkeyvalues();
444 char temp
[STRING_SIZE
], message
[STRING_SIZE
];
446 int green
= 0, red
= 0, blue
= 0, orange
= 0;
447 char MenuInhalt
[10][180];
448 char *pMenuInhalt
[10];
449 int count
= 0, choise
= 0, rc
;
452 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
455 errorbox(_("Unable to open settings file"));
459 runcommandwithstatus("/etc/rc.d/init.d/network stop red blue orange",
460 _("Networking"), _("Restarting non-local network..."), NULL
);
462 findkey(kv
, "CONFIG_TYPE", temp
); configtype
= atol(temp
);
464 { green
= 1; red
= 1; }
465 else if (configtype
== 2)
466 { green
= 1; red
= 1; orange
= 1; }
467 else if (configtype
== 3)
468 { green
= 1; red
= 1; blue
= 1; }
469 else if (configtype
== 4)
470 { green
= 1; red
=1; orange
=1; blue
= 1; }
471 else if (configtype
== "")
472 { green
= 1; red
= 1; }
477 strcpy(message
, _("Please choose the interface you wish to change.\n\n"));
480 strcpy(MenuInhalt
[count
], "GREEN");
481 pMenuInhalt
[count
] = MenuInhalt
[count
];
482 NicEntry
[_GREEN_CARD_
] = count
;
483 sprintf(temp
, "%-6s: %s\n", "GREEN", knics
[_GREEN_CARD_
].description
);
484 strcat(message
, temp
);
485 if ( strlen(knics
[_GREEN_CARD_
].macaddr
) ) {
486 sprintf(temp
, "%-6s: (%s)\n", "GREEN", knics
[_GREEN_CARD_
].macaddr
);
487 strcat(message
, temp
);
493 strcpy(MenuInhalt
[count
], "RED");
494 pMenuInhalt
[count
] = MenuInhalt
[count
];
495 NicEntry
[_RED_CARD_
] = count
;
496 sprintf(temp
, "%-6s: %s\n", "RED", knics
[_RED_CARD_
].description
);
497 strcat(message
, temp
);
498 if ( strlen(knics
[_RED_CARD_
].macaddr
) ) {
499 sprintf(temp
, "%-6s: (%s)\n", "RED", knics
[_RED_CARD_
].macaddr
);
500 strcat(message
, temp
);
506 strcpy(MenuInhalt
[count
], "ORANGE");
507 pMenuInhalt
[count
] = MenuInhalt
[count
];
508 NicEntry
[_ORANGE_CARD_
] = count
;
509 sprintf(temp
, "%-6s: %s\n", "ORANGE", knics
[_ORANGE_CARD_
].description
);
510 strcat(message
, temp
);
511 if ( strlen(knics
[_ORANGE_CARD_
].macaddr
) ) {
512 sprintf(temp
, "%-6s: (%s)\n", "ORANGE", knics
[_ORANGE_CARD_
].macaddr
);
513 strcat(message
, temp
);
519 strcpy(MenuInhalt
[count
], "BLUE");
520 pMenuInhalt
[count
] = MenuInhalt
[count
];
521 NicEntry
[_BLUE_CARD_
] = count
;
522 sprintf(temp
, "%-6s: %s\n", "BLUE", knics
[_BLUE_CARD_
].description
);
523 strcat(message
, temp
);
524 if ( strlen(knics
[_BLUE_CARD_
].macaddr
) ) {
525 sprintf(temp
, "%-6s: (%s)\n", "BLUE", knics
[_BLUE_CARD_
].macaddr
);
526 strcat(message
, temp
);
530 pMenuInhalt
[count
] = NULL
;
532 rc
= newtWinMenu(_("Assigned Cards"), message
, 70, 5, 5, 6, pMenuInhalt
,
533 &choise
, _("Select"), _("Remove"), _("Done"), NULL
);
535 if ( rc
== 0 || rc
== 1) {
536 if ((green
) && ( choise
== NicEntry
[0])) nicmenu(_GREEN_CARD_
);
537 if ((red
) && ( choise
== NicEntry
[1])) nicmenu(_RED_CARD_
);
538 if ((orange
) && ( choise
== NicEntry
[2])) nicmenu(_ORANGE_CARD_
);
539 if ((blue
) && ( choise
== NicEntry
[3])) nicmenu(_BLUE_CARD_
);
540 netaddresschange
= 1;
541 } else if (rc
== 2) {
542 if ((green
) && ( choise
== NicEntry
[0])) ask_clear_card_entry(_GREEN_CARD_
);
543 if ((red
) && ( choise
== NicEntry
[1])) ask_clear_card_entry(_RED_CARD_
);
544 if ((orange
) && ( choise
== NicEntry
[2])) ask_clear_card_entry(_ORANGE_CARD_
);
545 if ((blue
) && ( choise
== NicEntry
[3])) ask_clear_card_entry(_BLUE_CARD_
);
546 netaddresschange
= 1;
555 // Let user change GREEN address.
556 int greenaddressmenu(void)
558 struct keyvalue
*kv
= initkeyvalues();
562 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
565 errorbox(_("Unable to open settings file"));
569 sprintf(message
, _("If you change this IP address, and you are logged in remotely, "
570 "your connection to the %s machine will be broken, and you will have to reconnect "
571 "on the new IP. This is a risky operation, and should only be attempted if you "
572 "have physical access to the machine, should something go wrong."), NAME
);
573 rc
= newtWinChoice(_("Warning"), _("OK"), _("Cancel"), message
);
575 if (rc
== 0 || rc
== 1)
577 if (changeaddress(kv
, "GREEN", 0, ""))
579 netaddresschange
= 1;
580 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
590 // They can change BLUE, ORANGE and GREEN too :)
591 int addressesmenu(void)
593 struct keyvalue
*kv
= initkeyvalues();
594 struct keyvalue
*mainkv
= initkeyvalues();
597 char *green
= "GREEN";
598 char *orange
= "ORANGE";
602 char greenaddress
[STRING_SIZE
];
603 char oldgreenaddress
[STRING_SIZE
];
604 char temp
[STRING_SIZE
];
605 char temp2
[STRING_SIZE
];
610 char hostname
[STRING_SIZE
];
612 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
615 freekeyvalues(mainkv
);
616 errorbox(_("Unable to open settings file"));
619 if (!(readkeyvalues(mainkv
, CONFIG_ROOT
"/main/settings")))
622 freekeyvalues(mainkv
);
623 errorbox(_("Unable to open settings file"));
627 strcpy(temp
, "0"); findkey(kv
, "CONFIG_TYPE", temp
);
628 configtype
= atol(temp
);
639 sections
[c
] = orange
;
653 rc
= newtWinMenu(_("Address settings"),
654 _("Select the interface you wish to reconfigure."), 50, 5,
655 5, 6, sections
, &choice
, _("OK"), _("Done"), NULL
);
657 if (rc
== 0 || rc
== 1)
659 if (strcmp(sections
[choice
], "GREEN") == 0)
661 findkey(kv
, "GREEN_ADDRESS", oldgreenaddress
);
662 sprintf(message
, _("If you change this IP address, and you are logged in remotely, "
663 "your connection to the %s machine will be broken, and you will have to reconnect "
664 "on the new IP. This is a risky operation, and should only be attempted if you "
665 "have physical access to the machine, should something go wrong."), NAME
);
666 rc
= newtWinChoice(_("Warning"), _("OK"), _("Cancel"), message
);
667 if (rc
== 0 || rc
== 1)
669 if (changeaddress(kv
, "GREEN", 0, ""))
671 netaddresschange
= 1;
672 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
674 findkey(kv
, "GREEN_ADDRESS", greenaddress
);
675 snprintf(temp
, STRING_SIZE
-1, "option routers %s", oldgreenaddress
);
676 snprintf(temp2
, STRING_SIZE
-1, "option routers %s", greenaddress
);
677 replace (CONFIG_ROOT
"/dhcp/dhcpd.conf", temp
, temp2
);
678 chown (CONFIG_ROOT
"/dhcp/dhcpd.conf", 99, 99);
682 if (strcmp(sections
[choice
], "BLUE") == 0)
684 if (changeaddress(kv
, "BLUE", 0, ""))
685 netaddresschange
= 1;
687 if (strcmp(sections
[choice
], "ORANGE") == 0)
689 if (changeaddress(kv
, "ORANGE", 0, ""))
690 netaddresschange
= 1;
692 if (strcmp(sections
[choice
], "RED") == 0)
694 strcpy(hostname
, "");
695 findkey(mainkv
, "HOSTNAME", hostname
);
696 if (changeaddress(kv
, "RED", 1, hostname
))
697 netaddresschange
= 1;
704 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
706 freekeyvalues(mainkv
);
711 /* DNS and default gateway.... */
712 int dnsgatewaymenu(void)
714 struct keyvalue
*kv
= initkeyvalues();
716 char temp
[STRING_SIZE
] = "0";
717 struct newtWinEntry entries
[DNSGATEWAY_TOTAL
+1];
718 char *values
[DNSGATEWAY_TOTAL
]; /* pointers for the values. */
723 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
726 errorbox(_("Unable to open settings file"));
730 entries
[DNS1
].text
= _("Primary DNS:");
731 strcpy(temp
, ""); findkey(kv
, "DNS1", temp
);
732 values
[DNS1
] = strdup(temp
);
733 entries
[DNS1
].value
= &values
[DNS1
];
734 entries
[DNS1
].flags
= 0;
736 entries
[DNS2
].text
= _("Secondary DNS:");
737 strcpy(temp
, ""); findkey(kv
, "DNS2", temp
);
738 values
[DNS2
] = strdup(temp
);
739 entries
[DNS2
].value
= &values
[DNS2
];
740 entries
[DNS2
].flags
= 0;
742 entries
[DEFAULT_GATEWAY
].text
= _("Default gateway:");
743 strcpy(temp
, ""); findkey(kv
, "DEFAULT_GATEWAY", temp
);
744 values
[DEFAULT_GATEWAY
] = strdup(temp
);
745 entries
[DEFAULT_GATEWAY
].value
= &values
[DEFAULT_GATEWAY
];
746 entries
[DEFAULT_GATEWAY
].flags
= 0;
748 entries
[DNSGATEWAY_TOTAL
].text
= NULL
;
749 entries
[DNSGATEWAY_TOTAL
].value
= NULL
;
750 entries
[DNSGATEWAY_TOTAL
].flags
= 0;
756 rc
= newtWinEntries(_("DNS and Gateway settings"),
757 _("Enter the DNS and gateway information. "
758 "These settings are used only with Static IP (and DHCP if DNS set) on the RED interface."),
759 50, 5, 5, 18, entries
, _("OK"), _("Cancel"), NULL
);
760 if (rc
== 0 || rc
== 1)
762 strcpy(message
, _("The following fields are invalid:"));
763 strcpy(message
, "\n\n");
764 if (strlen(values
[DNS1
]))
766 if (inet_addr(values
[DNS1
]) == INADDR_NONE
)
768 strcat(message
, _("Primary DNS"));
769 strcat(message
, "\n");
773 if (strlen(values
[DNS2
]))
775 if (inet_addr(values
[DNS2
]) == INADDR_NONE
)
777 strcat(message
, _("Secondary DNS"));
778 strcat(message
, "\n");
782 if (strlen(values
[DEFAULT_GATEWAY
]))
784 if (inet_addr(values
[DEFAULT_GATEWAY
]) == INADDR_NONE
)
786 strcat(message
, _("Default gateway"));
787 strcat(message
, "\n");
791 if (!strlen(values
[DNS1
]) && strlen(values
[DNS2
]))
793 strcpy(message
, _("Secondary DNS specified without a Primary DNS"));
794 strcat(message
, "\n");
802 replacekeyvalue(kv
, "DNS1", values
[DNS1
]);
803 replacekeyvalue(kv
, "DNS2", values
[DNS2
]);
804 replacekeyvalue(kv
, "DEFAULT_GATEWAY", values
[DEFAULT_GATEWAY
]);
805 netaddresschange
= 1;
808 free(values
[DEFAULT_GATEWAY
]);
809 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");