]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/setup/networking.c
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
);
125 struct keyvalue
*kv
= initkeyvalues();
126 char temp
[STRING_SIZE
];
130 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
133 errorbox(_("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 errorbox(_("No GREEN interface assigned."));
149 if (!(interfacecheck(kv
, "GREEN")))
151 errorbox(_("Missing an IP address on GREEN."));
159 strcpy(temp
, ""); findkey(kv
, "RED_DEV", temp
);
162 rc
= newtWinChoice(_("Error"), _("OK"), _("Ignore"),
163 _("No RED interface assigned."));
164 if (rc
== 0 || rc
== 1)
170 if (!(interfacecheck(kv
, "RED")))
172 errorbox(_("Missing an IP address on RED."));
179 strcpy(temp
, ""); findkey(kv
, "ORANGE_DEV", temp
);
182 errorbox(_("No ORANGE interface assigned."));
186 if (!(interfacecheck(kv
, "ORANGE")))
188 errorbox(_("Missing an IP address on ORANGE."));
195 strcpy(temp
, ""); findkey(kv
, "BLUE_DEV", temp
);
198 errorbox(_("No BLUE interface assigned."));
202 if (!(interfacecheck(kv
, "BLUE")))
204 errorbox(_("Missing an IP address on BLUE."));
210 strcpy(temp
, ""); findkey(kv
, "RED_TYPE", temp
);
211 if ((configtype
== 0) || (strcmp(temp
, "STATIC") == 0))
213 strcpy(temp
, ""); findkey(kv
, "DNS1", temp
);
216 errorbox(_("Misssing DNS."));
220 strcpy(temp
, ""); findkey(kv
, "DEFAULT_GATEWAY", temp
);
223 errorbox(_("Missing Default Gateway."));
232 /* Shows the main menu and a summary of the current settings. */
236 _("Network configuration type"),
237 _("Drivers and card assignments"),
238 _("Address settings"),
239 _("DNS and Gateway settings"),
243 static int choice
= 0;
244 struct keyvalue
*kv
= initkeyvalues();
246 char temp
[STRING_SIZE
] = "1";
249 char networkrestart
[STRING_SIZE
] = "";
251 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
254 errorbox(_("Unable to open settings file"));
258 if (netaddresschange
)
259 strcpy(networkrestart
, _("When configuration is complete, a network restart will be required."));
261 strcpy(temp
, ""); findkey(kv
, "CONFIG_TYPE", temp
);
264 if (x
< 0 || x
> 4) x
= 0;
265 /* Format heading bit. */
266 snprintf(message
, 1000, _("Current config: %s\n\n%s"), configtypenames
[x
], networkrestart
);
267 rc
= newtWinMenu(_("Network configuration menu"), message
, 50, 5, 5, 6,
268 sections
, &choice
, _("OK"), _("Done"), NULL
);
270 if (rc
== 0 || rc
== 1)
278 /* Here they choose general network config, number of nics etc. */
279 int configtypemenu(void)
281 struct keyvalue
*kv
= initkeyvalues();
282 char temp
[STRING_SIZE
] = "1";
287 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
290 errorbox(_("Unable to open settings file"));
294 found
= scan_network_cards();
296 findkey(kv
, "CONFIG_TYPE", temp
); choise
= atol(temp
);
299 sprintf(message
, _("Select the network configuration for %s. "
300 "The following configuration types list those interfaces which have ethernet attached. "
301 "If you change this setting, a network restart will be required, and you will have to "
302 "reconfigure the network driver assignments."), NAME
);
303 rc
= newtWinMenu(_("Network configuration type"), message
, 50, 5, 5,
304 6, configtypenames
, &choise
, _("OK"), _("Cancel"), NULL
);
305 if ( configtypecards
[choise
] > found
) {
306 sprintf(message
, _("Not enough netcards for your choice.\n\nNeeded: %d - Available: %d\n"),
307 configtypecards
[choise
], found
);
311 if (rc
== 0 || rc
== 1)
314 sprintf(temp
, "%d", choise
);
315 replacekeyvalue(kv
, "CONFIG_TYPE", temp
);
316 configtype
= atol(temp
);
318 clear_card_entry(_RED_CARD_
);
320 clear_card_entry(_ORANGE_CARD_
);
322 clear_card_entry(_BLUE_CARD_
);
324 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
325 netaddresschange
= 1;
332 /* Driver menu. Choose drivers.. */
335 struct keyvalue
*kv
= initkeyvalues();
336 char message
[STRING_SIZE
];
337 char temp
[STRING_SIZE
] = "1";
340 int i
, rc
, kcount
= 0, neednics
;
342 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
345 errorbox(_("Unable to open settings file"));
349 if (findkey(kv
, "CONFIG_TYPE", temp
))
350 configtype
= atol(temp
);
352 fprintf(flog
,"setting CONFIG_TYPE = %s\n",temp
);
353 configtype
= atol(temp
);
354 replacekeyvalue(kv
, "CONFIG_TYPE", temp
);
355 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
358 strcpy(message
, _("Configure network drivers, and which interface each card is assigned to. "
359 "The current configuration is as follows:\n\n"));
364 sprintf(temp
, "%-6s: %s\n", "GREEN", knics
[_GREEN_CARD_
].description
);
365 strcat(message
, temp
);
366 if (strlen(knics
[_GREEN_CARD_
].macaddr
) ) {
367 sprintf(temp
, "%-6s: (%s)\n", "GREEN", knics
[_GREEN_CARD_
].macaddr
);
368 strcat(message
, temp
);
373 sprintf(temp
, "%-6s: %s\n", "RED", knics
[_RED_CARD_
].description
);
374 strcat(message
, temp
);
375 if (strlen(knics
[_RED_CARD_
].macaddr
) ) {
376 sprintf(temp
, "%-6s: (%s)\n", "RED", knics
[_RED_CARD_
].macaddr
);
377 strcat(message
, temp
);
382 sprintf(temp
, "%-6s: %s\n", "ORANGE", knics
[_ORANGE_CARD_
].description
);
383 strcat(message
, temp
);
384 if ( strlen(knics
[_ORANGE_CARD_
].macaddr
) ) {
385 sprintf(temp
, "%-6s: (%s)\n", "ORANGE", knics
[_ORANGE_CARD_
].macaddr
);
386 strcat(message
, temp
);
391 sprintf(temp
, "%-6s: %s\n", "BLUE", knics
[_BLUE_CARD_
].description
);
392 strcat(message
, temp
);
393 if (strlen(knics
[_BLUE_CARD_
].macaddr
)) {
394 sprintf(temp
, "%-6s: (%s)\n", "BLUE", knics
[_BLUE_CARD_
].macaddr
);
395 strcat(message
, temp
);
400 for ( i
=0 ; i
<4; i
++)
401 if (strcmp(knics
[i
].macaddr
, ""))
404 if (neednics
= kcount
)
406 strcat(message
, "\n");
407 strcat(message
, _("Do you wish to change these settings?"));
408 rc
= newtWinChoice(_("Drivers and card assignments"), _("OK"),
409 _("Cancel"), message
);
410 if (rc
== 0 || rc
== 1)
422 int set_menu_entry_for(int *nr
, int *card
)
427 int changedrivers(void)
429 struct keyvalue
*kv
= initkeyvalues();
430 char temp
[STRING_SIZE
], message
[STRING_SIZE
];
432 int green
= 0, red
= 0, blue
= 0, orange
= 0;
433 char MenuInhalt
[10][180];
434 char *pMenuInhalt
[10];
435 int count
= 0, choise
= 0, rc
;
438 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
441 errorbox(_("Unable to open settings file"));
445 runcommandwithstatus("/etc/rc.d/init.d/network stop red blue orange",
446 _("Networking"), _("Restarting non-local network..."), NULL
);
448 findkey(kv
, "CONFIG_TYPE", temp
); configtype
= atol(temp
);
450 { green
= 1; red
= 1; }
451 else if (configtype
== 2)
452 { green
= 1; red
= 1; orange
= 1; }
453 else if (configtype
== 3)
454 { green
= 1; red
= 1; blue
= 1; }
455 else if (configtype
== 4)
456 { green
= 1; red
=1; orange
=1; blue
= 1; }
457 else if (configtype
== "")
458 { green
= 1; red
= 1; }
463 strcpy(message
, _("Please choose the interface you wish to change.\n\n"));
466 strcpy(MenuInhalt
[count
], "GREEN");
467 pMenuInhalt
[count
] = MenuInhalt
[count
];
468 NicEntry
[_GREEN_CARD_
] = count
;
469 sprintf(temp
, "%-6s: %s\n", "GREEN", knics
[_GREEN_CARD_
].description
);
470 strcat(message
, temp
);
471 if ( strlen(knics
[_GREEN_CARD_
].macaddr
) ) {
472 sprintf(temp
, "%-6s: (%s)\n", "GREEN", knics
[_GREEN_CARD_
].macaddr
);
473 strcat(message
, temp
);
479 strcpy(MenuInhalt
[count
], "RED");
480 pMenuInhalt
[count
] = MenuInhalt
[count
];
481 NicEntry
[_RED_CARD_
] = count
;
482 sprintf(temp
, "%-6s: %s\n", "RED", knics
[_RED_CARD_
].description
);
483 strcat(message
, temp
);
484 if ( strlen(knics
[_RED_CARD_
].macaddr
) ) {
485 sprintf(temp
, "%-6s: (%s)\n", "RED", knics
[_RED_CARD_
].macaddr
);
486 strcat(message
, temp
);
492 strcpy(MenuInhalt
[count
], "ORANGE");
493 pMenuInhalt
[count
] = MenuInhalt
[count
];
494 NicEntry
[_ORANGE_CARD_
] = count
;
495 sprintf(temp
, "%-6s: %s\n", "ORANGE", knics
[_ORANGE_CARD_
].description
);
496 strcat(message
, temp
);
497 if ( strlen(knics
[_ORANGE_CARD_
].macaddr
) ) {
498 sprintf(temp
, "%-6s: (%s)\n", "ORANGE", knics
[_ORANGE_CARD_
].macaddr
);
499 strcat(message
, temp
);
505 strcpy(MenuInhalt
[count
], "BLUE");
506 pMenuInhalt
[count
] = MenuInhalt
[count
];
507 NicEntry
[_BLUE_CARD_
] = count
;
508 sprintf(temp
, "%-6s: %s\n", "BLUE", knics
[_BLUE_CARD_
].description
);
509 strcat(message
, temp
);
510 if ( strlen(knics
[_BLUE_CARD_
].macaddr
) ) {
511 sprintf(temp
, "%-6s: (%s)\n", "BLUE", knics
[_BLUE_CARD_
].macaddr
);
512 strcat(message
, temp
);
516 pMenuInhalt
[count
] = NULL
;
518 rc
= newtWinMenu(_("Assigned Cards"), message
, 70, 5, 5, 6, pMenuInhalt
,
519 &choise
, _("Select"), _("Remove"), _("Done"), NULL
);
521 if ( rc
== 0 || rc
== 1) {
522 if ((green
) && ( choise
== NicEntry
[0])) nicmenu(_GREEN_CARD_
);
523 if ((red
) && ( choise
== NicEntry
[1])) nicmenu(_RED_CARD_
);
524 if ((orange
) && ( choise
== NicEntry
[2])) nicmenu(_ORANGE_CARD_
);
525 if ((blue
) && ( choise
== NicEntry
[3])) nicmenu(_BLUE_CARD_
);
526 netaddresschange
= 1;
527 } else if (rc
== 2) {
528 if ((green
) && ( choise
== NicEntry
[0])) ask_clear_card_entry(_GREEN_CARD_
);
529 if ((red
) && ( choise
== NicEntry
[1])) ask_clear_card_entry(_RED_CARD_
);
530 if ((orange
) && ( choise
== NicEntry
[2])) ask_clear_card_entry(_ORANGE_CARD_
);
531 if ((blue
) && ( choise
== NicEntry
[3])) ask_clear_card_entry(_BLUE_CARD_
);
532 netaddresschange
= 1;
541 // Let user change GREEN address.
542 int greenaddressmenu(void)
544 struct keyvalue
*kv
= initkeyvalues();
548 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
551 errorbox(_("Unable to open settings file"));
555 sprintf(message
, _("If you change this IP address, and you are logged in remotely, "
556 "your connection to the %s machine will be broken, and you will have to reconnect "
557 "on the new IP. This is a risky operation, and should only be attempted if you "
558 "have physical access to the machine, should something go wrong."), NAME
);
559 rc
= newtWinChoice(_("Warning"), _("OK"), _("Cancel"), message
);
561 if (rc
== 0 || rc
== 1)
563 if (changeaddress(kv
, "GREEN", 0, ""))
565 netaddresschange
= 1;
566 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
576 // They can change BLUE, ORANGE and GREEN too :)
577 int addressesmenu(void)
579 struct keyvalue
*kv
= initkeyvalues();
580 struct keyvalue
*mainkv
= initkeyvalues();
583 char *green
= "GREEN";
584 char *orange
= "ORANGE";
588 char greenaddress
[STRING_SIZE
];
589 char oldgreenaddress
[STRING_SIZE
];
590 char temp
[STRING_SIZE
];
591 char temp2
[STRING_SIZE
];
596 char hostname
[STRING_SIZE
];
598 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
601 freekeyvalues(mainkv
);
602 errorbox(_("Unable to open settings file"));
605 if (!(readkeyvalues(mainkv
, CONFIG_ROOT
"/main/settings")))
608 freekeyvalues(mainkv
);
609 errorbox(_("Unable to open settings file"));
613 strcpy(temp
, "0"); findkey(kv
, "CONFIG_TYPE", temp
);
614 configtype
= atol(temp
);
625 sections
[c
] = orange
;
639 rc
= newtWinMenu(_("Address settings"),
640 _("Select the interface you wish to reconfigure."), 50, 5,
641 5, 6, sections
, &choice
, _("OK"), _("Done"), NULL
);
643 if (rc
== 0 || rc
== 1)
645 if (strcmp(sections
[choice
], "GREEN") == 0)
647 findkey(kv
, "GREEN_ADDRESS", oldgreenaddress
);
648 sprintf(message
, _("If you change this IP address, and you are logged in remotely, "
649 "your connection to the %s machine will be broken, and you will have to reconnect "
650 "on the new IP. This is a risky operation, and should only be attempted if you "
651 "have physical access to the machine, should something go wrong."), NAME
);
652 rc
= newtWinChoice(_("Warning"), _("OK"), _("Cancel"), message
);
653 if (rc
== 0 || rc
== 1)
655 if (changeaddress(kv
, "GREEN", 0, ""))
657 netaddresschange
= 1;
658 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
660 findkey(kv
, "GREEN_ADDRESS", greenaddress
);
661 snprintf(temp
, STRING_SIZE
-1, "option routers %s", oldgreenaddress
);
662 snprintf(temp2
, STRING_SIZE
-1, "option routers %s", greenaddress
);
663 replace (CONFIG_ROOT
"/dhcp/dhcpd.conf", temp
, temp2
);
664 chown (CONFIG_ROOT
"/dhcp/dhcpd.conf", 99, 99);
668 if (strcmp(sections
[choice
], "BLUE") == 0)
670 if (changeaddress(kv
, "BLUE", 0, ""))
671 netaddresschange
= 1;
673 if (strcmp(sections
[choice
], "ORANGE") == 0)
675 if (changeaddress(kv
, "ORANGE", 0, ""))
676 netaddresschange
= 1;
678 if (strcmp(sections
[choice
], "RED") == 0)
680 strcpy(hostname
, "");
681 findkey(mainkv
, "HOSTNAME", hostname
);
682 if (changeaddress(kv
, "RED", 1, hostname
))
683 netaddresschange
= 1;
690 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
692 freekeyvalues(mainkv
);
697 /* DNS and default gateway.... */
698 int dnsgatewaymenu(void)
700 struct keyvalue
*kv
= initkeyvalues();
702 char temp
[STRING_SIZE
] = "0";
703 struct newtWinEntry entries
[DNSGATEWAY_TOTAL
+1];
704 char *values
[DNSGATEWAY_TOTAL
]; /* pointers for the values. */
709 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
712 errorbox(_("Unable to open settings file"));
716 entries
[DNS1
].text
= _("Primary DNS:");
717 strcpy(temp
, ""); findkey(kv
, "DNS1", temp
);
718 values
[DNS1
] = strdup(temp
);
719 entries
[DNS1
].value
= &values
[DNS1
];
720 entries
[DNS1
].flags
= 0;
722 entries
[DNS2
].text
= _("Secondary DNS:");
723 strcpy(temp
, ""); findkey(kv
, "DNS2", temp
);
724 values
[DNS2
] = strdup(temp
);
725 entries
[DNS2
].value
= &values
[DNS2
];
726 entries
[DNS2
].flags
= 0;
728 entries
[DEFAULT_GATEWAY
].text
= _("Default gateway:");
729 strcpy(temp
, ""); findkey(kv
, "DEFAULT_GATEWAY", temp
);
730 values
[DEFAULT_GATEWAY
] = strdup(temp
);
731 entries
[DEFAULT_GATEWAY
].value
= &values
[DEFAULT_GATEWAY
];
732 entries
[DEFAULT_GATEWAY
].flags
= 0;
734 entries
[DNSGATEWAY_TOTAL
].text
= NULL
;
735 entries
[DNSGATEWAY_TOTAL
].value
= NULL
;
736 entries
[DNSGATEWAY_TOTAL
].flags
= 0;
742 rc
= newtWinEntries(_("DNS and Gateway settings"),
743 _("Enter the DNS and gateway information. "
744 "These settings are used only with Static IP (and DHCP if DNS set) on the RED interface."),
745 50, 5, 5, 18, entries
, _("OK"), _("Cancel"), NULL
);
746 if (rc
== 0 || rc
== 1)
748 strcpy(message
, _("The following fields are invalid:"));
749 strcpy(message
, "\n\n");
750 if (strlen(values
[DNS1
]))
752 if (inet_addr(values
[DNS1
]) == INADDR_NONE
)
754 strcat(message
, _("Primary DNS"));
755 strcat(message
, "\n");
759 if (strlen(values
[DNS2
]))
761 if (inet_addr(values
[DNS2
]) == INADDR_NONE
)
763 strcat(message
, _("Secondary DNS"));
764 strcat(message
, "\n");
768 if (strlen(values
[DEFAULT_GATEWAY
]))
770 if (inet_addr(values
[DEFAULT_GATEWAY
]) == INADDR_NONE
)
772 strcat(message
, _("Default gateway"));
773 strcat(message
, "\n");
777 if (!strlen(values
[DNS1
]) && strlen(values
[DNS2
]))
779 strcpy(message
, _("Secondary DNS specified without a Primary DNS"));
780 strcat(message
, "\n");
788 replacekeyvalue(kv
, "DNS1", values
[DNS1
]);
789 replacekeyvalue(kv
, "DNS2", values
[DNS2
]);
790 replacekeyvalue(kv
, "DEFAULT_GATEWAY", values
[DEFAULT_GATEWAY
]);
791 netaddresschange
= 1;
794 free(values
[DEFAULT_GATEWAY
]);
795 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");