1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
/* $OpenBSD: server_fcgi.c,v 1.22 2014/08/06 13:40:18 florian Exp $ */
/*
* Copyright (c) 2014 Florian Obser <florian@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/tree.h>
#include <sys/hash.h>
#include <net/if.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <err.h>
#include <event.h>
#include "httpd.h"
#include "http.h"
#define FCGI_CONTENT_SIZE 65535
#define FCGI_PADDING_SIZE 255
#define FCGI_RECORD_SIZE \
(sizeof(struct fcgi_record_header) + FCGI_CONTENT_SIZE + FCGI_PADDING_SIZE)
#define FCGI_BEGIN_REQUEST 1
#define FCGI_ABORT_REQUEST 2
#define FCGI_END_REQUEST 3
#define FCGI_PARAMS 4
#define FCGI_STDIN 5
#define FCGI_STDOUT 6
#define FCGI_STDERR 7
#define FCGI_DATA 8
#define FCGI_GET_VALUES 9
#define FCGI_GET_VALUES_RESULT 10
#define FCGI_UNKNOWN_TYPE 11
#define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
#define FCGI_RESPONDER 1
struct fcgi_record_header {
uint8_t version;
uint8_t type;
uint16_t id;
uint16_t content_len;
uint8_t padding_len;
uint8_t reserved;
} __packed;
struct fcgi_begin_request_body {
uint16_t role;
uint8_t flags;
uint8_t reserved[5];
} __packed;
struct server_fcgi_param {
int total_len;
uint8_t buf[FCGI_RECORD_SIZE];
};
int server_fcgi_header(struct client *, u_int);
void server_fcgi_read(struct bufferevent *, void *);
int server_fcgi_writeheader(struct client *, struct kv *, void *);
int fcgi_add_param(struct server_fcgi_param *, const char *, const char *,
struct client *);
int
server_fcgi(struct httpd *env, struct client *clt)
{
struct server_fcgi_param param;
char hbuf[MAXHOSTNAMELEN];
struct server_config *srv_conf = clt->clt_srv_conf;
struct http_descriptor *desc = clt->clt_desc;
struct sockaddr_un sun;
struct fcgi_record_header *h;
struct fcgi_begin_request_body *begin;
size_t len;
ssize_t scriptlen;
int fd = -1, ret;
const char *errstr = NULL;
char *str, *p, *script = NULL;
in_port_t port;
struct sockaddr_storage ss;
if (srv_conf->socket[0] == ':') {
p = srv_conf->socket + 1;
port = strtonum(p, 0, 0xffff, &errstr);
if (errstr != NULL) {
log_warn("%s: strtonum %s, %s", __func__, p, errstr);
goto fail;
}
memset(&ss, 0, sizeof(ss));
ss.ss_family = AF_INET;
((struct sockaddr_in *)
&ss)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
port = htons(port);
if ((fd = server_socket_connect(&ss, port, srv_conf)) == -1)
goto fail;
} else {
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
goto fail;
memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;
len = strlcpy(sun.sun_path,
srv_conf->socket, sizeof(sun.sun_path));
if (len >= sizeof(sun.sun_path)) {
errstr = "socket path to long";
goto fail;
}
sun.sun_len = len;
if (connect(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
goto fail;
}
socket_set_blockmode(fd, BM_NONBLOCK);
memset(&hbuf, 0, sizeof(hbuf));
clt->clt_fcgi_state = FCGI_READ_HEADER;
clt->clt_fcgi_toread = sizeof(struct fcgi_record_header);
if (clt->clt_srvevb != NULL)
evbuffer_free(clt->clt_srvevb);
clt->clt_srvevb = evbuffer_new();
if (clt->clt_srvevb == NULL) {
errstr = "failed to allocate evbuffer";
goto fail;
}
clt->clt_fd = fd;
if (clt->clt_srvbev != NULL)
bufferevent_free(clt->clt_srvbev);
clt->clt_srvbev = bufferevent_new(fd, server_fcgi_read,
NULL, server_file_error, clt);
if (clt->clt_srvbev == NULL) {
errstr = "failed to allocate fcgi buffer event";
goto fail;
}
memset(¶m, 0, sizeof(param));
h = (struct fcgi_record_header *)¶m.buf;
h->version = 1;
h->type = FCGI_BEGIN_REQUEST;
h->id = htons(1);
h->content_len = htons(sizeof(struct fcgi_begin_request_body));
h->padding_len = 0;
begin = (struct fcgi_begin_request_body *)¶m.buf[sizeof(struct
fcgi_record_header)];
begin->role = htons(FCGI_RESPONDER);
bufferevent_write(clt->clt_srvbev, ¶m.buf,
sizeof(struct fcgi_record_header) +
sizeof(struct fcgi_begin_request_body));
h->type = FCGI_PARAMS;
h->content_len = param.total_len = 0;
if (asprintf(&script, "%s%s", srv_conf->root,
desc->http_path) == -1 ||
(scriptlen = path_info(script)) == -1) {
errstr = "failed to get script name";
goto fail;
}
if (scriptlen) {
if (fcgi_add_param(¶m, "PATH_INFO",
script + scriptlen, clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
script[scriptlen] = '\0';
}
if (fcgi_add_param(¶m, "SCRIPT_NAME",
script + strlen(srv_conf->root), clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (fcgi_add_param(¶m, "SCRIPT_FILENAME", script, clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (desc->http_query)
if (fcgi_add_param(¶m, "QUERY_STRING", desc->http_query,
clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (fcgi_add_param(¶m, "DOCUMENT_ROOT", srv_conf->root,
clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (fcgi_add_param(¶m, "DOCUMENT_URI", desc->http_path,
clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (fcgi_add_param(¶m, "GATEWAY_INTERFACE", "CGI/1.1",
clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
/* Add HTTP_* headers */
if (server_headers(clt, server_fcgi_writeheader, ¶m) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (srv_conf->flags & SRVFLAG_SSL)
if (fcgi_add_param(¶m, "HTTPS", "on", clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
(void)print_host(&clt->clt_ss, hbuf, sizeof(hbuf));
if (fcgi_add_param(¶m, "REMOTE_ADDR", hbuf, clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
(void)snprintf(hbuf, sizeof(hbuf), "%d", ntohs(clt->clt_port));
if (fcgi_add_param(¶m, "REMOTE_PORT", hbuf, clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (fcgi_add_param(¶m, "REQUEST_METHOD",
server_httpmethod_byid(desc->http_method), clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (!desc->http_query) {
if (fcgi_add_param(¶m, "REQUEST_URI", desc->http_path,
clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
} else if (asprintf(&str, "%s?%s", desc->http_path,
desc->http_query) != -1) {
ret = fcgi_add_param(¶m, "REQUEST_URI", str, clt);
free(str);
if (ret == -1) {
errstr = "failed to encode param";
goto fail;
}
}
(void)print_host(&clt->clt_srv_ss, hbuf, sizeof(hbuf));
if (fcgi_add_param(¶m, "SERVER_ADDR", hbuf, clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
(void)snprintf(hbuf, sizeof(hbuf), "%d",
ntohs(server_socket_getport(&clt->clt_srv_ss)));
if (fcgi_add_param(¶m, "SERVER_PORT", hbuf, clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (fcgi_add_param(¶m, "SERVER_NAME", srv_conf->name,
clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (fcgi_add_param(¶m, "SERVER_PROTOCOL", desc->http_version,
clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (fcgi_add_param(¶m, "SERVER_SOFTWARE", HTTPD_SERVERNAME,
clt) == -1) {
errstr = "failed to encode param";
goto fail;
}
if (param.total_len != 0) { /* send last params record */
bufferevent_write(clt->clt_srvbev, ¶m.buf,
sizeof(struct fcgi_record_header) +
ntohs(h->content_len));
}
/* send "no more params" message */
h->content_len = 0;
bufferevent_write(clt->clt_srvbev, ¶m.buf,
sizeof(struct fcgi_record_header));
h->type = FCGI_STDIN;
bufferevent_write(clt->clt_srvbev, ¶m.buf,
sizeof(struct fcgi_record_header));
bufferevent_settimeout(clt->clt_srvbev,
srv_conf->timeout.tv_sec, srv_conf->timeout.tv_sec);
bufferevent_enable(clt->clt_srvbev, EV_READ|EV_WRITE);
bufferevent_disable(clt->clt_bev, EV_READ);
/*
* persist is not supported yet because we don't get the
* Content-Length from slowcgi and don't support chunked encoding.
*/
clt->clt_persist = 0;
clt->clt_done = 0;
free(script);
return (0);
fail:
free(script);
if (errstr == NULL)
errstr = strerror(errno);
server_abort_http(clt, 500, errstr);
return (-1);
}
int
fcgi_add_param(struct server_fcgi_param *p, const char *key,
const char *val, struct client *clt)
{
struct fcgi_record_header *h;
int len = 0;
int key_len = strlen(key);
int val_len = strlen(val);
uint8_t *param;
len += key_len + val_len;
len += key_len > 127 ? 4 : 1;
len += val_len > 127 ? 4 : 1;
DPRINTF("%s: %s[%d] => %s[%d], total_len: %d", __func__, key, key_len,
val, val_len, p->total_len);
if (len > FCGI_CONTENT_SIZE)
return (-1);
if (p->total_len + len > FCGI_CONTENT_SIZE) {
bufferevent_write(clt->clt_srvbev, p->buf,
sizeof(struct fcgi_record_header) + p->total_len);
p->total_len = 0;
}
h = (struct fcgi_record_header *)p->buf;
param = p->buf + sizeof(*h) + p->total_len;
if (key_len > 127) {
*param++ = ((key_len >> 24) & 0xff) | 0x80;
*param++ = ((key_len >> 16) & 0xff);
*param++ = ((key_len >> 8) & 0xff);
*param++ = (key_len & 0xff);
} else
*param++ = key_len;
if (val_len > 127) {
*param++ = ((val_len >> 24) & 0xff) | 0x80;
*param++ = ((val_len >> 16) & 0xff);
*param++ = ((val_len >> 8) & 0xff);
*param++ = (val_len & 0xff);
} else
*param++ = val_len;
memcpy(param, key, key_len);
param += key_len;
memcpy(param, val, val_len);
p->total_len += len;
h->content_len = htons(p->total_len);
return (0);
}
void
server_fcgi_read(struct bufferevent *bev, void *arg)
{
uint8_t buf[FCGI_RECORD_SIZE];
struct client *clt = (struct client *) arg;
struct fcgi_record_header *h;
size_t len;
do {
len = bufferevent_read(bev, &buf, clt->clt_fcgi_toread);
/* XXX error handling */
evbuffer_add(clt->clt_srvevb, &buf, len);
clt->clt_fcgi_toread -= len;
DPRINTF("%s: len: %lu toread: %d state: %d", __func__, len,
clt->clt_fcgi_toread, clt->clt_fcgi_state);
if (clt->clt_fcgi_toread != 0)
return;
switch (clt->clt_fcgi_state) {
case FCGI_READ_HEADER:
clt->clt_fcgi_state = FCGI_READ_CONTENT;
h = (struct fcgi_record_header *)
EVBUFFER_DATA(clt->clt_srvevb);
DPRINTF("%s: record header: version %d type %d id %d "
"content len %d padding %d", __func__,
h->version, h->type, ntohs(h->id),
ntohs(h->content_len), h->padding_len);
clt->clt_fcgi_type = h->type;
clt->clt_fcgi_toread = ntohs(h->content_len);
clt->clt_fcgi_padding_len = h->padding_len;
evbuffer_drain(clt->clt_srvevb,
EVBUFFER_LENGTH(clt->clt_srvevb));
if (clt->clt_fcgi_toread != 0)
break;
/* fallthrough if content_len == 0 */
case FCGI_READ_CONTENT:
if (clt->clt_fcgi_type == FCGI_STDOUT &&
EVBUFFER_LENGTH(clt->clt_srvevb) > 0) {
if (++clt->clt_chunk == 1)
server_fcgi_header(clt, 200);
server_bufferevent_write_buffer(clt,
clt->clt_srvevb);
}
evbuffer_drain(clt->clt_srvevb,
EVBUFFER_LENGTH(clt->clt_srvevb));
if (!clt->clt_fcgi_padding_len) {
clt->clt_fcgi_state = FCGI_READ_HEADER;
clt->clt_fcgi_toread =
sizeof(struct fcgi_record_header);
} else {
clt->clt_fcgi_state = FCGI_READ_PADDING;
clt->clt_fcgi_toread =
clt->clt_fcgi_padding_len;
}
break;
case FCGI_READ_PADDING:
evbuffer_drain(clt->clt_srvevb,
EVBUFFER_LENGTH(clt->clt_srvevb));
clt->clt_fcgi_state = FCGI_READ_HEADER;
clt->clt_fcgi_toread =
sizeof(struct fcgi_record_header);
break;
}
} while (len > 0);
}
int
server_fcgi_header(struct client *clt, u_int code)
{
struct http_descriptor *desc = clt->clt_desc;
const char *error;
char tmbuf[32];
if (desc == NULL || (error = server_httperror_byid(code)) == NULL)
return (-1);
if (server_log_http(clt, code, 0) == -1)
return (-1);
kv_purge(&desc->http_headers);
/* Add error codes */
if (kv_setkey(&desc->http_pathquery, "%lu", code) == -1 ||
kv_set(&desc->http_pathquery, "%s", error) == -1)
return (-1);
/* Add headers */
if (kv_add(&desc->http_headers, "Server", HTTPD_SERVERNAME) == NULL)
return (-1);
/* Is it a persistent connection? */
if (clt->clt_persist) {
if (kv_add(&desc->http_headers,
"Connection", "keep-alive") == NULL)
return (-1);
} else if (kv_add(&desc->http_headers, "Connection", "close") == NULL)
return (-1);
/* Date header is mandatory and should be added last */
server_http_date(tmbuf, sizeof(tmbuf));
if (kv_add(&desc->http_headers, "Date", tmbuf) == NULL)
return (-1);
/* Write initial header (fcgi might append more) */
if (server_writeresponse_http(clt) == -1 ||
server_bufferevent_print(clt, "\r\n") == -1 ||
server_headers(clt, server_writeheader_http, NULL) == -1)
return (-1);
return (0);
}
int
server_fcgi_writeheader(struct client *clt, struct kv *hdr, void *arg)
{
struct server_fcgi_param *param = arg;
char *val, *name, *p;
const char *key;
int ret;
if (hdr->kv_flags & KV_FLAG_INVALID)
return (0);
/* The key might have been updated in the parent */
if (hdr->kv_parent != NULL && hdr->kv_parent->kv_key != NULL)
key = hdr->kv_parent->kv_key;
else
key = hdr->kv_key;
val = hdr->kv_value;
if (strcasecmp(key, "Content-Length") == 0 ||
strcasecmp(key, "Content-Type") == 0) {
if ((name = strdup(key)) == NULL)
return (-1);
} else {
if (asprintf(&name, "HTTP_%s", key) == -1)
return (-1);
}
for (p = name; *p != '\0'; p++) {
if (isalpha((unsigned char)*p))
*p = toupper((unsigned char)*p);
else
*p = '_';
}
ret = fcgi_add_param(param, name, val, clt);
free(name);
return (ret);
}
|