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.

638 lines
18 KiB

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