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

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