1995-05-24 09:00:58 +00:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
|
|
|
* <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
|
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*
|
1996-08-01 12:35:51 +00:00
|
|
|
* $Id: ftp.c,v 1.17 1996/07/08 10:08:00 jkh Exp $
|
1995-05-24 09:00:58 +00:00
|
|
|
*
|
1995-06-11 19:33:05 +00:00
|
|
|
* Return values have been sanitized:
|
|
|
|
* -1 error, but you (still) have a session.
|
|
|
|
* -2 error, your session is dead.
|
|
|
|
*
|
1995-05-24 09:00:58 +00:00
|
|
|
*/
|
|
|
|
|
1995-05-23 02:41:18 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
1996-08-01 12:35:51 +00:00
|
|
|
#include <signal.h>
|
1995-05-23 02:41:18 +00:00
|
|
|
#include <errno.h>
|
1995-05-24 09:00:58 +00:00
|
|
|
#include <ctype.h>
|
1995-05-23 02:41:18 +00:00
|
|
|
#include "ftp.h"
|
1995-05-24 11:19:11 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
1995-06-11 19:33:05 +00:00
|
|
|
/* Handy global for us to stick the port # */
|
|
|
|
int FtpPort;
|
|
|
|
|
1995-12-07 10:34:59 +00:00
|
|
|
/* How to see by a given code whether or not the connection has timed out */
|
|
|
|
#define FTP_TIMEOUT(code) (code == 421)
|
|
|
|
|
1995-05-24 09:00:58 +00:00
|
|
|
#ifndef STANDALONE_FTP
|
1995-05-23 02:41:18 +00:00
|
|
|
#include "sysinstall.h"
|
1995-05-24 09:00:58 +00:00
|
|
|
#endif /*STANDALONE_FTP*/
|
1995-05-23 02:41:18 +00:00
|
|
|
|
1996-08-01 12:35:51 +00:00
|
|
|
static int sigpipe_caught = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
catch_pipe(int sig)
|
|
|
|
{
|
|
|
|
sigpipe_caught = TRUE;
|
|
|
|
}
|
|
|
|
|
1995-05-23 02:41:18 +00:00
|
|
|
static void
|
|
|
|
debug(FTP_t ftp, const char *fmt, ...)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
char p[BUFSIZ];
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
1995-05-25 06:15:38 +00:00
|
|
|
#ifdef STANDALONE_FTP
|
|
|
|
strcpy(p,"LIBFTP: ");
|
1996-04-28 20:54:11 +00:00
|
|
|
(void) vsnprintf(p + strlen(p), sizeof p - strlen(p), fmt, ap);
|
1995-05-24 09:00:58 +00:00
|
|
|
va_end(ap);
|
1996-04-28 20:54:11 +00:00
|
|
|
write(ftp->fd_debug, p, strlen(p));
|
1995-05-23 02:41:18 +00:00
|
|
|
#else
|
1995-06-11 19:33:05 +00:00
|
|
|
if (isDebug()) {
|
|
|
|
(void) vsnprintf(p, sizeof p - strlen(p), fmt, ap);
|
1995-05-29 11:01:42 +00:00
|
|
|
msgDebug(p);
|
1995-06-11 19:33:05 +00:00
|
|
|
}
|
1995-05-23 02:41:18 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
writes(int fd, char *s)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
int i = strlen(s);
|
1996-08-01 12:35:51 +00:00
|
|
|
|
|
|
|
signal(SIGPIPE, catch_pipe);
|
|
|
|
if (i != write(fd, s, i) || sigpipe_caught) {
|
|
|
|
if (sigpipe_caught)
|
|
|
|
msgDebug("sigpipe caught during write - connection invalid\n");
|
|
|
|
sigpipe_caught = FALSE;
|
1996-07-08 10:08:22 +00:00
|
|
|
return IO_ERROR;
|
1996-08-01 12:35:51 +00:00
|
|
|
}
|
1995-05-24 09:00:58 +00:00
|
|
|
return 0;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
1995-06-11 19:33:05 +00:00
|
|
|
static __inline char*
|
1995-05-23 02:41:18 +00:00
|
|
|
get_a_line(FTP_t ftp)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
static char buf[BUFSIZ];
|
|
|
|
int i,j;
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1996-08-01 12:35:51 +00:00
|
|
|
signal(SIGPIPE, catch_pipe);
|
|
|
|
for(i = 0; i < BUFSIZ;) {
|
1996-04-28 20:54:11 +00:00
|
|
|
j = read(ftp->fd_ctrl, buf+i, 1);
|
1996-08-01 12:35:51 +00:00
|
|
|
if (j != 1 || sigpipe_caught) {
|
|
|
|
if (sigpipe_caught)
|
|
|
|
msgDebug("sigpipe caught during read - connection invalid\n");
|
|
|
|
sigpipe_caught = FALSE;
|
1995-05-24 09:00:58 +00:00
|
|
|
return 0;
|
1996-08-01 12:35:51 +00:00
|
|
|
}
|
1995-05-24 09:00:58 +00:00
|
|
|
if (buf[i] == '\r' || buf[i] == '\n') {
|
|
|
|
if (!i)
|
|
|
|
continue;
|
|
|
|
buf[i] = '\0';
|
1996-04-28 20:54:11 +00:00
|
|
|
debug(ftp, "received <%s>\n", buf);
|
1995-05-24 09:00:58 +00:00
|
|
|
return buf;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
1995-05-24 09:00:58 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return buf;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
1995-05-24 09:00:58 +00:00
|
|
|
static int
|
|
|
|
get_a_number(FTP_t ftp, char **q)
|
1995-05-23 02:41:18 +00:00
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
char *p;
|
|
|
|
int i = -1,j;
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1995-05-24 09:00:58 +00:00
|
|
|
while(1) {
|
|
|
|
p = get_a_line(ftp);
|
1995-06-11 19:33:05 +00:00
|
|
|
if (!p)
|
1996-07-08 10:08:22 +00:00
|
|
|
return IO_ERROR;
|
1995-05-24 09:00:58 +00:00
|
|
|
if (!(isdigit(p[0]) && isdigit(p[1]) && isdigit(p[2])))
|
|
|
|
continue;
|
|
|
|
if (i == -1 && p[3] == '-') {
|
1995-06-11 19:33:05 +00:00
|
|
|
i = strtol(p, 0, 0);
|
1995-05-24 09:00:58 +00:00
|
|
|
continue;
|
1995-05-30 08:29:07 +00:00
|
|
|
}
|
|
|
|
if (p[3] != ' ' && p[3] != '\t')
|
1995-05-24 09:00:58 +00:00
|
|
|
continue;
|
1995-06-11 19:33:05 +00:00
|
|
|
j = strtol(p, 0, 0);
|
1995-05-24 09:00:58 +00:00
|
|
|
if (i == -1) {
|
|
|
|
if (q) *q = p+4;
|
|
|
|
return j;
|
|
|
|
} else if (j == i) {
|
|
|
|
if (q) *q = p+4;
|
|
|
|
return j;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
1995-05-24 09:00:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1995-06-11 19:33:05 +00:00
|
|
|
zap(FTP_t ftp)
|
1995-05-24 09:00:58 +00:00
|
|
|
{
|
1995-06-11 19:33:05 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
i = writes(ftp->fd_ctrl,"QUIT\r\n");
|
|
|
|
if (isDebug())
|
|
|
|
msgDebug("Zapping ftp connection on %d returns %d\n", ftp->fd_ctrl, i);
|
1995-05-24 09:00:58 +00:00
|
|
|
close(ftp->fd_ctrl); ftp->fd_ctrl = -1;
|
|
|
|
close(ftp->fd_xfer); ftp->fd_xfer = -1;
|
|
|
|
ftp->state = init;
|
1996-07-08 10:08:22 +00:00
|
|
|
return IO_ERROR;
|
1995-06-11 19:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
botch(FTP_t ftp, char *func, char *state)
|
|
|
|
{
|
|
|
|
debug(ftp, "Botch: %s called outside state %s\n",func,state);
|
1996-07-08 10:08:22 +00:00
|
|
|
return IO_ERROR;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cmd(FTP_t ftp, const char *fmt, ...)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
char p[BUFSIZ];
|
|
|
|
int i;
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1995-05-24 09:00:58 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
(void) vsnprintf(p, sizeof p, fmt, ap);
|
|
|
|
va_end(ap);
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1996-04-28 20:54:11 +00:00
|
|
|
debug(ftp, "send <%s>\n", p);
|
1995-05-24 09:00:58 +00:00
|
|
|
strcat(p,"\r\n");
|
1996-04-28 20:54:11 +00:00
|
|
|
if (writes(ftp->fd_ctrl, p))
|
1996-07-08 10:08:22 +00:00
|
|
|
return IO_ERROR;
|
1996-04-28 20:54:11 +00:00
|
|
|
i = get_a_number(ftp, 0);
|
1995-05-24 09:00:58 +00:00
|
|
|
return i;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FTP_t
|
|
|
|
FtpInit()
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
FTP_t ftp;
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1995-05-24 09:00:58 +00:00
|
|
|
ftp = malloc(sizeof *ftp);
|
|
|
|
if (!ftp)
|
1995-05-23 02:41:18 +00:00
|
|
|
return ftp;
|
1995-05-24 09:00:58 +00:00
|
|
|
memset(ftp, 0, sizeof *ftp);
|
|
|
|
ftp->fd_ctrl = -1;
|
1995-05-24 19:31:26 +00:00
|
|
|
ftp->fd_xfer = -1;
|
1995-05-24 09:00:58 +00:00
|
|
|
ftp->fd_debug = -1;
|
|
|
|
ftp->state = init;
|
|
|
|
return ftp;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
1995-05-24 09:00:58 +00:00
|
|
|
|
|
|
|
#ifdef STANDALONE_FTP
|
1995-05-23 02:41:18 +00:00
|
|
|
void
|
|
|
|
FtpDebug(FTP_t ftp, int i)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
ftp->fd_debug = i;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
#endif
|
1995-05-24 09:00:58 +00:00
|
|
|
|
1995-05-23 02:41:18 +00:00
|
|
|
int
|
|
|
|
FtpOpen(FTP_t ftp, char *host, char *user, char *passwd)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
struct hostent *he = NULL;
|
|
|
|
struct sockaddr_in sin;
|
1995-06-11 19:33:05 +00:00
|
|
|
int s;
|
|
|
|
unsigned long temp;
|
|
|
|
int i;
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1995-05-24 09:00:58 +00:00
|
|
|
if (ftp->state != init)
|
|
|
|
return botch(ftp,"FtpOpen","init");
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1995-05-24 09:00:58 +00:00
|
|
|
if (!user)
|
|
|
|
user = "ftp";
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1995-05-24 09:00:58 +00:00
|
|
|
if (!passwd)
|
|
|
|
passwd = "??@??(FreeBSD:libftp)"; /* XXX */
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1995-05-24 11:19:11 +00:00
|
|
|
debug(ftp, "FtpOpen(ftp, %s, %s, %s)\n", host, user, passwd);
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1995-05-24 09:00:58 +00:00
|
|
|
temp = inet_addr(host);
|
1995-06-11 19:33:05 +00:00
|
|
|
if (temp != INADDR_NONE) {
|
1995-05-24 11:19:11 +00:00
|
|
|
debug(ftp, "Using dotted IP address `%s'\n", host);
|
1995-05-24 09:00:58 +00:00
|
|
|
ftp->addrtype = sin.sin_family = AF_INET;
|
|
|
|
sin.sin_addr.s_addr = temp;
|
1995-06-11 19:33:05 +00:00
|
|
|
}
|
|
|
|
else {
|
1995-05-24 11:19:11 +00:00
|
|
|
debug(ftp, "Trying to resolve `%s'\n", host);
|
1995-05-24 09:00:58 +00:00
|
|
|
he = gethostbyname(host);
|
1995-06-11 19:33:05 +00:00
|
|
|
if (!he) {
|
1995-05-24 11:19:11 +00:00
|
|
|
debug(ftp, "Lookup of `%s' failed!\n", host);
|
1995-06-11 19:33:05 +00:00
|
|
|
return zap(ftp);
|
1995-05-24 01:27:15 +00:00
|
|
|
}
|
1995-05-24 09:00:58 +00:00
|
|
|
ftp->addrtype = sin.sin_family = he->h_addrtype;
|
|
|
|
bcopy(he->h_addr, (char *)&sin.sin_addr, he->h_length);
|
|
|
|
}
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1995-06-11 19:33:05 +00:00
|
|
|
sin.sin_port = htons(FtpPort ? FtpPort : 21);
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1995-05-24 09:00:58 +00:00
|
|
|
if ((s = socket(ftp->addrtype, SOCK_STREAM, 0)) < 0)
|
|
|
|
{
|
1995-05-24 11:19:11 +00:00
|
|
|
debug(ftp, "Socket open failed: %s (%i)\n", strerror(errno), errno);
|
1995-06-11 19:33:05 +00:00
|
|
|
return zap(ftp);
|
1995-05-24 09:00:58 +00:00
|
|
|
}
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1995-05-24 09:00:58 +00:00
|
|
|
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
|
1995-05-24 11:19:11 +00:00
|
|
|
debug(ftp,"Connection failed: %s (%i)\n", strerror(errno), errno);
|
1995-05-24 09:00:58 +00:00
|
|
|
(void)close(s);
|
1995-06-11 19:33:05 +00:00
|
|
|
return zap(ftp);
|
1995-05-24 09:00:58 +00:00
|
|
|
}
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1995-05-24 09:00:58 +00:00
|
|
|
ftp->fd_ctrl = s;
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1995-05-25 06:15:38 +00:00
|
|
|
debug(ftp, "open (%d)\n",get_a_number(ftp,0));
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1996-04-28 20:54:11 +00:00
|
|
|
i = cmd(ftp, "USER %s", user);
|
1995-05-30 08:29:07 +00:00
|
|
|
if (i >= 300 && i < 400)
|
1995-05-25 06:15:38 +00:00
|
|
|
i = cmd(ftp,"PASS %s",passwd);
|
1995-06-11 19:33:05 +00:00
|
|
|
if (i >= 299 || i < 0) {
|
1996-04-28 20:54:11 +00:00
|
|
|
close(ftp->fd_ctrl);
|
|
|
|
ftp->fd_ctrl = -1;
|
1995-06-11 19:33:05 +00:00
|
|
|
return zap(ftp);
|
|
|
|
}
|
1995-05-24 09:00:58 +00:00
|
|
|
ftp->state = isopen;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FtpClose(FTP_t ftp)
|
|
|
|
{
|
1995-06-11 19:33:05 +00:00
|
|
|
if (ftp->state != init)
|
|
|
|
return;
|
|
|
|
|
1995-05-30 08:29:07 +00:00
|
|
|
if (ftp->state != isopen)
|
1995-06-11 19:33:05 +00:00
|
|
|
botch(ftp,"FtpClose","open or init");
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1995-05-27 06:19:59 +00:00
|
|
|
debug(ftp, "FtpClose(ftp)\n");
|
1995-06-11 19:33:05 +00:00
|
|
|
zap(ftp);
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
FtpChdir(FTP_t ftp, char *dir)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
int i;
|
1996-04-28 20:54:11 +00:00
|
|
|
|
1995-05-30 08:29:07 +00:00
|
|
|
if (ftp->state != isopen)
|
1995-05-24 09:00:58 +00:00
|
|
|
return botch(ftp,"FtpChdir","open");
|
1996-04-28 20:54:11 +00:00
|
|
|
i = cmd(ftp, "CWD %s", dir);
|
1995-06-11 19:33:05 +00:00
|
|
|
if (i < 0)
|
|
|
|
return i;
|
|
|
|
else if (i != 250)
|
|
|
|
return -1;
|
1995-05-24 09:00:58 +00:00
|
|
|
return 0;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
FtpGet(FTP_t ftp, char *file)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
int i,s;
|
|
|
|
char *q;
|
1995-06-11 19:33:05 +00:00
|
|
|
unsigned char addr[64];
|
1995-05-24 09:00:58 +00:00
|
|
|
struct sockaddr_in sin;
|
1995-06-11 19:33:05 +00:00
|
|
|
u_long a;
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1996-04-28 20:54:11 +00:00
|
|
|
debug(ftp, "FtpGet(ftp,%s)\n", file);
|
1995-05-30 08:29:07 +00:00
|
|
|
if (ftp->state != isopen)
|
1996-04-28 20:54:11 +00:00
|
|
|
return botch(ftp, "FtpGet", "open");
|
|
|
|
if (ftp->binary) {
|
|
|
|
i = cmd(ftp, "TYPE I");
|
1995-12-07 10:34:59 +00:00
|
|
|
if (i < 0 || FTP_TIMEOUT(i))
|
1995-06-11 19:33:05 +00:00
|
|
|
return zap(ftp);
|
1995-05-24 09:00:58 +00:00
|
|
|
if (i > 299)
|
|
|
|
return -1;
|
|
|
|
}
|
1996-04-28 20:54:11 +00:00
|
|
|
else
|
|
|
|
return -1;
|
1995-06-11 19:33:05 +00:00
|
|
|
|
|
|
|
if ((s = socket(ftp->addrtype, SOCK_STREAM, 0)) < 0)
|
|
|
|
return zap(ftp);
|
|
|
|
|
1995-05-24 11:19:11 +00:00
|
|
|
if (ftp->passive) {
|
1996-04-28 20:54:11 +00:00
|
|
|
debug(ftp, "send <%s>\n", "PASV");
|
|
|
|
if (writes(ftp->fd_ctrl, "PASV\r\n"))
|
1995-06-11 19:33:05 +00:00
|
|
|
return zap(ftp);
|
1996-04-28 20:54:11 +00:00
|
|
|
i = get_a_number(ftp, &q);
|
1995-06-11 19:33:05 +00:00
|
|
|
if (i < 0)
|
|
|
|
return zap(ftp);
|
1995-05-24 09:00:58 +00:00
|
|
|
if (i != 227)
|
1995-06-11 19:33:05 +00:00
|
|
|
return zap(ftp);
|
1995-05-24 09:00:58 +00:00
|
|
|
while (*q && !isdigit(*q))
|
|
|
|
q++;
|
|
|
|
if (!*q)
|
1995-06-11 19:33:05 +00:00
|
|
|
return zap(ftp);
|
1995-05-24 09:00:58 +00:00
|
|
|
q--;
|
1996-04-28 20:54:11 +00:00
|
|
|
for(i = 0; i < 6; i++) {
|
1995-05-24 09:00:58 +00:00
|
|
|
q++;
|
1996-04-28 20:54:11 +00:00
|
|
|
addr[i] = strtol(q, &q, 10);
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1995-05-24 09:00:58 +00:00
|
|
|
sin.sin_family = ftp->addrtype;
|
|
|
|
bcopy(addr, (char *)&sin.sin_addr, 4);
|
1996-04-28 20:54:11 +00:00
|
|
|
bcopy(addr + 4, (char *)&sin.sin_port, 2);
|
1995-05-24 11:19:11 +00:00
|
|
|
debug(ftp, "Opening active socket to %s : %u\n", inet_ntoa(sin.sin_addr), htons(sin.sin_port));
|
|
|
|
|
|
|
|
debug(ftp, "Connecting to %s:%u\n", inet_ntoa(sin.sin_addr), htons(sin.sin_port));
|
1995-05-24 09:00:58 +00:00
|
|
|
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
|
|
|
|
(void)close(s);
|
1995-05-24 11:19:11 +00:00
|
|
|
debug(ftp, "connect: %s (%d)\n", strerror(errno), errno);
|
1995-05-24 09:00:58 +00:00
|
|
|
return -1;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
1995-06-11 19:33:05 +00:00
|
|
|
ftp->fd_xfer = s;
|
1996-04-28 20:54:11 +00:00
|
|
|
i = cmd(ftp,"RETR %s", file);
|
1995-12-07 10:34:59 +00:00
|
|
|
if (i < 0 || FTP_TIMEOUT(i)) {
|
1995-06-11 19:33:05 +00:00
|
|
|
close(s);
|
|
|
|
return zap(ftp);
|
|
|
|
}
|
|
|
|
else if (i > 299) {
|
|
|
|
if (isDebug())
|
|
|
|
msgDebug("FTP: No such file %s, moving on.\n", file);
|
|
|
|
close(s);
|
1995-05-24 09:00:58 +00:00
|
|
|
return -1;
|
1995-06-11 19:33:05 +00:00
|
|
|
}
|
1995-05-24 09:00:58 +00:00
|
|
|
ftp->state = xfer;
|
|
|
|
return s;
|
|
|
|
} else {
|
1995-06-11 19:33:05 +00:00
|
|
|
i = sizeof sin;
|
1996-04-28 20:54:11 +00:00
|
|
|
getsockname(ftp->fd_ctrl, (struct sockaddr *)&sin, &i);
|
1995-06-11 19:33:05 +00:00
|
|
|
sin.sin_port = 0;
|
|
|
|
i = sizeof sin;
|
|
|
|
if (bind(s,(struct sockaddr *)&sin, i) < 0) {
|
1996-04-28 20:54:11 +00:00
|
|
|
close (s);
|
|
|
|
debug(ftp, "bind failed %d\n", errno);
|
|
|
|
return zap(ftp);
|
1995-06-11 19:33:05 +00:00
|
|
|
}
|
1996-04-28 20:54:11 +00:00
|
|
|
getsockname(s, (struct sockaddr *)&sin, &i);
|
|
|
|
if (listen(s, 1) < 0) {
|
|
|
|
close (s);
|
|
|
|
debug(ftp, "listen failed %d\n", errno);
|
|
|
|
return zap(ftp);
|
1995-06-11 19:33:05 +00:00
|
|
|
}
|
|
|
|
a = ntohl(sin.sin_addr.s_addr);
|
1996-04-28 20:54:11 +00:00
|
|
|
i = cmd(ftp, "PORT %d,%d,%d,%d,%d,%d",
|
1995-06-11 19:33:05 +00:00
|
|
|
(a >> 24) & 0xff,
|
|
|
|
(a >> 16) & 0xff,
|
|
|
|
(a >> 8) & 0xff,
|
1996-04-28 20:54:11 +00:00
|
|
|
a & 0xff,
|
1995-06-11 19:33:05 +00:00
|
|
|
(ntohs(sin.sin_port) >> 8) & 0xff,
|
1996-04-28 20:54:11 +00:00
|
|
|
ntohs(sin.sin_port) & 0xff);
|
1995-06-11 19:33:05 +00:00
|
|
|
if (i != 200)
|
|
|
|
return -1;
|
1996-04-28 20:54:11 +00:00
|
|
|
i = cmd(ftp,"RETR %s", file);
|
1995-06-11 19:33:05 +00:00
|
|
|
if (i < 0) {
|
|
|
|
close(s);
|
|
|
|
return zap(ftp);
|
|
|
|
}
|
1995-12-07 10:34:59 +00:00
|
|
|
else if (i > 299 || FTP_TIMEOUT(i)) {
|
1995-06-11 19:33:05 +00:00
|
|
|
if (isDebug())
|
|
|
|
msgDebug("FTP: No such file %s, moving on.\n", file);
|
|
|
|
close(s);
|
1995-12-07 10:34:59 +00:00
|
|
|
if (FTP_TIMEOUT(i))
|
|
|
|
return zap(ftp);
|
|
|
|
else
|
|
|
|
return -1;
|
1995-06-11 19:33:05 +00:00
|
|
|
}
|
|
|
|
ftp->fd_xfer = accept(s, 0, 0);
|
|
|
|
if (ftp->fd_xfer < 0) {
|
|
|
|
close(s);
|
|
|
|
return zap(ftp);
|
|
|
|
}
|
|
|
|
ftp->state = xfer;
|
|
|
|
close(s);
|
|
|
|
return(ftp->fd_xfer);
|
1995-05-24 09:00:58 +00:00
|
|
|
}
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
FtpEOF(FTP_t ftp)
|
|
|
|
{
|
1995-06-11 19:33:05 +00:00
|
|
|
int i;
|
|
|
|
|
1995-05-30 08:29:07 +00:00
|
|
|
if (ftp->state != xfer)
|
1996-04-28 20:54:11 +00:00
|
|
|
return botch(ftp, "FtpEOF", "xfer");
|
1995-05-27 06:19:59 +00:00
|
|
|
debug(ftp, "FtpEOF(ftp)\n");
|
1996-04-28 20:54:11 +00:00
|
|
|
close(ftp->fd_xfer);
|
|
|
|
ftp->fd_xfer = -1;
|
1995-05-24 09:00:58 +00:00
|
|
|
ftp->state = isopen;
|
1995-06-11 19:33:05 +00:00
|
|
|
i = get_a_number(ftp,0);
|
|
|
|
if (i < 0)
|
|
|
|
return zap(ftp);
|
|
|
|
else if (i != 250 && i != 226)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef STANDALONE_FTP
|
|
|
|
|
|
|
|
/* main.c */
|
1995-05-30 08:29:07 +00:00
|
|
|
int
|
1995-05-23 02:41:18 +00:00
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
1995-05-24 09:00:58 +00:00
|
|
|
FTP_t ftp;
|
|
|
|
int i;
|
|
|
|
char c;
|
1995-05-30 08:29:07 +00:00
|
|
|
|
1995-05-24 09:00:58 +00:00
|
|
|
ftp = FtpInit();
|
1995-06-11 19:33:05 +00:00
|
|
|
if (!ftp)
|
|
|
|
err(1, "FtpInit()");
|
|
|
|
|
|
|
|
FtpDebug(ftp, 1);
|
1995-05-24 09:00:58 +00:00
|
|
|
i = FtpOpen(ftp, "freefall.cdrom.com", "ftp", "phk-libftp@");
|
1995-06-11 19:33:05 +00:00
|
|
|
FtpBinary(ftp, 1);
|
|
|
|
FtpPassive(ftp, 0);
|
|
|
|
FtpChdir(ftp, "/pub");
|
|
|
|
FtpChdir(ftp, "FreeBSD");
|
|
|
|
i = FtpGet(ftp, "README");
|
|
|
|
while (1 == read(i, &c, 1))
|
1995-05-24 09:00:58 +00:00
|
|
|
putchar(c);
|
|
|
|
FtpEOF(ftp);
|
|
|
|
return 0;
|
1995-05-23 02:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /*STANDALONE_FTP*/
|