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/string.h>
33 #include <ac/syslog.h>
35 #include <ac/socket.h>
36 #include <ac/unistd.h>
41 #include "../libldap/ldap-int.h"
47 /* Uncomment to use ldap pvt threads */
48 #define USE_REWRITE_LDAP_PVT_THREADS
49 #include <ldap_pvt_thread.h>
52 * For details, see RATIONALE.
55 #define REWRITE_MAX_MATCH 11 /* 0: overall string; 1-9: submatches */
56 #define REWRITE_MAX_PASSES 100
59 * Submatch escape char
61 //#define REWRITE_SUBMATCH_ESCAPE '\\'
62 #define REWRITE_SUBMATCH_ESCAPE '%'
68 #define REWRITE_FLAG_HONORCASE 'C'
69 #define REWRITE_FLAG_BASICREGEX 'R'
74 #define REWRITE_FLAG_EXECONCE ':'
75 #define REWRITE_FLAG_STOP '@'
76 #define REWRITE_FLAG_UNWILLING '#'
77 #define REWRITE_FLAG_GOTO 'G' /* requires an arg */
78 #define REWRITE_FLAG_IGNORE_ERR 'I'
83 #define REWRITE_OPERATOR_SUBCONTEXT '>'
84 #define REWRITE_OPERATOR_COMMAND '|'
85 #define REWRITE_OPERATOR_VARIABLE_SET '&'
86 #define REWRITE_OPERATOR_VARIABLE_GET '*'
87 #define REWRITE_OPERATOR_PARAM_GET '$'
97 struct rewrite_action {
98 struct rewrite_action *la_next;
100 #define REWRITE_ACTION_STOP 0x0001
101 #define REWRITE_ACTION_UNWILLING 0x0002
102 #define REWRITE_ACTION_GOTO 0x0003
103 #define REWRITE_ACTION_IGNORE_ERR 0x0004
116 #define REWRITE_MAP_XFILEMAP 0x0001 /* Rough implementation! */
117 #define REWRITE_MAP_XPWDMAP 0x0002 /* uid -> gecos */
118 #define REWRITE_MAP_XLDAPMAP 0x0003 /* Not implemented yet! */
123 #define REWRITE_MAP_SUBCONTEXT 0x0101
125 #define REWRITE_MAP_SET_OP_VAR 0x0102
126 #define REWRITE_MAP_SETW_OP_VAR 0x0103
127 #define REWRITE_MAP_GET_OP_VAR 0x0104
128 #define REWRITE_MAP_SET_SESN_VAR 0x0105
129 #define REWRITE_MAP_SETW_SESN_VAR 0x0106
130 #define REWRITE_MAP_GET_SESN_VAR 0x0107
131 #define REWRITE_MAP_GET_PARAM 0x0108
132 #define REWRITE_MAP_BUILTIN 0x0109
139 * Old maps store private data in _lm_args;
140 * new maps store the substitution pattern in _lm_subst
144 struct rewrite_subst *_lm_subst;
146 #define lm_args lm_union._lm_args
147 #define lm_subst lm_union._lm_subst
149 #ifdef USE_REWRITE_LDAP_PVT_THREADS
150 ldap_pvt_thread_mutex_t lm_mutex;
151 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
157 struct rewrite_builtin_map {
158 #define REWRITE_BUILTIN_MAP_LDAP 0x0201
163 #ifdef USE_REWRITE_LDAP_PVT_THREADS
164 ldap_pvt_thread_mutex_t lb_mutex;
165 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
169 * Submatch substitution
171 struct rewrite_submatch {
172 #define REWRITE_SUBMATCH_ASIS 0x0000
173 #define REWRITE_SUBMATCH_XMAP 0x0001
174 #define REWRITE_SUBMATCH_MAP_W_ARG 0x0002
176 struct rewrite_map *ls_map;
179 * The first one represents the index of the submatch in case
180 * the map has single submatch as argument;
181 * the latter represents the map argument scheme in case
182 * the map has substitution string argument form
187 * Pattern substitution
189 struct rewrite_subst {
191 struct berval **lt_subs;
194 struct rewrite_submatch **lt_submatch;
200 struct rewrite_rule {
201 struct rewrite_rule *lr_next;
202 struct rewrite_rule *lr_prev;
205 char *lr_subststring;
210 * I was thinking about some kind of per-rule mutex, but there's
211 * probably no need, because rules after compilation are only read;
212 * however, I need to check whether regexec is reentrant ...
215 struct rewrite_subst *lr_subst;
217 #define REWRITE_REGEX_ICASE REG_ICASE
218 #define REWRITE_REGEX_EXTENDED REG_EXTENDED
221 #define REWRITE_RECURSE 0x0001
222 #define REWRITE_EXEC_ONCE 0x0002
225 struct rewrite_action *lr_action;
229 * Rewrite Context (set of rules)
231 struct rewrite_context {
233 struct rewrite_context *lc_alias;
234 struct rewrite_rule *lc_rule;
240 struct rewrite_session {
243 #ifdef USE_REWRITE_LDAP_PVT_THREADS
244 ldap_pvt_thread_rdwr_t ls_vars_mutex;
245 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
253 struct berval lv_value;
265 const void *lo_cookie;
276 struct rewrite_info {
280 * No global mutex because maps are read only at
287 #ifdef USE_REWRITE_LDAP_PVT_THREADS
288 ldap_pvt_thread_rdwr_t li_params_mutex;
289 ldap_pvt_thread_rdwr_t li_cookies_mutex;
290 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
294 * use `rewriteEngine {on|off}' directive to alter
299 * Defaults to REWRITE_MAXPASSES;
300 * use `rewriteMaxPasses numPasses' directive to alter
302 #define REWRITE_MAXPASSES 100
306 * Behavior in case a NULL or non-existent context is required
321 * Parses a map (also in legacy 'x' version)
323 extern struct rewrite_map *
325 struct rewrite_info *info,
330 extern struct rewrite_map *
332 struct rewrite_info *info,
338 * Resolves key in val by means of map (also in legacy 'x' version)
342 struct rewrite_info *info,
343 struct rewrite_op *op,
344 struct rewrite_map *map,
351 struct rewrite_info *info,
352 struct rewrite_op *op,
353 struct rewrite_map *map,
360 * Submatch substitution
364 * Compiles a substitution pattern
366 extern struct rewrite_subst *
367 rewrite_subst_compile(
368 struct rewrite_info *info,
373 * Substitutes a portion of rewritten string according to substitution
374 * pattern using submatches
378 struct rewrite_info *info,
379 struct rewrite_op *op,
380 struct rewrite_subst *subst,
382 const regmatch_t *match,
392 * Compiles the rule and appends it at the running context
395 rewrite_rule_compile(
396 struct rewrite_info *info,
397 struct rewrite_context *context,
400 const char *flagstring
404 * Rewrites string according to rule; may return:
405 * REWRITE_REGEXEC_OK: fine; if *result != NULL rule matched
406 * and rewrite succeeded.
407 * REWRITE_REGEXEC_STOP: fine, rule matched; stop processing
409 * REWRITE_REGEXEC_UNWILL: rule matched; force 'unwilling to perform'
410 * REWRITE_REGEXEC_ERR: an error occurred
414 struct rewrite_info *info,
415 struct rewrite_op *op,
416 struct rewrite_rule *rule,
426 * Fetches a struct rewrite_session
428 extern struct rewrite_session *
429 rewrite_session_find(
430 struct rewrite_info *info,
435 * Defines and inits a variable with session scope
438 rewrite_session_var_set(
439 struct rewrite_info *info,
446 * Gets a var with session scope
449 rewrite_session_var_get(
450 struct rewrite_info *info,
460 rewrite_session_delete(
461 struct rewrite_info *info,
466 * Destroys the cookie tree
469 rewrite_session_destroy(
470 struct rewrite_info *info
481 extern struct rewrite_var *
488 * Inserts a newly created var
490 extern struct rewrite_var *
500 extern struct rewrite_var *
522 * Finds the context named rewriteContext in the context tree
524 extern struct rewrite_context *
525 rewrite_context_find(
526 struct rewrite_info *info,
527 const char *rewriteContext
531 * Creates a new context called rewriteContext and stores in into the tree
533 extern struct rewrite_context *
534 rewrite_context_create(
535 struct rewrite_info *info,
536 const char *rewriteContext
540 * Rewrites string according to context; may return:
541 * OK: fine; if *result != NULL rule matched and rewrite succeeded.
542 * STOP: fine, rule matched; stop processing following rules
543 * UNWILL: rule matched; force 'unwilling to perform'
546 rewrite_context_apply(
547 struct rewrite_info *info,
548 struct rewrite_op *op,
549 struct rewrite_context *context,
554 #endif /* REWRITE_INT_H */