]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/setup/networking.c
8d398f365fac9662140e365203a6c058f23e8b16
[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 changedrivers(void)
407 {
408 struct keyvalue *kv = initkeyvalues();
409 char temp[STRING_SIZE], message[STRING_SIZE];
410 int configtype;
411 int green = 0, red = 0, blue = 0, orange = 0;
412 char MenuInhalt[10][180];
413 char *pMenuInhalt[10];
414 int count = 0, choise = 0, rc;
415 int NicEntry[10];
416
417 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
418 {
419 freekeyvalues(kv);
420 errorbox(_("Unable to open settings file"));
421 return 0;
422 }
423 if (automode == 0)
424 runcommandwithstatus("/etc/rc.d/init.d/network stop red blue orange",
425 _("Networking"), _("Restarting non-local network..."), NULL);
426
427 findkey(kv, "CONFIG_TYPE", temp); configtype = atol(temp);
428 if (configtype == 1)
429 { green = 1; red = 1; }
430 else if (configtype == 2)
431 { green = 1; red = 1; orange = 1; }
432 else if (configtype == 3)
433 { green = 1; red = 1; blue = 1; }
434 else if (configtype == 4)
435 { green = 1; red=1; orange=1; blue = 1; }
436 else
437 { green = 1; red = 1; }
438
439 do
440 {
441 count = 0;
442 strcpy(message, _("Please choose the interface you wish to change.\n\n"));
443
444 if (green) {
445 strcpy(MenuInhalt[count], "GREEN");
446 pMenuInhalt[count] = MenuInhalt[count];
447 NicEntry[_GREEN_CARD_] = count;
448 sprintf(temp, "%-6s: %s\n", "GREEN", knics[_GREEN_CARD_].description);
449 strcat(message, temp);
450 if ( strlen(knics[_GREEN_CARD_].macaddr) ) {
451 sprintf(temp, "%-6s: (%s)\n", "GREEN", knics[_GREEN_CARD_].macaddr);
452 strcat(message, temp);
453 }
454 count++;
455 }
456
457 if (red) {
458 strcpy(MenuInhalt[count], "RED");
459 pMenuInhalt[count] = MenuInhalt[count];
460 NicEntry[_RED_CARD_] = count;
461 sprintf(temp, "%-6s: %s\n", "RED", knics[_RED_CARD_].description);
462 strcat(message, temp);
463 if ( strlen(knics[_RED_CARD_].macaddr) ) {
464 sprintf(temp, "%-6s: (%s)\n", "RED", knics[_RED_CARD_].macaddr);
465 strcat(message, temp);
466 }
467 count++;
468 }
469
470 if (orange) {
471 strcpy(MenuInhalt[count], "ORANGE");
472 pMenuInhalt[count] = MenuInhalt[count];
473 NicEntry[_ORANGE_CARD_] = count;
474 sprintf(temp, "%-6s: %s\n", "ORANGE", knics[_ORANGE_CARD_].description);
475 strcat(message, temp);
476 if ( strlen(knics[_ORANGE_CARD_].macaddr) ) {
477 sprintf(temp, "%-6s: (%s)\n", "ORANGE", knics[_ORANGE_CARD_].macaddr);
478 strcat(message, temp);
479 }
480 count++;
481 }
482
483 if (blue) {
484 strcpy(MenuInhalt[count], "BLUE");
485 pMenuInhalt[count] = MenuInhalt[count];
486 NicEntry[_BLUE_CARD_] = count;
487 sprintf(temp, "%-6s: %s\n", "BLUE", knics[_BLUE_CARD_].description);
488 strcat(message, temp);
489 if ( strlen(knics[_BLUE_CARD_].macaddr) ) {
490 sprintf(temp, "%-6s: (%s)\n", "BLUE", knics[_BLUE_CARD_].macaddr);
491 strcat(message, temp);
492 }
493 count++;
494 }
495 pMenuInhalt[count] = NULL;
496
497 rc = newtWinMenu(_("Assigned Cards"), message, 70, 5, 5, 6, pMenuInhalt,
498 &choise, _("Select"), _("Remove"), _("Done"), NULL);
499
500 if ( rc == 0 || rc == 1) {
501 if ((green) && ( choise == NicEntry[0])) nicmenu(_GREEN_CARD_);
502 if ((red) && ( choise == NicEntry[1])) nicmenu(_RED_CARD_);
503 if ((orange) && ( choise == NicEntry[2])) nicmenu(_ORANGE_CARD_);
504 if ((blue) && ( choise == NicEntry[3])) nicmenu(_BLUE_CARD_);
505 netaddresschange = 1;
506 } else if (rc == 2) {
507 if ((green) && ( choise == NicEntry[0])) ask_clear_card_entry(_GREEN_CARD_);
508 if ((red) && ( choise == NicEntry[1])) ask_clear_card_entry(_RED_CARD_);
509 if ((orange) && ( choise == NicEntry[2])) ask_clear_card_entry(_ORANGE_CARD_);
510 if ((blue) && ( choise == NicEntry[3])) ask_clear_card_entry(_BLUE_CARD_);
511 netaddresschange = 1;
512 }
513 }
514 while ( rc <= 2);
515
516 freekeyvalues(kv);
517 return 1;
518 }
519
520 // Let user change GREEN address.
521 int greenaddressmenu(void)
522 {
523 struct keyvalue *kv = initkeyvalues();
524 char message[1000];
525 int rc;
526
527 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
528 {
529 freekeyvalues(kv);
530 errorbox(_("Unable to open settings file"));
531 return 0;
532 }
533
534 sprintf(message, _("If you change this IP address, and you are logged in remotely, "
535 "your connection to the %s machine will be broken, and you will have to reconnect "
536 "on the new IP. This is a risky operation, and should only be attempted if you "
537 "have physical access to the machine, should something go wrong."), NAME);
538 rc = newtWinChoice(_("Warning"), _("OK"), _("Cancel"), message);
539
540 if (rc == 0 || rc == 1)
541 {
542 if (changeaddress(kv, "GREEN", 0, ""))
543 {
544 netaddresschange = 1;
545 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
546 writehostsfiles();
547 }
548 }
549
550 freekeyvalues(kv);
551
552 return 0;
553 }
554
555 // They can change BLUE, ORANGE and GREEN too :)
556 int addressesmenu(void)
557 {
558 struct keyvalue *kv = initkeyvalues();
559 struct keyvalue *mainkv = initkeyvalues();
560 int rc = 0;
561 char *sections[5];
562 char *green = "GREEN";
563 char *orange = "ORANGE";
564 char *blue = "BLUE";
565 char *red = "RED";
566 int c = 0;
567 char greenaddress[STRING_SIZE];
568 char oldgreenaddress[STRING_SIZE];
569 char temp[STRING_SIZE];
570 char temp2[STRING_SIZE];
571 char message[1000];
572 int configtype;
573 int done;
574 int choice;
575 char hostname[STRING_SIZE];
576
577 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
578 {
579 freekeyvalues(kv);
580 freekeyvalues(mainkv);
581 errorbox(_("Unable to open settings file"));
582 return 0;
583 }
584 if (!(readkeyvalues(mainkv, CONFIG_ROOT "/main/settings")))
585 {
586 freekeyvalues(kv);
587 freekeyvalues(mainkv);
588 errorbox(_("Unable to open settings file"));
589 return 0;
590 }
591
592 strcpy(temp, "0"); findkey(kv, "CONFIG_TYPE", temp);
593 configtype = atol(temp);
594
595 sections[c] = green;
596 c++;
597 if (HAS_BLUE)
598 {
599 sections[c] = blue;
600 c++;
601 }
602 if (HAS_ORANGE)
603 {
604 sections[c] = orange;
605 c++;
606 }
607 if (HAS_RED)
608 {
609 sections[c] = red;
610 c++;
611 }
612 sections[c] = NULL;
613
614 choice = 0;
615 done = 0;
616 while (!done)
617 {
618 rc = newtWinMenu(_("Address settings"),
619 _("Select the interface you wish to reconfigure."), 50, 5,
620 5, 6, sections, &choice, _("OK"), _("Done"), NULL);
621
622 if (rc == 0 || rc == 1)
623 {
624 if (strcmp(sections[choice], "GREEN") == 0)
625 {
626 findkey(kv, "GREEN_ADDRESS", oldgreenaddress);
627 sprintf(message, _("If you change this IP address, and you are logged in remotely, "
628 "your connection to the %s machine will be broken, and you will have to reconnect "
629 "on the new IP. This is a risky operation, and should only be attempted if you "
630 "have physical access to the machine, should something go wrong."), NAME);
631 rc = newtWinChoice(_("Warning"), _("OK"), _("Cancel"), message);
632 if (rc == 0 || rc == 1)
633 {
634 if (changeaddress(kv, "GREEN", 0, ""))
635 {
636 netaddresschange = 1;
637 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
638 writehostsfiles();
639 findkey(kv, "GREEN_ADDRESS", greenaddress);
640 snprintf(temp, STRING_SIZE-1, "option routers %s", oldgreenaddress);
641 snprintf(temp2, STRING_SIZE-1, "option routers %s", greenaddress);
642 replace (CONFIG_ROOT "/dhcp/dhcpd.conf", temp, temp2);
643 chown (CONFIG_ROOT "/dhcp/dhcpd.conf", 99, 99);
644 }
645 }
646 }
647 if (strcmp(sections[choice], "BLUE") == 0)
648 {
649 if (changeaddress(kv, "BLUE", 0, ""))
650 netaddresschange = 1;
651 }
652 if (strcmp(sections[choice], "ORANGE") == 0)
653 {
654 if (changeaddress(kv, "ORANGE", 0, ""))
655 netaddresschange = 1;
656 }
657 if (strcmp(sections[choice], "RED") == 0)
658 {
659 strcpy(hostname, "");
660 findkey(mainkv, "HOSTNAME", hostname);
661 if (changeaddress(kv, "RED", 1, hostname))
662 netaddresschange = 1;
663 }
664 }
665 else
666 done = 1;
667 }
668
669 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
670 freekeyvalues(kv);
671 freekeyvalues(mainkv);
672
673 return 0;
674 }