2 * Copyright (c) 1998-2011 Dag-Erling Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/socket.h>
37 #include <netinet/in.h>
54 /*** Local data **************************************************************/
57 * Error messages for resolver errors
59 static struct fetcherr netdb_errlist[] = {
61 { EAI_NODATA, FETCH_RESOLV, "Host not found" },
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" }
70 static const char ENDL[2] = "\r\n";
73 /*** Error-reporting functions ***********************************************/
76 * Map error code to string
78 static struct fetcherr *
79 fetch_finderr(struct fetcherr *p, int e)
81 while (p->num != -1 && p->num != e)
90 fetch_seterr(struct fetcherr *p, int e)
92 p = fetch_finderr(p, e);
93 fetchLastErrCode = p->cat;
94 snprintf(fetchLastErrString, MAXERRSTRING, "%s", p->string);
98 * Set error code according to errno
105 fetchLastErrCode = FETCH_OK;
112 fetchLastErrCode = FETCH_AUTH;
115 case EISDIR: /* XXX */
116 fetchLastErrCode = FETCH_UNAVAIL;
119 fetchLastErrCode = FETCH_MEMORY;
123 fetchLastErrCode = FETCH_TEMP;
126 fetchLastErrCode = FETCH_EXISTS;
129 fetchLastErrCode = FETCH_FULL;
137 fetchLastErrCode = FETCH_NETWORK;
141 fetchLastErrCode = FETCH_ABORT;
144 fetchLastErrCode = FETCH_TIMEOUT;
148 fetchLastErrCode = FETCH_DOWN;
151 fetchLastErrCode = FETCH_UNKNOWN;
153 snprintf(fetchLastErrString, MAXERRSTRING, "%s", strerror(errno));
158 * Emit status message
161 fetch_info(const char *fmt, ...)
166 vfprintf(stderr, fmt, ap);
172 /*** Network-related utility functions ***************************************/
175 * Return the default port for a scheme
178 fetch_default_port(const char *scheme)
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);
192 * Return the default proxy port for a scheme
195 fetch_default_proxy_port(const char *scheme)
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);
206 * Create a connection for an existing descriptor.
213 /* allocate and fill connection structure */
214 if ((conn = calloc(1, sizeof(*conn))) == NULL)
216 fcntl(sd, F_SETFD, FD_CLOEXEC);
224 * Bump a connection's reference count.
227 fetch_ref(conn_t *conn)
236 * Bind a socket to a specific local address
239 fetch_bind(int sd, int af, const char *addr)
241 struct addrinfo hints, *res, *res0;
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)
250 for (res = res0; res; res = res->ai_next)
251 if (bind(sd, res->ai_addr, res->ai_addrlen) == 0)
258 * Establish a TCP connection to the specified port on the specified host.
261 fetch_connect(const char *host, int port, int af, int verbose)
265 const char *bindaddr;
266 struct addrinfo hints, *res, *res0;
269 DEBUG(fprintf(stderr, "---> %s:%d\n", host, port));
272 fetch_info("looking up %s", host);
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) {
284 bindaddr = getenv("FETCH_BIND_ADDRESS");
287 fetch_info("connecting to %s:%d", host, port);
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)
294 if (bindaddr != NULL && *bindaddr != '\0' &&
295 fetch_bind(sd, res->ai_family, bindaddr) != 0) {
296 fetch_info("failed to bind to '%s'", bindaddr);
300 if (connect(sd, res->ai_addr, res->ai_addrlen) == 0 &&
301 fcntl(sd, F_SETFL, O_NONBLOCK) == 0)
311 if ((conn = fetch_reopen(sd)) == NULL) {
320 * Enable SSL on a connection.
323 fetch_ssl(conn_t *conn, int verbose)
328 /* Init the SSL library and context */
329 if (!SSL_library_init()){
330 fprintf(stderr, "SSL library init failed\n");
334 SSL_load_error_strings();
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);
340 conn->ssl = SSL_new(conn->ssl_ctx);
341 if (conn->ssl == NULL){
342 fprintf(stderr, "SSL context creation failed\n");
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);
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);
366 name = X509_get_issuer_name(conn->ssl_cert);
367 str = X509_NAME_oneline(name, 0, 0);
368 printf("Certificate issuer: %s\n", str);
376 fprintf(stderr, "SSL support disabled\n");
381 #define FETCH_READ_WAIT -2
382 #define FETCH_READ_ERROR -1
383 #define FETCH_READ_DONE 0
387 fetch_ssl_read(SSL *ssl, char *buf, size_t len)
392 rlen = SSL_read(ssl, buf, len);
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);
399 ERR_print_errors_fp(stderr);
400 return (FETCH_READ_ERROR);
408 * Cache some data that was read from a socket but cannot be immediately
409 * returned because of an interrupted system call.
412 fetch_cache_data(conn_t *conn, char *src, size_t nbytes)
416 if (conn->cache.size < nbytes) {
417 tmp = realloc(conn->cache.buf, nbytes);
422 conn->cache.buf = tmp;
423 conn->cache.size = nbytes;
426 memcpy(conn->cache.buf, src, nbytes);
427 conn->cache.len = nbytes;
435 fetch_socket_read(int sd, char *buf, size_t len)
439 rlen = read(sd, buf, len);
441 if (errno == EAGAIN || (errno == EINTR && fetchRestartCalls))
442 return (FETCH_READ_WAIT);
444 return (FETCH_READ_ERROR);
450 * Read a character from a connection w/ timeout
453 fetch_read(conn_t *conn, char *buf, size_t len)
455 struct timeval now, timeout, delta;
460 if (fetchTimeout > 0) {
461 gettimeofday(&timeout, NULL);
462 timeout.tv_sec += fetchTimeout;
468 if (conn->cache.len > 0) {
470 * The last invocation of fetch_read was interrupted by a
471 * signal after some data had been read from the socket. Copy
472 * the cached data into the supplied buffer before trying to
473 * read from the socket again.
475 total = (conn->cache.len < len) ? conn->cache.len : len;
476 memcpy(buf, conn->cache.buf, total);
478 conn->cache.len -= total;
479 conn->cache.pos += total;
486 * The socket is non-blocking. Instead of the canonical
487 * select() -> read(), we do the following:
489 * 1) call read() or SSL_read().
490 * 2) if an error occurred, return -1.
491 * 3) if we received data but we still expect more,
492 * update our counters and loop.
493 * 4) if read() or SSL_read() signaled EOF, return.
494 * 5) if we did not receive any data but we're not at EOF,
497 * In the SSL case, this is necessary because if we
498 * receive a close notification, we have to call
499 * SSL_read() one additional time after we've read
500 * everything we received.
502 * In the non-SSL case, it may improve performance (very
503 * slightly) when reading small amounts of data.
506 if (conn->ssl != NULL)
507 rlen = fetch_ssl_read(conn->ssl, buf, len);
510 rlen = fetch_socket_read(conn->sd, buf, len);
513 } else if (rlen > 0) {
518 } else if (rlen == FETCH_READ_ERROR) {
520 fetch_cache_data(conn, start, total);
523 // assert(rlen == FETCH_READ_WAIT);
525 while (!FD_ISSET(conn->sd, &readfds)) {
526 FD_SET(conn->sd, &readfds);
527 if (fetchTimeout > 0) {
528 gettimeofday(&now, NULL);
529 if (!timercmp(&timeout, &now, >)) {
534 timersub(&timeout, &now, &delta);
537 if (select(conn->sd + 1, &readfds, NULL, NULL,
538 fetchTimeout > 0 ? &delta : NULL) < 0) {
539 if (errno == EINTR) {
540 if (fetchRestartCalls)
542 /* Save anything that was read. */
543 fetch_cache_data(conn, start, total);
555 * Read a line of text from a connection w/ timeout
557 #define MIN_BUF_SIZE 1024
560 fetch_getln(conn_t *conn)
567 if (conn->buf == NULL) {
568 if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) {
572 conn->bufsize = MIN_BUF_SIZE;
579 len = fetch_read(conn, &c, 1);
584 conn->buf[conn->buflen++] = c;
585 if (conn->buflen == conn->bufsize) {
587 tmpsize = conn->bufsize * 2 + 1;
588 if ((tmp = realloc(tmp, tmpsize)) == NULL) {
593 conn->bufsize = tmpsize;
597 conn->buf[conn->buflen] = '\0';
598 DEBUG(fprintf(stderr, "<<< %s", conn->buf));
604 * Write to a connection w/ timeout
607 fetch_write(conn_t *conn, const char *buf, size_t len)
611 iov.iov_base = __DECONST(char *, buf);
613 return fetch_writev(conn, &iov, 1);
617 * Write a vector to a connection w/ timeout
618 * Note: can modify the iovec.
621 fetch_writev(conn_t *conn, struct iovec *iov, int iovcnt)
623 struct timeval now, timeout, delta;
630 gettimeofday(&timeout, NULL);
631 timeout.tv_sec += fetchTimeout;
636 while (fetchTimeout && !FD_ISSET(conn->sd, &writefds)) {
637 FD_SET(conn->sd, &writefds);
638 gettimeofday(&now, NULL);
639 delta.tv_sec = timeout.tv_sec - now.tv_sec;
640 delta.tv_usec = timeout.tv_usec - now.tv_usec;
641 if (delta.tv_usec < 0) {
642 delta.tv_usec += 1000000;
645 if (delta.tv_sec < 0) {
651 r = select(conn->sd + 1, NULL, &writefds, NULL, &delta);
653 if (errno == EINTR && fetchRestartCalls)
660 if (conn->ssl != NULL)
661 wlen = SSL_write(conn->ssl,
662 iov->iov_base, iov->iov_len);
665 wlen = writev(conn->sd, iov, iovcnt);
667 /* we consider a short write a failure */
668 /* XXX perhaps we shouldn't in the SSL case */
674 if (errno == EINTR && fetchRestartCalls)
679 while (iovcnt > 0 && wlen >= (ssize_t)iov->iov_len) {
680 wlen -= iov->iov_len;
685 iov->iov_len -= wlen;
686 iov->iov_base = __DECONST(char *, iov->iov_base) + wlen;
694 * Write a line of text to a connection w/ timeout
697 fetch_putln(conn_t *conn, const char *str, size_t len)
702 DEBUG(fprintf(stderr, ">>> %s\n", str));
703 iov[0].iov_base = __DECONST(char *, str);
704 iov[0].iov_len = len;
705 iov[1].iov_base = __DECONST(char *, ENDL);
706 iov[1].iov_len = sizeof(ENDL);
708 ret = fetch_writev(conn, &iov[1], 1);
710 ret = fetch_writev(conn, iov, 2);
721 fetch_close(conn_t *conn)
727 ret = close(conn->sd);
728 free(conn->cache.buf);
735 /*** Directory-related utility functions *************************************/
738 fetch_add_entry(struct url_ent **p, int *size, int *len,
739 const char *name, struct url_stat *us)
748 if (*len >= *size - 1) {
749 tmp = realloc(*p, (*size * 2 + 1) * sizeof(**p));
755 *size = (*size * 2 + 1);
760 snprintf(tmp->name, PATH_MAX, "%s", name);
761 memcpy(&tmp->stat, us, sizeof(*us));
764 (++tmp)->name[0] = 0;
770 /*** Authentication-related utility functions ********************************/
773 fetch_read_word(FILE *f)
775 static char word[1024];
777 if (fscanf(f, " %1023s ", word) != 1)
783 * Get authentication data for a URL from .netrc
786 fetch_netrc_auth(struct url *url)
793 if ((p = getenv("NETRC")) != NULL) {
794 if (snprintf(fn, sizeof(fn), "%s", p) >= (int)sizeof(fn)) {
795 fetch_info("$NETRC specifies a file name "
796 "longer than PATH_MAX");
800 if ((p = getenv("HOME")) != NULL) {
803 if ((pwd = getpwuid(getuid())) == NULL ||
804 (p = pwd->pw_dir) == NULL)
807 if (snprintf(fn, sizeof(fn), "%s/.netrc", p) >= (int)sizeof(fn))
811 if ((f = fopen(fn, "r")) == NULL)
813 while ((word = fetch_read_word(f)) != NULL) {
814 if (strcmp(word, "default") == 0) {
815 DEBUG(fetch_info("Using default .netrc settings"));
818 if (strcmp(word, "machine") == 0 &&
819 (word = fetch_read_word(f)) != NULL &&
820 strcasecmp(word, url->host) == 0) {
821 DEBUG(fetch_info("Using .netrc settings for %s", word));
827 while ((word = fetch_read_word(f)) != NULL) {
828 if (strcmp(word, "login") == 0) {
829 if ((word = fetch_read_word(f)) == NULL)
831 if (snprintf(url->user, sizeof(url->user),
832 "%s", word) > (int)sizeof(url->user)) {
833 fetch_info("login name in .netrc is too long");
836 } else if (strcmp(word, "password") == 0) {
837 if ((word = fetch_read_word(f)) == NULL)
839 if (snprintf(url->pwd, sizeof(url->pwd),
840 "%s", word) > (int)sizeof(url->pwd)) {
841 fetch_info("password in .netrc is too long");
844 } else if (strcmp(word, "account") == 0) {
845 if ((word = fetch_read_word(f)) == NULL)
847 /* XXX not supported! */
860 * The no_proxy environment variable specifies a set of domains for
861 * which the proxy should not be consulted; the contents is a comma-,
862 * or space-separated list of domain names. A single asterisk will
863 * override all proxy variables and no transactions will be proxied
864 * (for compatability with lynx and curl, see the discussion at
865 * <http://curl.haxx.se/mail/archive_pre_oct_99/0009.html>).
868 fetch_no_proxy_match(const char *host)
870 const char *no_proxy, *p, *q;
873 if ((no_proxy = getenv("NO_PROXY")) == NULL &&
874 (no_proxy = getenv("no_proxy")) == NULL)
877 /* asterisk matches any hostname */
878 if (strcmp(no_proxy, "*") == 0)
881 h_len = strlen(host);
884 /* position p at the beginning of a domain suffix */
885 while (*p == ',' || isspace((unsigned char)*p))
888 /* position q at the first separator character */
890 if (*q == ',' || isspace((unsigned char)*q))
894 if (d_len > 0 && h_len >= d_len &&
895 strncasecmp(host + h_len - d_len,
897 /* domain name matches */