xref: /PHP-8.0/sapi/litespeed/lsapi_main.c (revision 797edd62)
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 at through the world-wide-web at the following url:        |
8    | http://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     zend_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         zend_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(zend_file_handle * file_handle)646 static int lsapi_execute_script( zend_file_handle * file_handle)
647 {
648     char *p;
649     int len;
650 	zend_stream_init_filename(file_handle, SG(request_info).path_translated);
651 
652     p = argv0;
653     *p++ = ':';
654     len = strlen( SG(request_info).path_translated );
655     if ( len > 45 )
656         len = len - 45;
657     else
658         len = 0;
659     memccpy( p, SG(request_info).path_translated + len, 0, 46 );
660 
661     php_execute_script(file_handle);
662     return 0;
663 
664 }
665 
lsapi_sigsegv(int signal)666 static void lsapi_sigsegv( int signal )
667 {
668     //fprintf(stderr, "lsapi_sigsegv: %d: Segmentation violation signal is caught during request shutdown\n", getpid());
669     _exit(1);
670 }
671 
672 static int do_clean_shutdown = 1;
673 
674 static int clean_onexit = 1;
675 
676 
lsapi_clean_shutdown()677 static void lsapi_clean_shutdown()
678 {
679     struct sigaction act;
680     int sa_rc;
681     struct itimerval tmv;
682 #if PHP_MAJOR_VERSION >= 7
683     zend_string * key;
684 #endif
685     clean_onexit = 1;
686     sigemptyset(&act.sa_mask);
687     act.sa_flags = 0;
688     act.sa_handler = lsapi_sigsegv;
689     sa_rc = sigaction(SIGINT,  &act, NULL);
690     sa_rc = sigaction(SIGQUIT, &act, NULL);
691     sa_rc = sigaction(SIGILL,  &act, NULL);
692     sa_rc = sigaction(SIGABRT, &act, NULL);
693     sa_rc = sigaction(SIGBUS,  &act, NULL);
694     sa_rc = sigaction(SIGSEGV, &act, NULL);
695     sa_rc = sigaction(SIGTERM, &act, NULL);
696 
697     sa_rc = 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 = zend_string_init("error_reporting", 15, 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 
738 
lsapi_module_main(int show_source)739 static int lsapi_module_main(int show_source)
740 {
741     struct sigaction act;
742     int sa_rc;
743     zend_file_handle file_handle;
744     memset(&file_handle, 0, sizeof(file_handle));
745     if (php_request_startup() == FAILURE ) {
746         return -1;
747     }
748 
749     if (do_clean_shutdown) {
750         sigemptyset(&act.sa_mask);
751         act.sa_flags = SA_NODEFER;
752         act.sa_handler = lsapi_sigterm;
753         sa_rc = sigaction( SIGINT, &act, NULL);
754         sa_rc = sigaction( SIGQUIT, &act, NULL);
755         sa_rc = sigaction( SIGILL, &act, NULL);
756         sa_rc = sigaction( SIGABRT, &act, NULL);
757         sa_rc = sigaction( SIGBUS, &act, NULL);
758         sa_rc = sigaction( SIGSEGV, &act, NULL);
759         sa_rc = sigaction( SIGTERM, &act, NULL);
760 
761         clean_onexit = 0;
762     }
763 
764     if (show_source) {
765         zend_syntax_highlighter_ini syntax_highlighter_ini;
766 
767         php_get_highlight_struct(&syntax_highlighter_ini);
768         highlight_file(SG(request_info).path_translated, &syntax_highlighter_ini);
769     } else {
770         lsapi_execute_script( &file_handle);
771     }
772     zend_try {
773         php_request_shutdown(NULL);
774 
775         clean_onexit = 1;
776 
777         memset( argv0, 0, 46 );
778     } zend_end_try();
779     return 0;
780 }
781 
782 
alter_ini(const char * pKey,int keyLen,const char * pValue,int valLen,void * arg)783 static int alter_ini( const char * pKey, int keyLen, const char * pValue, int valLen,
784                 void * arg )
785 {
786     zend_string * psKey;
787 
788     int type = ZEND_INI_PERDIR;
789     int stage = PHP_INI_STAGE_RUNTIME;
790     if ( '\001' == *pKey ) {
791         ++pKey;
792         if ( *pKey == 4 ) {
793             type = ZEND_INI_SYSTEM;
794             /*
795               Use ACTIVATE stage in legacy mode only.
796 
797               RUNTIME stage should be used here,
798               as with ACTIVATE it's impossible to change the option from script with ini_set
799             */
800             if(!mod_lsapi_mode)
801             {
802                 stage = PHP_INI_STAGE_ACTIVATE;
803             }
804         }
805         else
806         {
807             stage = PHP_INI_STAGE_HTACCESS;
808         }
809         ++pKey;
810         --keyLen;
811         if (( keyLen == 7 )&&( strncasecmp( pKey, "engine", 6 )== 0 ))
812         {
813             if ( *pValue == '0' )
814                 engine = 0;
815         }
816         else
817         {
818             --keyLen;
819             psKey = zend_string_init(pKey, keyLen, 1);
820             zend_alter_ini_entry_chars(psKey,
821                              (char *)pValue, valLen,
822                              type, stage);
823             zend_string_release_ex(psKey, 1);
824         }
825     }
826     return 1;
827 }
828 
user_config_cache_entry_dtor(zval * el)829 static void user_config_cache_entry_dtor(zval *el)
830 {
831     user_config_cache_entry *entry = (user_config_cache_entry *)Z_PTR_P(el);
832     zend_hash_destroy(&entry->user_config);
833     free(entry);
834 }
835 
user_config_cache_init()836 static void user_config_cache_init()
837 {
838     zend_hash_init(&user_config_cache, 0, NULL, user_config_cache_entry_dtor, 1);
839 }
840 
pathlen_without_trailing_slash(char * path)841 static int pathlen_without_trailing_slash(char *path)
842 {
843     int len = (int)strlen(path);
844     while (len > 1 && /* mind "/" as root dir */
845            path[len-1] == DEFAULT_SLASH)
846     {
847         --len;
848     }
849     return len;
850 }
851 
skip_slash(char * s)852 static inline char* skip_slash(char *s)
853 {
854     while (*s == DEFAULT_SLASH) {
855         ++s;
856     }
857     return s;
858 }
859 
860 /**
861  * Walk down the path_stop starting at path_start.
862  *
863  * If path_start = "/path1" and path_stop = "/path1/path2/path3"
864  * the callback will be called 3 times with the next args:
865  *
866  *   1. "/path1/path2/path3"
867  *             ^ end
868  *       ^ start
869  *   2. "/path1/path2/path3"
870  *                   ^ end
871  *       ^ start
872  *   3. "/path1/path2/path3"
873  *                         ^ end
874  *       ^ start
875  *
876  * path_stop has to be a subdir of path_start
877  * or to be path_start itself.
878  *
879  * Both path args have to be absolute.
880  * Trailing slashes are allowed.
881  * NULL or empty string args are not allowed.
882  */
walk_down_the_path(char * path_start,char * path_stop,void (* cb)(char * begin,char * end,void * data),void * data)883 static void walk_down_the_path(char* path_start,
884                                char* path_stop,
885                                void (*cb)(char* begin,
886                                           char* end,
887                                           void* data),
888                                void* data)
889 {
890     char *pos = path_stop + pathlen_without_trailing_slash(path_start);
891     cb(path_stop, pos, data);
892 
893     while ((pos = skip_slash(pos))[0]) {
894         pos = strchr(pos, DEFAULT_SLASH);
895         if (!pos) {
896             /* The last token without trailing slash
897              */
898             cb(path_stop, path_stop + strlen(path_stop), data);
899             return;
900         }
901         cb(path_stop, pos, data);
902     }
903 }
904 
905 
906 typedef struct {
907     char *path;
908     uint32_t path_len;
909     char *doc_root;
910     user_config_cache_entry *entry;
911 } _lsapi_activate_user_ini_ctx;
912 
913 typedef int (*fn_activate_user_ini_chain_t)
914         (_lsapi_activate_user_ini_ctx *ctx, void* next);
915 
lsapi_activate_user_ini_basic_checks(_lsapi_activate_user_ini_ctx * ctx,void * next)916 static int lsapi_activate_user_ini_basic_checks(_lsapi_activate_user_ini_ctx *ctx,
917                                                 void* next)
918 {
919     int rc = SUCCESS;
920     fn_activate_user_ini_chain_t *fn_next = next;
921 
922     if (!PG(user_ini_filename) || !*PG(user_ini_filename)) {
923         return SUCCESS;
924     }
925 
926     /* PATH_TRANSLATED should be defined at this stage */
927     ctx->path = SG(request_info).path_translated;
928     if (!ctx->path || !*ctx->path) {
929         return FAILURE;
930     }
931 
932     ctx->doc_root = sapi_lsapi_getenv("DOCUMENT_ROOT", 0);
933     DEBUG_MESSAGE("doc_root: %s", ctx->doc_root);
934 
935     if (*fn_next) {
936         rc = (*fn_next)(ctx, fn_next + 1);
937     }
938 
939     return rc;
940 }
941 
lsapi_activate_user_ini_mk_path(_lsapi_activate_user_ini_ctx * ctx,void * next)942 static int lsapi_activate_user_ini_mk_path(_lsapi_activate_user_ini_ctx *ctx,
943                                            void* next)
944 {
945     char *path;
946     int rc = SUCCESS;
947     fn_activate_user_ini_chain_t *fn_next = next;
948 
949     /* Extract dir name from path_translated * and store it in 'path' */
950     ctx->path_len = strlen(ctx->path);
951     path = ctx->path = estrndup(SG(request_info).path_translated, ctx->path_len);
952     ctx->path_len = zend_dirname(path, ctx->path_len);
953 
954     if (*fn_next) {
955         rc = (*fn_next)(ctx, fn_next + 1);
956     }
957 
958     efree(path);
959     return rc;
960 }
961 
lsapi_activate_user_ini_mk_realpath(_lsapi_activate_user_ini_ctx * ctx,void * next)962 static int lsapi_activate_user_ini_mk_realpath(_lsapi_activate_user_ini_ctx *ctx,
963                                                void* next)
964 {
965     char *real_path;
966     int rc = SUCCESS;
967     fn_activate_user_ini_chain_t *fn_next = next;
968 
969     if (!IS_ABSOLUTE_PATH(ctx->path, ctx->path_len)) {
970         real_path = tsrm_realpath(ctx->path, NULL);
971         if (!real_path) {
972             return SUCCESS;
973         }
974         ctx->path = real_path;
975         ctx->path_len = strlen(ctx->path);
976     } else {
977         real_path = NULL;
978     }
979 
980     if (*fn_next) {
981         rc = (*fn_next)(ctx, fn_next + 1);
982     }
983 
984     if (real_path)
985         efree(real_path);
986     return rc;
987 }
988 
lsapi_activate_user_ini_mk_user_config(_lsapi_activate_user_ini_ctx * ctx,void * next)989 static int lsapi_activate_user_ini_mk_user_config(_lsapi_activate_user_ini_ctx *ctx,
990                                                   void* next)
991 {
992     fn_activate_user_ini_chain_t *fn_next = next;
993 
994     /* Find cached config entry: If not found, create one */
995     ctx->entry = zend_hash_str_find_ptr(&user_config_cache, ctx->path, ctx->path_len);
996 
997     if (!ctx->entry)
998     {
999         ctx->entry = pemalloc(sizeof(user_config_cache_entry), 1);
1000         ctx->entry->expires = 0;
1001         zend_hash_init(&ctx->entry->user_config, 0, NULL,
1002                        config_zval_dtor, 1);
1003         zend_hash_str_update_ptr(&user_config_cache, ctx->path,
1004                                             ctx->path_len, ctx->entry);
1005     }
1006 
1007     if (*fn_next) {
1008         return (*fn_next)(ctx, fn_next + 1);
1009     } else {
1010         return SUCCESS;
1011     }
1012 }
1013 
walk_down_the_path_callback(char * begin,char * end,void * data)1014 static void walk_down_the_path_callback(char* begin,
1015                                         char* end,
1016                                         void* data)
1017 {
1018     _lsapi_activate_user_ini_ctx *ctx = data;
1019     char tmp = end[0];
1020     end[0] = 0;
1021     php_parse_user_ini_file(begin, PG(user_ini_filename), &ctx->entry->user_config);
1022     end[0] = tmp;
1023 }
1024 
lsapi_activate_user_ini_walk_down_the_path(_lsapi_activate_user_ini_ctx * ctx,void * next)1025 static int lsapi_activate_user_ini_walk_down_the_path(_lsapi_activate_user_ini_ctx *ctx,
1026                                                       void* next)
1027 {
1028     time_t request_time = sapi_get_request_time();
1029     uint32_t docroot_len;
1030     int rc = SUCCESS;
1031     fn_activate_user_ini_chain_t *fn_next = next;
1032 
1033     if (!ctx->entry->expires || request_time > ctx->entry->expires)
1034     {
1035         docroot_len = ctx->doc_root && ctx->doc_root[0]
1036                     ? pathlen_without_trailing_slash(ctx->doc_root)
1037                     : 0;
1038 
1039         int is_outside_of_docroot = !docroot_len ||
1040                 ctx->path_len < docroot_len ||
1041                 strncmp(ctx->path, ctx->doc_root, docroot_len) != 0;
1042 
1043         if (is_outside_of_docroot) {
1044             php_parse_user_ini_file(ctx->path, PG(user_ini_filename),
1045                                     &ctx->entry->user_config);
1046         } else {
1047             walk_down_the_path(ctx->doc_root, ctx->path,
1048                                &walk_down_the_path_callback, ctx);
1049         }
1050 
1051         ctx->entry->expires = request_time + PG(user_ini_cache_ttl);
1052     }
1053 
1054     if (*fn_next) {
1055         rc = (*fn_next)(ctx, fn_next + 1);
1056     }
1057 
1058     return rc;
1059 }
1060 
lsapi_activate_user_ini_finally(_lsapi_activate_user_ini_ctx * ctx,void * next)1061 static int lsapi_activate_user_ini_finally(_lsapi_activate_user_ini_ctx *ctx,
1062                                            void* next)
1063 {
1064     int rc = SUCCESS;
1065     fn_activate_user_ini_chain_t *fn_next = next;
1066 
1067     php_ini_activate_config(&ctx->entry->user_config, PHP_INI_PERDIR,
1068                             PHP_INI_STAGE_HTACCESS);
1069 
1070     if (*fn_next) {
1071         rc = (*fn_next)(ctx, fn_next + 1);
1072     }
1073 
1074     return rc;
1075 }
1076 
lsapi_activate_user_ini(void)1077 static int lsapi_activate_user_ini( void )
1078 {
1079     _lsapi_activate_user_ini_ctx ctx;
1080     /**
1081      * The reason to have this function list stacked
1082      * is each function now can define a scoped destructor.
1083      *
1084      * Passing control via function pointer is a sign of low coupling,
1085      * which means dependencies between these functions are to be
1086      * controlled from a single place
1087      * (here below, by modifying this function list order)
1088      */
1089     static const fn_activate_user_ini_chain_t fn_chain[] = {
1090         &lsapi_activate_user_ini_basic_checks,
1091         &lsapi_activate_user_ini_mk_path,
1092         &lsapi_activate_user_ini_mk_realpath,
1093         &lsapi_activate_user_ini_mk_user_config,
1094         &lsapi_activate_user_ini_walk_down_the_path,
1095         &lsapi_activate_user_ini_finally,
1096         NULL
1097     };
1098 
1099     return fn_chain[0](&ctx, (fn_activate_user_ini_chain_t*)(fn_chain + 1));
1100 }
1101 
1102 
override_ini()1103 static void override_ini()
1104 {
1105 
1106     LSAPI_ForeachSpecialEnv( alter_ini, NULL );
1107 
1108 }
1109 
1110 
processReq(void)1111 static int processReq(void)
1112 {
1113     int ret = 0;
1114     zend_first_try {
1115 
1116         /* avoid server_context==NULL checks */
1117         SG(server_context) = (void *) 1;
1118 
1119         engine = 1;
1120         override_ini();
1121 
1122         if ( engine ) {
1123             init_request_info();
1124 
1125             if ( lsapi_module_main( source_highlight ) == -1 ) {
1126                 ret = -1;
1127             }
1128         } else {
1129             LSAPI_AppendRespHeader( "status: 403", 11 );
1130             LSAPI_AppendRespHeader( "content-type: text/html", 23 );
1131             LSAPI_Write( "Forbidden: PHP engine is disable.\n", 34 );
1132         }
1133     } zend_end_try();
1134     return ret;
1135 }
1136 
cli_usage(void)1137 static void cli_usage(void)
1138 {
1139     static const char * usage =
1140         "Usage: php\n"
1141         "      php -[b|c|n|h|i|q|s|v|?] [<file>] [args...]\n"
1142         "  Run in LSAPI mode, only '-b', '-s' and '-c' are effective\n"
1143         "  Run in Command Line Interpreter mode when parameters are specified\n"
1144         "\n"
1145         "  -b <address:port>|<port> Bind Path for external LSAPI Server mode\n"
1146         "  -c <path>|<file> Look for php.ini file in this directory\n"
1147         "  -n    No php.ini file will be used\n"
1148         "  -h    This help\n"
1149         "  -i    PHP information\n"
1150         "  -l    Syntax check\n"
1151         "  -q    Quiet-mode.  Suppress HTTP Header output.\n"
1152         "  -s    Display colour syntax highlighted source.\n"
1153         "  -v    Version number\n"
1154         "  -?    This help\n"
1155         "\n"
1156         "  args...    Arguments passed to script.\n";
1157     php_output_startup();
1158     php_output_activate();
1159     php_printf( "%s", usage );
1160 #ifdef PHP_OUTPUT_NEWAPI
1161     php_output_end_all();
1162 #else
1163     php_end_ob_buffers(1);
1164 #endif
1165 }
1166 
parse_opt(int argc,char * argv[],int * climode,char ** php_ini_path,char ** php_bind)1167 static int parse_opt( int argc, char * argv[], int *climode,
1168                         char **php_ini_path, char ** php_bind )
1169 {
1170     char ** p = &argv[1];
1171     char ** argend= &argv[argc];
1172     int c;
1173     while (( p < argend )&&(**p == '-' )) {
1174         c = *((*p)+1);
1175         ++p;
1176         switch( c ) {
1177         case 'b':
1178             if ( p >= argend ) {
1179                 fprintf( stderr, "TCP or socket address must be specified following '-b' option.\n");
1180                 return -1;
1181             }
1182             *php_bind = strdup(*p++);
1183             break;
1184 
1185         case 'c':
1186             if ( p >= argend ) {
1187                 fprintf( stderr, "<path> or <file> must be specified following '-c' option.\n");
1188 
1189                 return -1;
1190             }
1191             *php_ini_path = strdup( *p++ );
1192             break;
1193         case 's':
1194             source_highlight = 1;
1195             break;
1196         case 'n':
1197             ignore_php_ini = 1;
1198             break;
1199         case '?':
1200             if ( *((*(p-1))+2) == 's' )
1201                 exit( 99 );
1202         case 'h':
1203         case 'i':
1204         case 'l':
1205         case 'q':
1206         case 'v':
1207         default:
1208             *climode = 1;
1209             break;
1210         }
1211     }
1212     if ( p - argv < argc ) {
1213         *climode = 1;
1214     }
1215     return 0;
1216 }
1217 
cli_main(int argc,char * argv[])1218 static int cli_main( int argc, char * argv[] )
1219 {
1220 
1221     static const char * ini_defaults[] = {
1222         "display_errors",       "1",
1223         "register_argc_argv",   "1",
1224         "html_errors",          "0",
1225         "implicit_flush",       "1",
1226         "output_buffering",     "0",
1227         "max_execution_time",   "0",
1228         "max_input_time",       "-1",
1229         NULL
1230     };
1231 
1232     const char ** ini;
1233     char ** p = &argv[1];
1234     char ** argend= &argv[argc];
1235     int ret = -1;
1236     int c;
1237     zend_string *psKey;
1238     lsapi_mode = 0;        /* enter CLI mode */
1239 
1240     zend_first_try     {
1241         SG(server_context) = (void *) 1;
1242 
1243         zend_uv.html_errors = 0; /* tell the engine we're in non-html mode */
1244         CG(in_compilation) = 0; /* not initialized but needed for several options */
1245         SG(options) |= SAPI_OPTION_NO_CHDIR;
1246 
1247 #if PHP_MAJOR_VERSION < 7
1248         EG(uninitialized_zval_ptr) = NULL;
1249 #endif
1250         for( ini = ini_defaults; *ini; ini+=2 ) {
1251             psKey = zend_string_init(*ini, strlen( *ini ), 1);
1252             zend_alter_ini_entry_chars(psKey,
1253                                 (char *)*(ini+1), strlen( *(ini+1) ),
1254                                 PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
1255             zend_string_release_ex(psKey, 1);
1256         }
1257 
1258         while (( p < argend )&&(**p == '-' )) {
1259             c = *((*p)+1);
1260             ++p;
1261             switch( c ) {
1262             case 'q':
1263                 break;
1264             case 'i':
1265                 if (php_request_startup() != FAILURE) {
1266                     php_print_info(0xFFFFFFFF);
1267 #ifdef PHP_OUTPUT_NEWAPI
1268                     php_output_end_all();
1269 #else
1270                     php_end_ob_buffers(1);
1271 #endif
1272                     php_request_shutdown( NULL );
1273                     ret = 0;
1274                 }
1275                 break;
1276             case 'v':
1277                 if (php_request_startup() != FAILURE) {
1278 #if ZEND_DEBUG
1279                     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());
1280 #else
1281                     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());
1282 #endif
1283 #ifdef PHP_OUTPUT_NEWAPI
1284                     php_output_end_all();
1285 #else
1286                     php_end_ob_buffers(1);
1287 #endif
1288                     php_request_shutdown( NULL );
1289                     ret = 0;
1290                 }
1291                 break;
1292             case 'c':
1293                 ++p;
1294             /* fall through */
1295             case 's':
1296                 break;
1297             case 'l':
1298                 source_highlight = 2;
1299                 break;
1300             case 'h':
1301             case '?':
1302             default:
1303                 cli_usage();
1304                 ret = 0;
1305                 break;
1306 
1307             }
1308         }
1309         if ( ret == -1 ) {
1310             if ( *p ) {
1311                 zend_file_handle file_handle;
1312 				zend_stream_init_fp(&file_handle, VCWD_FOPEN(*p, "rb"), NULL);
1313 
1314                 if ( file_handle.handle.fp ) {
1315                     script_filename = *p;
1316                     php_self = *p;
1317 
1318                     SG(request_info).path_translated = estrdup(*p);
1319                     SG(request_info).argc = argc - (p - argv);
1320                     SG(request_info).argv = p;
1321 
1322                     if (php_request_startup() == FAILURE ) {
1323                         fclose( file_handle.handle.fp );
1324                         ret = 2;
1325                     } else {
1326                         if (source_highlight == 1) {
1327                             zend_syntax_highlighter_ini syntax_highlighter_ini;
1328 
1329                             php_get_highlight_struct(&syntax_highlighter_ini);
1330                             highlight_file(SG(request_info).path_translated, &syntax_highlighter_ini);
1331                         } else if (source_highlight == 2) {
1332                             file_handle.filename = *p;
1333                             file_handle.free_filename = 0;
1334                             file_handle.opened_path = NULL;
1335                             ret = php_lint_script(&file_handle);
1336                             if (ret==SUCCESS) {
1337                                 zend_printf("No syntax errors detected in %s\n", file_handle.filename);
1338                             } else {
1339                                 zend_printf("Errors parsing %s\n", file_handle.filename);
1340                             }
1341 
1342                         } else {
1343                             file_handle.filename = *p;
1344                             file_handle.free_filename = 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_procssed();
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