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

1.129   ! deraadt     1: /*     $OpenBSD: fetch.c,v 1.128 2014/08/25 11:33:55 jca 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:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     21:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     22:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1.15      millert    23:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     24:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1.1       millert    25:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     26:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     27:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     28:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     29:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     30:  * POSSIBILITY OF SUCH DAMAGE.
                     31:  */
                     32:
                     33: /*
                     34:  * FTP User Program -- Command line file retrieval
                     35:  */
                     36:
                     37: #include <sys/types.h>
                     38: #include <sys/param.h>
                     39: #include <sys/socket.h>
1.22      deraadt    40: #include <sys/stat.h>
1.1       millert    41:
                     42: #include <netinet/in.h>
                     43:
                     44: #include <arpa/ftp.h>
                     45: #include <arpa/inet.h>
                     46:
                     47: #include <ctype.h>
                     48: #include <err.h>
1.17      millert    49: #include <libgen.h>
1.58      grunk      50: #include <limits.h>
1.1       millert    51: #include <netdb.h>
                     52: #include <fcntl.h>
1.3       millert    53: #include <signal.h>
1.1       millert    54: #include <stdio.h>
1.61      deraadt    55: #include <stdarg.h>
1.19      deraadt    56: #include <errno.h>
1.1       millert    57: #include <stdlib.h>
                     58: #include <string.h>
                     59: #include <unistd.h>
1.40      fgsch      60: #include <util.h>
1.73      drahn      61: #include <resolv.h>
1.1       millert    62:
1.61      deraadt    63: #ifndef SMALL
1.126     jsing      64: #include <ressl.h>
1.78      martynas   65: #else /* !SMALL */
1.126     jsing      66: struct ressl;
1.78      martynas   67: #endif /* !SMALL */
1.61      deraadt    68:
1.1       millert    69: #include "ftp_var.h"
1.86      martynas   70: #include "cmds.h"
1.1       millert    71:
1.38      millert    72: static int     url_get(const char *, const char *, const char *);
                     73: void           aborthttp(int);
                     74: void           abortfile(int);
1.42      deraadt    75: char           hextochar(const char *);
                     76: char           *urldecode(const char *);
1.123     guenther   77: char           *recode_credentials(const char *_userinfo);
1.126     jsing      78: int            ftp_printf(FILE *, struct ressl *, const char *, ...) __attribute__((format(printf, 3, 4)));
                     79: char           *ftp_readline(FILE *, struct ressl *, size_t *);
                     80: size_t         ftp_read(FILE *, struct ressl *, char *, size_t);
1.61      deraadt    81: #ifndef SMALL
1.81      espie      82: int            proxy_connect(int, char *, char *);
1.126     jsing      83: int            SSL_vprintf(struct ressl *, const char *, va_list);
                     84: char           *SSL_readline(struct ressl *, size_t *);
1.78      martynas   85: #endif /* !SMALL */
1.14      millert    86:
1.1       millert    87: #define        FTP_URL         "ftp://"        /* ftp URL prefix */
                     88: #define        HTTP_URL        "http://"       /* http URL prefix */
1.61      deraadt    89: #define        HTTPS_URL       "https://"      /* https URL prefix */
1.22      deraadt    90: #define        FILE_URL        "file:"         /* file URL prefix */
1.6       millert    91: #define FTP_PROXY      "ftp_proxy"     /* env var with ftp proxy location */
1.1       millert    92: #define HTTP_PROXY     "http_proxy"    /* env var with http proxy location */
                     93:
                     94: #define EMPTYSTRING(x) ((x) == NULL || (*(x) == '\0'))
                     95:
1.110     guenther   96: static const char at_encoding_warning[] =
1.53      deraadt    97:     "Extra `@' characters in usernames and passwords should be encoded as %%40";
1.37      heko       98:
1.1       millert    99: jmp_buf        httpabort;
                    100:
1.54      fgsch     101: static int     redirect_loop;
                    102:
1.1       millert   103: /*
1.95      martynas  104:  * Determine whether the character needs encoding, per RFC1738:
                    105:  *     - No corresponding graphic US-ASCII.
                    106:  *     - Unsafe characters.
                    107:  */
                    108: static int
                    109: unsafe_char(const char *c)
                    110: {
                    111:        const char *unsafe_chars = " <>\"#{}|\\^~[]`";
                    112:
                    113:        /*
                    114:         * No corresponding graphic US-ASCII.
                    115:         * Control characters and octets not used in US-ASCII.
                    116:         */
                    117:        return (iscntrl(*c) || !isascii(*c) ||
                    118:
                    119:            /*
                    120:             * Unsafe characters.
                    121:             * '%' is also unsafe, if is not followed by two
                    122:             * hexadecimal digits.
                    123:             */
                    124:            strchr(unsafe_chars, *c) != NULL ||
                    125:            (*c == '%' && (!isxdigit(*++c) || !isxdigit(*++c))));
                    126: }
                    127:
                    128: /*
                    129:  * Encode given URL, per RFC1738.
                    130:  * Allocate and return string to the caller.
                    131:  */
                    132: static char *
                    133: url_encode(const char *path)
                    134: {
                    135:        size_t i, length, new_length;
                    136:        char *epath, *epathp;
                    137:
                    138:        length = new_length = strlen(path);
                    139:
                    140:        /*
                    141:         * First pass:
                    142:         * Count unsafe characters, and determine length of the
                    143:         * final URL.
                    144:         */
                    145:        for (i = 0; i < length; i++)
                    146:                if (unsafe_char(path + i))
                    147:                        new_length += 2;
                    148:
                    149:        epath = epathp = malloc(new_length + 1);        /* One more for '\0'. */
                    150:        if (epath == NULL)
1.102     halex     151:                err(1, "Can't allocate memory for URL encoding");
1.95      martynas  152:
                    153:        /*
                    154:         * Second pass:
                    155:         * Encode, and copy final URL.
                    156:         */
                    157:        for (i = 0; i < length; i++)
                    158:                if (unsafe_char(path + i)) {
                    159:                        snprintf(epathp, 4, "%%" "%02x", path[i]);
                    160:                        epathp += 3;
                    161:                } else
                    162:                        *(epathp++) = path[i];
                    163:
                    164:        *epathp = '\0';
                    165:        return (epath);
                    166: }
                    167:
                    168: /*
1.6       millert   169:  * Retrieve URL, via the proxy in $proxyvar if necessary.
1.1       millert   170:  * Modifies the string argument given.
                    171:  * Returns -1 on failure, 0 on success
                    172:  */
1.14      millert   173: static int
1.50      deraadt   174: url_get(const char *origline, const char *proxyenv, const char *outfile)
1.1       millert   175: {
1.69      jsg       176:        char pbuf[NI_MAXSERV], hbuf[NI_MAXHOST], *cp, *portnum, *path, ststr[4];
1.62      ray       177:        char *hosttail, *cause = "unknown", *newline, *host, *port, *buf = NULL;
1.106     haesbaer  178:        char *epath, *redirurl, *loctail, *h, *p;
1.53      deraadt   179:        int error, i, isftpurl = 0, isfileurl = 0, isredirect = 0, rval = -1;
1.105     haesbaer  180:        struct addrinfo hints, *res0, *res, *ares = NULL;
1.34      millert   181:        const char * volatile savefile;
1.62      ray       182:        char * volatile proxyurl = NULL;
1.123     guenther  183:        char *credentials = NULL;
1.53      deraadt   184:        volatile int s = -1, out;
1.97      martynas  185:        volatile sig_t oldintr, oldinti;
1.53      deraadt   186:        FILE *fin = NULL;
1.1       millert   187:        off_t hashbytes;
1.58      grunk     188:        const char *errstr;
1.114     tedu      189:        ssize_t len, wlen;
1.61      deraadt   190: #ifndef SMALL
                    191:        char *sslpath = NULL, *sslhost = NULL;
1.123     guenther  192:        char *locbase, *full_host = NULL;
1.102     halex     193:        const char *scheme;
1.123     guenther  194:        int ishttpurl = 0, ishttpsurl = 0;
1.78      martynas  195: #endif /* !SMALL */
1.126     jsing     196:        struct ressl *ssl = NULL;
1.70      deraadt   197:        int status;
1.105     haesbaer  198:        int save_errno;
1.113     tedu      199:        const size_t buflen = 128 * 1024;
1.14      millert   200:
1.97      martynas  201:        direction = "received";
                    202:
1.62      ray       203:        newline = strdup(origline);
                    204:        if (newline == NULL)
1.14      millert   205:                errx(1, "Can't allocate memory to parse URL");
1.102     halex     206:        if (strncasecmp(newline, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
1.62      ray       207:                host = newline + sizeof(HTTP_URL) - 1;
1.102     halex     208: #ifndef SMALL
1.123     guenther  209:                ishttpurl = 1;
1.102     halex     210:                scheme = HTTP_URL;
                    211: #endif /* !SMALL */
                    212:        } else if (strncasecmp(newline, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.62      ray       213:                host = newline + sizeof(FTP_URL) - 1;
1.14      millert   214:                isftpurl = 1;
1.102     halex     215: #ifndef SMALL
                    216:                scheme = FTP_URL;
                    217: #endif /* !SMALL */
1.62      ray       218:        } else if (strncasecmp(newline, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
                    219:                host = newline + sizeof(FILE_URL) - 1;
1.22      deraadt   220:                isfileurl = 1;
1.61      deraadt   221: #ifndef SMALL
1.102     halex     222:                scheme = FILE_URL;
1.62      ray       223:        } else if (strncasecmp(newline, HTTPS_URL, sizeof(HTTPS_URL) - 1) == 0) {
                    224:                host = newline + sizeof(HTTPS_URL) - 1;
1.61      deraadt   225:                ishttpsurl = 1;
1.102     halex     226:                scheme = HTTPS_URL;
1.78      martynas  227: #endif /* !SMALL */
1.14      millert   228:        } else
1.62      ray       229:                errx(1, "url_get: Invalid URL '%s'", newline);
1.6       millert   230:
1.22      deraadt   231:        if (isfileurl) {
                    232:                path = host;
                    233:        } else {
1.93      martynas  234:                path = strchr(host, '/');               /* Find path */
1.22      deraadt   235:                if (EMPTYSTRING(path)) {
1.93      martynas  236:                        if (outfile) {                  /* No slash, but */
                    237:                                path=strchr(host,'\0'); /* we have outfile. */
                    238:                                goto noslash;
                    239:                        }
1.22      deraadt   240:                        if (isftpurl)
                    241:                                goto noftpautologin;
1.94      martynas  242:                        warnx("No `/' after host (use -o): %s", origline);
1.22      deraadt   243:                        goto cleanup_url_get;
                    244:                }
                    245:                *path++ = '\0';
1.93      martynas  246:                if (EMPTYSTRING(path) && !outfile) {
1.22      deraadt   247:                        if (isftpurl)
                    248:                                goto noftpautologin;
1.94      martynas  249:                        warnx("No filename after host (use -o): %s", origline);
1.22      deraadt   250:                        goto cleanup_url_get;
                    251:                }
1.14      millert   252:        }
1.1       millert   253:
1.93      martynas  254: noslash:
1.106     haesbaer  255:
                    256: #ifndef SMALL
                    257:        /*
                    258:         * Look for auth header in host, since now host does not
                    259:         * contain the path. Basic auth from RFC 2617, valid
                    260:         * characters for path are in RFC 3986 section 3.3.
                    261:         */
1.123     guenther  262:        if (proxyenv == NULL && (ishttpurl || ishttpsurl)) {
1.106     haesbaer  263:                if ((p = strchr(host, '@')) != NULL) {
1.123     guenther  264:                        *p = '\0';
                    265:                        credentials = recode_credentials(host);
1.106     haesbaer  266:                        host = p + 1;
                    267:                }
                    268:        }
                    269: #endif /* SMALL */
                    270:
1.17      millert   271:        if (outfile)
                    272:                savefile = outfile;
1.93      martynas  273:        else {
                    274:                if (path[strlen(path) - 1] == '/')      /* Consider no file */
                    275:                        savefile = NULL;                /* after dir invalid. */
                    276:                else
                    277:                        savefile = basename(path);
1.75      martynas  278:        }
                    279:
1.14      millert   280:        if (EMPTYSTRING(savefile)) {
                    281:                if (isftpurl)
                    282:                        goto noftpautologin;
1.94      martynas  283:                warnx("No filename after directory (use -o): %s", origline);
1.6       millert   284:                goto cleanup_url_get;
1.14      millert   285:        }
1.1       millert   286:
1.93      martynas  287: #ifndef SMALL
1.100     halex     288:        if (resume && pipeout) {
1.93      martynas  289:                warnx("can't append to stdout");
                    290:                goto cleanup_url_get;
                    291:        }
                    292: #endif /* !SMALL */
                    293:
1.59      uwe       294:        if (!isfileurl && proxyenv != NULL) {           /* use proxy */
1.61      deraadt   295: #ifndef SMALL
                    296:                if (ishttpsurl) {
                    297:                        sslpath = strdup(path);
                    298:                        sslhost = strdup(host);
                    299:                        if (! sslpath || ! sslhost)
                    300:                                errx(1, "Can't allocate memory for https path/host.");
                    301:                }
1.78      martynas  302: #endif /* !SMALL */
1.62      ray       303:                proxyurl = strdup(proxyenv);
                    304:                if (proxyurl == NULL)
1.14      millert   305:                        errx(1, "Can't allocate memory for proxy URL.");
1.62      ray       306:                if (strncasecmp(proxyurl, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
                    307:                        host = proxyurl + sizeof(HTTP_URL) - 1;
                    308:                else if (strncasecmp(proxyurl, FTP_URL, sizeof(FTP_URL) - 1) == 0)
                    309:                        host = proxyurl + sizeof(FTP_URL) - 1;
1.6       millert   310:                else {
1.14      millert   311:                        warnx("Malformed proxy URL: %s", proxyenv);
1.6       millert   312:                        goto cleanup_url_get;
                    313:                }
1.14      millert   314:                if (EMPTYSTRING(host)) {
                    315:                        warnx("Malformed proxy URL: %s", proxyenv);
1.6       millert   316:                        goto cleanup_url_get;
1.14      millert   317:                }
1.93      martynas  318:                if (*--path == '\0')
                    319:                        *path = '/';            /* add / back to real path */
1.1       millert   320:                path = strchr(host, '/');       /* remove trailing / on host */
1.42      deraadt   321:                if (!EMPTYSTRING(path))
1.73      drahn     322:                        *path++ = '\0';         /* i guess this ++ is useless */
                    323:
                    324:                path = strchr(host, '@');       /* look for credentials in proxy */
                    325:                if (!EMPTYSTRING(path)) {
1.81      espie     326:                        *path = '\0';
1.123     guenther  327:                        if (strchr(host, ':') == NULL) {
1.73      drahn     328:                                warnx("Malformed proxy URL: %s", proxyenv);
                    329:                                goto cleanup_url_get;
                    330:                        }
1.123     guenther  331:                        credentials = recode_credentials(host);
1.81      espie     332:                        *path = '@'; /* restore @ in proxyurl */
1.123     guenther  333:
1.73      drahn     334:                        /*
1.81      espie     335:                         * This removes the password from proxyurl,
1.73      drahn     336:                         * filling with stars
                    337:                         */
1.81      espie     338:                        for (host = 1 + strchr(proxyurl + 5, ':');  *host != '@';
1.73      drahn     339:                             host++)
                    340:                                *host = '*';
                    341:
1.81      espie     342:                        host = path + 1;
1.73      drahn     343:                }
1.123     guenther  344:
1.62      ray       345:                path = newline;
1.129   ! deraadt   346: #ifndef SMALL
1.128     jca       347:        } else if (ishttpsurl) {
                    348:                sslhost = strdup(host);
                    349:                if (sslhost == NULL)
                    350:                        errx(1, "Can't allocate memory for https path/host.");
1.129   ! deraadt   351: #endif /* !SMALL */
1.1       millert   352:        }
                    353:
1.22      deraadt   354:        if (isfileurl) {
                    355:                struct stat st;
                    356:
                    357:                s = open(path, O_RDONLY);
                    358:                if (s == -1) {
                    359:                        warn("Can't open file %s", path);
                    360:                        goto cleanup_url_get;
                    361:                }
                    362:
                    363:                if (fstat(s, &st) == -1)
                    364:                        filesize = -1;
                    365:                else
                    366:                        filesize = st.st_size;
                    367:
1.83      martynas  368:                /* Open the output file.  */
1.100     halex     369:                if (!pipeout) {
1.75      martynas  370: #ifndef SMALL
                    371:                        if (resume)
1.82      deraadt   372:                                out = open(savefile, O_CREAT | O_WRONLY |
                    373:                                        O_APPEND, 0666);
                    374:
1.75      martynas  375:                        else
1.78      martynas  376: #endif /* !SMALL */
1.75      martynas  377:                                out = open(savefile, O_CREAT | O_WRONLY |
                    378:                                        O_TRUNC, 0666);
1.22      deraadt   379:                        if (out < 0) {
                    380:                                warn("Can't open %s", savefile);
                    381:                                goto cleanup_url_get;
                    382:                        }
                    383:                } else
                    384:                        out = fileno(stdout);
                    385:
1.75      martynas  386: #ifndef SMALL
                    387:                if (resume) {
                    388:                        if (fstat(out, &st) == -1) {
                    389:                                warn("Can't fstat %s", savefile);
                    390:                                goto cleanup_url_get;
                    391:                        }
                    392:                        if (lseek(s, st.st_size, SEEK_SET) == -1) {
                    393:                                warn("Can't lseek %s", path);
                    394:                                goto cleanup_url_get;
                    395:                        }
                    396:                        restart_point = st.st_size;
                    397:                }
1.78      martynas  398: #endif /* !SMALL */
1.75      martynas  399:
1.22      deraadt   400:                /* Trap signals */
                    401:                oldintr = NULL;
1.97      martynas  402:                oldinti = NULL;
1.22      deraadt   403:                if (setjmp(httpabort)) {
                    404:                        if (oldintr)
                    405:                                (void)signal(SIGINT, oldintr);
1.97      martynas  406:                        if (oldinti)
                    407:                                (void)signal(SIGINFO, oldinti);
1.22      deraadt   408:                        goto cleanup_url_get;
                    409:                }
                    410:                oldintr = signal(SIGINT, abortfile);
1.42      deraadt   411:
1.22      deraadt   412:                bytes = 0;
                    413:                hashbytes = mark;
1.84      martynas  414:                progressmeter(-1, path);
1.40      fgsch     415:
1.113     tedu      416:                if ((buf = malloc(buflen)) == NULL)
1.47      deraadt   417:                        errx(1, "Can't allocate memory for transfer buffer");
1.42      deraadt   418:
1.22      deraadt   419:                /* Finally, suck down the file. */
                    420:                i = 0;
1.97      martynas  421:                oldinti = signal(SIGINFO, psummary);
1.113     tedu      422:                while ((len = read(s, buf, buflen)) > 0) {
1.22      deraadt   423:                        bytes += len;
                    424:                        for (cp = buf; len > 0; len -= i, cp += i) {
                    425:                                if ((i = write(out, cp, len)) == -1) {
                    426:                                        warn("Writing %s", savefile);
1.97      martynas  427:                                        signal(SIGINFO, oldinti);
1.22      deraadt   428:                                        goto cleanup_url_get;
                    429:                                }
                    430:                                else if (i == 0)
                    431:                                        break;
                    432:                        }
                    433:                        if (hash && !progress) {
                    434:                                while (bytes >= hashbytes) {
                    435:                                        (void)putc('#', ttyout);
                    436:                                        hashbytes += mark;
                    437:                                }
                    438:                                (void)fflush(ttyout);
                    439:                        }
                    440:                }
1.97      martynas  441:                signal(SIGINFO, oldinti);
1.22      deraadt   442:                if (hash && !progress && bytes > 0) {
                    443:                        if (bytes < mark)
                    444:                                (void)putc('#', ttyout);
                    445:                        (void)putc('\n', ttyout);
                    446:                        (void)fflush(ttyout);
                    447:                }
                    448:                if (len != 0) {
                    449:                        warn("Reading from file");
                    450:                        goto cleanup_url_get;
                    451:                }
1.84      martynas  452:                progressmeter(1, NULL);
1.22      deraadt   453:                if (verbose)
1.97      martynas  454:                        ptransfer(0);
1.22      deraadt   455:                (void)signal(SIGINT, oldintr);
1.42      deraadt   456:
1.40      fgsch     457:                rval = 0;
                    458:                goto cleanup_url_get;
1.22      deraadt   459:        }
                    460:
1.28      itojun    461:        if (*host == '[' && (hosttail = strrchr(host, ']')) != NULL &&
                    462:            (hosttail[1] == '\0' || hosttail[1] == ':')) {
                    463:                host++;
                    464:                *hosttail++ = '\0';
1.102     halex     465: #ifndef SMALL
                    466:                if (asprintf(&full_host, "[%s]", host) == -1)
                    467:                        errx(1, "Cannot allocate memory for hostname");
                    468: #endif /* !SMALL */
1.28      itojun    469:        } else
                    470:                hosttail = host;
                    471:
                    472:        portnum = strrchr(hosttail, ':');               /* find portnum */
1.1       millert   473:        if (portnum != NULL)
                    474:                *portnum++ = '\0';
                    475:
1.80      martynas  476: #ifndef SMALL
1.102     halex     477:        if (full_host == NULL)
                    478:                if ((full_host = strdup(host)) == NULL)
                    479:                        errx(1, "Cannot allocate memory for hostname");
1.1       millert   480:        if (debug)
1.106     haesbaer  481:                fprintf(ttyout, "host %s, port %s, path %s, "
                    482:                    "save as %s, auth %s.\n",
1.123     guenther  483:                    host, portnum, path, savefile, credentials);
1.80      martynas  484: #endif /* !SMALL */
1.1       millert   485:
1.25      itojun    486:        memset(&hints, 0, sizeof(hints));
1.39      deraadt   487:        hints.ai_family = family;
1.25      itojun    488:        hints.ai_socktype = SOCK_STREAM;
1.61      deraadt   489: #ifndef SMALL
                    490:        port = portnum ? portnum : (ishttpsurl ? httpsport : httpport);
1.78      martynas  491: #else /* !SMALL */
1.25      itojun    492:        port = portnum ? portnum : httpport;
1.78      martynas  493: #endif /* !SMALL */
1.25      itojun    494:        error = getaddrinfo(host, port, &hints, &res0);
1.61      deraadt   495:        /*
                    496:         * If the services file is corrupt/missing, fall back
                    497:         * on our hard-coded defines.
                    498:         */
1.30      deraadt   499:        if (error == EAI_SERVICE && port == httpport) {
                    500:                snprintf(pbuf, sizeof(pbuf), "%d", HTTP_PORT);
                    501:                error = getaddrinfo(host, pbuf, &hints, &res0);
1.61      deraadt   502: #ifndef SMALL
                    503:        } else if (error == EAI_SERVICE && port == httpsport) {
                    504:                snprintf(pbuf, sizeof(pbuf), "%d", HTTPS_PORT);
                    505:                error = getaddrinfo(host, pbuf, &hints, &res0);
1.78      martynas  506: #endif /* !SMALL */
1.30      deraadt   507:        }
1.25      itojun    508:        if (error) {
1.122     guenther  509:                warnx("%s: %s", host, gai_strerror(error));
1.25      itojun    510:                goto cleanup_url_get;
1.1       millert   511:        }
                    512:
1.105     haesbaer  513: #ifndef SMALL
                    514:        if (srcaddr) {
                    515:                hints.ai_flags |= AI_NUMERICHOST;
                    516:                error = getaddrinfo(srcaddr, NULL, &hints, &ares);
                    517:                if (error) {
1.122     guenther  518:                        warnx("%s: %s", srcaddr, gai_strerror(error));
1.105     haesbaer  519:                        goto cleanup_url_get;
                    520:                }
                    521:        }
                    522: #endif /* !SMALL */
                    523:
1.25      itojun    524:        s = -1;
                    525:        for (res = res0; res; res = res->ai_next) {
1.44      itojun    526:                if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
                    527:                    sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
                    528:                        strlcpy(hbuf, "(unknown)", sizeof(hbuf));
1.41      deraadt   529:                if (verbose)
1.44      itojun    530:                        fprintf(ttyout, "Trying %s...\n", hbuf);
1.14      millert   531:
1.25      itojun    532:                s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
                    533:                if (s == -1) {
                    534:                        cause = "socket";
                    535:                        continue;
1.1       millert   536:                }
                    537:
1.105     haesbaer  538: #ifndef SMALL
                    539:                if (srcaddr) {
                    540:                        if (ares->ai_family != res->ai_family) {
                    541:                                close(s);
                    542:                                s = -1;
                    543:                                errno = EINVAL;
                    544:                                cause = "bind";
                    545:                                continue;
                    546:                        }
                    547:                        if (bind(s, ares->ai_addr, ares->ai_addrlen) < 0) {
                    548:                                save_errno = errno;
                    549:                                close(s);
                    550:                                errno = save_errno;
                    551:                                s = -1;
                    552:                                cause = "bind";
                    553:                                continue;
                    554:                        }
                    555:                }
                    556: #endif /* !SMALL */
                    557:
1.25      itojun    558: again:
                    559:                if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
                    560:                        if (errno == EINTR)
                    561:                                goto again;
1.57      otto      562:                        save_errno = errno;
1.25      itojun    563:                        close(s);
1.57      otto      564:                        errno = save_errno;
1.25      itojun    565:                        s = -1;
                    566:                        cause = "connect";
1.19      deraadt   567:                        continue;
                    568:                }
1.25      itojun    569:
1.29      itojun    570:                /* get port in numeric */
                    571:                if (getnameinfo(res->ai_addr, res->ai_addrlen, NULL, 0,
                    572:                    pbuf, sizeof(pbuf), NI_NUMERICSERV) == 0)
                    573:                        port = pbuf;
                    574:                else
                    575:                        port = NULL;
                    576:
1.61      deraadt   577: #ifndef SMALL
                    578:                if (proxyenv && sslhost)
1.123     guenther  579:                        proxy_connect(s, sslhost, credentials);
1.78      martynas  580: #endif /* !SMALL */
1.25      itojun    581:                break;
                    582:        }
                    583:        freeaddrinfo(res0);
1.105     haesbaer  584: #ifndef SMALL
                    585:        if (srcaddr)
                    586:                freeaddrinfo(ares);
                    587: #endif /* !SMALL */
1.25      itojun    588:        if (s < 0) {
1.33      millert   589:                warn("%s", cause);
1.6       millert   590:                goto cleanup_url_get;
1.1       millert   591:        }
                    592:
1.61      deraadt   593: #ifndef SMALL
                    594:        if (ishttpsurl) {
                    595:                if (proxyenv && sslpath) {
                    596:                        ishttpsurl = 0;
1.62      ray       597:                        proxyurl = NULL;
1.61      deraadt   598:                        path = sslpath;
                    599:                }
1.126     jsing     600:                if (ressl_init() != 0) {
                    601:                        fprintf(ttyout, "SSL initialisation failed\n");
1.116     jca       602:                        goto cleanup_url_get;
                    603:                }
1.126     jsing     604:                if ((ssl = ressl_client()) == NULL) {
                    605:                        fprintf(ttyout, "failed to create SSL client\n");
1.61      deraadt   606:                        goto cleanup_url_get;
                    607:                }
1.126     jsing     608:                if (ressl_configure(ssl, ressl_config) != 0) {
                    609:                        fprintf(ttyout, "SSL configuration failure: %s\n",
                    610:                            ressl_error(ssl));
1.61      deraadt   611:                        goto cleanup_url_get;
1.117     jca       612:                }
1.128     jca       613:                if (ressl_connect_socket(ssl, s, sslhost) != 0) {
1.126     jsing     614:                        fprintf(ttyout, "SSL failure: %s\n", ressl_error(ssl));
1.72      ray       615:                        goto cleanup_url_get;
1.118     jca       616:                }
1.61      deraadt   617:        } else {
                    618:                fin = fdopen(s, "r+");
                    619:        }
1.78      martynas  620: #else /* !SMALL */
1.40      fgsch     621:        fin = fdopen(s, "r+");
1.78      martynas  622: #endif /* !SMALL */
1.40      fgsch     623:
1.55      fgsch     624:        if (verbose)
                    625:                fprintf(ttyout, "Requesting %s", origline);
1.95      martynas  626:
1.1       millert   627:        /*
1.40      fgsch     628:         * Construct and send the request. Proxy requests don't want leading /.
1.1       millert   629:         */
1.74      pyr       630: #ifndef SMALL
                    631:        cookie_get(host, path, ishttpsurl, &buf);
1.78      martynas  632: #endif /* !SMALL */
1.95      martynas  633:
                    634:        epath = url_encode(path);
1.62      ray       635:        if (proxyurl) {
1.55      fgsch     636:                if (verbose)
1.81      espie     637:                        fprintf(ttyout, " (via %s)\n", proxyurl);
1.32      itojun    638:                /*
                    639:                 * Host: directive must use the destination host address for
                    640:                 * the original URI (path).  We do not attach it at this moment.
                    641:                 */
1.123     guenther  642:                if (credentials)
1.73      drahn     643:                        ftp_printf(fin, ssl, "GET %s HTTP/1.0\r\n"
1.74      pyr       644:                            "Proxy-Authorization: Basic %s%s\r\n%s\r\n\r\n",
1.123     guenther  645:                            epath, credentials, buf ? buf : "",
1.124     lteo      646:                            httpuseragent);
1.73      drahn     647:                else
1.74      pyr       648:                        ftp_printf(fin, ssl, "GET %s HTTP/1.0\r\n%s%s\r\n\r\n",
1.124     lteo      649:                            epath, buf ? buf : "", httpuseragent);
1.73      drahn     650:
1.28      itojun    651:        } else {
1.90      martynas  652: #ifndef SMALL
                    653:                if (resume) {
                    654:                        struct stat stbuf;
                    655:
                    656:                        if (stat(savefile, &stbuf) == 0)
                    657:                                restart_point = stbuf.st_size;
                    658:                        else
                    659:                                restart_point = 0;
                    660:                }
1.123     guenther  661:                if (credentials) {
1.106     haesbaer  662:                        ftp_printf(fin, ssl,
                    663:                            "GET /%s %s\r\nAuthorization: Basic %s\r\nHost: ",
                    664:                            epath, restart_point ?
                    665:                            "HTTP/1.1\r\nConnection: close" : "HTTP/1.0",
1.123     guenther  666:                            credentials);
                    667:                        free(credentials);
                    668:                        credentials = NULL;
1.106     haesbaer  669:                } else
                    670: #endif /* SMALL */
1.107     haesbaer  671:                        ftp_printf(fin, ssl, "GET /%s %s\r\nHost: ", epath,
                    672: #ifndef SMALL
                    673:                            restart_point ? "HTTP/1.1\r\nConnection: close" :
                    674: #endif /* !SMALL */
                    675:                            "HTTP/1.0");
1.32      itojun    676:                if (strchr(host, ':')) {
1.55      fgsch     677:                        /*
                    678:                         * strip off scoped address portion, since it's
                    679:                         * local to node
                    680:                         */
1.32      itojun    681:                        h = strdup(host);
                    682:                        if (h == NULL)
                    683:                                errx(1, "Can't allocate memory.");
                    684:                        if ((p = strchr(h, '%')) != NULL)
                    685:                                *p = '\0';
1.61      deraadt   686:                        ftp_printf(fin, ssl, "[%s]", h);
1.32      itojun    687:                        free(h);
1.55      fgsch     688:                } else
1.61      deraadt   689:                        ftp_printf(fin, ssl, "%s", host);
1.55      fgsch     690:
                    691:                /*
                    692:                 * Send port number only if it's specified and does not equal
                    693:                 * 80. Some broken HTTP servers get confused if you explicitly
                    694:                 * send them the port number.
                    695:                 */
1.61      deraadt   696: #ifndef SMALL
                    697:                if (port && strcmp(port, (ishttpsurl ? "443" : "80")) != 0)
                    698:                        ftp_printf(fin, ssl, ":%s", port);
1.90      martynas  699:                if (restart_point)
1.75      martynas  700:                        ftp_printf(fin, ssl, "\r\nRange: bytes=%lld-",
                    701:                                (long long)restart_point);
1.78      martynas  702: #else /* !SMALL */
1.55      fgsch     703:                if (port && strcmp(port, "80") != 0)
1.61      deraadt   704:                        ftp_printf(fin, ssl, ":%s", port);
1.78      martynas  705: #endif /* !SMALL */
1.74      pyr       706:                ftp_printf(fin, ssl, "\r\n%s%s\r\n\r\n",
1.124     lteo      707:                    buf ? buf : "", httpuseragent);
1.55      fgsch     708:                if (verbose)
                    709:                        fprintf(ttyout, "\n");
1.28      itojun    710:        }
1.95      martynas  711:        free(epath);
1.74      pyr       712:
                    713: #ifndef SMALL
                    714:        free(buf);
1.78      martynas  715: #endif /* !SMALL */
1.74      pyr       716:        buf = NULL;
                    717:
1.61      deraadt   718:        if (fin != NULL && fflush(fin) == EOF) {
1.14      millert   719:                warn("Writing HTTP request");
1.6       millert   720:                goto cleanup_url_get;
1.1       millert   721:        }
1.61      deraadt   722:        if ((buf = ftp_readline(fin, ssl, &len)) == NULL) {
1.40      fgsch     723:                warn("Receiving HTTP reply");
                    724:                goto cleanup_url_get;
1.1       millert   725:        }
1.40      fgsch     726:
                    727:        while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
                    728:                buf[--len] = '\0';
1.80      martynas  729: #ifndef SMALL
1.40      fgsch     730:        if (debug)
                    731:                fprintf(ttyout, "received '%s'\n", buf);
1.80      martynas  732: #endif /* !SMALL */
1.40      fgsch     733:
1.1       millert   734:        cp = strchr(buf, ' ');
                    735:        if (cp == NULL)
                    736:                goto improper;
                    737:        else
                    738:                cp++;
1.69      jsg       739:
                    740:        strlcpy(ststr, cp, sizeof(ststr));
1.75      martynas  741:        status = strtonum(ststr, 200, 416, &errstr);
1.69      jsg       742:        if (errstr) {
                    743:                warnx("Error retrieving file: %s", cp);
                    744:                goto cleanup_url_get;
                    745:        }
                    746:
                    747:        switch (status) {
                    748:        case 200:       /* OK */
1.75      martynas  749: #ifndef SMALL
1.109     sthen     750:                /*
1.98      phessler  751:                 * When we request a partial file, and we receive an HTTP 200
                    752:                 * it is a good indication that the server doesn't support
                    753:                 * range requests, and is about to send us the entire file.
                    754:                 * If the restart_point == 0, then we are not actually
                    755:                 * requesting a partial file, and an HTTP 200 is appropriate.
                    756:                 */
                    757:                if (resume && restart_point != 0) {
                    758:                        warnx("Server does not support resume.");
                    759:                        restart_point = resume = 0;
                    760:                }
1.101     halex     761:                /* FALLTHROUGH */
1.75      martynas  762:        case 206:       /* Partial Content */
1.91      halex     763: #endif /* !SMALL */
1.69      jsg       764:                break;
                    765:        case 301:       /* Moved Permanently */
                    766:        case 302:       /* Found */
                    767:        case 303:       /* See Other */
                    768:        case 307:       /* Temporary Redirect */
1.40      fgsch     769:                isredirect++;
1.54      fgsch     770:                if (redirect_loop++ > 10) {
                    771:                        warnx("Too many redirections requested");
                    772:                        goto cleanup_url_get;
                    773:                }
1.69      jsg       774:                break;
1.75      martynas  775: #ifndef SMALL
                    776:        case 416:       /* Requested Range Not Satisfiable */
                    777:                warnx("File is already fully retrieved.");
                    778:                goto cleanup_url_get;
1.78      martynas  779: #endif /* !SMALL */
1.69      jsg       780:        default:
1.1       millert   781:                warnx("Error retrieving file: %s", cp);
1.6       millert   782:                goto cleanup_url_get;
1.1       millert   783:        }
                    784:
                    785:        /*
                    786:         * Read the rest of the header.
                    787:         */
1.40      fgsch     788:        free(buf);
                    789:        filesize = -1;
                    790:
1.62      ray       791:        for (;;) {
1.61      deraadt   792:                if ((buf = ftp_readline(fin, ssl, &len)) == NULL) {
1.40      fgsch     793:                        warn("Receiving HTTP reply");
                    794:                        goto cleanup_url_get;
                    795:                }
1.61      deraadt   796:
1.40      fgsch     797:                while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
                    798:                        buf[--len] = '\0';
                    799:                if (len == 0)
1.1       millert   800:                        break;
1.80      martynas  801: #ifndef SMALL
1.40      fgsch     802:                if (debug)
                    803:                        fprintf(ttyout, "received '%s'\n", buf);
1.80      martynas  804: #endif /* !SMALL */
1.1       millert   805:
1.40      fgsch     806:                /* Look for some headers */
                    807:                cp = buf;
1.1       millert   808: #define CONTENTLEN "Content-Length: "
1.40      fgsch     809:                if (strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0) {
1.104     sthen     810:                        size_t s;
1.40      fgsch     811:                        cp += sizeof(CONTENTLEN) - 1;
1.111     deraadt   812:                        if ((s = strcspn(cp, " \t")))
1.104     sthen     813:                                *(cp+s) = 0;
1.58      grunk     814:                        filesize = strtonum(cp, 0, LLONG_MAX, &errstr);
                    815:                        if (errstr != NULL)
1.40      fgsch     816:                                goto improper;
1.75      martynas  817: #ifndef SMALL
1.90      martynas  818:                        if (restart_point)
1.75      martynas  819:                                filesize += restart_point;
1.78      martynas  820: #endif /* !SMALL */
1.40      fgsch     821: #define LOCATION "Location: "
                    822:                } else if (isredirect &&
                    823:                    strncasecmp(cp, LOCATION, sizeof(LOCATION) - 1) == 0) {
                    824:                        cp += sizeof(LOCATION) - 1;
1.102     halex     825:                        if (strstr(cp, "://") == NULL) {
                    826: #ifdef SMALL
                    827:                                errx(1, "Relative redirect not supported");
                    828: #else /* SMALL */
                    829:                                if (*cp == '/') {
                    830:                                        locbase = NULL;
                    831:                                        cp++;
                    832:                                } else {
                    833:                                        locbase = strdup(path);
                    834:                                        if (locbase == NULL)
                    835:                                                errx(1, "Can't allocate memory"
                    836:                                                    " for location base");
                    837:                                        loctail = strchr(locbase, '#');
                    838:                                        if (loctail != NULL)
                    839:                                                *loctail = '\0';
                    840:                                        loctail = strchr(locbase, '?');
                    841:                                        if (loctail != NULL)
                    842:                                                *loctail = '\0';
                    843:                                        loctail = strrchr(locbase, '/');
                    844:                                        if (loctail == NULL) {
                    845:                                                free(locbase);
                    846:                                                locbase = NULL;
                    847:                                        } else
                    848:                                                loctail[1] = '\0';
                    849:                                }
                    850:                                /* Contruct URL from relative redirect */
                    851:                                if (asprintf(&redirurl, "%s%s%s%s/%s%s",
                    852:                                    scheme, full_host,
                    853:                                    portnum ? ":" : "",
                    854:                                    portnum ? portnum : "",
                    855:                                    locbase ? locbase : "",
                    856:                                    cp) == -1)
                    857:                                        errx(1, "Cannot build "
                    858:                                            "redirect URL");
                    859:                                free(locbase);
                    860: #endif /* SMALL */
                    861:                        } else if ((redirurl = strdup(cp)) == NULL)
                    862:                                errx(1, "Cannot allocate memory for URL");
                    863:                        loctail = strchr(redirurl, '#');
                    864:                        if (loctail != NULL)
                    865:                                *loctail = '\0';
1.40      fgsch     866:                        if (verbose)
1.102     halex     867:                                fprintf(ttyout, "Redirected to %s\n", redirurl);
1.40      fgsch     868:                        if (fin != NULL)
                    869:                                fclose(fin);
                    870:                        else if (s != -1)
                    871:                                close(s);
1.102     halex     872:                        rval = url_get(redirurl, proxyenv, savefile);
                    873:                        free(redirurl);
                    874:                        goto cleanup_url_get;
1.40      fgsch     875:                }
1.108     tobias    876:                free(buf);
1.1       millert   877:        }
                    878:
1.17      millert   879:        /* Open the output file.  */
1.100     halex     880:        if (!pipeout) {
1.75      martynas  881: #ifndef SMALL
                    882:                if (resume)
1.83      martynas  883:                        out = open(savefile, O_CREAT | O_WRONLY | O_APPEND,
                    884:                                0666);
1.75      martynas  885:                else
1.78      martynas  886: #endif /* !SMALL */
1.75      martynas  887:                        out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC,
                    888:                                0666);
1.10      deraadt   889:                if (out < 0) {
                    890:                        warn("Can't open %s", savefile);
                    891:                        goto cleanup_url_get;
                    892:                }
                    893:        } else
1.17      millert   894:                out = fileno(stdout);
1.1       millert   895:
                    896:        /* Trap signals */
                    897:        oldintr = NULL;
1.97      martynas  898:        oldinti = NULL;
1.1       millert   899:        if (setjmp(httpabort)) {
                    900:                if (oldintr)
1.2       millert   901:                        (void)signal(SIGINT, oldintr);
1.97      martynas  902:                if (oldinti)
                    903:                        (void)signal(SIGINFO, oldinti);
1.6       millert   904:                goto cleanup_url_get;
1.1       millert   905:        }
                    906:        oldintr = signal(SIGINT, aborthttp);
                    907:
                    908:        bytes = 0;
                    909:        hashbytes = mark;
1.84      martynas  910:        progressmeter(-1, path);
1.43      millert   911:
                    912:        free(buf);
1.1       millert   913:
                    914:        /* Finally, suck down the file. */
1.113     tedu      915:        if ((buf = malloc(buflen)) == NULL)
1.47      deraadt   916:                errx(1, "Can't allocate memory for transfer buffer");
1.1       millert   917:        i = 0;
1.61      deraadt   918:        len = 1;
1.97      martynas  919:        oldinti = signal(SIGINFO, psummary);
1.61      deraadt   920:        while (len > 0) {
1.113     tedu      921:                len = ftp_read(fin, ssl, buf, buflen);
1.1       millert   922:                bytes += len;
1.61      deraadt   923:                for (cp = buf, wlen = len; wlen > 0; wlen -= i, cp += i) {
                    924:                        if ((i = write(out, cp, wlen)) == -1) {
1.1       millert   925:                                warn("Writing %s", savefile);
1.97      martynas  926:                                signal(SIGINFO, oldinti);
1.6       millert   927:                                goto cleanup_url_get;
1.1       millert   928:                        }
                    929:                        else if (i == 0)
                    930:                                break;
                    931:                }
                    932:                if (hash && !progress) {
                    933:                        while (bytes >= hashbytes) {
1.10      deraadt   934:                                (void)putc('#', ttyout);
1.1       millert   935:                                hashbytes += mark;
                    936:                        }
1.10      deraadt   937:                        (void)fflush(ttyout);
1.1       millert   938:                }
                    939:        }
1.97      martynas  940:        signal(SIGINFO, oldinti);
1.1       millert   941:        if (hash && !progress && bytes > 0) {
                    942:                if (bytes < mark)
1.10      deraadt   943:                        (void)putc('#', ttyout);
                    944:                (void)putc('\n', ttyout);
                    945:                (void)fflush(ttyout);
1.1       millert   946:        }
                    947:        if (len != 0) {
                    948:                warn("Reading from socket");
1.6       millert   949:                goto cleanup_url_get;
1.1       millert   950:        }
1.84      martynas  951:        progressmeter(1, NULL);
1.75      martynas  952:        if (
                    953: #ifndef SMALL
                    954:                !resume &&
1.78      martynas  955: #endif /* !SMALL */
1.75      martynas  956:                filesize != -1 && len == 0 && bytes != filesize) {
1.24      deraadt   957:                if (verbose)
                    958:                        fputs("Read short file.\n", ttyout);
                    959:                goto cleanup_url_get;
                    960:        }
                    961:
1.1       millert   962:        if (verbose)
1.97      martynas  963:                ptransfer(0);
1.2       millert   964:        (void)signal(SIGINT, oldintr);
1.1       millert   965:
1.40      fgsch     966:        rval = 0;
                    967:        goto cleanup_url_get;
1.1       millert   968:
1.14      millert   969: noftpautologin:
                    970:        warnx(
                    971:            "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
                    972:        goto cleanup_url_get;
                    973:
1.1       millert   974: improper:
1.8       millert   975:        warnx("Improper response from %s", host);
1.14      millert   976:
1.6       millert   977: cleanup_url_get:
1.61      deraadt   978: #ifndef SMALL
1.126     jsing     979:        if (ssl != NULL) {
                    980:                ressl_close(ssl);
                    981:                ressl_free(ssl);
1.61      deraadt   982:        }
1.102     halex     983:        free(full_host);
1.128     jca       984:        free(sslhost);
1.78      martynas  985: #endif /* !SMALL */
1.40      fgsch     986:        if (fin != NULL)
                    987:                fclose(fin);
                    988:        else if (s != -1)
1.1       millert   989:                close(s);
1.67      steven    990:        free(buf);
                    991:        free(proxyurl);
1.62      ray       992:        free(newline);
1.123     guenther  993:        free(credentials);
1.40      fgsch     994:        return (rval);
1.1       millert   995: }
                    996:
                    997: /*
                    998:  * Abort a http retrieval
                    999:  */
1.51      deraadt  1000: /* ARGSUSED */
1.1       millert  1001: void
1.51      deraadt  1002: aborthttp(int signo)
1.1       millert  1003: {
                   1004:
                   1005:        alarmtimer(0);
1.10      deraadt  1006:        fputs("\nhttp fetch aborted.\n", ttyout);
                   1007:        (void)fflush(ttyout);
1.1       millert  1008:        longjmp(httpabort, 1);
                   1009: }
                   1010:
                   1011: /*
1.22      deraadt  1012:  * Abort a http retrieval
                   1013:  */
1.51      deraadt  1014: /* ARGSUSED */
1.22      deraadt  1015: void
1.51      deraadt  1016: abortfile(int signo)
1.22      deraadt  1017: {
                   1018:
                   1019:        alarmtimer(0);
                   1020:        fputs("\nfile fetch aborted.\n", ttyout);
                   1021:        (void)fflush(ttyout);
                   1022:        longjmp(httpabort, 1);
                   1023: }
                   1024:
                   1025: /*
1.1       millert  1026:  * Retrieve multiple files from the command line, transferring
                   1027:  * files of the form "host:path", "ftp://host/path" using the
                   1028:  * ftp protocol, and files of the form "http://host/path" using
                   1029:  * the http protocol.
1.2       millert  1030:  * If path has a trailing "/", then return (-1);
1.1       millert  1031:  * the path will be cd-ed into and the connection remains open,
                   1032:  * and the function will return -1 (to indicate the connection
                   1033:  * is alive).
                   1034:  * If an error occurs the return value will be the offset+1 in
                   1035:  * argv[] of the file that caused a problem (i.e, argv[x]
                   1036:  * returns x+1)
                   1037:  * Otherwise, 0 is returned if all files retrieved successfully.
                   1038:  */
                   1039: int
1.50      deraadt  1040: auto_fetch(int argc, char *argv[], char *outfile)
1.1       millert  1041: {
                   1042:        char *xargv[5];
1.62      ray      1043:        char *cp, *url, *host, *dir, *file, *portnum;
                   1044:        char *username, *pass, *pathstart;
1.6       millert  1045:        char *ftpproxy, *httpproxy;
1.14      millert  1046:        int rval, xargc;
                   1047:        volatile int argpos;
1.49      krw      1048:        int dirhasglob, filehasglob, oautologin;
1.14      millert  1049:        char rempath[MAXPATHLEN];
1.1       millert  1050:
                   1051:        argpos = 0;
                   1052:
                   1053:        if (setjmp(toplevel)) {
                   1054:                if (connected)
                   1055:                        disconnect(0, NULL);
1.2       millert  1056:                return (argpos + 1);
1.1       millert  1057:        }
1.3       millert  1058:        (void)signal(SIGINT, (sig_t)intr);
                   1059:        (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1       millert  1060:
1.45      millert  1061:        if ((ftpproxy = getenv(FTP_PROXY)) != NULL && *ftpproxy == '\0')
                   1062:                ftpproxy = NULL;
                   1063:        if ((httpproxy = getenv(HTTP_PROXY)) != NULL && *httpproxy == '\0')
                   1064:                httpproxy = NULL;
1.6       millert  1065:
1.1       millert  1066:        /*
                   1067:         * Loop through as long as there's files to fetch.
                   1068:         */
1.123     guenther 1069:        username = pass = NULL;
1.62      ray      1070:        for (rval = 0; (rval == 0) && (argpos < argc); free(url), argpos++) {
1.1       millert  1071:                if (strchr(argv[argpos], ':') == NULL)
                   1072:                        break;
1.123     guenther 1073:
                   1074:                free(username);
                   1075:                free(pass);
1.62      ray      1076:                host = dir = file = portnum = username = pass = NULL;
1.1       millert  1077:
                   1078:                /*
                   1079:                 * We muck with the string, so we make a copy.
                   1080:                 */
1.62      ray      1081:                url = strdup(argv[argpos]);
                   1082:                if (url == NULL)
1.1       millert  1083:                        errx(1, "Can't allocate memory for auto-fetch.");
                   1084:
                   1085:                /*
                   1086:                 * Try HTTP URL-style arguments first.
                   1087:                 */
1.62      ray      1088:                if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
1.61      deraadt  1089: #ifndef SMALL
                   1090:                    /* even if we compiled without SSL, url_get will check */
1.62      ray      1091:                    strncasecmp(url, HTTPS_URL, sizeof(HTTPS_URL) -1) == 0 ||
1.78      martynas 1092: #endif /* !SMALL */
1.62      ray      1093:                    strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
1.54      fgsch    1094:                        redirect_loop = 0;
1.62      ray      1095:                        if (url_get(url, httpproxy, outfile) == -1)
1.1       millert  1096:                                rval = argpos + 1;
                   1097:                        continue;
                   1098:                }
                   1099:
                   1100:                /*
1.6       millert  1101:                 * Try FTP URL-style arguments next. If ftpproxy is
                   1102:                 * set, use url_get() instead of standard ftp.
                   1103:                 * Finally, try host:file.
1.1       millert  1104:                 */
1.62      ray      1105:                host = url;
                   1106:                if (strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.37      heko     1107:                        char *passend, *passagain, *userend;
1.31      itojun   1108:
1.6       millert  1109:                        if (ftpproxy) {
1.62      ray      1110:                                if (url_get(url, ftpproxy, outfile) == -1)
1.6       millert  1111:                                        rval = argpos + 1;
                   1112:                                continue;
                   1113:                        }
1.1       millert  1114:                        host += sizeof(FTP_URL) - 1;
1.8       millert  1115:                        dir = strchr(host, '/');
1.1       millert  1116:
1.8       millert  1117:                        /* Look for [user:pass@]host[:port] */
1.31      itojun   1118:
                   1119:                        /* check if we have "user:pass@" */
1.37      heko     1120:                        userend = strchr(host, ':');
1.31      itojun   1121:                        passend = strchr(host, '@');
                   1122:                        if (passend && userend && userend < passend &&
                   1123:                            (!dir || passend < dir)) {
1.62      ray      1124:                                username = host;
1.31      itojun   1125:                                pass = userend + 1;
                   1126:                                host = passend + 1;
                   1127:                                *userend = *passend = '\0';
1.37      heko     1128:                                passagain = strchr(host, '@');
1.42      deraadt  1129:                                if (strchr(pass, '@') != NULL ||
1.37      heko     1130:                                    (passagain != NULL && passagain < dir)) {
                   1131:                                        warnx(at_encoding_warning);
1.123     guenther 1132:                                        username = pass = NULL;
1.37      heko     1133:                                        goto bad_ftp_url;
1.42      deraadt  1134:                                }
1.31      itojun   1135:
1.77      martynas 1136:                                if (EMPTYSTRING(username)) {
1.11      millert  1137: bad_ftp_url:
1.31      itojun   1138:                                        warnx("Invalid URL: %s", argv[argpos]);
                   1139:                                        rval = argpos + 1;
1.123     guenther 1140:                                        username = pass = NULL;
1.31      itojun   1141:                                        continue;
                   1142:                                }
1.62      ray      1143:                                username = urldecode(username);
1.37      heko     1144:                                pass = urldecode(pass);
1.8       millert  1145:                        }
1.31      itojun   1146:
                   1147: #ifdef INET6
                   1148:                        /* check [host]:port, or [host] */
                   1149:                        if (host[0] == '[') {
                   1150:                                cp = strchr(host, ']');
                   1151:                                if (cp && (!dir || cp < dir)) {
                   1152:                                        if (cp + 1 == dir || cp[1] == ':') {
                   1153:                                                host++;
                   1154:                                                *cp++ = '\0';
                   1155:                                        } else
                   1156:                                                cp = NULL;
                   1157:                                } else
                   1158:                                        cp = host;
1.25      itojun   1159:                        } else
                   1160:                                cp = host;
1.31      itojun   1161: #else
                   1162:                        cp = host;
1.25      itojun   1163: #endif
1.31      itojun   1164:
                   1165:                        /* split off host[:port] if there is */
                   1166:                        if (cp) {
                   1167:                                portnum = strchr(cp, ':');
1.52      henning  1168:                                pathstart = strchr(cp, '/');
                   1169:                                /* : in path is not a port # indicator */
                   1170:                                if (portnum && pathstart &&
                   1171:                                    pathstart < portnum)
                   1172:                                        portnum = NULL;
                   1173:
1.31      itojun   1174:                                if (!portnum)
                   1175:                                        ;
                   1176:                                else {
                   1177:                                        if (!dir)
                   1178:                                                ;
                   1179:                                        else if (portnum + 1 < dir) {
                   1180:                                                *portnum++ = '\0';
                   1181:                                                /*
                   1182:                                                 * XXX should check if portnum
                   1183:                                                 * is decimal number
                   1184:                                                 */
                   1185:                                        } else {
                   1186:                                                /* empty portnum */
                   1187:                                                goto bad_ftp_url;
                   1188:                                        }
                   1189:                                }
                   1190:                        } else
                   1191:                                portnum = NULL;
1.8       millert  1192:                } else {                        /* classic style `host:file' */
                   1193:                        dir = strchr(host, ':');
                   1194:                }
1.1       millert  1195:                if (EMPTYSTRING(host)) {
                   1196:                        rval = argpos + 1;
                   1197:                        continue;
                   1198:                }
                   1199:
                   1200:                /*
1.9       millert  1201:                 * If dir is NULL, the file wasn't specified
1.1       millert  1202:                 * (URL looked something like ftp://host)
                   1203:                 */
1.8       millert  1204:                if (dir != NULL)
                   1205:                        *dir++ = '\0';
1.1       millert  1206:
                   1207:                /*
                   1208:                 * Extract the file and (if present) directory name.
                   1209:                 */
1.42      deraadt  1210:                if (!EMPTYSTRING(dir)) {
1.8       millert  1211:                        cp = strrchr(dir, '/');
1.1       millert  1212:                        if (cp != NULL) {
                   1213:                                *cp++ = '\0';
                   1214:                                file = cp;
                   1215:                        } else {
                   1216:                                file = dir;
                   1217:                                dir = NULL;
                   1218:                        }
                   1219:                }
1.80      martynas 1220: #ifndef SMALL
1.1       millert  1221:                if (debug)
1.42      deraadt  1222:                        fprintf(ttyout,
                   1223:                            "user %s:%s host %s port %s dir %s file %s\n",
1.76      martynas 1224:                            username, pass ? "XXXX" : NULL, host, portnum,
                   1225:                            dir, file);
1.80      martynas 1226: #endif /* !SMALL */
1.1       millert  1227:
                   1228:                /*
1.49      krw      1229:                 * Set up the connection.
1.1       millert  1230:                 */
1.49      krw      1231:                if (connected)
                   1232:                        disconnect(0, NULL);
                   1233:                xargv[0] = __progname;
                   1234:                xargv[1] = host;
1.8       millert  1235:                xargv[2] = NULL;
1.49      krw      1236:                xargc = 2;
                   1237:                if (!EMPTYSTRING(portnum)) {
                   1238:                        xargv[2] = portnum;
                   1239:                        xargv[3] = NULL;
                   1240:                        xargc = 3;
                   1241:                }
                   1242:                oautologin = autologin;
1.88      martynas 1243:                if (username == NULL)
1.89      halex    1244:                        anonftp = 1;
                   1245:                else {
                   1246:                        anonftp = 0;
1.49      krw      1247:                        autologin = 0;
1.89      halex    1248:                }
1.49      krw      1249:                setpeer(xargc, xargv);
                   1250:                autologin = oautologin;
1.87      martynas 1251:                if (connected == 0 ||
                   1252:                    (connected == 1 && autologin && (username == NULL ||
                   1253:                    !ftp_login(host, username, pass)))) {
1.49      krw      1254:                        warnx("Can't connect or login to host `%s'", host);
1.8       millert  1255:                        rval = argpos + 1;
                   1256:                        continue;
1.1       millert  1257:                }
1.49      krw      1258:
                   1259:                /* Always use binary transfers. */
                   1260:                setbinary(0, NULL);
1.1       millert  1261:
1.4       millert  1262:                dirhasglob = filehasglob = 0;
                   1263:                if (doglob) {
1.42      deraadt  1264:                        if (!EMPTYSTRING(dir) &&
1.4       millert  1265:                            strpbrk(dir, "*?[]{}") != NULL)
                   1266:                                dirhasglob = 1;
1.42      deraadt  1267:                        if (!EMPTYSTRING(file) &&
1.4       millert  1268:                            strpbrk(file, "*?[]{}") != NULL)
                   1269:                                filehasglob = 1;
                   1270:                }
                   1271:
1.1       millert  1272:                /* Change directories, if necessary. */
1.42      deraadt  1273:                if (!EMPTYSTRING(dir) && !dirhasglob) {
1.1       millert  1274:                        xargv[0] = "cd";
                   1275:                        xargv[1] = dir;
                   1276:                        xargv[2] = NULL;
                   1277:                        cd(2, xargv);
1.42      deraadt  1278:                        if (!dirchange) {
1.1       millert  1279:                                rval = argpos + 1;
                   1280:                                continue;
                   1281:                        }
                   1282:                }
                   1283:
                   1284:                if (EMPTYSTRING(file)) {
1.86      martynas 1285: #ifndef SMALL
1.1       millert  1286:                        rval = -1;
1.86      martynas 1287: #else /* !SMALL */
                   1288:                        recvrequest("NLST", "-", NULL, "w", 0, 0);
                   1289:                        rval = 0;
                   1290: #endif /* !SMALL */
1.1       millert  1291:                        continue;
                   1292:                }
                   1293:
1.21      marc     1294:                if (verbose)
1.10      deraadt  1295:                        fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);
1.1       millert  1296:
1.4       millert  1297:                if (dirhasglob) {
                   1298:                        snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
                   1299:                        file = rempath;
                   1300:                }
                   1301:
                   1302:                /* Fetch the file(s). */
1.10      deraadt  1303:                xargc = 2;
1.1       millert  1304:                xargv[0] = "get";
                   1305:                xargv[1] = file;
                   1306:                xargv[2] = NULL;
1.4       millert  1307:                if (dirhasglob || filehasglob) {
                   1308:                        int ointeractive;
                   1309:
                   1310:                        ointeractive = interactive;
                   1311:                        interactive = 0;
                   1312:                        xargv[0] = "mget";
1.78      martynas 1313: #ifndef SMALL
                   1314:                        if (resume) {
                   1315:                                xargc = 3;
                   1316:                                xargv[1] = "-c";
                   1317:                                xargv[2] = file;
                   1318:                                xargv[3] = NULL;
                   1319:                        }
                   1320: #endif /* !SMALL */
1.10      deraadt  1321:                        mget(xargc, xargv);
1.5       millert  1322:                        interactive = ointeractive;
1.10      deraadt  1323:                } else {
1.17      millert  1324:                        if (outfile != NULL) {
                   1325:                                xargv[2] = outfile;
                   1326:                                xargv[3] = NULL;
1.10      deraadt  1327:                                xargc++;
                   1328:                        }
1.75      martynas 1329: #ifndef SMALL
                   1330:                        if (resume)
                   1331:                                reget(xargc, xargv);
                   1332:                        else
1.78      martynas 1333: #endif /* !SMALL */
1.75      martynas 1334:                                get(xargc, xargv);
1.10      deraadt  1335:                }
1.1       millert  1336:
1.4       millert  1337:                if ((code / 100) != COMPLETE)
1.1       millert  1338:                        rval = argpos + 1;
                   1339:        }
                   1340:        if (connected && rval != -1)
                   1341:                disconnect(0, NULL);
                   1342:        return (rval);
1.37      heko     1343: }
                   1344:
                   1345: char *
1.50      deraadt  1346: urldecode(const char *str)
1.37      heko     1347: {
1.53      deraadt  1348:        char *ret, c;
                   1349:        int i, reallen;
1.37      heko     1350:
1.53      deraadt  1351:        if (str == NULL)
                   1352:                return NULL;
                   1353:        if ((ret = malloc(strlen(str)+1)) == NULL)
                   1354:                err(1, "Can't allocate memory for URL decoding");
                   1355:        for (i = 0, reallen = 0; str[i] != '\0'; i++, reallen++, ret++) {
                   1356:                c = str[i];
                   1357:                if (c == '+') {
                   1358:                        *ret = ' ';
                   1359:                        continue;
                   1360:                }
1.61      deraadt  1361:
                   1362:                /* Cannot use strtol here because next char
                   1363:                 * after %xx may be a digit.
                   1364:                 */
1.53      deraadt  1365:                if (c == '%' && isxdigit(str[i+1]) && isxdigit(str[i+2])) {
                   1366:                        *ret = hextochar(&str[i+1]);
                   1367:                        i+=2;
                   1368:                        continue;
                   1369:                }
                   1370:                *ret = c;
                   1371:        }
                   1372:        *ret = '\0';
                   1373:
                   1374:        return ret-reallen;
1.123     guenther 1375: }
                   1376:
                   1377: char *
                   1378: recode_credentials(const char *userinfo)
                   1379: {
                   1380:        char *ui, *creds;
                   1381:        size_t ulen, credsize;
                   1382:
                   1383:        /* url-decode the user and pass */
                   1384:        ui = urldecode(userinfo);
                   1385:
                   1386:        ulen = strlen(ui);
                   1387:        credsize = (ulen + 2) / 3 * 4 + 1;
                   1388:        creds = malloc(credsize);
                   1389:        if (creds == NULL)
                   1390:                errx(1, "out of memory");
                   1391:        if (b64_ntop(ui, ulen, creds, credsize) == -1)
                   1392:                errx(1, "error in base64 encoding");
                   1393:        free(ui);
                   1394:        return (creds);
1.37      heko     1395: }
                   1396:
                   1397: char
1.50      deraadt  1398: hextochar(const char *str)
1.37      heko     1399: {
1.53      deraadt  1400:        char c, ret;
1.37      heko     1401:
1.53      deraadt  1402:        c = str[0];
                   1403:        ret = c;
                   1404:        if (isalpha(c))
                   1405:                ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
                   1406:        else
                   1407:                ret -= '0';
                   1408:        ret *= 16;
                   1409:
                   1410:        c = str[1];
                   1411:        ret += c;
                   1412:        if (isalpha(c))
                   1413:                ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
                   1414:        else
                   1415:                ret -= '0';
                   1416:        return ret;
1.25      itojun   1417: }
                   1418:
                   1419: int
1.50      deraadt  1420: isurl(const char *p)
1.25      itojun   1421: {
1.27      millert  1422:
1.26      deraadt  1423:        if (strncasecmp(p, FTP_URL, sizeof(FTP_URL) - 1) == 0 ||
                   1424:            strncasecmp(p, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
1.61      deraadt  1425: #ifndef SMALL
                   1426:            strncasecmp(p, HTTPS_URL, sizeof(HTTPS_URL) - 1) == 0 ||
1.78      martynas 1427: #endif /* !SMALL */
1.27      millert  1428:            strncasecmp(p, FILE_URL, sizeof(FILE_URL) - 1) == 0 ||
                   1429:            strstr(p, ":/"))
                   1430:                return (1);
                   1431:        return (0);
1.1       millert  1432: }
1.61      deraadt  1433:
                   1434: char *
1.126     jsing    1435: ftp_readline(FILE *fp, struct ressl *ssl, size_t *lenp)
1.61      deraadt  1436: {
                   1437:        if (fp != NULL)
                   1438:                return fparseln(fp, lenp, NULL, "\0\0\0", 0);
                   1439: #ifndef SMALL
                   1440:        else if (ssl != NULL)
                   1441:                return SSL_readline(ssl, lenp);
1.78      martynas 1442: #endif /* !SMALL */
1.61      deraadt  1443:        else
                   1444:                return NULL;
                   1445: }
                   1446:
1.65      ray      1447: size_t
1.126     jsing    1448: ftp_read(FILE *fp, struct ressl *ssl, char *buf, size_t len)
1.61      deraadt  1449: {
1.65      ray      1450:        size_t ret;
1.61      deraadt  1451:        if (fp != NULL)
                   1452:                ret = fread(buf, sizeof(char), len, fp);
                   1453: #ifndef SMALL
1.65      ray      1454:        else if (ssl != NULL) {
1.126     jsing    1455:                size_t nr;
1.65      ray      1456:
1.126     jsing    1457:                if ((ret = ressl_read(ssl, buf, len, &nr)) != 0)
1.65      ray      1458:                        ret = 0;
                   1459:                else
                   1460:                        ret = nr;
                   1461:        }
1.78      martynas 1462: #endif /* !SMALL */
1.61      deraadt  1463:        else
                   1464:                ret = 0;
                   1465:        return (ret);
                   1466: }
                   1467:
                   1468: int
1.126     jsing    1469: ftp_printf(FILE *fp, struct ressl *ssl, const char *fmt, ...)
1.61      deraadt  1470: {
                   1471:        int ret;
                   1472:        va_list ap;
                   1473:
                   1474:        va_start(ap, fmt);
                   1475:
                   1476:        if (fp != NULL)
                   1477:                ret = vfprintf(fp, fmt, ap);
                   1478: #ifndef SMALL
                   1479:        else if (ssl != NULL)
1.126     jsing    1480:                ret = SSL_vprintf(ssl, fmt, ap);
1.78      martynas 1481: #endif /* !SMALL */
1.61      deraadt  1482:        else
1.92      jsg      1483:                ret = 0;
1.61      deraadt  1484:
                   1485:        va_end(ap);
                   1486:        return (ret);
                   1487: }
                   1488:
                   1489: #ifndef SMALL
                   1490: int
1.126     jsing    1491: SSL_vprintf(struct ressl *ssl, const char *fmt, va_list ap)
1.61      deraadt  1492: {
1.126     jsing    1493:        char *string;
                   1494:        size_t nw;
1.61      deraadt  1495:        int ret;
                   1496:
                   1497:        if ((ret = vasprintf(&string, fmt, ap)) == -1)
                   1498:                return ret;
1.126     jsing    1499:        ret = ressl_write(ssl, string, ret, &nw);
1.61      deraadt  1500:        free(string);
                   1501:        return ret;
                   1502: }
                   1503:
                   1504: char *
1.126     jsing    1505: SSL_readline(struct ressl *ssl, size_t *lenp)
1.61      deraadt  1506: {
1.126     jsing    1507:        size_t i, len, nr;
1.61      deraadt  1508:        char *buf, *q, c;
1.109     sthen    1509:        int ret;
1.61      deraadt  1510:
                   1511:        len = 128;
                   1512:        if ((buf = malloc(len)) == NULL)
                   1513:                errx(1, "Can't allocate memory for transfer buffer");
                   1514:        for (i = 0; ; i++) {
                   1515:                if (i >= len - 1) {
                   1516:                        if ((q = realloc(buf, 2 * len)) == NULL)
                   1517:                                errx(1, "Can't expand transfer buffer");
                   1518:                        buf = q;
                   1519:                        len *= 2;
                   1520:                }
1.109     sthen    1521: again:
1.126     jsing    1522:                ret = ressl_read(ssl, &c, 1, &nr);
                   1523:                if (ret == -2)
                   1524:                        goto again;
                   1525:                if (ret != 0)
                   1526:                        errx(1, "SSL read error: %u", ret);
                   1527:
1.61      deraadt  1528:                buf[i] = c;
                   1529:                if (c == '\n')
                   1530:                        break;
                   1531:        }
                   1532:        *lenp = i;
                   1533:        return (buf);
                   1534: }
                   1535:
                   1536: int
1.81      espie    1537: proxy_connect(int socket, char *host, char *cookie)
1.61      deraadt  1538: {
1.66      ray      1539:        int l;
1.61      deraadt  1540:        char buf[1024];
                   1541:        char *connstr, *hosttail, *port;
                   1542:
                   1543:        if (*host == '[' && (hosttail = strrchr(host, ']')) != NULL &&
                   1544:                (hosttail[1] == '\0' || hosttail[1] == ':')) {
                   1545:                host++;
                   1546:                *hosttail++ = '\0';
                   1547:        } else
                   1548:                hosttail = host;
                   1549:
                   1550:        port = strrchr(hosttail, ':');               /* find portnum */
                   1551:        if (port != NULL)
                   1552:                *port++ = '\0';
                   1553:        if (!port)
                   1554:                port = "443";
                   1555:
1.81      espie    1556:        if (cookie) {
                   1557:                l = asprintf(&connstr, "CONNECT %s:%s HTTP/1.1\r\n"
                   1558:                        "Proxy-Authorization: Basic %s\r\n%s\r\n\r\n",
                   1559:                        host, port, cookie, HTTP_USER_AGENT);
                   1560:        } else {
                   1561:                l = asprintf(&connstr, "CONNECT %s:%s HTTP/1.1\r\n%s\r\n\r\n",
                   1562:                        host, port, HTTP_USER_AGENT);
                   1563:        }
                   1564:
1.66      ray      1565:        if (l == -1)
1.61      deraadt  1566:                errx(1, "Could not allocate memory to assemble connect string!");
1.80      martynas 1567: #ifndef SMALL
1.68      ray      1568:        if (debug)
                   1569:                printf("%s", connstr);
1.80      martynas 1570: #endif /* !SMALL */
1.66      ray      1571:        if (write(socket, connstr, l) != l)
1.68      ray      1572:                err(1, "Could not send connect string");
1.61      deraadt  1573:        read(socket, &buf, sizeof(buf)); /* only proxy header XXX: error handling? */
1.71      ray      1574:        free(connstr);
1.61      deraadt  1575:        return(200);
                   1576: }
1.78      martynas 1577: #endif /* !SMALL */