build - Fix modifying operation to /usr/obj during installworld.
[dragonfly.git] / lib / libfetch / common.c
blob4d63eda8aa234a14548094d4677efbec245203f0
1 /*-
2 * Copyright (c) 1998-2014 Dag-Erling Smørgrav
3 * Copyright (c) 2013 Michael Gmelin <freebsd@grem.de>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: head/lib/libfetch/common.c 266291 2014-05-17 03:39:56Z des $
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <sys/time.h>
35 #include <sys/uio.h>
37 #include <netinet/in.h>
39 #include <ctype.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <netdb.h>
43 #include <poll.h>
44 #include <pwd.h>
45 #include <stdarg.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <unistd.h>
51 #ifdef WITH_SSL
52 #include <openssl/x509v3.h>
53 #endif
55 #include "fetch.h"
56 #include "common.h"
59 /*** Local data **************************************************************/
62 * Error messages for resolver errors
64 static struct fetcherr netdb_errlist[] = {
65 #ifdef EAI_NODATA
66 { EAI_NODATA, FETCH_RESOLV, "Host not found" },
67 #endif
68 { EAI_AGAIN, FETCH_TEMP, "Transient resolver failure" },
69 { EAI_FAIL, FETCH_RESOLV, "Non-recoverable resolver failure" },
70 { EAI_NONAME, FETCH_RESOLV, "No address record" },
71 { -1, FETCH_UNKNOWN, "Unknown resolver error" }
74 /* End-of-Line */
75 static const char ENDL[2] = "\r\n";
78 /*** Error-reporting functions ***********************************************/
81 * Map error code to string
83 static struct fetcherr *
84 fetch_finderr(struct fetcherr *p, int e)
86 while (p->num != -1 && p->num != e)
87 p++;
88 return (p);
92 * Set error code
94 void
95 fetch_seterr(struct fetcherr *p, int e)
97 p = fetch_finderr(p, e);
98 fetchLastErrCode = p->cat;
99 snprintf(fetchLastErrString, MAXERRSTRING, "%s", p->string);
103 * Set error code according to errno
105 void
106 fetch_syserr(void)
108 switch (errno) {
109 case 0:
110 fetchLastErrCode = FETCH_OK;
111 break;
112 case EPERM:
113 case EACCES:
114 case EROFS:
115 case EAUTH:
116 case ENEEDAUTH:
117 fetchLastErrCode = FETCH_AUTH;
118 break;
119 case ENOENT:
120 case EISDIR: /* XXX */
121 fetchLastErrCode = FETCH_UNAVAIL;
122 break;
123 case ENOMEM:
124 fetchLastErrCode = FETCH_MEMORY;
125 break;
126 case EBUSY:
127 case EAGAIN:
128 fetchLastErrCode = FETCH_TEMP;
129 break;
130 case EEXIST:
131 fetchLastErrCode = FETCH_EXISTS;
132 break;
133 case ENOSPC:
134 fetchLastErrCode = FETCH_FULL;
135 break;
136 case EADDRINUSE:
137 case EADDRNOTAVAIL:
138 case ENETDOWN:
139 case ENETUNREACH:
140 case ENETRESET:
141 case EHOSTUNREACH:
142 fetchLastErrCode = FETCH_NETWORK;
143 break;
144 case ECONNABORTED:
145 case ECONNRESET:
146 fetchLastErrCode = FETCH_ABORT;
147 break;
148 case ETIMEDOUT:
149 fetchLastErrCode = FETCH_TIMEOUT;
150 break;
151 case ECONNREFUSED:
152 case EHOSTDOWN:
153 fetchLastErrCode = FETCH_DOWN;
154 break;
155 default:
156 fetchLastErrCode = FETCH_UNKNOWN;
158 snprintf(fetchLastErrString, MAXERRSTRING, "%s", strerror(errno));
163 * Emit status message
165 void
166 fetch_info(const char *fmt, ...)
168 va_list ap;
170 va_start(ap, fmt);
171 vfprintf(stderr, fmt, ap);
172 va_end(ap);
173 fputc('\n', stderr);
177 /*** Network-related utility functions ***************************************/
180 * Return the default port for a scheme
183 fetch_default_port(const char *scheme)
185 struct servent *se;
187 if ((se = getservbyname(scheme, "tcp")) != NULL)
188 return (ntohs(se->s_port));
189 if (strcasecmp(scheme, SCHEME_FTP) == 0)
190 return (FTP_DEFAULT_PORT);
191 if (strcasecmp(scheme, SCHEME_HTTP) == 0)
192 return (HTTP_DEFAULT_PORT);
193 return (0);
197 * Return the default proxy port for a scheme
200 fetch_default_proxy_port(const char *scheme)
202 if (strcasecmp(scheme, SCHEME_FTP) == 0)
203 return (FTP_DEFAULT_PROXY_PORT);
204 if (strcasecmp(scheme, SCHEME_HTTP) == 0)
205 return (HTTP_DEFAULT_PROXY_PORT);
206 return (0);
211 * Create a connection for an existing descriptor.
213 conn_t *
214 fetch_reopen(int sd)
216 conn_t *conn;
217 int opt = 1;
219 /* allocate and fill connection structure */
220 if ((conn = calloc(1, sizeof(*conn))) == NULL)
221 return (NULL);
222 fcntl(sd, F_SETFD, FD_CLOEXEC);
223 setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof opt);
224 conn->sd = sd;
225 ++conn->ref;
226 return (conn);
231 * Bump a connection's reference count.
233 conn_t *
234 fetch_ref(conn_t *conn)
237 ++conn->ref;
238 return (conn);
243 * Bind a socket to a specific local address
246 fetch_bind(int sd, int af, const char *addr)
248 struct addrinfo hints, *res, *res0;
249 int err;
251 memset(&hints, 0, sizeof(hints));
252 hints.ai_family = af;
253 hints.ai_socktype = SOCK_STREAM;
254 hints.ai_protocol = 0;
255 if ((err = getaddrinfo(addr, NULL, &hints, &res0)) != 0)
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 return (-1);
265 * Establish a TCP connection to the specified port on the specified host.
267 conn_t *
268 fetch_connect(const char *host, int port, int af, int verbose)
270 conn_t *conn;
271 char pbuf[10];
272 const char *bindaddr;
273 struct addrinfo hints, *res, *res0;
274 int sd, err;
276 DEBUG(fprintf(stderr, "---> %s:%d\n", host, port));
278 if (verbose)
279 fetch_info("looking up %s", host);
281 /* look up host name and set up socket address structure */
282 snprintf(pbuf, sizeof(pbuf), "%d", port);
283 memset(&hints, 0, sizeof(hints));
284 hints.ai_family = af;
285 hints.ai_socktype = SOCK_STREAM;
286 hints.ai_protocol = 0;
287 if ((err = getaddrinfo(host, pbuf, &hints, &res0)) != 0) {
288 netdb_seterr(err);
289 return (NULL);
291 bindaddr = getenv("FETCH_BIND_ADDRESS");
293 if (verbose)
294 fetch_info("connecting to %s:%d", host, port);
296 /* try to connect */
297 for (sd = -1, res = res0; res; sd = -1, res = res->ai_next) {
298 if ((sd = socket(res->ai_family, res->ai_socktype,
299 res->ai_protocol)) == -1)
300 continue;
301 if (bindaddr != NULL && *bindaddr != '\0' &&
302 fetch_bind(sd, res->ai_family, bindaddr) != 0) {
303 fetch_info("failed to bind to '%s'", bindaddr);
304 close(sd);
305 continue;
307 if (connect(sd, res->ai_addr, res->ai_addrlen) == 0 &&
308 fcntl(sd, F_SETFL, O_NONBLOCK) == 0)
309 break;
310 close(sd);
312 freeaddrinfo(res0);
313 if (sd == -1) {
314 fetch_syserr();
315 return (NULL);
318 if ((conn = fetch_reopen(sd)) == NULL) {
319 fetch_syserr();
320 close(sd);
322 return (conn);
325 #ifdef WITH_SSL
327 * Convert characters A-Z to lowercase (intentionally avoid any locale
328 * specific conversions).
330 static char
331 fetch_ssl_tolower(char in)
333 if (in >= 'A' && in <= 'Z')
334 return (in + 32);
335 else
336 return (in);
340 * isalpha implementation that intentionally avoids any locale specific
341 * conversions.
343 static int
344 fetch_ssl_isalpha(char in)
346 return ((in >= 'A' && in <= 'Z') || (in >= 'a' && in <= 'z'));
350 * Check if passed hostnames a and b are equal.
352 static int
353 fetch_ssl_hname_equal(const char *a, size_t alen, const char *b,
354 size_t blen)
356 size_t i;
358 if (alen != blen)
359 return (0);
360 for (i = 0; i < alen; ++i) {
361 if (fetch_ssl_tolower(a[i]) != fetch_ssl_tolower(b[i]))
362 return (0);
364 return (1);
368 * Check if domain label is traditional, meaning that only A-Z, a-z, 0-9
369 * and '-' (hyphen) are allowed. Hyphens have to be surrounded by alpha-
370 * numeric characters. Double hyphens (like they're found in IDN a-labels
371 * 'xn--') are not allowed. Empty labels are invalid.
373 static int
374 fetch_ssl_is_trad_domain_label(const char *l, size_t len, int wcok)
376 size_t i;
378 if (!len || l[0] == '-' || l[len-1] == '-')
379 return (0);
380 for (i = 0; i < len; ++i) {
381 if (!isdigit(l[i]) &&
382 !fetch_ssl_isalpha(l[i]) &&
383 !(l[i] == '*' && wcok) &&
384 !(l[i] == '-' && l[i - 1] != '-'))
385 return (0);
387 return (1);
391 * Check if host name consists only of numbers. This might indicate an IP
392 * address, which is not a good idea for CN wildcard comparison.
394 static int
395 fetch_ssl_hname_is_only_numbers(const char *hostname, size_t len)
397 size_t i;
399 for (i = 0; i < len; ++i) {
400 if (!((hostname[i] >= '0' && hostname[i] <= '9') ||
401 hostname[i] == '.'))
402 return (0);
404 return (1);
408 * Check if the host name h passed matches the pattern passed in m which
409 * is usually part of subjectAltName or CN of a certificate presented to
410 * the client. This includes wildcard matching. The algorithm is based on
411 * RFC6125, sections 6.4.3 and 7.2, which clarifies RFC2818 and RFC3280.
413 static int
414 fetch_ssl_hname_match(const char *h, size_t hlen, const char *m,
415 size_t mlen)
417 int delta, hdotidx, mdot1idx, wcidx;
418 const char *hdot, *mdot1, *mdot2;
419 const char *wc; /* wildcard */
421 if (!(h && *h && m && *m))
422 return (0);
423 if ((wc = strnstr(m, "*", mlen)) == NULL)
424 return (fetch_ssl_hname_equal(h, hlen, m, mlen));
425 wcidx = wc - m;
426 /* hostname should not be just dots and numbers */
427 if (fetch_ssl_hname_is_only_numbers(h, hlen))
428 return (0);
429 /* only one wildcard allowed in pattern */
430 if (strnstr(wc + 1, "*", mlen - wcidx - 1) != NULL)
431 return (0);
433 * there must be at least two more domain labels and
434 * wildcard has to be in the leftmost label (RFC6125)
436 mdot1 = strnstr(m, ".", mlen);
437 if (mdot1 == NULL || mdot1 < wc || (mlen - (mdot1 - m)) < 4)
438 return (0);
439 mdot1idx = mdot1 - m;
440 mdot2 = strnstr(mdot1 + 1, ".", mlen - mdot1idx - 1);
441 if (mdot2 == NULL || (mlen - (mdot2 - m)) < 2)
442 return (0);
443 /* hostname must contain a dot and not be the 1st char */
444 hdot = strnstr(h, ".", hlen);
445 if (hdot == NULL || hdot == h)
446 return (0);
447 hdotidx = hdot - h;
449 * host part of hostname must be at least as long as
450 * pattern it's supposed to match
452 if (hdotidx < mdot1idx)
453 return (0);
455 * don't allow wildcards in non-traditional domain names
456 * (IDN, A-label, U-label...)
458 if (!fetch_ssl_is_trad_domain_label(h, hdotidx, 0) ||
459 !fetch_ssl_is_trad_domain_label(m, mdot1idx, 1))
460 return (0);
461 /* match domain part (part after first dot) */
462 if (!fetch_ssl_hname_equal(hdot, hlen - hdotidx, mdot1,
463 mlen - mdot1idx))
464 return (0);
465 /* match part left of wildcard */
466 if (!fetch_ssl_hname_equal(h, wcidx, m, wcidx))
467 return (0);
468 /* match part right of wildcard */
469 delta = mdot1idx - wcidx - 1;
470 if (!fetch_ssl_hname_equal(hdot - delta, delta,
471 mdot1 - delta, delta))
472 return (0);
473 /* all tests succeded, it's a match */
474 return (1);
478 * Get numeric host address info - returns NULL if host was not an IP
479 * address. The caller is responsible for deallocation using
480 * freeaddrinfo(3).
482 static struct addrinfo *
483 fetch_ssl_get_numeric_addrinfo(const char *hostname, size_t len)
485 struct addrinfo hints, *res;
486 char *host;
488 host = (char *)malloc(len + 1);
489 memcpy(host, hostname, len);
490 host[len] = '\0';
491 memset(&hints, 0, sizeof(hints));
492 hints.ai_family = PF_UNSPEC;
493 hints.ai_socktype = SOCK_STREAM;
494 hints.ai_protocol = 0;
495 hints.ai_flags = AI_NUMERICHOST;
496 /* port is not relevant for this purpose */
497 getaddrinfo(host, "443", &hints, &res);
498 free(host);
499 return res;
503 * Compare ip address in addrinfo with address passes.
505 static int
506 fetch_ssl_ipaddr_match_bin(const struct addrinfo *lhost, const char *rhost,
507 size_t rhostlen)
509 const void *left;
511 if (lhost->ai_family == AF_INET && rhostlen == 4) {
512 left = (void *)&((struct sockaddr_in*)(void *)
513 lhost->ai_addr)->sin_addr.s_addr;
514 #ifdef INET6
515 } else if (lhost->ai_family == AF_INET6 && rhostlen == 16) {
516 left = (void *)&((struct sockaddr_in6 *)(void *)
517 lhost->ai_addr)->sin6_addr;
518 #endif
519 } else
520 return (0);
521 return (!memcmp(left, (const void *)rhost, rhostlen) ? 1 : 0);
525 * Compare ip address in addrinfo with host passed. If host is not an IP
526 * address, comparison will fail.
528 static int
529 fetch_ssl_ipaddr_match(const struct addrinfo *laddr, const char *r,
530 size_t rlen)
532 struct addrinfo *raddr;
533 int ret;
534 char *rip;
536 ret = 0;
537 if ((raddr = fetch_ssl_get_numeric_addrinfo(r, rlen)) == NULL)
538 return 0; /* not a numeric host */
540 if (laddr->ai_family == raddr->ai_family) {
541 if (laddr->ai_family == AF_INET) {
542 rip = (char *)&((struct sockaddr_in *)(void *)
543 raddr->ai_addr)->sin_addr.s_addr;
544 ret = fetch_ssl_ipaddr_match_bin(laddr, rip, 4);
545 #ifdef INET6
546 } else if (laddr->ai_family == AF_INET6) {
547 rip = (char *)&((struct sockaddr_in6 *)(void *)
548 raddr->ai_addr)->sin6_addr;
549 ret = fetch_ssl_ipaddr_match_bin(laddr, rip, 16);
550 #endif
554 freeaddrinfo(raddr);
555 return (ret);
559 * Verify server certificate by subjectAltName.
561 static int
562 fetch_ssl_verify_altname(STACK_OF(GENERAL_NAME) *altnames,
563 const char *host, struct addrinfo *ip)
565 const GENERAL_NAME *name;
566 size_t nslen;
567 int i;
568 const char *ns;
570 for (i = 0; i < sk_GENERAL_NAME_num(altnames); ++i) {
571 #if OPENSSL_VERSION_NUMBER < 0x10000000L
573 * This is a workaround, since the following line causes
574 * alignment issues in clang:
575 * name = sk_GENERAL_NAME_value(altnames, i);
576 * OpenSSL explicitly warns not to use those macros
577 * directly, but there isn't much choice (and there
578 * shouldn't be any ill side effects)
580 name = (GENERAL_NAME *)SKM_sk_value(void, altnames, i);
581 #else
582 name = sk_GENERAL_NAME_value(altnames, i);
583 #endif
584 ns = (const char *)ASN1_STRING_data(name->d.ia5);
585 nslen = (size_t)ASN1_STRING_length(name->d.ia5);
587 if (name->type == GEN_DNS && ip == NULL &&
588 fetch_ssl_hname_match(host, strlen(host), ns, nslen))
589 return (1);
590 else if (name->type == GEN_IPADD && ip != NULL &&
591 fetch_ssl_ipaddr_match_bin(ip, ns, nslen))
592 return (1);
594 return (0);
598 * Verify server certificate by CN.
600 static int
601 fetch_ssl_verify_cn(X509_NAME *subject, const char *host,
602 struct addrinfo *ip)
604 ASN1_STRING *namedata;
605 X509_NAME_ENTRY *nameentry;
606 int cnlen, lastpos, loc, ret;
607 unsigned char *cn;
609 ret = 0;
610 lastpos = -1;
611 loc = -1;
612 cn = NULL;
613 /* get most specific CN (last entry in list) and compare */
614 while ((lastpos = X509_NAME_get_index_by_NID(subject,
615 NID_commonName, lastpos)) != -1)
616 loc = lastpos;
618 if (loc > -1) {
619 nameentry = X509_NAME_get_entry(subject, loc);
620 namedata = X509_NAME_ENTRY_get_data(nameentry);
621 cnlen = ASN1_STRING_to_UTF8(&cn, namedata);
622 if (ip == NULL &&
623 fetch_ssl_hname_match(host, strlen(host), cn, cnlen))
624 ret = 1;
625 else if (ip != NULL && fetch_ssl_ipaddr_match(ip, cn, cnlen))
626 ret = 1;
627 OPENSSL_free(cn);
629 return (ret);
633 * Verify that server certificate subjectAltName/CN matches
634 * hostname. First check, if there are alternative subject names. If yes,
635 * those have to match. Only if those don't exist it falls back to
636 * checking the subject's CN.
638 static int
639 fetch_ssl_verify_hname(X509 *cert, const char *host)
641 struct addrinfo *ip;
642 STACK_OF(GENERAL_NAME) *altnames;
643 X509_NAME *subject;
644 int ret;
646 ret = 0;
647 ip = fetch_ssl_get_numeric_addrinfo(host, strlen(host));
648 altnames = X509_get_ext_d2i(cert, NID_subject_alt_name,
649 NULL, NULL);
651 if (altnames != NULL) {
652 ret = fetch_ssl_verify_altname(altnames, host, ip);
653 } else {
654 subject = X509_get_subject_name(cert);
655 if (subject != NULL)
656 ret = fetch_ssl_verify_cn(subject, host, ip);
659 if (ip != NULL)
660 freeaddrinfo(ip);
661 if (altnames != NULL)
662 GENERAL_NAMES_free(altnames);
663 return (ret);
667 * Configure transport security layer based on environment.
669 static void
670 fetch_ssl_setup_transport_layer(SSL_CTX *ctx, int verbose)
672 long ssl_ctx_options;
674 ssl_ctx_options = SSL_OP_ALL | SSL_OP_NO_TICKET;
675 if (getenv("SSL_ALLOW_SSL2") == NULL)
676 ssl_ctx_options |= SSL_OP_NO_SSLv2;
677 if (getenv("SSL_NO_SSL3") != NULL)
678 ssl_ctx_options |= SSL_OP_NO_SSLv3;
679 if (getenv("SSL_NO_TLS1") != NULL)
680 ssl_ctx_options |= SSL_OP_NO_TLSv1;
681 if (verbose)
682 fetch_info("SSL options: %lx", ssl_ctx_options);
683 SSL_CTX_set_options(ctx, ssl_ctx_options);
688 * Configure peer verification based on environment.
690 #define LOCAL_CERT_FILE "/usr/local/etc/ssl/cert.pem"
691 #define BASE_CERT_FILE "/etc/ssl/cert.pem"
692 static int
693 fetch_ssl_setup_peer_verification(SSL_CTX *ctx, int verbose)
695 X509_LOOKUP *crl_lookup;
696 X509_STORE *crl_store;
697 const char *ca_cert_file, *ca_cert_path, *crl_file;
699 if (getenv("SSL_NO_VERIFY_PEER") == NULL) {
700 ca_cert_file = getenv("SSL_CA_CERT_FILE");
701 if (ca_cert_file == NULL &&
702 access(LOCAL_CERT_FILE, R_OK) == 0)
703 ca_cert_file = LOCAL_CERT_FILE;
704 if (ca_cert_file == NULL)
705 ca_cert_file = BASE_CERT_FILE;
706 ca_cert_path = getenv("SSL_CA_CERT_PATH");
707 if (verbose) {
708 fetch_info("Peer verification enabled");
709 if (ca_cert_file != NULL)
710 fetch_info("Using CA cert file: %s",
711 ca_cert_file);
712 if (ca_cert_path != NULL)
713 fetch_info("Using CA cert path: %s",
714 ca_cert_path);
716 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER,
717 fetch_ssl_cb_verify_crt);
718 SSL_CTX_load_verify_locations(ctx, ca_cert_file,
719 ca_cert_path);
720 if ((crl_file = getenv("SSL_CRL_FILE")) != NULL) {
721 if (verbose)
722 fetch_info("Using CRL file: %s", crl_file);
723 crl_store = SSL_CTX_get_cert_store(ctx);
724 crl_lookup = X509_STORE_add_lookup(crl_store,
725 X509_LOOKUP_file());
726 if (crl_lookup == NULL ||
727 !X509_load_crl_file(crl_lookup, crl_file,
728 X509_FILETYPE_PEM)) {
729 fprintf(stderr,
730 "Could not load CRL file %s\n",
731 crl_file);
732 return (0);
734 X509_STORE_set_flags(crl_store,
735 X509_V_FLAG_CRL_CHECK |
736 X509_V_FLAG_CRL_CHECK_ALL);
739 return (1);
743 * Configure client certificate based on environment.
745 static int
746 fetch_ssl_setup_client_certificate(SSL_CTX *ctx, int verbose)
748 const char *client_cert_file, *client_key_file;
750 if ((client_cert_file = getenv("SSL_CLIENT_CERT_FILE")) != NULL) {
751 client_key_file = getenv("SSL_CLIENT_KEY_FILE") != NULL ?
752 getenv("SSL_CLIENT_KEY_FILE") : client_cert_file;
753 if (verbose) {
754 fetch_info("Using client cert file: %s",
755 client_cert_file);
756 fetch_info("Using client key file: %s",
757 client_key_file);
759 if (SSL_CTX_use_certificate_chain_file(ctx,
760 client_cert_file) != 1) {
761 fprintf(stderr,
762 "Could not load client certificate %s\n",
763 client_cert_file);
764 return (0);
766 if (SSL_CTX_use_PrivateKey_file(ctx, client_key_file,
767 SSL_FILETYPE_PEM) != 1) {
768 fprintf(stderr,
769 "Could not load client key %s\n",
770 client_key_file);
771 return (0);
774 return (1);
778 * Callback for SSL certificate verification, this is called on server
779 * cert verification. It takes no decision, but informs the user in case
780 * verification failed.
783 fetch_ssl_cb_verify_crt(int verified, X509_STORE_CTX *ctx)
785 X509 *crt;
786 X509_NAME *name;
787 char *str;
789 str = NULL;
790 if (!verified) {
791 if ((crt = X509_STORE_CTX_get_current_cert(ctx)) != NULL &&
792 (name = X509_get_subject_name(crt)) != NULL)
793 str = X509_NAME_oneline(name, 0, 0);
794 fprintf(stderr, "Certificate verification failed for %s\n",
795 str != NULL ? str : "no relevant certificate");
796 OPENSSL_free(str);
798 return (verified);
801 #endif
804 * Enable SSL on a connection.
807 fetch_ssl(conn_t *conn, const struct url *URL, int verbose)
809 #ifdef WITH_SSL
810 int ret, ssl_err;
811 X509_NAME *name;
812 char *str;
814 /* Init the SSL library and context */
815 if (!SSL_library_init()){
816 fprintf(stderr, "SSL library init failed\n");
817 return (-1);
820 SSL_load_error_strings();
822 conn->ssl_meth = SSLv23_client_method();
823 conn->ssl_ctx = SSL_CTX_new(conn->ssl_meth);
824 SSL_CTX_set_mode(conn->ssl_ctx, SSL_MODE_AUTO_RETRY);
826 fetch_ssl_setup_transport_layer(conn->ssl_ctx, verbose);
827 if (!fetch_ssl_setup_peer_verification(conn->ssl_ctx, verbose))
828 return (-1);
829 if (!fetch_ssl_setup_client_certificate(conn->ssl_ctx, verbose))
830 return (-1);
832 conn->ssl = SSL_new(conn->ssl_ctx);
833 if (conn->ssl == NULL) {
834 fprintf(stderr, "SSL context creation failed\n");
835 return (-1);
837 SSL_set_fd(conn->ssl, conn->sd);
839 #if OPENSSL_VERSION_NUMBER >= 0x0090806fL && !defined(OPENSSL_NO_TLSEXT)
840 if (!SSL_set_tlsext_host_name(conn->ssl,
841 __DECONST(struct url *, URL)->host)) {
842 fprintf(stderr,
843 "TLS server name indication extension failed for host %s\n",
844 URL->host);
845 return (-1);
847 #endif
848 while ((ret = SSL_connect(conn->ssl)) == -1) {
849 ssl_err = SSL_get_error(conn->ssl, ret);
850 if (ssl_err != SSL_ERROR_WANT_READ &&
851 ssl_err != SSL_ERROR_WANT_WRITE) {
852 ERR_print_errors_fp(stderr);
853 return (-1);
856 conn->ssl_cert = SSL_get_peer_certificate(conn->ssl);
858 if (conn->ssl_cert == NULL) {
859 fprintf(stderr, "No server SSL certificate\n");
860 return (-1);
863 if (getenv("SSL_NO_VERIFY_HOSTNAME") == NULL) {
864 if (verbose)
865 fetch_info("Verify hostname");
866 if (!fetch_ssl_verify_hname(conn->ssl_cert, URL->host)) {
867 fprintf(stderr,
868 "SSL certificate subject doesn't match host %s\n",
869 URL->host);
870 return (-1);
874 if (verbose) {
875 fetch_info("SSL connection established using %s",
876 SSL_get_cipher(conn->ssl));
877 name = X509_get_subject_name(conn->ssl_cert);
878 str = X509_NAME_oneline(name, 0, 0);
879 fetch_info("Certificate subject: %s", str);
880 OPENSSL_free(str);
881 name = X509_get_issuer_name(conn->ssl_cert);
882 str = X509_NAME_oneline(name, 0, 0);
883 fetch_info("Certificate issuer: %s", str);
884 OPENSSL_free(str);
887 return (0);
888 #else
889 (void)conn;
890 (void)verbose;
891 fprintf(stderr, "SSL support disabled\n");
892 return (-1);
893 #endif
896 #define FETCH_READ_WAIT -2
897 #define FETCH_READ_ERROR -1
898 #define FETCH_READ_DONE 0
900 #ifdef WITH_SSL
901 static ssize_t
902 fetch_ssl_read(SSL *ssl, char *buf, size_t len)
904 ssize_t rlen;
905 int ssl_err;
907 rlen = SSL_read(ssl, buf, len);
908 if (rlen < 0) {
909 ssl_err = SSL_get_error(ssl, rlen);
910 if (ssl_err == SSL_ERROR_WANT_READ ||
911 ssl_err == SSL_ERROR_WANT_WRITE) {
912 return (FETCH_READ_WAIT);
913 } else {
914 ERR_print_errors_fp(stderr);
915 return (FETCH_READ_ERROR);
918 return (rlen);
920 #endif
922 static ssize_t
923 fetch_socket_read(int sd, char *buf, size_t len)
925 ssize_t rlen;
927 rlen = read(sd, buf, len);
928 if (rlen < 0) {
929 if (errno == EAGAIN || (errno == EINTR && fetchRestartCalls))
930 return (FETCH_READ_WAIT);
931 else
932 return (FETCH_READ_ERROR);
934 return (rlen);
938 * Read a character from a connection w/ timeout
940 ssize_t
941 fetch_read(conn_t *conn, char *buf, size_t len)
943 struct timeval now, timeout, delta;
944 struct pollfd pfd;
945 ssize_t rlen;
946 int deltams;
948 if (fetchTimeout > 0) {
949 gettimeofday(&timeout, NULL);
950 timeout.tv_sec += fetchTimeout;
953 deltams = INFTIM;
954 memset(&pfd, 0, sizeof pfd);
955 pfd.fd = conn->sd;
956 pfd.events = POLLIN | POLLERR;
958 for (;;) {
960 * The socket is non-blocking. Instead of the canonical
961 * poll() -> read(), we do the following:
963 * 1) call read() or SSL_read().
964 * 2) if we received some data, return it.
965 * 3) if an error occurred, return -1.
966 * 4) if read() or SSL_read() signaled EOF, return.
967 * 5) if we did not receive any data but we're not at EOF,
968 * call poll().
970 * In the SSL case, this is necessary because if we
971 * receive a close notification, we have to call
972 * SSL_read() one additional time after we've read
973 * everything we received.
975 * In the non-SSL case, it may improve performance (very
976 * slightly) when reading small amounts of data.
978 #ifdef WITH_SSL
979 if (conn->ssl != NULL)
980 rlen = fetch_ssl_read(conn->ssl, buf, len);
981 else
982 #endif
983 rlen = fetch_socket_read(conn->sd, buf, len);
984 if (rlen >= 0) {
985 break;
986 } else if (rlen == FETCH_READ_ERROR) {
987 fetch_syserr();
988 return (-1);
990 // assert(rlen == FETCH_READ_WAIT);
991 if (fetchTimeout > 0) {
992 gettimeofday(&now, NULL);
993 if (!timercmp(&timeout, &now, >)) {
994 errno = ETIMEDOUT;
995 fetch_syserr();
996 return (-1);
998 timersub(&timeout, &now, &delta);
999 deltams = delta.tv_sec * 1000 +
1000 delta.tv_usec / 1000;;
1002 errno = 0;
1003 pfd.revents = 0;
1004 if (poll(&pfd, 1, deltams) < 0) {
1005 if (errno == EINTR && fetchRestartCalls)
1006 continue;
1007 fetch_syserr();
1008 return (-1);
1011 return (rlen);
1016 * Read a line of text from a connection w/ timeout
1018 #define MIN_BUF_SIZE 1024
1021 fetch_getln(conn_t *conn)
1023 char *tmp;
1024 size_t tmpsize;
1025 ssize_t len;
1026 char c;
1028 if (conn->buf == NULL) {
1029 if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) {
1030 errno = ENOMEM;
1031 return (-1);
1033 conn->bufsize = MIN_BUF_SIZE;
1036 conn->buf[0] = '\0';
1037 conn->buflen = 0;
1039 do {
1040 len = fetch_read(conn, &c, 1);
1041 if (len == -1)
1042 return (-1);
1043 if (len == 0)
1044 break;
1045 conn->buf[conn->buflen++] = c;
1046 if (conn->buflen == conn->bufsize) {
1047 tmp = conn->buf;
1048 tmpsize = conn->bufsize * 2 + 1;
1049 if ((tmp = realloc(tmp, tmpsize)) == NULL) {
1050 errno = ENOMEM;
1051 return (-1);
1053 conn->buf = tmp;
1054 conn->bufsize = tmpsize;
1056 } while (c != '\n');
1058 conn->buf[conn->buflen] = '\0';
1059 DEBUG(fprintf(stderr, "<<< %s", conn->buf));
1060 return (0);
1065 * Write to a connection w/ timeout
1067 ssize_t
1068 fetch_write(conn_t *conn, const char *buf, size_t len)
1070 struct iovec iov;
1072 iov.iov_base = __DECONST(char *, buf);
1073 iov.iov_len = len;
1074 return fetch_writev(conn, &iov, 1);
1078 * Write a vector to a connection w/ timeout
1079 * Note: can modify the iovec.
1081 ssize_t
1082 fetch_writev(conn_t *conn, struct iovec *iov, int iovcnt)
1084 struct timeval now, timeout, delta;
1085 struct pollfd pfd;
1086 ssize_t wlen, total;
1087 int deltams;
1089 memset(&pfd, 0, sizeof pfd);
1090 if (fetchTimeout) {
1091 pfd.fd = conn->sd;
1092 pfd.events = POLLOUT | POLLERR;
1093 gettimeofday(&timeout, NULL);
1094 timeout.tv_sec += fetchTimeout;
1097 total = 0;
1098 while (iovcnt > 0) {
1099 while (fetchTimeout && pfd.revents == 0) {
1100 gettimeofday(&now, NULL);
1101 if (!timercmp(&timeout, &now, >)) {
1102 errno = ETIMEDOUT;
1103 fetch_syserr();
1104 return (-1);
1106 timersub(&timeout, &now, &delta);
1107 deltams = delta.tv_sec * 1000 +
1108 delta.tv_usec / 1000;
1109 errno = 0;
1110 pfd.revents = 0;
1111 if (poll(&pfd, 1, deltams) < 0) {
1112 if (errno == EINTR && fetchRestartCalls)
1113 continue;
1114 return (-1);
1117 errno = 0;
1118 #ifdef WITH_SSL
1119 if (conn->ssl != NULL)
1120 wlen = SSL_write(conn->ssl,
1121 iov->iov_base, iov->iov_len);
1122 else
1123 #endif
1124 wlen = writev(conn->sd, iov, iovcnt);
1125 if (wlen == 0) {
1126 /* we consider a short write a failure */
1127 /* XXX perhaps we shouldn't in the SSL case */
1128 errno = EPIPE;
1129 fetch_syserr();
1130 return (-1);
1132 if (wlen < 0) {
1133 if (errno == EINTR && fetchRestartCalls)
1134 continue;
1135 return (-1);
1137 total += wlen;
1138 while (iovcnt > 0 && wlen >= (ssize_t)iov->iov_len) {
1139 wlen -= iov->iov_len;
1140 iov++;
1141 iovcnt--;
1143 if (iovcnt > 0) {
1144 iov->iov_len -= wlen;
1145 iov->iov_base = __DECONST(char *, iov->iov_base) + wlen;
1148 return (total);
1153 * Write a line of text to a connection w/ timeout
1156 fetch_putln(conn_t *conn, const char *str, size_t len)
1158 struct iovec iov[2];
1159 int ret;
1161 DEBUG(fprintf(stderr, ">>> %s\n", str));
1162 iov[0].iov_base = __DECONST(char *, str);
1163 iov[0].iov_len = len;
1164 iov[1].iov_base = __DECONST(char *, ENDL);
1165 iov[1].iov_len = sizeof(ENDL);
1166 if (len == 0)
1167 ret = fetch_writev(conn, &iov[1], 1);
1168 else
1169 ret = fetch_writev(conn, iov, 2);
1170 if (ret == -1)
1171 return (-1);
1172 return (0);
1177 * Close connection
1180 fetch_close(conn_t *conn)
1182 int ret;
1184 if (--conn->ref > 0)
1185 return (0);
1186 #ifdef WITH_SSL
1187 if (conn->ssl) {
1188 SSL_shutdown(conn->ssl);
1189 SSL_set_connect_state(conn->ssl);
1190 SSL_free(conn->ssl);
1191 conn->ssl = NULL;
1193 if (conn->ssl_ctx) {
1194 SSL_CTX_free(conn->ssl_ctx);
1195 conn->ssl_ctx = NULL;
1197 if (conn->ssl_cert) {
1198 X509_free(conn->ssl_cert);
1199 conn->ssl_cert = NULL;
1201 #endif
1202 ret = close(conn->sd);
1203 free(conn->buf);
1204 free(conn);
1205 return (ret);
1209 /*** Directory-related utility functions *************************************/
1212 fetch_add_entry(struct url_ent **p, int *size, int *len,
1213 const char *name, struct url_stat *us)
1215 struct url_ent *tmp;
1217 if (*p == NULL) {
1218 *size = 0;
1219 *len = 0;
1222 if (*len >= *size - 1) {
1223 tmp = realloc(*p, (*size * 2 + 1) * sizeof(**p));
1224 if (tmp == NULL) {
1225 errno = ENOMEM;
1226 fetch_syserr();
1227 return (-1);
1229 *size = (*size * 2 + 1);
1230 *p = tmp;
1233 tmp = *p + *len;
1234 snprintf(tmp->name, PATH_MAX, "%s", name);
1235 memcpy(&tmp->stat, us, sizeof(*us));
1237 (*len)++;
1238 (++tmp)->name[0] = 0;
1240 return (0);
1244 /*** Authentication-related utility functions ********************************/
1246 static const char *
1247 fetch_read_word(FILE *f)
1249 static char word[1024];
1251 if (fscanf(f, " %1023s ", word) != 1)
1252 return (NULL);
1253 return (word);
1257 * Get authentication data for a URL from .netrc
1260 fetch_netrc_auth(struct url *url)
1262 char fn[PATH_MAX];
1263 const char *word;
1264 char *p;
1265 FILE *f;
1267 if ((p = getenv("NETRC")) != NULL) {
1268 if (snprintf(fn, sizeof(fn), "%s", p) >= (int)sizeof(fn)) {
1269 fetch_info("$NETRC specifies a file name "
1270 "longer than PATH_MAX");
1271 return (-1);
1273 } else {
1274 if ((p = getenv("HOME")) != NULL) {
1275 struct passwd *pwd;
1277 if ((pwd = getpwuid(getuid())) == NULL ||
1278 (p = pwd->pw_dir) == NULL)
1279 return (-1);
1281 if (snprintf(fn, sizeof(fn), "%s/.netrc", p) >= (int)sizeof(fn))
1282 return (-1);
1285 if ((f = fopen(fn, "r")) == NULL)
1286 return (-1);
1287 while ((word = fetch_read_word(f)) != NULL) {
1288 if (strcmp(word, "default") == 0) {
1289 DEBUG(fetch_info("Using default .netrc settings"));
1290 break;
1292 if (strcmp(word, "machine") == 0 &&
1293 (word = fetch_read_word(f)) != NULL &&
1294 strcasecmp(word, url->host) == 0) {
1295 DEBUG(fetch_info("Using .netrc settings for %s", word));
1296 break;
1299 if (word == NULL)
1300 goto ferr;
1301 while ((word = fetch_read_word(f)) != NULL) {
1302 if (strcmp(word, "login") == 0) {
1303 if ((word = fetch_read_word(f)) == NULL)
1304 goto ferr;
1305 if (snprintf(url->user, sizeof(url->user),
1306 "%s", word) > (int)sizeof(url->user)) {
1307 fetch_info("login name in .netrc is too long");
1308 url->user[0] = '\0';
1310 } else if (strcmp(word, "password") == 0) {
1311 if ((word = fetch_read_word(f)) == NULL)
1312 goto ferr;
1313 if (snprintf(url->pwd, sizeof(url->pwd),
1314 "%s", word) > (int)sizeof(url->pwd)) {
1315 fetch_info("password in .netrc is too long");
1316 url->pwd[0] = '\0';
1318 } else if (strcmp(word, "account") == 0) {
1319 if ((word = fetch_read_word(f)) == NULL)
1320 goto ferr;
1321 /* XXX not supported! */
1322 } else {
1323 break;
1326 fclose(f);
1327 return (0);
1328 ferr:
1329 fclose(f);
1330 return (-1);
1334 * The no_proxy environment variable specifies a set of domains for
1335 * which the proxy should not be consulted; the contents is a comma-,
1336 * or space-separated list of domain names. A single asterisk will
1337 * override all proxy variables and no transactions will be proxied
1338 * (for compatability with lynx and curl, see the discussion at
1339 * <http://curl.haxx.se/mail/archive_pre_oct_99/0009.html>).
1342 fetch_no_proxy_match(const char *host)
1344 const char *no_proxy, *p, *q;
1345 size_t h_len, d_len;
1347 if ((no_proxy = getenv("NO_PROXY")) == NULL &&
1348 (no_proxy = getenv("no_proxy")) == NULL)
1349 return (0);
1351 /* asterisk matches any hostname */
1352 if (strcmp(no_proxy, "*") == 0)
1353 return (1);
1355 h_len = strlen(host);
1356 p = no_proxy;
1357 do {
1358 /* position p at the beginning of a domain suffix */
1359 while (*p == ',' || isspace((unsigned char)*p))
1360 p++;
1362 /* position q at the first separator character */
1363 for (q = p; *q; ++q)
1364 if (*q == ',' || isspace((unsigned char)*q))
1365 break;
1367 d_len = q - p;
1368 if (d_len > 0 && h_len >= d_len &&
1369 strncasecmp(host + h_len - d_len,
1370 p, d_len) == 0) {
1371 /* domain name matches */
1372 return (1);
1375 p = q + 1;
1376 } while (*q);
1378 return (0);