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