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/param.h>
30 #include <sys/socket.h>
34 #include <netinet/in.h>
51 /*** Local data **************************************************************/
54 * Error messages for resolver errors
56 static struct fetcherr netdb_errlist[] = {
58 { EAI_NODATA, FETCH_RESOLV, "Host not found" },
60 { EAI_AGAIN, FETCH_TEMP, "Transient resolver failure" },
61 { EAI_FAIL, FETCH_RESOLV, "Non-recoverable resolver failure" },
62 { EAI_NONAME, FETCH_RESOLV, "No address record" },
63 { -1, FETCH_UNKNOWN, "Unknown resolver error" }
67 static const char ENDL[2] = "\r\n";
70 /*** Error-reporting functions ***********************************************/
73 * Map error code to string
75 static struct fetcherr *
76 fetch_finderr(struct fetcherr *p, int e)
78 while (p->num != -1 && p->num != e)
87 fetch_seterr(struct fetcherr *p, int e)
89 p = fetch_finderr(p, e);
90 fetchLastErrCode = p->cat;
91 snprintf(fetchLastErrString, MAXERRSTRING, "%s", p->string);
95 * Set error code according to errno
102 fetchLastErrCode = FETCH_OK;
109 fetchLastErrCode = FETCH_AUTH;
112 case EISDIR: /* XXX */
113 fetchLastErrCode = FETCH_UNAVAIL;
116 fetchLastErrCode = FETCH_MEMORY;
120 fetchLastErrCode = FETCH_TEMP;
123 fetchLastErrCode = FETCH_EXISTS;
126 fetchLastErrCode = FETCH_FULL;
134 fetchLastErrCode = FETCH_NETWORK;
138 fetchLastErrCode = FETCH_ABORT;
141 fetchLastErrCode = FETCH_TIMEOUT;
145 fetchLastErrCode = FETCH_DOWN;
148 fetchLastErrCode = FETCH_UNKNOWN;
150 snprintf(fetchLastErrString, MAXERRSTRING, "%s", strerror(errno));
155 * Emit status message
158 fetch_info(const char *fmt, ...)
163 vfprintf(stderr, fmt, ap);
169 /*** Network-related utility functions ***************************************/
172 * Return the default port for a scheme
175 fetch_default_port(const char *scheme)
179 if ((se = getservbyname(scheme, "tcp")) != NULL)
180 return (ntohs(se->s_port));
181 if (strcasecmp(scheme, SCHEME_FTP) == 0)
182 return (FTP_DEFAULT_PORT);
183 if (strcasecmp(scheme, SCHEME_HTTP) == 0)
184 return (HTTP_DEFAULT_PORT);
189 * Return the default proxy port for a scheme
192 fetch_default_proxy_port(const char *scheme)
194 if (strcasecmp(scheme, SCHEME_FTP) == 0)
195 return (FTP_DEFAULT_PROXY_PORT);
196 if (strcasecmp(scheme, SCHEME_HTTP) == 0)
197 return (HTTP_DEFAULT_PROXY_PORT);
203 * Create a connection for an existing descriptor.
211 /* allocate and fill connection structure */
212 if ((conn = calloc(1, sizeof(*conn))) == NULL)
214 fcntl(sd, F_SETFD, FD_CLOEXEC);
215 setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof opt);
223 * Bump a connection's reference count.
226 fetch_ref(conn_t *conn)
235 * Bind a socket to a specific local address
238 fetch_bind(int sd, int af, const char *addr)
240 struct addrinfo hints, *res, *res0;
243 memset(&hints, 0, sizeof(hints));
244 hints.ai_family = af;
245 hints.ai_socktype = SOCK_STREAM;
246 hints.ai_protocol = 0;
247 if ((err = getaddrinfo(addr, NULL, &hints, &res0)) != 0)
249 for (res = res0; res; res = res->ai_next)
250 if (bind(sd, res->ai_addr, res->ai_addrlen) == 0)
257 * Establish a TCP connection to the specified port on the specified host.
260 fetch_connect(const char *host, int port, int af, int verbose)
264 const char *bindaddr;
265 struct addrinfo hints, *res, *res0;
268 DEBUG(fprintf(stderr, "---> %s:%d\n", host, port));
271 fetch_info("looking up %s", host);
273 /* look up host name and set up socket address structure */
274 snprintf(pbuf, sizeof(pbuf), "%d", port);
275 memset(&hints, 0, sizeof(hints));
276 hints.ai_family = af;
277 hints.ai_socktype = SOCK_STREAM;
278 hints.ai_protocol = 0;
279 if ((err = getaddrinfo(host, pbuf, &hints, &res0)) != 0) {
283 bindaddr = getenv("FETCH_BIND_ADDRESS");
286 fetch_info("connecting to %s:%d", host, port);
289 for (sd = -1, res = res0; res; sd = -1, res = res->ai_next) {
290 if ((sd = socket(res->ai_family, res->ai_socktype,
291 res->ai_protocol)) == -1)
293 if (bindaddr != NULL && *bindaddr != '\0' &&
294 fetch_bind(sd, res->ai_family, bindaddr) != 0) {
295 fetch_info("failed to bind to '%s'", bindaddr);
299 if (connect(sd, res->ai_addr, res->ai_addrlen) == 0 &&
300 fcntl(sd, F_SETFL, O_NONBLOCK) == 0)
310 if ((conn = fetch_reopen(sd)) == NULL) {
319 * Enable SSL on a connection.
322 fetch_ssl(conn_t *conn, int verbose)
327 /* Init the SSL library and context */
328 if (!SSL_library_init()){
329 fprintf(stderr, "SSL library init failed\n");
333 SSL_load_error_strings();
335 conn->ssl_meth = SSLv23_client_method();
336 conn->ssl_ctx = SSL_CTX_new(conn->ssl_meth);
337 SSL_CTX_set_mode(conn->ssl_ctx, SSL_MODE_AUTO_RETRY);
339 conn->ssl = SSL_new(conn->ssl_ctx);
340 if (conn->ssl == NULL){
341 fprintf(stderr, "SSL context creation failed\n");
344 SSL_set_fd(conn->ssl, conn->sd);
345 while ((ret = SSL_connect(conn->ssl)) == -1) {
346 ssl_err = SSL_get_error(conn->ssl, ret);
347 if (ssl_err != SSL_ERROR_WANT_READ &&
348 ssl_err != SSL_ERROR_WANT_WRITE) {
349 ERR_print_errors_fp(stderr);
358 fprintf(stderr, "SSL connection established using %s\n",
359 SSL_get_cipher(conn->ssl));
360 conn->ssl_cert = SSL_get_peer_certificate(conn->ssl);
361 name = X509_get_subject_name(conn->ssl_cert);
362 str = X509_NAME_oneline(name, 0, 0);
363 printf("Certificate subject: %s\n", str);
365 name = X509_get_issuer_name(conn->ssl_cert);
366 str = X509_NAME_oneline(name, 0, 0);
367 printf("Certificate issuer: %s\n", str);
375 fprintf(stderr, "SSL support disabled\n");
380 #define FETCH_READ_WAIT -2
381 #define FETCH_READ_ERROR -1
382 #define FETCH_READ_DONE 0
386 fetch_ssl_read(SSL *ssl, char *buf, size_t len)
391 rlen = SSL_read(ssl, buf, len);
393 ssl_err = SSL_get_error(ssl, rlen);
394 if (ssl_err == SSL_ERROR_WANT_READ ||
395 ssl_err == SSL_ERROR_WANT_WRITE) {
396 return (FETCH_READ_WAIT);
398 ERR_print_errors_fp(stderr);
399 return (FETCH_READ_ERROR);
407 * Cache some data that was read from a socket but cannot be immediately
408 * returned because of an interrupted system call.
411 fetch_cache_data(conn_t *conn, char *src, size_t nbytes)
415 if (conn->cache.size < nbytes) {
416 tmp = realloc(conn->cache.buf, nbytes);
421 conn->cache.buf = tmp;
422 conn->cache.size = nbytes;
425 memcpy(conn->cache.buf, src, nbytes);
426 conn->cache.len = nbytes;
434 fetch_socket_read(int sd, char *buf, size_t len)
438 rlen = read(sd, buf, len);
440 if (errno == EAGAIN || (errno == EINTR && fetchRestartCalls))
441 return (FETCH_READ_WAIT);
443 return (FETCH_READ_ERROR);
449 * Read a character from a connection w/ timeout
452 fetch_read(conn_t *conn, char *buf, size_t len)
454 struct timeval now, timeout, delta;
459 if (fetchTimeout > 0) {
460 gettimeofday(&timeout, NULL);
461 timeout.tv_sec += fetchTimeout;
467 if (conn->cache.len > 0) {
469 * The last invocation of fetch_read was interrupted by a
470 * signal after some data had been read from the socket. Copy
471 * the cached data into the supplied buffer before trying to
472 * read from the socket again.
474 total = (conn->cache.len < len) ? conn->cache.len : len;
475 memcpy(buf, conn->cache.buf, total);
477 conn->cache.len -= total;
478 conn->cache.pos += total;
485 * The socket is non-blocking. Instead of the canonical
486 * select() -> read(), we do the following:
488 * 1) call read() or SSL_read().
489 * 2) if an error occurred, return -1.
490 * 3) if we received data but we still expect more,
491 * update our counters and loop.
492 * 4) if read() or SSL_read() signaled EOF, return.
493 * 5) if we did not receive any data but we're not at EOF,
496 * In the SSL case, this is necessary because if we
497 * receive a close notification, we have to call
498 * SSL_read() one additional time after we've read
499 * everything we received.
501 * In the non-SSL case, it may improve performance (very
502 * slightly) when reading small amounts of data.
505 if (conn->ssl != NULL)
506 rlen = fetch_ssl_read(conn->ssl, buf, len);
509 rlen = fetch_socket_read(conn->sd, buf, len);
512 } else if (rlen > 0) {
517 } else if (rlen == FETCH_READ_ERROR) {
519 fetch_cache_data(conn, start, total);
522 // assert(rlen == FETCH_READ_WAIT);
524 while (!FD_ISSET(conn->sd, &readfds)) {
525 FD_SET(conn->sd, &readfds);
526 if (fetchTimeout > 0) {
527 gettimeofday(&now, NULL);
528 if (!timercmp(&timeout, &now, >)) {
533 timersub(&timeout, &now, &delta);
536 if (select(conn->sd + 1, &readfds, NULL, NULL,
537 fetchTimeout > 0 ? &delta : NULL) < 0) {
538 if (errno == EINTR) {
539 if (fetchRestartCalls)
541 /* Save anything that was read. */
542 fetch_cache_data(conn, start, total);
554 * Read a line of text from a connection w/ timeout
556 #define MIN_BUF_SIZE 1024
559 fetch_getln(conn_t *conn)
566 if (conn->buf == NULL) {
567 if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) {
571 conn->bufsize = MIN_BUF_SIZE;
578 len = fetch_read(conn, &c, 1);
583 conn->buf[conn->buflen++] = c;
584 if (conn->buflen == conn->bufsize) {
586 tmpsize = conn->bufsize * 2 + 1;
587 if ((tmp = realloc(tmp, tmpsize)) == NULL) {
592 conn->bufsize = tmpsize;
596 conn->buf[conn->buflen] = '\0';
597 DEBUG(fprintf(stderr, "<<< %s", conn->buf));
603 * Write to a connection w/ timeout
606 fetch_write(conn_t *conn, const char *buf, size_t len)
610 iov.iov_base = __DECONST(char *, buf);
612 return fetch_writev(conn, &iov, 1);
616 * Write a vector to a connection w/ timeout
617 * Note: can modify the iovec.
620 fetch_writev(conn_t *conn, struct iovec *iov, int iovcnt)
622 struct timeval now, timeout, delta;
629 gettimeofday(&timeout, NULL);
630 timeout.tv_sec += fetchTimeout;
635 while (fetchTimeout && !FD_ISSET(conn->sd, &writefds)) {
636 FD_SET(conn->sd, &writefds);
637 gettimeofday(&now, NULL);
638 delta.tv_sec = timeout.tv_sec - now.tv_sec;
639 delta.tv_usec = timeout.tv_usec - now.tv_usec;
640 if (delta.tv_usec < 0) {
641 delta.tv_usec += 1000000;
644 if (delta.tv_sec < 0) {
650 r = select(conn->sd + 1, NULL, &writefds, NULL, &delta);
652 if (errno == EINTR && fetchRestartCalls)
659 if (conn->ssl != NULL)
660 wlen = SSL_write(conn->ssl,
661 iov->iov_base, iov->iov_len);
664 wlen = writev(conn->sd, iov, iovcnt);
666 /* we consider a short write a failure */
667 /* XXX perhaps we shouldn't in the SSL case */
673 if (errno == EINTR && fetchRestartCalls)
678 while (iovcnt > 0 && wlen >= (ssize_t)iov->iov_len) {
679 wlen -= iov->iov_len;
684 iov->iov_len -= wlen;
685 iov->iov_base = __DECONST(char *, iov->iov_base) + wlen;
693 * Write a line of text to a connection w/ timeout
696 fetch_putln(conn_t *conn, const char *str, size_t len)
701 DEBUG(fprintf(stderr, ">>> %s\n", str));
702 iov[0].iov_base = __DECONST(char *, str);
703 iov[0].iov_len = len;
704 iov[1].iov_base = __DECONST(char *, ENDL);
705 iov[1].iov_len = sizeof(ENDL);
707 ret = fetch_writev(conn, &iov[1], 1);
709 ret = fetch_writev(conn, iov, 2);
720 fetch_close(conn_t *conn)
726 ret = close(conn->sd);
727 free(conn->cache.buf);
734 /*** Directory-related utility functions *************************************/
737 fetch_add_entry(struct url_ent **p, int *size, int *len,
738 const char *name, struct url_stat *us)
747 if (*len >= *size - 1) {
748 tmp = realloc(*p, (*size * 2 + 1) * sizeof(**p));
754 *size = (*size * 2 + 1);
759 snprintf(tmp->name, PATH_MAX, "%s", name);
760 memcpy(&tmp->stat, us, sizeof(*us));
763 (++tmp)->name[0] = 0;
769 /*** Authentication-related utility functions ********************************/
772 fetch_read_word(FILE *f)
774 static char word[1024];
776 if (fscanf(f, " %1023s ", word) != 1)
782 * Get authentication data for a URL from .netrc
785 fetch_netrc_auth(struct url *url)
792 if ((p = getenv("NETRC")) != NULL) {
793 if (snprintf(fn, sizeof(fn), "%s", p) >= (int)sizeof(fn)) {
794 fetch_info("$NETRC specifies a file name "
795 "longer than PATH_MAX");
799 if ((p = getenv("HOME")) != NULL) {
802 if ((pwd = getpwuid(getuid())) == NULL ||
803 (p = pwd->pw_dir) == NULL)
806 if (snprintf(fn, sizeof(fn), "%s/.netrc", p) >= (int)sizeof(fn))
810 if ((f = fopen(fn, "r")) == NULL)
812 while ((word = fetch_read_word(f)) != NULL) {
813 if (strcmp(word, "default") == 0) {
814 DEBUG(fetch_info("Using default .netrc settings"));
817 if (strcmp(word, "machine") == 0 &&
818 (word = fetch_read_word(f)) != NULL &&
819 strcasecmp(word, url->host) == 0) {
820 DEBUG(fetch_info("Using .netrc settings for %s", word));
826 while ((word = fetch_read_word(f)) != NULL) {
827 if (strcmp(word, "login") == 0) {
828 if ((word = fetch_read_word(f)) == NULL)
830 if (snprintf(url->user, sizeof(url->user),
831 "%s", word) > (int)sizeof(url->user)) {
832 fetch_info("login name in .netrc is too long");
835 } else if (strcmp(word, "password") == 0) {
836 if ((word = fetch_read_word(f)) == NULL)
838 if (snprintf(url->pwd, sizeof(url->pwd),
839 "%s", word) > (int)sizeof(url->pwd)) {
840 fetch_info("password in .netrc is too long");
843 } else if (strcmp(word, "account") == 0) {
844 if ((word = fetch_read_word(f)) == NULL)
846 /* XXX not supported! */
859 * The no_proxy environment variable specifies a set of domains for
860 * which the proxy should not be consulted; the contents is a comma-,
861 * or space-separated list of domain names. A single asterisk will
862 * override all proxy variables and no transactions will be proxied
863 * (for compatability with lynx and curl, see the discussion at
864 * <http://curl.haxx.se/mail/archive_pre_oct_99/0009.html>).
867 fetch_no_proxy_match(const char *host)
869 const char *no_proxy, *p, *q;
872 if ((no_proxy = getenv("NO_PROXY")) == NULL &&
873 (no_proxy = getenv("no_proxy")) == NULL)
876 /* asterisk matches any hostname */
877 if (strcmp(no_proxy, "*") == 0)
880 h_len = strlen(host);
883 /* position p at the beginning of a domain suffix */
884 while (*p == ',' || isspace((unsigned char)*p))
887 /* position q at the first separator character */
889 if (*q == ',' || isspace((unsigned char)*q))
893 if (d_len > 0 && h_len >= d_len &&
894 strncasecmp(host + h_len - d_len,
896 /* domain name matches */