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