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