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