apache_config.c 7.3 KB

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