1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/* Copyright 2004-2006 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* mod_version.c
* Allow conditional configuration depending on the httpd version
*
* Andr� Malo (nd/perlig.de), January 2004
*
* Some stuff coded here is heavily based on the core <IfModule>
* containers.
*
* The module makes the following confgurations possible:
*
* <IfVersion op major.minor.patch>
* # conditional config here ...
*</IfVersion>
*
* where "op" is one of:
* = / == equal
* > greater than
* >= greater or equal
* < less than
* <= less or equal
*
* If minor version and patch level are omitted they are assumed to be 0.
*
* Alternatively you can match the whole version (including some vendor-added
* string of the CORE version, see ap_release.h) against a regular expression:
*
* <IfVersion op regex>
* # conditional config here ...
*</IfVersion>
*
* where "op" is one of:
* = / == match; regex must be surrounded by slashes
* ~ match; regex MAY NOT be surrounded by slashes
*
* Note that all operators may be preceeded by an exclamation mark
* (without spaces) in order to reverse their meaning.
*
*/
#include "apr.h"
#include "apr_strings.h"
#include "apr_lib.h"
#include "httpd.h"
#include "http_config.h"
#include "http_log.h"
/* module structure */
module AP_MODULE_DECLARE_DATA version_module;
/* queried httpd version */
static ap_version_t httpd_version;
/*
* compare the supplied version with the core one
*/
static int compare_version(char *version_string, const char **error)
{
char *p = version_string, *ep;
int version[3] = {0, 0, 0};
int c = 0;
*error = "Version appears to be invalid. It must have the format "
"major[.minor[.patch]] where major, minor and patch are "
"numbers.";
if (!apr_isdigit(*p)) {
return 0;
}
/* parse supplied version */
ep = version_string + strlen(version_string);
while (p <= ep && c < 3) {
if (*p == '.') {
*p = '\0';
}
if (!*p) {
version[c++] = atoi(version_string);
version_string = ++p;
continue;
}
if (!apr_isdigit(*p)) {
break;
}
++p;
}
if (p < ep) { /* syntax error */
return 0;
}
*error = NULL;
if (httpd_version.major > version[0]) {
return 1;
}
else if (httpd_version.major < version[0]) {
return -1;
}
else if (httpd_version.minor > version[1]) {
return 1;
}
else if (httpd_version.minor < version[1]) {
return -1;
}
else if (httpd_version.patch > version[2]) {
return 1;
}
else if (httpd_version.patch < version[2]) {
return -1;
}
/* seems to be the same */
return 0;
}
/*
* match version against a regular expression
*/
static int match_version(apr_pool_t *pool, char *version_string,
const char **error)
{
ap_regex_t *compiled;
const char *to_match;
int rc;
compiled = ap_pregcomp(pool, version_string, AP_REG_EXTENDED);
if (!compiled) {
*error = "Unable to compile regular expression";
return 0;
}
*error = NULL;
to_match = apr_psprintf(pool, "%d.%d.%d%s",
httpd_version.major,
httpd_version.minor,
httpd_version.patch,
httpd_version.add_string);
rc = !ap_regexec(compiled, to_match, 0, NULL, 0);
ap_pregfree(pool, compiled);
return rc;
}
/*
* Implements the <IfVersion> container
*/
static const char *start_ifversion(cmd_parms *cmd, void *mconfig,
const char *arg1, const char *arg2,
const char *arg3)
{
const char *endp;
int reverse = 0, done = 0, match = 0, compare;
const char *p, *error;
char c;
/* supplying one argument is possible, we assume an equality check then */
if (!arg2) {
arg2 = arg1;
arg1 = "=";
}
/* surrounding quotes without operator */
if (!arg3 && *arg2 == '>' && !arg2[1]) {
arg3 = ">";
arg2 = arg1;
arg1 = "=";
}
/* the third argument makes version surrounding quotes plus operator
* possible.
*/
endp = arg2 + strlen(arg2);
if ( endp == arg2
|| (!(arg3 && *arg3 == '>' && !arg3[1]) && *--endp != '>')) {
return apr_pstrcat(cmd->pool, cmd->cmd->name,
"> directive missing closing '>'", NULL);
}
p = arg1;
if (*p == '!') {
reverse = 1;
if (p[1]) {
++p;
}
}
c = *p++;
if (!*p || (*p == '=' && !p[1] && c != '~')) {
if (!httpd_version.major) {
ap_get_server_revision(&httpd_version);
}
done = 1;
switch (c) {
case '=':
/* normal comparison */
if (*arg2 != '/') {
compare = compare_version(apr_pstrmemdup(cmd->pool, arg2,
endp-arg2),
&error);
if (error) {
return error;
}
match = !compare;
break;
}
/* regexp otherwise */
if (endp == ++arg2 || *--endp != '/') {
return "Missing delimiting / of regular expression.";
}
case '~':
/* regular expression */
match = match_version(cmd->pool, apr_pstrmemdup(cmd->pool, arg2,
endp-arg2),
&error);
if (error) {
return error;
}
break;
case '<':
compare = compare_version(apr_pstrmemdup(cmd->pool, arg2,
endp-arg2),
&error);
if (error) {
return error;
}
match = ((-1 == compare) || (*p && !compare));
break;
case '>':
compare = compare_version(apr_pstrmemdup(cmd->pool, arg2,
endp-arg2),
&error);
if (error) {
return error;
}
match = ((1 == compare) || (*p && !compare));
break;
default:
done = 0;
break;
}
}
if (!done) {
return apr_pstrcat(cmd->pool, "unrecognized operator '", arg1, "'",
NULL);
}
if ((!reverse && match) || (reverse && !match)) {
ap_directive_t *parent = NULL;
ap_directive_t *current = NULL;
const char *retval;
retval = ap_build_cont_config(cmd->pool, cmd->temp_pool, cmd,
¤t, &parent, "<IfVersion");
*(ap_directive_t **)mconfig = current;
return retval;
}
*(ap_directive_t **)mconfig = NULL;
return ap_soak_end_container(cmd, "<IfVersion");
}
static const command_rec version_cmds[] = {
AP_INIT_TAKE123("<IfVersion", start_ifversion, NULL, EXEC_ON_READ | OR_ALL,
"a comparison operator, a version (and a delimiter)"),
{ NULL }
};
module AP_MODULE_DECLARE_DATA version_module =
{
STANDARD20_MODULE_STUFF,
NULL, /* dir config creater */
NULL, /* dir merger --- default is to override */
NULL, /* server config */
NULL, /* merge server configs */
version_cmds, /* command apr_table_t */
NULL, /* register hooks */
};
|