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.

601 lines
16 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. 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;
  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. *val = '\0';
  82. do {
  83. val++;
  84. } while (*val == ' ');
  85. if (!strcasecmp(sapi_header->header, "content-type")) {
  86. val = apr_pstrdup(ctx->r->pool, val);
  87. ap_set_content_type(ctx->r, val);
  88. } else if (sapi_header->replace) {
  89. apr_table_set(ctx->r->headers_out, sapi_header->header, val);
  90. } else {
  91. apr_table_add(ctx->r->headers_out, sapi_header->header, val);
  92. }
  93. sapi_free_header(sapi_header);
  94. return 0;
  95. }
  96. static int
  97. php_apache_sapi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
  98. {
  99. php_struct *ctx = SG(server_context);
  100. const char *sline = SG(sapi_headers).http_status_line;
  101. ctx->r->status = SG(sapi_headers).http_response_code;
  102. /* httpd requires that r->status_line is set to the first digit of
  103. * the status-code: */
  104. if (sline && strlen(sline) > 12 && strncmp(sline, "HTTP/1.", 7) == 0
  105. && sline[8] == ' ') {
  106. ctx->r->status_line = apr_pstrdup(ctx->r->pool, sline + 9);
  107. }
  108. return SAPI_HEADER_SENT_SUCCESSFULLY;
  109. }
  110. static int
  111. php_apache_sapi_read_post(char *buf, uint count_bytes TSRMLS_DC)
  112. {
  113. apr_size_t len, tlen=0;
  114. php_struct *ctx = SG(server_context);
  115. request_rec *r;
  116. apr_bucket_brigade *brigade;
  117. r = ctx->r;
  118. brigade = ctx->brigade;
  119. len = count_bytes;
  120. /*
  121. * This loop is needed because ap_get_brigade() can return us partial data
  122. * which would cause premature termination of request read. Therefor we
  123. * need to make sure that if data is available we fill the buffer completely.
  124. */
  125. while (ap_get_brigade(r->input_filters, brigade, AP_MODE_READBYTES, APR_BLOCK_READ, len) == APR_SUCCESS) {
  126. apr_brigade_flatten(brigade, buf, &len);
  127. apr_brigade_cleanup(brigade);
  128. tlen += len;
  129. if (tlen == count_bytes || !len) {
  130. break;
  131. }
  132. buf += len;
  133. len = count_bytes - tlen;
  134. }
  135. return tlen;
  136. }
  137. static struct stat*
  138. php_apache_sapi_get_stat(TSRMLS_D)
  139. {
  140. php_struct *ctx = SG(server_context);
  141. ctx->finfo.st_uid = ctx->r->finfo.user;
  142. ctx->finfo.st_gid = ctx->r->finfo.group;
  143. ctx->finfo.st_dev = ctx->r->finfo.device;
  144. ctx->finfo.st_ino = ctx->r->finfo.inode;
  145. #if defined(NETWARE) && defined(CLIB_STAT_PATCH)
  146. ctx->finfo.st_atime.tv_sec = apr_time_sec(ctx->r->finfo.atime);
  147. ctx->finfo.st_mtime.tv_sec = apr_time_sec(ctx->r->finfo.mtime);
  148. ctx->finfo.st_ctime.tv_sec = apr_time_sec(ctx->r->finfo.ctime);
  149. #else
  150. ctx->finfo.st_atime = apr_time_sec(ctx->r->finfo.atime);
  151. ctx->finfo.st_mtime = apr_time_sec(ctx->r->finfo.mtime);
  152. ctx->finfo.st_ctime = apr_time_sec(ctx->r->finfo.ctime);
  153. #endif
  154. ctx->finfo.st_size = ctx->r->finfo.size;
  155. ctx->finfo.st_nlink = ctx->r->finfo.nlink;
  156. return &ctx->finfo;
  157. }
  158. static char *
  159. php_apache_sapi_read_cookies(TSRMLS_D)
  160. {
  161. php_struct *ctx = SG(server_context);
  162. const char *http_cookie;
  163. http_cookie = apr_table_get(ctx->r->headers_in, "cookie");
  164. /* The SAPI interface should use 'const char *' */
  165. return (char *) http_cookie;
  166. }
  167. static char *
  168. php_apache_sapi_getenv(char *name, size_t name_len TSRMLS_DC)
  169. {
  170. php_struct *ctx = SG(server_context);
  171. const char *env_var;
  172. env_var = apr_table_get(ctx->r->subprocess_env, name);
  173. return (char *) env_var;
  174. }
  175. static void
  176. php_apache_sapi_register_variables(zval *track_vars_array TSRMLS_DC)
  177. {
  178. php_struct *ctx = SG(server_context);
  179. const apr_array_header_t *arr = apr_table_elts(ctx->r->subprocess_env);
  180. char *key, *val;
  181. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  182. if (!val) val = "";
  183. php_register_variable(key, val, track_vars_array TSRMLS_CC);
  184. APR_ARRAY_FOREACH_CLOSE()
  185. php_register_variable("PHP_SELF", ctx->r->uri, track_vars_array TSRMLS_CC);
  186. }
  187. static void
  188. php_apache_sapi_flush(void *server_context)
  189. {
  190. php_struct *ctx;
  191. request_rec *r;
  192. TSRMLS_FETCH();
  193. ctx = server_context;
  194. /* If we haven't registered a server_context yet,
  195. * then don't bother flushing. */
  196. if (!server_context) {
  197. return;
  198. }
  199. r = ctx->r;
  200. sapi_send_headers(TSRMLS_C);
  201. r->status = SG(sapi_headers).http_response_code;
  202. SG(headers_sent) = 1;
  203. if (ap_rflush(r) < 0 || r->connection->aborted) {
  204. php_handle_aborted_connection();
  205. }
  206. }
  207. static void php_apache_sapi_log_message(char *msg)
  208. {
  209. php_struct *ctx;
  210. TSRMLS_FETCH();
  211. ctx = SG(server_context);
  212. /* We use APLOG_STARTUP because it keeps us from printing the
  213. * data and time information at the beginning of the error log
  214. * line. Not sure if this is correct, but it mirrors what happens
  215. * with Apache 1.3 -- rbb
  216. */
  217. if (ctx == NULL) { /* we haven't initialized our ctx yet, oh well */
  218. ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, NULL, "%s", msg);
  219. } else {
  220. ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, ctx->r, "%s", msg);
  221. }
  222. }
  223. static void php_apache_sapi_log_message_ex(char *msg, request_rec *r)
  224. {
  225. if (r) {
  226. ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, r, msg, r->filename);
  227. } else {
  228. php_apache_sapi_log_message(msg);
  229. }
  230. }
  231. static time_t php_apache_sapi_get_request_time(TSRMLS_D) {
  232. php_struct *ctx = SG(server_context);
  233. return apr_time_sec(ctx->r->request_time);
  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. php_apache_sapi_get_request_time, /* Request Time */
  263. STANDARD_SAPI_MODULE_PROPERTIES
  264. };
  265. static apr_status_t
  266. php_apache_server_shutdown(void *tmp)
  267. {
  268. apache2_sapi_module.shutdown(&apache2_sapi_module);
  269. sapi_shutdown();
  270. #ifdef ZTS
  271. tsrm_shutdown();
  272. #endif
  273. return APR_SUCCESS;
  274. }
  275. static void php_apache_add_version(apr_pool_t *p)
  276. {
  277. TSRMLS_FETCH();
  278. if (PG(expose_php)) {
  279. ap_add_version_component(p, "PHP/" PHP_VERSION);
  280. }
  281. }
  282. static int php_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
  283. {
  284. #ifndef ZTS
  285. int threaded_mpm;
  286. ap_mpm_query(AP_MPMQ_IS_THREADED, &threaded_mpm);
  287. if(threaded_mpm) {
  288. 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.");
  289. return DONE;
  290. }
  291. #endif
  292. /* When this is NULL, apache won't override the hard-coded default
  293. * php.ini path setting. */
  294. apache2_php_ini_path_override = NULL;
  295. return OK;
  296. }
  297. static int
  298. php_apache_server_startup(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
  299. {
  300. void *data = NULL;
  301. const char *userdata_key = "apache2hook_post_config";
  302. /* Apache will load, unload and then reload a DSO module. This
  303. * prevents us from starting PHP until the second load. */
  304. apr_pool_userdata_get(&data, userdata_key, s->process->pool);
  305. if (data == NULL) {
  306. /* We must use set() here and *not* setn(), otherwise the
  307. * static string pointed to by userdata_key will be mapped
  308. * to a different location when the DSO is reloaded and the
  309. * pointers won't match, causing get() to return NULL when
  310. * we expected it to return non-NULL. */
  311. apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);
  312. return OK;
  313. }
  314. /* Set up our overridden path. */
  315. if (apache2_php_ini_path_override) {
  316. apache2_sapi_module.php_ini_path_override = apache2_php_ini_path_override;
  317. }
  318. #ifdef ZTS
  319. tsrm_startup(1, 1, 0, NULL);
  320. #endif
  321. sapi_startup(&apache2_sapi_module);
  322. apache2_sapi_module.startup(&apache2_sapi_module);
  323. apr_pool_cleanup_register(pconf, NULL, php_apache_server_shutdown, apr_pool_cleanup_null);
  324. php_apache_add_version(pconf);
  325. return OK;
  326. }
  327. static apr_status_t php_server_context_cleanup(void *data_)
  328. {
  329. void **data = data_;
  330. *data = NULL;
  331. return APR_SUCCESS;
  332. }
  333. static int php_apache_request_ctor(request_rec *r, php_struct *ctx TSRMLS_DC)
  334. {
  335. char *content_type;
  336. char *content_length;
  337. const char *auth;
  338. SG(sapi_headers).http_response_code = !r->status ? HTTP_OK : r->status;
  339. SG(request_info).content_type = apr_table_get(r->headers_in, "Content-Type");
  340. SG(request_info).query_string = apr_pstrdup(r->pool, r->args);
  341. SG(request_info).request_method = r->method;
  342. SG(request_info).request_uri = apr_pstrdup(r->pool, r->uri);
  343. SG(request_info).path_translated = apr_pstrdup(r->pool, r->filename);
  344. r->no_local_copy = 1;
  345. content_type = sapi_get_default_content_type(TSRMLS_C);
  346. ap_set_content_type(r, apr_pstrdup(r->pool, content_type));
  347. efree(content_type);
  348. content_length = (char *) apr_table_get(r->headers_in, "Content-Length");
  349. SG(request_info).content_length = (content_length ? atoi(content_length) : 0);
  350. apr_table_unset(r->headers_out, "Content-Length");
  351. apr_table_unset(r->headers_out, "Last-Modified");
  352. apr_table_unset(r->headers_out, "Expires");
  353. apr_table_unset(r->headers_out, "ETag");
  354. if (!PG(safe_mode) || (PG(safe_mode) && !ap_auth_type(r))) {
  355. auth = apr_table_get(r->headers_in, "Authorization");
  356. php_handle_auth_data(auth TSRMLS_CC);
  357. ctx->r->user = apr_pstrdup(ctx->r->pool, SG(request_info).auth_user);
  358. } else {
  359. SG(request_info).auth_user = NULL;
  360. SG(request_info).auth_password = NULL;
  361. }
  362. return php_request_startup(TSRMLS_C);
  363. }
  364. static void php_apache_request_dtor(request_rec *r TSRMLS_DC)
  365. {
  366. php_request_shutdown(NULL);
  367. }
  368. static int php_handler(request_rec *r)
  369. {
  370. php_struct *ctx;
  371. void *conf;
  372. apr_bucket_brigade *brigade;
  373. apr_bucket *bucket;
  374. apr_status_t rv;
  375. request_rec *parent_req = NULL;
  376. TSRMLS_FETCH();
  377. conf = ap_get_module_config(r->per_dir_config, &php5_module);
  378. apply_config(conf);
  379. if (strcmp(r->handler, PHP_MAGIC_TYPE) && strcmp(r->handler, PHP_SOURCE_MAGIC_TYPE) && strcmp(r->handler, PHP_SCRIPT)) {
  380. /* Check for xbithack in this case. */
  381. if (!AP2(xbithack) || strcmp(r->handler, "text/html") || !(r->finfo.protection & APR_UEXECUTE)) {
  382. zend_try {
  383. zend_ini_deactivate(TSRMLS_C);
  384. } zend_end_try();
  385. return DECLINED;
  386. }
  387. }
  388. /* handle situations where user turns the engine off */
  389. if (!AP2(engine)) {
  390. zend_try {
  391. zend_ini_deactivate(TSRMLS_C);
  392. } zend_end_try();
  393. return DECLINED;
  394. }
  395. if (r->finfo.filetype == 0) {
  396. php_apache_sapi_log_message_ex("script '%s' not found or unable to stat", r);
  397. zend_try {
  398. zend_ini_deactivate(TSRMLS_C);
  399. } zend_end_try();
  400. return HTTP_NOT_FOUND;
  401. }
  402. if (r->finfo.filetype == APR_DIR) {
  403. php_apache_sapi_log_message_ex("attempt to invoke directory '%s' as script", r);
  404. zend_try {
  405. zend_ini_deactivate(TSRMLS_C);
  406. } zend_end_try();
  407. return HTTP_FORBIDDEN;
  408. }
  409. /* Setup the CGI variables if this is the main request */
  410. if (r->main == NULL ||
  411. /* .. or if the sub-request envinronment differs from the main-request. */
  412. r->subprocess_env != r->main->subprocess_env
  413. ) {
  414. /* setup standard CGI variables */
  415. ap_add_common_vars(r);
  416. ap_add_cgi_vars(r);
  417. }
  418. zend_first_try {
  419. ctx = SG(server_context);
  420. if (ctx == NULL) {
  421. ctx = SG(server_context) = apr_pcalloc(r->pool, sizeof(*ctx));
  422. /* register a cleanup so we clear out the SG(server_context)
  423. * after each request. Note: We pass in the pointer to the
  424. * server_context in case this is handled by a different thread.
  425. */
  426. apr_pool_cleanup_register(r->pool, (void *)&SG(server_context), php_server_context_cleanup, apr_pool_cleanup_null);
  427. ctx->r = r;
  428. brigade = apr_brigade_create(r->pool, r->connection->bucket_alloc);
  429. ctx->brigade = brigade;
  430. if (php_apache_request_ctor(r, ctx TSRMLS_CC)!=SUCCESS) {
  431. zend_bailout();
  432. }
  433. } else {
  434. parent_req = ctx->r;
  435. ctx->r = r;
  436. brigade = ctx->brigade;
  437. }
  438. if (AP2(last_modified)) {
  439. ap_update_mtime(r, r->finfo.mtime);
  440. ap_set_last_modified(r);
  441. }
  442. /* Determine if we need to parse the file or show the source */
  443. if (strncmp(r->handler, PHP_SOURCE_MAGIC_TYPE, sizeof(PHP_SOURCE_MAGIC_TYPE) - 1) == 0) {
  444. zend_syntax_highlighter_ini syntax_highlighter_ini;
  445. php_get_highlight_struct(&syntax_highlighter_ini);
  446. highlight_file((char *)r->filename, &syntax_highlighter_ini TSRMLS_CC);
  447. } else {
  448. zend_file_handle zfd;
  449. zfd.type = ZEND_HANDLE_FILENAME;
  450. zfd.filename = (char *) r->filename;
  451. zfd.free_filename = 0;
  452. zfd.opened_path = NULL;
  453. if (!parent_req) {
  454. php_execute_script(&zfd TSRMLS_CC);
  455. } else {
  456. zend_execute_scripts(ZEND_INCLUDE TSRMLS_CC, NULL, 1, &zfd);
  457. }
  458. #if MEMORY_LIMIT
  459. {
  460. char *mem_usage;
  461. mem_usage = apr_psprintf(ctx->r->pool, "%u", AG(allocated_memory_peak));
  462. apr_table_set(r->notes, "mod_php_memory_usage", mem_usage);
  463. }
  464. #endif
  465. }
  466. } zend_end_try();
  467. if (!parent_req) {
  468. php_apache_request_dtor(r TSRMLS_CC);
  469. ctx->request_processed = 1;
  470. bucket = apr_bucket_eos_create(r->connection->bucket_alloc);
  471. APR_BRIGADE_INSERT_TAIL(brigade, bucket);
  472. rv = ap_pass_brigade(r->output_filters, brigade);
  473. if (rv != APR_SUCCESS || r->connection->aborted) {
  474. zend_first_try {
  475. php_handle_aborted_connection();
  476. } zend_end_try();
  477. }
  478. apr_brigade_cleanup(brigade);
  479. } else {
  480. ctx->r = parent_req;
  481. }
  482. return OK;
  483. }
  484. void php_ap2_register_hook(apr_pool_t *p)
  485. {
  486. ap_hook_pre_config(php_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
  487. ap_hook_post_config(php_apache_server_startup, NULL, NULL, APR_HOOK_MIDDLE);
  488. ap_hook_handler(php_handler, NULL, NULL, APR_HOOK_MIDDLE);
  489. }
  490. /*
  491. * Local variables:
  492. * tab-width: 4
  493. * c-basic-offset: 4
  494. * End:
  495. * vim600: sw=4 ts=4 fdm=marker
  496. * vim<600: sw=4 ts=4
  497. */