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