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.

634 lines
18 KiB

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