1999-12-02 02:42:23 -05:00
|
|
|
|
/* Basic FTP routines.
|
2000-11-01 20:50:03 -05:00
|
|
|
|
Copyright (C) 1995, 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2001-05-27 15:35:15 -04:00
|
|
|
|
This file is part of GNU Wget.
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
2001-05-27 15:35:15 -04:00
|
|
|
|
GNU Wget is free software; you can redistribute it and/or modify
|
1999-12-02 02:42:23 -05:00
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
2001-05-27 15:35:15 -04:00
|
|
|
|
GNU Wget is distributed in the hope that it will be useful,
|
1999-12-02 02:42:23 -05:00
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2001-05-27 15:35:15 -04:00
|
|
|
|
along with Wget; if not, write to the Free Software
|
1999-12-02 02:42:23 -05:00
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
|
# include <string.h>
|
|
|
|
|
#else
|
|
|
|
|
# include <strings.h>
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
|
# include <unistd.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
|
|
#ifdef WINDOWS
|
|
|
|
|
# include <winsock.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "wget.h"
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
#include "rbuf.h"
|
|
|
|
|
#include "connect.h"
|
|
|
|
|
#include "host.h"
|
2000-11-21 11:48:39 -05:00
|
|
|
|
#include "ftp.h"
|
1999-12-02 02:42:23 -05:00
|
|
|
|
|
|
|
|
|
char ftp_last_respline[128];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Get the response of FTP server and allocate enough room to handle
|
|
|
|
|
it. <CR> and <LF> characters are stripped from the line, and the
|
|
|
|
|
line is 0-terminated. All the response lines but the last one are
|
|
|
|
|
skipped. The last line is determined as described in RFC959. */
|
|
|
|
|
uerr_t
|
|
|
|
|
ftp_response (struct rbuf *rbuf, char **line)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int bufsize = 40;
|
|
|
|
|
|
|
|
|
|
*line = (char *)xmalloc (bufsize);
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
for (i = 0; 1; i++)
|
2001-02-13 02:44:59 -05:00
|
|
|
|
{
|
|
|
|
|
int res;
|
|
|
|
|
if (i > bufsize - 1)
|
|
|
|
|
*line = (char *)xrealloc (*line, (bufsize <<= 1));
|
|
|
|
|
res = RBUF_READCHAR (rbuf, *line + i);
|
|
|
|
|
/* RES is number of bytes read. */
|
|
|
|
|
if (res == 1)
|
|
|
|
|
{
|
|
|
|
|
if ((*line)[i] == '\n')
|
|
|
|
|
{
|
|
|
|
|
(*line)[i] = '\0';
|
|
|
|
|
/* Get rid of \r. */
|
|
|
|
|
if (i > 0 && (*line)[i - 1] == '\r')
|
|
|
|
|
(*line)[i - 1] = '\0';
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return FTPRERR;
|
|
|
|
|
}
|
1999-12-02 02:42:23 -05:00
|
|
|
|
if (opt.server_response)
|
2001-02-13 02:44:59 -05:00
|
|
|
|
logprintf (LOG_ALWAYS, "%s\n", *line);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
else
|
2001-02-13 02:44:59 -05:00
|
|
|
|
DEBUGP (("%s\n", *line));
|
1999-12-02 02:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
while (!(i >= 3 && ISDIGIT (**line) && ISDIGIT ((*line)[1]) &&
|
2001-02-13 02:44:59 -05:00
|
|
|
|
ISDIGIT ((*line)[2]) && (*line)[3] == ' '));
|
1999-12-02 02:42:23 -05:00
|
|
|
|
strncpy (ftp_last_respline, *line, sizeof (ftp_last_respline));
|
|
|
|
|
ftp_last_respline[sizeof (ftp_last_respline) - 1] = '\0';
|
|
|
|
|
return FTPOK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Returns the malloc-ed FTP request, ending with <CR><LF>, printing
|
|
|
|
|
it if printing is required. If VALUE is NULL, just use
|
|
|
|
|
command<CR><LF>. */
|
|
|
|
|
static char *
|
|
|
|
|
ftp_request (const char *command, const char *value)
|
|
|
|
|
{
|
|
|
|
|
char *res = (char *)xmalloc (strlen (command)
|
2001-02-13 02:44:59 -05:00
|
|
|
|
+ (value ? (1 + strlen (value)) : 0)
|
|
|
|
|
+ 2 + 1);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
sprintf (res, "%s%s%s\r\n", command, value ? " " : "", value ? value : "");
|
|
|
|
|
if (opt.server_response)
|
|
|
|
|
{
|
|
|
|
|
/* Hack: don't print out password. */
|
|
|
|
|
if (strncmp (res, "PASS", 4) != 0)
|
2001-02-13 02:44:59 -05:00
|
|
|
|
logprintf (LOG_ALWAYS, "--> %s\n", res);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
else
|
2001-02-13 02:44:59 -05:00
|
|
|
|
logputs (LOG_ALWAYS, "--> PASS Turtle Power!\n");
|
1999-12-02 02:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
DEBUGP (("\n--> %s\n", res));
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef USE_OPIE
|
|
|
|
|
const char *calculate_skey_response PARAMS ((int, const char *, const char *));
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Sends the USER and PASS commands to the server, to control
|
|
|
|
|
connection socket csock. */
|
|
|
|
|
uerr_t
|
|
|
|
|
ftp_login (struct rbuf *rbuf, const char *acc, const char *pass)
|
|
|
|
|
{
|
|
|
|
|
uerr_t err;
|
|
|
|
|
char *request, *respline;
|
|
|
|
|
int nwritten;
|
|
|
|
|
|
|
|
|
|
/* Get greeting. */
|
|
|
|
|
err = ftp_response (rbuf, &respline);
|
|
|
|
|
if (err != FTPOK)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
if (*respline != '2')
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return FTPSRVERR;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* Send USER username. */
|
|
|
|
|
request = ftp_request ("USER", acc);
|
|
|
|
|
nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
|
|
|
|
|
if (nwritten < 0)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return WRITEFAILED;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* Get appropriate response. */
|
|
|
|
|
err = ftp_response (rbuf, &respline);
|
|
|
|
|
if (err != FTPOK)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
/* An unprobable possibility of logging without a password. */
|
|
|
|
|
if (*respline == '2')
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return FTPOK;
|
|
|
|
|
}
|
|
|
|
|
/* Else, only response 3 is appropriate. */
|
|
|
|
|
if (*respline != '3')
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return FTPLOGREFUSED;
|
|
|
|
|
}
|
|
|
|
|
#ifdef USE_OPIE
|
|
|
|
|
{
|
|
|
|
|
static const char *skey_head[] = {
|
|
|
|
|
"331 s/key ",
|
|
|
|
|
"331 opiekey "
|
|
|
|
|
};
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE (skey_head); i++)
|
|
|
|
|
{
|
2001-02-13 02:44:59 -05:00
|
|
|
|
if (strncasecmp (skey_head[i], respline, strlen (skey_head[i])) == 0)
|
|
|
|
|
break;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
if (i < ARRAY_SIZE (skey_head))
|
|
|
|
|
{
|
2001-02-13 02:44:59 -05:00
|
|
|
|
const char *cp;
|
|
|
|
|
int skey_sequence = 0;
|
|
|
|
|
|
|
|
|
|
for (cp = respline + strlen (skey_head[i]);
|
|
|
|
|
'0' <= *cp && *cp <= '9';
|
|
|
|
|
cp++)
|
|
|
|
|
{
|
|
|
|
|
skey_sequence = skey_sequence * 10 + *cp - '0';
|
|
|
|
|
}
|
|
|
|
|
if (*cp == ' ')
|
|
|
|
|
cp++;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bad:
|
|
|
|
|
xfree (respline);
|
|
|
|
|
return FTPLOGREFUSED;
|
|
|
|
|
}
|
|
|
|
|
if ((cp = calculate_skey_response (skey_sequence, cp, pass)) == 0)
|
|
|
|
|
goto bad;
|
|
|
|
|
pass = cp;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif /* USE_OPIE */
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* Send PASS password. */
|
|
|
|
|
request = ftp_request ("PASS", pass);
|
|
|
|
|
nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
|
|
|
|
|
if (nwritten < 0)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return WRITEFAILED;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* Get appropriate response. */
|
|
|
|
|
err = ftp_response (rbuf, &respline);
|
|
|
|
|
if (err != FTPOK)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
if (*respline != '2')
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return FTPLOGINC;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* All OK. */
|
|
|
|
|
return FTPOK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Bind a port and send the appropriate PORT command to the FTP
|
|
|
|
|
server. Use acceptport after RETR, to get the socket of data
|
|
|
|
|
connection. */
|
|
|
|
|
uerr_t
|
|
|
|
|
ftp_port (struct rbuf *rbuf)
|
|
|
|
|
{
|
|
|
|
|
uerr_t err;
|
|
|
|
|
char *request, *respline, *bytes;
|
|
|
|
|
unsigned char *in_addr;
|
|
|
|
|
int nwritten;
|
|
|
|
|
unsigned short port;
|
|
|
|
|
|
|
|
|
|
/* Setting port to 0 lets the system choose a free port. */
|
|
|
|
|
port = 0;
|
|
|
|
|
/* Bind the port. */
|
|
|
|
|
err = bindport (&port);
|
|
|
|
|
if (err != BINDOK)
|
|
|
|
|
return err;
|
|
|
|
|
/* Get the address of this side of the connection. */
|
|
|
|
|
if (!(in_addr = conaddr (RBUF_FD (rbuf))))
|
|
|
|
|
return HOSTERR;
|
|
|
|
|
/* Construct the argument of PORT (of the form a,b,c,d,e,f). */
|
|
|
|
|
bytes = (char *)alloca (6 * 4 + 1);
|
|
|
|
|
sprintf (bytes, "%d,%d,%d,%d,%d,%d", in_addr[0], in_addr[1],
|
2001-02-13 02:44:59 -05:00
|
|
|
|
in_addr[2], in_addr[3], (unsigned) (port & 0xff00) >> 8,
|
|
|
|
|
port & 0xff);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* Send PORT request. */
|
|
|
|
|
request = ftp_request ("PORT", bytes);
|
|
|
|
|
nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
|
|
|
|
|
if (nwritten < 0)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return WRITEFAILED;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* Get appropriate response. */
|
|
|
|
|
err = ftp_response (rbuf, &respline);
|
|
|
|
|
if (err != FTPOK)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
if (*respline != '2')
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return FTPPORTERR;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return FTPOK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Similar to ftp_port, but uses `PASV' to initiate the passive FTP
|
|
|
|
|
transfer. Reads the response from server and parses it. Reads the
|
|
|
|
|
host and port addresses and returns them. */
|
|
|
|
|
uerr_t
|
|
|
|
|
ftp_pasv (struct rbuf *rbuf, unsigned char *addr)
|
|
|
|
|
{
|
|
|
|
|
char *request, *respline, *s;
|
|
|
|
|
int nwritten, i;
|
|
|
|
|
uerr_t err;
|
|
|
|
|
|
|
|
|
|
/* Form the request. */
|
|
|
|
|
request = ftp_request ("PASV", NULL);
|
|
|
|
|
/* And send it. */
|
|
|
|
|
nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
|
|
|
|
|
if (nwritten < 0)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return WRITEFAILED;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* Get the server response. */
|
|
|
|
|
err = ftp_response (rbuf, &respline);
|
|
|
|
|
if (err != FTPOK)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
if (*respline != '2')
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return FTPNOPASV;
|
|
|
|
|
}
|
|
|
|
|
/* Parse the request. */
|
|
|
|
|
s = respline;
|
|
|
|
|
for (s += 4; *s && !ISDIGIT (*s); s++);
|
|
|
|
|
if (!*s)
|
|
|
|
|
return FTPINVPASV;
|
|
|
|
|
for (i = 0; i < 6; i++)
|
|
|
|
|
{
|
|
|
|
|
addr[i] = 0;
|
|
|
|
|
for (; ISDIGIT (*s); s++)
|
2001-02-13 02:44:59 -05:00
|
|
|
|
addr[i] = (*s - '0') + 10 * addr[i];
|
1999-12-02 02:42:23 -05:00
|
|
|
|
if (*s == ',')
|
2001-02-13 02:44:59 -05:00
|
|
|
|
s++;
|
1999-12-02 02:42:23 -05:00
|
|
|
|
else if (i < 5)
|
2001-02-13 02:44:59 -05:00
|
|
|
|
{
|
|
|
|
|
/* When on the last number, anything can be a terminator. */
|
|
|
|
|
xfree (respline);
|
|
|
|
|
return FTPINVPASV;
|
|
|
|
|
}
|
1999-12-02 02:42:23 -05:00
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return FTPOK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Sends the TYPE request to the server. */
|
|
|
|
|
uerr_t
|
|
|
|
|
ftp_type (struct rbuf *rbuf, int type)
|
|
|
|
|
{
|
|
|
|
|
char *request, *respline;
|
|
|
|
|
int nwritten;
|
|
|
|
|
uerr_t err;
|
|
|
|
|
char stype[2];
|
|
|
|
|
|
|
|
|
|
/* Construct argument. */
|
|
|
|
|
stype[0] = type;
|
|
|
|
|
stype[1] = 0;
|
|
|
|
|
/* Send TYPE request. */
|
|
|
|
|
request = ftp_request ("TYPE", stype);
|
|
|
|
|
nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
|
|
|
|
|
if (nwritten < 0)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return WRITEFAILED;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* Get appropriate response. */
|
|
|
|
|
err = ftp_response (rbuf, &respline);
|
|
|
|
|
if (err != FTPOK)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
if (*respline != '2')
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return FTPUNKNOWNTYPE;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* All OK. */
|
|
|
|
|
return FTPOK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Changes the working directory by issuing a CWD command to the
|
|
|
|
|
server. */
|
|
|
|
|
uerr_t
|
|
|
|
|
ftp_cwd (struct rbuf *rbuf, const char *dir)
|
|
|
|
|
{
|
|
|
|
|
char *request, *respline;
|
|
|
|
|
int nwritten;
|
|
|
|
|
uerr_t err;
|
|
|
|
|
|
|
|
|
|
/* Send CWD request. */
|
|
|
|
|
request = ftp_request ("CWD", dir);
|
|
|
|
|
nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
|
|
|
|
|
if (nwritten < 0)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return WRITEFAILED;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* Get appropriate response. */
|
|
|
|
|
err = ftp_response (rbuf, &respline);
|
|
|
|
|
if (err != FTPOK)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
if (*respline == '5')
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return FTPNSFOD;
|
|
|
|
|
}
|
|
|
|
|
if (*respline != '2')
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return FTPRERR;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* All OK. */
|
|
|
|
|
return FTPOK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Sends REST command to the FTP server. */
|
|
|
|
|
uerr_t
|
|
|
|
|
ftp_rest (struct rbuf *rbuf, long offset)
|
|
|
|
|
{
|
|
|
|
|
char *request, *respline;
|
|
|
|
|
int nwritten;
|
|
|
|
|
uerr_t err;
|
|
|
|
|
static char numbuf[20]; /* Buffer for the number */
|
|
|
|
|
|
|
|
|
|
long_to_string (numbuf, offset);
|
|
|
|
|
request = ftp_request ("REST", numbuf);
|
|
|
|
|
nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
|
|
|
|
|
if (nwritten < 0)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return WRITEFAILED;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* Get appropriate response. */
|
|
|
|
|
err = ftp_response (rbuf, &respline);
|
|
|
|
|
if (err != FTPOK)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
if (*respline != '3')
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return FTPRESTFAIL;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* All OK. */
|
|
|
|
|
return FTPOK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Sends RETR command to the FTP server. */
|
|
|
|
|
uerr_t
|
|
|
|
|
ftp_retr (struct rbuf *rbuf, const char *file)
|
|
|
|
|
{
|
|
|
|
|
char *request, *respline;
|
|
|
|
|
int nwritten;
|
|
|
|
|
uerr_t err;
|
|
|
|
|
|
|
|
|
|
/* Send RETR request. */
|
|
|
|
|
request = ftp_request ("RETR", file);
|
|
|
|
|
nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
|
|
|
|
|
if (nwritten < 0)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return WRITEFAILED;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* Get appropriate response. */
|
|
|
|
|
err = ftp_response (rbuf, &respline);
|
|
|
|
|
if (err != FTPOK)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
if (*respline == '5')
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return FTPNSFOD;
|
|
|
|
|
}
|
|
|
|
|
if (*respline != '1')
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return FTPRERR;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* All OK. */
|
|
|
|
|
return FTPOK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Sends the LIST command to the server. If FILE is NULL, send just
|
|
|
|
|
`LIST' (no space). */
|
|
|
|
|
uerr_t
|
|
|
|
|
ftp_list (struct rbuf *rbuf, const char *file)
|
|
|
|
|
{
|
|
|
|
|
char *request, *respline;
|
|
|
|
|
int nwritten;
|
|
|
|
|
uerr_t err;
|
|
|
|
|
|
|
|
|
|
/* Send LIST request. */
|
|
|
|
|
request = ftp_request ("LIST", file);
|
|
|
|
|
nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
|
|
|
|
|
if (nwritten < 0)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return WRITEFAILED;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* Get appropriate respone. */
|
|
|
|
|
err = ftp_response (rbuf, &respline);
|
|
|
|
|
if (err != FTPOK)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
if (*respline == '5')
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return FTPNSFOD;
|
|
|
|
|
}
|
|
|
|
|
if (*respline != '1')
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
return FTPRERR;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
1999-12-02 02:42:23 -05:00
|
|
|
|
/* All OK. */
|
|
|
|
|
return FTPOK;
|
|
|
|
|
}
|
2000-11-21 11:48:39 -05:00
|
|
|
|
|
|
|
|
|
/* Sends the SYST command to the server. */
|
|
|
|
|
uerr_t
|
2000-12-05 17:29:47 -05:00
|
|
|
|
ftp_syst (struct rbuf *rbuf, enum stype *server_type)
|
2000-11-21 11:48:39 -05:00
|
|
|
|
{
|
|
|
|
|
char *request, *respline;
|
|
|
|
|
int nwritten;
|
|
|
|
|
uerr_t err;
|
|
|
|
|
|
|
|
|
|
/* Send SYST request. */
|
|
|
|
|
request = ftp_request ("SYST", NULL);
|
|
|
|
|
nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
|
|
|
|
|
if (nwritten < 0)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
2000-11-21 11:48:39 -05:00
|
|
|
|
return WRITEFAILED;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
2000-11-21 11:48:39 -05:00
|
|
|
|
/* Get appropriate response. */
|
|
|
|
|
err = ftp_response (rbuf, &respline);
|
|
|
|
|
if (err != FTPOK)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
2000-11-21 11:48:39 -05:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
if (*respline == '5')
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
2000-11-21 11:48:39 -05:00
|
|
|
|
return FTPSRVERR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Skip the number (215, but 200 (!!!) in case of VMS) */
|
|
|
|
|
strtok (respline, " ");
|
|
|
|
|
|
|
|
|
|
/* Which system type has been reported (we are interested just in the
|
|
|
|
|
first word of the server response)? */
|
|
|
|
|
request = strtok (NULL, " ");
|
|
|
|
|
|
|
|
|
|
if (!strcasecmp (request, "VMS"))
|
2000-12-05 17:29:47 -05:00
|
|
|
|
*server_type = ST_VMS;
|
2000-11-21 11:48:39 -05:00
|
|
|
|
else
|
|
|
|
|
if (!strcasecmp (request, "UNIX"))
|
2000-12-05 17:29:47 -05:00
|
|
|
|
*server_type = ST_UNIX;
|
2000-11-21 11:48:39 -05:00
|
|
|
|
else
|
2000-12-05 17:29:47 -05:00
|
|
|
|
if (!strcasecmp (request, "WINDOWS_NT"))
|
|
|
|
|
*server_type = ST_WINNT;
|
|
|
|
|
else
|
2001-02-13 02:44:59 -05:00
|
|
|
|
if (!strcasecmp (request, "MACOS"))
|
|
|
|
|
*server_type = ST_MACOS;
|
|
|
|
|
else
|
|
|
|
|
*server_type = ST_OTHER;
|
2000-11-21 11:48:39 -05:00
|
|
|
|
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
2000-11-21 11:48:39 -05:00
|
|
|
|
/* All OK. */
|
|
|
|
|
return FTPOK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Sends the PWD command to the server. */
|
|
|
|
|
uerr_t
|
|
|
|
|
ftp_pwd (struct rbuf *rbuf, char **pwd)
|
|
|
|
|
{
|
|
|
|
|
char *request, *respline;
|
|
|
|
|
int nwritten;
|
|
|
|
|
uerr_t err;
|
|
|
|
|
|
|
|
|
|
/* Send PWD request. */
|
|
|
|
|
request = ftp_request ("PWD", NULL);
|
|
|
|
|
nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
|
|
|
|
|
if (nwritten < 0)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
2000-11-21 11:48:39 -05:00
|
|
|
|
return WRITEFAILED;
|
|
|
|
|
}
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (request);
|
2000-11-21 11:48:39 -05:00
|
|
|
|
/* Get appropriate response. */
|
|
|
|
|
err = ftp_response (rbuf, &respline);
|
|
|
|
|
if (err != FTPOK)
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
2000-11-21 11:48:39 -05:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
if (*respline == '5')
|
|
|
|
|
{
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
2000-11-21 11:48:39 -05:00
|
|
|
|
return FTPSRVERR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Skip the number (257), leading citation mark, trailing citation mark
|
|
|
|
|
and everything following it. */
|
|
|
|
|
strtok (respline, "\"");
|
|
|
|
|
request = strtok (NULL, "\"");
|
|
|
|
|
|
2000-11-22 11:58:28 -05:00
|
|
|
|
/* Has the `pwd' been already allocated? Free! */
|
|
|
|
|
FREE_MAYBE (*pwd);
|
2000-11-21 11:48:39 -05:00
|
|
|
|
|
|
|
|
|
*pwd = xstrdup (request);
|
|
|
|
|
|
2000-11-22 11:58:28 -05:00
|
|
|
|
xfree (respline);
|
2000-11-21 11:48:39 -05:00
|
|
|
|
/* All OK. */
|
|
|
|
|
return FTPOK;
|
|
|
|
|
}
|