Home | History | Annotate | Line # | Download | only in ftp
ssl.c revision 1.15
      1  1.15     lukem /*	$NetBSD: ssl.c,v 1.15 2023/05/05 15:46:06 lukem 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.15     lukem __RCSID("$NetBSD: ssl.c,v 1.15 2023/05/05 15:46:06 lukem Exp $");
     39   1.1  christos #endif
     40   1.1  christos 
     41  1.15     lukem #include <err.h>
     42  1.10     lukem #include <errno.h>
     43  1.10     lukem #include <fcntl.h>
     44  1.10     lukem #include <stdarg.h>
     45  1.10     lukem #include <stdio.h>
     46  1.10     lukem #include <stdlib.h>
     47  1.10     lukem #include <string.h>
     48   1.1  christos #include <time.h>
     49   1.1  christos #include <unistd.h>
     50   1.1  christos 
     51   1.1  christos #include <sys/param.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.14     lukem #include "ftp_var.h"
     67  1.13   mlelstv 
     68   1.1  christos extern int quit_time, verbose, ftp_debug;
     69   1.1  christos extern FILE *ttyout;
     70   1.1  christos 
     71   1.1  christos struct fetch_connect {
     72   1.1  christos 	int			 sd;		/* file/socket descriptor */
     73   1.1  christos 	char			*buf;		/* buffer */
     74   1.1  christos 	size_t			 bufsize;	/* buffer size */
     75   1.1  christos 	size_t			 bufpos;	/* position of buffer */
     76   1.1  christos 	size_t			 buflen;	/* length of buffer contents */
     77   1.1  christos 	struct {				/* data cached after an
     78   1.1  christos 						   interrupted read */
     79   1.1  christos 		char	*buf;
     80   1.1  christos 		size_t	 size;
     81   1.1  christos 		size_t	 pos;
     82   1.1  christos 		size_t	 len;
     83   1.1  christos 	} cache;
     84   1.1  christos 	int 			 issock;
     85   1.1  christos 	int			 iserr;
     86   1.1  christos 	int			 iseof;
     87  1.10     lukem #ifdef WITH_SSL
     88   1.1  christos 	SSL			*ssl;		/* SSL handle */
     89  1.10     lukem #endif
     90   1.1  christos };
     91   1.1  christos 
     92   1.1  christos /*
     93   1.1  christos  * Write a vector to a connection w/ timeout
     94   1.1  christos  * Note: can modify the iovec.
     95   1.1  christos  */
     96   1.1  christos static ssize_t
     97   1.1  christos fetch_writev(struct fetch_connect *conn, struct iovec *iov, int iovcnt)
     98   1.1  christos {
     99  1.15     lukem 	struct timeval timeout, now, delta;
    100   1.1  christos 	ssize_t len, total;
    101   1.8  christos 	int fd = conn->sd;
    102  1.15     lukem 	int rv, timeout_secs;
    103  1.15     lukem 	struct pollfd pfd[1];
    104   1.1  christos 
    105  1.15     lukem 	pfd[0].fd = fd;
    106  1.15     lukem 	pfd[0].events = POLLOUT;
    107  1.15     lukem 	gettimeofday(&timeout, NULL);
    108  1.15     lukem 	timeout.tv_sec += quit_time;
    109   1.1  christos 
    110   1.1  christos 	total = 0;
    111   1.1  christos 	while (iovcnt > 0) {
    112  1.15     lukem 		if (quit_time > 0) {	/* enforce timeout */
    113  1.15     lukem 			do {
    114  1.15     lukem 				(void)gettimeofday(&now, NULL);
    115  1.15     lukem 				timersub(&timeout, &now, &delta);
    116  1.15     lukem 				timeout_secs = delta.tv_sec * 1000 + delta.tv_usec/1000;
    117  1.15     lukem 				if (timeout_secs < 0)
    118  1.15     lukem 					timeout_secs = 0;
    119  1.15     lukem 				rv = ftp_poll(pfd, 1, timeout_secs);
    120  1.15     lukem 					/* loop until poll !EINTR && !EAGAIN */
    121  1.15     lukem 			} while (rv == -1 && (errno == EINTR || errno == EAGAIN));
    122  1.15     lukem 			if (rv == -1)
    123  1.15     lukem 				return -1;
    124  1.15     lukem 			if (rv == 0) {
    125   1.1  christos 				errno = ETIMEDOUT;
    126   1.1  christos 				return -1;
    127   1.1  christos 			}
    128   1.1  christos 		}
    129   1.1  christos 		errno = 0;
    130  1.10     lukem #ifdef WITH_SSL
    131   1.1  christos 		if (conn->ssl != NULL)
    132   1.1  christos 			len = SSL_write(conn->ssl, iov->iov_base, iov->iov_len);
    133   1.1  christos 		else
    134  1.10     lukem #endif
    135   1.8  christos 			len = writev(fd, iov, iovcnt);
    136   1.1  christos 		if (len == 0) {
    137   1.1  christos 			/* we consider a short write a failure */
    138   1.1  christos 			/* XXX perhaps we shouldn't in the SSL case */
    139   1.1  christos 			errno = EPIPE;
    140   1.1  christos 			return -1;
    141   1.1  christos 		}
    142   1.1  christos 		if (len < 0) {
    143   1.8  christos 			if (errno == EINTR || errno == EAGAIN)
    144   1.1  christos 				continue;
    145   1.1  christos 			return -1;
    146   1.1  christos 		}
    147   1.1  christos 		total += len;
    148   1.1  christos 		while (iovcnt > 0 && len >= (ssize_t)iov->iov_len) {
    149   1.1  christos 			len -= iov->iov_len;
    150   1.1  christos 			iov++;
    151   1.1  christos 			iovcnt--;
    152   1.1  christos 		}
    153   1.1  christos 		if (iovcnt > 0) {
    154   1.1  christos 			iov->iov_len -= len;
    155   1.1  christos 			iov->iov_base = (char *)iov->iov_base + len;
    156   1.1  christos 		}
    157   1.1  christos 	}
    158   1.1  christos 	return total;
    159   1.1  christos }
    160   1.1  christos 
    161   1.8  christos static ssize_t
    162   1.8  christos fetch_write(const void *str, size_t len, struct fetch_connect *conn)
    163   1.1  christos {
    164   1.1  christos 	struct iovec iov[1];
    165   1.1  christos 
    166   1.1  christos 	iov[0].iov_base = (char *)__UNCONST(str);
    167   1.1  christos 	iov[0].iov_len = len;
    168   1.1  christos 	return fetch_writev(conn, iov, 1);
    169   1.1  christos }
    170   1.1  christos 
    171   1.1  christos /*
    172   1.1  christos  * Send a formatted line; optionally echo to terminal
    173   1.1  christos  */
    174   1.1  christos int
    175   1.1  christos fetch_printf(struct fetch_connect *conn, const char *fmt, ...)
    176   1.1  christos {
    177   1.1  christos 	va_list ap;
    178   1.1  christos 	size_t len;
    179   1.1  christos 	char *msg;
    180   1.1  christos 	int r;
    181   1.1  christos 
    182   1.1  christos 	va_start(ap, fmt);
    183   1.1  christos 	len = vasprintf(&msg, fmt, ap);
    184   1.1  christos 	va_end(ap);
    185   1.1  christos 
    186   1.1  christos 	if (msg == NULL) {
    187   1.1  christos 		errno = ENOMEM;
    188   1.1  christos 		return -1;
    189   1.1  christos 	}
    190   1.1  christos 
    191   1.8  christos 	r = fetch_write(msg, len, conn);
    192   1.1  christos 	free(msg);
    193   1.1  christos 	return r;
    194   1.1  christos }
    195   1.1  christos 
    196   1.1  christos int
    197   1.1  christos fetch_fileno(struct fetch_connect *conn)
    198   1.1  christos {
    199   1.1  christos 
    200   1.1  christos 	return conn->sd;
    201   1.1  christos }
    202   1.1  christos 
    203   1.1  christos int
    204   1.1  christos fetch_error(struct fetch_connect *conn)
    205   1.1  christos {
    206   1.1  christos 
    207   1.1  christos 	return conn->iserr;
    208   1.1  christos }
    209   1.1  christos 
    210   1.1  christos static void
    211   1.1  christos fetch_clearerr(struct fetch_connect *conn)
    212   1.1  christos {
    213   1.1  christos 
    214   1.1  christos 	conn->iserr = 0;
    215   1.1  christos }
    216   1.1  christos 
    217   1.1  christos int
    218   1.1  christos fetch_flush(struct fetch_connect *conn)
    219   1.1  christos {
    220   1.1  christos 
    221   1.1  christos 	if (conn->issock) {
    222   1.8  christos 		int fd = conn->sd;
    223   1.8  christos 		int v;
    224   1.1  christos #ifdef TCP_NOPUSH
    225   1.1  christos 		v = 0;
    226   1.8  christos 		setsockopt(fd, IPPROTO_TCP, TCP_NOPUSH, &v, sizeof(v));
    227   1.1  christos #endif
    228   1.1  christos 		v = 1;
    229   1.8  christos 		setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
    230   1.1  christos 	}
    231   1.1  christos 	return 0;
    232   1.1  christos }
    233   1.1  christos 
    234   1.1  christos /*ARGSUSED*/
    235   1.1  christos struct fetch_connect *
    236   1.1  christos fetch_open(const char *fname, const char *fmode)
    237   1.1  christos {
    238   1.1  christos 	struct fetch_connect *conn;
    239   1.1  christos 	int fd;
    240   1.1  christos 
    241   1.1  christos 	fd = open(fname, O_RDONLY); /* XXX: fmode */
    242   1.1  christos 	if (fd < 0)
    243   1.1  christos 		return NULL;
    244   1.1  christos 
    245   1.1  christos 	if ((conn = calloc(1, sizeof(*conn))) == NULL) {
    246   1.1  christos 		close(fd);
    247   1.1  christos 		return NULL;
    248   1.1  christos 	}
    249   1.1  christos 
    250   1.1  christos 	conn->sd = fd;
    251   1.1  christos 	conn->issock = 0;
    252   1.1  christos 	return conn;
    253   1.1  christos }
    254   1.1  christos 
    255   1.1  christos /*ARGSUSED*/
    256   1.1  christos struct fetch_connect *
    257   1.1  christos fetch_fdopen(int sd, const char *fmode)
    258   1.1  christos {
    259   1.1  christos 	struct fetch_connect *conn;
    260   1.2  christos #if defined(SO_NOSIGPIPE) || defined(TCP_NOPUSH)
    261   1.1  christos 	int opt = 1;
    262   1.2  christos #endif
    263   1.1  christos 
    264   1.1  christos 	if ((conn = calloc(1, sizeof(*conn))) == NULL)
    265   1.1  christos 		return NULL;
    266   1.1  christos 
    267   1.1  christos 	conn->sd = sd;
    268   1.1  christos 	conn->issock = 1;
    269   1.1  christos 	fcntl(sd, F_SETFD, FD_CLOEXEC);
    270   1.2  christos #ifdef SO_NOSIGPIPE
    271   1.1  christos 	setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
    272   1.2  christos #endif
    273   1.1  christos #ifdef TCP_NOPUSH
    274   1.1  christos 	setsockopt(sd, IPPROTO_TCP, TCP_NOPUSH, &opt, sizeof(opt));
    275   1.1  christos #endif
    276   1.1  christos 	return conn;
    277   1.1  christos }
    278   1.1  christos 
    279   1.1  christos int
    280   1.1  christos fetch_close(struct fetch_connect *conn)
    281   1.1  christos {
    282   1.8  christos 	if (conn == NULL)
    283   1.8  christos 		return 0;
    284   1.1  christos 
    285   1.8  christos 	fetch_flush(conn);
    286  1.10     lukem #ifdef WITH_SSL
    287   1.8  christos 	SSL_free(conn->ssl);
    288  1.10     lukem #endif
    289   1.8  christos 	close(conn->sd);
    290   1.8  christos 	free(conn->cache.buf);
    291   1.8  christos 	free(conn->buf);
    292   1.8  christos 	free(conn);
    293   1.8  christos 	return 0;
    294   1.1  christos }
    295   1.1  christos 
    296   1.8  christos #define FETCH_WRITE_WAIT	-3
    297   1.1  christos #define FETCH_READ_WAIT		-2
    298   1.1  christos #define FETCH_READ_ERROR	-1
    299   1.1  christos 
    300  1.10     lukem #ifdef WITH_SSL
    301   1.1  christos static ssize_t
    302   1.1  christos fetch_ssl_read(SSL *ssl, void *buf, size_t len)
    303   1.1  christos {
    304   1.1  christos 	ssize_t rlen;
    305   1.8  christos 	rlen = SSL_read(ssl, buf, len);
    306   1.8  christos 	if (rlen >= 0)
    307   1.8  christos 		return rlen;
    308   1.1  christos 
    309   1.8  christos 	switch (SSL_get_error(ssl, rlen)) {
    310   1.8  christos 	case SSL_ERROR_WANT_READ:
    311   1.8  christos 		return FETCH_READ_WAIT;
    312   1.8  christos 	case SSL_ERROR_WANT_WRITE:
    313   1.8  christos 		return FETCH_WRITE_WAIT;
    314   1.8  christos 	default:
    315   1.1  christos 		ERR_print_errors_fp(ttyout);
    316   1.1  christos 		return FETCH_READ_ERROR;
    317   1.1  christos 	}
    318   1.1  christos }
    319  1.10     lukem #endif /* WITH_SSL */
    320   1.1  christos 
    321   1.1  christos static ssize_t
    322   1.1  christos fetch_nonssl_read(int sd, void *buf, size_t len)
    323   1.1  christos {
    324   1.1  christos 	ssize_t rlen;
    325   1.1  christos 
    326   1.1  christos 	rlen = read(sd, buf, len);
    327   1.8  christos 	if (rlen == -1) {
    328  1.15     lukem 		if (errno == EINTR || errno == EAGAIN)
    329   1.1  christos 			return FETCH_READ_WAIT;
    330   1.1  christos 		return FETCH_READ_ERROR;
    331   1.1  christos 	}
    332   1.1  christos 	return rlen;
    333   1.1  christos }
    334   1.1  christos 
    335   1.1  christos /*
    336   1.1  christos  * Cache some data that was read from a socket but cannot be immediately
    337   1.1  christos  * returned because of an interrupted system call.
    338   1.1  christos  */
    339   1.1  christos static int
    340   1.1  christos fetch_cache_data(struct fetch_connect *conn, char *src, size_t nbytes)
    341   1.1  christos {
    342   1.1  christos 
    343   1.1  christos 	if (conn->cache.size < nbytes) {
    344   1.1  christos 		char *tmp = realloc(conn->cache.buf, nbytes);
    345   1.1  christos 		if (tmp == NULL)
    346   1.1  christos 			return -1;
    347   1.1  christos 
    348   1.1  christos 		conn->cache.buf = tmp;
    349   1.1  christos 		conn->cache.size = nbytes;
    350   1.1  christos 	}
    351   1.1  christos 
    352   1.1  christos 	memcpy(conn->cache.buf, src, nbytes);
    353   1.1  christos 	conn->cache.len = nbytes;
    354   1.1  christos 	conn->cache.pos = 0;
    355   1.1  christos 	return 0;
    356   1.1  christos }
    357   1.1  christos 
    358   1.8  christos static int
    359   1.8  christos fetch_wait(struct fetch_connect *conn, ssize_t rlen, struct timeval *timeout)
    360   1.8  christos {
    361   1.8  christos 	struct timeval now, delta;
    362   1.8  christos 	int fd = conn->sd;
    363  1.15     lukem 	int rv, timeout_secs;
    364  1.15     lukem 	struct pollfd pfd[1];
    365  1.15     lukem 
    366  1.15     lukem 	pfd[0].fd = fd;
    367  1.15     lukem 	if (rlen == FETCH_READ_WAIT) {
    368  1.15     lukem 		pfd[0].events = POLLIN;
    369  1.15     lukem 	} else if (rlen == FETCH_WRITE_WAIT) {
    370  1.15     lukem 		pfd[0].events = POLLOUT;
    371  1.15     lukem 	} else {
    372  1.15     lukem 		pfd[0].events = 0;
    373  1.15     lukem 	}
    374   1.8  christos 
    375  1.15     lukem 	do {
    376   1.8  christos 		if (quit_time > 0) {
    377   1.8  christos 			gettimeofday(&now, NULL);
    378   1.8  christos 			timersub(timeout, &now, &delta);
    379  1.15     lukem 			timeout_secs = delta.tv_sec * 1000 + delta.tv_usec/1000;
    380  1.15     lukem 			if (timeout_secs < 0)
    381  1.15     lukem 				timeout_secs = 0;
    382  1.15     lukem 		} else {
    383  1.15     lukem 			timeout_secs = INFTIM;
    384   1.8  christos 		}
    385   1.8  christos 		errno = 0;
    386  1.15     lukem 		rv = ftp_poll(pfd, 1, timeout_secs);
    387  1.15     lukem 				/* loop until poll !EINTR && !EAGAIN */
    388  1.15     lukem 	} while (rv == -1 && (errno == EINTR || errno == EAGAIN));
    389  1.15     lukem 	if (rv == 0) {		/* poll timeout */
    390  1.15     lukem 		fprintf(ttyout, "\r\n%s: transfer aborted"
    391  1.15     lukem 		    " because stalled for %lu sec.\r\n",
    392  1.15     lukem 		    getprogname(), (unsigned long)quit_time);
    393  1.15     lukem 		errno = ETIMEDOUT;
    394  1.15     lukem 		conn->iserr = ETIMEDOUT;
    395  1.15     lukem 		return -1;
    396  1.15     lukem 	}
    397  1.15     lukem 	if (rv == -1) {		/* poll error */
    398  1.15     lukem 		conn->iserr = errno;
    399  1.15     lukem 		return -1;
    400   1.8  christos 	}
    401   1.8  christos 	return 0;
    402   1.8  christos }
    403   1.8  christos 
    404   1.7  christos size_t
    405   1.1  christos fetch_read(void *ptr, size_t size, size_t nmemb, struct fetch_connect *conn)
    406   1.1  christos {
    407   1.1  christos 	ssize_t rlen, total;
    408   1.1  christos 	size_t len;
    409   1.1  christos 	char *start, *buf;
    410   1.8  christos 	struct timeval timeout;
    411   1.1  christos 
    412   1.1  christos 	if (quit_time > 0) {
    413   1.1  christos 		gettimeofday(&timeout, NULL);
    414   1.1  christos 		timeout.tv_sec += quit_time;
    415   1.1  christos 	}
    416   1.1  christos 
    417   1.1  christos 	total = 0;
    418   1.1  christos 	start = buf = ptr;
    419   1.1  christos 	len = size * nmemb;
    420   1.1  christos 
    421   1.1  christos 	if (conn->cache.len > 0) {
    422   1.1  christos 		/*
    423   1.1  christos 		 * The last invocation of fetch_read was interrupted by a
    424   1.1  christos 		 * signal after some data had been read from the socket. Copy
    425   1.1  christos 		 * the cached data into the supplied buffer before trying to
    426   1.1  christos 		 * read from the socket again.
    427   1.1  christos 		 */
    428   1.1  christos 		total = (conn->cache.len < len) ? conn->cache.len : len;
    429   1.1  christos 		memcpy(buf, conn->cache.buf, total);
    430   1.1  christos 
    431   1.1  christos 		conn->cache.len -= total;
    432   1.1  christos 		conn->cache.pos += total;
    433   1.1  christos 		len -= total;
    434   1.1  christos 		buf += total;
    435   1.1  christos 	}
    436   1.1  christos 
    437   1.1  christos 	while (len > 0) {
    438   1.1  christos 		/*
    439   1.1  christos 		 * The socket is non-blocking.  Instead of the canonical
    440  1.15     lukem 		 * poll() -> read(), we do the following:
    441   1.1  christos 		 *
    442   1.1  christos 		 * 1) call read() or SSL_read().
    443   1.1  christos 		 * 2) if an error occurred, return -1.
    444   1.1  christos 		 * 3) if we received data but we still expect more,
    445   1.1  christos 		 *    update our counters and loop.
    446   1.1  christos 		 * 4) if read() or SSL_read() signaled EOF, return.
    447   1.1  christos 		 * 5) if we did not receive any data but we're not at EOF,
    448  1.15     lukem 		 *    call poll().
    449   1.1  christos 		 *
    450   1.1  christos 		 * In the SSL case, this is necessary because if we
    451   1.1  christos 		 * receive a close notification, we have to call
    452   1.1  christos 		 * SSL_read() one additional time after we've read
    453   1.1  christos 		 * everything we received.
    454   1.1  christos 		 *
    455   1.1  christos 		 * In the non-SSL case, it may improve performance (very
    456   1.1  christos 		 * slightly) when reading small amounts of data.
    457   1.1  christos 		 */
    458  1.10     lukem #ifdef WITH_SSL
    459   1.1  christos 		if (conn->ssl != NULL)
    460   1.1  christos 			rlen = fetch_ssl_read(conn->ssl, buf, len);
    461   1.1  christos 		else
    462  1.10     lukem #endif
    463   1.1  christos 			rlen = fetch_nonssl_read(conn->sd, buf, len);
    464   1.8  christos 		switch (rlen) {
    465   1.8  christos 		case 0:
    466   1.7  christos 			conn->iseof = 1;
    467   1.8  christos 			return total;
    468   1.8  christos 		case FETCH_READ_ERROR:
    469   1.7  christos 			conn->iserr = errno;
    470  1.15     lukem 			if (errno == EINTR || errno == EAGAIN)
    471   1.1  christos 				fetch_cache_data(conn, start, total);
    472   1.7  christos 			return 0;
    473   1.8  christos 		case FETCH_READ_WAIT:
    474   1.8  christos 		case FETCH_WRITE_WAIT:
    475   1.8  christos 			if (fetch_wait(conn, rlen, &timeout) == -1)
    476   1.7  christos 				return 0;
    477   1.8  christos 			break;
    478   1.8  christos 		default:
    479   1.8  christos 			len -= rlen;
    480   1.8  christos 			buf += rlen;
    481   1.8  christos 			total += rlen;
    482   1.8  christos 			break;
    483   1.1  christos 		}
    484   1.1  christos 	}
    485   1.1  christos 	return total;
    486   1.1  christos }
    487   1.1  christos 
    488   1.1  christos #define MIN_BUF_SIZE 1024
    489   1.1  christos 
    490   1.1  christos /*
    491   1.1  christos  * Read a line of text from a connection w/ timeout
    492   1.1  christos  */
    493   1.1  christos char *
    494   1.1  christos fetch_getln(char *str, int size, struct fetch_connect *conn)
    495   1.1  christos {
    496   1.1  christos 	size_t tmpsize;
    497   1.7  christos 	size_t len;
    498   1.1  christos 	char c;
    499   1.1  christos 
    500   1.1  christos 	if (conn->buf == NULL) {
    501   1.1  christos 		if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) {
    502   1.1  christos 			errno = ENOMEM;
    503   1.1  christos 			conn->iserr = 1;
    504   1.1  christos 			return NULL;
    505   1.1  christos 		}
    506   1.1  christos 		conn->bufsize = MIN_BUF_SIZE;
    507   1.1  christos 	}
    508   1.1  christos 
    509   1.1  christos 	if (conn->iserr || conn->iseof)
    510   1.1  christos 		return NULL;
    511   1.1  christos 
    512   1.1  christos 	if (conn->buflen - conn->bufpos > 0)
    513   1.1  christos 		goto done;
    514   1.1  christos 
    515   1.1  christos 	conn->buf[0] = '\0';
    516   1.1  christos 	conn->bufpos = 0;
    517   1.1  christos 	conn->buflen = 0;
    518   1.1  christos 	do {
    519   1.1  christos 		len = fetch_read(&c, sizeof(c), 1, conn);
    520   1.1  christos 		if (len == 0) {
    521   1.7  christos 			if (conn->iserr)
    522   1.7  christos 				return NULL;
    523   1.7  christos 			if (conn->iseof)
    524   1.7  christos 				break;
    525   1.7  christos 			abort();
    526   1.1  christos 		}
    527   1.1  christos 		conn->buf[conn->buflen++] = c;
    528   1.1  christos 		if (conn->buflen == conn->bufsize) {
    529   1.1  christos 			char *tmp = conn->buf;
    530   1.1  christos 			tmpsize = conn->bufsize * 2 + 1;
    531   1.1  christos 			if ((tmp = realloc(tmp, tmpsize)) == NULL) {
    532   1.1  christos 				errno = ENOMEM;
    533   1.1  christos 				conn->iserr = 1;
    534   1.1  christos 				return NULL;
    535   1.1  christos 			}
    536   1.1  christos 			conn->buf = tmp;
    537   1.1  christos 			conn->bufsize = tmpsize;
    538   1.1  christos 		}
    539   1.1  christos 	} while (c != '\n');
    540   1.1  christos 
    541   1.1  christos 	if (conn->buflen == 0)
    542   1.1  christos 		return NULL;
    543   1.1  christos  done:
    544   1.1  christos 	tmpsize = MIN(size - 1, (int)(conn->buflen - conn->bufpos));
    545   1.1  christos 	memcpy(str, conn->buf + conn->bufpos, tmpsize);
    546   1.1  christos 	str[tmpsize] = '\0';
    547   1.1  christos 	conn->bufpos += tmpsize;
    548   1.1  christos 	return str;
    549   1.1  christos }
    550   1.1  christos 
    551   1.1  christos int
    552   1.1  christos fetch_getline(struct fetch_connect *conn, char *buf, size_t buflen,
    553   1.1  christos     const char **errormsg)
    554   1.1  christos {
    555   1.1  christos 	size_t len;
    556   1.1  christos 	int rv;
    557   1.1  christos 
    558   1.1  christos 	if (fetch_getln(buf, buflen, conn) == NULL) {
    559   1.1  christos 		if (conn->iseof) {	/* EOF */
    560   1.1  christos 			rv = -2;
    561   1.1  christos 			if (errormsg)
    562   1.1  christos 				*errormsg = "\nEOF received";
    563   1.1  christos 		} else {		/* error */
    564   1.1  christos 			rv = -1;
    565   1.1  christos 			if (errormsg)
    566   1.1  christos 				*errormsg = "Error encountered";
    567   1.1  christos 		}
    568   1.1  christos 		fetch_clearerr(conn);
    569   1.1  christos 		return rv;
    570   1.1  christos 	}
    571   1.1  christos 	len = strlen(buf);
    572   1.1  christos 	if (buf[len - 1] == '\n') {	/* clear any trailing newline */
    573   1.1  christos 		buf[--len] = '\0';
    574   1.1  christos 	} else if (len == buflen - 1) {	/* line too long */
    575   1.1  christos 		while (1) {
    576   1.1  christos 			char c;
    577   1.7  christos 			size_t rlen = fetch_read(&c, sizeof(c), 1, conn);
    578   1.7  christos 			if (rlen == 0 || c == '\n')
    579   1.1  christos 				break;
    580   1.1  christos 		}
    581   1.1  christos 		if (errormsg)
    582   1.1  christos 			*errormsg = "Input line is too long";
    583   1.1  christos 		fetch_clearerr(conn);
    584   1.1  christos 		return -3;
    585   1.1  christos 	}
    586   1.1  christos 	if (errormsg)
    587   1.1  christos 		*errormsg = NULL;
    588   1.1  christos 	return len;
    589   1.1  christos }
    590   1.1  christos 
    591  1.10     lukem #ifdef WITH_SSL
    592  1.15     lukem /*
    593  1.15     lukem  * Start the SSL/TLS negotiation.
    594  1.15     lukem  * Socket fcntl flags are temporarily updated to include O_NONBLOCK;
    595  1.15     lukem  * these will not be reverted on connection failure.
    596  1.15     lukem  * Returns pointer to allocated SSL structure on success,
    597  1.15     lukem  * or NULL upon failure.
    598  1.15     lukem  */
    599   1.1  christos void *
    600   1.3       wiz fetch_start_ssl(int sock, const char *servername)
    601   1.1  christos {
    602  1.15     lukem 	SSL *ssl = NULL;
    603  1.15     lukem 	SSL_CTX *ctx = NULL;
    604  1.11  christos 	X509_VERIFY_PARAM *param;
    605  1.15     lukem 	int ret, ssl_err, flags, rv, timeout_secs;
    606  1.13   mlelstv 	int verify = !ftp_truthy("sslnoverify", getoptionvalue("sslnoverify"), 0);
    607  1.15     lukem 	struct timeval timeout, now, delta;
    608  1.15     lukem 	struct pollfd pfd[1];
    609   1.1  christos 
    610   1.1  christos 	/* Init the SSL library and context */
    611   1.1  christos 	if (!SSL_library_init()){
    612  1.15     lukem 		warnx("SSL library init failed");
    613  1.15     lukem 		goto cleanup_start_ssl;
    614   1.1  christos 	}
    615   1.1  christos 
    616   1.1  christos 	SSL_load_error_strings();
    617   1.1  christos 
    618   1.1  christos 	ctx = SSL_CTX_new(SSLv23_client_method());
    619   1.1  christos 	SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
    620  1.11  christos 	if (verify) {
    621  1.11  christos 		SSL_CTX_set_default_verify_paths(ctx);
    622  1.11  christos 		SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
    623  1.11  christos 	}
    624   1.1  christos 
    625   1.1  christos 	ssl = SSL_new(ctx);
    626   1.1  christos 	if (ssl == NULL){
    627  1.15     lukem 		warnx("SSL context creation failed");
    628  1.15     lukem 		goto cleanup_start_ssl;
    629   1.1  christos 	}
    630  1.11  christos 
    631  1.11  christos 	if (verify) {
    632  1.11  christos 		param = SSL_get0_param(ssl);
    633  1.11  christos 		if (!X509_VERIFY_PARAM_set1_host(param, servername,
    634  1.11  christos 		    strlen(servername))) {
    635  1.15     lukem 			warnx("SSL verification setup failed");
    636  1.15     lukem 			goto cleanup_start_ssl;
    637  1.11  christos 		}
    638  1.11  christos 
    639  1.11  christos 		/* Enable peer verification, (using the default callback) */
    640  1.11  christos 		SSL_set_verify(ssl, SSL_VERIFY_PEER, NULL);
    641  1.11  christos 	}
    642  1.11  christos 
    643  1.15     lukem 						/* save current socket flags */
    644  1.15     lukem 	if ((flags = fcntl(sock, F_GETFL, 0)) == -1) {
    645  1.15     lukem 		warn("Can't %s socket flags for SSL connect to `%s'",
    646  1.15     lukem 		    "save", servername);
    647  1.15     lukem 		goto cleanup_start_ssl;
    648  1.15     lukem 	}
    649  1.15     lukem 						/* set non-blocking connect */
    650  1.15     lukem 	if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1) {
    651  1.15     lukem 		warn("Can't set socket non-blocking for SSL connect to `%s'",
    652  1.15     lukem 		    servername);
    653  1.15     lukem 		goto cleanup_start_ssl;
    654  1.15     lukem 	}
    655  1.15     lukem 
    656  1.15     lukem 	/* NOTE: we now must restore socket flags on successful connection */
    657  1.15     lukem 
    658  1.15     lukem 	(void)gettimeofday(&timeout, NULL);	/* setup SSL_connect() timeout */
    659  1.15     lukem 	timeout.tv_sec += (quit_time > 0) ? quit_time: 60;
    660  1.15     lukem 						/* without -q, default to 60s */
    661  1.15     lukem 
    662   1.1  christos 	SSL_set_fd(ssl, sock);
    663   1.5     joerg 	if (!SSL_set_tlsext_host_name(ssl, __UNCONST(servername))) {
    664  1.15     lukem 		warnx("SSL hostname setting failed");
    665  1.15     lukem 		goto cleanup_start_ssl;
    666   1.3       wiz 	}
    667  1.15     lukem 	pfd[0].fd = sock;
    668  1.15     lukem 	pfd[0].events = 0;
    669  1.15     lukem 	while ((ret = SSL_connect(ssl)) <= 0) {
    670   1.1  christos 		ssl_err = SSL_get_error(ssl, ret);
    671  1.15     lukem 		DPRINTF("%s: SSL_connect() ret=%d ssl_err=%d\n",
    672  1.15     lukem 		    __func__, ret, ssl_err);
    673  1.15     lukem 		if (ret == 0) { /* unsuccessful handshake */
    674  1.15     lukem 			ERR_print_errors_fp(ttyout);
    675  1.15     lukem 			goto cleanup_start_ssl;
    676  1.15     lukem 		}
    677  1.15     lukem 		if (ssl_err == SSL_ERROR_WANT_READ) {
    678  1.15     lukem 			pfd[0].events = POLLIN;
    679  1.15     lukem 		} else if (ssl_err == SSL_ERROR_WANT_WRITE) {
    680  1.15     lukem 			pfd[0].events = POLLOUT;
    681  1.15     lukem 		} else {
    682   1.1  christos 			ERR_print_errors_fp(ttyout);
    683  1.15     lukem 			goto cleanup_start_ssl;
    684  1.15     lukem 		}
    685  1.15     lukem 		(void)gettimeofday(&now, NULL);
    686  1.15     lukem 		timersub(&timeout, &now, &delta);
    687  1.15     lukem 		timeout_secs = delta.tv_sec * 1000 + delta.tv_usec/1000;
    688  1.15     lukem 		if (timeout_secs < 0)
    689  1.15     lukem 			timeout_secs = 0;
    690  1.15     lukem 		rv = ftp_poll(pfd, 1, timeout_secs);
    691  1.15     lukem 		if (rv == 0) {		/* poll for SSL_connect() timed out */
    692  1.15     lukem 			fprintf(ttyout, "Timeout establishing SSL connection to `%s'\n",
    693  1.15     lukem 			    servername);
    694  1.15     lukem 			goto cleanup_start_ssl;
    695  1.15     lukem 		} else if (rv == -1 && errno != EINTR && errno != EAGAIN) {
    696  1.15     lukem 			warn("Error polling for SSL connect to `%s'", servername);
    697  1.15     lukem 			goto cleanup_start_ssl;
    698   1.1  christos 		}
    699   1.1  christos 	}
    700   1.1  christos 
    701  1.15     lukem 	if (fcntl(sock, F_SETFL, flags) == -1) {
    702  1.15     lukem 						/* restore socket flags */
    703  1.15     lukem 		warn("Can't %s socket flags for SSL connect to `%s'",
    704  1.15     lukem 		    "restore", servername);
    705  1.15     lukem 		goto cleanup_start_ssl;
    706  1.15     lukem 	}
    707  1.15     lukem 
    708   1.1  christos 	if (ftp_debug && verbose) {
    709   1.1  christos 		X509 *cert;
    710   1.1  christos 		X509_NAME *name;
    711   1.1  christos 		char *str;
    712   1.1  christos 
    713   1.1  christos 		fprintf(ttyout, "SSL connection established using %s\n",
    714   1.1  christos 		    SSL_get_cipher(ssl));
    715   1.1  christos 		cert = SSL_get_peer_certificate(ssl);
    716   1.1  christos 		name = X509_get_subject_name(cert);
    717   1.1  christos 		str = X509_NAME_oneline(name, 0, 0);
    718   1.1  christos 		fprintf(ttyout, "Certificate subject: %s\n", str);
    719   1.1  christos 		free(str);
    720   1.1  christos 		name = X509_get_issuer_name(cert);
    721   1.1  christos 		str = X509_NAME_oneline(name, 0, 0);
    722   1.1  christos 		fprintf(ttyout, "Certificate issuer: %s\n", str);
    723   1.1  christos 		free(str);
    724   1.1  christos 	}
    725   1.1  christos 
    726   1.1  christos 	return ssl;
    727  1.15     lukem 
    728  1.15     lukem  cleanup_start_ssl:
    729  1.15     lukem 	if (ssl)
    730  1.15     lukem 		SSL_free(ssl);
    731  1.15     lukem 	if (ctx)
    732  1.15     lukem 		SSL_CTX_free(ctx);
    733  1.15     lukem 	return NULL;
    734   1.1  christos }
    735  1.10     lukem #endif /* WITH_SSL */
    736   1.1  christos 
    737   1.1  christos 
    738   1.1  christos void
    739   1.1  christos fetch_set_ssl(struct fetch_connect *conn, void *ssl)
    740   1.1  christos {
    741  1.10     lukem #ifdef WITH_SSL
    742   1.1  christos 	conn->ssl = ssl;
    743  1.10     lukem #endif
    744   1.1  christos }
    745