xref: /php-src/sapi/litespeed/lsapi_main.c (revision afc57381)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: George Wang <gwang@litespeedtech.com>                        |
14    +----------------------------------------------------------------------+
15 */
16 
17 #include "php.h"
18 #include "SAPI.h"
19 #include "php_main.h"
20 #include "php_ini.h"
21 #include "php_variables.h"
22 #include "zend_highlight.h"
23 #include "zend_portability.h"
24 #include "zend.h"
25 #include "ext/standard/basic_functions.h"
26 #include "ext/standard/info.h"
27 #include "ext/standard/head.h"
28 #include "lsapilib.h"
29 #include "lsapi_main_arginfo.h"
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 
38 #include <sys/wait.h>
39 #include <sys/stat.h>
40 
41 #ifdef HAVE_SYS_TYPES_H
42 #include <sys/types.h>
43 #endif
44 
45 #include <signal.h>
46 #include <sys/socket.h>
47 #include <arpa/inet.h>
48 #include <netinet/in.h>
49 #include <sys/time.h>
50 
51 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
52 #include "lscriu.c"
53 #endif
54 
55 #define SAPI_LSAPI_MAX_HEADER_LENGTH LSAPI_RESP_HTTP_HEADER_MAX
56 
57 /* Key for each cache entry is dirname(PATH_TRANSLATED).
58  *
59  * NOTE: Each cache entry config_hash contains the combination from all user ini files found in
60  *       the path starting from doc_root through to dirname(PATH_TRANSLATED).  There is no point
61  *       storing per-file entries as it would not be possible to detect added / deleted entries
62  *       between separate files.
63  */
64 typedef struct _user_config_cache_entry {
65     time_t expires;
66     HashTable user_config;
67 } user_config_cache_entry;
68 static HashTable user_config_cache;
69 
70 static int  lsapi_mode       = 0;
71 static char *php_self        = "";
72 static char *script_filename = "";
73 static int  source_highlight = 0;
74 static int  ignore_php_ini   = 0;
75 static char * argv0 = NULL;
76 static int  engine = 1;
77 static int  parse_user_ini   = 0;
78 
79 #ifdef ZTS
80 zend_compiler_globals    *compiler_globals;
81 zend_executor_globals    *executor_globals;
82 php_core_globals         *core_globals;
83 sapi_globals_struct      *sapi_globals;
84 #endif
85 
86 zend_module_entry litespeed_module_entry;
87 
init_sapi_from_env(sapi_module_struct * sapi_module)88 static void init_sapi_from_env(sapi_module_struct *sapi_module)
89 {
90     char *p;
91     p = getenv("LSPHPRC");
92     if (p)
93         sapi_module->php_ini_path_override = p;
94 }
95 
96 /* {{{ php_lsapi_startup */
php_lsapi_startup(sapi_module_struct * sapi_module)97 static int php_lsapi_startup(sapi_module_struct *sapi_module)
98 {
99     if (php_module_startup(sapi_module, NULL)==FAILURE) {
100         return FAILURE;
101     }
102     argv0 = sapi_module->executable_location;
103     return SUCCESS;
104 }
105 /* }}} */
106 
107 /* {{{ sapi_lsapi_ini_defaults */
108 
109 /* overwritable ini defaults must be set in sapi_cli_ini_defaults() */
110 #define INI_DEFAULT(name,value)\
111     ZVAL_STRING(tmp, value, 0);\
112     zend_hash_update(configuration_hash, name, sizeof(name), tmp, sizeof(zval), (void**)&entry);\
113     Z_STRVAL_P(entry) = zend_strndup(Z_STRVAL_P(entry), Z_STRLEN_P(entry))
114 
sapi_lsapi_ini_defaults(HashTable * configuration_hash)115 static void sapi_lsapi_ini_defaults(HashTable *configuration_hash)
116 {
117 #if PHP_MAJOR_VERSION > 4
118 /*
119     zval *tmp, *entry;
120 
121     MAKE_STD_ZVAL(tmp);
122 
123     INI_DEFAULT("register_long_arrays", "0");
124 
125     FREE_ZVAL(tmp);
126 */
127 #endif
128 
129 }
130 /* }}} */
131 
132 
133 /* {{{ sapi_lsapi_ub_write */
sapi_lsapi_ub_write(const char * str,size_t str_length)134 static size_t sapi_lsapi_ub_write(const char *str, size_t str_length)
135 {
136     int ret;
137     int remain;
138     if ( lsapi_mode ) {
139         ret  = LSAPI_Write( str, str_length );
140         if ( ret < str_length ) {
141             php_handle_aborted_connection();
142             return str_length - ret;
143         }
144     } else {
145         remain = str_length;
146         while( remain > 0 ) {
147             ret = write( 1, str, remain );
148             if ( ret <= 0 ) {
149                 php_handle_aborted_connection();
150                 return str_length - remain;
151             }
152             str += ret;
153             remain -= ret;
154         }
155     }
156     return str_length;
157 }
158 /* }}} */
159 
160 
161 /* {{{ sapi_lsapi_flush */
sapi_lsapi_flush(void * server_context)162 static void sapi_lsapi_flush(void * server_context)
163 {
164     if ( lsapi_mode ) {
165         if ( LSAPI_Flush() == -1) {
166             php_handle_aborted_connection();
167         }
168     }
169 }
170 /* }}} */
171 
172 
173 /* {{{ sapi_lsapi_deactivate */
sapi_lsapi_deactivate(void)174 static int sapi_lsapi_deactivate(void)
175 {
176     if ( SG(request_info).path_translated ) {
177         efree( SG(request_info).path_translated );
178         SG(request_info).path_translated = NULL;
179     }
180 
181     return SUCCESS;
182 }
183 /* }}} */
184 
185 
186 
187 
188 /* {{{ sapi_lsapi_getenv */
sapi_lsapi_getenv(const char * name,size_t name_len)189 static char *sapi_lsapi_getenv(const char * name, size_t name_len )
190 {
191     if ( lsapi_mode ) {
192         return LSAPI_GetEnv( name );
193     } else {
194         return getenv( name );
195     }
196 }
197 /* }}} */
198 
199 
add_variable(const char * pKey,int keyLen,const char * pValue,int valLen,void * arg)200 static int add_variable( const char * pKey, int keyLen, const char * pValue, int valLen,
201                          void * arg )
202 {
203     int filter_arg = (Z_ARR_P((zval *)arg) == Z_ARR(PG(http_globals)[TRACK_VARS_ENV]))
204         ? PARSE_ENV : PARSE_SERVER;
205     char * new_val = (char *) pValue;
206     size_t new_val_len;
207 
208     if (sapi_module.input_filter(filter_arg, (char *)pKey, &new_val, valLen, &new_val_len)) {
209         php_register_variable_safe((char *)pKey, new_val, new_val_len, (zval *)arg );
210     }
211     return 1;
212 }
213 
litespeed_php_import_environment_variables(zval * array_ptr)214 static void litespeed_php_import_environment_variables(zval *array_ptr)
215 {
216     char buf[128];
217     char **env, *p, *t = buf;
218     size_t alloc_size = sizeof(buf);
219     unsigned long nlen; /* ptrdiff_t is not portable */
220 
221     if (Z_TYPE(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY &&
222         Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_ENV]) &&
223         zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_ENV])) > 0
224     ) {
225         zval_ptr_dtor_nogc(array_ptr);
226         ZVAL_DUP(array_ptr, &PG(http_globals)[TRACK_VARS_ENV]);
227         return;
228     } else if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY &&
229         Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_SERVER]) &&
230         zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER])) > 0
231     ) {
232         zval_ptr_dtor_nogc(array_ptr);
233         ZVAL_DUP(array_ptr, &PG(http_globals)[TRACK_VARS_SERVER]);
234         return;
235     }
236 
237     tsrm_env_lock();
238     for (env = environ; env != NULL && *env != NULL; env++) {
239         p = strchr(*env, '=');
240         if (!p) {               /* malformed entry? */
241             continue;
242         }
243         nlen = p - *env;
244         if (nlen >= alloc_size) {
245             alloc_size = nlen + 64;
246             t = (t == buf ? emalloc(alloc_size): erealloc(t, alloc_size));
247         }
248         memcpy(t, *env, nlen);
249         t[nlen] = '\0';
250         add_variable(t, nlen, p + 1, strlen( p + 1 ), array_ptr);
251     }
252     tsrm_env_unlock();
253     if (t != buf && t != NULL) {
254         efree(t);
255     }
256 }
257 
258 /* {{{ sapi_lsapi_register_variables */
sapi_lsapi_register_variables(zval * track_vars_array)259 static void sapi_lsapi_register_variables(zval *track_vars_array)
260 {
261     char * php_self = "";
262     if ( lsapi_mode ) {
263         if ( (SG(request_info).request_uri ) )
264             php_self = (SG(request_info).request_uri );
265 
266         litespeed_php_import_environment_variables(track_vars_array);
267 
268         LSAPI_ForeachHeader( add_variable, track_vars_array );
269         LSAPI_ForeachEnv( add_variable, track_vars_array );
270         add_variable("PHP_SELF", 8, php_self, strlen( php_self ), track_vars_array );
271     } else {
272         php_import_environment_variables(track_vars_array);
273 
274         php_register_variable("PHP_SELF", php_self, track_vars_array);
275         php_register_variable("SCRIPT_NAME", php_self, track_vars_array);
276         php_register_variable("SCRIPT_FILENAME", script_filename, track_vars_array);
277         php_register_variable("PATH_TRANSLATED", script_filename, track_vars_array);
278         php_register_variable("DOCUMENT_ROOT", "", track_vars_array);
279 
280     }
281 }
282 /* }}} */
283 
284 
285 /* {{{ sapi_lsapi_read_post */
sapi_lsapi_read_post(char * buffer,size_t count_bytes)286 static size_t sapi_lsapi_read_post(char *buffer, size_t count_bytes)
287 {
288     if ( lsapi_mode ) {
289         ssize_t rv = LSAPI_ReadReqBody(buffer, (unsigned long long)count_bytes);
290         return (rv >= 0) ? (size_t)rv : 0;
291     } else {
292         return 0;
293     }
294 }
295 /* }}} */
296 
297 
298 
299 
300 /* {{{ sapi_lsapi_read_cookies */
sapi_lsapi_read_cookies(void)301 static char *sapi_lsapi_read_cookies(void)
302 {
303     if ( lsapi_mode ) {
304         return LSAPI_GetHeader( H_COOKIE );
305     } else {
306         return NULL;
307     }
308 }
309 /* }}} */
310 
311 
312 typedef struct _http_error {
313   int code;
314   const char* msg;
315 } http_error;
316 
317 static const http_error http_error_codes[] = {
318        {100, "Continue"},
319        {101, "Switching Protocols"},
320        {200, "OK"},
321        {201, "Created"},
322        {202, "Accepted"},
323        {203, "Non-Authoritative Information"},
324        {204, "No Content"},
325        {205, "Reset Content"},
326        {206, "Partial Content"},
327        {300, "Multiple Choices"},
328        {301, "Moved Permanently"},
329        {302, "Moved Temporarily"},
330        {303, "See Other"},
331        {304, "Not Modified"},
332        {305, "Use Proxy"},
333        {400, "Bad Request"},
334        {401, "Unauthorized"},
335        {402, "Payment Required"},
336        {403, "Forbidden"},
337        {404, "Not Found"},
338        {405, "Method Not Allowed"},
339        {406, "Not Acceptable"},
340        {407, "Proxy Authentication Required"},
341        {408, "Request Time-out"},
342        {409, "Conflict"},
343        {410, "Gone"},
344        {411, "Length Required"},
345        {412, "Precondition Failed"},
346        {413, "Request Entity Too Large"},
347        {414, "Request-URI Too Large"},
348        {415, "Unsupported Media Type"},
349        {428, "Precondition Required"},
350        {429, "Too Many Requests"},
351        {431, "Request Header Fields Too Large"},
352        {451, "Unavailable For Legal Reasons"},
353        {500, "Internal Server Error"},
354        {501, "Not Implemented"},
355        {502, "Bad Gateway"},
356        {503, "Service Unavailable"},
357        {504, "Gateway Time-out"},
358        {505, "HTTP Version not supported"},
359        {511, "Network Authentication Required"},
360        {0,   NULL}
361 };
362 
363 
sapi_lsapi_send_headers_like_cgi(sapi_headers_struct * sapi_headers)364 static int sapi_lsapi_send_headers_like_cgi(sapi_headers_struct *sapi_headers)
365 {
366     char buf[SAPI_LSAPI_MAX_HEADER_LENGTH];
367     sapi_header_struct *h;
368     zend_llist_position pos;
369     bool ignore_status = 0;
370     int response_status = SG(sapi_headers).http_response_code;
371 
372     if (SG(request_info).no_headers == 1) {
373         LSAPI_FinalizeRespHeaders();
374         return SAPI_HEADER_SENT_SUCCESSFULLY;
375     }
376 
377     if (SG(sapi_headers).http_response_code != 200)
378     {
379         int len;
380         bool has_status = 0;
381 
382         char *s;
383 
384         if (SG(sapi_headers).http_status_line &&
385              (s = strchr(SG(sapi_headers).http_status_line, ' ')) != 0 &&
386              (s - SG(sapi_headers).http_status_line) >= 5 &&
387              strncasecmp(SG(sapi_headers).http_status_line, "HTTP/", 5) == 0
388         ) {
389             len = slprintf(buf, sizeof(buf), "Status:%s", s);
390             response_status = atoi((s + 1));
391         } else {
392             h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
393             while (h) {
394                 if (h->header_len > sizeof("Status:")-1 &&
395                     strncasecmp(h->header, "Status:", sizeof("Status:")-1) == 0
396                 ) {
397                     has_status = 1;
398                     break;
399                 }
400                 h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
401             }
402             if (!has_status) {
403                 http_error *err = (http_error*)http_error_codes;
404 
405                 while (err->code != 0) {
406                     if (err->code == SG(sapi_headers).http_response_code) {
407                         break;
408                     }
409                     err++;
410                 }
411                 if (err->msg) {
412                     len = slprintf(buf, sizeof(buf), "Status: %d %s", SG(sapi_headers).http_response_code, err->msg);
413                 } else {
414                     len = slprintf(buf, sizeof(buf), "Status: %d", SG(sapi_headers).http_response_code);
415                 }
416             }
417         }
418 
419         if (!has_status) {
420             LSAPI_AppendRespHeader( buf, len );
421             ignore_status = 1;
422         }
423     }
424 
425     h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
426     while (h) {
427         /* prevent CRLFCRLF */
428         if (h->header_len) {
429             if (h->header_len > sizeof("Status:")-1 &&
430                 strncasecmp(h->header, "Status:", sizeof("Status:")-1) == 0
431             ) {
432                 if (!ignore_status) {
433                     ignore_status = 1;
434                     LSAPI_AppendRespHeader(h->header, h->header_len);
435                 }
436             } else if (response_status == 304 && h->header_len > sizeof("Content-Type:")-1 &&
437                        strncasecmp(h->header, "Content-Type:", sizeof("Content-Type:")-1) == 0
438                    ) {
439                 h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
440                 continue;
441             } else {
442                 LSAPI_AppendRespHeader(h->header, h->header_len);
443             }
444         }
445         h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
446     }
447 
448     LSAPI_FinalizeRespHeaders();
449     return SAPI_HEADER_SENT_SUCCESSFULLY;
450 }
451 
452 /*
453     mod_lsapi mode or legacy LS mode
454 */
455 static int mod_lsapi_mode = 0;
456 
457 
458 /* {{{ sapi_lsapi_send_headers */
sapi_lsapi_send_headers(sapi_headers_struct * sapi_headers)459 static int sapi_lsapi_send_headers(sapi_headers_struct *sapi_headers)
460 {
461     sapi_header_struct  *h;
462     zend_llist_position pos;
463 
464     if ( mod_lsapi_mode ) {
465         /* mod_lsapi mode */
466         return sapi_lsapi_send_headers_like_cgi(sapi_headers);
467     }
468 
469     /* Legacy mode */
470     if ( lsapi_mode ) {
471         LSAPI_SetRespStatus( SG(sapi_headers).http_response_code );
472 
473         h = zend_llist_get_first_ex(&sapi_headers->headers, &pos);
474         while (h) {
475             if ( h->header_len > 0 ) {
476                 LSAPI_AppendRespHeader(h->header, h->header_len);
477             }
478             h = zend_llist_get_next_ex(&sapi_headers->headers, &pos);
479         }
480         if (SG(sapi_headers).send_default_content_type) {
481             char    *hd;
482             int     len;
483             char    headerBuf[SAPI_LSAPI_MAX_HEADER_LENGTH];
484 
485             hd = sapi_get_default_content_type();
486             len = snprintf( headerBuf, SAPI_LSAPI_MAX_HEADER_LENGTH - 1,
487                             "Content-type: %s", hd );
488             efree(hd);
489 
490             LSAPI_AppendRespHeader( headerBuf, len );
491         }
492     }
493     LSAPI_FinalizeRespHeaders();
494     return SAPI_HEADER_SENT_SUCCESSFULLY;
495 
496 
497 }
498 /* }}} */
499 
500 
501 /* {{{ sapi_lsapi_send_headers */
sapi_lsapi_log_message(const char * message,int syslog_type_int)502 static void sapi_lsapi_log_message(const char *message, int syslog_type_int)
503 {
504     char buf[8192];
505     int len = strlen( message );
506     if ( *(message + len - 1 ) != '\n' )
507     {
508         snprintf( buf, 8191, "%s\n", message );
509         message = buf;
510         if (len > 8191)
511             len = 8191;
512         ++len;
513     }
514     LSAPI_Write_Stderr( message, len );
515 }
516 /* }}} */
517 
518 /* Set to 1 to turn on log messages useful during development:
519  */
520 #if 0
521 static void log_message (const char *fmt, ...)
522 {
523     va_list ap;
524     va_start(ap, fmt);
525     char buf[0x100];
526     vsnprintf(buf, sizeof(buf), fmt, ap);
527     va_end(ap);
528     sapi_lsapi_log_message(buf
529 #if PHP_MAJOR_VERSION > 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION >= 1)
530                                , 0
531 #endif
532                                   );
533 }
534 #define DEBUG_MESSAGE(fmt, ...) log_message("LS:%d " fmt "\n", __LINE__, ##__VA_ARGS__)
535 #else
536 #define DEBUG_MESSAGE(fmt, ...)
537 #endif
538 
539 static int lsapi_activate_user_ini(void);
540 
sapi_lsapi_activate(void)541 static int sapi_lsapi_activate(void)
542 {
543     char *path, *server_name;
544     size_t path_len, server_name_len;
545 
546     /* PATH_TRANSLATED should be defined at this stage but better safe than sorry :) */
547     if (!SG(request_info).path_translated) {
548             return FAILURE;
549     }
550 
551     if (php_ini_has_per_host_config()) {
552         server_name = sapi_lsapi_getenv("SERVER_NAME", 0);
553         /* SERVER_NAME should also be defined at this stage..but better check it anyway */
554         if (server_name) {
555                 server_name_len = strlen(server_name);
556                 server_name = estrndup(server_name, server_name_len);
557                 zend_str_tolower(server_name, server_name_len);
558                 php_ini_activate_per_host_config(server_name, server_name_len);
559                 efree(server_name);
560         }
561     }
562 
563     if (php_ini_has_per_dir_config()) {
564         /* Prepare search path */
565         path_len = strlen(SG(request_info).path_translated);
566 
567         /* Make sure we have trailing slash! */
568         if (!IS_SLASH(SG(request_info).path_translated[path_len])) {
569             path = emalloc(path_len + 2);
570             memcpy(path, SG(request_info).path_translated, path_len + 1);
571             path_len = zend_dirname(path, path_len);
572             path[path_len++] = DEFAULT_SLASH;
573         } else {
574             path = estrndup(SG(request_info).path_translated, path_len);
575             path_len = zend_dirname(path, path_len);
576         }
577         path[path_len] = 0;
578 
579         /* Activate per-dir-system-configuration defined in php.ini and stored into configuration_hash during startup */
580         php_ini_activate_per_dir_config(path, path_len); /* Note: for global settings sake we check from root to path */
581 
582         efree(path);
583     }
584 
585     if (parse_user_ini && lsapi_activate_user_ini() == FAILURE) {
586         return FAILURE;
587     }
588     return SUCCESS;
589 }
590 /* {{{ sapi_module_struct cgi_sapi_module */
591 static sapi_module_struct lsapi_sapi_module =
592 {
593     "litespeed",
594     "LiteSpeed V7.9",
595 
596     php_lsapi_startup,              /* startup */
597     php_module_shutdown_wrapper,    /* shutdown */
598 
599     sapi_lsapi_activate,            /* activate */
600     sapi_lsapi_deactivate,          /* deactivate */
601 
602     sapi_lsapi_ub_write,            /* unbuffered write */
603     sapi_lsapi_flush,               /* flush */
604     NULL,                           /* get uid */
605     sapi_lsapi_getenv,              /* getenv */
606 
607     php_error,                      /* error handler */
608 
609     NULL,                           /* header handler */
610     sapi_lsapi_send_headers,        /* send headers handler */
611     NULL,                           /* send header handler */
612 
613     sapi_lsapi_read_post,           /* read POST data */
614     sapi_lsapi_read_cookies,        /* read Cookies */
615 
616     sapi_lsapi_register_variables,  /* register server variables */
617     sapi_lsapi_log_message,         /* Log message */
618     NULL,                           /* Get request time */
619     NULL,                           /* Child terminate */
620 
621     STANDARD_SAPI_MODULE_PROPERTIES
622 
623 };
624 /* }}} */
625 
init_request_info(void)626 static void init_request_info( void )
627 {
628     char * pContentType = LSAPI_GetHeader( H_CONTENT_TYPE );
629     char * pAuth;
630 
631     SG(request_info).content_type = pContentType ? pContentType : "";
632     SG(request_info).request_method = LSAPI_GetRequestMethod();
633     SG(request_info).query_string = LSAPI_GetQueryString();
634     SG(request_info).request_uri = LSAPI_GetScriptName();
635     SG(request_info).content_length = LSAPI_GetReqBodyLen();
636     SG(request_info).path_translated = estrdup( LSAPI_GetScriptFileName());
637 
638     /* It is not reset by zend engine, set it to 200. */
639     SG(sapi_headers).http_response_code = 200;
640 
641     pAuth = LSAPI_GetHeader( H_AUTHORIZATION );
642     php_handle_auth_data(pAuth);
643 }
644 
lsapi_execute_script(void)645 static int lsapi_execute_script(void)
646 {
647 	zend_file_handle file_handle;
648     char *p;
649     int len;
650 	zend_stream_init_filename(&file_handle, SG(request_info).path_translated);
651 	file_handle.primary_script = true;
652 
653     p = argv0;
654     *p++ = ':';
655     len = strlen( SG(request_info).path_translated );
656     if ( len > 45 )
657         len = len - 45;
658     else
659         len = 0;
660     memccpy( p, SG(request_info).path_translated + len, 0, 46 );
661 
662     php_execute_script(&file_handle);
663     zend_destroy_file_handle(&file_handle);
664     return 0;
665 
666 }
667 
lsapi_sigsegv(int signal)668 static void lsapi_sigsegv( int signal )
669 {
670     //fprintf(stderr, "lsapi_sigsegv: %d: Segmentation violation signal is caught during request shutdown\n", getpid());
671     _exit(1);
672 }
673 
674 static int do_clean_shutdown = 1;
675 
676 static int clean_onexit = 1;
677 
678 
lsapi_clean_shutdown(void)679 static void lsapi_clean_shutdown(void)
680 {
681     struct sigaction act;
682     struct itimerval tmv;
683 #if PHP_MAJOR_VERSION >= 7
684     zend_string * key;
685 #endif
686     clean_onexit = 1;
687     sigemptyset(&act.sa_mask);
688     act.sa_flags = 0;
689     act.sa_handler = lsapi_sigsegv;
690     (void)sigaction(SIGINT,  &act, NULL);
691     (void)sigaction(SIGQUIT, &act, NULL);
692     (void)sigaction(SIGILL,  &act, NULL);
693     (void)sigaction(SIGABRT, &act, NULL);
694     (void)sigaction(SIGBUS,  &act, NULL);
695     (void)sigaction(SIGSEGV, &act, NULL);
696     (void)sigaction(SIGTERM, &act, NULL);
697     (void)sigaction(SIGPROF, &act, NULL);
698     memset(&tmv, 0, sizeof(struct itimerval));
699     tmv.it_value.tv_sec = 0;
700     tmv.it_value.tv_usec = 100000;
701     setitimer(ITIMER_PROF, &tmv, NULL);
702 
703 #if PHP_MAJOR_VERSION >= 7
704     key = ZSTR_INIT_LITERAL("error_reporting", 1);
705     zend_alter_ini_entry_chars_ex(key, "0", 1,
706                         PHP_INI_SYSTEM, PHP_INI_STAGE_SHUTDOWN, 1);
707     zend_string_release(key);
708 #else
709     zend_alter_ini_entry("error_reporting", 16, "0", 1,
710                         PHP_INI_SYSTEM, PHP_INI_STAGE_SHUTDOWN);
711 #endif
712 
713     zend_try {
714         php_request_shutdown(NULL);
715     } zend_end_try();
716 }
717 
lsapi_sigterm(int signal)718 static void lsapi_sigterm(int signal)
719 {
720 
721     // fprintf(stderr, "lsapi_sigterm: %d: clean_onexit %d\n", getpid(), clean_onexit );
722     if(!clean_onexit)
723     {
724         lsapi_clean_shutdown();
725     }
726     exit(1);
727 }
728 
lsapi_atexit(void)729 static void lsapi_atexit(void)
730 {
731     //fprintf(stderr, "lsapi_atexit: %d: clean_onexit %d\n", getpid(), clean_onexit );
732     if(!clean_onexit)
733     {
734         lsapi_clean_shutdown();
735     }
736 }
737 
lsapi_module_main(int show_source)738 static int lsapi_module_main(int show_source)
739 {
740     struct sigaction act;
741     if (php_request_startup() == FAILURE ) {
742         return -1;
743     }
744 
745     if (do_clean_shutdown) {
746         sigemptyset(&act.sa_mask);
747         act.sa_flags = SA_NODEFER;
748         act.sa_handler = lsapi_sigterm;
749         (void)sigaction( SIGINT, &act, NULL);
750         (void)sigaction( SIGQUIT, &act, NULL);
751         (void)sigaction( SIGILL, &act, NULL);
752         (void)sigaction( SIGABRT, &act, NULL);
753         (void)sigaction( SIGBUS, &act, NULL);
754         (void)sigaction( SIGSEGV, &act, NULL);
755         (void)sigaction( SIGTERM, &act, NULL);
756 
757         clean_onexit = 0;
758     }
759 
760     if (show_source) {
761         zend_syntax_highlighter_ini syntax_highlighter_ini;
762 
763         php_get_highlight_struct(&syntax_highlighter_ini);
764         highlight_file(SG(request_info).path_translated, &syntax_highlighter_ini);
765     } else {
766         lsapi_execute_script();
767     }
768     zend_try {
769         php_request_shutdown(NULL);
770 
771         clean_onexit = 1;
772 
773         memset( argv0, 0, 46 );
774     } zend_end_try();
775     return 0;
776 }
777 
778 
alter_ini(const char * pKey,int keyLen,const char * pValue,int valLen,void * arg)779 static int alter_ini( const char * pKey, int keyLen, const char * pValue, int valLen,
780                 void * arg )
781 {
782     zend_string * psKey;
783 
784     int type = ZEND_INI_PERDIR;
785     int stage = PHP_INI_STAGE_RUNTIME;
786     if ( '\001' == *pKey ) {
787         ++pKey;
788         if ( *pKey == 4 ) {
789             type = ZEND_INI_SYSTEM;
790             /*
791               Use ACTIVATE stage in legacy mode only.
792 
793               RUNTIME stage should be used here,
794               as with ACTIVATE it's impossible to change the option from script with ini_set
795             */
796             if(!mod_lsapi_mode)
797             {
798                 stage = PHP_INI_STAGE_ACTIVATE;
799             }
800         }
801         else
802         {
803             stage = PHP_INI_STAGE_HTACCESS;
804         }
805         ++pKey;
806         --keyLen;
807         if (( keyLen == 7 )&&( strncasecmp( pKey, "engine", 6 )== 0 ))
808         {
809             if ( *pValue == '0' )
810                 engine = 0;
811         }
812         else
813         {
814             --keyLen;
815             psKey = zend_string_init(pKey, keyLen, 1);
816             zend_alter_ini_entry_chars(psKey,
817                              (char *)pValue, valLen,
818                              type, stage);
819             zend_string_release_ex(psKey, 1);
820         }
821     }
822     return 1;
823 }
824 
user_config_cache_entry_dtor(zval * el)825 static void user_config_cache_entry_dtor(zval *el)
826 {
827     user_config_cache_entry *entry = (user_config_cache_entry *)Z_PTR_P(el);
828     zend_hash_destroy(&entry->user_config);
829     free(entry);
830 }
831 
user_config_cache_init(void)832 static void user_config_cache_init(void)
833 {
834     zend_hash_init(&user_config_cache, 0, NULL, user_config_cache_entry_dtor, 1);
835 }
836 
pathlen_without_trailing_slash(char * path)837 static int pathlen_without_trailing_slash(char *path)
838 {
839     int len = (int)strlen(path);
840     while (len > 1 && /* mind "/" as root dir */
841            path[len-1] == DEFAULT_SLASH)
842     {
843         --len;
844     }
845     return len;
846 }
847 
skip_slash(char * s)848 static inline char* skip_slash(char *s)
849 {
850     while (*s == DEFAULT_SLASH) {
851         ++s;
852     }
853     return s;
854 }
855 
856 /**
857  * Walk down the path_stop starting at path_start.
858  *
859  * If path_start = "/path1" and path_stop = "/path1/path2/path3"
860  * the callback will be called 3 times with the next args:
861  *
862  *   1. "/path1/path2/path3"
863  *             ^ end
864  *       ^ start
865  *   2. "/path1/path2/path3"
866  *                   ^ end
867  *       ^ start
868  *   3. "/path1/path2/path3"
869  *                         ^ end
870  *       ^ start
871  *
872  * path_stop has to be a subdir of path_start
873  * or to be path_start itself.
874  *
875  * Both path args have to be absolute.
876  * Trailing slashes are allowed.
877  * NULL or empty string args are not allowed.
878  */
walk_down_the_path(char * path_start,char * path_stop,void (* cb)(char * begin,char * end,void * data),void * data)879 static void walk_down_the_path(char* path_start,
880                                char* path_stop,
881                                void (*cb)(char* begin,
882                                           char* end,
883                                           void* data),
884                                void* data)
885 {
886     char *pos = path_stop + pathlen_without_trailing_slash(path_start);
887     cb(path_stop, pos, data);
888 
889     while ((pos = skip_slash(pos))[0]) {
890         pos = strchr(pos, DEFAULT_SLASH);
891         if (!pos) {
892             /* The last token without trailing slash
893              */
894             cb(path_stop, path_stop + strlen(path_stop), data);
895             return;
896         }
897         cb(path_stop, pos, data);
898     }
899 }
900 
901 
902 typedef struct {
903     char *path;
904     uint32_t path_len;
905     char *doc_root;
906     user_config_cache_entry *entry;
907 } _lsapi_activate_user_ini_ctx;
908 
909 typedef int (*fn_activate_user_ini_chain_t)
910         (_lsapi_activate_user_ini_ctx *ctx, void* next);
911 
lsapi_activate_user_ini_basic_checks(_lsapi_activate_user_ini_ctx * ctx,void * next)912 static int lsapi_activate_user_ini_basic_checks(_lsapi_activate_user_ini_ctx *ctx,
913                                                 void* next)
914 {
915     int rc = SUCCESS;
916     fn_activate_user_ini_chain_t *fn_next = next;
917 
918     if (!PG(user_ini_filename) || !*PG(user_ini_filename)) {
919         return SUCCESS;
920     }
921 
922     /* PATH_TRANSLATED should be defined at this stage */
923     ctx->path = SG(request_info).path_translated;
924     if (!ctx->path || !*ctx->path) {
925         return FAILURE;
926     }
927 
928     ctx->doc_root = sapi_lsapi_getenv("DOCUMENT_ROOT", 0);
929     DEBUG_MESSAGE("doc_root: %s", ctx->doc_root);
930 
931     if (*fn_next) {
932         rc = (*fn_next)(ctx, fn_next + 1);
933     }
934 
935     return rc;
936 }
937 
lsapi_activate_user_ini_mk_path(_lsapi_activate_user_ini_ctx * ctx,void * next)938 static int lsapi_activate_user_ini_mk_path(_lsapi_activate_user_ini_ctx *ctx,
939                                            void* next)
940 {
941     char *path;
942     int rc = SUCCESS;
943     fn_activate_user_ini_chain_t *fn_next = next;
944 
945     /* Extract dir name from path_translated * and store it in 'path' */
946     ctx->path_len = strlen(ctx->path);
947     path = ctx->path = estrndup(SG(request_info).path_translated, ctx->path_len);
948     ctx->path_len = zend_dirname(path, ctx->path_len);
949 
950     if (*fn_next) {
951         rc = (*fn_next)(ctx, fn_next + 1);
952     }
953 
954     efree(path);
955     return rc;
956 }
957 
lsapi_activate_user_ini_mk_realpath(_lsapi_activate_user_ini_ctx * ctx,void * next)958 static int lsapi_activate_user_ini_mk_realpath(_lsapi_activate_user_ini_ctx *ctx,
959                                                void* next)
960 {
961     char *real_path;
962     int rc = SUCCESS;
963     fn_activate_user_ini_chain_t *fn_next = next;
964 
965     if (!IS_ABSOLUTE_PATH(ctx->path, ctx->path_len)) {
966         real_path = tsrm_realpath(ctx->path, NULL);
967         if (!real_path) {
968             return SUCCESS;
969         }
970         ctx->path = real_path;
971         ctx->path_len = strlen(ctx->path);
972     } else {
973         real_path = NULL;
974     }
975 
976     if (*fn_next) {
977         rc = (*fn_next)(ctx, fn_next + 1);
978     }
979 
980     if (real_path)
981         efree(real_path);
982     return rc;
983 }
984 
lsapi_activate_user_ini_mk_user_config(_lsapi_activate_user_ini_ctx * ctx,void * next)985 static int lsapi_activate_user_ini_mk_user_config(_lsapi_activate_user_ini_ctx *ctx,
986                                                   void* next)
987 {
988     fn_activate_user_ini_chain_t *fn_next = next;
989 
990     /* Find cached config entry: If not found, create one */
991     ctx->entry = zend_hash_str_find_ptr(&user_config_cache, ctx->path, ctx->path_len);
992 
993     if (!ctx->entry)
994     {
995         ctx->entry = pemalloc(sizeof(user_config_cache_entry), 1);
996         ctx->entry->expires = 0;
997         zend_hash_init(&ctx->entry->user_config, 0, NULL,
998                        config_zval_dtor, 1);
999         zend_hash_str_update_ptr(&user_config_cache, ctx->path,
1000                                             ctx->path_len, ctx->entry);
1001     }
1002 
1003     if (*fn_next) {
1004         return (*fn_next)(ctx, fn_next + 1);
1005     } else {
1006         return SUCCESS;
1007     }
1008 }
1009 
walk_down_the_path_callback(char * begin,char * end,void * data)1010 static void walk_down_the_path_callback(char* begin,
1011                                         char* end,
1012                                         void* data)
1013 {
1014     _lsapi_activate_user_ini_ctx *ctx = data;
1015     char tmp = end[0];
1016     end[0] = 0;
1017     php_parse_user_ini_file(begin, PG(user_ini_filename), &ctx->entry->user_config);
1018     end[0] = tmp;
1019 }
1020 
lsapi_activate_user_ini_walk_down_the_path(_lsapi_activate_user_ini_ctx * ctx,void * next)1021 static int lsapi_activate_user_ini_walk_down_the_path(_lsapi_activate_user_ini_ctx *ctx,
1022                                                       void* next)
1023 {
1024     time_t request_time = sapi_get_request_time();
1025     uint32_t docroot_len;
1026     int rc = SUCCESS;
1027     fn_activate_user_ini_chain_t *fn_next = next;
1028 
1029     if (!ctx->entry->expires || request_time > ctx->entry->expires)
1030     {
1031         docroot_len = ctx->doc_root && ctx->doc_root[0]
1032                     ? pathlen_without_trailing_slash(ctx->doc_root)
1033                     : 0;
1034 
1035         int is_outside_of_docroot = !docroot_len ||
1036                 ctx->path_len < docroot_len ||
1037                 strncmp(ctx->path, ctx->doc_root, docroot_len) != 0;
1038 
1039         if (is_outside_of_docroot) {
1040             php_parse_user_ini_file(ctx->path, PG(user_ini_filename),
1041                                     &ctx->entry->user_config);
1042         } else {
1043             walk_down_the_path(ctx->doc_root, ctx->path,
1044                                &walk_down_the_path_callback, ctx);
1045         }
1046 
1047         ctx->entry->expires = request_time + PG(user_ini_cache_ttl);
1048     }
1049 
1050     if (*fn_next) {
1051         rc = (*fn_next)(ctx, fn_next + 1);
1052     }
1053 
1054     return rc;
1055 }
1056 
lsapi_activate_user_ini_finally(_lsapi_activate_user_ini_ctx * ctx,void * next)1057 static int lsapi_activate_user_ini_finally(_lsapi_activate_user_ini_ctx *ctx,
1058                                            void* next)
1059 {
1060     int rc = SUCCESS;
1061     fn_activate_user_ini_chain_t *fn_next = next;
1062 
1063     php_ini_activate_config(&ctx->entry->user_config, PHP_INI_PERDIR,
1064                             PHP_INI_STAGE_HTACCESS);
1065 
1066     if (*fn_next) {
1067         rc = (*fn_next)(ctx, fn_next + 1);
1068     }
1069 
1070     return rc;
1071 }
1072 
lsapi_activate_user_ini(void)1073 static int lsapi_activate_user_ini( void )
1074 {
1075     _lsapi_activate_user_ini_ctx ctx;
1076     /**
1077      * The reason to have this function list stacked
1078      * is each function now can define a scoped destructor.
1079      *
1080      * Passing control via function pointer is a sign of low coupling,
1081      * which means dependencies between these functions are to be
1082      * controlled from a single place
1083      * (here below, by modifying this function list order)
1084      */
1085     static const fn_activate_user_ini_chain_t fn_chain[] = {
1086         &lsapi_activate_user_ini_basic_checks,
1087         &lsapi_activate_user_ini_mk_path,
1088         &lsapi_activate_user_ini_mk_realpath,
1089         &lsapi_activate_user_ini_mk_user_config,
1090         &lsapi_activate_user_ini_walk_down_the_path,
1091         &lsapi_activate_user_ini_finally,
1092         NULL
1093     };
1094 
1095     return fn_chain[0](&ctx, (fn_activate_user_ini_chain_t*)(fn_chain + 1));
1096 }
1097 
1098 
override_ini(void)1099 static void override_ini(void)
1100 {
1101 
1102     LSAPI_ForeachSpecialEnv( alter_ini, NULL );
1103 
1104 }
1105 
1106 
processReq(void)1107 static int processReq(void)
1108 {
1109     int ret = 0;
1110     zend_first_try {
1111 
1112         /* avoid server_context==NULL checks */
1113         SG(server_context) = (void *) 1;
1114 
1115         engine = 1;
1116         override_ini();
1117 
1118         if ( engine ) {
1119             init_request_info();
1120 
1121             if ( lsapi_module_main( source_highlight ) == -1 ) {
1122                 ret = -1;
1123             }
1124         } else {
1125             LSAPI_AppendRespHeader( "status: 403", 11 );
1126             LSAPI_AppendRespHeader( "content-type: text/html", 23 );
1127             LSAPI_Write( "Forbidden: PHP engine is disable.\n", 34 );
1128         }
1129     } zend_end_try();
1130     return ret;
1131 }
1132 
cli_usage(void)1133 static void cli_usage(void)
1134 {
1135     static const char * usage =
1136         "Usage: php\n"
1137         "      php -[b|c|n|h|i|q|s|v|?] [<file>] [args...]\n"
1138         "  Run in LSAPI mode, only '-b', '-s' and '-c' are effective\n"
1139         "  Run in Command Line Interpreter mode when parameters are specified\n"
1140         "\n"
1141         "  -b <address:port>|<port> Bind Path for external LSAPI Server mode\n"
1142         "  -c <path>|<file> Look for php.ini file in this directory\n"
1143         "  -n    No php.ini file will be used\n"
1144         "  -h    This help\n"
1145         "  -i    PHP information\n"
1146         "  -l    Syntax check\n"
1147         "  -q    Quiet-mode.  Suppress HTTP Header output.\n"
1148         "  -s    Display colour syntax highlighted source.\n"
1149         "  -v    Version number\n"
1150         "  -?    This help\n"
1151         "\n"
1152         "  args...    Arguments passed to script.\n";
1153     php_output_startup();
1154     php_output_activate();
1155     php_printf( "%s", usage );
1156 #ifdef PHP_OUTPUT_NEWAPI
1157     php_output_end_all();
1158 #else
1159     php_end_ob_buffers(1);
1160 #endif
1161 }
1162 
parse_opt(int argc,char * argv[],int * climode,char ** php_ini_path,char ** php_bind)1163 static int parse_opt( int argc, char * argv[], int *climode,
1164                         char **php_ini_path, char ** php_bind )
1165 {
1166     char ** p = &argv[1];
1167     char ** argend= &argv[argc];
1168     int c;
1169     while (( p < argend )&&(**p == '-' )) {
1170         c = *((*p)+1);
1171         ++p;
1172         switch( c ) {
1173         case 'b':
1174             if ( p >= argend ) {
1175                 fprintf( stderr, "TCP or socket address must be specified following '-b' option.\n");
1176                 return -1;
1177             }
1178             *php_bind = strdup(*p++);
1179             break;
1180 
1181         case 'c':
1182             if ( p >= argend ) {
1183                 fprintf( stderr, "<path> or <file> must be specified following '-c' option.\n");
1184 
1185                 return -1;
1186             }
1187             *php_ini_path = strdup( *p++ );
1188             break;
1189         case 's':
1190             source_highlight = 1;
1191             break;
1192         case 'n':
1193             ignore_php_ini = 1;
1194             break;
1195         case '?':
1196             if ( *((*(p-1))+2) == 's' )
1197                 exit( 99 );
1198             ZEND_FALLTHROUGH;
1199         case 'h':
1200         case 'i':
1201         case 'l':
1202         case 'q':
1203         case 'v':
1204         default:
1205             *climode = 1;
1206             break;
1207         }
1208     }
1209     if ( p - argv < argc ) {
1210         *climode = 1;
1211     }
1212     return 0;
1213 }
1214 
cli_main(int argc,char * argv[])1215 static int cli_main( int argc, char * argv[] )
1216 {
1217 
1218     static const char * ini_defaults[] = {
1219         "display_errors",       "1",
1220         "register_argc_argv",   "1",
1221         "html_errors",          "0",
1222         "implicit_flush",       "1",
1223         "output_buffering",     "0",
1224         "max_execution_time",   "0",
1225         "max_input_time",       "-1",
1226         NULL
1227     };
1228 
1229     const char ** ini;
1230     char ** p = &argv[1];
1231     char ** argend= &argv[argc];
1232     int ret = -1;
1233     int c;
1234     zend_string *psKey;
1235     lsapi_mode = 0;        /* enter CLI mode */
1236 
1237     zend_first_try     {
1238         SG(server_context) = (void *) 1;
1239 
1240         zend_uv.html_errors = 0; /* tell the engine we're in non-html mode */
1241         CG(in_compilation) = 0; /* not initialized but needed for several options */
1242         SG(options) |= SAPI_OPTION_NO_CHDIR;
1243 
1244 #if PHP_MAJOR_VERSION < 7
1245         EG(uninitialized_zval_ptr) = NULL;
1246 #endif
1247         for( ini = ini_defaults; *ini; ini+=2 ) {
1248             psKey = zend_string_init(*ini, strlen( *ini ), 1);
1249             zend_alter_ini_entry_chars(psKey,
1250                                 (char *)*(ini+1), strlen( *(ini+1) ),
1251                                 PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
1252             zend_string_release_ex(psKey, 1);
1253         }
1254 
1255         while (( p < argend )&&(**p == '-' )) {
1256             c = *((*p)+1);
1257             ++p;
1258             switch( c ) {
1259             case 'q':
1260                 break;
1261             case 'i':
1262                 if (php_request_startup() != FAILURE) {
1263                     php_print_info(0xFFFFFFFF);
1264 #ifdef PHP_OUTPUT_NEWAPI
1265                     php_output_end_all();
1266 #else
1267                     php_end_ob_buffers(1);
1268 #endif
1269                     php_request_shutdown( NULL );
1270                     ret = 0;
1271                 }
1272                 break;
1273             case 'v':
1274                 if (php_request_startup() != FAILURE) {
1275                     php_print_version(&sapi_module);
1276 #ifdef PHP_OUTPUT_NEWAPI
1277                     php_output_end_all();
1278 #else
1279                     php_end_ob_buffers(1);
1280 #endif
1281                     php_request_shutdown( NULL );
1282                     ret = 0;
1283                 }
1284                 break;
1285             case 'c':
1286                 ++p;
1287             /* fall through */
1288             case 's':
1289                 break;
1290             case 'l':
1291                 source_highlight = 2;
1292                 break;
1293             case 'h':
1294             case '?':
1295             default:
1296                 cli_usage();
1297                 ret = 0;
1298                 break;
1299 
1300             }
1301         }
1302         if ( ret == -1 ) {
1303             if ( *p ) {
1304 				zend_file_handle file_handle;
1305 				zend_stream_init_fp(&file_handle, VCWD_FOPEN(*p, "rb"), NULL);
1306 				file_handle.primary_script = 1;
1307 
1308                 if ( file_handle.handle.fp ) {
1309                     script_filename = *p;
1310                     php_self = *p;
1311 
1312                     SG(request_info).path_translated = estrdup(*p);
1313                     SG(request_info).argc = argc - (p - argv);
1314                     SG(request_info).argv = p;
1315 
1316                     if (php_request_startup() == FAILURE ) {
1317                         fclose( file_handle.handle.fp );
1318                         ret = 2;
1319                     } else {
1320                         if (source_highlight == 1) {
1321                             zend_syntax_highlighter_ini syntax_highlighter_ini;
1322 
1323                             php_get_highlight_struct(&syntax_highlighter_ini);
1324                             highlight_file(SG(request_info).path_translated, &syntax_highlighter_ini);
1325                         } else if (source_highlight == 2) {
1326                             file_handle.filename = zend_string_init(*p, strlen(*p), 0);
1327                             file_handle.opened_path = NULL;
1328                             ret = php_lint_script(&file_handle);
1329                             if (ret==SUCCESS) {
1330                                 zend_printf("No syntax errors detected in %s\n", ZSTR_VAL(file_handle.filename));
1331                             } else {
1332                                 zend_printf("Errors parsing %s\n", ZSTR_VAL(file_handle.filename));
1333                             }
1334 
1335                         } else {
1336                             file_handle.filename = zend_string_init(*p, strlen(*p), 0);
1337                             file_handle.opened_path = NULL;
1338 
1339                             php_execute_script(&file_handle);
1340                             ret = EG(exit_status);
1341                        }
1342 
1343                         php_request_shutdown( NULL );
1344                     }
1345                 } else {
1346                     fprintf(stderr, "Could not open input file: %s.\n", *p);
1347                 }
1348             } else {
1349                 cli_usage();
1350             }
1351         }
1352 
1353     }zend_end_try();
1354 
1355     php_module_shutdown();
1356 
1357 #ifdef ZTS
1358     tsrm_shutdown();
1359 #endif
1360     return ret;
1361 }
1362 
1363 static int s_stop;
litespeed_cleanup(int signal)1364 void litespeed_cleanup(int signal)
1365 {
1366     s_stop = signal;
1367 }
1368 
1369 
start_children(int children)1370 void start_children( int children )
1371 {
1372     struct sigaction act, old_term, old_quit, old_int, old_usr1;
1373     int running = 0;
1374     int status;
1375     pid_t pid;
1376 
1377     /* Create a process group */
1378     setsid();
1379 
1380     /* Set up handler to kill children upon exit */
1381     sigemptyset(&act.sa_mask);
1382     act.sa_flags = 0;
1383     act.sa_handler = litespeed_cleanup;
1384     if( sigaction( SIGTERM, &act, &old_term ) ||
1385         sigaction( SIGINT,  &act, &old_int  ) ||
1386         sigaction( SIGUSR1, &act, &old_usr1 ) ||
1387         sigaction( SIGQUIT, &act, &old_quit )) {
1388         perror( "Can't set signals" );
1389         exit( 1 );
1390     }
1391     s_stop = 0;
1392     while( 1 ) {
1393         while((!s_stop )&&( running < children )) {
1394             pid = fork();
1395             switch( pid ) {
1396             case 0: /* children process */
1397 
1398                 /* don't catch our signals */
1399                 sigaction( SIGTERM, &old_term, 0 );
1400                 sigaction( SIGQUIT, &old_quit, 0 );
1401                 sigaction( SIGINT,  &old_int,  0 );
1402                 sigaction( SIGUSR1, &old_usr1, 0 );
1403                 return ;
1404             case -1:
1405                 perror( "php (pre-forking)" );
1406                 exit( 1 );
1407                 break;
1408             default: /* parent process */
1409                 running++;
1410                 break;
1411             }
1412         }
1413         if ( s_stop ) {
1414             break;
1415         }
1416         pid = wait( &status );
1417         running--;
1418     }
1419     kill( -getpgrp(), SIGUSR1 );
1420     exit( 0 );
1421 }
1422 
setArgv0(int argc,char * argv[])1423 void setArgv0( int argc, char * argv[] )
1424 {
1425     char * p;
1426     int i;
1427     argv0 = argv[0] + strlen( argv[0] );
1428     p = argv0;
1429     while(( p > argv[0] )&&( p[-1] != '/'))
1430         --p;
1431     if ( p > argv[0] )
1432     {
1433         memmove( argv[0], p, argv0 - p );
1434         memset( argv[0] + ( argv0 - p ), 0, p - argv[0] );
1435         argv0 = argv[0] + (argv0 - p);
1436     }
1437     for( i = 1; i < argc; ++i )
1438     {
1439         memset( argv[i], 0, strlen( argv[i] ) );
1440     }
1441 }
1442 
1443 #include <fcntl.h>
main(int argc,char * argv[])1444 int main( int argc, char * argv[] )
1445 {
1446     int ret;
1447     int bindFd;
1448 
1449     char * php_ini_path = NULL;
1450     char * php_bind     = NULL;
1451     int n;
1452     int climode = 0;
1453     struct timeval tv_req_begin;
1454     struct timeval tv_req_end;
1455     int slow_script_msec = 0;
1456     char time_buf[40];
1457 
1458 
1459 #if defined(SIGPIPE) && defined(SIG_IGN)
1460     signal(SIGPIPE, SIG_IGN);
1461 #endif
1462 
1463 #ifdef ZTS
1464     php_tsrm_startup();
1465 #endif
1466 
1467 #if PHP_MAJOR_VERSION >= 7
1468 #if defined(ZEND_SIGNALS) || PHP_MINOR_VERSION > 0
1469     zend_signal_startup();
1470 #endif
1471 #endif
1472 
1473     if (argc > 1 ) {
1474         if ( parse_opt( argc, argv, &climode,
1475                 &php_ini_path, &php_bind ) == -1 ) {
1476             return 1;
1477         }
1478     }
1479     if ( climode ) {
1480         lsapi_sapi_module.phpinfo_as_text = 1;
1481     } else {
1482         setArgv0(argc, argv );
1483     }
1484 
1485     sapi_startup(&lsapi_sapi_module);
1486 
1487 #ifdef ZTS
1488     compiler_globals = ts_resource(compiler_globals_id);
1489     executor_globals = ts_resource(executor_globals_id);
1490     core_globals = ts_resource(core_globals_id);
1491     sapi_globals = ts_resource(sapi_globals_id);
1492 
1493     SG(request_info).path_translated = NULL;
1494 #endif
1495 
1496     lsapi_sapi_module.executable_location = argv[0];
1497 
1498     /* Initialize from environment variables before processing command-line
1499      * options: the latter override the former.
1500      */
1501     init_sapi_from_env(&lsapi_sapi_module);
1502 
1503     if ( ignore_php_ini )
1504         lsapi_sapi_module.php_ini_ignore = 1;
1505 
1506     if ( php_ini_path ) {
1507         lsapi_sapi_module.php_ini_path_override = php_ini_path;
1508     }
1509 
1510 
1511     lsapi_sapi_module.ini_defaults = sapi_lsapi_ini_defaults;
1512 
1513     if (php_module_startup(&lsapi_sapi_module, &litespeed_module_entry) == FAILURE) {
1514 #ifdef ZTS
1515         tsrm_shutdown();
1516 #endif
1517         return FAILURE;
1518     }
1519 
1520     if ( climode ) {
1521         return cli_main(argc, argv);
1522     }
1523 
1524     if ( php_bind ) {
1525         bindFd = LSAPI_CreateListenSock( php_bind, 10 );
1526         if ( bindFd == -1 ) {
1527             fprintf( stderr,
1528                      "Failed to bind socket [%s]: %s\n", php_bind, strerror( errno ) );
1529             exit( 2 );
1530         }
1531         if ( bindFd != 0 ) {
1532             dup2( bindFd, 0 );
1533             close( bindFd );
1534         }
1535     }
1536 
1537     LSAPI_Init();
1538 
1539 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
1540     int is_criu = LSCRIU_Init(); // Must be called before the regular init as it unsets the parameters.
1541 #endif
1542 
1543     LSAPI_Init_Env_Parameters( NULL );
1544     lsapi_mode = 1;
1545 
1546     slow_script_msec = LSAPI_Get_Slow_Req_Msecs();
1547 
1548     if ( php_bind ) {
1549         LSAPI_No_Check_ppid();
1550         free( php_bind );
1551         php_bind = NULL;
1552     }
1553 
1554     int result;
1555 
1556     if (do_clean_shutdown)
1557         atexit(lsapi_atexit);
1558 
1559     while( ( result = LSAPI_Prefork_Accept_r( &g_req )) >= 0 ) {
1560 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
1561         if (is_criu && !result) {
1562             LSCRIU_inc_req_processed();
1563         }
1564 #endif
1565         if ( slow_script_msec ) {
1566             gettimeofday( &tv_req_begin, NULL );
1567         }
1568         ret = processReq();
1569         if ( slow_script_msec ) {
1570             gettimeofday( &tv_req_end, NULL );
1571             n = ((long) tv_req_end.tv_sec - tv_req_begin.tv_sec ) * 1000
1572                 + (tv_req_end.tv_usec - tv_req_begin.tv_usec) / 1000;
1573             if ( n > slow_script_msec )
1574             {
1575                 strftime( time_buf, 30, "%d/%b/%Y:%H:%M:%S", localtime( &tv_req_end.tv_sec ) );
1576                 fprintf( stderr, "[%s] Slow PHP script: %d ms\n  URL: %s %s\n  Query String: %s\n  Script: %s\n",
1577                          time_buf, n,  LSAPI_GetRequestMethod(),
1578                          LSAPI_GetScriptName(), LSAPI_GetQueryString(),
1579                          LSAPI_GetScriptFileName() );
1580 
1581             }
1582         }
1583         LSAPI_Finish();
1584         if ( ret ) {
1585             break;
1586         }
1587     }
1588 
1589     php_module_shutdown();
1590 
1591 #ifdef ZTS
1592     tsrm_shutdown();
1593 #endif
1594     return ret;
1595 }
1596 
1597 /*   LiteSpeed PHP module starts here */
1598 
1599 PHP_MINFO_FUNCTION(litespeed);
1600 
PHP_MINIT_FUNCTION(litespeed)1601 static PHP_MINIT_FUNCTION(litespeed)
1602 {
1603     user_config_cache_init();
1604 
1605     const char *p = getenv("LSPHP_ENABLE_USER_INI");
1606     if (p && 0 == strcasecmp(p, "on"))
1607         parse_user_ini = 1;
1608 
1609     p = getenv("LSAPI_CLEAN_SHUTDOWN");
1610     if (p) {
1611         if (*p == '1' || 0 == strcasecmp(p, "on"))
1612             do_clean_shutdown = 1;
1613         else if (*p == '0' || 0 == strcasecmp(p, "off"))
1614             do_clean_shutdown = 0;
1615     }
1616     /*
1617      * mod_lsapi always sets this env var,
1618      * so we can detect mod_lsapi mode with its presence.
1619      */
1620     mod_lsapi_mode = ( getenv("LSAPI_DISABLE_CPAN_BEHAV") != NULL );
1621 
1622     /* REGISTER_INI_ENTRIES(); */
1623     return SUCCESS;
1624 }
1625 
1626 
PHP_MSHUTDOWN_FUNCTION(litespeed)1627 static PHP_MSHUTDOWN_FUNCTION(litespeed)
1628 {
1629     zend_hash_destroy(&user_config_cache);
1630 
1631     /* UNREGISTER_INI_ENTRIES(); */
1632     return SUCCESS;
1633 }
1634 
1635 zend_module_entry litespeed_module_entry = {
1636     STANDARD_MODULE_HEADER,
1637     "litespeed",
1638     ext_functions,
1639     PHP_MINIT(litespeed),
1640     PHP_MSHUTDOWN(litespeed),
1641     NULL,
1642     NULL,
1643     NULL,
1644     PHP_VERSION,
1645     STANDARD_MODULE_PROPERTIES
1646 };
1647 
add_associate_array(const char * pKey,int keyLen,const char * pValue,int valLen,void * arg)1648 static int add_associate_array( const char * pKey, int keyLen, const char * pValue, int valLen,
1649                          void * arg )
1650 {
1651     add_assoc_string_ex((zval *)arg, (char *)pKey, keyLen, (char *)pValue);
1652     return 1;
1653 }
1654 
1655 
1656 /* {{{ Fetch all HTTP request headers */
PHP_FUNCTION(litespeed_request_headers)1657 PHP_FUNCTION(litespeed_request_headers)
1658 {
1659     if (zend_parse_parameters_none() == FAILURE) {
1660         RETURN_THROWS();
1661     }
1662 
1663     array_init(return_value);
1664 
1665     LSAPI_ForeachOrgHeader( add_associate_array, return_value );
1666 }
1667 /* }}} */
1668 
1669 
1670 
1671 /* {{{ Fetch all HTTP response headers */
PHP_FUNCTION(litespeed_response_headers)1672 PHP_FUNCTION(litespeed_response_headers)
1673 {
1674     sapi_header_struct  *h;
1675     zend_llist_position pos;
1676     char *       p;
1677     int          len;
1678     char         headerBuf[SAPI_LSAPI_MAX_HEADER_LENGTH];
1679 
1680     if (zend_parse_parameters_none() == FAILURE) {
1681 		RETURN_THROWS();
1682 	}
1683 
1684     if (!&SG(sapi_headers).headers) {
1685         RETURN_FALSE;
1686     }
1687     array_init(return_value);
1688 
1689     h = zend_llist_get_first_ex(&SG(sapi_headers).headers, &pos);
1690     while (h) {
1691         if ( h->header_len > 0 ) {
1692             p = strchr( h->header, ':' );
1693             len = p - h->header;
1694             if (p && len > 0 && len < LSAPI_RESP_HTTP_HEADER_MAX) {
1695                 memmove( headerBuf, h->header, len );
1696                 while( len > 0 && (isspace( headerBuf[len-1])) ) {
1697                     --len;
1698                 }
1699                 headerBuf[len] = 0;
1700                 if ( len ) {
1701                     while( isspace(*++p));
1702                     add_assoc_string_ex(return_value, headerBuf, len, p);
1703                 }
1704             }
1705         }
1706         h = zend_llist_get_next_ex(&SG(sapi_headers).headers, &pos);
1707     }
1708 }
1709 
1710 /* }}} */
1711 
1712 
1713 /* {{{ Fetch all loaded module names  */
PHP_FUNCTION(apache_get_modules)1714 PHP_FUNCTION(apache_get_modules)
1715 {
1716     static const char * mod_names[] =
1717     {
1718         "mod_rewrite", "mod_mime", "mod_headers", "mod_expires", "mod_auth_basic", NULL
1719     };
1720     const char **name = mod_names;
1721 
1722     if (zend_parse_parameters_none() == FAILURE) {
1723 		RETURN_THROWS();
1724 	}
1725 
1726     array_init(return_value);
1727     while( *name )
1728     {
1729         add_next_index_string(return_value, *name);
1730         ++name;
1731     }
1732 }
1733 /* }}} */
1734 
1735 
1736 /* {{{ Flushes all response data to the client */
PHP_FUNCTION(litespeed_finish_request)1737 PHP_FUNCTION(litespeed_finish_request)
1738 {
1739 	if (zend_parse_parameters_none() == FAILURE) {
1740 		RETURN_THROWS();
1741 	}
1742 
1743     php_output_end_all();
1744     php_header();
1745 
1746     if (LSAPI_End_Response() != -1) {
1747         RETURN_TRUE;
1748     }
1749     RETURN_FALSE;
1750 }
1751 /* }}} */
1752