xref: /freebsd/lib/libfetch/common.c (revision 07b202a847e08b9cd976e40315cda902ced1c84a)
1 /*-
2  * Copyright (c) 1998-2011 Dag-Erling Smørgrav
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <sys/time.h>
35 #include <sys/uio.h>
36 
37 #include <netinet/in.h>
38 
39 #include <ctype.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <netdb.h>
43 #include <pwd.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 #include "fetch.h"
51 #include "common.h"
52 
53 
54 /*** Local data **************************************************************/
55 
56 /*
57  * Error messages for resolver errors
58  */
59 static struct fetcherr netdb_errlist[] = {
60 #ifdef EAI_NODATA
61 	{ EAI_NODATA,	FETCH_RESOLV,	"Host not found" },
62 #endif
63 	{ EAI_AGAIN,	FETCH_TEMP,	"Transient resolver failure" },
64 	{ EAI_FAIL,	FETCH_RESOLV,	"Non-recoverable resolver failure" },
65 	{ EAI_NONAME,	FETCH_RESOLV,	"No address record" },
66 	{ -1,		FETCH_UNKNOWN,	"Unknown resolver error" }
67 };
68 
69 /* End-of-Line */
70 static const char ENDL[2] = "\r\n";
71 
72 
73 /*** Error-reporting functions ***********************************************/
74 
75 /*
76  * Map error code to string
77  */
78 static struct fetcherr *
79 fetch_finderr(struct fetcherr *p, int e)
80 {
81 	while (p->num != -1 && p->num != e)
82 		p++;
83 	return (p);
84 }
85 
86 /*
87  * Set error code
88  */
89 void
90 fetch_seterr(struct fetcherr *p, int e)
91 {
92 	p = fetch_finderr(p, e);
93 	fetchLastErrCode = p->cat;
94 	snprintf(fetchLastErrString, MAXERRSTRING, "%s", p->string);
95 }
96 
97 /*
98  * Set error code according to errno
99  */
100 void
101 fetch_syserr(void)
102 {
103 	switch (errno) {
104 	case 0:
105 		fetchLastErrCode = FETCH_OK;
106 		break;
107 	case EPERM:
108 	case EACCES:
109 	case EROFS:
110 	case EAUTH:
111 	case ENEEDAUTH:
112 		fetchLastErrCode = FETCH_AUTH;
113 		break;
114 	case ENOENT:
115 	case EISDIR: /* XXX */
116 		fetchLastErrCode = FETCH_UNAVAIL;
117 		break;
118 	case ENOMEM:
119 		fetchLastErrCode = FETCH_MEMORY;
120 		break;
121 	case EBUSY:
122 	case EAGAIN:
123 		fetchLastErrCode = FETCH_TEMP;
124 		break;
125 	case EEXIST:
126 		fetchLastErrCode = FETCH_EXISTS;
127 		break;
128 	case ENOSPC:
129 		fetchLastErrCode = FETCH_FULL;
130 		break;
131 	case EADDRINUSE:
132 	case EADDRNOTAVAIL:
133 	case ENETDOWN:
134 	case ENETUNREACH:
135 	case ENETRESET:
136 	case EHOSTUNREACH:
137 		fetchLastErrCode = FETCH_NETWORK;
138 		break;
139 	case ECONNABORTED:
140 	case ECONNRESET:
141 		fetchLastErrCode = FETCH_ABORT;
142 		break;
143 	case ETIMEDOUT:
144 		fetchLastErrCode = FETCH_TIMEOUT;
145 		break;
146 	case ECONNREFUSED:
147 	case EHOSTDOWN:
148 		fetchLastErrCode = FETCH_DOWN;
149 		break;
150 default:
151 		fetchLastErrCode = FETCH_UNKNOWN;
152 	}
153 	snprintf(fetchLastErrString, MAXERRSTRING, "%s", strerror(errno));
154 }
155 
156 
157 /*
158  * Emit status message
159  */
160 void
161 fetch_info(const char *fmt, ...)
162 {
163 	va_list ap;
164 
165 	va_start(ap, fmt);
166 	vfprintf(stderr, fmt, ap);
167 	va_end(ap);
168 	fputc('\n', stderr);
169 }
170 
171 
172 /*** Network-related utility functions ***************************************/
173 
174 /*
175  * Return the default port for a scheme
176  */
177 int
178 fetch_default_port(const char *scheme)
179 {
180 	struct servent *se;
181 
182 	if ((se = getservbyname(scheme, "tcp")) != NULL)
183 		return (ntohs(se->s_port));
184 	if (strcasecmp(scheme, SCHEME_FTP) == 0)
185 		return (FTP_DEFAULT_PORT);
186 	if (strcasecmp(scheme, SCHEME_HTTP) == 0)
187 		return (HTTP_DEFAULT_PORT);
188 	return (0);
189 }
190 
191 /*
192  * Return the default proxy port for a scheme
193  */
194 int
195 fetch_default_proxy_port(const char *scheme)
196 {
197 	if (strcasecmp(scheme, SCHEME_FTP) == 0)
198 		return (FTP_DEFAULT_PROXY_PORT);
199 	if (strcasecmp(scheme, SCHEME_HTTP) == 0)
200 		return (HTTP_DEFAULT_PROXY_PORT);
201 	return (0);
202 }
203 
204 
205 /*
206  * Create a connection for an existing descriptor.
207  */
208 conn_t *
209 fetch_reopen(int sd)
210 {
211 	conn_t *conn;
212 
213 	/* allocate and fill connection structure */
214 	if ((conn = calloc(1, sizeof(*conn))) == NULL)
215 		return (NULL);
216 	fcntl(sd, F_SETFD, FD_CLOEXEC);
217 	conn->sd = sd;
218 	++conn->ref;
219 	return (conn);
220 }
221 
222 
223 /*
224  * Bump a connection's reference count.
225  */
226 conn_t *
227 fetch_ref(conn_t *conn)
228 {
229 
230 	++conn->ref;
231 	return (conn);
232 }
233 
234 
235 /*
236  * Bind a socket to a specific local address
237  */
238 int
239 fetch_bind(int sd, int af, const char *addr)
240 {
241 	struct addrinfo hints, *res, *res0;
242 	int err;
243 
244 	memset(&hints, 0, sizeof(hints));
245 	hints.ai_family = af;
246 	hints.ai_socktype = SOCK_STREAM;
247 	hints.ai_protocol = 0;
248 	if ((err = getaddrinfo(addr, NULL, &hints, &res0)) != 0)
249 		return (-1);
250 	for (res = res0; res; res = res->ai_next)
251 		if (bind(sd, res->ai_addr, res->ai_addrlen) == 0)
252 			return (0);
253 	return (-1);
254 }
255 
256 
257 /*
258  * Establish a TCP connection to the specified port on the specified host.
259  */
260 conn_t *
261 fetch_connect(const char *host, int port, int af, int verbose)
262 {
263 	conn_t *conn;
264 	char pbuf[10];
265 	const char *bindaddr;
266 	struct addrinfo hints, *res, *res0;
267 	int sd, err;
268 
269 	DEBUG(fprintf(stderr, "---> %s:%d\n", host, port));
270 
271 	if (verbose)
272 		fetch_info("looking up %s", host);
273 
274 	/* look up host name and set up socket address structure */
275 	snprintf(pbuf, sizeof(pbuf), "%d", port);
276 	memset(&hints, 0, sizeof(hints));
277 	hints.ai_family = af;
278 	hints.ai_socktype = SOCK_STREAM;
279 	hints.ai_protocol = 0;
280 	if ((err = getaddrinfo(host, pbuf, &hints, &res0)) != 0) {
281 		netdb_seterr(err);
282 		return (NULL);
283 	}
284 	bindaddr = getenv("FETCH_BIND_ADDRESS");
285 
286 	if (verbose)
287 		fetch_info("connecting to %s:%d", host, port);
288 
289 	/* try to connect */
290 	for (sd = -1, res = res0; res; sd = -1, res = res->ai_next) {
291 		if ((sd = socket(res->ai_family, res->ai_socktype,
292 			 res->ai_protocol)) == -1)
293 			continue;
294 		if (bindaddr != NULL && *bindaddr != '\0' &&
295 		    fetch_bind(sd, res->ai_family, bindaddr) != 0) {
296 			fetch_info("failed to bind to '%s'", bindaddr);
297 			close(sd);
298 			continue;
299 		}
300 		if (connect(sd, res->ai_addr, res->ai_addrlen) == 0 &&
301 		    fcntl(sd, F_SETFL, O_NONBLOCK) == 0)
302 			break;
303 		close(sd);
304 	}
305 	freeaddrinfo(res0);
306 	if (sd == -1) {
307 		fetch_syserr();
308 		return (NULL);
309 	}
310 
311 	if ((conn = fetch_reopen(sd)) == NULL) {
312 		fetch_syserr();
313 		close(sd);
314 	}
315 	return (conn);
316 }
317 
318 
319 /*
320  * Enable SSL on a connection.
321  */
322 int
323 fetch_ssl(conn_t *conn, int verbose)
324 {
325 #ifdef WITH_SSL
326 	int ret, ssl_err;
327 
328 	/* Init the SSL library and context */
329 	if (!SSL_library_init()){
330 		fprintf(stderr, "SSL library init failed\n");
331 		return (-1);
332 	}
333 
334 	SSL_load_error_strings();
335 
336 	conn->ssl_meth = SSLv23_client_method();
337 	conn->ssl_ctx = SSL_CTX_new(conn->ssl_meth);
338 	SSL_CTX_set_mode(conn->ssl_ctx, SSL_MODE_AUTO_RETRY);
339 
340 	conn->ssl = SSL_new(conn->ssl_ctx);
341 	if (conn->ssl == NULL){
342 		fprintf(stderr, "SSL context creation failed\n");
343 		return (-1);
344 	}
345 	SSL_set_fd(conn->ssl, conn->sd);
346 	while ((ret = SSL_connect(conn->ssl)) == -1) {
347 		ssl_err = SSL_get_error(conn->ssl, ret);
348 		if (ssl_err != SSL_ERROR_WANT_READ &&
349 		    ssl_err != SSL_ERROR_WANT_WRITE) {
350 			ERR_print_errors_fp(stderr);
351 			return (-1);
352 		}
353 	}
354 
355 	if (verbose) {
356 		X509_NAME *name;
357 		char *str;
358 
359 		fprintf(stderr, "SSL connection established using %s\n",
360 		    SSL_get_cipher(conn->ssl));
361 		conn->ssl_cert = SSL_get_peer_certificate(conn->ssl);
362 		name = X509_get_subject_name(conn->ssl_cert);
363 		str = X509_NAME_oneline(name, 0, 0);
364 		printf("Certificate subject: %s\n", str);
365 		free(str);
366 		name = X509_get_issuer_name(conn->ssl_cert);
367 		str = X509_NAME_oneline(name, 0, 0);
368 		printf("Certificate issuer: %s\n", str);
369 		free(str);
370 	}
371 
372 	return (0);
373 #else
374 	(void)conn;
375 	(void)verbose;
376 	fprintf(stderr, "SSL support disabled\n");
377 	return (-1);
378 #endif
379 }
380 
381 #define FETCH_READ_WAIT		-2
382 #define FETCH_READ_ERROR	-1
383 #define FETCH_READ_DONE		 0
384 
385 #ifdef WITH_SSL
386 static ssize_t
387 fetch_ssl_read(SSL *ssl, char *buf, size_t len)
388 {
389 	ssize_t rlen;
390 	int ssl_err;
391 
392 	rlen = SSL_read(ssl, buf, len);
393 	if (rlen < 0) {
394 		ssl_err = SSL_get_error(ssl, rlen);
395 		if (ssl_err == SSL_ERROR_WANT_READ ||
396 		    ssl_err == SSL_ERROR_WANT_WRITE) {
397 			return (FETCH_READ_WAIT);
398 		} else {
399 			ERR_print_errors_fp(stderr);
400 			return (FETCH_READ_ERROR);
401 		}
402 	}
403 	return (rlen);
404 }
405 #endif
406 
407 /*
408  * Cache some data that was read from a socket but cannot be immediately
409  * returned because of an interrupted system call.
410  */
411 static int
412 fetch_cache_data(conn_t *conn, char *src, size_t nbytes)
413 {
414 	char *tmp;
415 
416 	if (conn->cache.size < nbytes) {
417 		tmp = realloc(conn->cache.buf, nbytes);
418 		if (tmp == NULL) {
419 			fetch_syserr();
420 			return (-1);
421 		}
422 		conn->cache.buf = tmp;
423 		conn->cache.size = nbytes;
424 	}
425 
426 	memcpy(conn->cache.buf, src, nbytes);
427 	conn->cache.len = nbytes;
428 	conn->cache.pos = 0;
429 
430 	return (0);
431 }
432 
433 
434 static ssize_t
435 fetch_socket_read(int sd, char *buf, size_t len)
436 {
437 	ssize_t rlen;
438 
439 	rlen = read(sd, buf, len);
440 	if (rlen < 0) {
441 		if (errno == EAGAIN || (errno == EINTR && fetchRestartCalls))
442 			return (FETCH_READ_WAIT);
443 		else
444 			return (FETCH_READ_ERROR);
445 	}
446 	return (rlen);
447 }
448 
449 /*
450  * Read a character from a connection w/ timeout
451  */
452 ssize_t
453 fetch_read(conn_t *conn, char *buf, size_t len)
454 {
455 	struct timeval now, timeout, delta;
456 	fd_set readfds;
457 	ssize_t rlen, total;
458 	int r;
459 	char *start;
460 
461 	if (fetchTimeout) {
462 		FD_ZERO(&readfds);
463 		gettimeofday(&timeout, NULL);
464 		timeout.tv_sec += fetchTimeout;
465 	}
466 
467 	total = 0;
468 	start = buf;
469 
470 	if (conn->cache.len > 0) {
471 		/*
472 		 * The last invocation of fetch_read was interrupted by a
473 		 * signal after some data had been read from the socket. Copy
474 		 * the cached data into the supplied buffer before trying to
475 		 * read from the socket again.
476 		 */
477 		total = (conn->cache.len < len) ? conn->cache.len : len;
478 		memcpy(buf, conn->cache.buf, total);
479 
480 		conn->cache.len -= total;
481 		conn->cache.pos += total;
482 		len -= total;
483 		buf += total;
484 	}
485 
486 	while (len > 0) {
487 		/*
488 		 * The socket is non-blocking.  Instead of the canonical
489 		 * select() -> read(), we do the following:
490 		 *
491 		 * 1) call read() or SSL_read().
492 		 * 2) if an error occurred, return -1.
493 		 * 3) if we received data but we still expect more,
494 		 *    update our counters and loop.
495 		 * 4) if read() or SSL_read() signaled EOF, return.
496 		 * 5) if we did not receive any data but we're not at EOF,
497 		 *    call select().
498 		 *
499 		 * In the SSL case, this is necessary because if we
500 		 * receive a close notification, we have to call
501 		 * SSL_read() one additional time after we've read
502 		 * everything we received.
503 		 *
504 		 * In the non-SSL case, it may improve performance (very
505 		 * slightly) when reading small amounts of data.
506 		 */
507 #ifdef WITH_SSL
508 		if (conn->ssl != NULL)
509 			rlen = fetch_ssl_read(conn->ssl, buf, len);
510 		else
511 #endif
512 			rlen = fetch_socket_read(conn->sd, buf, len);
513 		if (rlen == 0) {
514 			break;
515 		} else if (rlen > 0) {
516 			len -= rlen;
517 			buf += rlen;
518 			total += rlen;
519 			continue;
520 		} else if (rlen == FETCH_READ_ERROR) {
521 			if (errno == EINTR)
522 				fetch_cache_data(conn, start, total);
523 			return (-1);
524 		}
525 		// assert(rlen == FETCH_READ_WAIT);
526 		while (fetchTimeout && !FD_ISSET(conn->sd, &readfds)) {
527 			FD_SET(conn->sd, &readfds);
528 			gettimeofday(&now, NULL);
529 			delta.tv_sec = timeout.tv_sec - now.tv_sec;
530 			delta.tv_usec = timeout.tv_usec - now.tv_usec;
531 			if (delta.tv_usec < 0) {
532 				delta.tv_usec += 1000000;
533 				delta.tv_sec--;
534 			}
535 			if (delta.tv_sec < 0) {
536 				errno = ETIMEDOUT;
537 				fetch_syserr();
538 				return (-1);
539 			}
540 			errno = 0;
541 			r = select(conn->sd + 1, &readfds, NULL, NULL, &delta);
542 			if (r == -1) {
543 				if (errno == EINTR) {
544 					if (fetchRestartCalls)
545 						continue;
546 					/* Save anything that was read. */
547 					fetch_cache_data(conn, start, total);
548 				}
549 				fetch_syserr();
550 				return (-1);
551 			}
552 		}
553 	}
554 	return (total);
555 }
556 
557 
558 /*
559  * Read a line of text from a connection w/ timeout
560  */
561 #define MIN_BUF_SIZE 1024
562 
563 int
564 fetch_getln(conn_t *conn)
565 {
566 	char *tmp;
567 	size_t tmpsize;
568 	ssize_t len;
569 	char c;
570 
571 	if (conn->buf == NULL) {
572 		if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) {
573 			errno = ENOMEM;
574 			return (-1);
575 		}
576 		conn->bufsize = MIN_BUF_SIZE;
577 	}
578 
579 	conn->buf[0] = '\0';
580 	conn->buflen = 0;
581 
582 	do {
583 		len = fetch_read(conn, &c, 1);
584 		if (len == -1)
585 			return (-1);
586 		if (len == 0)
587 			break;
588 		conn->buf[conn->buflen++] = c;
589 		if (conn->buflen == conn->bufsize) {
590 			tmp = conn->buf;
591 			tmpsize = conn->bufsize * 2 + 1;
592 			if ((tmp = realloc(tmp, tmpsize)) == NULL) {
593 				errno = ENOMEM;
594 				return (-1);
595 			}
596 			conn->buf = tmp;
597 			conn->bufsize = tmpsize;
598 		}
599 	} while (c != '\n');
600 
601 	conn->buf[conn->buflen] = '\0';
602 	DEBUG(fprintf(stderr, "<<< %s", conn->buf));
603 	return (0);
604 }
605 
606 
607 /*
608  * Write to a connection w/ timeout
609  */
610 ssize_t
611 fetch_write(conn_t *conn, const char *buf, size_t len)
612 {
613 	struct iovec iov;
614 
615 	iov.iov_base = __DECONST(char *, buf);
616 	iov.iov_len = len;
617 	return fetch_writev(conn, &iov, 1);
618 }
619 
620 /*
621  * Write a vector to a connection w/ timeout
622  * Note: can modify the iovec.
623  */
624 ssize_t
625 fetch_writev(conn_t *conn, struct iovec *iov, int iovcnt)
626 {
627 	struct timeval now, timeout, delta;
628 	fd_set writefds;
629 	ssize_t wlen, total;
630 	int r;
631 
632 	if (fetchTimeout) {
633 		FD_ZERO(&writefds);
634 		gettimeofday(&timeout, NULL);
635 		timeout.tv_sec += fetchTimeout;
636 	}
637 
638 	total = 0;
639 	while (iovcnt > 0) {
640 		while (fetchTimeout && !FD_ISSET(conn->sd, &writefds)) {
641 			FD_SET(conn->sd, &writefds);
642 			gettimeofday(&now, NULL);
643 			delta.tv_sec = timeout.tv_sec - now.tv_sec;
644 			delta.tv_usec = timeout.tv_usec - now.tv_usec;
645 			if (delta.tv_usec < 0) {
646 				delta.tv_usec += 1000000;
647 				delta.tv_sec--;
648 			}
649 			if (delta.tv_sec < 0) {
650 				errno = ETIMEDOUT;
651 				fetch_syserr();
652 				return (-1);
653 			}
654 			errno = 0;
655 			r = select(conn->sd + 1, NULL, &writefds, NULL, &delta);
656 			if (r == -1) {
657 				if (errno == EINTR && fetchRestartCalls)
658 					continue;
659 				return (-1);
660 			}
661 		}
662 		errno = 0;
663 #ifdef WITH_SSL
664 		if (conn->ssl != NULL)
665 			wlen = SSL_write(conn->ssl,
666 			    iov->iov_base, iov->iov_len);
667 		else
668 #endif
669 			wlen = writev(conn->sd, iov, iovcnt);
670 		if (wlen == 0) {
671 			/* we consider a short write a failure */
672 			/* XXX perhaps we shouldn't in the SSL case */
673 			errno = EPIPE;
674 			fetch_syserr();
675 			return (-1);
676 		}
677 		if (wlen < 0) {
678 			if (errno == EINTR && fetchRestartCalls)
679 				continue;
680 			return (-1);
681 		}
682 		total += wlen;
683 		while (iovcnt > 0 && wlen >= (ssize_t)iov->iov_len) {
684 			wlen -= iov->iov_len;
685 			iov++;
686 			iovcnt--;
687 		}
688 		if (iovcnt > 0) {
689 			iov->iov_len -= wlen;
690 			iov->iov_base = __DECONST(char *, iov->iov_base) + wlen;
691 		}
692 	}
693 	return (total);
694 }
695 
696 
697 /*
698  * Write a line of text to a connection w/ timeout
699  */
700 int
701 fetch_putln(conn_t *conn, const char *str, size_t len)
702 {
703 	struct iovec iov[2];
704 	int ret;
705 
706 	DEBUG(fprintf(stderr, ">>> %s\n", str));
707 	iov[0].iov_base = __DECONST(char *, str);
708 	iov[0].iov_len = len;
709 	iov[1].iov_base = __DECONST(char *, ENDL);
710 	iov[1].iov_len = sizeof(ENDL);
711 	if (len == 0)
712 		ret = fetch_writev(conn, &iov[1], 1);
713 	else
714 		ret = fetch_writev(conn, iov, 2);
715 	if (ret == -1)
716 		return (-1);
717 	return (0);
718 }
719 
720 
721 /*
722  * Close connection
723  */
724 int
725 fetch_close(conn_t *conn)
726 {
727 	int ret;
728 
729 	if (--conn->ref > 0)
730 		return (0);
731 	ret = close(conn->sd);
732 	free(conn->cache.buf);
733 	free(conn->buf);
734 	free(conn);
735 	return (ret);
736 }
737 
738 
739 /*** Directory-related utility functions *************************************/
740 
741 int
742 fetch_add_entry(struct url_ent **p, int *size, int *len,
743     const char *name, struct url_stat *us)
744 {
745 	struct url_ent *tmp;
746 
747 	if (*p == NULL) {
748 		*size = 0;
749 		*len = 0;
750 	}
751 
752 	if (*len >= *size - 1) {
753 		tmp = realloc(*p, (*size * 2 + 1) * sizeof(**p));
754 		if (tmp == NULL) {
755 			errno = ENOMEM;
756 			fetch_syserr();
757 			return (-1);
758 		}
759 		*size = (*size * 2 + 1);
760 		*p = tmp;
761 	}
762 
763 	tmp = *p + *len;
764 	snprintf(tmp->name, PATH_MAX, "%s", name);
765 	memcpy(&tmp->stat, us, sizeof(*us));
766 
767 	(*len)++;
768 	(++tmp)->name[0] = 0;
769 
770 	return (0);
771 }
772 
773 
774 /*** Authentication-related utility functions ********************************/
775 
776 static const char *
777 fetch_read_word(FILE *f)
778 {
779 	static char word[1024];
780 
781 	if (fscanf(f, " %1023s ", word) != 1)
782 		return (NULL);
783 	return (word);
784 }
785 
786 /*
787  * Get authentication data for a URL from .netrc
788  */
789 int
790 fetch_netrc_auth(struct url *url)
791 {
792 	char fn[PATH_MAX];
793 	const char *word;
794 	char *p;
795 	FILE *f;
796 
797 	if ((p = getenv("NETRC")) != NULL) {
798 		if (snprintf(fn, sizeof(fn), "%s", p) >= (int)sizeof(fn)) {
799 			fetch_info("$NETRC specifies a file name "
800 			    "longer than PATH_MAX");
801 			return (-1);
802 		}
803 	} else {
804 		if ((p = getenv("HOME")) != NULL) {
805 			struct passwd *pwd;
806 
807 			if ((pwd = getpwuid(getuid())) == NULL ||
808 			    (p = pwd->pw_dir) == NULL)
809 				return (-1);
810 		}
811 		if (snprintf(fn, sizeof(fn), "%s/.netrc", p) >= (int)sizeof(fn))
812 			return (-1);
813 	}
814 
815 	if ((f = fopen(fn, "r")) == NULL)
816 		return (-1);
817 	while ((word = fetch_read_word(f)) != NULL) {
818 		if (strcmp(word, "default") == 0) {
819 			DEBUG(fetch_info("Using default .netrc settings"));
820 			break;
821 		}
822 		if (strcmp(word, "machine") == 0 &&
823 		    (word = fetch_read_word(f)) != NULL &&
824 		    strcasecmp(word, url->host) == 0) {
825 			DEBUG(fetch_info("Using .netrc settings for %s", word));
826 			break;
827 		}
828 	}
829 	if (word == NULL)
830 		goto ferr;
831 	while ((word = fetch_read_word(f)) != NULL) {
832 		if (strcmp(word, "login") == 0) {
833 			if ((word = fetch_read_word(f)) == NULL)
834 				goto ferr;
835 			if (snprintf(url->user, sizeof(url->user),
836 				"%s", word) > (int)sizeof(url->user)) {
837 				fetch_info("login name in .netrc is too long");
838 				url->user[0] = '\0';
839 			}
840 		} else if (strcmp(word, "password") == 0) {
841 			if ((word = fetch_read_word(f)) == NULL)
842 				goto ferr;
843 			if (snprintf(url->pwd, sizeof(url->pwd),
844 				"%s", word) > (int)sizeof(url->pwd)) {
845 				fetch_info("password in .netrc is too long");
846 				url->pwd[0] = '\0';
847 			}
848 		} else if (strcmp(word, "account") == 0) {
849 			if ((word = fetch_read_word(f)) == NULL)
850 				goto ferr;
851 			/* XXX not supported! */
852 		} else {
853 			break;
854 		}
855 	}
856 	fclose(f);
857 	return (0);
858  ferr:
859 	fclose(f);
860 	return (-1);
861 }
862 
863 /*
864  * The no_proxy environment variable specifies a set of domains for
865  * which the proxy should not be consulted; the contents is a comma-,
866  * or space-separated list of domain names.  A single asterisk will
867  * override all proxy variables and no transactions will be proxied
868  * (for compatability with lynx and curl, see the discussion at
869  * <http://curl.haxx.se/mail/archive_pre_oct_99/0009.html>).
870  */
871 int
872 fetch_no_proxy_match(const char *host)
873 {
874 	const char *no_proxy, *p, *q;
875 	size_t h_len, d_len;
876 
877 	if ((no_proxy = getenv("NO_PROXY")) == NULL &&
878 	    (no_proxy = getenv("no_proxy")) == NULL)
879 		return (0);
880 
881 	/* asterisk matches any hostname */
882 	if (strcmp(no_proxy, "*") == 0)
883 		return (1);
884 
885 	h_len = strlen(host);
886 	p = no_proxy;
887 	do {
888 		/* position p at the beginning of a domain suffix */
889 		while (*p == ',' || isspace((unsigned char)*p))
890 			p++;
891 
892 		/* position q at the first separator character */
893 		for (q = p; *q; ++q)
894 			if (*q == ',' || isspace((unsigned char)*q))
895 				break;
896 
897 		d_len = q - p;
898 		if (d_len > 0 && h_len >= d_len &&
899 		    strncasecmp(host + h_len - d_len,
900 			p, d_len) == 0) {
901 			/* domain name matches */
902 			return (1);
903 		}
904 
905 		p = q + 1;
906 	} while (*q);
907 
908 	return (0);
909 }
910