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