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

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