xref: /netbsd-src/usr.sbin/sysinst/net.c (revision 0578ab99d9607e1ea255c8c0b331c407434b2155)
1*0578ab99Smartin /*	$NetBSD: net.c,v 1.45 2023/12/17 18:46:42 martin Exp $	*/
250dbef1aSdholland 
350dbef1aSdholland /*
450dbef1aSdholland  * Copyright 1997 Piermont Information Systems Inc.
550dbef1aSdholland  * All rights reserved.
650dbef1aSdholland  *
750dbef1aSdholland  * Written by Philip A. Nelson for Piermont Information Systems Inc.
850dbef1aSdholland  *
950dbef1aSdholland  * Redistribution and use in source and binary forms, with or without
1050dbef1aSdholland  * modification, are permitted provided that the following conditions
1150dbef1aSdholland  * are met:
1250dbef1aSdholland  * 1. Redistributions of source code must retain the above copyright
1350dbef1aSdholland  *    notice, this list of conditions and the following disclaimer.
1450dbef1aSdholland  * 2. Redistributions in binary form must reproduce the above copyright
1550dbef1aSdholland  *    notice, this list of conditions and the following disclaimer in the
1650dbef1aSdholland  *    documentation and/or other materials provided with the distribution.
1750dbef1aSdholland  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
1850dbef1aSdholland  *    or promote products derived from this software without specific prior
1950dbef1aSdholland  *    written permission.
2050dbef1aSdholland  *
2150dbef1aSdholland  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
2250dbef1aSdholland  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2350dbef1aSdholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2450dbef1aSdholland  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
2550dbef1aSdholland  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2650dbef1aSdholland  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2750dbef1aSdholland  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2850dbef1aSdholland  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2950dbef1aSdholland  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3050dbef1aSdholland  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3150dbef1aSdholland  * THE POSSIBILITY OF SUCH DAMAGE.
3250dbef1aSdholland  *
3350dbef1aSdholland  */
3450dbef1aSdholland 
3550dbef1aSdholland /* net.c -- routines to fetch files off the network. */
3650dbef1aSdholland 
3750dbef1aSdholland #include <sys/ioctl.h>
3850dbef1aSdholland #include <sys/param.h>
3950dbef1aSdholland #include <sys/resource.h>
4050dbef1aSdholland #include <sys/socket.h>
4150dbef1aSdholland #include <sys/stat.h>
4250dbef1aSdholland #include <sys/statvfs.h>
4350dbef1aSdholland #include <sys/statvfs.h>
4450dbef1aSdholland #include <sys/sysctl.h>
4550dbef1aSdholland #include <sys/wait.h>
4650dbef1aSdholland #include <arpa/inet.h>
4750dbef1aSdholland #include <net/if.h>
4850dbef1aSdholland #include <net/if_media.h>
4950dbef1aSdholland #include <netinet/in.h>
508fa39f5eSnia #include <net80211/ieee80211_ioctl.h>
51a4e88c60Smartin #include <netinet/ip_var.h>
52a4e88c60Smartin #ifdef INET6
53a4e88c60Smartin #include <netinet6/ip6_var.h>
54a4e88c60Smartin #endif
5550dbef1aSdholland 
5650dbef1aSdholland #include <err.h>
5750dbef1aSdholland #include <stdio.h>
5850dbef1aSdholland #include <stdlib.h>
5950dbef1aSdholland #include <string.h>
6050dbef1aSdholland #include <curses.h>
6150dbef1aSdholland #include <time.h>
6250dbef1aSdholland #include <unistd.h>
6350dbef1aSdholland 
6450dbef1aSdholland #include "defs.h"
6550dbef1aSdholland #include "md.h"
6650dbef1aSdholland #include "msg_defs.h"
6750dbef1aSdholland #include "menu_defs.h"
6850dbef1aSdholland #include "txtwalk.h"
6950dbef1aSdholland 
7050dbef1aSdholland int network_up = 0;
7150dbef1aSdholland /* Access to network information */
724b2364d9Smartin #define MAX_NETS 15
734b2364d9Smartin struct net_desc {
744b2364d9Smartin 	char if_dev[STRSIZE];
754b2364d9Smartin 	char name[STRSIZE]; // TODO
764b2364d9Smartin };
774b2364d9Smartin 
7850dbef1aSdholland static char net_dev[STRSIZE];
7950dbef1aSdholland static char net_domain[STRSIZE];
8050dbef1aSdholland static char net_host[STRSIZE];
8150dbef1aSdholland static char net_ip[SSTRSIZE];
8250dbef1aSdholland static char net_srv_ip[SSTRSIZE];
8350dbef1aSdholland static char net_mask[SSTRSIZE];
8413c49796Sroy char net_namesvr[STRSIZE];
8550dbef1aSdholland static char net_defroute[STRSIZE];
8650dbef1aSdholland static char net_media[STRSIZE];
87f5d6f24eSnia static char net_ssid[STRSIZE];
88f5d6f24eSnia static char net_passphrase[STRSIZE];
8950dbef1aSdholland static char sl_flags[STRSIZE];
9050dbef1aSdholland static int net_dhcpconf;
9150dbef1aSdholland #define DHCPCONF_IPADDR         0x01
9250dbef1aSdholland #define DHCPCONF_NAMESVR        0x02
9350dbef1aSdholland #define DHCPCONF_HOST           0x04
9450dbef1aSdholland #define DHCPCONF_DOMAIN         0x08
9550dbef1aSdholland #ifdef INET6
9650dbef1aSdholland static char net_ip6[STRSIZE];
9750dbef1aSdholland #define IP6CONF_AUTOHOST        0x01
9850dbef1aSdholland #endif
9950dbef1aSdholland 
10050dbef1aSdholland 
10150dbef1aSdholland /* URL encode unsafe characters.  */
10250dbef1aSdholland 
10350dbef1aSdholland static char *url_encode (char *dst, const char *src, const char *ep,
10450dbef1aSdholland 				const char *safe_chars,
10550dbef1aSdholland 				int encode_leading_slash);
10650dbef1aSdholland 
10750dbef1aSdholland static void write_etc_hosts(FILE *f);
10850dbef1aSdholland 
10950dbef1aSdholland #define DHCPCD "/sbin/dhcpcd"
110f5d6f24eSnia #define WPA_SUPPLICANT "/usr/sbin/wpa_supplicant"
11150dbef1aSdholland #include <signal.h>
112f5d6f24eSnia static int config_eth_medium(char *);
11350dbef1aSdholland static int config_dhcp(char *);
114f5d6f24eSnia static int config_wlan(char *);
11550dbef1aSdholland 
11650dbef1aSdholland #ifdef INET6
11750dbef1aSdholland static int is_v6kernel (void);
11850dbef1aSdholland #endif
11950dbef1aSdholland 
12050dbef1aSdholland /*
12150dbef1aSdholland  * URL encode unsafe characters.  See RFC 1738.
12250dbef1aSdholland  *
12350dbef1aSdholland  * Copies src string to dst, encoding unsafe or reserved characters
12450dbef1aSdholland  * in %hex form as it goes, and returning a pointer to the result.
12550dbef1aSdholland  * The result is always a nul-terminated string even if it had to be
12650dbef1aSdholland  * truncated to avoid overflowing the available space.
12750dbef1aSdholland  *
12850dbef1aSdholland  * This url_encode() function does not operate on complete URLs, it
12950dbef1aSdholland  * operates on strings that make up parts of URLs.  For example, in a
13050dbef1aSdholland  * URL like "ftp://username:password@host/path", the username, password,
13150dbef1aSdholland  * host and path should each be encoded separately before they are
13250dbef1aSdholland  * joined together with the punctuation characters.
13350dbef1aSdholland  *
13450dbef1aSdholland  * In most ordinary use, the path portion of a URL does not start with
13550dbef1aSdholland  * a slash; the slash is a separator between the host portion and the
13650dbef1aSdholland  * path portion, and is dealt with by software outside the url_encode()
13750dbef1aSdholland  * function.  However, it is valid for url_encode() to be passed a
13850dbef1aSdholland  * string that does begin with a slash.  For example, the string might
13950dbef1aSdholland  * represent a password, or a path part of a URL that the user really
14050dbef1aSdholland  * does want to begin with a slash.
14150dbef1aSdholland  *
14250dbef1aSdholland  * len is the length of the destination buffer.  The result will be
14350dbef1aSdholland  * truncated if necessary to fit in the destination buffer.
14450dbef1aSdholland  *
14550dbef1aSdholland  * safe_chars is a string of characters that should not be encoded.  If
14650dbef1aSdholland  * safe_chars is non-NULL, any characters in safe_chars as well as any
14750dbef1aSdholland  * alphanumeric characters will be copied from src to dst without
14850dbef1aSdholland  * encoding.  Some potentially useful settings for this parameter are:
14950dbef1aSdholland  *
15050dbef1aSdholland  *	NULL		Everything is encoded (even alphanumerics)
15150dbef1aSdholland  *	""		Everything except alphanumerics are encoded
15250dbef1aSdholland  *	"/"		Alphanumerics and '/' remain unencoded
15350dbef1aSdholland  *	"$-_.+!*'(),"	Consistent with a strict reading of RFC 1738
15450dbef1aSdholland  *	"$-_.+!*'(),/"	As above, except '/' is not encoded
15550dbef1aSdholland  *	"-_.+!,/"	As above, except shell special characters are encoded
15650dbef1aSdholland  *
15750dbef1aSdholland  * encode_leading_slash is a flag that determines whether or not to
15850dbef1aSdholland  * encode a leading slash in a string.  If this flag is set, and if the
15950dbef1aSdholland  * first character in the src string is '/', then the leading slash will
16050dbef1aSdholland  * be encoded (as "%2F"), even if '/' is one of the characters in the
16150dbef1aSdholland  * safe_chars string.  Note that only the first character of the src
16250dbef1aSdholland  * string is affected by this flag, and that leading slashes are never
16350dbef1aSdholland  * deleted, but either retained unchanged or encoded.
16450dbef1aSdholland  *
16550dbef1aSdholland  * Unsafe and reserved characters are defined in RFC 1738 section 2.2.
16650dbef1aSdholland  * The most important parts are:
16750dbef1aSdholland  *
16850dbef1aSdholland  *      The characters ";", "/", "?", ":", "@", "=" and "&" are the
16950dbef1aSdholland  *      characters which may be reserved for special meaning within a
17050dbef1aSdholland  *      scheme. No other characters may be reserved within a scheme.
17150dbef1aSdholland  *      [...]
17250dbef1aSdholland  *
17350dbef1aSdholland  *      Thus, only alphanumerics, the special characters "$-_.+!*'(),",
17450dbef1aSdholland  *      and reserved characters used for their reserved purposes may be
17550dbef1aSdholland  *      used unencoded within a URL.
17650dbef1aSdholland  *
17750dbef1aSdholland  */
17850dbef1aSdholland 
17950dbef1aSdholland #define RFC1738_SAFE				"$-_.+!*'(),"
18050dbef1aSdholland #define RFC1738_SAFE_LESS_SHELL			"-_.+!,"
18150dbef1aSdholland #define RFC1738_SAFE_LESS_SHELL_PLUS_SLASH	"-_.+!,/"
18250dbef1aSdholland 
18350dbef1aSdholland static char *
url_encode(char * dst,const char * src,const char * ep,const char * safe_chars,int encode_leading_slash)18450dbef1aSdholland url_encode(char *dst, const char *src, const char *ep,
18550dbef1aSdholland 	const char *safe_chars, int encode_leading_slash)
18650dbef1aSdholland {
18750dbef1aSdholland 	int ch;
18850dbef1aSdholland 
18950dbef1aSdholland 	ep--;
19050dbef1aSdholland 
19150dbef1aSdholland 	for (; dst < ep; src++) {
19250dbef1aSdholland 		ch = *src & 0xff;
19350dbef1aSdholland 		if (ch == 0)
19450dbef1aSdholland 			break;
19550dbef1aSdholland 		if (safe_chars != NULL &&
19650dbef1aSdholland 		    (ch != '/' || !encode_leading_slash) &&
19750dbef1aSdholland 		    (isalnum(ch) || strchr(safe_chars, ch))) {
19850dbef1aSdholland 			*dst++ = ch;
19950dbef1aSdholland 		} else {
20050dbef1aSdholland 			/* encode this char */
20150dbef1aSdholland 			if (ep - dst < 3)
20250dbef1aSdholland 				break;
20350dbef1aSdholland 			snprintf(dst, ep - dst, "%%%02X", ch);
20450dbef1aSdholland 			dst += 3;
20550dbef1aSdholland 		}
20650dbef1aSdholland 		encode_leading_slash = 0;
20750dbef1aSdholland 	}
20850dbef1aSdholland 	*dst = '\0';
20950dbef1aSdholland 	return dst;
21050dbef1aSdholland }
21150dbef1aSdholland 
21250dbef1aSdholland static const char *ignored_if_names[] = {
21350dbef1aSdholland 	"gre",			/* net */
21450dbef1aSdholland 	"ipip",			/* netinet */
21550dbef1aSdholland 	"gif",			/* netinet6 */
21650dbef1aSdholland 	"faith",		/* netinet6 */
21750dbef1aSdholland 	"lo",			/* net */
2184b2364d9Smartin 	"lo0",			/* net */
21950dbef1aSdholland #if 0
22050dbef1aSdholland 	"mdecap",		/* netinet -- never in IF list (?) XXX */
22150dbef1aSdholland #endif
22250dbef1aSdholland 	"ppp",			/* net */
22350dbef1aSdholland #if 0
22450dbef1aSdholland 	"sl",			/* net */
22550dbef1aSdholland #endif
22650dbef1aSdholland 	"strip",		/* net */
22750dbef1aSdholland 	"tun",			/* net */
22850dbef1aSdholland 	/* XXX others? */
22950dbef1aSdholland 	NULL,
23050dbef1aSdholland };
23150dbef1aSdholland 
232a4e88c60Smartin static bool
have_working_ipv4(void)233a4e88c60Smartin have_working_ipv4(void)
234a4e88c60Smartin {
235a4e88c60Smartin 	uint64_t ipstats[IP_NSTATS];
236a4e88c60Smartin 	size_t size = sizeof(ipstats);
237a4e88c60Smartin 
238a4e88c60Smartin 	/* At least some packets delivered to upper layers? */
239a4e88c60Smartin 	if (sysctlbyname("net.inet.ip.stats", ipstats, &size, NULL, 0) == -1)
240a4e88c60Smartin 		return false;
241a4e88c60Smartin 	if (ipstats[IP_STAT_DELIVERED] < 10)	/* arbitrary threshold */
242a4e88c60Smartin 		return false;
243a4e88c60Smartin 
244a4e88c60Smartin 	/* do we have a default route? */
245a4e88c60Smartin 	if (run_program(RUN_SILENT|RUN_ERROR_OK,
246a4e88c60Smartin 	    "/sbin/route  get -inet default") != 0)
247a4e88c60Smartin 		return false;
248a4e88c60Smartin 
249a4e88c60Smartin 	return true;
250a4e88c60Smartin }
251a4e88c60Smartin 
252a4e88c60Smartin #ifdef INET6
253a4e88c60Smartin static bool
have_working_ipv6(void)254a4e88c60Smartin have_working_ipv6(void)
255a4e88c60Smartin {
256a4e88c60Smartin 	uint64_t ipstats[IP6_NSTATS];
257a4e88c60Smartin 	size_t size = sizeof(ipstats);
258a4e88c60Smartin 
259a4e88c60Smartin 	/* At least some packets delivered to upper layers? */
260a4e88c60Smartin 	if (sysctlbyname("net.inet6.ip6.stats", ipstats, &size, NULL, 0) == -1)
261a4e88c60Smartin 		return false;
262a4e88c60Smartin 	if (ipstats[IP6_STAT_DELIVERED] < 10)	/* arbitrary threshold */
263a4e88c60Smartin 		return false;
264a4e88c60Smartin 
265a4e88c60Smartin 	/* do we have a default route? */
266a4e88c60Smartin 	if (run_program(RUN_SILENT|RUN_ERROR_OK,
267a4e88c60Smartin 	    "/sbin/route  get -inet6 default") != 0)
268a4e88c60Smartin 		return false;
269a4e88c60Smartin 
270a4e88c60Smartin 	return true;
271a4e88c60Smartin }
272a4e88c60Smartin #else
273a4e88c60Smartin #define	have_working_ipv6()	false
274a4e88c60Smartin #endif
275a4e88c60Smartin 
2764b2364d9Smartin static int
get_ifconfig_info(struct net_desc * devs)2774b2364d9Smartin get_ifconfig_info(struct net_desc *devs)
27850dbef1aSdholland {
2794b2364d9Smartin 	char *buf_in;
2804b2364d9Smartin 	char *buf_tmp;
28150dbef1aSdholland 	const char **ignore;
2824b2364d9Smartin 	char *buf;
2834b2364d9Smartin 	char *tmp;
28450dbef1aSdholland 	int textsize;
2854b2364d9Smartin 	int i;
28650dbef1aSdholland 
28750dbef1aSdholland 	/* Get ifconfig information */
2884b2364d9Smartin 	textsize = collect(T_OUTPUT, &buf_in, "/sbin/ifconfig -l 2>/dev/null");
28950dbef1aSdholland 	if (textsize < 0) {
29050dbef1aSdholland 		if (logfp)
29150dbef1aSdholland 			(void)fprintf(logfp,
29250dbef1aSdholland 			    "Aborting: Could not run ifconfig.\n");
29350dbef1aSdholland 		(void)fprintf(stderr, "Could not run ifconfig.");
29450dbef1aSdholland 		exit(1);
29550dbef1aSdholland 	}
29650dbef1aSdholland 
2974b2364d9Smartin 	buf = malloc (STRSIZE * sizeof(char));
298f6144d97Smartin 	for (i = 0, buf_tmp = buf_in; i < MAX_NETS && strlen(buf_tmp) > 0
299f6144d97Smartin 	    && buf_tmp < buf_in + strlen(buf_in);) {
3004b2364d9Smartin 		tmp = stpncpy(buf, buf_tmp, strcspn(buf_tmp," \n"));
3014b2364d9Smartin 		*tmp='\0';
3024b2364d9Smartin 		buf_tmp += (strcspn(buf_tmp, " \n") + 1) * sizeof(char);
30350dbef1aSdholland 
3044b2364d9Smartin 		/* Skip ignored interfaces */
30550dbef1aSdholland 		for (ignore = ignored_if_names; *ignore != NULL; ignore++) {
30650dbef1aSdholland 			size_t len = strlen(*ignore);
3074b2364d9Smartin 			if (strncmp(buf, *ignore, len) == 0 &&
3084b2364d9Smartin 			    isdigit((unsigned char)buf[len]))
30950dbef1aSdholland 				break;
31050dbef1aSdholland 		}
31150dbef1aSdholland 		if (*ignore != NULL)
31250dbef1aSdholland 			continue;
31350dbef1aSdholland 
31414d42b6cSmartin 		strlcpy (devs[i].if_dev, buf, STRSIZE);
3154b2364d9Smartin 		i++;
31650dbef1aSdholland 	}
31785d19b88Smartin 	if (i < MAX_NETS)
31885d19b88Smartin 		devs[i].if_dev[0] = 0;	/* XXX ? */
31950dbef1aSdholland 
3204b2364d9Smartin 	free(buf);
3214b2364d9Smartin 	free(buf_in);
3224b2364d9Smartin 	return i;
32350dbef1aSdholland }
32450dbef1aSdholland 
32550dbef1aSdholland static int
do_ifreq(struct ifreq * ifr,unsigned long cmd,void * data)326d290f6b1Snia do_ifreq(struct ifreq *ifr, unsigned long cmd, void *data)
32750dbef1aSdholland {
32850dbef1aSdholland 	int sock;
32950dbef1aSdholland 	int rval;
33050dbef1aSdholland 
33150dbef1aSdholland 	sock = socket(PF_INET, SOCK_DGRAM, 0);
33250dbef1aSdholland 	if (sock == -1)
33350dbef1aSdholland 		return -1;
33450dbef1aSdholland 
33550dbef1aSdholland 	memset(ifr, 0, sizeof *ifr);
336d290f6b1Snia 	ifr->ifr_data = data;
3376a7f7ee8Smartin 	strlcpy(ifr->ifr_name, net_dev, sizeof ifr->ifr_name);
33850dbef1aSdholland 	rval = ioctl(sock, cmd, ifr);
33950dbef1aSdholland 	close(sock);
34050dbef1aSdholland 
34150dbef1aSdholland 	return rval;
34250dbef1aSdholland }
34350dbef1aSdholland 
34450dbef1aSdholland static int
do_ifmreq(struct ifmediareq * ifmr,unsigned long cmd)34550dbef1aSdholland do_ifmreq(struct ifmediareq *ifmr, unsigned long cmd)
34650dbef1aSdholland {
34750dbef1aSdholland 	int sock;
34850dbef1aSdholland 	int rval;
34950dbef1aSdholland 
35050dbef1aSdholland 	sock = socket(PF_INET, SOCK_DGRAM, 0);
35150dbef1aSdholland 	if (sock == -1)
35250dbef1aSdholland 		return -1;
35350dbef1aSdholland 
35450dbef1aSdholland 	memset(ifmr, 0, sizeof *ifmr);
3556a7f7ee8Smartin 	strlcpy(ifmr->ifm_name, net_dev, sizeof ifmr->ifm_name);
35650dbef1aSdholland 	rval = ioctl(sock, cmd, ifmr);
35750dbef1aSdholland 	close(sock);
35850dbef1aSdholland 
35950dbef1aSdholland 	return rval;
36050dbef1aSdholland }
36150dbef1aSdholland 
36250dbef1aSdholland /* Fill in defaults network values for the selected interface */
36350dbef1aSdholland static void
get_ifinterface_info(void)36450dbef1aSdholland get_ifinterface_info(void)
36550dbef1aSdholland {
36650dbef1aSdholland 	struct ifreq ifr;
36750dbef1aSdholland 	struct ifmediareq ifmr;
36850dbef1aSdholland 	struct sockaddr_in *sa_in = (void*)&ifr.ifr_addr;
36950dbef1aSdholland 	int modew;
37050dbef1aSdholland 	const char *media_opt;
37150dbef1aSdholland 	const char *sep;
37250dbef1aSdholland 
373d290f6b1Snia 	if (do_ifreq(&ifr, SIOCGIFADDR, NULL) == 0 &&
374d290f6b1Snia 	    sa_in->sin_addr.s_addr != 0)
37550dbef1aSdholland 		strlcpy(net_ip, inet_ntoa(sa_in->sin_addr), sizeof net_ip);
37650dbef1aSdholland 
377d290f6b1Snia 	if (do_ifreq(&ifr, SIOCGIFNETMASK, NULL) == 0 &&
378d290f6b1Snia 	    sa_in->sin_addr.s_addr != 0)
37950dbef1aSdholland 		strlcpy(net_mask, inet_ntoa(sa_in->sin_addr), sizeof net_mask);
38050dbef1aSdholland 
38150dbef1aSdholland 	if (do_ifmreq(&ifmr, SIOCGIFMEDIA) == 0) {
38250dbef1aSdholland 		/* Get the name of the media word */
38350dbef1aSdholland 		modew = ifmr.ifm_current;
38450dbef1aSdholland 		strlcpy(net_media, get_media_subtype_string(modew),
38550dbef1aSdholland 		    sizeof net_media);
38650dbef1aSdholland 		/* and add any media options */
38750dbef1aSdholland 		sep = " mediaopt ";
38850dbef1aSdholland 		while ((media_opt = get_media_option_string(&modew)) != NULL) {
38950dbef1aSdholland 			strlcat(net_media, sep, sizeof net_media);
39050dbef1aSdholland 			strlcat(net_media, media_opt, sizeof net_media);
39150dbef1aSdholland 			sep = ",";
39250dbef1aSdholland 		}
39350dbef1aSdholland 	}
39450dbef1aSdholland }
39550dbef1aSdholland 
39650dbef1aSdholland #ifndef INET6
39750dbef1aSdholland #define get_if6interface_info()
39850dbef1aSdholland #else
39950dbef1aSdholland static void
get_if6interface_info(void)40050dbef1aSdholland get_if6interface_info(void)
40150dbef1aSdholland {
40250dbef1aSdholland 	char *textbuf, *t;
40350dbef1aSdholland 	int textsize;
40450dbef1aSdholland 
40550dbef1aSdholland 	textsize = collect(T_OUTPUT, &textbuf,
40650dbef1aSdholland 	    "/sbin/ifconfig %s inet6 2>/dev/null", net_dev);
40750dbef1aSdholland 	if (textsize >= 0) {
40850dbef1aSdholland 		char *p;
40950dbef1aSdholland 
41050dbef1aSdholland 		(void)strtok(textbuf, "\n"); /* ignore first line */
41150dbef1aSdholland 		while ((t = strtok(NULL, "\n")) != NULL) {
41250dbef1aSdholland 			if (strncmp(t, "\tinet6 ", 7) != 0)
41350dbef1aSdholland 				continue;
41450dbef1aSdholland 			t += 7;
41550dbef1aSdholland 			if (strstr(t, "tentative") || strstr(t, "duplicated"))
41650dbef1aSdholland 				continue;
41750dbef1aSdholland 			if (strncmp(t, "fe80:", 5) == 0)
41850dbef1aSdholland 				continue;
41950dbef1aSdholland 
42050dbef1aSdholland 			p = t;
42150dbef1aSdholland 			while (*p && *p != ' ' && *p != '\n')
42250dbef1aSdholland 				p++;
42350dbef1aSdholland 			*p = '\0';
42450dbef1aSdholland 			strlcpy(net_ip6, t, sizeof(net_ip6));
42550dbef1aSdholland 			break;
42650dbef1aSdholland 		}
42750dbef1aSdholland 	}
42850dbef1aSdholland 	free(textbuf);
42950dbef1aSdholland }
43050dbef1aSdholland #endif
43150dbef1aSdholland 
43250dbef1aSdholland static void
get_host_info(void)43350dbef1aSdholland get_host_info(void)
43450dbef1aSdholland {
43550dbef1aSdholland 	char hostname[MAXHOSTNAMELEN + 1];
43650dbef1aSdholland 	char *dot;
43750dbef1aSdholland 
43850dbef1aSdholland 	/* Check host (and domain?) name */
43950dbef1aSdholland 	if (gethostname(hostname, sizeof(hostname)) == 0 && hostname[0] != 0) {
44050dbef1aSdholland 		hostname[sizeof(hostname) - 1] = 0;
44150dbef1aSdholland 		/* check for a . */
44250dbef1aSdholland 		dot = strchr(hostname, '.');
44350dbef1aSdholland 		if (dot == NULL) {
44450dbef1aSdholland 			/* if not found its just a host, punt on domain */
44550dbef1aSdholland 			strlcpy(net_host, hostname, sizeof net_host);
44650dbef1aSdholland 		} else {
44750dbef1aSdholland 			/* split hostname into host/domain parts */
44850dbef1aSdholland 			*dot++ = 0;
44950dbef1aSdholland 			strlcpy(net_host, hostname, sizeof net_host);
45050dbef1aSdholland 			strlcpy(net_domain, dot, sizeof net_domain);
45150dbef1aSdholland 		}
45250dbef1aSdholland 	}
45350dbef1aSdholland }
45450dbef1aSdholland 
45550dbef1aSdholland /*
45650dbef1aSdholland  * recombine name parts split in get_host_info and config_network
45750dbef1aSdholland  * (common code moved here from write_etc_hosts)
45850dbef1aSdholland  */
45950dbef1aSdholland static char *
recombine_host_domain(void)46050dbef1aSdholland recombine_host_domain(void)
46150dbef1aSdholland {
46250dbef1aSdholland 	static char recombined[MAXHOSTNAMELEN + 1];
46350dbef1aSdholland 	int l = strlen(net_host) - strlen(net_domain);
46450dbef1aSdholland 
46550dbef1aSdholland 	strlcpy(recombined, net_host, sizeof(recombined));
46650dbef1aSdholland 
46750dbef1aSdholland 	if (strlen(net_domain) != 0 && (l <= 0 ||
46850dbef1aSdholland 	    net_host[l - 1] != '.' ||
46950dbef1aSdholland 	    strcasecmp(net_domain, net_host + l) != 0)) {
47050dbef1aSdholland 		/* net_host isn't an FQDN. */
47150dbef1aSdholland 		strlcat(recombined, ".", sizeof(recombined));
47250dbef1aSdholland 		strlcat(recombined, net_domain, sizeof(recombined));
47350dbef1aSdholland 	}
47450dbef1aSdholland 	return recombined;
47550dbef1aSdholland }
47650dbef1aSdholland 
47750dbef1aSdholland #ifdef INET6
47850dbef1aSdholland static int
is_v6kernel(void)47950dbef1aSdholland is_v6kernel(void)
48050dbef1aSdholland {
48150dbef1aSdholland 	int s;
48250dbef1aSdholland 
48350dbef1aSdholland 	s = socket(PF_INET6, SOCK_DGRAM, 0);
48450dbef1aSdholland 	if (s < 0)
48550dbef1aSdholland 		return 0;
48650dbef1aSdholland 	close(s);
48750dbef1aSdholland 	return 1;
48850dbef1aSdholland }
48950dbef1aSdholland #endif
49050dbef1aSdholland 
49150dbef1aSdholland static int
handle_license(const char * dev)49250dbef1aSdholland handle_license(const char *dev)
49350dbef1aSdholland {
49450dbef1aSdholland 	static struct {
49550dbef1aSdholland 		const char *dev;
49650dbef1aSdholland 		const char *lic;
49750dbef1aSdholland 	} licdev[] = {
49850dbef1aSdholland 		{ "iwi", "/libdata/firmware/if_iwi/LICENSE.ipw2200-fw" },
49950dbef1aSdholland 		{ "ipw", "/libdata/firmware/if_ipw/LICENSE" },
50050dbef1aSdholland 	};
50150dbef1aSdholland 
50250dbef1aSdholland 	size_t i;
50350dbef1aSdholland 
50450dbef1aSdholland 	for (i = 0; i < __arraycount(licdev); i++)
50550dbef1aSdholland 		if (strncmp(dev, licdev[i].dev, 3) == 0) {
50650dbef1aSdholland 			char buf[64];
50750dbef1aSdholland 			int val;
50850dbef1aSdholland 			size_t len = sizeof(int);
50950dbef1aSdholland 			(void)snprintf(buf, sizeof(buf), "hw.%s.accept_eula",
51050dbef1aSdholland 			    licdev[i].dev);
51150dbef1aSdholland 			if (sysctlbyname(buf, &val, &len, NULL, 0) != -1
51250dbef1aSdholland 			    && val != 0)
51350dbef1aSdholland 				return 1;
51424ecf24eSchristos 			msg_fmt_display(MSG_license, "%s%s",
51524ecf24eSchristos 			    dev, licdev[i].lic);
516e21052b4Smartin 			if (ask_yesno(NULL)) {
51750dbef1aSdholland 				val = 1;
51850dbef1aSdholland 				if (sysctlbyname(buf, NULL, NULL, &val,
51950dbef1aSdholland 				    0) == -1)
52050dbef1aSdholland 					return 0;
52150dbef1aSdholland 				add_sysctl_conf("%s=1", buf);
52250dbef1aSdholland 				return 1;
52350dbef1aSdholland 			} else
52450dbef1aSdholland 				return 0;
52550dbef1aSdholland 		}
52650dbef1aSdholland 	return 1;
52750dbef1aSdholland }
52850dbef1aSdholland 
52950dbef1aSdholland /*
53050dbef1aSdholland  * Get the information to configure the network, configure it and
53150dbef1aSdholland  * make sure both the gateway and the name server are up.
53250dbef1aSdholland  */
53350dbef1aSdholland int
config_network(int force)534a4e88c60Smartin config_network(int force)
53550dbef1aSdholland {
53650dbef1aSdholland 	char *textbuf;
53750dbef1aSdholland 	int  octet0;
53850dbef1aSdholland 	int  dhcp_config;
53950dbef1aSdholland 	int  nfs_root = 0;
54050dbef1aSdholland  	int  slip = 0;
54150dbef1aSdholland  	int  pid, status;
54250dbef1aSdholland  	char **ap, *slcmd[10], *in_buf;
54350dbef1aSdholland  	char buffer[STRSIZE];
544f18efd62Sjmcneill 	char hostname[MAXHOSTNAMELEN + 1];
54550dbef1aSdholland  	struct statvfs sb;
5464b2364d9Smartin 	struct net_desc net_devs[MAX_NETS];
547f6144d97Smartin 	menu_ent *net_menu;
5484b2364d9Smartin 	int menu_no;
5494b2364d9Smartin 	int num_devs;
5504b2364d9Smartin 	int selected_net;
551a83f4e2cSmartin 	int i;
55250dbef1aSdholland #ifdef INET6
553a83f4e2cSmartin 	int v6config = 1, rv;
55450dbef1aSdholland #endif
55550dbef1aSdholland 
55650dbef1aSdholland 	FILE *f;
55750dbef1aSdholland 	time_t now;
55850dbef1aSdholland 
55950dbef1aSdholland 	if (network_up)
56050dbef1aSdholland 		return (1);
56150dbef1aSdholland 
5624b2364d9Smartin 	num_devs = get_ifconfig_info(net_devs);
56350dbef1aSdholland 
5644b2364d9Smartin 	if (num_devs < 1) {
56550dbef1aSdholland 		/* No network interfaces found! */
5664103857bSmartin 		hit_enter_to_continue(NULL, MSG_nonet);
567f6144d97Smartin 		return -1;
568f6144d97Smartin 	}
569f6144d97Smartin 
570a4e88c60Smartin 	if (!force && (have_working_ipv4() || have_working_ipv6())) {
571a4e88c60Smartin 		if (ask_yesno(MSG_network_ok)) {
572a4e88c60Smartin 			network_up = 1;
573a4e88c60Smartin 			return 1;
574a4e88c60Smartin 		}
575a4e88c60Smartin 	}
576a4e88c60Smartin 
577f6144d97Smartin 	net_menu = calloc(num_devs, sizeof(*net_menu));
578f6144d97Smartin 	if (net_menu == NULL) {
579f6144d97Smartin 		err_msg_win(err_outofmem);
580f6144d97Smartin 		return -1;
58150dbef1aSdholland 	}
5824b2364d9Smartin 
5834b2364d9Smartin 	for (i = 0; i < num_devs; i++) {
5844b2364d9Smartin 		net_menu[i].opt_name = net_devs[i].if_dev;
5854b2364d9Smartin 		net_menu[i].opt_flags = OPT_EXIT;
5864b2364d9Smartin 		net_menu[i].opt_action = set_menu_select;
5874b2364d9Smartin 	}
588f6144d97Smartin 
5894b2364d9Smartin 	menu_no = new_menu(MSG_netdevs,
5904b2364d9Smartin 		net_menu, num_devs, -1, 4, 0, 0,
5914b2364d9Smartin 		MC_SCROLL,
5927ca7eecaSmartin 		NULL, NULL, NULL, NULL, MSG_cancel);
593f6144d97Smartin again:
594f6144d97Smartin 	selected_net = -1;
59524ecf24eSchristos 	msg_display(MSG_asknetdev);
5964b2364d9Smartin 	process_menu(menu_no, &selected_net);
597f81a3204Smartin 	msg_clear();
5984b2364d9Smartin 
599f6144d97Smartin 	if (selected_net == -1) {
600f6144d97Smartin 		free_menu(menu_no);
601f6144d97Smartin 		free(net_menu);
6024b2364d9Smartin 		return 0;
603f6144d97Smartin 	}
6044b2364d9Smartin 
60550dbef1aSdholland 	network_up = 1;
6061c37fe71Sroy 	dhcp_config = 0;
60750dbef1aSdholland 
6086a7f7ee8Smartin 	strlcpy(net_dev, net_devs[selected_net].if_dev, sizeof net_dev);
6094b2364d9Smartin 
61050dbef1aSdholland 	if (!handle_license(net_dev))
61150dbef1aSdholland 		goto done;
61250dbef1aSdholland 
61350dbef1aSdholland 	slip = net_dev[0] == 's' && net_dev[1] == 'l' &&
61450dbef1aSdholland 	    isdigit((unsigned char)net_dev[2]);
61550dbef1aSdholland 
61650dbef1aSdholland 	/* If root is on NFS do not reconfigure the interface. */
61750dbef1aSdholland 	if (statvfs("/", &sb) == 0 && strcmp(sb.f_fstypename, "nfs") == 0) {
61850dbef1aSdholland 		nfs_root = 1;
61950dbef1aSdholland 		get_ifinterface_info();
62050dbef1aSdholland 		get_if6interface_info();
62150dbef1aSdholland 		get_host_info();
6221c37fe71Sroy 	} else if (!slip) {
62350dbef1aSdholland 		/* Preload any defaults we can find */
62450dbef1aSdholland 		get_ifinterface_info();
62550dbef1aSdholland 		get_if6interface_info();
62650dbef1aSdholland 		get_host_info();
62750dbef1aSdholland 
62850dbef1aSdholland 		/* domain and host */
62950dbef1aSdholland 		msg_display(MSG_netinfo);
63050dbef1aSdholland 
631f5d6f24eSnia 		if (!config_wlan(net_dev)) {
632f5d6f24eSnia 			config_eth_medium(net_dev);
63350dbef1aSdholland 		}
63450dbef1aSdholland 
63550dbef1aSdholland 		net_dhcpconf = 0;
63650dbef1aSdholland 		/* try a dhcp configuration */
63750dbef1aSdholland 		dhcp_config = config_dhcp(net_dev);
63850dbef1aSdholland 		if (dhcp_config) {
639ceb7a1b9Smartin 			char *nline;
640ec4a0efbSroy 
64150dbef1aSdholland 			/* Get newly configured data off interface. */
64250dbef1aSdholland 			get_ifinterface_info();
64350dbef1aSdholland 			get_if6interface_info();
64450dbef1aSdholland 			get_host_info();
64550dbef1aSdholland 
64650dbef1aSdholland 			net_dhcpconf |= DHCPCONF_IPADDR;
64750dbef1aSdholland 
64850dbef1aSdholland 			/*
64950dbef1aSdholland 			 * Extract default route from output of
65050dbef1aSdholland 			 * 'route -n show'
65150dbef1aSdholland 			 */
65250dbef1aSdholland 			if (collect(T_OUTPUT, &textbuf,
65350dbef1aSdholland 			    "/sbin/route -n show | "
65450dbef1aSdholland 			    "while read dest gateway flags;"
65550dbef1aSdholland 			    " do [ \"$dest\" = default ] && {"
656ec4a0efbSroy 			    " echo \"$gateway\"; break; };"
65750dbef1aSdholland 			    " done" ) > 0)
65850dbef1aSdholland 				strlcpy(net_defroute, textbuf,
65950dbef1aSdholland 				    sizeof net_defroute);
66050dbef1aSdholland 			free(textbuf);
661ceb7a1b9Smartin 			if ((nline = strchr(net_defroute, '\n')))
662ceb7a1b9Smartin 				*nline = '\0';
66350dbef1aSdholland 
66450dbef1aSdholland 			/* pull nameserver info out of /etc/resolv.conf */
66550dbef1aSdholland 			if (collect(T_OUTPUT, &textbuf,
66650dbef1aSdholland 			    "cat /etc/resolv.conf 2>/dev/null |"
66750dbef1aSdholland 			    " while read keyword address rest;"
668ec4a0efbSroy 			    " do [ \"$keyword\" = nameserver ] &&"
669ec4a0efbSroy 			    " { echo \"$address\"; break; };"
67050dbef1aSdholland 			    " done" ) > 0)
67150dbef1aSdholland 				strlcpy(net_namesvr, textbuf,
67250dbef1aSdholland 				    sizeof net_namesvr);
67350dbef1aSdholland 			free(textbuf);
674ceb7a1b9Smartin 			if ((nline = strchr(net_namesvr, '\n')))
675ceb7a1b9Smartin 				*nline = '\0';
67650dbef1aSdholland 			if (net_namesvr[0] != '\0')
67750dbef1aSdholland 				net_dhcpconf |= DHCPCONF_NAMESVR;
67850dbef1aSdholland 
679ec4a0efbSroy 			/* pull domain info out of /etc/resolv.conf */
680ec4a0efbSroy 			if (collect(T_OUTPUT, &textbuf,
681ec4a0efbSroy 			    "cat /etc/resolv.conf 2>/dev/null |"
682ec4a0efbSroy 			    " while read keyword domain rest;"
683ec4a0efbSroy 			    " do [ \"$keyword\" = domain ] &&"
684ec4a0efbSroy 			    " { echo \"$domain\"; break; };"
685ec4a0efbSroy 			    " done" ) > 0)
686ec4a0efbSroy 				strlcpy(net_domain, textbuf,
687ec4a0efbSroy 				    sizeof net_domain);
688ec4a0efbSroy 			free(textbuf);
689ec4a0efbSroy 			if (net_domain[0] == '\0') {
690ec4a0efbSroy 				/* pull domain info out of /etc/resolv.conf */
691ec4a0efbSroy 				if (collect(T_OUTPUT, &textbuf,
692ec4a0efbSroy 				    "cat /etc/resolv.conf 2>/dev/null |"
693ec4a0efbSroy 				    " while read keyword search rest;"
694ec4a0efbSroy 				    " do [ \"$keyword\" = search ] &&"
695ec4a0efbSroy 				    " { echo \"$search\"; break; };"
696ec4a0efbSroy 				    " done" ) > 0)
697ec4a0efbSroy 					strlcpy(net_domain, textbuf,
698ec4a0efbSroy 					    sizeof net_domain);
699ec4a0efbSroy 				free(textbuf);
700ec4a0efbSroy 			}
701ceb7a1b9Smartin 			if ((nline = strchr(net_domain, '\n')))
702ceb7a1b9Smartin 				*nline = '\0';
70350dbef1aSdholland 			if (net_domain[0] != '\0')
70450dbef1aSdholland 				net_dhcpconf |= DHCPCONF_DOMAIN;
70550dbef1aSdholland 
706ec4a0efbSroy 			if (gethostname(net_host, sizeof(net_host)) == 0 &&
707ec4a0efbSroy 			    net_host[0] != 0)
70850dbef1aSdholland 				net_dhcpconf |= DHCPCONF_HOST;
70950dbef1aSdholland 		}
71050dbef1aSdholland 	}
71150dbef1aSdholland 
712b2bd8c87Sjmcneill 	/*
713b2bd8c87Sjmcneill 	 * Prompt for hostname and domain, even when using DHCP. The names
714b2bd8c87Sjmcneill 	 * discovered on the network may not match the desired values
715b2bd8c87Sjmcneill 	 * for the target system.
716b2bd8c87Sjmcneill 	 */
717f18efd62Sjmcneill 	strlcpy(hostname, recombine_host_domain(), MAXHOSTNAMELEN);
71861e37473Sroy 	msg_prompt_add(MSG_net_host, net_host, net_host,
71961e37473Sroy 	    sizeof net_host);
72050dbef1aSdholland 	msg_prompt_add(MSG_net_domain, net_domain, net_domain,
72150dbef1aSdholland 	    sizeof net_domain);
722f18efd62Sjmcneill 	if (strcmp(hostname, recombine_host_domain()) != 0) {
723f18efd62Sjmcneill 		net_dhcpconf &= ~(DHCPCONF_DOMAIN|DHCPCONF_HOST);
724f18efd62Sjmcneill 	}
72550dbef1aSdholland 
72650dbef1aSdholland 	if (!dhcp_config) {
72750dbef1aSdholland 		/* Manually configure IPv4 */
72850dbef1aSdholland 		if (!nfs_root)
72950dbef1aSdholland 			msg_prompt_add(MSG_net_ip, net_ip, net_ip,
73050dbef1aSdholland 			    sizeof net_ip);
73150dbef1aSdholland 		if (slip)
73250dbef1aSdholland 			msg_prompt_add(MSG_net_srv_ip, net_srv_ip, net_srv_ip,
73350dbef1aSdholland 			    sizeof net_srv_ip);
73450dbef1aSdholland 		else if (!nfs_root) {
73550dbef1aSdholland 			/* We don't want netmasks for SLIP */
73650dbef1aSdholland 			octet0 = atoi(net_ip);
73750dbef1aSdholland 			if (!net_mask[0]) {
73850dbef1aSdholland 				if (0 <= octet0 && octet0 <= 127)
73950dbef1aSdholland 					strlcpy(net_mask, "0xff000000",
74050dbef1aSdholland 				    	sizeof(net_mask));
74150dbef1aSdholland 				else if (128 <= octet0 && octet0 <= 191)
74250dbef1aSdholland 					strlcpy(net_mask, "0xffff0000",
74350dbef1aSdholland 				    	sizeof(net_mask));
74450dbef1aSdholland 				else if (192 <= octet0 && octet0 <= 223)
74550dbef1aSdholland 					strlcpy(net_mask, "0xffffff00",
74650dbef1aSdholland 				    	sizeof(net_mask));
74750dbef1aSdholland 			}
74850dbef1aSdholland 			msg_prompt_add(MSG_net_mask, net_mask, net_mask,
74950dbef1aSdholland 			    sizeof net_mask);
75050dbef1aSdholland 		}
75150dbef1aSdholland 		msg_prompt_add(MSG_net_defroute, net_defroute, net_defroute,
75250dbef1aSdholland 		    sizeof net_defroute);
75350dbef1aSdholland 	}
75450dbef1aSdholland 
75513c49796Sroy 	if (!(net_dhcpconf & DHCPCONF_NAMESVR)) {
75650dbef1aSdholland #ifdef INET6
75750dbef1aSdholland 		if (v6config) {
758e21052b4Smartin 			rv = 0;
759e21052b4Smartin 			process_menu(MENU_namesrv6, &rv);
760e21052b4Smartin 			if (!rv)
76113c49796Sroy 				msg_prompt_add(MSG_net_namesrv, net_namesvr,
76213c49796Sroy 				    net_namesvr, sizeof net_namesvr);
76313c49796Sroy 		} else
76450dbef1aSdholland #endif
76513c49796Sroy 		msg_prompt_add(MSG_net_namesrv, net_namesvr, net_namesvr,
76613c49796Sroy 		    sizeof net_namesvr);
76713c49796Sroy 	}
76850dbef1aSdholland 
76950dbef1aSdholland 	/* confirm the setting */
77069403a14Smartin 	msg_clear();
77150dbef1aSdholland 	if (slip)
77269403a14Smartin 		msg_fmt_table_add(MSG_netok_slip, "%s%s%s%s%s%s%s%s%s",
77324ecf24eSchristos 		    net_domain,
77424ecf24eSchristos 		    net_host,
775eda03303Sroy 		    *net_namesvr == '\0' ? "<none>" : net_namesvr,
776eda03303Sroy 		    net_dev,
777eda03303Sroy 		    *net_media == '\0' ? "<default>" : net_media,
77850dbef1aSdholland 		    *net_ip == '\0' ? "<none>" : net_ip,
77950dbef1aSdholland 		    *net_srv_ip == '\0' ? "<none>" : net_srv_ip,
78050dbef1aSdholland 		    *net_mask == '\0' ? "<none>" : net_mask,
781eda03303Sroy 		    *net_defroute == '\0' ? "<none>" : net_defroute);
78250dbef1aSdholland 	else
78369403a14Smartin 		msg_fmt_table_add(MSG_netok, "%s%s%s%s%s%s%s%s",
78424ecf24eSchristos 		    net_domain,
78524ecf24eSchristos 		    net_host,
786eda03303Sroy 		    *net_namesvr == '\0' ? "<none>" : net_namesvr,
787eda03303Sroy 		    net_dev,
788eda03303Sroy 		    *net_media == '\0' ? "<default>" : net_media,
78950dbef1aSdholland 		    *net_ip == '\0' ? "<none>" : net_ip,
79050dbef1aSdholland 		    *net_mask == '\0' ? "<none>" : net_mask,
791eda03303Sroy 		    *net_defroute == '\0' ? "<none>" : net_defroute);
79250dbef1aSdholland #ifdef INET6
79369403a14Smartin 	msg_fmt_table_add(MSG_netokv6, "%s",
7949cc5846fSroy 		     !is_v6kernel() ? "<not supported>" : net_ip6);
79550dbef1aSdholland #endif
79650dbef1aSdholland done:
7975270515fSmartin 	if (!ask_yesno(MSG_netok_ok))
79850dbef1aSdholland 		goto again;
79950dbef1aSdholland 
800f6144d97Smartin 	free_menu(menu_no);
801f6144d97Smartin 	free(net_menu);
802f6144d97Smartin 
80314d86bcfSroy 	run_program(0, "/sbin/ifconfig lo0 127.0.0.1");
80414d86bcfSroy 
80514d86bcfSroy 	/* dhcpcd will have configured it all for us */
80614d86bcfSroy 	if (dhcp_config) {
80714d86bcfSroy 		fflush(NULL);
80814d86bcfSroy 		network_up = 1;
80914d86bcfSroy 		return network_up;
81014d86bcfSroy 	}
81114d86bcfSroy 
81250dbef1aSdholland 	/*
81350dbef1aSdholland 	 * we may want to perform checks against inconsistent configuration,
81450dbef1aSdholland 	 * like IPv4 DNS server without IPv4 configuration.
81550dbef1aSdholland 	 */
81650dbef1aSdholland 
81750dbef1aSdholland 	/* Create /etc/resolv.conf if a nameserver was given */
81813c49796Sroy 	if (net_namesvr[0] != '\0') {
81950dbef1aSdholland 		f = fopen("/etc/resolv.conf", "w");
82050dbef1aSdholland 		if (f == NULL) {
82150dbef1aSdholland 			if (logfp)
82250dbef1aSdholland 				(void)fprintf(logfp,
82350dbef1aSdholland 				    "%s", msg_string(MSG_resolv));
82450dbef1aSdholland 			(void)fprintf(stderr, "%s", msg_string(MSG_resolv));
82550dbef1aSdholland 			exit(1);
82650dbef1aSdholland 		}
82750dbef1aSdholland 		scripting_fprintf(NULL, "cat <<EOF >/etc/resolv.conf\n");
82850dbef1aSdholland 		time(&now);
82950dbef1aSdholland 		scripting_fprintf(f, ";\n; BIND data file\n; %s %s;\n",
830e47dca20Schristos 		    "Created by NetBSD sysinst on", safectime(&now));
83150dbef1aSdholland 		if (net_domain[0] != '\0')
83250dbef1aSdholland 			scripting_fprintf(f, "search %s\n", net_domain);
83350dbef1aSdholland 		if (net_namesvr[0] != '\0')
83450dbef1aSdholland 			scripting_fprintf(f, "nameserver %s\n", net_namesvr);
83550dbef1aSdholland 		scripting_fprintf(NULL, "EOF\n");
83650dbef1aSdholland 		fflush(NULL);
83750dbef1aSdholland 		fclose(f);
83850dbef1aSdholland 	}
83950dbef1aSdholland 
84050dbef1aSdholland 	if (net_ip[0] != '\0') {
84150dbef1aSdholland 		if (slip) {
84250dbef1aSdholland 			/* XXX: needs 'ifconfig sl0 create' much earlier */
84350dbef1aSdholland 			/* Set SLIP interface UP */
84450dbef1aSdholland 			run_program(0, "/sbin/ifconfig %s inet %s %s up",
84550dbef1aSdholland 			    net_dev, net_ip, net_srv_ip);
84650dbef1aSdholland 			strcpy(sl_flags, "-s 115200 -l /dev/tty00");
84750dbef1aSdholland 			msg_prompt_win(MSG_slattach, -1, 12, 70, 0,
848e65e3888Smartin 				sl_flags, sl_flags, sizeof sl_flags);
84950dbef1aSdholland 
85050dbef1aSdholland 			/* XXX: wtf isn't run_program() used here? */
85150dbef1aSdholland 			pid = fork();
85250dbef1aSdholland 			if (pid == 0) {
85350dbef1aSdholland 				strcpy(buffer, "/sbin/slattach ");
85450dbef1aSdholland 				strcat(buffer, sl_flags);
85550dbef1aSdholland 				in_buf = buffer;
85650dbef1aSdholland 
85750dbef1aSdholland 				for (ap = slcmd; (*ap = strsep(&in_buf, " ")) != NULL;)
85850dbef1aSdholland 				if (**ap != '\0')
85950dbef1aSdholland 					++ap;
86050dbef1aSdholland 
86150dbef1aSdholland 				execvp(slcmd[0], slcmd);
86250dbef1aSdholland 			} else
86350dbef1aSdholland 				wait4(pid, &status, WNOHANG, 0);
86450dbef1aSdholland 		} else if (!nfs_root) {
86550dbef1aSdholland 			if (net_mask[0] != '\0') {
86650dbef1aSdholland 				run_program(0, "/sbin/ifconfig %s inet %s netmask %s",
86750dbef1aSdholland 				    net_dev, net_ip, net_mask);
86850dbef1aSdholland 			} else {
86950dbef1aSdholland 				run_program(0, "/sbin/ifconfig %s inet %s",
87050dbef1aSdholland 			    	net_dev, net_ip);
87150dbef1aSdholland 			}
87250dbef1aSdholland 		}
87350dbef1aSdholland 	}
87450dbef1aSdholland 
87550dbef1aSdholland 	/* Set host name */
87650dbef1aSdholland 	if (net_host[0] != '\0')
87750dbef1aSdholland 	  	sethostname(net_host, strlen(net_host));
87850dbef1aSdholland 
87950dbef1aSdholland 	/* Set a default route if one was given */
88050dbef1aSdholland 	if (!nfs_root && net_defroute[0] != '\0') {
88150dbef1aSdholland 		run_program(RUN_DISPLAY | RUN_PROGRESS,
88250dbef1aSdholland 				"/sbin/route -n flush -inet");
88350dbef1aSdholland 		run_program(RUN_DISPLAY | RUN_PROGRESS,
88450dbef1aSdholland 				"/sbin/route -n add default %s", net_defroute);
88550dbef1aSdholland 	}
88650dbef1aSdholland 
88750dbef1aSdholland 	/*
888bdebc53aSroy 	 * wait for addresses to become valid
88950dbef1aSdholland 	 */
89050dbef1aSdholland 	if (!nfs_root) {
89150dbef1aSdholland 		msg_display_add(MSG_wait_network);
89287cdadbdSroy 		network_up = !run_program(RUN_DISPLAY | RUN_PROGRESS,
893bdebc53aSroy 		    "/sbin/ifconfig -w 15 -W 5");
89487cdadbdSroy 	} else {
89587cdadbdSroy 		/* Assume network is up. */
89687cdadbdSroy 		network_up = 1;
89750dbef1aSdholland 	}
89850dbef1aSdholland 
89950dbef1aSdholland 	fflush(NULL);
90050dbef1aSdholland 
90150dbef1aSdholland 	return network_up;
90250dbef1aSdholland }
90350dbef1aSdholland 
9049ce05eeeSmartin const char *
url_proto(unsigned int xfer)9059ce05eeeSmartin url_proto(unsigned int xfer)
9069ce05eeeSmartin {
9079ce05eeeSmartin 	switch (xfer) {
9089ce05eeeSmartin 	case XFER_FTP:		return "ftp";
9099ce05eeeSmartin 	case XFER_HTTP:		return "http";
910*0578ab99Smartin 	case XFER_HTTPS:	return "https";
9119ce05eeeSmartin 	}
9129ce05eeeSmartin 
9139ce05eeeSmartin 	return "";
9149ce05eeeSmartin }
9159ce05eeeSmartin 
91650dbef1aSdholland void
make_url(char * urlbuffer,struct ftpinfo * f,const char * dir)91750dbef1aSdholland make_url(char *urlbuffer, struct ftpinfo *f, const char *dir)
91850dbef1aSdholland {
91950dbef1aSdholland 	char ftp_user_encoded[STRSIZE];
92050dbef1aSdholland 	char ftp_dir_encoded[STRSIZE];
92150dbef1aSdholland 	char *cp;
92250dbef1aSdholland 	const char *dir2;
92350dbef1aSdholland 
92450dbef1aSdholland 	/*
92550dbef1aSdholland 	 * f->pass is quite likely to contain unsafe characters
92650dbef1aSdholland 	 * that need to be encoded in the URL (for example,
92750dbef1aSdholland 	 * "@", ":" and "/" need quoting).  Let's be
92850dbef1aSdholland 	 * paranoid and also encode f->user and f->dir.  (For
92950dbef1aSdholland 	 * example, f->dir could easily contain '~', which is
93050dbef1aSdholland 	 * unsafe by a strict reading of RFC 1738).
93150dbef1aSdholland 	 */
93250dbef1aSdholland 	if (strcmp("ftp", f->user) == 0 && f->pass[0] == 0) {
93350dbef1aSdholland 		ftp_user_encoded[0] = 0;
93450dbef1aSdholland 	} else {
93550dbef1aSdholland 		cp = url_encode(ftp_user_encoded, f->user,
93650dbef1aSdholland 			ftp_user_encoded + sizeof ftp_user_encoded - 1,
93750dbef1aSdholland 			RFC1738_SAFE_LESS_SHELL, 0);
93850dbef1aSdholland 		*cp++ = ':';
93950dbef1aSdholland 		cp = url_encode(cp, f->pass,
94050dbef1aSdholland 			ftp_user_encoded + sizeof ftp_user_encoded - 1,
94150dbef1aSdholland 			NULL, 0);
94250dbef1aSdholland 		*cp++ = '@';
94350dbef1aSdholland 		*cp = 0;
94450dbef1aSdholland 	}
94550dbef1aSdholland 	cp = url_encode(ftp_dir_encoded, f->dir,
94650dbef1aSdholland 			ftp_dir_encoded + sizeof ftp_dir_encoded - 1,
94750dbef1aSdholland 			RFC1738_SAFE_LESS_SHELL_PLUS_SLASH, 1);
94850dbef1aSdholland 	if (cp != ftp_dir_encoded && cp[-1] != '/')
94950dbef1aSdholland 		*cp++ = '/';
95050dbef1aSdholland 
95150dbef1aSdholland 	dir2 = dir;
95250dbef1aSdholland 	while (*dir2 == '/')
95350dbef1aSdholland 		++dir2;
95450dbef1aSdholland 
95550dbef1aSdholland 	url_encode(cp, dir2,
95650dbef1aSdholland 			ftp_dir_encoded + sizeof ftp_dir_encoded,
95750dbef1aSdholland 			RFC1738_SAFE_LESS_SHELL_PLUS_SLASH, 0);
95850dbef1aSdholland 
9599ce05eeeSmartin 	snprintf(urlbuffer, STRSIZE, "%s://%s%s/%s", url_proto(f->xfer),
960*0578ab99Smartin 	    ftp_user_encoded, f->xfer_host[XFER_HOST(f->xfer)],
961*0578ab99Smartin 	    ftp_dir_encoded);
96250dbef1aSdholland }
96350dbef1aSdholland 
96450dbef1aSdholland 
96550dbef1aSdholland /* ftp_fetch() and pkgsrc_fetch() are essentially the same, with a different
9660ef20e92Smartin  * ftpinfo var and pkgsrc always using .tgz suffix, while for
9670ef20e92Smartin  * regular sets we only use .tgz for source sets on some architectures. */
9680ef20e92Smartin static int do_ftp_fetch(const char *, bool, struct ftpinfo *);
96950dbef1aSdholland 
97050dbef1aSdholland static int
ftp_fetch(const char * set_name)97150dbef1aSdholland ftp_fetch(const char *set_name)
97250dbef1aSdholland {
9730ef20e92Smartin 	return do_ftp_fetch(set_name, use_tgz_for_set(set_name), &ftp);
97450dbef1aSdholland }
97550dbef1aSdholland 
97650dbef1aSdholland static int
pkgsrc_fetch(const char * set_name)97750dbef1aSdholland pkgsrc_fetch(const char *set_name)
97850dbef1aSdholland {
9790ef20e92Smartin 	return do_ftp_fetch(set_name, true, &pkgsrc);
98050dbef1aSdholland }
98150dbef1aSdholland 
98250dbef1aSdholland static int
do_ftp_fetch(const char * set_name,bool force_tgz,struct ftpinfo * f)9830ef20e92Smartin do_ftp_fetch(const char *set_name, bool force_tgz, struct ftpinfo *f)
98450dbef1aSdholland {
98550dbef1aSdholland 	const char *ftp_opt;
98650dbef1aSdholland 	char url[STRSIZE];
98750dbef1aSdholland 	int rval;
98850dbef1aSdholland 
98950dbef1aSdholland 	/*
99050dbef1aSdholland 	 * Invoke ftp to fetch the file.
99150dbef1aSdholland 	 */
99250dbef1aSdholland 	if (strcmp("ftp", f->user) == 0 && f->pass[0] == 0) {
99350dbef1aSdholland 		/* do anon ftp */
99450dbef1aSdholland 		ftp_opt = "-a ";
99550dbef1aSdholland 	} else {
99650dbef1aSdholland 		ftp_opt = "";
99750dbef1aSdholland 	}
99850dbef1aSdholland 
99950dbef1aSdholland 	make_url(url, f, set_dir_for_set(set_name));
100050dbef1aSdholland 	rval = run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_XFER_DIR,
100150dbef1aSdholland 		    "/usr/bin/ftp %s%s/%s%s",
10020ef20e92Smartin 		    ftp_opt, url, set_name,
10030ef20e92Smartin 		    force_tgz ? dist_tgz_postfix : dist_postfix);
100450dbef1aSdholland 
100550dbef1aSdholland 	return rval ? SET_RETRY : SET_OK;
100650dbef1aSdholland }
100750dbef1aSdholland 
100850dbef1aSdholland 
10094b2364d9Smartin // XXX: check MSG_netnotup_continueanyway and MSG_netnotup
101050dbef1aSdholland 
101150dbef1aSdholland int
get_pkgsrc(void)101250dbef1aSdholland get_pkgsrc(void)
101350dbef1aSdholland {
1014e21052b4Smartin 	int rv = -1;
101550dbef1aSdholland 
1016e21052b4Smartin 	process_menu(MENU_pkgsrc, &rv);
1017e21052b4Smartin 
1018e21052b4Smartin 	if (rv == SET_SKIP)
101950dbef1aSdholland 		return SET_SKIP;
10204b2364d9Smartin 
102150dbef1aSdholland 	fetch_fn = pkgsrc_fetch;
102250dbef1aSdholland 	snprintf(ext_dir_pkgsrc, sizeof ext_dir_pkgsrc, "%s/%s",
102350dbef1aSdholland 	    target_prefix(), xfer_dir + (*xfer_dir == '/'));
102450dbef1aSdholland 
102550dbef1aSdholland 	return SET_OK;
102650dbef1aSdholland }
102750dbef1aSdholland 
102850dbef1aSdholland int
get_via_ftp(unsigned int xfer)10299ce05eeeSmartin get_via_ftp(unsigned int xfer)
103050dbef1aSdholland {
1031e21052b4Smartin 	arg_rv arg;
103250dbef1aSdholland 
1033a4e88c60Smartin 	if (!network_up)
1034a4e88c60Smartin 		config_network(0);
1035a4e88c60Smartin 
1036e21052b4Smartin 	arg.rv = -1;
10379ce05eeeSmartin 	arg.arg = (void*)(uintptr_t)(xfer);
1038e21052b4Smartin 	process_menu(MENU_ftpsource, &arg);
1039e21052b4Smartin 
1040e21052b4Smartin 	if (arg.rv == SET_RETRY)
10414b2364d9Smartin 		return SET_RETRY;
10424b2364d9Smartin 
104350dbef1aSdholland 	/* We'll fetch each file just before installing it */
104450dbef1aSdholland 	fetch_fn = ftp_fetch;
10459ce05eeeSmartin 	ftp.xfer = xfer;
104650dbef1aSdholland 	snprintf(ext_dir_bin, sizeof ext_dir_bin, "%s/%s", target_prefix(),
104750dbef1aSdholland 	    xfer_dir + (*xfer_dir == '/'));
104850dbef1aSdholland 	snprintf(ext_dir_src, sizeof ext_dir_src, "%s/%s", target_prefix(),
104950dbef1aSdholland 	    xfer_dir + (*xfer_dir == '/'));
105050dbef1aSdholland 
105150dbef1aSdholland 	return SET_OK;
105250dbef1aSdholland }
105350dbef1aSdholland 
105450dbef1aSdholland int
get_via_nfs(void)105550dbef1aSdholland get_via_nfs(void)
105650dbef1aSdholland {
105750dbef1aSdholland 	struct statvfs sb;
1058e21052b4Smartin 	int rv;
105950dbef1aSdholland 
106050dbef1aSdholland 	/* If root is on NFS and we have sets, skip this step. */
106150dbef1aSdholland 	if (statvfs(set_dir_bin, &sb) == 0 &&
106250dbef1aSdholland 	    strcmp(sb.f_fstypename, "nfs") == 0) {
106350dbef1aSdholland 	    	strlcpy(ext_dir_bin, set_dir_bin, sizeof ext_dir_bin);
106450dbef1aSdholland 	    	strlcpy(ext_dir_src, set_dir_src, sizeof ext_dir_src);
106550dbef1aSdholland 		return SET_OK;
106650dbef1aSdholland 	}
106750dbef1aSdholland 
106850dbef1aSdholland 	/* Get server and filepath */
1069e21052b4Smartin 	rv = -1;
1070e21052b4Smartin 	process_menu(MENU_nfssource, &rv);
107150dbef1aSdholland 
1072e21052b4Smartin 	if (rv == SET_RETRY)
10734b2364d9Smartin 		return SET_RETRY;
10744b2364d9Smartin 
107550dbef1aSdholland 	/* Mount it */
107650dbef1aSdholland 	if (run_program(0, "/sbin/mount -r -o -2,-i,-r=1024 -t nfs %s:%s /mnt2",
107750dbef1aSdholland 	    nfs_host, nfs_dir))
107850dbef1aSdholland 		return SET_RETRY;
107950dbef1aSdholland 
108050dbef1aSdholland 	mnt2_mounted = 1;
108150dbef1aSdholland 
108250dbef1aSdholland 	snprintf(ext_dir_bin, sizeof ext_dir_bin, "/mnt2/%s", set_dir_bin);
108350dbef1aSdholland 	snprintf(ext_dir_src, sizeof ext_dir_src, "/mnt2/%s", set_dir_src);
108450dbef1aSdholland 
108550dbef1aSdholland 	/* return location, don't clean... */
108650dbef1aSdholland 	return SET_OK;
108750dbef1aSdholland }
108850dbef1aSdholland 
108950dbef1aSdholland /*
109050dbef1aSdholland  * write the new contents of /etc/hosts to the specified file
109150dbef1aSdholland  */
109250dbef1aSdholland static void
write_etc_hosts(FILE * f)109350dbef1aSdholland write_etc_hosts(FILE *f)
109450dbef1aSdholland {
109550dbef1aSdholland 	scripting_fprintf(f, "#\n");
109650dbef1aSdholland 	scripting_fprintf(f, "# Added by NetBSD sysinst\n");
109750dbef1aSdholland 	scripting_fprintf(f, "#\n");
109850dbef1aSdholland 
109950dbef1aSdholland 	if (net_domain[0] != '\0')
110050dbef1aSdholland 		scripting_fprintf(f, "127.0.0.1	localhost.%s\n", net_domain);
110150dbef1aSdholland 
110250dbef1aSdholland 	scripting_fprintf(f, "%s\t", net_ip);
110350dbef1aSdholland 	if (net_domain[0] != '\0')
110450dbef1aSdholland 		scripting_fprintf(f, "%s ", recombine_host_domain());
110550dbef1aSdholland 	scripting_fprintf(f, "%s\n", net_host);
110650dbef1aSdholland }
110750dbef1aSdholland 
110850dbef1aSdholland /*
110950dbef1aSdholland  * Write the network config info the user entered via menus into the
111050dbef1aSdholland  * config files in the target disk.  Be careful not to lose any
111150dbef1aSdholland  * information we don't immediately add back, in case the install
111250dbef1aSdholland  * target is the currently-active root.
111350dbef1aSdholland  */
111450dbef1aSdholland void
mnt_net_config(void)111550dbef1aSdholland mnt_net_config(void)
111650dbef1aSdholland {
111750dbef1aSdholland 	char ifconfig_fn[STRSIZE];
111850dbef1aSdholland 	FILE *ifconf = NULL;
111950dbef1aSdholland 
112050dbef1aSdholland 	if (!network_up)
112150dbef1aSdholland 		return;
11225270515fSmartin 	if (!ask_yesno(MSG_mntnetconfig))
112350dbef1aSdholland 		return;
112450dbef1aSdholland 
112550dbef1aSdholland 	/* Write hostname to /etc/rc.conf */
112650dbef1aSdholland 	if ((net_dhcpconf & DHCPCONF_HOST) == 0)
112750dbef1aSdholland 		if (del_rc_conf("hostname") == 0)
112850dbef1aSdholland 			add_rc_conf("hostname=%s\n", recombine_host_domain());
112950dbef1aSdholland 
113050dbef1aSdholland 	/* Copy resolv.conf to target.  If DHCP was used to create it,
113150dbef1aSdholland 	 * it will be replaced on next boot anyway. */
113250dbef1aSdholland 	if (net_namesvr[0] != '\0')
113350dbef1aSdholland 		dup_file_into_target("/etc/resolv.conf");
113450dbef1aSdholland 
1135f5d6f24eSnia 	/* Copy wpa_supplicant.conf to target. */
1136f5d6f24eSnia 	if (net_ssid[0] != '\0')
1137f5d6f24eSnia 		dup_file_into_target("/etc/wpa_supplicant.conf");
1138f5d6f24eSnia 
113950dbef1aSdholland 	/*
114050dbef1aSdholland 	 * bring the interface up, it will be necessary for IPv6, and
114150dbef1aSdholland 	 * it won't make trouble with IPv4 case either
114250dbef1aSdholland 	 */
114350dbef1aSdholland 	snprintf(ifconfig_fn, sizeof ifconfig_fn, "/etc/ifconfig.%s", net_dev);
114450dbef1aSdholland 	ifconf = target_fopen(ifconfig_fn, "w");
114550dbef1aSdholland 	if (ifconf != NULL) {
114650dbef1aSdholland 		scripting_fprintf(NULL, "cat <<EOF >>%s%s\n",
114750dbef1aSdholland 		    target_prefix(), ifconfig_fn);
114850dbef1aSdholland 		scripting_fprintf(ifconf, "up\n");
114950dbef1aSdholland 		if (*net_media != '\0')
115050dbef1aSdholland 			scripting_fprintf(ifconf, "media %s\n", net_media);
115150dbef1aSdholland 		scripting_fprintf(NULL, "EOF\n");
115250dbef1aSdholland 	}
115350dbef1aSdholland 
115450dbef1aSdholland 	if ((net_dhcpconf & DHCPCONF_IPADDR) == 0) {
115550dbef1aSdholland 		FILE *hosts;
115650dbef1aSdholland 
115750dbef1aSdholland 		/* Write IPaddr and netmask to /etc/ifconfig.if[0-9] */
115850dbef1aSdholland 		if (ifconf != NULL) {
115950dbef1aSdholland 			scripting_fprintf(NULL, "cat <<EOF >>%s%s\n",
116050dbef1aSdholland 			    target_prefix(), ifconfig_fn);
116150dbef1aSdholland 			if (*net_media != '\0')
116250dbef1aSdholland 				scripting_fprintf(ifconf,
116350dbef1aSdholland 				    "%s netmask %s media %s\n",
116450dbef1aSdholland 				    net_ip, net_mask, net_media);
116550dbef1aSdholland 			else
116650dbef1aSdholland 				scripting_fprintf(ifconf, "%s netmask %s\n",
116750dbef1aSdholland 				    net_ip, net_mask);
116850dbef1aSdholland 			scripting_fprintf(NULL, "EOF\n");
116950dbef1aSdholland 		}
117050dbef1aSdholland 
117150dbef1aSdholland 		/*
117250dbef1aSdholland 		 * Add IPaddr/hostname to  /etc/hosts.
117350dbef1aSdholland 		 * Be careful not to clobber any existing contents.
117450dbef1aSdholland 		 * Relies on ordered search of /etc/hosts. XXX YP?
117550dbef1aSdholland 		 */
117650dbef1aSdholland 		hosts = target_fopen("/etc/hosts", "a");
117750dbef1aSdholland 		if (hosts != 0) {
117850dbef1aSdholland 			scripting_fprintf(NULL, "cat <<EOF >>%s/etc/hosts\n",
117950dbef1aSdholland 			    target_prefix());
118050dbef1aSdholland 			write_etc_hosts(hosts);
118150dbef1aSdholland 			(void)fclose(hosts);
118250dbef1aSdholland 			scripting_fprintf(NULL, "EOF\n");
118350dbef1aSdholland 		}
118450dbef1aSdholland 
118550dbef1aSdholland 		if (del_rc_conf("defaultroute") == 0)
118650dbef1aSdholland 			add_rc_conf("defaultroute=\"%s\"\n", net_defroute);
118750dbef1aSdholland 	} else {
11884fabb2a3Sroy 		/*
11894fabb2a3Sroy 		 * Start dhcpcd quietly and in master mode, but restrict
11904fabb2a3Sroy 		 * it to our interface
11914fabb2a3Sroy 		 */
11924fabb2a3Sroy 		add_rc_conf("dhcpcd=YES\n");
11934fabb2a3Sroy 		add_rc_conf("dhcpcd_flags=\"-qM %s\"\n", net_dev);
119450dbef1aSdholland         }
119550dbef1aSdholland 
1196f5d6f24eSnia 	if (net_ssid[0] != '\0') {
1197f5d6f24eSnia 		add_rc_conf("wpa_supplicant=YES\n");
1198f5d6f24eSnia 		add_rc_conf("wpa_supplicant_flags=\"-B -s -i %s -D bsd -c /etc/wpa_supplicant.conf\"\n", net_dev);
1199f5d6f24eSnia 	}
1200f5d6f24eSnia 
120150dbef1aSdholland 	if (ifconf)
120250dbef1aSdholland 		fclose(ifconf);
120350dbef1aSdholland 
120450dbef1aSdholland 	fflush(NULL);
120550dbef1aSdholland }
120650dbef1aSdholland 
120750dbef1aSdholland int
config_wlan(char * inter)1208f5d6f24eSnia config_wlan(char *inter)
1209f5d6f24eSnia {
1210f5d6f24eSnia 	FILE *wpa_conf = NULL;
1211f5d6f24eSnia 	char wpa_cmd[256];
12128fa39f5eSnia 	struct ifreq ifr = {0};
12138fa39f5eSnia 	struct ieee80211_nwid nwid = {0};
12148fa39f5eSnia 
12158fa39f5eSnia 	/* skip non-WLAN devices */
1216d290f6b1Snia 	if (do_ifreq(&ifr, SIOCG80211NWID, &nwid) == -1)
12178fa39f5eSnia 		return 0;
1218f5d6f24eSnia 
1219f5d6f24eSnia 	if (!file_mode_match(WPA_SUPPLICANT, S_IFREG))
1220f5d6f24eSnia 		return 0;
1221f5d6f24eSnia 
1222f5d6f24eSnia 	msg_prompt_add(MSG_net_ssid, net_ssid, net_ssid,
1223f5d6f24eSnia 			sizeof net_ssid);
1224f5d6f24eSnia 	if (net_ssid[0] == '\0')
1225f5d6f24eSnia 		return 0;
1226f5d6f24eSnia 
1227f5d6f24eSnia 	msg_prompt_noecho(MSG_net_passphrase, net_passphrase, net_passphrase,
1228f5d6f24eSnia 			sizeof net_passphrase);
1229f5d6f24eSnia 
1230f5d6f24eSnia 	wpa_conf = fopen("/etc/wpa_supplicant.conf", "a");
1231f5d6f24eSnia 	if (wpa_conf == NULL)
1232f5d6f24eSnia 		return 0;
1233f5d6f24eSnia 
1234f5d6f24eSnia 	scripting_fprintf(NULL,
1235f5d6f24eSnia 	    "cat <<EOF >>%s/etc/wpa_supplicant.conf\n",
1236f5d6f24eSnia 	    target_prefix());
1237f5d6f24eSnia 	scripting_fprintf(wpa_conf, "\n#\n");
1238f5d6f24eSnia 	scripting_fprintf(wpa_conf, "# Added by NetBSD sysinst\n");
1239f5d6f24eSnia 	scripting_fprintf(wpa_conf, "#\n");
1240f5d6f24eSnia 	scripting_fprintf(wpa_conf, "network={\n");
1241f5d6f24eSnia 	scripting_fprintf(wpa_conf,
1242f5d6f24eSnia 	    "\tssid=\"%s\"\n", net_ssid);
1243f5d6f24eSnia 	if (net_passphrase[0] != '\0') {
1244f5d6f24eSnia 		scripting_fprintf(wpa_conf, "\tpsk=\"%s\"\n",
1245f5d6f24eSnia 		    net_passphrase);
1246f5d6f24eSnia 	} else {
1247f5d6f24eSnia 		scripting_fprintf(wpa_conf, "\tkey_mgmt=NONE\n");
1248f5d6f24eSnia 	}
12497a918532Snia 	scripting_fprintf(wpa_conf, "\tscan_ssid=1\n");
1250f5d6f24eSnia 	scripting_fprintf(wpa_conf, "}\n");
1251f5d6f24eSnia 	(void)fclose(wpa_conf);
1252f5d6f24eSnia 	scripting_fprintf(NULL, "EOF\n");
1253f5d6f24eSnia 
1254f5d6f24eSnia 	if (run_program(RUN_DISPLAY | RUN_PROGRESS,
1255f5d6f24eSnia 	    "/sbin/ifconfig %s up", inter) != 0)
1256f5d6f24eSnia 		return 0;
1257f5d6f24eSnia 
1258f5d6f24eSnia 	/*
1259f5d6f24eSnia 	 * have to use system() here to avoid the server process dying
1260f5d6f24eSnia 	 */
1261f5d6f24eSnia 	if (snprintf(wpa_cmd, sizeof(wpa_cmd),
1262f5d6f24eSnia 	    WPA_SUPPLICANT
1263f5d6f24eSnia 	    " -B -s -i %s -D bsd -c /etc/wpa_supplicant.conf", inter) < 0)
1264f5d6f24eSnia 		return 0;
1265f5d6f24eSnia 	(void)do_system(wpa_cmd);
1266f5d6f24eSnia 
1267f5d6f24eSnia 	return 1;
1268f5d6f24eSnia }
1269f5d6f24eSnia 
1270f5d6f24eSnia int
config_dhcp(char * inter)127150dbef1aSdholland config_dhcp(char *inter)
127250dbef1aSdholland {
127350dbef1aSdholland 	int dhcpautoconf;
127450dbef1aSdholland 
127550dbef1aSdholland 	/*
127650dbef1aSdholland 	 * Don't bother checking for an existing instance of dhcpcd, just
127750dbef1aSdholland 	 * ask it to renew the lease.  It will fork and daemonize if there
127850dbef1aSdholland 	 * wasn't already an instance.
127950dbef1aSdholland 	 */
128050dbef1aSdholland 
128150dbef1aSdholland 	if (!file_mode_match(DHCPCD, S_IFREG))
128250dbef1aSdholland 		return 0;
12835270515fSmartin 	if (ask_yesno(MSG_Perform_autoconfiguration)) {
128450dbef1aSdholland 		/* spawn off dhcpcd and wait for parent to exit */
128550dbef1aSdholland 		dhcpautoconf = run_program(RUN_DISPLAY | RUN_PROGRESS,
128650dbef1aSdholland 		    "%s -d -n %s", DHCPCD, inter);
128750dbef1aSdholland 		return dhcpautoconf ? 0 : 1;
128850dbef1aSdholland 	}
128950dbef1aSdholland 	return 0;
129050dbef1aSdholland }
1291f5d6f24eSnia 
1292f5d6f24eSnia 
1293f5d6f24eSnia int
config_eth_medium(char * inter)1294f5d6f24eSnia config_eth_medium(char *inter)
1295f5d6f24eSnia {
1296f5d6f24eSnia 	char *textbuf = NULL;
1297f5d6f24eSnia 
1298f5d6f24eSnia 	for (;;) {
1299f5d6f24eSnia 		msg_prompt_add(MSG_net_media, net_media, net_media,
1300f5d6f24eSnia 				sizeof net_media);
1301f5d6f24eSnia 
1302f5d6f24eSnia 		/*
1303f5d6f24eSnia 		 * ifconfig does not allow media specifiers on
1304f5d6f24eSnia 		 * IFM_MANUAL interfaces.  Our UI gives no way
1305f5d6f24eSnia 		 * to set an option back
1306f5d6f24eSnia 		 * to null-string if it gets accidentally set.
1307f5d6f24eSnia 		 * Check for plausible alternatives.
1308f5d6f24eSnia 		 */
1309f5d6f24eSnia 		if (strcmp(net_media, "<default>") == 0 ||
1310f5d6f24eSnia 		    strcmp(net_media, "default") == 0 ||
1311f5d6f24eSnia 		    strcmp(net_media, "<manual>") == 0 ||
1312f5d6f24eSnia 		    strcmp(net_media, "manual") == 0 ||
1313f5d6f24eSnia 		    strcmp(net_media, "<none>") == 0 ||
1314f5d6f24eSnia 		    strcmp(net_media, "none") == 0 ||
1315f5d6f24eSnia 		    strcmp(net_media, " ") == 0) {
1316f5d6f24eSnia 			*net_media = '\0';
1317f5d6f24eSnia 		}
1318f5d6f24eSnia 
1319f5d6f24eSnia 		if (*net_media == '\0')
1320f5d6f24eSnia 			break;
1321f5d6f24eSnia 		/*
1322f5d6f24eSnia 		 * We must set the media type here - to give dhcp
1323f5d6f24eSnia 		 * a chance
1324f5d6f24eSnia 		 */
1325f5d6f24eSnia 		if (run_program(0, "/sbin/ifconfig %s media %s",
1326f5d6f24eSnia 			    net_dev, net_media) == 0)
1327f5d6f24eSnia 			break;
1328f5d6f24eSnia 		/* Failed to set - output the supported values */
1329f5d6f24eSnia 		if (collect(T_OUTPUT, &textbuf, "/sbin/ifconfig -m %s |"
1330f5d6f24eSnia 			    "while IFS=; read line;"
1331f5d6f24eSnia 			    " do [ \"$line\" = \"${line#*media}\" ] || "
1332f5d6f24eSnia 			    "echo $line;"
1333f5d6f24eSnia 			    " done", net_dev ) > 0)
1334f5d6f24eSnia 			msg_display(textbuf);
1335f5d6f24eSnia 		free(textbuf);
1336f5d6f24eSnia 	}
1337f5d6f24eSnia 	return 0;
1338f5d6f24eSnia }
1339