1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
|
/* $NetBSD: net.c,v 1.100 2003/11/30 14:36:43 dsl Exp $ */
/*
* Copyright 1997 Piermont Information Systems Inc.
* All rights reserved.
*
* Written by Philip A. Nelson for Piermont Information Systems Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed for the NetBSD Project by
* Piermont Information Systems Inc.
* 4. The name of Piermont Information Systems Inc. may not be used to endorse
* or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/* net.c -- routines to fetch files off the network. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curses.h>
#include <time.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/stat.h>
#ifdef INET6
#include <sys/sysctl.h>
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include "defs.h"
#include "md.h"
#include "msg_defs.h"
#include "menu_defs.h"
#include "txtwalk.h"
int network_up = 0;
/* Access to network information */
static char *net_devices;
static char *net_up;
static char net_dev[STRSIZE];
static char net_domain[STRSIZE];
static char net_host[STRSIZE];
static char net_ip[STRSIZE];
static char net_mask[STRSIZE];
static char net_namesvr[STRSIZE];
static char net_defroute[STRSIZE];
static char net_media[STRSIZE];
static int net_dhcpconf;
#define DHCPCONF_IPADDR 0x01
#define DHCPCONF_NAMESVR 0x02
#define DHCPCONF_HOST 0x04
#define DHCPCONF_DOMAIN 0x08
#ifdef INET6
static char net_ip6[STRSIZE];
char net_namesvr6[STRSIZE];
static int net_ip6conf;
#define IP6CONF_AUTOHOST 0x01
#endif
/* URL encode unsafe characters. */
static char *url_encode (char *dst, const char *src, size_t len,
const char *safe_chars,
int encode_leading_slash);
/* Get the list of network interfaces. */
static void get_ifinterface_info (void);
static void write_etc_hosts(FILE *f);
#define DHCLIENT_EX "/sbin/dhclient"
#include <signal.h>
static int config_dhcp(char *);
static void get_command_out(char *, size_t, int, const char *, const char *);
static void get_dhcp_value(char *, size_t, const char *);
#ifdef INET6
static int is_v6kernel (void);
static void init_v6kernel (int);
static int get_v6wait (void);
#endif
/*
* URL encode unsafe characters. See RFC 1738.
*
* Copies src string to dst, encoding unsafe or reserved characters
* in %hex form as it goes, and returning a pointer to the result.
* The result is always a nul-terminated string even if it had to be
* truncated to avoid overflowing the available space.
*
* This url_encode() function does not operate on complete URLs, it
* operates on strings that make up parts of URLs. For example, in a
* URL like "ftp://username:password@host/path", the username, password,
* host and path should each be encoded separately before they are
* joined together with the punctuation characters.
*
* In most ordinary use, the path portion of a URL does not start with
* a slash; the slash is a separator between the host portion and the
* path portion, and is dealt with by software outside the url_encode()
* function. However, it is valid for url_encode() to be passed a
* string that does begin with a slash. For example, the string might
* represent a password, or a path part of a URL that the user really
* does want to begin with a slash.
*
* len is the length of the destination buffer. The result will be
* truncated if necessary to fit in the destination buffer.
*
* safe_chars is a string of characters that should not be encoded. If
* safe_chars is non-NULL, any characters in safe_chars as well as any
* alphanumeric characters will be copied from src to dst without
* encoding. Some potentially useful settings for this parameter are:
*
* NULL Everything is encoded (even alphanumerics)
* "" Everything except alphanumerics are encoded
* "/" Alphanumerics and '/' remain unencoded
* "$-_.+!*'()," Consistent with a strict reading of RFC 1738
* "$-_.+!*'(),/" As above, except '/' is not encoded
* "-_.+!,/" As above, except shell special characters are encoded
*
* encode_leading_slash is a flag that determines whether or not to
* encode a leading slash in a string. If this flag is set, and if the
* first character in the src string is '/', then the leading slash will
* be encoded (as "%2F"), even if '/' is one of the characters in the
* safe_chars string. Note that only the first character of the src
* string is affected by this flag, and that leading slashes are never
* deleted, but either retained unchanged or encoded.
*
* Unsafe and reserved characters are defined in RFC 1738 section 2.2.
* The most important parts are:
*
* The characters ";", "/", "?", ":", "@", "=" and "&" are the
* characters which may be reserved for special meaning within a
* scheme. No other characters may be reserved within a scheme.
* [...]
*
* Thus, only alphanumerics, the special characters "$-_.+!*'(),",
* and reserved characters used for their reserved purposes may be
* used unencoded within a URL.
*
*/
#define RFC1738_SAFE "$-_.+!*'(),"
#define RFC1738_SAFE_LESS_SHELL "-_.+!,"
#define RFC1738_SAFE_LESS_SHELL_PLUS_SLASH "-_.+!,/"
static char *
url_encode(char *dst, const char *src, size_t len,
const char *safe_chars, int encode_leading_slash)
{
char *p = dst;
char *ep = dst + len;
/*
* If encoding of a leading slash was desired, and there was in
* fact one or more leading slashes, encode one in the output string.
*/
if (encode_leading_slash && *src == '/') {
if (ep - p < 3)
goto done;
snprintf(p, ep - p, "%%%02X", '/');
src++;
p += 3;
}
while (ep - p > 1 && *src != '\0') {
if (safe_chars != NULL &&
(isalnum(*src) || strchr(safe_chars, *src))) {
*p++ = *src++;
} else {
/* encode this char */
if (ep - p < 3)
break;
snprintf(p, ep - p, "%%%02X", *src++);
p += 3;
}
}
done:
*p = '\0';
return dst;
}
static const char *ignored_if_names[] = {
"eon", /* netiso */
"gre", /* net */
"ipip", /* netinet */
"gif", /* netinet6 */
"faith", /* netinet6 */
"lo", /* net */
#if 0
"mdecap", /* netinet -- never in IF list (?) XXX */
#endif
"nsip", /* netns */
"ppp", /* net */
"sl", /* net */
"strip", /* net */
"tun", /* net */
/* XXX others? */
NULL,
};
static void
get_ifconfig_info(void)
{
char *textbuf;
char *t, *nt;
const char **ignore;
int textsize;
ulong fl;
char *cp;
free(net_devices);
net_devices = NULL;
free(net_up);
net_up = NULL;
/* Get ifconfig information */
textsize = collect(T_OUTPUT, &textbuf, "/sbin/ifconfig -a 2>/dev/null");
if (textsize < 0) {
if (logging)
(void)fprintf(logfp,
"Aborting: Could not run ifconfig.\n");
(void)fprintf(stderr, "Could not run ifconfig.");
exit(1);
}
for (t = textbuf; t != NULL && *t != 0; t = nt) {
/* find entry for next interface */
for (nt = t; (nt = strchr(nt, '\n')); ) {
if (*++nt != '\t')
break;
}
if (memcmp(t, "lo0:", 4) == 0)
/* completely ignore loopback interface */
continue;
cp = strchr(t, '=');
if (cp == NULL)
break;
/* get interface flags */
fl = strtoul(cp + 1, &cp, 16);
if (*cp != '<')
break;
if (fl & IFF_UP) {
/* This interface might be connected to the server */
cp = strchr(t, ':');
if (cp == NULL)
break;
asprintf(&cp, "%s%.*s ",
net_up ? net_up : "", (int)(cp - t), t);
free(net_up);
net_up = cp;
}
for (ignore = ignored_if_names; *ignore != NULL; ignore++) {
size_t len = strlen(*ignore);
if (strncmp(t, *ignore, len) == 0 &&
isdigit((unsigned char)t[len]))
break;
}
if (*ignore != NULL)
continue;
cp = strchr(t, ':');
if (cp == NULL)
break;
asprintf(&cp, "%s%.*s ",
net_devices ? net_devices : "", (int)(cp - t), t);
free(net_devices);
net_devices = cp;
}
free(textbuf);
}
/* Fill in defaults network values for the selected interface */
static void
get_ifinterface_info(void)
{
char *textbuf;
int textsize;
char *t;
char hostname[MAXHOSTNAMELEN + 1];
char *dot;
/* First look to see if the selected interface is already configured. */
textsize = collect(T_OUTPUT, &textbuf,
"/sbin/ifconfig %s inet 2>/dev/null", net_dev);
if (textsize >= 0) {
(void)strtok(textbuf, " \t\n"); /* ignore interface name */
while ((t = strtok(NULL, " \t\n")) != NULL) {
if (strcmp(t, "inet") == 0) {
t = strtok(NULL, " \t\n");
if (strcmp(t, "0.0.0.0") != 0)
strlcpy(net_ip, t, sizeof(net_ip));
continue;
}
if (strcmp(t, "netmask") == 0) {
t = strtok(NULL, " \t\n");
if (strcmp(t, "0x0") != 0)
strlcpy(net_mask, t, sizeof(net_mask));
continue;
}
if (strcmp(t, "media:") == 0) {
t = strtok(NULL, " \t\n");
/* handle "media: Ethernet manual" */
if (strcmp(t, "Ethernet") == 0)
t = strtok(NULL, " \t\n");
if (strcmp(t, "none") != 0 &&
strcmp(t, "manual") != 0)
strlcpy(net_media, t,
sizeof(net_media));
}
}
}
free(textbuf);
#ifdef INET6
textsize = collect(T_OUTPUT, &textbuf,
"/sbin/ifconfig %s inet6 2>/dev/null", net_dev);
if (textsize >= 0) {
char *p;
(void)strtok(textbuf, "\n"); /* ignore first line */
while ((t = strtok(NULL, "\n")) != NULL) {
if (strncmp(t, "\tinet6 ", 7) != 0)
continue;
t += 7;
if (strstr(t, "tentative") || strstr(t, "duplicated"))
continue;
if (strncmp(t, "fe80:", 5) == 0)
continue;
p = t;
while (*p && *p != ' ' && *p != '\n')
p++;
*p = '\0';
strlcpy(net_ip6, t, sizeof(net_ip6));
break;
}
}
free(textbuf);
#endif
/* Check host (and domain?) name */
if (gethostname(hostname, sizeof(hostname)) == 0) {
hostname[sizeof(hostname) - 1] = 0;
/* check for a . */
dot = strchr(hostname, '.');
if (dot == NULL) {
/* if not found its just a host, punt on domain */
strlcpy(net_host, hostname, sizeof net_host);
} else {
/* split hostname into host/domain parts */
*dot++ = 0;
strlcpy(net_host, hostname, sizeof net_host);
strlcpy(net_domain, dot, sizeof net_domain);
}
}
}
/*
* recombine name parts split in get_ifinterface_info and config_network
* (common code moved here from write_etc_hosts)
*/
static char *
recombine_host_domain(void)
{
static char recombined[MAXHOSTNAMELEN + 1];
int l = strlen(net_host) - strlen(net_domain);
strlcpy(recombined, net_host, sizeof(recombined));
if (l <= 0 ||
net_host[l - 1] != '.' ||
strcasecmp(net_domain, net_host + l) != 0) {
/* net_host isn't an FQDN. */
strlcat(recombined, ".", sizeof(recombined));
strlcat(recombined, net_domain, sizeof(recombined));
}
return recombined;
}
#ifdef INET6
static int
is_v6kernel(void)
{
int s;
s = socket(PF_INET6, SOCK_DGRAM, 0);
if (s < 0)
return 0;
close(s);
return 1;
}
/*
* initialize as v6 client.
* we are sure that we will never become router with boot floppy :-)
* (include and use sysctl(8) if you are willing to)
*/
static void
init_v6kernel(int autoconf)
{
int v;
int mib[4] = {CTL_NET, PF_INET6, IPPROTO_IPV6, 0};
mib[3] = IPV6CTL_FORWARDING;
v = 0;
if (sysctl(mib, 4, NULL, NULL, (void *)&v, sizeof(v)) < 0)
; /* warn("sysctl(net.inet6.ip6.fowarding"); */
mib[3] = IPV6CTL_ACCEPT_RTADV;
v = autoconf ? 1 : 0;
if (sysctl(mib, 4, NULL, NULL, (void *)&v, sizeof(v)) < 0)
; /* warn("sysctl(net.inet6.ip6.accept_rtadv"); */
}
static int
get_v6wait(void)
{
size_t len = sizeof(int);
int v;
int mib[4] = {CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_DAD_COUNT};
len = sizeof(v);
if (sysctl(mib, 4, (void *)&v, &len, NULL, 0) < 0) {
/* warn("sysctl(net.inet6.ip6.dadcount)"); */
return 1; /* guess */
}
return v;
}
#endif
/*
* Get the information to configure the network, configure it and
* make sure both the gateway and the name server are up.
*/
int
config_network(void)
{
char *tp;
char *defname;
const char *prompt;
int octet0;
int pass, dhcp_config;
int l;
#ifdef INET6
int v6config = 1;
#endif
FILE *f;
time_t now;
if (network_up)
return (1);
get_ifconfig_info();
if (net_up != NULL) {
/* active interfaces found */
msg_display(MSG_netup, net_up);
process_menu(MENU_yesno, NULL);
if (yesno)
return 1;
}
if (net_devices == NULL) {
/* No network interfaces found! */
msg_display(MSG_nonet);
process_menu(MENU_ok, NULL);
return (-1);
}
network_up = 1;
again:
tp = strchr(net_devices, ' ');
asprintf(&defname, "%.*s", (int)(tp - net_devices), net_devices);
for (prompt = MSG_asknetdev;; prompt = MSG_badnet) {
msg_prompt(prompt, defname, net_dev, sizeof net_dev - 1,
net_devices);
l = strlen(net_dev);
net_dev[l] = ' ';
net_dev[l + 1] = 0;
tp = strstr(net_devices, net_dev);
if (tp == NULL)
continue;
if (tp != net_devices && tp[-1] != ' ')
continue;
net_dev[l] = 0;
break;
}
free(defname);
/* Preload any defaults we can find */
get_ifinterface_info();
pass = net_mask[0] == '\0' ? 0 : 1;
/* domain and host */
msg_display(MSG_netinfo);
/* ethernet medium */
if (net_media[0] != '\0')
msg_prompt_add(MSG_net_media, net_media, net_media,
sizeof net_media);
/* try a dhcp configuration */
dhcp_config = config_dhcp(net_dev);
if (dhcp_config) {
/* Get newly configured data off interface. */
get_ifinterface_info();
net_dhcpconf |= DHCPCONF_IPADDR;
/* run route show and extract data */
get_command_out(net_defroute, sizeof(net_defroute), AF_INET,
"/sbin/route -n show -inet 2>/dev/null", "default");
/* pull nameserver info out of /etc/resolv.conf */
get_command_out(net_namesvr, sizeof(net_namesvr), AF_INET,
"cat /etc/resolv.conf 2> /dev/null", "nameserver");
if (net_namesvr[0] != '\0')
net_dhcpconf |= DHCPCONF_NAMESVR;
/* pull domainname out of leases file */
get_dhcp_value(net_domain, sizeof(net_domain), "domain-name");
if (net_domain[0] != '\0')
net_dhcpconf |= DHCPCONF_DOMAIN;
/* pull hostname out of leases file */
get_dhcp_value(net_host, sizeof(net_host), "hostname");
if (net_host[0] != '\0')
net_dhcpconf |= DHCPCONF_HOST;
}
msg_prompt_add(MSG_net_domain, net_domain, net_domain,
sizeof net_domain);
msg_prompt_add(MSG_net_host, net_host, net_host, sizeof net_host);
if (!dhcp_config) {
/* Manually configure IPv4 */
msg_prompt_add(MSG_net_ip, net_ip, net_ip, sizeof net_ip);
octet0 = atoi(net_ip);
if (!pass) {
if (0 <= octet0 && octet0 <= 127)
strlcpy(net_mask, "0xff000000",
sizeof(net_mask));
else if (128 <= octet0 && octet0 <= 191)
strlcpy(net_mask, "0xffff0000",
sizeof(net_mask));
else if (192 <= octet0 && octet0 <= 223)
strlcpy(net_mask, "0xffffff00",
sizeof(net_mask));
}
msg_prompt_add(MSG_net_mask, net_mask, net_mask,
sizeof net_mask);
msg_prompt_add(MSG_net_defroute, net_defroute, net_defroute,
sizeof net_defroute);
msg_prompt_add(MSG_net_namesrv, net_namesvr, net_namesvr,
sizeof net_namesvr);
}
#ifdef INET6
/* IPv6 autoconfiguration */
if (!is_v6kernel())
v6config = 0;
else if (v6config) {
process_menu(MENU_ip6autoconf, NULL);
v6config = yesno ? 1 : 0;
net_ip6conf |= yesno ? IP6CONF_AUTOHOST : 0;
}
if (v6config) {
process_menu(MENU_namesrv6, NULL);
if (!yesno)
msg_prompt_add(MSG_net_namesrv6, net_namesvr6,
net_namesvr6, sizeof net_namesvr6);
}
#endif
/* confirm the setting */
msg_display(MSG_netok, net_domain, net_host, net_dev,
*net_ip == '\0' ? "<none>" : net_ip,
*net_mask == '\0' ? "<none>" : net_mask,
*net_namesvr == '\0' ? "<none>" : net_namesvr,
*net_defroute == '\0' ? "<none>" : net_defroute,
*net_media == '\0' ? "<default>" : net_media);
#ifdef INET6
msg_display_add(MSG_netokv6,
!is_v6kernel() ? "<not supported>" :
(v6config ? "yes" : "no"),
*net_namesvr6 == '\0' ? "<none>" : net_namesvr6);
#endif
process_menu(MENU_yesno, NULL);
if (!yesno)
msg_display(MSG_netagain);
pass++;
if (!yesno)
goto again;
/*
* we may want to perform checks against inconsistent configuration,
* like IPv4 DNS server without IPv4 configuration.
*/
/* Create /etc/resolv.conf if a nameserver was given */
if (net_namesvr[0] != '\0'
#ifdef INET6
|| net_namesvr6[0] != '\0'
#endif
) {
#ifdef DEBUG
f = fopen("/tmp/resolv.conf", "w");
#else
f = fopen("/etc/resolv.conf", "w");
#endif
if (f == NULL) {
if (logging)
(void)fprintf(logfp,
"%s", msg_string(MSG_resolv));
(void)fprintf(stderr, "%s", msg_string(MSG_resolv));
exit(1);
}
scripting_fprintf(NULL, "cat <<EOF >/etc/resolv.conf\n");
time(&now);
/* NB: ctime() returns a string ending in '\n' */
scripting_fprintf(f, ";\n; BIND data file\n; %s %s;\n",
"Created by NetBSD sysinst on", ctime(&now));
if (net_domain[0] != '\0')
scripting_fprintf(f, "search %s\n", net_domain);
if (net_namesvr[0] != '\0')
scripting_fprintf(f, "nameserver %s\n", net_namesvr);
#ifdef INET6
if (net_namesvr6[0] != '\0')
scripting_fprintf(f, "nameserver %s\n", net_namesvr6);
#endif
scripting_fprintf(NULL, "EOF\n");
fflush(NULL);
fclose(f);
}
run_program(0, "/sbin/ifconfig lo0 127.0.0.1");
/*
* ifconfig does not allow media specifiers on IFM_MANUAL interfaces.
* Our UI gives no way to set an option back to null-string if it
* gets accidentally set.
* Check for plausible alternatives.
*/
if (strcmp(net_media, "<default>") == 0 ||
strcmp(net_media, "default") == 0 ||
strcmp(net_media, "<manual>") == 0 ||
strcmp(net_media, "manual") == 0 ||
strcmp(net_media, "<none>") == 0 ||
strcmp(net_media, "none") == 0 ||
strcmp(net_media, " ") == 0) {
*net_media = '\0';
}
if (*net_media != '\0')
run_program(0, "/sbin/ifconfig %s media %s",
net_dev, net_media);
#ifdef INET6
if (v6config) {
init_v6kernel(1);
run_program(0, "/sbin/ifconfig %s up", net_dev);
sleep(get_v6wait() + 1);
run_program(RUN_DISPLAY, "/sbin/rtsol -D %s", net_dev);
sleep(get_v6wait() + 1);
}
#endif
if (net_ip[0] != '\0') {
if (net_mask[0] != '\0') {
run_program(0, "/sbin/ifconfig %s inet %s netmask %s",
net_dev, net_ip, net_mask);
} else {
run_program(0, "/sbin/ifconfig %s inet %s",
net_dev, net_ip);
}
}
/* Set host name */
if (net_host[0] != '\0')
sethostname(net_host, strlen(net_host));
/* Set a default route if one was given */
if (net_defroute[0] != '\0') {
run_program(0, "/sbin/route -n flush -inet");
run_program(0, "/sbin/route -n add default %s", net_defroute);
}
/*
* wait a couple of seconds for the interface to go live.
*/
msg_display_add(MSG_wait_network);
sleep(5);
/*
* ping should be verbose, so users can see the cause
* of a network failure.
*/
#ifdef INET6
if (v6config && network_up) {
network_up = !run_program(RUN_DISPLAY | RUN_PROGRESS,
"/sbin/ping6 -v -c 3 -n -I %s ff02::2", net_dev);
if (net_namesvr6[0] != '\0')
network_up = !run_program(RUN_DISPLAY | RUN_PROGRESS,
"/sbin/ping6 -v -c 3 -n %s", net_namesvr6);
}
#endif
if (net_namesvr[0] != '\0' && network_up)
network_up = !run_program(RUN_DISPLAY | RUN_PROGRESS,
"/sbin/ping -v -c 5 -w 5 -o -n %s", net_namesvr);
if (net_defroute[0] != '\0' && network_up)
network_up = !run_program(RUN_DISPLAY | RUN_PROGRESS,
"/sbin/ping -v -c 5 -w 5 -o -n %s", net_defroute);
fflush(NULL);
return network_up;
}
int
get_via_ftp(void)
{
distinfo *list;
char ftp_user_encoded[STRSIZE];
char ftp_pass_encoded[STRSIZE];
char ftp_dir_encoded[STRSIZE];
char filename[SSTRSIZE];
int ret;
while ((ret = config_network()) <= 0) {
if (ret < 0)
return (-1);
msg_display(MSG_netnotup);
process_menu(MENU_yesno, NULL);
if (!yesno) {
msg_display(MSG_netnotup_continueanyway);
process_menu(MENU_yesno, NULL);
if (!yesno)
return 0;
network_up = 1;
}
}
cd_dist_dir("ftp");
process_menu(MENU_ftpsource, NULL);
list = dist_list;
while (list->desc) {
if (list->name == NULL || (sets_selected & list->set) == 0) {
list++;
continue;
}
(void)snprintf(filename, sizeof filename, "%s%s", list->name,
dist_postfix);
/*
* Invoke ftp to fetch the file.
*
* ftp_pass is quite likely to contain unsafe characters
* that need to be encoded in the URL (for example,
* "@", ":" and "/" need quoting). Let's be
* paranoid and also encode ftp_user and ftp_dir. (For
* example, ftp_dir could easily contain '~', which is
* unsafe by a strict reading of RFC 1738).
*/
if (strcmp("ftp", ftp_user) == 0 && ftp_pass[0] == 0)
ret = run_program(RUN_DISPLAY | RUN_PROGRESS,
"/usr/bin/ftp -a ftp://%s/%s/%s",
ftp_host,
url_encode(ftp_dir_encoded, ftp_dir,
sizeof ftp_dir_encoded,
RFC1738_SAFE_LESS_SHELL_PLUS_SLASH, 1),
filename);
else {
ret = run_program(RUN_DISPLAY | RUN_PROGRESS,
"/usr/bin/ftp ftp://%s:%s@%s/%s/%s",
url_encode(ftp_user_encoded, ftp_user,
sizeof ftp_user_encoded,
RFC1738_SAFE_LESS_SHELL, 0),
url_encode(ftp_pass_encoded, ftp_pass,
sizeof ftp_pass_encoded,
NULL, 0),
ftp_host,
url_encode(ftp_dir_encoded, ftp_dir,
sizeof ftp_dir_encoded,
RFC1738_SAFE_LESS_SHELL_PLUS_SLASH, 1),
filename);
}
if (ret) {
/* Error getting the file. Bad host name ... ? */
msg_display(MSG_ftperror_cont);
getchar();
wrefresh(curscr);
wmove(stdscr, 0, 0);
touchwin(stdscr);
wclear(stdscr);
wrefresh(stdscr);
msg_display(MSG_ftperror);
process_menu(MENU_yesno, NULL);
if (yesno)
process_menu(MENU_ftpsource, NULL);
else
return 0;
} else
list++;
}
wrefresh(curscr);
wmove(stdscr, 0, 0);
touchwin(stdscr);
wclear(stdscr);
wrefresh(stdscr);
#ifndef DEBUG
chdir("/"); /* back to current real root */
#endif
return (1);
}
int
get_via_nfs(void)
{
int ret;
while ((ret = config_network()) <= 0) {
if (ret < 0)
return (-1);
msg_display(MSG_netnotup);
process_menu(MENU_yesno, NULL);
if (!yesno) {
msg_display(MSG_netnotup_continueanyway);
process_menu(MENU_yesno, NULL);
if (!yesno)
return 0;
network_up = 1;
}
}
/* Get server and filepath */
process_menu(MENU_nfssource, NULL);
again:
umount_mnt2();
/* Mount it */
if (run_program(0, "/sbin/mount -r -o -2,-i,-r=1024 -t nfs %s:%s /mnt2",
nfs_host, nfs_dir)) {
msg_display(MSG_nfsbadmount, nfs_host, nfs_dir);
process_menu(MENU_nfsbadmount, NULL);
if (!yesno)
return (0);
if (!ignorerror)
goto again;
}
mnt2_mounted = 1;
/* Verify distribution files exist. */
if (distribution_sets_exist_p("/mnt2") == 0) {
msg_display(MSG_badsetdir, "/mnt2");
process_menu (MENU_nfsbadmount, NULL);
if (!yesno)
return (0);
if (!ignorerror)
goto again;
}
/* return location, don't clean... */
strlcpy(ext_dir, "/mnt2", sizeof(ext_dir));
clean_dist_dir = 0;
return 1;
}
/*
* write the new contents of /etc/hosts to the specified file
*/
static void
write_etc_hosts(FILE *f)
{
scripting_fprintf(f, "#\n");
scripting_fprintf(f, "# Added by NetBSD sysinst\n");
scripting_fprintf(f, "#\n");
if (net_domain[0] != '\0')
scripting_fprintf(f, "127.0.0.1 localhost.%s\n", net_domain);
scripting_fprintf(f, "%s\t", net_ip);
if (net_domain[0] != '\0')
scripting_fprintf(f, "%s ", recombine_host_domain());
scripting_fprintf(f, "%s\n", net_host);
}
/*
* Write the network config info the user entered via menus into the
* config files in the target disk. Be careful not to lose any
* information we don't immediately add back, in case the install
* target is the currently-active root.
*/
void
mnt_net_config(void)
{
char ans[8];
char ifconfig_fn[STRSIZE];
FILE *ifconf = NULL;
const char *yes = msg_string(MSG_Yes);
if (!network_up)
return;
msg_prompt(MSG_mntnetconfig, yes, ans, sizeof ans);
if (strcmp(ans, yes) != 0)
return;
/* Write hostname to /etc/rc.conf */
if ((net_dhcpconf & DHCPCONF_HOST) == 0)
add_rc_conf("hostname=%s\n", recombine_host_domain());
/* If not running in target, copy resolv.conf there. */
if ((net_dhcpconf & DHCPCONF_NAMESVR) == 0) {
#ifndef INET6
if (net_namesvr[0] != '\0')
dup_file_into_target("/etc/resolv.conf");
#else
/*
* not sure if it is a good idea, to allow dhcp config to
* override IPv6 configuration
*/
if (net_namesvr[0] != '\0' || net_namesvr6[0] != '\0')
dup_file_into_target("/etc/resolv.conf");
#endif
}
/*
* bring the interface up, it will be necessary for IPv6, and
* it won't make trouble with IPv4 case either
*/
snprintf(ifconfig_fn, sizeof ifconfig_fn, "/etc/ifconfig.%s", net_dev);
ifconf = target_fopen(ifconfig_fn, "w");
if (ifconf != NULL) {
scripting_fprintf(NULL, "cat <<EOF >>%s%s\n",
target_prefix(), ifconfig_fn);
scripting_fprintf(ifconf, "up\n");
scripting_fprintf(NULL, "EOF\n");
}
if ((net_dhcpconf & DHCPCONF_IPADDR) == 0) {
FILE *hosts;
/* Write IPaddr and netmask to /etc/ifconfig.if[0-9] */
if (ifconf != NULL) {
scripting_fprintf(NULL, "cat <<EOF >>%s%s\n",
target_prefix(), ifconfig_fn);
if (*net_media != '\0')
scripting_fprintf(ifconf,
"%s netmask %s media %s\n",
net_ip, net_mask, net_media);
else
scripting_fprintf(ifconf, "%s netmask %s\n",
net_ip, net_mask);
scripting_fprintf(NULL, "EOF\n");
}
/*
* Add IPaddr/hostname to /etc/hosts.
* Be careful not to clobber any existing contents.
* Relies on ordered seach of /etc/hosts. XXX YP?
*/
hosts = target_fopen("/etc/hosts", "a");
if (hosts != 0) {
scripting_fprintf(NULL, "cat <<EOF >>%s/etc/hosts\n",
target_prefix());
write_etc_hosts(hosts);
(void)fclose(hosts);
scripting_fprintf(NULL, "EOF\n");
fclose(hosts);
}
add_rc_conf("defaultroute=\"%s\"\n", net_defroute);
} else {
add_rc_conf("dhclient=YES\n");
add_rc_conf("dhclient_flags=\"%s\"\n", net_dev);
}
#ifdef INET6
if ((net_ip6conf & IP6CONF_AUTOHOST) != 0) {
add_rc_conf("ip6mode=autohost\n");
if (ifconf != NULL) {
scripting_fprintf(NULL, "cat <<EOF >>%s%s\n",
target_prefix(), ifconfig_fn);
scripting_fprintf(ifconf, "!rtsol $int\n");
scripting_fprintf(NULL, "EOF\n");
}
}
#endif
if (ifconf)
fclose(ifconf);
fflush(NULL);
}
int
config_dhcp(char *inter)
{
int dhcpautoconf;
int result;
char *textbuf;
int pid;
/* check if dhclient is running, if so, kill it */
result = collect(T_FILE, &textbuf, "/tmp/dhclient.pid");
if (result >= 0) {
pid = atoi(textbuf);
if (pid > 0) {
kill(pid, 15);
sleep(1);
kill(pid, 9);
}
}
free(textbuf);
if (!file_mode_match(DHCLIENT_EX, S_IFREG))
return 0;
process_menu(MENU_dhcpautoconf, NULL);
if (yesno) {
/* spawn off dhclient and wait for parent to exit */
dhcpautoconf = run_program(RUN_DISPLAY,
"%s -pf /tmp/dhclient.pid -lf /tmp/dhclient.leases %s",
DHCLIENT_EX, inter);
return dhcpautoconf ? 0 : 1;
}
return 0;
}
static void
get_command_out(char *targ, size_t l, int af, const char *command,
const char *search)
{
int textsize;
char *textbuf;
const char *t;
#ifndef INET6
struct in_addr in;
#else
struct in6_addr in;
#endif
textsize = collect(T_OUTPUT, &textbuf, command);
if (textsize < 0) {
if (logging)
(void)fprintf(logfp,
"Aborting: Could not run %s.\n", command);
(void)fprintf(stderr, "Could not run ifconfig.");
exit(1);
}
if (textsize >= 0) {
(void)strtok(textbuf, " \t\n"); /* ignore interface name */
while ((t = strtok(NULL, " \t\n")) != NULL) {
if (strcmp(t, search) == 0) {
t = strtok(NULL, " \t\n");
if (inet_pton(af, t, &in) == 1 &&
strcmp(t, "0.0.0.0") != 0) {
strlcpy(targ, t, l);
}
}
}
}
free(textbuf);
return;
}
static void
get_dhcp_value(char *targ, size_t l, const char *line)
{
int textsize;
char *textbuf;
char *t;
char *walkp;
textsize = collect(T_FILE, &textbuf, "/tmp/dhclient.leases");
if (textsize < 0) {
if (logging)
(void)fprintf(logfp,
"Could not open file /tmp/dhclient.leases.\n");
(void)fprintf(stderr, "Could not open /tmp/dhclient.leases\n");
/* not fatal, just assume value not found */
}
if (textsize >= 0) {
(void)strtok(textbuf, " \t\n"); /* jump past 'lease' */
while ((t = strtok(NULL, " \t\n")) != NULL) {
if (strcmp(t, line) == 0) {
t = strtok(NULL, " \t\n");
/* found the tag, extract the value */
/* last char should be a ';' */
walkp = strrchr(t, ';');
if (walkp != NULL) {
*walkp = '\0';
}
/* strip any " from the string */
walkp = strrchr(t, '"');
if (walkp != NULL) {
*walkp = '\0';
t++;
}
strlcpy(targ, t, l);
break;
}
}
}
free(textbuf);
return;
}
|