Home | History | Annotate | Line # | Download | only in testcode
petal.c revision 1.1.1.5
      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  christos 		if(buf[0] == 0)
    224      1.1  christos 			return 1;
    225      1.1  christos 		if(!process_one_header(buf, file, flen, host, hlen, vs))
    226      1.1  christos 			return 0;
    227      1.1  christos 	}
    228      1.1  christos 	return 0;
    229      1.1  christos }
    230      1.1  christos 
    231      1.1  christos /** setup SSL context */
    232      1.1  christos static SSL_CTX*
    233      1.1  christos setup_ctx(char* key, char* cert)
    234      1.1  christos {
    235      1.1  christos 	SSL_CTX* ctx = SSL_CTX_new(SSLv23_server_method());
    236      1.1  christos 	if(!ctx) print_exit("out of memory");
    237  1.1.1.5  christos #if SSL_OP_NO_SSLv2 != 0
    238      1.1  christos 	(void)SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
    239  1.1.1.5  christos #endif
    240      1.1  christos 	(void)SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3);
    241      1.1  christos 	if(!SSL_CTX_use_certificate_chain_file(ctx, cert))
    242      1.1  christos 		print_exit("cannot read cert");
    243      1.1  christos 	if(!SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM))
    244      1.1  christos 		print_exit("cannot read key");
    245      1.1  christos 	if(!SSL_CTX_check_private_key(ctx))
    246      1.1  christos 		print_exit("private key is not correct");
    247      1.1  christos #if HAVE_DECL_SSL_CTX_SET_ECDH_AUTO
    248      1.1  christos 	if (!SSL_CTX_set_ecdh_auto(ctx,1))
    249      1.1  christos 		if(verb>=1) printf("failed to set_ecdh_auto, not enabling ECDHE\n");
    250      1.1  christos #elif defined(USE_ECDSA)
    251      1.1  christos 	if(1) {
    252      1.1  christos 		EC_KEY *ecdh = EC_KEY_new_by_curve_name (NID_X9_62_prime256v1);
    253      1.1  christos 		if (!ecdh) {
    254      1.1  christos 			if(verb>=1) printf("could not find p256, not enabling ECDHE\n");
    255      1.1  christos 		} else {
    256      1.1  christos 			if (1 != SSL_CTX_set_tmp_ecdh (ctx, ecdh)) {
    257      1.1  christos 				if(verb>=1) printf("Error in SSL_CTX_set_tmp_ecdh, not enabling ECDHE\n");
    258      1.1  christos 			}
    259      1.1  christos 			EC_KEY_free(ecdh);
    260      1.1  christos 		}
    261      1.1  christos 	}
    262      1.1  christos #endif
    263      1.1  christos 	if(!SSL_CTX_load_verify_locations(ctx, cert, NULL))
    264      1.1  christos 		print_exit("cannot load cert verify locations");
    265      1.1  christos 	return ctx;
    266      1.1  christos }
    267      1.1  christos 
    268      1.1  christos /** setup listening TCP */
    269      1.1  christos static int
    270      1.1  christos setup_fd(char* addr, int port)
    271      1.1  christos {
    272      1.1  christos 	struct sockaddr_storage ad;
    273      1.1  christos 	socklen_t len;
    274      1.1  christos 	int fd;
    275      1.1  christos 	int c = 1;
    276      1.1  christos 	int fam = parse_ip_addr(addr, port, &ad, &len);
    277      1.1  christos 	fd = socket(fam, SOCK_STREAM, 0);
    278      1.1  christos 	if(fd == -1) {
    279      1.1  christos 		log_errno("socket");
    280      1.1  christos 		return -1;
    281      1.1  christos 	}
    282      1.1  christos 	if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
    283      1.1  christos 		(void*)&c, (socklen_t) sizeof(int)) < 0) {
    284      1.1  christos 		log_errno("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
    285      1.1  christos 	}
    286      1.1  christos 	if(bind(fd, (struct sockaddr*)&ad, len) == -1) {
    287      1.1  christos 		log_errno("bind");
    288      1.1  christos 		fd_close(fd);
    289      1.1  christos 		return -1;
    290      1.1  christos 	}
    291      1.1  christos 	if(listen(fd, 5) == -1) {
    292      1.1  christos 		log_errno("listen");
    293      1.1  christos 		fd_close(fd);
    294      1.1  christos 		return -1;
    295      1.1  christos 	}
    296      1.1  christos 	return fd;
    297      1.1  christos }
    298      1.1  christos 
    299      1.1  christos /** setup SSL connection to the client */
    300      1.1  christos static SSL*
    301      1.1  christos setup_ssl(int s, SSL_CTX* ctx)
    302      1.1  christos {
    303      1.1  christos 	SSL* ssl = SSL_new(ctx);
    304      1.1  christos 	if(!ssl) return NULL;
    305      1.1  christos 	SSL_set_accept_state(ssl);
    306  1.1.1.4  christos 	(void)SSL_set_mode(ssl, (long)SSL_MODE_AUTO_RETRY);
    307      1.1  christos 	if(!SSL_set_fd(ssl, s)) {
    308      1.1  christos 		SSL_free(ssl);
    309      1.1  christos 		return NULL;
    310      1.1  christos 	}
    311      1.1  christos 	return ssl;
    312      1.1  christos }
    313      1.1  christos 
    314      1.1  christos /** check a file name for safety */
    315      1.1  christos static int
    316      1.1  christos file_name_is_safe(char* s)
    317      1.1  christos {
    318      1.1  christos 	size_t l = strlen(s);
    319      1.1  christos 	if(s[0] != '/')
    320      1.1  christos 		return 0; /* must start with / */
    321      1.1  christos 	if(strstr(s, "/../"))
    322      1.1  christos 		return 0; /* no updirs in URL */
    323      1.1  christos 	if(l>=3 && s[l-1]=='.' && s[l-2]=='.' && s[l-3]=='/')
    324      1.1  christos 		return 0; /* ends with /.. */
    325      1.1  christos 	return 1;
    326      1.1  christos }
    327      1.1  christos 
    328  1.1.1.3  christos /** adjust host */
    329      1.1  christos static void
    330  1.1.1.3  christos adjust_host(char* host)
    331      1.1  christos {
    332      1.1  christos 	size_t i, len;
    333      1.1  christos 	/* remove a port number if present */
    334      1.1  christos 	if(strrchr(host, ':'))
    335      1.1  christos 		*strrchr(host, ':') = 0;
    336      1.1  christos 	/* lowercase */
    337      1.1  christos 	len = strlen(host);
    338      1.1  christos 	for(i=0; i<len; i++)
    339      1.1  christos 		host[i] = tolower((unsigned char)host[i]);
    340  1.1.1.3  christos }
    341  1.1.1.3  christos 
    342  1.1.1.3  christos /** adjust filename */
    343  1.1.1.3  christos static void
    344  1.1.1.3  christos adjust_file(char* file)
    345  1.1.1.3  christos {
    346  1.1.1.3  christos 	size_t i, len;
    347      1.1  christos 	len = strlen(file);
    348      1.1  christos 	for(i=0; i<len; i++)
    349      1.1  christos 		file[i] = tolower((unsigned char)file[i]);
    350      1.1  christos }
    351      1.1  christos 
    352      1.1  christos /** check a host name for safety */
    353      1.1  christos static int
    354      1.1  christos host_name_is_safe(char* s)
    355      1.1  christos {
    356      1.1  christos 	if(strchr(s, '/'))
    357      1.1  christos 		return 0;
    358      1.1  christos 	if(strcmp(s, "..") == 0)
    359      1.1  christos 		return 0;
    360      1.1  christos 	if(strcmp(s, ".") == 0)
    361      1.1  christos 		return 0;
    362      1.1  christos 	return 1;
    363      1.1  christos }
    364      1.1  christos 
    365      1.1  christos /** provide file in whole transfer */
    366      1.1  christos static void
    367      1.1  christos provide_file_10(SSL* ssl, char* fname)
    368      1.1  christos {
    369      1.1  christos 	char* buf, *at;
    370      1.1  christos 	size_t len, avail, header_reserve=1024;
    371      1.1  christos 	FILE* in = fopen(fname,
    372      1.1  christos #ifndef USE_WINSOCK
    373      1.1  christos 		"r"
    374      1.1  christos #else
    375      1.1  christos 		"rb"
    376      1.1  christos #endif
    377      1.1  christos 		);
    378      1.1  christos 	size_t r;
    379      1.1  christos 	const char* rcode = "200 OK";
    380      1.1  christos 	if(!in) {
    381      1.1  christos 		char hdr[1024];
    382      1.1  christos 		rcode = "404 File not found";
    383      1.1  christos 		snprintf(hdr, sizeof(hdr), "HTTP/1.1 %s\r\n\r\n", rcode);
    384      1.1  christos 		r = strlen(hdr);
    385      1.1  christos 		if(SSL_write(ssl, hdr, (int)r) <= 0) {
    386      1.1  christos 			/* write failure */
    387      1.1  christos 		}
    388      1.1  christos 		return;
    389      1.1  christos 	}
    390      1.1  christos 	fseek(in, 0, SEEK_END);
    391      1.1  christos 	len = (size_t)ftell(in);
    392      1.1  christos 	fseek(in, 0, SEEK_SET);
    393      1.1  christos 	/* plus some space for the header */
    394      1.1  christos 	buf = (char*)malloc(len+header_reserve);
    395      1.1  christos 	if(!buf) {
    396      1.1  christos 		fclose(in);
    397      1.1  christos 		return;
    398      1.1  christos 	}
    399      1.1  christos 	avail = len+header_reserve;
    400      1.1  christos 	at = buf;
    401      1.1  christos 	snprintf(at, avail, "HTTP/1.1 %s\r\n", rcode);
    402      1.1  christos 	r = strlen(at);
    403      1.1  christos 	at += r;
    404      1.1  christos 	avail -= r;
    405      1.1  christos 	snprintf(at, avail, "Server: petal/%s\r\n", PACKAGE_VERSION);
    406      1.1  christos 	r = strlen(at);
    407      1.1  christos 	at += r;
    408      1.1  christos 	avail -= r;
    409      1.1  christos 	snprintf(at, avail, "Content-Length: %u\r\n", (unsigned)len);
    410      1.1  christos 	r = strlen(at);
    411      1.1  christos 	at += r;
    412      1.1  christos 	avail -= r;
    413      1.1  christos 	snprintf(at, avail, "\r\n");
    414      1.1  christos 	r = strlen(at);
    415      1.1  christos 	at += r;
    416      1.1  christos 	avail -= r;
    417      1.1  christos 	if(avail < len) { /* robust */
    418      1.1  christos 		free(buf);
    419      1.1  christos 		fclose(in);
    420      1.1  christos 		return;
    421      1.1  christos 	}
    422      1.1  christos 	if(fread(at, 1, len, in) != len) {
    423      1.1  christos 		free(buf);
    424      1.1  christos 		fclose(in);
    425      1.1  christos 		return;
    426      1.1  christos 	}
    427      1.1  christos 	fclose(in);
    428      1.1  christos 	at += len;
    429  1.1.1.3  christos 	/* avail -= len; unused */
    430      1.1  christos 	if(SSL_write(ssl, buf, at-buf) <= 0) {
    431      1.1  christos 		/* write failure */
    432      1.1  christos 	}
    433      1.1  christos 	free(buf);
    434      1.1  christos }
    435      1.1  christos 
    436      1.1  christos /** provide file over SSL, chunked encoding */
    437      1.1  christos static void
    438      1.1  christos provide_file_chunked(SSL* ssl, char* fname)
    439      1.1  christos {
    440      1.1  christos 	char buf[16384];
    441  1.1.1.2  christos 	char* tmpbuf = NULL;
    442      1.1  christos 	char* at = buf;
    443      1.1  christos 	size_t avail = sizeof(buf);
    444      1.1  christos 	size_t r;
    445      1.1  christos 	FILE* in = fopen(fname,
    446      1.1  christos #ifndef USE_WINSOCK
    447      1.1  christos 		"r"
    448      1.1  christos #else
    449      1.1  christos 		"rb"
    450      1.1  christos #endif
    451      1.1  christos 		);
    452      1.1  christos 	const char* rcode = "200 OK";
    453      1.1  christos 	if(!in) {
    454      1.1  christos 		rcode = "404 File not found";
    455      1.1  christos 	}
    456      1.1  christos 
    457      1.1  christos 	/* print headers */
    458      1.1  christos 	snprintf(at, avail, "HTTP/1.1 %s\r\n", rcode);
    459      1.1  christos 	r = strlen(at);
    460      1.1  christos 	at += r;
    461      1.1  christos 	avail -= r;
    462      1.1  christos 	snprintf(at, avail, "Server: petal/%s\r\n", PACKAGE_VERSION);
    463      1.1  christos 	r = strlen(at);
    464      1.1  christos 	at += r;
    465      1.1  christos 	avail -= r;
    466      1.1  christos 	snprintf(at, avail, "Transfer-Encoding: chunked\r\n");
    467      1.1  christos 	r = strlen(at);
    468      1.1  christos 	at += r;
    469      1.1  christos 	avail -= r;
    470      1.1  christos 	snprintf(at, avail, "Connection: close\r\n");
    471      1.1  christos 	r = strlen(at);
    472      1.1  christos 	at += r;
    473      1.1  christos 	avail -= r;
    474      1.1  christos 	snprintf(at, avail, "\r\n");
    475      1.1  christos 	r = strlen(at);
    476      1.1  christos 	at += r;
    477      1.1  christos 	avail -= r;
    478      1.1  christos 	if(avail < 16) { /* robust */
    479      1.1  christos 		if(in) fclose(in);
    480      1.1  christos 		return;
    481      1.1  christos 	}
    482      1.1  christos 
    483      1.1  christos 	do {
    484  1.1.1.2  christos 		size_t red;
    485  1.1.1.2  christos 		free(tmpbuf);
    486  1.1.1.2  christos 		tmpbuf = malloc(avail-16);
    487  1.1.1.2  christos 		if(!tmpbuf)
    488  1.1.1.2  christos 			break;
    489      1.1  christos 		/* read chunk; space-16 for xxxxCRLF..CRLF0CRLFCRLF (3 spare)*/
    490  1.1.1.2  christos 		red = in?fread(tmpbuf, 1, avail-16, in):0;
    491      1.1  christos 		/* prepare chunk */
    492      1.1  christos 		snprintf(at, avail, "%x\r\n", (unsigned)red);
    493      1.1  christos 		r = strlen(at);
    494      1.1  christos 		if(verb >= 3)
    495      1.1  christos 		{printf("chunk len %x\n", (unsigned)red); fflush(stdout);}
    496      1.1  christos 		at += r;
    497      1.1  christos 		avail -= r;
    498      1.1  christos 		if(red != 0) {
    499      1.1  christos 			if(red > avail) break; /* robust */
    500      1.1  christos 			memmove(at, tmpbuf, red);
    501      1.1  christos 			at += red;
    502      1.1  christos 			avail -= red;
    503      1.1  christos 			snprintf(at, avail, "\r\n");
    504      1.1  christos 			r = strlen(at);
    505      1.1  christos 			at += r;
    506      1.1  christos 			avail -= r;
    507      1.1  christos 		}
    508      1.1  christos 		if(in && feof(in) && red != 0) {
    509      1.1  christos 			snprintf(at, avail, "0\r\n");
    510      1.1  christos 			r = strlen(at);
    511      1.1  christos 			at += r;
    512      1.1  christos 			avail -= r;
    513      1.1  christos 		}
    514      1.1  christos 		if(!in || feof(in)) {
    515      1.1  christos 			snprintf(at, avail, "\r\n");
    516      1.1  christos 			r = strlen(at);
    517      1.1  christos 			at += r;
    518  1.1.1.3  christos 			/* avail -= r; unused */
    519      1.1  christos 		}
    520      1.1  christos 		/* send chunk */
    521      1.1  christos 		if(SSL_write(ssl, buf, at-buf) <= 0) {
    522      1.1  christos 			/* SSL error */
    523      1.1  christos 			break;
    524      1.1  christos 		}
    525      1.1  christos 
    526      1.1  christos 		/* setup for next chunk */
    527      1.1  christos 		at = buf;
    528      1.1  christos 		avail = sizeof(buf);
    529      1.1  christos 	} while(in && !feof(in) && !ferror(in));
    530      1.1  christos 
    531  1.1.1.2  christos 	free(tmpbuf);
    532      1.1  christos 	if(in) fclose(in);
    533      1.1  christos }
    534      1.1  christos 
    535      1.1  christos /** provide service to the ssl descriptor */
    536      1.1  christos static void
    537      1.1  christos service_ssl(SSL* ssl, struct sockaddr_storage* from, socklen_t falen)
    538      1.1  christos {
    539      1.1  christos 	char file[1024];
    540      1.1  christos 	char host[1024];
    541      1.1  christos 	char combined[2048];
    542      1.1  christos 	int vs = 11;
    543      1.1  christos 	if(!read_http_headers(ssl, file, sizeof(file), host, sizeof(host),
    544      1.1  christos 		&vs))
    545      1.1  christos 		return;
    546  1.1.1.3  christos 	if(host[0] != 0) adjust_host(host);
    547  1.1.1.3  christos 	if(file[0] != 0) adjust_file(file);
    548      1.1  christos 	if(host[0] == 0 || !host_name_is_safe(host))
    549      1.1  christos 		(void)strlcpy(host, "default", sizeof(host));
    550      1.1  christos 	if(!file_name_is_safe(file)) {
    551      1.1  christos 		return;
    552      1.1  christos 	}
    553      1.1  christos 	snprintf(combined, sizeof(combined), "%s%s", host, file);
    554      1.1  christos 	if(verb) {
    555      1.1  christos 		char out[100];
    556      1.1  christos 		void* a = &((struct sockaddr_in*)from)->sin_addr;
    557      1.1  christos 		if(falen != (socklen_t)sizeof(struct sockaddr_in))
    558      1.1  christos 			a = &((struct sockaddr_in6*)from)->sin6_addr;
    559      1.1  christos 		out[0]=0;
    560      1.1  christos 		(void)inet_ntop((int)((struct sockaddr_in*)from)->sin_family,
    561      1.1  christos 			a, out, (socklen_t)sizeof(out));
    562      1.1  christos 		printf("%s requests %s\n", out, combined);
    563      1.1  christos 		fflush(stdout);
    564      1.1  christos 	}
    565      1.1  christos 	if(vs == 10)
    566      1.1  christos 		provide_file_10(ssl, combined);
    567      1.1  christos 	else	provide_file_chunked(ssl, combined);
    568      1.1  christos }
    569      1.1  christos 
    570      1.1  christos /** provide ssl service */
    571      1.1  christos static void
    572      1.1  christos do_service(char* addr, int port, char* key, char* cert)
    573      1.1  christos {
    574      1.1  christos 	SSL_CTX* sslctx = setup_ctx(key, cert);
    575      1.1  christos 	int fd = setup_fd(addr, port);
    576      1.1  christos 	int go = 1;
    577      1.1  christos 	if(fd == -1) print_exit("could not setup sockets");
    578      1.1  christos 	if(verb) {printf("petal start\n"); fflush(stdout);}
    579      1.1  christos 	while(go) {
    580      1.1  christos 		struct sockaddr_storage from;
    581      1.1  christos 		socklen_t flen = (socklen_t)sizeof(from);
    582  1.1.1.3  christos 		int s;
    583  1.1.1.3  christos 		memset(&from, 0, sizeof(from));
    584  1.1.1.3  christos 		s = accept(fd, (struct sockaddr*)&from, &flen);
    585      1.1  christos 		if(verb) fflush(stdout);
    586      1.1  christos 		if(s != -1) {
    587      1.1  christos 			SSL* ssl = setup_ssl(s, sslctx);
    588      1.1  christos 			if(verb) fflush(stdout);
    589      1.1  christos 			if(ssl) {
    590      1.1  christos 				service_ssl(ssl, &from, flen);
    591      1.1  christos 				if(verb) fflush(stdout);
    592      1.1  christos 				SSL_shutdown(ssl);
    593      1.1  christos 				SSL_free(ssl);
    594      1.1  christos 			}
    595      1.1  christos 			fd_close(s);
    596      1.1  christos 		} else if (verb >=2) log_errno("accept");
    597      1.1  christos 		if(verb) fflush(stdout);
    598      1.1  christos 	}
    599      1.1  christos 	/* if we get a kill signal, the process dies and the OS reaps us */
    600      1.1  christos 	if(verb) printf("petal end\n");
    601      1.1  christos 	fd_close(fd);
    602      1.1  christos 	SSL_CTX_free(sslctx);
    603      1.1  christos }
    604      1.1  christos 
    605      1.1  christos /** getopt global, in case header files fail to declare it. */
    606      1.1  christos extern int optind;
    607      1.1  christos /** getopt global, in case header files fail to declare it. */
    608      1.1  christos extern char* optarg;
    609      1.1  christos 
    610      1.1  christos /** Main routine for petal */
    611      1.1  christos int main(int argc, char* argv[])
    612      1.1  christos {
    613      1.1  christos 	int c;
    614      1.1  christos 	int port = 443;
    615      1.1  christos 	char* addr = "127.0.0.1", *key = "petal.key", *cert = "petal.pem";
    616      1.1  christos #ifdef USE_WINSOCK
    617      1.1  christos 	WSADATA wsa_data;
    618      1.1  christos 	if((c=WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0)
    619      1.1  christos 	{	printf("WSAStartup failed\n"); exit(1); }
    620      1.1  christos 	atexit((void (*)(void))WSACleanup);
    621      1.1  christos #endif
    622      1.1  christos 
    623      1.1  christos 	/* parse the options */
    624      1.1  christos 	while( (c=getopt(argc, argv, "a:c:k:hp:v")) != -1) {
    625      1.1  christos 		switch(c) {
    626      1.1  christos 		case 'a':
    627      1.1  christos 			addr = optarg;
    628      1.1  christos 			break;
    629      1.1  christos 		case 'c':
    630      1.1  christos 			cert = optarg;
    631      1.1  christos 			break;
    632      1.1  christos 		case 'k':
    633      1.1  christos 			key = optarg;
    634      1.1  christos 			break;
    635      1.1  christos 		case 'p':
    636      1.1  christos 			port = atoi(optarg);
    637      1.1  christos 			break;
    638      1.1  christos 		case 'v':
    639      1.1  christos 			verb++;
    640      1.1  christos 			break;
    641      1.1  christos 		case '?':
    642      1.1  christos 		case 'h':
    643      1.1  christos 		default:
    644      1.1  christos 			usage();
    645      1.1  christos 		}
    646      1.1  christos 	}
    647      1.1  christos 	argc -= optind;
    648  1.1.1.3  christos 	/* argv += optind; not using further arguments */
    649      1.1  christos 	if(argc != 0)
    650      1.1  christos 		usage();
    651      1.1  christos 
    652      1.1  christos #ifdef SIGPIPE
    653      1.1  christos 	(void)signal(SIGPIPE, SIG_IGN);
    654      1.1  christos #endif
    655  1.1.1.2  christos #ifdef HAVE_ERR_LOAD_CRYPTO_STRINGS
    656      1.1  christos 	ERR_load_crypto_strings();
    657  1.1.1.2  christos #endif
    658  1.1.1.2  christos #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
    659      1.1  christos 	ERR_load_SSL_strings();
    660  1.1.1.2  christos #endif
    661  1.1.1.2  christos #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_CRYPTO)
    662  1.1.1.4  christos #  ifndef S_SPLINT_S
    663      1.1  christos 	OpenSSL_add_all_algorithms();
    664  1.1.1.4  christos #  endif
    665  1.1.1.2  christos #else
    666  1.1.1.2  christos 	OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
    667  1.1.1.2  christos 		| OPENSSL_INIT_ADD_ALL_DIGESTS
    668  1.1.1.2  christos 		| OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
    669  1.1.1.2  christos #endif
    670  1.1.1.2  christos #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
    671      1.1  christos 	(void)SSL_library_init();
    672  1.1.1.2  christos #else
    673  1.1.1.2  christos 	(void)OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
    674  1.1.1.2  christos #endif
    675      1.1  christos 
    676      1.1  christos 	do_service(addr, port, key, cert);
    677      1.1  christos 
    678  1.1.1.2  christos #ifdef HAVE_CRYPTO_CLEANUP_ALL_EX_DATA
    679      1.1  christos 	CRYPTO_cleanup_all_ex_data();
    680  1.1.1.2  christos #endif
    681  1.1.1.2  christos #ifdef HAVE_ERR_FREE_STRINGS
    682      1.1  christos 	ERR_free_strings();
    683  1.1.1.2  christos #endif
    684      1.1  christos 	return 0;
    685      1.1  christos }
    686