xref: /lighttpd1.4/src/mod_fastcgi.c (revision fcf0dc3e)
1 #include "first.h"
2 
3 #include <sys/types.h>
4 #include <stdlib.h>
5 #include <string.h>
6 
7 #include "gw_backend.h"
8 typedef gw_plugin_config plugin_config;
9 typedef gw_plugin_data   plugin_data;
10 typedef gw_handler_ctx   handler_ctx;
11 
12 #include "buffer.h"
13 #include "fdevent.h"
14 #include "http_cgi.h"
15 #include "http_chunk.h"
16 #include "log.h"
17 #include "request.h"
18 
19 #include "compat/fastcgi.h"
20 
21 #if GW_RESPONDER  != FCGI_RESPONDER
22 #error "mismatched defines: (GW_RESPONDER != FCGI_RESPONDER)"
23 #endif
24 #if GW_AUTHORIZER != FCGI_AUTHORIZER
25 #error "mismatched defines: (GW_AUTHORIZER != FCGI_AUTHORIZER)"
26 #endif
27 #if GW_FILTER     != FCGI_FILTER
28 #error "mismatched defines: (GW_FILTER != FCGI_FILTER)"
29 #endif
30 
31 static void mod_fastcgi_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
32     switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
33       case 0: /* fastcgi.server */
34         if (cpv->vtype == T_CONFIG_LOCAL) {
35             gw_plugin_config * const gw = cpv->v.v;
36             pconf->exts      = gw->exts;
37             pconf->exts_auth = gw->exts_auth;
38             pconf->exts_resp = gw->exts_resp;
39         }
40         break;
41       case 1: /* fastcgi.balance */
42         /*if (cpv->vtype == T_CONFIG_LOCAL)*//*always true here for this param*/
43             pconf->balance = (int)cpv->v.u;
44         break;
45       case 2: /* fastcgi.debug */
46         pconf->debug = (int)cpv->v.u;
47         break;
48       case 3: /* fastcgi.map-extensions */
49         pconf->ext_mapping = cpv->v.a;
50         break;
51       default:/* should not happen */
52         return;
53     }
54 }
55 
56 static void mod_fastcgi_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
57     do {
58         mod_fastcgi_merge_config_cpv(pconf, cpv);
59     } while ((++cpv)->k_id != -1);
60 }
61 
62 static void mod_fastcgi_patch_config(request_st * const r, plugin_data * const p) {
63     memcpy(&p->conf, &p->defaults, sizeof(plugin_config));
64     for (int i = 1, used = p->nconfig; i < used; ++i) {
65         if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
66             mod_fastcgi_merge_config(&p->conf,p->cvlist + p->cvlist[i].v.u2[0]);
67     }
68 }
69 
70 SETDEFAULTS_FUNC(mod_fastcgi_set_defaults) {
71     static const config_plugin_keys_t cpk[] = {
72       { CONST_STR_LEN("fastcgi.server"),
73         T_CONFIG_ARRAY_KVARRAY,
74         T_CONFIG_SCOPE_CONNECTION }
75      ,{ CONST_STR_LEN("fastcgi.balance"),
76         T_CONFIG_STRING,
77         T_CONFIG_SCOPE_CONNECTION }
78      ,{ CONST_STR_LEN("fastcgi.debug"),
79         T_CONFIG_INT,
80         T_CONFIG_SCOPE_CONNECTION }
81      ,{ CONST_STR_LEN("fastcgi.map-extensions"),
82         T_CONFIG_ARRAY_KVSTRING,
83         T_CONFIG_SCOPE_CONNECTION }
84      ,{ NULL, 0,
85         T_CONFIG_UNSET,
86         T_CONFIG_SCOPE_UNSET }
87     };
88 
89     plugin_data * const p = p_d;
90     if (!config_plugin_values_init(srv, p, cpk, "mod_fastcgi"))
91         return HANDLER_ERROR;
92 
93     /* process and validate config directives
94      * (init i to 0 if global context; to 1 to skip empty global context) */
95     for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
96         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
97         for (; -1 != cpv->k_id; ++cpv) {
98             switch (cpv->k_id) {
99               case 0:{/* fastcgi.server */
100                 gw_plugin_config *gw = calloc(1, sizeof(gw_plugin_config));
101                 force_assert(gw);
102                 if (!gw_set_defaults_backend(srv, p, cpv->v.a, gw, 0,
103                                              cpk[cpv->k_id].k)) {
104                     gw_plugin_config_free(gw);
105                     return HANDLER_ERROR;
106                 }
107                 cpv->v.v = gw;
108                 cpv->vtype = T_CONFIG_LOCAL;
109                 break;
110               }
111               case 1: /* fastcgi.balance */
112                 cpv->v.u = (unsigned int)gw_get_defaults_balance(srv, cpv->v.b);
113                 break;
114               case 2: /* fastcgi.debug */
115               case 3: /* fastcgi.map-extensions */
116                 break;
117               default:/* should not happen */
118                 break;
119             }
120         }
121     }
122 
123     /* default is 0 */
124     /*p->defaults.balance = (unsigned int)gw_get_defaults_balance(srv, NULL);*/
125 
126     /* initialize p->defaults from global config context */
127     if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
128         const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
129         if (-1 != cpv->k_id)
130             mod_fastcgi_merge_config(&p->defaults, cpv);
131     }
132 
133     return HANDLER_GO_ON;
134 }
135 
136 
137 static int fcgi_env_add(void *venv, const char *key, size_t key_len, const char *val, size_t val_len) {
138 	buffer *env = venv;
139 	char len_enc[8];
140 	size_t len_enc_len = 0;
141 
142 	if (!key || (!val && val_len)) return -1;
143 
144 	/**
145 	 * field length can be 31bit max
146 	 *
147 	 * HINT: this can't happen as FCGI_MAX_LENGTH is only 16bit
148 	 */
149 	force_assert(key_len < 0x7fffffffu);
150 	force_assert(val_len < 0x7fffffffu);
151 
152 	if (key_len > 127) {
153 		len_enc[0] = ((key_len >> 24) & 0xff) | 0x80;
154 		len_enc[1] =  (key_len >> 16) & 0xff;
155 		len_enc[2] =  (key_len >>  8) & 0xff;
156 		len_enc_len += 3;
157 	}
158 	len_enc[len_enc_len++] = key_len & 0xff;
159 
160 	if (val_len > 127) {
161 		len_enc[len_enc_len++] = ((val_len >> 24) & 0xff) | 0x80;
162 		len_enc[len_enc_len++] = (val_len >> 16) & 0xff;
163 		len_enc[len_enc_len++] = (val_len >> 8) & 0xff;
164 	}
165 	len_enc[len_enc_len++] = val_len & 0xff;
166 
167 	const size_t len = len_enc_len + key_len + val_len;
168 	const size_t fmax =
169 	  FCGI_MAX_LENGTH + sizeof(FCGI_BeginRequestRecord) + sizeof(FCGI_Header);
170 	if (len > fmax - buffer_clen(env))
171 		return -1; /* we can't append more headers, ignore it */
172 
173 	buffer_append_str3(env, len_enc, len_enc_len, key, key_len, val, val_len);
174 	return 0;
175 }
176 
177 static void fcgi_header(FCGI_Header * header, unsigned char type, int request_id, int contentLength, unsigned char paddingLength) {
178 	force_assert(contentLength <= FCGI_MAX_LENGTH);
179 
180 	header->version = FCGI_VERSION_1;
181 	header->type = type;
182 	header->requestIdB0 = request_id & 0xff;
183 	header->requestIdB1 = (request_id >> 8) & 0xff;
184 	header->contentLengthB0 = contentLength & 0xff;
185 	header->contentLengthB1 = (contentLength >> 8) & 0xff;
186 	header->paddingLength = paddingLength;
187 	header->reserved = 0;
188 }
189 
190 static handler_t fcgi_stdin_append(handler_ctx *hctx) {
191 	FCGI_Header header;
192 	chunkqueue * const req_cq = &hctx->r->reqbody_queue;
193 	off_t offset, weWant;
194 	off_t req_cqlen = chunkqueue_length(req_cq);
195 	int request_id = hctx->request_id;
196 	if (req_cqlen > MAX_WRITE_LIMIT) req_cqlen = MAX_WRITE_LIMIT;
197 
198 	/* something to send ? */
199 	for (offset = 0; offset != req_cqlen; offset += weWant) {
200 		weWant = req_cqlen - offset > FCGI_MAX_LENGTH ? FCGI_MAX_LENGTH : req_cqlen - offset;
201 
202 		if (-1 != hctx->wb_reqlen) {
203 			if (hctx->wb_reqlen >= 0) {
204 				hctx->wb_reqlen += sizeof(header);
205 			} else {
206 				hctx->wb_reqlen -= sizeof(header);
207 			}
208 		}
209 
210 		fcgi_header(&(header), FCGI_STDIN, request_id, weWant, 0);
211 		(chunkqueue_is_empty(&hctx->wb) || hctx->wb.first->type == MEM_CHUNK) /* else FILE_CHUNK for temp file */
212 		  ? chunkqueue_append_mem(&hctx->wb, (const char *)&header, sizeof(header))
213 		  : chunkqueue_append_mem_min(&hctx->wb, (const char *)&header, sizeof(header));
214 		chunkqueue_steal(&hctx->wb, req_cq, weWant);
215 		/*(hctx->wb_reqlen already includes reqbody_length)*/
216 	}
217 
218 	if (hctx->wb.bytes_in == hctx->wb_reqlen) {
219 		/* terminate STDIN */
220 		/* (future: must defer ending FCGI_STDIN
221 		 *  if might later upgrade protocols
222 		 *  and then have more data to send) */
223 		fcgi_header(&(header), FCGI_STDIN, request_id, 0, 0);
224 		chunkqueue_append_mem(&hctx->wb, (const char *)&header, sizeof(header));
225 		hctx->wb_reqlen += (int)sizeof(header);
226 	}
227 
228 	return HANDLER_GO_ON;
229 }
230 
231 static handler_t fcgi_create_env(handler_ctx *hctx) {
232 	FCGI_BeginRequestRecord beginRecord;
233 	FCGI_Header header;
234 	int request_id;
235 
236 	gw_host *host = hctx->host;
237 	request_st * const r = hctx->r;
238 
239 	http_cgi_opts opts = {
240 	  (hctx->gw_mode == FCGI_AUTHORIZER),
241 	  host->break_scriptfilename_for_php,
242 	  host->docroot,
243 	  host->strip_request_uri
244 	};
245 
246 	size_t rsz = (size_t)(r->read_queue.bytes_out - hctx->wb.bytes_in);
247 	if (rsz >= 65536) rsz = r->rqst_header_len;
248 	buffer * const b = chunkqueue_prepend_buffer_open_sz(&hctx->wb, rsz);
249 
250 	/* send FCGI_BEGIN_REQUEST */
251 
252 	if (hctx->request_id == 0) {
253 		hctx->request_id = 1; /* always use id 1 as we don't use multiplexing */
254 	} else {
255 		log_error(r->conf.errh, __FILE__, __LINE__,
256 		  "fcgi-request is already in use: %d", hctx->request_id);
257 	}
258 	request_id = hctx->request_id;
259 
260 	fcgi_header(&(beginRecord.header), FCGI_BEGIN_REQUEST, request_id, sizeof(beginRecord.body), 0);
261 	beginRecord.body.roleB0 = hctx->gw_mode;
262 	beginRecord.body.roleB1 = 0;
263 	beginRecord.body.flags = 0;
264 	memset(beginRecord.body.reserved, 0, sizeof(beginRecord.body.reserved));
265 	fcgi_header(&header, FCGI_PARAMS, request_id, 0, 0); /*(set aside space to fill in later)*/
266 	buffer_append_str2(b, (const char *)&beginRecord, sizeof(beginRecord),
267 	                      (const char *)&header,      sizeof(header));
268 
269 	/* send FCGI_PARAMS */
270 
271 	if (0 != http_cgi_headers(r, &opts, fcgi_env_add, b)) {
272 		r->http_status = 400;
273 		r->handler_module = NULL;
274 		buffer_clear(b);
275 		chunkqueue_remove_finished_chunks(&hctx->wb);
276 		return HANDLER_FINISHED;
277 	} else {
278 		fcgi_header(&(header), FCGI_PARAMS, request_id,
279 			    buffer_clen(b) - sizeof(FCGI_BeginRequestRecord) - sizeof(FCGI_Header), 0);
280 		memcpy(b->ptr+sizeof(FCGI_BeginRequestRecord), (const char *)&header, sizeof(header));
281 
282 		fcgi_header(&(header), FCGI_PARAMS, request_id, 0, 0);
283 		buffer_append_string_len(b, (const char *)&header, sizeof(header));
284 
285 		hctx->wb_reqlen = buffer_clen(b);
286 		chunkqueue_prepend_buffer_commit(&hctx->wb);
287 	}
288 
289 	if (r->reqbody_length) {
290 		/*chunkqueue_append_chunkqueue(&hctx->wb, &r->reqbody_queue);*/
291 		if (r->reqbody_length > 0)
292 			hctx->wb_reqlen += r->reqbody_length;/* (eventual) (minimal) total request size, not necessarily including all fcgi_headers around content length yet */
293 		else /* as-yet-unknown total request size (Transfer-Encoding: chunked)*/
294 			hctx->wb_reqlen = -hctx->wb_reqlen;
295 	}
296 	fcgi_stdin_append(hctx);
297 
298 	plugin_stats_inc("fastcgi.requests");
299 	return HANDLER_GO_ON;
300 }
301 
302 typedef struct {
303 	unsigned int len;
304 	int      type;
305 	int      padding;
306 	int      request_id;
307 } fastcgi_response_packet;
308 
309 static int fastcgi_get_packet(handler_ctx *hctx, fastcgi_response_packet *packet) {
310 	FCGI_Header header;
311 	off_t rblen = chunkqueue_length(hctx->rb);
312 	if (rblen < (off_t)sizeof(FCGI_Header)) {
313 		/* no header */
314 		if (hctx->conf.debug && 0 != rblen) {
315 			log_error(hctx->r->conf.errh, __FILE__, __LINE__,
316 			  "FastCGI: header too small: %lld bytes < %zu bytes, "
317 			  "waiting for more data", (long long)rblen, sizeof(FCGI_Header));
318 		}
319 		return -1;
320 	}
321 	char *ptr = (char *)&header;
322 	uint32_t rd = sizeof(FCGI_Header);
323 	if (chunkqueue_peek_data(hctx->rb, &ptr, &rd, hctx->r->conf.errh) < 0)
324 		return -1;
325 	if (rd != sizeof(FCGI_Header))
326 		return -1;
327 	if (ptr != (char *)&header) /* copy into aligned struct */
328 		memcpy(&header, ptr, sizeof(FCGI_Header));
329 
330 	/* we have at least a header, now check how much we have to fetch */
331 	packet->len = (header.contentLengthB0 | (header.contentLengthB1 << 8)) + header.paddingLength;
332 	packet->request_id = (header.requestIdB0 | (header.requestIdB1 << 8));
333 	packet->type = header.type;
334 	packet->padding = header.paddingLength;
335 
336 	if (packet->len > (unsigned int)rblen-sizeof(FCGI_Header)) {
337 		return -1; /* we didn't get the full packet */
338 	}
339 
340 	chunkqueue_mark_written(hctx->rb, sizeof(FCGI_Header));
341 	return 0;
342 }
343 
344 static void fastcgi_get_packet_body(buffer * const b, handler_ctx * const hctx, const fastcgi_response_packet * const packet) {
345     /* copy content; hctx->rb must contain at least packet->len content */
346     /* (read entire packet and then truncate padding, if present) */
347     const uint32_t blen = buffer_clen(b);
348     if (chunkqueue_read_data(hctx->rb,
349                              buffer_string_prepare_append(b, packet->len),
350                              packet->len, hctx->r->conf.errh) < 0)
351         return; /*(should not happen; should all be in memory)*/
352     buffer_truncate(b, blen + packet->len - packet->padding);
353 }
354 
355 __attribute_cold__
356 static handler_t fcgi_recv_0(const request_st * const r, const handler_ctx * const hctx) {
357 		if (-1 == hctx->request_id) return HANDLER_FINISHED; /*(flag request ended)*/
358 		if (!(fdevent_fdnode_interest(hctx->fdn) & FDEVENT_IN)
359 		    && !(r->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_POLLRDHUP))
360 			return HANDLER_GO_ON;
361 		log_error(r->conf.errh, __FILE__, __LINE__,
362 		  "unexpected end-of-file (perhaps the fastcgi process died):"
363 		  "pid: %d socket: %s",
364 		  hctx->proc->pid, hctx->proc->connection_name->ptr);
365 
366 		return HANDLER_ERROR;
367 }
368 
369 static handler_t fcgi_recv_parse_loop(request_st * const r, handler_ctx * const hctx) {
370 	/*
371 	 * parse the fastcgi packets and forward the content to the write-queue
372 	 *
373 	 */
374 	fastcgi_response_packet packet;
375 	int fin = 0;
376 	do {
377 		/* check if we have at least one packet */
378 		if (0 != fastcgi_get_packet(hctx, &packet)) {
379 			/* no full packet */
380 			break;
381 		}
382 
383 		switch(packet.type) {
384 		case FCGI_STDOUT:
385 			if (packet.len == 0) break;
386 
387 			/* is the header already finished */
388 			if (0 == r->resp_body_started) {
389 				/* split header from body */
390 				buffer *hdrs = hctx->response;
391 				if (NULL == hdrs) {
392 					hdrs = r->tmp_buf;
393 					buffer_clear(hdrs);
394 				}
395 				fastcgi_get_packet_body(hdrs, hctx, &packet);
396 				if (HANDLER_GO_ON != http_response_parse_headers(r, &hctx->opts, hdrs)) {
397 					hctx->send_content_body = 0;
398 					fin = 1;
399 					break;
400 				}
401 				if (0 == r->resp_body_started) {
402 					if (!hctx->response) {
403 						hctx->response = chunk_buffer_acquire();
404 						buffer_copy_buffer(hctx->response, hdrs);
405 					}
406 				}
407 				else if (hctx->gw_mode == GW_AUTHORIZER &&
408 					 (r->http_status == 0 || r->http_status == 200)) {
409 					/* authorizer approved request; ignore the content here */
410 					hctx->send_content_body = 0;
411 					hctx->opts.authorizer |= /*(save response streaming flags)*/
412 					  (r->conf.stream_response_body
413 					   & (FDEVENT_STREAM_RESPONSE
414 					     |FDEVENT_STREAM_RESPONSE_BUFMIN)) << 1;
415 					r->conf.stream_response_body &=
416 					  ~(FDEVENT_STREAM_RESPONSE|FDEVENT_STREAM_RESPONSE_BUFMIN);
417 				}
418 			  #if 0
419 				else if ((r->conf.stream_response_body &
420 				           (FDEVENT_STREAM_RESPONSE|FDEVENT_STREAM_RESPONSE_BUFMIN))
421 				         && (   r->http_status == 204
422 				             || r->http_status == 205
423 				             || r->http_status == 304
424 				             || r->http_method == HTTP_METHOD_HEAD)) {
425 					/* disable streaming to wait for backend protocol to signal
426 					 * end of response (prevent http_response_write_prepare()
427 					 * from short-circuiting and finishing responses without
428 					 * response body) */
429 					r->conf.stream_response_body &=
430 					  ~(FDEVENT_STREAM_RESPONSE|FDEVENT_STREAM_RESPONSE_BUFMIN);
431 				}
432 			  #endif
433 			} else if (hctx->send_content_body) {
434 				if (0 != http_response_transfer_cqlen(r, hctx->rb, (size_t)(packet.len - packet.padding))) {
435 					/* error writing to tempfile;
436 					 * truncate response or send 500 if nothing sent yet */
437 					hctx->send_content_body = 0;
438 					fin = 1;
439 				}
440 				if (packet.padding) chunkqueue_mark_written(hctx->rb, packet.padding);
441 			} else {
442 				chunkqueue_mark_written(hctx->rb, packet.len);
443 			}
444 			break;
445 		case FCGI_STDERR:
446 			if (packet.len) {
447 				buffer * const tb = r->tmp_buf;
448 				buffer_clear(tb);
449 				fastcgi_get_packet_body(tb, hctx, &packet);
450 				log_error_multiline(r->conf.errh, __FILE__, __LINE__,
451 				  BUF_PTR_LEN(tb), "FastCGI-stderr:");
452 			}
453 			break;
454 		case FCGI_END_REQUEST:
455 			hctx->request_id = -1; /*(flag request ended)*/
456 			fin = 1;
457 			break;
458 		default:
459 			log_error(r->conf.errh, __FILE__, __LINE__,
460 			  "FastCGI: header.type not handled: %d", packet.type);
461 			chunkqueue_mark_written(hctx->rb, packet.len);
462 			break;
463 		}
464 	} while (0 == fin);
465 
466 	return 0 == fin ? HANDLER_GO_ON : HANDLER_FINISHED;
467 }
468 
469 static handler_t fcgi_recv_parse(request_st * const r, struct http_response_opts_t *opts, buffer *b, size_t n) {
470     handler_ctx * const hctx = (handler_ctx *)opts->pdata;
471     if (0 == n) return fcgi_recv_0(r, hctx);
472     chunkqueue_append_buffer(hctx->rb, b);
473     return fcgi_recv_parse_loop(r, hctx);
474 }
475 
476 static handler_t fcgi_check_extension(request_st * const r, void *p_d, int uri_path_handler) {
477 	plugin_data *p = p_d;
478 	handler_t rc;
479 
480 	if (NULL != r->handler_module) return HANDLER_GO_ON;
481 
482 	mod_fastcgi_patch_config(r, p);
483 	if (NULL == p->conf.exts) return HANDLER_GO_ON;
484 
485 	rc = gw_check_extension(r, p, uri_path_handler, 0);
486 	if (HANDLER_GO_ON != rc) return rc;
487 
488 	if (r->handler_module == p->self) {
489 		handler_ctx *hctx = r->plugin_ctx[p->id];
490 		hctx->opts.backend = BACKEND_FASTCGI;
491 		hctx->opts.parse = fcgi_recv_parse;
492 		hctx->opts.pdata = hctx;   /*(skip +255 for potential padding)*/
493 		hctx->opts.max_per_read = sizeof(FCGI_Header)+FCGI_MAX_LENGTH+1;
494 		hctx->stdin_append = fcgi_stdin_append;
495 		hctx->create_env = fcgi_create_env;
496 		if (!hctx->rb) {
497 			hctx->rb = chunkqueue_init(NULL);
498 		}
499 		else {
500 			chunkqueue_reset(hctx->rb);
501 		}
502 	}
503 
504 	return HANDLER_GO_ON;
505 }
506 
507 /* uri-path handler */
508 static handler_t fcgi_check_extension_1(request_st * const r, void *p_d) {
509 	return fcgi_check_extension(r, p_d, 1);
510 }
511 
512 /* start request handler */
513 static handler_t fcgi_check_extension_2(request_st * const r, void *p_d) {
514 	return fcgi_check_extension(r, p_d, 0);
515 }
516 
517 
518 int mod_fastcgi_plugin_init(plugin *p);
519 int mod_fastcgi_plugin_init(plugin *p) {
520 	p->version      = LIGHTTPD_VERSION_ID;
521 	p->name         = "fastcgi";
522 
523 	p->init         = gw_init;
524 	p->cleanup      = gw_free;
525 	p->set_defaults = mod_fastcgi_set_defaults;
526 	p->handle_request_reset    = gw_handle_request_reset;
527 	p->handle_uri_clean        = fcgi_check_extension_1;
528 	p->handle_subrequest_start = fcgi_check_extension_2;
529 	p->handle_subrequest       = gw_handle_subrequest;
530 	p->handle_trigger          = gw_handle_trigger;
531 	p->handle_waitpid          = gw_handle_waitpid_cb;
532 
533 	return 0;
534 }
535