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

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