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.
9 * $Id: networking.c,v 1.5.2.6 2006/02/06 22:00:13 gespinasse Exp $
17 #define DEFAULT_GATEWAY 2
18 #define DNSGATEWAY_TOTAL 3
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)
32 extern struct nic nics
[];
34 char *configtypenames
[] = {
35 "GREEN (RED is modem/ISDN)",
36 "GREEN + ORANGE (RED is modem/ISDN)",
38 "GREEN + ORANGE + RED",
39 "GREEN + BLUE (RED is modem/ISDN) ",
40 "GREEN + ORANGE + BLUE (RED is modem/ISDN)",
42 "GREEN + ORANGE + BLUE + RED",
46 int oktoleave(char *errormessage
);
48 int configtypemenu(void);
50 int changedrivers(void);
51 int greenaddressmenu(void);
52 int addressesmenu(void);
53 int dnsgatewaymenu(void);
55 int handlenetworking(void)
59 char errormessage
[STRING_SIZE
];
87 if (oktoleave(errormessage
))
90 errorbox(errormessage
);
100 /* Restart networking! */
101 if (netaddresschange
)
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
]);
113 int oktoleave(char *errormessage
)
115 struct keyvalue
*kv
= initkeyvalues();
116 char temp
[STRING_SIZE
];
119 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
122 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
126 strcpy(temp
, "0"); findkey(kv
, "CONFIG_TYPE", temp
); configtype
= atol(temp
);
127 if (configtype
< 0 || configtype
> 7) configtype
= 0;
131 strcpy(temp
, ""); findkey(kv
, "BLUE_DEV", temp
);
134 strcpy(errormessage
, ctr
[TR_NO_BLUE_INTERFACE
]);
137 if (!(interfacecheck(kv
, "BLUE")))
139 strcpy(errormessage
, ctr
[TR_MISSING_BLUE_IP
]);
145 strcpy(temp
, ""); findkey(kv
, "ORANGE_DEV", temp
);
148 strcpy(errormessage
, ctr
[TR_NO_ORANGE_INTERFACE
]);
151 if (!(interfacecheck(kv
, "ORANGE")))
153 strcpy(errormessage
, ctr
[TR_MISSING_ORANGE_IP
]);
159 strcpy(temp
, ""); findkey(kv
, "RED_DEV", temp
);
162 strcpy(errormessage
, ctr
[TR_NO_RED_INTERFACE
]);
165 if (!(interfacecheck(kv
, "RED")))
167 strcpy(errormessage
, ctr
[TR_MISSING_RED_IP
]);
171 strcpy(errormessage
, "");
175 if (strlen(errormessage
))
182 /* Shows the main menu and a summary of the current settings. */
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
};
190 static int choice
= 0;
191 struct keyvalue
*kv
= initkeyvalues();
193 char temp
[STRING_SIZE
];
196 char networkrestart
[STRING_SIZE
] = "";
198 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
201 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
205 if (netaddresschange
)
206 strcpy(networkrestart
, ctr
[TR_RESTART_REQUIRED
]);
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
],
213 rc
= newtWinMenu(ctr
[TR_NETWORK_CONFIGURATION_MENU
], message
, 50, 5, 5, 6,
214 sections
, &choice
, ctr
[TR_OK
], ctr
[TR_DONE
], NULL
);
216 if (rc
== 0 || rc
== 1)
224 /* Here they choose general network config, number of nics etc. */
225 int configtypemenu(void)
227 struct keyvalue
*kv
= initkeyvalues();
228 char temp
[STRING_SIZE
] = "0";
233 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
236 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
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
);
245 if (rc
== 0 || rc
== 1)
247 runcommandwithstatus("/etc/rc.d/init.d/network stop red blue orange",
248 ctr
[TR_PUSHING_NON_LOCAL_NETWORK_DOWN
]);
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;
264 /* Driver menu. Choose drivers.. */
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
;
275 int rc
, i
= 0, kcount
= 0;
277 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
280 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
284 strcpy(temp
, "0"); findkey(kv
, "CONFIG_TYPE", temp
);
285 configtype
= atol(temp
);
290 errorbox(ctr
[TR_YOUR_CONFIGURATION_IS_SINGLE_GREEN_ALREADY_HAS_DRIVER
]);
294 strcpy(message
, ctr
[TR_CONFIGURE_NETWORK_DRIVERS
]);
296 if( (fp
= fopen(KNOWN_NICS
, "r")) == NULL
)
298 fprintf(flog
,"Couldn't open " KNOWN_NICS
);
301 while (fgets(temp_line
, STRING_SIZE
, fp
) != NULL
)
303 strcpy(knics
[kcount
].description
, strtok(temp_line
,";"));
304 strcpy(knics
[kcount
].macaddr
, strtok(NULL
,";"));
305 if (strlen(knics
[kcount
].macaddr
) > 5 ) kcount
++;
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
)
316 sprintf(temp1
, "GREEN: %s (%s / green0)\n", pknics
[i
].description
, pknics
[i
].macaddr
);
317 strcat(message
, temp1
);
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
)
327 sprintf(temp1
, "BLUE: %s (%s / blue0)\n", pknics
[i
].description
, pknics
[i
].macaddr
);
328 strcat(message
, temp1
);
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
)
338 sprintf(temp1
, "ORANGE: %s (%s / orange0)\n", pknics
[i
].description
, pknics
[i
].macaddr
);
339 strcat(message
, temp1
);
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
)
349 sprintf(temp1
, "RED: %s (%s / red0)\n", pknics
[i
].description
, pknics
[i
].macaddr
);
350 strcat(message
, temp1
);
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)
357 /* Shit, got to do something.. */
366 int cardassigned(char *colour
)
368 char command
[STRING_SIZE
];
369 sprintf(command
, "grep -q %s < /etc/udev/rules.d/30-persistent-network.rules 2>/dev/null", colour
);
376 int changedrivers(void)
378 struct keyvalue
*kv
= initkeyvalues();
379 char temp
[STRING_SIZE
];
381 int green
= 0, red
= 0, blue
= 0, orange
= 0;
383 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
386 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
390 runcommandwithstatus("/etc/rc.d/init.d/network stop red blue orange",
391 ctr
[TR_PUSHING_NON_LOCAL_NETWORK_DOWN
]);
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; }
410 if (green
&& !cardassigned("green"))
412 if (red
&& !cardassigned("red"))
414 if (blue
&& !cardassigned("blue"))
416 if (orange
&& !cardassigned("orange"))
419 // writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
425 // Let user change GREEN address.
426 int greenaddressmenu(void)
428 struct keyvalue
*kv
= initkeyvalues();
432 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
435 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
439 sprintf(message
, ctr
[TR_WARNING_LONG
], NAME
);
440 rc
= newtWinChoice(ctr
[TR_WARNING
], ctr
[TR_OK
], ctr
[TR_CANCEL
], message
);
442 if (rc
== 0 || rc
== 1)
444 if (changeaddress(kv
, "GREEN", 0, ""))
446 netaddresschange
= 1;
447 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
457 // They can change BLUE, ORANGE and GREEN too :)
458 int addressesmenu(void)
460 struct keyvalue
*kv
= initkeyvalues();
461 struct keyvalue
*mainkv
= initkeyvalues();
464 char *green
= "GREEN";
465 char *orange
= "ORANGE";
469 char greenaddress
[STRING_SIZE
];
470 char oldgreenaddress
[STRING_SIZE
];
471 char temp
[STRING_SIZE
];
472 char temp2
[STRING_SIZE
];
477 char hostname
[STRING_SIZE
];
479 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
482 freekeyvalues(mainkv
);
483 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
486 if (!(readkeyvalues(mainkv
, CONFIG_ROOT
"/main/settings")))
489 freekeyvalues(mainkv
);
490 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
494 strcpy(temp
, "0"); findkey(kv
, "CONFIG_TYPE", temp
);
495 configtype
= atol(temp
);
506 sections
[c
] = orange
;
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
);
524 if (rc
== 0 || rc
== 1)
526 if (strcmp(sections
[choice
], "GREEN") == 0)
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
],
532 if (rc
== 0 || rc
== 1)
534 if (changeaddress(kv
, "GREEN", 0, ""))
536 netaddresschange
= 1;
537 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
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);
547 if (strcmp(sections
[choice
], "BLUE") == 0)
549 if (changeaddress(kv
, "BLUE", 0, ""))
550 netaddresschange
= 1;
552 if (strcmp(sections
[choice
], "ORANGE") == 0)
554 if (changeaddress(kv
, "ORANGE", 0, ""))
555 netaddresschange
= 1;
557 if (strcmp(sections
[choice
], "RED") == 0)
559 strcpy(hostname
, "");
560 findkey(mainkv
, "HOSTNAME", hostname
);
561 if (changeaddress(kv
, "RED", 1, hostname
))
562 netaddresschange
= 1;
569 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");
571 freekeyvalues(mainkv
);
576 /* DNS and default gateway.... */
577 int dnsgatewaymenu(void)
579 struct keyvalue
*kv
= initkeyvalues();
581 char temp
[STRING_SIZE
] = "0";
582 struct newtWinEntry entries
[DNSGATEWAY_TOTAL
+1];
583 char *values
[DNSGATEWAY_TOTAL
]; /* pointers for the values. */
588 if (!(readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings")))
591 errorbox(ctr
[TR_UNABLE_TO_OPEN_SETTINGS_FILE
]);
595 strcpy(temp
, "0"); findkey(kv
, "CONFIG_TYPE", temp
);
596 configtype
= atol(temp
);
601 errorbox(ctr
[TR_DNS_GATEWAY_WITH_GREEN
]);
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;
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;
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;
623 entries
[DNSGATEWAY_TOTAL
].text
= NULL
;
624 entries
[DNSGATEWAY_TOTAL
].value
= NULL
;
625 entries
[DNSGATEWAY_TOTAL
].flags
= 0;
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)
636 strcpy(message
, ctr
[TR_INVALID_FIELDS
]);
637 if (strlen(values
[DNS1
]))
639 if (inet_addr(values
[DNS1
]) == INADDR_NONE
)
641 strcat(message
, ctr
[TR_PRIMARY_DNS_CR
]);
645 if (strlen(values
[DNS2
]))
647 if (inet_addr(values
[DNS2
]) == INADDR_NONE
)
649 strcat(message
, ctr
[TR_SECONDARY_DNS_CR
]);
653 if (strlen(values
[DEFAULT_GATEWAY
]))
655 if (inet_addr(values
[DEFAULT_GATEWAY
]) == INADDR_NONE
)
657 strcat(message
, ctr
[TR_DEFAULT_GATEWAY_CR
]);
661 if (!strlen(values
[DNS1
]) && strlen(values
[DNS2
]))
663 strcpy(message
, ctr
[TR_SECONDARY_WITHOUT_PRIMARY_DNS
]);
671 replacekeyvalue(kv
, "DNS1", values
[DNS1
]);
672 replacekeyvalue(kv
, "DNS2", values
[DNS2
]);
673 replacekeyvalue(kv
, "DEFAULT_GATEWAY", values
[DEFAULT_GATEWAY
]);
674 netaddresschange
= 1;
677 free(values
[DEFAULT_GATEWAY
]);
678 writekeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings");