Home | History | Annotate | Line # | Download | only in ftp
ssl.c revision 1.13
      1  1.13   mlelstv /*	$NetBSD: ssl.c,v 1.13 2023/02/25 12:07:25 mlelstv Exp $	*/
      2   1.1  christos 
      3   1.1  christos /*-
      4   1.1  christos  * Copyright (c) 1998-2004 Dag-Erling Codan Smrgrav
      5   1.1  christos  * Copyright (c) 2008, 2010 Joerg Sonnenberger <joerg (at) NetBSD.org>
      6   1.3       wiz  * Copyright (c) 2015 Thomas Klausner <wiz (at) NetBSD.org>
      7  1.13   mlelstv  * Copyright (c) 2023 Michael van Elst <mlelstv (at) NetBSD.org>
      8   1.1  christos  * All rights reserved.
      9   1.1  christos  *
     10   1.1  christos  * Redistribution and use in source and binary forms, with or without
     11   1.1  christos  * modification, are permitted provided that the following conditions
     12   1.1  christos  * are met:
     13   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14   1.1  christos  *    notice, this list of conditions and the following disclaimer
     15   1.1  christos  *    in this position and unchanged.
     16   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     18   1.1  christos  *    documentation and/or other materials provided with the distribution.
     19   1.1  christos  * 3. The name of the author may not be used to endorse or promote products
     20   1.1  christos  *    derived from this software without specific prior written permission
     21   1.1  christos  *
     22   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1  christos  *
     33   1.1  christos  * $FreeBSD: common.c,v 1.53 2007/12/19 00:26:36 des Exp $
     34   1.1  christos  */
     35   1.1  christos 
     36   1.1  christos #include <sys/cdefs.h>
     37   1.1  christos #ifndef lint
     38  1.13   mlelstv __RCSID("$NetBSD: ssl.c,v 1.13 2023/02/25 12:07:25 mlelstv Exp $");
     39   1.1  christos #endif
     40   1.1  christos 
     41  1.10     lukem #include <errno.h>
     42  1.10     lukem #include <fcntl.h>
     43  1.10     lukem #include <stdarg.h>
     44  1.10     lukem #include <stdio.h>
     45  1.10     lukem #include <stdlib.h>
     46  1.10     lukem #include <string.h>
     47   1.1  christos #include <time.h>
     48   1.1  christos #include <unistd.h>
     49   1.1  christos 
     50   1.1  christos #include <sys/param.h>
     51   1.1  christos #include <sys/select.h>
     52   1.1  christos #include <sys/uio.h>
     53   1.1  christos 
     54   1.1  christos #include <netinet/tcp.h>
     55   1.1  christos #include <netinet/in.h>
     56  1.10     lukem 
     57  1.10     lukem #ifdef WITH_SSL
     58   1.1  christos #include <openssl/crypto.h>
     59   1.1  christos #include <openssl/x509.h>
     60   1.1  christos #include <openssl/pem.h>
     61   1.1  christos #include <openssl/ssl.h>
     62   1.1  christos #include <openssl/err.h>
     63  1.10     lukem #endif
     64   1.1  christos 
     65   1.1  christos #include "ssl.h"
     66   1.1  christos 
     67  1.13   mlelstv #include <stringlist.h>
     68  1.13   mlelstv #include <histedit.h>
     69  1.13   mlelstv #include <sys/poll.h>
     70  1.13   mlelstv #include "extern.h"
     71  1.13   mlelstv 
     72   1.1  christos extern int quit_time, verbose, ftp_debug;
     73   1.1  christos extern FILE *ttyout;
     74   1.1  christos 
     75   1.1  christos struct fetch_connect {
     76   1.1  christos 	int			 sd;		/* file/socket descriptor */
     77   1.1  christos 	char			*buf;		/* buffer */
     78   1.1  christos 	size_t			 bufsize;	/* buffer size */
     79   1.1  christos 	size_t			 bufpos;	/* position of buffer */
     80   1.1  christos 	size_t			 buflen;	/* length of buffer contents */
     81   1.1  christos 	struct {				/* data cached after an
     82   1.1  christos 						   interrupted read */
     83   1.1  christos 		char	*buf;
     84   1.1  christos 		size_t	 size;
     85   1.1  christos 		size_t	 pos;
     86   1.1  christos 		size_t	 len;
     87   1.1  christos 	} cache;
     88   1.1  christos 	int 			 issock;
     89   1.1  christos 	int			 iserr;
     90   1.1  christos 	int			 iseof;
     91  1.10     lukem #ifdef WITH_SSL
     92   1.1  christos 	SSL			*ssl;		/* SSL handle */
     93  1.10     lukem #endif
     94   1.1  christos };
     95   1.1  christos 
     96   1.1  christos /*
     97   1.1  christos  * Write a vector to a connection w/ timeout
     98   1.1  christos  * Note: can modify the iovec.
     99   1.1  christos  */
    100   1.1  christos static ssize_t
    101   1.1  christos fetch_writev(struct fetch_connect *conn, struct iovec *iov, int iovcnt)
    102   1.1  christos {
    103   1.1  christos 	struct timeval now, timeout, delta;
    104   1.1  christos 	fd_set writefds;
    105   1.1  christos 	ssize_t len, total;
    106   1.8  christos 	int fd = conn->sd;
    107   1.1  christos 	int r;
    108   1.1  christos 
    109   1.1  christos 	if (quit_time > 0) {
    110   1.1  christos 		FD_ZERO(&writefds);
    111   1.1  christos 		gettimeofday(&timeout, NULL);
    112   1.1  christos 		timeout.tv_sec += quit_time;
    113   1.1  christos 	}
    114   1.1  christos 
    115   1.1  christos 	total = 0;
    116   1.1  christos 	while (iovcnt > 0) {
    117   1.8  christos 		while (quit_time > 0 && !FD_ISSET(fd, &writefds)) {
    118   1.8  christos 			FD_SET(fd, &writefds);
    119   1.1  christos 			gettimeofday(&now, NULL);
    120   1.1  christos 			delta.tv_sec = timeout.tv_sec - now.tv_sec;
    121   1.1  christos 			delta.tv_usec = timeout.tv_usec - now.tv_usec;
    122   1.1  christos 			if (delta.tv_usec < 0) {
    123   1.1  christos 				delta.tv_usec += 1000000;
    124   1.1  christos 				delta.tv_sec--;
    125   1.1  christos 			}
    126   1.1  christos 			if (delta.tv_sec < 0) {
    127   1.1  christos 				errno = ETIMEDOUT;
    128   1.1  christos 				return -1;
    129   1.1  christos 			}
    130   1.1  christos 			errno = 0;
    131   1.8  christos 			r = select(fd + 1, NULL, &writefds, NULL, &delta);
    132   1.1  christos 			if (r == -1) {
    133   1.1  christos 				if (errno == EINTR)
    134   1.1  christos 					continue;
    135   1.1  christos 				return -1;
    136   1.1  christos 			}
    137   1.1  christos 		}
    138   1.1  christos 		errno = 0;
    139  1.10     lukem #ifdef WITH_SSL
    140   1.1  christos 		if (conn->ssl != NULL)
    141   1.1  christos 			len = SSL_write(conn->ssl, iov->iov_base, iov->iov_len);
    142   1.1  christos 		else
    143  1.10     lukem #endif
    144   1.8  christos 			len = writev(fd, iov, iovcnt);
    145   1.1  christos 		if (len == 0) {
    146   1.1  christos 			/* we consider a short write a failure */
    147   1.1  christos 			/* XXX perhaps we shouldn't in the SSL case */
    148   1.1  christos 			errno = EPIPE;
    149   1.1  christos 			return -1;
    150   1.1  christos 		}
    151   1.1  christos 		if (len < 0) {
    152   1.8  christos 			if (errno == EINTR || errno == EAGAIN)
    153   1.1  christos 				continue;
    154   1.1  christos 			return -1;
    155   1.1  christos 		}
    156   1.1  christos 		total += len;
    157   1.1  christos 		while (iovcnt > 0 && len >= (ssize_t)iov->iov_len) {
    158   1.1  christos 			len -= iov->iov_len;
    159   1.1  christos 			iov++;
    160   1.1  christos 			iovcnt--;
    161   1.1  christos 		}
    162   1.1  christos 		if (iovcnt > 0) {
    163   1.1  christos 			iov->iov_len -= len;
    164   1.1  christos 			iov->iov_base = (char *)iov->iov_base + len;
    165   1.1  christos 		}
    166   1.1  christos 	}
    167   1.1  christos 	return total;
    168   1.1  christos }
    169   1.1  christos 
    170   1.8  christos static ssize_t
    171   1.8  christos fetch_write(const void *str, size_t len, struct fetch_connect *conn)
    172   1.1  christos {
    173   1.1  christos 	struct iovec iov[1];
    174   1.1  christos 
    175   1.1  christos 	iov[0].iov_base = (char *)__UNCONST(str);
    176   1.1  christos 	iov[0].iov_len = len;
    177   1.1  christos 	return fetch_writev(conn, iov, 1);
    178   1.1  christos }
    179   1.1  christos 
    180   1.1  christos /*
    181   1.1  christos  * Send a formatted line; optionally echo to terminal
    182   1.1  christos  */
    183   1.1  christos int
    184   1.1  christos fetch_printf(struct fetch_connect *conn, const char *fmt, ...)
    185   1.1  christos {
    186   1.1  christos 	va_list ap;
    187   1.1  christos 	size_t len;
    188   1.1  christos 	char *msg;
    189   1.1  christos 	int r;
    190   1.1  christos 
    191   1.1  christos 	va_start(ap, fmt);
    192   1.1  christos 	len = vasprintf(&msg, fmt, ap);
    193   1.1  christos 	va_end(ap);
    194   1.1  christos 
    195   1.1  christos 	if (msg == NULL) {
    196   1.1  christos 		errno = ENOMEM;
    197   1.1  christos 		return -1;
    198   1.1  christos 	}
    199   1.1  christos 
    200   1.8  christos 	r = fetch_write(msg, len, conn);
    201   1.1  christos 	free(msg);
    202   1.1  christos 	return r;
    203   1.1  christos }
    204   1.1  christos 
    205   1.1  christos int
    206   1.1  christos fetch_fileno(struct fetch_connect *conn)
    207   1.1  christos {
    208   1.1  christos 
    209   1.1  christos 	return conn->sd;
    210   1.1  christos }
    211   1.1  christos 
    212   1.1  christos int
    213   1.1  christos fetch_error(struct fetch_connect *conn)
    214   1.1  christos {
    215   1.1  christos 
    216   1.1  christos 	return conn->iserr;
    217   1.1  christos }
    218   1.1  christos 
    219   1.1  christos static void
    220   1.1  christos fetch_clearerr(struct fetch_connect *conn)
    221   1.1  christos {
    222   1.1  christos 
    223   1.1  christos 	conn->iserr = 0;
    224   1.1  christos }
    225   1.1  christos 
    226   1.1  christos int
    227   1.1  christos fetch_flush(struct fetch_connect *conn)
    228   1.1  christos {
    229   1.1  christos 
    230   1.1  christos 	if (conn->issock) {
    231   1.8  christos 		int fd = conn->sd;
    232   1.8  christos 		int v;
    233   1.1  christos #ifdef TCP_NOPUSH
    234   1.1  christos 		v = 0;
    235   1.8  christos 		setsockopt(fd, IPPROTO_TCP, TCP_NOPUSH, &v, sizeof(v));
    236   1.1  christos #endif
    237   1.1  christos 		v = 1;
    238   1.8  christos 		setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
    239   1.1  christos 	}
    240   1.1  christos 	return 0;
    241   1.1  christos }
    242   1.1  christos 
    243   1.1  christos /*ARGSUSED*/
    244   1.1  christos struct fetch_connect *
    245   1.1  christos fetch_open(const char *fname, const char *fmode)
    246   1.1  christos {
    247   1.1  christos 	struct fetch_connect *conn;
    248   1.1  christos 	int fd;
    249   1.1  christos 
    250   1.1  christos 	fd = open(fname, O_RDONLY); /* XXX: fmode */
    251   1.1  christos 	if (fd < 0)
    252   1.1  christos 		return NULL;
    253   1.1  christos 
    254   1.1  christos 	if ((conn = calloc(1, sizeof(*conn))) == NULL) {
    255   1.1  christos 		close(fd);
    256   1.1  christos 		return NULL;
    257   1.1  christos 	}
    258   1.1  christos 
    259   1.1  christos 	conn->sd = fd;
    260   1.1  christos 	conn->issock = 0;
    261   1.1  christos 	return conn;
    262   1.1  christos }
    263   1.1  christos 
    264   1.1  christos /*ARGSUSED*/
    265   1.1  christos struct fetch_connect *
    266   1.1  christos fetch_fdopen(int sd, const char *fmode)
    267   1.1  christos {
    268   1.1  christos 	struct fetch_connect *conn;
    269   1.2  christos #if defined(SO_NOSIGPIPE) || defined(TCP_NOPUSH)
    270   1.1  christos 	int opt = 1;
    271   1.2  christos #endif
    272   1.1  christos 
    273   1.1  christos 	if ((conn = calloc(1, sizeof(*conn))) == NULL)
    274   1.1  christos 		return NULL;
    275   1.1  christos 
    276   1.1  christos 	conn->sd = sd;
    277   1.1  christos 	conn->issock = 1;
    278   1.1  christos 	fcntl(sd, F_SETFD, FD_CLOEXEC);
    279   1.2  christos #ifdef SO_NOSIGPIPE
    280   1.1  christos 	setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
    281   1.2  christos #endif
    282   1.1  christos #ifdef TCP_NOPUSH
    283   1.1  christos 	setsockopt(sd, IPPROTO_TCP, TCP_NOPUSH, &opt, sizeof(opt));
    284   1.1  christos #endif
    285   1.1  christos 	return conn;
    286   1.1  christos }
    287   1.1  christos 
    288   1.1  christos int
    289   1.1  christos fetch_close(struct fetch_connect *conn)
    290   1.1  christos {
    291   1.8  christos 	if (conn == NULL)
    292   1.8  christos 		return 0;
    293   1.1  christos 
    294   1.8  christos 	fetch_flush(conn);
    295  1.10     lukem #ifdef WITH_SSL
    296   1.8  christos 	SSL_free(conn->ssl);
    297  1.10     lukem #endif
    298   1.8  christos 	close(conn->sd);
    299   1.8  christos 	free(conn->cache.buf);
    300   1.8  christos 	free(conn->buf);
    301   1.8  christos 	free(conn);
    302   1.8  christos 	return 0;
    303   1.1  christos }
    304   1.1  christos 
    305   1.8  christos #define FETCH_WRITE_WAIT	-3
    306   1.1  christos #define FETCH_READ_WAIT		-2
    307   1.1  christos #define FETCH_READ_ERROR	-1
    308   1.1  christos 
    309  1.10     lukem #ifdef WITH_SSL
    310   1.1  christos static ssize_t
    311   1.1  christos fetch_ssl_read(SSL *ssl, void *buf, size_t len)
    312   1.1  christos {
    313   1.1  christos 	ssize_t rlen;
    314   1.8  christos 	rlen = SSL_read(ssl, buf, len);
    315   1.8  christos 	if (rlen >= 0)
    316   1.8  christos 		return rlen;
    317   1.1  christos 
    318   1.8  christos 	switch (SSL_get_error(ssl, rlen)) {
    319   1.8  christos 	case SSL_ERROR_WANT_READ:
    320   1.8  christos 		return FETCH_READ_WAIT;
    321   1.8  christos 	case SSL_ERROR_WANT_WRITE:
    322   1.8  christos 		return FETCH_WRITE_WAIT;
    323   1.8  christos 	default:
    324   1.1  christos 		ERR_print_errors_fp(ttyout);
    325   1.1  christos 		return FETCH_READ_ERROR;
    326   1.1  christos 	}
    327   1.1  christos }
    328  1.10     lukem #endif /* WITH_SSL */
    329   1.1  christos 
    330   1.1  christos static ssize_t
    331   1.1  christos fetch_nonssl_read(int sd, void *buf, size_t len)
    332   1.1  christos {
    333   1.1  christos 	ssize_t rlen;
    334   1.1  christos 
    335   1.1  christos 	rlen = read(sd, buf, len);
    336   1.8  christos 	if (rlen == -1) {
    337   1.1  christos 		if (errno == EAGAIN || errno == EINTR)
    338   1.1  christos 			return FETCH_READ_WAIT;
    339   1.1  christos 		return FETCH_READ_ERROR;
    340   1.1  christos 	}
    341   1.1  christos 	return rlen;
    342   1.1  christos }
    343   1.1  christos 
    344   1.1  christos /*
    345   1.1  christos  * Cache some data that was read from a socket but cannot be immediately
    346   1.1  christos  * returned because of an interrupted system call.
    347   1.1  christos  */
    348   1.1  christos static int
    349   1.1  christos fetch_cache_data(struct fetch_connect *conn, char *src, size_t nbytes)
    350   1.1  christos {
    351   1.1  christos 
    352   1.1  christos 	if (conn->cache.size < nbytes) {
    353   1.1  christos 		char *tmp = realloc(conn->cache.buf, nbytes);
    354   1.1  christos 		if (tmp == NULL)
    355   1.1  christos 			return -1;
    356   1.1  christos 
    357   1.1  christos 		conn->cache.buf = tmp;
    358   1.1  christos 		conn->cache.size = nbytes;
    359   1.1  christos 	}
    360   1.1  christos 
    361   1.1  christos 	memcpy(conn->cache.buf, src, nbytes);
    362   1.1  christos 	conn->cache.len = nbytes;
    363   1.1  christos 	conn->cache.pos = 0;
    364   1.1  christos 	return 0;
    365   1.1  christos }
    366   1.1  christos 
    367   1.8  christos static int
    368   1.8  christos fetch_wait(struct fetch_connect *conn, ssize_t rlen, struct timeval *timeout)
    369   1.8  christos {
    370   1.8  christos 	struct timeval now, delta;
    371   1.8  christos 	int fd = conn->sd;
    372   1.8  christos 	fd_set fds;
    373   1.8  christos 
    374   1.8  christos 	FD_ZERO(&fds);
    375   1.8  christos 	while (!FD_ISSET(fd, &fds)) {
    376   1.8  christos 		FD_SET(fd, &fds);
    377   1.8  christos 		if (quit_time > 0) {
    378   1.8  christos 			gettimeofday(&now, NULL);
    379   1.8  christos 			if (!timercmp(timeout, &now, >)) {
    380   1.9     lukem 				fprintf(ttyout, "\r\n%s: transfer aborted"
    381   1.9     lukem 				    " because stalled for %lu sec.\r\n",
    382   1.9     lukem 				    getprogname(), (unsigned long)quit_time);
    383   1.9     lukem 				errno = ETIMEDOUT;
    384   1.8  christos 				conn->iserr = ETIMEDOUT;
    385   1.8  christos 				return -1;
    386   1.8  christos 			}
    387   1.8  christos 			timersub(timeout, &now, &delta);
    388   1.8  christos 		}
    389   1.8  christos 		errno = 0;
    390   1.8  christos 		if (select(fd + 1,
    391   1.8  christos 			rlen == FETCH_READ_WAIT ? &fds : NULL,
    392   1.8  christos 			rlen == FETCH_WRITE_WAIT ? &fds : NULL,
    393   1.8  christos 			NULL, quit_time > 0 ? &delta : NULL) < 0) {
    394   1.8  christos 			if (errno == EINTR)
    395   1.8  christos 				continue;
    396   1.8  christos 			conn->iserr = errno;
    397   1.8  christos 			return -1;
    398   1.8  christos 		}
    399   1.8  christos 	}
    400   1.8  christos 	return 0;
    401   1.8  christos }
    402   1.8  christos 
    403   1.7  christos size_t
    404   1.1  christos fetch_read(void *ptr, size_t size, size_t nmemb, struct fetch_connect *conn)
    405   1.1  christos {
    406   1.1  christos 	ssize_t rlen, total;
    407   1.1  christos 	size_t len;
    408   1.1  christos 	char *start, *buf;
    409   1.8  christos 	struct timeval timeout;
    410   1.1  christos 
    411   1.1  christos 	if (quit_time > 0) {
    412   1.1  christos 		gettimeofday(&timeout, NULL);
    413   1.1  christos 		timeout.tv_sec += quit_time;
    414   1.1  christos 	}
    415   1.1  christos 
    416   1.1  christos 	total = 0;
    417   1.1  christos 	start = buf = ptr;
    418   1.1  christos 	len = size * nmemb;
    419   1.1  christos 
    420   1.1  christos 	if (conn->cache.len > 0) {
    421   1.1  christos 		/*
    422   1.1  christos 		 * The last invocation of fetch_read was interrupted by a
    423   1.1  christos 		 * signal after some data had been read from the socket. Copy
    424   1.1  christos 		 * the cached data into the supplied buffer before trying to
    425   1.1  christos 		 * read from the socket again.
    426   1.1  christos 		 */
    427   1.1  christos 		total = (conn->cache.len < len) ? conn->cache.len : len;
    428   1.1  christos 		memcpy(buf, conn->cache.buf, total);
    429   1.1  christos 
    430   1.1  christos 		conn->cache.len -= total;
    431   1.1  christos 		conn->cache.pos += total;
    432   1.1  christos 		len -= total;
    433   1.1  christos 		buf += total;
    434   1.1  christos 	}
    435   1.1  christos 
    436   1.1  christos 	while (len > 0) {
    437   1.1  christos 		/*
    438   1.1  christos 		 * The socket is non-blocking.  Instead of the canonical
    439   1.1  christos 		 * select() -> read(), we do the following:
    440   1.1  christos 		 *
    441   1.1  christos 		 * 1) call read() or SSL_read().
    442   1.1  christos 		 * 2) if an error occurred, return -1.
    443   1.1  christos 		 * 3) if we received data but we still expect more,
    444   1.1  christos 		 *    update our counters and loop.
    445   1.1  christos 		 * 4) if read() or SSL_read() signaled EOF, return.
    446   1.1  christos 		 * 5) if we did not receive any data but we're not at EOF,
    447   1.1  christos 		 *    call select().
    448   1.1  christos 		 *
    449   1.1  christos 		 * In the SSL case, this is necessary because if we
    450   1.1  christos 		 * receive a close notification, we have to call
    451   1.1  christos 		 * SSL_read() one additional time after we've read
    452   1.1  christos 		 * everything we received.
    453   1.1  christos 		 *
    454   1.1  christos 		 * In the non-SSL case, it may improve performance (very
    455   1.1  christos 		 * slightly) when reading small amounts of data.
    456   1.1  christos 		 */
    457  1.10     lukem #ifdef WITH_SSL
    458   1.1  christos 		if (conn->ssl != NULL)
    459   1.1  christos 			rlen = fetch_ssl_read(conn->ssl, buf, len);
    460   1.1  christos 		else
    461  1.10     lukem #endif
    462   1.1  christos 			rlen = fetch_nonssl_read(conn->sd, buf, len);
    463   1.8  christos 		switch (rlen) {
    464   1.8  christos 		case 0:
    465   1.7  christos 			conn->iseof = 1;
    466   1.8  christos 			return total;
    467   1.8  christos 		case FETCH_READ_ERROR:
    468   1.7  christos 			conn->iserr = errno;
    469   1.1  christos 			if (errno == EINTR)
    470   1.1  christos 				fetch_cache_data(conn, start, total);
    471   1.7  christos 			return 0;
    472   1.8  christos 		case FETCH_READ_WAIT:
    473   1.8  christos 		case FETCH_WRITE_WAIT:
    474   1.8  christos 			if (fetch_wait(conn, rlen, &timeout) == -1)
    475   1.7  christos 				return 0;
    476   1.8  christos 			break;
    477   1.8  christos 		default:
    478   1.8  christos 			len -= rlen;
    479   1.8  christos 			buf += rlen;
    480   1.8  christos 			total += rlen;
    481   1.8  christos 			break;
    482   1.1  christos 		}
    483   1.1  christos 	}
    484   1.1  christos 	return total;
    485   1.1  christos }
    486   1.1  christos 
    487   1.1  christos #define MIN_BUF_SIZE 1024
    488   1.1  christos 
    489   1.1  christos /*
    490   1.1  christos  * Read a line of text from a connection w/ timeout
    491   1.1  christos  */
    492   1.1  christos char *
    493   1.1  christos fetch_getln(char *str, int size, struct fetch_connect *conn)
    494   1.1  christos {
    495   1.1  christos 	size_t tmpsize;
    496   1.7  christos 	size_t len;
    497   1.1  christos 	char c;
    498   1.1  christos 
    499   1.1  christos 	if (conn->buf == NULL) {
    500   1.1  christos 		if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) {
    501   1.1  christos 			errno = ENOMEM;
    502   1.1  christos 			conn->iserr = 1;
    503   1.1  christos 			return NULL;
    504   1.1  christos 		}
    505   1.1  christos 		conn->bufsize = MIN_BUF_SIZE;
    506   1.1  christos 	}
    507   1.1  christos 
    508   1.1  christos 	if (conn->iserr || conn->iseof)
    509   1.1  christos 		return NULL;
    510   1.1  christos 
    511   1.1  christos 	if (conn->buflen - conn->bufpos > 0)
    512   1.1  christos 		goto done;
    513   1.1  christos 
    514   1.1  christos 	conn->buf[0] = '\0';
    515   1.1  christos 	conn->bufpos = 0;
    516   1.1  christos 	conn->buflen = 0;
    517   1.1  christos 	do {
    518   1.1  christos 		len = fetch_read(&c, sizeof(c), 1, conn);
    519   1.1  christos 		if (len == 0) {
    520   1.7  christos 			if (conn->iserr)
    521   1.7  christos 				return NULL;
    522   1.7  christos 			if (conn->iseof)
    523   1.7  christos 				break;
    524   1.7  christos 			abort();
    525   1.1  christos 		}
    526   1.1  christos 		conn->buf[conn->buflen++] = c;
    527   1.1  christos 		if (conn->buflen == conn->bufsize) {
    528   1.1  christos 			char *tmp = conn->buf;
    529   1.1  christos 			tmpsize = conn->bufsize * 2 + 1;
    530   1.1  christos 			if ((tmp = realloc(tmp, tmpsize)) == NULL) {
    531   1.1  christos 				errno = ENOMEM;
    532   1.1  christos 				conn->iserr = 1;
    533   1.1  christos 				return NULL;
    534   1.1  christos 			}
    535   1.1  christos 			conn->buf = tmp;
    536   1.1  christos 			conn->bufsize = tmpsize;
    537   1.1  christos 		}
    538   1.1  christos 	} while (c != '\n');
    539   1.1  christos 
    540   1.1  christos 	if (conn->buflen == 0)
    541   1.1  christos 		return NULL;
    542   1.1  christos  done:
    543   1.1  christos 	tmpsize = MIN(size - 1, (int)(conn->buflen - conn->bufpos));
    544   1.1  christos 	memcpy(str, conn->buf + conn->bufpos, tmpsize);
    545   1.1  christos 	str[tmpsize] = '\0';
    546   1.1  christos 	conn->bufpos += tmpsize;
    547   1.1  christos 	return str;
    548   1.1  christos }
    549   1.1  christos 
    550   1.1  christos int
    551   1.1  christos fetch_getline(struct fetch_connect *conn, char *buf, size_t buflen,
    552   1.1  christos     const char **errormsg)
    553   1.1  christos {
    554   1.1  christos 	size_t len;
    555   1.1  christos 	int rv;
    556   1.1  christos 
    557   1.1  christos 	if (fetch_getln(buf, buflen, conn) == NULL) {
    558   1.1  christos 		if (conn->iseof) {	/* EOF */
    559   1.1  christos 			rv = -2;
    560   1.1  christos 			if (errormsg)
    561   1.1  christos 				*errormsg = "\nEOF received";
    562   1.1  christos 		} else {		/* error */
    563   1.1  christos 			rv = -1;
    564   1.1  christos 			if (errormsg)
    565   1.1  christos 				*errormsg = "Error encountered";
    566   1.1  christos 		}
    567   1.1  christos 		fetch_clearerr(conn);
    568   1.1  christos 		return rv;
    569   1.1  christos 	}
    570   1.1  christos 	len = strlen(buf);
    571   1.1  christos 	if (buf[len - 1] == '\n') {	/* clear any trailing newline */
    572   1.1  christos 		buf[--len] = '\0';
    573   1.1  christos 	} else if (len == buflen - 1) {	/* line too long */
    574   1.1  christos 		while (1) {
    575   1.1  christos 			char c;
    576   1.7  christos 			size_t rlen = fetch_read(&c, sizeof(c), 1, conn);
    577   1.7  christos 			if (rlen == 0 || c == '\n')
    578   1.1  christos 				break;
    579   1.1  christos 		}
    580   1.1  christos 		if (errormsg)
    581   1.1  christos 			*errormsg = "Input line is too long";
    582   1.1  christos 		fetch_clearerr(conn);
    583   1.1  christos 		return -3;
    584   1.1  christos 	}
    585   1.1  christos 	if (errormsg)
    586   1.1  christos 		*errormsg = NULL;
    587   1.1  christos 	return len;
    588   1.1  christos }
    589   1.1  christos 
    590  1.10     lukem #ifdef WITH_SSL
    591   1.1  christos void *
    592   1.3       wiz fetch_start_ssl(int sock, const char *servername)
    593   1.1  christos {
    594   1.1  christos 	SSL *ssl;
    595   1.1  christos 	SSL_CTX *ctx;
    596  1.11  christos 	X509_VERIFY_PARAM *param;
    597   1.1  christos 	int ret, ssl_err;
    598  1.13   mlelstv 	int verify = !ftp_truthy("sslnoverify", getoptionvalue("sslnoverify"), 0);
    599   1.1  christos 
    600   1.1  christos 	/* Init the SSL library and context */
    601   1.1  christos 	if (!SSL_library_init()){
    602   1.1  christos 		fprintf(ttyout, "SSL library init failed\n");
    603   1.1  christos 		return NULL;
    604   1.1  christos 	}
    605   1.1  christos 
    606   1.1  christos 	SSL_load_error_strings();
    607   1.1  christos 
    608   1.1  christos 	ctx = SSL_CTX_new(SSLv23_client_method());
    609   1.1  christos 	SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
    610  1.11  christos 	if (verify) {
    611  1.11  christos 		SSL_CTX_set_default_verify_paths(ctx);
    612  1.11  christos 		SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
    613  1.11  christos 	}
    614   1.1  christos 
    615   1.1  christos 	ssl = SSL_new(ctx);
    616   1.1  christos 	if (ssl == NULL){
    617   1.1  christos 		fprintf(ttyout, "SSL context creation failed\n");
    618   1.1  christos 		SSL_CTX_free(ctx);
    619   1.1  christos 		return NULL;
    620   1.1  christos 	}
    621  1.11  christos 
    622  1.11  christos 	if (verify) {
    623  1.11  christos 		param = SSL_get0_param(ssl);
    624  1.11  christos 		if (!X509_VERIFY_PARAM_set1_host(param, servername,
    625  1.11  christos 		    strlen(servername))) {
    626  1.11  christos 			fprintf(ttyout, "SSL verification setup failed\n");
    627  1.13   mlelstv 			SSL_free(ssl);
    628  1.13   mlelstv 			SSL_CTX_free(ctx);
    629  1.11  christos 			return NULL;
    630  1.11  christos 		}
    631  1.11  christos 
    632  1.11  christos 		/* Enable peer verification, (using the default callback) */
    633  1.11  christos 		SSL_set_verify(ssl, SSL_VERIFY_PEER, NULL);
    634  1.11  christos 	}
    635  1.11  christos 
    636   1.1  christos 	SSL_set_fd(ssl, sock);
    637   1.5     joerg 	if (!SSL_set_tlsext_host_name(ssl, __UNCONST(servername))) {
    638   1.4       wiz 		fprintf(ttyout, "SSL hostname setting failed\n");
    639  1.13   mlelstv 		SSL_free(ssl);
    640   1.4       wiz 		SSL_CTX_free(ctx);
    641   1.4       wiz 		return NULL;
    642   1.3       wiz 	}
    643   1.1  christos 	while ((ret = SSL_connect(ssl)) == -1) {
    644   1.1  christos 		ssl_err = SSL_get_error(ssl, ret);
    645   1.1  christos 		if (ssl_err != SSL_ERROR_WANT_READ &&
    646   1.1  christos 		    ssl_err != SSL_ERROR_WANT_WRITE) {
    647   1.1  christos 			ERR_print_errors_fp(ttyout);
    648   1.1  christos 			SSL_free(ssl);
    649  1.13   mlelstv 			SSL_CTX_free(ctx);
    650   1.1  christos 			return NULL;
    651   1.1  christos 		}
    652   1.1  christos 	}
    653   1.1  christos 
    654   1.1  christos 	if (ftp_debug && verbose) {
    655   1.1  christos 		X509 *cert;
    656   1.1  christos 		X509_NAME *name;
    657   1.1  christos 		char *str;
    658   1.1  christos 
    659   1.1  christos 		fprintf(ttyout, "SSL connection established using %s\n",
    660   1.1  christos 		    SSL_get_cipher(ssl));
    661   1.1  christos 		cert = SSL_get_peer_certificate(ssl);
    662   1.1  christos 		name = X509_get_subject_name(cert);
    663   1.1  christos 		str = X509_NAME_oneline(name, 0, 0);
    664   1.1  christos 		fprintf(ttyout, "Certificate subject: %s\n", str);
    665   1.1  christos 		free(str);
    666   1.1  christos 		name = X509_get_issuer_name(cert);
    667   1.1  christos 		str = X509_NAME_oneline(name, 0, 0);
    668   1.1  christos 		fprintf(ttyout, "Certificate issuer: %s\n", str);
    669   1.1  christos 		free(str);
    670   1.1  christos 	}
    671   1.1  christos 
    672   1.1  christos 	return ssl;
    673   1.1  christos }
    674  1.10     lukem #endif /* WITH_SSL */
    675   1.1  christos 
    676   1.1  christos 
    677   1.1  christos void
    678   1.1  christos fetch_set_ssl(struct fetch_connect *conn, void *ssl)
    679   1.1  christos {
    680  1.10     lukem #ifdef WITH_SSL
    681   1.1  christos 	conn->ssl = ssl;
    682  1.10     lukem #endif
    683   1.1  christos }
    684