[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.38

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