]> git.ipfire.org Git - ipfire-2.x.git/blame - src/setup/networking.c
Merge remote-tracking branch 'ms/ipsec-subnets' into next
[ipfire-2.x.git] / src / setup / networking.c
CommitLineData
069680ac
MT
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 *
069680ac 9 */
e1457ba0
MT
10
11// Translation
12#include <libintl.h>
13#define _(x) dgettext("setup", x)
14
069680ac
MT
15#include "setup.h"
16
17#define DNS1 0
18#define DNS2 1
19#define DEFAULT_GATEWAY 2
20#define DNSGATEWAY_TOTAL 3
21
22extern FILE *flog;
23extern char *mylog;
24
069680ac
MT
25extern int automode;
26
5057b611 27#define HAS_GREEN 1
9b040aa0
HS
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)
32
069680ac 33extern struct nic nics[];
5057b611 34extern struct knic knics[];
069680ac 35
a358af8c 36char *configtypenames[] = {
9b040aa0
HS
37 "GREEN + RED",
38 "GREEN + RED + ORANGE",
39 "GREEN + RED + BLUE",
40 "GREEN + RED + ORANGE + BLUE",
41 NULL };
42int configtypecards[] = {
9b040aa0
HS
43 2, // "GREEN + RED",
44 3, // "GREEN + RED + ORANGE",
45 3, // "GREEN + RED + BLUE",
6fc15159 46 4 // "GREEN + RED + ORANGE + BLUE",
9b040aa0
HS
47};
48
49
069680ac
MT
50int netaddresschange;
51
855446c6 52int oktoleave(void);
069680ac
MT
53int firstmenu(void);
54int configtypemenu(void);
55int drivermenu(void);
56int changedrivers(void);
57int greenaddressmenu(void);
58int addressesmenu(void);
59int dnsgatewaymenu(void);
60
61int handlenetworking(void)
62{
63 int done;
64 int choice;
9b040aa0 65 int found;
069680ac
MT
66
67 netaddresschange = 0;
68
9b040aa0 69 found = scan_network_cards();
9c1c1c57 70 found = init_knics();
9b040aa0 71
069680ac
MT
72 done = 0;
73 while (!done)
74 {
75 choice = firstmenu();
76
77 switch (choice)
78 {
79 case 1:
80 configtypemenu();
81 break;
82
83 case 2:
84 drivermenu();
85 break;
86
87 case 3:
88 addressesmenu();
89 break;
90
91 case 4:
92 dnsgatewaymenu();
93 break;
94
95 case 0:
855446c6 96 if (oktoleave()) done = 1;
069680ac
MT
97 break;
98
99 default:
100 break;
101 }
102 }
103
104 if (automode == 0)
105 {
106 /* Restart networking! */
107 if (netaddresschange)
108 {
109 runcommandwithstatus("/etc/rc.d/init.d/network stop",
46b56e20 110 _("Networking"), _("Stopping network..."), NULL);
eea0467a
HS
111
112 rename_nics();
113
069680ac 114 runcommandwithstatus("/etc/rc.d/init.d/network start",
46b56e20 115 _("Networking"), _("Restarting network..."), NULL);
069680ac 116 }
a5d81233
AF
117 } else {
118 rename_nics();
069680ac 119 }
069680ac
MT
120 return 1;
121}
122
855446c6 123int oktoleave(void)
069680ac
MT
124{
125 struct keyvalue *kv = initkeyvalues();
126 char temp[STRING_SIZE];
127 int configtype;
855446c6 128 int rc;
069680ac
MT
129
130 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
131 {
132 freekeyvalues(kv);
e1457ba0 133 errorbox(_("Unable to open settings file"));
069680ac
MT
134 return 0;
135 }
136
0db33b56
MT
137 strcpy(temp, "1"); findkey(kv, "CONFIG_TYPE", temp); configtype = atol(temp);
138 if (configtype < 1 || configtype > 4) configtype = 1;
069680ac 139
f93a4b4f 140 if (HAS_GREEN)
069680ac 141 {
f93a4b4f 142 strcpy(temp, ""); findkey(kv, "GREEN_DEV", temp);
069680ac
MT
143 if (!(strlen(temp)))
144 {
e1457ba0 145 errorbox(_("No GREEN interface assigned."));
855446c6
MT
146 freekeyvalues(kv);
147 return 0;
069680ac 148 }
f93a4b4f 149 if (!(interfacecheck(kv, "GREEN")))
069680ac 150 {
e1457ba0 151 errorbox(_("Missing an IP address on GREEN."));
855446c6
MT
152 freekeyvalues(kv);
153 return 0;
f93a4b4f
HS
154 }
155 }
156 if (HAS_RED)
157 {
855446c6 158
f93a4b4f
HS
159 strcpy(temp, ""); findkey(kv, "RED_DEV", temp);
160 if (!(strlen(temp)))
161 {
e1457ba0
MT
162 rc = newtWinChoice(_("Error"), _("OK"), _("Ignore"),
163 _("No RED interface assigned."));
855446c6
MT
164 if (rc == 0 || rc == 1)
165 {
166 freekeyvalues(kv);
167 return 0;
168 }
f93a4b4f
HS
169 }
170 if (!(interfacecheck(kv, "RED")))
171 {
e1457ba0 172 errorbox(_("Missing an IP address on RED."));
855446c6
MT
173 freekeyvalues(kv);
174 return 0;
069680ac
MT
175 }
176 }
177 if (HAS_ORANGE)
178 {
179 strcpy(temp, ""); findkey(kv, "ORANGE_DEV", temp);
180 if (!(strlen(temp)))
181 {
e1457ba0 182 errorbox(_("No ORANGE interface assigned."));
855446c6
MT
183 freekeyvalues(kv);
184 return 0;
069680ac
MT
185 }
186 if (!(interfacecheck(kv, "ORANGE")))
187 {
e1457ba0 188 errorbox(_("Missing an IP address on ORANGE."));
855446c6
MT
189 freekeyvalues(kv);
190 return 0;
069680ac
MT
191 }
192 }
f93a4b4f 193 if (HAS_BLUE)
069680ac 194 {
f93a4b4f 195 strcpy(temp, ""); findkey(kv, "BLUE_DEV", temp);
069680ac
MT
196 if (!(strlen(temp)))
197 {
e1457ba0 198 errorbox(_("No BLUE interface assigned."));
855446c6
MT
199 freekeyvalues(kv);
200 return 0;
069680ac 201 }
f93a4b4f 202 if (!(interfacecheck(kv, "BLUE")))
069680ac 203 {
e1457ba0 204 errorbox(_("Missing an IP address on BLUE."));
855446c6
MT
205 freekeyvalues(kv);
206 return 0;
f93a4b4f
HS
207 }
208 }
b3f09dd8
MT
209
210 strcpy(temp, ""); findkey(kv, "RED_TYPE", temp);
211 if ((configtype == 0) || (strcmp(temp, "STATIC") == 0))
f93a4b4f
HS
212 {
213 strcpy(temp, ""); findkey(kv, "DNS1", temp);
214 if (!(strlen(temp)))
215 {
0a39488e 216 errorbox(_("Missing DNS."));
855446c6
MT
217 freekeyvalues(kv);
218 return 0;
f93a4b4f
HS
219 }
220 strcpy(temp, ""); findkey(kv, "DEFAULT_GATEWAY", temp);
221 if (!(strlen(temp)))
222 {
e1457ba0 223 errorbox(_("Missing Default Gateway."));
855446c6
MT
224 freekeyvalues(kv);
225 return 0;
069680ac
MT
226 }
227 }
855446c6 228 return 1;
069680ac
MT
229}
230
231
232/* Shows the main menu and a summary of the current settings. */
233int firstmenu(void)
234{
e1457ba0
MT
235 char *sections[] = {
236 _("Network configuration type"),
237 _("Drivers and card assignments"),
238 _("Address settings"),
239 _("DNS and Gateway settings"),
240 NULL
241 };
069680ac
MT
242 int rc;
243 static int choice = 0;
244 struct keyvalue *kv = initkeyvalues();
245 char message[1000];
0db33b56 246 char temp[STRING_SIZE] = "1";
069680ac
MT
247 int x;
248 int result;
249 char networkrestart[STRING_SIZE] = "";
250
251 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
252 {
253 freekeyvalues(kv);
e1457ba0 254 errorbox(_("Unable to open settings file"));
069680ac
MT
255 return 0;
256 }
257
258 if (netaddresschange)
e1457ba0 259 strcpy(networkrestart, _("When configuration is complete, a network restart will be required."));
069680ac 260
d0f2cc42
MT
261 strcpy(temp, ""); findkey(kv, "CONFIG_TYPE", temp);
262 x = atol(temp);
263 x--;
0db33b56 264 if (x < 0 || x > 4) x = 0;
069680ac 265 /* Format heading bit. */
782ae35d 266 snprintf(message, 1000, _("Current config: %s\n\n%s"), configtypenames[x], networkrestart);
e1457ba0
MT
267 rc = newtWinMenu(_("Network configuration menu"), message, 50, 5, 5, 6,
268 sections, &choice, _("OK"), _("Done"), NULL);
069680ac
MT
269
270 if (rc == 0 || rc == 1)
271 result = choice + 1;
272 else
273 result = 0;
274
275 return result;
276}
277
278/* Here they choose general network config, number of nics etc. */
279int configtypemenu(void)
280{
281 struct keyvalue *kv = initkeyvalues();
0db33b56 282 char temp[STRING_SIZE] = "1";
069680ac 283 char message[1000];
9b040aa0 284 int choise, found;
0db33b56 285 int rc, configtype;
9b040aa0 286
069680ac
MT
287 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
288 {
289 freekeyvalues(kv);
e1457ba0 290 errorbox(_("Unable to open settings file"));
069680ac
MT
291 return 0;
292 }
9b040aa0
HS
293
294 found = scan_network_cards();
069680ac 295
9b040aa0 296 findkey(kv, "CONFIG_TYPE", temp); choise = atol(temp);
6fc15159 297 choise--;
9b040aa0 298
e1457ba0
MT
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);
9b040aa0 305 if ( configtypecards[choise] > found ) {
e1457ba0
MT
306 sprintf(message, _("Not enough netcards for your choice.\n\nNeeded: %d - Available: %d\n"),
307 configtypecards[choise], found);
9b040aa0
HS
308 errorbox(message);
309 }
069680ac
MT
310
311 if (rc == 0 || rc == 1)
312 {
a358af8c 313 choise++;
9b040aa0 314 sprintf(temp, "%d", choise);
069680ac 315 replacekeyvalue(kv, "CONFIG_TYPE", temp);
0db33b56
MT
316 configtype = atol(temp);
317 if (!HAS_RED)
318 clear_card_entry(_RED_CARD_);
319 if (!HAS_ORANGE)
320 clear_card_entry(_ORANGE_CARD_);
321 if (!HAS_BLUE)
322 clear_card_entry(_BLUE_CARD_);
9b040aa0 323
069680ac
MT
324 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
325 netaddresschange = 1;
326 }
069680ac
MT
327 freekeyvalues(kv);
328
329 return 0;
330}
331
332/* Driver menu. Choose drivers.. */
333int drivermenu(void)
334{
335 struct keyvalue *kv = initkeyvalues();
5057b611 336 char message[STRING_SIZE];
0db33b56 337 char temp[STRING_SIZE] = "1";
9c1c1c57 338
069680ac 339 int configtype;
0db33b56 340 int i, rc, kcount = 0, neednics;
069680ac
MT
341
342 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
343 {
344 freekeyvalues(kv);
e1457ba0 345 errorbox(_("Unable to open settings file"));
069680ac
MT
346 return 0;
347 }
348
4061c03f
HS
349 if (findkey(kv, "CONFIG_TYPE", temp))
350 configtype = atol(temp);
351 else {
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");
356 }
357
e1457ba0
MT
358 strcpy(message, _("Configure network drivers, and which interface each card is assigned to. "
359 "The current configuration is as follows:\n\n"));
5057b611 360
0db33b56
MT
361 kcount = 0;
362 neednics = 0;
5057b611 363 if (HAS_GREEN) {
e1457ba0 364 sprintf(temp, "%-6s: %s\n", "GREEN", knics[_GREEN_CARD_].description);
5057b611
HS
365 strcat(message, temp);
366 if (strlen(knics[_GREEN_CARD_].macaddr) ) {
e1457ba0 367 sprintf(temp, "%-6s: (%s)\n", "GREEN", knics[_GREEN_CARD_].macaddr);
5057b611
HS
368 strcat(message, temp);
369 }
370 neednics++;
75ae2191 371 }
5057b611 372 if (HAS_RED) {
e1457ba0 373 sprintf(temp, "%-6s: %s\n", "RED", knics[_RED_CARD_].description);
5057b611
HS
374 strcat(message, temp);
375 if (strlen(knics[_RED_CARD_].macaddr) ) {
e1457ba0 376 sprintf(temp, "%-6s: (%s)\n", "RED", knics[_RED_CARD_].macaddr);
5057b611 377 strcat(message, temp);
75ae2191 378 }
5057b611 379 neednics++;
069680ac 380 }
75ae2191 381 if (HAS_ORANGE) {
e1457ba0 382 sprintf(temp, "%-6s: %s\n", "ORANGE", knics[_ORANGE_CARD_].description);
5057b611
HS
383 strcat(message, temp);
384 if ( strlen(knics[_ORANGE_CARD_].macaddr) ) {
e1457ba0 385 sprintf(temp, "%-6s: (%s)\n", "ORANGE", knics[_ORANGE_CARD_].macaddr);
5057b611
HS
386 strcat(message, temp);
387 }
388 neednics++;
75ae2191 389 }
5057b611 390 if (HAS_BLUE) {
e1457ba0 391 sprintf(temp, "%-6s: %s\n", "BLUE", knics[_BLUE_CARD_].description);
5057b611
HS
392 strcat(message, temp);
393 if (strlen(knics[_BLUE_CARD_].macaddr)) {
e1457ba0 394 sprintf(temp, "%-6s: (%s)\n", "BLUE", knics[_BLUE_CARD_].macaddr);
5057b611 395 strcat(message, temp);
75ae2191 396 }
5057b611 397 neednics++;
069680ac 398 }
5057b611 399
0db33b56
MT
400 for ( i=0 ; i<4; i++)
401 if (strcmp(knics[i].macaddr, ""))
402 kcount++;
5057b611 403
f93a4b4f
HS
404 if (neednics = kcount)
405 {
e1457ba0
MT
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);
5057b611
HS
410 if (rc == 0 || rc == 1)
411 {
5057b611
HS
412 changedrivers();
413 }
414 } else {
069680ac
MT
415 changedrivers();
416 }
069680ac
MT
417 freekeyvalues(kv);
418
419 return 1;
420}
421
9b040aa0
HS
422int set_menu_entry_for(int *nr, int *card)
423{
424
425}
426
069680ac
MT
427int changedrivers(void)
428{
429 struct keyvalue *kv = initkeyvalues();
5057b611 430 char temp[STRING_SIZE], message[STRING_SIZE];
069680ac 431 int configtype;
75ae2191 432 int green = 0, red = 0, blue = 0, orange = 0;
5057b611
HS
433 char MenuInhalt[10][180];
434 char *pMenuInhalt[10];
435 int count = 0, choise = 0, rc;
436 int NicEntry[10];
59de0b00 437
069680ac
MT
438 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
439 {
440 freekeyvalues(kv);
e1457ba0 441 errorbox(_("Unable to open settings file"));
069680ac
MT
442 return 0;
443 }
0db33b56
MT
444 if (automode == 0)
445 runcommandwithstatus("/etc/rc.d/init.d/network stop red blue orange",
46b56e20 446 _("Networking"), _("Restarting non-local network..."), NULL);
59de0b00 447
5057b611 448 findkey(kv, "CONFIG_TYPE", temp); configtype = atol(temp);
a358af8c 449 if (configtype == 1)
75ae2191 450 { green = 1; red = 1; }
9b040aa0 451 else if (configtype == 2)
75ae2191 452 { green = 1; red = 1; orange = 1; }
9b040aa0 453 else if (configtype == 3)
75ae2191 454 { green = 1; red = 1; blue = 1; }
9b040aa0
HS
455 else if (configtype == 4)
456 { green = 1; red=1; orange=1; blue = 1; }
da6e926b
CS
457 else if (configtype == "")
458 { green = 1; red = 1; }
5057b611
HS
459
460 do
461 {
462 count = 0;
e1457ba0 463 strcpy(message, _("Please choose the interface you wish to change.\n\n"));
5057b611
HS
464
465 if (green) {
466 strcpy(MenuInhalt[count], "GREEN");
467 pMenuInhalt[count] = MenuInhalt[count];
468 NicEntry[_GREEN_CARD_] = count;
e1457ba0 469 sprintf(temp, "%-6s: %s\n", "GREEN", knics[_GREEN_CARD_].description);
5057b611
HS
470 strcat(message, temp);
471 if ( strlen(knics[_GREEN_CARD_].macaddr) ) {
e1457ba0 472 sprintf(temp, "%-6s: (%s)\n", "GREEN", knics[_GREEN_CARD_].macaddr);
5057b611
HS
473 strcat(message, temp);
474 }
475 count++;
476 }
477
478 if (red) {
479 strcpy(MenuInhalt[count], "RED");
480 pMenuInhalt[count] = MenuInhalt[count];
481 NicEntry[_RED_CARD_] = count;
e1457ba0 482 sprintf(temp, "%-6s: %s\n", "RED", knics[_RED_CARD_].description);
5057b611
HS
483 strcat(message, temp);
484 if ( strlen(knics[_RED_CARD_].macaddr) ) {
e1457ba0 485 sprintf(temp, "%-6s: (%s)\n", "RED", knics[_RED_CARD_].macaddr);
5057b611
HS
486 strcat(message, temp);
487 }
488 count++;
489 }
490
491 if (orange) {
492 strcpy(MenuInhalt[count], "ORANGE");
493 pMenuInhalt[count] = MenuInhalt[count];
494 NicEntry[_ORANGE_CARD_] = count;
e1457ba0 495 sprintf(temp, "%-6s: %s\n", "ORANGE", knics[_ORANGE_CARD_].description);
5057b611
HS
496 strcat(message, temp);
497 if ( strlen(knics[_ORANGE_CARD_].macaddr) ) {
e1457ba0 498 sprintf(temp, "%-6s: (%s)\n", "ORANGE", knics[_ORANGE_CARD_].macaddr);
5057b611
HS
499 strcat(message, temp);
500 }
501 count++;
502 }
503
504 if (blue) {
505 strcpy(MenuInhalt[count], "BLUE");
506 pMenuInhalt[count] = MenuInhalt[count];
507 NicEntry[_BLUE_CARD_] = count;
e1457ba0 508 sprintf(temp, "%-6s: %s\n", "BLUE", knics[_BLUE_CARD_].description);
5057b611
HS
509 strcat(message, temp);
510 if ( strlen(knics[_BLUE_CARD_].macaddr) ) {
e1457ba0 511 sprintf(temp, "%-6s: (%s)\n", "BLUE", knics[_BLUE_CARD_].macaddr);
5057b611
HS
512 strcat(message, temp);
513 }
514 count++;
515 }
516 pMenuInhalt[count] = NULL;
517
e1457ba0
MT
518 rc = newtWinMenu(_("Assigned Cards"), message, 70, 5, 5, 6, pMenuInhalt,
519 &choise, _("Select"), _("Remove"), _("Done"), NULL);
5057b611
HS
520
521 if ( rc == 0 || rc == 1) {
9b040aa0
HS
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_);
eea0467a 526 netaddresschange = 1;
5057b611 527 } else if (rc == 2) {
9b040aa0
HS
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_);
eea0467a 532 netaddresschange = 1;
a358af8c 533 }
5057b611
HS
534 }
535 while ( rc <= 2);
069680ac
MT
536
537 freekeyvalues(kv);
069680ac
MT
538 return 1;
539}
540
75ae2191 541// Let user change GREEN address.
069680ac
MT
542int greenaddressmenu(void)
543{
544 struct keyvalue *kv = initkeyvalues();
545 char message[1000];
546 int rc;
547
548 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
549 {
550 freekeyvalues(kv);
e1457ba0 551 errorbox(_("Unable to open settings file"));
069680ac
MT
552 return 0;
553 }
554
e1457ba0
MT
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);
069680ac
MT
560
561 if (rc == 0 || rc == 1)
562 {
563 if (changeaddress(kv, "GREEN", 0, ""))
564 {
565 netaddresschange = 1;
566 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
567 writehostsfiles();
568 }
569 }
570
571 freekeyvalues(kv);
572
573 return 0;
574}
575
75ae2191 576// They can change BLUE, ORANGE and GREEN too :)
069680ac
MT
577int addressesmenu(void)
578{
579 struct keyvalue *kv = initkeyvalues();
580 struct keyvalue *mainkv = initkeyvalues();
581 int rc = 0;
582 char *sections[5];
583 char *green = "GREEN";
584 char *orange = "ORANGE";
585 char *blue = "BLUE";
586 char *red = "RED";
587 int c = 0;
588 char greenaddress[STRING_SIZE];
589 char oldgreenaddress[STRING_SIZE];
590 char temp[STRING_SIZE];
591 char temp2[STRING_SIZE];
592 char message[1000];
593 int configtype;
594 int done;
595 int choice;
596 char hostname[STRING_SIZE];
597
598 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
599 {
600 freekeyvalues(kv);
601 freekeyvalues(mainkv);
e1457ba0 602 errorbox(_("Unable to open settings file"));
069680ac
MT
603 return 0;
604 }
605 if (!(readkeyvalues(mainkv, CONFIG_ROOT "/main/settings")))
606 {
607 freekeyvalues(kv);
608 freekeyvalues(mainkv);
e1457ba0 609 errorbox(_("Unable to open settings file"));
069680ac
MT
610 return 0;
611 }
612
613 strcpy(temp, "0"); findkey(kv, "CONFIG_TYPE", temp);
614 configtype = atol(temp);
615
616 sections[c] = green;
617 c++;
618 if (HAS_BLUE)
619 {
620 sections[c] = blue;
621 c++;
622 }
623 if (HAS_ORANGE)
624 {
625 sections[c] = orange;
626 c++;
627 }
628 if (HAS_RED)
629 {
630 sections[c] = red;
631 c++;
632 }
633 sections[c] = NULL;
634
635 choice = 0;
636 done = 0;
637 while (!done)
638 {
e1457ba0
MT
639 rc = newtWinMenu(_("Address settings"),
640 _("Select the interface you wish to reconfigure."), 50, 5,
641 5, 6, sections, &choice, _("OK"), _("Done"), NULL);
069680ac
MT
642
643 if (rc == 0 || rc == 1)
644 {
645 if (strcmp(sections[choice], "GREEN") == 0)
646 {
647 findkey(kv, "GREEN_ADDRESS", oldgreenaddress);
e1457ba0
MT
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);
069680ac
MT
653 if (rc == 0 || rc == 1)
654 {
655 if (changeaddress(kv, "GREEN", 0, ""))
656 {
657 netaddresschange = 1;
658 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
659 writehostsfiles();
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);
665 }
666 }
667 }
668 if (strcmp(sections[choice], "BLUE") == 0)
669 {
670 if (changeaddress(kv, "BLUE", 0, ""))
671 netaddresschange = 1;
672 }
673 if (strcmp(sections[choice], "ORANGE") == 0)
674 {
675 if (changeaddress(kv, "ORANGE", 0, ""))
676 netaddresschange = 1;
677 }
678 if (strcmp(sections[choice], "RED") == 0)
679 {
680 strcpy(hostname, "");
681 findkey(mainkv, "HOSTNAME", hostname);
682 if (changeaddress(kv, "RED", 1, hostname))
683 netaddresschange = 1;
684 }
685 }
686 else
687 done = 1;
688 }
689
690 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
691 freekeyvalues(kv);
692 freekeyvalues(mainkv);
693
694 return 0;
695}
696
697/* DNS and default gateway.... */
698int dnsgatewaymenu(void)
699{
700 struct keyvalue *kv = initkeyvalues();
701 char message[1000];
702 char temp[STRING_SIZE] = "0";
703 struct newtWinEntry entries[DNSGATEWAY_TOTAL+1];
704 char *values[DNSGATEWAY_TOTAL]; /* pointers for the values. */
705 int error;
706 int configtype;
707 int rc;
708
709 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
710 {
711 freekeyvalues(kv);
e1457ba0 712 errorbox(_("Unable to open settings file"));
069680ac
MT
713 return 0;
714 }
715
e1457ba0 716 entries[DNS1].text = _("Primary DNS:");
069680ac
MT
717 strcpy(temp, ""); findkey(kv, "DNS1", temp);
718 values[DNS1] = strdup(temp);
719 entries[DNS1].value = &values[DNS1];
720 entries[DNS1].flags = 0;
721
e1457ba0 722 entries[DNS2].text = _("Secondary DNS:");
069680ac
MT
723 strcpy(temp, ""); findkey(kv, "DNS2", temp);
724 values[DNS2] = strdup(temp);
725 entries[DNS2].value = &values[DNS2];
726 entries[DNS2].flags = 0;
727
e1457ba0 728 entries[DEFAULT_GATEWAY].text = _("Default gateway:");
069680ac
MT
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;
733
734 entries[DNSGATEWAY_TOTAL].text = NULL;
735 entries[DNSGATEWAY_TOTAL].value = NULL;
736 entries[DNSGATEWAY_TOTAL].flags = 0;
737
738 do
739 {
740 error = 0;
741
e1457ba0
MT
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);
069680ac
MT
746 if (rc == 0 || rc == 1)
747 {
e1457ba0
MT
748 strcpy(message, _("The following fields are invalid:"));
749 strcpy(message, "\n\n");
069680ac
MT
750 if (strlen(values[DNS1]))
751 {
752 if (inet_addr(values[DNS1]) == INADDR_NONE)
753 {
e1457ba0
MT
754 strcat(message, _("Primary DNS"));
755 strcat(message, "\n");
069680ac
MT
756 error = 1;
757 }
758 }
759 if (strlen(values[DNS2]))
760 {
761 if (inet_addr(values[DNS2]) == INADDR_NONE)
762 {
e1457ba0
MT
763 strcat(message, _("Secondary DNS"));
764 strcat(message, "\n");
069680ac
MT
765 error = 1;
766 }
767 }
768 if (strlen(values[DEFAULT_GATEWAY]))
769 {
770 if (inet_addr(values[DEFAULT_GATEWAY]) == INADDR_NONE)
771 {
e1457ba0
MT
772 strcat(message, _("Default gateway"));
773 strcat(message, "\n");
069680ac
MT
774 error = 1;
775 }
776 }
777 if (!strlen(values[DNS1]) && strlen(values[DNS2]))
778 {
e1457ba0
MT
779 strcpy(message, _("Secondary DNS specified without a Primary DNS"));
780 strcat(message, "\n");
069680ac
MT
781 error = 1;
782 }
783
784 if (error)
785 errorbox(message);
786 else
787 {
788 replacekeyvalue(kv, "DNS1", values[DNS1]);
789 replacekeyvalue(kv, "DNS2", values[DNS2]);
790 replacekeyvalue(kv, "DEFAULT_GATEWAY", values[DEFAULT_GATEWAY]);
791 netaddresschange = 1;
792 free(values[DNS1]);
793 free(values[DNS2]);
794 free(values[DEFAULT_GATEWAY]);
795 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
796 }
797 }
798 }
799 while (error);
800
801 freekeyvalues(kv);
802
803 return 1;
804}