1 #ifndef _BASE_H_
2 #define _BASE_H_
3 
4 #ifdef HAVE_CONFIG_H
5 # include "config.h"
6 #endif
7 #include "settings.h"
8 
9 #include <sys/types.h>
10 #include <sys/time.h>
11 #include <sys/stat.h>
12 
13 #include <limits.h>
14 
15 #ifdef HAVE_STDINT_H
16 # include <stdint.h>
17 #endif
18 
19 #ifdef HAVE_INTTYPES_H
20 # include <inttypes.h>
21 #endif
22 
23 #ifdef HAVE_LIBMTCP
24 #include <pthread.h>
25 #include <sys/socket.h>
26 #include <mtcp_api.h>
27 #endif
28 
29 #include "buffer.h"
30 #include "array.h"
31 #include "chunk.h"
32 #include "keyvalue.h"
33 #include "fdevent.h"
34 #include "sys-socket.h"
35 #include "splaytree.h"
36 #include "etag.h"
37 
38 
39 #if defined HAVE_LIBSSL && defined HAVE_OPENSSL_SSL_H
40 # define USE_OPENSSL
41 # include <openssl/ssl.h>
42 # if ! defined OPENSSL_NO_TLSEXT && ! defined SSL_CTRL_SET_TLSEXT_HOSTNAME
43 #  define OPENSSL_NO_TLSEXT
44 # endif
45 #endif
46 
47 #ifdef HAVE_FAM_H
48 # include <fam.h>
49 #endif
50 
51 #ifndef O_BINARY
52 # define O_BINARY 0
53 #endif
54 
55 #ifndef O_LARGEFILE
56 # define O_LARGEFILE 0
57 #endif
58 
59 #ifndef SIZE_MAX
60 # ifdef SIZE_T_MAX
61 #  define SIZE_MAX SIZE_T_MAX
62 # else
63 #  define SIZE_MAX ((size_t)~0)
64 # endif
65 #endif
66 
67 #ifndef SSIZE_MAX
68 # define SSIZE_MAX ((size_t)~0 >> 1)
69 #endif
70 
71 #ifdef __APPLE__
72 #include <crt_externs.h>
73 #define environ (* _NSGetEnviron())
74 #else
75 extern char **environ;
76 #endif
77 
78 /* for solaris 2.5 and NetBSD 1.3.x */
79 #ifndef HAVE_SOCKLEN_T
80 typedef int socklen_t;
81 #endif
82 
83 /* solaris and NetBSD 1.3.x again */
84 #if (!defined(HAVE_STDINT_H)) && (!defined(HAVE_INTTYPES_H)) && (!defined(uint32_t))
85 # define uint32_t u_int32_t
86 #endif
87 
88 
89 #ifndef SHUT_WR
90 # define SHUT_WR 1
91 #endif
92 
93 typedef enum { T_CONFIG_UNSET,
94 		T_CONFIG_STRING,
95 		T_CONFIG_SHORT,
96 		T_CONFIG_INT,
97 		T_CONFIG_BOOLEAN,
98 		T_CONFIG_ARRAY,
99 		T_CONFIG_LOCAL,
100 		T_CONFIG_DEPRECATED,
101 		T_CONFIG_UNSUPPORTED
102 } config_values_type_t;
103 
104 typedef enum { T_CONFIG_SCOPE_UNSET,
105 		T_CONFIG_SCOPE_SERVER,
106 		T_CONFIG_SCOPE_CONNECTION
107 } config_scope_type_t;
108 
109 typedef struct {
110 	const char *key;
111 	void *destination;
112 
113 	config_values_type_t type;
114 	config_scope_type_t scope;
115 } config_values_t;
116 
117 typedef enum { DIRECT, EXTERNAL } connection_type;
118 
119 typedef struct {
120 	char *key;
121 	connection_type type;
122 	char *value;
123 } request_handler;
124 
125 typedef struct {
126 	char *key;
127 	char *host;
128 	unsigned short port;
129 	int used;
130 	short factor;
131 } fcgi_connections;
132 
133 
134 typedef union {
135 #ifdef HAVE_IPV6
136 	struct sockaddr_in6 ipv6;
137 #endif
138 	struct sockaddr_in ipv4;
139 #ifdef HAVE_SYS_UN_H
140 	struct sockaddr_un un;
141 #endif
142 	struct sockaddr plain;
143 } sock_addr;
144 
145 /* fcgi_response_header contains ... */
146 #define HTTP_STATUS         BV(0)
147 #define HTTP_CONNECTION     BV(1)
148 #define HTTP_CONTENT_LENGTH BV(2)
149 #define HTTP_DATE           BV(3)
150 #define HTTP_LOCATION       BV(4)
151 
152 typedef struct {
153 	/** HEADER */
154 	/* the request-line */
155 	buffer *request;
156 	buffer *uri;
157 
158 	buffer *orig_uri;
159 
160 	http_method_t  http_method;
161 	http_version_t http_version;
162 
163 	buffer *request_line;
164 
165 	/* strings to the header */
166 	buffer *http_host; /* not alloced */
167 	const char   *http_range;
168 	const char   *http_content_type;
169 	const char   *http_if_modified_since;
170 	const char   *http_if_none_match;
171 
172 	array  *headers;
173 
174 	/* CONTENT */
175 	size_t content_length; /* returned by strtoul() */
176 
177 	/* internal representation */
178 	int     accept_encoding;
179 
180 	/* internal */
181 	buffer *pathinfo;
182 } request;
183 
184 typedef struct {
185 	off_t   content_length;
186 	int     keep_alive;               /* used by  the subrequests in proxy, cgi and fcgi to say the subrequest was keep-alive or not */
187 
188 	array  *headers;
189 
190 	enum {
191 		HTTP_TRANSFER_ENCODING_IDENTITY, HTTP_TRANSFER_ENCODING_CHUNKED
192 	} transfer_encoding;
193 } response;
194 
195 typedef struct {
196 	buffer *scheme; /* scheme without colon or slashes ( "http" or "https" ) */
197 
198 	/* authority with optional portnumber ("site.name" or "site.name:8080" ) NOTE: without "username:password@" */
199 	buffer *authority;
200 
201 	/* path including leading slash ("/" or "/index.html") - urldecoded, and sanitized  ( buffer_path_simplify() && buffer_urldecode_path() ) */
202 	buffer *path;
203 	buffer *path_raw; /* raw path, as sent from client. no urldecoding or path simplifying */
204 	buffer *query; /* querystring ( everything after "?", ie: in "/index.php?foo=1", query is "foo=1" ) */
205 } request_uri;
206 
207 typedef struct {
208 	buffer *path;
209 	buffer *basedir; /* path = "(basedir)(.*)" */
210 
211 	buffer *doc_root; /* path = doc_root + rel_path */
212 	buffer *rel_path;
213 
214 	buffer *etag;
215 } physical;
216 
217 typedef struct {
218 	buffer *name;
219 	buffer *etag;
220 
221 	struct stat st;
222 
223 	time_t stat_ts;
224 
225 #ifdef HAVE_LSTAT
226 	char is_symlink;
227 #endif
228 
229 #ifdef HAVE_FAM_H
230 	int    dir_version;
231 	int    dir_ndx;
232 #endif
233 
234 	buffer *content_type;
235 } stat_cache_entry;
236 
237 typedef struct {
238 	splay_tree *files; /* the nodes of the tree are stat_cache_entry's */
239 
240 	buffer *dir_name; /* for building the dirname from the filename */
241 #ifdef HAVE_FAM_H
242 	splay_tree *dirs; /* the nodes of the tree are fam_dir_entry */
243 
244 	FAMConnection *fam;
245 	int    fam_fcce_ndx;
246 #endif
247 	buffer *hash_key;  /* temp-store for the hash-key */
248 } stat_cache;
249 
250 typedef struct {
251 	array *mimetypes;
252 
253 	/* virtual-servers */
254 	buffer *document_root;
255 	buffer *server_name;
256 	buffer *error_handler;
257 	buffer *server_tag;
258 	buffer *dirlist_encoding;
259 	buffer *errorfile_prefix;
260 
261 	unsigned short max_keep_alive_requests;
262 	unsigned short max_keep_alive_idle;
263 	unsigned short max_read_idle;
264 	unsigned short max_write_idle;
265 	unsigned short use_xattr;
266 	unsigned short follow_symlink;
267 	unsigned short range_requests;
268 	unsigned short infinite_keep_alive_requests;
269 
270 	/* debug */
271 
272 	unsigned short log_file_not_found;
273 	unsigned short log_request_header;
274 	unsigned short log_request_handling;
275 	unsigned short log_response_header;
276 	unsigned short log_condition_handling;
277 	unsigned short log_ssl_noise;
278 	unsigned short log_timeouts;
279 
280 
281 	/* server wide */
282 	buffer *ssl_pemfile;
283 	buffer *ssl_ca_file;
284 	buffer *ssl_cipher_list;
285 	buffer *ssl_dh_file;
286 	buffer *ssl_ec_curve;
287 	unsigned short ssl_honor_cipher_order; /* determine SSL cipher in server-preferred order, not client-order */
288 	unsigned short ssl_use_sslv2;
289 	unsigned short ssl_use_sslv3;
290 	unsigned short ssl_verifyclient;
291 	unsigned short ssl_verifyclient_enforce;
292 	unsigned short ssl_verifyclient_depth;
293 	buffer *ssl_verifyclient_username;
294 	unsigned short ssl_verifyclient_export_cert;
295 	unsigned short ssl_disable_client_renegotiation;
296 
297 	unsigned short use_ipv6, set_v6only; /* set_v6only is only a temporary option */
298 	unsigned short defer_accept;
299 	unsigned short is_ssl;
300 	unsigned short allow_http11;
301 	unsigned short etag_use_inode;
302 	unsigned short etag_use_mtime;
303 	unsigned short etag_use_size;
304 	unsigned short force_lowercase_filenames; /* if the FS is case-insensitive, force all files to lower-case */
305 	unsigned int max_request_size;
306 
307 	unsigned short kbytes_per_second; /* connection kb/s limit */
308 
309 	/* configside */
310 	unsigned short global_kbytes_per_second; /*  */
311 
312 	off_t  global_bytes_per_second_cnt;
313 	/* server-wide traffic-shaper
314 	 *
315 	 * each context has the counter which is inited once
316 	 * a second by the global_kbytes_per_second config-var
317 	 *
318 	 * as soon as global_kbytes_per_second gets below 0
319 	 * the connected conns are "offline" a little bit
320 	 *
321 	 * the problem:
322 	 * we somehow have to loose our "we are writable" signal
323 	 * on the way.
324 	 *
325 	 */
326 	off_t *global_bytes_per_second_cnt_ptr; /*  */
327 
328 #ifdef USE_OPENSSL
329 	SSL_CTX *ssl_ctx;
330 #endif
331 } specific_config;
332 
333 /* the order of the items should be the same as they are processed
334  * read before write as we use this later */
335 typedef enum {
336 	CON_STATE_CONNECT,
337 	CON_STATE_REQUEST_START,
338 	CON_STATE_READ,
339 	CON_STATE_REQUEST_END,
340 	CON_STATE_READ_POST,
341 	CON_STATE_HANDLE_REQUEST,
342 	CON_STATE_RESPONSE_START,
343 	CON_STATE_WRITE,
344 	CON_STATE_RESPONSE_END,
345 	CON_STATE_ERROR,
346 	CON_STATE_CLOSE
347 } connection_state_t;
348 
349 typedef enum { COND_RESULT_UNSET, COND_RESULT_FALSE, COND_RESULT_TRUE } cond_result_t;
350 typedef struct {
351 	cond_result_t result;
352 	int patterncount;
353 	int matches[3 * 10];
354 	buffer *comp_value; /* just a pointer */
355 
356 	comp_key_t comp_type;
357 } cond_cache_t;
358 
359 typedef struct {
360 	connection_state_t state;
361 
362 	/* timestamps */
363 	time_t read_idle_ts;
364 	time_t close_timeout_ts;
365 	time_t write_request_ts;
366 
367 	time_t connection_start;
368 	time_t request_start;
369 
370 	struct timeval start_tv;
371 
372 	size_t request_count;        /* number of requests handled in this connection */
373 	size_t loops_per_request;    /* to catch endless loops in a single request
374 				      *
375 				      * used by mod_rewrite, mod_fastcgi, ... and others
376 				      * this is self-protection
377 				      */
378 
379 	int fd;                      /* the FD for this connection */
380 	int fde_ndx;                 /* index for the fdevent-handler */
381 	int ndx;                     /* reverse mapping to server->connection[ndx] */
382 
383 	/* fd states */
384 	int is_readable;
385 	int is_writable;
386 
387 	int keep_alive;              /* only request.c can enable it, all other just disable */
388 	int keep_alive_idle;         /* remember max_keep_alive_idle from config */
389 
390 	int file_started;
391 	int file_finished;
392 
393 	chunkqueue *write_queue;      /* a large queue for low-level write ( HTTP response ) [ file, mem ] */
394 	chunkqueue *read_queue;       /* a small queue for low-level read ( HTTP request ) [ mem ] */
395 	chunkqueue *request_content_queue; /* takes request-content into tempfile if necessary [ tempfile, mem ]*/
396 
397 	int traffic_limit_reached;
398 
399 	off_t bytes_written;          /* used by mod_accesslog, mod_rrd */
400 	off_t bytes_written_cur_second; /* used by mod_accesslog, mod_rrd */
401 	off_t bytes_read;             /* used by mod_accesslog, mod_rrd */
402 	off_t bytes_header;
403 
404 	int http_status;
405 
406 	sock_addr dst_addr;
407 	buffer *dst_addr_buf;
408 
409 	/* request */
410 	buffer *parse_request;
411 	unsigned int parsed_response; /* bitfield which contains the important header-fields of the parsed response header */
412 
413 	request  request;
414 	request_uri uri;
415 	physical physical;
416 	response response;
417 
418 	size_t header_len;
419 
420 	buffer *authed_user;
421 	array  *environment; /* used to pass lighttpd internal stuff to the FastCGI/CGI apps, setenv does that */
422 
423 	/* response */
424 	int    got_response;
425 
426 	int    in_joblist;
427 
428 	connection_type mode;
429 
430 	void **plugin_ctx;           /* plugin connection specific config */
431 
432 	specific_config conf;        /* global connection specific config */
433 	cond_cache_t *cond_cache;
434 
435 	buffer *server_name;
436 
437 	/* error-handler */
438 	buffer *error_handler;
439 	int error_handler_saved_status;
440 	int in_error_handler;
441 
442 	void *srv_socket;   /* reference to the server-socket (typecast to server_socket) */
443 
444 #ifdef USE_OPENSSL
445 	SSL *ssl;
446 # ifndef OPENSSL_NO_TLSEXT
447 	buffer *tlsext_server_name;
448 # endif
449 	unsigned int renegotiations; /* count of SSL_CB_HANDSHAKE_START */
450 #endif
451 	/* etag handling */
452 	etag_flags_t etag_flags;
453 
454 	int conditional_is_valid[COMP_LAST_ELEMENT];
455 } connection;
456 
457 typedef struct {
458 	connection **ptr;
459 	size_t size;
460 	size_t used;
461 } connections;
462 
463 
464 #ifdef HAVE_IPV6
465 typedef struct {
466 	int family;
467 	union {
468 		struct in6_addr ipv6;
469 		struct in_addr  ipv4;
470 	} addr;
471 	char b2[INET6_ADDRSTRLEN + 1];
472 	time_t ts;
473 } inet_ntop_cache_type;
474 #endif
475 
476 
477 typedef struct {
478 	buffer *uri;
479 	time_t mtime;
480 	int http_status;
481 } realpath_cache_type;
482 
483 typedef struct {
484 	time_t  mtime;  /* the key */
485 	buffer *str;    /* a buffer for the string represenation */
486 } mtime_cache_type;
487 
488 typedef struct {
489 	void  *ptr;
490 	size_t used;
491 	size_t size;
492 } buffer_plugin;
493 
494 typedef struct {
495 	unsigned short port;
496 	buffer *bindhost;
497 
498 	buffer *errorlog_file;
499 	unsigned short errorlog_use_syslog;
500 	buffer *breakagelog_file;
501 
502 	unsigned short dont_daemonize;
503 	buffer *changeroot;
504 	buffer *username;
505 	buffer *groupname;
506 
507 	buffer *pid_file;
508 
509 	buffer *event_handler;
510 
511 	buffer *modules_dir;
512 	buffer *network_backend;
513 	array *modules;
514 	array *upload_tempdirs;
515 
516 	unsigned short max_worker;
517 	int max_fds;
518 	int max_conns;
519 #ifdef HAVE_LIBMTCP
520 	int listen_backlog;
521 #endif
522 	unsigned int max_request_size;
523 
524 	unsigned short log_request_header_on_error;
525 	unsigned short log_state_handling;
526 
527 	enum { STAT_CACHE_ENGINE_UNSET,
528 			STAT_CACHE_ENGINE_NONE,
529 			STAT_CACHE_ENGINE_SIMPLE
530 #ifdef HAVE_FAM_H
531 			, STAT_CACHE_ENGINE_FAM
532 #endif
533 	} stat_cache_engine;
534 	unsigned short enable_cores;
535 	unsigned short reject_expect_100_with_417;
536 } server_config;
537 
538 typedef struct {
539 	sock_addr addr;
540 	int       fd;
541 	int       fde_ndx;
542 
543 	buffer *ssl_pemfile;
544 	buffer *ssl_ca_file;
545 	buffer *ssl_cipher_list;
546 	buffer *ssl_dh_file;
547 	buffer *ssl_ec_curve;
548 	unsigned short ssl_use_sslv2;
549 	unsigned short ssl_use_sslv3;
550 	unsigned short use_ipv6;
551 	unsigned short is_ssl;
552 
553 	buffer *srv_token;
554 
555 #ifdef USE_OPENSSL
556 	SSL_CTX *ssl_ctx;
557 #endif
558        unsigned short is_proxy_ssl;
559 } server_socket;
560 
561 typedef struct {
562 	server_socket **ptr;
563 
564 	size_t size;
565 	size_t used;
566 } server_socket_array;
567 
568 typedef struct server {
569 	server_socket_array srv_sockets;
570 
571 	/* the errorlog */
572 	int errorlog_fd;
573 	enum { ERRORLOG_FILE, ERRORLOG_FD, ERRORLOG_SYSLOG, ERRORLOG_PIPE } errorlog_mode;
574 	buffer *errorlog_buf;
575 
576 	fdevents *ev, *ev_ins;
577 
578 	buffer_plugin plugins;
579 	void *plugin_slots;
580 
581 	/* counters */
582 	int con_opened;
583 	int con_read;
584 	int con_written;
585 	int con_closed;
586 
587 	int ssl_is_init;
588 
589 	int max_fds;    /* max possible fds */
590 	int cur_fds;    /* currently used fds */
591 	int want_fds;   /* waiting fds */
592 	int sockets_disabled;
593 
594 	size_t max_conns;
595 
596 	/* buffers */
597 	buffer *parse_full_path;
598 	buffer *response_header;
599 	buffer *response_range;
600 	buffer *tmp_buf;
601 
602 	buffer *tmp_chunk_len;
603 
604 	buffer *empty_string; /* is necessary for cond_match */
605 
606 	buffer *cond_check_buf;
607 
608 	/* caches */
609 #ifdef HAVE_IPV6
610 	inet_ntop_cache_type inet_ntop_cache[INET_NTOP_CACHE_MAX];
611 #endif
612 	mtime_cache_type mtime_cache[FILE_CACHE_MAX];
613 
614 	array *split_vals;
615 
616 	/* Timestamps */
617 	time_t cur_ts;
618 	time_t last_generated_date_ts;
619 	time_t last_generated_debug_ts;
620 	time_t startup_ts;
621 
622 	char entropy[8]; /* from /dev/[u]random if possible, otherwise rand() */
623 	char is_real_entropy; /* whether entropy is from /dev/[u]random */
624 
625 	buffer *ts_debug_str;
626 	buffer *ts_date_str;
627 
628 	/* config-file */
629 	array *config;
630 	array *config_touched;
631 
632 	array *config_context;
633 	specific_config **config_storage;
634 
635 	server_config  srvconf;
636 
637 	short int config_deprecated;
638 	short int config_unsupported;
639 
640 	connections *conns;
641 	connections *joblist;
642 	connections *fdwaitqueue;
643 
644 	stat_cache  *stat_cache;
645 
646 	/**
647 	 * The status array can carry all the status information you want
648 	 * the key to the array is <module-prefix>.<name>
649 	 * and the values are counters
650 	 *
651 	 * example:
652 	 *   fastcgi.backends        = 10
653 	 *   fastcgi.active-backends = 6
654 	 *   fastcgi.backend.<key>.load = 24
655 	 *   fastcgi.backend.<key>....
656 	 *
657 	 *   fastcgi.backend.<key>.disconnects = ...
658 	 */
659 	array *status;
660 
661 	fdevent_handler_t event_handler;
662 
663 	int (* network_backend_write)(struct server *srv, connection *con, int fd, chunkqueue *cq, off_t max_bytes);
664 #ifdef USE_OPENSSL
665 	int (* network_ssl_backend_write)(struct server *srv, connection *con, SSL *ssl, chunkqueue *cq, off_t max_bytes);
666 #endif
667 
668 #ifdef MULTI_THREADED
669 	unsigned char cpu;
670 	pthread_t running_thread;
671 #ifdef HAVE_LIBMTCP
672 	mctx_t mctx;
673 	int listen_backlog;
674 #else
675 	/* use to hold a pointer that tells that this is the first entry */
676 	/* this identifier field will be used to create server socket for */
677 	/* only the first element of the array */
678 	struct server *first_entry;
679 #endif
680 #endif
681 	uid_t uid;
682 	gid_t gid;
683 } server;
684 
685 
686 #endif
687