1995-05-27 10:39:04 +00:00
|
|
|
/*
|
|
|
|
* The new sysinstall program.
|
|
|
|
*
|
|
|
|
* This is probably the last attempt in the `sysinstall' line, the next
|
|
|
|
* generation being slated to essentially a complete rewrite.
|
|
|
|
*
|
1996-06-16 21:57:35 +00:00
|
|
|
* $Id: ftp_strat.c,v 1.17 1996/05/23 11:50:11 jkh Exp $
|
1995-05-27 10:39:04 +00:00
|
|
|
*
|
|
|
|
* Copyright (c) 1995
|
|
|
|
* Jordan Hubbard. All rights reserved.
|
|
|
|
* Copyright (c) 1995
|
|
|
|
* Gary J Palmer. 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
|
1995-05-30 08:29:07 +00:00
|
|
|
* notice, this list of conditions and the following disclaimer,
|
|
|
|
* verbatim and that no modifications are made prior to this
|
1995-05-27 10:39:04 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sysinstall.h"
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include "ftp.h"
|
|
|
|
|
1995-12-07 10:34:59 +00:00
|
|
|
Boolean ftpInitted = FALSE;
|
1995-05-27 10:39:04 +00:00
|
|
|
static FTP_t ftp;
|
1995-06-11 19:33:05 +00:00
|
|
|
extern int FtpPort;
|
|
|
|
|
1995-12-07 10:34:59 +00:00
|
|
|
static char *lastRequest;
|
1995-06-11 19:33:05 +00:00
|
|
|
|
|
|
|
static Boolean
|
1996-03-02 07:31:58 +00:00
|
|
|
get_new_host(Device *dev, Boolean probe)
|
1995-06-11 19:33:05 +00:00
|
|
|
{
|
|
|
|
Boolean i;
|
1995-12-07 10:34:59 +00:00
|
|
|
int j;
|
1995-06-11 19:33:05 +00:00
|
|
|
char *oldTitle = MenuMediaFTP.title;
|
1995-12-07 10:34:59 +00:00
|
|
|
char *cp = variable_get(VAR_FTP_ONERROR);
|
1995-06-11 19:33:05 +00:00
|
|
|
|
1996-03-02 07:31:58 +00:00
|
|
|
if (probe || (cp && strcmp(cp, "reselect")))
|
1995-12-07 10:34:59 +00:00
|
|
|
i = TRUE;
|
|
|
|
else {
|
|
|
|
i = FALSE;
|
|
|
|
msgConfirm("The %s file failed to load from the FTP site you\n"
|
|
|
|
"selected. Please select another one from the FTP menu.", lastRequest ? lastRequest : "requested");
|
|
|
|
MenuMediaFTP.title = "Request failed - please select another site";
|
|
|
|
j = mediaSetFTP(NULL);
|
|
|
|
MenuMediaFTP.title = oldTitle;
|
1996-04-28 03:27:26 +00:00
|
|
|
if (DITEM_STATUS(j) == DITEM_SUCCESS) {
|
1995-12-07 10:34:59 +00:00
|
|
|
/* Bounce the link if necessary */
|
|
|
|
if (ftpInitted) {
|
|
|
|
msgDebug("Bouncing FTP connection before reselecting new host.\n");
|
|
|
|
dev->shutdown(dev);
|
|
|
|
i = dev->init(dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
msgDebug("User elected not to reselect, shutting down open connection.\n");
|
|
|
|
dev->shutdown(dev);
|
|
|
|
}
|
1995-06-11 19:33:05 +00:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
1995-12-07 10:34:59 +00:00
|
|
|
/* Should we throw in the towel? */
|
|
|
|
static Boolean
|
|
|
|
ftpShouldAbort(Device *dev, int retries)
|
|
|
|
{
|
|
|
|
char *cp, *cp2;
|
|
|
|
int maxretries, rval = FALSE;
|
|
|
|
|
|
|
|
cp = variable_get(VAR_FTP_ONERROR);
|
|
|
|
cp2 = variable_get(VAR_FTP_RETRIES);
|
|
|
|
maxretries = atoi(cp2);
|
|
|
|
if (retries > maxretries || (cp && !strcmp(cp, "abort"))) {
|
|
|
|
rval = TRUE;
|
|
|
|
if (isDebug())
|
|
|
|
msgDebug("Aborting FTP connection.\n");
|
|
|
|
dev->shutdown(dev);
|
1996-04-28 03:27:26 +00:00
|
|
|
(void)dev->init(dev);
|
1995-12-07 10:34:59 +00:00
|
|
|
}
|
|
|
|
return rval;
|
|
|
|
}
|
1995-05-27 10:39:04 +00:00
|
|
|
|
|
|
|
Boolean
|
|
|
|
mediaInitFTP(Device *dev)
|
|
|
|
{
|
1995-12-07 10:34:59 +00:00
|
|
|
int i, retries;
|
1996-06-16 21:57:35 +00:00
|
|
|
char *cp, *rel, *hostname, *dir;
|
|
|
|
char *user, *login_name, password[80];
|
1995-05-27 10:39:04 +00:00
|
|
|
Device *netDevice = (Device *)dev->private;
|
|
|
|
|
|
|
|
if (ftpInitted)
|
|
|
|
return TRUE;
|
|
|
|
|
1995-12-07 10:34:59 +00:00
|
|
|
if (isDebug())
|
|
|
|
msgDebug("Init routine for FTP called. Net device is %x\n", netDevice);
|
1996-04-28 03:27:26 +00:00
|
|
|
if (!netDevice->init(netDevice)) {
|
|
|
|
if (isDebug())
|
|
|
|
msgDebug("InitFTP: Net device init returns FALSE\n");
|
1995-06-11 19:33:05 +00:00
|
|
|
return FALSE;
|
1996-04-28 03:27:26 +00:00
|
|
|
}
|
1995-05-27 10:39:04 +00:00
|
|
|
|
1995-12-07 10:34:59 +00:00
|
|
|
if (!ftp && (ftp = FtpInit()) == NULL) {
|
1995-05-27 10:39:04 +00:00
|
|
|
msgConfirm("FTP initialisation failed!");
|
1995-12-07 10:34:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
1996-04-28 03:27:26 +00:00
|
|
|
|
1995-12-07 10:34:59 +00:00
|
|
|
cp = variable_get(VAR_FTP_PATH);
|
|
|
|
if (!cp) {
|
|
|
|
msgConfirm("%s is not set!", VAR_FTP_PATH);
|
|
|
|
return FALSE;
|
1995-05-27 10:39:04 +00:00
|
|
|
}
|
1996-04-28 03:27:26 +00:00
|
|
|
|
1995-12-07 10:34:59 +00:00
|
|
|
user = variable_get(VAR_FTP_USER);
|
1995-06-11 19:33:05 +00:00
|
|
|
if (!user || !*user) {
|
1995-12-07 10:34:59 +00:00
|
|
|
snprintf(password, BUFSIZ, "installer@%s", variable_get(VAR_HOSTNAME));
|
1995-06-11 19:33:05 +00:00
|
|
|
login_name = "anonymous";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
login_name = user;
|
1995-12-07 10:34:59 +00:00
|
|
|
strcpy(password, variable_get(VAR_FTP_PASS) ? variable_get(VAR_FTP_PASS) : login_name);
|
1995-06-11 19:33:05 +00:00
|
|
|
}
|
1995-12-07 10:34:59 +00:00
|
|
|
retries = 0;
|
1996-06-16 21:57:35 +00:00
|
|
|
hostname = variable_get(VAR_FTP_HOST);
|
|
|
|
dir = variable_get(VAR_FTP_DIR);
|
|
|
|
if (!hostname || !dir)
|
|
|
|
msgFatal("Missing FTP host or directory specification - something's wrong!");
|
1995-12-07 10:34:59 +00:00
|
|
|
|
1995-06-11 19:33:05 +00:00
|
|
|
retry:
|
|
|
|
msgNotify("Logging in as %s..", login_name);
|
1995-12-07 10:34:59 +00:00
|
|
|
if (FtpOpen(ftp, hostname, login_name, password) != 0) {
|
|
|
|
if (variable_get(VAR_NO_CONFIRM))
|
|
|
|
msgNotify("Couldn't open FTP connection to %s", hostname);
|
1996-04-23 01:29:35 +00:00
|
|
|
else
|
1995-12-07 10:34:59 +00:00
|
|
|
msgConfirm("Couldn't open FTP connection to %s", hostname);
|
|
|
|
if (ftpShouldAbort(dev, ++retries) || !get_new_host(dev, FALSE))
|
|
|
|
return FALSE;
|
1995-06-11 19:33:05 +00:00
|
|
|
goto retry;
|
1995-05-27 10:39:04 +00:00
|
|
|
}
|
|
|
|
|
1995-12-07 10:34:59 +00:00
|
|
|
FtpPassive(ftp, !strcmp(variable_get(VAR_FTP_STATE), "passive"));
|
1995-05-27 10:39:04 +00:00
|
|
|
FtpBinary(ftp, 1);
|
|
|
|
if (dir && *dir != '\0') {
|
1996-03-21 09:30:18 +00:00
|
|
|
msgDebug("Attempt to chdir to distribution in %s\n", dir);
|
1995-12-07 10:34:59 +00:00
|
|
|
if ((i = FtpChdir(ftp, dir)) != 0) {
|
|
|
|
if (i == -2 || ftpShouldAbort(dev, ++retries))
|
|
|
|
goto punt;
|
|
|
|
else if (get_new_host(dev, FALSE))
|
|
|
|
retries = 0;
|
1995-06-11 19:33:05 +00:00
|
|
|
goto retry;
|
1995-12-07 10:34:59 +00:00
|
|
|
}
|
1995-06-11 19:33:05 +00:00
|
|
|
}
|
1995-12-07 10:34:59 +00:00
|
|
|
|
1996-05-16 11:47:46 +00:00
|
|
|
/* Give it a shot - can't hurt to try and zoom in if we can, unless the release is set to __RELEASE which signifies that it's not set */
|
1996-05-23 11:50:11 +00:00
|
|
|
rel = variable_get(VAR_RELNAME);
|
1996-05-16 11:47:46 +00:00
|
|
|
if (strcmp(rel, "__RELEASE"))
|
|
|
|
i = FtpChdir(ftp, rel);
|
|
|
|
else
|
|
|
|
i = 0;
|
1996-05-05 21:54:23 +00:00
|
|
|
if (i == -2)
|
1995-12-07 10:34:59 +00:00
|
|
|
goto punt;
|
1996-05-05 21:54:23 +00:00
|
|
|
else if (i == -1)
|
|
|
|
msgConfirm("Warning: Can't CD to `%s' distribution on this\n"
|
|
|
|
"FTP server. You may need to visit the Options menu\n"
|
|
|
|
"to set the release name explicitly if this FTP server\n"
|
|
|
|
"isn't exporting a CD (or some other custom release) at\n"
|
1996-05-16 11:47:46 +00:00
|
|
|
"the top level as a release tree.", rel);
|
1995-12-07 10:34:59 +00:00
|
|
|
|
1995-05-29 11:01:42 +00:00
|
|
|
if (isDebug())
|
1995-12-07 10:34:59 +00:00
|
|
|
msgDebug("mediaInitFTP was successful (logged in and chdir'd)\n");
|
1995-05-27 10:39:04 +00:00
|
|
|
ftpInitted = TRUE;
|
|
|
|
return TRUE;
|
1995-06-11 19:33:05 +00:00
|
|
|
|
|
|
|
punt:
|
1995-12-07 10:34:59 +00:00
|
|
|
if (ftp != NULL) {
|
|
|
|
FtpClose(ftp);
|
|
|
|
ftp = NULL;
|
|
|
|
}
|
1995-06-11 19:33:05 +00:00
|
|
|
return FALSE;
|
1995-05-27 10:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1996-03-02 07:31:58 +00:00
|
|
|
mediaGetFTP(Device *dev, char *file, Boolean probe)
|
1995-05-27 10:39:04 +00:00
|
|
|
{
|
1995-06-11 19:33:05 +00:00
|
|
|
int fd;
|
1995-12-07 10:34:59 +00:00
|
|
|
int nretries;
|
|
|
|
char *fp;
|
|
|
|
char buf[PATH_MAX];
|
1995-06-11 19:33:05 +00:00
|
|
|
|
1995-12-07 10:34:59 +00:00
|
|
|
fp = file;
|
|
|
|
nretries = 0;
|
1995-06-11 19:33:05 +00:00
|
|
|
|
1995-12-07 10:34:59 +00:00
|
|
|
lastRequest = file;
|
|
|
|
while ((fd = FtpGet(ftp, fp)) < 0) {
|
1995-06-11 19:33:05 +00:00
|
|
|
/* If a hard fail, try to "bounce" the ftp server to clear it */
|
1995-12-07 10:34:59 +00:00
|
|
|
if (fd == -2 && ++nretries < atoi(variable_get(VAR_FTP_RETRIES))) {
|
|
|
|
dev->shutdown(dev);
|
|
|
|
/* If we can't re-initialize, just forget it */
|
|
|
|
if (!dev->init(dev))
|
1995-06-11 19:33:05 +00:00
|
|
|
return -2;
|
|
|
|
}
|
1996-03-02 07:31:58 +00:00
|
|
|
else if (probe || ftpShouldAbort(dev, ++nretries))
|
1995-12-07 10:34:59 +00:00
|
|
|
return -1;
|
|
|
|
else {
|
|
|
|
/* Try some alternatives */
|
|
|
|
switch (nretries) {
|
|
|
|
case 1:
|
|
|
|
sprintf(buf, "dists/%s", file);
|
|
|
|
fp = buf;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
sprintf(buf, "%s/%s", variable_get(VAR_RELNAME), file);
|
|
|
|
fp = buf;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
sprintf(buf, "%s/dists/%s", variable_get(VAR_RELNAME), file);
|
|
|
|
fp = buf;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
fp = file;
|
1996-03-02 07:31:58 +00:00
|
|
|
if (get_new_host(dev, probe)) {
|
1995-12-07 10:34:59 +00:00
|
|
|
nretries = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
1995-06-11 19:33:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fd;
|
1995-05-27 10:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Boolean
|
|
|
|
mediaCloseFTP(Device *dev, int fd)
|
|
|
|
{
|
1995-12-07 10:34:59 +00:00
|
|
|
if (isDebug())
|
|
|
|
msgDebug("FTP Close called\n");
|
|
|
|
if (ftp)
|
|
|
|
FtpEOF(ftp);
|
1995-06-11 19:33:05 +00:00
|
|
|
return FALSE;
|
1995-05-27 10:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mediaShutdownFTP(Device *dev)
|
|
|
|
{
|
1995-12-07 10:34:59 +00:00
|
|
|
/* Device *netdev = (Device *)dev->private; */
|
1995-05-27 10:39:04 +00:00
|
|
|
|
|
|
|
if (!ftpInitted)
|
|
|
|
return;
|
|
|
|
|
1995-12-07 10:34:59 +00:00
|
|
|
if (isDebug())
|
|
|
|
msgDebug("FTP shutdown called. FTP = %x\n", ftp);
|
1995-05-27 10:39:04 +00:00
|
|
|
if (ftp != NULL) {
|
|
|
|
FtpClose(ftp);
|
|
|
|
ftp = NULL;
|
|
|
|
}
|
1995-12-07 10:34:59 +00:00
|
|
|
/* (*netdev->shutdown)(netdev); */
|
1995-05-27 10:39:04 +00:00
|
|
|
ftpInitted = FALSE;
|
|
|
|
}
|