Home | History | Annotate | Line # | Download | only in testcode
petal.c revision 1.1.1.6.6.1
      1          1.1  christos /*
      2          1.1  christos  * petal.c - https daemon that is small and beautiful.
      3          1.1  christos  *
      4          1.1  christos  * Copyright (c) 2010, NLnet Labs. All rights reserved.
      5          1.1  christos  *
      6          1.1  christos  * This software is open source.
      7          1.1  christos  *
      8          1.1  christos  * Redistribution and use in source and binary forms, with or without
      9          1.1  christos  * modification, are permitted provided that the following conditions
     10          1.1  christos  * are met:
     11          1.1  christos  *
     12          1.1  christos  * Redistributions of source code must retain the above copyright notice,
     13          1.1  christos  * this list of conditions and the following disclaimer.
     14          1.1  christos  *
     15          1.1  christos  * Redistributions in binary form must reproduce the above copyright notice,
     16          1.1  christos  * this list of conditions and the following disclaimer in the documentation
     17          1.1  christos  * and/or other materials provided with the distribution.
     18          1.1  christos  *
     19          1.1  christos  * Neither the name of the NLNET LABS nor the names of its contributors may
     20          1.1  christos  * be used to endorse or promote products derived from this software without
     21          1.1  christos  * specific prior written permission.
     22          1.1  christos  *
     23          1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     24          1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     25          1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     26          1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     27          1.1  christos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     28          1.1  christos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     29          1.1  christos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     30          1.1  christos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     31          1.1  christos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     32          1.1  christos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     33          1.1  christos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34          1.1  christos  */
     35          1.1  christos 
     36          1.1  christos /**
     37          1.1  christos  * \file
     38          1.1  christos  *
     39          1.1  christos  * HTTP1.1/SSL server.
     40          1.1  christos  */
     41          1.1  christos 
     42          1.1  christos #include "config.h"
     43          1.1  christos #ifdef HAVE_GETOPT_H
     44          1.1  christos #include <getopt.h>
     45          1.1  christos #endif
     46          1.1  christos #ifdef HAVE_OPENSSL_SSL_H
     47          1.1  christos #include <openssl/ssl.h>
     48          1.1  christos #endif
     49          1.1  christos #ifdef HAVE_OPENSSL_ERR_H
     50          1.1  christos #include <openssl/err.h>
     51          1.1  christos #endif
     52          1.1  christos #ifdef HAVE_OPENSSL_RAND_H
     53          1.1  christos #include <openssl/rand.h>
     54          1.1  christos #endif
     55          1.1  christos #include <openssl/x509.h>
     56          1.1  christos #include <openssl/pem.h>
     57          1.1  christos #include <ctype.h>
     58          1.1  christos #include <signal.h>
     59          1.1  christos #if defined(UNBOUND_ALLOC_LITE) || defined(UNBOUND_ALLOC_STATS)
     60          1.1  christos #ifdef malloc
     61          1.1  christos #undef malloc
     62          1.1  christos #endif
     63          1.1  christos #ifdef free
     64          1.1  christos #undef free
     65          1.1  christos #endif
     66          1.1  christos #endif /* alloc lite or alloc stats */
     67          1.1  christos 
     68          1.1  christos /** verbosity for this application */
     69          1.1  christos static int verb = 0;
     70          1.1  christos 
     71          1.1  christos /** Give petal usage, and exit (1). */
     72          1.1  christos static void
     73      1.1.1.2  christos usage(void)
     74          1.1  christos {
     75          1.1  christos 	printf("Usage:	petal [opts]\n");
     76          1.1  christos 	printf("	https daemon serves files from ./'host'/filename\n");
     77          1.1  christos 	printf("	(no hostname: from the 'default' directory)\n");
     78          1.1  christos 	printf("-a addr		bind to this address, 127.0.0.1\n");
     79          1.1  christos 	printf("-p port		port number, default 443\n");
     80          1.1  christos 	printf("-k keyfile	SSL private key file (PEM), petal.key\n");
     81          1.1  christos 	printf("-c certfile	SSL certificate file (PEM), petal.pem\n");
     82          1.1  christos 	printf("-v		more verbose\n");
     83          1.1  christos 	printf("-h		show this usage help\n");
     84          1.1  christos 	printf("Version %s\n", PACKAGE_VERSION);
     85          1.1  christos 	printf("BSD licensed, see LICENSE in source package for details.\n");
     86          1.1  christos 	printf("Report bugs to %s\n", PACKAGE_BUGREPORT);
     87          1.1  christos 	exit(1);
     88          1.1  christos }
     89          1.1  christos 
     90          1.1  christos /** fatal exit */
     91          1.1  christos static void print_exit(const char* str) {printf("error %s\n", str); exit(1);}
     92          1.1  christos /** print errno */
     93          1.1  christos static void log_errno(const char* str)
     94          1.1  christos {printf("error %s: %s\n", str, strerror(errno));}
     95          1.1  christos 
     96          1.1  christos /** parse a text IP address into a sockaddr */
     97          1.1  christos static int
     98          1.1  christos parse_ip_addr(char* str, int port, struct sockaddr_storage* ret, socklen_t* l)
     99          1.1  christos {
    100          1.1  christos 	socklen_t len = 0;
    101          1.1  christos 	struct sockaddr_storage* addr = NULL;
    102          1.1  christos 	struct sockaddr_in6 a6;
    103          1.1  christos 	struct sockaddr_in a;
    104          1.1  christos 	uint16_t p = (uint16_t)port;
    105          1.1  christos 	int fam = 0;
    106          1.1  christos 	memset(&a6, 0, sizeof(a6));
    107          1.1  christos 	memset(&a, 0, sizeof(a));
    108          1.1  christos 
    109          1.1  christos 	if(inet_pton(AF_INET6, str, &a6.sin6_addr) > 0) {
    110          1.1  christos 		/* it is an IPv6 */
    111          1.1  christos 		fam = AF_INET6;
    112          1.1  christos 		a6.sin6_family = AF_INET6;
    113          1.1  christos 		a6.sin6_port = (in_port_t)htons(p);
    114          1.1  christos 		addr = (struct sockaddr_storage*)&a6;
    115          1.1  christos 		len = (socklen_t)sizeof(struct sockaddr_in6);
    116          1.1  christos 	}
    117          1.1  christos 	if(inet_pton(AF_INET, str, &a.sin_addr) > 0) {
    118          1.1  christos 		/* it is an IPv4 */
    119          1.1  christos 		fam = AF_INET;
    120          1.1  christos 		a.sin_family = AF_INET;
    121          1.1  christos 		a.sin_port = (in_port_t)htons(p);
    122          1.1  christos 		addr = (struct sockaddr_storage*)&a;
    123          1.1  christos 		len = (socklen_t)sizeof(struct sockaddr_in);
    124          1.1  christos 	}
    125          1.1  christos 	if(!len) print_exit("cannot parse addr");
    126          1.1  christos 	*l = len;
    127          1.1  christos 	memmove(ret, addr, len);
    128          1.1  christos 	return fam;
    129          1.1  christos }
    130          1.1  christos 
    131          1.1  christos /** close the fd */
    132          1.1  christos static void
    133          1.1  christos fd_close(int fd)
    134          1.1  christos {
    135          1.1  christos #ifndef USE_WINSOCK
    136          1.1  christos 	close(fd);
    137          1.1  christos #else
    138          1.1  christos 	closesocket(fd);
    139          1.1  christos #endif
    140          1.1  christos }
    141          1.1  christos 
    142          1.1  christos /**
    143          1.1  christos  * Read one line from SSL
    144          1.1  christos  * zero terminates.
    145          1.1  christos  * skips "\r\n" (but not copied to buf).
    146          1.1  christos  * @param ssl: the SSL connection to read from (blocking).
    147          1.1  christos  * @param buf: buffer to return line in.
    148          1.1  christos  * @param len: size of the buffer.
    149          1.1  christos  * @return 0 on error, 1 on success.
    150          1.1  christos  */
    151          1.1  christos static int
    152          1.1  christos read_ssl_line(SSL* ssl, char* buf, size_t len)
    153          1.1  christos {
    154          1.1  christos 	size_t n = 0;
    155          1.1  christos 	int r;
    156          1.1  christos 	int endnl = 0;
    157          1.1  christos 	while(1) {
    158          1.1  christos 		if(n >= len) {
    159          1.1  christos 			if(verb) printf("line too long\n");
    160          1.1  christos 			return 0;
    161          1.1  christos 		}
    162          1.1  christos 		if((r = SSL_read(ssl, buf+n, 1)) <= 0) {
    163          1.1  christos 			if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
    164          1.1  christos 				/* EOF */
    165          1.1  christos 				break;
    166          1.1  christos 			}
    167          1.1  christos 			if(verb) printf("could not SSL_read\n");
    168          1.1  christos 			return 0;
    169          1.1  christos 		}
    170          1.1  christos 		if(endnl && buf[n] == '\n') {
    171          1.1  christos 			break;
    172          1.1  christos 		} else if(endnl) {
    173          1.1  christos 			/* bad data */
    174          1.1  christos 			if(verb) printf("error: stray linefeeds\n");
    175          1.1  christos 			return 0;
    176          1.1  christos 		} else if(buf[n] == '\r') {
    177          1.1  christos 			/* skip \r, and also \n on the wire */
    178          1.1  christos 			endnl = 1;
    179          1.1  christos 			continue;
    180          1.1  christos 		} else if(buf[n] == '\n') {
    181          1.1  christos 			/* skip the \n, we are done */
    182          1.1  christos 			break;
    183          1.1  christos 		} else n++;
    184          1.1  christos 	}
    185          1.1  christos 	buf[n] = 0;
    186          1.1  christos 	return 1;
    187          1.1  christos }
    188          1.1  christos 
    189          1.1  christos /** process one http header */
    190          1.1  christos static int
    191          1.1  christos process_one_header(char* buf, char* file, size_t flen, char* host, size_t hlen,
    192          1.1  christos 	int* vs)
    193          1.1  christos {
    194          1.1  christos 	if(strncasecmp(buf, "GET ", 4) == 0) {
    195          1.1  christos 		char* e = strstr(buf, " HTTP/1.1");
    196          1.1  christos 		if(!e) e = strstr(buf, " http/1.1");
    197          1.1  christos 		if(!e) {
    198          1.1  christos 			e = strstr(buf, " HTTP/1.0");
    199          1.1  christos 			if(!e) e = strstr(buf, " http/1.0");
    200          1.1  christos 			if(!e) e = strrchr(buf, ' ');
    201          1.1  christos 			if(!e) e = strrchr(buf, '\t');
    202          1.1  christos 			if(e) *vs = 10;
    203          1.1  christos 		}
    204          1.1  christos 		if(e) *e = 0;
    205          1.1  christos 		if(strlen(buf) < 4) return 0;
    206          1.1  christos 		(void)strlcpy(file, buf+4, flen);
    207          1.1  christos 	} else if(strncasecmp(buf, "Host: ", 6) == 0) {
    208          1.1  christos 		(void)strlcpy(host, buf+6, hlen);
    209          1.1  christos 	}
    210          1.1  christos 	return 1;
    211          1.1  christos }
    212          1.1  christos 
    213          1.1  christos /** read http headers and process them */
    214          1.1  christos static int
    215          1.1  christos read_http_headers(SSL* ssl, char* file, size_t flen, char* host, size_t hlen,
    216          1.1  christos 	int* vs)
    217          1.1  christos {
    218          1.1  christos 	char buf[1024];
    219          1.1  christos 	file[0] = 0;
    220          1.1  christos 	host[0] = 0;
    221          1.1  christos 	while(read_ssl_line(ssl, buf, sizeof(buf))) {
    222          1.1  christos 		if(verb>=2) printf("read: %s\n", buf);
    223      1.1.1.6  christos 		if(buf[0] == 0) {
    224      1.1.1.6  christos 			int e = ERR_peek_error();
    225      1.1.1.6  christos 			printf("error string: %s\n", ERR_reason_error_string(e));
    226          1.1  christos 			return 1;
    227      1.1.1.6  christos 		}
    228          1.1  christos 		if(!process_one_header(buf, file, flen, host, hlen, vs))
    229          1.1  christos 			return 0;
    230          1.1  christos 	}
    231          1.1  christos 	return 0;
    232          1.1  christos }
    233          1.1  christos 
    234          1.1  christos /** setup SSL context */
    235          1.1  christos static SSL_CTX*
    236          1.1  christos setup_ctx(char* key, char* cert)
    237          1.1  christos {
    238          1.1  christos 	SSL_CTX* ctx = SSL_CTX_new(SSLv23_server_method());
    239          1.1  christos 	if(!ctx) print_exit("out of memory");
    240      1.1.1.5  christos #if SSL_OP_NO_SSLv2 != 0
    241          1.1  christos 	(void)SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
    242      1.1.1.5  christos #endif
    243          1.1  christos 	(void)SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3);
    244      1.1.1.6  christos #ifdef HAVE_SSL_CTX_SET_SECURITY_LEVEL
    245      1.1.1.6  christos 	SSL_CTX_set_security_level(ctx, 0); /* for keys in tests */
    246      1.1.1.6  christos #endif
    247      1.1.1.6  christos 	if(!SSL_CTX_use_certificate_chain_file(ctx, cert)) {
    248      1.1.1.6  christos 		int e = ERR_peek_error();
    249      1.1.1.6  christos 		printf("error string: %s\n", ERR_reason_error_string(e));
    250          1.1  christos 		print_exit("cannot read cert");
    251      1.1.1.6  christos 	}
    252          1.1  christos 	if(!SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM))
    253          1.1  christos 		print_exit("cannot read key");
    254          1.1  christos 	if(!SSL_CTX_check_private_key(ctx))
    255          1.1  christos 		print_exit("private key is not correct");
    256          1.1  christos #if HAVE_DECL_SSL_CTX_SET_ECDH_AUTO
    257          1.1  christos 	if (!SSL_CTX_set_ecdh_auto(ctx,1))
    258          1.1  christos 		if(verb>=1) printf("failed to set_ecdh_auto, not enabling ECDHE\n");
    259  1.1.1.6.6.1    martin #elif defined(USE_ECDSA) && HAVE_DECL_SSL_CTX_SET_TMP_ECDH
    260          1.1  christos 	if(1) {
    261          1.1  christos 		EC_KEY *ecdh = EC_KEY_new_by_curve_name (NID_X9_62_prime256v1);
    262          1.1  christos 		if (!ecdh) {
    263          1.1  christos 			if(verb>=1) printf("could not find p256, not enabling ECDHE\n");
    264          1.1  christos 		} else {
    265          1.1  christos 			if (1 != SSL_CTX_set_tmp_ecdh (ctx, ecdh)) {
    266          1.1  christos 				if(verb>=1) printf("Error in SSL_CTX_set_tmp_ecdh, not enabling ECDHE\n");
    267          1.1  christos 			}
    268          1.1  christos 			EC_KEY_free(ecdh);
    269          1.1  christos 		}
    270          1.1  christos 	}
    271          1.1  christos #endif
    272          1.1  christos 	if(!SSL_CTX_load_verify_locations(ctx, cert, NULL))
    273          1.1  christos 		print_exit("cannot load cert verify locations");
    274          1.1  christos 	return ctx;
    275          1.1  christos }
    276          1.1  christos 
    277          1.1  christos /** setup listening TCP */
    278          1.1  christos static int
    279          1.1  christos setup_fd(char* addr, int port)
    280          1.1  christos {
    281          1.1  christos 	struct sockaddr_storage ad;
    282          1.1  christos 	socklen_t len;
    283          1.1  christos 	int fd;
    284          1.1  christos 	int c = 1;
    285          1.1  christos 	int fam = parse_ip_addr(addr, port, &ad, &len);
    286          1.1  christos 	fd = socket(fam, SOCK_STREAM, 0);
    287          1.1  christos 	if(fd == -1) {
    288          1.1  christos 		log_errno("socket");
    289          1.1  christos 		return -1;
    290          1.1  christos 	}
    291          1.1  christos 	if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
    292          1.1  christos 		(void*)&c, (socklen_t) sizeof(int)) < 0) {
    293          1.1  christos 		log_errno("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
    294          1.1  christos 	}
    295          1.1  christos 	if(bind(fd, (struct sockaddr*)&ad, len) == -1) {
    296          1.1  christos 		log_errno("bind");
    297          1.1  christos 		fd_close(fd);
    298          1.1  christos 		return -1;
    299          1.1  christos 	}
    300          1.1  christos 	if(listen(fd, 5) == -1) {
    301          1.1  christos 		log_errno("listen");
    302          1.1  christos 		fd_close(fd);
    303          1.1  christos 		return -1;
    304          1.1  christos 	}
    305          1.1  christos 	return fd;
    306          1.1  christos }
    307          1.1  christos 
    308          1.1  christos /** setup SSL connection to the client */
    309          1.1  christos static SSL*
    310          1.1  christos setup_ssl(int s, SSL_CTX* ctx)
    311          1.1  christos {
    312          1.1  christos 	SSL* ssl = SSL_new(ctx);
    313          1.1  christos 	if(!ssl) return NULL;
    314          1.1  christos 	SSL_set_accept_state(ssl);
    315      1.1.1.4  christos 	(void)SSL_set_mode(ssl, (long)SSL_MODE_AUTO_RETRY);
    316          1.1  christos 	if(!SSL_set_fd(ssl, s)) {
    317          1.1  christos 		SSL_free(ssl);
    318          1.1  christos 		return NULL;
    319          1.1  christos 	}
    320          1.1  christos 	return ssl;
    321          1.1  christos }
    322          1.1  christos 
    323          1.1  christos /** check a file name for safety */
    324          1.1  christos static int
    325          1.1  christos file_name_is_safe(char* s)
    326          1.1  christos {
    327          1.1  christos 	size_t l = strlen(s);
    328          1.1  christos 	if(s[0] != '/')
    329          1.1  christos 		return 0; /* must start with / */
    330          1.1  christos 	if(strstr(s, "/../"))
    331          1.1  christos 		return 0; /* no updirs in URL */
    332          1.1  christos 	if(l>=3 && s[l-1]=='.' && s[l-2]=='.' && s[l-3]=='/')
    333          1.1  christos 		return 0; /* ends with /.. */
    334          1.1  christos 	return 1;
    335          1.1  christos }
    336          1.1  christos 
    337      1.1.1.3  christos /** adjust host */
    338          1.1  christos static void
    339      1.1.1.3  christos adjust_host(char* host)
    340          1.1  christos {
    341          1.1  christos 	size_t i, len;
    342          1.1  christos 	/* remove a port number if present */
    343          1.1  christos 	if(strrchr(host, ':'))
    344          1.1  christos 		*strrchr(host, ':') = 0;
    345          1.1  christos 	/* lowercase */
    346          1.1  christos 	len = strlen(host);
    347          1.1  christos 	for(i=0; i<len; i++)
    348          1.1  christos 		host[i] = tolower((unsigned char)host[i]);
    349      1.1.1.3  christos }
    350      1.1.1.3  christos 
    351      1.1.1.3  christos /** adjust filename */
    352      1.1.1.3  christos static void
    353      1.1.1.3  christos adjust_file(char* file)
    354      1.1.1.3  christos {
    355      1.1.1.3  christos 	size_t i, len;
    356          1.1  christos 	len = strlen(file);
    357          1.1  christos 	for(i=0; i<len; i++)
    358          1.1  christos 		file[i] = tolower((unsigned char)file[i]);
    359          1.1  christos }
    360          1.1  christos 
    361          1.1  christos /** check a host name for safety */
    362          1.1  christos static int
    363          1.1  christos host_name_is_safe(char* s)
    364          1.1  christos {
    365          1.1  christos 	if(strchr(s, '/'))
    366          1.1  christos 		return 0;
    367          1.1  christos 	if(strcmp(s, "..") == 0)
    368          1.1  christos 		return 0;
    369          1.1  christos 	if(strcmp(s, ".") == 0)
    370          1.1  christos 		return 0;
    371          1.1  christos 	return 1;
    372          1.1  christos }
    373          1.1  christos 
    374          1.1  christos /** provide file in whole transfer */
    375          1.1  christos static void
    376          1.1  christos provide_file_10(SSL* ssl, char* fname)
    377          1.1  christos {
    378          1.1  christos 	char* buf, *at;
    379          1.1  christos 	size_t len, avail, header_reserve=1024;
    380          1.1  christos 	FILE* in = fopen(fname,
    381          1.1  christos #ifndef USE_WINSOCK
    382          1.1  christos 		"r"
    383          1.1  christos #else
    384          1.1  christos 		"rb"
    385          1.1  christos #endif
    386          1.1  christos 		);
    387          1.1  christos 	size_t r;
    388          1.1  christos 	const char* rcode = "200 OK";
    389          1.1  christos 	if(!in) {
    390          1.1  christos 		char hdr[1024];
    391          1.1  christos 		rcode = "404 File not found";
    392          1.1  christos 		snprintf(hdr, sizeof(hdr), "HTTP/1.1 %s\r\n\r\n", rcode);
    393          1.1  christos 		r = strlen(hdr);
    394          1.1  christos 		if(SSL_write(ssl, hdr, (int)r) <= 0) {
    395          1.1  christos 			/* write failure */
    396          1.1  christos 		}
    397          1.1  christos 		return;
    398          1.1  christos 	}
    399          1.1  christos 	fseek(in, 0, SEEK_END);
    400          1.1  christos 	len = (size_t)ftell(in);
    401          1.1  christos 	fseek(in, 0, SEEK_SET);
    402          1.1  christos 	/* plus some space for the header */
    403          1.1  christos 	buf = (char*)malloc(len+header_reserve);
    404          1.1  christos 	if(!buf) {
    405          1.1  christos 		fclose(in);
    406          1.1  christos 		return;
    407          1.1  christos 	}
    408          1.1  christos 	avail = len+header_reserve;
    409          1.1  christos 	at = buf;
    410          1.1  christos 	snprintf(at, avail, "HTTP/1.1 %s\r\n", rcode);
    411          1.1  christos 	r = strlen(at);
    412          1.1  christos 	at += r;
    413          1.1  christos 	avail -= r;
    414          1.1  christos 	snprintf(at, avail, "Server: petal/%s\r\n", PACKAGE_VERSION);
    415          1.1  christos 	r = strlen(at);
    416          1.1  christos 	at += r;
    417          1.1  christos 	avail -= r;
    418          1.1  christos 	snprintf(at, avail, "Content-Length: %u\r\n", (unsigned)len);
    419          1.1  christos 	r = strlen(at);
    420          1.1  christos 	at += r;
    421          1.1  christos 	avail -= r;
    422          1.1  christos 	snprintf(at, avail, "\r\n");
    423          1.1  christos 	r = strlen(at);
    424          1.1  christos 	at += r;
    425          1.1  christos 	avail -= r;
    426          1.1  christos 	if(avail < len) { /* robust */
    427          1.1  christos 		free(buf);
    428          1.1  christos 		fclose(in);
    429          1.1  christos 		return;
    430          1.1  christos 	}
    431          1.1  christos 	if(fread(at, 1, len, in) != len) {
    432          1.1  christos 		free(buf);
    433          1.1  christos 		fclose(in);
    434          1.1  christos 		return;
    435          1.1  christos 	}
    436          1.1  christos 	fclose(in);
    437          1.1  christos 	at += len;
    438      1.1.1.3  christos 	/* avail -= len; unused */
    439          1.1  christos 	if(SSL_write(ssl, buf, at-buf) <= 0) {
    440          1.1  christos 		/* write failure */
    441          1.1  christos 	}
    442          1.1  christos 	free(buf);
    443          1.1  christos }
    444          1.1  christos 
    445          1.1  christos /** provide file over SSL, chunked encoding */
    446          1.1  christos static void
    447          1.1  christos provide_file_chunked(SSL* ssl, char* fname)
    448          1.1  christos {
    449          1.1  christos 	char buf[16384];
    450      1.1.1.2  christos 	char* tmpbuf = NULL;
    451          1.1  christos 	char* at = buf;
    452          1.1  christos 	size_t avail = sizeof(buf);
    453          1.1  christos 	size_t r;
    454          1.1  christos 	FILE* in = fopen(fname,
    455          1.1  christos #ifndef USE_WINSOCK
    456          1.1  christos 		"r"
    457          1.1  christos #else
    458          1.1  christos 		"rb"
    459          1.1  christos #endif
    460          1.1  christos 		);
    461          1.1  christos 	const char* rcode = "200 OK";
    462          1.1  christos 	if(!in) {
    463          1.1  christos 		rcode = "404 File not found";
    464          1.1  christos 	}
    465          1.1  christos 
    466          1.1  christos 	/* print headers */
    467          1.1  christos 	snprintf(at, avail, "HTTP/1.1 %s\r\n", rcode);
    468          1.1  christos 	r = strlen(at);
    469          1.1  christos 	at += r;
    470          1.1  christos 	avail -= r;
    471          1.1  christos 	snprintf(at, avail, "Server: petal/%s\r\n", PACKAGE_VERSION);
    472          1.1  christos 	r = strlen(at);
    473          1.1  christos 	at += r;
    474          1.1  christos 	avail -= r;
    475          1.1  christos 	snprintf(at, avail, "Transfer-Encoding: chunked\r\n");
    476          1.1  christos 	r = strlen(at);
    477          1.1  christos 	at += r;
    478          1.1  christos 	avail -= r;
    479          1.1  christos 	snprintf(at, avail, "Connection: close\r\n");
    480          1.1  christos 	r = strlen(at);
    481          1.1  christos 	at += r;
    482          1.1  christos 	avail -= r;
    483          1.1  christos 	snprintf(at, avail, "\r\n");
    484          1.1  christos 	r = strlen(at);
    485          1.1  christos 	at += r;
    486          1.1  christos 	avail -= r;
    487          1.1  christos 	if(avail < 16) { /* robust */
    488          1.1  christos 		if(in) fclose(in);
    489          1.1  christos 		return;
    490          1.1  christos 	}
    491          1.1  christos 
    492          1.1  christos 	do {
    493      1.1.1.2  christos 		size_t red;
    494      1.1.1.2  christos 		free(tmpbuf);
    495      1.1.1.2  christos 		tmpbuf = malloc(avail-16);
    496      1.1.1.2  christos 		if(!tmpbuf)
    497      1.1.1.2  christos 			break;
    498          1.1  christos 		/* read chunk; space-16 for xxxxCRLF..CRLF0CRLFCRLF (3 spare)*/
    499      1.1.1.2  christos 		red = in?fread(tmpbuf, 1, avail-16, in):0;
    500          1.1  christos 		/* prepare chunk */
    501          1.1  christos 		snprintf(at, avail, "%x\r\n", (unsigned)red);
    502          1.1  christos 		r = strlen(at);
    503          1.1  christos 		if(verb >= 3)
    504          1.1  christos 		{printf("chunk len %x\n", (unsigned)red); fflush(stdout);}
    505          1.1  christos 		at += r;
    506          1.1  christos 		avail -= r;
    507          1.1  christos 		if(red != 0) {
    508          1.1  christos 			if(red > avail) break; /* robust */
    509          1.1  christos 			memmove(at, tmpbuf, red);
    510          1.1  christos 			at += red;
    511          1.1  christos 			avail -= red;
    512          1.1  christos 			snprintf(at, avail, "\r\n");
    513          1.1  christos 			r = strlen(at);
    514          1.1  christos 			at += r;
    515          1.1  christos 			avail -= r;
    516          1.1  christos 		}
    517          1.1  christos 		if(in && feof(in) && red != 0) {
    518          1.1  christos 			snprintf(at, avail, "0\r\n");
    519          1.1  christos 			r = strlen(at);
    520          1.1  christos 			at += r;
    521          1.1  christos 			avail -= r;
    522          1.1  christos 		}
    523          1.1  christos 		if(!in || feof(in)) {
    524          1.1  christos 			snprintf(at, avail, "\r\n");
    525          1.1  christos 			r = strlen(at);
    526          1.1  christos 			at += r;
    527      1.1.1.3  christos 			/* avail -= r; unused */
    528          1.1  christos 		}
    529          1.1  christos 		/* send chunk */
    530          1.1  christos 		if(SSL_write(ssl, buf, at-buf) <= 0) {
    531          1.1  christos 			/* SSL error */
    532          1.1  christos 			break;
    533          1.1  christos 		}
    534          1.1  christos 
    535          1.1  christos 		/* setup for next chunk */
    536          1.1  christos 		at = buf;
    537          1.1  christos 		avail = sizeof(buf);
    538          1.1  christos 	} while(in && !feof(in) && !ferror(in));
    539          1.1  christos 
    540      1.1.1.2  christos 	free(tmpbuf);
    541          1.1  christos 	if(in) fclose(in);
    542          1.1  christos }
    543          1.1  christos 
    544          1.1  christos /** provide service to the ssl descriptor */
    545          1.1  christos static void
    546          1.1  christos service_ssl(SSL* ssl, struct sockaddr_storage* from, socklen_t falen)
    547          1.1  christos {
    548          1.1  christos 	char file[1024];
    549          1.1  christos 	char host[1024];
    550          1.1  christos 	char combined[2048];
    551          1.1  christos 	int vs = 11;
    552          1.1  christos 	if(!read_http_headers(ssl, file, sizeof(file), host, sizeof(host),
    553          1.1  christos 		&vs))
    554          1.1  christos 		return;
    555      1.1.1.3  christos 	if(host[0] != 0) adjust_host(host);
    556      1.1.1.3  christos 	if(file[0] != 0) adjust_file(file);
    557          1.1  christos 	if(host[0] == 0 || !host_name_is_safe(host))
    558          1.1  christos 		(void)strlcpy(host, "default", sizeof(host));
    559          1.1  christos 	if(!file_name_is_safe(file)) {
    560          1.1  christos 		return;
    561          1.1  christos 	}
    562          1.1  christos 	snprintf(combined, sizeof(combined), "%s%s", host, file);
    563          1.1  christos 	if(verb) {
    564          1.1  christos 		char out[100];
    565          1.1  christos 		void* a = &((struct sockaddr_in*)from)->sin_addr;
    566          1.1  christos 		if(falen != (socklen_t)sizeof(struct sockaddr_in))
    567          1.1  christos 			a = &((struct sockaddr_in6*)from)->sin6_addr;
    568          1.1  christos 		out[0]=0;
    569          1.1  christos 		(void)inet_ntop((int)((struct sockaddr_in*)from)->sin_family,
    570          1.1  christos 			a, out, (socklen_t)sizeof(out));
    571          1.1  christos 		printf("%s requests %s\n", out, combined);
    572          1.1  christos 		fflush(stdout);
    573          1.1  christos 	}
    574          1.1  christos 	if(vs == 10)
    575          1.1  christos 		provide_file_10(ssl, combined);
    576          1.1  christos 	else	provide_file_chunked(ssl, combined);
    577          1.1  christos }
    578          1.1  christos 
    579          1.1  christos /** provide ssl service */
    580          1.1  christos static void
    581          1.1  christos do_service(char* addr, int port, char* key, char* cert)
    582          1.1  christos {
    583          1.1  christos 	SSL_CTX* sslctx = setup_ctx(key, cert);
    584          1.1  christos 	int fd = setup_fd(addr, port);
    585          1.1  christos 	if(fd == -1) print_exit("could not setup sockets");
    586          1.1  christos 	if(verb) {printf("petal start\n"); fflush(stdout);}
    587      1.1.1.6  christos 	while(1) {
    588          1.1  christos 		struct sockaddr_storage from;
    589          1.1  christos 		socklen_t flen = (socklen_t)sizeof(from);
    590      1.1.1.3  christos 		int s;
    591      1.1.1.3  christos 		memset(&from, 0, sizeof(from));
    592      1.1.1.3  christos 		s = accept(fd, (struct sockaddr*)&from, &flen);
    593          1.1  christos 		if(verb) fflush(stdout);
    594          1.1  christos 		if(s != -1) {
    595          1.1  christos 			SSL* ssl = setup_ssl(s, sslctx);
    596          1.1  christos 			if(verb) fflush(stdout);
    597          1.1  christos 			if(ssl) {
    598          1.1  christos 				service_ssl(ssl, &from, flen);
    599          1.1  christos 				if(verb) fflush(stdout);
    600          1.1  christos 				SSL_shutdown(ssl);
    601          1.1  christos 				SSL_free(ssl);
    602          1.1  christos 			}
    603          1.1  christos 			fd_close(s);
    604          1.1  christos 		} else if (verb >=2) log_errno("accept");
    605          1.1  christos 		if(verb) fflush(stdout);
    606          1.1  christos 	}
    607          1.1  christos 	/* if we get a kill signal, the process dies and the OS reaps us */
    608          1.1  christos 	if(verb) printf("petal end\n");
    609          1.1  christos 	fd_close(fd);
    610          1.1  christos 	SSL_CTX_free(sslctx);
    611          1.1  christos }
    612          1.1  christos 
    613          1.1  christos /** getopt global, in case header files fail to declare it. */
    614          1.1  christos extern int optind;
    615          1.1  christos /** getopt global, in case header files fail to declare it. */
    616          1.1  christos extern char* optarg;
    617          1.1  christos 
    618          1.1  christos /** Main routine for petal */
    619          1.1  christos int main(int argc, char* argv[])
    620          1.1  christos {
    621          1.1  christos 	int c;
    622          1.1  christos 	int port = 443;
    623          1.1  christos 	char* addr = "127.0.0.1", *key = "petal.key", *cert = "petal.pem";
    624          1.1  christos #ifdef USE_WINSOCK
    625          1.1  christos 	WSADATA wsa_data;
    626          1.1  christos 	if((c=WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0)
    627          1.1  christos 	{	printf("WSAStartup failed\n"); exit(1); }
    628          1.1  christos 	atexit((void (*)(void))WSACleanup);
    629          1.1  christos #endif
    630          1.1  christos 
    631          1.1  christos 	/* parse the options */
    632          1.1  christos 	while( (c=getopt(argc, argv, "a:c:k:hp:v")) != -1) {
    633          1.1  christos 		switch(c) {
    634          1.1  christos 		case 'a':
    635          1.1  christos 			addr = optarg;
    636          1.1  christos 			break;
    637          1.1  christos 		case 'c':
    638          1.1  christos 			cert = optarg;
    639          1.1  christos 			break;
    640          1.1  christos 		case 'k':
    641          1.1  christos 			key = optarg;
    642          1.1  christos 			break;
    643          1.1  christos 		case 'p':
    644          1.1  christos 			port = atoi(optarg);
    645          1.1  christos 			break;
    646          1.1  christos 		case 'v':
    647          1.1  christos 			verb++;
    648          1.1  christos 			break;
    649          1.1  christos 		case '?':
    650          1.1  christos 		case 'h':
    651          1.1  christos 		default:
    652          1.1  christos 			usage();
    653          1.1  christos 		}
    654          1.1  christos 	}
    655          1.1  christos 	argc -= optind;
    656      1.1.1.3  christos 	/* argv += optind; not using further arguments */
    657          1.1  christos 	if(argc != 0)
    658          1.1  christos 		usage();
    659          1.1  christos 
    660          1.1  christos #ifdef SIGPIPE
    661          1.1  christos 	(void)signal(SIGPIPE, SIG_IGN);
    662          1.1  christos #endif
    663      1.1.1.2  christos #ifdef HAVE_ERR_LOAD_CRYPTO_STRINGS
    664          1.1  christos 	ERR_load_crypto_strings();
    665      1.1.1.2  christos #endif
    666      1.1.1.2  christos #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
    667          1.1  christos 	ERR_load_SSL_strings();
    668      1.1.1.2  christos #endif
    669      1.1.1.2  christos #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_CRYPTO)
    670      1.1.1.4  christos #  ifndef S_SPLINT_S
    671          1.1  christos 	OpenSSL_add_all_algorithms();
    672      1.1.1.4  christos #  endif
    673      1.1.1.2  christos #else
    674      1.1.1.2  christos 	OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
    675      1.1.1.2  christos 		| OPENSSL_INIT_ADD_ALL_DIGESTS
    676      1.1.1.2  christos 		| OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
    677      1.1.1.2  christos #endif
    678      1.1.1.2  christos #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
    679          1.1  christos 	(void)SSL_library_init();
    680      1.1.1.2  christos #else
    681      1.1.1.2  christos 	(void)OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
    682      1.1.1.2  christos #endif
    683          1.1  christos 
    684          1.1  christos 	do_service(addr, port, key, cert);
    685          1.1  christos 
    686      1.1.1.2  christos #ifdef HAVE_CRYPTO_CLEANUP_ALL_EX_DATA
    687          1.1  christos 	CRYPTO_cleanup_all_ex_data();
    688      1.1.1.2  christos #endif
    689      1.1.1.2  christos #ifdef HAVE_ERR_FREE_STRINGS
    690          1.1  christos 	ERR_free_strings();
    691      1.1.1.2  christos #endif
    692          1.1  christos 	return 0;
    693          1.1  christos }
    694