Debian Codegraph @

ext/fcgi/fcgi.c

raw symbols SCIP
1 /*
2 * fcgi.c
3 * Copyright (C) 1998-1999 Network Applied Communication Laboratory, Inc.
4 * Copyright (C) 2002-2006 MoonWolf <[email protected]>
5 * Copyright (C) 2012-2014 mva <[email protected]>
6 */
7
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <fcntl.h>
13
14 #include "ruby.h"
15 #ifdef HAVE_RUBY_VERSION_H
16 #include "ruby/version.h"
17 #endif
18
19 #ifndef RSTRING_PTR
20 #define RSTRING_PTR(str) (RSTRING(str)->ptr)
21 #endif
22 #ifndef RSTRING_LEN
23 #define RSTRING_LEN(str) (RSTRING(str)->len)
24 #endif
25
26 #ifdef HAVE_FASTCGI_FCGIAPP_H
27 #include <fastcgi/fcgiapp.h>
28 #else
29 #include "fcgiapp.h"
30 #endif
31
32
33 static VALUE cFCGI;
34 static VALUE eFCGIError;
35 static VALUE cFCGIStream;
36 static VALUE eFCGIStreamError;
37 static VALUE eFCGIStreamUnsupportedVersionError;
38 static VALUE eFCGIStreamProtocolError;
39 static VALUE eFCGIStreamParamsError;
40 static VALUE eFCGIStreamCallSeqError;
41
42 typedef struct fcgi_stream_data {
43 VALUE req;
44 FCGX_Stream *stream;
45 } fcgi_stream_data;
46
47 typedef struct fcgi_data {
48 FCGX_Request *req;
49 VALUE in;
50 VALUE out;
51 VALUE err;
52 VALUE env;
53 } fcgi_data;
54
55 static void fcgi_stream_mark(fcgi_stream_data *stream_data)
56 {
57 rb_gc_mark(stream_data->req);
58 }
59
60 static void fcgi_stream_free(fcgi_stream_data *stream_data)
61 {
62 free(stream_data);
63 }
64
65 static void fcgi_mark(fcgi_data *data)
66 {
67 rb_gc_mark(data->in);
68 rb_gc_mark(data->out);
69 rb_gc_mark(data->err);
70 rb_gc_mark(data->env);
71 }
72
73 static void fcgi_free_req(fcgi_data *data)
74 {
75 FCGX_Free(data->req, 1);
76 free(data->req);
77 free(data);
78 }
79
80 static VALUE fcgi_s_accept(VALUE self)
81 {
82 int status;
83 FCGX_Request *req;
84 rb_fdset_t readfds;
85
86 req = ALLOC(FCGX_Request);
87
88 status = FCGX_InitRequest(req,0,0);
89 if (status != 0) {
90 rb_raise(eFCGIError, "FCGX_Init() failed");
91 return Qnil;
92 }
93
94 rb_fd_init(&readfds);
95 rb_fd_set(req->listen_sock, &readfds);
96 if (rb_thread_fd_select(readfds.maxfd, &readfds, NULL, NULL, NULL) < 1) {
97 return Qnil;
98 }
99
100 status = FCGX_Accept_r(req);
101 if (status >= 0) {
102 fcgi_data *data;
103 fcgi_stream_data *stream_data;
104 char **env;
105 VALUE obj,key, value;
106 char *pkey,*pvalue;
107 int flags, fd;
108
109 /* Unset NONBLOCKING */
110 fd = ((FCGX_Request*) req)->ipcFd;
111 flags = fcntl(fd, F_GETFL);
112
113 if (flags & O_NONBLOCK) {
114 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
115 }
116
117 obj = Data_Make_Struct(self, fcgi_data, fcgi_mark, fcgi_free_req, data);
118 data->req = req;
119 data->in = Data_Make_Struct(cFCGIStream, fcgi_stream_data, fcgi_stream_mark, fcgi_stream_free, stream_data);
120 stream_data->stream = req->in;
121 stream_data->req = obj;
122 data->out = Data_Make_Struct(cFCGIStream, fcgi_stream_data, fcgi_stream_mark, fcgi_stream_free, stream_data);
123 stream_data->stream = req->out;
124 stream_data->req = obj;
125 data->err = Data_Make_Struct(cFCGIStream, fcgi_stream_data, fcgi_stream_mark, fcgi_stream_free, stream_data);
126 stream_data->stream = req->err;
127 stream_data->req = obj;
128 data->env = rb_hash_new();
129 env = req->envp;
130 for (; *env; env++) {
131 int size = 0;
132 pkey = *env;
133 pvalue = pkey;
134 while( *(pvalue++) != '=') size++;
135 key = rb_str_new(pkey, size);
136 value = rb_str_new2(pvalue);
137 OBJ_TAINT(key);
138 OBJ_TAINT(value);
139 rb_hash_aset(data->env, key, value);
140 }
141
142 return obj;
143 } else {
144 FCGX_Free(req, 1);
145 free(req);
146 return Qnil;
147 }
148 }
149
150 static VALUE fcgi_s_each(VALUE self)
151 {
152 VALUE fcgi;
153
154 while ((fcgi = fcgi_s_accept(self)) != Qnil) {
155 rb_yield(fcgi);
156 }
157 return Qnil;
158 }
159
160 static VALUE fcgi_s_iscgi(VALUE self)
161 {
162 if (FCGX_IsCGI()) {
163 return Qtrue;
164 } else {
165 return Qfalse;
166 }
167 }
168
169 static VALUE fcgi_in(VALUE self)
170 {
171 fcgi_data *data;
172
173 Data_Get_Struct(self, fcgi_data, data);
174 return data->in;
175 }
176
177 static VALUE fcgi_out(VALUE self)
178 {
179 fcgi_data *data;
180
181 Data_Get_Struct(self, fcgi_data, data);
182 return data->out;
183 }
184
185 static VALUE fcgi_err(VALUE self)
186 {
187 fcgi_data *data;
188
189 Data_Get_Struct(self, fcgi_data, data);
190 return data->err;
191 }
192
193 static VALUE fcgi_env(VALUE self)
194 {
195 fcgi_data *data;
196
197 Data_Get_Struct(self, fcgi_data, data);
198 return data->env;
199 }
200
201 static VALUE fcgi_finish(VALUE self)
202 {
203 fcgi_data *data;
204 fcgi_stream_data *stream_data;
205
206 Data_Get_Struct(self, fcgi_data, data);
207
208 if (Qnil != data->in) {
209 Data_Get_Struct(data->in, fcgi_stream_data, stream_data);
210 stream_data->req = Qnil; stream_data->stream = NULL;
211 }
212 if (Qnil != data->out) {
213 Data_Get_Struct(data->out, fcgi_stream_data, stream_data);
214 stream_data->req = Qnil; stream_data->stream = NULL;
215 }
216 if (Qnil != data->err) {
217 Data_Get_Struct(data->err, fcgi_stream_data, stream_data);
218 stream_data->req = Qnil; stream_data->stream = NULL;
219 }
220
221 data->in = data->out = data->err = Qnil;
222 FCGX_Finish_r(data->req);
223
224 return Qtrue;
225 }
226
227 #define CHECK_STREAM_ERROR(stream) {\
228 int err = FCGX_GetError(stream);\
229 extern int errno; \
230 if (err) {\
231 if (err > 0) {\
232 rb_raise(eFCGIStreamError, "unknown error (syscall error)");\
233 }\
234 else {\
235 switch (err) {\
236 case FCGX_UNSUPPORTED_VERSION:\
237 rb_raise(eFCGIStreamUnsupportedVersionError, "unsupported version");\
238 break;\
239 case FCGX_PROTOCOL_ERROR:\
240 rb_raise(eFCGIStreamProtocolError, "protocol error");\
241 break;\
242 case FCGX_PARAMS_ERROR:\
243 rb_raise(eFCGIStreamProtocolError, "parameter error");\
244 break;\
245 case FCGX_CALL_SEQ_ERROR:\
246 rb_raise(eFCGIStreamCallSeqError, "preconditions are not met");\
247 break;\
248 default:\
249 rb_raise(eFCGIStreamError, "unknown error");\
250 break;\
251 }\
252 }\
253 }\
254 }
255
256 #define Data_Get_Stream(value, stream) do {\
257 fcgi_stream_data* _fsd;\
258 Data_Get_Struct(value, fcgi_stream_data, _fsd);\
259 if (NULL == (stream = _fsd->stream))\
260 rb_raise(eFCGIStreamError, "stream invalid as fastcgi request is already finished");\
261 } while (0)
262
263 static VALUE fcgi_stream_putc(VALUE self, VALUE ch)
264 {
265 FCGX_Stream *stream;
266 int c;
267
268 #if !defined(RUBY_API_VERSION_MAJOR) || (RUBY_API_VERSION_MAJOR < 3)
269 rb_secure(4);
270 #endif
271 Data_Get_Stream(self, stream);
272 if ((c = FCGX_PutChar(NUM2INT(ch), stream)) == EOF)
273 CHECK_STREAM_ERROR(stream);
274 return INT2NUM(c);
275 }
276
277 static VALUE fcgi_stream_write(VALUE self, VALUE str)
278 {
279 FCGX_Stream *stream;
280 int len;
281
282 #if !defined(RUBY_API_VERSION_MAJOR) || (RUBY_API_VERSION_MAJOR < 3)
283 rb_secure(4);
284 #endif
285 Data_Get_Stream(self, stream);
286 str = rb_obj_as_string(str);
287 len = FCGX_PutStr(RSTRING_PTR(str), RSTRING_LEN(str), stream);
288 if (len == EOF) CHECK_STREAM_ERROR(stream);
289 return INT2NUM(len);
290 }
291
292 static VALUE fcgi_stream_print(int argc, VALUE *argv, VALUE out)
293 {
294 int i;
295 VALUE line;
296
297 /* if no argument given, print `$_' */
298 if (argc == 0) {
299 argc = 1;
300 line = rb_lastline_get();
301 argv = &line;
302 }
303 for (i=0; i<argc; i++) {
304 if (!NIL_P(rb_output_fs) && i>0) {
305 fcgi_stream_write(out, rb_output_fs);
306 }
307 switch (TYPE(argv[i])) {
308 case T_NIL:
309 fcgi_stream_write(out, rb_str_new2("nil"));
310 break;
311 default:
312 fcgi_stream_write(out, argv[i]);
313 break;
314 }
315 }
316 if (!NIL_P(rb_output_rs)) {
317 fcgi_stream_write(out, rb_output_rs);
318 }
319
320 return Qnil;
321 }
322
323 static VALUE fcgi_stream_printf(int argc, VALUE *argv, VALUE out)
324 {
325 fcgi_stream_write(out, rb_f_sprintf(argc, argv));
326 return Qnil;
327 }
328
329 static VALUE fcgi_stream_puts _((int, VALUE*, VALUE));
330
331 static VALUE fcgi_stream_puts_ary(VALUE ary, VALUE out, int recur)
332 {
333 VALUE tmp;
334 int i;
335
336 if (recur) {
337 tmp = rb_str_new2("[...]");
338 fcgi_stream_puts(1, &tmp, out);
339 return Qnil;
340 }
341
342 for (i=0; i<RARRAY_LEN(ary); i++) {
343 tmp = RARRAY_PTR(ary)[i];
344 fcgi_stream_puts(1, &tmp, out);
345 }
346 return Qnil;
347 }
348
349 static VALUE fcgi_stream_puts(int argc, VALUE *argv, VALUE out)
350 {
351 int i;
352 VALUE line;
353
354 /* if no argument given, print newline. */
355 if (argc == 0) {
356 fcgi_stream_write(out, rb_default_rs);
357 return Qnil;
358 }
359 for (i=0; i<argc; i++) {
360 switch (TYPE(argv[i])) {
361 case T_NIL:
362 line = rb_str_new2("nil");
363 break;
364 case T_ARRAY:
365 rb_exec_recursive(fcgi_stream_puts_ary, argv[i], out);
366 continue;
367 default:
368 line = argv[i];
369 break;
370 }
371 line = rb_obj_as_string(line);
372 fcgi_stream_write(out, line);
373 if (RSTRING_PTR(line)[RSTRING_LEN(line)-1] != '\n') {
374 fcgi_stream_write(out, rb_default_rs);
375 }
376 }
377
378 return Qnil;
379 }
380
381 static VALUE fcgi_stream_addstr(VALUE out, VALUE str)
382 {
383 fcgi_stream_write(out, str);
384 return out;
385 }
386
387 static VALUE fcgi_stream_flush(VALUE self)
388 {
389 FCGX_Stream *stream;
390
391 Data_Get_Stream(self, stream);
392 if (FCGX_FFlush(stream) == EOF)
393 CHECK_STREAM_ERROR(stream);
394 return Qnil;
395 }
396
397 static VALUE fcgi_stream_getc(VALUE self)
398 {
399 FCGX_Stream *stream;
400 int c;
401
402 Data_Get_Stream(self, stream);
403 if ((c = FCGX_GetChar(stream)) == EOF) {
404 CHECK_STREAM_ERROR(stream);
405 return Qnil;
406 }
407 else {
408 return INT2NUM(c);
409 }
410 }
411
412 static VALUE fcgi_stream_ungetc(VALUE self, VALUE ch)
413 {
414 FCGX_Stream *stream;
415 int c;
416
417 #if !defined(RUBY_API_VERSION_MAJOR) || (RUBY_API_VERSION_MAJOR < 3)
418 if (rb_safe_level() >= 4 && !OBJ_TAINTED(self)) {
419 rb_raise(rb_eSecurityError, "Insecure: operation on untainted IO");
420 }
421 #endif
422 Data_Get_Stream(self, stream);
423 c = FCGX_UnGetChar(NUM2INT(ch), stream);
424 CHECK_STREAM_ERROR(stream);
425 return INT2NUM(c);
426 }
427
428 static VALUE fcgi_stream_gets(VALUE self) {
429 FCGX_Stream *stream;
430 char buff[BUFSIZ];
431 VALUE str = rb_str_new(0,0);
432 OBJ_TAINT(str);
433
434 #if !defined(RUBY_API_VERSION_MAJOR) || (RUBY_API_VERSION_MAJOR < 3)
435 if (rb_safe_level() >= 4 && !OBJ_TAINTED(self)) {
436 rb_raise(rb_eSecurityError, "Insecure: operation on untainted IO");
437 }
438 #endif
439
440 Data_Get_Stream(self, stream);
441
442 for (;;) {
443 if (FCGX_GetLine(buff, BUFSIZ, stream) == NULL) {
444 CHECK_STREAM_ERROR(stream);
445 break;
446 }
447 rb_str_cat(str, buff, strlen(buff));
448 if (strchr(buff, '\n')) break;
449 }
450 if (RSTRING_LEN(str) > 0)
451 return str;
452 else
453 return Qnil;
454 }
455
456 static VALUE fcgi_stream_read(int argc, VALUE *argv, VALUE self)
457 {
458 VALUE num,str;
459 FCGX_Stream *stream;
460 char *buff;
461 int n;
462
463 #if !defined(RUBY_API_VERSION_MAJOR) || (RUBY_API_VERSION_MAJOR < 3)
464 if (rb_safe_level() >= 4 && !OBJ_TAINTED(self)) {
465 rb_raise(rb_eSecurityError, "Insecure: operation on untainted IO");
466 }
467 #endif
468
469 Data_Get_Stream(self, stream);
470
471 if (argc==0) {
472 buff = ALLOC_N(char, 16384);
473 n = FCGX_GetStr(buff, 16384, stream);
474 CHECK_STREAM_ERROR(stream);
475 if (n == 0) {
476 free(buff);
477 return Qnil;
478 }
479 str = rb_str_new(buff, n);
480 OBJ_TAINT(str);
481
482 while(!FCGX_HasSeenEOF(stream)) {
483 n = FCGX_GetStr(buff, 16384, stream);
484 CHECK_STREAM_ERROR(stream);
485 if (n > 0) {
486 rb_str_cat(str, buff, n);
487 } else {
488 free(buff);
489 return Qnil;
490 }
491 }
492 free(buff);
493 return str;
494 }
495
496 num = argv[0];
497 n = NUM2INT(num);
498
499 buff = ALLOC_N(char, n);
500 n = FCGX_GetStr(buff, n, stream);
501 CHECK_STREAM_ERROR(stream);
502 if (n > 0) {
503 str = rb_str_new(buff, n);
504 OBJ_TAINT(str);
505 free(buff);
506 return str;
507 }
508 else {
509 free(buff);
510 return Qnil;
511 }
512 }
513
514 static VALUE fcgi_stream_eof(VALUE self)
515 {
516 FCGX_Stream *stream;
517
518 #if !defined(RUBY_API_VERSION_MAJOR) || (RUBY_API_VERSION_MAJOR < 3)
519 if (rb_safe_level() >= 4 && !OBJ_TAINTED(self)) {
520 rb_raise(rb_eSecurityError, "Insecure: operation on untainted IO");
521 }
522 #endif
523 Data_Get_Stream(self, stream);
524 return FCGX_HasSeenEOF(stream) ? Qtrue : Qfalse;
525 }
526
527 static VALUE fcgi_stream_close(VALUE self)
528 {
529 FCGX_Stream *stream;
530
531 #if !defined(RUBY_API_VERSION_MAJOR) || (RUBY_API_VERSION_MAJOR < 3)
532 if (rb_safe_level() >= 4 && !OBJ_TAINTED(self)) {
533 rb_raise(rb_eSecurityError, "Insecure: can't close");
534 }
535 #endif
536 Data_Get_Stream(self, stream);
537 if (FCGX_FClose(stream) == EOF)
538 CHECK_STREAM_ERROR(stream);
539 return Qnil;
540 }
541
542 static VALUE fcgi_stream_closed(VALUE self)
543 {
544 FCGX_Stream *stream;
545
546 Data_Get_Stream(self, stream);
547 return stream->isClosed ? Qtrue : Qfalse;
548 }
549
550 static VALUE fcgi_stream_binmode(VALUE self)
551 {
552 #if !defined(RUBY_API_VERSION_MAJOR) || (RUBY_API_VERSION_MAJOR < 3)
553 if (rb_safe_level() >= 4 && !OBJ_TAINTED(self)) {
554 rb_raise(rb_eSecurityError, "Insecure: operation on untainted IO");
555 }
556 #endif
557 return self;
558 }
559
560 static VALUE fcgi_stream_isatty(VALUE self)
561 {
562 #if !defined(RUBY_API_VERSION_MAJOR) || (RUBY_API_VERSION_MAJOR < 3)
563 if (rb_safe_level() >= 4 && !OBJ_TAINTED(self)) {
564 rb_raise(rb_eSecurityError, "Insecure: operation on untainted IO");
565 }
566 #endif
567 return Qfalse;
568 }
569
570 static VALUE fcgi_stream_sync(VALUE self)
571 {
572 #if !defined(RUBY_API_VERSION_MAJOR) || (RUBY_API_VERSION_MAJOR < 3)
573 if (rb_safe_level() >= 4 && !OBJ_TAINTED(self)) {
574 rb_raise(rb_eSecurityError, "Insecure: operation on untainted IO");
575 }
576 #endif
577 return Qfalse;
578 }
579
580 static VALUE fcgi_stream_setsync(VALUE self,VALUE sync)
581 {
582 #if !defined(RUBY_API_VERSION_MAJOR) || (RUBY_API_VERSION_MAJOR < 3)
583 if (rb_safe_level() >= 4 && !OBJ_TAINTED(self)) {
584 rb_raise(rb_eSecurityError, "Insecure: operation on untainted IO");
585 }
586 #endif
587 return Qfalse;
588 }
589
590
591
592 void Init_fcgi() {
593
594 FCGX_Init();
595
596 cFCGI = rb_define_class("FCGI", rb_cObject);
597 eFCGIError =rb_define_class_under(cFCGI, "Error", rb_eStandardError);
598 rb_define_singleton_method(cFCGI, "accept", fcgi_s_accept, 0);
599 rb_define_singleton_method(cFCGI, "each", fcgi_s_each, 0);
600 rb_define_singleton_method(cFCGI, "each_request", fcgi_s_each, 0);
601 rb_define_singleton_method(cFCGI, "is_cgi?", fcgi_s_iscgi, 0);
602 rb_define_method(cFCGI, "in", fcgi_in, 0);
603 rb_define_method(cFCGI, "out", fcgi_out, 0);
604 rb_define_method(cFCGI, "err", fcgi_err, 0);
605 rb_define_method(cFCGI, "env", fcgi_env, 0);
606 rb_define_method(cFCGI, "finish", fcgi_finish, 0);
607
608 cFCGIStream = rb_define_class_under(cFCGI, "Stream", rb_cObject);
609 eFCGIStreamError =rb_define_class_under(cFCGIStream, "Error", rb_eStandardError);
610 eFCGIStreamUnsupportedVersionError =
611 rb_define_class_under(cFCGIStream, "UnsupportedVersionError",
612 eFCGIStreamError);
613 eFCGIStreamProtocolError = rb_define_class_under(cFCGIStream, "ProtocolError",
614 eFCGIStreamError);
615 eFCGIStreamParamsError = rb_define_class_under(cFCGIStream, "ParamsError",
616 eFCGIStreamError);
617 eFCGIStreamCallSeqError = rb_define_class_under(cFCGIStream, "CallSeqError",
618 eFCGIStreamError);
619 rb_undef_method(CLASS_OF(cFCGIStream), "new");
620 rb_define_method(cFCGIStream, "putc", fcgi_stream_putc, 1);
621 rb_define_method(cFCGIStream, "write", fcgi_stream_write, 1);
622 rb_define_method(cFCGIStream, "print", fcgi_stream_print, -1);
623 rb_define_method(cFCGIStream, "printf", fcgi_stream_printf, -1);
624 rb_define_method(cFCGIStream, "puts", fcgi_stream_puts, -1);
625 rb_define_method(cFCGIStream, "<<", fcgi_stream_addstr, 1);
626 rb_define_method(cFCGIStream, "flush", fcgi_stream_flush, 0);
627 rb_define_method(cFCGIStream, "getc", fcgi_stream_getc, 0);
628 rb_define_method(cFCGIStream, "ungetc", fcgi_stream_ungetc, 1);
629 rb_define_method(cFCGIStream, "gets", fcgi_stream_gets, 0);
630 rb_define_method(cFCGIStream, "read", fcgi_stream_read, -1);
631 rb_define_method(cFCGIStream, "eof", fcgi_stream_eof, 0);
632 rb_define_method(cFCGIStream, "eof?", fcgi_stream_eof, 0);
633 rb_define_method(cFCGIStream, "close", fcgi_stream_close, 0);
634 rb_define_method(cFCGIStream, "closed?", fcgi_stream_closed, 0);
635 rb_define_method(cFCGIStream, "binmode", fcgi_stream_binmode, 0);
636 rb_define_method(cFCGIStream, "isatty", fcgi_stream_isatty, 0);
637 rb_define_method(cFCGIStream, "tty?", fcgi_stream_isatty, 0);
638 rb_define_method(cFCGIStream, "sync", fcgi_stream_sync, 0);
639 rb_define_method(cFCGIStream, "sync=", fcgi_stream_setsync, 1);
640 }