]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/setup/networking.c
setup: move gateway setting to red address setting.
[people/pmueller/ipfire-2.x.git] / src / setup / networking.c
1 /* SmoothWall setup program.
2 *
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
5 *
6 * (c) Lawrence Manning, 2001
7 * The big one: networking.
8 *
9 */
10
11 // Translation
12 #include <libintl.h>
13 #define _(x) dgettext("setup", x)
14
15 #include "setup.h"
16
17 extern FILE *flog;
18 extern char *mylog;
19
20 extern int automode;
21
22 #define HAS_GREEN 1
23 #define HAS_RED (configtype == 1 || configtype == 2 || configtype == 3 || configtype == 4)
24 #define HAS_ORANGE (configtype == 2 || configtype == 4)
25 #define HAS_BLUE (configtype == 3 || configtype == 4)
26 #define RED_IS_NOT_ETH (configtype == 0)
27
28 extern struct nic nics[];
29 extern struct knic knics[];
30
31 char *configtypenames[] = {
32 "GREEN + RED",
33 "GREEN + RED + ORANGE",
34 "GREEN + RED + BLUE",
35 "GREEN + RED + ORANGE + BLUE",
36 NULL };
37 int configtypecards[] = {
38 2, // "GREEN + RED",
39 3, // "GREEN + RED + ORANGE",
40 3, // "GREEN + RED + BLUE",
41 4 // "GREEN + RED + ORANGE + BLUE",
42 };
43
44
45 int netaddresschange;
46
47 int oktoleave(void);
48 int firstmenu(void);
49 int configtypemenu(void);
50 int drivermenu(void);
51 int changedrivers(void);
52 int greenaddressmenu(void);
53 int addressesmenu(void);
54
55 int handlenetworking(void)
56 {
57 int done;
58 int choice;
59 int found;
60
61 netaddresschange = 0;
62
63 found = scan_network_cards();
64 found = init_knics();
65
66 done = 0;
67 while (!done)
68 {
69 choice = firstmenu();
70
71 switch (choice)
72 {
73 case 1:
74 configtypemenu();
75 break;
76
77 case 2:
78 drivermenu();
79 break;
80
81 case 3:
82 addressesmenu();
83 break;
84
85 case 0:
86 if (oktoleave()) done = 1;
87 break;
88
89 default:
90 break;
91 }
92 }
93
94 if (automode == 0)
95 {
96 /* Restart networking! */
97 if (netaddresschange)
98 {
99 runcommandwithstatus("/etc/rc.d/init.d/network stop",
100 _("Networking"), _("Stopping network..."), NULL);
101
102 rename_nics();
103
104 runcommandwithstatus("/etc/rc.d/init.d/network start",
105 _("Networking"), _("Restarting network..."), NULL);
106 runcommandwithstatus("/etc/rc.d/init.d/unbound restart",
107 _("Networking"), _("Restarting unbound..."), NULL);
108 }
109 } else {
110 rename_nics();
111 }
112 return 1;
113 }
114
115 int oktoleave(void)
116 {
117 struct keyvalue *kv = initkeyvalues();
118 char temp[STRING_SIZE];
119 int configtype;
120 int rc;
121
122 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
123 {
124 freekeyvalues(kv);
125 errorbox(_("Unable to open settings file"));
126 return 0;
127 }
128
129 strcpy(temp, "1"); findkey(kv, "CONFIG_TYPE", temp); configtype = atol(temp);
130 if (configtype < 1 || configtype > 4) configtype = 1;
131
132 if (HAS_GREEN)
133 {
134 strcpy(temp, ""); findkey(kv, "GREEN_DEV", temp);
135 if (!(strlen(temp)))
136 {
137 rc = newtWinChoice(_("Error"), _("OK"), _("Ignore"),
138 _("No GREEN interface assigned."));
139 if (rc == 0 || rc == 1)
140 {
141 freekeyvalues(kv);
142 return 0;
143 }
144 }
145 if (!(interfacecheck(kv, "GREEN")))
146 {
147 errorbox(_("Missing an IP address on GREEN."));
148 freekeyvalues(kv);
149 return 0;
150 }
151 }
152 if (HAS_RED)
153 {
154
155 strcpy(temp, ""); findkey(kv, "RED_DEV", temp);
156 if (!(strlen(temp)))
157 {
158 rc = newtWinChoice(_("Error"), _("OK"), _("Ignore"),
159 _("No RED interface assigned."));
160 if (rc == 0 || rc == 1)
161 {
162 freekeyvalues(kv);
163 return 0;
164 }
165 }
166 if (!(interfacecheck(kv, "RED")))
167 {
168 errorbox(_("Missing an IP address on RED."));
169 freekeyvalues(kv);
170 return 0;
171 }
172 }
173 if (HAS_ORANGE)
174 {
175 strcpy(temp, ""); findkey(kv, "ORANGE_DEV", temp);
176 if (!(strlen(temp)))
177 {
178 rc = newtWinChoice(_("Error"), _("OK"), _("Ignore"),
179 _("No ORANGE interface assigned."));
180 if (rc == 0 || rc == 1)
181 {
182 freekeyvalues(kv);
183 return 0;
184 }
185 }
186 if (!(interfacecheck(kv, "ORANGE")))
187 {
188 errorbox(_("Missing an IP address on ORANGE."));
189 freekeyvalues(kv);
190 return 0;
191 }
192 }
193 if (HAS_BLUE)
194 {
195 strcpy(temp, ""); findkey(kv, "BLUE_DEV", temp);
196 if (!(strlen(temp)))
197 {
198 rc = newtWinChoice(_("Error"), _("OK"), _("Ignore"),
199 _("No BLUE interface assigned."));
200 if (rc == 0 || rc == 1)
201 {
202 freekeyvalues(kv);
203 return 0;
204 }
205 }
206 if (!(interfacecheck(kv, "BLUE")))
207 {
208 errorbox(_("Missing an IP address on BLUE."));
209 freekeyvalues(kv);
210 return 0;
211 }
212 }
213 return 1;
214 }
215
216
217 /* Shows the main menu and a summary of the current settings. */
218 int firstmenu(void)
219 {
220 char *sections[] = {
221 _("Network configuration type"),
222 _("Drivers and card assignments"),
223 _("Address settings"),
224 NULL
225 };
226 int rc;
227 static int choice = 0;
228 struct keyvalue *kv = initkeyvalues();
229 char message[1000];
230 char temp[STRING_SIZE] = "1";
231 int x;
232 int result;
233 char networkrestart[STRING_SIZE] = "";
234
235 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
236 {
237 freekeyvalues(kv);
238 errorbox(_("Unable to open settings file"));
239 return 0;
240 }
241
242 if (netaddresschange)
243 strcpy(networkrestart, _("When configuration is complete, a network restart will be required."));
244
245 strcpy(temp, ""); findkey(kv, "CONFIG_TYPE", temp);
246 x = atol(temp);
247 x--;
248 if (x < 0 || x > 4) x = 0;
249 /* Format heading bit. */
250 snprintf(message, 1000, _("Current config: %s\n\n%s"), configtypenames[x], networkrestart);
251 rc = newtWinMenu(_("Network configuration menu"), message, 50, 5, 5, 6,
252 sections, &choice, _("OK"), _("Done"), NULL);
253
254 if (rc == 0 || rc == 1)
255 result = choice + 1;
256 else
257 result = 0;
258
259 return result;
260 }
261
262 /* Here they choose general network config, number of nics etc. */
263 int configtypemenu(void)
264 {
265 struct keyvalue *kv = initkeyvalues();
266 char temp[STRING_SIZE] = "1";
267 char message[1000];
268 int choise, found;
269 int rc, configtype;
270
271 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
272 {
273 freekeyvalues(kv);
274 errorbox(_("Unable to open settings file"));
275 return 0;
276 }
277
278 found = scan_network_cards();
279
280 findkey(kv, "CONFIG_TYPE", temp); choise = atol(temp);
281 choise--;
282
283 sprintf(message, _("Select the network configuration for %s. "
284 "The following configuration types list those interfaces which have ethernet attached. "
285 "If you change this setting, a network restart will be required, and you will have to "
286 "reconfigure the network driver assignments."), NAME);
287 rc = newtWinMenu(_("Network configuration type"), message, 50, 5, 5,
288 6, configtypenames, &choise, _("OK"), _("Cancel"), NULL);
289 if ( configtypecards[choise] > found ) {
290 sprintf(message, _("Not enough netcards for your choice.\n\nNeeded: %d - Available: %d\n"),
291 configtypecards[choise], found);
292 errorbox(message);
293 }
294
295 if (rc == 0 || rc == 1)
296 {
297 choise++;
298 sprintf(temp, "%d", choise);
299 replacekeyvalue(kv, "CONFIG_TYPE", temp);
300 configtype = atol(temp);
301 if (!HAS_RED)
302 clear_card_entry(_RED_CARD_);
303 if (!HAS_ORANGE)
304 clear_card_entry(_ORANGE_CARD_);
305 if (!HAS_BLUE)
306 clear_card_entry(_BLUE_CARD_);
307
308 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
309 netaddresschange = 1;
310 }
311 freekeyvalues(kv);
312
313 return 0;
314 }
315
316 /* Driver menu. Choose drivers.. */
317 int drivermenu(void)
318 {
319 struct keyvalue *kv = initkeyvalues();
320 char message[STRING_SIZE];
321 char temp[STRING_SIZE] = "1";
322
323 int configtype;
324 int i, rc, kcount = 0, neednics;
325
326 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
327 {
328 freekeyvalues(kv);
329 errorbox(_("Unable to open settings file"));
330 return 0;
331 }
332
333 if (findkey(kv, "CONFIG_TYPE", temp))
334 configtype = atol(temp);
335 else {
336 fprintf(flog,"setting CONFIG_TYPE = %s\n",temp);
337 configtype = atol(temp);
338 replacekeyvalue(kv, "CONFIG_TYPE", temp);
339 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
340 }
341
342 strcpy(message, _("Configure network drivers, and which interface each card is assigned to. "
343 "The current configuration is as follows:\n\n"));
344
345 kcount = 0;
346 neednics = 0;
347 if (HAS_GREEN) {
348 sprintf(temp, "%-6s: %s\n", "GREEN", knics[_GREEN_CARD_].description);
349 strcat(message, temp);
350 if (strlen(knics[_GREEN_CARD_].macaddr) ) {
351 sprintf(temp, "%-6s: (%s)\n", "GREEN", knics[_GREEN_CARD_].macaddr);
352 strcat(message, temp);
353 }
354 neednics++;
355 }
356 if (HAS_RED) {
357 sprintf(temp, "%-6s: %s\n", "RED", knics[_RED_CARD_].description);
358 strcat(message, temp);
359 if (strlen(knics[_RED_CARD_].macaddr) ) {
360 sprintf(temp, "%-6s: (%s)\n", "RED", knics[_RED_CARD_].macaddr);
361 strcat(message, temp);
362 }
363 neednics++;
364 }
365 if (HAS_ORANGE) {
366 sprintf(temp, "%-6s: %s\n", "ORANGE", knics[_ORANGE_CARD_].description);
367 strcat(message, temp);
368 if ( strlen(knics[_ORANGE_CARD_].macaddr) ) {
369 sprintf(temp, "%-6s: (%s)\n", "ORANGE", knics[_ORANGE_CARD_].macaddr);
370 strcat(message, temp);
371 }
372 neednics++;
373 }
374 if (HAS_BLUE) {
375 sprintf(temp, "%-6s: %s\n", "BLUE", knics[_BLUE_CARD_].description);
376 strcat(message, temp);
377 if (strlen(knics[_BLUE_CARD_].macaddr)) {
378 sprintf(temp, "%-6s: (%s)\n", "BLUE", knics[_BLUE_CARD_].macaddr);
379 strcat(message, temp);
380 }
381 neednics++;
382 }
383
384 for ( i=0 ; i<4; i++)
385 if (strcmp(knics[i].macaddr, ""))
386 kcount++;
387
388 if (neednics = kcount)
389 {
390 strcat(message, "\n");
391 strcat(message, _("Do you wish to change these settings?"));
392 rc = newtWinChoice(_("Drivers and card assignments"), _("OK"),
393 _("Cancel"), message);
394 if (rc == 0 || rc == 1)
395 {
396 changedrivers();
397 }
398 } else {
399 changedrivers();
400 }
401 freekeyvalues(kv);
402
403 return 1;
404 }
405
406 int set_menu_entry_for(int *nr, int *card)
407 {
408
409 }
410
411 int changedrivers(void)
412 {
413 struct keyvalue *kv = initkeyvalues();
414 char temp[STRING_SIZE], message[STRING_SIZE];
415 int configtype;
416 int green = 0, red = 0, blue = 0, orange = 0;
417 char MenuInhalt[10][180];
418 char *pMenuInhalt[10];
419 int count = 0, choise = 0, rc;
420 int NicEntry[10];
421
422 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
423 {
424 freekeyvalues(kv);
425 errorbox(_("Unable to open settings file"));
426 return 0;
427 }
428 if (automode == 0)
429 runcommandwithstatus("/etc/rc.d/init.d/network stop red blue orange",
430 _("Networking"), _("Restarting non-local network..."), NULL);
431
432 findkey(kv, "CONFIG_TYPE", temp); configtype = atol(temp);
433 if (configtype == 1)
434 { green = 1; red = 1; }
435 else if (configtype == 2)
436 { green = 1; red = 1; orange = 1; }
437 else if (configtype == 3)
438 { green = 1; red = 1; blue = 1; }
439 else if (configtype == 4)
440 { green = 1; red=1; orange=1; blue = 1; }
441 else if (configtype == "")
442 { green = 1; red = 1; }
443
444 do
445 {
446 count = 0;
447 strcpy(message, _("Please choose the interface you wish to change.\n\n"));
448
449 if (green) {
450 strcpy(MenuInhalt[count], "GREEN");
451 pMenuInhalt[count] = MenuInhalt[count];
452 NicEntry[_GREEN_CARD_] = count;
453 sprintf(temp, "%-6s: %s\n", "GREEN", knics[_GREEN_CARD_].description);
454 strcat(message, temp);
455 if ( strlen(knics[_GREEN_CARD_].macaddr) ) {
456 sprintf(temp, "%-6s: (%s)\n", "GREEN", knics[_GREEN_CARD_].macaddr);
457 strcat(message, temp);
458 }
459 count++;
460 }
461
462 if (red) {
463 strcpy(MenuInhalt[count], "RED");
464 pMenuInhalt[count] = MenuInhalt[count];
465 NicEntry[_RED_CARD_] = count;
466 sprintf(temp, "%-6s: %s\n", "RED", knics[_RED_CARD_].description);
467 strcat(message, temp);
468 if ( strlen(knics[_RED_CARD_].macaddr) ) {
469 sprintf(temp, "%-6s: (%s)\n", "RED", knics[_RED_CARD_].macaddr);
470 strcat(message, temp);
471 }
472 count++;
473 }
474
475 if (orange) {
476 strcpy(MenuInhalt[count], "ORANGE");
477 pMenuInhalt[count] = MenuInhalt[count];
478 NicEntry[_ORANGE_CARD_] = count;
479 sprintf(temp, "%-6s: %s\n", "ORANGE", knics[_ORANGE_CARD_].description);
480 strcat(message, temp);
481 if ( strlen(knics[_ORANGE_CARD_].macaddr) ) {
482 sprintf(temp, "%-6s: (%s)\n", "ORANGE", knics[_ORANGE_CARD_].macaddr);
483 strcat(message, temp);
484 }
485 count++;
486 }
487
488 if (blue) {
489 strcpy(MenuInhalt[count], "BLUE");
490 pMenuInhalt[count] = MenuInhalt[count];
491 NicEntry[_BLUE_CARD_] = count;
492 sprintf(temp, "%-6s: %s\n", "BLUE", knics[_BLUE_CARD_].description);
493 strcat(message, temp);
494 if ( strlen(knics[_BLUE_CARD_].macaddr) ) {
495 sprintf(temp, "%-6s: (%s)\n", "BLUE", knics[_BLUE_CARD_].macaddr);
496 strcat(message, temp);
497 }
498 count++;
499 }
500 pMenuInhalt[count] = NULL;
501
502 rc = newtWinMenu(_("Assigned Cards"), message, 70, 5, 5, 6, pMenuInhalt,
503 &choise, _("Select"), _("Remove"), _("Done"), NULL);
504
505 if ( rc == 0 || rc == 1) {
506 if ((green) && ( choise == NicEntry[0])) nicmenu(_GREEN_CARD_);
507 if ((red) && ( choise == NicEntry[1])) nicmenu(_RED_CARD_);
508 if ((orange) && ( choise == NicEntry[2])) nicmenu(_ORANGE_CARD_);
509 if ((blue) && ( choise == NicEntry[3])) nicmenu(_BLUE_CARD_);
510 netaddresschange = 1;
511 } else if (rc == 2) {
512 if ((green) && ( choise == NicEntry[0])) ask_clear_card_entry(_GREEN_CARD_);
513 if ((red) && ( choise == NicEntry[1])) ask_clear_card_entry(_RED_CARD_);
514 if ((orange) && ( choise == NicEntry[2])) ask_clear_card_entry(_ORANGE_CARD_);
515 if ((blue) && ( choise == NicEntry[3])) ask_clear_card_entry(_BLUE_CARD_);
516 netaddresschange = 1;
517 }
518 }
519 while ( rc <= 2);
520
521 freekeyvalues(kv);
522 return 1;
523 }
524
525 // Let user change GREEN address.
526 int greenaddressmenu(void)
527 {
528 struct keyvalue *kv = initkeyvalues();
529 char message[1000];
530 int rc;
531
532 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
533 {
534 freekeyvalues(kv);
535 errorbox(_("Unable to open settings file"));
536 return 0;
537 }
538
539 sprintf(message, _("If you change this IP address, and you are logged in remotely, "
540 "your connection to the %s machine will be broken, and you will have to reconnect "
541 "on the new IP. This is a risky operation, and should only be attempted if you "
542 "have physical access to the machine, should something go wrong."), NAME);
543 rc = newtWinChoice(_("Warning"), _("OK"), _("Cancel"), message);
544
545 if (rc == 0 || rc == 1)
546 {
547 if (changeaddress(kv, "GREEN", 0, ""))
548 {
549 netaddresschange = 1;
550 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
551 writehostsfiles();
552 }
553 }
554
555 freekeyvalues(kv);
556
557 return 0;
558 }
559
560 // They can change BLUE, ORANGE and GREEN too :)
561 int addressesmenu(void)
562 {
563 struct keyvalue *kv = initkeyvalues();
564 struct keyvalue *mainkv = initkeyvalues();
565 int rc = 0;
566 char *sections[5];
567 char *green = "GREEN";
568 char *orange = "ORANGE";
569 char *blue = "BLUE";
570 char *red = "RED";
571 int c = 0;
572 char greenaddress[STRING_SIZE];
573 char oldgreenaddress[STRING_SIZE];
574 char temp[STRING_SIZE];
575 char temp2[STRING_SIZE];
576 char message[1000];
577 int configtype;
578 int done;
579 int choice;
580 char hostname[STRING_SIZE];
581
582 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
583 {
584 freekeyvalues(kv);
585 freekeyvalues(mainkv);
586 errorbox(_("Unable to open settings file"));
587 return 0;
588 }
589 if (!(readkeyvalues(mainkv, CONFIG_ROOT "/main/settings")))
590 {
591 freekeyvalues(kv);
592 freekeyvalues(mainkv);
593 errorbox(_("Unable to open settings file"));
594 return 0;
595 }
596
597 strcpy(temp, "0"); findkey(kv, "CONFIG_TYPE", temp);
598 configtype = atol(temp);
599
600 sections[c] = green;
601 c++;
602 if (HAS_BLUE)
603 {
604 sections[c] = blue;
605 c++;
606 }
607 if (HAS_ORANGE)
608 {
609 sections[c] = orange;
610 c++;
611 }
612 if (HAS_RED)
613 {
614 sections[c] = red;
615 c++;
616 }
617 sections[c] = NULL;
618
619 choice = 0;
620 done = 0;
621 while (!done)
622 {
623 rc = newtWinMenu(_("Address settings"),
624 _("Select the interface you wish to reconfigure."), 50, 5,
625 5, 6, sections, &choice, _("OK"), _("Done"), NULL);
626
627 if (rc == 0 || rc == 1)
628 {
629 if (strcmp(sections[choice], "GREEN") == 0)
630 {
631 findkey(kv, "GREEN_ADDRESS", oldgreenaddress);
632 sprintf(message, _("If you change this IP address, and you are logged in remotely, "
633 "your connection to the %s machine will be broken, and you will have to reconnect "
634 "on the new IP. This is a risky operation, and should only be attempted if you "
635 "have physical access to the machine, should something go wrong."), NAME);
636 rc = newtWinChoice(_("Warning"), _("OK"), _("Cancel"), message);
637 if (rc == 0 || rc == 1)
638 {
639 if (changeaddress(kv, "GREEN", 0, ""))
640 {
641 netaddresschange = 1;
642 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
643 writehostsfiles();
644 findkey(kv, "GREEN_ADDRESS", greenaddress);
645 snprintf(temp, STRING_SIZE-1, "option routers %s", oldgreenaddress);
646 snprintf(temp2, STRING_SIZE-1, "option routers %s", greenaddress);
647 replace (CONFIG_ROOT "/dhcp/dhcpd.conf", temp, temp2);
648 chown (CONFIG_ROOT "/dhcp/dhcpd.conf", 99, 99);
649 }
650 }
651 }
652 if (strcmp(sections[choice], "BLUE") == 0)
653 {
654 if (changeaddress(kv, "BLUE", 0, ""))
655 netaddresschange = 1;
656 }
657 if (strcmp(sections[choice], "ORANGE") == 0)
658 {
659 if (changeaddress(kv, "ORANGE", 0, ""))
660 netaddresschange = 1;
661 }
662 if (strcmp(sections[choice], "RED") == 0)
663 {
664 strcpy(hostname, "");
665 findkey(mainkv, "HOSTNAME", hostname);
666 if (changeaddress(kv, "RED", 1, hostname))
667 netaddresschange = 1;
668 }
669 }
670 else
671 done = 1;
672 }
673
674 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
675 freekeyvalues(kv);
676 freekeyvalues(mainkv);
677
678 return 0;
679 }