version 1.7, 1997/04/14 00:48:44 |
version 1.8, 1997/04/16 05:02:48 |
|
|
/* $OpenBSD$ */ |
/* $OpenBSD$ */ |
/* $NetBSD: fetch.c,v 1.5 1997/04/05 03:27:36 lukem Exp $ */ |
/* $NetBSD: fetch.c,v 1.6 1997/04/14 09:09:19 lukem Exp $ */ |
|
|
/*- |
/*- |
* Copyright (c) 1997 The NetBSD Foundation, Inc. |
* Copyright (c) 1997 The NetBSD Foundation, Inc. |
|
|
else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0) |
else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0) |
host = proxy + sizeof(FTP_URL) - 1; |
host = proxy + sizeof(FTP_URL) - 1; |
else { |
else { |
warnx("Malformed proxy url: %s", proxy); |
warnx("Malformed proxy URL: %s", proxy); |
goto cleanup_url_get; |
goto cleanup_url_get; |
} |
} |
if (EMPTYSTRING(host)) |
if (EMPTYSTRING(host)) |
|
|
|
|
if (isdigit(host[0])) { |
if (isdigit(host[0])) { |
if (inet_aton(host, &sin.sin_addr) == 0) { |
if (inet_aton(host, &sin.sin_addr) == 0) { |
warnx("invalid IP address: %s", host); |
warnx("Invalid IP address: %s", host); |
goto cleanup_url_get; |
goto cleanup_url_get; |
} |
} |
} else { |
} else { |
|
|
if (! EMPTYSTRING(portnum)) { |
if (! EMPTYSTRING(portnum)) { |
port = atoi(portnum); |
port = atoi(portnum); |
if (port < 1 || (port & 0xffff) != port) { |
if (port < 1 || (port & 0xffff) != port) { |
warnx("invalid port: %s", portnum); |
warnx("Invalid port: %s", portnum); |
goto cleanup_url_get; |
goto cleanup_url_get; |
} |
} |
port = htons(port); |
port = htons(port); |
|
|
return (0); |
return (0); |
|
|
improper: |
improper: |
warnx("improper response from %s", host); |
warnx("Improper response from %s", host); |
cleanup_url_get: |
cleanup_url_get: |
if (s != -1) |
if (s != -1) |
close(s); |
close(s); |
|
|
static char lasthost[MAXHOSTNAMELEN]; |
static char lasthost[MAXHOSTNAMELEN]; |
char *xargv[5]; |
char *xargv[5]; |
char *cp, *line, *host, *dir, *file, *portnum; |
char *cp, *line, *host, *dir, *file, *portnum; |
|
char *user, *pass; |
char *ftpproxy, *httpproxy; |
char *ftpproxy, *httpproxy; |
int rval, xargc, argpos; |
int rval, xargc, argpos; |
int dirhasglob, filehasglob; |
int dirhasglob, filehasglob; |
|
|
for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) { |
for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) { |
if (strchr(argv[argpos], ':') == NULL) |
if (strchr(argv[argpos], ':') == NULL) |
break; |
break; |
host = dir = file = portnum = NULL; |
host = dir = file = portnum = user = pass = NULL; |
|
|
/* |
/* |
* We muck with the string, so we make a copy. |
* We muck with the string, so we make a copy. |
|
|
continue; |
continue; |
} |
} |
host += sizeof(FTP_URL) - 1; |
host += sizeof(FTP_URL) - 1; |
cp = strchr(host, '/'); |
dir = strchr(host, '/'); |
|
|
/* Look for a port number after the host name. */ |
/* Look for [user:pass@]host[:port] */ |
|
user = host; |
|
pass = strpbrk(user, ":@/"); |
|
if (pass == NULL || *pass == '/') |
|
goto parsed_url; |
|
if (*pass == '@') { |
|
warnx("Bad ftp URL: %s", argv[argpos]); |
|
rval = argpos + 1; |
|
continue; |
|
} |
|
*pass++ = '\0'; |
|
cp = strpbrk(pass, ":@/"); |
|
if (cp == NULL || *cp == '/') { |
|
portnum = pass; |
|
user = pass = NULL; |
|
goto parsed_url; |
|
} |
|
if (*cp == ':') { |
|
warnx("Bad ftp URL: %s", argv[argpos]); |
|
rval = argpos + 1; |
|
continue; |
|
} |
|
*cp++ = '\0'; |
|
host = cp; |
portnum = strchr(host, ':'); |
portnum = strchr(host, ':'); |
if (portnum != NULL) |
if (portnum != NULL) |
*portnum++ = '\0'; |
*portnum++ = '\0'; |
} else /* classic style `host:file' */ |
parsed_url: |
cp = strchr(host, ':'); |
} else { /* classic style `host:file' */ |
|
dir = strchr(host, ':'); |
|
} |
if (EMPTYSTRING(host)) { |
if (EMPTYSTRING(host)) { |
rval = argpos + 1; |
rval = argpos + 1; |
continue; |
continue; |
|
|
* If cp is NULL, the file wasn't specified |
* If cp is NULL, the file wasn't specified |
* (URL looked something like ftp://host) |
* (URL looked something like ftp://host) |
*/ |
*/ |
if (cp != NULL) |
if (dir != NULL) |
*cp++ = '\0'; |
*dir++ = '\0'; |
|
|
/* |
/* |
* Extract the file and (if present) directory name. |
* Extract the file and (if present) directory name. |
*/ |
*/ |
dir = cp; |
|
if (! EMPTYSTRING(dir)) { |
if (! EMPTYSTRING(dir)) { |
cp = strrchr(cp, '/'); |
cp = strrchr(dir, '/'); |
if (cp != NULL) { |
if (cp != NULL) { |
*cp++ = '\0'; |
*cp++ = '\0'; |
file = cp; |
file = cp; |
|
|
} |
} |
} |
} |
if (debug) |
if (debug) |
printf("host '%s', dir '%s', file '%s'\n", |
printf("user %s:%s host %s port %s dir %s file %s\n", |
host, dir, file); |
user, pass, host, portnum, dir, file); |
|
|
/* |
/* |
* Set up the connection if we don't have one. |
* Set up the connection if we don't have one. |
*/ |
*/ |
if (strcmp(host, lasthost) != 0) { |
if (strcmp(host, lasthost) != 0) { |
|
int oautologin; |
|
|
(void)strcpy(lasthost, host); |
(void)strcpy(lasthost, host); |
if (connected) |
if (connected) |
disconnect(0, NULL); |
disconnect(0, NULL); |
|
|
xargv[1] = host; |
xargv[1] = host; |
xargv[2] = NULL; |
xargv[2] = NULL; |
xargc = 2; |
xargc = 2; |
if (portnum != NULL) { |
if (! EMPTYSTRING(portnum)) { |
xargv[2] = portnum; |
xargv[2] = portnum; |
xargv[3] = NULL; |
xargv[3] = NULL; |
xargc = 3; |
xargc = 3; |
} |
} |
|
oautologin = autologin; |
|
if (user != NULL) |
|
autologin = 0; |
setpeer(xargc, xargv); |
setpeer(xargc, xargv); |
if (connected == 0) { |
autologin = oautologin; |
warnx("Can't connect to host `%s'", host); |
if ((connected == 0) || |
|
((connected == 1) && !login(host, user, pass)) ) { |
|
warnx("Can't connect or login to host `%s'", |
|
host); |
rval = argpos + 1; |
rval = argpos + 1; |
continue; |
continue; |
} |
} |
|
|
/* Always use binary transfers. */ |
/* Always use binary transfers. */ |
setbinary(0, NULL); |
setbinary(0, NULL); |
} |
} |
else /* already have connection, cd back to '/' */ |
/* cd back to '/' */ |
{ |
xargv[0] = "cd"; |
xargv[0] = "cd"; |
xargv[1] = "/"; |
xargv[1] = "/"; |
xargv[2] = NULL; |
xargv[2] = NULL; |
cd(2, xargv); |
cd(2, xargv); |
if (! dirchange) { |
if (! dirchange) { |
rval = argpos + 1; |
rval = argpos + 1; |
continue; |
continue; |
|
} |
|
} |
} |
|
|
dirhasglob = filehasglob = 0; |
dirhasglob = filehasglob = 0; |