summaryrefslogtreecommitdiff
path: root/lib/libfetch/ftp.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libfetch/ftp.c')
-rw-r--r--lib/libfetch/ftp.c290
1 files changed, 211 insertions, 79 deletions
diff --git a/lib/libfetch/ftp.c b/lib/libfetch/ftp.c
index 78e2de1edf5f..5c28804641c1 100644
--- a/lib/libfetch/ftp.c
+++ b/lib/libfetch/ftp.c
@@ -61,6 +61,7 @@
#include <ctype.h>
#include <errno.h>
+#include <fcntl.h>
#include <netdb.h>
#include <stdarg.h>
#include <stdio.h>
@@ -304,11 +305,124 @@ _ftp_stat(int cd, char *file, struct url_stat *us)
}
/*
+ * I/O functions for FTP
+ */
+struct ftpio {
+ int csd; /* Control socket descriptor */
+ int dsd; /* Data socket descriptor */
+ int dir; /* Direction */
+ int eof; /* EOF reached */
+ int err; /* Error code */
+};
+
+static int _ftp_readfn(void *, char *, int);
+static int _ftp_writefn(void *, const char *, int);
+static fpos_t _ftp_seekfn(void *, fpos_t, int);
+static int _ftp_closefn(void *);
+
+static int
+_ftp_readfn(void *v, char *buf, int len)
+{
+ struct ftpio *io;
+ int r;
+
+ io = (struct ftpio *)v;
+ if (io->csd == -1 || io->dsd == -1 || io->dir == O_WRONLY) {
+ errno = EBADF;
+ return -1;
+ }
+ if (io->err) {
+ errno = io->err;
+ return -1;
+ }
+ if (io->eof)
+ return 0;
+ r = read(io->dsd, buf, len);
+ if (r > 0)
+ return r;
+ if (r == 0) {
+ io->eof = 1;
+ return _ftp_closefn(v);
+ }
+ io->err = errno;
+ return -1;
+}
+
+static int
+_ftp_writefn(void *v, const char *buf, int len)
+{
+ struct ftpio *io;
+ int w;
+
+ io = (struct ftpio *)v;
+ if (io->csd == -1 || io->dsd == -1 || io->dir == O_RDONLY) {
+ errno = EBADF;
+ return -1;
+ }
+ if (io->err) {
+ errno = io->err;
+ return -1;
+ }
+ w = write(io->dsd, buf, len);
+ if (w >= 0)
+ return w;
+ io->err = errno;
+ return -1;
+}
+
+static fpos_t
+_ftp_seekfn(void *v, fpos_t pos, int whence)
+{
+ errno = ESPIPE;
+ return -1;
+}
+
+static int
+_ftp_closefn(void *v)
+{
+ struct ftpio *io;
+
+ io = (struct ftpio *)v;
+ if (io->dir == -1)
+ return 0;
+ if (io->csd == -1 || io->dsd == -1) {
+ errno = EBADF;
+ return -1;
+ }
+ io->err = _ftp_chkerr(io->csd);
+ io->dir = -1;
+ if (close(io->dsd) == -1)
+ return -1;
+ io->dsd = -1;
+ close(io->csd);
+ io->csd = -1;
+ return io->err ? -1 : 0;
+}
+
+static FILE *
+_ftp_setup(int csd, int dsd, int mode)
+{
+ struct ftpio *io;
+ FILE *f;
+
+ if ((io = malloc(sizeof *io)) == NULL)
+ return NULL;
+ io->csd = dup(csd);
+ io->dsd = dsd;
+ io->dir = mode;
+ io->eof = io->err = 0;
+ f = funopen(io, _ftp_readfn, _ftp_writefn, _ftp_seekfn, _ftp_closefn);
+ if (f == NULL)
+ free(io);
+ return f;
+}
+
+/*
* Transfer file
*/
static FILE *
_ftp_transfer(int cd, char *oper, char *file,
- char *mode, off_t offset, char *flags)
+ int mode, off_t offset, char *flags)
{
struct sockaddr_storage sin;
struct sockaddr_in6 *sin6;
@@ -325,8 +439,9 @@ _ftp_transfer(int cd, char *oper, char *file,
verbose = (flags && strchr(flags, 'v'));
/* passive mode */
- if (!pasv && (s = getenv("FTP_PASSIVE_MODE")) != NULL)
- pasv = (strncasecmp(s, "no", 2) != 0);
+ if (!pasv)
+ pasv = ((s = getenv("FTP_PASSIVE_MODE")) == NULL ||
+ strncasecmp(s, "no", 2) != 0);
/* find our own address, bind, and listen */
l = sizeof sin;
@@ -553,7 +668,7 @@ _ftp_transfer(int cd, char *oper, char *file,
sd = d;
}
- if ((df = fdopen(sd, mode)) == NULL)
+ if ((df = _ftp_setup(cd, sd, mode)) == NULL)
goto sysouch;
return df;
@@ -579,7 +694,7 @@ _ftp_default_port(void)
{
struct servent *se;
- if ((se = getservbyname("ftp", "tcp")) != NULL)
+ if ((se = getservbyname(SCHEME_FTP, "tcp")) != NULL)
return ntohs(se->s_port);
return FTP_DEFAULT_PORT;
}
@@ -588,16 +703,16 @@ _ftp_default_port(void)
* Log on to FTP server
*/
static int
-_ftp_connect(char *host, int port, char *user, char *pwd, char *flags)
+_ftp_connect(struct url *url, struct url *purl, char *flags)
{
- int cd, e, pp = 0, direct, verbose;
+ int cd, e, direct, verbose;
#ifdef INET6
int af = AF_UNSPEC;
#else
int af = AF_INET;
#endif
- char *p, *q;
const char *logname;
+ char *user, *pwd;
char localhost[MAXHOSTNAMELEN];
char pbuf[MAXHOSTNAMELEN + MAXLOGNAME + 1];
@@ -608,43 +723,17 @@ _ftp_connect(char *host, int port, char *user, char *pwd, char *flags)
else if ((flags && strchr(flags, '6')))
af = AF_INET6;
+ if (direct)
+ purl = NULL;
+
/* check for proxy */
- if (!direct && (p = getenv("FTP_PROXY")) != NULL && *p) {
- char c = 0;
-
-#ifdef INET6
- if (*p != '[' || (q = strchr(p + 1, ']')) == NULL ||
- (*++q != '\0' && *q != ':'))
-#endif
- q = strchr(p, ':');
- if (q != NULL && *q == ':') {
- if (strspn(q+1, "0123456789") != strlen(q+1) || strlen(q+1) > 5) {
- /* XXX we should emit some kind of warning */
- }
- pp = atoi(q+1);
- if (pp < 1 || pp > 65535) {
- /* XXX we should emit some kind of warning */
- }
- }
- if (!pp)
- pp = _ftp_default_port();
- if (q) {
-#ifdef INET6
- if (q > p && *p == '[' && *(q - 1) == ']') {
- p++;
- q--;
- }
-#endif
- c = *q;
- *q = 0;
- }
- cd = _fetch_connect(p, pp, af, verbose);
- if (q)
- *q = c;
+ if (purl) {
+ /* XXX proxy authentication! */
+ cd = _fetch_connect(purl->host, purl->port, af, verbose);
} else {
/* no proxy, go straight to target */
- cd = _fetch_connect(host, port, af, verbose);
- p = NULL;
+ cd = _fetch_connect(url->host, url->port, af, verbose);
+ purl = NULL;
}
/* check connection */
@@ -656,19 +745,23 @@ _ftp_connect(char *host, int port, char *user, char *pwd, char *flags)
/* expect welcome message */
if ((e = _ftp_chkerr(cd)) != FTP_SERVICE_READY)
goto fouch;
+
+ /* XXX FTP_AUTH, and maybe .netrc */
/* send user name and password */
+ user = url->user;
if (!user || !*user)
user = FTP_ANONYMOUS_USER;
- if (p && port == FTP_DEFAULT_PORT)
- e = _ftp_cmd(cd, "USER %s@%s", user, host);
- else if (p)
- e = _ftp_cmd(cd, "USER %s@%s@%d", user, host, port);
+ if (purl && url->port == FTP_DEFAULT_PORT)
+ e = _ftp_cmd(cd, "USER %s@%s", user, url->host);
+ else if (purl)
+ e = _ftp_cmd(cd, "USER %s@%s@%d", user, url->host, url->port);
else
e = _ftp_cmd(cd, "USER %s", user);
/* did the server request a password? */
if (e == FTP_NEED_PASSWORD) {
+ pwd = url->pwd;
if (!pwd || !*pwd)
pwd = getenv("FTP_PASSWORD");
if (!pwd || !*pwd) {
@@ -734,7 +827,7 @@ _ftp_isconnected(struct url *url)
* Check the cache, reconnect if no luck
*/
static int
-_ftp_cached_connect(struct url *url, char *flags)
+_ftp_cached_connect(struct url *url, struct url *purl, char *flags)
{
int e, cd;
@@ -748,32 +841,40 @@ _ftp_cached_connect(struct url *url, char *flags)
if (_ftp_isconnected(url)) {
e = _ftp_cmd(cached_socket, "NOOP");
if (e == FTP_OK || e == FTP_SYNTAX_ERROR)
- cd = cached_socket;
+ return cached_socket;
}
/* connect to server */
- if (cd == -1) {
- cd = _ftp_connect(url->host, url->port, url->user, url->pwd, flags);
- if (cd == -1)
- return -1;
- if (cached_socket)
- _ftp_disconnect(cached_socket);
- cached_socket = cd;
- memcpy(&cached_host, url, sizeof *url);
- }
-
+ if ((cd = _ftp_connect(url, purl, flags)) == -1)
+ return -1;
+ if (cached_socket)
+ _ftp_disconnect(cached_socket);
+ cached_socket = cd;
+ memcpy(&cached_host, url, sizeof *url);
return cd;
}
/*
- * Check to see if we should use an HTTP proxy instead
+ * Check the proxy settings
*/
-static int
-_ftp_use_http_proxy(void)
+static struct url *
+_ftp_get_proxy(void)
{
+ struct url *purl;
char *p;
-
- return ((p = getenv("HTTP_PROXY")) && *p && !getenv("FTP_PROXY"));
+
+ if (((p = getenv("FTP_PROXY")) || (p = getenv("HTTP_PROXY"))) &&
+ *p && (purl = fetchParseURL(p)) != NULL) {
+ if (!*purl->scheme)
+ strcpy(purl->scheme, SCHEME_FTP);
+ if (!purl->port)
+ purl->port = _ftp_default_port();
+ if (strcasecmp(purl->scheme, SCHEME_FTP) == 0 ||
+ strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
+ return purl;
+ fetchFreeURL(purl);
+ }
+ return NULL;
}
/*
@@ -782,13 +883,22 @@ _ftp_use_http_proxy(void)
FILE *
fetchXGetFTP(struct url *url, struct url_stat *us, char *flags)
{
+ struct url *purl;
int cd;
- if (_ftp_use_http_proxy())
- return fetchXGetHTTP(url, us, flags);
+ /* get the proxy URL, and check if we should use HTTP instead */
+ if (!strchr(flags, 'd') && (purl = _ftp_get_proxy()) != NULL) {
+ if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
+ return _http_request(url, "GET", us, purl, flags);
+ } else {
+ purl = NULL;
+ }
/* connect to server */
- if ((cd = _ftp_cached_connect(url, flags)) == NULL)
+ cd = _ftp_cached_connect(url, purl, flags);
+ if (purl)
+ fetchFreeURL(purl);
+ if (cd == NULL)
return NULL;
/* change directory */
@@ -802,7 +912,7 @@ fetchXGetFTP(struct url *url, struct url_stat *us, char *flags)
return NULL;
/* initiate the transfer */
- return _ftp_transfer(cd, "RETR", url->doc, "r", url->offset, flags);
+ return _ftp_transfer(cd, "RETR", url->doc, O_RDONLY, url->offset, flags);
}
/*
@@ -820,13 +930,23 @@ fetchGetFTP(struct url *url, char *flags)
FILE *
fetchPutFTP(struct url *url, char *flags)
{
+ struct url *purl;
int cd;
- if (_ftp_use_http_proxy())
- return fetchPutHTTP(url, flags);
+ /* get the proxy URL, and check if we should use HTTP instead */
+ if (!strchr(flags, 'd') && (purl = _ftp_get_proxy()) != NULL) {
+ if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
+ /* XXX HTTP PUT is not implemented, so try without the proxy */
+ purl = NULL;
+ } else {
+ purl = NULL;
+ }
/* connect to server */
- if ((cd = _ftp_cached_connect(url, flags)) == NULL)
+ cd = _ftp_cached_connect(url, purl, flags);
+ if (purl)
+ fetchFreeURL(purl);
+ if (cd == NULL)
return NULL;
/* change directory */
@@ -835,7 +955,7 @@ fetchPutFTP(struct url *url, char *flags)
/* initiate the transfer */
return _ftp_transfer(cd, (flags && strchr(flags, 'a')) ? "APPE" : "STOR",
- url->doc, "w", url->offset, flags);
+ url->doc, O_WRONLY, url->offset, flags);
}
/*
@@ -844,15 +964,30 @@ fetchPutFTP(struct url *url, char *flags)
int
fetchStatFTP(struct url *url, struct url_stat *us, char *flags)
{
+ struct url *purl;
int cd;
- if (_ftp_use_http_proxy())
- return fetchStatHTTP(url, us, flags);
+ /* get the proxy URL, and check if we should use HTTP instead */
+ if (!strchr(flags, 'd') && (purl = _ftp_get_proxy()) != NULL) {
+ if (strcasecmp(purl->scheme, SCHEME_HTTP) == 0) {
+ FILE *f;
+
+ if ((f = _http_request(url, "HEAD", us, purl, flags)) == NULL)
+ return -1;
+ fclose(f);
+ return 0;
+ }
+ } else {
+ purl = NULL;
+ }
/* connect to server */
- if ((cd = _ftp_cached_connect(url, flags)) == NULL)
- return -1;
-
+ cd = _ftp_cached_connect(url, purl, flags);
+ if (purl)
+ fetchFreeURL(purl);
+ if (cd == NULL)
+ return NULL;
+
/* change directory */
if (_ftp_cwd(cd, url->doc) == -1)
return -1;
@@ -868,9 +1003,6 @@ extern void warnx(char *, ...);
struct url_ent *
fetchListFTP(struct url *url, char *flags)
{
- if (_ftp_use_http_proxy())
- return fetchListHTTP(url, flags);
-
warnx("fetchListFTP(): not implemented");
return NULL;
}