Home | History | Annotate | Line # | Download | only in sysinst
net.c revision 1.42
      1 /*	$NetBSD: net.c,v 1.42 2022/05/15 17:42:32 jmcneill 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, void *data)
    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 	ifr->ifr_data = data;
    289 	strlcpy(ifr->ifr_name, net_dev, sizeof ifr->ifr_name);
    290 	rval = ioctl(sock, cmd, ifr);
    291 	close(sock);
    292 
    293 	return rval;
    294 }
    295 
    296 static int
    297 do_ifmreq(struct ifmediareq *ifmr, unsigned long cmd)
    298 {
    299 	int sock;
    300 	int rval;
    301 
    302 	sock = socket(PF_INET, SOCK_DGRAM, 0);
    303 	if (sock == -1)
    304 		return -1;
    305 
    306 	memset(ifmr, 0, sizeof *ifmr);
    307 	strlcpy(ifmr->ifm_name, net_dev, sizeof ifmr->ifm_name);
    308 	rval = ioctl(sock, cmd, ifmr);
    309 	close(sock);
    310 
    311 	return rval;
    312 }
    313 
    314 /* Fill in defaults network values for the selected interface */
    315 static void
    316 get_ifinterface_info(void)
    317 {
    318 	struct ifreq ifr;
    319 	struct ifmediareq ifmr;
    320 	struct sockaddr_in *sa_in = (void*)&ifr.ifr_addr;
    321 	int modew;
    322 	const char *media_opt;
    323 	const char *sep;
    324 
    325 	if (do_ifreq(&ifr, SIOCGIFADDR, NULL) == 0 &&
    326 	    sa_in->sin_addr.s_addr != 0)
    327 		strlcpy(net_ip, inet_ntoa(sa_in->sin_addr), sizeof net_ip);
    328 
    329 	if (do_ifreq(&ifr, SIOCGIFNETMASK, NULL) == 0 &&
    330 	    sa_in->sin_addr.s_addr != 0)
    331 		strlcpy(net_mask, inet_ntoa(sa_in->sin_addr), sizeof net_mask);
    332 
    333 	if (do_ifmreq(&ifmr, SIOCGIFMEDIA) == 0) {
    334 		/* Get the name of the media word */
    335 		modew = ifmr.ifm_current;
    336 		strlcpy(net_media, get_media_subtype_string(modew),
    337 		    sizeof net_media);
    338 		/* and add any media options */
    339 		sep = " mediaopt ";
    340 		while ((media_opt = get_media_option_string(&modew)) != NULL) {
    341 			strlcat(net_media, sep, sizeof net_media);
    342 			strlcat(net_media, media_opt, sizeof net_media);
    343 			sep = ",";
    344 		}
    345 	}
    346 }
    347 
    348 #ifndef INET6
    349 #define get_if6interface_info()
    350 #else
    351 static void
    352 get_if6interface_info(void)
    353 {
    354 	char *textbuf, *t;
    355 	int textsize;
    356 
    357 	textsize = collect(T_OUTPUT, &textbuf,
    358 	    "/sbin/ifconfig %s inet6 2>/dev/null", net_dev);
    359 	if (textsize >= 0) {
    360 		char *p;
    361 
    362 		(void)strtok(textbuf, "\n"); /* ignore first line */
    363 		while ((t = strtok(NULL, "\n")) != NULL) {
    364 			if (strncmp(t, "\tinet6 ", 7) != 0)
    365 				continue;
    366 			t += 7;
    367 			if (strstr(t, "tentative") || strstr(t, "duplicated"))
    368 				continue;
    369 			if (strncmp(t, "fe80:", 5) == 0)
    370 				continue;
    371 
    372 			p = t;
    373 			while (*p && *p != ' ' && *p != '\n')
    374 				p++;
    375 			*p = '\0';
    376 			strlcpy(net_ip6, t, sizeof(net_ip6));
    377 			break;
    378 		}
    379 	}
    380 	free(textbuf);
    381 }
    382 #endif
    383 
    384 static void
    385 get_host_info(void)
    386 {
    387 	char hostname[MAXHOSTNAMELEN + 1];
    388 	char *dot;
    389 
    390 	/* Check host (and domain?) name */
    391 	if (gethostname(hostname, sizeof(hostname)) == 0 && hostname[0] != 0) {
    392 		hostname[sizeof(hostname) - 1] = 0;
    393 		/* check for a . */
    394 		dot = strchr(hostname, '.');
    395 		if (dot == NULL) {
    396 			/* if not found its just a host, punt on domain */
    397 			strlcpy(net_host, hostname, sizeof net_host);
    398 		} else {
    399 			/* split hostname into host/domain parts */
    400 			*dot++ = 0;
    401 			strlcpy(net_host, hostname, sizeof net_host);
    402 			strlcpy(net_domain, dot, sizeof net_domain);
    403 		}
    404 	}
    405 }
    406 
    407 /*
    408  * recombine name parts split in get_host_info and config_network
    409  * (common code moved here from write_etc_hosts)
    410  */
    411 static char *
    412 recombine_host_domain(void)
    413 {
    414 	static char recombined[MAXHOSTNAMELEN + 1];
    415 	int l = strlen(net_host) - strlen(net_domain);
    416 
    417 	strlcpy(recombined, net_host, sizeof(recombined));
    418 
    419 	if (strlen(net_domain) != 0 && (l <= 0 ||
    420 	    net_host[l - 1] != '.' ||
    421 	    strcasecmp(net_domain, net_host + l) != 0)) {
    422 		/* net_host isn't an FQDN. */
    423 		strlcat(recombined, ".", sizeof(recombined));
    424 		strlcat(recombined, net_domain, sizeof(recombined));
    425 	}
    426 	return recombined;
    427 }
    428 
    429 #ifdef INET6
    430 static int
    431 is_v6kernel(void)
    432 {
    433 	int s;
    434 
    435 	s = socket(PF_INET6, SOCK_DGRAM, 0);
    436 	if (s < 0)
    437 		return 0;
    438 	close(s);
    439 	return 1;
    440 }
    441 #endif
    442 
    443 static int
    444 handle_license(const char *dev)
    445 {
    446 	static struct {
    447 		const char *dev;
    448 		const char *lic;
    449 	} licdev[] = {
    450 		{ "iwi", "/libdata/firmware/if_iwi/LICENSE.ipw2200-fw" },
    451 		{ "ipw", "/libdata/firmware/if_ipw/LICENSE" },
    452 	};
    453 
    454 	size_t i;
    455 
    456 	for (i = 0; i < __arraycount(licdev); i++)
    457 		if (strncmp(dev, licdev[i].dev, 3) == 0) {
    458 			char buf[64];
    459 			int val;
    460 			size_t len = sizeof(int);
    461 			(void)snprintf(buf, sizeof(buf), "hw.%s.accept_eula",
    462 			    licdev[i].dev);
    463 			if (sysctlbyname(buf, &val, &len, NULL, 0) != -1
    464 			    && val != 0)
    465 				return 1;
    466 			msg_fmt_display(MSG_license, "%s%s",
    467 			    dev, licdev[i].lic);
    468 			if (ask_yesno(NULL)) {
    469 				val = 1;
    470 				if (sysctlbyname(buf, NULL, NULL, &val,
    471 				    0) == -1)
    472 					return 0;
    473 				add_sysctl_conf("%s=1", buf);
    474 				return 1;
    475 			} else
    476 				return 0;
    477 		}
    478 	return 1;
    479 }
    480 
    481 /*
    482  * Get the information to configure the network, configure it and
    483  * make sure both the gateway and the name server are up.
    484  */
    485 int
    486 config_network(void)
    487 {
    488 	char *textbuf;
    489 	int  octet0;
    490 	int  dhcp_config;
    491 	int  nfs_root = 0;
    492  	int  slip = 0;
    493  	int  pid, status;
    494  	char **ap, *slcmd[10], *in_buf;
    495  	char buffer[STRSIZE];
    496 	char hostname[MAXHOSTNAMELEN + 1];
    497  	struct statvfs sb;
    498 	struct net_desc net_devs[MAX_NETS];
    499 	menu_ent *net_menu;
    500 	int menu_no;
    501 	int num_devs;
    502 	int selected_net;
    503 	int i;
    504 #ifdef INET6
    505 	int v6config = 1, rv;
    506 #endif
    507 
    508 	FILE *f;
    509 	time_t now;
    510 
    511 	if (network_up)
    512 		return (1);
    513 
    514 	num_devs = get_ifconfig_info(net_devs);
    515 
    516 	if (num_devs < 1) {
    517 		/* No network interfaces found! */
    518 		hit_enter_to_continue(NULL, MSG_nonet);
    519 		return -1;
    520 	}
    521 
    522 	net_menu = calloc(num_devs, sizeof(*net_menu));
    523 	if (net_menu == NULL) {
    524 		err_msg_win(err_outofmem);
    525 		return -1;
    526 	}
    527 
    528 	for (i = 0; i < num_devs; i++) {
    529 		net_menu[i].opt_name = net_devs[i].if_dev;
    530 		net_menu[i].opt_flags = OPT_EXIT;
    531 		net_menu[i].opt_action = set_menu_select;
    532 	}
    533 
    534 	menu_no = new_menu(MSG_netdevs,
    535 		net_menu, num_devs, -1, 4, 0, 0,
    536 		MC_SCROLL,
    537 		NULL, NULL, NULL, NULL, MSG_cancel);
    538 again:
    539 	selected_net = -1;
    540 	msg_display(MSG_asknetdev);
    541 	process_menu(menu_no, &selected_net);
    542 
    543 	if (selected_net == -1) {
    544 		free_menu(menu_no);
    545 		free(net_menu);
    546 		return 0;
    547 	}
    548 
    549 	network_up = 1;
    550 	dhcp_config = 0;
    551 
    552 	strlcpy(net_dev, net_devs[selected_net].if_dev, sizeof net_dev);
    553 
    554 	if (!handle_license(net_dev))
    555 		goto done;
    556 
    557 	slip = net_dev[0] == 's' && net_dev[1] == 'l' &&
    558 	    isdigit((unsigned char)net_dev[2]);
    559 
    560 	/* If root is on NFS do not reconfigure the interface. */
    561 	if (statvfs("/", &sb) == 0 && strcmp(sb.f_fstypename, "nfs") == 0) {
    562 		nfs_root = 1;
    563 		get_ifinterface_info();
    564 		get_if6interface_info();
    565 		get_host_info();
    566 	} else if (!slip) {
    567 		/* Preload any defaults we can find */
    568 		get_ifinterface_info();
    569 		get_if6interface_info();
    570 		get_host_info();
    571 
    572 		/* domain and host */
    573 		msg_display(MSG_netinfo);
    574 
    575 		if (!config_wlan(net_dev)) {
    576 			config_eth_medium(net_dev);
    577 		}
    578 
    579 		net_dhcpconf = 0;
    580 		/* try a dhcp configuration */
    581 		dhcp_config = config_dhcp(net_dev);
    582 		if (dhcp_config) {
    583 			char *nline;
    584 
    585 			/* Get newly configured data off interface. */
    586 			get_ifinterface_info();
    587 			get_if6interface_info();
    588 			get_host_info();
    589 
    590 			net_dhcpconf |= DHCPCONF_IPADDR;
    591 
    592 			/*
    593 			 * Extract default route from output of
    594 			 * 'route -n show'
    595 			 */
    596 			if (collect(T_OUTPUT, &textbuf,
    597 			    "/sbin/route -n show | "
    598 			    "while read dest gateway flags;"
    599 			    " do [ \"$dest\" = default ] && {"
    600 			    " echo \"$gateway\"; break; };"
    601 			    " done" ) > 0)
    602 				strlcpy(net_defroute, textbuf,
    603 				    sizeof net_defroute);
    604 			free(textbuf);
    605 			if ((nline = strchr(net_defroute, '\n')))
    606 				*nline = '\0';
    607 
    608 			/* pull nameserver info out of /etc/resolv.conf */
    609 			if (collect(T_OUTPUT, &textbuf,
    610 			    "cat /etc/resolv.conf 2>/dev/null |"
    611 			    " while read keyword address rest;"
    612 			    " do [ \"$keyword\" = nameserver ] &&"
    613 			    " { echo \"$address\"; break; };"
    614 			    " done" ) > 0)
    615 				strlcpy(net_namesvr, textbuf,
    616 				    sizeof net_namesvr);
    617 			free(textbuf);
    618 			if ((nline = strchr(net_namesvr, '\n')))
    619 				*nline = '\0';
    620 			if (net_namesvr[0] != '\0')
    621 				net_dhcpconf |= DHCPCONF_NAMESVR;
    622 
    623 			/* pull domain info out of /etc/resolv.conf */
    624 			if (collect(T_OUTPUT, &textbuf,
    625 			    "cat /etc/resolv.conf 2>/dev/null |"
    626 			    " while read keyword domain rest;"
    627 			    " do [ \"$keyword\" = domain ] &&"
    628 			    " { echo \"$domain\"; break; };"
    629 			    " done" ) > 0)
    630 				strlcpy(net_domain, textbuf,
    631 				    sizeof net_domain);
    632 			free(textbuf);
    633 			if (net_domain[0] == '\0') {
    634 				/* pull domain info out of /etc/resolv.conf */
    635 				if (collect(T_OUTPUT, &textbuf,
    636 				    "cat /etc/resolv.conf 2>/dev/null |"
    637 				    " while read keyword search rest;"
    638 				    " do [ \"$keyword\" = search ] &&"
    639 				    " { echo \"$search\"; break; };"
    640 				    " done" ) > 0)
    641 					strlcpy(net_domain, textbuf,
    642 					    sizeof net_domain);
    643 				free(textbuf);
    644 			}
    645 			if ((nline = strchr(net_domain, '\n')))
    646 				*nline = '\0';
    647 			if (net_domain[0] != '\0')
    648 				net_dhcpconf |= DHCPCONF_DOMAIN;
    649 
    650 			if (gethostname(net_host, sizeof(net_host)) == 0 &&
    651 			    net_host[0] != 0)
    652 				net_dhcpconf |= DHCPCONF_HOST;
    653 		}
    654 	}
    655 
    656 	/*
    657 	 * Prompt for hostname and domain, even when using DHCP. The names
    658 	 * discovered on the network may not match the desired values
    659 	 * for the target system.
    660 	 */
    661 	strlcpy(hostname, recombine_host_domain(), MAXHOSTNAMELEN);
    662 	msg_prompt_add(MSG_net_host, net_host, net_host,
    663 	    sizeof net_host);
    664 	msg_prompt_add(MSG_net_domain, net_domain, net_domain,
    665 	    sizeof net_domain);
    666 	if (strcmp(hostname, recombine_host_domain()) != 0) {
    667 		net_dhcpconf &= ~(DHCPCONF_DOMAIN|DHCPCONF_HOST);
    668 	}
    669 
    670 	if (!dhcp_config) {
    671 		/* Manually configure IPv4 */
    672 		if (!nfs_root)
    673 			msg_prompt_add(MSG_net_ip, net_ip, net_ip,
    674 			    sizeof net_ip);
    675 		if (slip)
    676 			msg_prompt_add(MSG_net_srv_ip, net_srv_ip, net_srv_ip,
    677 			    sizeof net_srv_ip);
    678 		else if (!nfs_root) {
    679 			/* We don't want netmasks for SLIP */
    680 			octet0 = atoi(net_ip);
    681 			if (!net_mask[0]) {
    682 				if (0 <= octet0 && octet0 <= 127)
    683 					strlcpy(net_mask, "0xff000000",
    684 				    	sizeof(net_mask));
    685 				else if (128 <= octet0 && octet0 <= 191)
    686 					strlcpy(net_mask, "0xffff0000",
    687 				    	sizeof(net_mask));
    688 				else if (192 <= octet0 && octet0 <= 223)
    689 					strlcpy(net_mask, "0xffffff00",
    690 				    	sizeof(net_mask));
    691 			}
    692 			msg_prompt_add(MSG_net_mask, net_mask, net_mask,
    693 			    sizeof net_mask);
    694 		}
    695 		msg_prompt_add(MSG_net_defroute, net_defroute, net_defroute,
    696 		    sizeof net_defroute);
    697 	}
    698 
    699 	if (!(net_dhcpconf & DHCPCONF_NAMESVR)) {
    700 #ifdef INET6
    701 		if (v6config) {
    702 			rv = 0;
    703 			process_menu(MENU_namesrv6, &rv);
    704 			if (!rv)
    705 				msg_prompt_add(MSG_net_namesrv, net_namesvr,
    706 				    net_namesvr, sizeof net_namesvr);
    707 		} else
    708 #endif
    709 		msg_prompt_add(MSG_net_namesrv, net_namesvr, net_namesvr,
    710 		    sizeof net_namesvr);
    711 	}
    712 
    713 	/* confirm the setting */
    714 	msg_clear();
    715 	if (slip)
    716 		msg_fmt_table_add(MSG_netok_slip, "%s%s%s%s%s%s%s%s%s",
    717 		    net_domain,
    718 		    net_host,
    719 		    *net_namesvr == '\0' ? "<none>" : net_namesvr,
    720 		    net_dev,
    721 		    *net_media == '\0' ? "<default>" : net_media,
    722 		    *net_ip == '\0' ? "<none>" : net_ip,
    723 		    *net_srv_ip == '\0' ? "<none>" : net_srv_ip,
    724 		    *net_mask == '\0' ? "<none>" : net_mask,
    725 		    *net_defroute == '\0' ? "<none>" : net_defroute);
    726 	else
    727 		msg_fmt_table_add(MSG_netok, "%s%s%s%s%s%s%s%s",
    728 		    net_domain,
    729 		    net_host,
    730 		    *net_namesvr == '\0' ? "<none>" : net_namesvr,
    731 		    net_dev,
    732 		    *net_media == '\0' ? "<default>" : net_media,
    733 		    *net_ip == '\0' ? "<none>" : net_ip,
    734 		    *net_mask == '\0' ? "<none>" : net_mask,
    735 		    *net_defroute == '\0' ? "<none>" : net_defroute);
    736 #ifdef INET6
    737 	msg_fmt_table_add(MSG_netokv6, "%s",
    738 		     !is_v6kernel() ? "<not supported>" : net_ip6);
    739 #endif
    740 done:
    741 	if (!ask_yesno(MSG_netok_ok))
    742 		goto again;
    743 
    744 	free_menu(menu_no);
    745 	free(net_menu);
    746 
    747 	run_program(0, "/sbin/ifconfig lo0 127.0.0.1");
    748 
    749 	/* dhcpcd will have configured it all for us */
    750 	if (dhcp_config) {
    751 		fflush(NULL);
    752 		network_up = 1;
    753 		return network_up;
    754 	}
    755 
    756 	/*
    757 	 * we may want to perform checks against inconsistent configuration,
    758 	 * like IPv4 DNS server without IPv4 configuration.
    759 	 */
    760 
    761 	/* Create /etc/resolv.conf if a nameserver was given */
    762 	if (net_namesvr[0] != '\0') {
    763 		f = fopen("/etc/resolv.conf", "w");
    764 		if (f == NULL) {
    765 			if (logfp)
    766 				(void)fprintf(logfp,
    767 				    "%s", msg_string(MSG_resolv));
    768 			(void)fprintf(stderr, "%s", msg_string(MSG_resolv));
    769 			exit(1);
    770 		}
    771 		scripting_fprintf(NULL, "cat <<EOF >/etc/resolv.conf\n");
    772 		time(&now);
    773 		scripting_fprintf(f, ";\n; BIND data file\n; %s %s;\n",
    774 		    "Created by NetBSD sysinst on", safectime(&now));
    775 		if (net_domain[0] != '\0')
    776 			scripting_fprintf(f, "search %s\n", net_domain);
    777 		if (net_namesvr[0] != '\0')
    778 			scripting_fprintf(f, "nameserver %s\n", net_namesvr);
    779 		scripting_fprintf(NULL, "EOF\n");
    780 		fflush(NULL);
    781 		fclose(f);
    782 	}
    783 
    784 	if (net_ip[0] != '\0') {
    785 		if (slip) {
    786 			/* XXX: needs 'ifconfig sl0 create' much earlier */
    787 			/* Set SLIP interface UP */
    788 			run_program(0, "/sbin/ifconfig %s inet %s %s up",
    789 			    net_dev, net_ip, net_srv_ip);
    790 			strcpy(sl_flags, "-s 115200 -l /dev/tty00");
    791 			msg_prompt_win(MSG_slattach, -1, 12, 70, 0,
    792 				sl_flags, sl_flags, sizeof sl_flags);
    793 
    794 			/* XXX: wtf isn't run_program() used here? */
    795 			pid = fork();
    796 			if (pid == 0) {
    797 				strcpy(buffer, "/sbin/slattach ");
    798 				strcat(buffer, sl_flags);
    799 				in_buf = buffer;
    800 
    801 				for (ap = slcmd; (*ap = strsep(&in_buf, " ")) != NULL;)
    802 				if (**ap != '\0')
    803 					++ap;
    804 
    805 				execvp(slcmd[0], slcmd);
    806 			} else
    807 				wait4(pid, &status, WNOHANG, 0);
    808 		} else if (!nfs_root) {
    809 			if (net_mask[0] != '\0') {
    810 				run_program(0, "/sbin/ifconfig %s inet %s netmask %s",
    811 				    net_dev, net_ip, net_mask);
    812 			} else {
    813 				run_program(0, "/sbin/ifconfig %s inet %s",
    814 			    	net_dev, net_ip);
    815 			}
    816 		}
    817 	}
    818 
    819 	/* Set host name */
    820 	if (net_host[0] != '\0')
    821 	  	sethostname(net_host, strlen(net_host));
    822 
    823 	/* Set a default route if one was given */
    824 	if (!nfs_root && net_defroute[0] != '\0') {
    825 		run_program(RUN_DISPLAY | RUN_PROGRESS,
    826 				"/sbin/route -n flush -inet");
    827 		run_program(RUN_DISPLAY | RUN_PROGRESS,
    828 				"/sbin/route -n add default %s", net_defroute);
    829 	}
    830 
    831 	/*
    832 	 * wait for addresses to become valid
    833 	 */
    834 	if (!nfs_root) {
    835 		msg_display_add(MSG_wait_network);
    836 		network_up = !run_program(RUN_DISPLAY | RUN_PROGRESS,
    837 		    "/sbin/ifconfig -w 15 -W 5");
    838 	} else {
    839 		/* Assume network is up. */
    840 		network_up = 1;
    841 	}
    842 
    843 	fflush(NULL);
    844 
    845 	return network_up;
    846 }
    847 
    848 const char *
    849 url_proto(unsigned int xfer)
    850 {
    851 	switch (xfer) {
    852 	case XFER_FTP:	return "ftp";
    853 	case XFER_HTTP:	return "http";
    854 	}
    855 
    856 	return "";
    857 }
    858 
    859 void
    860 make_url(char *urlbuffer, struct ftpinfo *f, const char *dir)
    861 {
    862 	char ftp_user_encoded[STRSIZE];
    863 	char ftp_dir_encoded[STRSIZE];
    864 	char *cp;
    865 	const char *dir2;
    866 
    867 	/*
    868 	 * f->pass is quite likely to contain unsafe characters
    869 	 * that need to be encoded in the URL (for example,
    870 	 * "@", ":" and "/" need quoting).  Let's be
    871 	 * paranoid and also encode f->user and f->dir.  (For
    872 	 * example, f->dir could easily contain '~', which is
    873 	 * unsafe by a strict reading of RFC 1738).
    874 	 */
    875 	if (strcmp("ftp", f->user) == 0 && f->pass[0] == 0) {
    876 		ftp_user_encoded[0] = 0;
    877 	} else {
    878 		cp = url_encode(ftp_user_encoded, f->user,
    879 			ftp_user_encoded + sizeof ftp_user_encoded - 1,
    880 			RFC1738_SAFE_LESS_SHELL, 0);
    881 		*cp++ = ':';
    882 		cp = url_encode(cp, f->pass,
    883 			ftp_user_encoded + sizeof ftp_user_encoded - 1,
    884 			NULL, 0);
    885 		*cp++ = '@';
    886 		*cp = 0;
    887 	}
    888 	cp = url_encode(ftp_dir_encoded, f->dir,
    889 			ftp_dir_encoded + sizeof ftp_dir_encoded - 1,
    890 			RFC1738_SAFE_LESS_SHELL_PLUS_SLASH, 1);
    891 	if (cp != ftp_dir_encoded && cp[-1] != '/')
    892 		*cp++ = '/';
    893 
    894 	dir2 = dir;
    895 	while (*dir2 == '/')
    896 		++dir2;
    897 
    898 	url_encode(cp, dir2,
    899 			ftp_dir_encoded + sizeof ftp_dir_encoded,
    900 			RFC1738_SAFE_LESS_SHELL_PLUS_SLASH, 0);
    901 
    902 	snprintf(urlbuffer, STRSIZE, "%s://%s%s/%s", url_proto(f->xfer),
    903 	    ftp_user_encoded, f->xfer_host[f->xfer], ftp_dir_encoded);
    904 }
    905 
    906 
    907 /* ftp_fetch() and pkgsrc_fetch() are essentially the same, with a different
    908  * ftpinfo var and pkgsrc always using .tgz suffix, while for
    909  * regular sets we only use .tgz for source sets on some architectures. */
    910 static int do_ftp_fetch(const char *, bool, struct ftpinfo *);
    911 
    912 static int
    913 ftp_fetch(const char *set_name)
    914 {
    915 	return do_ftp_fetch(set_name, use_tgz_for_set(set_name), &ftp);
    916 }
    917 
    918 static int
    919 pkgsrc_fetch(const char *set_name)
    920 {
    921 	return do_ftp_fetch(set_name, true, &pkgsrc);
    922 }
    923 
    924 static int
    925 do_ftp_fetch(const char *set_name, bool force_tgz, struct ftpinfo *f)
    926 {
    927 	const char *ftp_opt;
    928 	char url[STRSIZE];
    929 	int rval;
    930 
    931 	/*
    932 	 * Invoke ftp to fetch the file.
    933 	 */
    934 	if (strcmp("ftp", f->user) == 0 && f->pass[0] == 0) {
    935 		/* do anon ftp */
    936 		ftp_opt = "-a ";
    937 	} else {
    938 		ftp_opt = "";
    939 	}
    940 
    941 	make_url(url, f, set_dir_for_set(set_name));
    942 	rval = run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_XFER_DIR,
    943 		    "/usr/bin/ftp %s%s/%s%s",
    944 		    ftp_opt, url, set_name,
    945 		    force_tgz ? dist_tgz_postfix : dist_postfix);
    946 
    947 	return rval ? SET_RETRY : SET_OK;
    948 }
    949 
    950 
    951 // XXX: check MSG_netnotup_continueanyway and MSG_netnotup
    952 
    953 int
    954 get_pkgsrc(void)
    955 {
    956 	int rv = -1;
    957 
    958 	process_menu(MENU_pkgsrc, &rv);
    959 
    960 	if (rv == SET_SKIP)
    961 		return SET_SKIP;
    962 
    963 	fetch_fn = pkgsrc_fetch;
    964 	snprintf(ext_dir_pkgsrc, sizeof ext_dir_pkgsrc, "%s/%s",
    965 	    target_prefix(), xfer_dir + (*xfer_dir == '/'));
    966 
    967 	return SET_OK;
    968 }
    969 
    970 int
    971 get_via_ftp(unsigned int xfer)
    972 {
    973 	arg_rv arg;
    974 
    975 	arg.rv = -1;
    976 	arg.arg = (void*)(uintptr_t)(xfer);
    977 	process_menu(MENU_ftpsource, &arg);
    978 
    979 	if (arg.rv == SET_RETRY)
    980 		return SET_RETRY;
    981 
    982 	/* We'll fetch each file just before installing it */
    983 	fetch_fn = ftp_fetch;
    984 	ftp.xfer = xfer;
    985 	snprintf(ext_dir_bin, sizeof ext_dir_bin, "%s/%s", target_prefix(),
    986 	    xfer_dir + (*xfer_dir == '/'));
    987 	snprintf(ext_dir_src, sizeof ext_dir_src, "%s/%s", target_prefix(),
    988 	    xfer_dir + (*xfer_dir == '/'));
    989 
    990 	return SET_OK;
    991 }
    992 
    993 int
    994 get_via_nfs(void)
    995 {
    996 	struct statvfs sb;
    997 	int rv;
    998 
    999 	/* If root is on NFS and we have sets, skip this step. */
   1000 	if (statvfs(set_dir_bin, &sb) == 0 &&
   1001 	    strcmp(sb.f_fstypename, "nfs") == 0) {
   1002 	    	strlcpy(ext_dir_bin, set_dir_bin, sizeof ext_dir_bin);
   1003 	    	strlcpy(ext_dir_src, set_dir_src, sizeof ext_dir_src);
   1004 		return SET_OK;
   1005 	}
   1006 
   1007 	/* Get server and filepath */
   1008 	rv = -1;
   1009 	process_menu(MENU_nfssource, &rv);
   1010 
   1011 	if (rv == SET_RETRY)
   1012 		return SET_RETRY;
   1013 
   1014 	/* Mount it */
   1015 	if (run_program(0, "/sbin/mount -r -o -2,-i,-r=1024 -t nfs %s:%s /mnt2",
   1016 	    nfs_host, nfs_dir))
   1017 		return SET_RETRY;
   1018 
   1019 	mnt2_mounted = 1;
   1020 
   1021 	snprintf(ext_dir_bin, sizeof ext_dir_bin, "/mnt2/%s", set_dir_bin);
   1022 	snprintf(ext_dir_src, sizeof ext_dir_src, "/mnt2/%s", set_dir_src);
   1023 
   1024 	/* return location, don't clean... */
   1025 	return SET_OK;
   1026 }
   1027 
   1028 /*
   1029  * write the new contents of /etc/hosts to the specified file
   1030  */
   1031 static void
   1032 write_etc_hosts(FILE *f)
   1033 {
   1034 	scripting_fprintf(f, "#\n");
   1035 	scripting_fprintf(f, "# Added by NetBSD sysinst\n");
   1036 	scripting_fprintf(f, "#\n");
   1037 
   1038 	if (net_domain[0] != '\0')
   1039 		scripting_fprintf(f, "127.0.0.1	localhost.%s\n", net_domain);
   1040 
   1041 	scripting_fprintf(f, "%s\t", net_ip);
   1042 	if (net_domain[0] != '\0')
   1043 		scripting_fprintf(f, "%s ", recombine_host_domain());
   1044 	scripting_fprintf(f, "%s\n", net_host);
   1045 }
   1046 
   1047 /*
   1048  * Write the network config info the user entered via menus into the
   1049  * config files in the target disk.  Be careful not to lose any
   1050  * information we don't immediately add back, in case the install
   1051  * target is the currently-active root.
   1052  */
   1053 void
   1054 mnt_net_config(void)
   1055 {
   1056 	char ifconfig_fn[STRSIZE];
   1057 	FILE *ifconf = NULL;
   1058 
   1059 	if (!network_up)
   1060 		return;
   1061 	if (!ask_yesno(MSG_mntnetconfig))
   1062 		return;
   1063 
   1064 	/* Write hostname to /etc/rc.conf */
   1065 	if ((net_dhcpconf & DHCPCONF_HOST) == 0)
   1066 		if (del_rc_conf("hostname") == 0)
   1067 			add_rc_conf("hostname=%s\n", recombine_host_domain());
   1068 
   1069 	/* Copy resolv.conf to target.  If DHCP was used to create it,
   1070 	 * it will be replaced on next boot anyway. */
   1071 	if (net_namesvr[0] != '\0')
   1072 		dup_file_into_target("/etc/resolv.conf");
   1073 
   1074 	/* Copy wpa_supplicant.conf to target. */
   1075 	if (net_ssid[0] != '\0')
   1076 		dup_file_into_target("/etc/wpa_supplicant.conf");
   1077 
   1078 	/*
   1079 	 * bring the interface up, it will be necessary for IPv6, and
   1080 	 * it won't make trouble with IPv4 case either
   1081 	 */
   1082 	snprintf(ifconfig_fn, sizeof ifconfig_fn, "/etc/ifconfig.%s", net_dev);
   1083 	ifconf = target_fopen(ifconfig_fn, "w");
   1084 	if (ifconf != NULL) {
   1085 		scripting_fprintf(NULL, "cat <<EOF >>%s%s\n",
   1086 		    target_prefix(), ifconfig_fn);
   1087 		scripting_fprintf(ifconf, "up\n");
   1088 		if (*net_media != '\0')
   1089 			scripting_fprintf(ifconf, "media %s\n", net_media);
   1090 		scripting_fprintf(NULL, "EOF\n");
   1091 	}
   1092 
   1093 	if ((net_dhcpconf & DHCPCONF_IPADDR) == 0) {
   1094 		FILE *hosts;
   1095 
   1096 		/* Write IPaddr and netmask to /etc/ifconfig.if[0-9] */
   1097 		if (ifconf != NULL) {
   1098 			scripting_fprintf(NULL, "cat <<EOF >>%s%s\n",
   1099 			    target_prefix(), ifconfig_fn);
   1100 			if (*net_media != '\0')
   1101 				scripting_fprintf(ifconf,
   1102 				    "%s netmask %s media %s\n",
   1103 				    net_ip, net_mask, net_media);
   1104 			else
   1105 				scripting_fprintf(ifconf, "%s netmask %s\n",
   1106 				    net_ip, net_mask);
   1107 			scripting_fprintf(NULL, "EOF\n");
   1108 		}
   1109 
   1110 		/*
   1111 		 * Add IPaddr/hostname to  /etc/hosts.
   1112 		 * Be careful not to clobber any existing contents.
   1113 		 * Relies on ordered search of /etc/hosts. XXX YP?
   1114 		 */
   1115 		hosts = target_fopen("/etc/hosts", "a");
   1116 		if (hosts != 0) {
   1117 			scripting_fprintf(NULL, "cat <<EOF >>%s/etc/hosts\n",
   1118 			    target_prefix());
   1119 			write_etc_hosts(hosts);
   1120 			(void)fclose(hosts);
   1121 			scripting_fprintf(NULL, "EOF\n");
   1122 		}
   1123 
   1124 		if (del_rc_conf("defaultroute") == 0)
   1125 			add_rc_conf("defaultroute=\"%s\"\n", net_defroute);
   1126 	} else {
   1127 		/*
   1128 		 * Start dhcpcd quietly and in master mode, but restrict
   1129 		 * it to our interface
   1130 		 */
   1131 		add_rc_conf("dhcpcd=YES\n");
   1132 		add_rc_conf("dhcpcd_flags=\"-qM %s\"\n", net_dev);
   1133         }
   1134 
   1135 	if (net_ssid[0] != '\0') {
   1136 		add_rc_conf("wpa_supplicant=YES\n");
   1137 		add_rc_conf("wpa_supplicant_flags=\"-B -s -i %s -D bsd -c /etc/wpa_supplicant.conf\"\n", net_dev);
   1138 	}
   1139 
   1140 	if (ifconf)
   1141 		fclose(ifconf);
   1142 
   1143 	fflush(NULL);
   1144 }
   1145 
   1146 int
   1147 config_wlan(char *inter)
   1148 {
   1149 	FILE *wpa_conf = NULL;
   1150 	char wpa_cmd[256];
   1151 	struct ifreq ifr = {0};
   1152 	struct ieee80211_nwid nwid = {0};
   1153 
   1154 	/* skip non-WLAN devices */
   1155 	if (do_ifreq(&ifr, SIOCG80211NWID, &nwid) == -1)
   1156 		return 0;
   1157 
   1158 	if (!file_mode_match(WPA_SUPPLICANT, S_IFREG))
   1159 		return 0;
   1160 
   1161 	msg_prompt_add(MSG_net_ssid, net_ssid, net_ssid,
   1162 			sizeof net_ssid);
   1163 	if (net_ssid[0] == '\0')
   1164 		return 0;
   1165 
   1166 	msg_prompt_noecho(MSG_net_passphrase, net_passphrase, net_passphrase,
   1167 			sizeof net_passphrase);
   1168 
   1169 	wpa_conf = fopen("/etc/wpa_supplicant.conf", "a");
   1170 	if (wpa_conf == NULL)
   1171 		return 0;
   1172 
   1173 	scripting_fprintf(NULL,
   1174 	    "cat <<EOF >>%s/etc/wpa_supplicant.conf\n",
   1175 	    target_prefix());
   1176 	scripting_fprintf(wpa_conf, "\n#\n");
   1177 	scripting_fprintf(wpa_conf, "# Added by NetBSD sysinst\n");
   1178 	scripting_fprintf(wpa_conf, "#\n");
   1179 	scripting_fprintf(wpa_conf, "network={\n");
   1180 	scripting_fprintf(wpa_conf,
   1181 	    "\tssid=\"%s\"\n", net_ssid);
   1182 	if (net_passphrase[0] != '\0') {
   1183 		scripting_fprintf(wpa_conf, "\tpsk=\"%s\"\n",
   1184 		    net_passphrase);
   1185 	} else {
   1186 		scripting_fprintf(wpa_conf, "\tkey_mgmt=NONE\n");
   1187 	}
   1188 	scripting_fprintf(wpa_conf, "\tscan_ssid=1\n");
   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