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.

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