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

1.72    ! ray         1: /*     $OpenBSD: fetch.c,v 1.71 2007/02/03 05:18:40 ray 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:
1.48      deraadt    40: #if !defined(lint) && !defined(SMALL)
1.72    ! ray        41: static const char rcsid[] = "$OpenBSD: fetch.c,v 1.71 2007/02/03 05:18:40 ray Exp $";
1.48      deraadt    42: #endif /* not lint and not SMALL */
1.1       millert    43:
                     44: /*
                     45:  * FTP User Program -- Command line file retrieval
                     46:  */
                     47:
                     48: #include <sys/types.h>
                     49: #include <sys/param.h>
                     50: #include <sys/socket.h>
1.22      deraadt    51: #include <sys/stat.h>
1.1       millert    52:
                     53: #include <netinet/in.h>
                     54:
                     55: #include <arpa/ftp.h>
                     56: #include <arpa/inet.h>
                     57:
                     58: #include <ctype.h>
                     59: #include <err.h>
1.17      millert    60: #include <libgen.h>
1.58      grunk      61: #include <limits.h>
1.1       millert    62: #include <netdb.h>
                     63: #include <fcntl.h>
1.3       millert    64: #include <signal.h>
1.1       millert    65: #include <stdio.h>
1.61      deraadt    66: #include <stdarg.h>
1.19      deraadt    67: #include <errno.h>
1.1       millert    68: #include <stdlib.h>
                     69: #include <string.h>
                     70: #include <unistd.h>
1.40      fgsch      71: #include <util.h>
1.1       millert    72:
1.61      deraadt    73: #ifndef SMALL
                     74: #include <openssl/ssl.h>
                     75: #include <openssl/err.h>
                     76: #else
                     77: #define SSL void
                     78: #endif
                     79:
1.1       millert    80: #include "ftp_var.h"
                     81:
1.38      millert    82: static int     url_get(const char *, const char *, const char *);
                     83: void           aborthttp(int);
                     84: void           abortfile(int);
1.42      deraadt    85: char           hextochar(const char *);
                     86: char           *urldecode(const char *);
1.61      deraadt    87: int            ftp_printf(FILE *, SSL *, const char *, ...) __attribute__((format(printf, 3, 4)));
                     88: char           *ftp_readline(FILE *, SSL *, size_t *);
1.65      ray        89: size_t         ftp_read(FILE *, SSL *, char *, size_t);
1.61      deraadt    90: #ifndef SMALL
                     91: int            proxy_connect(int, char *);
                     92: int            SSL_vprintf(SSL *, const char *, va_list);
                     93: char           *SSL_readline(SSL *, size_t *);
                     94: #endif
1.14      millert    95:
1.1       millert    96: #define        FTP_URL         "ftp://"        /* ftp URL prefix */
                     97: #define        HTTP_URL        "http://"       /* http URL prefix */
1.61      deraadt    98: #define        HTTPS_URL       "https://"      /* https URL prefix */
1.22      deraadt    99: #define        FILE_URL        "file:"         /* file URL prefix */
1.6       millert   100: #define FTP_PROXY      "ftp_proxy"     /* env var with ftp proxy location */
1.1       millert   101: #define HTTP_PROXY     "http_proxy"    /* env var with http proxy location */
                    102:
                    103:
                    104: #define EMPTYSTRING(x) ((x) == NULL || (*(x) == '\0'))
                    105:
1.37      heko      106: static const char *at_encoding_warning =
1.53      deraadt   107:     "Extra `@' characters in usernames and passwords should be encoded as %%40";
1.37      heko      108:
1.1       millert   109: jmp_buf        httpabort;
                    110:
1.54      fgsch     111: static int     redirect_loop;
                    112:
1.1       millert   113: /*
1.6       millert   114:  * Retrieve URL, via the proxy in $proxyvar if necessary.
1.1       millert   115:  * Modifies the string argument given.
                    116:  * Returns -1 on failure, 0 on success
                    117:  */
1.14      millert   118: static int
1.50      deraadt   119: url_get(const char *origline, const char *proxyenv, const char *outfile)
1.1       millert   120: {
1.69      jsg       121:        char pbuf[NI_MAXSERV], hbuf[NI_MAXHOST], *cp, *portnum, *path, ststr[4];
1.62      ray       122:        char *hosttail, *cause = "unknown", *newline, *host, *port, *buf = NULL;
1.53      deraadt   123:        int error, i, isftpurl = 0, isfileurl = 0, isredirect = 0, rval = -1;
1.25      itojun    124:        struct addrinfo hints, *res0, *res;
1.34      millert   125:        const char * volatile savefile;
1.62      ray       126:        char * volatile proxyurl = NULL;
1.53      deraadt   127:        volatile int s = -1, out;
1.14      millert   128:        volatile sig_t oldintr;
1.53      deraadt   129:        FILE *fin = NULL;
1.1       millert   130:        off_t hashbytes;
1.58      grunk     131:        const char *errstr;
1.61      deraadt   132:        size_t len, wlen;
                    133: #ifndef SMALL
                    134:        char *sslpath = NULL, *sslhost = NULL;
1.70      deraadt   135:        int ishttpsurl = 0;
1.61      deraadt   136:        SSL_CTX *ssl_ctx = NULL;
                    137: #endif
                    138:        SSL *ssl = NULL;
1.70      deraadt   139:        int status;
1.14      millert   140:
1.62      ray       141:        newline = strdup(origline);
                    142:        if (newline == NULL)
1.14      millert   143:                errx(1, "Can't allocate memory to parse URL");
1.62      ray       144:        if (strncasecmp(newline, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
                    145:                host = newline + sizeof(HTTP_URL) - 1;
                    146:        else if (strncasecmp(newline, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
                    147:                host = newline + sizeof(FTP_URL) - 1;
1.14      millert   148:                isftpurl = 1;
1.62      ray       149:        } else if (strncasecmp(newline, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
                    150:                host = newline + sizeof(FILE_URL) - 1;
1.22      deraadt   151:                isfileurl = 1;
1.61      deraadt   152: #ifndef SMALL
1.62      ray       153:        } else if (strncasecmp(newline, HTTPS_URL, sizeof(HTTPS_URL) - 1) == 0) {
                    154:                host = newline + sizeof(HTTPS_URL) - 1;
1.61      deraadt   155:                ishttpsurl = 1;
                    156: #endif
1.14      millert   157:        } else
1.62      ray       158:                errx(1, "url_get: Invalid URL '%s'", newline);
1.6       millert   159:
1.22      deraadt   160:        if (isfileurl) {
                    161:                path = host;
                    162:        } else {
                    163:                path = strchr(host, '/');               /* find path */
                    164:                if (EMPTYSTRING(path)) {
                    165:                        if (isftpurl)
                    166:                                goto noftpautologin;
                    167:                        warnx("Invalid URL (no `/' after host): %s", origline);
                    168:                        goto cleanup_url_get;
                    169:                }
                    170:                *path++ = '\0';
                    171:                if (EMPTYSTRING(path)) {
                    172:                        if (isftpurl)
                    173:                                goto noftpautologin;
                    174:                        warnx("Invalid URL (no file after host): %s", origline);
                    175:                        goto cleanup_url_get;
                    176:                }
1.14      millert   177:        }
1.1       millert   178:
1.17      millert   179:        if (outfile)
                    180:                savefile = outfile;
1.1       millert   181:        else
1.17      millert   182:                savefile = basename(path);
                    183:
1.14      millert   184:        if (EMPTYSTRING(savefile)) {
                    185:                if (isftpurl)
                    186:                        goto noftpautologin;
                    187:                warnx("Invalid URL (no file after directory): %s", origline);
1.6       millert   188:                goto cleanup_url_get;
1.14      millert   189:        }
1.1       millert   190:
1.59      uwe       191:        if (!isfileurl && proxyenv != NULL) {           /* use proxy */
1.61      deraadt   192: #ifndef SMALL
                    193:                if (ishttpsurl) {
                    194:                        sslpath = strdup(path);
                    195:                        sslhost = strdup(host);
                    196:                        if (! sslpath || ! sslhost)
                    197:                                errx(1, "Can't allocate memory for https path/host.");
                    198:                }
                    199: #endif
1.62      ray       200:                proxyurl = strdup(proxyenv);
                    201:                if (proxyurl == NULL)
1.14      millert   202:                        errx(1, "Can't allocate memory for proxy URL.");
1.62      ray       203:                if (strncasecmp(proxyurl, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
                    204:                        host = proxyurl + sizeof(HTTP_URL) - 1;
                    205:                else if (strncasecmp(proxyurl, FTP_URL, sizeof(FTP_URL) - 1) == 0)
                    206:                        host = proxyurl + sizeof(FTP_URL) - 1;
1.6       millert   207:                else {
1.14      millert   208:                        warnx("Malformed proxy URL: %s", proxyenv);
1.6       millert   209:                        goto cleanup_url_get;
                    210:                }
1.14      millert   211:                if (EMPTYSTRING(host)) {
                    212:                        warnx("Malformed proxy URL: %s", proxyenv);
1.6       millert   213:                        goto cleanup_url_get;
1.14      millert   214:                }
1.1       millert   215:                *--path = '/';                  /* add / back to real path */
                    216:                path = strchr(host, '/');       /* remove trailing / on host */
1.42      deraadt   217:                if (!EMPTYSTRING(path))
1.1       millert   218:                        *path++ = '\0';
1.62      ray       219:                path = newline;
1.1       millert   220:        }
                    221:
1.22      deraadt   222:        if (isfileurl) {
                    223:                struct stat st;
                    224:
                    225:                s = open(path, O_RDONLY);
                    226:                if (s == -1) {
                    227:                        warn("Can't open file %s", path);
                    228:                        goto cleanup_url_get;
                    229:                }
                    230:
                    231:                if (fstat(s, &st) == -1)
                    232:                        filesize = -1;
                    233:                else
                    234:                        filesize = st.st_size;
                    235:
                    236:                /* Open the output file.  */
                    237:                if (strcmp(savefile, "-") != 0) {
1.55      fgsch     238:                        out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC,
                    239:                            0666);
1.22      deraadt   240:                        if (out < 0) {
                    241:                                warn("Can't open %s", savefile);
                    242:                                goto cleanup_url_get;
                    243:                        }
                    244:                } else
                    245:                        out = fileno(stdout);
                    246:
                    247:                /* Trap signals */
                    248:                oldintr = NULL;
                    249:                if (setjmp(httpabort)) {
                    250:                        if (oldintr)
                    251:                                (void)signal(SIGINT, oldintr);
                    252:                        goto cleanup_url_get;
                    253:                }
                    254:                oldintr = signal(SIGINT, abortfile);
1.42      deraadt   255:
1.22      deraadt   256:                bytes = 0;
                    257:                hashbytes = mark;
                    258:                progressmeter(-1);
1.40      fgsch     259:
                    260:                if ((buf = malloc(4096)) == NULL)
1.47      deraadt   261:                        errx(1, "Can't allocate memory for transfer buffer");
1.42      deraadt   262:
1.22      deraadt   263:                /* Finally, suck down the file. */
                    264:                i = 0;
1.40      fgsch     265:                while ((len = read(s, buf, 4096)) > 0) {
1.22      deraadt   266:                        bytes += len;
                    267:                        for (cp = buf; len > 0; len -= i, cp += i) {
                    268:                                if ((i = write(out, cp, len)) == -1) {
                    269:                                        warn("Writing %s", savefile);
                    270:                                        goto cleanup_url_get;
                    271:                                }
                    272:                                else if (i == 0)
                    273:                                        break;
                    274:                        }
                    275:                        if (hash && !progress) {
                    276:                                while (bytes >= hashbytes) {
                    277:                                        (void)putc('#', ttyout);
                    278:                                        hashbytes += mark;
                    279:                                }
                    280:                                (void)fflush(ttyout);
                    281:                        }
                    282:                }
                    283:                if (hash && !progress && bytes > 0) {
                    284:                        if (bytes < mark)
                    285:                                (void)putc('#', ttyout);
                    286:                        (void)putc('\n', ttyout);
                    287:                        (void)fflush(ttyout);
                    288:                }
                    289:                if (len != 0) {
                    290:                        warn("Reading from file");
                    291:                        goto cleanup_url_get;
                    292:                }
                    293:                progressmeter(1);
                    294:                if (verbose)
                    295:                        fputs("Successfully retrieved file.\n", ttyout);
                    296:                (void)signal(SIGINT, oldintr);
1.42      deraadt   297:
1.40      fgsch     298:                rval = 0;
                    299:                goto cleanup_url_get;
1.22      deraadt   300:        }
                    301:
1.28      itojun    302:        if (*host == '[' && (hosttail = strrchr(host, ']')) != NULL &&
                    303:            (hosttail[1] == '\0' || hosttail[1] == ':')) {
                    304:                host++;
                    305:                *hosttail++ = '\0';
                    306:        } else
                    307:                hosttail = host;
                    308:
                    309:        portnum = strrchr(hosttail, ':');               /* find portnum */
1.1       millert   310:        if (portnum != NULL)
                    311:                *portnum++ = '\0';
                    312:
                    313:        if (debug)
1.10      deraadt   314:                fprintf(ttyout, "host %s, port %s, path %s, save as %s.\n",
1.1       millert   315:                    host, portnum, path, savefile);
                    316:
1.25      itojun    317:        memset(&hints, 0, sizeof(hints));
1.39      deraadt   318:        hints.ai_family = family;
1.25      itojun    319:        hints.ai_socktype = SOCK_STREAM;
1.61      deraadt   320: #ifndef SMALL
                    321:        port = portnum ? portnum : (ishttpsurl ? httpsport : httpport);
                    322: #else
1.25      itojun    323:        port = portnum ? portnum : httpport;
1.61      deraadt   324: #endif
1.25      itojun    325:        error = getaddrinfo(host, port, &hints, &res0);
1.61      deraadt   326:        /*
                    327:         * If the services file is corrupt/missing, fall back
                    328:         * on our hard-coded defines.
                    329:         */
1.30      deraadt   330:        if (error == EAI_SERVICE && port == httpport) {
                    331:                snprintf(pbuf, sizeof(pbuf), "%d", HTTP_PORT);
                    332:                error = getaddrinfo(host, pbuf, &hints, &res0);
1.61      deraadt   333: #ifndef SMALL
                    334:        } else if (error == EAI_SERVICE && port == httpsport) {
                    335:                snprintf(pbuf, sizeof(pbuf), "%d", HTTPS_PORT);
                    336:                error = getaddrinfo(host, pbuf, &hints, &res0);
                    337: #endif
1.30      deraadt   338:        }
1.25      itojun    339:        if (error) {
                    340:                warnx("%s: %s", gai_strerror(error), host);
                    341:                goto cleanup_url_get;
1.1       millert   342:        }
                    343:
1.25      itojun    344:        s = -1;
                    345:        for (res = res0; res; res = res->ai_next) {
1.44      itojun    346:                if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
                    347:                    sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
                    348:                        strlcpy(hbuf, "(unknown)", sizeof(hbuf));
1.41      deraadt   349:                if (verbose)
1.44      itojun    350:                        fprintf(ttyout, "Trying %s...\n", hbuf);
1.14      millert   351:
1.25      itojun    352:                s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
                    353:                if (s == -1) {
                    354:                        cause = "socket";
                    355:                        continue;
1.1       millert   356:                }
                    357:
1.25      itojun    358: again:
                    359:                if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
1.57      otto      360:                        int save_errno;
                    361:
1.25      itojun    362:                        if (errno == EINTR)
                    363:                                goto again;
1.57      otto      364:                        save_errno = errno;
1.25      itojun    365:                        close(s);
1.57      otto      366:                        errno = save_errno;
1.25      itojun    367:                        s = -1;
                    368:                        cause = "connect";
1.19      deraadt   369:                        continue;
                    370:                }
1.25      itojun    371:
1.29      itojun    372:                /* get port in numeric */
                    373:                if (getnameinfo(res->ai_addr, res->ai_addrlen, NULL, 0,
                    374:                    pbuf, sizeof(pbuf), NI_NUMERICSERV) == 0)
                    375:                        port = pbuf;
                    376:                else
                    377:                        port = NULL;
                    378:
1.61      deraadt   379: #ifndef SMALL
                    380:                if (proxyenv && sslhost)
                    381:                        proxy_connect(s, sslhost);
                    382: #endif
1.25      itojun    383:                break;
                    384:        }
                    385:        freeaddrinfo(res0);
                    386:        if (s < 0) {
1.33      millert   387:                warn("%s", cause);
1.6       millert   388:                goto cleanup_url_get;
1.1       millert   389:        }
                    390:
1.61      deraadt   391: #ifndef SMALL
                    392:        if (ishttpsurl) {
                    393:                if (proxyenv && sslpath) {
                    394:                        ishttpsurl = 0;
1.62      ray       395:                        proxyurl = NULL;
1.61      deraadt   396:                        path = sslpath;
                    397:                }
                    398:                SSL_library_init();
                    399:                SSL_load_error_strings();
                    400:                SSLeay_add_ssl_algorithms();
                    401:                ssl_ctx = SSL_CTX_new(SSLv23_client_method());
                    402:                ssl = SSL_new(ssl_ctx);
                    403:                if (ssl == NULL || ssl_ctx == NULL) {
                    404:                        ERR_print_errors_fp(ttyout);
                    405:                        goto cleanup_url_get;
                    406:                }
                    407:                if (SSL_set_fd(ssl, s) == 0) {
                    408:                        ERR_print_errors_fp(ttyout);
                    409:                        goto cleanup_url_get;
                    410:                }
                    411:                if (SSL_connect(ssl) <= 0) {
                    412:                        ERR_print_errors_fp(ttyout);
1.72    ! ray       413:                        goto cleanup_url_get;
1.61      deraadt   414:                }
                    415:        } else {
                    416:                fin = fdopen(s, "r+");
                    417:        }
                    418: #else
1.40      fgsch     419:        fin = fdopen(s, "r+");
1.61      deraadt   420: #endif
1.40      fgsch     421:
1.55      fgsch     422:        if (verbose)
                    423:                fprintf(ttyout, "Requesting %s", origline);
1.1       millert   424:        /*
1.40      fgsch     425:         * Construct and send the request. Proxy requests don't want leading /.
1.1       millert   426:         */
1.62      ray       427:        if (proxyurl) {
1.55      fgsch     428:                if (verbose)
                    429:                        fprintf(ttyout, " (via %s)\n", proxyenv);
1.32      itojun    430:                /*
                    431:                 * Host: directive must use the destination host address for
                    432:                 * the original URI (path).  We do not attach it at this moment.
                    433:                 */
1.61      deraadt   434:                ftp_printf(fin, ssl, "GET %s HTTP/1.0\r\n%s\r\n\r\n", path,
1.55      fgsch     435:                    HTTP_USER_AGENT);
1.28      itojun    436:        } else {
1.61      deraadt   437:                ftp_printf(fin, ssl, "GET /%s HTTP/1.0\r\nHost: ", path);
1.32      itojun    438:                if (strchr(host, ':')) {
                    439:                        char *h, *p;
                    440:
1.55      fgsch     441:                        /*
                    442:                         * strip off scoped address portion, since it's
                    443:                         * local to node
                    444:                         */
1.32      itojun    445:                        h = strdup(host);
                    446:                        if (h == NULL)
                    447:                                errx(1, "Can't allocate memory.");
                    448:                        if ((p = strchr(h, '%')) != NULL)
                    449:                                *p = '\0';
1.61      deraadt   450:                        ftp_printf(fin, ssl, "[%s]", h);
1.32      itojun    451:                        free(h);
1.55      fgsch     452:                } else
1.61      deraadt   453:                        ftp_printf(fin, ssl, "%s", host);
1.55      fgsch     454:
                    455:                /*
                    456:                 * Send port number only if it's specified and does not equal
                    457:                 * 80. Some broken HTTP servers get confused if you explicitly
                    458:                 * send them the port number.
                    459:                 */
1.61      deraadt   460: #ifndef SMALL
                    461:                if (port && strcmp(port, (ishttpsurl ? "443" : "80")) != 0)
                    462:                        ftp_printf(fin, ssl, ":%s", port);
                    463: #else
1.55      fgsch     464:                if (port && strcmp(port, "80") != 0)
1.61      deraadt   465:                        ftp_printf(fin, ssl, ":%s", port);
                    466: #endif
                    467:                ftp_printf(fin, ssl, "\r\n%s\r\n\r\n", HTTP_USER_AGENT);
1.55      fgsch     468:                if (verbose)
                    469:                        fprintf(ttyout, "\n");
1.28      itojun    470:        }
1.61      deraadt   471:        if (fin != NULL && fflush(fin) == EOF) {
1.14      millert   472:                warn("Writing HTTP request");
1.6       millert   473:                goto cleanup_url_get;
1.1       millert   474:        }
1.61      deraadt   475:        if ((buf = ftp_readline(fin, ssl, &len)) == NULL) {
1.40      fgsch     476:                warn("Receiving HTTP reply");
                    477:                goto cleanup_url_get;
1.1       millert   478:        }
1.40      fgsch     479:
                    480:        while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
                    481:                buf[--len] = '\0';
                    482:        if (debug)
                    483:                fprintf(ttyout, "received '%s'\n", buf);
                    484:
1.1       millert   485:        cp = strchr(buf, ' ');
                    486:        if (cp == NULL)
                    487:                goto improper;
                    488:        else
                    489:                cp++;
1.69      jsg       490:
                    491:        strlcpy(ststr, cp, sizeof(ststr));
                    492:        status = strtonum(ststr, 200, 307, &errstr);
                    493:        if (errstr) {
                    494:                warnx("Error retrieving file: %s", cp);
                    495:                goto cleanup_url_get;
                    496:        }
                    497:
                    498:        switch (status) {
                    499:        case 200:       /* OK */
                    500:                break;
                    501:        case 301:       /* Moved Permanently */
                    502:        case 302:       /* Found */
                    503:        case 303:       /* See Other */
                    504:        case 307:       /* Temporary Redirect */
1.40      fgsch     505:                isredirect++;
1.54      fgsch     506:                if (redirect_loop++ > 10) {
                    507:                        warnx("Too many redirections requested");
                    508:                        goto cleanup_url_get;
                    509:                }
1.69      jsg       510:                break;
                    511:        default:
1.1       millert   512:                warnx("Error retrieving file: %s", cp);
1.6       millert   513:                goto cleanup_url_get;
1.1       millert   514:        }
                    515:
                    516:        /*
                    517:         * Read the rest of the header.
                    518:         */
1.40      fgsch     519:        free(buf);
                    520:        filesize = -1;
                    521:
1.62      ray       522:        for (;;) {
1.61      deraadt   523:                if ((buf = ftp_readline(fin, ssl, &len)) == NULL) {
1.40      fgsch     524:                        warn("Receiving HTTP reply");
                    525:                        goto cleanup_url_get;
                    526:                }
1.61      deraadt   527:
1.40      fgsch     528:                while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
                    529:                        buf[--len] = '\0';
                    530:                if (len == 0)
1.1       millert   531:                        break;
1.40      fgsch     532:                if (debug)
                    533:                        fprintf(ttyout, "received '%s'\n", buf);
1.1       millert   534:
1.40      fgsch     535:                /* Look for some headers */
                    536:                cp = buf;
1.1       millert   537: #define CONTENTLEN "Content-Length: "
1.40      fgsch     538:                if (strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0) {
                    539:                        cp += sizeof(CONTENTLEN) - 1;
1.58      grunk     540:                        filesize = strtonum(cp, 0, LLONG_MAX, &errstr);
                    541:                        if (errstr != NULL)
1.40      fgsch     542:                                goto improper;
                    543: #define LOCATION "Location: "
                    544:                } else if (isredirect &&
                    545:                    strncasecmp(cp, LOCATION, sizeof(LOCATION) - 1) == 0) {
                    546:                        cp += sizeof(LOCATION) - 1;
                    547:                        if (verbose)
                    548:                                fprintf(ttyout, "Redirected to %s\n", cp);
                    549:                        if (fin != NULL)
                    550:                                fclose(fin);
                    551:                        else if (s != -1)
                    552:                                close(s);
1.67      steven    553:                        free(proxyurl);
1.62      ray       554:                        free(newline);
1.40      fgsch     555:                        rval = url_get(cp, proxyenv, outfile);
1.67      steven    556:                        free(buf);
1.40      fgsch     557:                        return (rval);
                    558:                }
1.1       millert   559:        }
                    560:
1.17      millert   561:        /* Open the output file.  */
                    562:        if (strcmp(savefile, "-") != 0) {
1.10      deraadt   563:                out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
                    564:                if (out < 0) {
                    565:                        warn("Can't open %s", savefile);
                    566:                        goto cleanup_url_get;
                    567:                }
                    568:        } else
1.17      millert   569:                out = fileno(stdout);
1.1       millert   570:
                    571:        /* Trap signals */
                    572:        oldintr = NULL;
                    573:        if (setjmp(httpabort)) {
                    574:                if (oldintr)
1.2       millert   575:                        (void)signal(SIGINT, oldintr);
1.6       millert   576:                goto cleanup_url_get;
1.1       millert   577:        }
                    578:        oldintr = signal(SIGINT, aborthttp);
                    579:
                    580:        bytes = 0;
                    581:        hashbytes = mark;
                    582:        progressmeter(-1);
1.43      millert   583:
                    584:        free(buf);
1.1       millert   585:
                    586:        /* Finally, suck down the file. */
1.40      fgsch     587:        if ((buf = malloc(4096)) == NULL)
1.47      deraadt   588:                errx(1, "Can't allocate memory for transfer buffer");
1.1       millert   589:        i = 0;
1.61      deraadt   590:        len = 1;
                    591:        while (len > 0) {
                    592:                len = ftp_read(fin, ssl, buf, 4096);
1.1       millert   593:                bytes += len;
1.61      deraadt   594:                for (cp = buf, wlen = len; wlen > 0; wlen -= i, cp += i) {
                    595:                        if ((i = write(out, cp, wlen)) == -1) {
1.1       millert   596:                                warn("Writing %s", savefile);
1.6       millert   597:                                goto cleanup_url_get;
1.1       millert   598:                        }
                    599:                        else if (i == 0)
                    600:                                break;
                    601:                }
                    602:                if (hash && !progress) {
                    603:                        while (bytes >= hashbytes) {
1.10      deraadt   604:                                (void)putc('#', ttyout);
1.1       millert   605:                                hashbytes += mark;
                    606:                        }
1.10      deraadt   607:                        (void)fflush(ttyout);
1.1       millert   608:                }
                    609:        }
                    610:        if (hash && !progress && bytes > 0) {
                    611:                if (bytes < mark)
1.10      deraadt   612:                        (void)putc('#', ttyout);
                    613:                (void)putc('\n', ttyout);
                    614:                (void)fflush(ttyout);
1.1       millert   615:        }
                    616:        if (len != 0) {
                    617:                warn("Reading from socket");
1.6       millert   618:                goto cleanup_url_get;
1.1       millert   619:        }
                    620:        progressmeter(1);
1.24      deraadt   621:        if (filesize != -1 && len == 0 && bytes != filesize) {
                    622:                if (verbose)
                    623:                        fputs("Read short file.\n", ttyout);
                    624:                goto cleanup_url_get;
                    625:        }
                    626:
1.1       millert   627:        if (verbose)
1.10      deraadt   628:                fputs("Successfully retrieved file.\n", ttyout);
1.2       millert   629:        (void)signal(SIGINT, oldintr);
1.1       millert   630:
1.40      fgsch     631:        rval = 0;
                    632:        goto cleanup_url_get;
1.1       millert   633:
1.14      millert   634: noftpautologin:
                    635:        warnx(
                    636:            "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
                    637:        goto cleanup_url_get;
                    638:
1.1       millert   639: improper:
1.8       millert   640:        warnx("Improper response from %s", host);
1.14      millert   641:
1.6       millert   642: cleanup_url_get:
1.61      deraadt   643: #ifndef SMALL
                    644:        if (ssl) {
                    645:                SSL_shutdown(ssl);
                    646:                SSL_free(ssl);
                    647:        }
                    648: #endif
1.40      fgsch     649:        if (fin != NULL)
                    650:                fclose(fin);
                    651:        else if (s != -1)
1.1       millert   652:                close(s);
1.67      steven    653:        free(buf);
                    654:        free(proxyurl);
1.62      ray       655:        free(newline);
1.40      fgsch     656:        return (rval);
1.1       millert   657: }
                    658:
                    659: /*
                    660:  * Abort a http retrieval
                    661:  */
1.51      deraadt   662: /* ARGSUSED */
1.1       millert   663: void
1.51      deraadt   664: aborthttp(int signo)
1.1       millert   665: {
                    666:
                    667:        alarmtimer(0);
1.10      deraadt   668:        fputs("\nhttp fetch aborted.\n", ttyout);
                    669:        (void)fflush(ttyout);
1.1       millert   670:        longjmp(httpabort, 1);
                    671: }
                    672:
                    673: /*
1.22      deraadt   674:  * Abort a http retrieval
                    675:  */
1.51      deraadt   676: /* ARGSUSED */
1.22      deraadt   677: void
1.51      deraadt   678: abortfile(int signo)
1.22      deraadt   679: {
                    680:
                    681:        alarmtimer(0);
                    682:        fputs("\nfile fetch aborted.\n", ttyout);
                    683:        (void)fflush(ttyout);
                    684:        longjmp(httpabort, 1);
                    685: }
                    686:
                    687: /*
1.1       millert   688:  * Retrieve multiple files from the command line, transferring
                    689:  * files of the form "host:path", "ftp://host/path" using the
                    690:  * ftp protocol, and files of the form "http://host/path" using
                    691:  * the http protocol.
1.2       millert   692:  * If path has a trailing "/", then return (-1);
1.1       millert   693:  * the path will be cd-ed into and the connection remains open,
                    694:  * and the function will return -1 (to indicate the connection
                    695:  * is alive).
                    696:  * If an error occurs the return value will be the offset+1 in
                    697:  * argv[] of the file that caused a problem (i.e, argv[x]
                    698:  * returns x+1)
                    699:  * Otherwise, 0 is returned if all files retrieved successfully.
                    700:  */
                    701: int
1.50      deraadt   702: auto_fetch(int argc, char *argv[], char *outfile)
1.1       millert   703: {
                    704:        char *xargv[5];
1.62      ray       705:        char *cp, *url, *host, *dir, *file, *portnum;
                    706:        char *username, *pass, *pathstart;
1.6       millert   707:        char *ftpproxy, *httpproxy;
1.14      millert   708:        int rval, xargc;
                    709:        volatile int argpos;
1.49      krw       710:        int dirhasglob, filehasglob, oautologin;
1.14      millert   711:        char rempath[MAXPATHLEN];
1.1       millert   712:
                    713:        argpos = 0;
                    714:
                    715:        if (setjmp(toplevel)) {
                    716:                if (connected)
                    717:                        disconnect(0, NULL);
1.2       millert   718:                return (argpos + 1);
1.1       millert   719:        }
1.3       millert   720:        (void)signal(SIGINT, (sig_t)intr);
                    721:        (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1       millert   722:
1.45      millert   723:        if ((ftpproxy = getenv(FTP_PROXY)) != NULL && *ftpproxy == '\0')
                    724:                ftpproxy = NULL;
                    725:        if ((httpproxy = getenv(HTTP_PROXY)) != NULL && *httpproxy == '\0')
                    726:                httpproxy = NULL;
1.6       millert   727:
1.1       millert   728:        /*
                    729:         * Loop through as long as there's files to fetch.
                    730:         */
1.62      ray       731:        for (rval = 0; (rval == 0) && (argpos < argc); free(url), argpos++) {
1.1       millert   732:                if (strchr(argv[argpos], ':') == NULL)
                    733:                        break;
1.62      ray       734:                host = dir = file = portnum = username = pass = NULL;
1.1       millert   735:
                    736:                /*
                    737:                 * We muck with the string, so we make a copy.
                    738:                 */
1.62      ray       739:                url = strdup(argv[argpos]);
                    740:                if (url == NULL)
1.1       millert   741:                        errx(1, "Can't allocate memory for auto-fetch.");
                    742:
                    743:                /*
                    744:                 * Try HTTP URL-style arguments first.
                    745:                 */
1.62      ray       746:                if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
1.61      deraadt   747: #ifndef SMALL
                    748:                    /* even if we compiled without SSL, url_get will check */
1.62      ray       749:                    strncasecmp(url, HTTPS_URL, sizeof(HTTPS_URL) -1) == 0 ||
1.61      deraadt   750: #endif
1.62      ray       751:                    strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
1.54      fgsch     752:                        redirect_loop = 0;
1.62      ray       753:                        if (url_get(url, httpproxy, outfile) == -1)
1.1       millert   754:                                rval = argpos + 1;
                    755:                        continue;
                    756:                }
                    757:
                    758:                /*
1.6       millert   759:                 * Try FTP URL-style arguments next. If ftpproxy is
                    760:                 * set, use url_get() instead of standard ftp.
                    761:                 * Finally, try host:file.
1.1       millert   762:                 */
1.62      ray       763:                host = url;
                    764:                if (strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.37      heko      765:                        char *passend, *passagain, *userend;
1.31      itojun    766:
1.6       millert   767:                        if (ftpproxy) {
1.62      ray       768:                                if (url_get(url, ftpproxy, outfile) == -1)
1.6       millert   769:                                        rval = argpos + 1;
                    770:                                continue;
                    771:                        }
1.1       millert   772:                        host += sizeof(FTP_URL) - 1;
1.8       millert   773:                        dir = strchr(host, '/');
1.1       millert   774:
1.8       millert   775:                        /* Look for [user:pass@]host[:port] */
1.31      itojun    776:
                    777:                        /* check if we have "user:pass@" */
1.37      heko      778:                        userend = strchr(host, ':');
1.31      itojun    779:                        passend = strchr(host, '@');
                    780:                        if (passend && userend && userend < passend &&
                    781:                            (!dir || passend < dir)) {
1.62      ray       782:                                username = host;
1.31      itojun    783:                                pass = userend + 1;
                    784:                                host = passend + 1;
                    785:                                *userend = *passend = '\0';
1.37      heko      786:                                passagain = strchr(host, '@');
1.42      deraadt   787:                                if (strchr(pass, '@') != NULL ||
1.37      heko      788:                                    (passagain != NULL && passagain < dir)) {
                    789:                                        warnx(at_encoding_warning);
                    790:                                        goto bad_ftp_url;
1.42      deraadt   791:                                }
1.31      itojun    792:
1.62      ray       793:                                if (EMPTYSTRING(username) || EMPTYSTRING(pass)) {
1.11      millert   794: bad_ftp_url:
1.31      itojun    795:                                        warnx("Invalid URL: %s", argv[argpos]);
                    796:                                        rval = argpos + 1;
                    797:                                        continue;
                    798:                                }
1.62      ray       799:                                username = urldecode(username);
1.37      heko      800:                                pass = urldecode(pass);
1.8       millert   801:                        }
1.31      itojun    802:
                    803: #ifdef INET6
                    804:                        /* check [host]:port, or [host] */
                    805:                        if (host[0] == '[') {
                    806:                                cp = strchr(host, ']');
                    807:                                if (cp && (!dir || cp < dir)) {
                    808:                                        if (cp + 1 == dir || cp[1] == ':') {
                    809:                                                host++;
                    810:                                                *cp++ = '\0';
                    811:                                        } else
                    812:                                                cp = NULL;
                    813:                                } else
                    814:                                        cp = host;
1.25      itojun    815:                        } else
                    816:                                cp = host;
1.31      itojun    817: #else
                    818:                        cp = host;
1.25      itojun    819: #endif
1.31      itojun    820:
                    821:                        /* split off host[:port] if there is */
                    822:                        if (cp) {
                    823:                                portnum = strchr(cp, ':');
1.52      henning   824:                                pathstart = strchr(cp, '/');
                    825:                                /* : in path is not a port # indicator */
                    826:                                if (portnum && pathstart &&
                    827:                                    pathstart < portnum)
                    828:                                        portnum = NULL;
                    829:
1.31      itojun    830:                                if (!portnum)
                    831:                                        ;
                    832:                                else {
                    833:                                        if (!dir)
                    834:                                                ;
                    835:                                        else if (portnum + 1 < dir) {
                    836:                                                *portnum++ = '\0';
                    837:                                                /*
                    838:                                                 * XXX should check if portnum
                    839:                                                 * is decimal number
                    840:                                                 */
                    841:                                        } else {
                    842:                                                /* empty portnum */
                    843:                                                goto bad_ftp_url;
                    844:                                        }
                    845:                                }
                    846:                        } else
                    847:                                portnum = NULL;
1.8       millert   848:                } else {                        /* classic style `host:file' */
                    849:                        dir = strchr(host, ':');
                    850:                }
1.1       millert   851:                if (EMPTYSTRING(host)) {
                    852:                        rval = argpos + 1;
                    853:                        continue;
                    854:                }
                    855:
                    856:                /*
1.9       millert   857:                 * If dir is NULL, the file wasn't specified
1.1       millert   858:                 * (URL looked something like ftp://host)
                    859:                 */
1.8       millert   860:                if (dir != NULL)
                    861:                        *dir++ = '\0';
1.1       millert   862:
                    863:                /*
                    864:                 * Extract the file and (if present) directory name.
                    865:                 */
1.42      deraadt   866:                if (!EMPTYSTRING(dir)) {
1.8       millert   867:                        cp = strrchr(dir, '/');
1.1       millert   868:                        if (cp != NULL) {
                    869:                                *cp++ = '\0';
                    870:                                file = cp;
                    871:                        } else {
                    872:                                file = dir;
                    873:                                dir = NULL;
                    874:                        }
                    875:                }
                    876:                if (debug)
1.42      deraadt   877:                        fprintf(ttyout,
                    878:                            "user %s:%s host %s port %s dir %s file %s\n",
1.62      ray       879:                            username, pass, host, portnum, dir, file);
1.1       millert   880:
                    881:                /*
1.49      krw       882:                 * Set up the connection.
1.1       millert   883:                 */
1.49      krw       884:                if (connected)
                    885:                        disconnect(0, NULL);
                    886:                xargv[0] = __progname;
                    887:                xargv[1] = host;
1.8       millert   888:                xargv[2] = NULL;
1.49      krw       889:                xargc = 2;
                    890:                if (!EMPTYSTRING(portnum)) {
                    891:                        xargv[2] = portnum;
                    892:                        xargv[3] = NULL;
                    893:                        xargc = 3;
                    894:                }
                    895:                oautologin = autologin;
1.62      ray       896:                if (username != NULL)
1.49      krw       897:                        autologin = 0;
                    898:                setpeer(xargc, xargv);
                    899:                autologin = oautologin;
                    900:                if ((connected == 0) ||
1.62      ray       901:                    ((connected == 1) && !ftp_login(host, username, pass))) {
1.49      krw       902:                        warnx("Can't connect or login to host `%s'", host);
1.8       millert   903:                        rval = argpos + 1;
                    904:                        continue;
1.1       millert   905:                }
1.49      krw       906:
                    907:                /* Always use binary transfers. */
                    908:                setbinary(0, NULL);
1.1       millert   909:
1.4       millert   910:                dirhasglob = filehasglob = 0;
                    911:                if (doglob) {
1.42      deraadt   912:                        if (!EMPTYSTRING(dir) &&
1.4       millert   913:                            strpbrk(dir, "*?[]{}") != NULL)
                    914:                                dirhasglob = 1;
1.42      deraadt   915:                        if (!EMPTYSTRING(file) &&
1.4       millert   916:                            strpbrk(file, "*?[]{}") != NULL)
                    917:                                filehasglob = 1;
                    918:                }
                    919:
1.1       millert   920:                /* Change directories, if necessary. */
1.42      deraadt   921:                if (!EMPTYSTRING(dir) && !dirhasglob) {
1.1       millert   922:                        xargv[0] = "cd";
                    923:                        xargv[1] = dir;
                    924:                        xargv[2] = NULL;
                    925:                        cd(2, xargv);
1.42      deraadt   926:                        if (!dirchange) {
1.1       millert   927:                                rval = argpos + 1;
                    928:                                continue;
                    929:                        }
                    930:                }
                    931:
                    932:                if (EMPTYSTRING(file)) {
                    933:                        rval = -1;
                    934:                        continue;
                    935:                }
                    936:
1.21      marc      937:                if (verbose)
1.10      deraadt   938:                        fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);
1.1       millert   939:
1.4       millert   940:                if (dirhasglob) {
                    941:                        snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
                    942:                        file = rempath;
                    943:                }
                    944:
                    945:                /* Fetch the file(s). */
1.10      deraadt   946:                xargc = 2;
1.1       millert   947:                xargv[0] = "get";
                    948:                xargv[1] = file;
                    949:                xargv[2] = NULL;
1.4       millert   950:                if (dirhasglob || filehasglob) {
                    951:                        int ointeractive;
                    952:
                    953:                        ointeractive = interactive;
                    954:                        interactive = 0;
                    955:                        xargv[0] = "mget";
1.10      deraadt   956:                        mget(xargc, xargv);
1.5       millert   957:                        interactive = ointeractive;
1.10      deraadt   958:                } else {
1.17      millert   959:                        if (outfile != NULL) {
                    960:                                xargv[2] = outfile;
                    961:                                xargv[3] = NULL;
1.10      deraadt   962:                                xargc++;
                    963:                        }
                    964:                        get(xargc, xargv);
                    965:                }
1.1       millert   966:
1.4       millert   967:                if ((code / 100) != COMPLETE)
1.1       millert   968:                        rval = argpos + 1;
                    969:        }
                    970:        if (connected && rval != -1)
                    971:                disconnect(0, NULL);
                    972:        return (rval);
1.37      heko      973: }
                    974:
                    975: char *
1.50      deraadt   976: urldecode(const char *str)
1.37      heko      977: {
1.53      deraadt   978:        char *ret, c;
                    979:        int i, reallen;
1.37      heko      980:
1.53      deraadt   981:        if (str == NULL)
                    982:                return NULL;
                    983:        if ((ret = malloc(strlen(str)+1)) == NULL)
                    984:                err(1, "Can't allocate memory for URL decoding");
                    985:        for (i = 0, reallen = 0; str[i] != '\0'; i++, reallen++, ret++) {
                    986:                c = str[i];
                    987:                if (c == '+') {
                    988:                        *ret = ' ';
                    989:                        continue;
                    990:                }
1.61      deraadt   991:
                    992:                /* Cannot use strtol here because next char
                    993:                 * after %xx may be a digit.
                    994:                 */
1.53      deraadt   995:                if (c == '%' && isxdigit(str[i+1]) && isxdigit(str[i+2])) {
                    996:                        *ret = hextochar(&str[i+1]);
                    997:                        i+=2;
                    998:                        continue;
                    999:                }
                   1000:                *ret = c;
                   1001:        }
                   1002:        *ret = '\0';
                   1003:
                   1004:        return ret-reallen;
1.37      heko     1005: }
                   1006:
                   1007: char
1.50      deraadt  1008: hextochar(const char *str)
1.37      heko     1009: {
1.53      deraadt  1010:        char c, ret;
1.37      heko     1011:
1.53      deraadt  1012:        c = str[0];
                   1013:        ret = c;
                   1014:        if (isalpha(c))
                   1015:                ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
                   1016:        else
                   1017:                ret -= '0';
                   1018:        ret *= 16;
                   1019:
                   1020:        c = str[1];
                   1021:        ret += c;
                   1022:        if (isalpha(c))
                   1023:                ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
                   1024:        else
                   1025:                ret -= '0';
                   1026:        return ret;
1.25      itojun   1027: }
                   1028:
                   1029: int
1.50      deraadt  1030: isurl(const char *p)
1.25      itojun   1031: {
1.27      millert  1032:
1.26      deraadt  1033:        if (strncasecmp(p, FTP_URL, sizeof(FTP_URL) - 1) == 0 ||
                   1034:            strncasecmp(p, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
1.61      deraadt  1035: #ifndef SMALL
                   1036:            strncasecmp(p, HTTPS_URL, sizeof(HTTPS_URL) - 1) == 0 ||
                   1037: #endif
1.27      millert  1038:            strncasecmp(p, FILE_URL, sizeof(FILE_URL) - 1) == 0 ||
                   1039:            strstr(p, ":/"))
                   1040:                return (1);
                   1041:        return (0);
1.1       millert  1042: }
1.61      deraadt  1043:
                   1044: char *
                   1045: ftp_readline(FILE *fp, SSL *ssl, size_t *lenp)
                   1046: {
                   1047:        if (fp != NULL)
                   1048:                return fparseln(fp, lenp, NULL, "\0\0\0", 0);
                   1049: #ifndef SMALL
                   1050:        else if (ssl != NULL)
                   1051:                return SSL_readline(ssl, lenp);
                   1052: #endif
                   1053:        else
                   1054:                return NULL;
                   1055: }
                   1056:
1.65      ray      1057: size_t
1.61      deraadt  1058: ftp_read(FILE *fp, SSL *ssl, char *buf, size_t len)
                   1059: {
1.65      ray      1060:        size_t ret;
1.61      deraadt  1061:        if (fp != NULL)
                   1062:                ret = fread(buf, sizeof(char), len, fp);
                   1063: #ifndef SMALL
1.65      ray      1064:        else if (ssl != NULL) {
                   1065:                int nr;
                   1066:
                   1067:                if (len > INT_MAX)
                   1068:                        len = INT_MAX;
                   1069:                if ((nr = SSL_read(ssl, buf, (int)len)) <= 0)
                   1070:                        ret = 0;
                   1071:                else
                   1072:                        ret = nr;
                   1073:        }
1.61      deraadt  1074: #endif
                   1075:        else
                   1076:                ret = 0;
                   1077:        return (ret);
                   1078: }
                   1079:
                   1080: int
                   1081: ftp_printf(FILE *fp, SSL *ssl, const char *fmt, ...)
                   1082: {
                   1083:        int ret;
                   1084:        va_list ap;
                   1085:
                   1086:        va_start(ap, fmt);
                   1087:
                   1088:        if (fp != NULL)
                   1089:                ret = vfprintf(fp, fmt, ap);
                   1090: #ifndef SMALL
                   1091:        else if (ssl != NULL)
                   1092:                ret = SSL_vprintf((SSL*)ssl, fmt, ap);
                   1093: #endif
                   1094:        else
                   1095:                ret = NULL;
                   1096:
                   1097:        va_end(ap);
                   1098:        return (ret);
                   1099: }
                   1100:
                   1101: #ifndef SMALL
                   1102: int
                   1103: SSL_vprintf(SSL *ssl, const char *fmt, va_list ap)
                   1104: {
                   1105:        int ret;
                   1106:        char *string;
                   1107:
                   1108:        if ((ret = vasprintf(&string, fmt, ap)) == -1)
                   1109:                return ret;
1.64      ray      1110:        ret = SSL_write(ssl, string, ret);
1.61      deraadt  1111:        free(string);
                   1112:        return ret;
                   1113: }
                   1114:
                   1115: char *
                   1116: SSL_readline(SSL *ssl, size_t *lenp)
                   1117: {
1.63      ray      1118:        size_t i, len;
1.61      deraadt  1119:        char *buf, *q, c;
                   1120:
                   1121:        len = 128;
                   1122:        if ((buf = malloc(len)) == NULL)
                   1123:                errx(1, "Can't allocate memory for transfer buffer");
                   1124:        for (i = 0; ; i++) {
                   1125:                if (i >= len - 1) {
                   1126:                        if ((q = realloc(buf, 2 * len)) == NULL)
                   1127:                                errx(1, "Can't expand transfer buffer");
                   1128:                        buf = q;
                   1129:                        len *= 2;
                   1130:                }
                   1131:                if (SSL_read(ssl, &c, 1) <= 0)
                   1132:                        break;
                   1133:                buf[i] = c;
                   1134:                if (c == '\n')
                   1135:                        break;
                   1136:        }
                   1137:        *lenp = i;
                   1138:        return (buf);
                   1139: }
                   1140:
                   1141: int
                   1142: proxy_connect(int socket, char *host)
                   1143: {
1.66      ray      1144:        int l;
1.61      deraadt  1145:        char buf[1024];
                   1146:        char *connstr, *hosttail, *port;
                   1147:
                   1148:        if (*host == '[' && (hosttail = strrchr(host, ']')) != NULL &&
                   1149:                (hosttail[1] == '\0' || hosttail[1] == ':')) {
                   1150:                host++;
                   1151:                *hosttail++ = '\0';
                   1152:        } else
                   1153:                hosttail = host;
                   1154:
                   1155:        port = strrchr(hosttail, ':');               /* find portnum */
                   1156:        if (port != NULL)
                   1157:                *port++ = '\0';
                   1158:        if (!port)
                   1159:                port = "443";
                   1160:
1.66      ray      1161:        l = asprintf(&connstr, "CONNECT %s:%s HTTP/1.1\n\n", host, port);
                   1162:        if (l == -1)
1.61      deraadt  1163:                errx(1, "Could not allocate memory to assemble connect string!");
1.68      ray      1164:        if (debug)
                   1165:                printf("%s", connstr);
1.66      ray      1166:        if (write(socket, connstr, l) != l)
1.68      ray      1167:                err(1, "Could not send connect string");
1.61      deraadt  1168:        read(socket, &buf, sizeof(buf)); /* only proxy header XXX: error handling? */
1.71      ray      1169:        free(connstr);
1.61      deraadt  1170:        return(200);
                   1171: }
                   1172: #endif