lsapi_main.c 50 KB

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