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.

605 lines
17 KiB

23 years ago
23 years ago
23 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2004 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.0 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_0.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. apr_bucket *bucket;
  62. apr_bucket_brigade *brigade;
  63. request_rec *r;
  64. php_struct *ctx;
  65. char *copy_str;
  66. if (str_length == 0) {
  67. return 0;
  68. }
  69. ctx = SG(server_context);
  70. r = ctx->r;
  71. brigade = ctx->brigade;
  72. copy_str = apr_pmemdup(r->pool, str, str_length);
  73. bucket = apr_bucket_pool_create(copy_str, str_length, r->pool, r->connection->bucket_alloc);
  74. APR_BRIGADE_INSERT_TAIL(brigade, bucket);
  75. if (ap_pass_brigade(r->output_filters, brigade) != APR_SUCCESS || r->connection->aborted) {
  76. php_handle_aborted_connection();
  77. }
  78. /* Ensure this brigade is empty for the next usage. */
  79. apr_brigade_cleanup(brigade);
  80. return str_length; /* we always consume all the data passed to us. */
  81. }
  82. static int
  83. php_apache_sapi_header_handler(sapi_header_struct *sapi_header,sapi_headers_struct *sapi_headers TSRMLS_DC)
  84. {
  85. php_struct *ctx;
  86. char *val;
  87. ctx = SG(server_context);
  88. val = strchr(sapi_header->header, ':');
  89. if (!val) {
  90. sapi_free_header(sapi_header);
  91. return 0;
  92. }
  93. *val = '\0';
  94. do {
  95. val++;
  96. } while (*val == ' ');
  97. if (!strcasecmp(sapi_header->header, "content-type")) {
  98. val = apr_pstrdup(ctx->r->pool, val);
  99. ap_set_content_type(ctx->r, val);
  100. } else if (sapi_header->replace) {
  101. apr_table_set(ctx->r->headers_out, sapi_header->header, val);
  102. } else {
  103. apr_table_add(ctx->r->headers_out, sapi_header->header, val);
  104. }
  105. sapi_free_header(sapi_header);
  106. return 0;
  107. }
  108. static int
  109. php_apache_sapi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
  110. {
  111. php_struct *ctx = SG(server_context);
  112. ctx->r->status = SG(sapi_headers).http_response_code;
  113. return SAPI_HEADER_SENT_SUCCESSFULLY;
  114. }
  115. static int
  116. php_apache_sapi_read_post(char *buf, uint count_bytes TSRMLS_DC)
  117. {
  118. apr_size_t len, tlen=0;
  119. php_struct *ctx = SG(server_context);
  120. request_rec *r;
  121. apr_bucket_brigade *brigade;
  122. r = ctx->r;
  123. brigade = ctx->brigade;
  124. len = count_bytes;
  125. /*
  126. * This loop is needed because ap_get_brigade() can return us partial data
  127. * which would cause premature termination of request read. Therefor we
  128. * need to make sure that if data is avaliable we fill the buffer completely.
  129. */
  130. while (ap_get_brigade(r->input_filters, brigade, AP_MODE_READBYTES, APR_BLOCK_READ, len) == APR_SUCCESS) {
  131. apr_brigade_flatten(brigade, buf, &len);
  132. apr_brigade_cleanup(brigade);
  133. tlen += len;
  134. if (tlen == count_bytes || !len) {
  135. break;
  136. }
  137. buf += len;
  138. len = count_bytes - tlen;
  139. }
  140. return tlen;
  141. }
  142. static struct stat*
  143. php_apache_sapi_get_stat(TSRMLS_D)
  144. {
  145. php_struct *ctx = SG(server_context);
  146. ctx->finfo.st_uid = ctx->r->finfo.user;
  147. ctx->finfo.st_gid = ctx->r->finfo.group;
  148. ctx->finfo.st_ino = ctx->r->finfo.inode;
  149. #if defined(NETWARE) && defined(CLIB_STAT_PATCH)
  150. ctx->finfo.st_atime.tv_sec = ctx->r->finfo.atime/1000000;
  151. ctx->finfo.st_mtime.tv_sec = ctx->r->finfo.mtime/1000000;
  152. ctx->finfo.st_ctime.tv_sec = ctx->r->finfo.ctime/1000000;
  153. #else
  154. ctx->finfo.st_atime = ctx->r->finfo.atime/1000000;
  155. ctx->finfo.st_mtime = ctx->r->finfo.mtime/1000000;
  156. ctx->finfo.st_ctime = ctx->r->finfo.ctime/1000000;
  157. #endif
  158. ctx->finfo.st_size = ctx->r->finfo.size;
  159. ctx->finfo.st_nlink = ctx->r->finfo.nlink;
  160. return &ctx->finfo;
  161. }
  162. static char *
  163. php_apache_sapi_read_cookies(TSRMLS_D)
  164. {
  165. php_struct *ctx = SG(server_context);
  166. const char *http_cookie;
  167. http_cookie = apr_table_get(ctx->r->headers_in, "cookie");
  168. /* The SAPI interface should use 'const char *' */
  169. return (char *) http_cookie;
  170. }
  171. static char *
  172. php_apache_sapi_getenv(char *name, size_t name_len TSRMLS_DC)
  173. {
  174. php_struct *ctx = SG(server_context);
  175. const char *env_var;
  176. env_var = apr_table_get(ctx->r->subprocess_env, name);
  177. return (char *) env_var;
  178. }
  179. static void
  180. php_apache_sapi_register_variables(zval *track_vars_array TSRMLS_DC)
  181. {
  182. php_struct *ctx = SG(server_context);
  183. const apr_array_header_t *arr = apr_table_elts(ctx->r->subprocess_env);
  184. char *key, *val;
  185. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  186. if (!val) val = empty_string;
  187. php_register_variable(key, val, track_vars_array TSRMLS_CC);
  188. APR_ARRAY_FOREACH_CLOSE()
  189. php_register_variable("PHP_SELF", ctx->r->uri, track_vars_array TSRMLS_CC);
  190. }
  191. static void
  192. php_apache_sapi_flush(void *server_context)
  193. {
  194. php_struct *ctx;
  195. apr_bucket_brigade *brigade;
  196. apr_bucket *bucket;
  197. request_rec *r;
  198. TSRMLS_FETCH();
  199. ctx = server_context;
  200. /* If we haven't registered a server_context yet,
  201. * then don't bother flushing. */
  202. if (!server_context) {
  203. return;
  204. }
  205. r = ctx->r;
  206. brigade = ctx->brigade;
  207. sapi_send_headers(TSRMLS_C);
  208. r->status = SG(sapi_headers).http_response_code;
  209. SG(headers_sent) = 1;
  210. /* Send a flush bucket down the filter chain. */
  211. bucket = apr_bucket_flush_create(r->connection->bucket_alloc);
  212. APR_BRIGADE_INSERT_TAIL(brigade, bucket);
  213. if (ap_pass_brigade(r->output_filters, brigade) != APR_SUCCESS || r->connection->aborted) {
  214. php_handle_aborted_connection();
  215. }
  216. apr_brigade_cleanup(brigade);
  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. /* We use APLOG_STARTUP because it keeps us from printing the
  224. * data and time information at the beginning of the error log
  225. * line. Not sure if this is correct, but it mirrors what happens
  226. * with Apache 1.3 -- rbb
  227. */
  228. if (ctx == NULL) { /* we haven't initialized our ctx yet, oh well */
  229. ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, NULL, "%s", msg);
  230. } else {
  231. ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, ctx->r, "%s", msg);
  232. }
  233. }
  234. static void php_apache_sapi_log_message_ex(char *msg, request_rec *r)
  235. {
  236. if (r) {
  237. ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, r, msg, r->filename);
  238. } else {
  239. php_apache_sapi_log_message(msg);
  240. }
  241. }
  242. extern zend_module_entry php_apache_module;
  243. static int php_apache2_startup(sapi_module_struct *sapi_module)
  244. {
  245. if (php_module_startup(sapi_module, &php_apache_module, 1)==FAILURE) {
  246. return FAILURE;
  247. }
  248. return SUCCESS;
  249. }
  250. static sapi_module_struct apache2_sapi_module = {
  251. "apache2handler",
  252. "Apache 2.0 Handler",
  253. php_apache2_startup, /* startup */
  254. php_module_shutdown_wrapper, /* shutdown */
  255. NULL, /* activate */
  256. NULL, /* deactivate */
  257. php_apache_sapi_ub_write, /* unbuffered write */
  258. php_apache_sapi_flush, /* flush */
  259. php_apache_sapi_get_stat, /* get uid */
  260. php_apache_sapi_getenv, /* getenv */
  261. php_error, /* error handler */
  262. php_apache_sapi_header_handler, /* header handler */
  263. php_apache_sapi_send_headers, /* send headers handler */
  264. NULL, /* send header handler */
  265. php_apache_sapi_read_post, /* read POST data */
  266. php_apache_sapi_read_cookies, /* read Cookies */
  267. php_apache_sapi_register_variables,
  268. php_apache_sapi_log_message, /* Log message */
  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 void php_apache_request_ctor(request_rec *r, php_struct *ctx TSRMLS_DC)
  340. {
  341. char *content_type;
  342. char *content_length;
  343. const char *auth;
  344. SG(sapi_headers).http_response_code = !r->status ? HTTP_OK : r->status;
  345. SG(request_info).content_type = apr_table_get(r->headers_in, "Content-Type");
  346. SG(request_info).query_string = apr_pstrdup(r->pool, r->args);
  347. SG(request_info).request_method = r->method;
  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_type = sapi_get_default_content_type(TSRMLS_C);
  352. ap_set_content_type(r, apr_pstrdup(r->pool, content_type));
  353. efree(content_type);
  354. content_length = (char *) apr_table_get(r->headers_in, "Content-Length");
  355. SG(request_info).content_length = (content_length ? atoi(content_length) : 0);
  356. apr_table_unset(r->headers_out, "Content-Length");
  357. apr_table_unset(r->headers_out, "Last-Modified");
  358. apr_table_unset(r->headers_out, "Expires");
  359. apr_table_unset(r->headers_out, "ETag");
  360. if (!PG(safe_mode) || (PG(safe_mode) && !ap_auth_type(r))) {
  361. auth = apr_table_get(r->headers_in, "Authorization");
  362. php_handle_auth_data(auth TSRMLS_CC);
  363. ctx->r->user = apr_pstrdup(ctx->r->pool, SG(request_info).auth_user);
  364. } else {
  365. SG(request_info).auth_user = NULL;
  366. SG(request_info).auth_password = NULL;
  367. }
  368. php_request_startup(TSRMLS_C);
  369. }
  370. static void php_apache_request_dtor(request_rec *r TSRMLS_DC)
  371. {
  372. php_request_shutdown(NULL);
  373. }
  374. static int php_handler(request_rec *r)
  375. {
  376. php_struct *ctx;
  377. void *conf;
  378. apr_bucket_brigade *brigade;
  379. apr_bucket *bucket;
  380. apr_status_t rv;
  381. request_rec *parent_req = NULL;
  382. TSRMLS_FETCH();
  383. conf = ap_get_module_config(r->per_dir_config, &php5_module);
  384. apply_config(conf);
  385. if (strcmp(r->handler, PHP_MAGIC_TYPE) && strcmp(r->handler, PHP_SOURCE_MAGIC_TYPE) && strcmp(r->handler, PHP_SCRIPT)) {
  386. /* Check for xbithack in this case. */
  387. if (!AP2(xbithack) || strcmp(r->handler, "text/html") || !(r->finfo.protection & APR_UEXECUTE)) {
  388. zend_try {
  389. zend_ini_deactivate(TSRMLS_C);
  390. } zend_end_try();
  391. return DECLINED;
  392. }
  393. }
  394. /* handle situations where user turns the engine off */
  395. if (!AP2(engine)) {
  396. zend_try {
  397. zend_ini_deactivate(TSRMLS_C);
  398. } zend_end_try();
  399. return DECLINED;
  400. }
  401. if (r->finfo.filetype == 0) {
  402. php_apache_sapi_log_message_ex("script '%s' not found or unable to stat", r);
  403. zend_try {
  404. zend_ini_deactivate(TSRMLS_C);
  405. } zend_end_try();
  406. return HTTP_NOT_FOUND;
  407. }
  408. if (r->finfo.filetype == APR_DIR) {
  409. php_apache_sapi_log_message_ex("attempt to invoke directory '%s' as script", r);
  410. zend_try {
  411. zend_ini_deactivate(TSRMLS_C);
  412. } zend_end_try();
  413. return HTTP_FORBIDDEN;
  414. }
  415. /* Setup the CGI variables if this is the main request */
  416. if (r->main == NULL ||
  417. /* .. or if the sub-request envinronment differs from the main-request. */
  418. r->subprocess_env != r->main->subprocess_env
  419. ) {
  420. /* setup standard CGI variables */
  421. ap_add_common_vars(r);
  422. ap_add_cgi_vars(r);
  423. }
  424. zend_first_try {
  425. ctx = SG(server_context);
  426. if (ctx == NULL) {
  427. ctx = SG(server_context) = apr_pcalloc(r->pool, sizeof(*ctx));
  428. /* register a cleanup so we clear out the SG(server_context)
  429. * after each request. Note: We pass in the pointer to the
  430. * server_context in case this is handled by a different thread.
  431. */
  432. apr_pool_cleanup_register(r->pool, (void *)&SG(server_context), php_server_context_cleanup, apr_pool_cleanup_null);
  433. ctx->r = r;
  434. brigade = apr_brigade_create(r->pool, r->connection->bucket_alloc);
  435. ctx->brigade = brigade;
  436. php_apache_request_ctor(r, ctx TSRMLS_CC);
  437. } else {
  438. parent_req = ctx->r;
  439. ctx->r = r;
  440. brigade = ctx->brigade;
  441. }
  442. if (AP2(last_modified)) {
  443. ap_update_mtime(r, r->finfo.mtime);
  444. ap_set_last_modified(r);
  445. }
  446. /* Determine if we need to parse the file or show the source */
  447. if (strncmp(r->handler, PHP_SOURCE_MAGIC_TYPE, sizeof(PHP_SOURCE_MAGIC_TYPE) - 1) == 0) {
  448. zend_syntax_highlighter_ini syntax_highlighter_ini;
  449. php_get_highlight_struct(&syntax_highlighter_ini);
  450. highlight_file((char *)r->filename, &syntax_highlighter_ini TSRMLS_CC);
  451. } else {
  452. zend_file_handle zfd;
  453. zfd.type = ZEND_HANDLE_FILENAME;
  454. zfd.filename = (char *) r->filename;
  455. zfd.free_filename = 0;
  456. zfd.opened_path = NULL;
  457. if (!parent_req) {
  458. php_execute_script(&zfd TSRMLS_CC);
  459. } else {
  460. zend_execute_scripts(ZEND_INCLUDE TSRMLS_CC, NULL, 1, &zfd);
  461. }
  462. #if MEMORY_LIMIT
  463. {
  464. char *mem_usage;
  465. mem_usage = apr_psprintf(ctx->r->pool, "%u", AG(allocated_memory_peak));
  466. AG(allocated_memory_peak) = 0;
  467. apr_table_set(r->notes, "mod_php_memory_usage", mem_usage);
  468. }
  469. #endif
  470. }
  471. if (!parent_req) {
  472. php_apache_request_dtor(r TSRMLS_CC);
  473. ctx->request_processed = 1;
  474. bucket = apr_bucket_eos_create(r->connection->bucket_alloc);
  475. APR_BRIGADE_INSERT_TAIL(brigade, bucket);
  476. rv = ap_pass_brigade(r->output_filters, brigade);
  477. if (rv != APR_SUCCESS || r->connection->aborted) {
  478. php_handle_aborted_connection();
  479. }
  480. apr_brigade_cleanup(brigade);
  481. } else {
  482. ctx->r = parent_req;
  483. }
  484. } zend_end_try();
  485. return OK;
  486. }
  487. void php_ap2_register_hook(apr_pool_t *p)
  488. {
  489. ap_hook_pre_config(php_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
  490. ap_hook_post_config(php_apache_server_startup, NULL, NULL, APR_HOOK_MIDDLE);
  491. ap_hook_handler(php_handler, NULL, NULL, APR_HOOK_MIDDLE);
  492. }
  493. /*
  494. * Local variables:
  495. * tab-width: 4
  496. * c-basic-offset: 4
  497. * End:
  498. * vim600: sw=4 ts=4 fdm=marker
  499. * vim<600: sw=4 ts=4
  500. */