summaryrefslogtreecommitdiffstats
path: root/mod_virtualvalue.c
blob: 02605544a02f99349a141321b9792dc217a7c8d5 (plain) (blame)
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
/*
 * (c) 2010-2012 - Bertrand Jacquin/Exosec <bjacquin@exosec.fr>
 *    Thierry Fournier/Exosec <tfournier@exosec.fr>
 *
 * mod_virtualvalue
 */

#include <httpd.h>
#include <http_config.h>
#include <http_core.h>
#include <http_request.h>
#include <http_log.h>

#include <apr_strings.h>

#include <sys/stat.h>

#include "mod_virtualvalue.h"

#include "al_alias.h"
#include "al_authn_file.h"
#include "al_core.h"
#include "al_dumb.h"
#include "al_php5.h"

#ifdef DEBUG
# warning DEBUG is ON, this will slow every processing. You are warned !
#endif /* DEBUG */

module AP_MODULE_DECLARE_DATA virtualvalue_module;

typedef struct {
  char  *tok;
  int    argc;
  char **argv;
  int  (*handler)(int, char **, request_rec *);
  void  *next;
} virtualvalue_var_t;

typedef struct {
  char                enable;
  virtualvalue_var_t *vars;
} virtualvalue_cfg_t;

typedef struct {
  const char   *tok;
  unsigned int  len;
  int         (*func)(int, char **, request_rec *);
} token_t;

typedef struct {
  const char  *tag;
  unsigned int len;
  const char  *(*func)(char *, char *, request_rec *);
} tag_t;

/*
 * Create new configuration element
 */
static virtualvalue_var_t *vv_var_new (void) {
  virtualvalue_var_t *elm;

  elm = (virtualvalue_var_t *)malloc(sizeof(virtualvalue_var_t));

  if (!elm)
    return NULL;

  elm->tok  = NULL;
  elm->argc = 0;
  elm->argv = NULL;
  elm->next = NULL;

  return (elm);
}

static virtualvalue_var_t *vv_var_append (virtualvalue_var_t *list, virtualvalue_var_t *elm) {
  virtualvalue_var_t *c;

  if (!list || !elm)
    return NULL;

  c = list;

  while (c->next) {
    c = (virtualvalue_var_t *)c->next;
  }

  c->next = elm;
  return list;
}

static const char *vv_replace_HTTP (char *full __U__, char *search, request_rec *r);
static const char *vv_replace_ENV  (char *full __U__, char *search, request_rec *r);
static const char *vv_replace_SYM  (char *full __U__, char *search, request_rec *r);

token_t token_tab[] = {
/*{ Config Token      , Token lenght
 *                        , Token handler         }, */
#ifdef HAVE_PHP
  { "php_value"       , 9 , vv_php_alter_ini      },
  { "php_flag"        , 8 , vv_php_alter_ini      },
  { "php_admin_value" , 15, vv_php_alter_ini      },
  { "php_admin_flag"  , 14, vv_php_alter_ini      },
#endif /* HAVE_PHP */
  { "DocumentRoot"    , 13, vv_alter_docroot      },
  { "ScriptAlias"     , 12, vv_alter_scriptalias  },
  { "ServerAdmin"     , 12, vv_alter_serveradmin  },
  { "ServerName"      , 11, vv_alter_servername   },
  { "AuthUserFile"    , 13, vv_alter_authuserfile },
  { "echo"            , 5 , vv_echo               },
  { NULL              , 0 , NULL                  }
};

tag_t tag_tab[] = {
  { "HTTP", 4, vv_replace_HTTP },
  { "ENV" , 3, vv_replace_ENV  },
  { "SYM" , 3, vv_replace_SYM  },
  { NULL  , 0, NULL            }
};

static char *vv_strrrstr (char *start, char *cur, const char *needle) {
  char *s;
  int len = strlen (needle);

  s = NULL;

  while (cur >= start) {
    if (strncmp(cur, needle, len) == 0) {
      s = cur;
      break;
    }

    cur--;
  }

  return s;
}

static const char *vv_replace_HTTP (char *full __U__, char *search, request_rec *r) {
  const char *header;

  header = apr_table_get(r->headers_in, search);

  if (!header) {
    VV_error("Header %s not found", search);
    header = apr_pstrdup (r->pool, "NULL");
  }

  return (header);
}

static const char *vv_replace_ENV (char *full __U__, char *search, request_rec *r) {
  const char *env;

  env = apr_table_get(r->subprocess_env, search);

  if (!env) {
    VV_error("ENV %s not found", search);
    env = apr_pstrdup (r->pool, "NULL");
  }

  return (env);
}

static const char *vv_replace_SYM (char *full __U__, char *search, request_rec *r) {
  struct stat st;
  char        *real;

  real = (char *)apr_palloc (r->pool, PATH_MAX * sizeof(char));

  if (search[0] != '/') {
    VV_error("Bad search: %s", search);
    apr_cpystrn (real, "NULL", 5);
    return (real);
  }

  real[0] = 0;

  if (lstat(search, &st) == -1) {
    apr_cpystrn (real, "NULL", 5);
    return (real);
  }

  realpath (search, real);

  if (lstat(real, &st) == -1) {
    apr_cpystrn (real, "NULL", 5);
    return (real);
  }

  return (real);
}

char *vv_flat_arg (char *s1, request_rec *r) {
  char *c, *end;
  const char *s2;

  tag_t *i;

  char final[DEFAULT_MAX_SIZE];
  char search[DEFAULT_MAX_SEARCH];

  apr_cpystrn (final, s1, strlen(s1)+1);

  c = final + strlen(s1);

  while ((c = vv_strrrstr(final, c, "%{"))) {
    end = strchr(c+2, '}');

    if (!end) {
      VV_error("No end found for %s", c);
      return NULL;
    }

    for (i = tag_tab, s2 = NULL ; i->tag != NULL ; i++) {
      if (strncmp (c+2, i->tag, i->len) == 0) {
        memcpy (&search, c+2 + i->len +1, end - (c+2 + i->len) -1);
        search[end - (c+2 + i->len) -1] = '\0';

        s2 = i->func (c, search, r);

        break;
      }
    }

    if (s2) {
      memmove (c + strlen(s2), end+1, strlen(c));
      memcpy (c, s2, strlen(s2));
    }

    c--;
  }

  return apr_pstrndup(r->pool, final, strlen(final));
}

static void *vv_create_server_config (apr_pool_t *p, server_rec *s __U__) {
  virtualvalue_cfg_t *cfg = (virtualvalue_cfg_t *)apr_pcalloc(p, sizeof(virtualvalue_cfg_t));

  return (void *)cfg;
}

static void *vv_merge_server_config (apr_pool_t *p __U__, void *parent __U__, void *child) {
  virtualvalue_cfg_t *c_cfg = (virtualvalue_cfg_t *) child;

  return (void *)c_cfg;
}

static int vv_translate(request_rec *r) {
  virtualvalue_cfg_t *c = (virtualvalue_cfg_t *)ap_get_module_config(r->server->module_config, &virtualvalue_module);
  virtualvalue_var_t *n;

  if (!c->vars)
    return (DECLINED);

  n = c->vars;

  while (n) {
    n->handler (n->argc, n->argv, r);

    n = (virtualvalue_var_t *)n->next;
  }

  /* Don't return OK, as mod_userdir will also change file path */
  return (DECLINED);
}

static const char *vv_set (cmd_parms *cmd, void *struct_ptr __U__, int argc, char *const argv[]) {
  virtualvalue_cfg_t *c = (virtualvalue_cfg_t *)ap_get_module_config(cmd->server->module_config, &virtualvalue_module);
  virtualvalue_var_t *n;
  token_t *i;

  if (!c->enable) {
    VV_debug("c->enable: %d", c->enable);
    return NULL;
  }

  if (!argc || !argv[0])
    return NULL;

  n = vv_var_new ();

  if (!n)
    return NULL;

  if (!c->vars) {
    c->vars = n;
  }
  else {
    vv_var_append (c->vars, n);
  }

  n->tok = argv[0];
  n->argc = argc - 1;

  for (i = token_tab ; i->tok != NULL ; i++) {
    if (strncmp(i->tok, n->tok, i->len) == 0) {
      n->handler = i->func;
      break;
    }
  }

  if (i->tok == NULL) {
    return apr_pstrcat(cmd->pool, "Invalid Token ", n->tok, NULL);
  }

  n->argv = (char **)malloc(n->argc * sizeof(char*));

  while (--argc > 0) { /* Or --argc >= 1, We don't need argv[0], as is tok */
    n->argv[argc-1] = argv[argc];
  }

  return NULL;
}

static const char *vv_on (cmd_parms *cmd, void *struct_ptr __U__, int flag) {
  virtualvalue_cfg_t *cfg = (virtualvalue_cfg_t *)ap_get_module_config(cmd->server->module_config, &virtualvalue_module);

  cfg->enable = (flag ? 1 : 0);
  VV_debug ("VirtualValue enable: %i", (unsigned int)cfg->enable);

  return NULL;
}

static void register_hooks(apr_pool_t *p __U__) {
  /* Theses modules need to be loaded *before* mod_virtualvalue */
  static const char *const aszPre[] = {
    "mod_alias.c",
    "mod_setenvif.c",
    NULL
  };

  /* Theses modules need to be loaded *after* mod_virtualvalue */
  static const char *const aszSucc[] = {
    "mod_proxy.c",
    "mod_proxy_http.c",
    "mod_userdir.c",
    NULL
  };

  //ap_hook_translate_name(vv_translate, aszPre, aszSucc, APR_HOOK_FIRST);
  ap_hook_translate_name(vv_translate, aszPre, aszSucc, APR_HOOK_REALLY_FIRST);
}

static const command_rec vv_cmds[] = {
  AP_INIT_FLAG (
    "VirtualValueEnabled",
    vv_on,
    NULL,
    RSRC_CONF,
    "Turn on Apache VirtualValue"
  ),
  AP_INIT_TAKE_ARGV (
    "VirtualValue",
    vv_set,
    NULL,
    RSRC_CONF,
    "Append a VirtualValue"
  ),
  { NULL, { NULL }, NULL, 0, NO_ARGS, NULL}
};

module AP_MODULE_DECLARE_DATA virtualvalue_module = {
  STANDARD20_MODULE_STUFF,
  NULL,
  NULL,
  vv_create_server_config,
  vv_merge_server_config,
  vv_cmds,
  register_hooks,
};