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