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