You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

642 lines
18 KiB

23 years ago
23 years ago
21 years ago
23 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2006 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 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. | Authors: Sascha Schumann <sascha@schumann.cx> |
  16. | Parts based on Apache 1.3 SAPI module by |
  17. | Rasmus Lerdorf and Zeev Suraski |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id$ */
  21. #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
  22. #include "php.h"
  23. #include "php_main.h"
  24. #include "php_ini.h"
  25. #include "php_variables.h"
  26. #include "SAPI.h"
  27. #include <fcntl.h>
  28. #include "ext/standard/php_smart_str.h"
  29. #ifndef NETWARE
  30. #include "ext/standard/php_standard.h"
  31. #else
  32. #include "ext/standard/basic_functions.h"
  33. #endif
  34. #include "apr_strings.h"
  35. #include "ap_config.h"
  36. #include "util_filter.h"
  37. #include "httpd.h"
  38. #include "http_config.h"
  39. #include "http_request.h"
  40. #include "http_core.h"
  41. #include "http_protocol.h"
  42. #include "http_log.h"
  43. #include "http_main.h"
  44. #include "util_script.h"
  45. #include "http_core.h"
  46. #include "ap_mpm.h"
  47. #include "php_apache.h"
  48. /* UnixWare and Netware define shutdown to _shutdown, which causes problems later
  49. * on when using a structure member named shutdown. Since this source
  50. * file does not use the system call shutdown, it is safe to #undef it.K
  51. */
  52. #undef shutdown
  53. #define PHP_MAGIC_TYPE "application/x-httpd-php"
  54. #define PHP_SOURCE_MAGIC_TYPE "application/x-httpd-php-source"
  55. #define PHP_SCRIPT "php5-script"
  56. /* A way to specify the location of the php.ini dir in an apache directive */
  57. char *apache2_php_ini_path_override = NULL;
  58. static int
  59. php_apache_sapi_ub_write(const char *str, uint str_length TSRMLS_DC)
  60. {
  61. request_rec *r;
  62. php_struct *ctx;
  63. ctx = SG(server_context);
  64. r = ctx->r;
  65. if (ap_rwrite(str, str_length, r) < 0) {
  66. php_handle_aborted_connection();
  67. }
  68. return str_length; /* we always consume all the data passed to us. */
  69. }
  70. static int
  71. php_apache_sapi_header_handler(sapi_header_struct *sapi_header,sapi_headers_struct *sapi_headers TSRMLS_DC)
  72. {
  73. php_struct *ctx;
  74. char *val, *ptr;
  75. ctx = SG(server_context);
  76. val = strchr(sapi_header->header, ':');
  77. if (!val) {
  78. sapi_free_header(sapi_header);
  79. return 0;
  80. }
  81. ptr = val;
  82. *val = '\0';
  83. do {
  84. val++;
  85. } while (*val == ' ');
  86. if (!strcasecmp(sapi_header->header, "content-type")) {
  87. if (ctx->content_type) {
  88. efree(ctx->content_type);
  89. }
  90. ctx->content_type = estrdup(val);
  91. } else if (sapi_header->replace) {
  92. apr_table_set(ctx->r->headers_out, sapi_header->header, val);
  93. } else {
  94. apr_table_add(ctx->r->headers_out, sapi_header->header, val);
  95. }
  96. *ptr = ':';
  97. return SAPI_HEADER_ADD;
  98. }
  99. static int
  100. php_apache_sapi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
  101. {
  102. php_struct *ctx = SG(server_context);
  103. const char *sline = SG(sapi_headers).http_status_line;
  104. ctx->r->status = SG(sapi_headers).http_response_code;
  105. /* httpd requires that r->status_line is set to the first digit of
  106. * the status-code: */
  107. if (sline && strlen(sline) > 12 && strncmp(sline, "HTTP/1.", 7) == 0
  108. && sline[8] == ' ') {
  109. ctx->r->status_line = apr_pstrdup(ctx->r->pool, sline + 9);
  110. }
  111. /* call ap_set_content_type only once, else each time we call it,
  112. configured output filters for that content type will be added */
  113. if (!ctx->content_type) {
  114. ctx->content_type = sapi_get_default_content_type(TSRMLS_C);
  115. }
  116. ap_set_content_type(ctx->r, apr_pstrdup(ctx->r->pool, ctx->content_type));
  117. efree(ctx->content_type);
  118. ctx->content_type = NULL;
  119. return SAPI_HEADER_SENT_SUCCESSFULLY;
  120. }
  121. static int
  122. php_apache_sapi_read_post(char *buf, uint count_bytes TSRMLS_DC)
  123. {
  124. apr_size_t len, tlen=0;
  125. php_struct *ctx = SG(server_context);
  126. request_rec *r;
  127. apr_bucket_brigade *brigade;
  128. r = ctx->r;
  129. brigade = ctx->brigade;
  130. len = count_bytes;
  131. /*
  132. * This loop is needed because ap_get_brigade() can return us partial data
  133. * which would cause premature termination of request read. Therefor we
  134. * need to make sure that if data is available we fill the buffer completely.
  135. */
  136. while (ap_get_brigade(r->input_filters, brigade, AP_MODE_READBYTES, APR_BLOCK_READ, len) == APR_SUCCESS) {
  137. apr_brigade_flatten(brigade, buf, &len);
  138. apr_brigade_cleanup(brigade);
  139. tlen += len;
  140. if (tlen == count_bytes || !len) {
  141. break;
  142. }
  143. buf += len;
  144. len = count_bytes - tlen;
  145. }
  146. return tlen;
  147. }
  148. static struct stat*
  149. php_apache_sapi_get_stat(TSRMLS_D)
  150. {
  151. php_struct *ctx = SG(server_context);
  152. ctx->finfo.st_uid = ctx->r->finfo.user;
  153. ctx->finfo.st_gid = ctx->r->finfo.group;
  154. ctx->finfo.st_dev = ctx->r->finfo.device;
  155. ctx->finfo.st_ino = ctx->r->finfo.inode;
  156. #if defined(NETWARE) && defined(CLIB_STAT_PATCH)
  157. ctx->finfo.st_atime.tv_sec = apr_time_sec(ctx->r->finfo.atime);
  158. ctx->finfo.st_mtime.tv_sec = apr_time_sec(ctx->r->finfo.mtime);
  159. ctx->finfo.st_ctime.tv_sec = apr_time_sec(ctx->r->finfo.ctime);
  160. #else
  161. ctx->finfo.st_atime = apr_time_sec(ctx->r->finfo.atime);
  162. ctx->finfo.st_mtime = apr_time_sec(ctx->r->finfo.mtime);
  163. ctx->finfo.st_ctime = apr_time_sec(ctx->r->finfo.ctime);
  164. #endif
  165. ctx->finfo.st_size = ctx->r->finfo.size;
  166. ctx->finfo.st_nlink = ctx->r->finfo.nlink;
  167. return &ctx->finfo;
  168. }
  169. static char *
  170. php_apache_sapi_read_cookies(TSRMLS_D)
  171. {
  172. php_struct *ctx = SG(server_context);
  173. const char *http_cookie;
  174. http_cookie = apr_table_get(ctx->r->headers_in, "cookie");
  175. /* The SAPI interface should use 'const char *' */
  176. return (char *) http_cookie;
  177. }
  178. static char *
  179. php_apache_sapi_getenv(char *name, size_t name_len TSRMLS_DC)
  180. {
  181. php_struct *ctx = SG(server_context);
  182. const char *env_var;
  183. env_var = apr_table_get(ctx->r->subprocess_env, name);
  184. return (char *) env_var;
  185. }
  186. static void
  187. php_apache_sapi_register_variables(zval *track_vars_array TSRMLS_DC)
  188. {
  189. php_struct *ctx = SG(server_context);
  190. const apr_array_header_t *arr = apr_table_elts(ctx->r->subprocess_env);
  191. char *key, *val;
  192. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  193. if (!val) val = "";
  194. php_register_variable(key, val, track_vars_array TSRMLS_CC);
  195. APR_ARRAY_FOREACH_CLOSE()
  196. php_register_variable("PHP_SELF", ctx->r->uri, track_vars_array TSRMLS_CC);
  197. }
  198. static void
  199. php_apache_sapi_flush(void *server_context)
  200. {
  201. php_struct *ctx;
  202. request_rec *r;
  203. TSRMLS_FETCH();
  204. ctx = server_context;
  205. /* If we haven't registered a server_context yet,
  206. * then don't bother flushing. */
  207. if (!server_context) {
  208. return;
  209. }
  210. r = ctx->r;
  211. sapi_send_headers(TSRMLS_C);
  212. r->status = SG(sapi_headers).http_response_code;
  213. SG(headers_sent) = 1;
  214. if (ap_rflush(r) < 0 || r->connection->aborted) {
  215. php_handle_aborted_connection();
  216. }
  217. }
  218. static void php_apache_sapi_log_message(char *msg)
  219. {
  220. php_struct *ctx;
  221. TSRMLS_FETCH();
  222. ctx = SG(server_context);
  223. if (ctx == NULL) { /* we haven't initialized our ctx yet, oh well */
  224. ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, NULL, "%s", msg);
  225. } else {
  226. ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, "%s", msg);
  227. }
  228. }
  229. static void php_apache_sapi_log_message_ex(char *msg, request_rec *r)
  230. {
  231. if (r) {
  232. ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, msg, r->filename);
  233. } else {
  234. php_apache_sapi_log_message(msg);
  235. }
  236. }
  237. static time_t php_apache_sapi_get_request_time(TSRMLS_D) {
  238. php_struct *ctx = SG(server_context);
  239. return apr_time_sec(ctx->r->request_time);
  240. }
  241. extern zend_module_entry php_apache_module;
  242. static int php_apache2_startup(sapi_module_struct *sapi_module)
  243. {
  244. if (php_module_startup(sapi_module, &php_apache_module, 1)==FAILURE) {
  245. return FAILURE;
  246. }
  247. return SUCCESS;
  248. }
  249. static sapi_module_struct apache2_sapi_module = {
  250. "apache2handler",
  251. "Apache 2.0 Handler",
  252. php_apache2_startup, /* startup */
  253. php_module_shutdown_wrapper, /* shutdown */
  254. NULL, /* activate */
  255. NULL, /* deactivate */
  256. php_apache_sapi_ub_write, /* unbuffered write */
  257. php_apache_sapi_flush, /* flush */
  258. php_apache_sapi_get_stat, /* get uid */
  259. php_apache_sapi_getenv, /* getenv */
  260. php_error, /* error handler */
  261. php_apache_sapi_header_handler, /* header handler */
  262. php_apache_sapi_send_headers, /* send headers handler */
  263. NULL, /* send header handler */
  264. php_apache_sapi_read_post, /* read POST data */
  265. php_apache_sapi_read_cookies, /* read Cookies */
  266. php_apache_sapi_register_variables,
  267. php_apache_sapi_log_message, /* Log message */
  268. php_apache_sapi_get_request_time, /* Request Time */
  269. STANDARD_SAPI_MODULE_PROPERTIES
  270. };
  271. static apr_status_t
  272. php_apache_server_shutdown(void *tmp)
  273. {
  274. apache2_sapi_module.shutdown(&apache2_sapi_module);
  275. sapi_shutdown();
  276. #ifdef ZTS
  277. tsrm_shutdown();
  278. #endif
  279. return APR_SUCCESS;
  280. }
  281. static void php_apache_add_version(apr_pool_t *p)
  282. {
  283. TSRMLS_FETCH();
  284. if (PG(expose_php)) {
  285. ap_add_version_component(p, "PHP/" PHP_VERSION);
  286. }
  287. }
  288. static int php_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
  289. {
  290. #ifndef ZTS
  291. int threaded_mpm;
  292. ap_mpm_query(AP_MPMQ_IS_THREADED, &threaded_mpm);
  293. if(threaded_mpm) {
  294. ap_log_error(APLOG_MARK, APLOG_CRIT, 0, 0, "Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP.");
  295. return DONE;
  296. }
  297. #endif
  298. /* When this is NULL, apache won't override the hard-coded default
  299. * php.ini path setting. */
  300. apache2_php_ini_path_override = NULL;
  301. return OK;
  302. }
  303. static int
  304. php_apache_server_startup(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
  305. {
  306. void *data = NULL;
  307. const char *userdata_key = "apache2hook_post_config";
  308. /* Apache will load, unload and then reload a DSO module. This
  309. * prevents us from starting PHP until the second load. */
  310. apr_pool_userdata_get(&data, userdata_key, s->process->pool);
  311. if (data == NULL) {
  312. /* We must use set() here and *not* setn(), otherwise the
  313. * static string pointed to by userdata_key will be mapped
  314. * to a different location when the DSO is reloaded and the
  315. * pointers won't match, causing get() to return NULL when
  316. * we expected it to return non-NULL. */
  317. apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
  318. return OK;
  319. }
  320. /* Set up our overridden path. */
  321. if (apache2_php_ini_path_override) {
  322. apache2_sapi_module.php_ini_path_override = apache2_php_ini_path_override;
  323. }
  324. #ifdef ZTS
  325. tsrm_startup(1, 1, 0, NULL);
  326. #endif
  327. sapi_startup(&apache2_sapi_module);
  328. apache2_sapi_module.startup(&apache2_sapi_module);
  329. apr_pool_cleanup_register(pconf, NULL, php_apache_server_shutdown, apr_pool_cleanup_null);
  330. php_apache_add_version(pconf);
  331. return OK;
  332. }
  333. static apr_status_t php_server_context_cleanup(void *data_)
  334. {
  335. void **data = data_;
  336. *data = NULL;
  337. return APR_SUCCESS;
  338. }
  339. static int php_apache_request_ctor(request_rec *r, php_struct *ctx TSRMLS_DC)
  340. {
  341. char *content_length;
  342. const char *auth;
  343. SG(sapi_headers).http_response_code = !r->status ? HTTP_OK : r->status;
  344. SG(request_info).content_type = apr_table_get(r->headers_in, "Content-Type");
  345. SG(request_info).query_string = apr_pstrdup(r->pool, r->args);
  346. SG(request_info).request_method = r->method;
  347. SG(request_info).proto_num = r->proto_num;
  348. SG(request_info).request_uri = apr_pstrdup(r->pool, r->uri);
  349. SG(request_info).path_translated = apr_pstrdup(r->pool, r->filename);
  350. r->no_local_copy = 1;
  351. content_length = (char *) apr_table_get(r->headers_in, "Content-Length");
  352. SG(request_info).content_length = (content_length ? atoi(content_length) : 0);
  353. apr_table_unset(r->headers_out, "Content-Length");
  354. apr_table_unset(r->headers_out, "Last-Modified");
  355. apr_table_unset(r->headers_out, "Expires");
  356. apr_table_unset(r->headers_out, "ETag");
  357. if (!PG(safe_mode) || (PG(safe_mode) && !ap_auth_type(r))) {
  358. auth = apr_table_get(r->headers_in, "Authorization");
  359. php_handle_auth_data(auth TSRMLS_CC);
  360. ctx->r->user = apr_pstrdup(ctx->r->pool, SG(request_info).auth_user);
  361. } else {
  362. SG(request_info).auth_user = NULL;
  363. SG(request_info).auth_password = NULL;
  364. }
  365. return php_request_startup(TSRMLS_C);
  366. }
  367. static void php_apache_request_dtor(request_rec *r TSRMLS_DC)
  368. {
  369. php_request_shutdown(NULL);
  370. }
  371. static void php_apache_ini_dtor(request_rec *r, request_rec *p TSRMLS_DC)
  372. {
  373. if (strcmp(r->protocol, "INCLUDED")) {
  374. zend_try { zend_ini_deactivate(TSRMLS_C); } zend_end_try();
  375. }
  376. if (p) {
  377. ((php_struct *)SG(server_context))->r = p;
  378. } else {
  379. apr_pool_cleanup_run(r->pool, (void *)&SG(server_context), php_server_context_cleanup);
  380. }
  381. }
  382. static int php_handler(request_rec *r)
  383. {
  384. php_struct * volatile ctx;
  385. void *conf;
  386. apr_bucket_brigade * volatile brigade;
  387. apr_bucket *bucket;
  388. apr_status_t rv;
  389. request_rec * volatile parent_req = NULL;
  390. TSRMLS_FETCH();
  391. #define PHPAP_INI_OFF php_apache_ini_dtor(r, parent_req TSRMLS_CC);
  392. conf = ap_get_module_config(r->per_dir_config, &php5_module);
  393. /* apply_config() needs r in some cases, so allocate server_context early */
  394. ctx = SG(server_context);
  395. if (ctx == NULL || (ctx && ctx->request_processed && !strcmp(r->protocol, "INCLUDED"))) {
  396. normal:
  397. ctx = SG(server_context) = apr_pcalloc(r->pool, sizeof(*ctx));
  398. /* register a cleanup so we clear out the SG(server_context)
  399. * after each request. Note: We pass in the pointer to the
  400. * server_context in case this is handled by a different thread.
  401. */
  402. apr_pool_cleanup_register(r->pool, (void *)&SG(server_context), php_server_context_cleanup, apr_pool_cleanup_null);
  403. ctx->r = r;
  404. ctx = NULL; /* May look weird to null it here, but it is to catch the right case in the first_try later on */
  405. } else {
  406. parent_req = ctx->r;
  407. ctx->r = r;
  408. }
  409. apply_config(conf);
  410. if (strcmp(r->handler, PHP_MAGIC_TYPE) && strcmp(r->handler, PHP_SOURCE_MAGIC_TYPE) && strcmp(r->handler, PHP_SCRIPT)) {
  411. /* Check for xbithack in this case. */
  412. if (!AP2(xbithack) || strcmp(r->handler, "text/html") || !(r->finfo.protection & APR_UEXECUTE)) {
  413. PHPAP_INI_OFF;
  414. return DECLINED;
  415. }
  416. }
  417. /* Give a 404 if PATH_INFO is used but is explicitly disabled in
  418. * the configuration; default behaviour is to accept. */
  419. if (r->used_path_info == AP_REQ_REJECT_PATH_INFO
  420. && r->path_info && r->path_info[0]) {
  421. PHPAP_INI_OFF;
  422. return HTTP_NOT_FOUND;
  423. }
  424. /* handle situations where user turns the engine off */
  425. if (!AP2(engine)) {
  426. PHPAP_INI_OFF;
  427. return DECLINED;
  428. }
  429. if (r->finfo.filetype == 0) {
  430. php_apache_sapi_log_message_ex("script '%s' not found or unable to stat", r);
  431. PHPAP_INI_OFF;
  432. return HTTP_NOT_FOUND;
  433. }
  434. if (r->finfo.filetype == APR_DIR) {
  435. php_apache_sapi_log_message_ex("attempt to invoke directory '%s' as script", r);
  436. PHPAP_INI_OFF;
  437. return HTTP_FORBIDDEN;
  438. }
  439. /* Setup the CGI variables if this is the main request */
  440. if (r->main == NULL ||
  441. /* .. or if the sub-request environment differs from the main-request. */
  442. r->subprocess_env != r->main->subprocess_env
  443. ) {
  444. /* setup standard CGI variables */
  445. ap_add_common_vars(r);
  446. ap_add_cgi_vars(r);
  447. }
  448. zend_first_try {
  449. if (ctx == NULL) {
  450. brigade = apr_brigade_create(r->pool, r->connection->bucket_alloc);
  451. ctx = SG(server_context);
  452. ctx->brigade = brigade;
  453. if (php_apache_request_ctor(r, ctx TSRMLS_CC)!=SUCCESS) {
  454. zend_bailout();
  455. }
  456. } else {
  457. if (!parent_req) {
  458. parent_req = ctx->r;
  459. }
  460. if (parent_req && parent_req->handler &&
  461. strcmp(parent_req->handler, PHP_MAGIC_TYPE) &&
  462. strcmp(parent_req->handler, PHP_SOURCE_MAGIC_TYPE) &&
  463. strcmp(parent_req->handler, PHP_SCRIPT)) {
  464. if (php_apache_request_ctor(r, ctx TSRMLS_CC)!=SUCCESS) {
  465. zend_bailout();
  466. }
  467. }
  468. /* check if comming due to ErrorDocument */
  469. if (parent_req && parent_req->status != HTTP_OK) {
  470. parent_req = NULL;
  471. goto normal;
  472. }
  473. ctx->r = r;
  474. brigade = ctx->brigade;
  475. }
  476. if (AP2(last_modified)) {
  477. ap_update_mtime(r, r->finfo.mtime);
  478. ap_set_last_modified(r);
  479. }
  480. /* Determine if we need to parse the file or show the source */
  481. if (strncmp(r->handler, PHP_SOURCE_MAGIC_TYPE, sizeof(PHP_SOURCE_MAGIC_TYPE) - 1) == 0) {
  482. zend_syntax_highlighter_ini syntax_highlighter_ini;
  483. php_get_highlight_struct(&syntax_highlighter_ini);
  484. highlight_file((char *)r->filename, &syntax_highlighter_ini TSRMLS_CC);
  485. } else {
  486. zend_file_handle zfd;
  487. zfd.type = ZEND_HANDLE_FILENAME;
  488. zfd.filename = (char *) r->filename;
  489. zfd.free_filename = 0;
  490. zfd.opened_path = NULL;
  491. if (!parent_req) {
  492. php_execute_script(&zfd TSRMLS_CC);
  493. } else {
  494. zend_execute_scripts(ZEND_INCLUDE TSRMLS_CC, NULL, 1, &zfd);
  495. }
  496. #if MEMORY_LIMIT
  497. {
  498. char *mem_usage;
  499. mem_usage = apr_psprintf(ctx->r->pool, "%u", zend_memory_peak_usage(1 TSRMLS_CC));
  500. apr_table_set(r->notes, "mod_php_memory_usage", mem_usage);
  501. }
  502. #endif
  503. }
  504. } zend_end_try();
  505. if (!parent_req) {
  506. php_apache_request_dtor(r TSRMLS_CC);
  507. ctx->request_processed = 1;
  508. bucket = apr_bucket_eos_create(r->connection->bucket_alloc);
  509. APR_BRIGADE_INSERT_TAIL(brigade, bucket);
  510. rv = ap_pass_brigade(r->output_filters, brigade);
  511. if (rv != APR_SUCCESS || r->connection->aborted) {
  512. zend_first_try {
  513. php_handle_aborted_connection();
  514. } zend_end_try();
  515. }
  516. apr_brigade_cleanup(brigade);
  517. } else {
  518. ctx->r = parent_req;
  519. }
  520. return OK;
  521. }
  522. void php_ap2_register_hook(apr_pool_t *p)
  523. {
  524. ap_hook_pre_config(php_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
  525. ap_hook_post_config(php_apache_server_startup, NULL, NULL, APR_HOOK_MIDDLE);
  526. ap_hook_handler(php_handler, NULL, NULL, APR_HOOK_MIDDLE);
  527. }
  528. /*
  529. * Local variables:
  530. * tab-width: 4
  531. * c-basic-offset: 4
  532. * End:
  533. * vim600: sw=4 ts=4 fdm=marker
  534. * vim<600: sw=4 ts=4
  535. */