lsapi_main.c 49 KB

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