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

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