[BACK]Return to net.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / distrib / utils / sysinst

Annotation of src/distrib/utils/sysinst/net.c, Revision 1.24

1.24    ! jonathan    1: /*     $NetBSD: net.c,v 1.23 1997/11/25 06:53:11 thorpej Exp $ */
1.1       phil        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. All advertising materials mentioning features or use of this software
                     18:  *    must display the following acknowledgement:
                     19:  *      This product includes software develooped for the NetBSD Project by
                     20:  *      Piermont Information Systems Inc.
                     21:  * 4. The name of Piermont Information Systems Inc. may not be used to endorse
                     22:  *    or promote products derived from this software without specific prior
                     23:  *    written permission.
                     24:  *
                     25:  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
                     26:  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     27:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     28:  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
                     29:  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     30:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     31:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     32:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     33:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     34:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
                     35:  * THE POSSIBILITY OF SUCH DAMAGE.
                     36:  *
                     37:  */
                     38:
                     39: /* net.c -- routines to fetch files off the network. */
                     40:
                     41: #include <stdio.h>
                     42: #include <stdlib.h>
                     43: #include <string.h>
                     44: #include <curses.h>
                     45: #include <unistd.h>
1.21      simonb     46: #include <sys/param.h>
1.1       phil       47: #include "defs.h"
                     48: #include "md.h"
                     49: #include "msg_defs.h"
                     50: #include "menu_defs.h"
                     51: #include "txtwalk.h"
                     52:
1.10      phil       53: static int network_up = 0;
1.8       phil       54:
1.9       phil       55: /* Get the list of network interfaces. */
                     56:
1.1       phil       57: static void get_ifconfig_info (void)
                     58: {
                     59:        char *textbuf;
                     60:        int   textsize;
1.4       phil       61:        char *t;
1.1       phil       62:
                     63:        /* Get ifconfig information */
                     64:
                     65:        textsize = collect (T_OUTPUT, &textbuf,
1.3       phil       66:                            "/sbin/ifconfig -l 2>/dev/null");
1.1       phil       67:        if (textsize < 0) {
                     68:                endwin();
                     69:                (void) fprintf (stderr, "Could not run ifconfig.");
                     70:                exit (1);
                     71:        }
1.3       phil       72:        (void) strtok(textbuf,"\n");
                     73:        strncpy (net_devices, textbuf, textsize<STRSIZE ? textsize : STRSIZE);
                     74:        net_devices[STRSIZE] = 0;
1.1       phil       75:        free (textbuf);
1.4       phil       76:
                     77:        /* Remove lo0 and anything after ... */
                     78:        t = strstr (net_devices, "lo0");
                     79:        if (t != NULL)
                     80:                *t = 0;
1.1       phil       81: }
                     82:
1.21      simonb     83: /* Fill in defaults network values for the selected interface */
                     84:
                     85: static void get_ifinterface_info(void)
                     86: {
                     87:        char *textbuf;
                     88:        int textsize;
                     89:        char *t;
                     90:        char hostname[MAXHOSTNAMELEN];
                     91:
                     92:        /* First look to see if the selected interface is already configured. */
                     93:        textsize = collect(T_OUTPUT, &textbuf, "/sbin/ifconfig %s 2>/dev/null",
                     94:                           net_dev);
                     95:        if (textsize >= 0) {
                     96:                (void)strtok(textbuf, " \t\n"); /* ignore interface name */
                     97:                while ((t = strtok(NULL, " \t\n")) != NULL) {
                     98:                        if (strcmp(t, "inet") == 0) {
                     99:                                t = strtok(NULL, " \t\n");
                    100:                                if (strcmp(t, "0.0.0.0") != 0)
                    101:                                        strcpy(net_ip, t);
                    102:                        }
                    103:                        else if (strcmp(t, "netmask") == 0) {
                    104:                                t = strtok(NULL, " \t\n");
                    105:                                if (strcmp(t, "0x0") != 0)
                    106:                                        strcpy(net_mask, t);
                    107:                        }
                    108:                }
                    109:        }
                    110:
                    111:        /* Check host (and domain?) name */
                    112:        if (gethostname(hostname, sizeof(hostname)) == 0)
                    113:                strncpy(net_host, hostname, sizeof(net_host));
                    114: }
                    115:
1.9       phil      116: /* Get the information to configure the network, configure it and
                    117:    make sure both the gateway and the name server are up. */
1.1       phil      118:
1.9       phil      119: int config_network (void)
1.1       phil      120: {      char *tp;
                    121:        char defname[255];
                    122:        int  octet0;
                    123:        int  pass;
                    124:
                    125:        FILE *f;
1.12      jonathan  126:        time_t now;
1.1       phil      127:
1.8       phil      128:        if (network_up)
                    129:                return 1;
                    130:
1.20      simonb    131:        network_up = 1;
1.1       phil      132:        net_devices[0] = '\0';
                    133:        get_ifconfig_info ();
1.19      simonb    134:        if (strlen(net_devices) == 0) {
                    135:                /* No network interfaces found! */
                    136:                msg_display (MSG_nonet);
                    137:                process_menu (MENU_ok);
                    138:                return -1;
                    139:        }
1.1       phil      140:        strncpy (defname, net_devices, 255);
                    141:        tp = defname;
                    142:        strsep(&tp, " ");
                    143:        msg_prompt (MSG_asknetdev, defname, net_dev, 255, net_devices);
                    144:        tp = net_dev;
                    145:        strsep(&tp, " ");
                    146:        net_dev[strlen(net_dev)+1] = 0;
                    147:        net_dev[strlen(net_dev)] = ' ';
                    148:        while ((strlen(net_dev) != 4 && strlen(net_dev) != 5) ||
                    149:                       strstr(net_devices, net_dev) == NULL) {
                    150:                msg_prompt (MSG_badnet, defname,  net_dev, 10,
                    151:                            net_devices);
                    152:                tp = net_dev;
                    153:                strsep(&tp, " ");
                    154:                net_dev[strlen(net_dev)+1] = 0;
                    155:                net_dev[strlen(net_dev)] = ' ';
                    156:        }
                    157:
                    158:        /* Remove that space we added. */
                    159:        net_dev[strlen(net_dev)-1] = 0;
1.21      simonb    160:
                    161:        /* Preload any defaults we can find */
                    162:        get_ifinterface_info ();
                    163:        pass = strlen(net_mask) == 0 ? 0 : 1;
1.1       phil      164:
                    165:        /* Get other net information */
                    166:        msg_display (MSG_netinfo);
                    167:        do {
                    168:                msg_prompt_add (MSG_net_domain, net_domain, net_domain,
                    169:                                STRSIZE);
                    170:                msg_prompt_add (MSG_net_host, net_host, net_host, STRSIZE);
                    171:                msg_prompt_add (MSG_net_ip, net_ip, net_ip, STRSIZE);
                    172:                octet0 = atoi(net_ip);
                    173:                if (!pass) {
                    174:                        if (0 <= octet0 && octet0 <= 127)
                    175:                                strcpy (net_mask, "0xff000000");
                    176:                        else if (127 <= octet0 && octet0 <= 191)
                    177:                                strcpy (net_mask, "0xffff0000");
                    178:                        else if (192 <= octet0 && octet0 <= 223)
                    179:                                strcpy (net_mask, "0xffff0000");
                    180:                }
                    181:                msg_prompt_add (MSG_net_mask, net_mask, net_mask, STRSIZE);
1.8       phil      182:                msg_prompt_add (MSG_net_defroute, net_defroute, net_defroute,
                    183:                                STRSIZE);
1.1       phil      184:                msg_prompt_add (MSG_net_namesrv, net_namesvr, net_namesvr,
                    185:                                STRSIZE);
                    186:
1.20      simonb    187:                msg_display (MSG_netok, net_domain, net_host, net_ip, net_mask,
                    188:                             *net_namesvr == '\0' ? "<none>" : net_namesvr,
                    189:                             *net_defroute == '\0' ? "<none>" : net_defroute);
1.1       phil      190:                process_menu (MENU_yesno);
                    191:                if (!yesno)
                    192:                        msg_display (MSG_netagain);
                    193:                pass++;
                    194:        } while (!yesno);
                    195:
1.20      simonb    196:        /* Create /etc/resolv.conf if a nameserver was given */
                    197:        if (strcmp(net_namesvr, "") != 0) {
1.1       phil      198: #ifdef DEBUG
1.20      simonb    199:                f = fopen ("/tmp/resolv.conf", "w");
1.1       phil      200: #else
1.20      simonb    201:                f = fopen ("/etc/resolv.conf", "w");
1.1       phil      202: #endif
1.20      simonb    203:                if (f == NULL) {
                    204:                        endwin();
                    205:                        (void)fprintf(stderr, "%s", msg_string(MSG_resolv));
                    206:                        exit(1);
                    207:                }
                    208:                time(&now);
                    209:                /* NB: ctime() returns a string ending in  '\n' */
                    210:                (void)fprintf (f, ";\n; BIND data file\n; %s %s;\n",
                    211:                               "Created by NetBSD sysinst on", ctime(&now));
                    212:                (void)fprintf (f,
                    213:                               "nameserver %s\nlookup file bind\nsearch %s\n",
                    214:                               net_namesvr, net_domain);
                    215:                fclose (f);
1.1       phil      216:        }
                    217:
                    218:        run_prog ("/sbin/ifconfig lo0 127.0.0.1");
                    219:        run_prog ("/sbin/ifconfig %s inet %s netmask %s", net_dev, net_ip,
                    220:                  net_mask);
1.3       phil      221:
1.20      simonb    222:        /* Set a default route if one was given */
                    223:        if (strcmp(net_defroute, "") != 0) {
                    224:                run_prog ("/sbin/route -f > /dev/null 2> /dev/null");
                    225:                run_prog ("/sbin/route add default %s > /dev/null 2> /dev/null",
                    226:                          net_defroute);
                    227:        }
                    228:
                    229:        if (strcmp(net_namesvr, "") != 0 && network_up)
                    230:                network_up = !run_prog ("/sbin/ping -c 2 %s > /dev/null",
                    231:                                        net_defroute);
                    232:
                    233:        if (strcmp(net_defroute, "") != 0 && network_up)
                    234:                network_up = !run_prog ("/sbin/ping -c 2 %s > /dev/null",
                    235:                                        net_namesvr);
1.8       phil      236:
                    237:        return network_up;
1.1       phil      238: }
                    239:
1.2       phil      240: int
1.1       phil      241: get_via_ftp (void)
1.2       phil      242: {
1.10      phil      243:        distinfo *list;
1.5       phil      244:        char filename[SSTRSIZE];
1.2       phil      245:        int  ret;
1.1       phil      246:
1.11      phil      247:        while (!config_network ()) {
1.3       phil      248:                msg_display (MSG_netnotup);
                    249:                process_menu (MENU_yesno);
                    250:                if (!yesno)
                    251:                        return 0;
                    252:        }
1.10      phil      253:
                    254:        cd_dist_dir ("ftp");
                    255:
1.15      phil      256:        /* Fill in final values for ftp_dir. */
1.23      thorpej   257:        strncat (ftp_dir, rel, STRSIZE-strlen(ftp_dir));
1.15      phil      258:        strcat  (ftp_dir, "/");
                    259:        strncat (ftp_dir, machine, STRSIZE-strlen(ftp_dir));
1.1       phil      260:        strncat (ftp_dir, ftp_prefix, STRSIZE-strlen(ftp_dir));
                    261:        process_menu (MENU_ftpsource);
                    262:
1.6       phil      263:        list = dist_list;
1.1       phil      264:        endwin();
1.10      phil      265:        while (list->name) {
                    266:                if (!list->getit) {
                    267:                        list++;
                    268:                        continue;
                    269:                }
1.23      thorpej   270:                snprintf (filename, SSTRSIZE, "%s%s", list->name, dist_postfix);
1.1       phil      271:                if (strcmp ("ftp", ftp_user) == 0)
1.5       phil      272:                        ret = run_prog("/usr/bin/ftp -a ftp://%s/%s/%s",
1.2       phil      273:                                       ftp_host, ftp_dir,
1.5       phil      274:                                       filename);
1.1       phil      275:                else
1.5       phil      276:                        ret = run_prog("/usr/bin/ftp ftp://%s:%s@%s/%s/%s",
1.2       phil      277:                                       ftp_user, ftp_pass, ftp_host, ftp_dir,
1.5       phil      278:                                       filename);
1.2       phil      279:                if (ret) {
                    280:                        /* Error getting the file.  Bad host name ... ? */
1.22      thorpej   281:                        msg_display (MSG_ftperror_cont);
                    282:                        getchar();
1.7       phil      283:                        puts (CL);
1.2       phil      284:                        wrefresh (stdscr);
                    285:                        msg_display (MSG_ftperror);
                    286:                        process_menu (MENU_yesno);
                    287:                        if (yesno)
                    288:                                process_menu (MENU_ftpsource);
                    289:                        else
                    290:                                return 0;
                    291:                        endwin();
                    292:                } else
                    293:                        list++;
1.18      phil      294:
1.1       phil      295:        }
                    296:        puts (CL); /* Just to make sure. */
1.2       phil      297:        wrefresh (stdscr);
1.1       phil      298: #ifndef DEBUG
1.12      jonathan  299:        chdir("/");     /* back to current real root */
1.1       phil      300: #endif
1.2       phil      301:        return 1;
1.1       phil      302: }
                    303:
1.5       phil      304: int
1.1       phil      305: get_via_nfs(void)
                    306: {
1.11      phil      307:         while (!config_network ()) {
1.5       phil      308:                 msg_display (MSG_netnotup);
                    309:                 process_menu (MENU_yesno);
                    310:                 if (!yesno)
                    311:                         return 0;
                    312:         }
                    313:
1.1       phil      314:        /* Get server and filepath */
1.5       phil      315:        process_menu (MENU_nfssource);
1.24    ! jonathan  316: again:
1.5       phil      317:
1.24    ! jonathan  318:        run_prog("/sbin/umount /mnt2  2> /dev/null");
        !           319:
1.1       phil      320:        /* Mount it */
1.24    ! jonathan  321:        if (run_prog("/sbin/mount -t nfs %s:%s /mnt2", nfs_host, nfs_dir)) {
        !           322:                msg_display (MSG_nfsbadmount, nfs_host, nfs_dir);
1.5       phil      323:                process_menu (MENU_nfsbadmount);
                    324:                if (!yesno)
                    325:                        return 0;
1.24    ! jonathan  326:                if (!ignorerror)
        !           327:                        goto again;
        !           328:        }
        !           329:
        !           330:        /* Verify distribution files exist.  */
        !           331:        if (distribution_sets_exist_p("/mnt2") == 0) {
        !           332:                msg_display(MSG_badsetdir, "/mnt2");
        !           333:                process_menu (MENU_nfsbadmount);
        !           334:                if (!yesno)
        !           335:                        return (0);
        !           336:                if (!ignorerror)
        !           337:                        goto again;
1.5       phil      338:        }
                    339:
                    340:        /* return location, don't clean... */
1.18      phil      341:        strcpy (ext_dir, "/mnt2");
1.5       phil      342:        clean_dist_dir = 0;
1.8       phil      343:        mnt2_mounted = 1;
1.5       phil      344:        return 1;
1.8       phil      345: }
                    346:
1.13      jonathan  347: /*
                    348:  * Write the network config info the user entered via menus into the
                    349:  * config files in the target disk.  Be careful not to lose any
                    350:  * information we don't immediately add back, in case the install
                    351:  * target is the currently-active root.
                    352:  */
1.8       phil      353: void
                    354: mnt_net_config(void)
                    355: {
                    356:        char ans [5] = "y";
1.12      jonathan  357:        char ifconfig_fn [STRSIZE];
1.16      jonathan  358:        FILE *f;
1.8       phil      359:
                    360:        if (network_up) {
                    361:                msg_prompt (MSG_mntnetconfig, ans, ans, 5);
                    362:                if (*ans == 'y') {
1.12      jonathan  363:
1.13      jonathan  364:                        /* If not running in target, copy resolv.conf there. */
1.16      jonathan  365:                        dup_file_into_target("/etc/resolv.conf");
1.13      jonathan  366:                        /*
                    367:                         * Add IPaddr/hostname to  /etc/hosts.
                    368:                         * Be careful not to clobber any existing contents.
                    369:                         * Relies on ordered seach of /etc/hosts. XXX YP?
                    370:                         */
1.16      jonathan  371:                        f = target_fopen("/etc/hosts", "a");
                    372:                        if (f != 0) {
1.17      phil      373:                                fprintf(f, msg_string(MSG_etc_hosts),
                    374:                                        net_ip, net_host, net_domain,
                    375:                                        net_host);
                    376:                                fclose(f);
1.16      jonathan  377:                        }
1.12      jonathan  378:
1.13      jonathan  379:                        /* Write IPaddr and netmask to /etc/ifconfig.if[0-9] */
1.16      jonathan  380:                        snprintf (ifconfig_fn, STRSIZE,
                    381:                                  "/etc/ifconfig.%s", net_dev);
                    382:                        f = target_fopen(ifconfig_fn, "w");
                    383:                        if (f != 0) {
                    384:                                fprintf(f, "%s netmask %s\n",
                    385:                                        net_ip, net_mask);
                    386:                                fclose(f);
                    387:                        }
                    388:
                    389:                        f = target_fopen("/etc/mygate", "w");
                    390:                        if (f != 0) {
                    391:                                fprintf(f, "%s\n", net_defroute);
                    392:                                fclose(f);
                    393:                        }
1.8       phil      394:                }
                    395:        }
1.1       phil      396: }

CVSweb <webmaster@jp.NetBSD.org>