]>
git.ipfire.org Git - people/pmueller/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
);
126 struct keyvalue
*kv
= initkeyvalues();
127 char temp
[STRING_SIZE
];
131 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
134 errorbox(_("Unable to open settings file"));
138 strcpy(temp
, "1"); findkey(kv
, "CONFIG_TYPE", temp
); configtype
= atol(temp
);
139 if (configtype
< 1 || configtype
> 4) configtype
= 1;
143 strcpy(temp
, ""); findkey(kv
, "GREEN_DEV", temp
);
146 errorbox(_("No GREEN interface assigned."));
150 if (!(interfacecheck(kv
, "GREEN")))
152 errorbox(_("Missing an IP address on GREEN."));
160 strcpy(temp
, ""); findkey(kv
, "RED_DEV", temp
);
163 rc
= newtWinChoice(_("Error"), _("OK"), _("Ignore"),
164 _("No RED interface assigned."));
165 if (rc
== 0 || rc
== 1)
171 if (!(interfacecheck(kv
, "RED")))
173 errorbox(_("Missing an IP address on RED."));
180 strcpy(temp
, ""); findkey(kv
, "ORANGE_DEV", temp
);
183 errorbox(_("No ORANGE interface assigned."));
187 if (!(interfacecheck(kv
, "ORANGE")))
189 errorbox(_("Missing an IP address on ORANGE."));
196 strcpy(temp
, ""); findkey(kv
, "BLUE_DEV", temp
);
199 errorbox(_("No BLUE interface assigned."));
203 if (!(interfacecheck(kv
, "BLUE")))
205 errorbox(_("Missing an IP address on BLUE."));
211 strcpy(temp
, ""); findkey(kv
, "RED_TYPE", temp
);
212 if ((configtype
== 0) || (strcmp(temp
, "STATIC") == 0))
214 strcpy(temp
, ""); findkey(kv
, "DNS1", temp
);
217 errorbox(_("Misssing DNS."));
221 strcpy(temp
, ""); findkey(kv
, "DEFAULT_GATEWAY", temp
);
224 errorbox(_("Missing Default Gateway."));
233 /* Shows the main menu and a summary of the current settings. */
237 _("Network configuration type"),
238 _("Drivers and card assignments"),
239 _("Address settings"),
240 _("DNS and Gateway settings"),
244 static int choice
= 0;
245 struct keyvalue
*kv
= initkeyvalues();
247 char temp
[STRING_SIZE
] = "1";
250 char networkrestart
[STRING_SIZE
] = "";
252 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
255 errorbox(_("Unable to open settings file"));
259 if (netaddresschange
)
260 strcpy(networkrestart
, _("When configuration is complete, a network restart will be required."));
262 strcpy(temp
, ""); findkey(kv
, "CONFIG_TYPE", temp
);
265 if (x
< 0 || x
> 4) x
= 0;
266 /* Format heading bit. */
267 snprintf(message
, 1000, _("Current config: %s\n\n%s"), configtypenames
[x
], networkrestart
);
268 rc
= newtWinMenu(_("Network configuration menu"), message
, 50, 5, 5, 6,
269 sections
, &choice
, _("OK"), _("Done"), NULL
);
271 if (rc
== 0 || rc
== 1)
279 /* Here they choose general network config, number of nics etc. */
280 int configtypemenu(void)
282 struct keyvalue
*kv
= initkeyvalues();
283 char temp
[STRING_SIZE
] = "1";
288 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
291 errorbox(_("Unable to open settings file"));
295 found
= scan_network_cards();
297 findkey(kv
, "CONFIG_TYPE", temp
); choise
= atol(temp
);
300 sprintf(message
, _("Select the network configuration for %s. "
301 "The following configuration types list those interfaces which have ethernet attached. "
302 "If you change this setting, a network restart will be required, and you will have to "
303 "reconfigure the network driver assignments."), NAME
);
304 rc
= newtWinMenu(_("Network configuration type"), message
, 50, 5, 5,
305 6, configtypenames
, &choise
, _("OK"), _("Cancel"), NULL
);
306 if ( configtypecards
[choise
] > found
) {
307 sprintf(message
, _("Not enough netcards for your choice.\n\nNeeded: %d - Available: %d\n"),
308 configtypecards
[choise
], found
);
312 if (rc
== 0 || rc
== 1)
315 sprintf(temp
, "%d", choise
);
316 replacekeyvalue(kv
, "CONFIG_TYPE", temp
);
317 configtype
= atol(temp
);
319 clear_card_entry(_RED_CARD_
);
321 clear_card_entry(_ORANGE_CARD_
);
323 clear_card_entry(_BLUE_CARD_
);
325 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
326 netaddresschange
= 1;
333 /* Driver menu. Choose drivers.. */
336 struct keyvalue
*kv
= initkeyvalues();
337 char message
[STRING_SIZE
];
338 char temp
[STRING_SIZE
] = "1";
341 int i
, rc
, kcount
= 0, neednics
;
343 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
346 errorbox(_("Unable to open settings file"));
350 if (findkey(kv
, "CONFIG_TYPE", temp
))
351 configtype
= atol(temp
);
353 fprintf(flog
,"setting CONFIG_TYPE = %s\n",temp
);
354 configtype
= atol(temp
);
355 replacekeyvalue(kv
, "CONFIG_TYPE", temp
);
356 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
359 strcpy(message
, _("Configure network drivers, and which interface each card is assigned to. "
360 "The current configuration is as follows:\n\n"));
365 sprintf(temp
, "%-6s: %s\n", "GREEN", knics
[_GREEN_CARD_
].description
);
366 strcat(message
, temp
);
367 if (strlen(knics
[_GREEN_CARD_
].macaddr
) ) {
368 sprintf(temp
, "%-6s: (%s)\n", "GREEN", knics
[_GREEN_CARD_
].macaddr
);
369 strcat(message
, temp
);
374 sprintf(temp
, "%-6s: %s\n", "RED", knics
[_RED_CARD_
].description
);
375 strcat(message
, temp
);
376 if (strlen(knics
[_RED_CARD_
].macaddr
) ) {
377 sprintf(temp
, "%-6s: (%s)\n", "RED", knics
[_RED_CARD_
].macaddr
);
378 strcat(message
, temp
);
383 sprintf(temp
, "%-6s: %s\n", "ORANGE", knics
[_ORANGE_CARD_
].description
);
384 strcat(message
, temp
);
385 if ( strlen(knics
[_ORANGE_CARD_
].macaddr
) ) {
386 sprintf(temp
, "%-6s: (%s)\n", "ORANGE", knics
[_ORANGE_CARD_
].macaddr
);
387 strcat(message
, temp
);
392 sprintf(temp
, "%-6s: %s\n", "BLUE", knics
[_BLUE_CARD_
].description
);
393 strcat(message
, temp
);
394 if (strlen(knics
[_BLUE_CARD_
].macaddr
)) {
395 sprintf(temp
, "%-6s: (%s)\n", "BLUE", knics
[_BLUE_CARD_
].macaddr
);
396 strcat(message
, temp
);
401 for ( i
=0 ; i
<4; i
++)
402 if (strcmp(knics
[i
].macaddr
, ""))
405 if (neednics
= kcount
)
407 strcat(message
, "\n");
408 strcat(message
, _("Do you wish to change these settings?"));
409 rc
= newtWinChoice(_("Drivers and card assignments"), _("OK"),
410 _("Cancel"), message
);
411 if (rc
== 0 || rc
== 1)
423 int set_menu_entry_for(int *nr
, int *card
)
428 int changedrivers(void)
430 struct keyvalue
*kv
= initkeyvalues();
431 char temp
[STRING_SIZE
], message
[STRING_SIZE
];
433 int green
= 0, red
= 0, blue
= 0, orange
= 0;
434 char MenuInhalt
[10][180];
435 char *pMenuInhalt
[10];
436 int count
= 0, choise
= 0, rc
;
439 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
442 errorbox(_("Unable to open settings file"));
446 runcommandwithstatus("/etc/rc.d/init.d/network stop red blue orange",
447 _("Networking"), _("Restarting non-local network..."), NULL
);
449 findkey(kv
, "CONFIG_TYPE", temp
); configtype
= atol(temp
);
451 { green
= 1; red
= 1; }
452 else if (configtype
== 2)
453 { green
= 1; red
= 1; orange
= 1; }
454 else if (configtype
== 3)
455 { green
= 1; red
= 1; blue
= 1; }
456 else if (configtype
== 4)
457 { green
= 1; red
=1; orange
=1; blue
= 1; }
458 else if (configtype
== "")
459 { green
= 1; red
= 1; }
464 strcpy(message
, _("Please choose the interface you wish to change.\n\n"));
467 strcpy(MenuInhalt
[count
], "GREEN");
468 pMenuInhalt
[count
] = MenuInhalt
[count
];
469 NicEntry
[_GREEN_CARD_
] = count
;
470 sprintf(temp
, "%-6s: %s\n", "GREEN", knics
[_GREEN_CARD_
].description
);
471 strcat(message
, temp
);
472 if ( strlen(knics
[_GREEN_CARD_
].macaddr
) ) {
473 sprintf(temp
, "%-6s: (%s)\n", "GREEN", knics
[_GREEN_CARD_
].macaddr
);
474 strcat(message
, temp
);
480 strcpy(MenuInhalt
[count
], "RED");
481 pMenuInhalt
[count
] = MenuInhalt
[count
];
482 NicEntry
[_RED_CARD_
] = count
;
483 sprintf(temp
, "%-6s: %s\n", "RED", knics
[_RED_CARD_
].description
);
484 strcat(message
, temp
);
485 if ( strlen(knics
[_RED_CARD_
].macaddr
) ) {
486 sprintf(temp
, "%-6s: (%s)\n", "RED", knics
[_RED_CARD_
].macaddr
);
487 strcat(message
, temp
);
493 strcpy(MenuInhalt
[count
], "ORANGE");
494 pMenuInhalt
[count
] = MenuInhalt
[count
];
495 NicEntry
[_ORANGE_CARD_
] = count
;
496 sprintf(temp
, "%-6s: %s\n", "ORANGE", knics
[_ORANGE_CARD_
].description
);
497 strcat(message
, temp
);
498 if ( strlen(knics
[_ORANGE_CARD_
].macaddr
) ) {
499 sprintf(temp
, "%-6s: (%s)\n", "ORANGE", knics
[_ORANGE_CARD_
].macaddr
);
500 strcat(message
, temp
);
506 strcpy(MenuInhalt
[count
], "BLUE");
507 pMenuInhalt
[count
] = MenuInhalt
[count
];
508 NicEntry
[_BLUE_CARD_
] = count
;
509 sprintf(temp
, "%-6s: %s\n", "BLUE", knics
[_BLUE_CARD_
].description
);
510 strcat(message
, temp
);
511 if ( strlen(knics
[_BLUE_CARD_
].macaddr
) ) {
512 sprintf(temp
, "%-6s: (%s)\n", "BLUE", knics
[_BLUE_CARD_
].macaddr
);
513 strcat(message
, temp
);
517 pMenuInhalt
[count
] = NULL
;
519 rc
= newtWinMenu(_("Assigned Cards"), message
, 70, 5, 5, 6, pMenuInhalt
,
520 &choise
, _("Select"), _("Remove"), _("Done"), NULL
);
522 if ( rc
== 0 || rc
== 1) {
523 if ((green
) && ( choise
== NicEntry
[0])) nicmenu(_GREEN_CARD_
);
524 if ((red
) && ( choise
== NicEntry
[1])) nicmenu(_RED_CARD_
);
525 if ((orange
) && ( choise
== NicEntry
[2])) nicmenu(_ORANGE_CARD_
);
526 if ((blue
) && ( choise
== NicEntry
[3])) nicmenu(_BLUE_CARD_
);
527 netaddresschange
= 1;
528 } else if (rc
== 2) {
529 if ((green
) && ( choise
== NicEntry
[0])) ask_clear_card_entry(_GREEN_CARD_
);
530 if ((red
) && ( choise
== NicEntry
[1])) ask_clear_card_entry(_RED_CARD_
);
531 if ((orange
) && ( choise
== NicEntry
[2])) ask_clear_card_entry(_ORANGE_CARD_
);
532 if ((blue
) && ( choise
== NicEntry
[3])) ask_clear_card_entry(_BLUE_CARD_
);
533 netaddresschange
= 1;
542 // Let user change GREEN address.
543 int greenaddressmenu(void)
545 struct keyvalue
*kv
= initkeyvalues();
549 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
552 errorbox(_("Unable to open settings file"));
556 sprintf(message
, _("If you change this IP address, and you are logged in remotely, "
557 "your connection to the %s machine will be broken, and you will have to reconnect "
558 "on the new IP. This is a risky operation, and should only be attempted if you "
559 "have physical access to the machine, should something go wrong."), NAME
);
560 rc
= newtWinChoice(_("Warning"), _("OK"), _("Cancel"), message
);
562 if (rc
== 0 || rc
== 1)
564 if (changeaddress(kv
, "GREEN", 0, ""))
566 netaddresschange
= 1;
567 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
577 // They can change BLUE, ORANGE and GREEN too :)
578 int addressesmenu(void)
580 struct keyvalue
*kv
= initkeyvalues();
581 struct keyvalue
*mainkv
= initkeyvalues();
584 char *green
= "GREEN";
585 char *orange
= "ORANGE";
589 char greenaddress
[STRING_SIZE
];
590 char oldgreenaddress
[STRING_SIZE
];
591 char temp
[STRING_SIZE
];
592 char temp2
[STRING_SIZE
];
597 char hostname
[STRING_SIZE
];
599 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
602 freekeyvalues(mainkv
);
603 errorbox(_("Unable to open settings file"));
606 if (!(readkeyvalues(mainkv
, CONFIG_ROOT
"/main/settings")))
609 freekeyvalues(mainkv
);
610 errorbox(_("Unable to open settings file"));
614 strcpy(temp
, "0"); findkey(kv
, "CONFIG_TYPE", temp
);
615 configtype
= atol(temp
);
626 sections
[c
] = orange
;
640 rc
= newtWinMenu(_("Address settings"),
641 _("Select the interface you wish to reconfigure."), 50, 5,
642 5, 6, sections
, &choice
, _("OK"), _("Done"), NULL
);
644 if (rc
== 0 || rc
== 1)
646 if (strcmp(sections
[choice
], "GREEN") == 0)
648 findkey(kv
, "GREEN_ADDRESS", oldgreenaddress
);
649 sprintf(message
, _("If you change this IP address, and you are logged in remotely, "
650 "your connection to the %s machine will be broken, and you will have to reconnect "
651 "on the new IP. This is a risky operation, and should only be attempted if you "
652 "have physical access to the machine, should something go wrong."), NAME
);
653 rc
= newtWinChoice(_("Warning"), _("OK"), _("Cancel"), message
);
654 if (rc
== 0 || rc
== 1)
656 if (changeaddress(kv
, "GREEN", 0, ""))
658 netaddresschange
= 1;
659 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
661 findkey(kv
, "GREEN_ADDRESS", greenaddress
);
662 snprintf(temp
, STRING_SIZE
-1, "option routers %s", oldgreenaddress
);
663 snprintf(temp2
, STRING_SIZE
-1, "option routers %s", greenaddress
);
664 replace (CONFIG_ROOT
"/dhcp/dhcpd.conf", temp
, temp2
);
665 chown (CONFIG_ROOT
"/dhcp/dhcpd.conf", 99, 99);
669 if (strcmp(sections
[choice
], "BLUE") == 0)
671 if (changeaddress(kv
, "BLUE", 0, ""))
672 netaddresschange
= 1;
674 if (strcmp(sections
[choice
], "ORANGE") == 0)
676 if (changeaddress(kv
, "ORANGE", 0, ""))
677 netaddresschange
= 1;
679 if (strcmp(sections
[choice
], "RED") == 0)
681 strcpy(hostname
, "");
682 findkey(mainkv
, "HOSTNAME", hostname
);
683 if (changeaddress(kv
, "RED", 1, hostname
))
684 netaddresschange
= 1;
691 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
693 freekeyvalues(mainkv
);
698 /* DNS and default gateway.... */
699 int dnsgatewaymenu(void)
701 struct keyvalue
*kv
= initkeyvalues();
703 char temp
[STRING_SIZE
] = "0";
704 struct newtWinEntry entries
[DNSGATEWAY_TOTAL
+1];
705 char *values
[DNSGATEWAY_TOTAL
]; /* pointers for the values. */
710 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
713 errorbox(_("Unable to open settings file"));
717 entries
[DNS1
].text
= _("Primary DNS:");
718 strcpy(temp
, ""); findkey(kv
, "DNS1", temp
);
719 values
[DNS1
] = strdup(temp
);
720 entries
[DNS1
].value
= &values
[DNS1
];
721 entries
[DNS1
].flags
= 0;
723 entries
[DNS2
].text
= _("Secondary DNS:");
724 strcpy(temp
, ""); findkey(kv
, "DNS2", temp
);
725 values
[DNS2
] = strdup(temp
);
726 entries
[DNS2
].value
= &values
[DNS2
];
727 entries
[DNS2
].flags
= 0;
729 entries
[DEFAULT_GATEWAY
].text
= _("Default gateway:");
730 strcpy(temp
, ""); findkey(kv
, "DEFAULT_GATEWAY", temp
);
731 values
[DEFAULT_GATEWAY
] = strdup(temp
);
732 entries
[DEFAULT_GATEWAY
].value
= &values
[DEFAULT_GATEWAY
];
733 entries
[DEFAULT_GATEWAY
].flags
= 0;
735 entries
[DNSGATEWAY_TOTAL
].text
= NULL
;
736 entries
[DNSGATEWAY_TOTAL
].value
= NULL
;
737 entries
[DNSGATEWAY_TOTAL
].flags
= 0;
743 rc
= newtWinEntries(_("DNS and Gateway settings"),
744 _("Enter the DNS and gateway information. "
745 "These settings are used only with Static IP (and DHCP if DNS set) on the RED interface."),
746 50, 5, 5, 18, entries
, _("OK"), _("Cancel"), NULL
);
747 if (rc
== 0 || rc
== 1)
749 strcpy(message
, _("The following fields are invalid:"));
750 strcpy(message
, "\n\n");
751 if (strlen(values
[DNS1
]))
753 if (inet_addr(values
[DNS1
]) == INADDR_NONE
)
755 strcat(message
, _("Primary DNS"));
756 strcat(message
, "\n");
760 if (strlen(values
[DNS2
]))
762 if (inet_addr(values
[DNS2
]) == INADDR_NONE
)
764 strcat(message
, _("Secondary DNS"));
765 strcat(message
, "\n");
769 if (strlen(values
[DEFAULT_GATEWAY
]))
771 if (inet_addr(values
[DEFAULT_GATEWAY
]) == INADDR_NONE
)
773 strcat(message
, _("Default gateway"));
774 strcat(message
, "\n");
778 if (!strlen(values
[DNS1
]) && strlen(values
[DNS2
]))
780 strcpy(message
, _("Secondary DNS specified without a Primary DNS"));
781 strcat(message
, "\n");
789 replacekeyvalue(kv
, "DNS1", values
[DNS1
]);
790 replacekeyvalue(kv
, "DNS2", values
[DNS2
]);
791 replacekeyvalue(kv
, "DEFAULT_GATEWAY", values
[DEFAULT_GATEWAY
]);
792 netaddresschange
= 1;
795 free(values
[DEFAULT_GATEWAY
]);
796 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");