apache_config.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Sascha Schumann <sascha@schumann.cx> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
  17. #include "php.h"
  18. #ifdef strcasecmp
  19. # undef strcasecmp
  20. #endif
  21. #ifdef strncasecmp
  22. # undef strncasecmp
  23. #endif
  24. #include "php_ini.h"
  25. #include "php_apache.h"
  26. #include "apr_strings.h"
  27. #include "ap_config.h"
  28. #include "util_filter.h"
  29. #include "httpd.h"
  30. #include "http_config.h"
  31. #include "http_request.h"
  32. #include "http_core.h"
  33. #include "http_protocol.h"
  34. #include "http_log.h"
  35. #include "http_main.h"
  36. #include "util_script.h"
  37. #include "http_core.h"
  38. #ifdef PHP_AP_DEBUG
  39. #define phpapdebug(a) fprintf a
  40. #else
  41. #define phpapdebug(a)
  42. #endif
  43. typedef struct {
  44. HashTable config;
  45. } php_conf_rec;
  46. typedef struct {
  47. char *value;
  48. size_t value_len;
  49. char status;
  50. char htaccess;
  51. } php_dir_entry;
  52. static const char *real_value_hnd(cmd_parms *cmd, void *dummy, const char *name, const char *value, int status)
  53. {
  54. php_conf_rec *d = dummy;
  55. php_dir_entry e;
  56. phpapdebug((stderr, "Getting %s=%s for %p (%d)\n", name, value, dummy, zend_hash_num_elements(&d->config)));
  57. if (!strncasecmp(value, "none", sizeof("none"))) {
  58. value = "";
  59. }
  60. e.value = apr_pstrdup(cmd->pool, value);
  61. e.value_len = strlen(value);
  62. e.status = status;
  63. e.htaccess = ((cmd->override & (RSRC_CONF|ACCESS_CONF)) == 0);
  64. zend_hash_str_update_mem(&d->config, (char *) name, strlen(name), &e, sizeof(e));
  65. return NULL;
  66. }
  67. static const char *php_apache_value_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
  68. {
  69. return real_value_hnd(cmd, dummy, name, value, PHP_INI_PERDIR);
  70. }
  71. static const char *php_apache_admin_value_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
  72. {
  73. return real_value_hnd(cmd, dummy, name, value, PHP_INI_SYSTEM);
  74. }
  75. static const char *real_flag_hnd(cmd_parms *cmd, void *dummy, const char *arg1, const char *arg2, int status)
  76. {
  77. char bool_val[2];
  78. if (!strcasecmp(arg2, "On") || (arg2[0] == '1' && arg2[1] == '\0')) {
  79. bool_val[0] = '1';
  80. } else {
  81. bool_val[0] = '0';
  82. }
  83. bool_val[1] = 0;
  84. return real_value_hnd(cmd, dummy, arg1, bool_val, status);
  85. }
  86. static const char *php_apache_flag_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
  87. {
  88. return real_flag_hnd(cmd, dummy, name, value, PHP_INI_PERDIR);
  89. }
  90. static const char *php_apache_admin_flag_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
  91. {
  92. return real_flag_hnd(cmd, dummy, name, value, PHP_INI_SYSTEM);
  93. }
  94. static const char *php_apache_phpini_set(cmd_parms *cmd, void *mconfig, const char *arg)
  95. {
  96. if (apache2_php_ini_path_override) {
  97. return "Only first PHPINIDir directive honored per configuration tree - subsequent ones ignored";
  98. }
  99. apache2_php_ini_path_override = ap_server_root_relative(cmd->pool, arg);
  100. return NULL;
  101. }
  102. static bool should_overwrite_per_dir_entry(HashTable *target_ht, zval *zv, zend_hash_key *hash_key, void *pData)
  103. {
  104. php_dir_entry *new_per_dir_entry = Z_PTR_P(zv);
  105. php_dir_entry *orig_per_dir_entry;
  106. if ((orig_per_dir_entry = zend_hash_find_ptr(target_ht, hash_key->key)) == NULL) {
  107. return 1; /* does not exist in dest, copy from source */
  108. }
  109. if (new_per_dir_entry->status >= orig_per_dir_entry->status) {
  110. /* use new entry */
  111. phpapdebug((stderr, "ADDING/OVERWRITING %s (%d vs. %d)\n", ZSTR_VAL(hash_key->key), new_per_dir_entry->status, orig_per_dir_entry->status));
  112. return 1;
  113. } else {
  114. return 0;
  115. }
  116. }
  117. void config_entry_ctor(zval *zv)
  118. {
  119. php_dir_entry *pe = (php_dir_entry*)Z_PTR_P(zv);
  120. php_dir_entry *npe = malloc(sizeof(php_dir_entry));
  121. memcpy(npe, pe, sizeof(php_dir_entry));
  122. ZVAL_PTR(zv, npe);
  123. }
  124. void *merge_php_config(apr_pool_t *p, void *base_conf, void *new_conf)
  125. {
  126. php_conf_rec *d = base_conf, *e = new_conf, *n = NULL;
  127. #ifdef ZTS
  128. zend_string *str;
  129. zval *data;
  130. #endif
  131. n = create_php_config(p, "merge_php_config");
  132. /* copy old config */
  133. #ifdef ZTS
  134. ZEND_HASH_FOREACH_STR_KEY_VAL(&d->config, str, data) {
  135. zend_string *key;
  136. zval *new_entry;
  137. /* Avoid sharing the non interned string among threads. */
  138. key = zend_string_dup(str, 1);
  139. new_entry = zend_hash_add(&n->config, key, data);
  140. config_entry_ctor(new_entry);
  141. } ZEND_HASH_FOREACH_END();
  142. #else
  143. zend_hash_copy(&n->config, &d->config, config_entry_ctor);
  144. #endif
  145. /* merge new config */
  146. phpapdebug((stderr, "Merge dir (%p)+(%p)=(%p)\n", base_conf, new_conf, n));
  147. zend_hash_merge_ex(&n->config, &e->config, config_entry_ctor, should_overwrite_per_dir_entry, NULL);
  148. return n;
  149. }
  150. char *get_php_config(void *conf, char *name, size_t name_len)
  151. {
  152. php_conf_rec *d = conf;
  153. php_dir_entry *pe;
  154. if ((pe = zend_hash_str_find_ptr(&d->config, name, name_len)) != NULL) {
  155. return pe->value;
  156. }
  157. return "";
  158. }
  159. void apply_config(void *dummy)
  160. {
  161. php_conf_rec *d = dummy;
  162. zend_string *str;
  163. php_dir_entry *data;
  164. ZEND_HASH_FOREACH_STR_KEY_PTR(&d->config, str, data) {
  165. phpapdebug((stderr, "APPLYING (%s)(%s)\n", ZSTR_VAL(str), data->value));
  166. if (zend_alter_ini_entry_chars(str, data->value, data->value_len, data->status, data->htaccess?PHP_INI_STAGE_HTACCESS:PHP_INI_STAGE_ACTIVATE) == FAILURE) {
  167. phpapdebug((stderr, "..FAILED\n"));
  168. }
  169. } ZEND_HASH_FOREACH_END();
  170. }
  171. const command_rec php_dir_cmds[] =
  172. {
  173. AP_INIT_TAKE2("php_value", php_apache_value_handler, NULL, OR_OPTIONS, "PHP Value Modifier"),
  174. AP_INIT_TAKE2("php_flag", php_apache_flag_handler, NULL, OR_OPTIONS, "PHP Flag Modifier"),
  175. AP_INIT_TAKE2("php_admin_value", php_apache_admin_value_handler, NULL, ACCESS_CONF|RSRC_CONF, "PHP Value Modifier (Admin)"),
  176. AP_INIT_TAKE2("php_admin_flag", php_apache_admin_flag_handler, NULL, ACCESS_CONF|RSRC_CONF, "PHP Flag Modifier (Admin)"),
  177. AP_INIT_TAKE1("PHPINIDir", php_apache_phpini_set, NULL, RSRC_CONF, "Directory containing the php.ini file"),
  178. {NULL}
  179. };
  180. static apr_status_t destroy_php_config(void *data)
  181. {
  182. php_conf_rec *d = data;
  183. phpapdebug((stderr, "Destroying config %p\n", data));
  184. zend_hash_destroy(&d->config);
  185. return APR_SUCCESS;
  186. }
  187. static void config_entry_dtor(zval *zv)
  188. {
  189. free((php_dir_entry*)Z_PTR_P(zv));
  190. }
  191. void *create_php_config(apr_pool_t *p, char *dummy)
  192. {
  193. php_conf_rec *newx = (php_conf_rec *) apr_pcalloc(p, sizeof(*newx));
  194. phpapdebug((stderr, "Creating new config (%p) for %s\n", newx, dummy));
  195. zend_hash_init(&newx->config, 0, NULL, config_entry_dtor, 1);
  196. apr_pool_cleanup_register(p, newx, destroy_php_config, apr_pool_cleanup_null);
  197. return (void *) newx;
  198. }