xref: /dragonfly/lib/libfetch/http.c (revision d46e5e54)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2000-2014 Dag-Erling Smørgrav
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD: head/lib/libfetch/http.c 351573 2019-08-28 17:01:28Z markj $
31  */
32 
33 #include <sys/cdefs.h>
34 
35 /*
36  * The following copyright applies to the base64 code:
37  *
38  *-
39  * Copyright 1997 Massachusetts Institute of Technology
40  *
41  * Permission to use, copy, modify, and distribute this software and
42  * its documentation for any purpose and without fee is hereby
43  * granted, provided that both the above copyright notice and this
44  * permission notice appear in all copies, that both the above
45  * copyright notice and this permission notice appear in all
46  * supporting documentation, and that the name of M.I.T. not be used
47  * in advertising or publicity pertaining to distribution of the
48  * software without specific, written prior permission.  M.I.T. makes
49  * no representations about the suitability of this software for any
50  * purpose.  It is provided "as is" without express or implied
51  * warranty.
52  *
53  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
54  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
55  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
56  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
57  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
58  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
59  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
60  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
61  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
62  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
63  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  */
66 
67 #include <sys/param.h>
68 #include <sys/socket.h>
69 #include <sys/time.h>
70 
71 #include <ctype.h>
72 #include <err.h>
73 #include <errno.h>
74 #include <locale.h>
75 #include <netdb.h>
76 #include <stdarg.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <time.h>
81 #include <unistd.h>
82 
83 #ifdef WITH_SSL
84 #include <openssl/md5.h>
85 #define MD5Init(c) MD5_Init(c)
86 #define MD5Update(c, data, len) MD5_Update(c, data, len)
87 #define MD5Final(md, c) MD5_Final(md, c)
88 #else
89 #include <md5.h>
90 #endif
91 
92 #include <netinet/in.h>
93 #include <netinet/tcp.h>
94 
95 #include "fetch.h"
96 #include "common.h"
97 #include "httperr.h"
98 
99 /* Maximum number of redirects to follow */
100 #define MAX_REDIRECT 20
101 
102 /* Symbolic names for reply codes we care about */
103 #define HTTP_OK			200
104 #define HTTP_PARTIAL		206
105 #define HTTP_MOVED_PERM		301
106 #define HTTP_MOVED_TEMP		302
107 #define HTTP_SEE_OTHER		303
108 #define HTTP_NOT_MODIFIED	304
109 #define HTTP_USE_PROXY		305
110 #define HTTP_TEMP_REDIRECT	307
111 #define HTTP_PERM_REDIRECT	308
112 #define HTTP_NEED_AUTH		401
113 #define HTTP_NEED_PROXY_AUTH	407
114 #define HTTP_BAD_RANGE		416
115 #define HTTP_PROTOCOL_ERROR	999
116 
117 #define HTTP_REDIRECT(xyz) ((xyz) == HTTP_MOVED_PERM \
118 			    || (xyz) == HTTP_MOVED_TEMP \
119 			    || (xyz) == HTTP_TEMP_REDIRECT \
120 			    || (xyz) == HTTP_PERM_REDIRECT \
121 			    || (xyz) == HTTP_USE_PROXY \
122 			    || (xyz) == HTTP_SEE_OTHER)
123 
124 #define HTTP_ERROR(xyz) ((xyz) >= 400 && (xyz) <= 599)
125 
126 
127 /*****************************************************************************
128  * I/O functions for decoding chunked streams
129  */
130 
131 struct httpio
132 {
133 	conn_t		*conn;		/* connection */
134 	int		 chunked;	/* chunked mode */
135 	char		*buf;		/* chunk buffer */
136 	size_t		 bufsize;	/* size of chunk buffer */
137 	size_t		 buflen;	/* amount of data currently in buffer */
138 	size_t		 bufpos;	/* current read offset in buffer */
139 	int		 eof;		/* end-of-file flag */
140 	int		 error;		/* error flag */
141 	size_t		 chunksize;	/* remaining size of current chunk */
142 #ifndef NDEBUG
143 	size_t		 total;
144 #endif
145 };
146 
147 /*
148  * Get next chunk header
149  */
150 static int
http_new_chunk(struct httpio * io)151 http_new_chunk(struct httpio *io)
152 {
153 	char *p;
154 
155 	if (fetch_getln(io->conn) == -1)
156 		return (-1);
157 
158 	if (io->conn->buflen < 2 || !isxdigit((unsigned char)*io->conn->buf))
159 		return (-1);
160 
161 	for (p = io->conn->buf; *p && !isspace((unsigned char)*p); ++p) {
162 		if (*p == ';')
163 			break;
164 		if (!isxdigit((unsigned char)*p))
165 			return (-1);
166 		if (isdigit((unsigned char)*p)) {
167 			io->chunksize = io->chunksize * 16 +
168 			    *p - '0';
169 		} else {
170 			io->chunksize = io->chunksize * 16 +
171 			    10 + tolower((unsigned char)*p) - 'a';
172 		}
173 	}
174 
175 #ifndef NDEBUG
176 	if (fetchDebug) {
177 		io->total += io->chunksize;
178 		if (io->chunksize == 0)
179 			fprintf(stderr, "%s(): end of last chunk\n", __func__);
180 		else
181 			fprintf(stderr, "%s(): new chunk: %lu (%lu)\n",
182 			    __func__, (unsigned long)io->chunksize,
183 			    (unsigned long)io->total);
184 	}
185 #endif
186 
187 	return (io->chunksize);
188 }
189 
190 /*
191  * Grow the input buffer to at least len bytes
192  */
193 static inline int
http_growbuf(struct httpio * io,size_t len)194 http_growbuf(struct httpio *io, size_t len)
195 {
196 	char *tmp;
197 
198 	if (io->bufsize >= len)
199 		return (0);
200 
201 	if ((tmp = realloc(io->buf, len)) == NULL)
202 		return (-1);
203 	io->buf = tmp;
204 	io->bufsize = len;
205 	return (0);
206 }
207 
208 /*
209  * Fill the input buffer, do chunk decoding on the fly
210  */
211 static ssize_t
http_fillbuf(struct httpio * io,size_t len)212 http_fillbuf(struct httpio *io, size_t len)
213 {
214 	ssize_t nbytes;
215 	char ch;
216 
217 	if (io->error)
218 		return (-1);
219 	if (io->eof)
220 		return (0);
221 
222 	/* not chunked: just fetch the requested amount */
223 	if (io->chunked == 0) {
224 		if (http_growbuf(io, len) == -1)
225 			return (-1);
226 		if ((nbytes = fetch_read(io->conn, io->buf, len)) == -1) {
227 			io->error = errno;
228 			return (-1);
229 		}
230 		io->buflen = nbytes;
231 		io->bufpos = 0;
232 		return (io->buflen);
233 	}
234 
235 	/* chunked, but we ran out: get the next chunk header */
236 	if (io->chunksize == 0) {
237 		switch (http_new_chunk(io)) {
238 		case -1:
239 			io->error = EPROTO;
240 			return (-1);
241 		case 0:
242 			io->eof = 1;
243 			return (0);
244 		}
245 	}
246 
247 	/* fetch the requested amount, but no more than the current chunk */
248 	if (len > io->chunksize)
249 		len = io->chunksize;
250 	if (http_growbuf(io, len) == -1)
251 		return (-1);
252 	if ((nbytes = fetch_read(io->conn, io->buf, len)) == -1) {
253 		io->error = errno;
254 		return (-1);
255 	}
256 	io->bufpos = 0;
257 	io->buflen = nbytes;
258 	io->chunksize -= nbytes;
259 
260 	if (io->chunksize == 0) {
261 		if (fetch_read(io->conn, &ch, 1) != 1 || ch != '\r' ||
262 		    fetch_read(io->conn, &ch, 1) != 1 || ch != '\n')
263 			return (-1);
264 	}
265 
266 	return (io->buflen);
267 }
268 
269 /*
270  * Read function
271  */
272 static int
http_readfn(void * v,char * buf,int len)273 http_readfn(void *v, char *buf, int len)
274 {
275 	struct httpio *io = (struct httpio *)v;
276 	int rlen;
277 
278 	if (io->error)
279 		return (-1);
280 	if (io->eof)
281 		return (0);
282 
283 	/* empty buffer */
284 	if (!io->buf || io->bufpos == io->buflen) {
285 		if ((rlen = http_fillbuf(io, len)) < 0) {
286 			if ((errno = io->error) == EINTR)
287 				io->error = 0;
288 			return (-1);
289 		} else if (rlen == 0) {
290 			return (0);
291 		}
292 	}
293 
294 	rlen = io->buflen - io->bufpos;
295 	if (len < rlen)
296 		rlen = len;
297 	memcpy(buf, io->buf + io->bufpos, rlen);
298 	io->bufpos += rlen;
299 	return (rlen);
300 }
301 
302 /*
303  * Write function
304  */
305 static int
http_writefn(void * v,const char * buf,int len)306 http_writefn(void *v, const char *buf, int len)
307 {
308 	struct httpio *io = (struct httpio *)v;
309 
310 	return (fetch_write(io->conn, buf, len));
311 }
312 
313 /*
314  * Close function
315  */
316 static int
http_closefn(void * v)317 http_closefn(void *v)
318 {
319 	struct httpio *io = (struct httpio *)v;
320 	int r;
321 
322 	r = fetch_close(io->conn);
323 	if (io->buf)
324 		free(io->buf);
325 	free(io);
326 	return (r);
327 }
328 
329 /*
330  * Wrap a file descriptor up
331  */
332 static FILE *
http_funopen(conn_t * conn,int chunked)333 http_funopen(conn_t *conn, int chunked)
334 {
335 	struct httpio *io;
336 	FILE *f;
337 
338 	if ((io = calloc(1, sizeof(*io))) == NULL) {
339 		fetch_syserr();
340 		return (NULL);
341 	}
342 	io->conn = conn;
343 	io->chunked = chunked;
344 	f = funopen(io, http_readfn, http_writefn, NULL, http_closefn);
345 	if (f == NULL) {
346 		fetch_syserr();
347 		free(io);
348 		return (NULL);
349 	}
350 	return (f);
351 }
352 
353 
354 /*****************************************************************************
355  * Helper functions for talking to the server and parsing its replies
356  */
357 
358 /* Header types */
359 typedef enum {
360 	hdr_syserror = -2,
361 	hdr_error = -1,
362 	hdr_end = 0,
363 	hdr_unknown = 1,
364 	hdr_content_length,
365 	hdr_content_range,
366 	hdr_last_modified,
367 	hdr_location,
368 	hdr_transfer_encoding,
369 	hdr_www_authenticate,
370 	hdr_proxy_authenticate,
371 } hdr_t;
372 
373 /* Names of interesting headers */
374 static struct {
375 	hdr_t		 num;
376 	const char	*name;
377 } hdr_names[] = {
378 	{ hdr_content_length,		"Content-Length" },
379 	{ hdr_content_range,		"Content-Range" },
380 	{ hdr_last_modified,		"Last-Modified" },
381 	{ hdr_location,			"Location" },
382 	{ hdr_transfer_encoding,	"Transfer-Encoding" },
383 	{ hdr_www_authenticate,		"WWW-Authenticate" },
384 	{ hdr_proxy_authenticate,	"Proxy-Authenticate" },
385 	{ hdr_unknown,			NULL },
386 };
387 
388 /*
389  * Send a formatted line; optionally echo to terminal
390  */
391 static int
http_cmd(conn_t * conn,const char * fmt,...)392 http_cmd(conn_t *conn, const char *fmt, ...)
393 {
394 	va_list ap;
395 	size_t len;
396 	char *msg;
397 	int r;
398 
399 	va_start(ap, fmt);
400 	len = vasprintf(&msg, fmt, ap);
401 	va_end(ap);
402 
403 	if (msg == NULL) {
404 		errno = ENOMEM;
405 		fetch_syserr();
406 		return (-1);
407 	}
408 
409 	r = fetch_putln(conn, msg, len);
410 	free(msg);
411 
412 	if (r == -1) {
413 		fetch_syserr();
414 		return (-1);
415 	}
416 
417 	return (0);
418 }
419 
420 /*
421  * Get and parse status line
422  */
423 static int
http_get_reply(conn_t * conn)424 http_get_reply(conn_t *conn)
425 {
426 	char *p;
427 
428 	if (fetch_getln(conn) == -1)
429 		return (-1);
430 	/*
431 	 * A valid status line looks like "HTTP/m.n xyz reason" where m
432 	 * and n are the major and minor protocol version numbers and xyz
433 	 * is the reply code.
434 	 * Unfortunately, there are servers out there (NCSA 1.5.1, to name
435 	 * just one) that do not send a version number, so we can't rely
436 	 * on finding one, but if we do, insist on it being 1.0 or 1.1.
437 	 * We don't care about the reason phrase.
438 	 */
439 	if (strncmp(conn->buf, "HTTP", 4) != 0)
440 		return (HTTP_PROTOCOL_ERROR);
441 	p = conn->buf + 4;
442 	if (*p == '/') {
443 		if (p[1] != '1' || p[2] != '.' || (p[3] != '0' && p[3] != '1'))
444 			return (HTTP_PROTOCOL_ERROR);
445 		p += 4;
446 	}
447 	if (*p != ' ' ||
448 	    !isdigit((unsigned char)p[1]) ||
449 	    !isdigit((unsigned char)p[2]) ||
450 	    !isdigit((unsigned char)p[3]))
451 		return (HTTP_PROTOCOL_ERROR);
452 
453 	conn->err = (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0');
454 	return (conn->err);
455 }
456 
457 /*
458  * Check a header; if the type matches the given string, return a pointer
459  * to the beginning of the value.
460  */
461 static const char *
http_match(const char * str,const char * hdr)462 http_match(const char *str, const char *hdr)
463 {
464 	while (*str && *hdr &&
465 	    tolower((unsigned char)*str++) == tolower((unsigned char)*hdr++))
466 		/* nothing */;
467 	if (*str || *hdr != ':')
468 		return (NULL);
469 	while (*hdr && isspace((unsigned char)*++hdr))
470 		/* nothing */;
471 	return (hdr);
472 }
473 
474 
475 /*
476  * Get the next header and return the appropriate symbolic code.  We
477  * need to read one line ahead for checking for a continuation line
478  * belonging to the current header (continuation lines start with
479  * white space).
480  *
481  * We get called with a fresh line already in the conn buffer, either
482  * from the previous http_next_header() invocation, or, the first
483  * time, from a fetch_getln() performed by our caller.
484  *
485  * This stops when we encounter an empty line (we dont read beyond the header
486  * area).
487  *
488  * Note that the "headerbuf" is just a place to return the result. Its
489  * contents are not used for the next call. This means that no cleanup
490  * is needed when ie doing another connection, just call the cleanup when
491  * fully done to deallocate memory.
492  */
493 
494 /* Limit the max number of continuation lines to some reasonable value */
495 #define HTTP_MAX_CONT_LINES 10
496 
497 /* Place into which to build a header from one or several lines */
498 typedef struct {
499 	char	*buf;		/* buffer */
500 	size_t	 bufsize;	/* buffer size */
501 	size_t	 buflen;	/* length of buffer contents */
502 } http_headerbuf_t;
503 
504 static void
init_http_headerbuf(http_headerbuf_t * buf)505 init_http_headerbuf(http_headerbuf_t *buf)
506 {
507 	buf->buf = NULL;
508 	buf->bufsize = 0;
509 	buf->buflen = 0;
510 }
511 
512 static void
clean_http_headerbuf(http_headerbuf_t * buf)513 clean_http_headerbuf(http_headerbuf_t *buf)
514 {
515 	if (buf->buf)
516 		free(buf->buf);
517 	init_http_headerbuf(buf);
518 }
519 
520 /* Remove whitespace at the end of the buffer */
521 static void
http_conn_trimright(conn_t * conn)522 http_conn_trimright(conn_t *conn)
523 {
524 	while (conn->buflen &&
525 	       isspace((unsigned char)conn->buf[conn->buflen - 1]))
526 		conn->buflen--;
527 	conn->buf[conn->buflen] = '\0';
528 }
529 
530 static hdr_t
http_next_header(conn_t * conn,http_headerbuf_t * hbuf,const char ** p)531 http_next_header(conn_t *conn, http_headerbuf_t *hbuf, const char **p)
532 {
533 	unsigned int i, len;
534 
535 	/*
536 	 * Have to do the stripping here because of the first line. So
537 	 * it's done twice for the subsequent lines. No big deal
538 	 */
539 	http_conn_trimright(conn);
540 	if (conn->buflen == 0)
541 		return (hdr_end);
542 
543 	/* Copy the line to the headerbuf */
544 	if (hbuf->bufsize < conn->buflen + 1) {
545 		if ((hbuf->buf = realloc(hbuf->buf, conn->buflen + 1)) == NULL)
546 			return (hdr_syserror);
547 		hbuf->bufsize = conn->buflen + 1;
548 	}
549 	strcpy(hbuf->buf, conn->buf);
550 	hbuf->buflen = conn->buflen;
551 
552 	/*
553 	 * Fetch possible continuation lines. Stop at 1st non-continuation
554 	 * and leave it in the conn buffer
555 	 */
556 	for (i = 0; i < HTTP_MAX_CONT_LINES; i++) {
557 		if (fetch_getln(conn) == -1)
558 			return (hdr_syserror);
559 
560 		/*
561 		 * Note: we carry on the idea from the previous version
562 		 * that a pure whitespace line is equivalent to an empty
563 		 * one (so it's not continuation and will be handled when
564 		 * we are called next)
565 		 */
566 		http_conn_trimright(conn);
567 		if (conn->buf[0] != ' ' && conn->buf[0] != "\t"[0])
568 			break;
569 
570 		/* Got a continuation line. Concatenate to previous */
571 		len = hbuf->buflen + conn->buflen;
572 		if (hbuf->bufsize < len + 1) {
573 			len *= 2;
574 			if ((hbuf->buf = realloc(hbuf->buf, len + 1)) == NULL)
575 				return (hdr_syserror);
576 			hbuf->bufsize = len + 1;
577 		}
578 		strcpy(hbuf->buf + hbuf->buflen, conn->buf);
579 		hbuf->buflen += conn->buflen;
580 	}
581 
582 	/*
583 	 * We could check for malformed headers but we don't really care.
584 	 * A valid header starts with a token immediately followed by a
585 	 * colon; a token is any sequence of non-control, non-whitespace
586 	 * characters except "()<>@,;:\\\"{}".
587 	 */
588 	for (i = 0; hdr_names[i].num != hdr_unknown; i++)
589 		if ((*p = http_match(hdr_names[i].name, hbuf->buf)) != NULL)
590 			return (hdr_names[i].num);
591 
592 	return (hdr_unknown);
593 }
594 
595 /**************************
596  * [Proxy-]Authenticate header parsing
597  */
598 
599 /*
600  * Read doublequote-delimited string into output buffer obuf (allocated
601  * by caller, whose responsibility it is to ensure that it's big enough)
602  * cp points to the first char after the initial '"'
603  * Handles \ quoting
604  * Returns pointer to the first char after the terminating double quote, or
605  * NULL for error.
606  */
607 static const char *
http_parse_headerstring(const char * cp,char * obuf)608 http_parse_headerstring(const char *cp, char *obuf)
609 {
610 	for (;;) {
611 		switch (*cp) {
612 		case 0: /* Unterminated string */
613 			*obuf = 0;
614 			return (NULL);
615 		case '"': /* Ending quote */
616 			*obuf = 0;
617 			return (++cp);
618 		case '\\':
619 			if (*++cp == 0) {
620 				*obuf = 0;
621 				return (NULL);
622 			}
623 			/* FALLTHROUGH */
624 		default:
625 			*obuf++ = *cp++;
626 		}
627 	}
628 }
629 
630 /* Http auth challenge schemes */
631 typedef enum {HTTPAS_UNKNOWN, HTTPAS_BASIC,HTTPAS_DIGEST} http_auth_schemes_t;
632 
633 /* Data holder for a Basic or Digest challenge. */
634 typedef struct {
635 	http_auth_schemes_t scheme;
636 	char	*realm;
637 	char	*qop;
638 	char	*nonce;
639 	char	*opaque;
640 	char	*algo;
641 	int	 stale;
642 	int	 nc; /* Nonce count */
643 } http_auth_challenge_t;
644 
645 static void
init_http_auth_challenge(http_auth_challenge_t * b)646 init_http_auth_challenge(http_auth_challenge_t *b)
647 {
648 	b->scheme = HTTPAS_UNKNOWN;
649 	b->realm = b->qop = b->nonce = b->opaque = b->algo = NULL;
650 	b->stale = b->nc = 0;
651 }
652 
653 static void
clean_http_auth_challenge(http_auth_challenge_t * b)654 clean_http_auth_challenge(http_auth_challenge_t *b)
655 {
656 	if (b->realm)
657 		free(b->realm);
658 	if (b->qop)
659 		free(b->qop);
660 	if (b->nonce)
661 		free(b->nonce);
662 	if (b->opaque)
663 		free(b->opaque);
664 	if (b->algo)
665 		free(b->algo);
666 	init_http_auth_challenge(b);
667 }
668 
669 /* Data holder for an array of challenges offered in an http response. */
670 #define MAX_CHALLENGES 10
671 typedef struct {
672 	http_auth_challenge_t *challenges[MAX_CHALLENGES];
673 	int	count; /* Number of parsed challenges in the array */
674 	int	valid; /* We did parse an authenticate header */
675 } http_auth_challenges_t;
676 
677 static void
init_http_auth_challenges(http_auth_challenges_t * cs)678 init_http_auth_challenges(http_auth_challenges_t *cs)
679 {
680 	int i;
681 	for (i = 0; i < MAX_CHALLENGES; i++)
682 		cs->challenges[i] = NULL;
683 	cs->count = cs->valid = 0;
684 }
685 
686 static void
clean_http_auth_challenges(http_auth_challenges_t * cs)687 clean_http_auth_challenges(http_auth_challenges_t *cs)
688 {
689 	int i;
690 	/* We rely on non-zero pointers being allocated, not on the count */
691 	for (i = 0; i < MAX_CHALLENGES; i++) {
692 		if (cs->challenges[i] != NULL) {
693 			clean_http_auth_challenge(cs->challenges[i]);
694 			free(cs->challenges[i]);
695 		}
696 	}
697 	init_http_auth_challenges(cs);
698 }
699 
700 /*
701  * Enumeration for lexical elements. Separators will be returned as their own
702  * ascii value
703  */
704 typedef enum {HTTPHL_WORD=256, HTTPHL_STRING=257, HTTPHL_END=258,
705 	      HTTPHL_ERROR = 259} http_header_lex_t;
706 
707 /*
708  * Determine what kind of token comes next and return possible value
709  * in buf, which is supposed to have been allocated big enough by
710  * caller. Advance input pointer and return element type.
711  */
712 static int
http_header_lex(const char ** cpp,char * buf)713 http_header_lex(const char **cpp, char *buf)
714 {
715 	size_t l;
716 	/* Eat initial whitespace */
717 	*cpp += strspn(*cpp, " \t");
718 	if (**cpp == 0)
719 		return (HTTPHL_END);
720 
721 	/* Separator ? */
722 	if (**cpp == ',' || **cpp == '=')
723 		return (*((*cpp)++));
724 
725 	/* String ? */
726 	if (**cpp == '"') {
727 		*cpp = http_parse_headerstring(++*cpp, buf);
728 		if (*cpp == NULL)
729 			return (HTTPHL_ERROR);
730 		return (HTTPHL_STRING);
731 	}
732 
733 	/* Read other token, until separator or whitespace */
734 	l = strcspn(*cpp, " \t,=");
735 	memcpy(buf, *cpp, l);
736 	buf[l] = 0;
737 	*cpp += l;
738 	return (HTTPHL_WORD);
739 }
740 
741 /*
742  * Read challenges from http xxx-authenticate header and accumulate them
743  * in the challenges list structure.
744  *
745  * Headers with multiple challenges are specified by rfc2617, but
746  * servers (ie: squid) often send them in separate headers instead,
747  * which in turn is forbidden by the http spec (multiple headers with
748  * the same name are only allowed for pure comma-separated lists, see
749  * rfc2616 sec 4.2).
750  *
751  * We support both approaches anyway
752  */
753 static int
http_parse_authenticate(const char * cp,http_auth_challenges_t * cs)754 http_parse_authenticate(const char *cp, http_auth_challenges_t *cs)
755 {
756 	int ret = -1;
757 	http_header_lex_t lex;
758 	char *key = malloc(strlen(cp) + 1);
759 	char *value = malloc(strlen(cp) + 1);
760 	char *buf = malloc(strlen(cp) + 1);
761 
762 	if (key == NULL || value == NULL || buf == NULL) {
763 		fetch_syserr();
764 		goto out;
765 	}
766 
767 	/* In any case we've seen the header and we set the valid bit */
768 	cs->valid = 1;
769 
770 	/* Need word first */
771 	lex = http_header_lex(&cp, key);
772 	if (lex != HTTPHL_WORD)
773 		goto out;
774 
775 	/* Loop on challenges */
776 	for (; cs->count < MAX_CHALLENGES; cs->count++) {
777 		cs->challenges[cs->count] =
778 			malloc(sizeof(http_auth_challenge_t));
779 		if (cs->challenges[cs->count] == NULL) {
780 			fetch_syserr();
781 			goto out;
782 		}
783 		init_http_auth_challenge(cs->challenges[cs->count]);
784 		if (strcasecmp(key, "basic") == 0) {
785 			cs->challenges[cs->count]->scheme = HTTPAS_BASIC;
786 		} else if (strcasecmp(key, "digest") == 0) {
787 			cs->challenges[cs->count]->scheme = HTTPAS_DIGEST;
788 		} else {
789 			cs->challenges[cs->count]->scheme = HTTPAS_UNKNOWN;
790 			/*
791 			 * Continue parsing as basic or digest may
792 			 * follow, and the syntax is the same for
793 			 * all. We'll just ignore this one when
794 			 * looking at the list
795 			 */
796 		}
797 
798 		/* Loop on attributes */
799 		for (;;) {
800 			/* Key */
801 			lex = http_header_lex(&cp, key);
802 			if (lex != HTTPHL_WORD)
803 				goto out;
804 
805 			/* Equal sign */
806 			lex = http_header_lex(&cp, buf);
807 			if (lex != '=')
808 				goto out;
809 
810 			/* Value */
811 			lex = http_header_lex(&cp, value);
812 			if (lex != HTTPHL_WORD && lex != HTTPHL_STRING)
813 				goto out;
814 
815 			if (strcasecmp(key, "realm") == 0) {
816 				cs->challenges[cs->count]->realm =
817 				    strdup(value);
818 			} else if (strcasecmp(key, "qop") == 0) {
819 				cs->challenges[cs->count]->qop =
820 				    strdup(value);
821 			} else if (strcasecmp(key, "nonce") == 0) {
822 				cs->challenges[cs->count]->nonce =
823 				    strdup(value);
824 			} else if (strcasecmp(key, "opaque") == 0) {
825 				cs->challenges[cs->count]->opaque =
826 				    strdup(value);
827 			} else if (strcasecmp(key, "algorithm") == 0) {
828 				cs->challenges[cs->count]->algo =
829 				    strdup(value);
830 			} else if (strcasecmp(key, "stale") == 0) {
831 				cs->challenges[cs->count]->stale =
832 				    strcasecmp(value, "no");
833 			} else {
834 				/* ignore unknown attributes */
835 			}
836 
837 			/* Comma or Next challenge or End */
838 			lex = http_header_lex(&cp, key);
839 			/*
840 			 * If we get a word here, this is the beginning of the
841 			 * next challenge. Break the attributes loop
842 			 */
843 			if (lex == HTTPHL_WORD)
844 				break;
845 
846 			if (lex == HTTPHL_END) {
847 				/* End while looking for ',' is normal exit */
848 				cs->count++;
849 				ret = 0;
850 				goto out;
851 			}
852 			/* Anything else is an error */
853 			if (lex != ',')
854 				goto out;
855 
856 		} /* End attributes loop */
857 	} /* End challenge loop */
858 
859 	/*
860 	 * Challenges max count exceeded. This really can't happen
861 	 * with normal data, something's fishy -> error
862 	 */
863 
864 out:
865 	if (key)
866 		free(key);
867 	if (value)
868 		free(value);
869 	if (buf)
870 		free(buf);
871 	return (ret);
872 }
873 
874 
875 /*
876  * Parse a last-modified header
877  */
878 static int
http_parse_mtime(const char * p,time_t * mtime)879 http_parse_mtime(const char *p, time_t *mtime)
880 {
881 	char locale[64], *r;
882 	struct tm tm;
883 
884 	strlcpy(locale, setlocale(LC_TIME, NULL), sizeof(locale));
885 	setlocale(LC_TIME, "C");
886 	r = strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm);
887 	/*
888 	 * Some proxies use UTC in response, but it should still be
889 	 * parsed. RFC2616 states GMT and UTC are exactly equal for HTTP.
890 	 */
891 	if (r == NULL)
892 		r = strptime(p, "%a, %d %b %Y %H:%M:%S UTC", &tm);
893 	/* XXX should add support for date-2 and date-3 */
894 	setlocale(LC_TIME, locale);
895 	if (r == NULL)
896 		return (-1);
897 	DEBUGF("last modified: [%04d-%02d-%02d %02d:%02d:%02d]\n",
898 	    tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
899 	    tm.tm_hour, tm.tm_min, tm.tm_sec);
900 	*mtime = timegm(&tm);
901 	return (0);
902 }
903 
904 /*
905  * Parse a content-length header
906  */
907 static int
http_parse_length(const char * p,off_t * length)908 http_parse_length(const char *p, off_t *length)
909 {
910 	off_t len;
911 
912 	for (len = 0; *p && isdigit((unsigned char)*p); ++p)
913 		len = len * 10 + (*p - '0');
914 	if (*p)
915 		return (-1);
916 	DEBUGF("content length: [%lld]\n", (long long)len);
917 	*length = len;
918 	return (0);
919 }
920 
921 /*
922  * Parse a content-range header
923  */
924 static int
http_parse_range(const char * p,off_t * offset,off_t * length,off_t * size)925 http_parse_range(const char *p, off_t *offset, off_t *length, off_t *size)
926 {
927 	off_t first, last, len;
928 
929 	if (strncasecmp(p, "bytes ", 6) != 0)
930 		return (-1);
931 	p += 6;
932 	if (*p == '*') {
933 		first = last = -1;
934 		++p;
935 	} else {
936 		for (first = 0; *p && isdigit((unsigned char)*p); ++p)
937 			first = first * 10 + *p - '0';
938 		if (*p != '-')
939 			return (-1);
940 		for (last = 0, ++p; *p && isdigit((unsigned char)*p); ++p)
941 			last = last * 10 + *p - '0';
942 	}
943 	if (first > last || *p != '/')
944 		return (-1);
945 	for (len = 0, ++p; *p && isdigit((unsigned char)*p); ++p)
946 		len = len * 10 + *p - '0';
947 	if (*p || len < last - first + 1)
948 		return (-1);
949 	if (first == -1) {
950 		DEBUGF("content range: [*/%lld]\n", (long long)len);
951 		*length = 0;
952 	} else {
953 		DEBUGF("content range: [%lld-%lld/%lld]\n",
954 		    (long long)first, (long long)last, (long long)len);
955 		*length = last - first + 1;
956 	}
957 	*offset = first;
958 	*size = len;
959 	return (0);
960 }
961 
962 
963 /*****************************************************************************
964  * Helper functions for authorization
965  */
966 
967 /*
968  * Base64 encoding
969  */
970 static char *
http_base64(const char * src)971 http_base64(const char *src)
972 {
973 	static const char base64[] =
974 	    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
975 	    "abcdefghijklmnopqrstuvwxyz"
976 	    "0123456789+/";
977 	char *str, *dst;
978 	size_t l;
979 	int t, r;
980 
981 	l = strlen(src);
982 	if ((str = malloc(((l + 2) / 3) * 4 + 1)) == NULL)
983 		return (NULL);
984 	dst = str;
985 	r = 0;
986 
987 	while (l >= 3) {
988 		t = (src[0] << 16) | (src[1] << 8) | src[2];
989 		dst[0] = base64[(t >> 18) & 0x3f];
990 		dst[1] = base64[(t >> 12) & 0x3f];
991 		dst[2] = base64[(t >> 6) & 0x3f];
992 		dst[3] = base64[(t >> 0) & 0x3f];
993 		src += 3; l -= 3;
994 		dst += 4; r += 4;
995 	}
996 
997 	switch (l) {
998 	case 2:
999 		t = (src[0] << 16) | (src[1] << 8);
1000 		dst[0] = base64[(t >> 18) & 0x3f];
1001 		dst[1] = base64[(t >> 12) & 0x3f];
1002 		dst[2] = base64[(t >> 6) & 0x3f];
1003 		dst[3] = '=';
1004 		dst += 4;
1005 		r += 4;
1006 		break;
1007 	case 1:
1008 		t = src[0] << 16;
1009 		dst[0] = base64[(t >> 18) & 0x3f];
1010 		dst[1] = base64[(t >> 12) & 0x3f];
1011 		dst[2] = dst[3] = '=';
1012 		dst += 4;
1013 		r += 4;
1014 		break;
1015 	case 0:
1016 		break;
1017 	}
1018 
1019 	*dst = 0;
1020 	return (str);
1021 }
1022 
1023 
1024 /*
1025  * Extract authorization parameters from environment value.
1026  * The value is like scheme:realm:user:pass
1027  */
1028 typedef struct {
1029 	char	*scheme;
1030 	char	*realm;
1031 	char	*user;
1032 	char	*password;
1033 } http_auth_params_t;
1034 
1035 static void
init_http_auth_params(http_auth_params_t * s)1036 init_http_auth_params(http_auth_params_t *s)
1037 {
1038 	s->scheme = s->realm = s->user = s->password = NULL;
1039 }
1040 
1041 static void
clean_http_auth_params(http_auth_params_t * s)1042 clean_http_auth_params(http_auth_params_t *s)
1043 {
1044 	if (s->scheme)
1045 		free(s->scheme);
1046 	if (s->realm)
1047 		free(s->realm);
1048 	if (s->user)
1049 		free(s->user);
1050 	if (s->password)
1051 		free(s->password);
1052 	init_http_auth_params(s);
1053 }
1054 
1055 static int
http_authfromenv(const char * p,http_auth_params_t * parms)1056 http_authfromenv(const char *p, http_auth_params_t *parms)
1057 {
1058 	int ret = -1;
1059 	char *v, *ve;
1060 	char *str = strdup(p);
1061 
1062 	if (str == NULL) {
1063 		fetch_syserr();
1064 		return (-1);
1065 	}
1066 	v = str;
1067 
1068 	if ((ve = strchr(v, ':')) == NULL)
1069 		goto out;
1070 
1071 	*ve = 0;
1072 	if ((parms->scheme = strdup(v)) == NULL) {
1073 		fetch_syserr();
1074 		goto out;
1075 	}
1076 	v = ve + 1;
1077 
1078 	if ((ve = strchr(v, ':')) == NULL)
1079 		goto out;
1080 
1081 	*ve = 0;
1082 	if ((parms->realm = strdup(v)) == NULL) {
1083 		fetch_syserr();
1084 		goto out;
1085 	}
1086 	v = ve + 1;
1087 
1088 	if ((ve = strchr(v, ':')) == NULL)
1089 		goto out;
1090 
1091 	*ve = 0;
1092 	if ((parms->user = strdup(v)) == NULL) {
1093 		fetch_syserr();
1094 		goto out;
1095 	}
1096 	v = ve + 1;
1097 
1098 
1099 	if ((parms->password = strdup(v)) == NULL) {
1100 		fetch_syserr();
1101 		goto out;
1102 	}
1103 	ret = 0;
1104 out:
1105 	if (ret == -1)
1106 		clean_http_auth_params(parms);
1107 	if (str)
1108 		free(str);
1109 	return (ret);
1110 }
1111 
1112 
1113 /*
1114  * Digest response: the code to compute the digest is taken from the
1115  * sample implementation in RFC2616
1116  */
1117 #define IN const
1118 #define OUT
1119 
1120 #define HASHLEN 16
1121 typedef char HASH[HASHLEN];
1122 #define HASHHEXLEN 32
1123 typedef char HASHHEX[HASHHEXLEN+1];
1124 
1125 static const char *hexchars = "0123456789abcdef";
1126 static void
CvtHex(IN HASH Bin,OUT HASHHEX Hex)1127 CvtHex(IN HASH Bin, OUT HASHHEX Hex)
1128 {
1129 	unsigned short i;
1130 	unsigned char j;
1131 
1132 	for (i = 0; i < HASHLEN; i++) {
1133 		j = (Bin[i] >> 4) & 0xf;
1134 		Hex[i*2] = hexchars[j];
1135 		j = Bin[i] & 0xf;
1136 		Hex[i*2+1] = hexchars[j];
1137 	}
1138 	Hex[HASHHEXLEN] = '\0';
1139 };
1140 
1141 /* calculate H(A1) as per spec */
1142 static void
DigestCalcHA1(IN char * pszAlg,IN char * pszUserName,IN char * pszRealm,IN char * pszPassword,IN char * pszNonce,IN char * pszCNonce,OUT HASHHEX SessionKey)1143 DigestCalcHA1(
1144 	IN char * pszAlg,
1145 	IN char * pszUserName,
1146 	IN char * pszRealm,
1147 	IN char * pszPassword,
1148 	IN char * pszNonce,
1149 	IN char * pszCNonce,
1150 	OUT HASHHEX SessionKey
1151 	)
1152 {
1153 	MD5_CTX Md5Ctx;
1154 	HASH HA1;
1155 
1156 	MD5Init(&Md5Ctx);
1157 	MD5Update(&Md5Ctx, pszUserName, strlen(pszUserName));
1158 	MD5Update(&Md5Ctx, ":", 1);
1159 	MD5Update(&Md5Ctx, pszRealm, strlen(pszRealm));
1160 	MD5Update(&Md5Ctx, ":", 1);
1161 	MD5Update(&Md5Ctx, pszPassword, strlen(pszPassword));
1162 	MD5Final(HA1, &Md5Ctx);
1163 	if (strcasecmp(pszAlg, "md5-sess") == 0) {
1164 
1165 		MD5Init(&Md5Ctx);
1166 		MD5Update(&Md5Ctx, HA1, HASHLEN);
1167 		MD5Update(&Md5Ctx, ":", 1);
1168 		MD5Update(&Md5Ctx, pszNonce, strlen(pszNonce));
1169 		MD5Update(&Md5Ctx, ":", 1);
1170 		MD5Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
1171 		MD5Final(HA1, &Md5Ctx);
1172 	}
1173 	CvtHex(HA1, SessionKey);
1174 }
1175 
1176 /* calculate request-digest/response-digest as per HTTP Digest spec */
1177 static void
DigestCalcResponse(IN HASHHEX HA1,IN char * pszNonce,IN char * pszNonceCount,IN char * pszCNonce,IN char * pszQop,IN char * pszMethod,IN char * pszDigestUri,IN HASHHEX HEntity,OUT HASHHEX Response)1178 DigestCalcResponse(
1179 	IN HASHHEX HA1,           /* H(A1) */
1180 	IN char * pszNonce,       /* nonce from server */
1181 	IN char * pszNonceCount,  /* 8 hex digits */
1182 	IN char * pszCNonce,      /* client nonce */
1183 	IN char * pszQop,         /* qop-value: "", "auth", "auth-int" */
1184 	IN char * pszMethod,      /* method from the request */
1185 	IN char * pszDigestUri,   /* requested URL */
1186 	IN HASHHEX HEntity,       /* H(entity body) if qop="auth-int" */
1187 	OUT HASHHEX Response      /* request-digest or response-digest */
1188 	)
1189 {
1190 #if 0
1191 	DEBUGF("Calc: HA1[%s] Nonce[%s] qop[%s] method[%s] URI[%s]\n",
1192 	    HA1, pszNonce, pszQop, pszMethod, pszDigestUri);
1193 #endif
1194 	MD5_CTX Md5Ctx;
1195 	HASH HA2;
1196 	HASH RespHash;
1197 	HASHHEX HA2Hex;
1198 
1199 	// calculate H(A2)
1200 	MD5Init(&Md5Ctx);
1201 	MD5Update(&Md5Ctx, pszMethod, strlen(pszMethod));
1202 	MD5Update(&Md5Ctx, ":", 1);
1203 	MD5Update(&Md5Ctx, pszDigestUri, strlen(pszDigestUri));
1204 	if (strcasecmp(pszQop, "auth-int") == 0) {
1205 		MD5Update(&Md5Ctx, ":", 1);
1206 		MD5Update(&Md5Ctx, HEntity, HASHHEXLEN);
1207 	}
1208 	MD5Final(HA2, &Md5Ctx);
1209 	CvtHex(HA2, HA2Hex);
1210 
1211 	// calculate response
1212 	MD5Init(&Md5Ctx);
1213 	MD5Update(&Md5Ctx, HA1, HASHHEXLEN);
1214 	MD5Update(&Md5Ctx, ":", 1);
1215 	MD5Update(&Md5Ctx, pszNonce, strlen(pszNonce));
1216 	MD5Update(&Md5Ctx, ":", 1);
1217 	if (*pszQop) {
1218 		MD5Update(&Md5Ctx, pszNonceCount, strlen(pszNonceCount));
1219 		MD5Update(&Md5Ctx, ":", 1);
1220 		MD5Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
1221 		MD5Update(&Md5Ctx, ":", 1);
1222 		MD5Update(&Md5Ctx, pszQop, strlen(pszQop));
1223 		MD5Update(&Md5Ctx, ":", 1);
1224 	}
1225 	MD5Update(&Md5Ctx, HA2Hex, HASHHEXLEN);
1226 	MD5Final(RespHash, &Md5Ctx);
1227 	CvtHex(RespHash, Response);
1228 }
1229 
1230 /*
1231  * Generate/Send a Digest authorization header
1232  * This looks like: [Proxy-]Authorization: credentials
1233  *
1234  *  credentials      = "Digest" digest-response
1235  *  digest-response  = 1#( username | realm | nonce | digest-uri
1236  *                      | response | [ algorithm ] | [cnonce] |
1237  *                      [opaque] | [message-qop] |
1238  *                          [nonce-count]  | [auth-param] )
1239  *  username         = "username" "=" username-value
1240  *  username-value   = quoted-string
1241  *  digest-uri       = "uri" "=" digest-uri-value
1242  *  digest-uri-value = request-uri   ; As specified by HTTP/1.1
1243  *  message-qop      = "qop" "=" qop-value
1244  *  cnonce           = "cnonce" "=" cnonce-value
1245  *  cnonce-value     = nonce-value
1246  *  nonce-count      = "nc" "=" nc-value
1247  *  nc-value         = 8LHEX
1248  *  response         = "response" "=" request-digest
1249  *  request-digest = <"> 32LHEX <">
1250  */
1251 static int
http_digest_auth(conn_t * conn,const char * hdr,http_auth_challenge_t * c,http_auth_params_t * parms,struct url * url)1252 http_digest_auth(conn_t *conn, const char *hdr, http_auth_challenge_t *c,
1253 		 http_auth_params_t *parms, struct url *url)
1254 {
1255 	int r;
1256 	char noncecount[10];
1257 	char cnonce[40];
1258 	char *options = NULL;
1259 
1260 	if (!c->realm || !c->nonce) {
1261 		DEBUGF("realm/nonce not set in challenge\n");
1262 		return(-1);
1263 	}
1264 	if (!c->algo)
1265 		c->algo = strdup("");
1266 
1267 	if (asprintf(&options, "%s%s%s%s",
1268 	    *c->algo? ",algorithm=" : "", c->algo,
1269 	    c->opaque? ",opaque=" : "", c->opaque?c->opaque:"") < 0)
1270 		return (-1);
1271 
1272 	if (!c->qop) {
1273 		c->qop = strdup("");
1274 		*noncecount = 0;
1275 		*cnonce = 0;
1276 	} else {
1277 		c->nc++;
1278 		sprintf(noncecount, "%08x", c->nc);
1279 		/* We don't try very hard with the cnonce ... */
1280 		sprintf(cnonce, "%x%lx", getpid(), (unsigned long)time(0));
1281 	}
1282 
1283 	HASHHEX HA1;
1284 	DigestCalcHA1(c->algo, parms->user, c->realm,
1285 		      parms->password, c->nonce, cnonce, HA1);
1286 	DEBUGF("HA1: [%s]\n", HA1);
1287 	HASHHEX digest;
1288 	DigestCalcResponse(HA1, c->nonce, noncecount, cnonce, c->qop,
1289 			   "GET", url->doc, "", digest);
1290 
1291 	if (c->qop[0]) {
1292 		r = http_cmd(conn, "%s: Digest username=\"%s\",realm=\"%s\","
1293 			     "nonce=\"%s\",uri=\"%s\",response=\"%s\","
1294 			     "qop=\"auth\", cnonce=\"%s\", nc=%s%s",
1295 			     hdr, parms->user, c->realm,
1296 			     c->nonce, url->doc, digest,
1297 			     cnonce, noncecount, options);
1298 	} else {
1299 		r = http_cmd(conn, "%s: Digest username=\"%s\",realm=\"%s\","
1300 			     "nonce=\"%s\",uri=\"%s\",response=\"%s\"%s",
1301 			     hdr, parms->user, c->realm,
1302 			     c->nonce, url->doc, digest, options);
1303 	}
1304 	if (options)
1305 		free(options);
1306 	return (r);
1307 }
1308 
1309 /*
1310  * Encode username and password
1311  */
1312 static int
http_basic_auth(conn_t * conn,const char * hdr,const char * usr,const char * pwd)1313 http_basic_auth(conn_t *conn, const char *hdr, const char *usr, const char *pwd)
1314 {
1315 	char *upw, *auth;
1316 	int r;
1317 
1318 	DEBUGF("basic: usr: [%s]\n", usr);
1319 	DEBUGF("basic: pwd: [%s]\n", pwd);
1320 	if (asprintf(&upw, "%s:%s", usr, pwd) == -1)
1321 		return (-1);
1322 	auth = http_base64(upw);
1323 	free(upw);
1324 	if (auth == NULL)
1325 		return (-1);
1326 	r = http_cmd(conn, "%s: Basic %s", hdr, auth);
1327 	free(auth);
1328 	return (r);
1329 }
1330 
1331 /*
1332  * Chose the challenge to answer and call the appropriate routine to
1333  * produce the header.
1334  */
1335 static int
http_authorize(conn_t * conn,const char * hdr,http_auth_challenges_t * cs,http_auth_params_t * parms,struct url * url)1336 http_authorize(conn_t *conn, const char *hdr, http_auth_challenges_t *cs,
1337 	       http_auth_params_t *parms, struct url *url)
1338 {
1339 	http_auth_challenge_t *digest = NULL;
1340 	int i;
1341 
1342 	/* If user or pass are null we're not happy */
1343 	if (!parms->user || !parms->password) {
1344 		DEBUGF("NULL usr or pass\n");
1345 		return (-1);
1346 	}
1347 
1348 	/* Look for a Digest */
1349 	for (i = 0; i < cs->count; i++) {
1350 		if (cs->challenges[i]->scheme == HTTPAS_DIGEST)
1351 			digest = cs->challenges[i];
1352 	}
1353 
1354 	/* Error if "Digest" was specified and there is no Digest challenge */
1355 	if (!digest &&
1356 	    (parms->scheme && strcasecmp(parms->scheme, "digest") == 0)) {
1357 		DEBUGF("Digest auth in env, not supported by peer\n");
1358 		return (-1);
1359 	}
1360 	/*
1361 	 * If "basic" was specified in the environment, or there is no Digest
1362 	 * challenge, do the basic thing. Don't need a challenge for this,
1363 	 * so no need to check basic!=NULL
1364 	 */
1365 	if (!digest ||
1366 	    (parms->scheme && strcasecmp(parms->scheme, "basic") == 0))
1367 		return (http_basic_auth(conn,hdr,parms->user,parms->password));
1368 
1369 	/* Else, prefer digest. We just checked that it's not NULL */
1370 	return (http_digest_auth(conn, hdr, digest, parms, url));
1371 }
1372 
1373 /*****************************************************************************
1374  * Helper functions for connecting to a server or proxy
1375  */
1376 
1377 /*
1378  * Connect to the correct HTTP server or proxy.
1379  */
1380 static conn_t *
http_connect(struct url * URL,struct url * purl,const char * flags)1381 http_connect(struct url *URL, struct url *purl, const char *flags)
1382 {
1383 	struct url *curl;
1384 	conn_t *conn;
1385 	hdr_t h;
1386 	http_headerbuf_t headerbuf;
1387 	const char *p;
1388 	int verbose;
1389 	int af, val;
1390 	int serrno;
1391 
1392 #ifdef INET6
1393 	af = AF_UNSPEC;
1394 #else
1395 	af = AF_INET;
1396 #endif
1397 
1398 	verbose = CHECK_FLAG('v');
1399 	if (CHECK_FLAG('4'))
1400 		af = AF_INET;
1401 #ifdef INET6
1402 	else if (CHECK_FLAG('6'))
1403 		af = AF_INET6;
1404 #endif
1405 
1406 	curl = (purl != NULL) ? purl : URL;
1407 
1408 	if ((conn = fetch_connect(curl->host, curl->port, af, verbose)) == NULL)
1409 		/* fetch_connect() has already set an error code */
1410 		return (NULL);
1411 	init_http_headerbuf(&headerbuf);
1412 	if (strcmp(URL->scheme, SCHEME_HTTPS) == 0 && purl) {
1413 		http_cmd(conn, "CONNECT %s:%d HTTP/1.1",
1414 		    URL->host, URL->port);
1415 		http_cmd(conn, "Host: %s:%d",
1416 		    URL->host, URL->port);
1417 		http_cmd(conn, "");
1418 		if (http_get_reply(conn) != HTTP_OK) {
1419 			http_seterr(conn->err);
1420 			goto ouch;
1421 		}
1422 		/* Read and discard the rest of the proxy response */
1423 		if (fetch_getln(conn) < 0) {
1424 			fetch_syserr();
1425 			goto ouch;
1426 		}
1427 		do {
1428 			switch ((h = http_next_header(conn, &headerbuf, &p))) {
1429 			case hdr_syserror:
1430 				fetch_syserr();
1431 				goto ouch;
1432 			case hdr_error:
1433 				http_seterr(HTTP_PROTOCOL_ERROR);
1434 				goto ouch;
1435 			default:
1436 				/* ignore */ ;
1437 			}
1438 		} while (h > hdr_end);
1439 	}
1440 	if (strcmp(URL->scheme, SCHEME_HTTPS) == 0 &&
1441 	    fetch_ssl(conn, URL, verbose) == -1) {
1442 		/* grrr */
1443 		errno = EAUTH;
1444 		fetch_syserr();
1445 		goto ouch;
1446 	}
1447 
1448 	val = 1;
1449 	setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val, sizeof(val));
1450 
1451 	clean_http_headerbuf(&headerbuf);
1452 	return (conn);
1453 ouch:
1454 	serrno = errno;
1455 	clean_http_headerbuf(&headerbuf);
1456 	fetch_close(conn);
1457 	errno = serrno;
1458 	return (NULL);
1459 }
1460 
1461 static struct url *
http_get_proxy(struct url * url,const char * flags)1462 http_get_proxy(struct url * url, const char *flags)
1463 {
1464 	struct url *purl;
1465 	char *p;
1466 
1467 	if (flags != NULL && strchr(flags, 'd') != NULL)
1468 		return (NULL);
1469 	if (fetch_no_proxy_match(url->host))
1470 		return (NULL);
1471 	if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
1472 	    *p && (purl = fetchParseURL(p))) {
1473 		if (!*purl->scheme)
1474 			strcpy(purl->scheme, SCHEME_HTTP);
1475 		if (!purl->port)
1476 			purl->port = fetch_default_proxy_port(purl->scheme);
1477 		if (strcmp(purl->scheme, SCHEME_HTTP) == 0)
1478 			return (purl);
1479 		fetchFreeURL(purl);
1480 	}
1481 	return (NULL);
1482 }
1483 
1484 static void
http_print_html(FILE * out,FILE * in)1485 http_print_html(FILE *out, FILE *in)
1486 {
1487 	size_t len;
1488 	char *line, *p, *q;
1489 	int comment, tag;
1490 
1491 	comment = tag = 0;
1492 	while ((line = fgetln(in, &len)) != NULL) {
1493 		while (len && isspace((unsigned char)line[len - 1]))
1494 			--len;
1495 		for (p = q = line; q < line + len; ++q) {
1496 			if (comment && *q == '-') {
1497 				if (q + 2 < line + len &&
1498 				    strcmp(q, "-->") == 0) {
1499 					tag = comment = 0;
1500 					q += 2;
1501 				}
1502 			} else if (tag && !comment && *q == '>') {
1503 				p = q + 1;
1504 				tag = 0;
1505 			} else if (!tag && *q == '<') {
1506 				if (q > p)
1507 					fwrite(p, q - p, 1, out);
1508 				tag = 1;
1509 				if (q + 3 < line + len &&
1510 				    strcmp(q, "<!--") == 0) {
1511 					comment = 1;
1512 					q += 3;
1513 				}
1514 			}
1515 		}
1516 		if (!tag && q > p)
1517 			fwrite(p, q - p, 1, out);
1518 		fputc('\n', out);
1519 	}
1520 }
1521 
1522 
1523 /*****************************************************************************
1524  * Core
1525  */
1526 
1527 FILE *
http_request(struct url * URL,const char * op,struct url_stat * us,struct url * purl,const char * flags)1528 http_request(struct url *URL, const char *op, struct url_stat *us,
1529 	struct url *purl, const char *flags)
1530 {
1531 
1532 	return (http_request_body(URL, op, us, purl, flags, NULL, NULL));
1533 }
1534 
1535 /*
1536  * Send a request and process the reply
1537  *
1538  * XXX This function is way too long, the do..while loop should be split
1539  * XXX off into a separate function.
1540  */
1541 FILE *
http_request_body(struct url * URL,const char * op,struct url_stat * us,struct url * purl,const char * flags,const char * content_type,const char * body)1542 http_request_body(struct url *URL, const char *op, struct url_stat *us,
1543 	struct url *purl, const char *flags, const char *content_type,
1544 	const char *body)
1545 {
1546 	char timebuf[80];
1547 	char hbuf[MAXHOSTNAMELEN + 7], *host;
1548 	conn_t *conn;
1549 	struct url *url, *new;
1550 	int chunked, direct, ims, noredirect, verbose;
1551 	int e, i, n, val;
1552 	off_t offset, clength, length, size;
1553 	time_t mtime;
1554 	const char *p;
1555 	FILE *f;
1556 	hdr_t h;
1557 	struct tm *timestruct;
1558 	http_headerbuf_t headerbuf;
1559 	http_auth_challenges_t server_challenges;
1560 	http_auth_challenges_t proxy_challenges;
1561 	size_t body_len;
1562 
1563 	/* The following calls don't allocate anything */
1564 	init_http_headerbuf(&headerbuf);
1565 	init_http_auth_challenges(&server_challenges);
1566 	init_http_auth_challenges(&proxy_challenges);
1567 
1568 	direct = CHECK_FLAG('d');
1569 	noredirect = CHECK_FLAG('A');
1570 	verbose = CHECK_FLAG('v');
1571 	ims = CHECK_FLAG('i');
1572 
1573 	if (direct && purl) {
1574 		fetchFreeURL(purl);
1575 		purl = NULL;
1576 	}
1577 
1578 	/* try the provided URL first */
1579 	url = URL;
1580 
1581 	n = MAX_REDIRECT;
1582 	i = 0;
1583 
1584 	e = HTTP_PROTOCOL_ERROR;
1585 	do {
1586 		new = NULL;
1587 		chunked = 0;
1588 		offset = 0;
1589 		clength = -1;
1590 		length = -1;
1591 		size = -1;
1592 		mtime = 0;
1593 
1594 		/* check port */
1595 		if (!url->port)
1596 			url->port = fetch_default_port(url->scheme);
1597 
1598 		/* were we redirected to an FTP URL? */
1599 		if (purl == NULL && strcmp(url->scheme, SCHEME_FTP) == 0) {
1600 			if (strcmp(op, "GET") == 0)
1601 				return (ftp_request(url, "RETR", us, purl, flags));
1602 			else if (strcmp(op, "HEAD") == 0)
1603 				return (ftp_request(url, "STAT", us, purl, flags));
1604 		}
1605 
1606 		/* connect to server or proxy */
1607 		if ((conn = http_connect(url, purl, flags)) == NULL)
1608 			goto ouch;
1609 
1610 		/* append port number only if necessary */
1611 		host = url->host;
1612 		if (url->port != fetch_default_port(url->scheme)) {
1613 			snprintf(hbuf, sizeof(hbuf), "%s:%d", host, url->port);
1614 			host = hbuf;
1615 		}
1616 
1617 		/* send request */
1618 		if (verbose)
1619 			fetch_info("requesting %s://%s%s",
1620 			    url->scheme, host, url->doc);
1621 		if (purl && strcmp(url->scheme, SCHEME_HTTPS) != 0) {
1622 			http_cmd(conn, "%s %s://%s%s HTTP/1.1",
1623 			    op, url->scheme, host, url->doc);
1624 		} else {
1625 			http_cmd(conn, "%s %s HTTP/1.1",
1626 			    op, url->doc);
1627 		}
1628 
1629 		if (ims && url->ims_time) {
1630 			timestruct = gmtime((time_t *)&url->ims_time);
1631 			(void)strftime(timebuf, 80, "%a, %d %b %Y %T GMT",
1632 			    timestruct);
1633 			if (verbose)
1634 				fetch_info("If-Modified-Since: %s", timebuf);
1635 			http_cmd(conn, "If-Modified-Since: %s", timebuf);
1636 		}
1637 		/* virtual host */
1638 		http_cmd(conn, "Host: %s", host);
1639 
1640 		/*
1641 		 * Proxy authorization: we only send auth after we received
1642 		 * a 407 error. We do not first try basic anyway (changed
1643 		 * when support was added for digest-auth)
1644 		 */
1645 		if (purl && proxy_challenges.valid) {
1646 			http_auth_params_t aparams;
1647 			init_http_auth_params(&aparams);
1648 			if (*purl->user || *purl->pwd) {
1649 				aparams.user = strdup(purl->user);
1650 				aparams.password = strdup(purl->pwd);
1651 			} else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL &&
1652 				   *p != '\0') {
1653 				if (http_authfromenv(p, &aparams) < 0) {
1654 					http_seterr(HTTP_NEED_PROXY_AUTH);
1655 					goto ouch;
1656 				}
1657 			} else if (fetch_netrc_auth(purl) == 0) {
1658 				aparams.user = strdup(purl->user);
1659 				aparams.password = strdup(purl->pwd);
1660 			}
1661 			http_authorize(conn, "Proxy-Authorization",
1662 				       &proxy_challenges, &aparams, url);
1663 			clean_http_auth_params(&aparams);
1664 		}
1665 
1666 		/*
1667 		 * Server authorization: we never send "a priori"
1668 		 * Basic auth, which used to be done if user/pass were
1669 		 * set in the url. This would be weird because we'd send the
1670 		 * password in the clear even if Digest is finally to be
1671 		 * used (it would have made more sense for the
1672 		 * pre-digest version to do this when Basic was specified
1673 		 * in the environment)
1674 		 */
1675 		if (server_challenges.valid) {
1676 			http_auth_params_t aparams;
1677 			init_http_auth_params(&aparams);
1678 			if (*url->user || *url->pwd) {
1679 				aparams.user = strdup(url->user);
1680 				aparams.password = strdup(url->pwd);
1681 			} else if ((p = getenv("HTTP_AUTH")) != NULL &&
1682 				   *p != '\0') {
1683 				if (http_authfromenv(p, &aparams) < 0) {
1684 					http_seterr(HTTP_NEED_AUTH);
1685 					goto ouch;
1686 				}
1687 			} else if (fetch_netrc_auth(url) == 0) {
1688 				aparams.user = strdup(url->user);
1689 				aparams.password = strdup(url->pwd);
1690 			} else if (fetchAuthMethod &&
1691 				   fetchAuthMethod(url) == 0) {
1692 				aparams.user = strdup(url->user);
1693 				aparams.password = strdup(url->pwd);
1694 			} else {
1695 				http_seterr(HTTP_NEED_AUTH);
1696 				goto ouch;
1697 			}
1698 			http_authorize(conn, "Authorization",
1699 				       &server_challenges, &aparams, url);
1700 			clean_http_auth_params(&aparams);
1701 		}
1702 
1703 		/* other headers */
1704 		if ((p = getenv("HTTP_ACCEPT")) != NULL) {
1705 			if (*p != '\0')
1706 				http_cmd(conn, "Accept: %s", p);
1707 		} else {
1708 			http_cmd(conn, "Accept: */*");
1709 		}
1710 		if ((p = getenv("HTTP_REFERER")) != NULL && *p != '\0') {
1711 			if (strcasecmp(p, "auto") == 0)
1712 				http_cmd(conn, "Referer: %s://%s%s",
1713 				    url->scheme, host, url->doc);
1714 			else
1715 				http_cmd(conn, "Referer: %s", p);
1716 		}
1717 		if ((p = getenv("HTTP_USER_AGENT")) != NULL) {
1718 			/* no User-Agent if defined but empty */
1719 			if  (*p != '\0')
1720 				http_cmd(conn, "User-Agent: %s", p);
1721 		} else {
1722 			/* default User-Agent */
1723 			http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER,
1724 			    getprogname());
1725 		}
1726 		if (url->offset > 0)
1727 			http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset);
1728 		http_cmd(conn, "Connection: close");
1729 
1730 		if (body) {
1731 			body_len = strlen(body);
1732 			http_cmd(conn, "Content-Length: %zu", body_len);
1733 			if (content_type != NULL)
1734 				http_cmd(conn, "Content-Type: %s", content_type);
1735 		}
1736 
1737 		http_cmd(conn, "");
1738 
1739 		if (body)
1740 			fetch_write(conn, body, body_len);
1741 
1742 		/*
1743 		 * Force the queued request to be dispatched.  Normally, one
1744 		 * would do this with shutdown(2) but squid proxies can be
1745 		 * configured to disallow such half-closed connections.  To
1746 		 * be compatible with such configurations, fiddle with socket
1747 		 * options to force the pending data to be written.
1748 		 */
1749 		val = 0;
1750 		setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val,
1751 			   sizeof(val));
1752 		val = 1;
1753 		setsockopt(conn->sd, IPPROTO_TCP, TCP_NODELAY, &val,
1754 			   sizeof(val));
1755 
1756 		/* get reply */
1757 		switch (http_get_reply(conn)) {
1758 		case HTTP_OK:
1759 		case HTTP_PARTIAL:
1760 		case HTTP_NOT_MODIFIED:
1761 			/* fine */
1762 			break;
1763 		case HTTP_MOVED_PERM:
1764 		case HTTP_MOVED_TEMP:
1765 		case HTTP_TEMP_REDIRECT:
1766 		case HTTP_PERM_REDIRECT:
1767 		case HTTP_SEE_OTHER:
1768 		case HTTP_USE_PROXY:
1769 			/*
1770 			 * Not so fine, but we still have to read the
1771 			 * headers to get the new location.
1772 			 */
1773 			break;
1774 		case HTTP_NEED_AUTH:
1775 			if (server_challenges.valid) {
1776 				/*
1777 				 * We already sent out authorization code,
1778 				 * so there's nothing more we can do.
1779 				 */
1780 				http_seterr(conn->err);
1781 				goto ouch;
1782 			}
1783 			/* try again, but send the password this time */
1784 			if (verbose)
1785 				fetch_info("server requires authorization");
1786 			break;
1787 		case HTTP_NEED_PROXY_AUTH:
1788 			if (proxy_challenges.valid) {
1789 				/*
1790 				 * We already sent our proxy
1791 				 * authorization code, so there's
1792 				 * nothing more we can do. */
1793 				http_seterr(conn->err);
1794 				goto ouch;
1795 			}
1796 			/* try again, but send the password this time */
1797 			if (verbose)
1798 				fetch_info("proxy requires authorization");
1799 			break;
1800 		case HTTP_BAD_RANGE:
1801 			/*
1802 			 * This can happen if we ask for 0 bytes because
1803 			 * we already have the whole file.  Consider this
1804 			 * a success for now, and check sizes later.
1805 			 */
1806 			break;
1807 		case HTTP_PROTOCOL_ERROR:
1808 			/* fall through */
1809 		case -1:
1810 			fetch_syserr();
1811 			goto ouch;
1812 		default:
1813 			http_seterr(conn->err);
1814 			if (!verbose)
1815 				goto ouch;
1816 			/* fall through so we can get the full error message */
1817 		}
1818 
1819 		/* get headers. http_next_header expects one line readahead */
1820 		if (fetch_getln(conn) == -1) {
1821 			fetch_syserr();
1822 			goto ouch;
1823 		}
1824 		do {
1825 			switch ((h = http_next_header(conn, &headerbuf, &p))) {
1826 			case hdr_syserror:
1827 				fetch_syserr();
1828 				goto ouch;
1829 			case hdr_error:
1830 				http_seterr(HTTP_PROTOCOL_ERROR);
1831 				goto ouch;
1832 			case hdr_content_length:
1833 				http_parse_length(p, &clength);
1834 				break;
1835 			case hdr_content_range:
1836 				http_parse_range(p, &offset, &length, &size);
1837 				break;
1838 			case hdr_last_modified:
1839 				http_parse_mtime(p, &mtime);
1840 				break;
1841 			case hdr_location:
1842 				if (!HTTP_REDIRECT(conn->err))
1843 					break;
1844 				/*
1845 				 * if the A flag is set, we don't follow
1846 				 * temporary redirects.
1847 				 */
1848 				if (noredirect &&
1849 				    conn->err != HTTP_MOVED_PERM &&
1850 				    conn->err != HTTP_PERM_REDIRECT &&
1851 				    conn->err != HTTP_USE_PROXY) {
1852 					n = 1;
1853 					break;
1854 				}
1855 				if (new)
1856 					free(new);
1857 				if (verbose)
1858 					fetch_info("%d redirect to %s",
1859 					    conn->err, p);
1860 				if (*p == '/')
1861 					/* absolute path */
1862 					new = fetchMakeURL(url->scheme, url->host,
1863 					    url->port, p, url->user, url->pwd);
1864 				else
1865 					new = fetchParseURL(p);
1866 				if (new == NULL) {
1867 					/* XXX should set an error code */
1868 					DEBUGF("failed to parse new URL\n");
1869 					goto ouch;
1870 				}
1871 
1872 				/* Only copy credentials if the host matches */
1873 				if (strcmp(new->host, url->host) == 0 &&
1874 				    !*new->user && !*new->pwd) {
1875 					strcpy(new->user, url->user);
1876 					strcpy(new->pwd, url->pwd);
1877 				}
1878 				new->offset = url->offset;
1879 				new->length = url->length;
1880 				new->ims_time = url->ims_time;
1881 				break;
1882 			case hdr_transfer_encoding:
1883 				/* XXX weak test*/
1884 				chunked = (strcasecmp(p, "chunked") == 0);
1885 				break;
1886 			case hdr_www_authenticate:
1887 				if (conn->err != HTTP_NEED_AUTH)
1888 					break;
1889 				if (http_parse_authenticate(p, &server_challenges) == 0)
1890 					++n;
1891 				break;
1892 			case hdr_proxy_authenticate:
1893 				if (conn->err != HTTP_NEED_PROXY_AUTH)
1894 					break;
1895 				if (http_parse_authenticate(p, &proxy_challenges) == 0)
1896 					++n;
1897 				break;
1898 			case hdr_end:
1899 				/* fall through */
1900 			case hdr_unknown:
1901 				/* ignore */
1902 				break;
1903 			}
1904 		} while (h > hdr_end);
1905 
1906 		/* we need to provide authentication */
1907 		if (conn->err == HTTP_NEED_AUTH ||
1908 		    conn->err == HTTP_NEED_PROXY_AUTH) {
1909 			e = conn->err;
1910 			if ((conn->err == HTTP_NEED_AUTH &&
1911 			     !server_challenges.valid) ||
1912 			    (conn->err == HTTP_NEED_PROXY_AUTH &&
1913 			     !proxy_challenges.valid)) {
1914 				/* 401/7 but no www/proxy-authenticate ?? */
1915 				DEBUGF("%03d without auth header\n", conn->err);
1916 				goto ouch;
1917 			}
1918 			fetch_close(conn);
1919 			conn = NULL;
1920 			continue;
1921 		}
1922 
1923 		/* requested range not satisfiable */
1924 		if (conn->err == HTTP_BAD_RANGE) {
1925 			if (url->offset > 0 && url->length == 0) {
1926 				/* asked for 0 bytes; fake it */
1927 				offset = url->offset;
1928 				clength = -1;
1929 				conn->err = HTTP_OK;
1930 				break;
1931 			} else {
1932 				http_seterr(conn->err);
1933 				goto ouch;
1934 			}
1935 		}
1936 
1937 		/* we have a hit or an error */
1938 		if (conn->err == HTTP_OK
1939 		    || conn->err == HTTP_NOT_MODIFIED
1940 		    || conn->err == HTTP_PARTIAL
1941 		    || HTTP_ERROR(conn->err))
1942 			break;
1943 
1944 		/* all other cases: we got a redirect */
1945 		e = conn->err;
1946 		clean_http_auth_challenges(&server_challenges);
1947 		fetch_close(conn);
1948 		conn = NULL;
1949 		if (!new) {
1950 			DEBUGF("redirect with no new location\n");
1951 			break;
1952 		}
1953 		if (url != URL)
1954 			fetchFreeURL(url);
1955 		url = new;
1956 	} while (++i < n);
1957 
1958 	/* we failed, or ran out of retries */
1959 	if (conn == NULL) {
1960 		http_seterr(e);
1961 		goto ouch;
1962 	}
1963 
1964 	DEBUGF("offset %lld, length %lld, size %lld, clength %lld\n",
1965 	    (long long)offset, (long long)length,
1966 	    (long long)size, (long long)clength);
1967 
1968 	if (conn->err == HTTP_NOT_MODIFIED) {
1969 		http_seterr(HTTP_NOT_MODIFIED);
1970 		return (NULL);
1971 	}
1972 
1973 	/* check for inconsistencies */
1974 	if (clength != -1 && length != -1 && clength != length) {
1975 		http_seterr(HTTP_PROTOCOL_ERROR);
1976 		goto ouch;
1977 	}
1978 	if (clength == -1)
1979 		clength = length;
1980 	if (clength != -1)
1981 		length = offset + clength;
1982 	if (length != -1 && size != -1 && length != size) {
1983 		http_seterr(HTTP_PROTOCOL_ERROR);
1984 		goto ouch;
1985 	}
1986 	if (size == -1)
1987 		size = length;
1988 
1989 	/* fill in stats */
1990 	if (us) {
1991 		us->size = size;
1992 		us->atime = us->mtime = mtime;
1993 	}
1994 
1995 	/* too far? */
1996 	if (URL->offset > 0 && offset > URL->offset) {
1997 		http_seterr(HTTP_PROTOCOL_ERROR);
1998 		goto ouch;
1999 	}
2000 
2001 	/* report back real offset and size */
2002 	URL->offset = offset;
2003 	URL->length = clength;
2004 
2005 	/* wrap it up in a FILE */
2006 	if ((f = http_funopen(conn, chunked)) == NULL) {
2007 		fetch_syserr();
2008 		goto ouch;
2009 	}
2010 
2011 	if (url != URL)
2012 		fetchFreeURL(url);
2013 	if (purl)
2014 		fetchFreeURL(purl);
2015 
2016 	if (HTTP_ERROR(conn->err)) {
2017 		http_print_html(stderr, f);
2018 		fclose(f);
2019 		f = NULL;
2020 	}
2021 	clean_http_headerbuf(&headerbuf);
2022 	clean_http_auth_challenges(&server_challenges);
2023 	clean_http_auth_challenges(&proxy_challenges);
2024 	return (f);
2025 
2026 ouch:
2027 	if (url != URL)
2028 		fetchFreeURL(url);
2029 	if (purl)
2030 		fetchFreeURL(purl);
2031 	if (conn != NULL)
2032 		fetch_close(conn);
2033 	clean_http_headerbuf(&headerbuf);
2034 	clean_http_auth_challenges(&server_challenges);
2035 	clean_http_auth_challenges(&proxy_challenges);
2036 	return (NULL);
2037 }
2038 
2039 
2040 /*****************************************************************************
2041  * Entry points
2042  */
2043 
2044 /*
2045  * Retrieve and stat a file by HTTP
2046  */
2047 FILE *
fetchXGetHTTP(struct url * URL,struct url_stat * us,const char * flags)2048 fetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags)
2049 {
2050 	return (http_request(URL, "GET", us, http_get_proxy(URL, flags), flags));
2051 }
2052 
2053 /*
2054  * Retrieve a file by HTTP
2055  */
2056 FILE *
fetchGetHTTP(struct url * URL,const char * flags)2057 fetchGetHTTP(struct url *URL, const char *flags)
2058 {
2059 	return (fetchXGetHTTP(URL, NULL, flags));
2060 }
2061 
2062 /*
2063  * Store a file by HTTP
2064  */
2065 FILE *
fetchPutHTTP(struct url * URL __unused,const char * flags __unused)2066 fetchPutHTTP(struct url *URL __unused, const char *flags __unused)
2067 {
2068 	warnx("fetchPutHTTP(): not implemented");
2069 	return (NULL);
2070 }
2071 
2072 /*
2073  * Get an HTTP document's metadata
2074  */
2075 int
fetchStatHTTP(struct url * URL,struct url_stat * us,const char * flags)2076 fetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags)
2077 {
2078 	FILE *f;
2079 
2080 	f = http_request(URL, "HEAD", us, http_get_proxy(URL, flags), flags);
2081 	if (f == NULL)
2082 		return (-1);
2083 	fclose(f);
2084 	return (0);
2085 }
2086 
2087 /*
2088  * List a directory
2089  */
2090 struct url_ent *
fetchListHTTP(struct url * url __unused,const char * flags __unused)2091 fetchListHTTP(struct url *url __unused, const char *flags __unused)
2092 {
2093 	warnx("fetchListHTTP(): not implemented");
2094 	return (NULL);
2095 }
2096 
2097 /*
2098  * Arbitrary HTTP verb and content requests
2099  */
2100 FILE *
fetchReqHTTP(struct url * URL,const char * method,const char * flags,const char * content_type,const char * body)2101 fetchReqHTTP(struct url *URL, const char *method, const char *flags,
2102 	const char *content_type, const char *body)
2103 {
2104 
2105 	return (http_request_body(URL, method, NULL, http_get_proxy(URL, flags),
2106 	    flags, content_type, body));
2107 }
2108