]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - src/install+setup/libsmooth/netstuff.c
6e0897c21a2ca07d895256317b1d3a125ea817e8
[people/teissler/ipfire-2.x.git] / src / install+setup / libsmooth / netstuff.c
1 /* SmoothWall libsmooth.
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 * Contains network library functions.
8 *
9 */
10
11 #include "libsmooth.h"
12 #include <signal.h>
13
14 extern FILE *flog;
15 extern char *mylog;
16
17 extern char **ctr;
18
19 newtComponent networkform;
20 newtComponent addressentry;
21 newtComponent netmaskentry;
22 newtComponent statictyperadio;
23 newtComponent dhcptyperadio;
24 newtComponent pppoetyperadio;
25 newtComponent pptptyperadio;
26 newtComponent dhcphostnameentry;
27
28 /* acceptable character filter for IP and netmaks entry boxes */
29 static int ip_input_filter(newtComponent entry, void * data, int ch, int cursor)
30 {
31 if ((ch >= '0' && ch <= '9') || ch == '.' || ch == '\r' || ch >= NEWT_KEY_EXTRA_BASE)
32 return ch;
33 return 0;
34 }
35
36 /* This is a groovie dialog for showing network info. Takes a keyvalue list,
37 * a colour and a dhcp flag. Shows the current settings, and rewrites them
38 * if necessary. DHCP flag sets wether to show the dhcp checkbox. */
39 int changeaddress(struct keyvalue *kv, char *colour, int typeflag,
40 char *defaultdhcphostname)
41 {
42 char *addressresult;
43 char *netmaskresult;
44 char *dhcphostnameresult;
45 struct newtExitStruct es;
46 newtComponent header;
47 newtComponent addresslabel;
48 newtComponent netmasklabel;
49 newtComponent dhcphostnamelabel;
50 newtComponent ok, cancel;
51 char message[1000];
52 char temp[STRING_SIZE];
53 char addressfield[STRING_SIZE];
54 char netmaskfield[STRING_SIZE];
55 char typefield[STRING_SIZE];
56 char dhcphostnamefield[STRING_SIZE];
57 int error;
58 int result = 0;
59 char type[STRING_SIZE];
60 int startstatictype = 0;
61 int startdhcptype = 0;
62 int startpppoetype = 0;
63 int startpptptype = 0;
64
65 /* Build some key strings. */
66 sprintf(addressfield, "%s_ADDRESS", colour);
67 sprintf(netmaskfield, "%s_NETMASK", colour);
68 sprintf(typefield, "%s_TYPE", colour);
69 sprintf(dhcphostnamefield, "%s_DHCP_HOSTNAME", colour);
70
71 sprintf(message, ctr[TR_INTERFACE], colour);
72 newtCenteredWindow(44, (typeflag ? 18 : 12), message);
73
74 networkform = newtForm(NULL, NULL, 0);
75
76 sprintf(message, ctr[TR_ENTER_THE_IP_ADDRESS_INFORMATION], colour);
77 header = newtTextboxReflowed(1, 1, message, 42, 0, 0, 0);
78 newtFormAddComponent(networkform, header);
79
80 /* See if we need a dhcp checkbox. If we do, then we shift the contents
81 * of the window down two rows to make room. */
82 if (typeflag)
83 {
84 strcpy(temp, "STATIC"); findkey(kv, typefield, temp);
85 if (strcmp(temp, "STATIC") == 0) startstatictype = 1;
86 if (strcmp(temp, "DHCP") == 0) startdhcptype = 1;
87 if (strcmp(temp, "PPPOE") == 0) startpppoetype = 1;
88 if (strcmp(temp, "PPTP") == 0) startpptptype = 1;
89 statictyperadio = newtRadiobutton(2, 4, ctr[TR_STATIC], startstatictype, NULL);
90 dhcptyperadio = newtRadiobutton(2, 5, "DHCP", startdhcptype, statictyperadio);
91 pppoetyperadio = newtRadiobutton(2, 6, "PPPOE", startpppoetype, dhcptyperadio);
92 pptptyperadio = newtRadiobutton(2, 7, "PPTP", startpptptype, pppoetyperadio);
93 newtFormAddComponents(networkform, statictyperadio, dhcptyperadio,
94 pppoetyperadio, pptptyperadio, NULL);
95 newtComponentAddCallback(statictyperadio, networkdialogcallbacktype, NULL);
96 newtComponentAddCallback(dhcptyperadio, networkdialogcallbacktype, NULL);
97 newtComponentAddCallback(pppoetyperadio, networkdialogcallbacktype, NULL);
98 newtComponentAddCallback(pptptyperadio, networkdialogcallbacktype, NULL);
99 dhcphostnamelabel = newtTextbox(2, 9, 18, 1, 0);
100 newtTextboxSetText(dhcphostnamelabel, ctr[TR_DHCP_HOSTNAME]);
101 strcpy(temp, defaultdhcphostname);
102 findkey(kv, dhcphostnamefield, temp);
103 dhcphostnameentry = newtEntry(20, 9, temp, 20, &dhcphostnameresult, 0);
104 newtFormAddComponent(networkform, dhcphostnamelabel);
105 newtFormAddComponent(networkform, dhcphostnameentry);
106 if (startdhcptype == 0)
107 newtEntrySetFlags(dhcphostnameentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);
108 }
109 /* Address */
110 addresslabel = newtTextbox(2, (typeflag ? 11 : 4) + 0, 18, 1, 0);
111 newtTextboxSetText(addresslabel, ctr[TR_IP_ADDRESS_PROMPT]);
112 strcpy(temp, "");
113 findkey(kv, addressfield, temp);
114 addressentry = newtEntry(20, (typeflag ? 11 : 4) + 0, temp, 20, &addressresult, 0);
115 newtEntrySetFilter(addressentry, ip_input_filter, NULL);
116 if (typeflag == 1 && startstatictype == 0 && startpptptype == 0 )
117 newtEntrySetFlags(addressentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);
118 newtFormAddComponent(networkform, addresslabel);
119 newtFormAddComponent(networkform, addressentry);
120
121 /* Netmask */
122 netmasklabel = newtTextbox(2, (typeflag ? 11 : 4) + 1, 18, 1, 0);
123 newtTextboxSetText(netmasklabel, ctr[TR_NETMASK_PROMPT]);
124 strcpy(temp, "255.255.255.0"); findkey(kv, netmaskfield, temp);
125 netmaskentry = newtEntry(20, (typeflag ? 11 : 4) + 1, temp, 20, &netmaskresult, 0);
126 newtEntrySetFilter(netmaskentry, ip_input_filter, NULL);
127 if (typeflag == 1 && startstatictype == 0 && startpptptype == 0 )
128 newtEntrySetFlags(netmaskentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);
129
130 newtFormAddComponent(networkform, netmasklabel);
131 newtFormAddComponent(networkform, netmaskentry);
132
133 /* Buttons. */
134 ok = newtButton(8, (typeflag ? 14 : 7), ctr[TR_OK]);
135 cancel = newtButton(26, (typeflag ? 14 : 7), ctr[TR_CANCEL]);
136
137 newtFormAddComponents(networkform, ok, cancel, NULL);
138
139 newtRefresh();
140 newtDrawForm(networkform);
141
142 do
143 {
144 error = 0;
145 newtFormRun(networkform, &es);
146
147 if (es.u.co == ok)
148 {
149 /* OK was pressed; verify the contents of each entry. */
150 strcpy(message, ctr[TR_INVALID_FIELDS]);
151
152 strcpy(type, "STATIC");
153 if (typeflag)
154 gettype(type);
155 if (strcmp(type, "STATIC") == 0 || strcmp(type, "PPTP") == 0 )
156 {
157 if (inet_addr(addressresult) == INADDR_NONE)
158 {
159 strcat(message, ctr[TR_IP_ADDRESS_CR]);
160 error = 1;
161 }
162 if (inet_addr(netmaskresult) == INADDR_NONE)
163 {
164 strcat(message, ctr[TR_NETWORK_MASK_CR]);
165 error = 1;
166 }
167 }
168 if (strcmp(type, "DHCP") == 0)
169 {
170 if (!strlen(dhcphostnameresult))
171 {
172 strcat(message, ctr[TR_DHCP_HOSTNAME_CR]);
173 error = 1;
174 }
175 }
176 if (error)
177 errorbox(message);
178 else
179 {
180 /* No errors! Set new values, depending on dhcp flag etc. */
181 if (typeflag)
182 {
183 replacekeyvalue(kv, dhcphostnamefield, dhcphostnameresult);
184 if (strcmp(type, "STATIC") != 0 && strcmp(type, "PPTP") != 0)
185 {
186 replacekeyvalue(kv, addressfield, "0.0.0.0");
187 replacekeyvalue(kv, netmaskfield, "0.0.0.0");
188 }
189 else
190 {
191 replacekeyvalue(kv, addressfield, addressresult);
192 replacekeyvalue(kv, netmaskfield, netmaskresult);
193 }
194 replacekeyvalue(kv, typefield, type);
195 }
196 else
197 {
198 replacekeyvalue(kv, addressfield, addressresult);
199 replacekeyvalue(kv, netmaskfield, netmaskresult);
200 }
201
202 setnetaddress(kv, colour);
203 result = 1;
204 }
205 }
206 }
207 while (error);
208
209 newtFormDestroy(networkform);
210 newtPopWindow();
211
212 return result;
213 }
214
215 /* for pppoe: return string thats type STATIC, DHCP or PPPOE */
216 int gettype(char *type)
217 {
218 newtComponent selected = newtRadioGetCurrent(statictyperadio);
219
220 if (selected == statictyperadio)
221 strcpy(type, "STATIC");
222 else if (selected == dhcptyperadio)
223 strcpy(type, "DHCP");
224 else if (selected == pppoetyperadio)
225 strcpy(type, "PPPOE");
226 else if (selected == pptptyperadio)
227 strcpy(type, "PPTP");
228 else
229 strcpy(type, "ERROR");
230
231 return 0;
232 }
233
234 /* 0.9.9: calculates broadcast too. */
235 int setnetaddress(struct keyvalue *kv, char *colour)
236 {
237 char addressfield[STRING_SIZE];
238 char netaddressfield[STRING_SIZE];
239 char netmaskfield[STRING_SIZE];
240 char broadcastfield[STRING_SIZE];
241 char address[STRING_SIZE];
242 char netmask[STRING_SIZE];
243 unsigned long int intaddress;
244 unsigned long int intnetaddress;
245 unsigned long int intnetmask;
246 unsigned long int intbroadcast;
247 struct in_addr temp;
248 char *netaddress;
249 char *broadcast;
250
251 /* Build some key strings. */
252 sprintf(addressfield, "%s_ADDRESS", colour);
253 sprintf(netaddressfield, "%s_NETADDRESS", colour);
254 sprintf(netmaskfield, "%s_NETMASK", colour);
255 sprintf(broadcastfield, "%s_BROADCAST", colour);
256
257 strcpy(address, ""); findkey(kv, addressfield, address);
258 strcpy(netmask, ""); findkey(kv, netmaskfield, netmask);
259
260 /* Calculate netaddress. Messy.. */
261 intaddress = inet_addr(address);
262 intnetmask = inet_addr(netmask);
263
264 intnetaddress = intaddress & intnetmask;
265 temp.s_addr = intnetaddress;
266 netaddress = inet_ntoa(temp);
267
268 replacekeyvalue(kv, netaddressfield, netaddress);
269
270 intbroadcast = intnetaddress | ~intnetmask;
271 temp.s_addr = intbroadcast;
272 broadcast = inet_ntoa(temp);
273
274 replacekeyvalue(kv, broadcastfield, broadcast);
275
276 return 1;
277 }
278
279 /* Called when dhcp flag is toggled. Toggle disabled state of other 3
280 * controls. */
281 void networkdialogcallbacktype(newtComponent cm, void *data)
282 {
283 char type[STRING_SIZE];
284
285 gettype(type);
286
287 if (strcmp(type, "STATIC") != 0 && strcmp(type, "PPTP") != 0 )
288 {
289 newtEntrySetFlags(addressentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);
290 newtEntrySetFlags(netmaskentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);
291 }
292 else
293 {
294 newtEntrySetFlags(addressentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET);
295 newtEntrySetFlags(netmaskentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET);
296 }
297 if (strcmp(type, "DHCP") == 0)
298 newtEntrySetFlags(dhcphostnameentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET);
299 else
300 newtEntrySetFlags(dhcphostnameentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);
301
302 newtRefresh();
303 newtDrawForm(networkform);
304 }
305
306 int interfacecheck(struct keyvalue *kv, char *colour)
307 {
308 char temp[STRING_SIZE];
309 char colourfields[NETCHANGE_TOTAL][STRING_SIZE];
310 int c;
311
312 sprintf(colourfields[ADDRESS], "%s_ADDRESS", colour);
313 sprintf(colourfields[NETADDRESS], "%s_NETADDRESS", colour);
314 sprintf(colourfields[NETMASK], "%s_NETMASK", colour);
315
316 for (c = 0; c < 3; c++)
317 {
318 strcpy(temp, ""); findkey(kv, colourfields[c], temp);
319 if (!(strlen(temp))) return 0;
320 }
321 return 1;
322 }
323
324 /* Funky routine for loading all drivers (cept those are already loaded.). */
325 int probecards(char *driver, char *driveroptions)
326 {
327 return 0;
328 }
329
330 char *strupper(char *s)
331 {
332 int n;
333 for (n=0;s[n];n++) s[n]=toupper(s[n]);
334 return s;
335 }
336
337
338 int write_configs_netudev(char *description, char *macaddr, char *colour)
339 {
340 #define UDEV_NET_CONF "/etc/udev/rules.d/30-persistent-network.rules"
341
342 FILE *fp;
343 char commandstring[STRING_SIZE];
344 struct keyvalue *kv = initkeyvalues();
345 char temp1[STRING_SIZE], temp2[STRING_SIZE], temp3[STRING_SIZE];
346 char ucolour[STRING_SIZE];
347
348 sprintf(ucolour, colour);
349 strupper(ucolour);
350
351 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
352 {
353 freekeyvalues(kv);
354 errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]);
355 return 0;
356 }
357
358 sprintf(temp1, "%s_DEV", ucolour);
359 sprintf(temp2, "%s_MACADDR", ucolour);
360 sprintf(temp3, "%s0", colour);
361 replacekeyvalue(kv, temp1, temp3);
362 replacekeyvalue(kv, temp2, macaddr);
363
364 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
365 freekeyvalues(kv);
366
367 if( (fp = fopen(KNOWN_NICS, "a")) == NULL )
368 {
369 fprintf(stderr,"Couldn't open "KNOWN_NICS);
370 return 1;
371 }
372 fprintf(fp,"%s;%s;\n", description, macaddr);
373 fclose(fp);
374
375 // Make sure that there is no conflict
376 snprintf(commandstring, STRING_SIZE, "/usr/bin/touch "UDEV_NET_CONF" >/dev/null 2>&1");
377 system(commandstring);
378 snprintf(commandstring, STRING_SIZE, "/bin/cat "UDEV_NET_CONF" | /bin/grep -v \"%s\" > "UDEV_NET_CONF" 2>/dev/null", macaddr);
379 system(commandstring);
380 snprintf(commandstring, STRING_SIZE, "/bin/cat "UDEV_NET_CONF" | /bin/grep -v \"%s\" > "UDEV_NET_CONF" 2>/dev/null", colour);
381 system(commandstring);
382
383 if( (fp = fopen(UDEV_NET_CONF, "a")) == NULL )
384 {
385 fprintf(stderr,"Couldn't open" UDEV_NET_CONF);
386 return 1;
387 }
388 fprintf(fp,"ACTION==\"add\", SUBSYSTEM==\"net\", SYSFS{address}==\"%s\", NAME=\"%s0\" # %s\n", macaddr, colour, description);
389 fclose(fp);
390
391 return 0;
392 }
393
394 int nicmenu(char *colour)
395 {
396 FILE *fp;
397 char temp_line[STRING_SIZE];
398 struct nic nics[20], *pnics;
399 pnics = nics;
400 struct knic knics[20], *pknics;
401 pknics = knics;
402 int rc, choise, count = 0, kcount = 0, i, found;
403 char macaddr[STRING_SIZE], description[STRING_SIZE];
404 char message[STRING_SIZE];
405
406 char MenuInhalt[20][80];
407 char *pMenuInhalt[20];
408
409 mysystem("/bin/probenic.sh");
410
411 // Read the nics we already use
412 if((fp = fopen(KNOWN_NICS, "r")) == NULL)
413 {
414 fprintf(flog,"Couldn't open " KNOWN_NICS);
415 return 1;
416 }
417 while (fgets(temp_line, STRING_SIZE, fp) != NULL)
418 {
419 strcpy(knics[kcount].description, strtok(temp_line,";"));
420 strcpy(knics[kcount].macaddr , strtok(NULL,";"));
421 if (strlen(knics[kcount].macaddr) > 5 ) kcount++;
422 }
423 fclose(fp);
424
425 // Read our scanned nics
426 if( (fp = fopen(SCANNED_NICS, "r")) == NULL )
427 {
428 fprintf(stderr,"Couldn't open "SCANNED_NICS);
429 return 1;
430 }
431 while (fgets(temp_line, STRING_SIZE, fp) != NULL)
432 {
433 strcpy(description, strtok(temp_line,";"));
434 strcpy(macaddr, strtok(NULL,";"));
435 found = 0;
436 if (strlen(macaddr) > 5 ) {
437 for (i=0; i < kcount; i++)
438 {
439 // Check if the nic is already in use
440 if (strcmp(pknics[i].macaddr, macaddr) == NULL )
441 {
442 found = 1;
443 }
444 }
445 if (!found)
446 {
447 strcpy( pnics[count].description , description );
448 strcpy( pnics[count].macaddr , macaddr );
449 count++;
450 }
451 }
452 }
453 fclose(fp);
454
455 // If new nics are found...
456 if (count > 0) {
457 char cMenuInhalt[STRING_SIZE];
458 for (i=0 ; i < count ; i++)
459 {
460 if ( strlen(nics[i].description) < 52 )
461 strncpy(MenuInhalt[i], nics[i].description + 1, strlen(nics[i].description)- 2);
462 else
463 {
464 strncpy(cMenuInhalt, nics[i].description + 1, 50);
465 strncpy(MenuInhalt[i], cMenuInhalt,(strrchr(cMenuInhalt,' ') - cMenuInhalt));
466 strcat (MenuInhalt[i], "...");
467 }
468 while ( strlen(MenuInhalt[i]) < 50)
469 // Fill with space.
470 strcat( MenuInhalt[i], " ");
471
472 strcat(MenuInhalt[i], " (");
473 strcat(MenuInhalt[i], nics[i].macaddr);
474 strcat(MenuInhalt[i], ")");
475 pMenuInhalt[i] = MenuInhalt[i];
476 }
477
478 sprintf(message, "Es wurde(n) %d freie Netzwerkkarte(n) in Ihrem System gefunden.\nBitte waehlen Sie im naechsten Dialog eine davon aus.\n", count);
479
480 newtWinMessage("NetcardMenu", ctr[TR_OK], message);
481
482 sprintf(message, "Bitte waehlen Sie eine der untenstehenden Netzwerkkarten fuer die Schnittstelle \"%s\" aus.\n", colour);
483
484 rc = newtWinMenu("NetcardMenu", message, 50, 5, 5, 6, pMenuInhalt, &choise, ctr[TR_OK], ctr[TR_SELECT], ctr[TR_CANCEL], NULL);
485
486 if ( rc == 0 || rc == 1) {
487 write_configs_netudev(pnics[choise].description, pnics[choise].macaddr, colour);
488 } else if (rc == 2) {
489 manualdriver("pcnet32","");
490 } else {
491 errorbox("Sie haben keine Netzwerkkarte ausgewaehlt.\n");
492 return 1;
493 }
494 return 0;
495 } else {
496 // We have to add here that you can manually add a device
497 errorbox("Es wurden leider keine freien Netzwerkkarten fuer die Schnittstelle in ihrem System gefunden.");
498 return 1;
499 }
500 }
501
502 /* Manual entry for gurus. */
503 int manualdriver(char *driver, char *driveroptions)
504 {
505 char *values[] = { NULL, NULL }; /* pointers for the values. */
506 struct newtWinEntry entries[] =
507 { { "", &values[0], 0,}, { NULL, NULL, 0 } };
508 int rc;
509 char commandstring[STRING_SIZE];
510 char *driverend;
511
512 strcpy(driver, "");
513 strcpy(driveroptions, "");
514
515 rc = newtWinEntries(ctr[TR_SELECT_NETWORK_DRIVER],
516 ctr[TR_MODULE_PARAMETERS], 50, 5, 5, 40, entries,
517 ctr[TR_OK], ctr[TR_CANCEL], NULL);
518 if (rc == 0 || rc == 1)
519 {
520 if (strlen(values[0]))
521 {
522 sprintf(commandstring, "/sbin/modprobe %s", values[0]);
523 if (runcommandwithstatus(commandstring, ctr[TR_LOADING_MODULE]) == 0)
524 {
525 if ((driverend = strchr(values[0], ' ')))
526 {
527 *driverend = '\0';
528 strcpy(driver, values[0]);
529 strcpy(driveroptions, driverend + 1);
530 }
531 else
532 {
533 strcpy(driver, values[0]);
534 strcpy(driveroptions, "");
535 }
536 }
537 else
538 errorbox(ctr[TR_UNABLE_TO_LOAD_DRIVER_MODULE]);
539 }
540 else
541 errorbox(ctr[TR_MODULE_NAME_CANNOT_BE_BLANK]);
542 }
543 free(values[0]);
544
545 return 1;
546 }