1996-06-17 13:26:06 +01: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
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Major Changelog:
|
|
|
|
*
|
|
|
|
* Jordan K. Hubbard
|
|
|
|
* 17 Jan 1996
|
|
|
|
*
|
|
|
|
* Turned inside out. Now returns xfers as new file ids, not as a special
|
|
|
|
* `state' of FTP_t
|
|
|
|
*
|
1999-08-28 01:22:10 +01:00
|
|
|
* $FreeBSD$
|
1996-06-17 13:26:06 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
1996-09-19 18:28:34 +01:00
|
|
|
|
1996-06-17 13:26:06 +01:00
|
|
|
#include <netinet/in.h>
|
1996-09-19 18:28:34 +01:00
|
|
|
|
1996-06-17 13:26:06 +01:00
|
|
|
#include <arpa/inet.h>
|
1996-09-19 18:28:34 +01:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
1996-06-17 13:26:06 +01:00
|
|
|
#include <ftpio.h>
|
1996-09-19 18:28:34 +01:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1996-06-17 13:26:06 +01:00
|
|
|
|
|
|
|
#define SUCCESS 0
|
|
|
|
#define FAILURE -1
|
|
|
|
|
|
|
|
#ifndef TRUE
|
|
|
|
#define TRUE (1)
|
|
|
|
#define FALSE (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* How to see by a given code whether or not the connection has timed out */
|
1996-12-17 20:19:35 +00:00
|
|
|
#define FTP_TIMEOUT(code) (FtpTimedOut || code == FTP_TIMED_OUT)
|
1996-06-17 13:26:06 +01:00
|
|
|
|
|
|
|
/* Internal routines - deal only with internal FTP_t type */
|
|
|
|
static FTP_t ftp_new(void);
|
1996-06-22 22:43:56 +01:00
|
|
|
static void check_passive(FILE *fp);
|
1996-06-17 13:26:06 +01:00
|
|
|
static int ftp_read_method(void *n, char *buf, int nbytes);
|
|
|
|
static int ftp_write_method(void *n, const char *buf, int nbytes);
|
|
|
|
static int ftp_close_method(void *n);
|
|
|
|
static int writes(int fd, char *s);
|
|
|
|
static __inline char *get_a_line(FTP_t ftp);
|
|
|
|
static int get_a_number(FTP_t ftp, char **q);
|
|
|
|
static int botch(char *func, char *botch_state);
|
|
|
|
static int cmd(FTP_t ftp, const char *fmt, ...);
|
2000-07-05 20:34:43 +01:00
|
|
|
static int ftp_login_session(FTP_t ftp, char *host, int af, char *user, char *passwd, int port, int verbose);
|
1996-09-19 18:28:34 +01:00
|
|
|
static int ftp_file_op(FTP_t ftp, char *operation, char *file, FILE **fp, char *mode, off_t *seekto);
|
1996-06-17 13:26:06 +01:00
|
|
|
static int ftp_close(FTP_t ftp);
|
|
|
|
static int get_url_info(char *url_in, char *host_ret, int *port_ret, char *name_ret);
|
1996-12-17 20:19:35 +00:00
|
|
|
static void ftp_timeout(int sig);
|
|
|
|
static void ftp_set_timeout(void);
|
|
|
|
static void ftp_clear_timeout(void);
|
2000-07-05 20:34:43 +01:00
|
|
|
static void ai_unmapped(struct addrinfo *);
|
1996-12-17 20:19:35 +00:00
|
|
|
|
1996-06-17 13:26:06 +01:00
|
|
|
|
|
|
|
/* Global status variable - ick */
|
|
|
|
int FtpTimedOut;
|
|
|
|
|
1996-12-17 20:19:35 +00:00
|
|
|
/* FTP happy status codes */
|
|
|
|
#define FTP_GENERALLY_HAPPY 200
|
|
|
|
#define FTP_ASCII_HAPPY FTP_GENERALLY_HAPPY
|
|
|
|
#define FTP_BINARY_HAPPY FTP_GENERALLY_HAPPY
|
|
|
|
#define FTP_PORT_HAPPY FTP_GENERALLY_HAPPY
|
|
|
|
#define FTP_HAPPY_COMMENT 220
|
1996-06-17 13:26:06 +01:00
|
|
|
#define FTP_QUIT_HAPPY 221
|
|
|
|
#define FTP_TRANSFER_HAPPY 226
|
|
|
|
#define FTP_PASSIVE_HAPPY 227
|
2000-07-05 20:34:43 +01:00
|
|
|
#define FTP_LPASSIVE_HAPPY 228
|
|
|
|
#define FTP_EPASSIVE_HAPPY 229
|
1996-06-17 13:26:06 +01:00
|
|
|
#define FTP_CHDIR_HAPPY 250
|
|
|
|
|
1996-12-17 20:19:35 +00:00
|
|
|
/* FTP unhappy status codes */
|
|
|
|
#define FTP_TIMED_OUT 421
|
|
|
|
|
1996-06-17 13:26:06 +01:00
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* gross! evil! bad! We really need an access primitive for cookie in stdio itself.
|
|
|
|
* it's too convenient a hook to bury and it's already exported through funopen as it is, so...
|
|
|
|
* XXX
|
|
|
|
*/
|
|
|
|
#define fcookie(fp) ((fp)->_cookie)
|
|
|
|
|
|
|
|
/* Placeholder in case we want to do any pre-init stuff at some point */
|
|
|
|
int
|
|
|
|
networkInit()
|
|
|
|
{
|
|
|
|
return SUCCESS; /* XXX dummy function for now XXX */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check a return code with some lenience for back-dated garbage that might be in the buffer */
|
|
|
|
static int
|
|
|
|
check_code(FTP_t ftp, int var, int preferred)
|
|
|
|
{
|
1997-12-20 04:06:06 +00:00
|
|
|
ftp->error = 0;
|
1996-06-17 13:26:06 +01:00
|
|
|
while (1) {
|
|
|
|
if (var == preferred)
|
|
|
|
return 0;
|
1996-12-17 20:19:35 +00:00
|
|
|
else if (var == FTP_TRANSFER_HAPPY) /* last operation succeeded */
|
1996-06-17 13:26:06 +01:00
|
|
|
var = get_a_number(ftp, NULL);
|
1996-12-17 20:19:35 +00:00
|
|
|
else if (var == FTP_HAPPY_COMMENT) /* chit-chat */
|
1996-06-17 13:26:06 +01:00
|
|
|
var = get_a_number(ftp, NULL);
|
1996-12-17 20:19:35 +00:00
|
|
|
else if (var == FTP_GENERALLY_HAPPY) /* general success code */
|
1996-06-17 13:26:06 +01:00
|
|
|
var = get_a_number(ftp, NULL);
|
|
|
|
else {
|
1997-12-20 04:06:06 +00:00
|
|
|
ftp->error = var;
|
1996-06-17 13:26:06 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1996-06-22 22:43:56 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
ftpAscii(FILE *fp)
|
1996-06-17 13:26:06 +01:00
|
|
|
{
|
1996-06-22 22:43:56 +01:00
|
|
|
FTP_t ftp = fcookie(fp);
|
|
|
|
int i;
|
1996-06-17 13:26:06 +01:00
|
|
|
|
1996-06-22 22:43:56 +01:00
|
|
|
if (!ftp->is_binary)
|
|
|
|
return SUCCESS;
|
|
|
|
i = cmd(ftp, "TYPE A");
|
|
|
|
if (i < 0 || check_code(ftp, i, FTP_ASCII_HAPPY))
|
|
|
|
return i;
|
|
|
|
ftp->is_binary = FALSE;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
1996-06-17 13:26:06 +01:00
|
|
|
|
1996-06-22 22:43:56 +01:00
|
|
|
int
|
|
|
|
ftpBinary(FILE *fp)
|
|
|
|
{
|
|
|
|
FTP_t ftp = fcookie(fp);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (ftp->is_binary)
|
|
|
|
return SUCCESS;
|
|
|
|
i = cmd(ftp, "TYPE I");
|
|
|
|
if (i < 0 || check_code(ftp, i, FTP_BINARY_HAPPY))
|
|
|
|
return i;
|
|
|
|
ftp->is_binary = TRUE;
|
|
|
|
return SUCCESS;
|
1996-06-17 13:26:06 +01:00
|
|
|
}
|
1996-07-04 01:55:21 +01:00
|
|
|
void
|
|
|
|
ftpVerbose(FILE *fp, int status)
|
|
|
|
{
|
|
|
|
FTP_t ftp = fcookie(fp);
|
|
|
|
ftp->is_verbose = status;
|
|
|
|
}
|
1996-06-17 13:26:06 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
ftpChdir(FILE *fp, char *dir)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
FTP_t ftp = fcookie(fp);
|
|
|
|
|
|
|
|
i = cmd(ftp, "CWD %s", dir);
|
|
|
|
if (i < 0 || check_code(ftp, i, FTP_CHDIR_HAPPY))
|
1996-06-17 21:36:57 +01:00
|
|
|
return i;
|
1996-06-17 13:26:06 +01:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ftpErrno(FILE *fp)
|
|
|
|
{
|
|
|
|
FTP_t ftp = fcookie(fp);
|
1997-12-20 04:06:06 +00:00
|
|
|
return ftp->error;
|
1996-06-17 13:26:06 +01:00
|
|
|
}
|
|
|
|
|
1996-08-21 02:12:11 +01:00
|
|
|
const char *
|
1997-12-20 04:06:06 +00:00
|
|
|
ftpErrString(int error)
|
1996-08-21 02:12:11 +01:00
|
|
|
{
|
|
|
|
int k;
|
|
|
|
|
1997-12-20 04:06:06 +00:00
|
|
|
if (error == -1)
|
1997-01-17 12:51:55 +00:00
|
|
|
return("connection in wrong state");
|
1997-12-20 04:06:06 +00:00
|
|
|
if (error < 100)
|
1997-10-03 00:26:03 +01:00
|
|
|
/* XXX soon UNIX errnos will catch up with FTP protocol errnos */
|
1997-12-20 04:06:06 +00:00
|
|
|
return strerror(error);
|
1996-08-21 02:12:11 +01:00
|
|
|
for (k = 0; k < ftpErrListLength; k++)
|
1997-12-20 04:06:06 +00:00
|
|
|
if (ftpErrList[k].num == error)
|
1996-08-21 02:12:11 +01:00
|
|
|
return(ftpErrList[k].string);
|
|
|
|
return("Unknown error");
|
|
|
|
}
|
|
|
|
|
1996-09-19 18:28:34 +01:00
|
|
|
off_t
|
1996-06-17 13:26:06 +01:00
|
|
|
ftpGetSize(FILE *fp, char *name)
|
|
|
|
{
|
|
|
|
int i;
|
1996-09-19 18:28:34 +01:00
|
|
|
char p[BUFSIZ], *cp, *ep;
|
1996-06-17 13:26:06 +01:00
|
|
|
FTP_t ftp = fcookie(fp);
|
1996-09-19 18:28:34 +01:00
|
|
|
off_t size;
|
1996-06-17 13:26:06 +01:00
|
|
|
|
1996-06-22 22:43:56 +01:00
|
|
|
check_passive(fp);
|
1996-06-17 13:26:06 +01:00
|
|
|
sprintf(p, "SIZE %s\r\n", name);
|
1996-08-31 23:02:18 +01:00
|
|
|
if (ftp->is_verbose)
|
|
|
|
fprintf(stderr, "Sending %s", p);
|
1996-12-17 20:19:35 +00:00
|
|
|
if (writes(ftp->fd_ctrl, p))
|
1996-09-19 18:28:34 +01:00
|
|
|
return (off_t)-1;
|
1996-06-17 13:26:06 +01:00
|
|
|
i = get_a_number(ftp, &cp);
|
|
|
|
if (check_code(ftp, i, 213))
|
1996-09-19 18:28:34 +01:00
|
|
|
return (off_t)-1;
|
|
|
|
|
|
|
|
errno = 0; /* to check for ERANGE */
|
|
|
|
size = (off_t)strtoq(cp, &ep, 10);
|
|
|
|
if (*ep != '\0' || errno == ERANGE)
|
|
|
|
return (off_t)-1;
|
|
|
|
return size;
|
1996-06-17 13:26:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
time_t
|
|
|
|
ftpGetModtime(FILE *fp, char *name)
|
|
|
|
{
|
|
|
|
char p[BUFSIZ], *cp;
|
|
|
|
struct tm t;
|
|
|
|
time_t t0 = time (0);
|
|
|
|
FTP_t ftp = fcookie(fp);
|
|
|
|
int i;
|
|
|
|
|
1996-06-22 22:43:56 +01:00
|
|
|
check_passive(fp);
|
1996-06-17 13:26:06 +01:00
|
|
|
sprintf(p, "MDTM %s\r\n", name);
|
1996-08-31 23:02:18 +01:00
|
|
|
if (ftp->is_verbose)
|
|
|
|
fprintf(stderr, "Sending %s", p);
|
1996-12-17 20:19:35 +00:00
|
|
|
if (writes(ftp->fd_ctrl, p))
|
1996-06-17 13:26:06 +01:00
|
|
|
return (time_t)0;
|
|
|
|
i = get_a_number(ftp, &cp);
|
|
|
|
if (check_code(ftp, i, 213))
|
|
|
|
return (time_t)0;
|
|
|
|
while (*cp && !isdigit(*cp))
|
|
|
|
cp++;
|
|
|
|
if (!*cp)
|
|
|
|
return (time_t)0;
|
|
|
|
t0 = localtime (&t0)->tm_gmtoff;
|
|
|
|
sscanf(cp, "%04d%02d%02d%02d%02d%02d", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec);
|
|
|
|
t.tm_mon--;
|
|
|
|
t.tm_year -= 1900;
|
|
|
|
t.tm_isdst=-1;
|
|
|
|
t.tm_gmtoff = 0;
|
|
|
|
t0 += mktime (&t);
|
|
|
|
return t0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *
|
1996-09-19 18:28:34 +01:00
|
|
|
ftpGet(FILE *fp, char *file, off_t *seekto)
|
1996-06-17 13:26:06 +01:00
|
|
|
{
|
|
|
|
FILE *fp2;
|
|
|
|
FTP_t ftp = fcookie(fp);
|
|
|
|
|
1996-06-22 22:43:56 +01:00
|
|
|
check_passive(fp);
|
|
|
|
if (ftpBinary(fp) != SUCCESS)
|
|
|
|
return NULL;
|
|
|
|
|
1996-06-17 16:28:08 +01:00
|
|
|
if (ftp_file_op(ftp, "RETR", file, &fp2, "r", seekto) == SUCCESS)
|
1996-06-17 13:26:06 +01:00
|
|
|
return fp2;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1996-06-22 22:43:56 +01:00
|
|
|
/* Returns a standard FILE pointer type representing an open control connection */
|
|
|
|
FILE *
|
1996-11-14 06:59:41 +00:00
|
|
|
ftpLogin(char *host, char *user, char *passwd, int port, int verbose, int *retcode)
|
2000-07-05 20:34:43 +01:00
|
|
|
{
|
|
|
|
#ifdef INET6
|
|
|
|
return ftpLoginAf(host, AF_UNSPEC, user, passwd, port, verbose, retcode);
|
|
|
|
#else
|
|
|
|
return ftpLoginAf(host, AF_INET, user, passwd, port, verbose, retcode);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *
|
|
|
|
ftpLoginAf(char *host, int af, char *user, char *passwd, int port, int verbose, int *retcode)
|
1996-06-22 22:43:56 +01:00
|
|
|
{
|
|
|
|
FTP_t n;
|
|
|
|
FILE *fp;
|
|
|
|
|
1996-11-14 06:59:41 +00:00
|
|
|
if (retcode)
|
|
|
|
*retcode = 0;
|
1996-06-22 22:43:56 +01:00
|
|
|
if (networkInit() != SUCCESS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
n = ftp_new();
|
|
|
|
fp = NULL;
|
2000-07-05 20:34:43 +01:00
|
|
|
if (n && ftp_login_session(n, host, af, user, passwd, port, verbose) == SUCCESS) {
|
1996-06-22 22:43:56 +01:00
|
|
|
fp = funopen(n, ftp_read_method, ftp_write_method, NULL, ftp_close_method); /* BSD 4.4 function! */
|
|
|
|
fp->_file = n->fd_ctrl;
|
|
|
|
}
|
1997-01-17 12:51:55 +00:00
|
|
|
if (retcode) {
|
|
|
|
if (!n)
|
|
|
|
*retcode = (FtpTimedOut ? FTP_TIMED_OUT : -1);
|
|
|
|
/* Poor attempt at mapping real errnos to FTP error codes */
|
1997-12-20 04:06:06 +00:00
|
|
|
else switch(n->error) {
|
1997-01-17 12:51:55 +00:00
|
|
|
case EADDRNOTAVAIL:
|
|
|
|
*retcode = FTP_TIMED_OUT; /* Actually no such host, but we have no way of saying that. :-( */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ETIMEDOUT:
|
|
|
|
*retcode = FTP_TIMED_OUT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1997-12-20 04:06:06 +00:00
|
|
|
*retcode = n->error;
|
1997-01-17 12:51:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1996-06-22 22:43:56 +01:00
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
1996-06-17 13:26:06 +01:00
|
|
|
FILE *
|
|
|
|
ftpPut(FILE *fp, char *file)
|
|
|
|
{
|
|
|
|
FILE *fp2;
|
|
|
|
FTP_t ftp = fcookie(fp);
|
|
|
|
|
1996-06-22 22:43:56 +01:00
|
|
|
check_passive(fp);
|
1996-06-17 16:28:08 +01:00
|
|
|
if (ftp_file_op(ftp, "STOR", file, &fp2, "w", NULL) == SUCCESS)
|
1996-06-17 13:26:06 +01:00
|
|
|
return fp2;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ftpPassive(FILE *fp, int st)
|
|
|
|
{
|
|
|
|
FTP_t ftp = fcookie(fp);
|
|
|
|
|
2000-08-18 00:46:13 +01:00
|
|
|
ftp->is_passive = !!st; /* normalize "st" to zero or one */
|
1996-06-17 13:26:06 +01:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *
|
1996-11-14 06:59:41 +00:00
|
|
|
ftpGetURL(char *url, char *user, char *passwd, int *retcode)
|
2000-07-05 20:34:43 +01:00
|
|
|
{
|
|
|
|
#ifdef INET6
|
|
|
|
return ftpGetURLAf(url, AF_UNSPEC, user, passwd, retcode);
|
|
|
|
#else
|
|
|
|
return ftpGetURLAf(url, AF_INET, user, passwd, retcode);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *
|
|
|
|
ftpGetURLAf(char *url, int af, char *user, char *passwd, int *retcode)
|
1996-06-17 13:26:06 +01:00
|
|
|
{
|
|
|
|
char host[255], name[255];
|
|
|
|
int port;
|
1996-06-18 00:16:04 +01:00
|
|
|
FILE *fp2;
|
1996-08-31 23:02:18 +01:00
|
|
|
static FILE *fp = NULL;
|
|
|
|
static char *prev_host;
|
1996-06-17 13:26:06 +01:00
|
|
|
|
1996-11-14 06:59:41 +00:00
|
|
|
if (retcode)
|
|
|
|
*retcode = 0;
|
1996-06-17 13:26:06 +01:00
|
|
|
if (get_url_info(url, host, &port, name) == SUCCESS) {
|
1996-11-14 06:59:41 +00:00
|
|
|
if (fp && prev_host) {
|
1996-08-31 23:02:18 +01:00
|
|
|
if (!strcmp(prev_host, host)) {
|
|
|
|
/* Try to use cached connection */
|
|
|
|
fp2 = ftpGet(fp, name, NULL);
|
|
|
|
if (!fp2) {
|
|
|
|
/* Connection timed out or was no longer valid */
|
|
|
|
fclose(fp);
|
|
|
|
free(prev_host);
|
1996-11-14 06:59:41 +00:00
|
|
|
prev_host = NULL;
|
1996-08-31 23:02:18 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return fp2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* It's a different host now, flush old */
|
|
|
|
fclose(fp);
|
1996-11-14 06:59:41 +00:00
|
|
|
free(prev_host);
|
|
|
|
prev_host = NULL;
|
1996-08-31 23:02:18 +01:00
|
|
|
}
|
|
|
|
}
|
2000-07-05 20:34:43 +01:00
|
|
|
fp = ftpLoginAf(host, af, user, passwd, port, 0, retcode);
|
1996-06-17 13:26:06 +01:00
|
|
|
if (fp) {
|
1996-06-17 16:28:08 +01:00
|
|
|
fp2 = ftpGet(fp, name, NULL);
|
1996-11-14 06:59:41 +00:00
|
|
|
if (!fp2) {
|
|
|
|
/* Connection timed out or was no longer valid */
|
|
|
|
if (retcode)
|
|
|
|
*retcode = ftpErrno(fp);
|
|
|
|
fclose(fp);
|
|
|
|
fp = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
prev_host = strdup(host);
|
1996-06-17 13:26:06 +01:00
|
|
|
return fp2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *
|
1996-11-14 06:59:41 +00:00
|
|
|
ftpPutURL(char *url, char *user, char *passwd, int *retcode)
|
2000-07-05 20:34:43 +01:00
|
|
|
{
|
|
|
|
#ifdef INET6
|
|
|
|
return ftpPutURLAf(url, AF_UNSPEC, user, passwd, retcode);
|
|
|
|
#else
|
|
|
|
return ftpPutURLAf(url, AF_INET, user, passwd, retcode);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *
|
|
|
|
ftpPutURLAf(char *url, int af, char *user, char *passwd, int *retcode)
|
1996-06-17 13:26:06 +01:00
|
|
|
{
|
|
|
|
char host[255], name[255];
|
|
|
|
int port;
|
1996-11-14 09:51:47 +00:00
|
|
|
static FILE *fp = NULL;
|
|
|
|
FILE *fp2;
|
1996-06-17 13:26:06 +01:00
|
|
|
|
1996-11-14 06:59:41 +00:00
|
|
|
if (retcode)
|
|
|
|
*retcode = 0;
|
1996-11-14 09:51:47 +00:00
|
|
|
if (fp) { /* Close previous managed connection */
|
|
|
|
fclose(fp);
|
|
|
|
fp = NULL;
|
|
|
|
}
|
1996-06-17 13:26:06 +01:00
|
|
|
if (get_url_info(url, host, &port, name) == SUCCESS) {
|
2000-07-05 20:34:43 +01:00
|
|
|
fp = ftpLoginAf(host, af, user, passwd, port, 0, retcode);
|
1996-06-17 13:26:06 +01:00
|
|
|
if (fp) {
|
|
|
|
fp2 = ftpPut(fp, name);
|
1996-11-14 09:51:47 +00:00
|
|
|
if (!fp2) {
|
|
|
|
if (retcode)
|
|
|
|
*retcode = ftpErrno(fp);
|
|
|
|
fclose(fp);
|
|
|
|
fp = NULL;
|
|
|
|
}
|
1996-06-17 13:26:06 +01:00
|
|
|
return fp2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Internal workhorse function for dissecting URLs. Takes a URL as the first argument and returns the
|
|
|
|
result of such disection in the host, user, passwd, port and name variables. */
|
|
|
|
static int
|
|
|
|
get_url_info(char *url_in, char *host_ret, int *port_ret, char *name_ret)
|
|
|
|
{
|
|
|
|
char *name, *host, *cp, url[BUFSIZ];
|
|
|
|
int port;
|
|
|
|
|
|
|
|
name = host = NULL;
|
|
|
|
/* XXX add http:// here or somewhere reasonable at some point XXX */
|
1997-09-18 15:08:40 +01:00
|
|
|
if (strncmp("ftp://", url_in, 6) != 0)
|
1996-06-17 13:26:06 +01:00
|
|
|
return FAILURE;
|
|
|
|
/* We like to stomp a lot on the URL string in dissecting it, so copy it first */
|
|
|
|
strncpy(url, url_in, BUFSIZ);
|
|
|
|
host = url + 6;
|
|
|
|
if ((cp = index(host, ':')) != NULL) {
|
|
|
|
*(cp++) = '\0';
|
|
|
|
port = strtol(cp, 0, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
port = 0; /* use default */
|
|
|
|
if (port_ret)
|
|
|
|
*port_ret = port;
|
|
|
|
|
|
|
|
if ((name = index(cp ? cp : host, '/')) != NULL)
|
|
|
|
*(name++) = '\0';
|
|
|
|
if (host_ret)
|
|
|
|
strcpy(host_ret, host);
|
|
|
|
if (name && name_ret)
|
|
|
|
strcpy(name_ret, name);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FTP_t
|
|
|
|
ftp_new(void)
|
|
|
|
{
|
|
|
|
FTP_t ftp;
|
|
|
|
|
|
|
|
ftp = (FTP_t)malloc(sizeof *ftp);
|
|
|
|
if (!ftp)
|
|
|
|
return NULL;
|
|
|
|
memset(ftp, 0, sizeof *ftp);
|
|
|
|
ftp->fd_ctrl = -1;
|
|
|
|
ftp->con_state = init;
|
1996-06-22 22:43:56 +01:00
|
|
|
ftp->is_binary = FALSE;
|
|
|
|
ftp->is_passive = FALSE;
|
1996-07-04 01:55:21 +01:00
|
|
|
ftp->is_verbose = FALSE;
|
1997-12-20 04:06:06 +00:00
|
|
|
ftp->error = 0;
|
1996-06-17 13:26:06 +01:00
|
|
|
return ftp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ftp_read_method(void *vp, char *buf, int nbytes)
|
|
|
|
{
|
|
|
|
int i, fd;
|
|
|
|
FTP_t n = (FTP_t)vp;
|
|
|
|
|
|
|
|
fd = n->fd_ctrl;
|
|
|
|
i = (fd >= 0) ? read(fd, buf, nbytes) : EOF;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ftp_write_method(void *vp, const char *buf, int nbytes)
|
|
|
|
{
|
|
|
|
int i, fd;
|
|
|
|
FTP_t n = (FTP_t)vp;
|
|
|
|
|
|
|
|
fd = n->fd_ctrl;
|
|
|
|
i = (fd >= 0) ? write(fd, buf, nbytes) : EOF;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ftp_close_method(void *n)
|
|
|
|
{
|
1996-06-17 21:36:57 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
i = ftp_close((FTP_t)n);
|
|
|
|
free(n);
|
|
|
|
return i;
|
1996-06-17 13:26:06 +01:00
|
|
|
}
|
|
|
|
|
2000-08-18 00:46:13 +01:00
|
|
|
/*
|
|
|
|
* This function checks whether the FTP_PASSIVE_MODE environment
|
|
|
|
* variable is set, and, if so, enforces the desired mode.
|
|
|
|
*/
|
1996-06-22 22:43:56 +01:00
|
|
|
static void
|
|
|
|
check_passive(FILE *fp)
|
|
|
|
{
|
2000-08-18 00:46:13 +01:00
|
|
|
const char *cp = getenv("FTP_PASSIVE_MODE");
|
2000-04-23 22:23:34 +01:00
|
|
|
|
2000-08-18 00:46:13 +01:00
|
|
|
if (cp != NULL)
|
|
|
|
ftpPassive(fp, strncasecmp(cp, "no", 2));
|
1996-06-22 22:43:56 +01:00
|
|
|
}
|
|
|
|
|
1996-06-17 13:26:06 +01:00
|
|
|
static void
|
1996-12-17 20:19:35 +00:00
|
|
|
ftp_timeout(int sig)
|
1996-06-17 13:26:06 +01:00
|
|
|
{
|
|
|
|
FtpTimedOut = TRUE;
|
|
|
|
/* Debug("ftp_pkg: ftp_timeout called - operation timed out"); */
|
|
|
|
}
|
|
|
|
|
1996-12-17 20:19:35 +00:00
|
|
|
static void
|
|
|
|
ftp_set_timeout(void)
|
|
|
|
{
|
1999-09-28 14:33:13 +01:00
|
|
|
struct sigaction new;
|
1996-12-17 20:19:35 +00:00
|
|
|
char *cp;
|
|
|
|
int ival;
|
|
|
|
|
|
|
|
FtpTimedOut = FALSE;
|
1999-09-28 14:33:13 +01:00
|
|
|
sigemptyset(&new.sa_mask);
|
|
|
|
new.sa_flags = 0;
|
1997-01-17 12:51:55 +00:00
|
|
|
new.sa_handler = ftp_timeout;
|
|
|
|
sigaction(SIGALRM, &new, NULL);
|
|
|
|
cp = getenv("FTP_TIMEOUT");
|
1996-12-17 20:19:35 +00:00
|
|
|
if (!cp || !(ival = atoi(cp)))
|
|
|
|
ival = 120;
|
|
|
|
alarm(ival);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ftp_clear_timeout(void)
|
|
|
|
{
|
1999-09-28 14:33:13 +01:00
|
|
|
struct sigaction new;
|
|
|
|
|
1996-12-17 20:19:35 +00:00
|
|
|
alarm(0);
|
1999-09-28 14:33:13 +01:00
|
|
|
sigemptyset(&new.sa_mask);
|
|
|
|
new.sa_flags = 0;
|
1997-01-17 12:51:55 +00:00
|
|
|
new.sa_handler = SIG_DFL;
|
|
|
|
sigaction(SIGALRM, &new, NULL);
|
1996-12-17 20:19:35 +00:00
|
|
|
}
|
|
|
|
|
1996-06-17 13:26:06 +01:00
|
|
|
static int
|
|
|
|
writes(int fd, char *s)
|
|
|
|
{
|
|
|
|
int n, i = strlen(s);
|
|
|
|
|
1996-12-17 20:19:35 +00:00
|
|
|
ftp_set_timeout();
|
1996-06-17 13:26:06 +01:00
|
|
|
n = write(fd, s, i);
|
1996-12-17 20:19:35 +00:00
|
|
|
ftp_clear_timeout();
|
|
|
|
if (FtpTimedOut || i != n)
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
1996-06-17 13:26:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline char *
|
|
|
|
get_a_line(FTP_t ftp)
|
|
|
|
{
|
|
|
|
static char buf[BUFSIZ];
|
|
|
|
int i,j;
|
|
|
|
|
|
|
|
/* Debug("ftp_pkg: trying to read a line from %d", ftp->fd_ctrl); */
|
|
|
|
for(i = 0; i < BUFSIZ;) {
|
1996-12-17 20:19:35 +00:00
|
|
|
ftp_set_timeout();
|
1996-06-17 13:26:06 +01:00
|
|
|
j = read(ftp->fd_ctrl, buf + i, 1);
|
1996-12-17 20:19:35 +00:00
|
|
|
ftp_clear_timeout();
|
|
|
|
if (FtpTimedOut || j != 1)
|
1996-06-17 13:26:06 +01:00
|
|
|
return NULL;
|
|
|
|
if (buf[i] == '\r' || buf[i] == '\n') {
|
|
|
|
if (!i)
|
|
|
|
continue;
|
|
|
|
buf[i] = '\0';
|
1996-07-04 01:55:21 +01:00
|
|
|
if (ftp->is_verbose == TRUE)
|
1996-08-24 10:51:59 +01:00
|
|
|
fprintf(stderr, "%s\n",buf+4);
|
1996-06-17 13:26:06 +01:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
/* Debug("ftp_pkg: read string \"%s\" from %d", buf, ftp->fd_ctrl); */
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_a_number(FTP_t ftp, char **q)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int i = -1, j;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
p = get_a_line(ftp);
|
1996-06-17 21:36:57 +01:00
|
|
|
if (!p) {
|
|
|
|
ftp_close(ftp);
|
1996-12-17 20:19:35 +00:00
|
|
|
if (FtpTimedOut)
|
|
|
|
return FTP_TIMED_OUT;
|
1996-06-17 13:26:06 +01:00
|
|
|
return FAILURE;
|
1996-06-17 21:36:57 +01:00
|
|
|
}
|
1996-06-17 13:26:06 +01:00
|
|
|
if (!(isdigit(p[0]) && isdigit(p[1]) && isdigit(p[2])))
|
|
|
|
continue;
|
|
|
|
if (i == -1 && p[3] == '-') {
|
|
|
|
i = strtol(p, 0, 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (p[3] != ' ' && p[3] != '\t')
|
|
|
|
continue;
|
|
|
|
j = strtol(p, 0, 0);
|
|
|
|
if (i == -1) {
|
|
|
|
if (q) *q = p+4;
|
|
|
|
/* Debug("ftp_pkg: read reply %d from server (%s)", j, p); */
|
|
|
|
return j;
|
|
|
|
} else if (j == i) {
|
|
|
|
if (q) *q = p+4;
|
|
|
|
/* Debug("ftp_pkg: read reply %d from server (%s)", j, p); */
|
|
|
|
return j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ftp_close(FTP_t ftp)
|
|
|
|
{
|
1997-10-01 08:21:41 +01:00
|
|
|
int i, rcode;
|
1996-06-17 13:26:06 +01:00
|
|
|
|
1997-10-01 08:21:41 +01:00
|
|
|
rcode = FAILURE;
|
1996-11-14 05:22:12 +00:00
|
|
|
if (ftp->con_state == isopen) {
|
|
|
|
ftp->con_state = quit;
|
1997-01-21 20:37:59 +00:00
|
|
|
/* If last operation timed out, don't try to quit - just close */
|
1997-12-20 04:06:06 +00:00
|
|
|
if (ftp->error != FTP_TIMED_OUT)
|
1997-01-21 20:37:59 +00:00
|
|
|
i = cmd(ftp, "QUIT");
|
|
|
|
else
|
|
|
|
i = FTP_QUIT_HAPPY;
|
1997-10-01 08:21:41 +01:00
|
|
|
if (!check_code(ftp, i, FTP_QUIT_HAPPY))
|
|
|
|
rcode = SUCCESS;
|
1996-06-17 13:26:06 +01:00
|
|
|
close(ftp->fd_ctrl);
|
|
|
|
ftp->fd_ctrl = -1;
|
|
|
|
}
|
1997-10-01 08:21:41 +01:00
|
|
|
else if (ftp->con_state == quit)
|
|
|
|
rcode = SUCCESS;
|
|
|
|
return rcode;
|
1996-06-17 13:26:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
botch(char *func, char *botch_state)
|
|
|
|
{
|
|
|
|
/* Debug("ftp_pkg: botch: %s(%s)", func, botch_state); */
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cmd(FTP_t ftp, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char p[BUFSIZ];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
(void)vsnprintf(p, sizeof p, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
1996-11-14 05:22:12 +00:00
|
|
|
if (ftp->con_state == init)
|
1996-06-17 13:26:06 +01:00
|
|
|
return botch("cmd", "open");
|
|
|
|
|
|
|
|
strcat(p, "\r\n");
|
1996-08-31 23:02:18 +01:00
|
|
|
if (ftp->is_verbose)
|
|
|
|
fprintf(stderr, "Sending: %s", p);
|
1996-12-17 20:19:35 +00:00
|
|
|
if (writes(ftp->fd_ctrl, p)) {
|
|
|
|
if (FtpTimedOut)
|
|
|
|
return FTP_TIMED_OUT;
|
1996-06-17 13:26:06 +01:00
|
|
|
return FAILURE;
|
1996-12-17 20:19:35 +00:00
|
|
|
}
|
|
|
|
while ((i = get_a_number(ftp, NULL)) == FTP_HAPPY_COMMENT);
|
1996-06-17 13:26:06 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-05 20:34:43 +01:00
|
|
|
ftp_login_session(FTP_t ftp, char *host, int af,
|
|
|
|
char *user, char *passwd, int port, int verbose)
|
1996-06-17 13:26:06 +01:00
|
|
|
{
|
2000-07-05 20:34:43 +01:00
|
|
|
char pbuf[10];
|
|
|
|
struct addrinfo hints, *res, *res0;
|
|
|
|
int err;
|
1996-06-17 13:26:06 +01:00
|
|
|
int s;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (networkInit() != SUCCESS)
|
|
|
|
return FAILURE;
|
|
|
|
|
1996-06-17 21:36:57 +01:00
|
|
|
if (ftp->con_state != init) {
|
1996-06-17 13:26:06 +01:00
|
|
|
ftp_close(ftp);
|
1997-12-20 04:06:06 +00:00
|
|
|
ftp->error = -1;
|
1996-06-17 21:36:57 +01:00
|
|
|
return FAILURE;
|
|
|
|
}
|
1996-06-17 13:26:06 +01:00
|
|
|
|
|
|
|
if (!user)
|
|
|
|
user = "ftp";
|
|
|
|
|
|
|
|
if (!passwd)
|
|
|
|
passwd = "setup@";
|
|
|
|
|
|
|
|
if (!port)
|
|
|
|
port = 21;
|
|
|
|
|
2000-07-05 20:34:43 +01:00
|
|
|
snprintf(pbuf, sizeof(pbuf), "%d", port);
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = af;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = 0;
|
|
|
|
err = getaddrinfo(host, pbuf, &hints, &res0);
|
|
|
|
if (err) {
|
|
|
|
ftp->error = 0;
|
|
|
|
return FAILURE;
|
1996-06-17 13:26:06 +01:00
|
|
|
}
|
|
|
|
|
2000-07-05 20:34:43 +01:00
|
|
|
s = -1;
|
|
|
|
for (res = res0; res; res = res->ai_next) {
|
|
|
|
ai_unmapped(res);
|
|
|
|
ftp->addrtype = res->ai_family;
|
1996-06-17 13:26:06 +01:00
|
|
|
|
2000-07-05 20:34:43 +01:00
|
|
|
if ((s = socket(res->ai_family, res->ai_socktype,
|
|
|
|
res->ai_protocol)) < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
|
|
|
|
(void)close(s);
|
|
|
|
s = -1;
|
|
|
|
continue;
|
|
|
|
}
|
1996-06-17 13:26:06 +01:00
|
|
|
|
2000-07-05 20:34:43 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
freeaddrinfo(res0);
|
|
|
|
if (s < 0) {
|
1997-12-20 04:06:06 +00:00
|
|
|
ftp->error = errno;
|
1996-06-17 13:26:06 +01:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ftp->fd_ctrl = s;
|
|
|
|
ftp->con_state = isopen;
|
1996-08-03 12:58:54 +01:00
|
|
|
ftp->is_verbose = verbose;
|
1996-06-17 13:26:06 +01:00
|
|
|
|
|
|
|
i = cmd(ftp, "USER %s", user);
|
|
|
|
if (i >= 300 && i < 400)
|
|
|
|
i = cmd(ftp, "PASS %s", passwd);
|
|
|
|
if (i >= 299 || i < 0) {
|
|
|
|
ftp_close(ftp);
|
1996-11-14 05:05:26 +00:00
|
|
|
if (i > 0)
|
1997-12-20 04:06:06 +00:00
|
|
|
ftp->error = i;
|
1996-06-17 13:26:06 +01:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1996-09-19 18:28:34 +01:00
|
|
|
ftp_file_op(FTP_t ftp, char *operation, char *file, FILE **fp, char *mode, off_t *seekto)
|
1996-06-17 13:26:06 +01:00
|
|
|
{
|
2000-07-05 20:34:43 +01:00
|
|
|
int i,l,s;
|
1996-06-17 13:26:06 +01:00
|
|
|
char *q;
|
|
|
|
unsigned char addr[64];
|
2000-07-05 20:34:43 +01:00
|
|
|
union sockaddr_cmn {
|
|
|
|
struct sockaddr_in sin4;
|
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
} sin;
|
|
|
|
char *cmdstr;
|
1996-06-17 13:26:06 +01:00
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
return FAILURE;
|
|
|
|
*fp = NULL;
|
|
|
|
|
|
|
|
if (ftp->con_state != isopen)
|
|
|
|
return botch("ftp_file_op", "open");
|
|
|
|
|
1997-01-17 12:51:55 +00:00
|
|
|
if ((s = socket(ftp->addrtype, SOCK_STREAM, 0)) < 0) {
|
1997-12-20 04:06:06 +00:00
|
|
|
ftp->error = errno;
|
1996-06-17 13:26:06 +01:00
|
|
|
return FAILURE;
|
1997-01-17 12:51:55 +00:00
|
|
|
}
|
1996-06-17 13:26:06 +01:00
|
|
|
|
1996-06-22 22:43:56 +01:00
|
|
|
if (ftp->is_passive) {
|
2000-07-05 20:34:43 +01:00
|
|
|
if (ftp->addrtype == AF_INET) {
|
|
|
|
if (ftp->is_verbose)
|
|
|
|
fprintf(stderr, "Sending PASV\n");
|
|
|
|
if (writes(ftp->fd_ctrl, "PASV\r\n")) {
|
|
|
|
ftp_close(ftp);
|
|
|
|
if (FtpTimedOut)
|
|
|
|
ftp->error = FTP_TIMED_OUT;
|
|
|
|
return FTP_TIMED_OUT;
|
|
|
|
}
|
|
|
|
i = get_a_number(ftp, &q);
|
|
|
|
if (check_code(ftp, i, FTP_PASSIVE_HAPPY)) {
|
|
|
|
ftp_close(ftp);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
cmdstr = "PASV";
|
|
|
|
} else {
|
|
|
|
if (ftp->is_verbose)
|
|
|
|
fprintf(stderr, "Sending EPSV\n");
|
|
|
|
if (writes(ftp->fd_ctrl, "EPSV\r\n")) {
|
|
|
|
ftp_close(ftp);
|
|
|
|
if (FtpTimedOut)
|
|
|
|
ftp->error = FTP_TIMED_OUT;
|
|
|
|
return FTP_TIMED_OUT;
|
|
|
|
}
|
|
|
|
i = get_a_number(ftp, &q);
|
|
|
|
if (check_code(ftp, i, FTP_EPASSIVE_HAPPY)) {
|
|
|
|
if (ftp->is_verbose)
|
|
|
|
fprintf(stderr, "Sending LPSV\n");
|
|
|
|
if (writes(ftp->fd_ctrl, "LPSV\r\n")) {
|
|
|
|
ftp_close(ftp);
|
|
|
|
if (FtpTimedOut)
|
|
|
|
ftp->error = FTP_TIMED_OUT;
|
|
|
|
return FTP_TIMED_OUT;
|
|
|
|
}
|
|
|
|
i = get_a_number(ftp, &q);
|
|
|
|
if (check_code(ftp, i, FTP_LPASSIVE_HAPPY)) {
|
|
|
|
ftp_close(ftp);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
cmdstr = "LPSV";
|
|
|
|
} else
|
|
|
|
cmdstr = "EPSV";
|
1996-06-17 21:36:57 +01:00
|
|
|
}
|
2000-07-05 20:34:43 +01:00
|
|
|
if (strcmp(cmdstr, "PASV") == 0 || strcmp(cmdstr, "LPSV") == 0) {
|
2000-07-10 11:00:20 +01:00
|
|
|
while (*q && !isdigit(*q))
|
|
|
|
q++;
|
|
|
|
if (!*q) {
|
|
|
|
ftp_close(ftp);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
q--;
|
2000-07-05 20:34:43 +01:00
|
|
|
l = (ftp->addrtype == AF_INET ? 6 : 21);
|
|
|
|
for (i = 0; i < l; i++) {
|
|
|
|
q++;
|
|
|
|
addr[i] = strtol(q, &q, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
sin.sin4.sin_family = ftp->addrtype;
|
|
|
|
if (ftp->addrtype == AF_INET6) {
|
|
|
|
sin.sin6.sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
bcopy(addr + 2, (char *)&sin.sin6.sin6_addr, 16);
|
|
|
|
bcopy(addr + 19, (char *)&sin.sin6.sin6_port, 2);
|
|
|
|
} else {
|
|
|
|
sin.sin4.sin_len = sizeof(struct sockaddr_in);
|
|
|
|
bcopy(addr, (char *)&sin.sin4.sin_addr, 4);
|
|
|
|
bcopy(addr + 4, (char *)&sin.sin4.sin_port, 2);
|
|
|
|
}
|
|
|
|
} else if (strcmp(cmdstr, "EPSV") == 0) {
|
|
|
|
int port;
|
|
|
|
int sinlen;
|
2000-07-10 11:00:20 +01:00
|
|
|
while (*q && *q != '(') /* ) */
|
|
|
|
q++;
|
|
|
|
if (!*q) {
|
|
|
|
ftp_close(ftp);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
1996-06-17 13:26:06 +01:00
|
|
|
q++;
|
2000-07-05 20:34:43 +01:00
|
|
|
if (sscanf(q, "%c%c%c%d%c", &addr[0], &addr[1], &addr[2],
|
|
|
|
&port, &addr[3]) != 5
|
|
|
|
|| addr[0] != addr[1] || addr[0] != addr[2] || addr[0] != addr[3]) {
|
|
|
|
ftp_close(ftp);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
sinlen = sizeof(sin);
|
|
|
|
if (getpeername(ftp->fd_ctrl, (struct sockaddr *)&sin, &sinlen) < 0) {
|
|
|
|
ftp_close(ftp);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
switch (sin.sin4.sin_family) {
|
|
|
|
case AF_INET:
|
|
|
|
sin.sin4.sin_port = htons(port);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
sin.sin6.sin6_port = htons(port);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ftp_close(ftp);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
1996-06-17 13:26:06 +01:00
|
|
|
}
|
|
|
|
|
2000-07-05 20:34:43 +01:00
|
|
|
if (connect(s, (struct sockaddr *)&sin, sin.sin4.sin_len) < 0) {
|
1996-06-17 13:26:06 +01:00
|
|
|
(void)close(s);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
1996-12-17 20:19:35 +00:00
|
|
|
|
1996-06-17 16:28:08 +01:00
|
|
|
if (seekto && *seekto) {
|
1996-08-31 23:02:18 +01:00
|
|
|
i = cmd(ftp, "REST %d", *seekto);
|
1996-06-17 13:26:06 +01:00
|
|
|
if (i < 0 || FTP_TIMEOUT(i)) {
|
|
|
|
close(s);
|
1997-12-20 04:06:06 +00:00
|
|
|
ftp->error = i;
|
1996-09-19 18:28:34 +01:00
|
|
|
*seekto = (off_t)0;
|
1996-06-17 13:26:06 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i = cmd(ftp, "%s %s", operation, file);
|
|
|
|
if (i < 0 || i > 299) {
|
|
|
|
close(s);
|
1997-12-20 04:06:06 +00:00
|
|
|
ftp->error = i;
|
1996-06-17 13:26:06 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
*fp = fdopen(s, mode);
|
|
|
|
}
|
|
|
|
else {
|
1999-03-30 23:06:08 +01:00
|
|
|
int fd,portrange;
|
|
|
|
|
2000-07-05 20:34:43 +01:00
|
|
|
#ifdef IPV6_PORTRANGE
|
|
|
|
if (ftp->addrtype == AF_INET6) {
|
|
|
|
portrange = IPV6_PORTRANGE_HIGH;
|
|
|
|
if (setsockopt(s, IPPROTO_IPV6, IPV6_PORTRANGE, (char *)
|
|
|
|
&portrange, sizeof(portrange)) < 0) {
|
|
|
|
close(s);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1999-03-30 23:06:08 +01:00
|
|
|
#ifdef IP_PORTRANGE
|
2000-07-05 20:34:43 +01:00
|
|
|
if (ftp->addrtype == AF_INET) {
|
|
|
|
portrange = IP_PORTRANGE_HIGH;
|
|
|
|
if (setsockopt(s, IPPROTO_IP, IP_PORTRANGE, (char *)
|
|
|
|
&portrange, sizeof(portrange)) < 0) {
|
|
|
|
close(s);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
}
|
1999-03-30 23:06:08 +01:00
|
|
|
#endif
|
1996-06-17 13:26:06 +01:00
|
|
|
|
|
|
|
i = sizeof sin;
|
|
|
|
getsockname(ftp->fd_ctrl, (struct sockaddr *)&sin, &i);
|
2000-07-05 20:34:43 +01:00
|
|
|
sin.sin4.sin_port = 0;
|
|
|
|
i = ((struct sockaddr *)&sin)->sa_len;
|
1996-06-17 13:26:06 +01:00
|
|
|
if (bind(s, (struct sockaddr *)&sin, i) < 0) {
|
1996-12-17 20:19:35 +00:00
|
|
|
close(s);
|
1996-06-17 13:26:06 +01:00
|
|
|
return FAILURE;
|
|
|
|
}
|
2000-07-05 20:34:43 +01:00
|
|
|
i = sizeof sin;
|
1996-06-17 13:26:06 +01:00
|
|
|
getsockname(s,(struct sockaddr *)&sin,&i);
|
|
|
|
if (listen(s, 1) < 0) {
|
|
|
|
close(s);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
2000-07-05 20:34:43 +01:00
|
|
|
if (sin.sin4.sin_family == AF_INET) {
|
|
|
|
u_long a;
|
|
|
|
a = ntohl(sin.sin4.sin_addr.s_addr);
|
|
|
|
i = cmd(ftp, "PORT %d,%d,%d,%d,%d,%d",
|
|
|
|
(a >> 24) & 0xff,
|
|
|
|
(a >> 16) & 0xff,
|
|
|
|
(a >> 8) & 0xff,
|
|
|
|
a & 0xff,
|
|
|
|
(ntohs(sin.sin4.sin_port) >> 8) & 0xff,
|
|
|
|
ntohs(sin.sin4.sin_port) & 0xff);
|
|
|
|
if (check_code(ftp, i, FTP_PORT_HAPPY)) {
|
|
|
|
close(s);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#define UC(b) (((int)b)&0xff)
|
|
|
|
char *a;
|
|
|
|
char hname[INET6_ADDRSTRLEN];
|
|
|
|
|
|
|
|
if (getnameinfo((struct sockaddr *)&sin, sin.sin6.sin6_len,
|
|
|
|
hname, sizeof(hname),
|
|
|
|
NULL, 0, NI_NUMERICHOST) != 0) {
|
|
|
|
goto try_lprt;
|
|
|
|
}
|
|
|
|
i = cmd(ftp, "EPRT |%d|%s|%d|", 2, hname,
|
|
|
|
htons(sin.sin6.sin6_port));
|
|
|
|
if (check_code(ftp, i, FTP_PORT_HAPPY)) {
|
|
|
|
try_lprt:
|
|
|
|
a = (char *)&sin.sin6.sin6_addr;
|
|
|
|
i = cmd(ftp,
|
|
|
|
"LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
|
|
|
|
6, 16,
|
|
|
|
UC(a[0]),UC(a[1]),UC(a[2]),UC(a[3]),
|
|
|
|
UC(a[4]),UC(a[5]),UC(a[6]),UC(a[7]),
|
|
|
|
UC(a[8]),UC(a[9]),UC(a[10]),UC(a[11]),
|
|
|
|
UC(a[12]),UC(a[13]),UC(a[14]),UC(a[15]),
|
|
|
|
2,
|
|
|
|
(ntohs(sin.sin4.sin_port) >> 8) & 0xff,
|
|
|
|
ntohs(sin.sin4.sin_port) & 0xff);
|
|
|
|
if (check_code(ftp, i, FTP_PORT_HAPPY)) {
|
|
|
|
close(s);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
1996-06-17 13:26:06 +01:00
|
|
|
}
|
1996-06-17 16:28:08 +01:00
|
|
|
if (seekto && *seekto) {
|
1996-08-31 23:02:18 +01:00
|
|
|
i = cmd(ftp, "REST %d", *seekto);
|
1996-06-17 13:26:06 +01:00
|
|
|
if (i < 0 || FTP_TIMEOUT(i)) {
|
|
|
|
close(s);
|
1997-12-20 04:06:06 +00:00
|
|
|
ftp->error = i;
|
1996-06-17 13:26:06 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
else if (i != 350)
|
1996-09-19 18:28:34 +01:00
|
|
|
*seekto = (off_t)0;
|
1996-06-17 13:26:06 +01:00
|
|
|
}
|
|
|
|
i = cmd(ftp, "%s %s", operation, file);
|
|
|
|
if (i < 0 || i > 299) {
|
|
|
|
close(s);
|
1997-12-20 04:06:06 +00:00
|
|
|
ftp->error = i;
|
1996-06-17 13:26:06 +01:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
fd = accept(s, 0, 0);
|
|
|
|
if (fd < 0) {
|
|
|
|
close(s);
|
1997-12-20 04:06:06 +00:00
|
|
|
ftp->error = 401;
|
1996-06-17 13:26:06 +01:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
*fp = fdopen(fd, mode);
|
|
|
|
}
|
|
|
|
if (*fp)
|
|
|
|
return SUCCESS;
|
|
|
|
else
|
|
|
|
return FAILURE;
|
|
|
|
}
|
2000-07-05 20:34:43 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
ai_unmapped(struct addrinfo *ai)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 *sin6;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
|
|
|
if (ai->ai_family != AF_INET6)
|
|
|
|
return;
|
|
|
|
if (ai->ai_addrlen != sizeof(struct sockaddr_in6) ||
|
|
|
|
sizeof(sin) > ai->ai_addrlen)
|
|
|
|
return;
|
|
|
|
sin6 = (struct sockaddr_in6 *)ai->ai_addr;
|
|
|
|
if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
|
|
|
|
return;
|
|
|
|
|
|
|
|
memset(&sin, 0, sizeof(sin));
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_len = sizeof(struct sockaddr_in);
|
|
|
|
memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
|
|
|
|
sizeof(sin.sin_addr));
|
|
|
|
sin.sin_port = sin6->sin6_port;
|
|
|
|
|
|
|
|
ai->ai_family = AF_INET;
|
|
|
|
memcpy(ai->ai_addr, &sin, sin.sin_len);
|
|
|
|
ai->ai_addrlen = sin.sin_len;
|
|
|
|
}
|