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