xref: /freebsd/lib/libfetch/common.c (revision fb3dc55a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1998-2016 Dag-Erling Smørgrav
5  * Copyright (c) 2013 Michael Gmelin <freebsd@grem.de>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer
13  *    in this position and unchanged.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
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 <inttypes.h>
43 #include <netdb.h>
44 #include <paths.h>
45 #include <poll.h>
46 #include <pwd.h>
47 #include <stdarg.h>
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <unistd.h>
52 
53 #ifdef WITH_SSL
54 #include <openssl/x509v3.h>
55 #endif
56 
57 #include "fetch.h"
58 #include "common.h"
59 
60 
61 /*** Local data **************************************************************/
62 
63 /*
64  * Error messages for resolver errors
65  */
66 static struct fetcherr netdb_errlist[] = {
67 #ifdef EAI_ADDRFAMILY
68 	{ EAI_ADDRFAMILY, FETCH_RESOLV, "Address family for host not supported" },
69 #endif
70 #ifdef EAI_NODATA
71 	{ EAI_NODATA,	FETCH_RESOLV,	"No address for host" },
72 #endif
73 	{ EAI_AGAIN,	FETCH_TEMP,	"Transient resolver failure" },
74 	{ EAI_FAIL,	FETCH_RESOLV,	"Non-recoverable resolver failure" },
75 	{ EAI_NONAME,	FETCH_RESOLV,	"Host does not resolve" },
76 	{ -1,		FETCH_UNKNOWN,	"Unknown resolver error" }
77 };
78 
79 /*
80  * SOCKS5 error enumerations
81  */
82 enum SOCKS5_ERR {
83 /* Protocol errors */
84 	SOCKS5_ERR_SELECTION,
85 	SOCKS5_ERR_READ_METHOD,
86 	SOCKS5_ERR_VER5_ONLY,
87 	SOCKS5_ERR_NOMETHODS,
88 	SOCKS5_ERR_NOTIMPLEMENTED,
89 	SOCKS5_ERR_HOSTNAME_SIZE,
90 	SOCKS5_ERR_REQUEST,
91 	SOCKS5_ERR_REPLY,
92 	SOCKS5_ERR_NON_VER5_RESP,
93 	SOCKS5_ERR_GENERAL,
94 	SOCKS5_ERR_NOT_ALLOWED,
95 	SOCKS5_ERR_NET_UNREACHABLE,
96 	SOCKS5_ERR_HOST_UNREACHABLE,
97 	SOCKS5_ERR_CONN_REFUSED,
98 	SOCKS5_ERR_TTL_EXPIRED,
99 	SOCKS5_ERR_COM_UNSUPPORTED,
100 	SOCKS5_ERR_ADDR_UNSUPPORTED,
101 	SOCKS5_ERR_UNSPECIFIED,
102 /* Configuration errors */
103 	SOCKS5_ERR_BAD_HOST,
104 	SOCKS5_ERR_BAD_PROXY_FORMAT,
105 	SOCKS5_ERR_BAD_PORT
106 };
107 
108 /*
109  * Error messages for SOCKS5 errors
110  */
111 static struct fetcherr socks5_errlist[] = {
112 /* SOCKS5 protocol errors */
113 	{ SOCKS5_ERR_SELECTION,		FETCH_ABORT,	"SOCKS5: Failed to send selection method" },
114 	{ SOCKS5_ERR_READ_METHOD,	FETCH_ABORT,	"SOCKS5: Failed to read method" },
115 	{ SOCKS5_ERR_VER5_ONLY,		FETCH_PROTO,	"SOCKS5: Only version 5 is implemented" },
116 	{ SOCKS5_ERR_NOMETHODS,		FETCH_PROTO,	"SOCKS5: No acceptable methods" },
117 	{ SOCKS5_ERR_NOTIMPLEMENTED,	FETCH_PROTO,	"SOCKS5: Method currently not implemented" },
118 	{ SOCKS5_ERR_HOSTNAME_SIZE,	FETCH_PROTO,	"SOCKS5: Hostname size is above 256 bytes" },
119 	{ SOCKS5_ERR_REQUEST,		FETCH_PROTO,	"SOCKS5: Failed to request" },
120 	{ SOCKS5_ERR_REPLY,		FETCH_PROTO,	"SOCKS5: Failed to receive reply" },
121 	{ SOCKS5_ERR_NON_VER5_RESP,	FETCH_PROTO,	"SOCKS5: Server responded with a non-version 5 response" },
122 	{ SOCKS5_ERR_GENERAL,		FETCH_ABORT,	"SOCKS5: General server failure" },
123 	{ SOCKS5_ERR_NOT_ALLOWED,	FETCH_AUTH,	"SOCKS5: Connection not allowed by ruleset" },
124 	{ SOCKS5_ERR_NET_UNREACHABLE,	FETCH_NETWORK,	"SOCKS5: Network unreachable" },
125 	{ SOCKS5_ERR_HOST_UNREACHABLE,	FETCH_ABORT,	"SOCKS5: Host unreachable" },
126 	{ SOCKS5_ERR_CONN_REFUSED,	FETCH_ABORT,	"SOCKS5: Connection refused" },
127 	{ SOCKS5_ERR_TTL_EXPIRED,	FETCH_TIMEOUT,	"SOCKS5: TTL expired" },
128 	{ SOCKS5_ERR_COM_UNSUPPORTED,	FETCH_PROTO,	"SOCKS5: Command not supported" },
129 	{ SOCKS5_ERR_ADDR_UNSUPPORTED,	FETCH_ABORT,	"SOCKS5: Address type not supported" },
130 	{ SOCKS5_ERR_UNSPECIFIED,	FETCH_UNKNOWN,	"SOCKS5: Unspecified error" },
131 /* Configuration error */
132 	{ SOCKS5_ERR_BAD_HOST,		FETCH_ABORT,	"SOCKS5: Bad proxy host" },
133 	{ SOCKS5_ERR_BAD_PROXY_FORMAT,	FETCH_ABORT,	"SOCKS5: Bad proxy format" },
134 	{ SOCKS5_ERR_BAD_PORT,		FETCH_ABORT,	"SOCKS5: Bad port" }
135 };
136 
137 /* End-of-Line */
138 static const char ENDL[2] = "\r\n";
139 
140 
141 /*** Error-reporting functions ***********************************************/
142 
143 /*
144  * Map error code to string
145  */
146 static struct fetcherr *
fetch_finderr(struct fetcherr * p,int e)147 fetch_finderr(struct fetcherr *p, int e)
148 {
149 	while (p->num != -1 && p->num != e)
150 		p++;
151 	return (p);
152 }
153 
154 /*
155  * Set error code
156  */
157 void
fetch_seterr(struct fetcherr * p,int e)158 fetch_seterr(struct fetcherr *p, int e)
159 {
160 	p = fetch_finderr(p, e);
161 	fetchLastErrCode = p->cat;
162 	snprintf(fetchLastErrString, MAXERRSTRING, "%s", p->string);
163 }
164 
165 /*
166  * Set error code according to errno
167  */
168 void
fetch_syserr(void)169 fetch_syserr(void)
170 {
171 	switch (errno) {
172 	case 0:
173 		fetchLastErrCode = FETCH_OK;
174 		break;
175 	case EPERM:
176 	case EACCES:
177 	case EROFS:
178 	case EAUTH:
179 	case ENEEDAUTH:
180 		fetchLastErrCode = FETCH_AUTH;
181 		break;
182 	case ENOENT:
183 	case EISDIR: /* XXX */
184 		fetchLastErrCode = FETCH_UNAVAIL;
185 		break;
186 	case ENOMEM:
187 		fetchLastErrCode = FETCH_MEMORY;
188 		break;
189 	case EBUSY:
190 	case EAGAIN:
191 		fetchLastErrCode = FETCH_TEMP;
192 		break;
193 	case EEXIST:
194 		fetchLastErrCode = FETCH_EXISTS;
195 		break;
196 	case ENOSPC:
197 		fetchLastErrCode = FETCH_FULL;
198 		break;
199 	case EADDRINUSE:
200 	case EADDRNOTAVAIL:
201 	case ENETDOWN:
202 	case ENETUNREACH:
203 	case ENETRESET:
204 	case EHOSTUNREACH:
205 		fetchLastErrCode = FETCH_NETWORK;
206 		break;
207 	case ECONNABORTED:
208 	case ECONNRESET:
209 		fetchLastErrCode = FETCH_ABORT;
210 		break;
211 	case ETIMEDOUT:
212 		fetchLastErrCode = FETCH_TIMEOUT;
213 		break;
214 	case ECONNREFUSED:
215 	case EHOSTDOWN:
216 		fetchLastErrCode = FETCH_DOWN;
217 		break;
218 	default:
219 		fetchLastErrCode = FETCH_UNKNOWN;
220 	}
221 	snprintf(fetchLastErrString, MAXERRSTRING, "%s", strerror(errno));
222 }
223 
224 
225 /*
226  * Emit status message
227  */
228 void
fetch_info(const char * fmt,...)229 fetch_info(const char *fmt, ...)
230 {
231 	va_list ap;
232 
233 	va_start(ap, fmt);
234 	vfprintf(stderr, fmt, ap);
235 	va_end(ap);
236 	fputc('\n', stderr);
237 }
238 
239 
240 /*** Network-related utility functions ***************************************/
241 
242 /*
243  * Return the default port for a scheme
244  */
245 int
fetch_default_port(const char * scheme)246 fetch_default_port(const char *scheme)
247 {
248 	struct servent *se;
249 
250 	if ((se = getservbyname(scheme, "tcp")) != NULL)
251 		return (ntohs(se->s_port));
252 	if (strcmp(scheme, SCHEME_FTP) == 0)
253 		return (FTP_DEFAULT_PORT);
254 	if (strcmp(scheme, SCHEME_HTTP) == 0)
255 		return (HTTP_DEFAULT_PORT);
256 	return (0);
257 }
258 
259 /*
260  * Return the default proxy port for a scheme
261  */
262 int
fetch_default_proxy_port(const char * scheme)263 fetch_default_proxy_port(const char *scheme)
264 {
265 	if (strcmp(scheme, SCHEME_FTP) == 0)
266 		return (FTP_DEFAULT_PROXY_PORT);
267 	if (strcmp(scheme, SCHEME_HTTP) == 0)
268 		return (HTTP_DEFAULT_PROXY_PORT);
269 	return (0);
270 }
271 
272 
273 /*
274  * Create a connection for an existing descriptor.
275  */
276 conn_t *
fetch_reopen(int sd)277 fetch_reopen(int sd)
278 {
279 	conn_t *conn;
280 	int opt = 1;
281 
282 	/* allocate and fill connection structure */
283 	if ((conn = calloc(1, sizeof(*conn))) == NULL)
284 		return (NULL);
285 	fcntl(sd, F_SETFD, FD_CLOEXEC);
286 	setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof opt);
287 	conn->sd = sd;
288 	++conn->ref;
289 	return (conn);
290 }
291 
292 
293 /*
294  * Bump a connection's reference count.
295  */
296 conn_t *
fetch_ref(conn_t * conn)297 fetch_ref(conn_t *conn)
298 {
299 
300 	++conn->ref;
301 	return (conn);
302 }
303 
304 
305 /*
306  * Resolve an address
307  */
308 struct addrinfo *
fetch_resolve(const char * addr,int port,int af)309 fetch_resolve(const char *addr, int port, int af)
310 {
311 	char hbuf[256], sbuf[8];
312 	struct addrinfo hints, *res;
313 	const char *hb, *he, *sep;
314 	const char *host, *service;
315 	int err, len;
316 
317 	/* first, check for a bracketed IPv6 address */
318 	if (*addr == '[') {
319 		hb = addr + 1;
320 		if ((sep = strchr(hb, ']')) == NULL) {
321 			errno = EINVAL;
322 			goto syserr;
323 		}
324 		he = sep++;
325 	} else {
326 		hb = addr;
327 		sep = strchrnul(hb, ':');
328 		he = sep;
329 	}
330 
331 	/* see if we need to copy the host name */
332 	if (*he != '\0') {
333 		len = snprintf(hbuf, sizeof(hbuf),
334 		    "%.*s", (int)(he - hb), hb);
335 		if (len < 0)
336 			goto syserr;
337 		if (len >= (int)sizeof(hbuf)) {
338 			errno = ENAMETOOLONG;
339 			goto syserr;
340 		}
341 		host = hbuf;
342 	} else {
343 		host = hb;
344 	}
345 
346 	/* was it followed by a service name? */
347 	if (*sep == '\0' && port != 0) {
348 		if (port < 1 || port > 65535) {
349 			errno = EINVAL;
350 			goto syserr;
351 		}
352 		if (snprintf(sbuf, sizeof(sbuf), "%d", port) < 0)
353 			goto syserr;
354 		service = sbuf;
355 	} else if (*sep != '\0') {
356 		service = sep + 1;
357 	} else {
358 		service = NULL;
359 	}
360 
361 	/* resolve */
362 	memset(&hints, 0, sizeof(hints));
363 	hints.ai_family = af;
364 	hints.ai_socktype = SOCK_STREAM;
365 	hints.ai_flags = AI_ADDRCONFIG;
366 	if ((err = getaddrinfo(host, service, &hints, &res)) != 0) {
367 		netdb_seterr(err);
368 		return (NULL);
369 	}
370 	return (res);
371 syserr:
372 	fetch_syserr();
373 	return (NULL);
374 }
375 
376 
377 /*
378  * Bind a socket to a specific local address
379  */
380 int
fetch_bind(int sd,int af,const char * addr)381 fetch_bind(int sd, int af, const char *addr)
382 {
383 	struct addrinfo *cliai, *ai;
384 	int err;
385 
386 	if ((cliai = fetch_resolve(addr, 0, af)) == NULL)
387 		return (-1);
388 	for (ai = cliai; ai != NULL; ai = ai->ai_next)
389 		if ((err = bind(sd, ai->ai_addr, ai->ai_addrlen)) == 0)
390 			break;
391 	if (err != 0)
392 		fetch_syserr();
393 	freeaddrinfo(cliai);
394 	return (err == 0 ? 0 : -1);
395 }
396 
397 
398 /*
399  * SOCKS5 connection initiation, based on RFC 1928
400  * Default DNS resolution over SOCKS5
401  */
402 int
fetch_socks5_init(conn_t * conn,const char * host,int port,int verbose)403 fetch_socks5_init(conn_t *conn, const char *host, int port, int verbose)
404 {
405 	/*
406 	 * Size is based on largest packet prefix (4 bytes) +
407 	 * Largest FQDN (256) + one byte size (1) +
408 	 * Port (2)
409 	 */
410 	unsigned char buf[BUFF_SIZE];
411 	unsigned char *ptr;
412 	int ret = 1;
413 
414 	if (verbose)
415 		fetch_info("Initializing SOCKS5 connection: %s:%d", host, port);
416 
417 	/* Connection initialization */
418 	ptr = buf;
419 	*ptr++ = SOCKS_VERSION_5;
420 	*ptr++ = SOCKS_CONNECTION;
421 	*ptr++ = SOCKS_RSV;
422 
423 	if (fetch_write(conn, buf, 3) != 3) {
424 		ret = SOCKS5_ERR_SELECTION;
425 		goto fail;
426 	}
427 
428 	/* Verify response from SOCKS5 server */
429 	if (fetch_read(conn, buf, 2) != 2) {
430 		ret = SOCKS5_ERR_READ_METHOD;
431 		goto fail;
432 	}
433 
434 	ptr = buf;
435 	if (ptr[0] != SOCKS_VERSION_5) {
436 		ret = SOCKS5_ERR_VER5_ONLY;
437 		goto fail;
438 	}
439 	if (ptr[1] == SOCKS_NOMETHODS) {
440 		ret = SOCKS5_ERR_NOMETHODS;
441 		goto fail;
442 	}
443 	else if (ptr[1] != SOCKS5_NOTIMPLEMENTED) {
444 		ret = SOCKS5_ERR_NOTIMPLEMENTED;
445 		goto fail;
446 	}
447 
448 	/* Send Request */
449 	*ptr++ = SOCKS_VERSION_5;
450 	*ptr++ = SOCKS_CONNECTION;
451 	*ptr++ = SOCKS_RSV;
452 	/* Encode all targets as a hostname to avoid DNS leaks */
453 	*ptr++ = SOCKS_ATYP_DOMAINNAME;
454 	if (strlen(host) > FQDN_SIZE) {
455 		ret = SOCKS5_ERR_HOSTNAME_SIZE;
456 		goto fail;
457 	}
458 	*ptr++ = strlen(host);
459 	memcpy(ptr, host, strlen(host));
460 	ptr = ptr + strlen(host);
461 
462 	port = htons(port);
463 	*ptr++ = port & 0x00ff;
464 	*ptr++ = (port & 0xff00) >> 8;
465 
466 	if (fetch_write(conn, buf, ptr - buf) != ptr - buf) {
467 		ret = SOCKS5_ERR_REQUEST;
468 		goto fail;
469 	}
470 
471 	/* BND.ADDR is variable length, read the largest on non-blocking socket */
472 	if (!fetch_read(conn, buf, BUFF_SIZE)) {
473 		ret = SOCKS5_ERR_REPLY;
474 		goto fail;
475 	}
476 
477 	ptr = buf;
478 	if (*ptr++ != SOCKS_VERSION_5) {
479 		ret = SOCKS5_ERR_NON_VER5_RESP;
480 		goto fail;
481 	}
482 
483 	switch(*ptr++) {
484 	case SOCKS_SUCCESS:
485 		break;
486 	case SOCKS_GENERAL_FAILURE:
487 		ret = SOCKS5_ERR_GENERAL;
488 		goto fail;
489 	case SOCKS_CONNECTION_NOT_ALLOWED:
490 		ret = SOCKS5_ERR_NOT_ALLOWED;
491 		goto fail;
492 	case SOCKS_NETWORK_UNREACHABLE:
493 		ret = SOCKS5_ERR_NET_UNREACHABLE;
494 		goto fail;
495 	case SOCKS_HOST_UNREACHABLE:
496 		ret = SOCKS5_ERR_HOST_UNREACHABLE;
497 		goto fail;
498 	case SOCKS_CONNECTION_REFUSED:
499 		ret = SOCKS5_ERR_CONN_REFUSED;
500 		goto fail;
501 	case SOCKS_TTL_EXPIRED:
502 		ret = SOCKS5_ERR_TTL_EXPIRED;
503 		goto fail;
504 	case SOCKS_COMMAND_NOT_SUPPORTED:
505 		ret = SOCKS5_ERR_COM_UNSUPPORTED;
506 		goto fail;
507 	case SOCKS_ADDRESS_NOT_SUPPORTED:
508 		ret = SOCKS5_ERR_ADDR_UNSUPPORTED;
509 		goto fail;
510 	default:
511 		ret = SOCKS5_ERR_UNSPECIFIED;
512 		goto fail;
513 	}
514 
515 	return (ret);
516 
517 fail:
518 	socks5_seterr(ret);
519 	return (0);
520 }
521 
522 /*
523  * Perform SOCKS5 initialization
524  */
525 int
fetch_socks5_getenv(char ** host,int * port)526 fetch_socks5_getenv(char **host, int *port)
527 {
528 	char *socks5env, *endptr, *ext;
529 	const char *portDelim;
530 	size_t slen;
531 
532 	portDelim = ":";
533 	if ((socks5env = getenv("SOCKS5_PROXY")) == NULL || *socks5env == '\0') {
534 		*host = NULL;
535 		*port = -1;
536 		return (-1);
537 	}
538 
539 	/*
540 	 * IPv6 addresses begin and end in brackets.  Set the port delimiter
541 	 * accordingly and search for it so we can do appropriate validation.
542 	 */
543 	if (socks5env[0] == '[')
544 		portDelim = "]:";
545 
546 	slen = strlen(socks5env);
547 	ext = strstr(socks5env, portDelim);
548 	if (socks5env[0] == '[') {
549 		if (socks5env[slen - 1] == ']') {
550 			*host = strndup(socks5env, slen);
551 		} else if (ext != NULL) {
552 			*host = strndup(socks5env, ext - socks5env + 1);
553 		} else {
554 			socks5_seterr(SOCKS5_ERR_BAD_PROXY_FORMAT);
555 			return (0);
556 		}
557 	} else {
558 		*host = strndup(socks5env, ext - socks5env);
559 	}
560 
561 	if (*host == NULL) {
562 		fprintf(stderr, "Failure to allocate memory, exiting.\n");
563 		return (-1);
564 	}
565 	if (ext == NULL) {
566 		*port = 1080; /* Default port as defined in RFC1928 */
567 	} else {
568 		ext += strlen(portDelim);
569 		errno = 0;
570 		*port = strtoimax(ext, (char **)&endptr, 10);
571 		if (*endptr != '\0' || errno != 0 || *port < 0 ||
572 		    *port > 65535) {
573 			free(*host);
574 			*host = NULL;
575 			socks5_seterr(SOCKS5_ERR_BAD_PORT);
576 			return (0);
577 		}
578 	}
579 
580 	return (2);
581 }
582 
583 
584 /*
585  * Establish a TCP connection to the specified port on the specified host.
586  */
587 conn_t *
fetch_connect(const char * host,int port,int af,int verbose)588 fetch_connect(const char *host, int port, int af, int verbose)
589 {
590 	struct addrinfo *cais = NULL, *sais = NULL, *cai, *sai;
591 	const char *bindaddr;
592 	conn_t *conn = NULL;
593 	int err = 0, sd = -1;
594 	char *sockshost;
595 	int socksport;
596 
597 	DEBUGF("---> %s:%d\n", host, port);
598 
599 	/*
600 	 * Check if SOCKS5_PROXY env variable is set.  fetch_socks5_getenv
601 	 * will either set sockshost = NULL or allocate memory in all cases.
602 	 */
603 	sockshost = NULL;
604 	if (!fetch_socks5_getenv(&sockshost, &socksport))
605 		goto fail;
606 
607 	/* Not using SOCKS5 proxy */
608 	if (sockshost == NULL) {
609 		/* resolve server address */
610 		if (verbose)
611 			fetch_info("resolving server address: %s:%d", host,
612 			    port);
613 		if ((sais = fetch_resolve(host, port, af)) == NULL)
614 			goto fail;
615 
616 		/* resolve client address */
617 		bindaddr = getenv("FETCH_BIND_ADDRESS");
618 		if (bindaddr != NULL && *bindaddr != '\0') {
619 			if (verbose)
620 				fetch_info("resolving client address: %s",
621 				    bindaddr);
622 			if ((cais = fetch_resolve(bindaddr, 0, af)) == NULL)
623 				goto fail;
624 		}
625 	} else {
626 		/* resolve socks5 proxy address */
627 		if (verbose)
628 			fetch_info("resolving SOCKS5 server address: %s:%d",
629 			    sockshost, socksport);
630 		if ((sais = fetch_resolve(sockshost, socksport, af)) == NULL) {
631 			socks5_seterr(SOCKS5_ERR_BAD_HOST);
632 			goto fail;
633 		}
634 	}
635 
636 	/* try each server address in turn */
637 	for (err = 0, sai = sais; sai != NULL; sai = sai->ai_next) {
638 		/* open socket */
639 		if ((sd = socket(sai->ai_family, SOCK_STREAM, 0)) < 0)
640 			goto syserr;
641 		/* attempt to bind to client address */
642 		for (err = 0, cai = cais; cai != NULL; cai = cai->ai_next) {
643 			if (cai->ai_family != sai->ai_family)
644 				continue;
645 			if ((err = bind(sd, cai->ai_addr, cai->ai_addrlen)) == 0)
646 				break;
647 		}
648 		if (err != 0) {
649 			if (verbose)
650 				fetch_info("failed to bind to %s", bindaddr);
651 			goto syserr;
652 		}
653 		/* attempt to connect to server address */
654 		if ((err = connect(sd, sai->ai_addr, sai->ai_addrlen)) == 0)
655 			break;
656 		/* clean up before next attempt */
657 		close(sd);
658 		sd = -1;
659 	}
660 	if (err != 0) {
661 		if (verbose && sockshost == NULL) {
662 			fetch_info("failed to connect to %s:%d", host, port);
663 			goto syserr;
664 		} else if (sockshost != NULL) {
665 			if (verbose)
666 				fetch_info(
667 				    "failed to connect to SOCKS5 server %s:%d",
668 				    sockshost, socksport);
669 			socks5_seterr(SOCKS5_ERR_CONN_REFUSED);
670 			goto fail;
671 		}
672 		goto syserr;
673 	}
674 
675 	if ((conn = fetch_reopen(sd)) == NULL)
676 		goto syserr;
677 
678 	if (sockshost)
679 		if (!fetch_socks5_init(conn, host, port, verbose))
680 			goto fail;
681 	free(sockshost);
682 	if (cais != NULL)
683 		freeaddrinfo(cais);
684 	if (sais != NULL)
685 		freeaddrinfo(sais);
686 	return (conn);
687 syserr:
688 	fetch_syserr();
689 fail:
690 	free(sockshost);
691 	/* Fully close if it was opened; otherwise just don't leak the fd. */
692 	if (conn != NULL)
693 		fetch_close(conn);
694 	else if (sd >= 0)
695 		close(sd);
696 	if (cais != NULL)
697 		freeaddrinfo(cais);
698 	if (sais != NULL)
699 		freeaddrinfo(sais);
700 	return (NULL);
701 }
702 
703 #ifdef WITH_SSL
704 /*
705  * Convert characters A-Z to lowercase (intentionally avoid any locale
706  * specific conversions).
707  */
708 static char
fetch_ssl_tolower(char in)709 fetch_ssl_tolower(char in)
710 {
711 	if (in >= 'A' && in <= 'Z')
712 		return (in + 32);
713 	else
714 		return (in);
715 }
716 
717 /*
718  * isalpha implementation that intentionally avoids any locale specific
719  * conversions.
720  */
721 static int
fetch_ssl_isalpha(char in)722 fetch_ssl_isalpha(char in)
723 {
724 	return ((in >= 'A' && in <= 'Z') || (in >= 'a' && in <= 'z'));
725 }
726 
727 /*
728  * Check if passed hostnames a and b are equal.
729  */
730 static int
fetch_ssl_hname_equal(const char * a,size_t alen,const char * b,size_t blen)731 fetch_ssl_hname_equal(const char *a, size_t alen, const char *b,
732     size_t blen)
733 {
734 	size_t i;
735 
736 	if (alen != blen)
737 		return (0);
738 	for (i = 0; i < alen; ++i) {
739 		if (fetch_ssl_tolower(a[i]) != fetch_ssl_tolower(b[i]))
740 			return (0);
741 	}
742 	return (1);
743 }
744 
745 /*
746  * Check if domain label is traditional, meaning that only A-Z, a-z, 0-9
747  * and '-' (hyphen) are allowed. Hyphens have to be surrounded by alpha-
748  * numeric characters. Double hyphens (like they're found in IDN a-labels
749  * 'xn--') are not allowed. Empty labels are invalid.
750  */
751 static int
fetch_ssl_is_trad_domain_label(const char * l,size_t len,int wcok)752 fetch_ssl_is_trad_domain_label(const char *l, size_t len, int wcok)
753 {
754 	size_t i;
755 
756 	if (!len || l[0] == '-' || l[len-1] == '-')
757 		return (0);
758 	for (i = 0; i < len; ++i) {
759 		if (!isdigit(l[i]) &&
760 		    !fetch_ssl_isalpha(l[i]) &&
761 		    !(l[i] == '*' && wcok) &&
762 		    !(l[i] == '-' && l[i - 1] != '-'))
763 			return (0);
764 	}
765 	return (1);
766 }
767 
768 /*
769  * Check if host name consists only of numbers. This might indicate an IP
770  * address, which is not a good idea for CN wildcard comparison.
771  */
772 static int
fetch_ssl_hname_is_only_numbers(const char * hostname,size_t len)773 fetch_ssl_hname_is_only_numbers(const char *hostname, size_t len)
774 {
775 	size_t i;
776 
777 	for (i = 0; i < len; ++i) {
778 		if (!((hostname[i] >= '0' && hostname[i] <= '9') ||
779 		    hostname[i] == '.'))
780 			return (0);
781 	}
782 	return (1);
783 }
784 
785 /*
786  * Check if the host name h passed matches the pattern passed in m which
787  * is usually part of subjectAltName or CN of a certificate presented to
788  * the client. This includes wildcard matching. The algorithm is based on
789  * RFC6125, sections 6.4.3 and 7.2, which clarifies RFC2818 and RFC3280.
790  */
791 static int
fetch_ssl_hname_match(const char * h,size_t hlen,const char * m,size_t mlen)792 fetch_ssl_hname_match(const char *h, size_t hlen, const char *m,
793     size_t mlen)
794 {
795 	int delta, hdotidx, mdot1idx, wcidx;
796 	const char *hdot, *mdot1, *mdot2;
797 	const char *wc; /* wildcard */
798 
799 	if (!(h && *h && m && *m))
800 		return (0);
801 	if ((wc = strnstr(m, "*", mlen)) == NULL)
802 		return (fetch_ssl_hname_equal(h, hlen, m, mlen));
803 	wcidx = wc - m;
804 	/* hostname should not be just dots and numbers */
805 	if (fetch_ssl_hname_is_only_numbers(h, hlen))
806 		return (0);
807 	/* only one wildcard allowed in pattern */
808 	if (strnstr(wc + 1, "*", mlen - wcidx - 1) != NULL)
809 		return (0);
810 	/*
811 	 * there must be at least two more domain labels and
812 	 * wildcard has to be in the leftmost label (RFC6125)
813 	 */
814 	mdot1 = strnstr(m, ".", mlen);
815 	if (mdot1 == NULL || mdot1 < wc || (mlen - (mdot1 - m)) < 4)
816 		return (0);
817 	mdot1idx = mdot1 - m;
818 	mdot2 = strnstr(mdot1 + 1, ".", mlen - mdot1idx - 1);
819 	if (mdot2 == NULL || (mlen - (mdot2 - m)) < 2)
820 		return (0);
821 	/* hostname must contain a dot and not be the 1st char */
822 	hdot = strnstr(h, ".", hlen);
823 	if (hdot == NULL || hdot == h)
824 		return (0);
825 	hdotidx = hdot - h;
826 	/*
827 	 * host part of hostname must be at least as long as
828 	 * pattern it's supposed to match
829 	 */
830 	if (hdotidx < mdot1idx)
831 		return (0);
832 	/*
833 	 * don't allow wildcards in non-traditional domain names
834 	 * (IDN, A-label, U-label...)
835 	 */
836 	if (!fetch_ssl_is_trad_domain_label(h, hdotidx, 0) ||
837 	    !fetch_ssl_is_trad_domain_label(m, mdot1idx, 1))
838 		return (0);
839 	/* match domain part (part after first dot) */
840 	if (!fetch_ssl_hname_equal(hdot, hlen - hdotidx, mdot1,
841 	    mlen - mdot1idx))
842 		return (0);
843 	/* match part left of wildcard */
844 	if (!fetch_ssl_hname_equal(h, wcidx, m, wcidx))
845 		return (0);
846 	/* match part right of wildcard */
847 	delta = mdot1idx - wcidx - 1;
848 	if (!fetch_ssl_hname_equal(hdot - delta, delta,
849 	    mdot1 - delta, delta))
850 		return (0);
851 	/* all tests succeeded, it's a match */
852 	return (1);
853 }
854 
855 /*
856  * Get numeric host address info - returns NULL if host was not an IP
857  * address. The caller is responsible for deallocation using
858  * freeaddrinfo(3).
859  */
860 static struct addrinfo *
fetch_ssl_get_numeric_addrinfo(const char * hostname,size_t len)861 fetch_ssl_get_numeric_addrinfo(const char *hostname, size_t len)
862 {
863 	struct addrinfo hints, *res;
864 	char *host;
865 
866 	host = (char *)malloc(len + 1);
867 	memcpy(host, hostname, len);
868 	host[len] = '\0';
869 	memset(&hints, 0, sizeof(hints));
870 	hints.ai_family = PF_UNSPEC;
871 	hints.ai_socktype = SOCK_STREAM;
872 	hints.ai_protocol = 0;
873 	hints.ai_flags = AI_NUMERICHOST;
874 	/* port is not relevant for this purpose */
875 	if (getaddrinfo(host, "443", &hints, &res) != 0)
876 		res = NULL;
877 	free(host);
878 	return res;
879 }
880 
881 /*
882  * Compare ip address in addrinfo with address passes.
883  */
884 static int
fetch_ssl_ipaddr_match_bin(const struct addrinfo * lhost,const char * rhost,size_t rhostlen)885 fetch_ssl_ipaddr_match_bin(const struct addrinfo *lhost, const char *rhost,
886     size_t rhostlen)
887 {
888 	const void *left;
889 
890 	if (lhost->ai_family == AF_INET && rhostlen == 4) {
891 		left = (void *)&((struct sockaddr_in*)(void *)
892 		    lhost->ai_addr)->sin_addr.s_addr;
893 #ifdef INET6
894 	} else if (lhost->ai_family == AF_INET6 && rhostlen == 16) {
895 		left = (void *)&((struct sockaddr_in6 *)(void *)
896 		    lhost->ai_addr)->sin6_addr;
897 #endif
898 	} else
899 		return (0);
900 	return (!memcmp(left, (const void *)rhost, rhostlen) ? 1 : 0);
901 }
902 
903 /*
904  * Compare ip address in addrinfo with host passed. If host is not an IP
905  * address, comparison will fail.
906  */
907 static int
fetch_ssl_ipaddr_match(const struct addrinfo * laddr,const char * r,size_t rlen)908 fetch_ssl_ipaddr_match(const struct addrinfo *laddr, const char *r,
909     size_t rlen)
910 {
911 	struct addrinfo *raddr;
912 	int ret;
913 	char *rip;
914 
915 	ret = 0;
916 	if ((raddr = fetch_ssl_get_numeric_addrinfo(r, rlen)) == NULL)
917 		return 0; /* not a numeric host */
918 
919 	if (laddr->ai_family == raddr->ai_family) {
920 		if (laddr->ai_family == AF_INET) {
921 			rip = (char *)&((struct sockaddr_in *)(void *)
922 			    raddr->ai_addr)->sin_addr.s_addr;
923 			ret = fetch_ssl_ipaddr_match_bin(laddr, rip, 4);
924 #ifdef INET6
925 		} else if (laddr->ai_family == AF_INET6) {
926 			rip = (char *)&((struct sockaddr_in6 *)(void *)
927 			    raddr->ai_addr)->sin6_addr;
928 			ret = fetch_ssl_ipaddr_match_bin(laddr, rip, 16);
929 #endif
930 		}
931 
932 	}
933 	freeaddrinfo(raddr);
934 	return (ret);
935 }
936 
937 /*
938  * Verify server certificate by subjectAltName.
939  */
940 static int
fetch_ssl_verify_altname(STACK_OF (GENERAL_NAME)* altnames,const char * host,struct addrinfo * ip)941 fetch_ssl_verify_altname(STACK_OF(GENERAL_NAME) *altnames,
942     const char *host, struct addrinfo *ip)
943 {
944 	const GENERAL_NAME *name;
945 	size_t nslen;
946 	int i;
947 	const char *ns;
948 
949 	for (i = 0; i < sk_GENERAL_NAME_num(altnames); ++i) {
950 		name = sk_GENERAL_NAME_value(altnames, i);
951 		ns = (const char *)ASN1_STRING_get0_data(name->d.ia5);
952 		nslen = (size_t)ASN1_STRING_length(name->d.ia5);
953 
954 		if (name->type == GEN_DNS && ip == NULL &&
955 		    fetch_ssl_hname_match(host, strlen(host), ns, nslen))
956 			return (1);
957 		else if (name->type == GEN_IPADD && ip != NULL &&
958 		    fetch_ssl_ipaddr_match_bin(ip, ns, nslen))
959 			return (1);
960 	}
961 	return (0);
962 }
963 
964 /*
965  * Verify server certificate by CN.
966  */
967 static int
fetch_ssl_verify_cn(X509_NAME * subject,const char * host,struct addrinfo * ip)968 fetch_ssl_verify_cn(X509_NAME *subject, const char *host,
969     struct addrinfo *ip)
970 {
971 	ASN1_STRING *namedata;
972 	X509_NAME_ENTRY *nameentry;
973 	int cnlen, lastpos, loc, ret;
974 	unsigned char *cn;
975 
976 	ret = 0;
977 	lastpos = -1;
978 	loc = -1;
979 	cn = NULL;
980 	/* get most specific CN (last entry in list) and compare */
981 	while ((lastpos = X509_NAME_get_index_by_NID(subject,
982 	    NID_commonName, lastpos)) != -1)
983 		loc = lastpos;
984 
985 	if (loc > -1) {
986 		nameentry = X509_NAME_get_entry(subject, loc);
987 		namedata = X509_NAME_ENTRY_get_data(nameentry);
988 		cnlen = ASN1_STRING_to_UTF8(&cn, namedata);
989 		if (ip == NULL &&
990 		    fetch_ssl_hname_match(host, strlen(host), cn, cnlen))
991 			ret = 1;
992 		else if (ip != NULL && fetch_ssl_ipaddr_match(ip, cn, cnlen))
993 			ret = 1;
994 		OPENSSL_free(cn);
995 	}
996 	return (ret);
997 }
998 
999 /*
1000  * Verify that server certificate subjectAltName/CN matches
1001  * hostname. First check, if there are alternative subject names. If yes,
1002  * those have to match. Only if those don't exist it falls back to
1003  * checking the subject's CN.
1004  */
1005 static int
fetch_ssl_verify_hname(X509 * cert,const char * host)1006 fetch_ssl_verify_hname(X509 *cert, const char *host)
1007 {
1008 	struct addrinfo *ip;
1009 	STACK_OF(GENERAL_NAME) *altnames;
1010 	X509_NAME *subject;
1011 	int ret;
1012 
1013 	ret = 0;
1014 	ip = fetch_ssl_get_numeric_addrinfo(host, strlen(host));
1015 	altnames = X509_get_ext_d2i(cert, NID_subject_alt_name,
1016 	    NULL, NULL);
1017 
1018 	if (altnames != NULL) {
1019 		ret = fetch_ssl_verify_altname(altnames, host, ip);
1020 	} else {
1021 		subject = X509_get_subject_name(cert);
1022 		if (subject != NULL)
1023 			ret = fetch_ssl_verify_cn(subject, host, ip);
1024 	}
1025 
1026 	if (ip != NULL)
1027 		freeaddrinfo(ip);
1028 	if (altnames != NULL)
1029 		GENERAL_NAMES_free(altnames);
1030 	return (ret);
1031 }
1032 
1033 /*
1034  * Configure transport security layer based on environment.
1035  */
1036 static void
fetch_ssl_setup_transport_layer(SSL_CTX * ctx,int verbose)1037 fetch_ssl_setup_transport_layer(SSL_CTX *ctx, int verbose)
1038 {
1039 	long ssl_ctx_options;
1040 
1041 	ssl_ctx_options = SSL_OP_ALL | SSL_OP_NO_SSLv3 | SSL_OP_NO_TICKET;
1042 	if (getenv("SSL_NO_TLS1") != NULL)
1043 		ssl_ctx_options |= SSL_OP_NO_TLSv1;
1044 	if (getenv("SSL_NO_TLS1_1") != NULL)
1045 		ssl_ctx_options |= SSL_OP_NO_TLSv1_1;
1046 	if (getenv("SSL_NO_TLS1_2") != NULL)
1047 		ssl_ctx_options |= SSL_OP_NO_TLSv1_2;
1048 	if (verbose)
1049 		fetch_info("SSL options: %lx", ssl_ctx_options);
1050 	SSL_CTX_set_options(ctx, ssl_ctx_options);
1051 }
1052 
1053 
1054 /*
1055  * Configure peer verification based on environment.
1056  */
1057 static int
fetch_ssl_setup_peer_verification(SSL_CTX * ctx,int verbose)1058 fetch_ssl_setup_peer_verification(SSL_CTX *ctx, int verbose)
1059 {
1060 	X509_LOOKUP *crl_lookup;
1061 	X509_STORE *crl_store;
1062 	const char *ca_cert_file, *ca_cert_path, *crl_file;
1063 
1064 	if (getenv("SSL_NO_VERIFY_PEER") == NULL) {
1065 		ca_cert_file = getenv("SSL_CA_CERT_FILE");
1066 		ca_cert_path = getenv("SSL_CA_CERT_PATH");
1067 		if (verbose) {
1068 			fetch_info("Peer verification enabled");
1069 			if (ca_cert_file != NULL)
1070 				fetch_info("Using CA cert file: %s",
1071 				    ca_cert_file);
1072 			if (ca_cert_path != NULL)
1073 				fetch_info("Using CA cert path: %s",
1074 				    ca_cert_path);
1075 			if (ca_cert_file == NULL && ca_cert_path == NULL)
1076 				fetch_info("Using OpenSSL default "
1077 				    "CA cert file and path");
1078 		}
1079 		SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER,
1080 		    fetch_ssl_cb_verify_crt);
1081 		if (ca_cert_file != NULL || ca_cert_path != NULL)
1082 			SSL_CTX_load_verify_locations(ctx, ca_cert_file,
1083 			    ca_cert_path);
1084 		else
1085 			SSL_CTX_set_default_verify_paths(ctx);
1086 		if ((crl_file = getenv("SSL_CRL_FILE")) != NULL) {
1087 			if (verbose)
1088 				fetch_info("Using CRL file: %s", crl_file);
1089 			crl_store = SSL_CTX_get_cert_store(ctx);
1090 			crl_lookup = X509_STORE_add_lookup(crl_store,
1091 			    X509_LOOKUP_file());
1092 			if (crl_lookup == NULL ||
1093 			    !X509_load_crl_file(crl_lookup, crl_file,
1094 				X509_FILETYPE_PEM)) {
1095 				fprintf(stderr,
1096 				    "Could not load CRL file %s\n",
1097 				    crl_file);
1098 				return (0);
1099 			}
1100 			X509_STORE_set_flags(crl_store,
1101 			    X509_V_FLAG_CRL_CHECK |
1102 			    X509_V_FLAG_CRL_CHECK_ALL);
1103 		}
1104 	}
1105 	return (1);
1106 }
1107 
1108 /*
1109  * Configure client certificate based on environment.
1110  */
1111 static int
fetch_ssl_setup_client_certificate(SSL_CTX * ctx,int verbose)1112 fetch_ssl_setup_client_certificate(SSL_CTX *ctx, int verbose)
1113 {
1114 	const char *client_cert_file, *client_key_file;
1115 
1116 	if ((client_cert_file = getenv("SSL_CLIENT_CERT_FILE")) != NULL) {
1117 		client_key_file = getenv("SSL_CLIENT_KEY_FILE") != NULL ?
1118 		    getenv("SSL_CLIENT_KEY_FILE") : client_cert_file;
1119 		if (verbose) {
1120 			fetch_info("Using client cert file: %s",
1121 			    client_cert_file);
1122 			fetch_info("Using client key file: %s",
1123 			    client_key_file);
1124 		}
1125 		if (SSL_CTX_use_certificate_chain_file(ctx,
1126 			client_cert_file) != 1) {
1127 			fprintf(stderr,
1128 			    "Could not load client certificate %s\n",
1129 			    client_cert_file);
1130 			return (0);
1131 		}
1132 		if (SSL_CTX_use_PrivateKey_file(ctx, client_key_file,
1133 			SSL_FILETYPE_PEM) != 1) {
1134 			fprintf(stderr,
1135 			    "Could not load client key %s\n",
1136 			    client_key_file);
1137 			return (0);
1138 		}
1139 	}
1140 	return (1);
1141 }
1142 
1143 /*
1144  * Callback for SSL certificate verification, this is called on server
1145  * cert verification. It takes no decision, but informs the user in case
1146  * verification failed.
1147  */
1148 int
fetch_ssl_cb_verify_crt(int verified,X509_STORE_CTX * ctx)1149 fetch_ssl_cb_verify_crt(int verified, X509_STORE_CTX *ctx)
1150 {
1151 	X509 *crt;
1152 	X509_NAME *name;
1153 	char *str;
1154 
1155 	str = NULL;
1156 	if (!verified) {
1157 		if ((crt = X509_STORE_CTX_get_current_cert(ctx)) != NULL &&
1158 		    (name = X509_get_subject_name(crt)) != NULL)
1159 			str = X509_NAME_oneline(name, 0, 0);
1160 		fprintf(stderr, "Certificate verification failed for %s\n",
1161 		    str != NULL ? str : "no relevant certificate");
1162 		OPENSSL_free(str);
1163 	}
1164 	return (verified);
1165 }
1166 
1167 #endif
1168 
1169 /*
1170  * Enable SSL on a connection.
1171  */
1172 int
fetch_ssl(conn_t * conn,const struct url * URL,int verbose)1173 fetch_ssl(conn_t *conn, const struct url *URL, int verbose)
1174 {
1175 #ifdef WITH_SSL
1176 	int ret, ssl_err;
1177 	X509_NAME *name;
1178 	char *str;
1179 
1180 	conn->ssl_meth = SSLv23_client_method();
1181 	conn->ssl_ctx = SSL_CTX_new(conn->ssl_meth);
1182 	SSL_CTX_set_mode(conn->ssl_ctx, SSL_MODE_AUTO_RETRY);
1183 
1184 	fetch_ssl_setup_transport_layer(conn->ssl_ctx, verbose);
1185 	if (!fetch_ssl_setup_peer_verification(conn->ssl_ctx, verbose))
1186 		return (-1);
1187 	if (!fetch_ssl_setup_client_certificate(conn->ssl_ctx, verbose))
1188 		return (-1);
1189 
1190 	conn->ssl = SSL_new(conn->ssl_ctx);
1191 	if (conn->ssl == NULL) {
1192 		fprintf(stderr, "SSL context creation failed\n");
1193 		return (-1);
1194 	}
1195 	SSL_set_fd(conn->ssl, conn->sd);
1196 
1197 #if !defined(OPENSSL_NO_TLSEXT)
1198 	if (!SSL_set_tlsext_host_name(conn->ssl,
1199 	    __DECONST(struct url *, URL)->host)) {
1200 		fprintf(stderr,
1201 		    "TLS server name indication extension failed for host %s\n",
1202 		    URL->host);
1203 		return (-1);
1204 	}
1205 #endif
1206 	while ((ret = SSL_connect(conn->ssl)) == -1) {
1207 		ssl_err = SSL_get_error(conn->ssl, ret);
1208 		if (ssl_err != SSL_ERROR_WANT_READ &&
1209 		    ssl_err != SSL_ERROR_WANT_WRITE) {
1210 			ERR_print_errors_fp(stderr);
1211 			return (-1);
1212 		}
1213 	}
1214 	conn->ssl_cert = SSL_get_peer_certificate(conn->ssl);
1215 
1216 	if (conn->ssl_cert == NULL) {
1217 		fprintf(stderr, "No server SSL certificate\n");
1218 		return (-1);
1219 	}
1220 
1221 	if (getenv("SSL_NO_VERIFY_HOSTNAME") == NULL) {
1222 		if (verbose)
1223 			fetch_info("Verify hostname");
1224 		if (!fetch_ssl_verify_hname(conn->ssl_cert, URL->host)) {
1225 			fprintf(stderr,
1226 			    "SSL certificate subject doesn't match host %s\n",
1227 			    URL->host);
1228 			return (-1);
1229 		}
1230 	}
1231 
1232 	if (verbose) {
1233 		fetch_info("%s connection established using %s",
1234 		    SSL_get_version(conn->ssl), SSL_get_cipher(conn->ssl));
1235 		name = X509_get_subject_name(conn->ssl_cert);
1236 		str = X509_NAME_oneline(name, 0, 0);
1237 		fetch_info("Certificate subject: %s", str);
1238 		OPENSSL_free(str);
1239 		name = X509_get_issuer_name(conn->ssl_cert);
1240 		str = X509_NAME_oneline(name, 0, 0);
1241 		fetch_info("Certificate issuer: %s", str);
1242 		OPENSSL_free(str);
1243 	}
1244 
1245 	return (0);
1246 #else
1247 	(void)conn;
1248 	(void)verbose;
1249 	(void)URL;
1250 	fprintf(stderr, "SSL support disabled\n");
1251 	return (-1);
1252 #endif
1253 }
1254 
1255 #define FETCH_READ_WAIT		-2
1256 #define FETCH_READ_ERROR	-1
1257 #define FETCH_READ_DONE		 0
1258 
1259 #ifdef WITH_SSL
1260 static ssize_t
fetch_ssl_read(SSL * ssl,char * buf,size_t len)1261 fetch_ssl_read(SSL *ssl, char *buf, size_t len)
1262 {
1263 	ssize_t rlen;
1264 	int ssl_err;
1265 
1266 	rlen = SSL_read(ssl, buf, len);
1267 	if (rlen < 0) {
1268 		ssl_err = SSL_get_error(ssl, rlen);
1269 		if (ssl_err == SSL_ERROR_WANT_READ ||
1270 		    ssl_err == SSL_ERROR_WANT_WRITE) {
1271 			return (FETCH_READ_WAIT);
1272 		} else {
1273 			ERR_print_errors_fp(stderr);
1274 			return (FETCH_READ_ERROR);
1275 		}
1276 	}
1277 	return (rlen);
1278 }
1279 #endif
1280 
1281 static ssize_t
fetch_socket_read(int sd,char * buf,size_t len)1282 fetch_socket_read(int sd, char *buf, size_t len)
1283 {
1284 	ssize_t rlen;
1285 
1286 	rlen = read(sd, buf, len);
1287 	if (rlen < 0) {
1288 		if (errno == EAGAIN || (errno == EINTR && fetchRestartCalls))
1289 			return (FETCH_READ_WAIT);
1290 		else
1291 			return (FETCH_READ_ERROR);
1292 	}
1293 	return (rlen);
1294 }
1295 
1296 /*
1297  * Read a character from a connection w/ timeout
1298  */
1299 ssize_t
fetch_read(conn_t * conn,char * buf,size_t len)1300 fetch_read(conn_t *conn, char *buf, size_t len)
1301 {
1302 	struct timeval now, timeout, delta;
1303 	struct pollfd pfd;
1304 	ssize_t rlen;
1305 	int deltams;
1306 
1307 	if (fetchTimeout > 0) {
1308 		gettimeofday(&timeout, NULL);
1309 		timeout.tv_sec += fetchTimeout;
1310 	}
1311 
1312 	deltams = INFTIM;
1313 	memset(&pfd, 0, sizeof pfd);
1314 	pfd.fd = conn->sd;
1315 	pfd.events = POLLIN | POLLERR;
1316 
1317 	for (;;) {
1318 		/*
1319 		 * The socket is non-blocking.  Instead of the canonical
1320 		 * poll() -> read(), we do the following:
1321 		 *
1322 		 * 1) call read() or SSL_read().
1323 		 * 2) if we received some data, return it.
1324 		 * 3) if an error occurred, return -1.
1325 		 * 4) if read() or SSL_read() signaled EOF, return.
1326 		 * 5) if we did not receive any data but we're not at EOF,
1327 		 *    call poll().
1328 		 *
1329 		 * In the SSL case, this is necessary because if we
1330 		 * receive a close notification, we have to call
1331 		 * SSL_read() one additional time after we've read
1332 		 * everything we received.
1333 		 *
1334 		 * In the non-SSL case, it may improve performance (very
1335 		 * slightly) when reading small amounts of data.
1336 		 */
1337 #ifdef WITH_SSL
1338 		if (conn->ssl != NULL)
1339 			rlen = fetch_ssl_read(conn->ssl, buf, len);
1340 		else
1341 #endif
1342 			rlen = fetch_socket_read(conn->sd, buf, len);
1343 		if (rlen >= 0) {
1344 			break;
1345 		} else if (rlen == FETCH_READ_ERROR) {
1346 			fetch_syserr();
1347 			return (-1);
1348 		}
1349 		// assert(rlen == FETCH_READ_WAIT);
1350 		if (fetchTimeout > 0) {
1351 			gettimeofday(&now, NULL);
1352 			if (!timercmp(&timeout, &now, >)) {
1353 				errno = ETIMEDOUT;
1354 				fetch_syserr();
1355 				return (-1);
1356 			}
1357 			timersub(&timeout, &now, &delta);
1358 			deltams = delta.tv_sec * 1000 +
1359 			    delta.tv_usec / 1000;
1360 		}
1361 		errno = 0;
1362 		pfd.revents = 0;
1363 		if (poll(&pfd, 1, deltams) < 0) {
1364 			if (errno == EINTR && fetchRestartCalls)
1365 				continue;
1366 			fetch_syserr();
1367 			return (-1);
1368 		}
1369 	}
1370 	return (rlen);
1371 }
1372 
1373 
1374 /*
1375  * Read a line of text from a connection w/ timeout
1376  */
1377 #define MIN_BUF_SIZE 1024
1378 
1379 int
fetch_getln(conn_t * conn)1380 fetch_getln(conn_t *conn)
1381 {
1382 	char *tmp;
1383 	size_t tmpsize;
1384 	ssize_t len;
1385 	char c;
1386 
1387 	if (conn->buf == NULL) {
1388 		if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) {
1389 			errno = ENOMEM;
1390 			return (-1);
1391 		}
1392 		conn->bufsize = MIN_BUF_SIZE;
1393 	}
1394 
1395 	conn->buf[0] = '\0';
1396 	conn->buflen = 0;
1397 
1398 	do {
1399 		len = fetch_read(conn, &c, 1);
1400 		if (len == -1)
1401 			return (-1);
1402 		if (len == 0)
1403 			break;
1404 		conn->buf[conn->buflen++] = c;
1405 		if (conn->buflen == conn->bufsize) {
1406 			tmp = conn->buf;
1407 			tmpsize = conn->bufsize * 2 + 1;
1408 			if ((tmp = realloc(tmp, tmpsize)) == NULL) {
1409 				errno = ENOMEM;
1410 				return (-1);
1411 			}
1412 			conn->buf = tmp;
1413 			conn->bufsize = tmpsize;
1414 		}
1415 	} while (c != '\n');
1416 
1417 	conn->buf[conn->buflen] = '\0';
1418 	DEBUGF("<<< %s", conn->buf);
1419 	return (0);
1420 }
1421 
1422 
1423 /*
1424  * Write to a connection w/ timeout
1425  */
1426 ssize_t
fetch_write(conn_t * conn,const char * buf,size_t len)1427 fetch_write(conn_t *conn, const char *buf, size_t len)
1428 {
1429 	struct iovec iov;
1430 
1431 	iov.iov_base = __DECONST(char *, buf);
1432 	iov.iov_len = len;
1433 	return fetch_writev(conn, &iov, 1);
1434 }
1435 
1436 /*
1437  * Write a vector to a connection w/ timeout
1438  * Note: can modify the iovec.
1439  */
1440 ssize_t
fetch_writev(conn_t * conn,struct iovec * iov,int iovcnt)1441 fetch_writev(conn_t *conn, struct iovec *iov, int iovcnt)
1442 {
1443 	struct timeval now, timeout, delta;
1444 	struct pollfd pfd;
1445 	ssize_t wlen, total;
1446 	int deltams;
1447 
1448 	memset(&pfd, 0, sizeof pfd);
1449 	if (fetchTimeout) {
1450 		pfd.fd = conn->sd;
1451 		pfd.events = POLLOUT | POLLERR;
1452 		gettimeofday(&timeout, NULL);
1453 		timeout.tv_sec += fetchTimeout;
1454 	}
1455 
1456 	total = 0;
1457 	while (iovcnt > 0) {
1458 		while (fetchTimeout && pfd.revents == 0) {
1459 			gettimeofday(&now, NULL);
1460 			if (!timercmp(&timeout, &now, >)) {
1461 				errno = ETIMEDOUT;
1462 				fetch_syserr();
1463 				return (-1);
1464 			}
1465 			timersub(&timeout, &now, &delta);
1466 			deltams = delta.tv_sec * 1000 +
1467 			    delta.tv_usec / 1000;
1468 			errno = 0;
1469 			pfd.revents = 0;
1470 			if (poll(&pfd, 1, deltams) < 0) {
1471 				/* POSIX compliance */
1472 				if (errno == EAGAIN)
1473 					continue;
1474 				if (errno == EINTR && fetchRestartCalls)
1475 					continue;
1476 				return (-1);
1477 			}
1478 		}
1479 		errno = 0;
1480 #ifdef WITH_SSL
1481 		if (conn->ssl != NULL)
1482 			wlen = SSL_write(conn->ssl,
1483 			    iov->iov_base, iov->iov_len);
1484 		else
1485 #endif
1486 			wlen = writev(conn->sd, iov, iovcnt);
1487 		if (wlen == 0) {
1488 			/* we consider a short write a failure */
1489 			/* XXX perhaps we shouldn't in the SSL case */
1490 			errno = EPIPE;
1491 			fetch_syserr();
1492 			return (-1);
1493 		}
1494 		if (wlen < 0) {
1495 			if (errno == EINTR && fetchRestartCalls)
1496 				continue;
1497 			return (-1);
1498 		}
1499 		total += wlen;
1500 		while (iovcnt > 0 && wlen >= (ssize_t)iov->iov_len) {
1501 			wlen -= iov->iov_len;
1502 			iov++;
1503 			iovcnt--;
1504 		}
1505 		if (iovcnt > 0) {
1506 			iov->iov_len -= wlen;
1507 			iov->iov_base = __DECONST(char *, iov->iov_base) + wlen;
1508 		}
1509 	}
1510 	return (total);
1511 }
1512 
1513 
1514 /*
1515  * Write a line of text to a connection w/ timeout
1516  */
1517 int
fetch_putln(conn_t * conn,const char * str,size_t len)1518 fetch_putln(conn_t *conn, const char *str, size_t len)
1519 {
1520 	struct iovec iov[2];
1521 	int ret;
1522 
1523 	DEBUGF(">>> %s\n", str);
1524 	iov[0].iov_base = __DECONST(char *, str);
1525 	iov[0].iov_len = len;
1526 	iov[1].iov_base = __DECONST(char *, ENDL);
1527 	iov[1].iov_len = sizeof(ENDL);
1528 	if (len == 0)
1529 		ret = fetch_writev(conn, &iov[1], 1);
1530 	else
1531 		ret = fetch_writev(conn, iov, 2);
1532 	if (ret == -1)
1533 		return (-1);
1534 	return (0);
1535 }
1536 
1537 
1538 /*
1539  * Close connection
1540  */
1541 int
fetch_close(conn_t * conn)1542 fetch_close(conn_t *conn)
1543 {
1544 	int ret;
1545 
1546 	if (--conn->ref > 0)
1547 		return (0);
1548 #ifdef WITH_SSL
1549 	if (conn->ssl) {
1550 		SSL_shutdown(conn->ssl);
1551 		SSL_set_connect_state(conn->ssl);
1552 		SSL_free(conn->ssl);
1553 		conn->ssl = NULL;
1554 	}
1555 	if (conn->ssl_ctx) {
1556 		SSL_CTX_free(conn->ssl_ctx);
1557 		conn->ssl_ctx = NULL;
1558 	}
1559 	if (conn->ssl_cert) {
1560 		X509_free(conn->ssl_cert);
1561 		conn->ssl_cert = NULL;
1562 	}
1563 #endif
1564 	ret = close(conn->sd);
1565 	free(conn->buf);
1566 	free(conn);
1567 	return (ret);
1568 }
1569 
1570 
1571 /*** Directory-related utility functions *************************************/
1572 
1573 int
fetch_add_entry(struct url_ent ** p,int * size,int * len,const char * name,struct url_stat * us)1574 fetch_add_entry(struct url_ent **p, int *size, int *len,
1575     const char *name, struct url_stat *us)
1576 {
1577 	struct url_ent *tmp;
1578 
1579 	if (*p == NULL) {
1580 		*size = 0;
1581 		*len = 0;
1582 	}
1583 
1584 	if (*len >= *size - 1) {
1585 		tmp = reallocarray(*p, *size * 2 + 1, sizeof(**p));
1586 		if (tmp == NULL) {
1587 			errno = ENOMEM;
1588 			fetch_syserr();
1589 			return (-1);
1590 		}
1591 		*size = (*size * 2 + 1);
1592 		*p = tmp;
1593 	}
1594 
1595 	tmp = *p + *len;
1596 	snprintf(tmp->name, PATH_MAX, "%s", name);
1597 	memcpy(&tmp->stat, us, sizeof(*us));
1598 
1599 	(*len)++;
1600 	(++tmp)->name[0] = 0;
1601 
1602 	return (0);
1603 }
1604 
1605 
1606 /*** Authentication-related utility functions ********************************/
1607 
1608 static const char *
fetch_read_word(FILE * f)1609 fetch_read_word(FILE *f)
1610 {
1611 	static char word[1024];
1612 
1613 	if (fscanf(f, " %1023s ", word) != 1)
1614 		return (NULL);
1615 	return (word);
1616 }
1617 
1618 static int
fetch_netrc_open(void)1619 fetch_netrc_open(void)
1620 {
1621 	struct passwd *pwd;
1622 	char fn[PATH_MAX];
1623 	const char *p;
1624 	int fd, serrno;
1625 
1626 	if ((p = getenv("NETRC")) != NULL) {
1627 		DEBUGF("NETRC=%s\n", p);
1628 		if (snprintf(fn, sizeof(fn), "%s", p) >= (int)sizeof(fn)) {
1629 			fetch_info("$NETRC specifies a file name "
1630 			    "longer than PATH_MAX");
1631 			return (-1);
1632 		}
1633 	} else {
1634 		if ((p = getenv("HOME")) == NULL) {
1635 			if ((pwd = getpwuid(getuid())) == NULL ||
1636 			    (p = pwd->pw_dir) == NULL)
1637 				return (-1);
1638 		}
1639 		if (snprintf(fn, sizeof(fn), "%s/.netrc", p) >= (int)sizeof(fn))
1640 			return (-1);
1641 	}
1642 
1643 	if ((fd = open(fn, O_RDONLY)) < 0) {
1644 		serrno = errno;
1645 		DEBUGF("%s: %s\n", fn, strerror(serrno));
1646 		errno = serrno;
1647 	}
1648 	return (fd);
1649 }
1650 
1651 /*
1652  * Get authentication data for a URL from .netrc
1653  */
1654 int
fetch_netrc_auth(struct url * url)1655 fetch_netrc_auth(struct url *url)
1656 {
1657 	const char *word;
1658 	int serrno;
1659 	FILE *f;
1660 
1661 	if (url->netrcfd < 0)
1662 		url->netrcfd = fetch_netrc_open();
1663 	if (url->netrcfd < 0)
1664 		return (-1);
1665 	if ((f = fdopen(url->netrcfd, "r")) == NULL) {
1666 		serrno = errno;
1667 		DEBUGF("fdopen(netrcfd): %s", strerror(errno));
1668 		close(url->netrcfd);
1669 		url->netrcfd = -1;
1670 		errno = serrno;
1671 		return (-1);
1672 	}
1673 	rewind(f);
1674 	DEBUGF("searching netrc for %s\n", url->host);
1675 	while ((word = fetch_read_word(f)) != NULL) {
1676 		if (strcmp(word, "default") == 0) {
1677 			DEBUGF("using default netrc settings\n");
1678 			break;
1679 		}
1680 		if (strcmp(word, "machine") == 0 &&
1681 		    (word = fetch_read_word(f)) != NULL &&
1682 		    strcasecmp(word, url->host) == 0) {
1683 			DEBUGF("using netrc settings for %s\n", word);
1684 			break;
1685 		}
1686 	}
1687 	if (word == NULL)
1688 		goto ferr;
1689 	while ((word = fetch_read_word(f)) != NULL) {
1690 		if (strcmp(word, "login") == 0) {
1691 			if ((word = fetch_read_word(f)) == NULL)
1692 				goto ferr;
1693 			if (snprintf(url->user, sizeof(url->user),
1694 				"%s", word) > (int)sizeof(url->user)) {
1695 				fetch_info("login name in .netrc is too long");
1696 				url->user[0] = '\0';
1697 			}
1698 		} else if (strcmp(word, "password") == 0) {
1699 			if ((word = fetch_read_word(f)) == NULL)
1700 				goto ferr;
1701 			if (snprintf(url->pwd, sizeof(url->pwd),
1702 				"%s", word) > (int)sizeof(url->pwd)) {
1703 				fetch_info("password in .netrc is too long");
1704 				url->pwd[0] = '\0';
1705 			}
1706 		} else if (strcmp(word, "account") == 0) {
1707 			if ((word = fetch_read_word(f)) == NULL)
1708 				goto ferr;
1709 			/* XXX not supported! */
1710 		} else {
1711 			break;
1712 		}
1713 	}
1714 	fclose(f);
1715 	url->netrcfd = -1;
1716 	return (0);
1717 ferr:
1718 	serrno = errno;
1719 	fclose(f);
1720 	url->netrcfd = -1;
1721 	errno = serrno;
1722 	return (-1);
1723 }
1724 
1725 /*
1726  * The no_proxy environment variable specifies a set of domains for
1727  * which the proxy should not be consulted; the contents is a comma-,
1728  * or space-separated list of domain names.  A single asterisk will
1729  * override all proxy variables and no transactions will be proxied
1730  * (for compatibility with lynx and curl, see the discussion at
1731  * <http://curl.haxx.se/mail/archive_pre_oct_99/0009.html>).
1732  */
1733 int
fetch_no_proxy_match(const char * host)1734 fetch_no_proxy_match(const char *host)
1735 {
1736 	const char *no_proxy, *p, *q;
1737 	size_t h_len, d_len;
1738 
1739 	if ((no_proxy = getenv("NO_PROXY")) == NULL &&
1740 	    (no_proxy = getenv("no_proxy")) == NULL)
1741 		return (0);
1742 
1743 	/* asterisk matches any hostname */
1744 	if (strcmp(no_proxy, "*") == 0)
1745 		return (1);
1746 
1747 	h_len = strlen(host);
1748 	p = no_proxy;
1749 	do {
1750 		/* position p at the beginning of a domain suffix */
1751 		while (*p == ',' || isspace((unsigned char)*p))
1752 			p++;
1753 
1754 		/* position q at the first separator character */
1755 		for (q = p; *q; ++q)
1756 			if (*q == ',' || isspace((unsigned char)*q))
1757 				break;
1758 
1759 		d_len = q - p;
1760 		if (d_len > 0 && h_len >= d_len &&
1761 		    strncasecmp(host + h_len - d_len,
1762 			p, d_len) == 0) {
1763 			/* domain name matches */
1764 			return (1);
1765 		}
1766 
1767 		p = q + 1;
1768 	} while (*q);
1769 
1770 	return (0);
1771 }
1772