xref: /netbsd/external/bsd/fetch/dist/libfetch/common.c (revision 59ddf36f)
1 /*	$NetBSD: common.c,v 1.4 2023/01/24 08:02:57 mlelstv Exp $	*/
2 /*-
3  * Copyright (c) 1998-2004 Dag-Erling Co�dan Sm�rgrav
4  * Copyright (c) 2008, 2010 Joerg Sonnenberger <joerg@NetBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD: common.c,v 1.53 2007/12/19 00:26:36 des Exp $
31  */
32 
33 #if HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 #ifndef NETBSD
37 #include <nbcompat.h>
38 #endif
39 
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/time.h>
43 #include <sys/uio.h>
44 
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 
48 #include <ctype.h>
49 #include <errno.h>
50 #if defined(HAVE_INTTYPES_H) || defined(NETBSD)
51 #include <inttypes.h>
52 #endif
53 #ifndef NETBSD
54 #include <nbcompat/netdb.h>
55 #else
56 #include <netdb.h>
57 #endif
58 #include <pwd.h>
59 #include <stdarg.h>
60 #include <stdlib.h>
61 #include <stdio.h>
62 #include <string.h>
63 #include <unistd.h>
64 
65 #ifndef MSG_NOSIGNAL
66 #include <signal.h>
67 #endif
68 
69 #include "fetch.h"
70 #include "common.h"
71 
72 /*** Local data **************************************************************/
73 
74 /*
75  * Error messages for resolver errors
76  */
77 static struct fetcherr netdb_errlist[] = {
78 #ifdef EAI_NODATA
79 	{ EAI_NODATA,	FETCH_RESOLV,	"Host not found" },
80 #endif
81 	{ EAI_AGAIN,	FETCH_TEMP,	"Transient resolver failure" },
82 	{ EAI_FAIL,	FETCH_RESOLV,	"Non-recoverable resolver failure" },
83 	{ EAI_NONAME,	FETCH_RESOLV,	"No address record" },
84 	{ -1,		FETCH_UNKNOWN,	"Unknown resolver error" }
85 };
86 
87 /*** Error-reporting functions ***********************************************/
88 
89 /*
90  * Map error code to string
91  */
92 static struct fetcherr *
fetch_finderr(struct fetcherr * p,int e)93 fetch_finderr(struct fetcherr *p, int e)
94 {
95 	while (p->num != -1 && p->num != e)
96 		p++;
97 	return (p);
98 }
99 
100 /*
101  * Set error code
102  */
103 void
fetch_seterr(struct fetcherr * p,int e)104 fetch_seterr(struct fetcherr *p, int e)
105 {
106 	p = fetch_finderr(p, e);
107 	fetchLastErrCode = p->cat;
108 	snprintf(fetchLastErrString, MAXERRSTRING, "%s", p->string);
109 }
110 
111 /*
112  * Set error code according to errno
113  */
114 void
fetch_syserr(void)115 fetch_syserr(void)
116 {
117 	switch (errno) {
118 	case 0:
119 		fetchLastErrCode = FETCH_OK;
120 		break;
121 	case EPERM:
122 	case EACCES:
123 	case EROFS:
124 #ifdef EAUTH
125 	case EAUTH:
126 #endif
127 #ifdef ENEEDAUTH
128 	case ENEEDAUTH:
129 #endif
130 		fetchLastErrCode = FETCH_AUTH;
131 		break;
132 	case ENOENT:
133 	case EISDIR: /* XXX */
134 		fetchLastErrCode = FETCH_UNAVAIL;
135 		break;
136 	case ENOMEM:
137 		fetchLastErrCode = FETCH_MEMORY;
138 		break;
139 	case EBUSY:
140 	case EAGAIN:
141 		fetchLastErrCode = FETCH_TEMP;
142 		break;
143 	case EEXIST:
144 		fetchLastErrCode = FETCH_EXISTS;
145 		break;
146 	case ENOSPC:
147 		fetchLastErrCode = FETCH_FULL;
148 		break;
149 	case EADDRINUSE:
150 	case EADDRNOTAVAIL:
151 	case ENETDOWN:
152 	case ENETUNREACH:
153 	case ENETRESET:
154 	case EHOSTUNREACH:
155 		fetchLastErrCode = FETCH_NETWORK;
156 		break;
157 	case ECONNABORTED:
158 	case ECONNRESET:
159 		fetchLastErrCode = FETCH_ABORT;
160 		break;
161 	case ETIMEDOUT:
162 		fetchLastErrCode = FETCH_TIMEOUT;
163 		break;
164 	case ECONNREFUSED:
165 	case EHOSTDOWN:
166 		fetchLastErrCode = FETCH_DOWN;
167 		break;
168 default:
169 		fetchLastErrCode = FETCH_UNKNOWN;
170 	}
171 	snprintf(fetchLastErrString, MAXERRSTRING, "%s", strerror(errno));
172 }
173 
174 
175 /*
176  * Emit status message
177  */
178 void
fetch_info(const char * fmt,...)179 fetch_info(const char *fmt, ...)
180 {
181 	va_list ap;
182 
183 	va_start(ap, fmt);
184 	vfprintf(stderr, fmt, ap);
185 	va_end(ap);
186 	fputc('\n', stderr);
187 }
188 
189 
190 /*** Network-related utility functions ***************************************/
191 
192 /*
193  * Return the default port for a scheme
194  */
195 int
fetch_default_port(const char * scheme)196 fetch_default_port(const char *scheme)
197 {
198 	struct servent *se;
199 
200 	if ((se = getservbyname(scheme, "tcp")) != NULL)
201 		return (ntohs(se->s_port));
202 	if (strcasecmp(scheme, SCHEME_FTP) == 0)
203 		return (FTP_DEFAULT_PORT);
204 	if (strcasecmp(scheme, SCHEME_HTTP) == 0)
205 		return (HTTP_DEFAULT_PORT);
206 	return (0);
207 }
208 
209 /*
210  * Return the default proxy port for a scheme
211  */
212 int
fetch_default_proxy_port(const char * scheme)213 fetch_default_proxy_port(const char *scheme)
214 {
215 	if (strcasecmp(scheme, SCHEME_FTP) == 0)
216 		return (FTP_DEFAULT_PROXY_PORT);
217 	if (strcasecmp(scheme, SCHEME_HTTP) == 0)
218 		return (HTTP_DEFAULT_PROXY_PORT);
219 	return (0);
220 }
221 
222 
223 /*
224  * Create a connection for an existing descriptor.
225  */
226 conn_t *
fetch_reopen(int sd)227 fetch_reopen(int sd)
228 {
229 	conn_t *conn;
230 
231 	/* allocate and fill connection structure */
232 	if ((conn = calloc(1, sizeof(*conn))) == NULL)
233 		return (NULL);
234 	conn->ftp_home = NULL;
235 	conn->cache_url = NULL;
236 	conn->next_buf = NULL;
237 	conn->next_len = 0;
238 	conn->sd = sd;
239 	return (conn);
240 }
241 
242 
243 /*
244  * Bind a socket to a specific local address
245  */
246 int
fetch_bind(int sd,int af,const char * addr)247 fetch_bind(int sd, int af, const char *addr)
248 {
249 	struct addrinfo hints, *res, *res0;
250 
251 	memset(&hints, 0, sizeof(hints));
252 	hints.ai_family = af;
253 	hints.ai_socktype = SOCK_STREAM;
254 	hints.ai_protocol = 0;
255 	if (getaddrinfo(addr, NULL, &hints, &res0))
256 		return (-1);
257 	for (res = res0; res; res = res->ai_next) {
258 		if (bind(sd, res->ai_addr, res->ai_addrlen) == 0)
259 			return (0);
260 	}
261 	return (-1);
262 }
263 
264 
265 /*
266  * Establish a TCP connection to the specified port on the specified host.
267  */
268 conn_t *
fetch_connect(struct url * url,int af,int verbose)269 fetch_connect(struct url *url, int af, int verbose)
270 {
271 	conn_t *conn;
272 	char pbuf[10];
273 	const char *bindaddr;
274 	struct addrinfo hints, *res, *res0;
275 	int sd, error;
276 
277 	if (verbose)
278 		fetch_info("looking up %s", url->host);
279 
280 	/* look up host name and set up socket address structure */
281 	snprintf(pbuf, sizeof(pbuf), "%d", url->port);
282 	memset(&hints, 0, sizeof(hints));
283 	hints.ai_family = af;
284 	hints.ai_socktype = SOCK_STREAM;
285 	hints.ai_protocol = 0;
286 	if ((error = getaddrinfo(url->host, pbuf, &hints, &res0)) != 0) {
287 		netdb_seterr(error);
288 		return (NULL);
289 	}
290 	bindaddr = getenv("FETCH_BIND_ADDRESS");
291 
292 	if (verbose)
293 		fetch_info("connecting to %s:%d", url->host, url->port);
294 
295 	/* try to connect */
296 	for (sd = -1, res = res0; res; sd = -1, res = res->ai_next) {
297 		if ((sd = socket(res->ai_family, res->ai_socktype,
298 			 res->ai_protocol)) == -1)
299 			continue;
300 		if (bindaddr != NULL && *bindaddr != '\0' &&
301 		    fetch_bind(sd, res->ai_family, bindaddr) != 0) {
302 			fetch_info("failed to bind to '%s'", bindaddr);
303 			close(sd);
304 			continue;
305 		}
306 		if (connect(sd, res->ai_addr, res->ai_addrlen) == 0)
307 			break;
308 		close(sd);
309 	}
310 	freeaddrinfo(res0);
311 	if (sd == -1) {
312 		fetch_syserr();
313 		return (NULL);
314 	}
315 
316 	if ((conn = fetch_reopen(sd)) == NULL) {
317 		fetch_syserr();
318 		close(sd);
319 	}
320 	conn->cache_url = fetchCopyURL(url);
321 	conn->cache_af = af;
322 	return (conn);
323 }
324 
325 static conn_t *connection_cache;
326 static int cache_global_limit = 0;
327 static int cache_per_host_limit = 0;
328 
329 /*
330  * Initialise cache with the given limits.
331  */
332 void
fetchConnectionCacheInit(int global_limit,int per_host_limit)333 fetchConnectionCacheInit(int global_limit, int per_host_limit)
334 {
335 
336 	if (global_limit < 0)
337 		cache_global_limit = INT_MAX;
338 	else if (per_host_limit > global_limit)
339 		cache_global_limit = per_host_limit;
340 	else
341 		cache_global_limit = global_limit;
342 	if (per_host_limit < 0)
343 		cache_per_host_limit = INT_MAX;
344 	else
345 		cache_per_host_limit = per_host_limit;
346 }
347 
348 /*
349  * Flush cache and free all associated resources.
350  */
351 void
fetchConnectionCacheClose(void)352 fetchConnectionCacheClose(void)
353 {
354 	conn_t *conn;
355 
356 	while ((conn = connection_cache) != NULL) {
357 		connection_cache = conn->next_cached;
358 		(*conn->cache_close)(conn);
359 	}
360 }
361 
362 /*
363  * Check connection cache for an existing entry matching
364  * protocol/host/port/user/password/family.
365  */
366 conn_t *
fetch_cache_get(const struct url * url,int af)367 fetch_cache_get(const struct url *url, int af)
368 {
369 	conn_t *conn, *last_conn = NULL;
370 
371 	for (conn = connection_cache; conn; conn = conn->next_cached) {
372 		if (conn->cache_url->port == url->port &&
373 		    strcmp(conn->cache_url->scheme, url->scheme) == 0 &&
374 		    strcmp(conn->cache_url->host, url->host) == 0 &&
375 		    strcmp(conn->cache_url->user, url->user) == 0 &&
376 		    strcmp(conn->cache_url->pwd, url->pwd) == 0 &&
377 		    (conn->cache_af == AF_UNSPEC || af == AF_UNSPEC ||
378 		     conn->cache_af == af)) {
379 			if (last_conn != NULL)
380 				last_conn->next_cached = conn->next_cached;
381 			else
382 				connection_cache = conn->next_cached;
383 			return conn;
384 		}
385 	}
386 
387 	return NULL;
388 }
389 
390 /*
391  * Put the connection back into the cache for reuse.
392  * If the connection is freed due to LRU or if the cache
393  * is explicitly closed, the given callback is called.
394  */
395 void
fetch_cache_put(conn_t * conn,int (* closecb)(conn_t *))396 fetch_cache_put(conn_t *conn, int (*closecb)(conn_t *))
397 {
398 	conn_t *iter, *last;
399 	int global_count, host_count;
400 
401 	if (conn->cache_url == NULL || cache_global_limit == 0) {
402 		(*closecb)(conn);
403 		return;
404 	}
405 
406 	global_count = host_count = 0;
407 	last = NULL;
408 	for (iter = connection_cache; iter;
409 	    last = iter, iter = iter->next_cached) {
410 		++global_count;
411 		if (strcmp(conn->cache_url->host, iter->cache_url->host) == 0)
412 			++host_count;
413 		if (global_count < cache_global_limit &&
414 		    host_count < cache_per_host_limit)
415 			continue;
416 		--global_count;
417 		if (last != NULL)
418 			last->next_cached = iter->next_cached;
419 		else
420 			connection_cache = iter->next_cached;
421 		(*iter->cache_close)(iter);
422 	}
423 
424 	conn->cache_close = closecb;
425 	conn->next_cached = connection_cache;
426 	connection_cache = conn;
427 }
428 
429 /*
430  * Enable SSL on a connection.
431  */
432 int
fetch_ssl(conn_t * conn,int verbose)433 fetch_ssl(conn_t *conn, int verbose)
434 {
435 
436 #ifdef WITH_SSL
437 	/* Init the SSL library and context */
438 	if (!SSL_library_init()){
439 		fprintf(stderr, "SSL library init failed\n");
440 		return (-1);
441 	}
442 
443 	SSL_load_error_strings();
444 
445 	conn->ssl_meth = SSLv23_client_method();
446 	conn->ssl_ctx = SSL_CTX_new(conn->ssl_meth);
447 	SSL_CTX_set_mode(conn->ssl_ctx, SSL_MODE_AUTO_RETRY);
448 
449 	conn->ssl = SSL_new(conn->ssl_ctx);
450 	if (conn->ssl == NULL){
451 		fprintf(stderr, "SSL context creation failed\n");
452 		return (-1);
453 	}
454 	SSL_set_fd(conn->ssl, conn->sd);
455 	if (!SSL_set_tlsext_host_name(conn->ssl, conn->cache_url->host)) {
456 		fprintf(stderr, "SSL hostname setting failed\n");
457 		return (-1);
458 	}
459 	if (SSL_connect(conn->ssl) == -1){
460 		ERR_print_errors_fp(stderr);
461 		return (-1);
462 	}
463 
464 	if (verbose) {
465 		X509_NAME *name;
466 		char *str;
467 
468 		fprintf(stderr, "SSL connection established using %s\n",
469 		    SSL_get_cipher(conn->ssl));
470 		conn->ssl_cert = SSL_get_peer_certificate(conn->ssl);
471 		name = X509_get_subject_name(conn->ssl_cert);
472 		str = X509_NAME_oneline(name, 0, 0);
473 		printf("Certificate subject: %s\n", str);
474 		free(str);
475 		name = X509_get_issuer_name(conn->ssl_cert);
476 		str = X509_NAME_oneline(name, 0, 0);
477 		printf("Certificate issuer: %s\n", str);
478 		free(str);
479 	}
480 
481 	return (0);
482 #else
483 	(void)conn;
484 	(void)verbose;
485 	fprintf(stderr, "SSL support disabled\n");
486 	return (-1);
487 #endif
488 }
489 
490 
491 /*
492  * Read a character from a connection w/ timeout
493  */
494 ssize_t
fetch_read(conn_t * conn,char * buf,size_t len)495 fetch_read(conn_t *conn, char *buf, size_t len)
496 {
497 	struct timeval now, timeout, waittv;
498 	fd_set readfds;
499 	ssize_t rlen;
500 	int r;
501 
502 	if (len == 0)
503 		return 0;
504 
505 	if (conn->next_len != 0) {
506 		if (conn->next_len < len)
507 			len = conn->next_len;
508 		memmove(buf, conn->next_buf, len);
509 		conn->next_len -= len;
510 		conn->next_buf += len;
511 		return len;
512 	}
513 
514 	if (fetchTimeout) {
515 		FD_ZERO(&readfds);
516 		gettimeofday(&timeout, NULL);
517 		timeout.tv_sec += fetchTimeout;
518 	}
519 
520 	for (;;) {
521 		while (fetchTimeout && !FD_ISSET(conn->sd, &readfds)) {
522 			FD_SET(conn->sd, &readfds);
523 			gettimeofday(&now, NULL);
524 			waittv.tv_sec = timeout.tv_sec - now.tv_sec;
525 			waittv.tv_usec = timeout.tv_usec - now.tv_usec;
526 			if (waittv.tv_usec < 0) {
527 				waittv.tv_usec += 1000000;
528 				waittv.tv_sec--;
529 			}
530 			if (waittv.tv_sec < 0) {
531 				errno = ETIMEDOUT;
532 				fetch_syserr();
533 				return (-1);
534 			}
535 			errno = 0;
536 			r = select(conn->sd + 1, &readfds, NULL, NULL, &waittv);
537 			if (r == -1) {
538 				if (errno == EINTR && fetchRestartCalls)
539 					continue;
540 				fetch_syserr();
541 				return (-1);
542 			}
543 		}
544 #ifdef WITH_SSL
545 		if (conn->ssl != NULL)
546 			rlen = SSL_read(conn->ssl, buf, (int)len);
547 		else
548 #endif
549 			rlen = read(conn->sd, buf, len);
550 		if (rlen >= 0)
551 			break;
552 
553 		if (errno != EINTR || !fetchRestartCalls)
554 			return (-1);
555 	}
556 	return (rlen);
557 }
558 
559 
560 /*
561  * Read a line of text from a connection w/ timeout
562  */
563 #define MIN_BUF_SIZE 1024
564 
565 int
fetch_getln(conn_t * conn)566 fetch_getln(conn_t *conn)
567 {
568 	char *tmp, *next;
569 	size_t tmpsize;
570 	ssize_t len;
571 
572 	if (conn->buf == NULL) {
573 		if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) {
574 			errno = ENOMEM;
575 			return (-1);
576 		}
577 		conn->bufsize = MIN_BUF_SIZE;
578 	}
579 
580 	conn->buflen = 0;
581 	next = NULL;
582 
583 	do {
584 		/*
585 		 * conn->bufsize != conn->buflen at this point,
586 		 * so the buffer can be NUL-terminated below for
587 		 * the case of len == 0.
588 		 */
589 		len = fetch_read(conn, conn->buf + conn->buflen,
590 		    conn->bufsize - conn->buflen);
591 		if (len == -1)
592 			return (-1);
593 		if (len == 0)
594 			break;
595 		next = memchr(conn->buf + conn->buflen, '\n', (size_t)len);
596 		conn->buflen += len;
597 		if (conn->buflen == conn->bufsize && next == NULL) {
598 			tmp = conn->buf;
599 			tmpsize = conn->bufsize * 2;
600 			if (tmpsize < conn->bufsize) {
601 				errno = ENOMEM;
602 				return (-1);
603 			}
604 			if ((tmp = realloc(tmp, tmpsize)) == NULL) {
605 				errno = ENOMEM;
606 				return (-1);
607 			}
608 			conn->buf = tmp;
609 			conn->bufsize = tmpsize;
610 		}
611 	} while (next == NULL);
612 
613 	if (next != NULL) {
614 		*next = '\0';
615 		conn->next_buf = next + 1;
616 		conn->next_len = conn->buflen - (conn->next_buf - conn->buf);
617 		conn->buflen = next - conn->buf;
618 	} else {
619 		conn->buf[conn->buflen] = '\0';
620 		conn->next_len = 0;
621 	}
622 	return (0);
623 }
624 
625 /*
626  * Write a vector to a connection w/ timeout
627  * Note: can modify the iovec.
628  */
629 ssize_t
fetch_write(conn_t * conn,const void * buf,size_t len)630 fetch_write(conn_t *conn, const void *buf, size_t len)
631 {
632 	struct timeval now, timeout, waittv;
633 	fd_set writefds;
634 	ssize_t wlen, total;
635 	int r;
636 #ifndef MSG_NOSIGNAL
637 	static int killed_sigpipe;
638 #endif
639 
640 #ifndef MSG_NOSIGNAL
641 	if (!killed_sigpipe) {
642 		signal(SIGPIPE, SIG_IGN);
643 		killed_sigpipe = 1;
644 	}
645 #endif
646 
647 
648 	if (fetchTimeout) {
649 		FD_ZERO(&writefds);
650 		gettimeofday(&timeout, NULL);
651 		timeout.tv_sec += fetchTimeout;
652 	}
653 
654 	total = 0;
655 	while (len) {
656 		while (fetchTimeout && !FD_ISSET(conn->sd, &writefds)) {
657 			FD_SET(conn->sd, &writefds);
658 			gettimeofday(&now, NULL);
659 			waittv.tv_sec = timeout.tv_sec - now.tv_sec;
660 			waittv.tv_usec = timeout.tv_usec - now.tv_usec;
661 			if (waittv.tv_usec < 0) {
662 				waittv.tv_usec += 1000000;
663 				waittv.tv_sec--;
664 			}
665 			if (waittv.tv_sec < 0) {
666 				errno = ETIMEDOUT;
667 				fetch_syserr();
668 				return (-1);
669 			}
670 			errno = 0;
671 			r = select(conn->sd + 1, NULL, &writefds, NULL, &waittv);
672 			if (r == -1) {
673 				if (errno == EINTR && fetchRestartCalls)
674 					continue;
675 				return (-1);
676 			}
677 		}
678 		errno = 0;
679 #ifdef WITH_SSL
680 		if (conn->ssl != NULL)
681 			wlen = SSL_write(conn->ssl, buf, (int)len);
682 		else
683 #endif
684 #ifndef MSG_NOSIGNAL
685 			wlen = send(conn->sd, buf, len, 0);
686 #else
687 			wlen = send(conn->sd, buf, len, MSG_NOSIGNAL);
688 #endif
689 		if (wlen == 0) {
690 			/* we consider a short write a failure */
691 			errno = EPIPE;
692 			fetch_syserr();
693 			return (-1);
694 		}
695 		if (wlen < 0) {
696 			if (errno == EINTR && fetchRestartCalls)
697 				continue;
698 			return (-1);
699 		}
700 		total += wlen;
701 		buf = (const char *)buf + wlen;
702 		len -= wlen;
703 	}
704 	return (total);
705 }
706 
707 
708 /*
709  * Close connection
710  */
711 int
fetch_close(conn_t * conn)712 fetch_close(conn_t *conn)
713 {
714 	int ret;
715 
716 #ifdef WITH_SSL
717 	if (conn->ssl) {
718 		SSL_shutdown(conn->ssl);
719 		SSL_set_connect_state(conn->ssl);
720 		SSL_free(conn->ssl);
721 		conn->ssl = NULL;
722 	}
723 	if (conn->ssl_ctx) {
724 		SSL_CTX_free(conn->ssl_ctx);
725 		conn->ssl_ctx = NULL;
726 	}
727 	if (conn->ssl_cert) {
728 		X509_free(conn->ssl_cert);
729 		conn->ssl_cert = NULL;
730 	}
731 #endif
732 	ret = close(conn->sd);
733 	if (conn->cache_url)
734 		fetchFreeURL(conn->cache_url);
735 	free(conn->ftp_home);
736 	free(conn->buf);
737 	free(conn);
738 	return (ret);
739 }
740 
741 
742 /*** Directory-related utility functions *************************************/
743 
744 int
fetch_add_entry(struct url_list * ue,struct url * base,const char * name,int pre_quoted)745 fetch_add_entry(struct url_list *ue, struct url *base, const char *name,
746     int pre_quoted)
747 {
748 	struct url *tmp;
749 	char *tmp_name;
750 	size_t base_doc_len, name_len, i;
751 	unsigned char c;
752 
753 	if (strchr(name, '/') != NULL ||
754 	    strcmp(name, "..") == 0 ||
755 	    strcmp(name, ".") == 0)
756 		return 0;
757 
758 	if (strcmp(base->doc, "/") == 0)
759 		base_doc_len = 0;
760 	else
761 		base_doc_len = strlen(base->doc);
762 
763 	name_len = 1;
764 	for (i = 0; name[i] != '\0'; ++i) {
765 		if ((!pre_quoted && name[i] == '%') ||
766 		    !fetch_urlpath_safe(name[i]))
767 			name_len += 3;
768 		else
769 			++name_len;
770 	}
771 
772 	tmp_name = malloc( base_doc_len + name_len + 1);
773 	if (tmp_name == NULL) {
774 		errno = ENOMEM;
775 		fetch_syserr();
776 		return (-1);
777 	}
778 
779 	if (ue->length + 1 >= ue->alloc_size) {
780 		tmp = realloc(ue->urls, (ue->alloc_size * 2 + 1) * sizeof(*tmp));
781 		if (tmp == NULL) {
782 			free(tmp_name);
783 			errno = ENOMEM;
784 			fetch_syserr();
785 			return (-1);
786 		}
787 		ue->alloc_size = ue->alloc_size * 2 + 1;
788 		ue->urls = tmp;
789 	}
790 
791 	tmp = ue->urls + ue->length;
792 	strcpy(tmp->scheme, base->scheme);
793 	strcpy(tmp->user, base->user);
794 	strcpy(tmp->pwd, base->pwd);
795 	strcpy(tmp->host, base->host);
796 	tmp->port = base->port;
797 	tmp->doc = tmp_name;
798 	memcpy(tmp->doc, base->doc, base_doc_len);
799 	tmp->doc[base_doc_len] = '/';
800 
801 	for (i = base_doc_len + 1; *name != '\0'; ++name) {
802 		if ((!pre_quoted && *name == '%') ||
803 		    !fetch_urlpath_safe(*name)) {
804 			tmp->doc[i++] = '%';
805 			c = (unsigned char)*name / 16;
806 			if (c < 10)
807 				tmp->doc[i++] = '0' + c;
808 			else
809 				tmp->doc[i++] = 'a' - 10 + c;
810 			c = (unsigned char)*name % 16;
811 			if (c < 10)
812 				tmp->doc[i++] = '0' + c;
813 			else
814 				tmp->doc[i++] = 'a' - 10 + c;
815 		} else {
816 			tmp->doc[i++] = *name;
817 		}
818 	}
819 	tmp->doc[i] = '\0';
820 
821 	tmp->offset = 0;
822 	tmp->length = 0;
823 	tmp->last_modified = -1;
824 
825 	++ue->length;
826 
827 	return (0);
828 }
829 
830 void
fetchInitURLList(struct url_list * ue)831 fetchInitURLList(struct url_list *ue)
832 {
833 	ue->length = ue->alloc_size = 0;
834 	ue->urls = NULL;
835 }
836 
837 int
fetchAppendURLList(struct url_list * dst,const struct url_list * src)838 fetchAppendURLList(struct url_list *dst, const struct url_list *src)
839 {
840 	size_t i, j, len;
841 
842 	len = dst->length + src->length;
843 	if (len > dst->alloc_size) {
844 		struct url *tmp;
845 
846 		tmp = realloc(dst->urls, len * sizeof(*tmp));
847 		if (tmp == NULL) {
848 			errno = ENOMEM;
849 			fetch_syserr();
850 			return (-1);
851 		}
852 		dst->alloc_size = len;
853 		dst->urls = tmp;
854 	}
855 
856 	for (i = 0, j = dst->length; i < src->length; ++i, ++j) {
857 		dst->urls[j] = src->urls[i];
858 		dst->urls[j].doc = strdup(src->urls[i].doc);
859 		if (dst->urls[j].doc == NULL) {
860 			while (i-- > 0)
861 				free(dst->urls[j].doc);
862 			fetch_syserr();
863 			return -1;
864 		}
865 	}
866 	dst->length = len;
867 
868 	return 0;
869 }
870 
871 void
fetchFreeURLList(struct url_list * ue)872 fetchFreeURLList(struct url_list *ue)
873 {
874 	size_t i;
875 
876 	for (i = 0; i < ue->length; ++i)
877 		free(ue->urls[i].doc);
878 	free(ue->urls);
879 	ue->length = ue->alloc_size = 0;
880 }
881 
882 
883 /*** Authentication-related utility functions ********************************/
884 
885 static const char *
fetch_read_word(FILE * f)886 fetch_read_word(FILE *f)
887 {
888 	static char word[1024];
889 
890 	if (fscanf(f, " %1023s ", word) != 1)
891 		return (NULL);
892 	return (word);
893 }
894 
895 /*
896  * Get authentication data for a URL from .netrc
897  */
898 int
fetch_netrc_auth(struct url * url)899 fetch_netrc_auth(struct url *url)
900 {
901 	char fn[PATH_MAX];
902 	const char *word;
903 	char *p;
904 	FILE *f;
905 
906 	if ((p = getenv("NETRC")) != NULL) {
907 		if (snprintf(fn, sizeof(fn), "%s", p) >= (int)sizeof(fn)) {
908 			fetch_info("$NETRC specifies a file name "
909 			    "longer than PATH_MAX");
910 			return (-1);
911 		}
912 	} else {
913 		if ((p = getenv("HOME")) != NULL) {
914 			struct passwd *pwd;
915 
916 			if ((pwd = getpwuid(getuid())) == NULL ||
917 			    (p = pwd->pw_dir) == NULL)
918 				return (-1);
919 		}
920 		if (snprintf(fn, sizeof(fn), "%s/.netrc", p) >= (int)sizeof(fn))
921 			return (-1);
922 	}
923 
924 	if ((f = fopen(fn, "r")) == NULL)
925 		return (-1);
926 	while ((word = fetch_read_word(f)) != NULL) {
927 		if (strcmp(word, "default") == 0)
928 			break;
929 		if (strcmp(word, "machine") == 0 &&
930 		    (word = fetch_read_word(f)) != NULL &&
931 		    strcasecmp(word, url->host) == 0) {
932 			break;
933 		}
934 	}
935 	if (word == NULL)
936 		goto ferr;
937 	while ((word = fetch_read_word(f)) != NULL) {
938 		if (strcmp(word, "login") == 0) {
939 			if ((word = fetch_read_word(f)) == NULL)
940 				goto ferr;
941 			if (snprintf(url->user, sizeof(url->user),
942 				"%s", word) > (int)sizeof(url->user)) {
943 				fetch_info("login name in .netrc is too long");
944 				url->user[0] = '\0';
945 			}
946 		} else if (strcmp(word, "password") == 0) {
947 			if ((word = fetch_read_word(f)) == NULL)
948 				goto ferr;
949 			if (snprintf(url->pwd, sizeof(url->pwd),
950 				"%s", word) > (int)sizeof(url->pwd)) {
951 				fetch_info("password in .netrc is too long");
952 				url->pwd[0] = '\0';
953 			}
954 		} else if (strcmp(word, "account") == 0) {
955 			if ((word = fetch_read_word(f)) == NULL)
956 				goto ferr;
957 			/* XXX not supported! */
958 		} else {
959 			break;
960 		}
961 	}
962 	fclose(f);
963 	return (0);
964  ferr:
965 	fclose(f);
966 	return (-1);
967 }
968 
969 /*
970  * The no_proxy environment variable specifies a set of domains for
971  * which the proxy should not be consulted; the contents is a comma-,
972  * or space-separated list of domain names.  A single asterisk will
973  * override all proxy variables and no transactions will be proxied
974  * (for compatability with lynx and curl, see the discussion at
975  * <http://curl.haxx.se/mail/archive_pre_oct_99/0009.html>).
976  */
977 int
fetch_no_proxy_match(const char * host)978 fetch_no_proxy_match(const char *host)
979 {
980 	const char *no_proxy, *p, *q;
981 	size_t h_len, d_len;
982 
983 	if ((no_proxy = getenv("NO_PROXY")) == NULL &&
984 	    (no_proxy = getenv("no_proxy")) == NULL)
985 		return (0);
986 
987 	/* asterisk matches any hostname */
988 	if (strcmp(no_proxy, "*") == 0)
989 		return (1);
990 
991 	h_len = strlen(host);
992 	p = no_proxy;
993 	do {
994 		/* position p at the beginning of a domain suffix */
995 		while (*p == ',' || isspace((unsigned char)*p))
996 			p++;
997 
998 		/* position q at the first separator character */
999 		for (q = p; *q; ++q)
1000 			if (*q == ',' || isspace((unsigned char)*q))
1001 				break;
1002 
1003 		d_len = q - p;
1004 		if (d_len > 0 && h_len > d_len &&
1005 		    strncasecmp(host + h_len - d_len,
1006 			p, d_len) == 0) {
1007 			/* domain name matches */
1008 			return (1);
1009 		}
1010 
1011 		p = q + 1;
1012 	} while (*q);
1013 
1014 	return (0);
1015 }
1016 
1017 struct fetchIO {
1018 	void *io_cookie;
1019 	ssize_t (*io_read)(void *, void *, size_t);
1020 	ssize_t (*io_write)(void *, const void *, size_t);
1021 	void (*io_close)(void *);
1022 };
1023 
1024 void
fetchIO_close(fetchIO * f)1025 fetchIO_close(fetchIO *f)
1026 {
1027 	if (f->io_close != NULL)
1028 		(*f->io_close)(f->io_cookie);
1029 
1030 	free(f);
1031 }
1032 
1033 fetchIO *
fetchIO_unopen(void * io_cookie,ssize_t (* io_read)(void *,void *,size_t),ssize_t (* io_write)(void *,const void *,size_t),void (* io_close)(void *))1034 fetchIO_unopen(void *io_cookie, ssize_t (*io_read)(void *, void *, size_t),
1035     ssize_t (*io_write)(void *, const void *, size_t),
1036     void (*io_close)(void *))
1037 {
1038 	fetchIO *f;
1039 
1040 	f = malloc(sizeof(*f));
1041 	if (f == NULL)
1042 		return f;
1043 
1044 	f->io_cookie = io_cookie;
1045 	f->io_read = io_read;
1046 	f->io_write = io_write;
1047 	f->io_close = io_close;
1048 
1049 	return f;
1050 }
1051 
1052 ssize_t
fetchIO_read(fetchIO * f,void * buf,size_t len)1053 fetchIO_read(fetchIO *f, void *buf, size_t len)
1054 {
1055 	if (f->io_read == NULL)
1056 		return EBADF;
1057 	return (*f->io_read)(f->io_cookie, buf, len);
1058 }
1059 
1060 ssize_t
fetchIO_write(fetchIO * f,const void * buf,size_t len)1061 fetchIO_write(fetchIO *f, const void *buf, size_t len)
1062 {
1063 	if (f->io_read == NULL)
1064 		return EBADF;
1065 	return (*f->io_write)(f->io_cookie, buf, len);
1066 }
1067