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