1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2017 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 
151 	n = create_php_config(p, "merge_php_config");
152 	/* copy old config */
153 	zend_hash_copy(&n->config, &d->config, config_entry_ctor);
154 	/* merge new config */
155 	phpapdebug((stderr, "Merge dir (%p)+(%p)=(%p)\n", base_conf, new_conf, n));
156 	zend_hash_merge_ex(&n->config, &e->config, config_entry_ctor, should_overwrite_per_dir_entry, NULL);
157 	return n;
158 }
159 
get_php_config(void * conf,char * name,size_t name_len)160 char *get_php_config(void *conf, char *name, size_t name_len)
161 {
162 	php_conf_rec *d = conf;
163 	php_dir_entry *pe;
164 
165 	if ((pe = zend_hash_str_find_ptr(&d->config, name, name_len)) != NULL) {
166 		return pe->value;
167 	}
168 
169 	return "";
170 }
171 
apply_config(void * dummy)172 void apply_config(void *dummy)
173 {
174 	php_conf_rec *d = dummy;
175 	zend_string *str;
176 	php_dir_entry *data;
177 
178 	ZEND_HASH_FOREACH_STR_KEY_PTR(&d->config, str, data) {
179 		phpapdebug((stderr, "APPLYING (%s)(%s)\n", ZSTR_VAL(str), data->value));
180 		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) {
181 			phpapdebug((stderr, "..FAILED\n"));
182 		}
183 	} ZEND_HASH_FOREACH_END();
184 }
185 
186 const command_rec php_dir_cmds[] =
187 {
188 	AP_INIT_TAKE2("php_value", php_apache_value_handler, NULL, OR_OPTIONS, "PHP Value Modifier"),
189 	AP_INIT_TAKE2("php_flag", php_apache_flag_handler, NULL, OR_OPTIONS, "PHP Flag Modifier"),
190 	AP_INIT_TAKE2("php_admin_value", php_apache_admin_value_handler, NULL, ACCESS_CONF|RSRC_CONF, "PHP Value Modifier (Admin)"),
191 	AP_INIT_TAKE2("php_admin_flag", php_apache_admin_flag_handler, NULL, ACCESS_CONF|RSRC_CONF, "PHP Flag Modifier (Admin)"),
192 	AP_INIT_TAKE1("PHPINIDir", php_apache_phpini_set, NULL, RSRC_CONF, "Directory containing the php.ini file"),
193 	{NULL}
194 };
195 
destroy_php_config(void * data)196 static apr_status_t destroy_php_config(void *data)
197 {
198 	php_conf_rec *d = data;
199 
200 	phpapdebug((stderr, "Destroying config %p\n", data));
201 	zend_hash_destroy(&d->config);
202 
203 	return APR_SUCCESS;
204 }
205 
config_entry_dtor(zval * zv)206 static void config_entry_dtor(zval *zv)
207 {
208 	free((php_dir_entry*)Z_PTR_P(zv));
209 }
210 
create_php_config(apr_pool_t * p,char * dummy)211 void *create_php_config(apr_pool_t *p, char *dummy)
212 {
213 	php_conf_rec *newx = (php_conf_rec *) apr_pcalloc(p, sizeof(*newx));
214 
215 	phpapdebug((stderr, "Creating new config (%p) for %s\n", newx, dummy));
216 	zend_hash_init(&newx->config, 0, NULL, config_entry_dtor, 1);
217 	apr_pool_cleanup_register(p, newx, destroy_php_config, apr_pool_cleanup_null);
218 	return (void *) newx;
219 }
220 
221 /*
222  * Local variables:
223  * tab-width: 4
224  * c-basic-offset: 4
225  * End:
226  * vim600: sw=4 ts=4 fdm=marker
227  * vim<600: sw=4 ts=4
228  */
229