]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/install+setup/setup/networking.c
Padlock-Modul aktiviert fuer VIA Epia Boards
[people/pmueller/ipfire-2.x.git] / src / install+setup / 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 * $Id: networking.c,v 1.5.2.6 2006/02/06 22:00:13 gespinasse Exp $
10 *
11 */
12
13 #include "setup.h"
14
15 #define DNS1 0
16 #define DNS2 1
17 #define DEFAULT_GATEWAY 2
18 #define DNSGATEWAY_TOTAL 3
19
20 extern FILE *flog;
21 extern char *mylog;
22
23 extern char **ctr;
24
25 extern int automode;
26
27 #define HAS_ORANGE (configtype == 1 || configtype == 3 || configtype == 5 || configtype == 7)
28 #define HAS_RED (configtype == 2 || configtype == 3 || configtype == 6 || configtype == 7)
29 #define HAS_BLUE (configtype == 4 || configtype == 5 || configtype == 6 || configtype == 7)
30 #define RED_IS_NOT_ETH (configtype == 0 || configtype == 1 || configtype == 4 || configtype == 5)
31
32 extern struct nic nics[];
33
34 char *configtypenames[] = {
35 "GREEN (RED is modem/ISDN)",
36 "GREEN + ORANGE (RED is modem/ISDN)",
37 "GREEN + RED",
38 "GREEN + ORANGE + RED",
39 "GREEN + BLUE (RED is modem/ISDN) ",
40 "GREEN + ORANGE + BLUE (RED is modem/ISDN)",
41 "GREEN + BLUE + RED",
42 "GREEN + ORANGE + BLUE + RED",
43 NULL };
44 int netaddresschange;
45
46 int oktoleave(char *errormessage);
47 int firstmenu(void);
48 int configtypemenu(void);
49 int drivermenu(void);
50 int changedrivers(void);
51 int greenaddressmenu(void);
52 int addressesmenu(void);
53 int dnsgatewaymenu(void);
54
55 int handlenetworking(void)
56 {
57 int done;
58 int choice;
59 char errormessage[STRING_SIZE];
60
61 netaddresschange = 0;
62
63 done = 0;
64 while (!done)
65 {
66 choice = firstmenu();
67
68 switch (choice)
69 {
70 case 1:
71 configtypemenu();
72 break;
73
74 case 2:
75 drivermenu();
76 break;
77
78 case 3:
79 addressesmenu();
80 break;
81
82 case 4:
83 dnsgatewaymenu();
84 break;
85
86 case 0:
87 if (oktoleave(errormessage))
88 done = 1;
89 else
90 errorbox(errormessage);
91 break;
92
93 default:
94 break;
95 }
96 }
97
98 if (automode == 0)
99 {
100 /* Restart networking! */
101 if (netaddresschange)
102 {
103 runcommandwithstatus("/etc/rc.d/init.d/network stop",
104 ctr[TR_PUSHING_NETWORK_DOWN]);
105 runcommandwithstatus("/etc/rc.d/init.d/network start",
106 ctr[TR_PULLING_NETWORK_UP]);
107 }
108 }
109
110 return 1;
111 }
112
113 int oktoleave(char *errormessage)
114 {
115 struct keyvalue *kv = initkeyvalues();
116 char temp[STRING_SIZE];
117 int configtype;
118
119 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
120 {
121 freekeyvalues(kv);
122 errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]);
123 return 0;
124 }
125
126 strcpy(temp, "0"); findkey(kv, "CONFIG_TYPE", temp); configtype = atol(temp);
127 if (configtype < 0 || configtype > 7) configtype = 0;
128
129 if (HAS_BLUE)
130 {
131 strcpy(temp, ""); findkey(kv, "BLUE_DEV", temp);
132 if (!(strlen(temp)))
133 {
134 strcpy(errormessage, ctr[TR_NO_BLUE_INTERFACE]);
135 goto EXIT;
136 }
137 if (!(interfacecheck(kv, "BLUE")))
138 {
139 strcpy(errormessage, ctr[TR_MISSING_BLUE_IP]);
140 goto EXIT;
141 }
142 }
143 if (HAS_ORANGE)
144 {
145 strcpy(temp, ""); findkey(kv, "ORANGE_DEV", temp);
146 if (!(strlen(temp)))
147 {
148 strcpy(errormessage, ctr[TR_NO_ORANGE_INTERFACE]);
149 goto EXIT;
150 }
151 if (!(interfacecheck(kv, "ORANGE")))
152 {
153 strcpy(errormessage, ctr[TR_MISSING_ORANGE_IP]);
154 goto EXIT;
155 }
156 }
157 if (HAS_RED)
158 {
159 strcpy(temp, ""); findkey(kv, "RED_DEV", temp);
160 if (!(strlen(temp)))
161 {
162 strcpy(errormessage, ctr[TR_NO_RED_INTERFACE]);
163 goto EXIT;
164 }
165 if (!(interfacecheck(kv, "RED")))
166 {
167 strcpy(errormessage, ctr[TR_MISSING_RED_IP]);
168 goto EXIT;
169 }
170 }
171 strcpy(errormessage, "");
172 EXIT:
173 freekeyvalues(kv);
174
175 if (strlen(errormessage))
176 return 0;
177 else
178 return 1;
179 }
180
181
182 /* Shows the main menu and a summary of the current settings. */
183 int firstmenu(void)
184 {
185 char *sections[] = { ctr[TR_NETWORK_CONFIGURATION_TYPE],
186 ctr[TR_DRIVERS_AND_CARD_ASSIGNMENTS],
187 ctr[TR_ADDRESS_SETTINGS],
188 ctr[TR_DNS_AND_GATEWAY_SETTINGS], NULL };
189 int rc;
190 static int choice = 0;
191 struct keyvalue *kv = initkeyvalues();
192 char message[1000];
193 char temp[STRING_SIZE];
194 int x;
195 int result;
196 char networkrestart[STRING_SIZE] = "";
197
198 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
199 {
200 freekeyvalues(kv);
201 errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]);
202 return 0;
203 }
204
205 if (netaddresschange)
206 strcpy(networkrestart, ctr[TR_RESTART_REQUIRED]);
207
208 strcpy(temp, ""); findkey(kv, "CONFIG_TYPE", temp); x = atol(temp);
209 if (x < 0 || x > 7) x = 0;
210 /* Format heading bit. */
211 snprintf(message, 1000, ctr[TR_CURRENT_CONFIG], configtypenames[x],
212 networkrestart);
213 rc = newtWinMenu(ctr[TR_NETWORK_CONFIGURATION_MENU], message, 50, 5, 5, 6,
214 sections, &choice, ctr[TR_OK], ctr[TR_DONE], NULL);
215
216 if (rc == 0 || rc == 1)
217 result = choice + 1;
218 else
219 result = 0;
220
221 return result;
222 }
223
224 /* Here they choose general network config, number of nics etc. */
225 int configtypemenu(void)
226 {
227 struct keyvalue *kv = initkeyvalues();
228 char temp[STRING_SIZE] = "0";
229 char message[1000];
230 int choice;
231 int rc;
232
233 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
234 {
235 freekeyvalues(kv);
236 errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]);
237 return 0;
238 }
239
240 findkey(kv, "CONFIG_TYPE", temp); choice = atol(temp);
241 sprintf(message, ctr[TR_NETWORK_CONFIGURATION_TYPE_LONG], NAME);
242 rc = newtWinMenu(ctr[TR_NETWORK_CONFIGURATION_TYPE], message, 50, 5, 5,
243 6, configtypenames, &choice, ctr[TR_OK], ctr[TR_CANCEL], NULL);
244
245 if (rc == 0 || rc == 1)
246 {
247 runcommandwithstatus("/etc/rc.d/init.d/network stop red blue orange",
248 ctr[TR_PUSHING_NON_LOCAL_NETWORK_DOWN]);
249
250 sprintf(temp, "%d", choice);
251 replacekeyvalue(kv, "CONFIG_TYPE", temp);
252 replacekeyvalue(kv, "ORANGE_DEV", "");
253 replacekeyvalue(kv, "BLUE_DEV", "");
254 replacekeyvalue(kv, "RED_DEV", "");
255 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
256 netaddresschange = 1;
257 }
258
259 freekeyvalues(kv);
260
261 return 0;
262 }
263
264 /* Driver menu. Choose drivers.. */
265 int drivermenu(void)
266 {
267 FILE *fp;
268 struct keyvalue *kv = initkeyvalues();
269 char message[1000], macaddr[STRING_SIZE];
270 char temp_line[STRING_SIZE];
271 char temp[STRING_SIZE], temp1[STRING_SIZE];
272 struct knic knics[20], *pknics;
273 pknics = knics;
274 int configtype;
275 int rc, i = 0, kcount = 0;
276
277 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
278 {
279 freekeyvalues(kv);
280 errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]);
281 return 0;
282 }
283
284 strcpy(temp, "0"); findkey(kv, "CONFIG_TYPE", temp);
285 configtype = atol(temp);
286
287 if (configtype == 0)
288 {
289 freekeyvalues(kv);
290 errorbox(ctr[TR_YOUR_CONFIGURATION_IS_SINGLE_GREEN_ALREADY_HAS_DRIVER]);
291 return 0;
292 }
293
294 strcpy(message, ctr[TR_CONFIGURE_NETWORK_DRIVERS]);
295
296 if( (fp = fopen(KNOWN_NICS, "r")) == NULL )
297 {
298 fprintf(flog,"Couldn't open " KNOWN_NICS);
299 return 1;
300 }
301 while (fgets(temp_line, STRING_SIZE, fp) != NULL)
302 {
303 strcpy(knics[kcount].description, strtok(temp_line,";"));
304 strcpy(knics[kcount].macaddr , strtok(NULL,";"));
305 if (strlen(knics[kcount].macaddr) > 5 ) kcount++;
306 }
307 fclose(fp);
308
309 strcpy(macaddr, ctr[TR_UNSET]);
310 findkey(kv, "GREEN_MACADDR", macaddr);
311 for (i=0; i < kcount; i++)
312 { // Check if the nic is already in use
313 if (strcmp(pknics[i].macaddr, macaddr) == NULL )
314 break;
315 }
316 sprintf(temp1, "GREEN: %s (%s / green0)\n", pknics[i].description, pknics[i].macaddr);
317 strcat(message, temp1);
318
319 if (HAS_BLUE) {
320 strcpy(macaddr, ctr[TR_UNSET]);
321 findkey(kv, "BLUE_MACADDR", macaddr);
322 for (i=0; i < kcount; i++)
323 { // Check if the nic is already in use
324 if (strcmp(pknics[i].macaddr, macaddr) == NULL )
325 break;
326 }
327 sprintf(temp1, "BLUE: %s (%s / blue0)\n", pknics[i].description, pknics[i].macaddr);
328 strcat(message, temp1);
329 }
330 if (HAS_ORANGE) {
331 strcpy(macaddr, ctr[TR_UNSET]);
332 findkey(kv, "ORANGE_MACADDR", macaddr);
333 for (i=0; i < kcount; i++)
334 { // Check if the nic is already in use
335 if (strcmp(pknics[i].macaddr, macaddr) == NULL )
336 break;
337 }
338 sprintf(temp1, "ORANGE: %s (%s / orange0)\n", pknics[i].description, pknics[i].macaddr);
339 strcat(message, temp1);
340 }
341 if (HAS_RED) {
342 strcpy(macaddr, ctr[TR_UNSET]);
343 findkey(kv, "RED_MACADDR", macaddr);
344 for (i=0; i < kcount; i++)
345 { // Check if the nic is already in use
346 if (strcmp(pknics[i].macaddr, macaddr) == NULL )
347 break;
348 }
349 sprintf(temp1, "RED: %s (%s / red0)\n", pknics[i].description, pknics[i].macaddr);
350 strcat(message, temp1);
351 }
352 strcat(message, ctr[TR_DO_YOU_WISH_TO_CHANGE_THESE_SETTINGS]);
353 rc = newtWinChoice(ctr[TR_DRIVERS_AND_CARD_ASSIGNMENTS], ctr[TR_OK],
354 ctr[TR_CANCEL], message);
355 if (rc == 0 || rc == 1)
356 {
357 /* Shit, got to do something.. */
358 changedrivers();
359 }
360
361 freekeyvalues(kv);
362
363 return 1;
364 }
365
366 int cardassigned(char *colour)
367 {
368 char command[STRING_SIZE];
369 sprintf(command, "grep -q %s < /etc/udev/rules.d/30-persistent-network.rules 2>/dev/null", colour);
370 if (system(command))
371 return 0;
372 else
373 return 1;
374 }
375
376 int changedrivers(void)
377 {
378 struct keyvalue *kv = initkeyvalues();
379 char temp[STRING_SIZE];
380 int configtype;
381 int green = 0, red = 0, blue = 0, orange = 0;
382
383 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
384 {
385 freekeyvalues(kv);
386 errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]);
387 return 0;
388 }
389
390 runcommandwithstatus("/etc/rc.d/init.d/network stop red blue orange",
391 ctr[TR_PUSHING_NON_LOCAL_NETWORK_DOWN]);
392
393 if (configtype == 0)
394 { green = 1; }
395 else if (configtype == 1)
396 { green = 1; orange = 1; }
397 else if (configtype == 2)
398 { green = 1; red = 1; }
399 else if (configtype == 3)
400 { green = 1; red = 1; orange = 1; }
401 else if (configtype == 4)
402 { green = 1; blue = 1; }
403 else if (configtype == 5)
404 { green = 1; blue = 1; orange = 1; }
405 else if (configtype == 6)
406 { green = 1; red = 1; blue = 1; }
407 else if (configtype == 7)
408 { green = 1; red = 1; blue = 1; orange = 1; }
409
410 if (green && !cardassigned("green"))
411 nicmenu("green");
412 if (red && !cardassigned("red"))
413 nicmenu("red");
414 if (blue && !cardassigned("blue"))
415 nicmenu("blue");
416 if (orange && !cardassigned("orange"))
417 nicmenu("orange");
418
419 // writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
420
421 freekeyvalues(kv);
422 return 1;
423 }
424
425 // Let user change GREEN address.
426 int greenaddressmenu(void)
427 {
428 struct keyvalue *kv = initkeyvalues();
429 char message[1000];
430 int rc;
431
432 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
433 {
434 freekeyvalues(kv);
435 errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]);
436 return 0;
437 }
438
439 sprintf(message, ctr[TR_WARNING_LONG], NAME);
440 rc = newtWinChoice(ctr[TR_WARNING], ctr[TR_OK], ctr[TR_CANCEL], message);
441
442 if (rc == 0 || rc == 1)
443 {
444 if (changeaddress(kv, "GREEN", 0, ""))
445 {
446 netaddresschange = 1;
447 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
448 writehostsfiles();
449 }
450 }
451
452 freekeyvalues(kv);
453
454 return 0;
455 }
456
457 // They can change BLUE, ORANGE and GREEN too :)
458 int addressesmenu(void)
459 {
460 struct keyvalue *kv = initkeyvalues();
461 struct keyvalue *mainkv = initkeyvalues();
462 int rc = 0;
463 char *sections[5];
464 char *green = "GREEN";
465 char *orange = "ORANGE";
466 char *blue = "BLUE";
467 char *red = "RED";
468 int c = 0;
469 char greenaddress[STRING_SIZE];
470 char oldgreenaddress[STRING_SIZE];
471 char temp[STRING_SIZE];
472 char temp2[STRING_SIZE];
473 char message[1000];
474 int configtype;
475 int done;
476 int choice;
477 char hostname[STRING_SIZE];
478
479 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
480 {
481 freekeyvalues(kv);
482 freekeyvalues(mainkv);
483 errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]);
484 return 0;
485 }
486 if (!(readkeyvalues(mainkv, CONFIG_ROOT "/main/settings")))
487 {
488 freekeyvalues(kv);
489 freekeyvalues(mainkv);
490 errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]);
491 return 0;
492 }
493
494 strcpy(temp, "0"); findkey(kv, "CONFIG_TYPE", temp);
495 configtype = atol(temp);
496
497 sections[c] = green;
498 c++;
499 if (HAS_BLUE)
500 {
501 sections[c] = blue;
502 c++;
503 }
504 if (HAS_ORANGE)
505 {
506 sections[c] = orange;
507 c++;
508 }
509 if (HAS_RED)
510 {
511 sections[c] = red;
512 c++;
513 }
514 sections[c] = NULL;
515
516 choice = 0;
517 done = 0;
518 while (!done)
519 {
520 rc = newtWinMenu(ctr[TR_ADDRESS_SETTINGS],
521 ctr[TR_SELECT_THE_INTERFACE_YOU_WISH_TO_RECONFIGURE], 50, 5,
522 5, 6, sections, &choice, ctr[TR_OK], ctr[TR_DONE], NULL);
523
524 if (rc == 0 || rc == 1)
525 {
526 if (strcmp(sections[choice], "GREEN") == 0)
527 {
528 findkey(kv, "GREEN_ADDRESS", oldgreenaddress);
529 sprintf(message, ctr[TR_WARNING_LONG], NAME);
530 rc = newtWinChoice(ctr[TR_WARNING], ctr[TR_OK], ctr[TR_CANCEL],
531 message);
532 if (rc == 0 || rc == 1)
533 {
534 if (changeaddress(kv, "GREEN", 0, ""))
535 {
536 netaddresschange = 1;
537 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
538 writehostsfiles();
539 findkey(kv, "GREEN_ADDRESS", greenaddress);
540 snprintf(temp, STRING_SIZE-1, "option routers %s", oldgreenaddress);
541 snprintf(temp2, STRING_SIZE-1, "option routers %s", greenaddress);
542 replace (CONFIG_ROOT "/dhcp/dhcpd.conf", temp, temp2);
543 chown (CONFIG_ROOT "/dhcp/dhcpd.conf", 99, 99);
544 }
545 }
546 }
547 if (strcmp(sections[choice], "BLUE") == 0)
548 {
549 if (changeaddress(kv, "BLUE", 0, ""))
550 netaddresschange = 1;
551 }
552 if (strcmp(sections[choice], "ORANGE") == 0)
553 {
554 if (changeaddress(kv, "ORANGE", 0, ""))
555 netaddresschange = 1;
556 }
557 if (strcmp(sections[choice], "RED") == 0)
558 {
559 strcpy(hostname, "");
560 findkey(mainkv, "HOSTNAME", hostname);
561 if (changeaddress(kv, "RED", 1, hostname))
562 netaddresschange = 1;
563 }
564 }
565 else
566 done = 1;
567 }
568
569 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
570 freekeyvalues(kv);
571 freekeyvalues(mainkv);
572
573 return 0;
574 }
575
576 /* DNS and default gateway.... */
577 int dnsgatewaymenu(void)
578 {
579 struct keyvalue *kv = initkeyvalues();
580 char message[1000];
581 char temp[STRING_SIZE] = "0";
582 struct newtWinEntry entries[DNSGATEWAY_TOTAL+1];
583 char *values[DNSGATEWAY_TOTAL]; /* pointers for the values. */
584 int error;
585 int configtype;
586 int rc;
587
588 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
589 {
590 freekeyvalues(kv);
591 errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]);
592 return 0;
593 }
594
595 strcpy(temp, "0"); findkey(kv, "CONFIG_TYPE", temp);
596 configtype = atol(temp);
597
598 if (RED_IS_NOT_ETH)
599 {
600 freekeyvalues(kv);
601 errorbox(ctr[TR_DNS_GATEWAY_WITH_GREEN]);
602 return 0;
603 }
604
605 entries[DNS1].text = ctr[TR_PRIMARY_DNS];
606 strcpy(temp, ""); findkey(kv, "DNS1", temp);
607 values[DNS1] = strdup(temp);
608 entries[DNS1].value = &values[DNS1];
609 entries[DNS1].flags = 0;
610
611 entries[DNS2].text = ctr[TR_SECONDARY_DNS];
612 strcpy(temp, ""); findkey(kv, "DNS2", temp);
613 values[DNS2] = strdup(temp);
614 entries[DNS2].value = &values[DNS2];
615 entries[DNS2].flags = 0;
616
617 entries[DEFAULT_GATEWAY].text = ctr[TR_DEFAULT_GATEWAY];
618 strcpy(temp, ""); findkey(kv, "DEFAULT_GATEWAY", temp);
619 values[DEFAULT_GATEWAY] = strdup(temp);
620 entries[DEFAULT_GATEWAY].value = &values[DEFAULT_GATEWAY];
621 entries[DEFAULT_GATEWAY].flags = 0;
622
623 entries[DNSGATEWAY_TOTAL].text = NULL;
624 entries[DNSGATEWAY_TOTAL].value = NULL;
625 entries[DNSGATEWAY_TOTAL].flags = 0;
626
627 do
628 {
629 error = 0;
630
631 rc = newtWinEntries(ctr[TR_DNS_AND_GATEWAY_SETTINGS],
632 ctr[TR_DNS_AND_GATEWAY_SETTINGS_LONG], 50, 5, 5, 18, entries,
633 ctr[TR_OK], ctr[TR_CANCEL], NULL);
634 if (rc == 0 || rc == 1)
635 {
636 strcpy(message, ctr[TR_INVALID_FIELDS]);
637 if (strlen(values[DNS1]))
638 {
639 if (inet_addr(values[DNS1]) == INADDR_NONE)
640 {
641 strcat(message, ctr[TR_PRIMARY_DNS_CR]);
642 error = 1;
643 }
644 }
645 if (strlen(values[DNS2]))
646 {
647 if (inet_addr(values[DNS2]) == INADDR_NONE)
648 {
649 strcat(message, ctr[TR_SECONDARY_DNS_CR]);
650 error = 1;
651 }
652 }
653 if (strlen(values[DEFAULT_GATEWAY]))
654 {
655 if (inet_addr(values[DEFAULT_GATEWAY]) == INADDR_NONE)
656 {
657 strcat(message, ctr[TR_DEFAULT_GATEWAY_CR]);
658 error = 1;
659 }
660 }
661 if (!strlen(values[DNS1]) && strlen(values[DNS2]))
662 {
663 strcpy(message, ctr[TR_SECONDARY_WITHOUT_PRIMARY_DNS]);
664 error = 1;
665 }
666
667 if (error)
668 errorbox(message);
669 else
670 {
671 replacekeyvalue(kv, "DNS1", values[DNS1]);
672 replacekeyvalue(kv, "DNS2", values[DNS2]);
673 replacekeyvalue(kv, "DEFAULT_GATEWAY", values[DEFAULT_GATEWAY]);
674 netaddresschange = 1;
675 free(values[DNS1]);
676 free(values[DNS2]);
677 free(values[DEFAULT_GATEWAY]);
678 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
679 }
680 }
681 }
682 while (error);
683
684 freekeyvalues(kv);
685
686 return 1;
687 }