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

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