4 #include "stat_cache.h"
7 #include "connections.h"
10 #include "http_chunk.h"
11 #include "network_backends.h"
15 #include <sys/types.h>
19 # include <winsock2.h>
21 # include <sys/socket.h>
22 # include <sys/wait.h>
23 # include <netinet/in.h>
24 # include <arpa/inet.h>
39 #if defined(O_CLOEXEC) && (!defined(__FreeBSD__) || defined(SOCK_CLOEXEC)) \
40 && !(defined(__APPLE__) && defined(__MACH__))
41 #define pipe_cloexec(pipefd) pipe2((pipefd), O_CLOEXEC)
42 #elif defined FD_CLOEXEC
43 #define pipe_cloexec(pipefd) \
45 && 0 == fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) \
46 && 0 == fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) \
50 #define pipe_cloexec(pipefd) pipe(pipefd)
53 enum {EOL_UNSET
, EOL_N
, EOL_RN
};
70 unsigned short execute_x_only
;
71 unsigned short xsendfile_allow
;
72 array
*xsendfile_docroot
;
79 buffer
*parse_response
;
81 plugin_config
**config_storage
;
90 int fde_ndx
; /* index into the fd-event buffer */
91 int fde_ndx_tocgi
; /* index into the fd-event buffer */
93 connection
*remote_conn
; /* dumb pointer */
94 plugin_data
*plugin_data
; /* dumb pointer */
97 buffer
*response_header
;
101 static handler_ctx
* cgi_handler_ctx_init(void) {
102 handler_ctx
*hctx
= calloc(1, sizeof(*hctx
));
106 hctx
->response
= buffer_init();
107 hctx
->response_header
= buffer_init();
114 static void cgi_handler_ctx_free(handler_ctx
*hctx
) {
115 buffer_free(hctx
->response
);
116 buffer_free(hctx
->response_header
);
121 enum {FDEVENT_HANDLED_UNSET
, FDEVENT_HANDLED_FINISHED
, FDEVENT_HANDLED_NOT_FINISHED
, FDEVENT_HANDLED_COMEBACK
, FDEVENT_HANDLED_ERROR
};
123 INIT_FUNC(mod_cgi_init
) {
126 p
= calloc(1, sizeof(*p
));
130 p
->parse_response
= buffer_init();
136 FREE_FUNC(mod_cgi_free
) {
137 plugin_data
*p
= p_d
;
138 buffer_pid_t
*r
= &(p
->cgi_pid
);
142 if (p
->config_storage
) {
144 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
145 plugin_config
*s
= p
->config_storage
[i
];
147 if (NULL
== s
) continue;
150 array_free(s
->xsendfile_docroot
);
154 free(p
->config_storage
);
158 if (r
->ptr
) free(r
->ptr
);
160 buffer_free(p
->parse_response
);
164 return HANDLER_GO_ON
;
167 SETDEFAULTS_FUNC(mod_fastcgi_set_defaults
) {
168 plugin_data
*p
= p_d
;
171 config_values_t cv
[] = {
172 { "cgi.assign", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
173 { "cgi.execute-x-only", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
174 { "cgi.x-sendfile", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
175 { "cgi.x-sendfile-docroot", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 3 */
176 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
179 if (!p
) return HANDLER_ERROR
;
181 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
182 force_assert(p
->config_storage
);
184 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
185 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
188 s
= calloc(1, sizeof(plugin_config
));
191 s
->cgi
= array_init();
192 s
->execute_x_only
= 0;
193 s
->xsendfile_allow
= 0;
194 s
->xsendfile_docroot
= array_init();
196 cv
[0].destination
= s
->cgi
;
197 cv
[1].destination
= &(s
->execute_x_only
);
198 cv
[2].destination
= &(s
->xsendfile_allow
);
199 cv
[3].destination
= s
->xsendfile_docroot
;
201 p
->config_storage
[i
] = s
;
203 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
204 return HANDLER_ERROR
;
207 if (s
->xsendfile_docroot
->used
) {
209 for (j
= 0; j
< s
->xsendfile_docroot
->used
; ++j
) {
210 data_string
*ds
= (data_string
*)s
->xsendfile_docroot
->data
[j
];
211 if (ds
->type
!= TYPE_STRING
) {
212 log_error_write(srv
, __FILE__
, __LINE__
, "s",
213 "unexpected type for key cgi.x-sendfile-docroot; expected: cgi.x-sendfile-docroot = ( \"/allowed/path\", ... )");
214 return HANDLER_ERROR
;
216 if (ds
->value
->ptr
[0] != '/') {
217 log_error_write(srv
, __FILE__
, __LINE__
, "SBs",
218 "cgi.x-sendfile-docroot paths must begin with '/'; invalid: \"", ds
->value
, "\"");
219 return HANDLER_ERROR
;
221 buffer_path_simplify(ds
->value
, ds
->value
);
222 buffer_append_slash(ds
->value
);
227 return HANDLER_GO_ON
;
231 static int cgi_pid_add(server
*srv
, plugin_data
*p
, pid_t pid
) {
234 buffer_pid_t
*r
= &(p
->cgi_pid
);
238 for (i
= 0; i
< r
->used
; i
++) {
239 if (r
->ptr
[i
] > m
) m
= r
->ptr
[i
];
244 r
->ptr
= malloc(sizeof(*r
->ptr
) * r
->size
);
245 force_assert(r
->ptr
);
246 } else if (r
->used
== r
->size
) {
248 r
->ptr
= realloc(r
->ptr
, sizeof(*r
->ptr
) * r
->size
);
249 force_assert(r
->ptr
);
252 r
->ptr
[r
->used
++] = pid
;
257 static int cgi_pid_del(server
*srv
, plugin_data
*p
, pid_t pid
) {
259 buffer_pid_t
*r
= &(p
->cgi_pid
);
263 for (i
= 0; i
< r
->used
; i
++) {
264 if (r
->ptr
[i
] == pid
) break;
270 if (i
!= r
->used
- 1) {
271 r
->ptr
[i
] = r
->ptr
[r
->used
- 1];
279 static int cgi_response_parse(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*in
) {
286 buffer_copy_buffer(p
->parse_response
, in
);
288 for (s
= p
->parse_response
->ptr
;
289 NULL
!= (ns
= strchr(s
, '\n'));
290 s
= ns
+ 1, line
++) {
291 const char *key
, *value
;
298 if (ns
> s
&& ns
[-1] == '\r') ns
[-1] = '\0';
301 0 == strncmp(s
, "HTTP/1.", 7)) {
302 /* non-parsed header ... we parse them anyway */
308 /* after the space should be a status code for us */
310 status
= strtol(s
+9, NULL
, 10);
314 /* we expected 3 digits and didn't got them */
315 con
->parsed_response
|= HTTP_STATUS
;
316 con
->http_status
= status
;
320 /* parse the headers */
322 if (NULL
== (value
= strchr(s
, ':'))) {
323 /* we expect: "<key>: <value>\r\n" */
327 key_len
= value
- key
;
331 while (*value
== ' ' || *value
== '\t') value
++;
333 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->response
.headers
, TYPE_STRING
))) {
334 ds
= data_response_init();
336 buffer_copy_string_len(ds
->key
, key
, key_len
);
337 buffer_copy_string(ds
->value
, value
);
339 array_insert_unique(con
->response
.headers
, (data_unset
*)ds
);
343 if (0 == strncasecmp(key
, "Date", key_len
)) {
344 con
->parsed_response
|= HTTP_DATE
;
348 if (0 == strncasecmp(key
, "Status", key_len
)) {
349 int status
= strtol(value
, NULL
, 10);
350 if (status
>= 100 && status
< 1000) {
351 con
->http_status
= status
;
352 con
->parsed_response
|= HTTP_STATUS
;
354 con
->http_status
= 502;
359 if (0 == strncasecmp(key
, "Location", key_len
)) {
360 con
->parsed_response
|= HTTP_LOCATION
;
364 if (0 == strncasecmp(key
, "Connection", key_len
)) {
365 con
->response
.keep_alive
= (0 == strcasecmp(value
, "Keep-Alive")) ? 1 : 0;
366 con
->parsed_response
|= HTTP_CONNECTION
;
370 if (0 == strncasecmp(key
, "Content-Length", key_len
)) {
371 con
->response
.content_length
= strtoul(value
, NULL
, 10);
372 con
->parsed_response
|= HTTP_CONTENT_LENGTH
;
381 /* CGI/1.1 rev 03 - 7.2.1.2 */
382 if ((con
->parsed_response
& HTTP_LOCATION
) &&
383 !(con
->parsed_response
& HTTP_STATUS
)) {
384 con
->http_status
= 302;
391 static int cgi_demux_response(server
*srv
, handler_ctx
*hctx
) {
392 plugin_data
*p
= hctx
->plugin_data
;
393 connection
*con
= hctx
->remote_conn
;
400 buffer_string_prepare_copy(hctx
->response
, 4 * 1024);
402 if (ioctl(con
->fd
, FIONREAD
, &toread
) || toread
<= 4*1024) {
403 buffer_string_prepare_copy(hctx
->response
, 4 * 1024);
405 if (toread
> MAX_READ_LIMIT
) toread
= MAX_READ_LIMIT
;
406 buffer_string_prepare_copy(hctx
->response
, toread
);
410 if (-1 == (n
= read(hctx
->fd
, hctx
->response
->ptr
, hctx
->response
->size
- 1))) {
411 if (errno
== EAGAIN
|| errno
== EINTR
) {
412 /* would block, wait for signal */
413 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
414 return FDEVENT_HANDLED_NOT_FINISHED
;
417 log_error_write(srv
, __FILE__
, __LINE__
, "sdd", strerror(errno
), con
->fd
, hctx
->fd
);
418 return FDEVENT_HANDLED_ERROR
;
423 return FDEVENT_HANDLED_FINISHED
;
426 buffer_commit(hctx
->response
, n
);
428 /* split header from body */
430 if (con
->file_started
== 0) {
432 int is_header_end
= 0;
434 size_t i
, header_len
;
436 buffer_append_string_buffer(hctx
->response_header
, hctx
->response
);
439 * we have to handle a few cases:
452 * and different mixes of \n and \r\n combinations
454 * Some users also forget about CGI and just send a response and hope
455 * we handle it. No headers, no header-content seperator
459 /* nph (non-parsed headers) */
460 if (0 == strncmp(hctx
->response_header
->ptr
, "HTTP/1.", 7)) is_header
= 1;
462 header_len
= buffer_string_length(hctx
->response_header
);
463 for (i
= 0; !is_header_end
&& i
< header_len
; i
++) {
464 char c
= hctx
->response_header
->ptr
[i
];
470 * looks like we have a normal header
476 if (is_header
== 0) {
477 /* we got a EOL but we don't seem to got a HTTP header */
485 * check if we saw a \n(\r)?\n sequence
488 ((i
- last_eol
== 1) ||
489 (i
- last_eol
== 2 && hctx
->response_header
->ptr
[i
- 1] == '\r'))) {
502 /* no header, but a body */
503 if (0 != http_chunk_append_buffer(srv
, con
, hctx
->response_header
)) {
504 return FDEVENT_HANDLED_ERROR
;
510 /* the body starts after the EOL */
511 bstart
= hctx
->response_header
->ptr
+ i
;
512 blen
= header_len
- i
;
515 * i still points to the char after the terminating EOL EOL
517 * put it on the last \n again
521 /* string the last \r?\n */
522 if (i
> 0 && (hctx
->response_header
->ptr
[i
- 1] == '\r')) {
526 buffer_string_set_length(hctx
->response_header
, i
);
528 /* parse the response header */
529 cgi_response_parse(srv
, con
, p
, hctx
->response_header
);
531 if (con
->http_status
>= 300 && con
->http_status
< 400) {
532 /*(con->parsed_response & HTTP_LOCATION)*/
534 if (NULL
!= (ds
= (data_string
*) array_get_element(con
->response
.headers
, "Location"))
535 && ds
->value
->ptr
[0] == '/') {
536 if (++con
->loops_per_request
> 5) {
537 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "too many internal loops while processing request:", con
->request
.orig_uri
);
538 con
->http_status
= 500; /* Internal Server Error */
540 return FDEVENT_HANDLED_FINISHED
;
543 buffer_copy_buffer(con
->request
.uri
, ds
->value
);
545 if (con
->request
.content_length
) {
546 if ((off_t
)con
->request
.content_length
!= chunkqueue_length(con
->request_content_queue
)) {
549 con
->request
.content_length
= 0;
550 chunkqueue_reset(con
->request_content_queue
);
553 if (con
->http_status
!= 307 && con
->http_status
!= 308) {
554 /* Note: request body (if any) sent to initial dynamic handler
555 * and is not available to the internal redirect */
556 con
->request
.http_method
= HTTP_METHOD_GET
;
559 connection_response_reset(srv
, con
); /*(includes con->http_status = 0)*/
562 return FDEVENT_HANDLED_COMEBACK
;
566 if (hctx
->conf
.xsendfile_allow
) {
568 if (NULL
!= (ds
= (data_string
*) array_get_element(con
->response
.headers
, "X-Sendfile"))) {
569 http_response_xsendfile(srv
, con
, ds
->value
, hctx
->conf
.xsendfile_docroot
);
570 return FDEVENT_HANDLED_FINISHED
;
575 if (0 != http_chunk_append_mem(srv
, con
, bstart
, blen
)) {
576 return FDEVENT_HANDLED_ERROR
;
581 con
->file_started
= 1;
583 /*(reuse MAX_HTTP_REQUEST_HEADER as max size for response headers from backends)*/
584 if (header_len
> MAX_HTTP_REQUEST_HEADER
) {
585 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "response headers too large for", con
->uri
.path
);
586 con
->http_status
= 502; /* Bad Gateway */
588 return FDEVENT_HANDLED_FINISHED
;
592 if (0 != http_chunk_append_buffer(srv
, con
, hctx
->response
)) {
593 return FDEVENT_HANDLED_ERROR
;
595 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)
596 && chunkqueue_length(con
->write_queue
) > 65536 - 4096) {
597 if (!con
->is_writable
) {
598 /*(defer removal of FDEVENT_IN interest since
599 * connection_state_machine() might be able to send data
600 * immediately, unless !con->is_writable, where
601 * connection_state_machine() might not loop back to call
602 * mod_cgi_handle_subrequest())*/
603 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
610 log_error_write(srv
, __FILE__
, __LINE__
, "ddss", con
->fd
, hctx
->fd
, connection_get_state(con
->state
), b
->ptr
);
614 return FDEVENT_HANDLED_NOT_FINISHED
;
617 static void cgi_connection_close_fdtocgi(server
*srv
, handler_ctx
*hctx
) {
618 /*(closes only hctx->fdtocgi)*/
619 fdevent_event_del(srv
->ev
, &(hctx
->fde_ndx_tocgi
), hctx
->fdtocgi
);
620 fdevent_unregister(srv
->ev
, hctx
->fdtocgi
);
621 fdevent_sched_close(srv
->ev
, hctx
->fdtocgi
, 0);
625 static void cgi_connection_close(server
*srv
, handler_ctx
*hctx
) {
628 plugin_data
*p
= hctx
->plugin_data
;
629 connection
*con
= hctx
->remote_conn
;
633 /* the connection to the browser went away, but we still have a connection
636 * close cgi-connection
639 if (hctx
->fd
!= -1) {
640 /* close connection to the cgi-script */
641 fdevent_event_del(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
);
642 fdevent_unregister(srv
->ev
, hctx
->fd
);
643 fdevent_sched_close(srv
->ev
, hctx
->fd
, 0);
646 if (hctx
->fdtocgi
!= -1) {
647 cgi_connection_close_fdtocgi(srv
, hctx
); /*(closes only hctx->fdtocgi)*/
652 con
->plugin_ctx
[p
->id
] = NULL
;
654 cgi_handler_ctx_free(hctx
);
656 /* if waitpid hasn't been called by response.c yet, do it here */
658 /* check if the CGI-script is already gone */
659 switch(waitpid(pid
, &status
, WNOHANG
)) {
661 /* not finished yet */
663 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "(debug) child isn't done yet, pid:", pid
);
668 if (errno
== EINTR
) break;
671 * errno == ECHILD happens if _subrequest catches the process-status before
672 * we have read the response of the cgi process
677 * -> we get here with waitpid == ECHILD
680 if (errno
!= ECHILD
) {
681 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "waitpid failed: ", strerror(errno
));
683 /* anyway: don't wait for it anymore */
687 if (WIFEXITED(status
)) {
689 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "(debug) cgi exited fine, pid:", pid
);
692 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "cgi died, pid:", pid
);
702 /* cgi-script is still alive, queue the PID for removal */
703 cgi_pid_add(srv
, p
, pid
);
708 /* finish response (if not already con->file_started, con->file_finished) */
709 if (con
->mode
== p
->id
) {
710 http_response_backend_done(srv
, con
);
714 static handler_t
cgi_connection_close_callback(server
*srv
, connection
*con
, void *p_d
) {
715 plugin_data
*p
= p_d
;
716 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
717 if (hctx
) cgi_connection_close(srv
, hctx
);
719 return HANDLER_GO_ON
;
723 static int cgi_write_request(server
*srv
, handler_ctx
*hctx
, int fd
);
726 static handler_t
cgi_handle_fdevent_send (server
*srv
, void *ctx
, int revents
) {
727 handler_ctx
*hctx
= ctx
;
728 connection
*con
= hctx
->remote_conn
;
730 /*(joblist only actually necessary here in mod_cgi fdevent send if returning HANDLER_ERROR)*/
731 joblist_append(srv
, con
);
733 if (revents
& FDEVENT_OUT
) {
734 if (0 != cgi_write_request(srv
, hctx
, hctx
->fdtocgi
)) {
735 cgi_connection_close(srv
, hctx
);
736 return HANDLER_ERROR
;
738 /* more request body to be sent to CGI */
741 if (revents
& FDEVENT_HUP
) {
742 /* skip sending remaining data to CGI */
743 if (con
->request
.content_length
) {
744 chunkqueue
*cq
= con
->request_content_queue
;
745 chunkqueue_mark_written(cq
, chunkqueue_length(cq
));
746 if (cq
->bytes_in
!= (off_t
)con
->request
.content_length
) {
751 cgi_connection_close_fdtocgi(srv
, hctx
); /*(closes only hctx->fdtocgi)*/
752 } else if (revents
& FDEVENT_ERR
) {
753 /* kill all connections to the cgi process */
755 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cgi-FDEVENT_ERR");
757 cgi_connection_close(srv
, hctx
);
758 return HANDLER_ERROR
;
761 return HANDLER_FINISHED
;
765 static int cgi_recv_response(server
*srv
, handler_ctx
*hctx
) {
766 switch (cgi_demux_response(srv
, hctx
)) {
767 case FDEVENT_HANDLED_NOT_FINISHED
:
769 case FDEVENT_HANDLED_FINISHED
:
773 log_error_write(srv
, __FILE__
, __LINE__
, "ddss", con
->fd
, hctx
->fd
, connection_get_state(con
->state
), "finished");
775 cgi_connection_close(srv
, hctx
);
777 /* if we get a IN|HUP and have read everything don't exec the close twice */
778 return HANDLER_FINISHED
;
779 case FDEVENT_HANDLED_COMEBACK
:
780 cgi_connection_close(srv
, hctx
);
781 return HANDLER_COMEBACK
;
782 case FDEVENT_HANDLED_ERROR
:
783 log_error_write(srv
, __FILE__
, __LINE__
, "s", "demuxer failed: ");
785 cgi_connection_close(srv
, hctx
);
786 return HANDLER_FINISHED
;
789 return HANDLER_GO_ON
;
793 static handler_t
cgi_handle_fdevent(server
*srv
, void *ctx
, int revents
) {
794 handler_ctx
*hctx
= ctx
;
795 connection
*con
= hctx
->remote_conn
;
797 joblist_append(srv
, con
);
799 if (revents
& FDEVENT_IN
) {
800 handler_t rc
= cgi_recv_response(srv
, hctx
);/*(might invalidate hctx)*/
801 if (rc
!= HANDLER_GO_ON
) return rc
; /*(unless HANDLER_GO_ON)*/
804 /* perhaps this issue is already handled */
805 if (revents
& FDEVENT_HUP
) {
806 if (con
->file_started
) {
807 /* drain any remaining data from kernel pipe buffers
808 * even if (con->conf.stream_response_body
809 * & FDEVENT_STREAM_RESPONSE_BUFMIN)
810 * since event loop will spin on fd FDEVENT_HUP event
811 * until unregistered. */
814 rc
= cgi_recv_response(srv
,hctx
);/*(might invalidate hctx)*/
815 } while (rc
== HANDLER_GO_ON
); /*(unless HANDLER_GO_ON)*/
816 return rc
; /* HANDLER_FINISHED or HANDLER_COMEBACK or HANDLER_ERROR */
817 } else if (!buffer_string_is_empty(hctx
->response_header
)) {
818 /* unfinished header package which is a body in reality */
819 con
->file_started
= 1;
820 if (0 != http_chunk_append_buffer(srv
, con
, hctx
->response_header
)) {
821 cgi_connection_close(srv
, hctx
);
822 return HANDLER_ERROR
;
826 log_error_write(srv
, __FILE__
, __LINE__
, "sddd", "got HUP from cgi", con
->fd
, hctx
->fd
, revents
);
829 cgi_connection_close(srv
, hctx
);
830 } else if (revents
& FDEVENT_ERR
) {
831 /* kill all connections to the cgi process */
832 cgi_connection_close(srv
, hctx
);
834 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cgi-FDEVENT_ERR");
836 return HANDLER_ERROR
;
839 return HANDLER_FINISHED
;
843 static int cgi_env_add(void *venv
, const char *key
, size_t key_len
, const char *val
, size_t val_len
) {
844 char_array
*env
= venv
;
847 if (!key
|| !val
) return -1;
849 dst
= malloc(key_len
+ val_len
+ 2);
851 memcpy(dst
, key
, key_len
);
853 memcpy(dst
+ key_len
+ 1, val
, val_len
);
854 dst
[key_len
+ 1 + val_len
] = '\0';
856 if (env
->size
== 0) {
858 env
->ptr
= malloc(env
->size
* sizeof(*env
->ptr
));
859 force_assert(env
->ptr
);
860 } else if (env
->size
== env
->used
) {
862 env
->ptr
= realloc(env
->ptr
, env
->size
* sizeof(*env
->ptr
));
863 force_assert(env
->ptr
);
866 env
->ptr
[env
->used
++] = dst
;
871 /* returns: 0: continue, -1: fatal error, -2: connection reset */
872 /* similar to network_write_file_chunk_mmap, but doesn't use send on windows (because we're on pipes),
873 * also mmaps and sends complete chunk instead of only small parts - the files
874 * are supposed to be temp files with reasonable chunk sizes.
876 * Also always use mmap; the files are "trusted", as we created them.
878 static ssize_t
cgi_write_file_chunk_mmap(server
*srv
, connection
*con
, int fd
, chunkqueue
*cq
) {
879 chunk
* const c
= cq
->first
;
880 off_t offset
, toSend
, file_end
;
882 size_t mmap_offset
, mmap_avail
;
885 force_assert(NULL
!= c
);
886 force_assert(FILE_CHUNK
== c
->type
);
887 force_assert(c
->offset
>= 0 && c
->offset
<= c
->file
.length
);
889 offset
= c
->file
.start
+ c
->offset
;
890 toSend
= c
->file
.length
- c
->offset
;
891 file_end
= c
->file
.start
+ c
->file
.length
; /* offset to file end in this chunk */
894 chunkqueue_remove_finished_chunks(cq
);
898 if (0 != network_open_file_chunk(srv
, con
, cq
)) return -1;
900 /* (re)mmap the buffer if range is not covered completely */
901 if (MAP_FAILED
== c
->file
.mmap
.start
902 || offset
< c
->file
.mmap
.offset
903 || file_end
> (off_t
)(c
->file
.mmap
.offset
+ c
->file
.mmap
.length
)) {
905 if (MAP_FAILED
!= c
->file
.mmap
.start
) {
906 munmap(c
->file
.mmap
.start
, c
->file
.mmap
.length
);
907 c
->file
.mmap
.start
= MAP_FAILED
;
910 c
->file
.mmap
.offset
= mmap_align_offset(offset
);
911 c
->file
.mmap
.length
= file_end
- c
->file
.mmap
.offset
;
913 if (MAP_FAILED
== (c
->file
.mmap
.start
= mmap(NULL
, c
->file
.mmap
.length
, PROT_READ
, MAP_PRIVATE
, c
->file
.fd
, c
->file
.mmap
.offset
))) {
914 if (toSend
> 65536) toSend
= 65536;
915 data
= malloc(toSend
);
917 if (-1 == lseek(c
->file
.fd
, offset
, SEEK_SET
)
918 || 0 >= (toSend
= read(c
->file
.fd
, data
, toSend
))) {
920 log_error_write(srv
, __FILE__
, __LINE__
, "ssbdo", "lseek/read failed:",
921 strerror(errno
), c
->file
.name
, c
->file
.fd
, offset
);
922 } else { /*(0 == toSend)*/
923 log_error_write(srv
, __FILE__
, __LINE__
, "sbdo", "unexpected EOF (input truncated?):",
924 c
->file
.name
, c
->file
.fd
, offset
);
932 if (MAP_FAILED
!= c
->file
.mmap
.start
) {
933 force_assert(offset
>= c
->file
.mmap
.offset
);
934 mmap_offset
= offset
- c
->file
.mmap
.offset
;
935 force_assert(c
->file
.mmap
.length
> mmap_offset
);
936 mmap_avail
= c
->file
.mmap
.length
- mmap_offset
;
937 force_assert(toSend
<= (off_t
) mmap_avail
);
939 data
= c
->file
.mmap
.start
+ mmap_offset
;
942 r
= write(fd
, data
, toSend
);
944 if (MAP_FAILED
== c
->file
.mmap
.start
) free(data
);
955 log_error_write(srv
, __FILE__
, __LINE__
, "ssd",
956 "write failed:", strerror(errno
), fd
);
962 chunkqueue_mark_written(cq
, r
);
968 static int cgi_write_request(server
*srv
, handler_ctx
*hctx
, int fd
) {
969 connection
*con
= hctx
->remote_conn
;
970 chunkqueue
*cq
= con
->request_content_queue
;
973 /* old comment: windows doesn't support select() on pipes - wouldn't be easy to fix for all platforms.
974 * solution: if this is still a problem on windows, then substitute
975 * socketpair() for pipe() and closesocket() for close() on windows.
978 for (c
= cq
->first
; c
; c
= cq
->first
) {
983 r
= cgi_write_file_chunk_mmap(srv
, con
, fd
, cq
);
987 if ((r
= write(fd
, c
->mem
->ptr
+ c
->offset
, buffer_string_length(c
->mem
) - c
->offset
)) < 0) {
991 /* ignore and try again */
996 /* connection closed */
1001 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "write failed due to: ", strerror(errno
));
1006 chunkqueue_mark_written(cq
, r
);
1011 if (0 == r
) break; /*(might block)*/
1018 /* connection reset */
1019 log_error_write(srv
, __FILE__
, __LINE__
, "s", "failed to send post data to cgi, connection closed by CGI");
1020 /* skip all remaining data */
1021 chunkqueue_mark_written(cq
, chunkqueue_length(cq
));
1028 if (cq
->bytes_out
== (off_t
)con
->request
.content_length
) {
1029 /* sent all request body input */
1030 /* close connection to the cgi-script */
1031 if (-1 == hctx
->fdtocgi
) { /*(received request body sent in initial send to pipe buffer)*/
1034 log_error_write(srv
, __FILE__
, __LINE__
, "sds", "cgi stdin close failed ", fd
, strerror(errno
));
1037 cgi_connection_close_fdtocgi(srv
, hctx
); /*(closes only hctx->fdtocgi)*/
1040 off_t cqlen
= cq
->bytes_in
- cq
->bytes_out
;
1041 if (cq
->bytes_in
< (off_t
)con
->request
.content_length
&& cqlen
< 65536 - 16384) {
1042 /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
1043 if (!(con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_POLLIN
)) {
1044 con
->conf
.stream_request_body
|= FDEVENT_STREAM_REQUEST_POLLIN
;
1045 con
->is_readable
= 1; /* trigger optimistic read from client */
1048 if (-1 == hctx
->fdtocgi
) { /*(not registered yet)*/
1050 hctx
->fde_ndx_tocgi
= -1;
1051 fdevent_register(srv
->ev
, hctx
->fdtocgi
, cgi_handle_fdevent_send
, hctx
);
1053 if (0 == cqlen
) { /*(chunkqueue_is_empty(cq))*/
1054 if ((fdevent_event_get_interest(srv
->ev
, hctx
->fdtocgi
) & FDEVENT_OUT
)) {
1055 fdevent_event_set(srv
->ev
, &(hctx
->fde_ndx_tocgi
), hctx
->fdtocgi
, 0);
1058 /* more request body remains to be sent to CGI so register for fdevents */
1059 fdevent_event_set(srv
->ev
, &(hctx
->fde_ndx_tocgi
), hctx
->fdtocgi
, FDEVENT_OUT
);
1066 static int cgi_create_env(server
*srv
, connection
*con
, plugin_data
*p
, handler_ctx
*hctx
, buffer
*cgi_handler
) {
1070 int from_cgi_fds
[2];
1076 if (!buffer_string_is_empty(cgi_handler
)) {
1077 /* stat the exec file */
1078 if (-1 == (stat(cgi_handler
->ptr
, &st
))) {
1079 log_error_write(srv
, __FILE__
, __LINE__
, "sbss",
1080 "stat for cgi-handler", cgi_handler
,
1081 "failed:", strerror(errno
));
1086 if (pipe_cloexec(to_cgi_fds
)) {
1087 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "pipe failed:", strerror(errno
));
1091 if (pipe_cloexec(from_cgi_fds
)) {
1092 close(to_cgi_fds
[0]);
1093 close(to_cgi_fds
[1]);
1094 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "pipe failed:", strerror(errno
));
1099 switch (pid
= fork()) {
1108 http_cgi_opts opts
= { 0, 0, NULL
, NULL
};
1110 /* move stdout to from_cgi_fd[1] */
1111 dup2(from_cgi_fds
[1], STDOUT_FILENO
);
1113 close(from_cgi_fds
[1]);
1115 close(from_cgi_fds
[0]);
1118 /* move the stdin to to_cgi_fd[0] */
1119 dup2(to_cgi_fds
[0], STDIN_FILENO
);
1121 close(to_cgi_fds
[0]);
1123 close(to_cgi_fds
[1]);
1126 /* create environment */
1131 http_cgi_headers(srv
, con
, &opts
, cgi_env_add
, &env
);
1134 if (NULL
!= (s
= getenv("LD_PRELOAD"))) {
1135 cgi_env_add(&env
, CONST_STR_LEN("LD_PRELOAD"), s
, strlen(s
));
1138 if (NULL
!= (s
= getenv("LD_LIBRARY_PATH"))) {
1139 cgi_env_add(&env
, CONST_STR_LEN("LD_LIBRARY_PATH"), s
, strlen(s
));
1142 /* CYGWIN needs SYSTEMROOT */
1143 if (NULL
!= (s
= getenv("SYSTEMROOT"))) {
1144 cgi_env_add(&env
, CONST_STR_LEN("SYSTEMROOT"), s
, strlen(s
));
1148 if (env
.size
== env
.used
) {
1150 env
.ptr
= realloc(env
.ptr
, env
.size
* sizeof(*env
.ptr
));
1153 env
.ptr
[env
.used
] = NULL
;
1157 args
= malloc(sizeof(*args
) * argc
);
1161 if (!buffer_string_is_empty(cgi_handler
)) {
1162 args
[i
++] = cgi_handler
->ptr
;
1164 args
[i
++] = con
->physical
.path
->ptr
;
1167 /* search for the last / */
1168 if (NULL
!= (c
= strrchr(con
->physical
.path
->ptr
, '/'))) {
1169 /* handle special case of file in root directory */
1170 const char* physdir
= (c
== con
->physical
.path
->ptr
) ? "/" : con
->physical
.path
->ptr
;
1172 /* temporarily shorten con->physical.path to directory without terminating '/' */
1174 /* change to the physical directory */
1175 if (-1 == chdir(physdir
)) {
1176 log_error_write(srv
, __FILE__
, __LINE__
, "ssb", "chdir failed:", strerror(errno
), con
->physical
.path
);
1181 /* we don't need the client socket */
1182 for (i
= 3; i
< 256; i
++) {
1183 if (i
!= srv
->errorlog_fd
) close(i
);
1187 execve(args
[0], args
, env
.ptr
);
1189 /* most log files may have been closed/redirected by this point,
1190 * though stderr might still point to lighttpd.breakage.log */
1196 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fork failed:", strerror(errno
));
1197 close(from_cgi_fds
[0]);
1198 close(from_cgi_fds
[1]);
1199 close(to_cgi_fds
[0]);
1200 close(to_cgi_fds
[1]);
1203 /* parent process */
1205 close(from_cgi_fds
[1]);
1206 close(to_cgi_fds
[0]);
1208 /* register PID and wait for them asynchronously */
1211 hctx
->fd
= from_cgi_fds
[0];
1216 if (0 == con
->request
.content_length
) {
1217 close(to_cgi_fds
[1]);
1219 /* there is content to send */
1220 if (-1 == fdevent_fcntl_set_nb(srv
->ev
, to_cgi_fds
[1])) {
1221 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fcntl failed: ", strerror(errno
));
1222 close(to_cgi_fds
[1]);
1223 cgi_connection_close(srv
, hctx
);
1227 if (0 != cgi_write_request(srv
, hctx
, to_cgi_fds
[1])) {
1228 close(to_cgi_fds
[1]);
1229 cgi_connection_close(srv
, hctx
);
1236 fdevent_register(srv
->ev
, hctx
->fd
, cgi_handle_fdevent
, hctx
);
1237 if (-1 == fdevent_fcntl_set_nb(srv
->ev
, hctx
->fd
)) {
1238 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fcntl failed: ", strerror(errno
));
1239 cgi_connection_close(srv
, hctx
);
1242 fdevent_event_set(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
1254 static buffer
* cgi_get_handler(array
*a
, buffer
*fn
) {
1255 size_t k
, s_len
= buffer_string_length(fn
);
1256 for (k
= 0; k
< a
->used
; ++k
) {
1257 data_string
*ds
= (data_string
*)a
->data
[k
];
1258 size_t ct_len
= buffer_string_length(ds
->key
);
1260 if (buffer_is_empty(ds
->key
)) continue;
1261 if (s_len
< ct_len
) continue;
1263 if (0 == strncmp(fn
->ptr
+ s_len
- ct_len
, ds
->key
->ptr
, ct_len
)) {
1273 static int mod_cgi_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
1275 plugin_config
*s
= p
->config_storage
[0];
1278 PATCH(execute_x_only
);
1279 PATCH(xsendfile_allow
);
1280 PATCH(xsendfile_docroot
);
1282 /* skip the first, the global context */
1283 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
1284 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
1285 s
= p
->config_storage
[i
];
1287 /* condition didn't match */
1288 if (!config_check_cond(srv
, con
, dc
)) continue;
1291 for (j
= 0; j
< dc
->value
->used
; j
++) {
1292 data_unset
*du
= dc
->value
->data
[j
];
1294 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cgi.assign"))) {
1296 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cgi.execute-x-only"))) {
1297 PATCH(execute_x_only
);
1298 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cgi.x-sendfile"))) {
1299 PATCH(xsendfile_allow
);
1300 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cgi.x-sendfile-docroot"))) {
1301 PATCH(xsendfile_docroot
);
1310 URIHANDLER_FUNC(cgi_is_handled
) {
1311 plugin_data
*p
= p_d
;
1312 buffer
*fn
= con
->physical
.path
;
1313 stat_cache_entry
*sce
= NULL
;
1317 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
1319 if (buffer_is_empty(fn
)) return HANDLER_GO_ON
;
1321 mod_cgi_patch_connection(srv
, con
, p
);
1323 if (HANDLER_ERROR
!= stat_cache_get_entry(srv
, con
, con
->physical
.path
, &sce
)) {
1326 /* CGI might be executable even if it is not readable
1327 * (stat_cache_get_entry() currently checks file is readable)*/
1328 if (0 != stat(con
->physical
.path
->ptr
, &stbuf
)) return HANDLER_GO_ON
;
1332 if (!S_ISREG(st
->st_mode
)) return HANDLER_GO_ON
;
1333 if (p
->conf
.execute_x_only
== 1 && (st
->st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) == 0) return HANDLER_GO_ON
;
1335 if (NULL
!= cgi_get_handler(p
->conf
.cgi
, fn
)) {
1336 handler_ctx
*hctx
= cgi_handler_ctx_init();
1337 hctx
->remote_conn
= con
;
1338 hctx
->plugin_data
= p
;
1339 memcpy(&hctx
->conf
, &p
->conf
, sizeof(plugin_config
));
1340 con
->plugin_ctx
[p
->id
] = hctx
;
1344 return HANDLER_GO_ON
;
1347 TRIGGER_FUNC(cgi_trigger
) {
1348 plugin_data
*p
= p_d
;
1350 /* the trigger handle only cares about lonely PID which we have to wait for */
1353 for (ndx
= 0; ndx
< p
->cgi_pid
.used
; ndx
++) {
1356 switch(waitpid(p
->cgi_pid
.ptr
[ndx
], &status
, WNOHANG
)) {
1358 /* not finished yet */
1360 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "(debug) child isn't done yet, pid:", p
->cgi_pid
.ptr
[ndx
]);
1364 if (errno
== ECHILD
) {
1365 /* someone else called waitpid... remove the pid to stop looping the error each time */
1366 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cgi child vanished, probably someone else called waitpid");
1368 cgi_pid_del(srv
, p
, p
->cgi_pid
.ptr
[ndx
]);
1373 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "waitpid failed: ", strerror(errno
));
1375 return HANDLER_ERROR
;
1378 if (WIFEXITED(status
)) {
1380 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "(debug) cgi exited fine, pid:", p
->cgi_pid
.ptr
[ndx
]);
1382 } else if (WIFSIGNALED(status
)) {
1383 /* FIXME: what if we killed the CGI script with a kill(..., SIGTERM) ?
1385 if (WTERMSIG(status
) != SIGTERM
) {
1386 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "cleaning up CGI: process died with signal", WTERMSIG(status
));
1389 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cleaning up CGI: ended unexpectedly");
1392 cgi_pid_del(srv
, p
, p
->cgi_pid
.ptr
[ndx
]);
1393 /* del modified the buffer structure
1394 * and copies the last entry to the current one
1395 * -> recheck the current index
1401 return HANDLER_GO_ON
;
1405 * - HANDLER_GO_ON : not our job
1406 * - HANDLER_FINISHED: got response
1407 * - HANDLER_WAIT_FOR_EVENT: waiting for response
1409 SUBREQUEST_FUNC(mod_cgi_handle_subrequest
) {
1410 plugin_data
*p
= p_d
;
1411 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
1412 chunkqueue
*cq
= con
->request_content_queue
;
1414 if (con
->mode
!= p
->id
) return HANDLER_GO_ON
;
1415 if (NULL
== hctx
) return HANDLER_GO_ON
;
1417 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)
1418 && con
->file_started
) {
1419 if (chunkqueue_length(con
->write_queue
) > 65536 - 4096) {
1420 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
1421 } else if (!(fdevent_event_get_interest(srv
->ev
, hctx
->fd
) & FDEVENT_IN
)) {
1422 /* optimistic read from backend, which might re-enable FDEVENT_IN */
1423 handler_t rc
= cgi_recv_response(srv
, hctx
); /*(might invalidate hctx)*/
1424 if (rc
!= HANDLER_GO_ON
) return rc
; /*(unless HANDLER_GO_ON)*/
1428 if (cq
->bytes_in
!= (off_t
)con
->request
.content_length
) {
1429 /*(64k - 4k to attempt to avoid temporary files
1430 * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
1431 if (cq
->bytes_in
- cq
->bytes_out
> 65536 - 4096
1432 && (con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_BUFMIN
)){
1433 con
->conf
.stream_request_body
&= ~FDEVENT_STREAM_REQUEST_POLLIN
;
1434 if (-1 != hctx
->fd
) return HANDLER_WAIT_FOR_EVENT
;
1436 handler_t r
= connection_handle_read_post_state(srv
, con
);
1437 if (!chunkqueue_is_empty(cq
)) {
1438 if (fdevent_event_get_interest(srv
->ev
, hctx
->fdtocgi
) & FDEVENT_OUT
) {
1439 return (r
== HANDLER_GO_ON
) ? HANDLER_WAIT_FOR_EVENT
: r
;
1442 if (r
!= HANDLER_GO_ON
) return r
;
1446 if (-1 == hctx
->fd
) {
1447 buffer
*handler
= cgi_get_handler(hctx
->conf
.cgi
, con
->physical
.path
);
1448 if (!handler
) return HANDLER_GO_ON
; /*(should not happen; checked in cgi_is_handled())*/
1449 if (cgi_create_env(srv
, con
, p
, hctx
, handler
)) {
1450 con
->http_status
= 500;
1453 return HANDLER_FINISHED
;
1456 log_error_write(srv
, __FILE__
, __LINE__
, "sdd", "subrequest, pid =", hctx
, hctx
->pid
);
1458 } else if (!chunkqueue_is_empty(con
->request_content_queue
)) {
1459 if (0 != cgi_write_request(srv
, hctx
, hctx
->fdtocgi
)) {
1460 cgi_connection_close(srv
, hctx
);
1461 return HANDLER_ERROR
;
1465 /* if not done, wait for CGI to close stdout, so we read EOF on pipe */
1466 return HANDLER_WAIT_FOR_EVENT
;
1470 int mod_cgi_plugin_init(plugin
*p
);
1471 int mod_cgi_plugin_init(plugin
*p
) {
1472 p
->version
= LIGHTTPD_VERSION_ID
;
1473 p
->name
= buffer_init_string("cgi");
1475 p
->connection_reset
= cgi_connection_close_callback
;
1476 p
->handle_subrequest_start
= cgi_is_handled
;
1477 p
->handle_subrequest
= mod_cgi_handle_subrequest
;
1478 p
->handle_trigger
= cgi_trigger
;
1479 p
->init
= mod_cgi_init
;
1480 p
->cleanup
= mod_cgi_free
;
1481 p
->set_defaults
= mod_fastcgi_set_defaults
;