1995-05-24 09:00:58 +00:00
|
|
|
/*
|
1996-12-11 09:35:06 +00:00
|
|
|
* The new sysinstall program.
|
1995-05-24 09:00:58 +00:00
|
|
|
*
|
1996-12-11 09:35:06 +00:00
|
|
|
* This is probably the last attempt in the `sysinstall' line, the next
|
|
|
|
* generation being slated to essentially a complete rewrite.
|
|
|
|
*
|
1999-08-28 01:35:59 +00:00
|
|
|
* $FreeBSD$
|
1996-12-11 09:35:06 +00:00
|
|
|
*
|
|
|
|
* Copyright (c) 1995
|
|
|
|
* Jordan Hubbard. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer,
|
|
|
|
* verbatim and that no modifications are made prior to this
|
|
|
|
* point in the file.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
1995-05-24 09:00:58 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
1996-12-11 09:35:06 +00:00
|
|
|
#include "sysinstall.h"
|
1995-05-24 11:19:11 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
1996-12-11 09:35:06 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <netdb.h>
|
1997-10-03 03:32:03 +00:00
|
|
|
#include <pwd.h>
|
1996-12-11 09:35:06 +00:00
|
|
|
#include <ftpio.h>
|
1995-05-24 11:19:11 +00:00
|
|
|
|
1996-12-11 09:35:06 +00:00
|
|
|
Boolean ftpInitted = FALSE;
|
|
|
|
static FILE *OpenConn;
|
1995-06-11 19:33:05 +00:00
|
|
|
int FtpPort;
|
|
|
|
|
2000-07-25 23:22:16 +00:00
|
|
|
/* List of sub directories to look for under a given FTP server. */
|
2000-12-06 00:47:54 +00:00
|
|
|
const char *ftp_dirs[] = { ".", "releases/"MACHINE, "snapshots/"MACHINE,
|
2000-07-25 23:22:16 +00:00
|
|
|
"pub/FreeBSD", "pub/FreeBSD/releases/"MACHINE,
|
|
|
|
"pub/FreeBSD/snapshots/"MACHINE, NULL };
|
|
|
|
|
1997-01-30 06:38:19 +00:00
|
|
|
/* Brings up attached network device, if any - takes FTP device as arg */
|
|
|
|
static Boolean
|
|
|
|
netUp(Device *dev)
|
|
|
|
{
|
|
|
|
Device *netdev = (Device *)dev->private;
|
|
|
|
|
|
|
|
if (netdev)
|
|
|
|
return netdev->init(netdev);
|
|
|
|
else
|
|
|
|
return TRUE; /* No net == happy net */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Brings down attached network device, if any - takes FTP device as arg */
|
|
|
|
static void
|
|
|
|
netDown(Device *dev)
|
|
|
|
{
|
|
|
|
Device *netdev = (Device *)dev->private;
|
|
|
|
|
|
|
|
if (netdev)
|
|
|
|
netdev->shutdown(netdev);
|
|
|
|
}
|
|
|
|
|
1996-12-11 09:35:06 +00:00
|
|
|
Boolean
|
|
|
|
mediaInitFTP(Device *dev)
|
1996-08-01 12:35:51 +00:00
|
|
|
{
|
2000-07-25 23:22:16 +00:00
|
|
|
int i, code, af, fdir;
|
1996-12-11 09:35:06 +00:00
|
|
|
char *cp, *rel, *hostname, *dir;
|
|
|
|
char *user, *login_name, password[80];
|
1996-08-01 12:35:51 +00:00
|
|
|
|
1996-12-11 09:35:06 +00:00
|
|
|
if (ftpInitted)
|
|
|
|
return TRUE;
|
1995-05-23 02:41:18 +00:00
|
|
|
|
1996-12-11 09:35:06 +00:00
|
|
|
if (OpenConn) {
|
|
|
|
fclose(OpenConn);
|
|
|
|
OpenConn = NULL;
|
1996-08-01 12:35:51 +00:00
|
|
|
}
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1997-01-01 12:36:09 +00:00
|
|
|
/* If we can't initialize the network, bag it! */
|
1997-01-30 06:38:19 +00:00
|
|
|
if (!netUp(dev))
|
1997-01-01 12:36:09 +00:00
|
|
|
return FALSE;
|
|
|
|
|
1996-12-11 09:35:06 +00:00
|
|
|
try:
|
|
|
|
cp = variable_get(VAR_FTP_PATH);
|
|
|
|
if (!cp) {
|
1997-01-17 08:47:13 +00:00
|
|
|
if (DITEM_STATUS(mediaSetFTP(NULL)) == DITEM_FAILURE || (cp = variable_get(VAR_FTP_PATH)) == NULL) {
|
|
|
|
msgConfirm("Unable to get proper FTP path. FTP media not initialized.");
|
1997-01-30 06:38:19 +00:00
|
|
|
netDown(dev);
|
1997-01-17 08:47:13 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
1995-05-24 09:00:58 +00:00
|
|
|
}
|
1995-05-23 02:41:18 +00:00
|
|
|
|
1996-12-11 09:35:06 +00:00
|
|
|
hostname = variable_get(VAR_FTP_HOST);
|
|
|
|
dir = variable_get(VAR_FTP_DIR);
|
1997-01-17 08:47:13 +00:00
|
|
|
if (!hostname || !dir) {
|
|
|
|
msgConfirm("Missing FTP host or directory specification. FTP media not initialized,");
|
1997-01-30 06:38:19 +00:00
|
|
|
netDown(dev);
|
1997-01-17 08:47:13 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
1996-12-11 09:35:06 +00:00
|
|
|
user = variable_get(VAR_FTP_USER);
|
1997-01-01 12:36:09 +00:00
|
|
|
login_name = (!user || !*user) ? "anonymous" : user;
|
|
|
|
|
1996-12-11 09:35:06 +00:00
|
|
|
if (variable_get(VAR_FTP_PASS))
|
|
|
|
SAFE_STRCPY(password, variable_get(VAR_FTP_PASS));
|
1997-10-03 03:32:03 +00:00
|
|
|
else if (RunningAsInit)
|
1996-12-11 09:35:06 +00:00
|
|
|
sprintf(password, "installer@%s", variable_get(VAR_HOSTNAME));
|
1997-10-03 03:32:03 +00:00
|
|
|
else {
|
|
|
|
struct passwd *pw;
|
|
|
|
char *user;
|
|
|
|
|
|
|
|
pw = getpwuid(getuid());
|
|
|
|
user = pw ? pw->pw_name : "ftp";
|
|
|
|
sprintf(password, "%s@%s", user, variable_get(VAR_HOSTNAME));
|
|
|
|
}
|
2000-07-14 08:33:10 +00:00
|
|
|
af = variable_cmp(VAR_IPV6_ENABLE, "YES") ? AF_INET : AF_UNSPEC;
|
1996-12-11 19:35:26 +00:00
|
|
|
msgNotify("Logging in to %s@%s..", login_name, hostname);
|
2000-07-14 08:33:10 +00:00
|
|
|
if ((OpenConn = ftpLoginAf(hostname, af, login_name, password, FtpPort, isDebug(), &code)) == NULL) {
|
1997-01-29 21:45:30 +00:00
|
|
|
msgConfirm("Couldn't open FTP connection to %s:\n %s.", hostname, ftpErrString(code));
|
1996-12-11 09:35:06 +00:00
|
|
|
goto punt;
|
1995-05-24 09:00:58 +00:00
|
|
|
}
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1996-12-11 09:35:06 +00:00
|
|
|
ftpPassive(OpenConn, !strcmp(variable_get(VAR_FTP_STATE), "passive"));
|
|
|
|
ftpBinary(OpenConn);
|
|
|
|
if (dir && *dir != '\0') {
|
|
|
|
if ((i = ftpChdir(OpenConn, dir)) != 0) {
|
1997-01-15 06:30:50 +00:00
|
|
|
if (i == 550)
|
1997-01-15 16:21:10 +00:00
|
|
|
msgConfirm("No such directory ftp://%s/%s\n"
|
|
|
|
"please check your URL and try again.", hostname, dir);
|
1997-01-15 06:30:50 +00:00
|
|
|
else
|
1997-01-29 21:45:30 +00:00
|
|
|
msgConfirm("FTP chdir to ftp://%s/%s returned error status:\n %s.", hostname, dir, ftpErrString(i));
|
1996-12-11 09:35:06 +00:00
|
|
|
goto punt;
|
1995-05-24 01:27:15 +00:00
|
|
|
}
|
1995-05-24 09:00:58 +00:00
|
|
|
}
|
1995-05-30 08:29:07 +00:00
|
|
|
|
2000-07-25 23:22:16 +00:00
|
|
|
/*
|
|
|
|
* Now that we've verified that the path we're given is ok, let's try to
|
|
|
|
* be a bit intelligent in locating the release we are looking for. First
|
2000-09-22 08:13:36 +00:00
|
|
|
* off, if the release is specified as "__RELEASE" or "any", then just
|
2000-07-25 23:22:16 +00:00
|
|
|
* assume that the current directory is the one we want and give up.
|
|
|
|
*/
|
1996-12-11 09:35:06 +00:00
|
|
|
rel = variable_get(VAR_RELNAME);
|
2000-09-22 08:13:36 +00:00
|
|
|
if (strcmp(rel, "__RELEASE") && strcmp(rel, "any")) {
|
2000-07-25 23:22:16 +00:00
|
|
|
/*
|
|
|
|
* Ok, since we have a release variable, let's walk through the list
|
|
|
|
* of directories looking for a release directory. The first one to
|
|
|
|
* match wins. For each case, we chdir to ftp_dirs[fdir] first. If
|
|
|
|
* that fails, we skip to the next one. Otherwise, we try to chdir to
|
|
|
|
* rel. If it succeeds we break out. If it fails, then we go back to
|
|
|
|
* the base directory and try again. Lots of chdirs, but oh well. :)
|
|
|
|
*/
|
2000-09-19 07:45:08 +00:00
|
|
|
for (fdir = 0; ftp_dirs[fdir]; fdir++) {
|
2001-03-14 10:05:48 +00:00
|
|
|
/* Avoid sending CWD . commands which confuse some ftp servers */
|
|
|
|
if (strcmp(ftp_dirs[fdir], ".") &&
|
|
|
|
(ftpChdir(OpenConn, (char *)ftp_dirs[fdir]) != 0))
|
2000-07-25 23:22:16 +00:00
|
|
|
continue;
|
|
|
|
if (ftpChdir(OpenConn, rel) == 0) {
|
|
|
|
ftpInitted = TRUE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2000-09-19 07:45:08 +00:00
|
|
|
else /* reset to "root" dir for a fresh try */
|
|
|
|
ftpChdir(OpenConn, "/");
|
2000-07-25 23:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we get here, then all of the directories we tried failed, so
|
|
|
|
* print out the error message and ask the user if they want to try
|
|
|
|
* again.
|
|
|
|
*/
|
|
|
|
if (!msgYesNo("Warning: Can't find the `%s' distribution on this\n"
|
1996-12-11 09:35:06 +00:00
|
|
|
"FTP server. You may need to visit a different server for\n"
|
2000-07-25 23:22:16 +00:00
|
|
|
"the release you are trying to fetch or go to the Options\n"
|
1996-12-11 09:35:06 +00:00
|
|
|
"menu and to set the release name to explicitly match what's\n"
|
2000-09-22 08:13:36 +00:00
|
|
|
"available on %s (or set to \"any\").\n\n"
|
1996-12-11 09:35:06 +00:00
|
|
|
"Would you like to select another FTP server?",
|
|
|
|
rel, hostname)) {
|
|
|
|
variable_unset(VAR_FTP_PATH);
|
2000-07-25 23:22:16 +00:00
|
|
|
if (DITEM_STATUS(mediaSetFTP(NULL)) != DITEM_FAILURE)
|
1996-12-11 09:35:06 +00:00
|
|
|
goto try;
|
|
|
|
}
|
2000-07-25 23:22:16 +00:00
|
|
|
} else {
|
|
|
|
ftpInitted = TRUE;
|
|
|
|
return TRUE;
|
1995-05-24 09:00:58 +00:00
|
|
|
}
|
1996-12-11 09:35:06 +00:00
|
|
|
|
|
|
|
punt:
|
1997-01-30 06:38:19 +00:00
|
|
|
ftpInitted = FALSE;
|
1996-12-11 09:35:06 +00:00
|
|
|
if (OpenConn != NULL) {
|
|
|
|
fclose(OpenConn);
|
|
|
|
OpenConn = NULL;
|
1995-06-11 19:33:05 +00:00
|
|
|
}
|
1997-01-30 06:38:19 +00:00
|
|
|
netDown(dev);
|
1996-12-11 09:35:06 +00:00
|
|
|
variable_unset(VAR_FTP_PATH);
|
|
|
|
return FALSE;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
1996-12-11 09:35:06 +00:00
|
|
|
FILE *
|
|
|
|
mediaGetFTP(Device *dev, char *file, Boolean probe)
|
1995-05-23 02:41:18 +00:00
|
|
|
{
|
1996-12-11 09:35:06 +00:00
|
|
|
int nretries = 1;
|
|
|
|
FILE *fp;
|
|
|
|
char *try, buf[PATH_MAX];
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1996-12-11 09:35:06 +00:00
|
|
|
if (!OpenConn) {
|
|
|
|
msgDebug("No FTP connection open, can't get file %s\n", file);
|
|
|
|
return NULL;
|
1995-05-24 09:00:58 +00:00
|
|
|
}
|
1995-05-24 11:19:11 +00:00
|
|
|
|
1996-12-11 09:35:06 +00:00
|
|
|
try = file;
|
|
|
|
while ((fp = ftpGet(OpenConn, try, 0)) == NULL) {
|
1997-10-03 14:00:09 +00:00
|
|
|
int ftperr = ftpErrno(OpenConn);
|
|
|
|
|
1996-12-11 09:35:06 +00:00
|
|
|
/* If a hard fail, try to "bounce" the ftp server to clear it */
|
1997-10-03 14:00:09 +00:00
|
|
|
if (ftperr != 550) {
|
|
|
|
if (ftperr != 421) /* Timeout? */
|
1997-02-18 01:35:37 +00:00
|
|
|
variable_unset(VAR_FTP_PATH);
|
1996-12-11 09:35:06 +00:00
|
|
|
/* If we can't re-initialize, just forget it */
|
1997-10-03 14:00:09 +00:00
|
|
|
dev->shutdown(dev);
|
1996-12-11 09:35:06 +00:00
|
|
|
if (!dev->init(dev)) {
|
1997-01-30 06:38:19 +00:00
|
|
|
netDown(dev);
|
1998-01-28 04:42:38 +00:00
|
|
|
if (OpenConn) {
|
|
|
|
fclose(OpenConn);
|
|
|
|
OpenConn = NULL;
|
|
|
|
}
|
1997-02-18 01:35:37 +00:00
|
|
|
variable_unset(VAR_FTP_PATH);
|
1996-12-11 09:35:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (probe)
|
|
|
|
return NULL;
|
|
|
|
else {
|
|
|
|
/* Try some alternatives */
|
|
|
|
switch (nretries++) {
|
|
|
|
case 1:
|
1998-12-22 12:31:26 +00:00
|
|
|
sprintf(buf, "releases/%s", file);
|
1996-12-11 09:35:06 +00:00
|
|
|
try = buf;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
sprintf(buf, "%s/%s", variable_get(VAR_RELNAME), file);
|
|
|
|
try = buf;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
1998-12-22 12:31:26 +00:00
|
|
|
sprintf(buf, "%s/releases/%s", variable_get(VAR_RELNAME), file);
|
1996-12-11 09:35:06 +00:00
|
|
|
try = buf;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
try = file;
|
|
|
|
break;
|
|
|
|
}
|
1995-06-11 19:33:05 +00:00
|
|
|
}
|
1995-05-24 09:00:58 +00:00
|
|
|
}
|
1996-12-11 09:35:06 +00:00
|
|
|
return fp;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
1996-12-11 09:35:06 +00:00
|
|
|
void
|
|
|
|
mediaShutdownFTP(Device *dev)
|
1995-05-23 02:41:18 +00:00
|
|
|
{
|
1996-12-11 09:35:06 +00:00
|
|
|
if (!ftpInitted)
|
|
|
|
return;
|
1995-06-11 19:33:05 +00:00
|
|
|
|
1996-12-11 09:35:06 +00:00
|
|
|
if (OpenConn != NULL) {
|
|
|
|
fclose(OpenConn);
|
|
|
|
OpenConn = NULL;
|
|
|
|
}
|
|
|
|
ftpInitted = FALSE;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|