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