PageRenderTime 67ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/sapi/fpm/fpm/fpm_main.c

http://github.com/php/php-src
C | 2007 lines | 1455 code | 230 blank | 322 comment | 291 complexity | ba486828c257d50f8179b50f997d4159 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  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. | http://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
  14. | Stig Bakken <ssb@php.net> |
  15. | Zeev Suraski <zeev@php.net> |
  16. | FastCGI: Ben Mansell <php@slimyhorror.com> |
  17. | Shane Caraveo <shane@caraveo.com> |
  18. | Dmitry Stogov <dmitry@php.net> |
  19. +----------------------------------------------------------------------+
  20. */
  21. #include "php.h"
  22. #include "php_globals.h"
  23. #include "php_variables.h"
  24. #include "zend_modules.h"
  25. #include "php.h"
  26. #include "zend_ini_scanner.h"
  27. #include "zend_globals.h"
  28. #include "zend_stream.h"
  29. #include "SAPI.h"
  30. #include <stdio.h>
  31. #include "php.h"
  32. #if HAVE_SYS_TIME_H
  33. # include <sys/time.h>
  34. #endif
  35. #if HAVE_UNISTD_H
  36. # include <unistd.h>
  37. #endif
  38. #include <signal.h>
  39. #include <locale.h>
  40. #if HAVE_SYS_TYPES_H
  41. # include <sys/types.h>
  42. #endif
  43. #if HAVE_SYS_WAIT_H
  44. # include <sys/wait.h>
  45. #endif
  46. #if HAVE_FCNTL_H
  47. # include <fcntl.h>
  48. #endif
  49. #include "zend.h"
  50. #include "zend_extensions.h"
  51. #include "php_ini.h"
  52. #include "php_globals.h"
  53. #include "php_main.h"
  54. #include "fopen_wrappers.h"
  55. #include "ext/standard/php_standard.h"
  56. #ifdef __riscos__
  57. # include <unixlib/local.h>
  58. int __riscosify_control = __RISCOSIFY_STRICT_UNIX_SPECS;
  59. #endif
  60. #include "zend_compile.h"
  61. #include "zend_execute.h"
  62. #include "zend_highlight.h"
  63. #include "php_getopt.h"
  64. #include "http_status_codes.h"
  65. #include "fastcgi.h"
  66. #include <php_config.h>
  67. #include "fpm.h"
  68. #include "fpm_request.h"
  69. #include "fpm_status.h"
  70. #include "fpm_signals.h"
  71. #include "fpm_stdio.h"
  72. #include "fpm_conf.h"
  73. #include "fpm_php.h"
  74. #include "fpm_log.h"
  75. #include "zlog.h"
  76. /* XXX this will need to change later when threaded fastcgi is implemented. shane */
  77. struct sigaction act, old_term, old_quit, old_int;
  78. static void (*php_php_import_environment_variables)(zval *array_ptr);
  79. /* these globals used for forking children on unix systems */
  80. /**
  81. * Set to non-zero if we are the parent process
  82. */
  83. static int parent = 1;
  84. static int request_body_fd;
  85. static int fpm_is_running = 0;
  86. static char *sapi_cgibin_getenv(char *name, size_t name_len);
  87. static void fastcgi_ini_parser(zval *arg1, zval *arg2, zval *arg3, int callback_type, void *arg);
  88. #define PHP_MODE_STANDARD 1
  89. #define PHP_MODE_HIGHLIGHT 2
  90. #define PHP_MODE_INDENT 3
  91. #define PHP_MODE_LINT 4
  92. #define PHP_MODE_STRIP 5
  93. static char *php_optarg = NULL;
  94. static int php_optind = 1;
  95. static zend_module_entry cgi_module_entry;
  96. static const opt_struct OPTIONS[] = {
  97. {'c', 1, "php-ini"},
  98. {'d', 1, "define"},
  99. {'e', 0, "profile-info"},
  100. {'h', 0, "help"},
  101. {'i', 0, "info"},
  102. {'m', 0, "modules"},
  103. {'n', 0, "no-php-ini"},
  104. {'?', 0, "usage"},/* help alias (both '?' and 'usage') */
  105. {'v', 0, "version"},
  106. {'y', 1, "fpm-config"},
  107. {'t', 0, "test"},
  108. {'p', 1, "prefix"},
  109. {'g', 1, "pid"},
  110. {'R', 0, "allow-to-run-as-root"},
  111. {'D', 0, "daemonize"},
  112. {'F', 0, "nodaemonize"},
  113. {'O', 0, "force-stderr"},
  114. {'-', 0, NULL} /* end of args */
  115. };
  116. typedef struct _php_cgi_globals_struct {
  117. zend_bool rfc2616_headers;
  118. zend_bool nph;
  119. zend_bool fix_pathinfo;
  120. zend_bool force_redirect;
  121. zend_bool discard_path;
  122. zend_bool fcgi_logging;
  123. char *redirect_status_env;
  124. HashTable user_config_cache;
  125. char *error_header;
  126. char *fpm_config;
  127. } php_cgi_globals_struct;
  128. /* {{{ user_config_cache
  129. *
  130. * Key for each cache entry is dirname(PATH_TRANSLATED).
  131. *
  132. * NOTE: Each cache entry config_hash contains the combination from all user ini files found in
  133. * the path starting from doc_root through to dirname(PATH_TRANSLATED). There is no point
  134. * storing per-file entries as it would not be possible to detect added / deleted entries
  135. * between separate files.
  136. */
  137. typedef struct _user_config_cache_entry {
  138. time_t expires;
  139. HashTable *user_config;
  140. } user_config_cache_entry;
  141. static void user_config_cache_entry_dtor(zval *el)
  142. {
  143. user_config_cache_entry *entry = (user_config_cache_entry *)Z_PTR_P(el);
  144. zend_hash_destroy(entry->user_config);
  145. free(entry->user_config);
  146. free(entry);
  147. }
  148. /* }}} */
  149. #ifdef ZTS
  150. static int php_cgi_globals_id;
  151. #define CGIG(v) ZEND_TSRMG(php_cgi_globals_id, php_cgi_globals_struct *, v)
  152. #else
  153. static php_cgi_globals_struct php_cgi_globals;
  154. #define CGIG(v) (php_cgi_globals.v)
  155. #endif
  156. static int module_name_cmp(Bucket *f, Bucket *s) /* {{{ */
  157. {
  158. return strcasecmp( ((zend_module_entry *) Z_PTR(f->val))->name,
  159. ((zend_module_entry *) Z_PTR(s->val))->name);
  160. }
  161. /* }}} */
  162. static void print_modules(void) /* {{{ */
  163. {
  164. HashTable sorted_registry;
  165. zend_module_entry *module;
  166. zend_hash_init(&sorted_registry, 50, NULL, NULL, 1);
  167. zend_hash_copy(&sorted_registry, &module_registry, NULL);
  168. zend_hash_sort(&sorted_registry, module_name_cmp, 0);
  169. ZEND_HASH_FOREACH_PTR(&sorted_registry, module) {
  170. php_printf("%s\n", module->name);
  171. } ZEND_HASH_FOREACH_END();
  172. zend_hash_destroy(&sorted_registry);
  173. }
  174. /* }}} */
  175. static void print_extension_info(zend_extension *ext) /* {{{ */
  176. {
  177. php_printf("%s\n", ext->name);
  178. }
  179. /* }}} */
  180. static int extension_name_cmp(const zend_llist_element **f, const zend_llist_element **s) /* {{{ */
  181. {
  182. zend_extension *fe = (zend_extension*)(*f)->data;
  183. zend_extension *se = (zend_extension*)(*s)->data;
  184. return strcmp(fe->name, se->name);
  185. }
  186. /* }}} */
  187. static void print_extensions(void) /* {{{ */
  188. {
  189. zend_llist sorted_exts;
  190. zend_llist_copy(&sorted_exts, &zend_extensions);
  191. sorted_exts.dtor = NULL;
  192. zend_llist_sort(&sorted_exts, extension_name_cmp);
  193. zend_llist_apply(&sorted_exts, (llist_apply_func_t) print_extension_info);
  194. zend_llist_destroy(&sorted_exts);
  195. }
  196. /* }}} */
  197. #ifndef STDOUT_FILENO
  198. #define STDOUT_FILENO 1
  199. #endif
  200. static inline size_t sapi_cgibin_single_write(const char *str, uint32_t str_length) /* {{{ */
  201. {
  202. ssize_t ret;
  203. /* sapi has started which means everything must be send through fcgi */
  204. if (fpm_is_running) {
  205. fcgi_request *request = (fcgi_request*) SG(server_context);
  206. ret = fcgi_write(request, FCGI_STDOUT, str, str_length);
  207. if (ret <= 0) {
  208. return 0;
  209. }
  210. return (size_t)ret;
  211. }
  212. /* sapi has not started, output to stdout instead of fcgi */
  213. #ifdef PHP_WRITE_STDOUT
  214. ret = write(STDOUT_FILENO, str, str_length);
  215. if (ret <= 0) {
  216. return 0;
  217. }
  218. return (size_t)ret;
  219. #else
  220. return fwrite(str, 1, MIN(str_length, 16384), stdout);
  221. #endif
  222. }
  223. /* }}} */
  224. static size_t sapi_cgibin_ub_write(const char *str, size_t str_length) /* {{{ */
  225. {
  226. const char *ptr = str;
  227. uint32_t remaining = str_length;
  228. size_t ret;
  229. while (remaining > 0) {
  230. ret = sapi_cgibin_single_write(ptr, remaining);
  231. if (!ret) {
  232. php_handle_aborted_connection();
  233. return str_length - remaining;
  234. }
  235. ptr += ret;
  236. remaining -= ret;
  237. }
  238. return str_length;
  239. }
  240. /* }}} */
  241. static void sapi_cgibin_flush(void *server_context) /* {{{ */
  242. {
  243. /* fpm has started, let use fcgi instead of stdout */
  244. if (fpm_is_running) {
  245. fcgi_request *request = (fcgi_request*) server_context;
  246. if (!parent && request && !fcgi_flush(request, 0)) {
  247. php_handle_aborted_connection();
  248. }
  249. return;
  250. }
  251. /* fpm has not started yet, let use stdout instead of fcgi */
  252. if (fflush(stdout) == EOF) {
  253. php_handle_aborted_connection();
  254. }
  255. }
  256. /* }}} */
  257. #define SAPI_CGI_MAX_HEADER_LENGTH 1024
  258. static int sapi_cgi_send_headers(sapi_headers_struct *sapi_headers) /* {{{ */
  259. {
  260. char buf[SAPI_CGI_MAX_HEADER_LENGTH];
  261. sapi_header_struct *h;
  262. zend_llist_position pos;
  263. zend_bool ignore_status = 0;
  264. int response_status = SG(sapi_headers).http_response_code;
  265. if (SG(request_info).no_headers == 1) {
  266. return SAPI_HEADER_SENT_SUCCESSFULLY;
  267. }
  268. if (CGIG(nph) || SG(sapi_headers).http_response_code != 200)
  269. {
  270. int len;
  271. zend_bool has_status = 0;
  272. if (CGIG(rfc2616_headers) && SG(sapi_headers).http_status_line) {
  273. char *s;
  274. len = slprintf(buf, SAPI_CGI_MAX_HEADER_LENGTH, "%s\r\n", SG(sapi_headers).http_status_line);
  275. if ((s = strchr(SG(sapi_headers).http_status_line, ' '))) {
  276. response_status = atoi((s + 1));
  277. }
  278. if (len > SAPI_CGI_MAX_HEADER_LENGTH) {
  279. len = SAPI_CGI_MAX_HEADER_LENGTH;
  280. }
  281. } else {
  282. char *s;
  283. if (SG(sapi_headers).http_status_line &&
  284. (s = strchr(SG(sapi_headers).http_status_line, ' ')) != 0 &&
  285. (s - SG(sapi_headers).http_status_line) >= 5 &&
  286. strncasecmp(SG(sapi_headers).http_status_line, "HTTP/", 5) == 0
  287. ) {
  288. len = slprintf(buf, sizeof(buf), "Status:%s\r\n", s);
  289. response_status = atoi((s + 1));
  290. } else {
  291. h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
  292. while (h) {
  293. if (h->header_len > sizeof("Status:") - 1 &&
  294. strncasecmp(h->header, "Status:", sizeof("Status:") - 1) == 0
  295. ) {
  296. has_status = 1;
  297. break;
  298. }
  299. h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
  300. }
  301. if (!has_status) {
  302. http_response_status_code_pair *err = (http_response_status_code_pair*)http_status_map;
  303. while (err->code != 0) {
  304. if (err->code == SG(sapi_headers).http_response_code) {
  305. break;
  306. }
  307. err++;
  308. }
  309. if (err->str) {
  310. len = slprintf(buf, sizeof(buf), "Status: %d %s\r\n", SG(sapi_headers).http_response_code, err->str);
  311. } else {
  312. len = slprintf(buf, sizeof(buf), "Status: %d\r\n", SG(sapi_headers).http_response_code);
  313. }
  314. }
  315. }
  316. }
  317. if (!has_status) {
  318. PHPWRITE_H(buf, len);
  319. ignore_status = 1;
  320. }
  321. }
  322. h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
  323. while (h) {
  324. /* prevent CRLFCRLF */
  325. if (h->header_len) {
  326. if (h->header_len > sizeof("Status:") - 1 &&
  327. strncasecmp(h->header, "Status:", sizeof("Status:") - 1) == 0
  328. ) {
  329. if (!ignore_status) {
  330. ignore_status = 1;
  331. PHPWRITE_H(h->header, h->header_len);
  332. PHPWRITE_H("\r\n", 2);
  333. }
  334. } else if (response_status == 304 && h->header_len > sizeof("Content-Type:") - 1 &&
  335. strncasecmp(h->header, "Content-Type:", sizeof("Content-Type:") - 1) == 0
  336. ) {
  337. h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
  338. continue;
  339. } else {
  340. PHPWRITE_H(h->header, h->header_len);
  341. PHPWRITE_H("\r\n", 2);
  342. }
  343. }
  344. h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
  345. }
  346. PHPWRITE_H("\r\n", 2);
  347. return SAPI_HEADER_SENT_SUCCESSFULLY;
  348. }
  349. /* }}} */
  350. #ifndef STDIN_FILENO
  351. # define STDIN_FILENO 0
  352. #endif
  353. #ifndef HAVE_ATTRIBUTE_WEAK
  354. static void fpm_fcgi_log(int type, const char *fmt, ...) /* {{{ */
  355. #else
  356. void fcgi_log(int type, const char *fmt, ...)
  357. #endif
  358. {
  359. va_list args;
  360. va_start(args, fmt);
  361. vzlog("", 0, type, fmt, args);
  362. va_end(args);
  363. }
  364. /* }}} */
  365. static size_t sapi_cgi_read_post(char *buffer, size_t count_bytes) /* {{{ */
  366. {
  367. uint32_t read_bytes = 0;
  368. int tmp_read_bytes;
  369. size_t remaining = SG(request_info).content_length - SG(read_post_bytes);
  370. if (remaining < count_bytes) {
  371. count_bytes = remaining;
  372. }
  373. while (read_bytes < count_bytes) {
  374. fcgi_request *request = (fcgi_request*) SG(server_context);
  375. if (request_body_fd == -1) {
  376. char *request_body_filename = FCGI_GETENV(request, "REQUEST_BODY_FILE");
  377. if (request_body_filename && *request_body_filename) {
  378. request_body_fd = open(request_body_filename, O_RDONLY);
  379. if (0 > request_body_fd) {
  380. php_error(E_WARNING, "REQUEST_BODY_FILE: open('%s') failed: %s (%d)",
  381. request_body_filename, strerror(errno), errno);
  382. return 0;
  383. }
  384. }
  385. }
  386. /* If REQUEST_BODY_FILE variable not available - read post body from fastcgi stream */
  387. if (request_body_fd < 0) {
  388. tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes);
  389. } else {
  390. tmp_read_bytes = read(request_body_fd, buffer + read_bytes, count_bytes - read_bytes);
  391. }
  392. if (tmp_read_bytes <= 0) {
  393. break;
  394. }
  395. read_bytes += tmp_read_bytes;
  396. }
  397. return read_bytes;
  398. }
  399. /* }}} */
  400. static char *sapi_cgibin_getenv(char *name, size_t name_len) /* {{{ */
  401. {
  402. /* if fpm has started, use fcgi env */
  403. if (fpm_is_running) {
  404. fcgi_request *request = (fcgi_request*) SG(server_context);
  405. return fcgi_getenv(request, name, name_len);
  406. }
  407. /* if fpm has not started yet, use std env */
  408. return getenv(name);
  409. }
  410. /* }}} */
  411. #if 0
  412. static char *_sapi_cgibin_putenv(char *name, char *value) /* {{{ */
  413. {
  414. int name_len;
  415. if (!name) {
  416. return NULL;
  417. }
  418. name_len = strlen(name);
  419. fcgi_request *request = (fcgi_request*) SG(server_context);
  420. return fcgi_putenv(request, name, name_len, value);
  421. }
  422. /* }}} */
  423. #endif
  424. static char *sapi_cgi_read_cookies(void) /* {{{ */
  425. {
  426. fcgi_request *request = (fcgi_request*) SG(server_context);
  427. return FCGI_GETENV(request, "HTTP_COOKIE");
  428. }
  429. /* }}} */
  430. static void cgi_php_load_env_var(char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg) /* {{{ */
  431. {
  432. zval *array_ptr = (zval*)arg;
  433. int filter_arg = (Z_ARR_P(array_ptr) == Z_ARR(PG(http_globals)[TRACK_VARS_ENV]))?PARSE_ENV:PARSE_SERVER;
  434. size_t new_val_len;
  435. if (sapi_module.input_filter(filter_arg, var, &val, strlen(val), &new_val_len)) {
  436. php_register_variable_safe(var, val, new_val_len, array_ptr);
  437. }
  438. }
  439. /* }}} */
  440. void cgi_php_import_environment_variables(zval *array_ptr) /* {{{ */
  441. {
  442. fcgi_request *request = NULL;
  443. if (Z_TYPE(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY &&
  444. Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_ENV]) &&
  445. zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_ENV])) > 0
  446. ) {
  447. zend_array_destroy(Z_ARR_P(array_ptr));
  448. Z_ARR_P(array_ptr) = zend_array_dup(Z_ARR(PG(http_globals)[TRACK_VARS_ENV]));
  449. return;
  450. } else if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY &&
  451. Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_SERVER]) &&
  452. zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER])) > 0
  453. ) {
  454. zend_array_destroy(Z_ARR_P(array_ptr));
  455. Z_ARR_P(array_ptr) = zend_array_dup(Z_ARR(PG(http_globals)[TRACK_VARS_SERVER]));
  456. return;
  457. }
  458. /* call php's original import as a catch-all */
  459. php_php_import_environment_variables(array_ptr);
  460. request = (fcgi_request*) SG(server_context);
  461. fcgi_loadenv(request, cgi_php_load_env_var, array_ptr);
  462. }
  463. /* }}} */
  464. static void sapi_cgi_register_variables(zval *track_vars_array) /* {{{ */
  465. {
  466. size_t php_self_len;
  467. char *php_self;
  468. /* In CGI mode, we consider the environment to be a part of the server
  469. * variables
  470. */
  471. php_import_environment_variables(track_vars_array);
  472. if (CGIG(fix_pathinfo)) {
  473. char *script_name = SG(request_info).request_uri;
  474. unsigned int script_name_len = script_name ? strlen(script_name) : 0;
  475. char *path_info = sapi_cgibin_getenv("PATH_INFO", sizeof("PATH_INFO") - 1);
  476. unsigned int path_info_len = path_info ? strlen(path_info) : 0;
  477. php_self_len = script_name_len + path_info_len;
  478. php_self = emalloc(php_self_len + 1);
  479. /* Concat script_name and path_info into php_self */
  480. if (script_name) {
  481. memcpy(php_self, script_name, script_name_len + 1);
  482. }
  483. if (path_info) {
  484. memcpy(php_self + script_name_len, path_info, path_info_len + 1);
  485. }
  486. /* Build the special-case PHP_SELF variable for the CGI version */
  487. if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &php_self, php_self_len, &php_self_len)) {
  488. php_register_variable_safe("PHP_SELF", php_self, php_self_len, track_vars_array);
  489. }
  490. efree(php_self);
  491. } else {
  492. php_self = SG(request_info).request_uri ? SG(request_info).request_uri : "";
  493. php_self_len = strlen(php_self);
  494. if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &php_self, php_self_len, &php_self_len)) {
  495. php_register_variable_safe("PHP_SELF", php_self, php_self_len, track_vars_array);
  496. }
  497. }
  498. }
  499. /* }}} */
  500. /* {{{ sapi_cgi_log_fastcgi
  501. *
  502. * Ignore level, we want to send all messages through fastcgi
  503. */
  504. void sapi_cgi_log_fastcgi(int level, char *message, size_t len)
  505. {
  506. fcgi_request *request = (fcgi_request*) SG(server_context);
  507. /* message is written to FCGI_STDERR if following conditions are met:
  508. * - logging is enabled (fastcgi.logging in php.ini)
  509. * - we are currently dealing with a request
  510. * - the message is not empty
  511. * - the fcgi_write did not fail
  512. */
  513. if (CGIG(fcgi_logging) && request && message && len > 0
  514. && fcgi_write(request, FCGI_STDERR, message, len) < 0) {
  515. php_handle_aborted_connection();
  516. }
  517. }
  518. /* }}} */
  519. /* {{{ sapi_cgi_log_message
  520. */
  521. static void sapi_cgi_log_message(char *message, int syslog_type_int)
  522. {
  523. zlog_msg(ZLOG_NOTICE, "PHP message: ", message);
  524. }
  525. /* }}} */
  526. /* {{{ php_cgi_ini_activate_user_config
  527. */
  528. static void php_cgi_ini_activate_user_config(char *path, int path_len, const char *doc_root, int doc_root_len)
  529. {
  530. char *ptr;
  531. time_t request_time = sapi_get_request_time();
  532. user_config_cache_entry *entry = zend_hash_str_find_ptr(&CGIG(user_config_cache), path, path_len);
  533. /* Find cached config entry: If not found, create one */
  534. if (!entry) {
  535. entry = pemalloc(sizeof(user_config_cache_entry), 1);
  536. entry->expires = 0;
  537. entry->user_config = (HashTable *) pemalloc(sizeof(HashTable), 1);
  538. zend_hash_init(entry->user_config, 0, NULL, config_zval_dtor, 1);
  539. zend_hash_str_update_ptr(&CGIG(user_config_cache), path, path_len, entry);
  540. }
  541. /* Check whether cache entry has expired and rescan if it is */
  542. if (request_time > entry->expires) {
  543. char * real_path;
  544. int real_path_len;
  545. char *s1, *s2;
  546. int s_len;
  547. /* Clear the expired config */
  548. zend_hash_clean(entry->user_config);
  549. if (!IS_ABSOLUTE_PATH(path, path_len)) {
  550. real_path = tsrm_realpath(path, NULL);
  551. if (real_path == NULL) {
  552. return;
  553. }
  554. real_path_len = strlen(real_path);
  555. path = real_path;
  556. path_len = real_path_len;
  557. }
  558. if (path_len > doc_root_len) {
  559. s1 = (char *) doc_root;
  560. s2 = path;
  561. s_len = doc_root_len;
  562. } else {
  563. s1 = path;
  564. s2 = (char *) doc_root;
  565. s_len = path_len;
  566. }
  567. /* we have to test if path is part of DOCUMENT_ROOT.
  568. if it is inside the docroot, we scan the tree up to the docroot
  569. to find more user.ini, if not we only scan the current path.
  570. */
  571. if (strncmp(s1, s2, s_len) == 0) {
  572. ptr = s2 + doc_root_len;
  573. while ((ptr = strchr(ptr, DEFAULT_SLASH)) != NULL) {
  574. *ptr = 0;
  575. php_parse_user_ini_file(path, PG(user_ini_filename), entry->user_config);
  576. *ptr = '/';
  577. ptr++;
  578. }
  579. } else {
  580. php_parse_user_ini_file(path, PG(user_ini_filename), entry->user_config);
  581. }
  582. entry->expires = request_time + PG(user_ini_cache_ttl);
  583. }
  584. /* Activate ini entries with values from the user config hash */
  585. php_ini_activate_config(entry->user_config, PHP_INI_PERDIR, PHP_INI_STAGE_HTACCESS);
  586. }
  587. /* }}} */
  588. static int sapi_cgi_activate(void) /* {{{ */
  589. {
  590. fcgi_request *request = (fcgi_request*) SG(server_context);
  591. char *path, *doc_root, *server_name;
  592. uint32_t path_len, doc_root_len, server_name_len;
  593. /* PATH_TRANSLATED should be defined at this stage but better safe than sorry :) */
  594. if (!SG(request_info).path_translated) {
  595. return FAILURE;
  596. }
  597. if (php_ini_has_per_host_config()) {
  598. /* Activate per-host-system-configuration defined in php.ini and stored into configuration_hash during startup */
  599. server_name = FCGI_GETENV(request, "SERVER_NAME");
  600. /* SERVER_NAME should also be defined at this stage..but better check it anyway */
  601. if (server_name) {
  602. server_name_len = strlen(server_name);
  603. server_name = estrndup(server_name, server_name_len);
  604. zend_str_tolower(server_name, server_name_len);
  605. php_ini_activate_per_host_config(server_name, server_name_len);
  606. efree(server_name);
  607. }
  608. }
  609. if (php_ini_has_per_dir_config() ||
  610. (PG(user_ini_filename) && *PG(user_ini_filename))
  611. ) {
  612. /* Prepare search path */
  613. path_len = strlen(SG(request_info).path_translated);
  614. /* Make sure we have trailing slash! */
  615. if (!IS_SLASH(SG(request_info).path_translated[path_len])) {
  616. path = emalloc(path_len + 2);
  617. memcpy(path, SG(request_info).path_translated, path_len + 1);
  618. path_len = zend_dirname(path, path_len);
  619. path[path_len++] = DEFAULT_SLASH;
  620. } else {
  621. path = estrndup(SG(request_info).path_translated, path_len);
  622. path_len = zend_dirname(path, path_len);
  623. }
  624. path[path_len] = 0;
  625. /* Activate per-dir-system-configuration defined in php.ini and stored into configuration_hash during startup */
  626. php_ini_activate_per_dir_config(path, path_len); /* Note: for global settings sake we check from root to path */
  627. /* Load and activate user ini files in path starting from DOCUMENT_ROOT */
  628. if (PG(user_ini_filename) && *PG(user_ini_filename)) {
  629. /* Prefer CONTEXT_DOCUMENT_ROOT if set */
  630. doc_root = FCGI_GETENV(request, "CONTEXT_DOCUMENT_ROOT");
  631. doc_root = doc_root ? doc_root : FCGI_GETENV(request, "DOCUMENT_ROOT");
  632. /* DOCUMENT_ROOT should also be defined at this stage..but better check it anyway */
  633. if (doc_root) {
  634. doc_root_len = strlen(doc_root);
  635. if (doc_root_len > 0 && IS_SLASH(doc_root[doc_root_len - 1])) {
  636. --doc_root_len;
  637. }
  638. php_cgi_ini_activate_user_config(path, path_len, doc_root, doc_root_len);
  639. }
  640. }
  641. efree(path);
  642. }
  643. return SUCCESS;
  644. }
  645. /* }}} */
  646. static int sapi_cgi_deactivate(void) /* {{{ */
  647. {
  648. /* flush only when SAPI was started. The reasons are:
  649. 1. SAPI Deactivate is called from two places: module init and request shutdown
  650. 2. When the first call occurs and the request is not set up, flush fails on FastCGI.
  651. */
  652. if (SG(sapi_started)) {
  653. if (!parent && !fcgi_finish_request((fcgi_request*)SG(server_context), 0)) {
  654. php_handle_aborted_connection();
  655. }
  656. }
  657. return SUCCESS;
  658. }
  659. /* }}} */
  660. static int php_cgi_startup(sapi_module_struct *sapi_module) /* {{{ */
  661. {
  662. if (php_module_startup(sapi_module, &cgi_module_entry, 1) == FAILURE) {
  663. return FAILURE;
  664. }
  665. return SUCCESS;
  666. }
  667. /* }}} */
  668. /* {{{ sapi_module_struct cgi_sapi_module
  669. */
  670. static sapi_module_struct cgi_sapi_module = {
  671. "fpm-fcgi", /* name */
  672. "FPM/FastCGI", /* pretty name */
  673. php_cgi_startup, /* startup */
  674. php_module_shutdown_wrapper, /* shutdown */
  675. sapi_cgi_activate, /* activate */
  676. sapi_cgi_deactivate, /* deactivate */
  677. sapi_cgibin_ub_write, /* unbuffered write */
  678. sapi_cgibin_flush, /* flush */
  679. NULL, /* get uid */
  680. sapi_cgibin_getenv, /* getenv */
  681. php_error, /* error handler */
  682. NULL, /* header handler */
  683. sapi_cgi_send_headers, /* send headers handler */
  684. NULL, /* send header handler */
  685. sapi_cgi_read_post, /* read POST data */
  686. sapi_cgi_read_cookies, /* read Cookies */
  687. sapi_cgi_register_variables, /* register server variables */
  688. sapi_cgi_log_message, /* Log message */
  689. NULL, /* Get request time */
  690. NULL, /* Child terminate */
  691. STANDARD_SAPI_MODULE_PROPERTIES
  692. };
  693. /* }}} */
  694. /* {{{ php_cgi_usage
  695. */
  696. static void php_cgi_usage(char *argv0)
  697. {
  698. char *prog;
  699. prog = strrchr(argv0, '/');
  700. if (prog) {
  701. prog++;
  702. } else {
  703. prog = "php";
  704. }
  705. php_printf( "Usage: %s [-n] [-e] [-h] [-i] [-m] [-v] [-t] [-p <prefix>] [-g <pid>] [-c <file>] [-d foo[=bar]] [-y <file>] [-D] [-F [-O]]\n"
  706. " -c <path>|<file> Look for php.ini file in this directory\n"
  707. " -n No php.ini file will be used\n"
  708. " -d foo[=bar] Define INI entry foo with value 'bar'\n"
  709. " -e Generate extended information for debugger/profiler\n"
  710. " -h This help\n"
  711. " -i PHP information\n"
  712. " -m Show compiled in modules\n"
  713. " -v Version number\n"
  714. " -p, --prefix <dir>\n"
  715. " Specify alternative prefix path to FastCGI process manager (default: %s).\n"
  716. " -g, --pid <file>\n"
  717. " Specify the PID file location.\n"
  718. " -y, --fpm-config <file>\n"
  719. " Specify alternative path to FastCGI process manager config file.\n"
  720. " -t, --test Test FPM configuration and exit\n"
  721. " -D, --daemonize force to run in background, and ignore daemonize option from config file\n"
  722. " -F, --nodaemonize\n"
  723. " force to stay in foreground, and ignore daemonize option from config file\n"
  724. " -O, --force-stderr\n"
  725. " force output to stderr in nodaemonize even if stderr is not a TTY\n"
  726. " -R, --allow-to-run-as-root\n"
  727. " Allow pool to run as root (disabled by default)\n",
  728. prog, PHP_PREFIX);
  729. }
  730. /* }}} */
  731. /* {{{ is_valid_path
  732. *
  733. * some server configurations allow '..' to slip through in the
  734. * translated path. We'll just refuse to handle such a path.
  735. */
  736. static int is_valid_path(const char *path)
  737. {
  738. const char *p;
  739. if (!path) {
  740. return 0;
  741. }
  742. p = strstr(path, "..");
  743. if (p) {
  744. if ((p == path || IS_SLASH(*(p-1))) &&
  745. (*(p+2) == 0 || IS_SLASH(*(p+2)))
  746. ) {
  747. return 0;
  748. }
  749. while (1) {
  750. p = strstr(p+1, "..");
  751. if (!p) {
  752. break;
  753. }
  754. if (IS_SLASH(*(p-1)) &&
  755. (*(p+2) == 0 || IS_SLASH(*(p+2)))
  756. ) {
  757. return 0;
  758. }
  759. }
  760. }
  761. return 1;
  762. }
  763. /* }}} */
  764. /* {{{ init_request_info
  765. initializes request_info structure
  766. specifically in this section we handle proper translations
  767. for:
  768. PATH_INFO
  769. derived from the portion of the URI path following
  770. the script name but preceding any query data
  771. may be empty
  772. PATH_TRANSLATED
  773. derived by taking any path-info component of the
  774. request URI and performing any virtual-to-physical
  775. translation appropriate to map it onto the server's
  776. document repository structure
  777. empty if PATH_INFO is empty
  778. The env var PATH_TRANSLATED **IS DIFFERENT** than the
  779. request_info.path_translated variable, the latter should
  780. match SCRIPT_FILENAME instead.
  781. SCRIPT_NAME
  782. set to a URL path that could identify the CGI script
  783. rather than the interpreter. PHP_SELF is set to this
  784. REQUEST_URI
  785. uri section following the domain:port part of a URI
  786. SCRIPT_FILENAME
  787. The virtual-to-physical translation of SCRIPT_NAME (as per
  788. PATH_TRANSLATED)
  789. These settings are documented at
  790. http://cgi-spec.golux.com/
  791. Based on the following URL request:
  792. http://localhost/info.php/test?a=b
  793. should produce, which btw is the same as if
  794. we were running under mod_cgi on apache (ie. not
  795. using ScriptAlias directives):
  796. PATH_INFO=/test
  797. PATH_TRANSLATED=/docroot/test
  798. SCRIPT_NAME=/info.php
  799. REQUEST_URI=/info.php/test?a=b
  800. SCRIPT_FILENAME=/docroot/info.php
  801. QUERY_STRING=a=b
  802. but what we get is (cgi/mod_fastcgi under apache):
  803. PATH_INFO=/info.php/test
  804. PATH_TRANSLATED=/docroot/info.php/test
  805. SCRIPT_NAME=/php/php-cgi (from the Action setting I suppose)
  806. REQUEST_URI=/info.php/test?a=b
  807. SCRIPT_FILENAME=/path/to/php/bin/php-cgi (Action setting translated)
  808. QUERY_STRING=a=b
  809. Comments in the code below refer to using the above URL in a request
  810. */
  811. static void init_request_info(void)
  812. {
  813. fcgi_request *request = (fcgi_request*) SG(server_context);
  814. char *env_script_filename = FCGI_GETENV(request, "SCRIPT_FILENAME");
  815. char *env_path_translated = FCGI_GETENV(request, "PATH_TRANSLATED");
  816. char *script_path_translated = env_script_filename;
  817. char *ini;
  818. int apache_was_here = 0;
  819. /* some broken servers do not have script_filename or argv0
  820. * an example, IIS configured in some ways. then they do more
  821. * broken stuff and set path_translated to the cgi script location */
  822. if (!script_path_translated && env_path_translated) {
  823. script_path_translated = env_path_translated;
  824. }
  825. /* initialize the defaults */
  826. SG(request_info).path_translated = NULL;
  827. SG(request_info).request_method = NULL;
  828. SG(request_info).proto_num = 1000;
  829. SG(request_info).query_string = NULL;
  830. SG(request_info).request_uri = NULL;
  831. SG(request_info).content_type = NULL;
  832. SG(request_info).content_length = 0;
  833. SG(sapi_headers).http_response_code = 200;
  834. /* script_path_translated being set is a good indication that
  835. * we are running in a cgi environment, since it is always
  836. * null otherwise. otherwise, the filename
  837. * of the script will be retrieved later via argc/argv */
  838. if (script_path_translated) {
  839. const char *auth;
  840. char *content_length = FCGI_GETENV(request, "CONTENT_LENGTH");
  841. char *content_type = FCGI_GETENV(request, "CONTENT_TYPE");
  842. char *env_path_info = FCGI_GETENV(request, "PATH_INFO");
  843. char *env_script_name = FCGI_GETENV(request, "SCRIPT_NAME");
  844. /* Hack for buggy IIS that sets incorrect PATH_INFO */
  845. char *env_server_software = FCGI_GETENV(request, "SERVER_SOFTWARE");
  846. if (env_server_software &&
  847. env_script_name &&
  848. env_path_info &&
  849. strncmp(env_server_software, "Microsoft-IIS", sizeof("Microsoft-IIS") - 1) == 0 &&
  850. strncmp(env_path_info, env_script_name, strlen(env_script_name)) == 0
  851. ) {
  852. env_path_info = FCGI_PUTENV(request, "ORIG_PATH_INFO", env_path_info);
  853. env_path_info += strlen(env_script_name);
  854. if (*env_path_info == 0) {
  855. env_path_info = NULL;
  856. }
  857. env_path_info = FCGI_PUTENV(request, "PATH_INFO", env_path_info);
  858. }
  859. #define APACHE_PROXY_FCGI_PREFIX "proxy:fcgi://"
  860. #define APACHE_PROXY_BALANCER_PREFIX "proxy:balancer://"
  861. /* Fix proxy URLs in SCRIPT_FILENAME generated by Apache mod_proxy_fcgi and mod_proxy_balancer:
  862. * proxy:fcgi://localhost:9000/some-dir/info.php/test?foo=bar
  863. * proxy:balancer://localhost:9000/some-dir/info.php/test?foo=bar
  864. * should be changed to:
  865. * /some-dir/info.php/test
  866. * See: http://bugs.php.net/bug.php?id=54152
  867. * http://bugs.php.net/bug.php?id=62172
  868. * https://issues.apache.org/bugzilla/show_bug.cgi?id=50851
  869. */
  870. if (env_script_filename &&
  871. strncasecmp(env_script_filename, APACHE_PROXY_FCGI_PREFIX, sizeof(APACHE_PROXY_FCGI_PREFIX) - 1) == 0) {
  872. /* advance to first character of hostname */
  873. char *p = env_script_filename + (sizeof(APACHE_PROXY_FCGI_PREFIX) - 1);
  874. while (*p != '\0' && *p != '/') {
  875. p++; /* move past hostname and port */
  876. }
  877. if (*p != '\0') {
  878. /* Copy path portion in place to avoid memory leak. Note
  879. * that this also affects what script_path_translated points
  880. * to. */
  881. memmove(env_script_filename, p, strlen(p) + 1);
  882. apache_was_here = 1;
  883. }
  884. /* ignore query string if sent by Apache (RewriteRule) */
  885. p = strchr(env_script_filename, '?');
  886. if (p) {
  887. *p =0;
  888. }
  889. }
  890. if (env_script_filename &&
  891. strncasecmp(env_script_filename, APACHE_PROXY_BALANCER_PREFIX, sizeof(APACHE_PROXY_BALANCER_PREFIX) - 1) == 0) {
  892. /* advance to first character of hostname */
  893. char *p = env_script_filename + (sizeof(APACHE_PROXY_BALANCER_PREFIX) - 1);
  894. while (*p != '\0' && *p != '/') {
  895. p++; /* move past hostname and port */
  896. }
  897. if (*p != '\0') {
  898. /* Copy path portion in place to avoid memory leak. Note
  899. * that this also affects what script_path_translated points
  900. * to. */
  901. memmove(env_script_filename, p, strlen(p) + 1);
  902. apache_was_here = 1;
  903. }
  904. /* ignore query string if sent by Apache (RewriteRule) */
  905. p = strchr(env_script_filename, '?');
  906. if (p) {
  907. *p =0;
  908. }
  909. }
  910. if (CGIG(fix_pathinfo)) {
  911. struct stat st;
  912. char *real_path = NULL;
  913. char *env_redirect_url = FCGI_GETENV(request, "REDIRECT_URL");
  914. char *env_document_root = FCGI_GETENV(request, "DOCUMENT_ROOT");
  915. char *orig_path_translated = env_path_translated;
  916. char *orig_path_info = env_path_info;
  917. char *orig_script_name = env_script_name;
  918. char *orig_script_filename = env_script_filename;
  919. int script_path_translated_len;
  920. if (!env_document_root && PG(doc_root)) {
  921. env_document_root = FCGI_PUTENV(request, "DOCUMENT_ROOT", PG(doc_root));
  922. }
  923. if (!apache_was_here && env_path_translated != NULL && env_redirect_url != NULL &&
  924. env_path_translated != script_path_translated &&
  925. strcmp(env_path_translated, script_path_translated) != 0) {
  926. /*
  927. * pretty much apache specific. If we have a redirect_url
  928. * then our script_filename and script_name point to the
  929. * php executable
  930. * we don't want to do this for the new mod_proxy_fcgi approach,
  931. * where redirect_url may also exist but the below will break
  932. * with rewrites to PATH_INFO, hence the !apache_was_here check
  933. */
  934. script_path_translated = env_path_translated;
  935. /* we correct SCRIPT_NAME now in case we don't have PATH_INFO */
  936. env_script_name = env_redirect_url;
  937. }
  938. #ifdef __riscos__
  939. /* Convert path to unix format*/
  940. __riscosify_control |= __RISCOSIFY_DONT_CHECK_DIR;
  941. script_path_translated = __unixify(script_path_translated, 0, NULL, 1, 0);
  942. #endif
  943. /*
  944. * if the file doesn't exist, try to extract PATH_INFO out
  945. * of it by stat'ing back through the '/'
  946. * this fixes url's like /info.php/test
  947. */
  948. if (script_path_translated &&
  949. (script_path_translated_len = strlen(script_path_translated)) > 0 &&
  950. (script_path_translated[script_path_translated_len-1] == '/' ||
  951. (real_path = tsrm_realpath(script_path_translated, NULL)) == NULL)
  952. ) {
  953. char *pt = estrndup(script_path_translated, script_path_translated_len);
  954. int len = script_path_translated_len;
  955. char *ptr;
  956. if (pt) {
  957. while ((ptr = strrchr(pt, '/')) || (ptr = strrchr(pt, '\\'))) {
  958. *ptr = 0;
  959. if (stat(pt, &st) == 0 && S_ISREG(st.st_mode)) {
  960. /*
  961. * okay, we found the base script!
  962. * work out how many chars we had to strip off;
  963. * then we can modify PATH_INFO
  964. * accordingly
  965. *
  966. * we now have the makings of
  967. * PATH_INFO=/test
  968. * SCRIPT_FILENAME=/docroot/info.php
  969. *
  970. * we now need to figure out what docroot is.
  971. * if DOCUMENT_ROOT is set, this is easy, otherwise,
  972. * we have to play the game of hide and seek to figure
  973. * out what SCRIPT_NAME should be
  974. */
  975. int ptlen = strlen(pt);
  976. int slen = len - ptlen;
  977. int pilen = env_path_info ? strlen(env_path_info) : 0;
  978. int tflag = 0;
  979. char *path_info;
  980. if (apache_was_here) {
  981. /* recall that PATH_INFO won't exist */
  982. path_info = script_path_translated + ptlen;
  983. tflag = (slen != 0 && (!orig_path_info || strcmp(orig_path_info, path_info) != 0));
  984. } else {
  985. path_info = (env_path_info && pilen > slen) ? env_path_info + pilen - slen : NULL;
  986. tflag = path_info && (orig_path_info != path_info);
  987. }
  988. if (tflag) {
  989. if (orig_path_info) {
  990. char old;
  991. FCGI_PUTENV(request, "ORIG_PATH_INFO", orig_path_info);
  992. old = path_info[0];
  993. path_info[0] = 0;
  994. if (!orig_script_name ||
  995. strcmp(orig_script_name, env_path_info) != 0) {
  996. if (orig_script_name) {
  997. FCGI_PUTENV(request, "ORIG_SCRIPT_NAME", orig_script_name);
  998. }
  999. SG(request_info).request_uri = FCGI_PUTENV(request, "SCRIPT_NAME", env_path_info);
  1000. } else {
  1001. SG(request_info).request_uri = orig_script_name;
  1002. }
  1003. path_info[0] = old;
  1004. } else if (apache_was_here && env_script_name) {
  1005. /* Using mod_proxy_fcgi and ProxyPass, apache cannot set PATH_INFO
  1006. * As we can extract PATH_INFO from PATH_TRANSLATED
  1007. * it is probably also in SCRIPT_NAME and need to be removed
  1008. */
  1009. int snlen = strlen(env_script_name);
  1010. if (snlen>slen && !strcmp(env_script_name+snlen-slen, path_info)) {
  1011. FCGI_PUTENV(request, "ORIG_SCRIPT_NAME", orig_script_name);
  1012. env_script_name[snlen-slen] = 0;
  1013. SG(request_info).request_uri = FCGI_PUTENV(request, "SCRIPT_NAME", env_script_name);
  1014. }
  1015. }
  1016. env_path_info = FCGI_PUTENV(request, "PATH_INFO", path_info);
  1017. }
  1018. if (!orig_script_filename ||
  1019. strcmp(orig_script_filename, pt) != 0) {
  1020. if (orig_script_filename) {
  1021. FCGI_PUTENV(request, "ORIG_SCRIPT_FILENAME", orig_script_filename);
  1022. }
  1023. script_path_translated = FCGI_PUTENV(request, "SCRIPT_FILENAME", pt);
  1024. }
  1025. /* figure out docroot
  1026. * SCRIPT_FILENAME minus SCRIPT_NAME
  1027. */
  1028. if (env_document_root) {
  1029. int l = strlen(env_document_root);
  1030. int path_translated_len = 0;
  1031. char *path_translated = NULL;
  1032. if (l && env_document_root[l - 1] == '/') {
  1033. --l;
  1034. }
  1035. /* we have docroot, so we should have:
  1036. * DOCUMENT_ROOT=/docroot
  1037. * SCRIPT_FILENAME=/docroot/info.php
  1038. */
  1039. /* PATH_TRANSLATED = DOCUMENT_ROOT + PATH_INFO */
  1040. path_translated_len = l + (env_path_info ? strlen(env_path_info) : 0);
  1041. path_translated = (char *) emalloc(path_translated_len + 1);
  1042. memcpy(path_translated, env_document_root, l);
  1043. if (env_path_info) {
  1044. memcpy(path_translated + l, env_path_info, (path_translated_len - l));
  1045. }
  1046. path_translated[path_translated_len] = '\0';
  1047. if (orig_path_translated) {
  1048. FCGI_PUTENV(request, "ORIG_PATH_TRANSLATED", orig_path_translated);
  1049. }
  1050. env_path_translated = FCGI_PUTENV(request, "PATH_TRANSLATED", path_translated);
  1051. efree(path_translated);
  1052. } else if ( env_script_name &&
  1053. strstr(pt, env_script_name)
  1054. ) {
  1055. /* PATH_TRANSLATED = PATH_TRANSLATED - SCRIPT_NAME + PATH_INFO */
  1056. int ptlen = strlen(pt) - strlen(env_script_name);
  1057. int path_translated_len = ptlen + (env_path_info ? strlen(env_path_info) : 0);
  1058. char *path_translated = NULL;
  1059. path_translated = (char *) emalloc(path_translated_len + 1);
  1060. memcpy(path_translated, pt, ptlen);
  1061. if (env_path_info) {
  1062. memcpy(path_translated + ptlen, env_path_info, path_translated_len - ptlen);
  1063. }
  1064. path_translated[path_translated_len] = '\0';
  1065. if (orig_path_translated) {
  1066. FCGI_PUTENV(request, "ORIG_PATH_TRANSLATED", orig_path_translated);
  1067. }
  1068. env_path_translated = FCGI_PUTENV(request, "PATH_TRANSLATED", path_translated);
  1069. efree(path_translated);
  1070. }
  1071. break;
  1072. }
  1073. }
  1074. } else {
  1075. ptr = NULL;
  1076. }
  1077. if (!ptr) {
  1078. /*
  1079. * if we stripped out all the '/' and still didn't find
  1080. * a valid path... we will fail, badly. of course we would
  1081. * have failed anyway... we output 'no input file' now.
  1082. */
  1083. if (orig_script_filename) {
  1084. FCGI_PUTENV(request, "ORIG_SCRIPT_FILENAME", orig_script_filename);
  1085. }
  1086. script_path_translated = FCGI_PUTENV(request, "SCRIPT_FILENAME", NULL);
  1087. SG(sapi_headers).http_response_code = 404;
  1088. }
  1089. if (!SG(request_info).request_uri) {
  1090. if (!orig_script_name ||
  1091. strcmp(orig_script_name, env_script_name) != 0) {
  1092. if (orig_script_name) {
  1093. FCGI_PUTENV(request, "ORIG_SCRIPT_NAME", orig_script_name);
  1094. }
  1095. SG(request_info).request_uri = FCGI_PUTENV(request, "SCRIPT_NAME", env_script_name);
  1096. } else {
  1097. SG(request_info).request_uri = orig_script_name;
  1098. }
  1099. }
  1100. if (pt) {
  1101. efree(pt);
  1102. }
  1103. } else {
  1104. /* make sure original values are remembered in ORIG_ copies if we've changed them */
  1105. if (!orig_script_filename ||
  1106. (script_path_translated != orig_script_filename &&
  1107. strcmp(script_path_translated, orig_script_filename) != 0)) {
  1108. if (orig_script_filename) {
  1109. FCGI_PUTENV(request, "ORIG_SCRIPT_FILENAME", orig_script_filename);
  1110. }
  1111. script_path_translated = FCGI_PUTENV(request, "SCRIPT_FILENAME", script_path_translated);
  1112. }
  1113. if (!apache_was_here && env_redirect_url) {
  1114. /* if we used PATH_TRANSLATED to work around Apache mod_fastcgi (but not mod_proxy_fcgi,
  1115. * hence !apache_was_here) weirdness, strip info accordingly */
  1116. if (orig_path_info) {
  1117. FCGI_PUTENV(request, "ORIG_PATH_INFO", orig_path_info);
  1118. FCGI_PUTENV(request, "PATH_INFO", NULL);
  1119. }
  1120. if (orig_path_translated) {
  1121. FCGI_PUTENV(request, "ORIG_PATH_TRANSLATED", orig_path_translated);
  1122. FCGI_PUTENV(request, "PATH_TRANSLATED", NULL);
  1123. }
  1124. }
  1125. if (env_script_name != orig_script_name) {
  1126. if (orig_script_name) {
  1127. FCGI_PUTENV(request, "ORIG_SCRIPT_NAME", orig_script_name);
  1128. }
  1129. SG(request_info).request_uri = FCGI_PUTENV(request, "SCRIPT_NAME", env_script_name);
  1130. } else {
  1131. SG(request_info).request_uri = env_script_name;
  1132. }
  1133. efree(real_path);
  1134. }
  1135. } else {
  1136. /* pre 4.3 behaviour, shouldn't be used but provides BC */
  1137. if (env_path_info) {
  1138. SG(request_info).request_uri = env_path_info;
  1139. } else {
  1140. SG(request_info).request_uri = env_script_name;
  1141. }
  1142. if (!CGIG(discard_path) && env_path_translated) {
  1143. script_path_translated = env_path_translated;
  1144. }
  1145. }
  1146. if (is_valid_path(script_path_translated)) {
  1147. SG(request_info).path_translated = estrdup(script_path_translated);
  1148. }
  1149. SG(request_info).request_method = FCGI_GETENV(request, "REQUEST_METHOD");
  1150. /* FIXME - Work out proto_num here */
  1151. SG(request_info).query_string = FCGI_GETENV(request, "QUERY_STRING");
  1152. SG(request_info).content_type = (content_type ? content_type : "" );
  1153. SG(request_info).content_length = (content_length ? atol(content_length) : 0);
  1154. /* The CGI RFC allows servers to pass on unvalidated Authorization data */
  1155. auth = FCGI_GETENV(request, "HTTP_AUTHORIZATION");
  1156. php_handle_auth_data(auth);
  1157. }
  1158. /* INI stuff */
  1159. ini = FCGI_GETENV(request, "PHP_VALUE");
  1160. if (ini) {
  1161. int mode = ZEND_INI_USER;
  1162. char *tmp;
  1163. spprintf(&tmp, 0, "%s\n", ini);
  1164. zend_parse_ini_string(tmp, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t)fastcgi_ini_parser, &mode);
  1165. efree(tmp);
  1166. }
  1167. ini = FCGI_GETENV(request, "PHP_ADMIN_VALUE");
  1168. if (ini) {
  1169. int mode = ZEND_INI_SYSTEM;
  1170. char *tmp;
  1171. spprintf(&tmp, 0, "%s\n", ini);
  1172. zend_parse_ini_string(tmp, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t)fastcgi_ini_parser, &mode);
  1173. efree(tmp);
  1174. }
  1175. }
  1176. /* }}} */
  1177. static fcgi_request *fpm_init_request(int listen_fd) /* {{{ */ {
  1178. fcgi_request *req = fcgi_init_request(listen_fd,
  1179. fpm_request_accepting,
  1180. fpm_request_reading_headers,
  1181. fpm_request_finished);
  1182. return req;
  1183. }
  1184. /* }}} */
  1185. static void fastcgi_ini_parser(zval *arg1, zval *arg2, zval *arg3, int callback_type, void *arg) /* {{{ */
  1186. {
  1187. int *mode = (int *)arg;
  1188. char *key;
  1189. char *value = NULL;
  1190. struct key_value_s kv;
  1191. if (!mode || !arg1) return;
  1192. if (callback_type != ZEND_INI_PARSER_ENTRY) {
  1193. zlog(ZLOG_ERROR, "Passing INI directive through FastCGI: only classic entries are allowed");
  1194. return;
  1195. }
  1196. key = Z_STRVAL_P(arg1);
  1197. if (!key || strlen(key) < 1) {
  1198. zlog(ZLOG_ERROR, "Passing INI directive through FastCGI: empty key");
  1199. return;
  1200. }
  1201. if (arg2) {
  1202. value = Z_STRVAL_P(arg2);
  1203. }
  1204. if (!value) {
  1205. zlog(ZLOG_ERROR, "Passing INI directive through FastCGI: empty value for key '%s'", key);
  1206. return;
  1207. }
  1208. kv.key = key;
  1209. kv.value = value;
  1210. kv.next = NULL;
  1211. if (fpm_php_apply_defines_ex(&kv, *mode) == -1) {
  1212. zlog(ZLOG_ERROR, "Passing INI directive through FastCGI: unable to set '%s'", key);
  1213. }
  1214. }
  1215. /* }}} */
  1216. PHP_INI_BEGIN()
  1217. STD_PHP_INI_ENTRY("cgi.rfc2616_headers", "0", PHP_INI_ALL, OnUpdateBool, rfc2616_headers, php_cgi_globals_struct, php_cgi_globals)
  1218. STD_PHP_INI_ENTRY("cgi.nph", "0", PHP_INI_ALL, OnUpdateBool, nph, php_cgi_globals_struct, php_cgi_globals)
  1219. STD_PHP_INI_ENTRY("cgi.force_redirect", "1", PHP_INI_SYSTEM, OnUpdateBool, force_redirect, php_cgi_globals_struct, php_cgi_globals)
  1220. STD_PHP_INI_ENTRY("cgi.redirect_status_env", NULL, PHP_INI_SYSTEM, OnUpdateString, redirect_status_env, php_cgi_globals_struct, php_cgi_globals)
  1221. STD_PHP_INI_ENTRY("cgi.fix_pathinfo", "1", PHP_INI_SYSTEM, OnUpdateBool, fix_pathinfo, php_cgi_globals_struct, php_cgi_globals)
  1222. STD_PHP_INI_ENTRY("cgi.discard_path", "0", PHP_INI_SYSTEM, OnUpdateBool, discard_path, php_cgi_globals_struct, php_cgi_globals)
  1223. STD_PHP_INI_ENTRY("fastcgi.logging", "1", PHP_INI_SYSTEM, OnUpdateBool, fcgi_logging, php_cgi_globals_struct, php_cgi_globals)
  1224. STD_PHP_INI_ENTRY("fastcgi.error_header", NULL, PHP_INI_SYSTEM, OnUpdateString, error_header, php_cgi_globals_struct, php_cgi_globals)
  1225. STD_PHP_INI_ENTRY("fpm.config", NULL, PHP_INI_SYSTEM, OnUpdateString, fpm_config, php_cgi_globals_struct, php_cgi_globals)
  1226. PHP_INI_END()
  1227. /* {{{ php_cgi_globals_ctor
  1228. */
  1229. static void php_cgi_globals_ctor(php_cgi_globals_struct *php_cgi_globals)
  1230. {
  1231. php_cgi_globals->rfc2616_headers = 0;
  1232. php_cgi_globals->nph = 0;
  1233. php_cgi_globals->force_redirect = 1;
  1234. php_cgi_globals->redirect_status_env = NULL;
  1235. php_cgi_globals->fix_pathinfo = 1;
  1236. php_cgi_globals->discard_path = 0;
  1237. php_cgi_globals->fcgi_logging = 1;
  1238. zend_hash_init(&php_cgi_globals->user_config_cache, 0, NULL, user_config_cache_entry_dtor, 1);
  1239. php_cgi_globals->error_header = NULL;
  1240. php_cgi_globals->fpm_config = NULL;
  1241. }
  1242. /* }}} */
  1243. /* {{{ PHP_MINIT_FUNCTION
  1244. */
  1245. static PHP_MINIT_FUNCTION(cgi)
  1246. {
  1247. #ifdef ZTS
  1248. ts_allocate_id(&php_cgi_globals_id, sizeof(php_cgi_globals_struct), (ts_allocate_ctor) php_cgi_globals_ctor, NULL);
  1249. #else
  1250. php_cgi_globals_ctor(&php_cgi_globals);
  1251. #endif
  1252. REGISTER_INI_ENTRIES();
  1253. return SUCCESS;
  1254. }
  1255. /* }}} */
  1256. /* {{{ PHP_MSHUTDOWN_FUNCTION
  1257. */
  1258. static PHP_MSHUTDOWN_FUNCTION(cgi)
  1259. {
  1260. zend_hash_destroy(&CGIG(user_config_cache));
  1261. UNREGISTER_INI_ENTRIES();
  1262. return SUCCESS;
  1263. }
  1264. /* }}} */
  1265. /* {{{ PHP_MINFO_FUNCTION
  1266. */
  1267. static PHP_MINFO_FUNCTION(cgi)
  1268. {
  1269. php_info_print_table_start();
  1270. php_info_print_table_row(2, "php-fpm", "active");
  1271. php_info_print_table_end();
  1272. DISPLAY_INI_ENTRIES();
  1273. }
  1274. /* }}} */
  1275. ZEND_BEGIN_ARG_INFO(cgi_fcgi_sapi_no_arginfo, 0)
  1276. ZEND_END_ARG_INFO()
  1277. PHP_FUNCTION(fastcgi_finish_request) /* {{{ */
  1278. {
  1279. fcgi_request *request = (fcgi_request*) SG(server_context);
  1280. if (zend_parse_parameters_none() == FAILURE) {
  1281. RETURN_THROWS();
  1282. }
  1283. if (!fcgi_is_closed(request)) {
  1284. php_output_end_all();
  1285. php_header();
  1286. fcgi_end(request);
  1287. fcgi_close(request, 0, 0);
  1288. RETURN_TRUE;
  1289. }
  1290. RETURN_FALSE;
  1291. }
  1292. /* }}} */
  1293. PHP_FUNCTION(apache_request_headers) /* {{{ */
  1294. {
  1295. fcgi_request *request;
  1296. if (zend_parse_parameters_none() == FAILURE) {
  1297. RETURN_THROWS();
  1298. }
  1299. array_init(return_value);
  1300. if ((request = (fcgi_request*) SG(server_context))) {
  1301. fcgi_loadenv(request, sapi_add_request_header, return_value);
  1302. }
  1303. } /* }}} */
  1304. /* {{{ proto array fpm_get_status
  1305. * Returns the status of the fastcgi process manager */
  1306. PHP_FUNCTION(fpm_get_status) /* {{{ */
  1307. {
  1308. if (zend_parse_parameters_none() == FAILURE) {
  1309. RETURN_THROWS();
  1310. }
  1311. if (fpm_status_export_to_zval(return_value)) {
  1312. RETURN_FALSE;
  1313. }
  1314. }
  1315. /* }}} */
  1316. static const zend_function_entry cgi_fcgi_sapi_functions[] = {
  1317. PHP_FE(fastcgi_finish_request, cgi_fcgi_sapi_no_arginfo)
  1318. PHP_FE(fpm_get_status, cgi_fcgi_sapi_no_arginfo)
  1319. PHP_FE(apache_request_headers, cgi_fcgi_sapi_no_arginfo)
  1320. PHP_FALIAS(getallheaders, apache_request_headers, cgi_fcgi_sapi_no_arginfo)
  1321. PHP_FE_END
  1322. };
  1323. static zend_module_entry cgi_module_entry = {
  1324. STANDARD_MODULE_HEADER,
  1325. "cgi-fcgi",
  1326. cgi_fcgi_sapi_functions,
  1327. PHP_MINIT(cgi),
  1328. PHP_MSHUTDOWN(cgi),
  1329. NULL,
  1330. NULL,
  1331. PHP_MINFO(cgi),
  1332. PHP_VERSION,
  1333. STANDARD_MODULE_PROPERTIES
  1334. };
  1335. /* {{{ main
  1336. */
  1337. int main(int argc, char *argv[])
  1338. {
  1339. int exit_status = FPM_EXIT_OK;
  1340. int cgi = 0, c, use_extended_info = 0;
  1341. zend_file_handle file_handle;
  1342. /* temporary locals */
  1343. int orig_optind = php_optind;
  1344. char *orig_optarg = php_optarg;
  1345. int ini_entries_len = 0;
  1346. /* end of temporary locals */
  1347. int max_requests = 0;
  1348. int requests = 0;
  1349. int fcgi_fd = 0;
  1350. fcgi_request *request;
  1351. char *fpm_config = NULL;
  1352. char *fpm_prefix = NULL;
  1353. char *fpm_pid = NULL;
  1354. int test_conf = 0;
  1355. int force_daemon = -1;
  1356. int force_stderr = 0;
  1357. int php_information = 0;
  1358. int php_allow_to_run_as_root = 0;
  1359. int ret;
  1360. #if ZEND_RC_DEBUG
  1361. zend_bool old_rc_debug;
  1362. #endif
  1363. #if defined(SIGPIPE) && defined(SIG_IGN)
  1364. signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
  1365. that sockets created via fsockopen()
  1366. don't kill PHP if the remote site
  1367. closes it. in apache|apxs mode apache
  1368. does that for us! thies@thieso.net
  1369. 20000419 */
  1370. if (0 > fpm_signals_init_mask() || 0 > fpm_signals_block()) {
  1371. zlog(ZLOG_WARNING, "Could die in the case of too early reload signal");
  1372. }
  1373. zlog(ZLOG_DEBUG, "Blocked some signals");
  1374. #endif
  1375. #ifdef ZTS
  1376. php_tsrm_startup();
  1377. #endif
  1378. zend_signal_startup();
  1379. sapi_startup(&cgi_sapi_module);
  1380. cgi_sapi_module.php_ini_path_override = NULL;
  1381. cgi_sapi_module.php_ini_ignore_cwd = 1;
  1382. #ifndef HAVE_ATTRIBUTE_WEAK
  1383. fcgi_set_logger(fpm_fcgi_log);
  1384. #endif
  1385. fcgi_init();
  1386. while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
  1387. switch (c) {
  1388. case 'c':
  1389. if (cgi_sapi_module.php_ini_path_override) {
  1390. free(cgi_sapi_module.php_ini_path_override);
  1391. }
  1392. cgi_sapi_module.php_ini_path_override = strdup(php_optarg);
  1393. break;
  1394. case 'n':
  1395. cgi_sapi_module.php_ini_ignore = 1;
  1396. break;
  1397. case 'd': {
  1398. /* define ini entries on command line */
  1399. int len = strlen(php_optarg);
  1400. char *val;
  1401. if ((val = strchr(php_optarg, '='))) {
  1402. val++;
  1403. if (!isalnum(*val) && *val != '"' && *val != '\'' && *val != '\0') {
  1404. cgi_sapi_module.ini_entries = realloc(cgi_sapi_module.ini_entries, ini_entries_len + len + sizeof("\"\"\n\0"));
  1405. memcpy(cgi_sapi_module.ini_entries + ini_entries_len, php_optarg, (val - php_optarg));
  1406. ini_entries_len += (val - php_optarg);
  1407. memcpy(cgi_sapi_module.ini_entries + ini_entries_len, "\"", 1);
  1408. ini_entries_len++;
  1409. memcpy(cgi_sapi_module.ini_entries + ini_entries_len, val, len - (val - php_optarg));
  1410. ini_entries_len += len - (val - php_optarg);

Large files files are truncated, but you can click here to view the full file