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

1.7     ! millert     1: /*     $OpenBSD: fetch.c,v 1.6 1997/04/10 00:17:08 millert Exp $       */
1.6       millert     2: /*     $NetBSD: fetch.c,v 1.5 1997/04/05 03:27:36 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
                     30:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
                     31:  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     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.7     ! millert    41: static char rcsid[] = "$OpenBSD: fetch.c,v 1.6 1997/04/10 00:17:08 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>
                     51:
                     52: #include <netinet/in.h>
                     53:
                     54: #include <arpa/ftp.h>
                     55: #include <arpa/inet.h>
                     56:
                     57: #include <ctype.h>
                     58: #include <err.h>
                     59: #include <netdb.h>
                     60: #include <fcntl.h>
1.3       millert    61: #include <signal.h>
1.1       millert    62: #include <stdio.h>
                     63: #include <stdlib.h>
                     64: #include <string.h>
                     65: #include <unistd.h>
                     66:
                     67: #include "ftp_var.h"
                     68:
                     69: #define        FTP_URL         "ftp://"        /* ftp URL prefix */
                     70: #define        HTTP_URL        "http://"       /* http URL prefix */
1.6       millert    71: #define FTP_PROXY      "ftp_proxy"     /* env var with ftp proxy location */
1.1       millert    72: #define HTTP_PROXY     "http_proxy"    /* env var with http proxy location */
                     73:
                     74:
                     75: #define EMPTYSTRING(x) ((x) == NULL || (*(x) == '\0'))
                     76:
                     77: jmp_buf        httpabort;
                     78:
                     79: /*
1.6       millert    80:  * Retrieve URL, via the proxy in $proxyvar if necessary.
1.1       millert    81:  * Modifies the string argument given.
                     82:  * Returns -1 on failure, 0 on success
                     83:  */
                     84: int
1.6       millert    85: url_get(line, proxyenv)
1.1       millert    86:        char *line;
1.6       millert    87:        char *proxyenv;
1.1       millert    88: {
                     89:        struct sockaddr_in sin;
                     90:        int i, out, port, s;
                     91:        size_t buflen, len;
                     92:        char c, *cp, *cp2, *savefile, *portnum, *path, buf[4096];
1.6       millert    93:        char *proxy, *host;
1.1       millert    94:        sig_t oldintr;
                     95:        off_t hashbytes;
                     96:
                     97:        s = -1;
                     98:        proxy = NULL;
                     99:
1.7     ! millert   100:        if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
1.6       millert   101:                host = line + sizeof(HTTP_URL) - 1;
1.7     ! millert   102:        else if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0)
1.6       millert   103:                host = line + sizeof(FTP_URL) - 1;
                    104:        else
                    105:                errx(1, "url_get: invalid url '%s'", line);
                    106:
1.1       millert   107:        path = strchr(host, '/');               /* find path */
                    108:        if (EMPTYSTRING(path))
1.6       millert   109:                goto cleanup_url_get;
1.1       millert   110:        *path++ = '\0';
                    111:        if (EMPTYSTRING(path))
1.6       millert   112:                goto cleanup_url_get;
1.1       millert   113:
                    114:        savefile = strrchr(path, '/');                  /* find savefile */
                    115:        if (savefile != NULL)
                    116:                savefile++;
                    117:        else
                    118:                savefile = path;
                    119:        if (EMPTYSTRING(savefile))
1.6       millert   120:                goto cleanup_url_get;
1.1       millert   121:
                    122:        if (proxyenv != NULL) {                         /* use proxy */
                    123:                proxy = strdup(proxyenv);
                    124:                if (proxy == NULL)
                    125:                        errx(1, "Can't allocate memory for proxy url.");
1.7     ! millert   126:                if (strncasecmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
1.6       millert   127:                        host = proxy + sizeof(HTTP_URL) - 1;
1.7     ! millert   128:                else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0)
1.6       millert   129:                        host = proxy + sizeof(FTP_URL) - 1;
                    130:                else {
                    131:                        warnx("Malformed proxy url: %s", proxy);
                    132:                        goto cleanup_url_get;
                    133:                }
1.1       millert   134:                if (EMPTYSTRING(host))
1.6       millert   135:                        goto cleanup_url_get;
1.1       millert   136:                *--path = '/';                  /* add / back to real path */
                    137:                path = strchr(host, '/');       /* remove trailing / on host */
                    138:                if (! EMPTYSTRING(path))
                    139:                        *path++ = '\0';
                    140:                path = line;
                    141:        }
                    142:
                    143:        portnum = strchr(host, ':');                    /* find portnum */
                    144:        if (portnum != NULL)
                    145:                *portnum++ = '\0';
                    146:
                    147:        if (debug)
                    148:                printf("host %s, port %s, path %s, save as %s.\n",
                    149:                    host, portnum, path, savefile);
                    150:
                    151:        memset(&sin, 0, sizeof(sin));
                    152:        sin.sin_family = AF_INET;
                    153:
                    154:        if (isdigit(host[0])) {
                    155:                if (inet_aton(host, &sin.sin_addr) == 0) {
                    156:                        warnx("invalid IP address: %s", host);
1.6       millert   157:                        goto cleanup_url_get;
1.1       millert   158:                }
                    159:        } else {
                    160:                struct hostent *hp;
                    161:
                    162:                hp = gethostbyname(host);
                    163:                if (hp == NULL) {
                    164:                        warnx("%s: %s", host, hstrerror(h_errno));
1.6       millert   165:                        goto cleanup_url_get;
1.1       millert   166:                }
                    167:                if (hp->h_addrtype != AF_INET) {
                    168:                        warnx("%s: not an Internet address?", host);
1.6       millert   169:                        goto cleanup_url_get;
1.1       millert   170:                }
                    171:                memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
                    172:        }
                    173:
                    174:        if (! EMPTYSTRING(portnum)) {
                    175:                port = atoi(portnum);
                    176:                if (port < 1 || (port & 0xffff) != port) {
                    177:                        warnx("invalid port: %s", portnum);
1.6       millert   178:                        goto cleanup_url_get;
1.1       millert   179:                }
                    180:                port = htons(port);
                    181:        } else
                    182:                port = httpport;
                    183:        sin.sin_port = port;
                    184:
                    185:        s = socket(AF_INET, SOCK_STREAM, 0);
                    186:        if (s == -1) {
                    187:                warnx("Can't create socket");
1.6       millert   188:                goto cleanup_url_get;
1.1       millert   189:        }
                    190:
                    191:        if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
                    192:                warn("Can't connect to %s", host);
1.6       millert   193:                goto cleanup_url_get;
1.1       millert   194:        }
                    195:
                    196:        /*
                    197:         * Construct and send the request.  We're expecting a return
                    198:         * status of "200". Proxy requests don't want leading /.
                    199:         */
                    200:        if (!proxy)
                    201:                printf("Requesting %s:%d/%s\n", line, ntohs(port), path);
                    202:        else
                    203:                printf("Requesting %s (via %s)\n", line, proxyenv);
                    204:        snprintf(buf, sizeof(buf), "GET %s%s HTTP/1.0\n\n",
                    205:            proxy ? "" : "/", path);
                    206:        buflen = strlen(buf);
                    207:        if (write(s, buf, buflen) < buflen) {
                    208:                warn("write");
1.6       millert   209:                goto cleanup_url_get;
1.1       millert   210:        }
                    211:        memset(buf, 0, sizeof(buf));
                    212:        for (i = 0, buflen = sizeof(buf), cp = buf; i < buflen; cp++, i++) {
                    213:                if (read(s, cp, 1) != 1)
                    214:                        goto improper;
1.4       millert   215:                if (*cp == '\r')
                    216:                        continue;
1.1       millert   217:                if (*cp == '\n')
                    218:                        break;
                    219:        }
                    220:        buf[buflen - 1] = '\0';         /* sanity */
                    221:        cp = strchr(buf, ' ');
                    222:        if (cp == NULL)
                    223:                goto improper;
                    224:        else
                    225:                cp++;
                    226:        if (strncmp(cp, "200", 3)) {
                    227:                warnx("Error retrieving file: %s", cp);
1.6       millert   228:                goto cleanup_url_get;
1.1       millert   229:        }
                    230:
                    231:        /*
                    232:         * Read the rest of the header.
                    233:         */
                    234:        memset(buf, 0, sizeof(buf));
                    235:        c = '\0';
                    236:        for (i = 0, buflen = sizeof(buf), cp = buf; i < buflen; cp++, i++) {
                    237:                if (read(s, cp, 1) != 1)
                    238:                        goto improper;
1.4       millert   239:                if (*cp == '\r')
                    240:                        continue;
1.1       millert   241:                if (*cp == '\n' && c == '\n')
                    242:                        break;
                    243:                c = *cp;
                    244:        }
                    245:        buf[buflen - 1] = '\0';         /* sanity */
                    246:
                    247:        /*
                    248:         * Look for the "Content-length: " header.
                    249:         */
                    250: #define CONTENTLEN "Content-Length: "
                    251:        for (cp = buf; *cp != '\0'; cp++) {
                    252:                if (tolower(*cp) == 'c' &&
                    253:                    strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0)
                    254:                        break;
                    255:        }
                    256:        if (*cp == '\0')
                    257:                goto improper;
                    258:        cp += sizeof(CONTENTLEN) - 1;
                    259:        cp2 = strchr(cp, '\n');
                    260:        if (cp2 == NULL)
                    261:                goto improper;
                    262:        else
                    263:                *cp2 = '\0';
                    264:        filesize = atoi(cp);
                    265:        if (filesize < 1)
                    266:                goto improper;
                    267:
                    268:        /* Open the output file. */
                    269:        out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
                    270:        if (out < 0) {
                    271:                warn("Can't open %s", savefile);
1.6       millert   272:                goto cleanup_url_get;
1.1       millert   273:        }
                    274:
                    275:        /* Trap signals */
                    276:        oldintr = NULL;
                    277:        if (setjmp(httpabort)) {
                    278:                if (oldintr)
1.2       millert   279:                        (void)signal(SIGINT, oldintr);
1.6       millert   280:                goto cleanup_url_get;
1.1       millert   281:        }
                    282:        oldintr = signal(SIGINT, aborthttp);
                    283:
                    284:        bytes = 0;
                    285:        hashbytes = mark;
                    286:        progressmeter(-1);
                    287:
                    288:        /* Finally, suck down the file. */
                    289:        i = 0;
                    290:        while ((len = read(s, buf, sizeof(buf))) > 0) {
                    291:                bytes += len;
                    292:                for (cp = buf; len > 0; len -= i, cp += i) {
                    293:                        if ((i = write(out, cp, len)) == -1) {
                    294:                                warn("Writing %s", savefile);
1.6       millert   295:                                goto cleanup_url_get;
1.1       millert   296:                        }
                    297:                        else if (i == 0)
                    298:                                break;
                    299:                }
                    300:                if (hash && !progress) {
                    301:                        while (bytes >= hashbytes) {
1.2       millert   302:                                (void)putchar('#');
1.1       millert   303:                                hashbytes += mark;
                    304:                        }
1.2       millert   305:                        (void)fflush(stdout);
1.1       millert   306:                }
                    307:        }
                    308:        if (hash && !progress && bytes > 0) {
                    309:                if (bytes < mark)
1.2       millert   310:                        (void)putchar('#');
                    311:                (void)putchar('\n');
                    312:                (void)fflush(stdout);
1.1       millert   313:        }
                    314:        if (len != 0) {
                    315:                warn("Reading from socket");
1.6       millert   316:                goto cleanup_url_get;
1.1       millert   317:        }
                    318:        progressmeter(1);
                    319:        if (verbose)
1.2       millert   320:                puts("Successfully retrieved file.");
                    321:        (void)signal(SIGINT, oldintr);
1.1       millert   322:
                    323:        close(s);
                    324:        close(out);
                    325:        if (proxy)
                    326:                free(proxy);
1.2       millert   327:        return (0);
1.1       millert   328:
                    329: improper:
                    330:        warnx("improper response from %s", host);
1.6       millert   331: cleanup_url_get:
1.1       millert   332:        if (s != -1)
                    333:                close(s);
                    334:        if (proxy)
                    335:                free(proxy);
1.2       millert   336:        return (-1);
1.1       millert   337: }
                    338:
                    339: /*
                    340:  * Abort a http retrieval
                    341:  */
                    342: void
1.3       millert   343: aborthttp(notused)
                    344:        int notused;
1.1       millert   345: {
                    346:
                    347:        alarmtimer(0);
1.4       millert   348:        puts("\nhttp fetch aborted.");
1.2       millert   349:        (void)fflush(stdout);
1.1       millert   350:        longjmp(httpabort, 1);
                    351: }
                    352:
                    353: /*
                    354:  * Retrieve multiple files from the command line, transferring
                    355:  * files of the form "host:path", "ftp://host/path" using the
                    356:  * ftp protocol, and files of the form "http://host/path" using
                    357:  * the http protocol.
1.2       millert   358:  * If path has a trailing "/", then return (-1);
1.1       millert   359:  * the path will be cd-ed into and the connection remains open,
                    360:  * and the function will return -1 (to indicate the connection
                    361:  * is alive).
                    362:  * If an error occurs the return value will be the offset+1 in
                    363:  * argv[] of the file that caused a problem (i.e, argv[x]
                    364:  * returns x+1)
                    365:  * Otherwise, 0 is returned if all files retrieved successfully.
                    366:  */
                    367: int
                    368: auto_fetch(argc, argv)
                    369:        int argc;
                    370:        char *argv[];
                    371: {
                    372:        static char lasthost[MAXHOSTNAMELEN];
                    373:        char *xargv[5];
                    374:        char *cp, *line, *host, *dir, *file, *portnum;
1.6       millert   375:        char *ftpproxy, *httpproxy;
1.1       millert   376:        int rval, xargc, argpos;
1.4       millert   377:        int dirhasglob, filehasglob;
                    378:        char rempath[MAXPATHLEN];
1.1       millert   379:
                    380:        argpos = 0;
                    381:
                    382:        if (setjmp(toplevel)) {
                    383:                if (connected)
                    384:                        disconnect(0, NULL);
1.2       millert   385:                return (argpos + 1);
1.1       millert   386:        }
1.3       millert   387:        (void)signal(SIGINT, (sig_t)intr);
                    388:        (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1       millert   389:
1.6       millert   390:        ftpproxy = getenv(FTP_PROXY);
                    391:        httpproxy = getenv(HTTP_PROXY);
                    392:
1.1       millert   393:        /*
                    394:         * Loop through as long as there's files to fetch.
                    395:         */
                    396:        for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
                    397:                if (strchr(argv[argpos], ':') == NULL)
                    398:                        break;
                    399:                host = dir = file = portnum = NULL;
                    400:
                    401:                /*
                    402:                 * We muck with the string, so we make a copy.
                    403:                 */
                    404:                line = strdup(argv[argpos]);
                    405:                if (line == NULL)
                    406:                        errx(1, "Can't allocate memory for auto-fetch.");
                    407:
                    408:                /*
                    409:                 * Try HTTP URL-style arguments first.
                    410:                 */
1.7     ! millert   411:                if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
1.6       millert   412:                        if (url_get(line, httpproxy) == -1)
1.1       millert   413:                                rval = argpos + 1;
                    414:                        continue;
                    415:                }
                    416:
                    417:                /*
1.6       millert   418:                 * Try FTP URL-style arguments next. If ftpproxy is
                    419:                 * set, use url_get() instead of standard ftp.
                    420:                 * Finally, try host:file.
1.1       millert   421:                 */
                    422:                host = line;
1.7     ! millert   423:                if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.6       millert   424:                        if (ftpproxy) {
                    425:                                if (url_get(line, ftpproxy) == -1)
                    426:                                        rval = argpos + 1;
                    427:                                continue;
                    428:                        }
1.1       millert   429:                        host += sizeof(FTP_URL) - 1;
                    430:                        cp = strchr(host, '/');
                    431:
                    432:                        /* Look for a port number after the host name. */
                    433:                        portnum = strchr(host, ':');
                    434:                        if (portnum != NULL)
                    435:                                *portnum++ = '\0';
                    436:                } else                          /* classic style `host:file' */
                    437:                        cp = strchr(host, ':');
                    438:                if (EMPTYSTRING(host)) {
                    439:                        rval = argpos + 1;
                    440:                        continue;
                    441:                }
                    442:
                    443:                /*
                    444:                 * If cp is NULL, the file wasn't specified
                    445:                 * (URL looked something like ftp://host)
                    446:                 */
                    447:                if (cp != NULL)
                    448:                        *cp++ = '\0';
                    449:
                    450:                /*
                    451:                 * Extract the file and (if present) directory name.
                    452:                 */
                    453:                dir = cp;
                    454:                if (! EMPTYSTRING(dir)) {
                    455:                        cp = strrchr(cp, '/');
                    456:                        if (cp != NULL) {
                    457:                                *cp++ = '\0';
                    458:                                file = cp;
                    459:                        } else {
                    460:                                file = dir;
                    461:                                dir = NULL;
                    462:                        }
                    463:                }
                    464:                if (debug)
                    465:                        printf("host '%s', dir '%s', file '%s'\n",
                    466:                            host, dir, file);
                    467:
                    468:                /*
                    469:                 * Set up the connection if we don't have one.
                    470:                 */
                    471:                if (strcmp(host, lasthost) != 0) {
1.5       millert   472:                        (void)strcpy(lasthost, host);
1.1       millert   473:                        if (connected)
                    474:                                disconnect(0, NULL);
                    475:                        xargv[0] = __progname;
                    476:                        xargv[1] = host;
                    477:                        xargv[2] = NULL;
                    478:                        xargc = 2;
                    479:                        if (portnum != NULL) {
                    480:                                xargv[2] = portnum;
                    481:                                xargv[3] = NULL;
                    482:                                xargc = 3;
                    483:                        }
                    484:                        setpeer(xargc, xargv);
                    485:                        if (connected == 0) {
                    486:                                warnx("Can't connect to host `%s'", host);
                    487:                                rval = argpos + 1;
                    488:                                continue;
                    489:                        }
                    490:
                    491:                        /* Always use binary transfers. */
                    492:                        setbinary(0, NULL);
                    493:                }
                    494:                else    /* already have connection, cd back to '/' */
                    495:                {
                    496:                        xargv[0] = "cd";
                    497:                        xargv[1] = "/";
                    498:                        xargv[2] = NULL;
                    499:                        cd(2, xargv);
                    500:                        if (! dirchange) {
                    501:                                rval = argpos + 1;
                    502:                                continue;
                    503:                        }
                    504:                }
                    505:
1.4       millert   506:                dirhasglob = filehasglob = 0;
                    507:                if (doglob) {
                    508:                        if (! EMPTYSTRING(dir) &&
                    509:                            strpbrk(dir, "*?[]{}") != NULL)
                    510:                                dirhasglob = 1;
                    511:                        if (! EMPTYSTRING(file) &&
                    512:                            strpbrk(file, "*?[]{}") != NULL)
                    513:                                filehasglob = 1;
                    514:                }
                    515:
1.1       millert   516:                /* Change directories, if necessary. */
1.4       millert   517:                if (! EMPTYSTRING(dir) && !dirhasglob) {
1.1       millert   518:                        xargv[0] = "cd";
                    519:                        xargv[1] = dir;
                    520:                        xargv[2] = NULL;
                    521:                        cd(2, xargv);
                    522:                        if (! dirchange) {
                    523:                                rval = argpos + 1;
                    524:                                continue;
                    525:                        }
                    526:                }
                    527:
                    528:                if (EMPTYSTRING(file)) {
                    529:                        rval = -1;
                    530:                        continue;
                    531:                }
                    532:
                    533:                if (!verbose)
                    534:                        printf("Retrieving %s/%s\n", dir ? dir : "", file);
                    535:
1.4       millert   536:                if (dirhasglob) {
                    537:                        snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
                    538:                        file = rempath;
                    539:                }
                    540:
                    541:                /* Fetch the file(s). */
1.1       millert   542:                xargv[0] = "get";
                    543:                xargv[1] = file;
                    544:                xargv[2] = NULL;
1.4       millert   545:                if (dirhasglob || filehasglob) {
                    546:                        int ointeractive;
                    547:
                    548:                        ointeractive = interactive;
                    549:                        interactive = 0;
                    550:                        xargv[0] = "mget";
                    551:                        mget(2, xargv);
1.5       millert   552:                        interactive = ointeractive;
1.4       millert   553:                } else
                    554:                        get(2, xargv);
1.1       millert   555:
1.4       millert   556:                if ((code / 100) != COMPLETE)
1.1       millert   557:                        rval = argpos + 1;
                    558:        }
                    559:        if (connected && rval != -1)
                    560:                disconnect(0, NULL);
                    561:        return (rval);
                    562: }