common.c revision 109960
1/*-
2 * Copyright (c) 1998 Dag-Erling Co�dan 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/cdefs.h>
30__FBSDID("$FreeBSD: head/lib/libfetch/common.c 109960 2003-01-28 00:33:53Z jwd $");
31
32#include <sys/param.h>
33#include <sys/socket.h>
34#include <sys/time.h>
35#include <sys/uio.h>
36#include <netinet/in.h>
37
38#include <errno.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 */
56static struct fetcherr _netdb_errlist[] = {
57	{ EAI_NODATA,	FETCH_RESOLV,	"Host not found" },
58	{ EAI_AGAIN,	FETCH_TEMP,	"Transient resolver failure" },
59	{ EAI_FAIL,	FETCH_RESOLV,	"Non-recoverable resolver failure" },
60	{ EAI_NONAME,	FETCH_RESOLV,	"No address record" },
61	{ -1,		FETCH_UNKNOWN,	"Unknown resolver error" }
62};
63
64/* End-of-Line */
65static const char ENDL[2] = "\r\n";
66
67
68/*** Error-reporting functions ***********************************************/
69
70/*
71 * Map error code to string
72 */
73static struct fetcherr *
74_fetch_finderr(struct fetcherr *p, int e)
75{
76	while (p->num != -1 && p->num != e)
77		p++;
78	return (p);
79}
80
81/*
82 * Set error code
83 */
84void
85_fetch_seterr(struct fetcherr *p, int e)
86{
87	p = _fetch_finderr(p, e);
88	fetchLastErrCode = p->cat;
89	snprintf(fetchLastErrString, MAXERRSTRING, "%s", p->string);
90}
91
92/*
93 * Set error code according to errno
94 */
95void
96_fetch_syserr(void)
97{
98	switch (errno) {
99	case 0:
100		fetchLastErrCode = FETCH_OK;
101		break;
102	case EPERM:
103	case EACCES:
104	case EROFS:
105	case EAUTH:
106	case ENEEDAUTH:
107		fetchLastErrCode = FETCH_AUTH;
108		break;
109	case ENOENT:
110	case EISDIR: /* XXX */
111		fetchLastErrCode = FETCH_UNAVAIL;
112		break;
113	case ENOMEM:
114		fetchLastErrCode = FETCH_MEMORY;
115		break;
116	case EBUSY:
117	case EAGAIN:
118		fetchLastErrCode = FETCH_TEMP;
119		break;
120	case EEXIST:
121		fetchLastErrCode = FETCH_EXISTS;
122		break;
123	case ENOSPC:
124		fetchLastErrCode = FETCH_FULL;
125		break;
126	case EADDRINUSE:
127	case EADDRNOTAVAIL:
128	case ENETDOWN:
129	case ENETUNREACH:
130	case ENETRESET:
131	case EHOSTUNREACH:
132		fetchLastErrCode = FETCH_NETWORK;
133		break;
134	case ECONNABORTED:
135	case ECONNRESET:
136		fetchLastErrCode = FETCH_ABORT;
137		break;
138	case ETIMEDOUT:
139		fetchLastErrCode = FETCH_TIMEOUT;
140		break;
141	case ECONNREFUSED:
142	case EHOSTDOWN:
143		fetchLastErrCode = FETCH_DOWN;
144		break;
145default:
146		fetchLastErrCode = FETCH_UNKNOWN;
147	}
148	snprintf(fetchLastErrString, MAXERRSTRING, "%s", strerror(errno));
149}
150
151
152/*
153 * Emit status message
154 */
155void
156_fetch_info(const char *fmt, ...)
157{
158	va_list ap;
159
160	va_start(ap, fmt);
161	vfprintf(stderr, fmt, ap);
162	va_end(ap);
163	fputc('\n', stderr);
164}
165
166
167/*** Network-related utility functions ***************************************/
168
169/*
170 * Return the default port for a scheme
171 */
172int
173_fetch_default_port(const char *scheme)
174{
175	struct servent *se;
176
177	if ((se = getservbyname(scheme, "tcp")) != NULL)
178		return (ntohs(se->s_port));
179	if (strcasecmp(scheme, SCHEME_FTP) == 0)
180		return (FTP_DEFAULT_PORT);
181	if (strcasecmp(scheme, SCHEME_HTTP) == 0)
182		return (HTTP_DEFAULT_PORT);
183	return (0);
184}
185
186/*
187 * Return the default proxy port for a scheme
188 */
189int
190_fetch_default_proxy_port(const char *scheme)
191{
192	if (strcasecmp(scheme, SCHEME_FTP) == 0)
193		return (FTP_DEFAULT_PROXY_PORT);
194	if (strcasecmp(scheme, SCHEME_HTTP) == 0)
195		return (HTTP_DEFAULT_PROXY_PORT);
196	return (0);
197}
198
199
200/*
201 * Create a connection for an existing descriptor.
202 */
203conn_t *
204_fetch_reopen(int sd)
205{
206	conn_t *conn;
207
208	/* allocate and fill connection structure */
209	if ((conn = calloc(1, sizeof *conn)) == NULL)
210		return (NULL);
211	conn->sd = sd;
212	++conn->ref;
213	return (conn);
214}
215
216
217/*
218 * Bump a connection's reference count.
219 */
220conn_t *
221_fetch_ref(conn_t *conn)
222{
223
224	++conn->ref;
225	return (conn);
226}
227
228
229/*
230 * Establish a TCP connection to the specified port on the specified host.
231 */
232conn_t *
233_fetch_connect(const char *host, int port, int af, int verbose)
234{
235	conn_t *conn;
236	char pbuf[10];
237	struct addrinfo hints, *res, *res0;
238	int sd, err;
239
240	DEBUG(fprintf(stderr, "---> %s:%d\n", host, port));
241
242	if (verbose)
243		_fetch_info("looking up %s", host);
244
245	/* look up host name and set up socket address structure */
246	snprintf(pbuf, sizeof(pbuf), "%d", port);
247	memset(&hints, 0, sizeof(hints));
248	hints.ai_family = af;
249	hints.ai_socktype = SOCK_STREAM;
250	hints.ai_protocol = 0;
251	if ((err = getaddrinfo(host, pbuf, &hints, &res0)) != 0) {
252		_netdb_seterr(err);
253		return (NULL);
254	}
255
256	if (verbose)
257		_fetch_info("connecting to %s:%d", host, port);
258
259	/* try to connect */
260	for (sd = -1, res = res0; res; res = res->ai_next) {
261		if ((sd = socket(res->ai_family, res->ai_socktype,
262			 res->ai_protocol)) == -1)
263			continue;
264		if (connect(sd, res->ai_addr, res->ai_addrlen) != -1)
265			break;
266		close(sd);
267		sd = -1;
268	}
269	freeaddrinfo(res0);
270	if (sd == -1) {
271		_fetch_syserr();
272		return (NULL);
273	}
274
275	if ((conn = _fetch_reopen(sd)) == NULL) {
276		_fetch_syserr();
277		close(sd);
278	}
279	return (conn);
280}
281
282
283/*
284 * Enable SSL on a connection.
285 */
286int
287_fetch_ssl(conn_t *conn, int verbose)
288{
289
290#ifdef WITH_SSL
291	/* Init the SSL library and context */
292	if (!SSL_library_init()){
293		fprintf(stderr, "SSL library init failed\n");
294		return (-1);
295	}
296
297	SSL_load_error_strings();
298
299	conn->ssl_meth = SSLv23_client_method();
300	conn->ssl_ctx = SSL_CTX_new(conn->ssl_meth);
301	SSL_CTX_set_mode(conn->ssl_ctx, SSL_MODE_AUTO_RETRY);
302
303	conn->ssl = SSL_new(conn->ssl_ctx);
304	if (conn->ssl == NULL){
305		fprintf(stderr, "SSL context creation failed\n");
306		return (-1);
307	}
308	SSL_set_fd(conn->ssl, conn->sd);
309	if (SSL_connect(conn->ssl) == -1){
310		ERR_print_errors_fp(stderr);
311		return (-1);
312	}
313
314	if (verbose) {
315		X509_NAME *name;
316		char *str;
317
318		fprintf(stderr, "SSL connection established using %s\n",
319		    SSL_get_cipher(conn->ssl));
320		conn->ssl_cert = SSL_get_peer_certificate(conn->ssl);
321		name = X509_get_subject_name(conn->ssl_cert);
322		str = X509_NAME_oneline(name, 0, 0);
323		printf("Certificate subject: %s\n", str);
324		free(str);
325		name = X509_get_issuer_name(conn->ssl_cert);
326		str = X509_NAME_oneline(name, 0, 0);
327		printf("Certificate issuer: %s\n", str);
328		free(str);
329	}
330
331	return (0);
332#else
333	(void)conn;
334	(void)verbose;
335	fprintf(stderr, "SSL support disabled\n");
336	return (-1);
337#endif
338}
339
340
341/*
342 * Read a character from a connection w/ timeout
343 */
344ssize_t
345_fetch_read(conn_t *conn, char *buf, size_t len)
346{
347	struct timeval now, timeout, wait;
348	fd_set readfds;
349	ssize_t rlen, total;
350	int r;
351
352	if (fetchTimeout) {
353		FD_ZERO(&readfds);
354		gettimeofday(&timeout, NULL);
355		timeout.tv_sec += fetchTimeout;
356	}
357
358	total = 0;
359	while (len > 0) {
360		while (fetchTimeout && !FD_ISSET(conn->sd, &readfds)) {
361			FD_SET(conn->sd, &readfds);
362			gettimeofday(&now, NULL);
363			wait.tv_sec = timeout.tv_sec - now.tv_sec;
364			wait.tv_usec = timeout.tv_usec - now.tv_usec;
365			if (wait.tv_usec < 0) {
366				wait.tv_usec += 1000000;
367				wait.tv_sec--;
368			}
369			if (wait.tv_sec < 0) {
370				errno = ETIMEDOUT;
371				_fetch_syserr();
372				return (-1);
373			}
374			errno = 0;
375			r = select(conn->sd + 1, &readfds, NULL, NULL, &wait);
376			if (r == -1) {
377				if (errno == EINTR && fetchRestartCalls)
378					continue;
379				_fetch_syserr();
380				return (-1);
381			}
382		}
383#ifdef WITH_SSL
384		if (conn->ssl != NULL)
385			rlen = SSL_read(conn->ssl, buf, len);
386		else
387#endif
388			rlen = read(conn->sd, buf, len);
389		if (rlen == 0)
390			break;
391		if (rlen < 0) {
392			if (errno == EINTR && fetchRestartCalls)
393				continue;
394			return (-1);
395		}
396		len -= rlen;
397		buf += rlen;
398		total += rlen;
399	}
400	return (total);
401}
402
403
404/*
405 * Read a line of text from a connection w/ timeout
406 */
407#define MIN_BUF_SIZE 1024
408
409int
410_fetch_getln(conn_t *conn)
411{
412	char *tmp;
413	size_t tmpsize;
414	ssize_t len;
415	char c;
416
417	if (conn->buf == NULL) {
418		if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) {
419			errno = ENOMEM;
420			return (-1);
421		}
422		conn->bufsize = MIN_BUF_SIZE;
423	}
424
425	conn->buf[0] = '\0';
426	conn->buflen = 0;
427
428	do {
429		len = _fetch_read(conn, &c, 1);
430		if (len == -1)
431			return (-1);
432		if (len == 0)
433			break;
434		conn->buf[conn->buflen++] = c;
435		if (conn->buflen == conn->bufsize) {
436			tmp = conn->buf;
437			tmpsize = conn->bufsize * 2 + 1;
438			if ((tmp = realloc(tmp, tmpsize)) == NULL) {
439				errno = ENOMEM;
440				return (-1);
441			}
442			conn->buf = tmp;
443			conn->bufsize = tmpsize;
444		}
445	} while (c != '\n');
446
447	conn->buf[conn->buflen] = '\0';
448	DEBUG(fprintf(stderr, "<<< %s", conn->buf));
449	return (0);
450}
451
452
453/*
454 * Write to a connection w/ timeout
455 */
456ssize_t
457_fetch_write(conn_t *conn, const char *buf, size_t len)
458{
459	struct iovec iov;
460
461	iov.iov_base = __DECONST(char *, buf);
462	iov.iov_len = len;
463	return _fetch_writev(conn, &iov, 1);
464}
465
466/*
467 * Write a vector to a connection w/ timeout
468 * Note: can modify the iovec.
469 */
470ssize_t
471_fetch_writev(conn_t *conn, struct iovec *iov, int iovcnt)
472{
473	struct timeval now, timeout, wait;
474	fd_set writefds;
475	ssize_t wlen, total;
476	int r;
477
478	if (fetchTimeout) {
479		FD_ZERO(&writefds);
480		gettimeofday(&timeout, NULL);
481		timeout.tv_sec += fetchTimeout;
482	}
483
484	total = 0;
485	while (iovcnt > 0) {
486		while (fetchTimeout && !FD_ISSET(conn->sd, &writefds)) {
487			FD_SET(conn->sd, &writefds);
488			gettimeofday(&now, NULL);
489			wait.tv_sec = timeout.tv_sec - now.tv_sec;
490			wait.tv_usec = timeout.tv_usec - now.tv_usec;
491			if (wait.tv_usec < 0) {
492				wait.tv_usec += 1000000;
493				wait.tv_sec--;
494			}
495			if (wait.tv_sec < 0) {
496				errno = ETIMEDOUT;
497				_fetch_syserr();
498				return (-1);
499			}
500			errno = 0;
501			r = select(conn->sd + 1, NULL, &writefds, NULL, &wait);
502			if (r == -1) {
503				if (errno == EINTR && fetchRestartCalls)
504					continue;
505				return (-1);
506			}
507		}
508		errno = 0;
509#ifdef WITH_SSL
510		if (conn->ssl != NULL)
511			wlen = SSL_write(conn->ssl,
512			    iov->iov_base, iov->iov_len);
513		else
514#endif
515			wlen = writev(conn->sd, iov, iovcnt);
516		if (wlen == 0) {
517			/* we consider a short write a failure */
518			errno = EPIPE;
519			_fetch_syserr();
520			return (-1);
521		}
522		if (wlen < 0) {
523			if (errno == EINTR && fetchRestartCalls)
524				continue;
525			return (-1);
526		}
527		total += wlen;
528		while (iovcnt > 0 && wlen >= (ssize_t)iov->iov_len) {
529			wlen -= iov->iov_len;
530			iov++;
531			iovcnt--;
532		}
533		if (iovcnt > 0) {
534			iov->iov_len -= wlen;
535			iov->iov_base = __DECONST(char *, iov->iov_base) + wlen;
536		}
537	}
538	return (total);
539}
540
541
542/*
543 * Write a line of text to a connection w/ timeout
544 */
545int
546_fetch_putln(conn_t *conn, const char *str, size_t len)
547{
548	struct iovec iov[2];
549	int ret;
550
551	DEBUG(fprintf(stderr, ">>> %s\n", str));
552	iov[0].iov_base = __DECONST(char *, str);
553	iov[0].iov_len = len;
554	iov[1].iov_base = __DECONST(char *, ENDL);
555	iov[1].iov_len = sizeof ENDL;
556	if (len == 0)
557		ret = _fetch_writev(conn, &iov[1], 1);
558	else
559		ret = _fetch_writev(conn, iov, 2);
560	if (ret == -1)
561		return (-1);
562	return (0);
563}
564
565
566/*
567 * Close connection
568 */
569int
570_fetch_close(conn_t *conn)
571{
572	int ret;
573
574	if (--conn->ref > 0)
575		return (0);
576	ret = close(conn->sd);
577	free(conn);
578	return (ret);
579}
580
581
582/*** Directory-related utility functions *************************************/
583
584int
585_fetch_add_entry(struct url_ent **p, int *size, int *len,
586    const char *name, struct url_stat *us)
587{
588	struct url_ent *tmp;
589
590	if (*p == NULL) {
591		*size = 0;
592		*len = 0;
593	}
594
595	if (*len >= *size - 1) {
596		tmp = realloc(*p, (*size * 2 + 1) * sizeof **p);
597		if (tmp == NULL) {
598			errno = ENOMEM;
599			_fetch_syserr();
600			return (-1);
601		}
602		*size = (*size * 2 + 1);
603		*p = tmp;
604	}
605
606	tmp = *p + *len;
607	snprintf(tmp->name, PATH_MAX, "%s", name);
608	bcopy(us, &tmp->stat, sizeof *us);
609
610	(*len)++;
611	(++tmp)->name[0] = 0;
612
613	return (0);
614}
615
616
617/*** Authentication-related utility functions ********************************/
618
619static const char *
620_fetch_read_word(FILE *f)
621{
622	static char word[1024];
623
624	if (fscanf(f, " %1024s ", word) != 1)
625		return (NULL);
626	return (word);
627}
628
629/*
630 * Get authentication data for a URL from .netrc
631 */
632int
633_fetch_netrc_auth(struct url *url)
634{
635	char fn[PATH_MAX];
636	const char *word;
637	char *p;
638	FILE *f;
639
640	if ((p = getenv("NETRC")) != NULL) {
641		if (snprintf(fn, sizeof fn, "%s", p) >= (int)sizeof(fn)) {
642			_fetch_info("$NETRC specifies a file name "
643			    "longer than PATH_MAX");
644			return (-1);
645		}
646	} else {
647		if ((p = getenv("HOME")) != NULL) {
648			struct passwd *pwd;
649
650			if ((pwd = getpwuid(getuid())) == NULL ||
651			    (p = pwd->pw_dir) == NULL)
652				return (-1);
653		}
654		if (snprintf(fn, sizeof fn, "%s/.netrc", p) >= (int)sizeof(fn))
655			return (-1);
656	}
657
658	if ((f = fopen(fn, "r")) == NULL)
659		return (-1);
660	while ((word = _fetch_read_word(f)) != NULL) {
661		if (strcmp(word, "default") == 0) {
662			DEBUG(_fetch_info("Using default .netrc settings"));
663			break;
664		}
665		if (strcmp(word, "machine") == 0 &&
666		    (word = _fetch_read_word(f)) != NULL &&
667		    strcasecmp(word, url->host) == 0) {
668			DEBUG(_fetch_info("Using .netrc settings for %s", word));
669			break;
670		}
671	}
672	if (word == NULL)
673		goto ferr;
674	while ((word = _fetch_read_word(f)) != NULL) {
675		if (strcmp(word, "login") == 0) {
676			if ((word = _fetch_read_word(f)) == NULL)
677				goto ferr;
678			if (snprintf(url->user, sizeof url->user,
679				"%s", word) > (int)sizeof(url->user)) {
680				_fetch_info("login name in .netrc is too long");
681				url->user[0] = '\0';
682			}
683		} else if (strcmp(word, "password") == 0) {
684			if ((word = _fetch_read_word(f)) == NULL)
685				goto ferr;
686			if (snprintf(url->pwd, sizeof url->pwd,
687				"%s", word) > (int)sizeof(url->pwd)) {
688				_fetch_info("password in .netrc is too long");
689				url->pwd[0] = '\0';
690			}
691		} else if (strcmp(word, "account") == 0) {
692			if ((word = _fetch_read_word(f)) == NULL)
693				goto ferr;
694			/* XXX not supported! */
695		} else {
696			break;
697		}
698	}
699	fclose(f);
700	return (0);
701 ferr:
702	fclose(f);
703	return (-1);
704}
705