[BACK]Return to fetch.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ftp

Annotation of src/usr.bin/ftp/fetch.c, Revision 1.50

1.50    ! deraadt     1: /*     $OpenBSD: fetch.c,v 1.49 2004/02/28 20:08:38 krw Exp $  */
1.15      millert     2: /*     $NetBSD: fetch.c,v 1.14 1997/08/18 10:20:20 lukem Exp $ */
1.1       millert     3:
                      4: /*-
                      5:  * Copyright (c) 1997 The NetBSD Foundation, Inc.
                      6:  * All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to The NetBSD Foundation
                      9:  * by Jason Thorpe and Luke Mewburn.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *        This product includes software developed by the NetBSD
                     22:  *        Foundation, Inc. and its contributors.
                     23:  * 4. Neither the name of The NetBSD Foundation nor the names of its
                     24:  *    contributors may be used to endorse or promote products derived
                     25:  *    from this software without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     28:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     29:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1.15      millert    30:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     31:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1.1       millert    32:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     33:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     34:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     35:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     36:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     37:  * POSSIBILITY OF SUCH DAMAGE.
                     38:  */
                     39:
1.48      deraadt    40: #if !defined(lint) && !defined(SMALL)
1.50    ! deraadt    41: static char rcsid[] = "$OpenBSD: fetch.c,v 1.49 2004/02/28 20:08:38 krw Exp $";
1.48      deraadt    42: #endif /* not lint and not SMALL */
1.1       millert    43:
                     44: /*
                     45:  * FTP User Program -- Command line file retrieval
                     46:  */
                     47:
                     48: #include <sys/types.h>
                     49: #include <sys/param.h>
                     50: #include <sys/socket.h>
1.22      deraadt    51: #include <sys/stat.h>
1.1       millert    52:
                     53: #include <netinet/in.h>
                     54:
                     55: #include <arpa/ftp.h>
                     56: #include <arpa/inet.h>
                     57:
                     58: #include <ctype.h>
                     59: #include <err.h>
1.17      millert    60: #include <libgen.h>
1.1       millert    61: #include <netdb.h>
                     62: #include <fcntl.h>
1.3       millert    63: #include <signal.h>
1.1       millert    64: #include <stdio.h>
1.19      deraadt    65: #include <errno.h>
1.1       millert    66: #include <stdlib.h>
                     67: #include <string.h>
                     68: #include <unistd.h>
1.40      fgsch      69: #include <util.h>
1.1       millert    70:
                     71: #include "ftp_var.h"
                     72:
1.38      millert    73: static int     url_get(const char *, const char *, const char *);
                     74: void           aborthttp(int);
                     75: void           abortfile(int);
1.42      deraadt    76: char           hextochar(const char *);
                     77: char           *urldecode(const char *);
1.14      millert    78:
1.1       millert    79: #define        FTP_URL         "ftp://"        /* ftp URL prefix */
                     80: #define        HTTP_URL        "http://"       /* http URL prefix */
1.22      deraadt    81: #define        FILE_URL        "file:"         /* file URL prefix */
1.6       millert    82: #define FTP_PROXY      "ftp_proxy"     /* env var with ftp proxy location */
1.1       millert    83: #define HTTP_PROXY     "http_proxy"    /* env var with http proxy location */
                     84:
                     85:
                     86: #define EMPTYSTRING(x) ((x) == NULL || (*(x) == '\0'))
                     87:
1.37      heko       88: static const char *at_encoding_warning =
                     89:    "Extra `@' characters in usernames and passwords should be encoded as %%40";
                     90:
1.1       millert    91: jmp_buf        httpabort;
                     92:
                     93: /*
1.6       millert    94:  * Retrieve URL, via the proxy in $proxyvar if necessary.
1.1       millert    95:  * Modifies the string argument given.
                     96:  * Returns -1 on failure, 0 on success
                     97:  */
1.14      millert    98: static int
1.50    ! deraadt    99: url_get(const char *origline, const char *proxyenv, const char *outfile)
1.1       millert   100: {
1.25      itojun    101:        struct addrinfo hints, *res0, *res;
                    102:        int error;
1.40      fgsch     103:        int i, isftpurl, isfileurl, isredirect;
1.34      millert   104:        volatile int s, out;
1.14      millert   105:        size_t len;
1.40      fgsch     106:        char *cp, *ep, *portnum, *path;
1.44      itojun    107:        char pbuf[NI_MAXSERV], hbuf[NI_MAXHOST];
1.34      millert   108:        const char * volatile savefile;
1.40      fgsch     109:        char *line, *host, *port, *buf;
1.34      millert   110:        char * volatile proxy;
1.28      itojun    111:        char *hosttail;
1.14      millert   112:        volatile sig_t oldintr;
1.1       millert   113:        off_t hashbytes;
1.25      itojun    114:        char *cause = "unknown";
1.40      fgsch     115:        FILE *fin;
                    116:        int rval;
1.1       millert   117:
                    118:        s = -1;
                    119:        proxy = NULL;
1.40      fgsch     120:        fin = NULL;
                    121:        buf = NULL;
1.14      millert   122:        isftpurl = 0;
1.22      deraadt   123:        isfileurl = 0;
1.40      fgsch     124:        isredirect = 0;
                    125:        rval = -1;
1.14      millert   126:
                    127:        line = strdup(origline);
                    128:        if (line == NULL)
                    129:                errx(1, "Can't allocate memory to parse URL");
1.7       millert   130:        if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
1.6       millert   131:                host = line + sizeof(HTTP_URL) - 1;
1.14      millert   132:        else if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.6       millert   133:                host = line + sizeof(FTP_URL) - 1;
1.14      millert   134:                isftpurl = 1;
1.22      deraadt   135:        } else if (strncasecmp(line, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
                    136:                host = line + sizeof(FILE_URL) - 1;
                    137:                isfileurl = 1;
1.14      millert   138:        } else
                    139:                errx(1, "url_get: Invalid URL '%s'", line);
1.6       millert   140:
1.22      deraadt   141:        if (isfileurl) {
                    142:                path = host;
                    143:        } else {
                    144:                path = strchr(host, '/');               /* find path */
                    145:                if (EMPTYSTRING(path)) {
                    146:                        if (isftpurl)
                    147:                                goto noftpautologin;
                    148:                        warnx("Invalid URL (no `/' after host): %s", origline);
                    149:                        goto cleanup_url_get;
                    150:                }
                    151:                *path++ = '\0';
                    152:                if (EMPTYSTRING(path)) {
                    153:                        if (isftpurl)
                    154:                                goto noftpautologin;
                    155:                        warnx("Invalid URL (no file after host): %s", origline);
                    156:                        goto cleanup_url_get;
                    157:                }
1.14      millert   158:        }
1.1       millert   159:
1.17      millert   160:        if (outfile)
                    161:                savefile = outfile;
1.1       millert   162:        else
1.17      millert   163:                savefile = basename(path);
                    164:
1.14      millert   165:        if (EMPTYSTRING(savefile)) {
                    166:                if (isftpurl)
                    167:                        goto noftpautologin;
                    168:                warnx("Invalid URL (no file after directory): %s", origline);
1.6       millert   169:                goto cleanup_url_get;
1.14      millert   170:        }
1.1       millert   171:
                    172:        if (proxyenv != NULL) {                         /* use proxy */
                    173:                proxy = strdup(proxyenv);
                    174:                if (proxy == NULL)
1.14      millert   175:                        errx(1, "Can't allocate memory for proxy URL.");
1.7       millert   176:                if (strncasecmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
1.6       millert   177:                        host = proxy + sizeof(HTTP_URL) - 1;
1.7       millert   178:                else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0)
1.6       millert   179:                        host = proxy + sizeof(FTP_URL) - 1;
                    180:                else {
1.14      millert   181:                        warnx("Malformed proxy URL: %s", proxyenv);
1.6       millert   182:                        goto cleanup_url_get;
                    183:                }
1.14      millert   184:                if (EMPTYSTRING(host)) {
                    185:                        warnx("Malformed proxy URL: %s", proxyenv);
1.6       millert   186:                        goto cleanup_url_get;
1.14      millert   187:                }
1.1       millert   188:                *--path = '/';                  /* add / back to real path */
                    189:                path = strchr(host, '/');       /* remove trailing / on host */
1.42      deraadt   190:                if (!EMPTYSTRING(path))
1.1       millert   191:                        *path++ = '\0';
                    192:                path = line;
                    193:        }
                    194:
1.22      deraadt   195:        if (isfileurl) {
                    196:                struct stat st;
                    197:
                    198:                s = open(path, O_RDONLY);
                    199:                if (s == -1) {
                    200:                        warn("Can't open file %s", path);
                    201:                        goto cleanup_url_get;
                    202:                }
                    203:
                    204:                if (fstat(s, &st) == -1)
                    205:                        filesize = -1;
                    206:                else
                    207:                        filesize = st.st_size;
                    208:
                    209:                /* Open the output file.  */
                    210:                if (strcmp(savefile, "-") != 0) {
                    211:                        out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
                    212:                        if (out < 0) {
                    213:                                warn("Can't open %s", savefile);
                    214:                                goto cleanup_url_get;
                    215:                        }
                    216:                } else
                    217:                        out = fileno(stdout);
                    218:
                    219:                /* Trap signals */
                    220:                oldintr = NULL;
                    221:                if (setjmp(httpabort)) {
                    222:                        if (oldintr)
                    223:                                (void)signal(SIGINT, oldintr);
                    224:                        goto cleanup_url_get;
                    225:                }
                    226:                oldintr = signal(SIGINT, abortfile);
1.42      deraadt   227:
1.22      deraadt   228:                bytes = 0;
                    229:                hashbytes = mark;
                    230:                progressmeter(-1);
1.40      fgsch     231:
                    232:                if ((buf = malloc(4096)) == NULL)
1.47      deraadt   233:                        errx(1, "Can't allocate memory for transfer buffer");
1.42      deraadt   234:
1.22      deraadt   235:                /* Finally, suck down the file. */
                    236:                i = 0;
1.40      fgsch     237:                while ((len = read(s, buf, 4096)) > 0) {
1.22      deraadt   238:                        bytes += len;
                    239:                        for (cp = buf; len > 0; len -= i, cp += i) {
                    240:                                if ((i = write(out, cp, len)) == -1) {
                    241:                                        warn("Writing %s", savefile);
                    242:                                        goto cleanup_url_get;
                    243:                                }
                    244:                                else if (i == 0)
                    245:                                        break;
                    246:                        }
                    247:                        if (hash && !progress) {
                    248:                                while (bytes >= hashbytes) {
                    249:                                        (void)putc('#', ttyout);
                    250:                                        hashbytes += mark;
                    251:                                }
                    252:                                (void)fflush(ttyout);
                    253:                        }
                    254:                }
                    255:                if (hash && !progress && bytes > 0) {
                    256:                        if (bytes < mark)
                    257:                                (void)putc('#', ttyout);
                    258:                        (void)putc('\n', ttyout);
                    259:                        (void)fflush(ttyout);
                    260:                }
                    261:                if (len != 0) {
                    262:                        warn("Reading from file");
                    263:                        goto cleanup_url_get;
                    264:                }
                    265:                progressmeter(1);
                    266:                if (verbose)
                    267:                        fputs("Successfully retrieved file.\n", ttyout);
                    268:                (void)signal(SIGINT, oldintr);
1.42      deraadt   269:
1.40      fgsch     270:                rval = 0;
                    271:                goto cleanup_url_get;
1.22      deraadt   272:        }
                    273:
1.28      itojun    274:        if (*host == '[' && (hosttail = strrchr(host, ']')) != NULL &&
                    275:            (hosttail[1] == '\0' || hosttail[1] == ':')) {
                    276:                host++;
                    277:                *hosttail++ = '\0';
                    278:        } else
                    279:                hosttail = host;
                    280:
                    281:        portnum = strrchr(hosttail, ':');               /* find portnum */
1.1       millert   282:        if (portnum != NULL)
                    283:                *portnum++ = '\0';
                    284:
                    285:        if (debug)
1.10      deraadt   286:                fprintf(ttyout, "host %s, port %s, path %s, save as %s.\n",
1.1       millert   287:                    host, portnum, path, savefile);
                    288:
1.25      itojun    289:        memset(&hints, 0, sizeof(hints));
1.39      deraadt   290:        hints.ai_family = family;
1.25      itojun    291:        hints.ai_socktype = SOCK_STREAM;
                    292:        port = portnum ? portnum : httpport;
                    293:        error = getaddrinfo(host, port, &hints, &res0);
1.30      deraadt   294:        if (error == EAI_SERVICE && port == httpport) {
                    295:                /*
                    296:                 * If the services file is corrupt/missing, fall back
                    297:                 * on our hard-coded defines.
                    298:                 */
                    299:                snprintf(pbuf, sizeof(pbuf), "%d", HTTP_PORT);
                    300:                error = getaddrinfo(host, pbuf, &hints, &res0);
                    301:        }
1.25      itojun    302:        if (error) {
                    303:                warnx("%s: %s", gai_strerror(error), host);
                    304:                goto cleanup_url_get;
1.1       millert   305:        }
                    306:
1.25      itojun    307:        s = -1;
                    308:        for (res = res0; res; res = res->ai_next) {
1.44      itojun    309:                if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
                    310:                    sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
                    311:                        strlcpy(hbuf, "(unknown)", sizeof(hbuf));
1.41      deraadt   312:                if (verbose)
1.44      itojun    313:                        fprintf(ttyout, "Trying %s...\n", hbuf);
1.14      millert   314:
1.25      itojun    315:                s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
                    316:                if (s == -1) {
                    317:                        cause = "socket";
                    318:                        continue;
1.1       millert   319:                }
                    320:
1.25      itojun    321: again:
                    322:                if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
                    323:                        if (errno == EINTR)
                    324:                                goto again;
                    325:                        close(s);
                    326:                        s = -1;
                    327:                        cause = "connect";
1.19      deraadt   328:                        continue;
                    329:                }
1.25      itojun    330:
1.29      itojun    331:                /* get port in numeric */
                    332:                if (getnameinfo(res->ai_addr, res->ai_addrlen, NULL, 0,
                    333:                    pbuf, sizeof(pbuf), NI_NUMERICSERV) == 0)
                    334:                        port = pbuf;
                    335:                else
                    336:                        port = NULL;
                    337:
1.25      itojun    338:                break;
                    339:        }
                    340:        freeaddrinfo(res0);
                    341:        if (s < 0) {
1.33      millert   342:                warn("%s", cause);
1.6       millert   343:                goto cleanup_url_get;
1.1       millert   344:        }
                    345:
1.40      fgsch     346:        fin = fdopen(s, "r+");
                    347:
1.1       millert   348:        /*
1.40      fgsch     349:         * Construct and send the request. Proxy requests don't want leading /.
1.1       millert   350:         */
1.32      itojun    351:        if (proxy) {
                    352:                /*
                    353:                 * Host: directive must use the destination host address for
                    354:                 * the original URI (path).  We do not attach it at this moment.
                    355:                 */
                    356:                if (verbose)
1.23      deraadt   357:                        fprintf(ttyout, "Requesting %s (via %s)\n",
                    358:                            origline, proxyenv);
1.40      fgsch     359:                fprintf(fin, "GET %s HTTP/1.0\r\n%s\r\n\r\n", path, HTTP_USER_AGENT);
1.28      itojun    360:        } else {
1.32      itojun    361:                if (verbose)
                    362:                        fprintf(ttyout, "Requesting %s\n", origline);
                    363:                if (strchr(host, ':')) {
                    364:                        char *h, *p;
                    365:
                    366:                        /* strip off scoped address portion, since it's local to node */
                    367:                        h = strdup(host);
                    368:                        if (h == NULL)
                    369:                                errx(1, "Can't allocate memory.");
                    370:                        if ((p = strchr(h, '%')) != NULL)
                    371:                                *p = '\0';
1.35      heko      372:                        /*
                    373:                         * Send port number only if it's specified and does not equal
                    374:                         * 80. Some broken HTTP servers get confused if you explicitly
                    375:                         * send them the port number.
                    376:                         */
                    377:                        if (port && strcmp(port, "80") != 0)
1.42      deraadt   378:                                fprintf(fin,
                    379:                                    "GET /%s HTTP/1.0\r\nHost: [%s]:%s\r\n%s\r\n\r\n",
1.36      heko      380:                                    path, h, port, HTTP_USER_AGENT);
1.35      heko      381:                        else
1.42      deraadt   382:                                fprintf(fin,
                    383:                                    "GET /%s HTTP/1.0\r\nHost: [%s]\r\n%s\r\n\r\n",
1.36      heko      384:                                    path, h, HTTP_USER_AGENT);
1.32      itojun    385:                        free(h);
                    386:                } else {
1.35      heko      387:                        if (port && strcmp(port, "80") != 0)
1.42      deraadt   388:                                fprintf(fin,
                    389:                                    "GET /%s HTTP/1.0\r\nHost: %s:%s\r\n%s\r\n\r\n",
1.36      heko      390:                                    path, host, port, HTTP_USER_AGENT);
1.35      heko      391:                        else
1.42      deraadt   392:                                fprintf(fin,
                    393:                                    "GET /%s HTTP/1.0\r\nHost: %s\r\n%s\r\n\r\n",
1.36      heko      394:                                    path, host, HTTP_USER_AGENT);
1.32      itojun    395:                }
1.28      itojun    396:        }
1.40      fgsch     397:        if (fflush(fin) == EOF) {
1.14      millert   398:                warn("Writing HTTP request");
1.6       millert   399:                goto cleanup_url_get;
1.1       millert   400:        }
1.40      fgsch     401:
                    402:        if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
                    403:                warn("Receiving HTTP reply");
                    404:                goto cleanup_url_get;
1.1       millert   405:        }
1.40      fgsch     406:
                    407:        while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
                    408:                buf[--len] = '\0';
                    409:        if (debug)
                    410:                fprintf(ttyout, "received '%s'\n", buf);
                    411:
1.1       millert   412:        cp = strchr(buf, ' ');
                    413:        if (cp == NULL)
                    414:                goto improper;
                    415:        else
                    416:                cp++;
1.40      fgsch     417:        if (strncmp(cp, "301", 3) == 0 || strncmp(cp, "302", 3) == 0) {
                    418:                isredirect++;
                    419:        } else if (strncmp(cp, "200", 3)) {
1.1       millert   420:                warnx("Error retrieving file: %s", cp);
1.6       millert   421:                goto cleanup_url_get;
1.1       millert   422:        }
                    423:
                    424:        /*
                    425:         * Read the rest of the header.
                    426:         */
1.40      fgsch     427:        free(buf);
                    428:        filesize = -1;
                    429:
                    430:        while (1) {
                    431:                if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
                    432:                        warn("Receiving HTTP reply");
                    433:                        goto cleanup_url_get;
                    434:                }
                    435:                while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
                    436:                        buf[--len] = '\0';
                    437:                if (len == 0)
1.1       millert   438:                        break;
1.40      fgsch     439:                if (debug)
                    440:                        fprintf(ttyout, "received '%s'\n", buf);
1.1       millert   441:
1.40      fgsch     442:                /* Look for some headers */
                    443:                cp = buf;
1.1       millert   444: #define CONTENTLEN "Content-Length: "
1.40      fgsch     445:                if (strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0) {
                    446:                        cp += sizeof(CONTENTLEN) - 1;
                    447:                        filesize = strtol(cp, &ep, 10);
                    448:                        if (filesize < 1 || *ep != '\0')
                    449:                                goto improper;
                    450: #define LOCATION "Location: "
                    451:                } else if (isredirect &&
                    452:                    strncasecmp(cp, LOCATION, sizeof(LOCATION) - 1) == 0) {
                    453:                        cp += sizeof(LOCATION) - 1;
                    454:                        if (verbose)
                    455:                                fprintf(ttyout, "Redirected to %s\n", cp);
                    456:                        if (fin != NULL)
                    457:                                fclose(fin);
                    458:                        else if (s != -1)
                    459:                                close(s);
                    460:                        if (proxy)
                    461:                                free(proxy);
                    462:                        free(line);
                    463:                        rval = url_get(cp, proxyenv, outfile);
                    464:                        if (buf)
                    465:                                free(buf);
                    466:                        return (rval);
                    467:                }
1.1       millert   468:        }
                    469:
1.17      millert   470:        /* Open the output file.  */
                    471:        if (strcmp(savefile, "-") != 0) {
1.10      deraadt   472:                out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
                    473:                if (out < 0) {
                    474:                        warn("Can't open %s", savefile);
                    475:                        goto cleanup_url_get;
                    476:                }
                    477:        } else
1.17      millert   478:                out = fileno(stdout);
1.1       millert   479:
                    480:        /* Trap signals */
                    481:        oldintr = NULL;
                    482:        if (setjmp(httpabort)) {
                    483:                if (oldintr)
1.2       millert   484:                        (void)signal(SIGINT, oldintr);
1.6       millert   485:                goto cleanup_url_get;
1.1       millert   486:        }
                    487:        oldintr = signal(SIGINT, aborthttp);
                    488:
                    489:        bytes = 0;
                    490:        hashbytes = mark;
                    491:        progressmeter(-1);
1.43      millert   492:
                    493:        free(buf);
1.1       millert   494:
                    495:        /* Finally, suck down the file. */
1.40      fgsch     496:        if ((buf = malloc(4096)) == NULL)
1.47      deraadt   497:                errx(1, "Can't allocate memory for transfer buffer");
1.1       millert   498:        i = 0;
1.40      fgsch     499:        while ((len = fread(buf, sizeof(char), 4096, fin)) > 0) {
1.1       millert   500:                bytes += len;
                    501:                for (cp = buf; len > 0; len -= i, cp += i) {
                    502:                        if ((i = write(out, cp, len)) == -1) {
                    503:                                warn("Writing %s", savefile);
1.6       millert   504:                                goto cleanup_url_get;
1.1       millert   505:                        }
                    506:                        else if (i == 0)
                    507:                                break;
                    508:                }
                    509:                if (hash && !progress) {
                    510:                        while (bytes >= hashbytes) {
1.10      deraadt   511:                                (void)putc('#', ttyout);
1.1       millert   512:                                hashbytes += mark;
                    513:                        }
1.10      deraadt   514:                        (void)fflush(ttyout);
1.1       millert   515:                }
                    516:        }
                    517:        if (hash && !progress && bytes > 0) {
                    518:                if (bytes < mark)
1.10      deraadt   519:                        (void)putc('#', ttyout);
                    520:                (void)putc('\n', ttyout);
                    521:                (void)fflush(ttyout);
1.1       millert   522:        }
                    523:        if (len != 0) {
                    524:                warn("Reading from socket");
1.6       millert   525:                goto cleanup_url_get;
1.1       millert   526:        }
                    527:        progressmeter(1);
1.24      deraadt   528:        if (filesize != -1 && len == 0 && bytes != filesize) {
                    529:                if (verbose)
                    530:                        fputs("Read short file.\n", ttyout);
                    531:                goto cleanup_url_get;
                    532:        }
                    533:
1.1       millert   534:        if (verbose)
1.10      deraadt   535:                fputs("Successfully retrieved file.\n", ttyout);
1.2       millert   536:        (void)signal(SIGINT, oldintr);
1.1       millert   537:
1.40      fgsch     538:        rval = 0;
                    539:        goto cleanup_url_get;
1.1       millert   540:
1.14      millert   541: noftpautologin:
                    542:        warnx(
                    543:            "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
                    544:        goto cleanup_url_get;
                    545:
1.1       millert   546: improper:
1.8       millert   547:        warnx("Improper response from %s", host);
1.14      millert   548:
1.6       millert   549: cleanup_url_get:
1.40      fgsch     550:        if (fin != NULL)
                    551:                fclose(fin);
                    552:        else if (s != -1)
1.1       millert   553:                close(s);
1.40      fgsch     554:        if (buf)
                    555:                free(buf);
1.1       millert   556:        if (proxy)
                    557:                free(proxy);
1.14      millert   558:        free(line);
1.40      fgsch     559:        return (rval);
1.1       millert   560: }
                    561:
                    562: /*
                    563:  * Abort a http retrieval
                    564:  */
                    565: void
1.50    ! deraadt   566: aborthttp(int notused)
1.1       millert   567: {
                    568:
                    569:        alarmtimer(0);
1.10      deraadt   570:        fputs("\nhttp fetch aborted.\n", ttyout);
                    571:        (void)fflush(ttyout);
1.1       millert   572:        longjmp(httpabort, 1);
                    573: }
                    574:
                    575: /*
1.22      deraadt   576:  * Abort a http retrieval
                    577:  */
                    578: void
1.50    ! deraadt   579: abortfile(int notused)
1.22      deraadt   580: {
                    581:
                    582:        alarmtimer(0);
                    583:        fputs("\nfile fetch aborted.\n", ttyout);
                    584:        (void)fflush(ttyout);
                    585:        longjmp(httpabort, 1);
                    586: }
                    587:
                    588: /*
1.1       millert   589:  * Retrieve multiple files from the command line, transferring
                    590:  * files of the form "host:path", "ftp://host/path" using the
                    591:  * ftp protocol, and files of the form "http://host/path" using
                    592:  * the http protocol.
1.2       millert   593:  * If path has a trailing "/", then return (-1);
1.1       millert   594:  * the path will be cd-ed into and the connection remains open,
                    595:  * and the function will return -1 (to indicate the connection
                    596:  * is alive).
                    597:  * If an error occurs the return value will be the offset+1 in
                    598:  * argv[] of the file that caused a problem (i.e, argv[x]
                    599:  * returns x+1)
                    600:  * Otherwise, 0 is returned if all files retrieved successfully.
                    601:  */
                    602: int
1.50    ! deraadt   603: auto_fetch(int argc, char *argv[], char *outfile)
1.1       millert   604: {
                    605:        char *xargv[5];
                    606:        char *cp, *line, *host, *dir, *file, *portnum;
1.8       millert   607:        char *user, *pass;
1.6       millert   608:        char *ftpproxy, *httpproxy;
1.14      millert   609:        int rval, xargc;
                    610:        volatile int argpos;
1.49      krw       611:        int dirhasglob, filehasglob, oautologin;
1.14      millert   612:        char rempath[MAXPATHLEN];
1.1       millert   613:
                    614:        argpos = 0;
                    615:
                    616:        if (setjmp(toplevel)) {
                    617:                if (connected)
                    618:                        disconnect(0, NULL);
1.2       millert   619:                return (argpos + 1);
1.1       millert   620:        }
1.3       millert   621:        (void)signal(SIGINT, (sig_t)intr);
                    622:        (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1       millert   623:
1.45      millert   624:        if ((ftpproxy = getenv(FTP_PROXY)) != NULL && *ftpproxy == '\0')
                    625:                ftpproxy = NULL;
                    626:        if ((httpproxy = getenv(HTTP_PROXY)) != NULL && *httpproxy == '\0')
                    627:                httpproxy = NULL;
1.6       millert   628:
1.1       millert   629:        /*
                    630:         * Loop through as long as there's files to fetch.
                    631:         */
                    632:        for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
                    633:                if (strchr(argv[argpos], ':') == NULL)
                    634:                        break;
1.8       millert   635:                host = dir = file = portnum = user = pass = NULL;
1.1       millert   636:
                    637:                /*
                    638:                 * We muck with the string, so we make a copy.
                    639:                 */
                    640:                line = strdup(argv[argpos]);
                    641:                if (line == NULL)
                    642:                        errx(1, "Can't allocate memory for auto-fetch.");
                    643:
                    644:                /*
                    645:                 * Try HTTP URL-style arguments first.
                    646:                 */
1.22      deraadt   647:                if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
                    648:                    strncasecmp(line, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
1.17      millert   649:                        if (url_get(line, httpproxy, outfile) == -1)
1.1       millert   650:                                rval = argpos + 1;
                    651:                        continue;
                    652:                }
                    653:
                    654:                /*
1.6       millert   655:                 * Try FTP URL-style arguments next. If ftpproxy is
                    656:                 * set, use url_get() instead of standard ftp.
                    657:                 * Finally, try host:file.
1.1       millert   658:                 */
                    659:                host = line;
1.7       millert   660:                if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.37      heko      661:                        char *passend, *passagain, *userend;
1.31      itojun    662:
1.6       millert   663:                        if (ftpproxy) {
1.17      millert   664:                                if (url_get(line, ftpproxy, outfile) == -1)
1.6       millert   665:                                        rval = argpos + 1;
                    666:                                continue;
                    667:                        }
1.1       millert   668:                        host += sizeof(FTP_URL) - 1;
1.8       millert   669:                        dir = strchr(host, '/');
1.1       millert   670:
1.8       millert   671:                        /* Look for [user:pass@]host[:port] */
1.31      itojun    672:
                    673:                        /* check if we have "user:pass@" */
1.37      heko      674:                        userend = strchr(host, ':');
1.31      itojun    675:                        passend = strchr(host, '@');
                    676:                        if (passend && userend && userend < passend &&
                    677:                            (!dir || passend < dir)) {
                    678:                                user = host;
                    679:                                pass = userend + 1;
                    680:                                host = passend + 1;
                    681:                                *userend = *passend = '\0';
1.37      heko      682:                                passagain = strchr(host, '@');
1.42      deraadt   683:                                if (strchr(pass, '@') != NULL ||
1.37      heko      684:                                    (passagain != NULL && passagain < dir)) {
                    685:                                        warnx(at_encoding_warning);
                    686:                                        goto bad_ftp_url;
1.42      deraadt   687:                                }
1.31      itojun    688:
                    689:                                if (EMPTYSTRING(user) || EMPTYSTRING(pass)) {
1.11      millert   690: bad_ftp_url:
1.31      itojun    691:                                        warnx("Invalid URL: %s", argv[argpos]);
                    692:                                        rval = argpos + 1;
                    693:                                        continue;
                    694:                                }
1.37      heko      695:                                user = urldecode(user);
                    696:                                pass = urldecode(pass);
1.8       millert   697:                        }
1.31      itojun    698:
                    699: #ifdef INET6
                    700:                        /* check [host]:port, or [host] */
                    701:                        if (host[0] == '[') {
                    702:                                cp = strchr(host, ']');
                    703:                                if (cp && (!dir || cp < dir)) {
                    704:                                        if (cp + 1 == dir || cp[1] == ':') {
                    705:                                                host++;
                    706:                                                *cp++ = '\0';
                    707:                                        } else
                    708:                                                cp = NULL;
                    709:                                } else
                    710:                                        cp = host;
1.25      itojun    711:                        } else
                    712:                                cp = host;
1.31      itojun    713: #else
                    714:                        cp = host;
1.25      itojun    715: #endif
1.31      itojun    716:
                    717:                        /* split off host[:port] if there is */
                    718:                        if (cp) {
                    719:                                portnum = strchr(cp, ':');
                    720:                                if (!portnum)
                    721:                                        ;
                    722:                                else {
                    723:                                        if (!dir)
                    724:                                                ;
                    725:                                        else if (portnum + 1 < dir) {
                    726:                                                *portnum++ = '\0';
                    727:                                                /*
                    728:                                                 * XXX should check if portnum
                    729:                                                 * is decimal number
                    730:                                                 */
                    731:                                        } else {
                    732:                                                /* empty portnum */
                    733:                                                goto bad_ftp_url;
                    734:                                        }
                    735:                                }
                    736:                        } else
                    737:                                portnum = NULL;
1.8       millert   738:                } else {                        /* classic style `host:file' */
                    739:                        dir = strchr(host, ':');
                    740:                }
1.1       millert   741:                if (EMPTYSTRING(host)) {
                    742:                        rval = argpos + 1;
                    743:                        continue;
                    744:                }
                    745:
                    746:                /*
1.9       millert   747:                 * If dir is NULL, the file wasn't specified
1.1       millert   748:                 * (URL looked something like ftp://host)
                    749:                 */
1.8       millert   750:                if (dir != NULL)
                    751:                        *dir++ = '\0';
1.1       millert   752:
                    753:                /*
                    754:                 * Extract the file and (if present) directory name.
                    755:                 */
1.42      deraadt   756:                if (!EMPTYSTRING(dir)) {
1.8       millert   757:                        cp = strrchr(dir, '/');
1.1       millert   758:                        if (cp != NULL) {
                    759:                                *cp++ = '\0';
                    760:                                file = cp;
                    761:                        } else {
                    762:                                file = dir;
                    763:                                dir = NULL;
                    764:                        }
                    765:                }
                    766:                if (debug)
1.42      deraadt   767:                        fprintf(ttyout,
                    768:                            "user %s:%s host %s port %s dir %s file %s\n",
1.8       millert   769:                            user, pass, host, portnum, dir, file);
1.1       millert   770:
                    771:                /*
1.49      krw       772:                 * Set up the connection.
1.1       millert   773:                 */
1.49      krw       774:                if (connected)
                    775:                        disconnect(0, NULL);
                    776:                xargv[0] = __progname;
                    777:                xargv[1] = host;
1.8       millert   778:                xargv[2] = NULL;
1.49      krw       779:                xargc = 2;
                    780:                if (!EMPTYSTRING(portnum)) {
                    781:                        xargv[2] = portnum;
                    782:                        xargv[3] = NULL;
                    783:                        xargc = 3;
                    784:                }
                    785:                oautologin = autologin;
                    786:                if (user != NULL)
                    787:                        autologin = 0;
                    788:                setpeer(xargc, xargv);
                    789:                autologin = oautologin;
                    790:                if ((connected == 0) ||
                    791:                    ((connected == 1) && !ftp_login(host, user, pass))) {
                    792:                        warnx("Can't connect or login to host `%s'", host);
1.8       millert   793:                        rval = argpos + 1;
                    794:                        continue;
1.1       millert   795:                }
1.49      krw       796:
                    797:                /* Always use binary transfers. */
                    798:                setbinary(0, NULL);
1.1       millert   799:
1.4       millert   800:                dirhasglob = filehasglob = 0;
                    801:                if (doglob) {
1.42      deraadt   802:                        if (!EMPTYSTRING(dir) &&
1.4       millert   803:                            strpbrk(dir, "*?[]{}") != NULL)
                    804:                                dirhasglob = 1;
1.42      deraadt   805:                        if (!EMPTYSTRING(file) &&
1.4       millert   806:                            strpbrk(file, "*?[]{}") != NULL)
                    807:                                filehasglob = 1;
                    808:                }
                    809:
1.1       millert   810:                /* Change directories, if necessary. */
1.42      deraadt   811:                if (!EMPTYSTRING(dir) && !dirhasglob) {
1.1       millert   812:                        xargv[0] = "cd";
                    813:                        xargv[1] = dir;
                    814:                        xargv[2] = NULL;
                    815:                        cd(2, xargv);
1.42      deraadt   816:                        if (!dirchange) {
1.1       millert   817:                                rval = argpos + 1;
                    818:                                continue;
                    819:                        }
                    820:                }
                    821:
                    822:                if (EMPTYSTRING(file)) {
                    823:                        rval = -1;
                    824:                        continue;
                    825:                }
                    826:
1.21      marc      827:                if (verbose)
1.10      deraadt   828:                        fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);
1.1       millert   829:
1.4       millert   830:                if (dirhasglob) {
                    831:                        snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
                    832:                        file = rempath;
                    833:                }
                    834:
                    835:                /* Fetch the file(s). */
1.10      deraadt   836:                xargc = 2;
1.1       millert   837:                xargv[0] = "get";
                    838:                xargv[1] = file;
                    839:                xargv[2] = NULL;
1.4       millert   840:                if (dirhasglob || filehasglob) {
                    841:                        int ointeractive;
                    842:
                    843:                        ointeractive = interactive;
                    844:                        interactive = 0;
                    845:                        xargv[0] = "mget";
1.10      deraadt   846:                        mget(xargc, xargv);
1.5       millert   847:                        interactive = ointeractive;
1.10      deraadt   848:                } else {
1.17      millert   849:                        if (outfile != NULL) {
                    850:                                xargv[2] = outfile;
                    851:                                xargv[3] = NULL;
1.10      deraadt   852:                                xargc++;
                    853:                        }
                    854:                        get(xargc, xargv);
                    855:                }
1.1       millert   856:
1.4       millert   857:                if ((code / 100) != COMPLETE)
1.1       millert   858:                        rval = argpos + 1;
                    859:        }
                    860:        if (connected && rval != -1)
                    861:                disconnect(0, NULL);
                    862:        return (rval);
1.37      heko      863: }
                    864:
                    865: char *
1.50    ! deraadt   866: urldecode(const char *str)
1.37      heko      867: {
                    868:         char *ret;
                    869:         char c;
                    870:         int i, reallen;
                    871:
1.42      deraadt   872:         if (str == NULL)
1.37      heko      873:                 return NULL;
                    874:         if ((ret = malloc(strlen(str)+1)) == NULL)
                    875:                 err(1, "Can't allocate memory for URL decoding");
                    876:         for (i = 0, reallen = 0; str[i] != '\0'; i++, reallen++, ret++) {
                    877:                 c = str[i];
                    878:                 if (c == '+') {
                    879:                         *ret = ' ';
                    880:                         continue;
                    881:                 }
                    882:                 /* Can't use strtol here because next char after %xx may be
                    883:                  * a digit. */
                    884:                 if (c == '%' && isxdigit(str[i+1]) && isxdigit(str[i+2])) {
                    885:                         *ret = hextochar(&str[i+1]);
                    886:                         i+=2;
                    887:                         continue;
                    888:                 }
                    889:                 *ret = c;
                    890:         }
                    891:         *ret = '\0';
                    892:
                    893:         return ret-reallen;
                    894: }
                    895:
                    896: char
1.50    ! deraadt   897: hextochar(const char *str)
1.37      heko      898: {
                    899:         char c, ret;
                    900:
                    901:         c = str[0];
                    902:         ret = c;
                    903:         if (isalpha(c))
                    904:                 ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
                    905:         else
                    906:                 ret -= '0';
                    907:         ret *= 16;
                    908:
                    909:         c = str[1];
                    910:         ret += c;
                    911:         if (isalpha(c))
                    912:                 ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
                    913:         else
                    914:                 ret -= '0';
                    915:         return ret;
1.25      itojun    916: }
                    917:
                    918: int
1.50    ! deraadt   919: isurl(const char *p)
1.25      itojun    920: {
1.27      millert   921:
1.26      deraadt   922:        if (strncasecmp(p, FTP_URL, sizeof(FTP_URL) - 1) == 0 ||
                    923:            strncasecmp(p, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
1.27      millert   924:            strncasecmp(p, FILE_URL, sizeof(FILE_URL) - 1) == 0 ||
                    925:            strstr(p, ":/"))
                    926:                return (1);
                    927:        return (0);
1.1       millert   928: }