1 /******************************************************************************
3 * Copyright (C) 2000 Pierangelo Masarati, <ando@sys-net.it>
6 * Permission is granted to anyone to use this software for any purpose
7 * on any computer system, and to alter it and redistribute it, subject
8 * to the following restrictions:
10 * 1. The author is not responsible for the consequences of use of this
11 * software, no matter how awful, even if they arise from flaws in it.
13 * 2. The origin of this software must not be misrepresented, either by
14 * explicit claim or by omission. Since few users ever read sources,
15 * credits should appear in the documentation.
17 * 3. Altered versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software. Since few users
19 * ever read sources, credits should appear in the documentation.
21 * 4. This notice may not be removed or altered.
23 ******************************************************************************/
29 * These are required by every file of the library, so they're included here
31 #include <ac/stdlib.h>
32 #include <ac/syslog.h>
34 #include <ac/socket.h>
35 #include <ac/unistd.h>
40 #include "../libldap/ldap-int.h"
46 /* Uncomment to use ldap pvt threads */
47 #define USE_REWRITE_LDAP_PVT_THREADS
48 #include <ldap_pvt_thread.h>
51 * For details, see RATIONALE.
54 #define REWRITE_MAX_MATCH 11 /* 0: overall string; 1-9: submatches */
55 #define REWRITE_MAX_PASSES 100
58 * Submatch escape char
60 //#define REWRITE_SUBMATCH_ESCAPE '\\'
61 #define REWRITE_SUBMATCH_ESCAPE '%'
67 #define REWRITE_FLAG_HONORCASE 'C'
68 #define REWRITE_FLAG_BASICREGEX 'R'
73 #define REWRITE_FLAG_EXECONCE ':'
74 #define REWRITE_FLAG_STOP '@'
75 #define REWRITE_FLAG_UNWILLING '#'
76 #define REWRITE_FLAG_GOTO 'G' /* requires an arg */
77 #define REWRITE_FLAG_IGNORE_ERR 'I'
82 #define REWRITE_OPERATOR_SUBCONTEXT '>'
83 #define REWRITE_OPERATOR_COMMAND '|'
84 #define REWRITE_OPERATOR_VARIABLE_SET '&'
85 #define REWRITE_OPERATOR_VARIABLE_GET '*'
86 #define REWRITE_OPERATOR_PARAM_GET '$'
96 struct rewrite_action {
97 struct rewrite_action *la_next;
99 #define REWRITE_ACTION_STOP 0x0001
100 #define REWRITE_ACTION_UNWILLING 0x0002
101 #define REWRITE_ACTION_GOTO 0x0003
102 #define REWRITE_ACTION_IGNORE_ERR 0x0004
115 #define REWRITE_MAP_XFILEMAP 0x0001 /* Rough implementation! */
116 #define REWRITE_MAP_XPWDMAP 0x0002 /* uid -> gecos */
117 #define REWRITE_MAP_XLDAPMAP 0x0003 /* Not implemented yet! */
122 #define REWRITE_MAP_SUBCONTEXT 0x0101
124 #define REWRITE_MAP_SET_OP_VAR 0x0102
125 #define REWRITE_MAP_SETW_OP_VAR 0x0103
126 #define REWRITE_MAP_GET_OP_VAR 0x0104
127 #define REWRITE_MAP_SET_SESN_VAR 0x0105
128 #define REWRITE_MAP_SETW_SESN_VAR 0x0106
129 #define REWRITE_MAP_GET_SESN_VAR 0x0107
130 #define REWRITE_MAP_GET_PARAM 0x0108
131 #define REWRITE_MAP_BUILTIN 0x0109
138 * Old maps store private data in _lm_args;
139 * new maps store the substitution pattern in _lm_subst
143 struct rewrite_subst *_lm_subst;
145 #define lm_args lm_union._lm_args
146 #define lm_subst lm_union._lm_subst
148 #ifdef USE_REWRITE_LDAP_PVT_THREADS
149 ldap_pvt_thread_mutex_t lm_mutex;
150 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
156 struct rewrite_builtin_map {
157 #define REWRITE_BUILTIN_MAP_LDAP 0x0201
162 #ifdef USE_REWRITE_LDAP_PVT_THREADS
163 ldap_pvt_thread_mutex_t lb_mutex;
164 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
168 * Submatch substitution
170 struct rewrite_submatch {
171 #define REWRITE_SUBMATCH_ASIS 0x0000
172 #define REWRITE_SUBMATCH_XMAP 0x0001
173 #define REWRITE_SUBMATCH_MAP_W_ARG 0x0002
175 struct rewrite_map *ls_map;
178 * The first one represents the index of the submatch in case
179 * the map has single submatch as argument;
180 * the latter represents the map argument scheme in case
181 * the map has substitution string argument form
186 * Pattern substitution
188 struct rewrite_subst {
190 struct berval **lt_subs;
193 struct rewrite_submatch **lt_submatch;
199 struct rewrite_rule {
200 struct rewrite_rule *lr_next;
201 struct rewrite_rule *lr_prev;
204 char *lr_subststring;
209 * I was thinking about some kind of per-rule mutex, but there's
210 * probably no need, because rules after compilation are only read;
211 * however, I need to check whether regexec is reentrant ...
214 struct rewrite_subst *lr_subst;
216 #define REWRITE_REGEX_ICASE REG_ICASE
217 #define REWRITE_REGEX_EXTENDED REG_EXTENDED
220 #define REWRITE_RECURSE 0x0001
221 #define REWRITE_EXEC_ONCE 0x0002
224 struct rewrite_action *lr_action;
228 * Rewrite Context (set of rules)
230 struct rewrite_context {
232 struct rewrite_context *lc_alias;
233 struct rewrite_rule *lc_rule;
239 struct rewrite_session {
242 #ifdef USE_REWRITE_LDAP_PVT_THREADS
243 ldap_pvt_thread_rdwr_t ls_vars_mutex;
244 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
252 struct berval lv_value;
264 const void *lo_cookie;
275 struct rewrite_info {
279 * No global mutex because maps are read only at
286 #ifdef USE_REWRITE_LDAP_PVT_THREADS
287 ldap_pvt_thread_rdwr_t li_params_mutex;
288 ldap_pvt_thread_rdwr_t li_cookies_mutex;
289 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
293 * use `rewriteEngine {on|off}' directive to alter
298 * Defaults to REWRITE_MAXPASSES;
299 * use `rewriteMaxPasses numPasses' directive to alter
301 #define REWRITE_MAXPASSES 100
305 * Behavior in case a NULL or non-existent context is required
320 * Parses a map (also in legacy 'x' version)
322 extern struct rewrite_map *
324 struct rewrite_info *info,
329 extern struct rewrite_map *
331 struct rewrite_info *info,
337 * Resolves key in val by means of map (also in legacy 'x' version)
341 struct rewrite_info *info,
342 struct rewrite_op *op,
343 struct rewrite_map *map,
350 struct rewrite_info *info,
351 struct rewrite_op *op,
352 struct rewrite_map *map,
359 * Submatch substitution
363 * Compiles a substitution pattern
365 extern struct rewrite_subst *
366 rewrite_subst_compile(
367 struct rewrite_info *info,
372 * Substitutes a portion of rewritten string according to substitution
373 * pattern using submatches
377 struct rewrite_info *info,
378 struct rewrite_op *op,
379 struct rewrite_subst *subst,
381 const regmatch_t *match,
391 * Compiles the rule and appends it at the running context
394 rewrite_rule_compile(
395 struct rewrite_info *info,
396 struct rewrite_context *context,
399 const char *flagstring
403 * Rewrites string according to rule; may return:
404 * REWRITE_REGEXEC_OK: fine; if *result != NULL rule matched
405 * and rewrite succeeded.
406 * REWRITE_REGEXEC_STOP: fine, rule matched; stop processing
408 * REWRITE_REGEXEC_UNWILL: rule matched; force 'unwilling to perform'
409 * REWRITE_REGEXEC_ERR: an error occurred
413 struct rewrite_info *info,
414 struct rewrite_op *op,
415 struct rewrite_rule *rule,
425 * Fetches a struct rewrite_session
427 extern struct rewrite_session *
428 rewrite_session_find(
429 struct rewrite_info *info,
434 * Defines and inits a variable with session scope
437 rewrite_session_var_set(
438 struct rewrite_info *info,
445 * Gets a var with session scope
448 rewrite_session_var_get(
449 struct rewrite_info *info,
459 rewrite_session_delete(
460 struct rewrite_info *info,
465 * Destroys the cookie tree
468 rewrite_session_destroy(
469 struct rewrite_info *info
480 extern struct rewrite_var *
487 * Inserts a newly created var
489 extern struct rewrite_var *
499 extern struct rewrite_var *
521 * Finds the context named rewriteContext in the context tree
523 extern struct rewrite_context *
524 rewrite_context_find(
525 struct rewrite_info *info,
526 const char *rewriteContext
530 * Creates a new context called rewriteContext and stores in into the tree
532 extern struct rewrite_context *
533 rewrite_context_create(
534 struct rewrite_info *info,
535 const char *rewriteContext
539 * Rewrites string according to context; may return:
540 * OK: fine; if *result != NULL rule matched and rewrite succeeded.
541 * STOP: fine, rule matched; stop processing following rules
542 * UNWILL: rule matched; force 'unwilling to perform'
545 rewrite_context_apply(
546 struct rewrite_info *info,
547 struct rewrite_op *op,
548 struct rewrite_context *context,
553 #endif /* REWRITE_INT_H */