1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2013 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_update(&d->config, (char *) name, strlen(name) + 1, &e, sizeof(e), NULL);
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,php_dir_entry * new_per_dir_entry,zend_hash_key * hash_key,void * pData)120 static zend_bool should_overwrite_per_dir_entry(HashTable *target_ht, php_dir_entry *new_per_dir_entry, zend_hash_key *hash_key, void *pData)
121 {
122 	php_dir_entry *orig_per_dir_entry;
123 
124 	if (zend_hash_find(target_ht, hash_key->arKey, hash_key->nKeyLength, (void **) &orig_per_dir_entry)==FAILURE) {
125 		return 1; /* does not exist in dest, copy from source */
126 	}
127 
128 	if (new_per_dir_entry->status >= orig_per_dir_entry->status) {
129 		/* use new entry */
130 		phpapdebug((stderr, "ADDING/OVERWRITING %s (%d vs. %d)\n", hash_key->arKey, new_per_dir_entry->status, orig_per_dir_entry->status));
131 		return 1;
132 	} else {
133 		return 0;
134 	}
135 }
136 
137 
merge_php_config(apr_pool_t * p,void * base_conf,void * new_conf)138 void *merge_php_config(apr_pool_t *p, void *base_conf, void *new_conf)
139 {
140 	php_conf_rec *d = base_conf, *e = new_conf, *n = NULL;
141 #if STAS_0
142 	php_dir_entry *pe;
143 	php_dir_entry *data;
144 	char *str;
145 	uint str_len;
146 	ulong num_index;
147 #endif
148 
149 	n = create_php_config(p, "merge_php_config");
150 	/* copy old config */
151 	zend_hash_copy(&n->config, &d->config, NULL, NULL, sizeof(php_dir_entry));
152 	/* merge new config */
153 	phpapdebug((stderr, "Merge dir (%p)+(%p)=(%p)\n", base_conf, new_conf, n));
154 	zend_hash_merge_ex(&n->config, &e->config, NULL, sizeof(php_dir_entry), (merge_checker_func_t) should_overwrite_per_dir_entry, NULL);
155 #if STAS_0
156 	for (zend_hash_internal_pointer_reset(&d->config);
157 			zend_hash_get_current_key_ex(&d->config, &str, &str_len,
158 				&num_index, 0, NULL) == HASH_KEY_IS_STRING;
159 			zend_hash_move_forward(&d->config)) {
160 		pe = NULL;
161 		zend_hash_get_current_data(&d->config, (void **) &data);
162 		if (zend_hash_find(&n->config, str, str_len, (void **) &pe) == SUCCESS) {
163 			if (pe->status >= data->status) continue;
164 		}
165 		phpapdebug((stderr, "ADDING/OVERWRITING %s (%d vs. %d)\n", str, data->status, pe?pe->status:-1));
166 		zend_hash_update(&n->config, str, str_len, data, sizeof(*data), NULL);
167 	}
168 #endif
169 	return n;
170 }
171 
get_php_config(void * conf,char * name,size_t name_len)172 char *get_php_config(void *conf, char *name, size_t name_len)
173 {
174 	php_conf_rec *d = conf;
175 	php_dir_entry *pe;
176 
177 	if (zend_hash_find(&d->config, name, name_len, (void **) &pe) == SUCCESS) {
178 		return pe->value;
179 	}
180 
181 	return "";
182 }
183 
apply_config(void * dummy)184 void apply_config(void *dummy)
185 {
186 	php_conf_rec *d = dummy;
187 	char *str;
188 	uint str_len;
189 	php_dir_entry *data;
190 
191 	for (zend_hash_internal_pointer_reset(&d->config);
192 			zend_hash_get_current_key_ex(&d->config, &str, &str_len, NULL, 0,
193 				NULL) == HASH_KEY_IS_STRING;
194 			zend_hash_move_forward(&d->config)) {
195 		if (zend_hash_get_current_data(&d->config, (void **) &data) == SUCCESS) {
196 			phpapdebug((stderr, "APPLYING (%s)(%s)\n", str, data->value));
197 			if (zend_alter_ini_entry(str, str_len, data->value, data->value_len, data->status, data->htaccess?PHP_INI_STAGE_HTACCESS:PHP_INI_STAGE_ACTIVATE) == FAILURE) {
198 				phpapdebug((stderr, "..FAILED\n"));
199 			}
200 		}
201 	}
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 
create_php_config(apr_pool_t * p,char * dummy)224 void *create_php_config(apr_pool_t *p, char *dummy)
225 {
226 	php_conf_rec *newx = (php_conf_rec *) apr_pcalloc(p, sizeof(*newx));
227 
228 	phpapdebug((stderr, "Creating new config (%p) for %s\n", newx, dummy));
229 	zend_hash_init(&newx->config, 0, NULL, NULL, 1);
230 	apr_pool_cleanup_register(p, newx, destroy_php_config, apr_pool_cleanup_null);
231 	return (void *) newx;
232 }
233 
234 /*
235  * Local variables:
236  * tab-width: 4
237  * c-basic-offset: 4
238  * End:
239  * vim600: sw=4 ts=4 fdm=marker
240  * vim<600: sw=4 ts=4
241  */
242