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

1.14    ! millert     1: /*     $OpenBSD: fetch.c,v 1.13 1997/07/24 14:22:21 deraadt Exp $      */
        !             2: /*     $NetBSD: fetch.c,v 1.13 1997/07/20 12:49:26 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.14    ! millert    41: static char rcsid[] = "$OpenBSD: fetch.c,v 1.13 1997/07/24 14:22:21 deraadt Exp $";
1.1       millert    42: #endif /* not lint */
                     43:
                     44: /*
                     45:  * FTP User Program -- Command line file retrieval
                     46:  */
                     47:
                     48: #include <sys/types.h>
                     49: #include <sys/param.h>
                     50: #include <sys/socket.h>
                     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:
1.14    ! millert    69: static int     url_get __P((const char *, const char *, int));
        !            70: void           aborthttp __P((int));
        !            71:
        !            72:
1.1       millert    73: #define        FTP_URL         "ftp://"        /* ftp URL prefix */
                     74: #define        HTTP_URL        "http://"       /* http URL prefix */
1.6       millert    75: #define FTP_PROXY      "ftp_proxy"     /* env var with ftp proxy location */
1.1       millert    76: #define HTTP_PROXY     "http_proxy"    /* env var with http proxy location */
                     77:
                     78:
                     79: #define EMPTYSTRING(x) ((x) == NULL || (*(x) == '\0'))
                     80:
                     81: jmp_buf        httpabort;
                     82:
                     83: /*
1.6       millert    84:  * Retrieve URL, via the proxy in $proxyvar if necessary.
1.1       millert    85:  * Modifies the string argument given.
                     86:  * Returns -1 on failure, 0 on success
                     87:  */
1.14    ! millert    88: static int
        !            89: url_get(origline, proxyenv, fd)
        !            90:        const char *origline;
        !            91:        const char *proxyenv;
1.10      deraadt    92:        int fd;
1.1       millert    93: {
                     94:        struct sockaddr_in sin;
1.14    ! millert    95:        int i, out, isftpurl;
        !            96:        in_port_t port;
        !            97:        volatile int s;
        !            98:        size_t len;
        !            99:        char c, *cp, *ep, *portnum, *path, buf[4096];
        !           100:        const char *savefile;
        !           101:        char *line, *proxy, *host;
        !           102:        volatile sig_t oldintr;
1.1       millert   103:        off_t hashbytes;
                    104:
                    105:        s = -1;
                    106:        proxy = NULL;
1.14    ! millert   107:        isftpurl = 0;
1.1       millert   108:
1.14    ! millert   109: #ifdef __GNUC__                                /* XXX: to shut up gcc warnings */
        !           110:        (void)&out;
        !           111:        (void)&proxy;
        !           112:        (void)&savefile;
        !           113: #endif
        !           114:
        !           115:        line = strdup(origline);
        !           116:        if (line == NULL)
        !           117:                errx(1, "Can't allocate memory to parse URL");
1.7       millert   118:        if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
1.6       millert   119:                host = line + sizeof(HTTP_URL) - 1;
1.14    ! millert   120:        else if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.6       millert   121:                host = line + sizeof(FTP_URL) - 1;
1.14    ! millert   122:                isftpurl = 1;
        !           123:        } else
        !           124:                errx(1, "url_get: Invalid URL '%s'", line);
1.6       millert   125:
1.1       millert   126:        path = strchr(host, '/');               /* find path */
1.14    ! millert   127:        if (EMPTYSTRING(path)) {
        !           128:                if (isftpurl)
        !           129:                        goto noftpautologin;
        !           130:                warnx("Invalid URL (no `/' after host): %s", origline);
1.6       millert   131:                goto cleanup_url_get;
1.14    ! millert   132:        }
1.1       millert   133:        *path++ = '\0';
1.14    ! millert   134:        if (EMPTYSTRING(path)) {
        !           135:                if (isftpurl)
        !           136:                        goto noftpautologin;
        !           137:                warnx("Invalid URL (no file after host): %s", origline);
1.6       millert   138:                goto cleanup_url_get;
1.14    ! millert   139:        }
1.1       millert   140:
                    141:        savefile = strrchr(path, '/');                  /* find savefile */
                    142:        if (savefile != NULL)
                    143:                savefile++;
                    144:        else
                    145:                savefile = path;
1.14    ! millert   146:        if (EMPTYSTRING(savefile)) {
        !           147:                if (isftpurl)
        !           148:                        goto noftpautologin;
        !           149:                warnx("Invalid URL (no file after directory): %s", origline);
1.6       millert   150:                goto cleanup_url_get;
1.14    ! millert   151:        }
1.1       millert   152:
                    153:        if (proxyenv != NULL) {                         /* use proxy */
                    154:                proxy = strdup(proxyenv);
                    155:                if (proxy == NULL)
1.14    ! millert   156:                        errx(1, "Can't allocate memory for proxy URL.");
1.7       millert   157:                if (strncasecmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
1.6       millert   158:                        host = proxy + sizeof(HTTP_URL) - 1;
1.7       millert   159:                else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0)
1.6       millert   160:                        host = proxy + sizeof(FTP_URL) - 1;
                    161:                else {
1.14    ! millert   162:                        warnx("Malformed proxy URL: %s", proxyenv);
1.6       millert   163:                        goto cleanup_url_get;
                    164:                }
1.14    ! millert   165:                if (EMPTYSTRING(host)) {
        !           166:                        warnx("Malformed proxy URL: %s", proxyenv);
1.6       millert   167:                        goto cleanup_url_get;
1.14    ! millert   168:                }
1.1       millert   169:                *--path = '/';                  /* add / back to real path */
                    170:                path = strchr(host, '/');       /* remove trailing / on host */
                    171:                if (! EMPTYSTRING(path))
                    172:                        *path++ = '\0';
                    173:                path = line;
                    174:        }
                    175:
                    176:        portnum = strchr(host, ':');                    /* find portnum */
                    177:        if (portnum != NULL)
                    178:                *portnum++ = '\0';
                    179:
                    180:        if (debug)
1.10      deraadt   181:                fprintf(ttyout, "host %s, port %s, path %s, save as %s.\n",
1.1       millert   182:                    host, portnum, path, savefile);
                    183:
                    184:        memset(&sin, 0, sizeof(sin));
                    185:        sin.sin_family = AF_INET;
                    186:
                    187:        if (isdigit(host[0])) {
                    188:                if (inet_aton(host, &sin.sin_addr) == 0) {
1.8       millert   189:                        warnx("Invalid IP address: %s", host);
1.6       millert   190:                        goto cleanup_url_get;
1.1       millert   191:                }
                    192:        } else {
                    193:                struct hostent *hp;
                    194:
                    195:                hp = gethostbyname(host);
                    196:                if (hp == NULL) {
                    197:                        warnx("%s: %s", host, hstrerror(h_errno));
1.6       millert   198:                        goto cleanup_url_get;
1.1       millert   199:                }
                    200:                if (hp->h_addrtype != AF_INET) {
                    201:                        warnx("%s: not an Internet address?", host);
1.6       millert   202:                        goto cleanup_url_get;
1.1       millert   203:                }
1.14    ! millert   204:                memcpy(&sin.sin_addr, hp->h_addr, (size_t)hp->h_length);
1.1       millert   205:        }
                    206:
                    207:        if (! EMPTYSTRING(portnum)) {
1.14    ! millert   208:                char *ep;
        !           209:                long nport;
        !           210:
        !           211:                nport = strtol(portnum, &ep, 10);
        !           212:                if (nport < 1 || nport > 0xffff || *ep != '\0') {
1.8       millert   213:                        warnx("Invalid port: %s", portnum);
1.6       millert   214:                        goto cleanup_url_get;
1.1       millert   215:                }
1.14    ! millert   216:                port = htons((in_port_t)nport);
1.1       millert   217:        } else
                    218:                port = httpport;
                    219:        sin.sin_port = port;
                    220:
                    221:        s = socket(AF_INET, SOCK_STREAM, 0);
                    222:        if (s == -1) {
1.14    ! millert   223:                warn("Can't create socket");
1.6       millert   224:                goto cleanup_url_get;
1.1       millert   225:        }
                    226:
                    227:        if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
                    228:                warn("Can't connect to %s", host);
1.6       millert   229:                goto cleanup_url_get;
1.1       millert   230:        }
                    231:
                    232:        /*
                    233:         * Construct and send the request.  We're expecting a return
                    234:         * status of "200". Proxy requests don't want leading /.
                    235:         */
                    236:        if (!proxy)
1.14    ! millert   237:                fprintf(ttyout, "Requesting %s\n", origline);
1.1       millert   238:        else
1.14    ! millert   239:                fprintf(ttyout, "Requesting %s (via %s)\n", origline, proxyenv);
1.13      deraadt   240:        snprintf(buf, sizeof(buf), "GET %s%s HTTP/1.0\r\n\r\n",
1.1       millert   241:            proxy ? "" : "/", path);
1.14    ! millert   242:        len = strlen(buf);
        !           243:        if (write(s, buf, len) < len) {
        !           244:                warn("Writing HTTP request");
1.6       millert   245:                goto cleanup_url_get;
1.1       millert   246:        }
                    247:        memset(buf, 0, sizeof(buf));
1.14    ! millert   248:        for (cp = buf; cp < buf + sizeof(buf); ) {
1.1       millert   249:                if (read(s, cp, 1) != 1)
                    250:                        goto improper;
1.4       millert   251:                if (*cp == '\r')
                    252:                        continue;
1.1       millert   253:                if (*cp == '\n')
                    254:                        break;
1.14    ! millert   255:                cp++;
1.1       millert   256:        }
1.14    ! millert   257:        buf[sizeof(buf) - 1] = '\0';            /* sanity */
1.1       millert   258:        cp = strchr(buf, ' ');
                    259:        if (cp == NULL)
                    260:                goto improper;
                    261:        else
                    262:                cp++;
                    263:        if (strncmp(cp, "200", 3)) {
                    264:                warnx("Error retrieving file: %s", cp);
1.6       millert   265:                goto cleanup_url_get;
1.1       millert   266:        }
                    267:
                    268:        /*
                    269:         * Read the rest of the header.
                    270:         */
                    271:        memset(buf, 0, sizeof(buf));
                    272:        c = '\0';
1.14    ! millert   273:        for (cp = buf; cp < buf + sizeof(buf); ) {
1.1       millert   274:                if (read(s, cp, 1) != 1)
                    275:                        goto improper;
1.4       millert   276:                if (*cp == '\r')
                    277:                        continue;
1.1       millert   278:                if (*cp == '\n' && c == '\n')
                    279:                        break;
                    280:                c = *cp;
1.14    ! millert   281:                cp++;
1.1       millert   282:        }
1.14    ! millert   283:        buf[sizeof(buf) - 1] = '\0';            /* sanity */
1.1       millert   284:
1.9       millert   285:        /* Look for the "Content-length: " header.  */
1.1       millert   286: #define CONTENTLEN "Content-Length: "
                    287:        for (cp = buf; *cp != '\0'; cp++) {
                    288:                if (tolower(*cp) == 'c' &&
                    289:                    strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0)
                    290:                        break;
                    291:        }
1.13      deraadt   292:        if (*cp != '\0') {
                    293:                cp += sizeof(CONTENTLEN) - 1;
1.14    ! millert   294:                ep = strchr(cp, '\n');
        !           295:                if (ep == NULL)
1.13      deraadt   296:                        goto improper;
                    297:                else
1.14    ! millert   298:                        *ep = '\0';
        !           299:                filesize = strtol(cp, &ep, 10);
        !           300:                if (filesize < 1 || *ep != '\0')
1.13      deraadt   301:                        goto improper;
                    302:        } else
                    303:                filesize = -1;
1.1       millert   304:
                    305:        /* Open the output file. */
1.10      deraadt   306:        if (fd == -1) {
                    307:                out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
                    308:                if (out < 0) {
                    309:                        warn("Can't open %s", savefile);
                    310:                        goto cleanup_url_get;
                    311:                }
                    312:        } else
                    313:                out = fd;
1.1       millert   314:
                    315:        /* Trap signals */
                    316:        oldintr = NULL;
                    317:        if (setjmp(httpabort)) {
                    318:                if (oldintr)
1.2       millert   319:                        (void)signal(SIGINT, oldintr);
1.6       millert   320:                goto cleanup_url_get;
1.1       millert   321:        }
                    322:        oldintr = signal(SIGINT, aborthttp);
                    323:
                    324:        bytes = 0;
                    325:        hashbytes = mark;
                    326:        progressmeter(-1);
                    327:
                    328:        /* Finally, suck down the file. */
                    329:        i = 0;
                    330:        while ((len = read(s, buf, sizeof(buf))) > 0) {
                    331:                bytes += len;
                    332:                for (cp = buf; len > 0; len -= i, cp += i) {
                    333:                        if ((i = write(out, cp, len)) == -1) {
                    334:                                warn("Writing %s", savefile);
1.6       millert   335:                                goto cleanup_url_get;
1.1       millert   336:                        }
                    337:                        else if (i == 0)
                    338:                                break;
                    339:                }
                    340:                if (hash && !progress) {
                    341:                        while (bytes >= hashbytes) {
1.10      deraadt   342:                                (void)putc('#', ttyout);
1.1       millert   343:                                hashbytes += mark;
                    344:                        }
1.10      deraadt   345:                        (void)fflush(ttyout);
1.1       millert   346:                }
                    347:        }
                    348:        if (hash && !progress && bytes > 0) {
                    349:                if (bytes < mark)
1.10      deraadt   350:                        (void)putc('#', ttyout);
                    351:                (void)putc('\n', ttyout);
                    352:                (void)fflush(ttyout);
1.1       millert   353:        }
                    354:        if (len != 0) {
                    355:                warn("Reading from socket");
1.6       millert   356:                goto cleanup_url_get;
1.1       millert   357:        }
                    358:        progressmeter(1);
                    359:        if (verbose)
1.10      deraadt   360:                fputs("Successfully retrieved file.\n", ttyout);
1.2       millert   361:        (void)signal(SIGINT, oldintr);
1.1       millert   362:
                    363:        close(s);
1.10      deraadt   364:        if (fd != -1)
                    365:                close(out);
1.1       millert   366:        if (proxy)
                    367:                free(proxy);
1.14    ! millert   368:        free(line);
1.2       millert   369:        return (0);
1.1       millert   370:
1.14    ! millert   371: noftpautologin:
        !           372:        warnx(
        !           373:            "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
        !           374:        goto cleanup_url_get;
        !           375:
1.1       millert   376: improper:
1.8       millert   377:        warnx("Improper response from %s", host);
1.14    ! millert   378:
1.6       millert   379: cleanup_url_get:
1.1       millert   380:        if (s != -1)
                    381:                close(s);
                    382:        if (proxy)
                    383:                free(proxy);
1.14    ! millert   384:        free(line);
1.2       millert   385:        return (-1);
1.1       millert   386: }
                    387:
                    388: /*
                    389:  * Abort a http retrieval
                    390:  */
                    391: void
1.3       millert   392: aborthttp(notused)
                    393:        int notused;
1.1       millert   394: {
                    395:
                    396:        alarmtimer(0);
1.10      deraadt   397:        fputs("\nhttp fetch aborted.\n", ttyout);
                    398:        (void)fflush(ttyout);
1.1       millert   399:        longjmp(httpabort, 1);
                    400: }
                    401:
                    402: /*
                    403:  * Retrieve multiple files from the command line, transferring
                    404:  * files of the form "host:path", "ftp://host/path" using the
                    405:  * ftp protocol, and files of the form "http://host/path" using
                    406:  * the http protocol.
1.2       millert   407:  * If path has a trailing "/", then return (-1);
1.1       millert   408:  * the path will be cd-ed into and the connection remains open,
                    409:  * and the function will return -1 (to indicate the connection
                    410:  * is alive).
                    411:  * If an error occurs the return value will be the offset+1 in
                    412:  * argv[] of the file that caused a problem (i.e, argv[x]
                    413:  * returns x+1)
                    414:  * Otherwise, 0 is returned if all files retrieved successfully.
                    415:  */
                    416: int
1.10      deraadt   417: auto_fetch(argc, argv, fd)
1.1       millert   418:        int argc;
                    419:        char *argv[];
1.10      deraadt   420:        int fd;
1.1       millert   421: {
                    422:        static char lasthost[MAXHOSTNAMELEN];
                    423:        char *xargv[5];
                    424:        char *cp, *line, *host, *dir, *file, *portnum;
1.8       millert   425:        char *user, *pass;
1.6       millert   426:        char *ftpproxy, *httpproxy;
1.14    ! millert   427:        int rval, xargc;
        !           428:        volatile int argpos;
1.4       millert   429:        int dirhasglob, filehasglob;
1.14    ! millert   430:        char rempath[MAXPATHLEN];
1.1       millert   431:
                    432:        argpos = 0;
                    433:
                    434:        if (setjmp(toplevel)) {
                    435:                if (connected)
                    436:                        disconnect(0, NULL);
1.2       millert   437:                return (argpos + 1);
1.1       millert   438:        }
1.3       millert   439:        (void)signal(SIGINT, (sig_t)intr);
                    440:        (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1       millert   441:
1.6       millert   442:        ftpproxy = getenv(FTP_PROXY);
                    443:        httpproxy = getenv(HTTP_PROXY);
                    444:
1.1       millert   445:        /*
                    446:         * Loop through as long as there's files to fetch.
                    447:         */
                    448:        for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
                    449:                if (strchr(argv[argpos], ':') == NULL)
                    450:                        break;
1.8       millert   451:                host = dir = file = portnum = user = pass = NULL;
1.1       millert   452:
                    453:                /*
                    454:                 * We muck with the string, so we make a copy.
                    455:                 */
                    456:                line = strdup(argv[argpos]);
                    457:                if (line == NULL)
                    458:                        errx(1, "Can't allocate memory for auto-fetch.");
                    459:
                    460:                /*
                    461:                 * Try HTTP URL-style arguments first.
                    462:                 */
1.7       millert   463:                if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
1.10      deraadt   464:                        if (url_get(line, httpproxy, fd) == -1)
1.1       millert   465:                                rval = argpos + 1;
                    466:                        continue;
                    467:                }
                    468:
                    469:                /*
1.6       millert   470:                 * Try FTP URL-style arguments next. If ftpproxy is
                    471:                 * set, use url_get() instead of standard ftp.
                    472:                 * Finally, try host:file.
1.1       millert   473:                 */
                    474:                host = line;
1.7       millert   475:                if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.6       millert   476:                        if (ftpproxy) {
1.10      deraadt   477:                                if (url_get(line, ftpproxy, fd) == -1)
1.6       millert   478:                                        rval = argpos + 1;
                    479:                                continue;
                    480:                        }
1.1       millert   481:                        host += sizeof(FTP_URL) - 1;
1.8       millert   482:                        dir = strchr(host, '/');
1.1       millert   483:
1.8       millert   484:                        /* Look for [user:pass@]host[:port] */
1.11      millert   485:                        pass = strpbrk(host, ":@/");
1.9       millert   486:                        if (pass == NULL || *pass == '/') {
1.11      millert   487:                                pass = NULL;
1.8       millert   488:                                goto parsed_url;
1.9       millert   489:                        }
1.11      millert   490:                        if (pass == host || *pass == '@') {
                    491: bad_ftp_url:
1.14    ! millert   492:                                warnx("Invalid URL: %s", argv[argpos]);
1.8       millert   493:                                rval = argpos + 1;
                    494:                                continue;
                    495:                        }
                    496:                        *pass++ = '\0';
1.12      millert   497:                        /* XXX - assumes no '@' in pathname */
                    498:                        if ((cp = strrchr(pass, '@')) == NULL)
                    499:                                cp = strpbrk(pass, ":@/");
1.8       millert   500:                        if (cp == NULL || *cp == '/') {
                    501:                                portnum = pass;
1.11      millert   502:                                pass = NULL;
1.8       millert   503:                                goto parsed_url;
                    504:                        }
1.11      millert   505:                        if (EMPTYSTRING(cp) || *cp == ':')
                    506:                                goto bad_ftp_url;
1.8       millert   507:                        *cp++ = '\0';
1.11      millert   508:                        user = host;
                    509:                        if (EMPTYSTRING(user))
                    510:                                goto bad_ftp_url;
1.8       millert   511:                        host = cp;
1.1       millert   512:                        portnum = strchr(host, ':');
                    513:                        if (portnum != NULL)
                    514:                                *portnum++ = '\0';
1.8       millert   515: parsed_url:
                    516:                } else {                        /* classic style `host:file' */
                    517:                        dir = strchr(host, ':');
                    518:                }
1.1       millert   519:                if (EMPTYSTRING(host)) {
                    520:                        rval = argpos + 1;
                    521:                        continue;
                    522:                }
                    523:
                    524:                /*
1.9       millert   525:                 * If dir is NULL, the file wasn't specified
1.1       millert   526:                 * (URL looked something like ftp://host)
                    527:                 */
1.8       millert   528:                if (dir != NULL)
                    529:                        *dir++ = '\0';
1.1       millert   530:
                    531:                /*
                    532:                 * Extract the file and (if present) directory name.
                    533:                 */
                    534:                if (! EMPTYSTRING(dir)) {
1.8       millert   535:                        cp = strrchr(dir, '/');
1.1       millert   536:                        if (cp != NULL) {
                    537:                                *cp++ = '\0';
                    538:                                file = cp;
                    539:                        } else {
                    540:                                file = dir;
                    541:                                dir = NULL;
                    542:                        }
                    543:                }
                    544:                if (debug)
1.10      deraadt   545:                        fprintf(ttyout, "user %s:%s host %s port %s dir %s file %s\n",
1.8       millert   546:                            user, pass, host, portnum, dir, file);
1.1       millert   547:
                    548:                /*
                    549:                 * Set up the connection if we don't have one.
                    550:                 */
                    551:                if (strcmp(host, lasthost) != 0) {
1.8       millert   552:                        int oautologin;
                    553:
1.5       millert   554:                        (void)strcpy(lasthost, host);
1.1       millert   555:                        if (connected)
                    556:                                disconnect(0, NULL);
                    557:                        xargv[0] = __progname;
                    558:                        xargv[1] = host;
                    559:                        xargv[2] = NULL;
                    560:                        xargc = 2;
1.8       millert   561:                        if (! EMPTYSTRING(portnum)) {
1.1       millert   562:                                xargv[2] = portnum;
                    563:                                xargv[3] = NULL;
                    564:                                xargc = 3;
                    565:                        }
1.8       millert   566:                        oautologin = autologin;
                    567:                        if (user != NULL)
                    568:                                autologin = 0;
1.1       millert   569:                        setpeer(xargc, xargv);
1.8       millert   570:                        autologin = oautologin;
                    571:                        if ((connected == 0) ||
1.14    ! millert   572:                            ((connected == 1) && !login(host, user, pass))) {
1.8       millert   573:                                warnx("Can't connect or login to host `%s'",
                    574:                                    host);
1.1       millert   575:                                rval = argpos + 1;
                    576:                                continue;
                    577:                        }
                    578:
                    579:                        /* Always use binary transfers. */
                    580:                        setbinary(0, NULL);
                    581:                }
1.8       millert   582:                /* cd back to '/' */
                    583:                xargv[0] = "cd";
                    584:                xargv[1] = "/";
                    585:                xargv[2] = NULL;
                    586:                cd(2, xargv);
                    587:                if (! dirchange) {
                    588:                        rval = argpos + 1;
                    589:                        continue;
1.1       millert   590:                }
                    591:
1.4       millert   592:                dirhasglob = filehasglob = 0;
                    593:                if (doglob) {
                    594:                        if (! EMPTYSTRING(dir) &&
                    595:                            strpbrk(dir, "*?[]{}") != NULL)
                    596:                                dirhasglob = 1;
                    597:                        if (! EMPTYSTRING(file) &&
                    598:                            strpbrk(file, "*?[]{}") != NULL)
                    599:                                filehasglob = 1;
                    600:                }
                    601:
1.1       millert   602:                /* Change directories, if necessary. */
1.4       millert   603:                if (! EMPTYSTRING(dir) && !dirhasglob) {
1.1       millert   604:                        xargv[0] = "cd";
                    605:                        xargv[1] = dir;
                    606:                        xargv[2] = NULL;
                    607:                        cd(2, xargv);
                    608:                        if (! dirchange) {
                    609:                                rval = argpos + 1;
                    610:                                continue;
                    611:                        }
                    612:                }
                    613:
                    614:                if (EMPTYSTRING(file)) {
                    615:                        rval = -1;
                    616:                        continue;
                    617:                }
                    618:
                    619:                if (!verbose)
1.10      deraadt   620:                        fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);
1.1       millert   621:
1.4       millert   622:                if (dirhasglob) {
                    623:                        snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
                    624:                        file = rempath;
                    625:                }
                    626:
                    627:                /* Fetch the file(s). */
1.10      deraadt   628:                xargc = 2;
1.1       millert   629:                xargv[0] = "get";
                    630:                xargv[1] = file;
                    631:                xargv[2] = NULL;
1.4       millert   632:                if (dirhasglob || filehasglob) {
                    633:                        int ointeractive;
                    634:
                    635:                        ointeractive = interactive;
                    636:                        interactive = 0;
                    637:                        xargv[0] = "mget";
1.10      deraadt   638:                        mget(xargc, xargv);
1.5       millert   639:                        interactive = ointeractive;
1.10      deraadt   640:                } else {
                    641:                        if (fd != -1) {
                    642:                                xargv[2] = "-";
                    643:                                xargc++;
                    644:                        }
                    645:                        xargv[3] = NULL;
                    646:                        get(xargc, xargv);
                    647:                }
1.1       millert   648:
1.4       millert   649:                if ((code / 100) != COMPLETE)
1.1       millert   650:                        rval = argpos + 1;
                    651:        }
                    652:        if (connected && rval != -1)
                    653:                disconnect(0, NULL);
                    654:        return (rval);
                    655: }