net.c revision 1.6 1 /* $NetBSD: net.c,v 1.6 2014/09/12 20:42:13 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 /*
803 * we may want to perform checks against inconsistent configuration,
804 * like IPv4 DNS server without IPv4 configuration.
805 */
806
807 /* Create /etc/resolv.conf if a nameserver was given */
808 if (net_namesvr[0] != '\0'
809 #ifdef INET6
810 || net_namesvr6[0] != '\0'
811 #endif
812 ) {
813 f = fopen("/etc/resolv.conf", "w");
814 if (f == NULL) {
815 if (logfp)
816 (void)fprintf(logfp,
817 "%s", msg_string(MSG_resolv));
818 (void)fprintf(stderr, "%s", msg_string(MSG_resolv));
819 exit(1);
820 }
821 scripting_fprintf(NULL, "cat <<EOF >/etc/resolv.conf\n");
822 time(&now);
823 /* NB: ctime() returns a string ending in '\n' */
824 scripting_fprintf(f, ";\n; BIND data file\n; %s %s;\n",
825 "Created by NetBSD sysinst on", ctime(&now));
826 if (net_domain[0] != '\0')
827 scripting_fprintf(f, "search %s\n", net_domain);
828 if (net_namesvr[0] != '\0')
829 scripting_fprintf(f, "nameserver %s\n", net_namesvr);
830 #ifdef INET6
831 if (net_namesvr6[0] != '\0')
832 scripting_fprintf(f, "nameserver %s\n", net_namesvr6);
833 #endif
834 scripting_fprintf(NULL, "EOF\n");
835 fflush(NULL);
836 fclose(f);
837 }
838
839 run_program(0, "/sbin/ifconfig lo0 127.0.0.1");
840
841 #ifdef INET6
842 if (v6config && !nfs_root) {
843 init_v6kernel(1);
844 run_program(0, "/sbin/ifconfig %s up", net_dev);
845 sleep(get_v6wait() + 1);
846 run_program(RUN_DISPLAY, "/sbin/rtsol -D %s", net_dev);
847 sleep(get_v6wait() + 1);
848 }
849 #endif
850
851 if (net_ip[0] != '\0') {
852 if (slip) {
853 /* XXX: needs 'ifconfig sl0 create' much earlier */
854 /* Set SLIP interface UP */
855 run_program(0, "/sbin/ifconfig %s inet %s %s up",
856 net_dev, net_ip, net_srv_ip);
857 strcpy(sl_flags, "-s 115200 -l /dev/tty00");
858 msg_prompt_win(MSG_slattach, -1, 12, 70, 0,
859 sl_flags, sl_flags, 255);
860
861 /* XXX: wtf isn't run_program() used here? */
862 pid = fork();
863 if (pid == 0) {
864 strcpy(buffer, "/sbin/slattach ");
865 strcat(buffer, sl_flags);
866 in_buf = buffer;
867
868 for (ap = slcmd; (*ap = strsep(&in_buf, " ")) != NULL;)
869 if (**ap != '\0')
870 ++ap;
871
872 execvp(slcmd[0], slcmd);
873 } else
874 wait4(pid, &status, WNOHANG, 0);
875 } else if (!nfs_root) {
876 if (net_mask[0] != '\0') {
877 run_program(0, "/sbin/ifconfig %s inet %s netmask %s",
878 net_dev, net_ip, net_mask);
879 } else {
880 run_program(0, "/sbin/ifconfig %s inet %s",
881 net_dev, net_ip);
882 }
883 }
884 }
885
886 /* Set host name */
887 if (net_host[0] != '\0')
888 sethostname(net_host, strlen(net_host));
889
890 /* Set a default route if one was given */
891 if (!nfs_root && net_defroute[0] != '\0') {
892 run_program(RUN_DISPLAY | RUN_PROGRESS,
893 "/sbin/route -n flush -inet");
894 run_program(RUN_DISPLAY | RUN_PROGRESS,
895 "/sbin/route -n add default %s", net_defroute);
896 }
897
898 /*
899 * wait a couple of seconds for the interface to go live.
900 */
901 if (!nfs_root) {
902 msg_display_add(MSG_wait_network);
903 sleep(5);
904 }
905
906 /*
907 * ping should be verbose, so users can see the cause
908 * of a network failure.
909 */
910
911 #ifdef INET6
912 if (v6config && network_up) {
913 network_up = !run_program(RUN_DISPLAY | RUN_PROGRESS,
914 "/sbin/ping6 -v -c 3 -n -I %s ff02::2", net_dev);
915
916 if (net_namesvr6[0] != '\0')
917 network_up = !run_program(RUN_DISPLAY | RUN_PROGRESS,
918 "/sbin/ping6 -v -c 3 -n %s", net_namesvr6);
919 }
920 #endif
921
922 if (net_namesvr[0] != '\0' && network_up)
923 network_up = !run_program(RUN_DISPLAY | RUN_PROGRESS,
924 "/sbin/ping -v -c 5 -w 5 -o -n %s", net_namesvr);
925
926 if (net_defroute[0] != '\0' && network_up)
927 network_up = !run_program(RUN_DISPLAY | RUN_PROGRESS,
928 "/sbin/ping -v -c 5 -w 5 -o -n %s", net_defroute);
929 fflush(NULL);
930
931 return network_up;
932 }
933
934 void
935 make_url(char *urlbuffer, struct ftpinfo *f, const char *dir)
936 {
937 char ftp_user_encoded[STRSIZE];
938 char ftp_dir_encoded[STRSIZE];
939 char *cp;
940 const char *dir2;
941
942 /*
943 * f->pass is quite likely to contain unsafe characters
944 * that need to be encoded in the URL (for example,
945 * "@", ":" and "/" need quoting). Let's be
946 * paranoid and also encode f->user and f->dir. (For
947 * example, f->dir could easily contain '~', which is
948 * unsafe by a strict reading of RFC 1738).
949 */
950 if (strcmp("ftp", f->user) == 0 && f->pass[0] == 0) {
951 ftp_user_encoded[0] = 0;
952 } else {
953 cp = url_encode(ftp_user_encoded, f->user,
954 ftp_user_encoded + sizeof ftp_user_encoded - 1,
955 RFC1738_SAFE_LESS_SHELL, 0);
956 *cp++ = ':';
957 cp = url_encode(cp, f->pass,
958 ftp_user_encoded + sizeof ftp_user_encoded - 1,
959 NULL, 0);
960 *cp++ = '@';
961 *cp = 0;
962 }
963 cp = url_encode(ftp_dir_encoded, f->dir,
964 ftp_dir_encoded + sizeof ftp_dir_encoded - 1,
965 RFC1738_SAFE_LESS_SHELL_PLUS_SLASH, 1);
966 if (cp != ftp_dir_encoded && cp[-1] != '/')
967 *cp++ = '/';
968
969 dir2 = dir;
970 while (*dir2 == '/')
971 ++dir2;
972
973 url_encode(cp, dir2,
974 ftp_dir_encoded + sizeof ftp_dir_encoded,
975 RFC1738_SAFE_LESS_SHELL_PLUS_SLASH, 0);
976
977 snprintf(urlbuffer, STRSIZE, "%s://%s%s/%s", f->xfer_type,
978 ftp_user_encoded, f->host, ftp_dir_encoded);
979 }
980
981
982 /* ftp_fetch() and pkgsrc_fetch() are essentially the same, with a different
983 * ftpinfo var. */
984 static int do_ftp_fetch(const char *, struct ftpinfo *);
985
986 static int
987 ftp_fetch(const char *set_name)
988 {
989 return do_ftp_fetch(set_name, &ftp);
990 }
991
992 static int
993 pkgsrc_fetch(const char *set_name)
994 {
995 return do_ftp_fetch(set_name, &pkgsrc);
996 }
997
998 static int
999 do_ftp_fetch(const char *set_name, struct ftpinfo *f)
1000 {
1001 const char *ftp_opt;
1002 char url[STRSIZE];
1003 int rval;
1004
1005 /*
1006 * Invoke ftp to fetch the file.
1007 */
1008 if (strcmp("ftp", f->user) == 0 && f->pass[0] == 0) {
1009 /* do anon ftp */
1010 ftp_opt = "-a ";
1011 } else {
1012 ftp_opt = "";
1013 }
1014
1015 make_url(url, f, set_dir_for_set(set_name));
1016 rval = run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_XFER_DIR,
1017 "/usr/bin/ftp %s%s/%s%s",
1018 ftp_opt, url, set_name, dist_postfix);
1019
1020 return rval ? SET_RETRY : SET_OK;
1021 }
1022
1023
1024 // XXX: check MSG_netnotup_continueanyway and MSG_netnotup
1025
1026 int
1027 get_pkgsrc(void)
1028 {
1029 yesno = -1;
1030 process_menu(MENU_pkgsrc, NULL);
1031
1032 if (yesno == SET_SKIP)
1033 return SET_SKIP;
1034
1035 fetch_fn = pkgsrc_fetch;
1036 snprintf(ext_dir_pkgsrc, sizeof ext_dir_pkgsrc, "%s/%s",
1037 target_prefix(), xfer_dir + (*xfer_dir == '/'));
1038
1039 return SET_OK;
1040 }
1041
1042 int
1043 get_via_ftp(const char *xfer_type)
1044 {
1045 yesno = -1;
1046 process_menu(MENU_ftpsource, deconst(xfer_type));
1047
1048 if (yesno == SET_RETRY)
1049 return SET_RETRY;
1050
1051 /* We'll fetch each file just before installing it */
1052 fetch_fn = ftp_fetch;
1053 ftp.xfer_type = xfer_type;
1054 snprintf(ext_dir_bin, sizeof ext_dir_bin, "%s/%s", target_prefix(),
1055 xfer_dir + (*xfer_dir == '/'));
1056 snprintf(ext_dir_src, sizeof ext_dir_src, "%s/%s", target_prefix(),
1057 xfer_dir + (*xfer_dir == '/'));
1058
1059 return SET_OK;
1060 }
1061
1062 int
1063 get_via_nfs(void)
1064 {
1065 struct statvfs sb;
1066
1067 /* If root is on NFS and we have sets, skip this step. */
1068 if (statvfs(set_dir_bin, &sb) == 0 &&
1069 strcmp(sb.f_fstypename, "nfs") == 0) {
1070 strlcpy(ext_dir_bin, set_dir_bin, sizeof ext_dir_bin);
1071 strlcpy(ext_dir_src, set_dir_src, sizeof ext_dir_src);
1072 return SET_OK;
1073 }
1074
1075 /* Get server and filepath */
1076 yesno = -1;
1077 process_menu(MENU_nfssource, NULL);
1078
1079 if (yesno == SET_RETRY)
1080 return SET_RETRY;
1081
1082 /* Mount it */
1083 if (run_program(0, "/sbin/mount -r -o -2,-i,-r=1024 -t nfs %s:%s /mnt2",
1084 nfs_host, nfs_dir))
1085 return SET_RETRY;
1086
1087 mnt2_mounted = 1;
1088
1089 snprintf(ext_dir_bin, sizeof ext_dir_bin, "/mnt2/%s", set_dir_bin);
1090 snprintf(ext_dir_src, sizeof ext_dir_src, "/mnt2/%s", set_dir_src);
1091
1092 /* return location, don't clean... */
1093 return SET_OK;
1094 }
1095
1096 /*
1097 * write the new contents of /etc/hosts to the specified file
1098 */
1099 static void
1100 write_etc_hosts(FILE *f)
1101 {
1102 scripting_fprintf(f, "#\n");
1103 scripting_fprintf(f, "# Added by NetBSD sysinst\n");
1104 scripting_fprintf(f, "#\n");
1105
1106 if (net_domain[0] != '\0')
1107 scripting_fprintf(f, "127.0.0.1 localhost.%s\n", net_domain);
1108
1109 scripting_fprintf(f, "%s\t", net_ip);
1110 if (net_domain[0] != '\0')
1111 scripting_fprintf(f, "%s ", recombine_host_domain());
1112 scripting_fprintf(f, "%s\n", net_host);
1113 }
1114
1115 /*
1116 * Write the network config info the user entered via menus into the
1117 * config files in the target disk. Be careful not to lose any
1118 * information we don't immediately add back, in case the install
1119 * target is the currently-active root.
1120 */
1121 void
1122 mnt_net_config(void)
1123 {
1124 char ifconfig_fn[STRSIZE];
1125 FILE *ifconf = NULL;
1126
1127 if (!network_up)
1128 return;
1129 process_menu(MENU_yesno, deconst(MSG_mntnetconfig));
1130 if (!yesno)
1131 return;
1132
1133 /* Write hostname to /etc/rc.conf */
1134 if ((net_dhcpconf & DHCPCONF_HOST) == 0)
1135 if (del_rc_conf("hostname") == 0)
1136 add_rc_conf("hostname=%s\n", recombine_host_domain());
1137
1138 /* Copy resolv.conf to target. If DHCP was used to create it,
1139 * it will be replaced on next boot anyway. */
1140 #ifndef INET6
1141 if (net_namesvr[0] != '\0')
1142 dup_file_into_target("/etc/resolv.conf");
1143 #else
1144 /*
1145 * not sure if it is a good idea, to allow dhcp config to
1146 * override IPv6 configuration
1147 */
1148 if (net_namesvr[0] != '\0' || net_namesvr6[0] != '\0')
1149 dup_file_into_target("/etc/resolv.conf");
1150 #endif
1151
1152 /*
1153 * bring the interface up, it will be necessary for IPv6, and
1154 * it won't make trouble with IPv4 case either
1155 */
1156 snprintf(ifconfig_fn, sizeof ifconfig_fn, "/etc/ifconfig.%s", net_dev);
1157 ifconf = target_fopen(ifconfig_fn, "w");
1158 if (ifconf != NULL) {
1159 scripting_fprintf(NULL, "cat <<EOF >>%s%s\n",
1160 target_prefix(), ifconfig_fn);
1161 scripting_fprintf(ifconf, "up\n");
1162 if (*net_media != '\0')
1163 scripting_fprintf(ifconf, "media %s\n", net_media);
1164 scripting_fprintf(NULL, "EOF\n");
1165 }
1166
1167 if ((net_dhcpconf & DHCPCONF_IPADDR) == 0) {
1168 FILE *hosts;
1169
1170 /* Write IPaddr and netmask to /etc/ifconfig.if[0-9] */
1171 if (ifconf != NULL) {
1172 scripting_fprintf(NULL, "cat <<EOF >>%s%s\n",
1173 target_prefix(), ifconfig_fn);
1174 if (*net_media != '\0')
1175 scripting_fprintf(ifconf,
1176 "%s netmask %s media %s\n",
1177 net_ip, net_mask, net_media);
1178 else
1179 scripting_fprintf(ifconf, "%s netmask %s\n",
1180 net_ip, net_mask);
1181 scripting_fprintf(NULL, "EOF\n");
1182 }
1183
1184 /*
1185 * Add IPaddr/hostname to /etc/hosts.
1186 * Be careful not to clobber any existing contents.
1187 * Relies on ordered search of /etc/hosts. XXX YP?
1188 */
1189 hosts = target_fopen("/etc/hosts", "a");
1190 if (hosts != 0) {
1191 scripting_fprintf(NULL, "cat <<EOF >>%s/etc/hosts\n",
1192 target_prefix());
1193 write_etc_hosts(hosts);
1194 (void)fclose(hosts);
1195 scripting_fprintf(NULL, "EOF\n");
1196 }
1197
1198 if (del_rc_conf("defaultroute") == 0)
1199 add_rc_conf("defaultroute=\"%s\"\n", net_defroute);
1200 } else {
1201 /*
1202 * Start dhcpcd quietly and in master mode, but restrict
1203 * it to our interface
1204 */
1205 add_rc_conf("dhcpcd=YES\n");
1206 add_rc_conf("dhcpcd_flags=\"-qM %s\"\n", net_dev);
1207 }
1208
1209 #ifdef INET6
1210 if ((net_ip6conf & IP6CONF_AUTOHOST) != 0) {
1211 if (del_rc_conf("ip6mode") == 0)
1212 add_rc_conf("ip6mode=autohost\n");
1213 if (ifconf != NULL) {
1214 scripting_fprintf(NULL, "cat <<EOF >>%s%s\n",
1215 target_prefix(), ifconfig_fn);
1216 scripting_fprintf(ifconf, "!rtsol $int\n");
1217 scripting_fprintf(NULL, "EOF\n");
1218 }
1219 }
1220 #endif
1221
1222 if (ifconf)
1223 fclose(ifconf);
1224
1225 fflush(NULL);
1226 }
1227
1228 int
1229 config_dhcp(char *inter)
1230 {
1231 int dhcpautoconf;
1232
1233 /*
1234 * Don't bother checking for an existing instance of dhcpcd, just
1235 * ask it to renew the lease. It will fork and daemonize if there
1236 * wasn't already an instance.
1237 */
1238
1239 if (!file_mode_match(DHCPCD, S_IFREG))
1240 return 0;
1241 process_menu(MENU_yesno, deconst(MSG_Perform_DHCP_autoconfiguration));
1242 if (yesno) {
1243 /* spawn off dhcpcd and wait for parent to exit */
1244 dhcpautoconf = run_program(RUN_DISPLAY | RUN_PROGRESS,
1245 "%s -d -n %s", DHCPCD, inter);
1246 return dhcpautoconf ? 0 : 1;
1247 }
1248 return 0;
1249 }
1250