1 /* $NetBSD: fetch.c,v 1.202 2013/02/23 13:47:36 christos Exp $ */
4 * Copyright (c) 1997-2009 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Scott Aaron Bamford.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: fetch.c,v 1.202 2013/02/23 13:47:36 christos Exp $");
41 * FTP User Program -- Command line file retrieval
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/socket.h>
50 #include <netinet/in.h>
53 #include <arpa/inet.h>
83 __dead static void aborthttp(int);
85 static int auth_url(const char *, char **, const char *, const char *);
86 static void base64_encode(const unsigned char *, size_t, unsigned char *);
88 static int go_fetch(const char *);
89 static int fetch_ftp(const char *);
90 static int fetch_url(const char *, const char *, char *, char *);
91 static const char *match_token(const char **, const char *);
92 static int parse_url(const char *, const char *, url_t *, char **,
93 char **, char **, char **, in_port_t *, char **);
94 static void url_decode(char *);
96 static int redirect_loop;
99 #define STRNEQUAL(a,b) (strncasecmp((a), (b), sizeof((b))-1) == 0)
100 #define ISLWS(x) ((x)=='\r' || (x)=='\n' || (x)==' ' || (x)=='\t')
101 #define SKIPLWS(x) do { while (ISLWS((*x))) x++; } while (0)
104 #define ABOUT_URL "about:" /* propaganda */
105 #define FILE_URL "file://" /* file URL prefix */
106 #define FTP_URL "ftp://" /* ftp URL prefix */
107 #define HTTP_URL "http://" /* http URL prefix */
109 #define HTTPS_URL "https://" /* https URL prefix */
111 #define IS_HTTP_TYPE(urltype) \
112 (((urltype) == HTTP_URL_T) || ((urltype) == HTTPS_URL_T))
114 #define IS_HTTP_TYPE(urltype) \
115 ((urltype) == HTTP_URL_T)
119 * Determine if token is the next word in buf (case insensitive).
120 * If so, advance buf past the token and any trailing LWS, and
121 * return a pointer to the token (in buf). Otherwise, return NULL.
122 * token may be preceded by LWS.
123 * token must be followed by LWS or NUL. (I.e, don't partial match).
126 match_token(const char **buf, const char *token)
128 const char *p, *orig;
131 tlen = strlen(token);
135 if (strncasecmp(p, token, tlen) != 0)
138 if (*p != '\0' && !ISLWS(*p))
148 * Generate authorization response based on given authentication challenge.
149 * Returns -1 if an error occurred, otherwise 0.
150 * Sets response to a malloc(3)ed string; caller should free.
153 auth_url(const char *challenge, char **response, const char *guser,
156 const char *cp, *scheme, *errormsg;
157 char *ep, *clear, *realm;
158 char uuser[BUFSIZ], *gotpass;
161 size_t len, clen, rlen;
164 clear = realm = NULL;
167 scheme = "Basic"; /* only support Basic authentication */
170 DPRINTF("auth_url: challenge `%s'\n", challenge);
172 if (! match_token(&cp, scheme)) {
173 warnx("Unsupported authentication challenge `%s'",
175 goto cleanup_auth_url;
178 #define REALM "realm=\""
179 if (STRNEQUAL(cp, REALM))
180 cp += sizeof(REALM) - 1;
182 warnx("Unsupported authentication challenge `%s'",
184 goto cleanup_auth_url;
186 /* XXX: need to improve quoted-string parsing to support \ quoting, etc. */
187 if ((ep = strchr(cp, '\"')) != NULL) {
189 realm = (char *)ftp_malloc(len + 1);
190 (void)strlcpy(realm, cp, len + 1);
192 warnx("Unsupported authentication challenge `%s'",
194 goto cleanup_auth_url;
197 fprintf(ttyout, "Username for `%s': ", realm);
199 (void)strlcpy(uuser, guser, sizeof(uuser));
200 fprintf(ttyout, "%s\n", uuser);
202 (void)fflush(ttyout);
203 if (get_line(stdin, uuser, sizeof(uuser), &errormsg) < 0) {
204 warnx("%s; can't authenticate", errormsg);
205 goto cleanup_auth_url;
211 gotpass = getpass("Password: ");
212 if (gotpass == NULL) {
213 warnx("Can't read password");
214 goto cleanup_auth_url;
219 clen = strlen(uuser) + strlen(upass) + 2; /* user + ":" + pass + "\0" */
220 clear = (char *)ftp_malloc(clen);
221 (void)strlcpy(clear, uuser, clen);
222 (void)strlcat(clear, ":", clen);
223 (void)strlcat(clear, upass, clen);
225 memset(gotpass, 0, strlen(gotpass));
227 /* scheme + " " + enc + "\0" */
228 rlen = strlen(scheme) + 1 + (clen + 2) * 4 / 3 + 1;
229 *response = (char *)ftp_malloc(rlen);
230 (void)strlcpy(*response, scheme, rlen);
231 len = strlcat(*response, " ", rlen);
232 /* use `clen - 1' to not encode the trailing NUL */
233 base64_encode((unsigned char *)clear, clen - 1,
234 (unsigned char *)*response + len);
235 memset(clear, 0, clen);
245 * Encode len bytes starting at clear using base64 encoding into encoded,
246 * which should be at least ((len + 2) * 4 / 3 + 1) in size.
249 base64_encode(const unsigned char *clear, size_t len, unsigned char *encoded)
251 static const unsigned char enc[] =
252 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
257 for (i = 0; i < len; i += 3) {
258 *(cp++) = enc[((clear[i + 0] >> 2))];
259 *(cp++) = enc[((clear[i + 0] << 4) & 0x30)
260 | ((clear[i + 1] >> 4) & 0x0f)];
261 *(cp++) = enc[((clear[i + 1] << 2) & 0x3c)
262 | ((clear[i + 2] >> 6) & 0x03)];
263 *(cp++) = enc[((clear[i + 2] ) & 0x3f)];
272 * Decode %xx escapes in given string, `in-place'.
275 url_decode(char *url)
277 unsigned char *p, *q;
279 if (EMPTYSTRING(url))
281 p = q = (unsigned char *)url;
283 #define HEXTOINT(x) (x - (isdigit(x) ? '0' : (islower(x) ? 'a' : 'A') - 10))
286 && p[1] && isxdigit((unsigned char)p[1])
287 && p[2] && isxdigit((unsigned char)p[2])) {
288 *q++ = HEXTOINT(p[1]) * 16 + HEXTOINT(p[2]);
298 * Parse URL of form (per RFC 3986):
299 * <type>://[<user>[:<password>]@]<host>[:<port>][/<path>]
300 * Returns -1 if a parse error occurred, otherwise 0.
301 * It's the caller's responsibility to url_decode() the returned
302 * user, pass and path.
304 * Sets type to url_t, each of the given char ** pointers to a
305 * malloc(3)ed strings of the relevant section, and port to
306 * the number given, or ftpport if ftp://, or httpport if http://.
308 * XXX: this is not totally RFC 3986 compliant; <path> will have the
309 * leading `/' unless it's an ftp:// URL, as this makes things easier
310 * for file:// and http:// URLs. ftp:// URLs have the `/' between the
311 * host and the URL-path removed, but any additional leading slashes
312 * in the URL-path are retained (because they imply that we should
313 * later do "CWD" with a null argument).
316 * input URL output path
317 * --------- -----------
320 * "http://host/path" "/path"
321 * "file://host/dir/file" "dir/file"
325 * "ftp://host/dir/file" "dir/file"
326 * "ftp://host//dir/file" "/dir/file"
329 parse_url(const char *url, const char *desc, url_t *utype,
330 char **uuser, char **pass, char **host, char **port,
331 in_port_t *portnum, char **path)
333 const char *origurl, *tport;
334 char *cp, *ep, *thost;
337 if (url == NULL || desc == NULL || utype == NULL || uuser == NULL
338 || pass == NULL || host == NULL || port == NULL || portnum == NULL
340 errx(1, "parse_url: invoked with NULL argument!");
341 DPRINTF("parse_url: %s `%s'\n", desc, url);
344 *utype = UNKNOWN_URL_T;
345 *uuser = *pass = *host = *port = *path = NULL;
349 if (STRNEQUAL(url, HTTP_URL)) {
350 url += sizeof(HTTP_URL) - 1;
352 *portnum = HTTP_PORT;
354 } else if (STRNEQUAL(url, FTP_URL)) {
355 url += sizeof(FTP_URL) - 1;
359 } else if (STRNEQUAL(url, FILE_URL)) {
360 url += sizeof(FILE_URL) - 1;
363 } else if (STRNEQUAL(url, HTTPS_URL)) {
364 url += sizeof(HTTPS_URL) - 1;
365 *utype = HTTPS_URL_T;
366 *portnum = HTTPS_PORT;
370 warnx("Invalid %s `%s'", desc, url);
374 memset(*pass, 0, strlen(*pass));
385 /* find [user[:pass]@]host[:port] */
386 ep = strchr(url, '/');
388 thost = ftp_strdup(url);
391 thost = (char *)ftp_malloc(len + 1);
392 (void)strlcpy(thost, url, len + 1);
393 if (*utype == FTP_URL_T) /* skip first / for ftp URLs */
395 *path = ftp_strdup(ep);
398 cp = strchr(thost, '@'); /* look for user[:pass]@ in URLs */
400 if (*utype == FTP_URL_T)
401 anonftp = 0; /* disable anonftp */
404 thost = ftp_strdup(cp + 1);
405 cp = strchr(*uuser, ':');
408 *pass = ftp_strdup(cp + 1);
417 * Check if thost is an encoded IPv6 address, as per
419 * `[' ipv6-address ']'
423 if ((ep = strchr(cp, ']')) == NULL ||
424 (ep[1] != '\0' && ep[1] != ':')) {
425 warnx("Invalid address `%s' in %s `%s'",
426 thost, desc, origurl);
427 goto cleanup_parse_url;
429 len = ep - cp; /* change `[xyz]' -> `xyz' */
430 memmove(thost, thost + 1, len);
432 if (! isipv6addr(thost)) {
433 warnx("Invalid IPv6 address `%s' in %s `%s'",
434 thost, desc, origurl);
435 goto cleanup_parse_url;
444 if ((cp = strchr(thost, ':')) != NULL)
448 /* look for [:port] */
452 nport = strtoul(cp, &ep, 10);
453 if (*cp == '\0' || *ep != '\0' ||
454 nport < 1 || nport > MAX_IN_PORT_T) {
455 warnx("Unknown port `%s' in %s `%s'",
457 goto cleanup_parse_url;
464 *port = ftp_strdup(tport);
466 const char *emptypath = "/";
467 if (*utype == FTP_URL_T) /* skip first / for ftp URLs */
469 *path = ftp_strdup(emptypath);
472 DPRINTF("parse_url: user `%s' pass `%s' host %s port %s(%d) "
474 STRorNULL(*uuser), STRorNULL(*pass),
475 STRorNULL(*host), STRorNULL(*port),
476 *portnum ? *portnum : -1, STRorNULL(*path));
481 sigjmp_buf httpabort;
484 * Retrieve URL, via a proxy if necessary, using HTTP.
485 * If proxyenv is set, use that for the proxy, otherwise try ftp_proxy or
486 * http_proxy/https_proxy as appropriate.
487 * Supports HTTP redirects.
488 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
489 * is still open (e.g, ftp xfer with trailing /)
492 fetch_url(const char *url, const char *proxyenv, char *proxyauth, char *wwwauth)
494 struct addrinfo hints, *res, *res0 = NULL;
496 sigfunc volatile oldintr;
497 sigfunc volatile oldintp;
500 int volatile ischunked;
501 int volatile isproxy;
506 static size_t bufsize;
507 static char *xferbuf;
508 const char *cp, *token;
511 const char *errormsg;
512 char *volatile savefile;
514 char *volatile location;
515 char *volatile message;
516 char *uuser, *pass, *host, *port, *path;
517 char *volatile decodedpath;
518 char *puser, *ppass, *useragent;
519 off_t hashbytes, rangestart, rangeend, entitylen;
520 int (*volatile closefunc)(FILE *);
530 DPRINTF("fetch_url: `%s' proxyenv `%s'\n", url, STRorNULL(proxyenv));
532 oldintr = oldintp = NULL;
538 auth = location = message = NULL;
539 ischunked = isproxy = hcode = 0;
541 uuser = pass = host = path = decodedpath = puser = ppass = NULL;
543 if (parse_url(url, "URL", &urltype, &uuser, &pass, &host, &port,
544 &portnum, &path) == -1)
545 goto cleanup_fetch_url;
547 if (urltype == FILE_URL_T && ! EMPTYSTRING(host)
548 && strcasecmp(host, "localhost") != 0) {
549 warnx("No support for non local file URL `%s'", url);
550 goto cleanup_fetch_url;
553 if (EMPTYSTRING(path)) {
554 if (urltype == FTP_URL_T) {
555 rval = fetch_ftp(url);
556 goto cleanup_fetch_url;
558 if (!IS_HTTP_TYPE(urltype) || outfile == NULL) {
559 warnx("Invalid URL (no file after host) `%s'", url);
560 goto cleanup_fetch_url;
564 decodedpath = ftp_strdup(path);
565 url_decode(decodedpath);
568 savefile = ftp_strdup(outfile);
570 cp = strrchr(decodedpath, '/'); /* find savefile */
572 savefile = ftp_strdup(cp + 1);
574 savefile = ftp_strdup(decodedpath);
576 DPRINTF("fetch_url: savefile `%s'\n", savefile);
577 if (EMPTYSTRING(savefile)) {
578 if (urltype == FTP_URL_T) {
579 rval = fetch_ftp(url);
580 goto cleanup_fetch_url;
582 warnx("No file after directory (you must specify an "
583 "output file) `%s'", url);
584 goto cleanup_fetch_url;
589 rangestart = rangeend = entitylen = -1;
591 if (restartautofetch) {
592 if (strcmp(savefile, "-") != 0 && *savefile != '|' &&
593 stat(savefile, &sb) == 0)
594 restart_point = sb.st_size;
596 if (urltype == FILE_URL_T) { /* file:// URLs */
597 direction = "copied";
598 fin = fetch_open(decodedpath, "r");
600 warn("Can't open `%s'", decodedpath);
601 goto cleanup_fetch_url;
603 if (fstat(fetch_fileno(fin), &sb) == 0) {
605 filesize = sb.st_size;
608 if (lseek(fetch_fileno(fin), restart_point, SEEK_SET) < 0) {
609 warn("Can't seek to restart `%s'",
611 goto cleanup_fetch_url;
615 fprintf(ttyout, "Copying %s", decodedpath);
617 fprintf(ttyout, " (restarting at " LLF ")",
621 if (0 == rcvbuf_size) {
622 rcvbuf_size = 8 * 1024; /* XXX */
624 } else { /* ftp:// or http:// URLs */
628 if (proxyenv == NULL) {
630 if (urltype == HTTPS_URL_T)
631 proxyenv = getoptionvalue("https_proxy");
633 if (proxyenv == NULL && IS_HTTP_TYPE(urltype))
634 proxyenv = getoptionvalue("http_proxy");
635 else if (urltype == FTP_URL_T)
636 proxyenv = getoptionvalue("ftp_proxy");
638 direction = "retrieved";
639 if (! EMPTYSTRING(proxyenv)) { /* use proxy */
642 char *pport, *no_proxy;
647 /* check URL against list of no_proxied sites */
648 no_proxy = getoptionvalue("no_proxy");
649 if (! EMPTYSTRING(no_proxy)) {
650 char *np, *np_copy, *np_iter;
651 unsigned long np_port;
654 np_iter = np_copy = ftp_strdup(no_proxy);
656 while ((cp = strsep(&np_iter, " ,")) != NULL) {
659 if ((np = strrchr(cp, ':')) != NULL) {
661 np_port = strtoul(np, &ep, 10);
662 if (*np == '\0' || *ep != '\0')
664 if (np_port != portnum)
670 if (strncasecmp(host + hlen - plen,
677 if (isproxy == 0 && urltype == FTP_URL_T) {
678 rval = fetch_ftp(url);
679 goto cleanup_fetch_url;
685 warnx("Can't restart via proxy URL `%s'",
687 goto cleanup_fetch_url;
689 if (parse_url(proxyenv, "proxy URL", &purltype,
690 &puser, &ppass, &phost, &pport, &pportnum,
692 goto cleanup_fetch_url;
694 if ((!IS_HTTP_TYPE(purltype)
695 && purltype != FTP_URL_T) ||
696 EMPTYSTRING(phost) ||
697 (! EMPTYSTRING(ppath)
698 && strcmp(ppath, "/") != 0)) {
699 warnx("Malformed proxy URL `%s'",
704 goto cleanup_fetch_url;
706 if (isipv6addr(host) &&
707 strchr(host, '%') != NULL) {
709 "Scoped address notation `%s' disallowed via web proxy",
714 goto cleanup_fetch_url;
722 path = ftp_strdup(url);
726 } /* ! EMPTYSTRING(proxyenv) */
728 memset(&hints, 0, sizeof(hints));
730 hints.ai_family = family;
731 hints.ai_socktype = SOCK_STREAM;
732 hints.ai_protocol = 0;
733 error = getaddrinfo(host, port, &hints, &res0);
735 warnx("Can't LOOKUP `%s:%s': %s", host, port,
736 (error == EAI_SYSTEM) ? strerror(errno)
737 : gai_strerror(error));
738 goto cleanup_fetch_url;
740 if (res0->ai_canonname)
741 host = res0->ai_canonname;
747 for (res = res0; res; res = res->ai_next) {
748 char hname[NI_MAXHOST], sname[NI_MAXSERV];
751 if (getnameinfo(res->ai_addr, res->ai_addrlen,
752 hname, sizeof(hname), sname, sizeof(sname),
753 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
754 strlcpy(hname, "?", sizeof(hname));
755 strlcpy(sname, "?", sizeof(sname));
758 if (verbose && res0->ai_next) {
759 fprintf(ttyout, "Trying %s:%s ...\n",
763 s = socket(res->ai_family, SOCK_STREAM,
767 "Can't create socket for connection to "
768 "`%s:%s'", hname, sname);
772 if (ftp_connect(s, res->ai_addr, res->ai_addrlen,
773 verbose || !res->ai_next) < 0) {
780 if (urltype == HTTPS_URL_T) {
781 if ((ssl = fetch_start_ssl(s)) == NULL) {
794 warnx("Can't connect to `%s:%s'", host, port);
795 goto cleanup_fetch_url;
798 fin = fetch_fdopen(s, "r+");
799 fetch_set_ssl(fin, ssl);
802 * Construct and send the request.
805 fprintf(ttyout, "Requesting %s\n", url);
810 fprintf(ttyout, "%svia %s:%s", leading,
815 fetch_printf(fin, "GET %s HTTP/1.0\r\n", path);
817 fetch_printf(fin, "Pragma: no-cache\r\n");
819 fetch_printf(fin, "GET %s HTTP/1.1\r\n", path);
820 if (strchr(host, ':')) {
824 * strip off IPv6 scope identifier, since it is
827 h = ftp_strdup(host);
829 (p = strchr(h, '%')) != NULL) {
832 fetch_printf(fin, "Host: [%s]", h);
835 fetch_printf(fin, "Host: %s", host);
837 if ((urltype == HTTP_URL_T && portnum != HTTP_PORT) ||
838 (urltype == HTTPS_URL_T && portnum != HTTPS_PORT))
840 if (portnum != HTTP_PORT)
842 fetch_printf(fin, ":%u", portnum);
843 fetch_printf(fin, "\r\n");
844 fetch_printf(fin, "Accept: */*\r\n");
845 fetch_printf(fin, "Connection: close\r\n");
847 fputs(leading, ttyout);
848 fetch_printf(fin, "Range: bytes=" LLF "-\r\n",
850 fprintf(ttyout, "restarting at " LLF,
856 fetch_printf(fin, "Cache-Control: no-cache\r\n");
858 if ((useragent=getenv("FTPUSERAGENT")) != NULL) {
859 fetch_printf(fin, "User-Agent: %s\r\n", useragent);
861 fetch_printf(fin, "User-Agent: %s/%s\r\n",
862 FTP_PRODUCT, FTP_VERSION);
866 fprintf(ttyout, "%swith authorization",
871 fetch_printf(fin, "Authorization: %s\r\n", wwwauth);
876 "%swith proxy authorization", leading);
880 fetch_printf(fin, "Proxy-Authorization: %s\r\n", proxyauth);
882 if (verbose && hasleading)
883 fputs(")\n", ttyout);
884 fetch_printf(fin, "\r\n");
885 if (fetch_flush(fin) == EOF) {
886 warn("Writing HTTP request");
887 goto cleanup_fetch_url;
890 /* Read the response */
891 len = fetch_getline(fin, buf, sizeof(buf), &errormsg);
893 if (*errormsg == '\n')
895 warnx("Receiving HTTP reply: %s", errormsg);
896 goto cleanup_fetch_url;
898 while (len > 0 && (ISLWS(buf[len-1])))
900 DPRINTF("fetch_url: received `%s'\n", buf);
902 /* Determine HTTP response code */
903 cp = strchr(buf, ' ');
908 hcode = strtol(cp, &ep, 10);
909 if (*ep != '\0' && !isspace((unsigned char)*ep))
911 message = ftp_strdup(cp);
913 /* Read the rest of the header. */
915 len = fetch_getline(fin, buf, sizeof(buf), &errormsg);
917 if (*errormsg == '\n')
919 warnx("Receiving HTTP reply: %s", errormsg);
920 goto cleanup_fetch_url;
922 while (len > 0 && (ISLWS(buf[len-1])))
926 DPRINTF("fetch_url: received `%s'\n", buf);
929 * Look for some headers
934 if (match_token(&cp, "Content-Length:")) {
935 filesize = STRTOLL(cp, &ep, 10);
936 if (filesize < 0 || *ep != '\0')
938 DPRINTF("fetch_url: parsed len as: " LLF "\n",
941 } else if (match_token(&cp, "Content-Range:")) {
942 if (! match_token(&cp, "bytes"))
948 rangestart = STRTOLL(cp, &ep, 10);
949 if (rangestart < 0 || *ep != '-')
952 rangeend = STRTOLL(cp, &ep, 10);
953 if (rangeend < 0 || rangeend < rangestart)
963 entitylen = STRTOLL(cp, &ep, 10);
973 fprintf(ttyout, "parsed range as: ");
974 if (rangestart == -1)
975 fprintf(ttyout, "*");
977 fprintf(ttyout, LLF "-" LLF,
980 fprintf(ttyout, "/" LLF "\n", (LLT)entitylen);
983 if (! restart_point) {
985 "Received unexpected Content-Range header");
986 goto cleanup_fetch_url;
989 } else if (match_token(&cp, "Last-Modified:")) {
993 memset(&parsed, 0, sizeof(parsed));
994 t = parse_rfc2616time(&parsed, cp);
996 parsed.tm_isdst = -1;
998 mtime = timegm(&parsed);
1000 if (ftp_debug && mtime != -1) {
1002 "parsed time as: %s",
1003 rfc2822time(localtime(&mtime)));
1008 } else if (match_token(&cp, "Location:")) {
1009 location = ftp_strdup(cp);
1010 DPRINTF("fetch_url: parsed location as `%s'\n",
1013 } else if (match_token(&cp, "Transfer-Encoding:")) {
1014 if (match_token(&cp, "binary")) {
1016 "Bogus transfer encoding `binary' (fetching anyway)");
1019 if (! (token = match_token(&cp, "chunked"))) {
1021 "Unsupported transfer encoding `%s'",
1023 goto cleanup_fetch_url;
1026 DPRINTF("fetch_url: using chunked encoding\n");
1028 } else if (match_token(&cp, "Proxy-Authenticate:")
1029 || match_token(&cp, "WWW-Authenticate:")) {
1030 if (! (token = match_token(&cp, "Basic"))) {
1032 "fetch_url: skipping unknown auth scheme `%s'\n",
1037 auth = ftp_strdup(token);
1038 DPRINTF("fetch_url: parsed auth as `%s'\n", cp);
1042 /* finished parsing header */
1048 if (! restart_point) {
1049 warnx("Not expecting partial content header");
1050 goto cleanup_fetch_url;
1059 if (EMPTYSTRING(location)) {
1061 "No redirection Location provided by server");
1062 goto cleanup_fetch_url;
1064 if (redirect_loop++ > 5) {
1065 warnx("Too many redirections requested");
1066 goto cleanup_fetch_url;
1070 fprintf(ttyout, "Redirected via %s\n",
1072 rval = fetch_url(url, location,
1073 proxyauth, wwwauth);
1076 fprintf(ttyout, "Redirected to %s\n",
1078 rval = go_fetch(location);
1080 goto cleanup_fetch_url;
1086 char *auser, *apass;
1097 if (verbose || *authp == NULL ||
1098 auser == NULL || apass == NULL)
1099 fprintf(ttyout, "%s\n", message);
1100 if (EMPTYSTRING(auth)) {
1102 "No authentication challenge provided by server");
1103 goto cleanup_fetch_url;
1105 if (*authp != NULL) {
1109 "Authorization failed. Retry (y/n)? ");
1110 if (get_line(stdin, reply, sizeof(reply), NULL)
1112 goto cleanup_fetch_url;
1114 if (tolower((unsigned char)reply[0]) != 'y')
1115 goto cleanup_fetch_url;
1119 if (auth_url(auth, authp, auser, apass) == 0) {
1120 rval = fetch_url(url, proxyenv,
1121 proxyauth, wwwauth);
1122 memset(*authp, 0, strlen(*authp));
1125 goto cleanup_fetch_url;
1130 warnx("Error retrieving file `%s'", message);
1132 warnx("Unknown error retrieving file");
1133 goto cleanup_fetch_url;
1135 } /* end of ftp:// or http:// specific setup */
1137 /* Open the output file. */
1138 if (strcmp(savefile, "-") == 0) {
1140 } else if (*savefile == '|') {
1141 oldintp = xsignal(SIGPIPE, SIG_IGN);
1142 fout = popen(savefile + 1, "w");
1144 warn("Can't execute `%s'", savefile + 1);
1145 goto cleanup_fetch_url;
1149 if ((rangeend != -1 && rangeend <= restart_point) ||
1150 (rangestart == -1 && filesize != -1 && filesize <= restart_point)) {
1153 fprintf(ttyout, "already done\n");
1155 goto cleanup_fetch_url;
1157 if (restart_point && rangestart != -1) {
1158 if (entitylen != -1)
1159 filesize = entitylen;
1160 if (rangestart != restart_point) {
1162 "Size of `%s' differs from save file `%s'",
1164 goto cleanup_fetch_url;
1166 fout = fopen(savefile, "a");
1168 fout = fopen(savefile, "w");
1170 warn("Can't open `%s'", savefile);
1171 goto cleanup_fetch_url;
1177 if (sigsetjmp(httpabort, 1))
1178 goto cleanup_fetch_url;
1179 (void)xsignal(SIGQUIT, psummary);
1180 oldintr = xsignal(SIGINT, aborthttp);
1182 assert(rcvbuf_size > 0);
1183 if ((size_t)rcvbuf_size > bufsize) {
1185 (void)free(xferbuf);
1186 bufsize = rcvbuf_size;
1187 xferbuf = ftp_malloc(bufsize);
1194 /* Finally, suck down the file. */
1201 /* read chunk-size */
1203 if (fetch_getln(xferbuf, bufsize, fin) == NULL) {
1204 warnx("Unexpected EOF reading chunk-size");
1205 goto cleanup_fetch_url;
1208 chunksize = strtol(xferbuf, &ep, 16);
1209 if (ep == xferbuf) {
1210 warnx("Invalid chunk-size");
1211 goto cleanup_fetch_url;
1213 if (errno == ERANGE || chunksize < 0) {
1215 warn("Chunk-size `%.*s'",
1216 (int)(ep-xferbuf), xferbuf);
1217 goto cleanup_fetch_url;
1221 * XXX: Work around bug in Apache 1.3.9 and
1222 * 1.3.11, which incorrectly put trailing
1223 * space after the chunk-size.
1228 /* skip [ chunk-ext ] */
1230 while (*ep && *ep != '\r')
1234 if (strcmp(ep, "\r\n") != 0) {
1235 warnx("Unexpected data following chunk-size");
1236 goto cleanup_fetch_url;
1238 DPRINTF("fetch_url: got chunk-size of " LLF "\n",
1240 if (chunksize == 0) {
1245 /* transfer file or chunk */
1247 struct timeval then, now, td;
1251 (void)gettimeofday(&then, NULL);
1252 bufrem = rate_get ? rate_get : (off_t)bufsize;
1254 bufrem = MIN(chunksize, bufrem);
1255 while (bufrem > 0) {
1256 flen = fetch_read(xferbuf, sizeof(char),
1257 MIN((off_t)bufsize, bufrem), fin);
1262 if (fwrite(xferbuf, sizeof(char), flen, fout)
1264 warn("Writing `%s'", savefile);
1265 goto cleanup_fetch_url;
1267 if (hash && !progress) {
1268 while (bytes >= hashbytes) {
1269 (void)putc('#', ttyout);
1272 (void)fflush(ttyout);
1282 (void)gettimeofday(&now, NULL);
1283 timersub(&now, &then, &td);
1286 usleep(1000000 - td.tv_usec);
1289 if (ischunked && chunksize <= 0)
1292 /* read CRLF after chunk*/
1295 if (fetch_getln(xferbuf, bufsize, fin) == NULL) {
1296 warnx("Unexpected EOF reading chunk CRLF");
1297 goto cleanup_fetch_url;
1299 if (strcmp(xferbuf, "\r\n") != 0) {
1300 warnx("Unexpected data following chunk");
1301 goto cleanup_fetch_url;
1306 } while (ischunked);
1308 /* XXX: deal with optional trailer & CRLF here? */
1310 if (hash && !progress && bytes > 0) {
1312 (void)putc('#', ttyout);
1313 (void)putc('\n', ttyout);
1315 if (fetch_error(fin)) {
1316 warn("Reading file");
1317 goto cleanup_fetch_url;
1321 if (closefunc == fclose && mtime != -1) {
1322 struct timeval tval[2];
1324 (void)gettimeofday(&tval[0], NULL);
1325 tval[1].tv_sec = mtime;
1326 tval[1].tv_usec = 0;
1330 if (utimes(savefile, tval) == -1) {
1332 "Can't change modification time to %s",
1333 rfc2822time(localtime(&mtime)));
1341 goto cleanup_fetch_url;
1344 warnx("Improper response from `%s:%s'", host, port);
1348 (void)xsignal(SIGINT, oldintr);
1350 (void)xsignal(SIGPIPE, oldintp);
1355 if (closefunc != NULL && fout != NULL)
1362 memset(pass, 0, strlen(pass));
1367 FREEPTR(decodedpath);
1370 memset(ppass, 0, strlen(ppass));
1379 * Abort a HTTP retrieval
1382 aborthttp(int notused)
1389 len = strlcpy(msgbuf, "\nHTTP fetch aborted.\n", sizeof(msgbuf));
1390 write(fileno(ttyout), msgbuf, len);
1391 siglongjmp(httpabort, 1);
1395 * Retrieve ftp URL or classic ftp argument using FTP.
1396 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
1397 * is still open (e.g, ftp xfer with trailing /)
1400 fetch_ftp(const char *url)
1402 char *cp, *xargv[5], rempath[MAXPATHLEN];
1403 char *host, *path, *dir, *file, *uuser, *pass;
1405 char cmdbuf[MAXPATHLEN];
1407 int dirhasglob, filehasglob, rval, transtype, xargc;
1408 int oanonftp, oautologin;
1412 DPRINTF("fetch_ftp: `%s'\n", url);
1413 host = path = dir = file = uuser = pass = NULL;
1418 if (STRNEQUAL(url, FTP_URL)) {
1419 if ((parse_url(url, "URL", &urltype, &uuser, &pass,
1420 &host, &port, &portnum, &path) == -1) ||
1421 (uuser != NULL && *uuser == '\0') ||
1422 EMPTYSTRING(host)) {
1423 warnx("Invalid URL `%s'", url);
1424 goto cleanup_fetch_ftp;
1427 * Note: Don't url_decode(path) here. We need to keep the
1428 * distinction between "/" and "%2F" until later.
1431 /* check for trailing ';type=[aid]' */
1432 if (! EMPTYSTRING(path) && (cp = strrchr(path, ';')) != NULL) {
1433 if (strcasecmp(cp, ";type=a") == 0)
1435 else if (strcasecmp(cp, ";type=i") == 0)
1437 else if (strcasecmp(cp, ";type=d") == 0) {
1439 "Directory listing via a URL is not supported");
1440 goto cleanup_fetch_ftp;
1442 warnx("Invalid suffix `%s' in URL `%s'", cp,
1444 goto cleanup_fetch_ftp;
1448 } else { /* classic style `[user@]host:[file]' */
1449 urltype = CLASSIC_URL_T;
1450 host = ftp_strdup(url);
1451 cp = strchr(host, '@');
1455 anonftp = 0; /* disable anonftp */
1456 host = ftp_strdup(cp + 1);
1458 cp = strchr(host, ':');
1461 path = ftp_strdup(cp + 1);
1464 if (EMPTYSTRING(host))
1465 goto cleanup_fetch_ftp;
1467 /* Extract the file and (if present) directory name. */
1469 if (! EMPTYSTRING(dir)) {
1471 * If we are dealing with classic `[user@]host:[path]' syntax,
1472 * then a path of the form `/file' (resulting from input of the
1473 * form `host:/file') means that we should do "CWD /" before
1474 * retrieving the file. So we set dir="/" and file="file".
1476 * But if we are dealing with URLs like `ftp://host/path' then
1477 * a path of the form `/file' (resulting from a URL of the form
1478 * `ftp://host//file') means that we should do `CWD ' (with an
1479 * empty argument) before retrieving the file. So we set
1480 * dir="" and file="file".
1482 * If the path does not contain / at all, we set dir=NULL.
1483 * (We get a path without any slashes if we are dealing with
1484 * classic `[user@]host:[file]' or URL `ftp://host/file'.)
1486 * In all other cases, we set dir to a string that does not
1487 * include the final '/' that separates the dir part from the
1488 * file part of the path. (This will be the empty string if
1489 * and only if we are dealing with a path of the form `/file'
1490 * resulting from an URL of the form `ftp://host//file'.)
1492 cp = strrchr(dir, '/');
1493 if (cp == dir && urltype == CLASSIC_URL_T) {
1495 (void)strlcpy(dirbuf, "/", sizeof(dirbuf));
1497 } else if (cp != NULL) {
1506 if (urltype == FTP_URL_T && file != NULL) {
1508 /* but still don't url_decode(dir) */
1510 DPRINTF("fetch_ftp: user `%s' pass `%s' host %s port %s "
1511 "path `%s' dir `%s' file `%s'\n",
1512 STRorNULL(uuser), STRorNULL(pass),
1513 STRorNULL(host), STRorNULL(port),
1514 STRorNULL(path), STRorNULL(dir), STRorNULL(file));
1516 dirhasglob = filehasglob = 0;
1517 if (doglob && urltype == CLASSIC_URL_T) {
1518 if (! EMPTYSTRING(dir) && strpbrk(dir, "*?[]{}") != NULL)
1520 if (! EMPTYSTRING(file) && strpbrk(file, "*?[]{}") != NULL)
1524 /* Set up the connection */
1527 disconnect(0, NULL);
1529 (void)strlcpy(cmdbuf, getprogname(), sizeof(cmdbuf));
1539 oautologin = autologin;
1540 /* don't autologin in setpeer(), use ftp_login() below */
1542 setpeer(xargc, xargv);
1543 autologin = oautologin;
1544 if ((connected == 0) ||
1545 (connected == 1 && !ftp_login(host, uuser, pass))) {
1546 warnx("Can't connect or login to host `%s:%s'",
1547 host, port ? port : "?");
1548 goto cleanup_fetch_ftp;
1551 switch (transtype) {
1556 setbinary(1, xargv);
1559 errx(1, "fetch_ftp: unknown transfer type %d", transtype);
1563 * Change directories, if necessary.
1565 * Note: don't use EMPTYSTRING(dir) below, because
1566 * dir=="" means something different from dir==NULL.
1568 if (dir != NULL && !dirhasglob) {
1572 * If we are dealing with a classic `[user@]host:[path]'
1573 * (urltype is CLASSIC_URL_T) then we have a raw directory
1574 * name (not encoded in any way) and we can change
1575 * directories in one step.
1577 * If we are dealing with an `ftp://host/path' URL
1578 * (urltype is FTP_URL_T), then RFC 3986 says we need to
1579 * send a separate CWD command for each unescaped "/"
1580 * in the path, and we have to interpret %hex escaping
1581 * *after* we find the slashes. It's possible to get
1582 * empty components here, (from multiple adjacent
1583 * slashes in the path) and RFC 3986 says that we should
1584 * still do `CWD ' (with a null argument) in such cases.
1586 * Many ftp servers don't support `CWD ', so if there's an
1587 * error performing that command, bail out with a descriptive
1592 * host: dir="", urltype=CLASSIC_URL_T
1593 * logged in (to default directory)
1594 * host:file dir=NULL, urltype=CLASSIC_URL_T
1596 * host:dir/ dir="dir", urltype=CLASSIC_URL_T
1597 * "CWD dir", logged in
1598 * ftp://host/ dir="", urltype=FTP_URL_T
1599 * logged in (to default directory)
1600 * ftp://host/dir/ dir="dir", urltype=FTP_URL_T
1601 * "CWD dir", logged in
1602 * ftp://host/file dir=NULL, urltype=FTP_URL_T
1604 * ftp://host//file dir="", urltype=FTP_URL_T
1605 * "CWD ", "RETR file"
1606 * host:/file dir="/", urltype=CLASSIC_URL_T
1607 * "CWD /", "RETR file"
1608 * ftp://host///file dir="/", urltype=FTP_URL_T
1609 * "CWD ", "CWD ", "RETR file"
1610 * ftp://host/%2F/file dir="%2F", urltype=FTP_URL_T
1611 * "CWD /", "RETR file"
1612 * ftp://host/foo/file dir="foo", urltype=FTP_URL_T
1613 * "CWD foo", "RETR file"
1614 * ftp://host/foo/bar/file dir="foo/bar"
1615 * "CWD foo", "CWD bar", "RETR file"
1616 * ftp://host//foo/bar/file dir="/foo/bar"
1617 * "CWD ", "CWD foo", "CWD bar", "RETR file"
1618 * ftp://host/foo//bar/file dir="foo//bar"
1619 * "CWD foo", "CWD ", "CWD bar", "RETR file"
1620 * ftp://host/%2F/foo/bar/file dir="%2F/foo/bar"
1621 * "CWD /", "CWD foo", "CWD bar", "RETR file"
1622 * ftp://host/%2Ffoo/bar/file dir="%2Ffoo/bar"
1623 * "CWD /foo", "CWD bar", "RETR file"
1624 * ftp://host/%2Ffoo%2Fbar/file dir="%2Ffoo%2Fbar"
1625 * "CWD /foo/bar", "RETR file"
1626 * ftp://host/%2Ffoo%2Fbar%2Ffile dir=NULL
1627 * "RETR /foo/bar/file"
1629 * Note that we don't need `dir' after this point.
1632 if (urltype == FTP_URL_T) {
1633 nextpart = strchr(dir, '/');
1641 DPRINTF("fetch_ftp: dir `%s', nextpart `%s'\n",
1642 STRorNULL(dir), STRorNULL(nextpart));
1643 if (urltype == FTP_URL_T || *dir != '\0') {
1644 (void)strlcpy(cmdbuf, "cd", sizeof(cmdbuf));
1651 if (*dir == '\0' && code == 500)
1654 "ftp: The `CWD ' command (without a directory), which is required by\n"
1655 " RFC 3986 to support the empty directory in the URL pathname (`//'),\n"
1656 " conflicts with the server's conformance to RFC 959.\n"
1657 " Try the same URL without the `//' in the URL pathname.\n"
1659 goto cleanup_fetch_ftp;
1663 } while (dir != NULL);
1666 if (EMPTYSTRING(file)) {
1668 goto cleanup_fetch_ftp;
1672 (void)strlcpy(rempath, dir, sizeof(rempath));
1673 (void)strlcat(rempath, "/", sizeof(rempath));
1674 (void)strlcat(rempath, file, sizeof(rempath));
1678 /* Fetch the file(s). */
1680 (void)strlcpy(cmdbuf, "get", sizeof(cmdbuf));
1684 if (dirhasglob || filehasglob) {
1687 ointeractive = interactive;
1689 if (restartautofetch)
1690 (void)strlcpy(cmdbuf, "mreget", sizeof(cmdbuf));
1692 (void)strlcpy(cmdbuf, "mget", sizeof(cmdbuf));
1695 interactive = ointeractive;
1697 if (outfile == NULL) {
1698 cp = strrchr(file, '/'); /* find savefile */
1704 xargv[2] = (char *)outfile;
1707 if (restartautofetch)
1708 reget(xargc, xargv);
1713 if ((code / 100) == COMPLETE)
1722 memset(pass, 0, strlen(pass));
1728 * Retrieve the given file to outfile.
1729 * Supports arguments of the form:
1730 * "host:path", "ftp://host/path" if $ftpproxy, call fetch_url() else
1732 * "http://host/path" call fetch_url() to use HTTP
1733 * "file:///path" call fetch_url() to copy
1734 * "about:..." print a message
1736 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
1737 * is still open (e.g, ftp xfer with trailing /)
1740 go_fetch(const char *url)
1749 if (STRNEQUAL(url, ABOUT_URL)) {
1750 url += sizeof(ABOUT_URL) -1;
1751 if (strcasecmp(url, "ftp") == 0 ||
1752 strcasecmp(url, "tnftp") == 0) {
1754 "This version of ftp has been enhanced by Luke Mewburn <lukem@NetBSD.org>\n"
1755 "for the NetBSD project. Execute `man ftp' for more details.\n", ttyout);
1756 } else if (strcasecmp(url, "lukem") == 0) {
1758 "Luke Mewburn is the author of most of the enhancements in this ftp client.\n"
1759 "Please email feedback to <lukem@NetBSD.org>.\n", ttyout);
1760 } else if (strcasecmp(url, "netbsd") == 0) {
1762 "NetBSD is a freely available and redistributable UNIX-like operating system.\n"
1763 "For more information, see http://www.NetBSD.org/\n", ttyout);
1764 } else if (strcasecmp(url, "version") == 0) {
1765 fprintf(ttyout, "Version: %s %s%s\n",
1766 FTP_PRODUCT, FTP_VERSION,
1774 fprintf(ttyout, "`%s' is an interesting topic.\n", url);
1776 fputs("\n", ttyout);
1782 * Check for file:// and http:// URLs.
1784 if (STRNEQUAL(url, HTTP_URL)
1786 || STRNEQUAL(url, HTTPS_URL)
1788 || STRNEQUAL(url, FILE_URL))
1789 return (fetch_url(url, NULL, NULL, NULL));
1792 * If it contains "://" but does not begin with ftp://
1793 * or something that was already handled, then it's
1796 * If it contains ":" but not "://" then we assume the
1797 * part before the colon is a host name, not an URL scheme,
1798 * so we don't try to match that here.
1800 if ((p = strstr(url, "://")) != NULL && ! STRNEQUAL(url, FTP_URL))
1801 errx(1, "Unsupported URL scheme `%.*s'", (int)(p - url), url);
1804 * Try FTP URL-style and host:file arguments next.
1805 * If ftpproxy is set with an FTP URL, use fetch_url()
1806 * Othewise, use fetch_ftp().
1808 proxyenv = getoptionvalue("ftp_proxy");
1809 if (!EMPTYSTRING(proxyenv) && STRNEQUAL(url, FTP_URL))
1810 return (fetch_url(url, NULL, NULL, NULL));
1812 return (fetch_ftp(url));
1816 * Retrieve multiple files from the command line,
1817 * calling go_fetch() for each file.
1819 * If an ftp path has a trailing "/", the path will be cd-ed into and
1820 * the connection remains open, and the function will return -1
1821 * (to indicate the connection is alive).
1822 * If an error occurs the return value will be the offset+1 in
1823 * argv[] of the file that caused a problem (i.e, argv[x]
1825 * Otherwise, 0 is returned if all files retrieved successfully.
1828 auto_fetch(int argc, char *argv[])
1830 volatile int argpos, rval;
1834 if (sigsetjmp(toplevel, 1)) {
1836 disconnect(0, NULL);
1841 (void)xsignal(SIGINT, intr);
1842 (void)xsignal(SIGPIPE, lostpeer);
1845 * Loop through as long as there's files to fetch.
1847 for (; (rval == 0) && (argpos < argc); argpos++) {
1848 if (strchr(argv[argpos], ':') == NULL)
1852 anonftp = 2; /* Handle "automatic" transfers. */
1853 rval = go_fetch(argv[argpos]);
1854 if (outfile != NULL && strcmp(outfile, "-") != 0
1855 && outfile[0] != '|')
1861 if (connected && rval != -1)
1862 disconnect(0, NULL);
1868 * Upload multiple files from the command line.
1870 * If an error occurs the return value will be the offset+1 in
1871 * argv[] of the file that caused a problem (i.e, argv[x]
1873 * Otherwise, 0 is returned if all files uploaded successfully.
1876 auto_put(int argc, char **argv, const char *uploadserver)
1878 char *uargv[4], *path, *pathsep;
1879 int uargc, rval, argpos;
1881 char cmdbuf[MAX_C_NAME];
1883 (void)strlcpy(cmdbuf, "mput", sizeof(cmdbuf));
1887 uargv[2] = uargv[3] = NULL;
1891 DPRINTF("auto_put: target `%s'\n", uploadserver);
1893 path = ftp_strdup(uploadserver);
1895 if (path[len - 1] != '/' && path[len - 1] != ':') {
1897 * make sure we always pass a directory to auto_fetch
1899 if (argc > 1) { /* more than one file to upload */
1900 len = strlen(uploadserver) + 2; /* path + "/" + "\0" */
1902 path = (char *)ftp_malloc(len);
1903 (void)strlcpy(path, uploadserver, len);
1904 (void)strlcat(path, "/", len);
1905 } else { /* single file to upload */
1906 (void)strlcpy(cmdbuf, "put", sizeof(cmdbuf));
1908 pathsep = strrchr(path, '/');
1909 if (pathsep == NULL) {
1910 pathsep = strrchr(path, ':');
1911 if (pathsep == NULL) {
1912 warnx("Invalid URL `%s'", path);
1913 goto cleanup_auto_put;
1916 uargv[2] = ftp_strdup(pathsep);
1919 uargv[2] = ftp_strdup(pathsep + 1);
1924 DPRINTF("auto_put: URL `%s' argv[2] `%s'\n",
1925 path, STRorNULL(uargv[2]));
1927 /* connect and cwd */
1928 rval = auto_fetch(1, &path);
1930 goto cleanup_auto_put;
1934 /* target filename provided; upload 1 file */
1935 /* XXX : is this the best way? */
1939 if ((code / 100) != COMPLETE)
1941 } else { /* otherwise a target dir: upload all files to it */
1942 for(argpos = 0; argv[argpos] != NULL; argpos++) {
1943 uargv[1] = argv[argpos];
1945 if ((code / 100) != COMPLETE) {