]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/sysinstall/ftp.c
This commit was generated by cvs2svn to compensate for changes in r61843,
[FreeBSD/FreeBSD.git] / usr.sbin / sysinstall / ftp.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$
8  *
9  * Copyright (c) 1995
10  *      Jordan Hubbard.  All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer,
17  *    verbatim and that no modifications are made prior to this
18  *    point in the file.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36
37 #include "sysinstall.h"
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <sys/param.h>
42 #include <sys/wait.h>
43 #include <netdb.h>
44 #include <pwd.h>
45 #include <ftpio.h>
46
47 Boolean ftpInitted = FALSE;
48 static FILE *OpenConn;
49 int FtpPort;
50
51 /* Brings up attached network device, if any - takes FTP device as arg */
52 static Boolean
53 netUp(Device *dev)
54 {
55     Device *netdev = (Device *)dev->private;
56
57     if (netdev)
58         return netdev->init(netdev);
59     else
60         return TRUE;    /* No net == happy net */
61 }
62
63 /* Brings down attached network device, if any - takes FTP device as arg */
64 static void
65 netDown(Device *dev)
66 {
67     Device *netdev = (Device *)dev->private;
68
69     if (netdev)
70         netdev->shutdown(netdev);
71 }
72
73 Boolean
74 mediaInitFTP(Device *dev)
75 {
76     int i, code;
77     char *cp, *rel, *hostname, *dir;
78     char *user, *login_name, password[80];
79
80     if (ftpInitted)
81         return TRUE;
82
83     if (OpenConn) {
84         fclose(OpenConn);
85         OpenConn = NULL;
86     }
87
88     /* If we can't initialize the network, bag it! */
89     if (!netUp(dev))
90         return FALSE;
91
92 try:
93     cp = variable_get(VAR_FTP_PATH);
94     if (!cp) {
95         if (DITEM_STATUS(mediaSetFTP(NULL)) == DITEM_FAILURE || (cp = variable_get(VAR_FTP_PATH)) == NULL) {
96             msgConfirm("Unable to get proper FTP path.  FTP media not initialized.");
97             netDown(dev);
98             return FALSE;
99         }
100     }
101
102     hostname = variable_get(VAR_FTP_HOST);
103     dir = variable_get(VAR_FTP_DIR);
104     if (!hostname || !dir) {
105         msgConfirm("Missing FTP host or directory specification.  FTP media not initialized,");
106         netDown(dev);
107         return FALSE;
108     }
109     user = variable_get(VAR_FTP_USER);
110     login_name = (!user || !*user) ? "anonymous" : user;
111
112     if (variable_get(VAR_FTP_PASS))
113         SAFE_STRCPY(password, variable_get(VAR_FTP_PASS));
114     else if (RunningAsInit)
115         sprintf(password, "installer@%s", variable_get(VAR_HOSTNAME));
116     else {
117         struct passwd *pw;
118         char *user;
119
120         pw = getpwuid(getuid());
121         user = pw ? pw->pw_name : "ftp";
122         sprintf(password, "%s@%s", user, variable_get(VAR_HOSTNAME));
123     }
124     msgNotify("Logging in to %s@%s..", login_name, hostname);
125     if ((OpenConn = ftpLogin(hostname, login_name, password, FtpPort, isDebug(), &code)) == NULL) {
126         msgConfirm("Couldn't open FTP connection to %s:\n  %s.", hostname, ftpErrString(code));
127         goto punt;
128     }
129
130     ftpPassive(OpenConn, !strcmp(variable_get(VAR_FTP_STATE), "passive"));
131     ftpBinary(OpenConn);
132     if (dir && *dir != '\0') {
133         if ((i = ftpChdir(OpenConn, dir)) != 0) {
134             if (i == 550)
135                 msgConfirm("No such directory ftp://%s/%s\n"
136                            "please check your URL and try again.", hostname, dir);
137             else
138                 msgConfirm("FTP chdir to ftp://%s/%s returned error status:\n  %s.", hostname, dir, ftpErrString(i));
139             goto punt;
140         }
141     }
142
143     /* Give it a shot - can't hurt to try and zoom in if we can, unless the release is set to
144        __RELEASE or "none" which signifies that it's not set */
145     rel = variable_get(VAR_RELNAME);
146     if (strcmp(rel, "__RELEASE") && strcmp(rel, "none"))
147         i = ftpChdir(OpenConn, rel);
148     else
149         i = 0;
150     if (i) {
151         if (!msgYesNo("Warning:  Can't CD to `%s' distribution on this\n"
152                       "FTP server.  You may need to visit a different server for\n"
153                       "the release you're trying to fetch or go to the Options\n"
154                       "menu and to set the release name to explicitly match what's\n"
155                       "available on %s (or set to \"none\").\n\n"
156                       "Would you like to select another FTP server?",
157                       rel, hostname)) {
158             variable_unset(VAR_FTP_PATH);
159             if (DITEM_STATUS(mediaSetFTP(NULL)) == DITEM_FAILURE)
160                 goto punt;
161             else
162                 goto try;
163         }
164         else
165             goto punt;
166     }
167     ftpInitted = TRUE;
168     return TRUE;
169
170 punt:
171     ftpInitted = FALSE;
172     if (OpenConn != NULL) {
173         fclose(OpenConn);
174         OpenConn = NULL;
175     }
176     netDown(dev);
177     variable_unset(VAR_FTP_PATH);
178     return FALSE;
179 }
180
181 FILE *
182 mediaGetFTP(Device *dev, char *file, Boolean probe)
183 {
184     int nretries = 1;
185     FILE *fp;
186     char *try, buf[PATH_MAX];
187
188     if (!OpenConn) {
189         msgDebug("No FTP connection open, can't get file %s\n", file);
190         return NULL;
191     }
192
193     try = file;
194     while ((fp = ftpGet(OpenConn, try, 0)) == NULL) {
195         int ftperr = ftpErrno(OpenConn);
196
197         /* If a hard fail, try to "bounce" the ftp server to clear it */
198         if (ftperr != 550) {
199             if (ftperr != 421)  /* Timeout? */
200                 variable_unset(VAR_FTP_PATH);
201             /* If we can't re-initialize, just forget it */
202             dev->shutdown(dev);
203             if (!dev->init(dev)) {
204                 netDown(dev);
205                 if (OpenConn) {
206                     fclose(OpenConn);
207                     OpenConn = NULL;
208                 }
209                 variable_unset(VAR_FTP_PATH);
210                 return NULL;
211             }
212         }
213         else if (probe)
214             return NULL;
215         else {
216             /* Try some alternatives */
217             switch (nretries++) {
218             case 1:
219                 sprintf(buf, "releases/%s", file);
220                 try = buf;
221                 break;
222
223             case 2:
224                 sprintf(buf, "%s/%s", variable_get(VAR_RELNAME), file);
225                 try = buf;
226                 break;
227
228             case 3:
229                 sprintf(buf, "%s/releases/%s", variable_get(VAR_RELNAME), file);
230                 try = buf;
231                 break;
232
233             case 4:
234                 try = file;
235                 break;
236             }
237         }
238     }
239     return fp;
240 }
241
242 void
243 mediaShutdownFTP(Device *dev)
244 {
245     if (!ftpInitted)
246         return;
247
248     if (OpenConn != NULL) {
249         fclose(OpenConn);
250         OpenConn = NULL;
251     }
252     ftpInitted = FALSE;
253 }