-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod_define.c
executable file
·511 lines (463 loc) · 15.2 KB
/
mod_define.c
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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_define.c - Apache module for configuration defines ($xxx)
**
** Copyright (c) 2011 W. Kleedorfer <[email protected]>
** Copyright (c) 2006 Rainer Jung <[email protected]>
** Copyright (c) 1998-2000 Ralf S. Engelschall <[email protected]>
** Copyright (c) 1998-2000 Christian Reiber <[email protected]>
**
*/
/*
* $Id: mod_define.c,v 1.4 2006/10/11 01:28:35 jung Exp $
*
* HISTORY
*
* v1.0: Originally written in December 1998 by
* Ralf S. Engelschall <[email protected]> and
* Christian Reiber <[email protected]>
*
* v1.1: Completely Overhauled in August 1999 by
* Ralf S. Engelschall <[email protected]>
*
* v2.0: Ported for Apache 2.0 and 2.2 in August 2006 by
* Rainer Jung <[email protected]>
*
* v2.1: Minor fixes in October 2006 by
* Rainer Jung <[email protected]>
*
* v2.2: Added file scope to the variables in Feb. 2011
* W. Kleedorfer <[email protected]>
*/
#include "httpd.h"
#include "http_config.h"
#include "http_log.h"
#include "apr_strings.h"
#include "apr_lib.h"
module AP_MODULE_DECLARE_DATA define_module;
/*
* The global table of defines
*/
static apr_table_t *tDefines = NULL; /* global table of defines */
static int bOnceSeenADefine = FALSE; /* optimization flag */
/*
* Forward declaration
*/
static int DefineIndex (apr_pool_t *, const char *, char *, int *, int *, char **);
static char *DefineFetch (apr_pool_t *, const char *, char *);
static char *DefineExpand (apr_pool_t *, char *, int, char *);
static void DefineInit (apr_pool_t *);
static apr_status_t DefineCleanup (void *);
static int DefineRewriteHook(apr_pool_t *pconf, apr_pool_t *plog,
ap_directive_t *current);
static int DefineWalkConfigSub(apr_pool_t *pconf, apr_pool_t *plog,
ap_directive_t *current);
static int DefineWalkConfig(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp);
/*
* Character classes for scanner function
*/
typedef enum {
CC_ESCAPE, CC_DOLLAR, CC_BRACEOPEN, CC_BRACECLOSE,
CC_IDCHAR1, CC_IDCHAR, CC_OTHER, CC_EOS
} CharClass;
/*
* Scanner states for scanner function
*/
typedef enum {
SS_NONE, SS_SKIP, SS_DOLLAR, SS_TOKEN_BRACED,
SS_TOKEN_UNBRACED, SS_ERROR, SS_FOUND
} ScanState;
/*
* Default meta characters
*/
#define DEFAULT_MC_ESCAPE "\\"
#define DEFAULT_MC_DOLLAR "$"
#define DEFAULT_MC_BRACEOPEN "{"
#define DEFAULT_MC_BRACECLOSE "}"
/*
* Scanner for variable constructs $xxx and ${xxx}
*/
static int DefineIndex(apr_pool_t *p, const char *pScope, char *cpLine, int *pos, int *len, char **cpVar)
{
int rc;
char *cp;
char *cp2;
CharClass cc;
char cEscape;
char cDefine;
char cBraceOpen;
char cBraceClose;
char *cpError;
ScanState s;
cEscape = DEFAULT_MC_ESCAPE[0];
if ((cp = DefineFetch(p, "mod_define", "escape")) != NULL)
cEscape = cp[0];
cDefine = DEFAULT_MC_DOLLAR[0];
if ((cp = DefineFetch(p, "mod_define", "dollar")) != NULL)
cDefine = cp[0];
cBraceOpen = DEFAULT_MC_BRACEOPEN[0];
if ((cp = DefineFetch(p, "mod_define", "braceopen")) != NULL)
cBraceOpen = cp[0];
cBraceClose = DEFAULT_MC_BRACECLOSE[0];
if ((cp = DefineFetch(p, "mod_define", "braceclose")) != NULL)
cBraceClose = cp[0];
rc = 0;
*len = 0;
cc = CC_OTHER;
s = SS_NONE;
for (cp = cpLine+(*pos); cc != CC_EOS; cp++) {
if (*cp == cEscape)
cc = CC_ESCAPE;
else if (*cp == cDefine)
cc = CC_DOLLAR;
else if (*cp == cBraceOpen)
cc = CC_BRACEOPEN;
else if (*cp == cBraceClose)
cc = CC_BRACECLOSE;
else if (apr_isalpha(*cp))
cc = CC_IDCHAR1;
else if (apr_isdigit(*cp) || *cp == '_' || *cp == ':' || *cp == '-')
cc = CC_IDCHAR;
else if (*cp == '\0')
cc = CC_EOS;
else
cc = CC_OTHER;
switch (s) {
case SS_NONE:
switch (cc) {
case CC_ESCAPE:
s = SS_SKIP;
break;
case CC_DOLLAR:
s = SS_DOLLAR;
break;
default:
break;
}
break;
case SS_SKIP:
s = SS_NONE;
continue;
break;
case SS_DOLLAR:
switch (cc) {
case CC_BRACEOPEN:
s = SS_TOKEN_BRACED;
*pos = cp-cpLine-1;
(*len) = 2;
*cpVar = cp+1;
break;
case CC_IDCHAR1:
s = SS_TOKEN_UNBRACED;
*pos = cp-cpLine-1;
(*len) = 2;
*cpVar = cp;
break;
case CC_ESCAPE:
s = SS_SKIP;
break;
default:
s = SS_NONE;
break;
}
break;
case SS_TOKEN_BRACED:
switch (cc) {
case CC_IDCHAR1:
case CC_IDCHAR:
(*len)++;
break;
case CC_BRACECLOSE:
(*len)++;
cp2 = apr_palloc(p, cp-*cpVar+1);
apr_cpystrn(cp2, *cpVar, cp-*cpVar+1);
*cpVar = cp2;
s = SS_FOUND;
break;
default:
cpError = apr_psprintf(p, "Illegal character '%c' in identifier", *cp);
s = SS_ERROR;
break;
}
break;
case SS_TOKEN_UNBRACED:
switch (cc) {
case CC_IDCHAR1:
case CC_IDCHAR:
(*len)++;
break;
default:
cp2 = apr_palloc(p, cp-*cpVar+1);
apr_cpystrn(cp2, *cpVar, cp-*cpVar+1);
*cpVar = cp2;
s = SS_FOUND;
break;
}
break;
case SS_FOUND:
case SS_ERROR:
break;
}
if (s == SS_ERROR) {
fprintf(stderr, "Error: %s\n", cpError);
break;
}
else if (s == SS_FOUND) {
rc = 1;
break;
}
}
return rc;
}
/*
* Since mod_define reads all its vars before parsing starts, if you have more than one of each var
* (i.e. in your VirtualHost files) then only the last one would be used, instead of each define in its
* own file. to counter this a scope filename::varname is added.
*/
static char *CreateNewVarName(const char *cpVar, const char *pScope, apr_pool_t *p)
{
int pos = strcspn(cpVar, ":");
int varlen = strlen(cpVar);
// check if ":" is in the varname and there is enough space for a second ":" and at least one char
if (pos < (varlen - 2) && cpVar[pos + 1] == ':')
{
// obviously there is already a scope there
pScope = NULL;
}
char *newname;
if (pScope != NULL)
{
// allocate some memory from the pool
// enough so filename::varname (plus trailing zero) fits
newname = apr_palloc(p, strlen(cpVar) + strlen(pScope) + 3);
strcpy(newname, pScope);
strcat(newname, "::");
strcat(newname, cpVar);
}
else
{
// if there is no filename just keep using the old names
newname = apr_palloc(p, strlen(cpVar) + 1);
strcpy(newname, cpVar);
}
return newname;
}
/*
* Determine the value of a variable
*/
static char *DefineFetch(apr_pool_t *p, const char *pScope, char *cpVar)
{
char *cpVal;
char *cpNewName;
// check if the scope is available
if (pScope != NULL && strlen(pScope) > 0)
cpNewName = CreateNewVarName(cpVar, pScope, p);
else
cpNewName = CreateNewVarName(cpVar, NULL, p);
/* first try out table */
if ((cpVal = (char *)apr_table_get(tDefines, cpNewName)) != NULL)
return cpVal;
/* second try the environment */
if ((cpVal = getenv(cpVar)) != NULL)
return cpVal;
return NULL;
}
/*
* Expand a variable
*/
static char *DefineExpand(apr_pool_t *p, char *cpToken, int tok_len, char *cpVal)
{
char *cp;
int val_len, rest_len;
val_len = strlen(cpVal);
rest_len = strlen(cpToken+tok_len);
if (val_len < tok_len)
memcpy(cpToken+val_len, cpToken+tok_len, rest_len+1);
else if (val_len > tok_len)
for (cp = cpToken+strlen(cpToken); cp > cpToken+tok_len-1; cp--)
*(cp+(val_len-tok_len)) = *cp;
memcpy(cpToken, cpVal, val_len);
return NULL;
}
/*
* This routine is called before the server processes the configuration
* files.
*/
static int DefineWalkConfig(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp)
{
/* runtime optimization */
if (!bOnceSeenADefine)
return OK;
DefineWalkConfigSub(pconf, plog, ap_conftree);
return OK;
}
/*
* This routine is called to patch the variables recursively.
*/
static int DefineWalkConfigSub(apr_pool_t *pconf, apr_pool_t *plog,
ap_directive_t *current)
{
/* scan through all directives, executing each one */
if ( current != NULL ) {
DefineRewriteHook(pconf, plog, current);
if ( current->first_child != NULL ) {
DefineWalkConfigSub(pconf, plog, current->first_child);
}
if ( current->next != NULL ) {
DefineWalkConfigSub(pconf, plog, current->next);
}
}
return OK;
}
/*
* This routine is called to patch the variables
* into one directive.
*/
static int DefineRewriteHook(apr_pool_t *pconf, apr_pool_t *plog,
ap_directive_t *current)
{
char *cpBuf;
char *cpLine;
int pos;
int len;
char *cpError;
char *cpVar;
char *cpVal;
const char *pFilename = current?current->filename:NULL;
/*
* Search for:
* ....\$[a-zA-Z][:_a-zA-Z0-9]*....
* ....\${[a-zA-Z][:_a-zA-Z0-9]*}....
*/
cpBuf = NULL;
cpLine = (char *)current->args;
pos = 0;
while (DefineIndex(pconf, pFilename, cpLine, &pos, &len, &cpVar)) {
#ifdef DEFINE_DEBUG
{
char prefix[1024];
char marker[1024];
int i;
for (i = 0; i < pos; i++)
prefix[i] = ' ';
prefix[i] = '\0';
for (i = 0; i < len; i++)
marker[i] = '^';
marker[i] = '\0';
fprintf(stderr,
"Found variable `%s' (pos: %d, len: %d)\n"
" %s\n"
" %s%s\n",
cpVar, pos, len, cpLine, prefix, marker);
}
#endif
if (cpBuf == NULL) {
cpBuf = apr_palloc(pconf, MAX_STRING_LEN);
apr_cpystrn(cpBuf, current->args, MAX_STRING_LEN);
cpLine = cpBuf;
}
if ((cpVal = DefineFetch(pconf, pFilename, cpVar)) == NULL) {
ap_log_perror(APLOG_MARK, APLOG_ERR, APR_SUCCESS, plog,
"mod_define: Variable '%s' not defined: file %s, line %d",
cpVar, current->filename,
current->line_num);
cpBuf = NULL;
break;
}
if ((cpError = DefineExpand(pconf, cpLine+pos, len, cpVal)) != NULL) {
ap_log_perror(APLOG_MARK, APLOG_ERR, APR_SUCCESS, plog,
"mod_define: %s: file %s, line %d",
cpError, current->filename,
current->line_num);
cpBuf = NULL;
break;
}
}
if ( cpBuf ) {
current->args = cpBuf;
}
return OK;
}
/*
* Implementation of the `Define' configuration directive
*/
static const char *cmd_define(cmd_parms *cmd, void *config,
const char *cpVar, const char *cpVal)
{
char *cpNewName = NULL;
if (tDefines == NULL)
DefineInit(cmd->pool);
// due to lack of documentation i am not sure when what parameters are actually
// valid, so this is me being careful
if (cmd != NULL && cmd->config_file != NULL)
cpNewName = CreateNewVarName(cpVar, cmd->config_file->name, cmd->pool);
else if (cmd != NULL)
cpNewName = CreateNewVarName(cpVar, NULL, cmd->pool);
else
cpNewName = (char*)cpVar;
apr_table_set(tDefines, cpNewName, cpVal);
bOnceSeenADefine = TRUE;
return NULL;
}
/*
* Module Initialization
*/
static void DefineInit(apr_pool_t *p)
{
tDefines = apr_table_make(p, 10);
/* predefine delimiters */
apr_table_set(tDefines, CreateNewVarName("escape", "mod_define", p), DEFAULT_MC_ESCAPE);
apr_table_set(tDefines, "mod_define::dollar", DEFAULT_MC_DOLLAR);
apr_table_set(tDefines, "mod_define::open", DEFAULT_MC_BRACEOPEN);
apr_table_set(tDefines, "mod_define::close", DEFAULT_MC_BRACECLOSE);
apr_table_set(tDefines, "mod_define::empty", "");
apr_pool_cleanup_register(p, NULL, DefineCleanup, apr_pool_cleanup_null);
return;
}
/*
* Module Cleanup
*/
static apr_status_t DefineCleanup(void *data)
{
/* reset private variables when config pool is cleared */
tDefines = NULL;
bOnceSeenADefine = FALSE;
return APR_SUCCESS;
}
/*
* Module Directive lists
*/
static const command_rec DefineDirectives[] = {
AP_INIT_TAKE2("Define", cmd_define, NULL, RSRC_CONF|ACCESS_CONF|EXEC_ON_READ,
"Define a configuration variable"),
{ NULL }
};
static void define_register_hooks(apr_pool_t *p)
{
ap_hook_pre_config(DefineWalkConfig, NULL, NULL, APR_HOOK_MIDDLE);
}
module AP_MODULE_DECLARE_DATA define_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-dir config structures */
NULL, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
DefineDirectives, /* table of config file commands */
define_register_hooks /* register hooks */
};