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