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