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