"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/fetch.c" between
tnftp-20151004.tar.gz and tnftp-20200705.tar.gz

About: tnftp is an enhanced ftp client (prior name "lukemftp").

fetch.c  (tnftp-20151004):fetch.c  (tnftp-20200705)
/* $NetBSD: fetch.c,v 1.21 2015/10/04 04:53:26 lukem Exp $ */ /* $NetBSD: fetch.c,v 1.24 2020/07/04 09:59:07 lukem Exp $ */
/* from NetBSD: fetch.c,v 1.207 2015/09/12 19:38:42 wiz Exp */ /* from NetBSD: fetch.c,v 1.231 2019/04/04 00:36:09 christos Exp *
/
/*- /*-
* Copyright (c) 1997-2015 The NetBSD Foundation, Inc. * Copyright (c) 1997-2015 The NetBSD Foundation, Inc.
* All rights reserved. * All rights reserved.
* *
* This code is derived from software contributed to The NetBSD Foundation * This code is derived from software contributed to The NetBSD Foundation
* by Luke Mewburn. * by Luke Mewburn.
* *
* This code is derived from software contributed to The NetBSD Foundation * This code is derived from software contributed to The NetBSD Foundation
* by Scott Aaron Bamford. * by Scott Aaron Bamford.
skipping to change at line 45 skipping to change at line 45
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "tnftp.h" #include "tnftp.h"
#if 0 /* tnftp */ #if 0 /* tnftp */
#include <sys/cdefs.h> #include <sys/cdefs.h>
#ifndef lint #ifndef lint
__RCSID(" NetBSD: fetch.c,v 1.207 2015/09/12 19:38:42 wiz Exp "); __RCSID(" NetBSD: fetch.c,v 1.231 2019/04/04 00:36:09 christos Exp ");
#endif /* not lint */ #endif /* not lint */
/* /*
* FTP User Program -- Command line file retrieval * FTP User Program -- Command line file retrieval
*/ */
#include <sys/types.h> #include <sys/types.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
skipping to change at line 84 skipping to change at line 84
#endif /* tnftp */ #endif /* tnftp */
#include "ssl.h" #include "ssl.h"
#include "ftp_var.h" #include "ftp_var.h"
#include "version.h" #include "version.h"
typedef enum { typedef enum {
UNKNOWN_URL_T=-1, UNKNOWN_URL_T=-1,
HTTP_URL_T, HTTP_URL_T,
#ifdef WITH_SSL
HTTPS_URL_T, HTTPS_URL_T,
#endif
FTP_URL_T, FTP_URL_T,
FILE_URL_T, FILE_URL_T,
CLASSIC_URL_T CLASSIC_URL_T
} url_t; } url_t;
struct authinfo {
char *auth;
char *user;
char *pass;
};
struct urlinfo {
char *host;
char *port;
char *path;
url_t utype;
in_port_t portnum;
};
struct posinfo {
off_t rangestart;
off_t rangeend;
off_t entitylen;
};
__dead static void aborthttp(int); __dead static void aborthttp(int);
__dead static void timeouthttp(int); __dead static void timeouthttp(int);
#ifndef NO_AUTH #ifndef NO_AUTH
static int auth_url(const char *, char **, const char *, const char *); static int auth_url(const char *, char **, const struct authinfo *);
static void base64_encode(const unsigned char *, size_t, unsigned char *); static void base64_encode(const unsigned char *, size_t, unsigned char *);
#endif #endif
static int go_fetch(const char *); static int go_fetch(const char *);
static int fetch_ftp(const char *); static int fetch_ftp(const char *);
static int fetch_url(const char *, const char *, char *, char *); static int fetch_url(const char *, const char *, char *, char *);
static const char *match_token(const char **, const char *); static const char *match_token(const char **, const char *);
static int parse_url(const char *, const char *, url_t *, char **, static int parse_url(const char *, const char *, struct urlinfo *,
char **, char **, char **, in_port_t *, char **); struct authinfo *);
static void url_decode(char *); static void url_decode(char *);
static void freeauthinfo(struct authinfo *);
static void freeurlinfo(struct urlinfo *);
static int redirect_loop; static int redirect_loop;
#define STRNEQUAL(a,b) (strncasecmp((a), (b), sizeof((b))-1) == 0) #define STRNEQUAL(a,b) (strncasecmp((a), (b), sizeof((b))-1) == 0)
#define ISLWS(x) ((x)=='\r' || (x)=='\n' || (x)==' ' || (x)=='\t') #define ISLWS(x) ((x)=='\r' || (x)=='\n' || (x)==' ' || (x)=='\t')
#define SKIPLWS(x) do { while (ISLWS((*x))) x++; } while (0) #define SKIPLWS(x) do { while (ISLWS((*x))) x++; } while (0)
#define ABOUT_URL "about:" /* propaganda */ #define ABOUT_URL "about:" /* propaganda */
#define FILE_URL "file://" /* file URL prefix */ #define FILE_URL "file://" /* file URL prefix */
#define FTP_URL "ftp://" /* ftp URL prefix */ #define FTP_URL "ftp://" /* ftp URL prefix */
skipping to change at line 154 skipping to change at line 174
return NULL; return NULL;
p += tlen; p += tlen;
if (*p != '\0' && !ISLWS(*p)) if (*p != '\0' && !ISLWS(*p))
return NULL; return NULL;
SKIPLWS(p); SKIPLWS(p);
orig = *buf; orig = *buf;
*buf = p; *buf = p;
return orig; return orig;
} }
static void
initposinfo(struct posinfo *pi)
{
pi->rangestart = pi->rangeend = pi->entitylen = -1;
}
static void
initauthinfo(struct authinfo *ai, char *auth)
{
ai->auth = auth;
ai->user = ai->pass = 0;
}
static void
freeauthinfo(struct authinfo *a)
{
FREEPTR(a->user);
if (a->pass != NULL)
memset(a->pass, 0, strlen(a->pass));
FREEPTR(a->pass);
}
static void
initurlinfo(struct urlinfo *ui)
{
ui->host = ui->port = ui->path = 0;
ui->utype = UNKNOWN_URL_T;
ui->portnum = 0;
}
static void
copyurlinfo(struct urlinfo *dui, struct urlinfo *sui)
{
dui->host = ftp_strdup(sui->host);
dui->port = ftp_strdup(sui->port);
dui->path = ftp_strdup(sui->path);
dui->utype = sui->utype;
dui->portnum = sui->portnum;
}
static void
freeurlinfo(struct urlinfo *ui)
{
FREEPTR(ui->host);
FREEPTR(ui->port);
FREEPTR(ui->path);
}
#ifndef NO_AUTH #ifndef NO_AUTH
/* /*
* Generate authorization response based on given authentication challenge. * Generate authorization response based on given authentication challenge.
* Returns -1 if an error occurred, otherwise 0. * Returns -1 if an error occurred, otherwise 0.
* Sets response to a malloc(3)ed string; caller should free. * Sets response to a malloc(3)ed string; caller should free.
*/ */
static int static int
auth_url(const char *challenge, char **response, const char *guser, auth_url(const char *challenge, char **response, const struct authinfo *auth)
const char *gpass)
{ {
const char *cp, *scheme, *errormsg; const char *cp, *scheme, *errormsg;
char *ep, *clear, *realm; char *ep, *clear, *realm;
char uuser[BUFSIZ], *gotpass; char uuser[BUFSIZ], *gotpass;
const char *upass; const char *upass;
int rval; int rval;
size_t len, clen, rlen; size_t len, clen, rlen;
*response = NULL; *response = NULL;
clear = realm = NULL; clear = realm = NULL;
skipping to change at line 206 skipping to change at line 273
len = ep - cp; len = ep - cp;
realm = (char *)ftp_malloc(len + 1); realm = (char *)ftp_malloc(len + 1);
(void)strlcpy(realm, cp, len + 1); (void)strlcpy(realm, cp, len + 1);
} else { } else {
warnx("Unsupported authentication challenge `%s'", warnx("Unsupported authentication challenge `%s'",
challenge); challenge);
goto cleanup_auth_url; goto cleanup_auth_url;
} }
fprintf(ttyout, "Username for `%s': ", realm); fprintf(ttyout, "Username for `%s': ", realm);
if (guser != NULL) { if (auth->user != NULL) {
(void)strlcpy(uuser, guser, sizeof(uuser)); (void)strlcpy(uuser, auth->user, sizeof(uuser));
fprintf(ttyout, "%s\n", uuser); fprintf(ttyout, "%s\n", uuser);
} else { } else {
(void)fflush(ttyout); (void)fflush(ttyout);
if (get_line(stdin, uuser, sizeof(uuser), &errormsg) < 0) { if (get_line(stdin, uuser, sizeof(uuser), &errormsg) < 0) {
warnx("%s; can't authenticate", errormsg); warnx("%s; can't authenticate", errormsg);
goto cleanup_auth_url; goto cleanup_auth_url;
} }
} }
if (gpass != NULL) if (auth->pass != NULL)
upass = gpass; upass = auth->pass;
else { else {
gotpass = getpass("Password: "); gotpass = getpass("Password: ");
if (gotpass == NULL) { if (gotpass == NULL) {
warnx("Can't read password"); warnx("Can't read password");
goto cleanup_auth_url; goto cleanup_auth_url;
} }
upass = gotpass; upass = gotpass;
} }
clen = strlen(uuser) + strlen(upass) + 2; /* user + ":" + pass + "\ 0" */ clen = strlen(uuser) + strlen(upass) + 2; /* user + ":" + pass + "\ 0" */
clear = (char *)ftp_malloc(clen); clear = (char *)ftp_malloc(clen);
(void)strlcpy(clear, uuser, clen); (void)strlcpy(clear, uuser, clen);
(void)strlcat(clear, ":", clen); (void)strlcat(clear, ":", clen);
(void)strlcat(clear, upass, clen); (void)strlcat(clear, upass, clen);
if (gotpass) if (gotpass)
memset(gotpass, 0, strlen(gotpass)); memset(gotpass, 0, strlen(gotpass));
/* scheme + " " + enc + "\0" */ /* scheme + " " + enc + "\0" */
rlen = strlen(scheme) + 1 + (clen + 2) * 4 / 3 + 1; rlen = strlen(scheme) + 1 + (clen + 2) * 4 / 3 + 1;
*response = (char *)ftp_malloc(rlen); *response = ftp_malloc(rlen);
(void)strlcpy(*response, scheme, rlen); (void)strlcpy(*response, scheme, rlen);
len = strlcat(*response, " ", rlen); len = strlcat(*response, " ", rlen);
/* use `clen - 1' to not encode the trailing NUL */ /* use `clen - 1' to not encode the trailing NUL */
base64_encode((unsigned char *)clear, clen - 1, base64_encode((unsigned char *)clear, clen - 1,
(unsigned char *)*response + len); (unsigned char *)*response + len);
memset(clear, 0, clen); memset(clear, 0, clen);
rval = 0; rval = 0;
cleanup_auth_url: cleanup_auth_url:
FREEPTR(clear); FREEPTR(clear);
skipping to change at line 335 skipping to change at line 402
* "http://host" "/" * "http://host" "/"
* "http://host/" "/" * "http://host/" "/"
* "http://host/path" "/path" * "http://host/path" "/path"
* "file://host/dir/file" "dir/file" * "file://host/dir/file" "dir/file"
* "ftp://host" "" * "ftp://host" ""
* "ftp://host/" "" * "ftp://host/" ""
* "ftp://host//" "/" * "ftp://host//" "/"
* "ftp://host/dir/file" "dir/file" * "ftp://host/dir/file" "dir/file"
* "ftp://host//dir/file" "/dir/file" * "ftp://host//dir/file" "/dir/file"
*/ */
static int static int
parse_url(const char *url, const char *desc, url_t *utype, parse_url(const char *url, const char *desc, struct urlinfo *ui,
char **uuser, char **pass, char **host, char **port, struct authinfo *auth)
in_port_t *portnum, char **path)
{ {
const char *origurl, *tport; const char *origurl, *tport;
char *cp, *ep, *thost; char *cp, *ep, *thost;
size_t len; size_t len;
if (url == NULL || desc == NULL || utype == NULL || uuser == NULL if (url == NULL || desc == NULL || ui == NULL || auth == NULL)
|| pass == NULL || host == NULL || port == NULL || portnum == NULL
|| path == NULL)
errx(1, "parse_url: invoked with NULL argument!"); errx(1, "parse_url: invoked with NULL argument!");
DPRINTF("parse_url: %s `%s'\n", desc, url); DPRINTF("parse_url: %s `%s'\n", desc, url);
origurl = url; origurl = url;
*utype = UNKNOWN_URL_T;
*uuser = *pass = *host = *port = *path = NULL;
*portnum = 0;
tport = NULL; tport = NULL;
if (STRNEQUAL(url, HTTP_URL)) { if (STRNEQUAL(url, HTTP_URL)) {
url += sizeof(HTTP_URL) - 1; url += sizeof(HTTP_URL) - 1;
*utype = HTTP_URL_T; ui->utype = HTTP_URL_T;
*portnum = HTTP_PORT; ui->portnum = HTTP_PORT;
tport = httpport; tport = httpport;
} else if (STRNEQUAL(url, FTP_URL)) { } else if (STRNEQUAL(url, FTP_URL)) {
url += sizeof(FTP_URL) - 1; url += sizeof(FTP_URL) - 1;
*utype = FTP_URL_T; ui->utype = FTP_URL_T;
*portnum = FTP_PORT; ui->portnum = FTP_PORT;
tport = ftpport; tport = ftpport;
} else if (STRNEQUAL(url, FILE_URL)) { } else if (STRNEQUAL(url, FILE_URL)) {
url += sizeof(FILE_URL) - 1; url += sizeof(FILE_URL) - 1;
*utype = FILE_URL_T; ui->utype = FILE_URL_T;
tport = "";
#ifdef WITH_SSL #ifdef WITH_SSL
} else if (STRNEQUAL(url, HTTPS_URL)) { } else if (STRNEQUAL(url, HTTPS_URL)) {
url += sizeof(HTTPS_URL) - 1; url += sizeof(HTTPS_URL) - 1;
*utype = HTTPS_URL_T; ui->utype = HTTPS_URL_T;
*portnum = HTTPS_PORT; ui->portnum = HTTPS_PORT;
tport = httpsport; tport = httpsport;
#endif #endif
} else { } else {
warnx("Invalid %s `%s'", desc, url); warnx("Invalid %s `%s'", desc, url);
cleanup_parse_url: cleanup_parse_url:
FREEPTR(*uuser); freeauthinfo(auth);
if (*pass != NULL) freeurlinfo(ui);
memset(*pass, 0, strlen(*pass));
FREEPTR(*pass);
FREEPTR(*host);
FREEPTR(*port);
FREEPTR(*path);
return (-1); return (-1);
} }
if (*url == '\0') if (*url == '\0')
return (0); return (0);
/* find [user[:pass]@]host[:port] */ /* find [user[:pass]@]host[:port] */
ep = strchr(url, '/'); ep = strchr(url, '/');
if (ep == NULL) if (ep == NULL)
thost = ftp_strdup(url); thost = ftp_strdup(url);
else { else {
len = ep - url; len = ep - url;
thost = (char *)ftp_malloc(len + 1); thost = (char *)ftp_malloc(len + 1);
(void)strlcpy(thost, url, len + 1); (void)strlcpy(thost, url, len + 1);
if (*utype == FTP_URL_T) /* skip first / for ftp URLs */ if (ui->utype == FTP_URL_T) /* skip first / for ftp URLs */
ep++; ep++;
*path = ftp_strdup(ep); ui->path = ftp_strdup(ep);
} }
cp = strchr(thost, '@'); /* look for user[:pass]@ in URLs */ cp = strchr(thost, '@'); /* look for user[:pass]@ in URLs */
if (cp != NULL) { if (cp != NULL) {
if (*utype == FTP_URL_T) if (ui->utype == FTP_URL_T)
anonftp = 0; /* disable anonftp */ anonftp = 0; /* disable anonftp */
*uuser = thost; auth->user = thost;
*cp = '\0'; *cp = '\0';
thost = ftp_strdup(cp + 1); thost = ftp_strdup(cp + 1);
cp = strchr(*uuser, ':'); cp = strchr(auth->user, ':');
if (cp != NULL) { if (cp != NULL) {
*cp = '\0'; *cp = '\0';
*pass = ftp_strdup(cp + 1); auth->pass = ftp_strdup(cp + 1);
} }
url_decode(*uuser); url_decode(auth->user);
if (*pass) if (auth->pass)
url_decode(*pass); url_decode(auth->pass);
} }
#ifdef INET6 #ifdef INET6
/* /*
* Check if thost is an encoded IPv6 address, as per * Check if thost is an encoded IPv6 address, as per
* RFC 3986: * RFC 3986:
* `[' ipv6-address ']' * `[' ipv6-address ']'
*/ */
if (*thost == '[') { if (*thost == '[') {
cp = thost + 1; cp = thost + 1;
skipping to change at line 453 skipping to change at line 511
} }
cp = ep + 1; cp = ep + 1;
if (*cp == ':') if (*cp == ':')
cp++; cp++;
else else
cp = NULL; cp = NULL;
} else } else
#endif /* INET6 */ #endif /* INET6 */
if ((cp = strchr(thost, ':')) != NULL) if ((cp = strchr(thost, ':')) != NULL)
*cp++ = '\0'; *cp++ = '\0';
*host = thost; ui->host = thost;
/* look for [:port] */ /* look for [:port] */
if (cp != NULL) { if (cp != NULL) {
unsigned long nport; unsigned long nport;
nport = strtoul(cp, &ep, 10); nport = strtoul(cp, &ep, 10);
if (*cp == '\0' || *ep != '\0' || if (*cp == '\0' || *ep != '\0' ||
nport < 1 || nport > MAX_IN_PORT_T) { nport < 1 || nport > MAX_IN_PORT_T) {
warnx("Unknown port `%s' in %s `%s'", warnx("Unknown port `%s' in %s `%s'",
cp, desc, origurl); cp, desc, origurl);
goto cleanup_parse_url; goto cleanup_parse_url;
} }
*portnum = nport; ui->portnum = nport;
tport = cp; tport = cp;
} }
if (tport != NULL) if (tport != NULL)
*port = ftp_strdup(tport); ui->port = ftp_strdup(tport);
if (*path == NULL) { if (ui->path == NULL) {
const char *emptypath = "/"; const char *emptypath = "/";
if (*utype == FTP_URL_T) /* skip first / for ftp URLs */ if (ui->utype == FTP_URL_T) /* skip first / for ftp URLs */
emptypath++; emptypath++;
*path = ftp_strdup(emptypath); ui->path = ftp_strdup(emptypath);
} }
DPRINTF("parse_url: user `%s' pass `%s' host %s port %s(%d) " DPRINTF("parse_url: user `%s' pass `%s' host %s port %s(%d) "
"path `%s'\n", "path `%s'\n",
STRorNULL(*uuser), STRorNULL(*pass), STRorNULL(auth->user), STRorNULL(auth->pass),
STRorNULL(*host), STRorNULL(*port), STRorNULL(ui->host), STRorNULL(ui->port),
*portnum ? *portnum : -1, STRorNULL(*path)); ui->portnum ? ui->portnum : -1, STRorNULL(ui->path));
return (0); return (0);
} }
sigjmp_buf httpabort; sigjmp_buf httpabort;
static int
ftp_socket(const struct urlinfo *ui, void **ssl)
{
struct addrinfo hints, *res, *res0 = NULL;
int error;
int s;
const char *host = ui->host;
const char *port = ui->port;
if (ui->utype != HTTPS_URL_T)
ssl = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = 0;
hints.ai_family = family;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
error = getaddrinfo(host, port, &hints, &res0);
if (error) {
warnx("Can't LOOKUP `%s:%s': %s", host, port,
(error == EAI_SYSTEM) ? strerror(errno)
: gai_strerror(error));
return -1;
}
if (res0->ai_canonname)
host = res0->ai_canonname;
s = -1;
if (ssl)
*ssl = NULL;
for (res = res0; res; res = res->ai_next) {
char hname[NI_MAXHOST], sname[NI_MAXSERV];
ai_unmapped(res);
if (getnameinfo(res->ai_addr, res->ai_addrlen,
hname, sizeof(hname), sname, sizeof(sname),
NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
strlcpy(hname, "?", sizeof(hname));
strlcpy(sname, "?", sizeof(sname));
}
if (verbose && res0->ai_next) {
#ifdef INET6
if(res->ai_family == AF_INET6) {
fprintf(ttyout, "Trying [%s]:%s ...\n",
hname, sname);
} else {
#endif
fprintf(ttyout, "Trying %s:%s ...\n",
hname, sname);
#ifdef INET6
}
#endif
}
s = socket(res->ai_family, SOCK_STREAM, res->ai_protocol);
if (s < 0) {
warn(
"Can't create socket for connection to "
"`%s:%s'", hname, sname);
continue;
}
if (ftp_connect(s, res->ai_addr, res->ai_addrlen,
verbose || !res->ai_next) < 0) {
close(s);
s = -1;
continue;
}
#ifdef WITH_SSL
if (ssl) {
if ((*ssl = fetch_start_ssl(s, host)) == NULL) {
close(s);
s = -1;
continue;
}
}
#endif
break;
}
if (res0)
freeaddrinfo(res0);
return s;
}
static int
handle_noproxy(const char *host, in_port_t portnum)
{
char *cp, *ep, *np, *np_copy, *np_iter, *no_proxy;
unsigned long np_port;
size_t hlen, plen;
int isproxy = 1;
/* check URL against list of no_proxied sites */
no_proxy = getoptionvalue("no_proxy");
if (EMPTYSTRING(no_proxy))
return isproxy;
np_iter = np_copy = ftp_strdup(no_proxy);
hlen = strlen(host);
while ((cp = strsep(&np_iter, " ,")) != NULL) {
if (*cp == '\0')
continue;
if ((np = strrchr(cp, ':')) != NULL) {
*np++ = '\0';
np_port = strtoul(np, &ep, 10);
if (*np == '\0' || *ep != '\0')
continue;
if (np_port != portnum)
continue;
}
plen = strlen(cp);
if (hlen < plen)
continue;
if (strncasecmp(host + hlen - plen, cp, plen) == 0) {
isproxy = 0;
break;
}
}
FREEPTR(np_copy);
return isproxy;
}
static int
handle_proxy(const char *url, const char *penv, struct urlinfo *ui,
struct authinfo *pauth)
{
struct urlinfo pui;
if (isipv6addr(ui->host) && strchr(ui->host, '%') != NULL) {
warnx("Scoped address notation `%s' disallowed via web proxy",
ui->host);
return -1;
}
initurlinfo(&pui);
if (parse_url(penv, "proxy URL", &pui, pauth) == -1)
return -1;
if ((!IS_HTTP_TYPE(pui.utype) && pui.utype != FTP_URL_T) ||
EMPTYSTRING(pui.host) ||
(! EMPTYSTRING(pui.path) && strcmp(pui.path, "/") != 0)) {
warnx("Malformed proxy URL `%s'", penv);
freeurlinfo(&pui);
return -1;
}
FREEPTR(pui.path);
pui.path = ftp_strdup(url);
freeurlinfo(ui);
*ui = pui;
return 0;
}
static void
print_host(FETCH *fin, const struct urlinfo *ui)
{
char *h, *p;
if (strchr(ui->host, ':') == NULL) {
fetch_printf(fin, "Host: %s", ui->host);
} else {
/*
* strip off IPv6 scope identifier, since it is
* local to the node
*/
h = ftp_strdup(ui->host);
if (isipv6addr(h) && (p = strchr(h, '%')) != NULL)
*p = '\0';
fetch_printf(fin, "Host: [%s]", h);
free(h);
}
if ((ui->utype == HTTP_URL_T && ui->portnum != HTTP_PORT) ||
(ui->utype == HTTPS_URL_T && ui->portnum != HTTPS_PORT))
fetch_printf(fin, ":%u", ui->portnum);
fetch_printf(fin, "\r\n");
}
static void
print_agent(FETCH *fin)
{
const char *useragent;
if ((useragent = getenv("FTPUSERAGENT")) != NULL) {
fetch_printf(fin, "User-Agent: %s\r\n", useragent);
} else {
fetch_printf(fin, "User-Agent: %s/%s\r\n",
FTP_PRODUCT, FTP_VERSION);
}
}
static void
print_cache(FETCH *fin, int isproxy)
{
fetch_printf(fin, isproxy ?
"Pragma: no-cache\r\n" :
"Cache-Control: no-cache\r\n");
}
static int
print_get(FETCH *fin, int hasleading, int isproxy, const struct urlinfo *oui,
const struct urlinfo *ui)
{
const char *leading = hasleading ? ", " : " (";
if (isproxy) {
if (verbose) {
fprintf(ttyout, "%svia %s:%u", leading,
ui->host, ui->portnum);
leading = ", ";
hasleading++;
}
fetch_printf(fin, "GET %s HTTP/1.0\r\n", ui->path);
print_host(fin, oui);
return hasleading;
}
fetch_printf(fin, "GET %s HTTP/1.1\r\n", ui->path);
print_host(fin, ui);
fetch_printf(fin, "Accept: */*\r\n");
fetch_printf(fin, "Connection: close\r\n");
if (restart_point) {
fputs(leading, ttyout);
fetch_printf(fin, "Range: bytes=" LLF "-\r\n",
(LLT)restart_point);
fprintf(ttyout, "restarting at " LLF, (LLT)restart_point);
hasleading++;
}
return hasleading;
}
static void
getmtime(const char *cp, time_t *mtime)
{
struct tm parsed;
const char *t;
memset(&parsed, 0, sizeof(parsed));
t = parse_rfc2616time(&parsed, cp);
if (t == NULL)
return;
parsed.tm_isdst = -1;
if (*t == '\0')
*mtime = timegm(&parsed);
#ifndef NO_DEBUG
if (ftp_debug && *mtime != -1) {
fprintf(ttyout, "parsed time as: %s",
rfc2822time(localtime(mtime)));
}
#endif
}
static int
print_proxy(FETCH *fin, int hasleading, const char *wwwauth,
const char *proxyauth)
{
const char *leading = hasleading ? ", " : " (";
if (wwwauth) {
if (verbose) {
fprintf(ttyout, "%swith authorization", leading);
hasleading++;
}
fetch_printf(fin, "Authorization: %s\r\n", wwwauth);
}
if (proxyauth) {
if (verbose) {
fprintf(ttyout, "%swith proxy authorization", leading);
hasleading++;
}
fetch_printf(fin, "Proxy-Authorization: %s\r\n", proxyauth);
}
return hasleading;
}
#ifdef WITH_SSL
static void
print_connect(FETCH *fin, const struct urlinfo *ui)
{
char hname[NI_MAXHOST], *p;
const char *h;
if (isipv6addr(ui->host)) {
/*
* strip off IPv6 scope identifier,
* since it is local to the node
*/
if ((p = strchr(ui->host, '%')) == NULL)
snprintf(hname, sizeof(hname), "[%s]", ui->host);
else
snprintf(hname, sizeof(hname), "[%.*s]",
(int)(p - ui->host), ui->host);
h = hname;
} else
h = ui->host;
fetch_printf(fin, "CONNECT %s:%d HTTP/1.1\r\n", h, ui->portnum);
fetch_printf(fin, "Host: %s:%d\r\n", h, ui->portnum);
}
#endif
#define C_OK 0
#define C_CLEANUP 1
#define C_IMPROPER 2
static int
getresponseline(FETCH *fin, char *buf, size_t buflen, int *len)
{
const char *errormsg;
alarmtimer(quit_time ? quit_time : 60);
*len = fetch_getline(fin, buf, buflen, &errormsg);
alarmtimer(0);
if (*len < 0) {
if (*errormsg == '\n')
errormsg++;
warnx("Receiving HTTP reply: %s", errormsg);
return C_CLEANUP;
}
while (*len > 0 && (ISLWS(buf[*len-1])))
buf[--*len] = '\0';
if (*len)
DPRINTF("%s: received `%s'\n", __func__, buf);
return C_OK;
}
static int
getresponse(FETCH *fin, char **cp, size_t buflen, int *hcode)
{
int len, rv;
char *ep, *buf = *cp;
*hcode = 0;
if ((rv = getresponseline(fin, buf, buflen, &len)) != C_OK)
return rv;
/* Determine HTTP response code */
*cp = strchr(buf, ' ');
if (*cp == NULL)
return C_IMPROPER;
(*cp)++;
*hcode = strtol(*cp, &ep, 10);
if (*ep != '\0' && !isspace((unsigned char)*ep))
return C_IMPROPER;
return C_OK;
}
static int
parse_posinfo(const char **cp, struct posinfo *pi)
{
char *ep;
if (!match_token(cp, "bytes"))
return -1;
if (**cp == '*')
(*cp)++;
else {
pi->rangestart = STRTOLL(*cp, &ep, 10);
if (pi->rangestart < 0 || *ep != '-')
return -1;
*cp = ep + 1;
pi->rangeend = STRTOLL(*cp, &ep, 10);
if (pi->rangeend < 0 || pi->rangeend < pi->rangestart)
return -1;
*cp = ep;
}
if (**cp != '/')
return -1;
(*cp)++;
if (**cp == '*')
(*cp)++;
else {
pi->entitylen = STRTOLL(*cp, &ep, 10);
if (pi->entitylen < 0)
return -1;
*cp = ep;
}
if (**cp != '\0')
return -1;
#ifndef NO_DEBUG
if (ftp_debug) {
fprintf(ttyout, "parsed range as: ");
if (pi->rangestart == -1)
fprintf(ttyout, "*");
else
fprintf(ttyout, LLF "-" LLF, (LLT)pi->rangestart,
(LLT)pi->rangeend);
fprintf(ttyout, "/" LLF "\n", (LLT)pi->entitylen);
}
#endif
return 0;
}
#ifndef NO_AUTH
static void
do_auth(int hcode, const char *url, const char *penv, struct authinfo *wauth,
struct authinfo *pauth, char **auth, const char *message,
volatile int *rval)
{
struct authinfo aauth;
char *response;
if (hcode == 401)
aauth = *wauth;
else
aauth = *pauth;
if (verbose || aauth.auth == NULL ||
aauth.user == NULL || aauth.pass == NULL)
fprintf(ttyout, "%s\n", message);
if (EMPTYSTRING(*auth)) {
warnx("No authentication challenge provided by server");
return;
}
if (aauth.auth != NULL) {
char reply[10];
fprintf(ttyout, "Authorization failed. Retry (y/n)? ");
if (get_line(stdin, reply, sizeof(reply), NULL) < 0) {
return;
}
if (tolower((unsigned char)reply[0]) != 'y')
return;
aauth.user = NULL;
aauth.pass = NULL;
}
if (auth_url(*auth, &response, &aauth) == 0) {
*rval = fetch_url(url, penv,
hcode == 401 ? pauth->auth : response,
hcode == 401 ? response: wauth->auth);
memset(response, 0, strlen(response));
FREEPTR(response);
}
}
#endif
static int
negotiate_connection(FETCH *fin, const char *url, const char *penv,
struct posinfo *pi, time_t *mtime, struct authinfo *wauth,
struct authinfo *pauth, volatile int *rval, volatile int *ischunked,
char **auth)
{
int len, hcode, rv;
char buf[FTPBUFLEN], *ep;
const char *cp, *token;
char *location, *message;
*auth = message = location = NULL;
/* Read the response */
ep = buf;
switch (getresponse(fin, &ep, sizeof(buf), &hcode)) {
case C_CLEANUP:
goto cleanup_fetch_url;
case C_IMPROPER:
goto improper;
case C_OK:
message = ftp_strdup(ep);
break;
}
/* Read the rest of the header. */
for (;;) {
if ((rv = getresponseline(fin, buf, sizeof(buf), &len)) != C_OK)
goto cleanup_fetch_url;
if (len == 0)
break;
/*
* Look for some headers
*/
cp = buf;
if (match_token(&cp, "Content-Length:")) {
filesize = STRTOLL(cp, &ep, 10);
if (filesize < 0 || *ep != '\0')
goto improper;
DPRINTF("%s: parsed len as: " LLF "\n",
__func__, (LLT)filesize);
} else if (match_token(&cp, "Content-Range:")) {
if (parse_posinfo(&cp, pi) == -1)
goto improper;
if (! restart_point) {
warnx(
"Received unexpected Content-Range header");
goto cleanup_fetch_url;
}
} else if (match_token(&cp, "Last-Modified:")) {
getmtime(cp, mtime);
} else if (match_token(&cp, "Location:")) {
location = ftp_strdup(cp);
DPRINTF("%s: parsed location as `%s'\n",
__func__, cp);
} else if (match_token(&cp, "Transfer-Encoding:")) {
if (match_token(&cp, "binary")) {
warnx(
"Bogus transfer encoding `binary' (fetching anyway)");
continue;
}
if (! (token = match_token(&cp, "chunked"))) {
warnx(
"Unsupported transfer encoding `%s'",
token);
goto cleanup_fetch_url;
}
(*ischunked)++;
DPRINTF("%s: using chunked encoding\n",
__func__);
} else if (match_token(&cp, "Proxy-Authenticate:")
|| match_token(&cp, "WWW-Authenticate:")) {
if (! (token = match_token(&cp, "Basic"))) {
DPRINTF("%s: skipping unknown auth "
"scheme `%s'\n", __func__, token);
continue;
}
FREEPTR(*auth);
*auth = ftp_strdup(token);
DPRINTF("%s: parsed auth as `%s'\n",
__func__, cp);
}
}
/* finished parsing header */
switch (hcode) {
case 200:
break;
case 206:
if (! restart_point) {
warnx("Not expecting partial content header");
goto cleanup_fetch_url;
}
break;
case 300:
case 301:
case 302:
case 303:
case 305:
case 307:
if (EMPTYSTRING(location)) {
warnx(
"No redirection Location provided by server");
goto cleanup_fetch_url;
}
if (redirect_loop++ > 5) {
warnx("Too many redirections requested");
goto cleanup_fetch_url;
}
if (hcode == 305) {
if (verbose)
fprintf(ttyout, "Redirected via %s\n",
location);
*rval = fetch_url(url, location,
pauth->auth, wauth->auth);
} else {
if (verbose)
fprintf(ttyout, "Redirected to %s\n",
location);
*rval = go_fetch(location);
}
goto cleanup_fetch_url;
#ifndef NO_AUTH
case 401:
case 407:
do_auth(hcode, url, penv, wauth, pauth, auth, message, rval);
goto cleanup_fetch_url;
#endif
default:
if (message)
warnx("Error retrieving file `%s'", message);
else
warnx("Unknown error retrieving file");
goto cleanup_fetch_url;
}
rv = C_OK;
goto out;
cleanup_fetch_url:
rv = C_CLEANUP;
goto out;
improper:
rv = C_IMPROPER;
goto out;
out:
FREEPTR(message);
FREEPTR(location);
return rv;
} /* end of ftp:// or http:// specific setup */
#ifdef WITH_SSL
static int
connectmethod(FETCH *fin, const char *url, const char *penv,
struct urlinfo *oui, struct urlinfo *ui, struct authinfo *wauth,
struct authinfo *pauth, char **auth, int *hasleading, volatile int *rval)
{
void *ssl;
int hcode, rv;
const char *cp;
char buf[FTPBUFLEN], *ep;
char *message = NULL;
print_connect(fin, oui);
print_agent(fin);
*hasleading = print_proxy(fin, *hasleading, NULL, pauth->auth);
if (verbose && *hasleading)
fputs(")\n", ttyout);
*hasleading = 0;
fetch_printf(fin, "\r\n");
if (fetch_flush(fin) == EOF) {
warn("Writing HTTP request");
alarmtimer(0);
goto cleanup_fetch_url;
}
alarmtimer(0);
/* Read the response */
ep = buf;
switch (getresponse(fin, &ep, sizeof(buf), &hcode)) {
case C_CLEANUP:
goto cleanup_fetch_url;
case C_IMPROPER:
goto improper;
case C_OK:
message = ftp_strdup(ep);
break;
}
for (;;) {
int len;
if (getresponseline(fin, buf, sizeof(buf), &len) != C_OK)
goto cleanup_fetch_url;
if (len == 0)
break;
cp = buf;
if (match_token(&cp, "Proxy-Authenticate:")) {
const char *token;
if (!(token = match_token(&cp, "Basic"))) {
DPRINTF(
"%s: skipping unknown auth scheme `%s'\n",
__func__, token);
continue;
}
FREEPTR(*auth);
*auth = ftp_strdup(token);
DPRINTF("%s: parsed auth as " "`%s'\n", __func__, cp);
}
}
/* finished parsing header */
switch (hcode) {
case 200:
break;
#ifndef NO_AUTH
case 407:
do_auth(hcode, url, penv, wauth, pauth, auth, message, rval);
goto cleanup_fetch_url;
#endif
default:
if (message)
warnx("Error proxy connect " "`%s'", message);
else
warnx("Unknown error proxy " "connect");
goto cleanup_fetch_url;
}
if ((ssl = fetch_start_ssl(fetch_fileno(fin), oui->host)) == NULL)
goto cleanup_fetch_url;
fetch_set_ssl(fin, ssl);
rv = C_OK;
goto out;
improper:
rv = C_IMPROPER;
goto out;
cleanup_fetch_url:
rv = C_CLEANUP;
goto out;
out:
FREEPTR(message);
return rv;
}
#endif
/* /*
* Retrieve URL, via a proxy if necessary, using HTTP. * Retrieve URL, via a proxy if necessary, using HTTP.
* If proxyenv is set, use that for the proxy, otherwise try ftp_proxy or * If proxyenv is set, use that for the proxy, otherwise try ftp_proxy or
* http_proxy/https_proxy as appropriate. * http_proxy/https_proxy as appropriate.
* Supports HTTP redirects. * Supports HTTP redirects.
* Returns 1 on failure, 0 on completed xfer, -1 if ftp connection * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
* is still open (e.g, ftp xfer with trailing /) * is still open (e.g, ftp xfer with trailing /)
*/ */
static int static int
fetch_url(const char *url, const char *proxyenv, char *proxyauth, char *wwwauth) fetch_url(const char *url, const char *proxyenv, char *proxyauth, char *wwwauth)
{ {
struct addrinfo hints, *res, *res0 = NULL;
int error;
sigfunc volatile oldint; sigfunc volatile oldint;
sigfunc volatile oldpipe; sigfunc volatile oldpipe;
sigfunc volatile oldalrm; sigfunc volatile oldalrm;
sigfunc volatile oldquit; sigfunc volatile oldquit;
int volatile s; int volatile s;
struct stat sb; struct stat sb;
int volatile ischunked;
int volatile isproxy; int volatile isproxy;
int volatile rval; int volatile rval, ischunked;
int volatile hcode;
int len;
size_t flen; size_t flen;
static size_t bufsize; static size_t bufsize;
static char *xferbuf; static char *xferbuf;
const char *cp, *token; const char *cp;
char *ep; char *ep;
char buf[FTPBUFLEN];
const char *errormsg;
char *volatile savefile;
char *volatile auth; char *volatile auth;
char *volatile savefile;
char *volatile location; char *volatile location;
char *volatile message; char *volatile message;
char *uuser, *pass, *host, *port, *path;
char *volatile decodedpath; char *volatile decodedpath;
char *puser, *ppass, *useragent; struct authinfo wauth, pauth;
off_t hashbytes, rangestart, rangeend, entitylen; struct posinfo pi;
off_t hashbytes;
int (*volatile closefunc)(FILE *); int (*volatile closefunc)(FILE *);
FETCH *volatile fin; FETCH *volatile fin;
FILE *volatile fout; FILE *volatile fout;
const char *volatile penv = proxyenv; const char *volatile penv = proxyenv;
struct urlinfo ui, oui;
time_t mtime; time_t mtime;
url_t urltype; void *ssl = NULL;
in_port_t portnum;
#ifdef WITH_SSL
void *ssl;
#endif
DPRINTF("%s: `%s' proxyenv `%s'\n", __func__, url, STRorNULL(penv)); DPRINTF("%s: `%s' proxyenv `%s'\n", __func__, url, STRorNULL(penv));
oldquit = oldalrm = oldint = oldpipe = NULL; oldquit = oldalrm = oldint = oldpipe = NULL;
closefunc = NULL; closefunc = NULL;
fin = NULL; fin = NULL;
fout = NULL; fout = NULL;
s = -1; s = -1;
savefile = NULL; savefile = NULL;
auth = location = message = NULL; auth = location = message = NULL;
ischunked = isproxy = hcode = 0; ischunked = isproxy = 0;
rval = 1; rval = 1;
uuser = pass = host = path = decodedpath = puser = ppass = NULL;
initurlinfo(&ui);
initurlinfo(&oui);
initauthinfo(&wauth, wwwauth);
initauthinfo(&pauth, proxyauth);
decodedpath = NULL;
if (sigsetjmp(httpabort, 1)) if (sigsetjmp(httpabort, 1))
goto cleanup_fetch_url; goto cleanup_fetch_url;
if (parse_url(url, "URL", &urltype, &uuser, &pass, &host, &port, if (parse_url(url, "URL", &ui, &wauth) == -1)
&portnum, &path) == -1)
goto cleanup_fetch_url; goto cleanup_fetch_url;
if (urltype == FILE_URL_T && ! EMPTYSTRING(host) copyurlinfo(&oui, &ui);
&& strcasecmp(host, "localhost") != 0) {
if (ui.utype == FILE_URL_T && ! EMPTYSTRING(ui.host)
&& strcasecmp(ui.host, "localhost") != 0) {
warnx("No support for non local file URL `%s'", url); warnx("No support for non local file URL `%s'", url);
goto cleanup_fetch_url; goto cleanup_fetch_url;
} }
if (EMPTYSTRING(path)) { if (EMPTYSTRING(ui.path)) {
if (urltype == FTP_URL_T) { if (ui.utype == FTP_URL_T) {
rval = fetch_ftp(url); rval = fetch_ftp(url);
goto cleanup_fetch_url; goto cleanup_fetch_url;
} }
if (!IS_HTTP_TYPE(urltype) || outfile == NULL) { if (!IS_HTTP_TYPE(ui.utype) || outfile == NULL) {
warnx("Invalid URL (no file after host) `%s'", url); warnx("Invalid URL (no file after host) `%s'", url);
goto cleanup_fetch_url; goto cleanup_fetch_url;
} }
} }
decodedpath = ftp_strdup(path); decodedpath = ftp_strdup(ui.path);
url_decode(decodedpath); url_decode(decodedpath);
if (outfile) if (outfile)
savefile = outfile; savefile = outfile;
else { else {
cp = strrchr(decodedpath, '/'); /* find savefile */ cp = strrchr(decodedpath, '/'); /* find savefile */
if (cp != NULL) if (cp != NULL)
savefile = ftp_strdup(cp + 1); savefile = ftp_strdup(cp + 1);
else else
savefile = ftp_strdup(decodedpath); savefile = ftp_strdup(decodedpath);
/*
* Use the first URL we requested not the name after a
* possible redirect, but careful to save it because our
* "safety" check is the match to outfile.
*/
outfile = ftp_strdup(savefile);
} }
DPRINTF("%s: savefile `%s'\n", __func__, savefile); DPRINTF("%s: savefile `%s'\n", __func__, savefile);
if (EMPTYSTRING(savefile)) { if (EMPTYSTRING(savefile)) {
if (urltype == FTP_URL_T) { if (ui.utype == FTP_URL_T) {
rval = fetch_ftp(url); rval = fetch_ftp(url);
goto cleanup_fetch_url; goto cleanup_fetch_url;
} }
warnx("No file after directory (you must specify an " warnx("No file after directory (you must specify an "
"output file) `%s'", url); "output file) `%s'", url);
goto cleanup_fetch_url; goto cleanup_fetch_url;
} }
restart_point = 0; restart_point = 0;
filesize = -1; filesize = -1;
rangestart = rangeend = entitylen = -1; initposinfo(&pi);
mtime = -1; mtime = -1;
if (restartautofetch) { if (restartautofetch) {
if (stat(savefile, &sb) == 0) if (stat(savefile, &sb) == 0)
restart_point = sb.st_size; restart_point = sb.st_size;
} }
if (urltype == FILE_URL_T) { /* file:// URLs */ if (ui.utype == FILE_URL_T) { /* file:// URLs */
direction = "copied"; direction = "copied";
fin = fetch_open(decodedpath, "r"); fin = fetch_open(decodedpath, "r");
if (fin == NULL) { if (fin == NULL) {
warn("Can't open `%s'", decodedpath); warn("Can't open `%s'", decodedpath);
goto cleanup_fetch_url; goto cleanup_fetch_url;
} }
if (fstat(fetch_fileno(fin), &sb) == 0) { if (fstat(fetch_fileno(fin), &sb) == 0) {
mtime = sb.st_mtime; mtime = sb.st_mtime;
filesize = sb.st_size; filesize = sb.st_size;
} }
skipping to change at line 637 skipping to change at line 1410
fprintf(ttyout, "Copying %s", decodedpath); fprintf(ttyout, "Copying %s", decodedpath);
if (restart_point) if (restart_point)
fprintf(ttyout, " (restarting at " LLF ")", fprintf(ttyout, " (restarting at " LLF ")",
(LLT)restart_point); (LLT)restart_point);
fputs("\n", ttyout); fputs("\n", ttyout);
} }
if (0 == rcvbuf_size) { if (0 == rcvbuf_size) {
rcvbuf_size = 8 * 1024; /* XXX */ rcvbuf_size = 8 * 1024; /* XXX */
} }
} else { /* ftp:// or http:// URLs */ } else { /* ftp:// or http:// URLs */
const char *leading;
int hasleading; int hasleading;
if (penv == NULL) { if (penv == NULL) {
#ifdef WITH_SSL #ifdef WITH_SSL
if (urltype == HTTPS_URL_T) if (ui.utype == HTTPS_URL_T)
penv = getoptionvalue("https_proxy"); penv = getoptionvalue("https_proxy");
#endif #endif
if (penv == NULL && IS_HTTP_TYPE(urltype)) if (penv == NULL && IS_HTTP_TYPE(ui.utype))
penv = getoptionvalue("http_proxy"); penv = getoptionvalue("http_proxy");
else if (urltype == FTP_URL_T) else if (ui.utype == FTP_URL_T)
penv = getoptionvalue("ftp_proxy"); penv = getoptionvalue("ftp_proxy");
} }
direction = "retrieved"; direction = "retrieved";
if (! EMPTYSTRING(penv)) { /* use proxy */ if (! EMPTYSTRING(penv)) { /* use proxy */
url_t purltype;
char *phost, *ppath; isproxy = handle_noproxy(ui.host, ui.portnum);
char *pport, *no_proxy;
in_port_t pportnum; if (isproxy == 0 && ui.utype == FTP_URL_T) {
rval = fetch_ftp(url);
isproxy = 1; goto cleanup_fetch_url;
/* check URL against list of no_proxied sites */
no_proxy = getoptionvalue("no_proxy");
if (! EMPTYSTRING(no_proxy)) {
char *np, *np_copy, *np_iter;
unsigned long np_port;
size_t hlen, plen;
np_iter = np_copy = ftp_strdup(no_proxy);
hlen = strlen(host);
while ((cp = strsep(&np_iter, " ,")) != NULL) {
if (*cp == '\0')
continue;
if ((np = strrchr(cp, ':')) != NULL) {
*np++ = '\0';
np_port = strtoul(np, &ep, 10);
if (*np == '\0' || *ep != '\0')
continue;
if (np_port != portnum)
continue;
}
plen = strlen(cp);
if (hlen < plen)
continue;
if (strncasecmp(host + hlen - plen,
cp, plen) == 0) {
isproxy = 0;
break;
}
}
FREEPTR(np_copy);
if (isproxy == 0 && urltype == FTP_URL_T) {
rval = fetch_ftp(url);
goto cleanup_fetch_url;
}
} }
if (isproxy) { if (isproxy) {
if (restart_point) { if (restart_point) {
warnx("Can't restart via proxy URL `%s'", warnx(
"Can't restart via proxy URL `%s'",
penv); penv);
goto cleanup_fetch_url; goto cleanup_fetch_url;
} }
if (parse_url(penv, "proxy URL", &purltype, if (handle_proxy(url, penv, &ui, &pauth) < 0)
&puser, &ppass, &phost, &pport, &pportnum,
&ppath) == -1)
goto cleanup_fetch_url;
if ((!IS_HTTP_TYPE(purltype)
&& purltype != FTP_URL_T) ||
EMPTYSTRING(phost) ||
(! EMPTYSTRING(ppath)
&& strcmp(ppath, "/") != 0)) {
warnx("Malformed proxy URL `%s'", penv);
FREEPTR(phost);
FREEPTR(pport);
FREEPTR(ppath);
goto cleanup_fetch_url; goto cleanup_fetch_url;
}
if (isipv6addr(host) &&
strchr(host, '%') != NULL) {
warnx(
"Scoped address notation `%s' disallowed via web proxy",
host);
FREEPTR(phost);
FREEPTR(pport);
FREEPTR(ppath);
goto cleanup_fetch_url;
}
FREEPTR(host);
host = phost;
FREEPTR(port);
port = pport;
FREEPTR(path);
path = ftp_strdup(url);
FREEPTR(ppath);
urltype = purltype;
} }
} /* ! EMPTYSTRING(penv) */ } /* ! EMPTYSTRING(penv) */
memset(&hints, 0, sizeof(hints)); s = ftp_socket(&ui, &ssl);
hints.ai_flags = 0;
hints.ai_family = family;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
error = getaddrinfo(host, port, &hints, &res0);
if (error) {
warnx("Can't LOOKUP `%s:%s': %s", host, port,
(error == EAI_SYSTEM) ? strerror(errno)
: gai_strerror(error));
goto cleanup_fetch_url;
}
if (res0->ai_canonname)
host = res0->ai_canonname;
s = -1;
#ifdef WITH_SSL
ssl = NULL;
#endif
for (res = res0; res; res = res->ai_next) {
char hname[NI_MAXHOST], sname[NI_MAXSERV];
ai_unmapped(res);
if (getnameinfo(res->ai_addr, res->ai_addrlen,
hname, sizeof(hname), sname, sizeof(sname),
NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
strlcpy(hname, "?", sizeof(hname));
strlcpy(sname, "?", sizeof(sname));
}
if (verbose && res0->ai_next) {
fprintf(ttyout, "Trying %s:%s ...\n",
hname, sname);
}
s = socket(res->ai_family, SOCK_STREAM,
res->ai_protocol);
if (s < 0) {
warn(
"Can't create socket for connection to "
"`%s:%s'", hname, sname);
continue;
}
if (ftp_connect(s, res->ai_addr, res->ai_addrlen,
verbose || !res->ai_next) < 0) {
close(s);
s = -1;
continue;
}
#ifdef WITH_SSL
if (urltype == HTTPS_URL_T) {
if ((ssl = fetch_start_ssl(s, host)) == NULL) {
close(s);
s = -1;
continue;
}
}
#endif
/* success */
break;
}
if (s < 0) { if (s < 0) {
warnx("Can't connect to `%s:%s'", host, port); warnx("Can't connect to `%s:%s'", ui.host, ui.port);
goto cleanup_fetch_url; goto cleanup_fetch_url;
} }
oldalrm = xsignal(SIGALRM, timeouthttp); oldalrm = xsignal(SIGALRM, timeouthttp);
alarmtimer(quit_time ? quit_time : 60); alarmtimer(quit_time ? quit_time : 60);
fin = fetch_fdopen(s, "r+"); fin = fetch_fdopen(s, "r+");
fetch_set_ssl(fin, ssl); fetch_set_ssl(fin, ssl);
alarmtimer(0); alarmtimer(0);
alarmtimer(quit_time ? quit_time : 60); alarmtimer(quit_time ? quit_time : 60);
/* /*
* Construct and send the request. * Construct and send the request.
*/ */
if (verbose) if (verbose)
fprintf(ttyout, "Requesting %s\n", url); fprintf(ttyout, "Requesting %s\n", url);
leading = " (";
hasleading = 0;
if (isproxy) {
if (verbose) {
fprintf(ttyout, "%svia %s:%s", leading,
host, port);
leading = ", ";
hasleading++;
}
fetch_printf(fin, "GET %s HTTP/1.0\r\n", path);
if (flushcache)
fetch_printf(fin, "Pragma: no-cache\r\n");
} else {
fetch_printf(fin, "GET %s HTTP/1.1\r\n", path);
if (strchr(host, ':')) {
char *h, *p;
/* hasleading = 0;
* strip off IPv6 scope identifier, since it is
* local to the node
*/
h = ftp_strdup(host);
if (isipv6addr(h) &&
(p = strchr(h, '%')) != NULL) {
*p = '\0';
}
fetch_printf(fin, "Host: [%s]", h);
free(h);
} else
fetch_printf(fin, "Host: %s", host);
#ifdef WITH_SSL #ifdef WITH_SSL
if ((urltype == HTTP_URL_T && portnum != HTTP_PORT) || if (isproxy && oui.utype == HTTPS_URL_T) {
(urltype == HTTPS_URL_T && portnum != HTTPS_PORT)) switch (connectmethod(fin, url, penv, &oui, &ui,
#else &wauth, &pauth, __UNVOLATILE(&auth), &hasleading,
if (portnum != HTTP_PORT) &rval)) {
#endif case C_CLEANUP:
fetch_printf(fin, ":%u", portnum); goto cleanup_fetch_url;
fetch_printf(fin, "\r\n"); case C_IMPROPER:
fetch_printf(fin, "Accept: */*\r\n"); goto improper;
fetch_printf(fin, "Connection: close\r\n"); case C_OK:
if (restart_point) { break;
fputs(leading, ttyout); default:
fetch_printf(fin, "Range: bytes=" LLF "-\r\n", abort();
(LLT)restart_point);
fprintf(ttyout, "restarting at " LLF,
(LLT)restart_point);
leading = ", ";
hasleading++;
} }
if (flushcache)
fetch_printf(fin, "Cache-Control: no-cache\r\n");
}
if ((useragent=getenv("FTPUSERAGENT")) != NULL) {
fetch_printf(fin, "User-Agent: %s\r\n", useragent);
} else {
fetch_printf(fin, "User-Agent: %s/%s\r\n",
FTP_PRODUCT, FTP_VERSION);
} }
if (wwwauth) { #endif
if (verbose) {
fprintf(ttyout, "%swith authorization", hasleading = print_get(fin, hasleading, isproxy, &oui, &ui);
leading);
leading = ", "; if (flushcache)
hasleading++; print_cache(fin, isproxy);
}
fetch_printf(fin, "Authorization: %s\r\n", wwwauth); print_agent(fin);
} hasleading = print_proxy(fin, hasleading, wauth.auth,
if (proxyauth) { auth ? NULL : pauth.auth);
if (verbose) { if (hasleading) {
fprintf(ttyout, hasleading = 0;
"%swith proxy authorization", leading); if (verbose)
leading = ", "; fputs(")\n", ttyout);
hasleading++;
}
fetch_printf(fin, "Proxy-Authorization: %s\r\n", proxyaut
h);
} }
if (verbose && hasleading)
fputs(")\n", ttyout);
fetch_printf(fin, "\r\n"); fetch_printf(fin, "\r\n");
if (fetch_flush(fin) == EOF) { if (fetch_flush(fin) == EOF) {
warn("Writing HTTP request"); warn("Writing HTTP request");
alarmtimer(0); alarmtimer(0);
goto cleanup_fetch_url; goto cleanup_fetch_url;
} }
alarmtimer(0); alarmtimer(0);
/* Read the response */ switch (negotiate_connection(fin, url, penv, &pi,
alarmtimer(quit_time ? quit_time : 60); &mtime, &wauth, &pauth, &rval, &ischunked,
len = fetch_getline(fin, buf, sizeof(buf), &errormsg); __UNVOLATILE(&auth))) {
alarmtimer(0); case C_OK:
if (len < 0) {
if (*errormsg == '\n')
errormsg++;
warnx("Receiving HTTP reply: %s", errormsg);
goto cleanup_fetch_url;
}
while (len > 0 && (ISLWS(buf[len-1])))
buf[--len] = '\0';
DPRINTF("%s: received `%s'\n", __func__, buf);
/* Determine HTTP response code */
cp = strchr(buf, ' ');
if (cp == NULL)
goto improper;
else
cp++;
hcode = strtol(cp, &ep, 10);
if (*ep != '\0' && !isspace((unsigned char)*ep))
goto improper;
message = ftp_strdup(cp);
/* Read the rest of the header. */
while (1) {
alarmtimer(quit_time ? quit_time : 60);
len = fetch_getline(fin, buf, sizeof(buf), &errormsg);
alarmtimer(0);
if (len < 0) {
if (*errormsg == '\n')
errormsg++;
warnx("Receiving HTTP reply: %s", errormsg);
goto cleanup_fetch_url;
}
while (len > 0 && (ISLWS(buf[len-1])))
buf[--len] = '\0';
if (len == 0)
break;
DPRINTF("%s: received `%s'\n", __func__, buf);
/*
* Look for some headers
*/
cp = buf;
if (match_token(&cp, "Content-Length:")) {
filesize = STRTOLL(cp, &ep, 10);
if (filesize < 0 || *ep != '\0')
goto improper;
DPRINTF("%s: parsed len as: " LLF "\n",
__func__, (LLT)filesize);
} else if (match_token(&cp, "Content-Range:")) {
if (! match_token(&cp, "bytes"))
goto improper;
if (*cp == '*')
cp++;
else {
rangestart = STRTOLL(cp, &ep, 10);
if (rangestart < 0 || *ep != '-')
goto improper;
cp = ep + 1;
rangeend = STRTOLL(cp, &ep, 10);
if (rangeend < 0 || rangeend < rangestart
)
goto improper;
cp = ep;
}
if (*cp != '/')
goto improper;
cp++;
if (*cp == '*')
cp++;
else {
entitylen = STRTOLL(cp, &ep, 10);
if (entitylen < 0)
goto improper;
cp = ep;
}
if (*cp != '\0')
goto improper;
#ifndef NO_DEBUG
if (ftp_debug) {
fprintf(ttyout, "parsed range as: ");
if (rangestart == -1)
fprintf(ttyout, "*");
else
fprintf(ttyout, LLF "-" LLF,
(LLT)rangestart,
(LLT)rangeend);
fprintf(ttyout, "/" LLF "\n", (LLT)entity
len);
}
#endif
if (! restart_point) {
warnx(
"Received unexpected Content-Range header");
goto cleanup_fetch_url;
}
} else if (match_token(&cp, "Last-Modified:")) {
struct tm parsed;
const char *t;
memset(&parsed, 0, sizeof(parsed));
t = parse_rfc2616time(&parsed, cp);
if (t != NULL) {
parsed.tm_isdst = -1;
if (*t == '\0')
mtime = timegm(&parsed);
#ifndef NO_DEBUG
if (ftp_debug && mtime != -1) {
fprintf(ttyout,
"parsed time as: %s",
rfc2822time(localtime(&mtime)));
}
#endif
}
} else if (match_token(&cp, "Location:")) {
location = ftp_strdup(cp);
DPRINTF("%s: parsed location as `%s'\n",
__func__, cp);
} else if (match_token(&cp, "Transfer-Encoding:")) {
if (match_token(&cp, "binary")) {
warnx(
"Bogus transfer encoding `binary' (fetching anyway)");
continue;
}
if (! (token = match_token(&cp, "chunked"))) {
warnx(
"Unsupported transfer encoding `%s'",
token);
goto cleanup_fetch_url;
}
ischunked++;
DPRINTF("%s: using chunked encoding\n",
__func__);
} else if (match_token(&cp, "Proxy-Authenticate:")
|| match_token(&cp, "WWW-Authenticate:")) {
if (! (token = match_token(&cp, "Basic"))) {
DPRINTF("%s: skipping unknown auth "
"scheme `%s'\n", __func__, token);
continue;
}
FREEPTR(auth);
auth = ftp_strdup(token);
DPRINTF("%s: parsed auth as `%s'\n",
__func__, cp);
}
}
/* finished parsing header */
switch (hcode) {
case 200:
break; break;
case 206: case C_CLEANUP:
if (! restart_point) {
warnx("Not expecting partial content header");
goto cleanup_fetch_url;
}
break;
case 300:
case 301:
case 302:
case 303:
case 305:
case 307:
if (EMPTYSTRING(location)) {
warnx(
"No redirection Location provided by server");
goto cleanup_fetch_url;
}
if (redirect_loop++ > 5) {
warnx("Too many redirections requested");
goto cleanup_fetch_url;
}
if (hcode == 305) {
if (verbose)
fprintf(ttyout, "Redirected via %s\n",
location);
rval = fetch_url(url, location,
proxyauth, wwwauth);
} else {
if (verbose)
fprintf(ttyout, "Redirected to %s\n",
location);
rval = go_fetch(location);
}
goto cleanup_fetch_url;
#ifndef NO_AUTH
case 401:
case 407:
{
char **authp;
char *auser, *apass;
if (hcode == 401) {
authp = &wwwauth;
auser = uuser;
apass = pass;
} else {
authp = &proxyauth;
auser = puser;
apass = ppass;
}
if (verbose || *authp == NULL ||
auser == NULL || apass == NULL)
fprintf(ttyout, "%s\n", message);
if (EMPTYSTRING(auth)) {
warnx(
"No authentication challenge provided by server");
goto cleanup_fetch_url;
}
if (*authp != NULL) {
char reply[10];
fprintf(ttyout,
"Authorization failed. Retry (y/n)? ");
if (get_line(stdin, reply, sizeof(reply), NULL)
< 0) {
goto cleanup_fetch_url;
}
if (tolower((unsigned char)reply[0]) != 'y')
goto cleanup_fetch_url;
auser = NULL;
apass = NULL;
}
if (auth_url(auth, authp, auser, apass) == 0) {
rval = fetch_url(url, penv,
proxyauth, wwwauth);
memset(*authp, 0, strlen(*authp));
FREEPTR(*authp);
}
goto cleanup_fetch_url; goto cleanup_fetch_url;
} case C_IMPROPER:
#endif goto improper;
default: default:
if (message) abort();
warnx("Error retrieving file `%s'", message);
else
warnx("Unknown error retrieving file");
goto cleanup_fetch_url;
} }
} /* end of ftp:// or http:// specific setup */ }
/* Open the output file. */ /* Open the output file. */
/* /*
* Only trust filenames with special meaning if they came from * Only trust filenames with special meaning if they came from
* the command line * the command line
*/ */
if (outfile == savefile) { if (outfile == savefile) {
if (strcmp(savefile, "-") == 0) { if (strcmp(savefile, "-") == 0) {
fout = stdout; fout = stdout;
skipping to change at line 1179 skipping to change at line 1537
oldpipe = xsignal(SIGPIPE, SIG_IGN); oldpipe = xsignal(SIGPIPE, SIG_IGN);
fout = popen(savefile + 1, "w"); fout = popen(savefile + 1, "w");
if (fout == NULL) { if (fout == NULL) {
warn("Can't execute `%s'", savefile + 1); warn("Can't execute `%s'", savefile + 1);
goto cleanup_fetch_url; goto cleanup_fetch_url;
} }
closefunc = pclose; closefunc = pclose;
} }
} }
if (fout == NULL) { if (fout == NULL) {
if ((rangeend != -1 && rangeend <= restart_point) || if ((pi.rangeend != -1 && pi.rangeend <= restart_point) ||
(rangestart == -1 && filesize != -1 && filesize <= restart_po (pi.rangestart == -1 &&
int)) { filesize != -1 && filesize <= restart_point)) {
/* already done */ /* already done */
if (verbose) if (verbose)
fprintf(ttyout, "already done\n"); fprintf(ttyout, "already done\n");
rval = 0; rval = 0;
goto cleanup_fetch_url; goto cleanup_fetch_url;
} }
if (restart_point && rangestart != -1) { if (restart_point && pi.rangestart != -1) {
if (entitylen != -1) if (pi.entitylen != -1)
filesize = entitylen; filesize = pi.entitylen;
if (rangestart != restart_point) { if (pi.rangestart != restart_point) {
warnx( warnx(
"Size of `%s' differs from save file `%s'", "Size of `%s' differs from save file `%s'",
url, savefile); url, savefile);
goto cleanup_fetch_url; goto cleanup_fetch_url;
} }
fout = fopen(savefile, "a"); fout = fopen(savefile, "a");
} else } else
fout = fopen(savefile, "w"); fout = fopen(savefile, "w");
if (fout == NULL) { if (fout == NULL) {
warn("Can't open `%s'", savefile); warn("Can't open `%s'", savefile);
skipping to change at line 1288 skipping to change at line 1647
while (1) { while (1) {
struct timeval then, now, td; struct timeval then, now, td;
volatile off_t bufrem; volatile off_t bufrem;
if (rate_get) if (rate_get)
(void)gettimeofday(&then, NULL); (void)gettimeofday(&then, NULL);
bufrem = rate_get ? rate_get : (off_t)bufsize; bufrem = rate_get ? rate_get : (off_t)bufsize;
if (ischunked) if (ischunked)
bufrem = MIN(chunksize, bufrem); bufrem = MIN(chunksize, bufrem);
while (bufrem > 0) { while (bufrem > 0) {
size_t nr = MIN((off_t)bufsize, bufrem);
flen = fetch_read(xferbuf, sizeof(char), flen = fetch_read(xferbuf, sizeof(char),
MIN((off_t)bufsize, bufrem), fin); nr, fin);
if (flen <= 0) if (flen == 0) {
if (fetch_error(fin))
goto chunkerror;
goto chunkdone; goto chunkdone;
}
bytes += flen; bytes += flen;
bufrem -= flen; bufrem -= flen;
if (fwrite(xferbuf, sizeof(char), flen, fout) if (fwrite(xferbuf, sizeof(char), flen, fout)
!= flen) { != flen) {
warn("Writing `%s'", savefile); warn("Writing `%s'", savefile);
goto cleanup_fetch_url; goto cleanup_fetch_url;
} }
if (hash && !progress) { if (hash && !progress) {
while (bytes >= hashbytes) { while (bytes >= hashbytes) {
(void)putc('#', ttyout); (void)putc('#', ttyout);
skipping to change at line 1342 skipping to change at line 1705
if (strcmp(xferbuf, "\r\n") != 0) { if (strcmp(xferbuf, "\r\n") != 0) {
warnx("Unexpected data following chunk"); warnx("Unexpected data following chunk");
goto cleanup_fetch_url; goto cleanup_fetch_url;
} }
if (lastchunk) if (lastchunk)
break; break;
} }
} while (ischunked); } while (ischunked);
/* XXX: deal with optional trailer & CRLF here? */ /* XXX: deal with optional trailer & CRLF here? */
chunkerror:
if (hash && !progress && bytes > 0) { if (hash && !progress && bytes > 0) {
if (bytes < mark) if (bytes < mark)
(void)putc('#', ttyout); (void)putc('#', ttyout);
(void)putc('\n', ttyout); (void)putc('\n', ttyout);
} }
if (fetch_error(fin)) { if (fetch_error(fin)) {
warn("Reading file"); warn("Reading file");
goto cleanup_fetch_url; goto cleanup_fetch_url;
} }
progressmeter(1); progressmeter(1);
skipping to change at line 1377 skipping to change at line 1740
} }
} }
if (bytes > 0) if (bytes > 0)
ptransfer(0); ptransfer(0);
bytes = 0; bytes = 0;
rval = 0; rval = 0;
goto cleanup_fetch_url; goto cleanup_fetch_url;
improper: improper:
warnx("Improper response from `%s:%s'", host, port); warnx("Improper response from `%s:%s'", ui.host, ui.port);
cleanup_fetch_url: cleanup_fetch_url:
if (oldint) if (oldint)
(void)xsignal(SIGINT, oldint); (void)xsignal(SIGINT, oldint);
if (oldpipe) if (oldpipe)
(void)xsignal(SIGPIPE, oldpipe); (void)xsignal(SIGPIPE, oldpipe);
if (oldalrm) if (oldalrm)
(void)xsignal(SIGALRM, oldalrm); (void)xsignal(SIGALRM, oldalrm);
if (oldquit) if (oldquit)
(void)xsignal(SIGQUIT, oldpipe); (void)xsignal(SIGQUIT, oldpipe);
if (fin != NULL) if (fin != NULL)
fetch_close(fin); fetch_close(fin);
else if (s != -1) else if (s != -1)
close(s); close(s);
if (closefunc != NULL && fout != NULL) if (closefunc != NULL && fout != NULL)
(*closefunc)(fout); (*closefunc)(fout);
if (res0)
freeaddrinfo(res0);
if (savefile != outfile) if (savefile != outfile)
FREEPTR(savefile); FREEPTR(savefile);
FREEPTR(uuser); freeurlinfo(&ui);
if (pass != NULL) freeurlinfo(&oui);
memset(pass, 0, strlen(pass)); freeauthinfo(&wauth);
FREEPTR(pass); freeauthinfo(&pauth);
FREEPTR(host);
FREEPTR(port);
FREEPTR(path);
FREEPTR(decodedpath); FREEPTR(decodedpath);
FREEPTR(puser);
if (ppass != NULL)
memset(ppass, 0, strlen(ppass));
FREEPTR(ppass);
FREEPTR(auth); FREEPTR(auth);
FREEPTR(location); FREEPTR(location);
FREEPTR(message); FREEPTR(message);
return (rval); return (rval);
} }
/* /*
* Abort a HTTP retrieval * Abort a HTTP retrieval
*/ */
static void static void
skipping to change at line 1461 skipping to change at line 1815
/* /*
* Retrieve ftp URL or classic ftp argument using FTP. * Retrieve ftp URL or classic ftp argument using FTP.
* Returns 1 on failure, 0 on completed xfer, -1 if ftp connection * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
* is still open (e.g, ftp xfer with trailing /) * is still open (e.g, ftp xfer with trailing /)
*/ */
static int static int
fetch_ftp(const char *url) fetch_ftp(const char *url)
{ {
char *cp, *xargv[5], rempath[MAXPATHLEN]; char *cp, *xargv[5], rempath[MAXPATHLEN];
char *host, *path, *dir, *file, *uuser, *pass; char *dir, *file;
char *port;
char cmdbuf[MAXPATHLEN]; char cmdbuf[MAXPATHLEN];
char dirbuf[4]; char dirbuf[4];
int dirhasglob, filehasglob, rval, transtype, xargc; int dirhasglob, filehasglob, rval, transtype, xargc;
int oanonftp, oautologin; int oanonftp, oautologin;
in_port_t portnum; struct authinfo auth;
url_t urltype; struct urlinfo ui;
DPRINTF("fetch_ftp: `%s'\n", url); DPRINTF("fetch_ftp: `%s'\n", url);
host = path = dir = file = uuser = pass = NULL; dir = file = NULL;
port = NULL;
rval = 1; rval = 1;
transtype = TYPE_I; transtype = TYPE_I;
initurlinfo(&ui);
initauthinfo(&auth, NULL);
if (STRNEQUAL(url, FTP_URL)) { if (STRNEQUAL(url, FTP_URL)) {
if ((parse_url(url, "URL", &urltype, &uuser, &pass, if ((parse_url(url, "URL", &ui, &auth) == -1) ||
&host, &port, &portnum, &path) == -1) || (auth.user != NULL && *auth.user == '\0') ||
(uuser != NULL && *uuser == '\0') || EMPTYSTRING(ui.host)) {
EMPTYSTRING(host)) {
warnx("Invalid URL `%s'", url); warnx("Invalid URL `%s'", url);
goto cleanup_fetch_ftp; goto cleanup_fetch_ftp;
} }
/* /*
* Note: Don't url_decode(path) here. We need to keep the * Note: Don't url_decode(path) here. We need to keep the
* distinction between "/" and "%2F" until later. * distinction between "/" and "%2F" until later.
*/ */
/* check for trailing ';type=[aid]' */ /* check for trailing ';type=[aid]' */
if (! EMPTYSTRING(path) && (cp = strrchr(path, ';')) != NULL) { if (! EMPTYSTRING(ui.path) && (cp = strrchr(ui.path, ';')) != NUL L) {
if (strcasecmp(cp, ";type=a") == 0) if (strcasecmp(cp, ";type=a") == 0)
transtype = TYPE_A; transtype = TYPE_A;
else if (strcasecmp(cp, ";type=i") == 0) else if (strcasecmp(cp, ";type=i") == 0)
transtype = TYPE_I; transtype = TYPE_I;
else if (strcasecmp(cp, ";type=d") == 0) { else if (strcasecmp(cp, ";type=d") == 0) {
warnx( warnx(
"Directory listing via a URL is not supported"); "Directory listing via a URL is not supported");
goto cleanup_fetch_ftp; goto cleanup_fetch_ftp;
} else { } else {
warnx("Invalid suffix `%s' in URL `%s'", cp, warnx("Invalid suffix `%s' in URL `%s'", cp,
url); url);
goto cleanup_fetch_ftp; goto cleanup_fetch_ftp;
} }
*cp = 0; *cp = 0;
} }
} else { /* classic style `[user@]host:[file]' */ } else { /* classic style `[user@]host:[file]' */
urltype = CLASSIC_URL_T; ui.utype = CLASSIC_URL_T;
host = ftp_strdup(url); ui.host = ftp_strdup(url);
cp = strchr(host, '@'); cp = strchr(ui.host, '@');
if (cp != NULL) { if (cp != NULL) {
*cp = '\0'; *cp = '\0';
uuser = host; auth.user = ui.host;
anonftp = 0; /* disable anonftp */ anonftp = 0; /* disable anonftp */
host = ftp_strdup(cp + 1); ui.host = ftp_strdup(cp + 1);
} }
cp = strchr(host, ':'); cp = strchr(ui.host, ':');
if (cp != NULL) { if (cp != NULL) {
*cp = '\0'; *cp = '\0';
path = ftp_strdup(cp + 1); ui.path = ftp_strdup(cp + 1);
} }
} }
if (EMPTYSTRING(host)) if (EMPTYSTRING(ui.host))
goto cleanup_fetch_ftp; goto cleanup_fetch_ftp;
/* Extract the file and (if present) directory name. */ /* Extract the file and (if present) directory name. */
dir = path; dir = ui.path;
if (! EMPTYSTRING(dir)) { if (! EMPTYSTRING(dir)) {
/* /*
* If we are dealing with classic `[user@]host:[path]' syntax, * If we are dealing with classic `[user@]host:[path]' syntax,
* then a path of the form `/file' (resulting from input of the * then a path of the form `/file' (resulting from input of the
* form `host:/file') means that we should do "CWD /" before * form `host:/file') means that we should do "CWD /" before
* retrieving the file. So we set dir="/" and file="file". * retrieving the file. So we set dir="/" and file="file".
* *
* But if we are dealing with URLs like `ftp://host/path' then * But if we are dealing with URLs like `ftp://host/path' then
* a path of the form `/file' (resulting from a URL of the form * a path of the form `/file' (resulting from a URL of the form
* `ftp://host//file') means that we should do `CWD ' (with an * `ftp://host//file') means that we should do `CWD ' (with an
skipping to change at line 1551 skipping to change at line 1905
* (We get a path without any slashes if we are dealing with * (We get a path without any slashes if we are dealing with
* classic `[user@]host:[file]' or URL `ftp://host/file'.) * classic `[user@]host:[file]' or URL `ftp://host/file'.)
* *
* In all other cases, we set dir to a string that does not * In all other cases, we set dir to a string that does not
* include the final '/' that separates the dir part from the * include the final '/' that separates the dir part from the
* file part of the path. (This will be the empty string if * file part of the path. (This will be the empty string if
* and only if we are dealing with a path of the form `/file' * and only if we are dealing with a path of the form `/file'
* resulting from an URL of the form `ftp://host//file'.) * resulting from an URL of the form `ftp://host//file'.)
*/ */
cp = strrchr(dir, '/'); cp = strrchr(dir, '/');
if (cp == dir && urltype == CLASSIC_URL_T) { if (cp == dir && ui.utype == CLASSIC_URL_T) {
file = cp + 1; file = cp + 1;
(void)strlcpy(dirbuf, "/", sizeof(dirbuf)); (void)strlcpy(dirbuf, "/", sizeof(dirbuf));
dir = dirbuf; dir = dirbuf;
} else if (cp != NULL) { } else if (cp != NULL) {
*cp++ = '\0'; *cp++ = '\0';
file = cp; file = cp;
} else { } else {
file = dir; file = dir;
dir = NULL; dir = NULL;
} }
} else } else
dir = NULL; dir = NULL;
if (urltype == FTP_URL_T && file != NULL) { if (ui.utype == FTP_URL_T && file != NULL) {
url_decode(file); url_decode(file);
/* but still don't url_decode(dir) */ /* but still don't url_decode(dir) */
} }
DPRINTF("fetch_ftp: user `%s' pass `%s' host %s port %s " DPRINTF("fetch_ftp: user `%s' pass `%s' host %s port %s "
"path `%s' dir `%s' file `%s'\n", "path `%s' dir `%s' file `%s'\n",
STRorNULL(uuser), STRorNULL(pass), STRorNULL(auth.user), STRorNULL(auth.pass),
STRorNULL(host), STRorNULL(port), STRorNULL(ui.host), STRorNULL(ui.port),
STRorNULL(path), STRorNULL(dir), STRorNULL(file)); STRorNULL(ui.path), STRorNULL(dir), STRorNULL(file));
dirhasglob = filehasglob = 0; dirhasglob = filehasglob = 0;
if (doglob && urltype == CLASSIC_URL_T) { if (doglob &&
(ui.utype == CLASSIC_URL_T || ui.utype == FTP_URL_T)) {
if (! EMPTYSTRING(dir) && strpbrk(dir, "*?[]{}") != NULL) if (! EMPTYSTRING(dir) && strpbrk(dir, "*?[]{}") != NULL)
dirhasglob = 1; dirhasglob = 1;
if (! EMPTYSTRING(file) && strpbrk(file, "*?[]{}") != NULL) if (! EMPTYSTRING(file) && strpbrk(file, "*?[]{}") != NULL)
filehasglob = 1; filehasglob = 1;
} }
/* Set up the connection */ /* Set up the connection */
oanonftp = anonftp; oanonftp = anonftp;
if (connected) if (connected)
disconnect(0, NULL); disconnect(0, NULL);
anonftp = oanonftp; anonftp = oanonftp;
(void)strlcpy(cmdbuf, getprogname(), sizeof(cmdbuf)); (void)strlcpy(cmdbuf, getprogname(), sizeof(cmdbuf));
xargv[0] = cmdbuf; xargv[0] = cmdbuf;
xargv[1] = host; xargv[1] = ui.host;
xargv[2] = NULL; xargv[2] = NULL;
xargc = 2; xargc = 2;
if (port) { if (ui.port) {
xargv[2] = port; xargv[2] = ui.port;
xargv[3] = NULL; xargv[3] = NULL;
xargc = 3; xargc = 3;
} }
oautologin = autologin; oautologin = autologin;
/* don't autologin in setpeer(), use ftp_login() below */ /* don't autologin in setpeer(), use ftp_login() below */
autologin = 0; autologin = 0;
setpeer(xargc, xargv); setpeer(xargc, xargv);
autologin = oautologin; autologin = oautologin;
if ((connected == 0) || if ((connected == 0) ||
(connected == 1 && !ftp_login(host, uuser, pass))) { (connected == 1 && !ftp_login(ui.host, auth.user, auth.pass))) {
warnx("Can't connect or login to host `%s:%s'", warnx("Can't connect or login to host `%s:%s'",
host, port ? port : "?"); ui.host, ui.port ? ui.port : "?");
goto cleanup_fetch_ftp; goto cleanup_fetch_ftp;
} }
switch (transtype) { switch (transtype) {
case TYPE_A: case TYPE_A:
setascii(1, xargv); setascii(1, xargv);
break; break;
case TYPE_I: case TYPE_I:
setbinary(1, xargv); setbinary(1, xargv);
break; break;
skipping to change at line 1690 skipping to change at line 2045
* ftp://host/%2Ffoo/bar/file dir="%2Ffoo/bar" * ftp://host/%2Ffoo/bar/file dir="%2Ffoo/bar"
* "CWD /foo", "CWD bar", "RETR file" * "CWD /foo", "CWD bar", "RETR file"
* ftp://host/%2Ffoo%2Fbar/file dir="%2Ffoo%2Fbar" * ftp://host/%2Ffoo%2Fbar/file dir="%2Ffoo%2Fbar"
* "CWD /foo/bar", "RETR file" * "CWD /foo/bar", "RETR file"
* ftp://host/%2Ffoo%2Fbar%2Ffile dir=NULL * ftp://host/%2Ffoo%2Fbar%2Ffile dir=NULL
* "RETR /foo/bar/file" * "RETR /foo/bar/file"
* *
* Note that we don't need `dir' after this point. * Note that we don't need `dir' after this point.
*/ */
do { do {
if (urltype == FTP_URL_T) { if (ui.utype == FTP_URL_T) {
nextpart = strchr(dir, '/'); nextpart = strchr(dir, '/');
if (nextpart) { if (nextpart) {
*nextpart = '\0'; *nextpart = '\0';
nextpart++; nextpart++;
} }
url_decode(dir); url_decode(dir);
} else } else
nextpart = NULL; nextpart = NULL;
DPRINTF("fetch_ftp: dir `%s', nextpart `%s'\n", DPRINTF("fetch_ftp: dir `%s', nextpart `%s'\n",
STRorNULL(dir), STRorNULL(nextpart)); STRorNULL(dir), STRorNULL(nextpart));
if (urltype == FTP_URL_T || *dir != '\0') { if (ui.utype == FTP_URL_T || *dir != '\0') {
(void)strlcpy(cmdbuf, "cd", sizeof(cmdbuf)); (void)strlcpy(cmdbuf, "cd", sizeof(cmdbuf));
xargv[0] = cmdbuf; xargv[0] = cmdbuf;
xargv[1] = dir; xargv[1] = dir;
xargv[2] = NULL; xargv[2] = NULL;
dirchange = 0; dirchange = 0;
cd(2, xargv); cd(2, xargv);
if (! dirchange) { if (! dirchange) {
if (*dir == '\0' && code == 500) if (*dir == '\0' && code == 500)
fprintf(stderr, fprintf(stderr,
"\n" "\n"
skipping to change at line 1755 skipping to change at line 2110
ointeractive = interactive; ointeractive = interactive;
interactive = 0; interactive = 0;
if (restartautofetch) if (restartautofetch)
(void)strlcpy(cmdbuf, "mreget", sizeof(cmdbuf)); (void)strlcpy(cmdbuf, "mreget", sizeof(cmdbuf));
else else
(void)strlcpy(cmdbuf, "mget", sizeof(cmdbuf)); (void)strlcpy(cmdbuf, "mget", sizeof(cmdbuf));
xargv[0] = cmdbuf; xargv[0] = cmdbuf;
mget(xargc, xargv); mget(xargc, xargv);
interactive = ointeractive; interactive = ointeractive;
} else { } else {
if (outfile == NULL) { char *destfile = outfile;
if (destfile == NULL) {
cp = strrchr(file, '/'); /* find savefile */ cp = strrchr(file, '/'); /* find savefile */
if (cp != NULL) if (cp != NULL)
outfile = cp + 1; destfile = cp + 1;
else else
outfile = file; destfile = file;
} }
xargv[2] = (char *)outfile; xargv[2] = (char *)destfile;
xargv[3] = NULL; xargv[3] = NULL;
xargc++; xargc++;
if (restartautofetch) if (restartautofetch)
reget(xargc, xargv); reget(xargc, xargv);
else else
get(xargc, xargv); get(xargc, xargv);
} }
if ((code / 100) == COMPLETE) if ((code / 100) == COMPLETE)
rval = 0; rval = 0;
cleanup_fetch_ftp: cleanup_fetch_ftp:
FREEPTR(port); freeurlinfo(&ui);
FREEPTR(host); freeauthinfo(&auth);
FREEPTR(path);
FREEPTR(uuser);
if (pass)
memset(pass, 0, strlen(pass));
FREEPTR(pass);
return (rval); return (rval);
} }
/* /*
* Retrieve the given file to outfile. * Retrieve the given file to outfile.
* Supports arguments of the form: * Supports arguments of the form:
* "host:path", "ftp://host/path" if $ftpproxy, call fetch_url() else * "host:path", "ftp://host/path" if $ftpproxy, call fetch_url() else
* call fetch_ftp() * call fetch_ftp()
* "http://host/path" call fetch_url() to use HTTP * "http://host/path" call fetch_url() to use HTTP
* "file:///path" call fetch_url() to copy * "file:///path" call fetch_url() to copy
skipping to change at line 1913 skipping to change at line 2264
* Loop through as long as there's files to fetch. * Loop through as long as there's files to fetch.
*/ */
for (; (rval == 0) && (argpos < argc); argpos++) { for (; (rval == 0) && (argpos < argc); argpos++) {
if (strchr(argv[argpos], ':') == NULL) if (strchr(argv[argpos], ':') == NULL)
break; break;
redirect_loop = 0; redirect_loop = 0;
if (!anonftp) if (!anonftp)
anonftp = 2; /* Handle "automatic" transfers. */ anonftp = 2; /* Handle "automatic" transfers. */
rval = go_fetch(argv[argpos]); rval = go_fetch(argv[argpos]);
if (outfile != NULL && strcmp(outfile, "-") != 0 if (outfile != NULL && strcmp(outfile, "-") != 0
&& outfile[0] != '|') && outfile[0] != '|') {
outfile = NULL; FREEPTR(outfile);
}
if (rval > 0) if (rval > 0)
rval = argpos + 1; rval = argpos + 1;
} }
if (connected && rval != -1) if (connected && rval != -1)
disconnect(0, NULL); disconnect(0, NULL);
return (rval); return (rval);
} }
/* /*
 End of changes. 117 change blocks. 
626 lines changed or deleted 975 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)