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.

581 lines
16 KiB

23 years ago
23 years ago
23 years ago
23 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 4 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2003 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. #include <fcntl.h>
  22. #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
  23. #include "php.h"
  24. #include "php_main.h"
  25. #include "php_ini.h"
  26. #include "php_variables.h"
  27. #include "SAPI.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.
  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 + 1);
  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. r->status = SG(sapi_headers).http_response_code;
  208. SG(headers_sent) = 1;
  209. /* Send a flush bucket down the filter chain. */
  210. bucket = apr_bucket_flush_create(r->connection->bucket_alloc);
  211. APR_BRIGADE_INSERT_TAIL(brigade, bucket);
  212. if (ap_pass_brigade(r->output_filters, brigade) != APR_SUCCESS || r->connection->aborted) {
  213. php_handle_aborted_connection();
  214. }
  215. apr_brigade_cleanup(brigade);
  216. }
  217. static void php_apache_sapi_log_message(char *msg)
  218. {
  219. php_struct *ctx;
  220. TSRMLS_FETCH();
  221. ctx = SG(server_context);
  222. /* We use APLOG_STARTUP because it keeps us from printing the
  223. * data and time information at the beginning of the error log
  224. * line. Not sure if this is correct, but it mirrors what happens
  225. * with Apache 1.3 -- rbb
  226. */
  227. if (ctx == NULL) { /* we haven't initialized our ctx yet, oh well */
  228. ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO | APLOG_STARTUP,
  229. 0, NULL, "%s", msg);
  230. } else {
  231. ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO | APLOG_STARTUP,
  232. 0, ctx->r, "%s", msg);
  233. }
  234. }
  235. extern zend_module_entry php_apache_module;
  236. static int php_apache2_startup(sapi_module_struct *sapi_module)
  237. {
  238. if (php_module_startup(sapi_module, &php_apache_module, 1)==FAILURE) {
  239. return FAILURE;
  240. }
  241. return SUCCESS;
  242. }
  243. static sapi_module_struct apache2_sapi_module = {
  244. "apache2handler",
  245. "Apache 2.0 Handler",
  246. php_apache2_startup, /* startup */
  247. php_module_shutdown_wrapper, /* shutdown */
  248. NULL, /* activate */
  249. NULL, /* deactivate */
  250. php_apache_sapi_ub_write, /* unbuffered write */
  251. php_apache_sapi_flush, /* flush */
  252. php_apache_sapi_get_stat, /* get uid */
  253. php_apache_sapi_getenv, /* getenv */
  254. php_error, /* error handler */
  255. php_apache_sapi_header_handler, /* header handler */
  256. php_apache_sapi_send_headers, /* send headers handler */
  257. NULL, /* send header handler */
  258. php_apache_sapi_read_post, /* read POST data */
  259. php_apache_sapi_read_cookies, /* read Cookies */
  260. php_apache_sapi_register_variables,
  261. php_apache_sapi_log_message, /* Log message */
  262. STANDARD_SAPI_MODULE_PROPERTIES
  263. };
  264. static apr_status_t
  265. php_apache_server_shutdown(void *tmp)
  266. {
  267. apache2_sapi_module.shutdown(&apache2_sapi_module);
  268. sapi_shutdown();
  269. #ifdef ZTS
  270. tsrm_shutdown();
  271. #endif
  272. return APR_SUCCESS;
  273. }
  274. static void php_apache_add_version(apr_pool_t *p)
  275. {
  276. TSRMLS_FETCH();
  277. if (PG(expose_php)) {
  278. ap_add_version_component(p, "PHP/" PHP_VERSION);
  279. }
  280. }
  281. static int php_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
  282. {
  283. #ifndef ZTS
  284. int threaded_mpm;
  285. ap_mpm_query(AP_MPMQ_IS_THREADED, &threaded_mpm);
  286. if(threaded_mpm) {
  287. 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.");
  288. return DONE;
  289. }
  290. #endif
  291. /* When this is NULL, apache won't override the hard-coded default
  292. * php.ini path setting. */
  293. apache2_php_ini_path_override = NULL;
  294. return OK;
  295. }
  296. static int
  297. php_apache_server_startup(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
  298. {
  299. void *data = NULL;
  300. const char *userdata_key = "apache2hook_post_config";
  301. /* Apache will load, unload and then reload a DSO module. This
  302. * prevents us from starting PHP until the second load. */
  303. apr_pool_userdata_get(&data, userdata_key, s->process->pool);
  304. if (data == NULL) {
  305. /* We must use set() here and *not* setn(), otherwise the
  306. * static string pointed to by userdata_key will be mapped
  307. * to a different location when the DSO is reloaded and the
  308. * pointers won't match, causing get() to return NULL when
  309. * we expected it to return non-NULL. */
  310. apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
  311. return OK;
  312. }
  313. /* Set up our overridden path. */
  314. if (apache2_php_ini_path_override) {
  315. apache2_sapi_module.php_ini_path_override = apache2_php_ini_path_override;
  316. }
  317. #ifdef ZTS
  318. tsrm_startup(1, 1, 0, NULL);
  319. #endif
  320. sapi_startup(&apache2_sapi_module);
  321. apache2_sapi_module.startup(&apache2_sapi_module);
  322. apr_pool_cleanup_register(pconf, NULL, php_apache_server_shutdown, apr_pool_cleanup_null);
  323. php_apache_add_version(pconf);
  324. return OK;
  325. }
  326. static apr_status_t php_server_context_cleanup(void *data_)
  327. {
  328. void **data = data_;
  329. *data = NULL;
  330. return APR_SUCCESS;
  331. }
  332. static void php_apache_request_ctor(request_rec *r, php_struct *ctx TSRMLS_DC)
  333. {
  334. char *content_type;
  335. const char *auth;
  336. SG(sapi_headers).http_response_code = !r->status ? HTTP_OK : r->status;
  337. SG(request_info).content_type = apr_table_get(r->headers_in, "Content-Type");
  338. SG(request_info).query_string = apr_pstrdup(r->pool, r->args);
  339. SG(request_info).request_method = r->method;
  340. SG(request_info).request_uri = apr_pstrdup(r->pool, r->uri);
  341. SG(request_info).path_translated = apr_pstrdup(r->pool, r->filename);
  342. r->no_local_copy = 1;
  343. content_type = sapi_get_default_content_type(TSRMLS_C);
  344. ap_set_content_type(r, apr_pstrdup(r->pool, content_type));
  345. efree(content_type);
  346. apr_table_unset(r->headers_out, "Content-Length");
  347. apr_table_unset(r->headers_out, "Last-Modified");
  348. apr_table_unset(r->headers_out, "Expires");
  349. apr_table_unset(r->headers_out, "ETag");
  350. apr_table_unset(r->headers_in, "Connection");
  351. if (!PG(safe_mode) || (PG(safe_mode) && !ap_auth_type(r))) {
  352. auth = apr_table_get(r->headers_in, "Authorization");
  353. php_handle_auth_data(auth TSRMLS_CC);
  354. ctx->r->user = apr_pstrdup(ctx->r->pool, SG(request_info).auth_user);
  355. } else {
  356. SG(request_info).auth_user = NULL;
  357. SG(request_info).auth_password = NULL;
  358. }
  359. php_request_startup(TSRMLS_C);
  360. }
  361. static void php_apache_request_dtor(request_rec *r TSRMLS_DC)
  362. {
  363. php_request_shutdown(NULL);
  364. }
  365. static int php_handler(request_rec *r)
  366. {
  367. php_struct *ctx;
  368. void *conf;
  369. apr_bucket_brigade *brigade;
  370. apr_bucket *bucket;
  371. apr_status_t rv;
  372. request_rec *parent_req = NULL;
  373. TSRMLS_FETCH();
  374. conf = ap_get_module_config(r->per_dir_config, &php5_module);
  375. apply_config(conf);
  376. if (strcmp(r->handler, PHP_MAGIC_TYPE) && strcmp(r->handler, PHP_SOURCE_MAGIC_TYPE) && strcmp(r->handler, PHP_SCRIPT)) {
  377. /* Check for xbithack in this case. */
  378. if (!AP2(xbithack) || strcmp(r->handler, "text/html") || !(r->finfo.protection & APR_UEXECUTE)) {
  379. return DECLINED;
  380. }
  381. }
  382. /* handle situations where user turns the engine off */
  383. if (!AP2(engine)) {
  384. return DECLINED;
  385. }
  386. if (r->finfo.filetype == 0) {
  387. php_apache_sapi_log_message("script not found or unable to stat");
  388. return HTTP_NOT_FOUND;
  389. }
  390. if (r->finfo.filetype == APR_DIR) {
  391. php_apache_sapi_log_message("attempt to invoke directory as script");
  392. return HTTP_FORBIDDEN;
  393. }
  394. /* Setup the CGI variables if this is the main request */
  395. if (r->main == NULL ||
  396. /* .. or if the sub-request envinronment differs from the main-request. */
  397. r->subprocess_env != r->main->subprocess_env
  398. ) {
  399. /* setup standard CGI variables */
  400. ap_add_common_vars(r);
  401. ap_add_cgi_vars(r);
  402. }
  403. zend_first_try {
  404. ctx = SG(server_context);
  405. if (ctx == NULL) {
  406. ctx = SG(server_context) = apr_pcalloc(r->pool, sizeof(*ctx));
  407. /* register a cleanup so we clear out the SG(server_context)
  408. * after each request. Note: We pass in the pointer to the
  409. * server_context in case this is handled by a different thread.
  410. */
  411. apr_pool_cleanup_register(r->pool, (void *)&SG(server_context), php_server_context_cleanup, apr_pool_cleanup_null);
  412. ctx->r = r;
  413. brigade = apr_brigade_create(r->pool, r->connection->bucket_alloc);
  414. ctx->brigade = brigade;
  415. php_apache_request_ctor(r, ctx TSRMLS_CC);
  416. } else {
  417. parent_req = ctx->r;
  418. ctx->r = r;
  419. brigade = ctx->brigade;
  420. }
  421. if (AP2(last_modified)) {
  422. ap_update_mtime(r, r->finfo.mtime);
  423. ap_set_last_modified(r);
  424. }
  425. /* Determine if we need to parse the file or show the source */
  426. if (strncmp(r->handler, PHP_SOURCE_MAGIC_TYPE, sizeof(PHP_SOURCE_MAGIC_TYPE) - 1) == 0) {
  427. zend_syntax_highlighter_ini syntax_highlighter_ini;
  428. php_get_highlight_struct(&syntax_highlighter_ini);
  429. highlight_file((char *)r->filename, &syntax_highlighter_ini TSRMLS_CC);
  430. } else {
  431. zend_file_handle zfd;
  432. zfd.type = ZEND_HANDLE_FILENAME;
  433. zfd.filename = (char *) r->filename;
  434. zfd.free_filename = 0;
  435. zfd.opened_path = NULL;
  436. if (!parent_req) {
  437. php_execute_script(&zfd TSRMLS_CC);
  438. } else {
  439. zend_execute_scripts(ZEND_INCLUDE TSRMLS_CC, NULL, 1, &zfd);
  440. }
  441. #if MEMORY_LIMIT
  442. {
  443. char *mem_usage;
  444. mem_usage = apr_psprintf(ctx->r->pool, "%u", AG(allocated_memory_peak));
  445. AG(allocated_memory_peak) = 0;
  446. apr_table_set(r->notes, "mod_php_memory_usage", mem_usage);
  447. }
  448. #endif
  449. }
  450. if (!parent_req) {
  451. php_apache_request_dtor(r TSRMLS_CC);
  452. ctx->request_processed = 1;
  453. bucket = apr_bucket_eos_create(r->connection->bucket_alloc);
  454. APR_BRIGADE_INSERT_TAIL(brigade, bucket);
  455. rv = ap_pass_brigade(r->output_filters, brigade);
  456. if (rv != APR_SUCCESS || r->connection->aborted) {
  457. php_handle_aborted_connection();
  458. }
  459. apr_brigade_cleanup(brigade);
  460. } else {
  461. ctx->r = parent_req;
  462. }
  463. } zend_end_try();
  464. return OK;
  465. }
  466. void php_ap2_register_hook(apr_pool_t *p)
  467. {
  468. ap_hook_pre_config(php_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
  469. ap_hook_post_config(php_apache_server_startup, NULL, NULL, APR_HOOK_MIDDLE);
  470. ap_hook_handler(php_handler, NULL, NULL, APR_HOOK_MIDDLE);
  471. }
  472. /*
  473. * Local variables:
  474. * tab-width: 4
  475. * c-basic-offset: 4
  476. * End:
  477. * vim600: sw=4 ts=4 fdm=marker
  478. * vim<600: sw=4 ts=4
  479. */