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

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