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