Remove NOSECURE which no longer serves a purpose. Note: FreeBSD also removed
[dragonfly.git] / release / sysinstall / network.c
1 /*
2  * The new sysinstall program.
3  *
4  * This is probably the last attempt in the `sysinstall' line, the next
5  * generation being slated to essentially a complete rewrite.
6  *
7  * $FreeBSD: src/release/sysinstall/network.c,v 1.46.2.4 2001/07/19 01:33:02 kris Exp $
8  * $DragonFly: src/release/sysinstall/Attic/network.c,v 1.2 2003/06/17 04:27:21 dillon Exp $
9  *
10  * Copyright (c) 1995
11  *      Jordan Hubbard.  All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer,
18  *    verbatim and that no modifications are made prior to this
19  *    point in the file.
20  *
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  *
25  * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
26  * 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 JORDAN HUBBARD OR HIS PETS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  */
38
39 /* These routines deal with getting things off of network media */
40
41 #include "sysinstall.h"
42 #include <signal.h>
43 #include <termios.h>
44 #include <sys/fcntl.h>
45 #include <sys/ioctl.h>
46 #include <sys/stat.h>
47
48 static Boolean  networkInitialized;
49 static pid_t    startPPP(Device *devp);
50
51 static pid_t    pppPID;
52
53 Boolean
54 mediaInitNetwork(Device *dev)
55 {
56     int i;
57     char *rp;
58     char *cp, ifconfig[255];
59     WINDOW *w;
60     
61     if (!RunningAsInit || networkInitialized)
62         return TRUE;
63
64     if (isDebug())
65         msgDebug("Init routine called for network device %s.\n", dev->name);
66
67     if (!file_readable("/etc/resolv.conf")) {
68         if (DITEM_STATUS(configResolv(NULL)) == DITEM_FAILURE) {
69             msgConfirm("Can't seem to write out /etc/resolv.conf.  Net cannot be used.");
70             return FALSE;
71         }
72     }
73
74     w = savescr();
75     dialog_clear_norefresh();
76
77     /* Old PPP process lying around? */
78     if (pppPID) {
79         msgConfirm("Killing previous PPP process %d.", pppPID);
80         kill(pppPID, SIGTERM);
81         pppPID = 0;
82     }
83     if (!strncmp("ppp", dev->name, 3)) {        /* PPP? */
84         if (!(pppPID = startPPP(dev))) {
85             msgConfirm("Unable to start PPP!  This installation method cannot be used.");
86             return FALSE;
87         }
88         networkInitialized = TRUE;
89         return TRUE;
90     }
91     else if (!strncmp("sl", dev->name, 2)) {    /* SLIP? */
92         char *val;
93         char attach[256];
94
95         /* Cheesy slip attach */
96         snprintf(attach, 256, "slattach -a -h -l -s 9600 %s", dev->devname);
97         val = msgGetInput(attach,
98                           "Warning:  SLIP is rather poorly supported in this revision\n"
99                           "of the installation due to the lack of a dialing utility.\n"
100                           "If you can use PPP for this instead then you're much better\n"
101                           "off doing so, otherwise SLIP works fairly well for *hardwired*\n"
102                           "links.  Please edit the following slattach command for\n"
103                           "correctness (default here is: VJ compression, Hardware flow-\n"
104                           "control, ignore carrier and 9600 baud data rate).  When you're\n"
105                           "ready, press [ENTER] to execute it.");
106         if (!val) {
107             msgConfirm("slattach command was empty.  Try again!");
108             restorescr(w);
109             return FALSE;
110         }
111         else
112             SAFE_STRCPY(attach, val);
113         /*
114          * Doing this with vsystem() is actually bogus since we should be storing the pid of slattach
115          * for later killing.  It's just too convenient to call vsystem(), however, rather than
116          * constructing a proper argument for exec() so we punt on doing slip right for now.
117          */
118         if (vsystem("%s", attach)) {
119             msgConfirm("slattach returned a bad status!  Please verify that\n"
120                        "the command is correct and try this operation again.");
121             restorescr(w);
122             return FALSE;
123         }
124         restorescr(w);
125     }
126
127     snprintf(ifconfig, 255, "%s%s", VAR_IFCONFIG, dev->name);
128     cp = variable_get(ifconfig);
129     if (cp) {
130         if (strcmp(cp, "DHCP")) {
131             msgDebug("ifconfig %s %s", dev->name, cp);
132             i = vsystem("ifconfig %s %s", dev->name, cp);
133             if (i) {
134                 msgConfirm("Unable to configure the %s interface!\n"
135                            "This installation method cannot be used.",
136                            dev->name);
137                 return FALSE;
138             }
139             rp = variable_get(VAR_GATEWAY);
140             if (!rp || *rp == '0') {
141                 msgConfirm("No gateway has been set. You may be unable to access hosts\n"
142                            "not on your local network");
143             }
144             else {
145                 msgDebug("Adding default route to %s.", rp);
146                 vsystem("route -n add default %s", rp);
147             }
148         }
149     } else if ((cp = variable_get(VAR_IPV6ADDR)) == NULL || *cp == '\0') {
150         msgConfirm("The %s device is not configured.  You will need to do so\n"
151                    "in the Networking configuration menu before proceeding.", dev->name);
152         return FALSE;
153     }
154
155     if (isDebug())
156         msgDebug("Network initialized successfully.\n");
157     networkInitialized = TRUE;
158     return TRUE;
159 }
160
161 void
162 mediaShutdownNetwork(Device *dev)
163 {
164     char *cp;
165
166     if (!RunningAsInit || !networkInitialized)
167         return;
168
169     msgDebug("Shutdown called for network device %s\n", dev->name);
170     /* Not a serial device? */
171     if (strncmp("sl", dev->name, 2) && strncmp("ppp", dev->name, 3)) {
172         int i;
173         char ifconfig[255];
174
175         snprintf(ifconfig, 255, "%s%s", VAR_IFCONFIG, dev->name);
176         cp = variable_get(ifconfig);
177         if (!cp)
178             return;
179         msgDebug("ifconfig %s down", dev->name);
180         i = vsystem("ifconfig %s down", dev->name);
181         if (i)
182             msgConfirm("Warning: Unable to down the %s interface properly", dev->name);
183         cp = variable_get(VAR_GATEWAY);
184         if (cp) {
185             msgDebug("Deleting default route.");
186             vsystem("route -n delete default");
187         }
188     }
189     else if (pppPID) {
190         msgConfirm("Killing previous PPP process %d.", pppPID);
191         kill(pppPID, SIGTERM);
192         pppPID = 0;
193     }
194     networkInitialized = FALSE;
195 }
196
197 /* Start PPP on the 3rd screen */
198 static pid_t
199 startPPP(Device *devp)
200 {
201     int fd2, pulse;
202     FILE *fp;
203     char *val;
204     pid_t pid = 0;
205     char myaddr[16], provider[16], speed[16], authname[32], authkey[16];
206     char phone[16];
207     WINDOW *w = savescr();
208     
209     /* These are needed to make ppp work */
210     Mkdir("/var/log");
211     Mkdir("/var/run");
212     Mkdir("/var/spool/lock");
213     Mkdir("/etc/ppp");
214
215     dialog_clear_norefresh();
216     if (!variable_get(VAR_SERIAL_SPEED))
217         variable_set2(VAR_SERIAL_SPEED, "115200", 0);
218     /* Get any important user values */
219     val = variable_get_value(VAR_SERIAL_SPEED,
220                       "Enter the baud rate for your modem - this can be higher than the actual\n"
221                       "maximum data rate since most modems can talk at one speed to the\n"
222                       "computer and at another speed to the remote end.\n\n"
223                       "If you're not sure what to put here, just select the default.", 0);
224     SAFE_STRCPY(speed, (val && *val) ? val : "115200");
225
226     val = variable_get(VAR_GATEWAY);
227     SAFE_STRCPY(provider, (val && *val) ? val : "0");
228
229     dialog_clear_norefresh();
230     val = msgGetInput(provider, "Enter the IP address of your service provider or 0 if you\n"
231                       "don't know it and would prefer to negotiate it dynamically.");
232     SAFE_STRCPY(provider, (val && *val) ? val : "0");
233
234     if (devp->private && ((DevInfo *)devp->private)->ipaddr[0])
235         SAFE_STRCPY(myaddr, ((DevInfo *)devp->private)->ipaddr);
236     else
237         strcpy(myaddr, "0");
238
239     if (!Fake)
240         fp = fopen("/etc/ppp/ppp.linkup", "w");
241     else
242         fp = fopen("/dev/stderr", "w");
243     if (fp != NULL) {
244         fprintf(fp, "MYADDR:\n");
245         fprintf(fp, " delete ALL\n");
246         fprintf(fp, " add 0 0 HISADDR\n");
247         fchmod(fileno(fp), 0755);
248         fclose(fp);
249     }
250     if (!Fake)
251         fd2 = open("/etc/ppp/ppp.secret", O_CREAT);
252     else
253         fd2 = -1;
254     if (fd2 != -1) {
255         fchmod(fd2, 0700);
256         close(fd2);
257     }
258     if (!Fake)
259         fp = fopen("/etc/ppp/ppp.conf", "a");
260     else
261         fp = fopen("/dev/stderr", "w");
262     if (!fp) {
263         msgConfirm("Couldn't open /etc/ppp/ppp.conf file!  This isn't going to work");
264         restorescr(w);
265         return 0;
266     }
267     authname[0] = '\0';
268     pulse = 0;
269     dialog_clear_norefresh();
270     if (!dialog_yesno("", "Does your ISP support PAP or CHAP ppp logins?", -1, -1)) {
271         val = msgGetInput(NULL, "Enter the name you use to login to your provider.");
272         SAFE_STRCPY(authname, val);
273         dialog_clear_norefresh();
274         val = msgGetInput(NULL, "Enter the password you use to login to your provider.");
275         SAFE_STRCPY(authkey, val);
276         dialog_clear_norefresh();
277         val = msgGetInput(NULL, "Enter the your provider's login phone number.");
278         SAFE_STRCPY(phone, val);
279         dialog_clear_norefresh();
280         pulse = dialog_yesno("", "Does your telephone line support tone dialing?", -1, -1);
281     }
282     fprintf(fp, "\ninstall:\n");
283     fprintf(fp, " set speed %s\n", speed);
284     fprintf(fp, " set device %s\n", devp->devname);
285     fprintf(fp, " set ifaddr %s %s 255.255.255.0 0.0.0.0\n", myaddr, provider);
286     fprintf(fp, " add! default HISADDR\n");
287     fprintf(fp, " set timeout 0\n");
288     fprintf(fp, " enable dns\n");
289     fprintf(fp, " set log local phase\n");
290     if(authname[0] != '\0'){
291         fprintf(fp, " set dial \"ABORT BUSY ABORT NO\\\\sCARRIER TIMEOUT 5 \\\"\\\" AT OK-AT-OK ATE1Q0 OK \\\\dATD%c\\\\T TIMEOUT 40 CONNECT\"\n", pulse ? 'P' : 'T');
292         fprintf(fp, " set login\n");
293         fprintf(fp, " set authname %s\n", authname);
294         fprintf(fp, " set authkey %s\n", authkey);
295         fprintf(fp, " set phone %s\n", phone);
296     }
297     if (fchmod(fileno(fp), 0600) != 0)
298         msgConfirm("Warning: Failed to fix permissions on /etc/ppp/ppp.conf !");
299     fclose(fp);
300
301     /* Make the ppp config persistent */
302     variable_set2(VAR_PPP_ENABLE, "YES", 0);
303     variable_set2(VAR_PPP_PROFILE, "install", 0);
304
305     if (!Fake && !file_readable("/dev/tun0") && mknod("/dev/tun0", 0600 | S_IFCHR, makedev(52, 0))) {
306         msgConfirm("Warning:  No /dev/tun0 device.  PPP will not work!");
307         restorescr(w);
308         return 0;
309     }
310
311     if (isDebug())
312         msgDebug("About to start PPP on device %s @ %s baud.  Provider = %s\n", devp->devname, speed, provider);
313
314     if (!Fake && !(pid = fork())) {
315         int i, fd;
316         struct termios foo;
317         extern int login_tty(int);
318
319         for (i = getdtablesize(); i >= 0; i--)
320             close(i);
321
322         /* We're going over to VTY2 */
323         fd = open("/dev/ttyv2", O_RDWR);
324         ioctl(0, TIOCSCTTY, &fd);
325         dup2(0, 1);
326         dup2(0, 2);
327         DebugFD = 2;
328         if (login_tty(fd) == -1)
329             msgDebug("ppp: Can't set the controlling terminal.\n");
330         signal(SIGTTOU, SIG_IGN);
331         if (tcgetattr(fd, &foo) != -1) {
332             foo.c_cc[VERASE] = '\010';
333             if (tcsetattr(fd, TCSANOW, &foo) == -1)
334                 msgDebug("ppp: Unable to set the erase character.\n");
335         }
336         else
337             msgDebug("ppp: Unable to get the terminal attributes!\n");
338         execlp("ppp", "ppp", "install", (char *)NULL);
339         msgDebug("PPP process failed to exec!\n");
340         exit(1);
341     }
342     else {
343         dialog_clear_norefresh();
344         msgConfirm("NOTICE: The PPP command is now started on VTY3 (type ALT-F3 to\n"
345            "interact with it, ALT-F1 to switch back here). If you are using\n"
346            "a PAP or CHAP login simply enter \"dial\", otherwise you'll need\n"
347            "to use the \"term\" command which starts a terminal emulator\n"
348            "which you can use to talk to your modem and dial the service\n"
349            "provider.  Once you're connected, come back to this screen and\n"
350            "press return.\n\n"
351            "DO NOT PRESS [ENTER] HERE UNTIL THE CONNECTION IS FULLY\n"
352            "ESTABLISHED!");
353     }
354     restorescr(w);
355     return pid;
356 }