2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by Pierangelo Masarati for
17 * inclusion in OpenLDAP Software.
24 * These are required by every file of the library, so they're included here
26 #include <ac/stdlib.h>
27 #include <ac/string.h>
28 #include <ac/syslog.h>
30 #include <ac/socket.h>
31 #include <ac/unistd.h>
36 #include "../libldap/ldap-int.h"
42 /* Uncomment to use ldap pvt threads */
43 #define USE_REWRITE_LDAP_PVT_THREADS
44 #include <ldap_pvt_thread.h>
47 * For details, see RATIONALE.
50 #define REWRITE_MAX_MATCH 11 /* 0: overall string; 1-9: submatches */
51 #define REWRITE_MAX_PASSES 100
54 * Submatch escape char
56 /* the '\' conflicts with slapd.conf parsing */
57 /* #define REWRITE_SUBMATCH_ESCAPE '\\' */
58 #define REWRITE_SUBMATCH_ESCAPE_ORIG '%'
59 #define REWRITE_SUBMATCH_ESCAPE '$'
60 #define IS_REWRITE_SUBMATCH_ESCAPE(c) \
61 ((c) == REWRITE_SUBMATCH_ESCAPE || (c) == REWRITE_SUBMATCH_ESCAPE_ORIG)
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_USER 'U' /* requires an arg */
78 #define REWRITE_FLAG_MAX_PASSES 'M' /* requires an arg */
79 #define REWRITE_FLAG_IGNORE_ERR 'I'
84 #define REWRITE_OPERATOR_SUBCONTEXT '>'
85 #define REWRITE_OPERATOR_COMMAND '|'
86 #define REWRITE_OPERATOR_VARIABLE_SET '&'
87 #define REWRITE_OPERATOR_VARIABLE_GET '*'
88 #define REWRITE_OPERATOR_PARAM_GET '$'
98 struct rewrite_action {
99 struct rewrite_action *la_next;
101 #define REWRITE_ACTION_STOP 0x0001
102 #define REWRITE_ACTION_UNWILLING 0x0002
103 #define REWRITE_ACTION_GOTO 0x0003
104 #define REWRITE_ACTION_IGNORE_ERR 0x0004
105 #define REWRITE_ACTION_USER 0x0005
118 #define REWRITE_MAP_XFILEMAP 0x0001 /* Rough implementation! */
119 #define REWRITE_MAP_XPWDMAP 0x0002 /* uid -> gecos */
120 #define REWRITE_MAP_XLDAPMAP 0x0003 /* Not implemented yet! */
125 #define REWRITE_MAP_SUBCONTEXT 0x0101
127 #define REWRITE_MAP_SET_OP_VAR 0x0102
128 #define REWRITE_MAP_SETW_OP_VAR 0x0103
129 #define REWRITE_MAP_GET_OP_VAR 0x0104
130 #define REWRITE_MAP_SET_SESN_VAR 0x0105
131 #define REWRITE_MAP_SETW_SESN_VAR 0x0106
132 #define REWRITE_MAP_GET_SESN_VAR 0x0107
133 #define REWRITE_MAP_GET_PARAM 0x0108
134 #define REWRITE_MAP_BUILTIN 0x0109
141 * Old maps store private data in _lm_args;
142 * new maps store the substitution pattern in _lm_subst
146 struct rewrite_subst *_lm_subst;
148 #define lm_args lm_union._lm_args
149 #define lm_subst lm_union._lm_subst
151 #ifdef USE_REWRITE_LDAP_PVT_THREADS
152 ldap_pvt_thread_mutex_t lm_mutex;
153 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
159 struct rewrite_builtin_map {
160 #define REWRITE_BUILTIN_MAP_LDAP 0x0201
165 #ifdef USE_REWRITE_LDAP_PVT_THREADS
166 ldap_pvt_thread_mutex_t lb_mutex;
167 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
171 * Submatch substitution
173 struct rewrite_submatch {
174 #define REWRITE_SUBMATCH_ASIS 0x0000
175 #define REWRITE_SUBMATCH_XMAP 0x0001
176 #define REWRITE_SUBMATCH_MAP_W_ARG 0x0002
178 struct rewrite_map *ls_map;
181 * The first one represents the index of the submatch in case
182 * the map has single submatch as argument;
183 * the latter represents the map argument scheme in case
184 * the map has substitution string argument form
189 * Pattern substitution
191 struct rewrite_subst {
193 struct berval *lt_subs;
196 struct rewrite_submatch *lt_submatch;
202 struct rewrite_rule {
203 struct rewrite_rule *lr_next;
204 struct rewrite_rule *lr_prev;
207 char *lr_subststring;
209 #ifdef USE_REWRITE_LDAP_PVT_THREADS
210 ldap_pvt_thread_mutex_t lr_mutex;
211 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
215 * I was thinking about some kind of per-rule mutex, but there's
216 * probably no need, because rules after compilation are only read;
217 * however, I need to check whether regexec is reentrant ...
220 struct rewrite_subst *lr_subst;
222 #define REWRITE_REGEX_ICASE REG_ICASE
223 #define REWRITE_REGEX_EXTENDED REG_EXTENDED
226 #define REWRITE_RECURSE 0x0001
227 #define REWRITE_EXEC_ONCE 0x0002
231 struct rewrite_action *lr_action;
235 * Rewrite Context (set of rules)
237 struct rewrite_context {
239 struct rewrite_context *lc_alias;
240 struct rewrite_rule *lc_rule;
246 struct rewrite_session {
249 #ifdef USE_REWRITE_LDAP_PVT_THREADS
250 ldap_pvt_thread_rdwr_t ls_vars_mutex;
251 ldap_pvt_thread_mutex_t ls_mutex;
252 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
262 struct berval lv_value;
271 #if 0 /* FIXME: not used anywhere! (debug? then, why strdup?) */
276 const void *lo_cookie;
287 struct rewrite_info {
291 * No global mutex because maps are read only at
298 #ifdef USE_REWRITE_LDAP_PVT_THREADS
299 ldap_pvt_thread_rdwr_t li_params_mutex;
300 ldap_pvt_thread_rdwr_t li_cookies_mutex;
301 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
305 * use `rewriteEngine {on|off}' directive to alter
310 * Defaults to REWRITE_MAXPASSES;
311 * use `rewriteMaxPasses numPasses' directive to alter
313 #define REWRITE_MAXPASSES 100
315 int li_max_passes_per_rule;
318 * Behavior in case a NULL or non-existent context is required
327 LDAP_REWRITE_V (struct rewrite_context*) rewrite_int_curr_context;
334 * Parses a map (also in legacy 'x' version)
336 LDAP_REWRITE_F (struct rewrite_map *)
338 struct rewrite_info *info,
343 LDAP_REWRITE_F (struct rewrite_map *)
345 struct rewrite_info *info,
351 * Resolves key in val by means of map (also in legacy 'x' version)
355 struct rewrite_info *info,
356 struct rewrite_op *op,
357 struct rewrite_map *map,
364 struct rewrite_info *info,
365 struct rewrite_op *op,
366 struct rewrite_map *map,
373 struct rewrite_map **map
377 rewrite_xmap_destroy(
378 struct rewrite_map **map
381 LDAP_REWRITE_F (void)
382 rewrite_builtin_map_free(
386 * Submatch substitution
390 * Compiles a substitution pattern
392 LDAP_REWRITE_F (struct rewrite_subst *)
393 rewrite_subst_compile(
394 struct rewrite_info *info,
399 * Substitutes a portion of rewritten string according to substitution
400 * pattern using submatches
404 struct rewrite_info *info,
405 struct rewrite_op *op,
406 struct rewrite_subst *subst,
408 const regmatch_t *match,
413 rewrite_subst_destroy(
414 struct rewrite_subst **subst
423 * Compiles the rule and appends it at the running context
426 rewrite_rule_compile(
427 struct rewrite_info *info,
428 struct rewrite_context *context,
431 const char *flagstring
435 * Rewrites string according to rule; may return:
436 * REWRITE_REGEXEC_OK: fine; if *result != NULL rule matched
437 * and rewrite succeeded.
438 * REWRITE_REGEXEC_STOP: fine, rule matched; stop processing
440 * REWRITE_REGEXEC_UNWILL: rule matched; force 'unwilling to perform'
441 * REWRITE_REGEXEC_ERR: an error occurred
445 struct rewrite_info *info,
446 struct rewrite_op *op,
447 struct rewrite_rule *rule,
453 rewrite_rule_destroy(
454 struct rewrite_rule **rule
462 * Fetches a struct rewrite_session
464 LDAP_REWRITE_F (struct rewrite_session *)
465 rewrite_session_find(
466 struct rewrite_info *info,
471 * Defines and inits a variable with session scope
474 rewrite_session_var_set_f(
475 struct rewrite_info *info,
483 * Gets a var with session scope
486 rewrite_session_var_get(
487 struct rewrite_info *info,
497 rewrite_session_delete(
498 struct rewrite_info *info,
503 * Destroys the cookie tree
506 rewrite_session_destroy(
507 struct rewrite_info *info
518 LDAP_REWRITE_F (struct rewrite_var *)
525 * Replaces the value of a variable
529 struct rewrite_var *var,
535 * Inserts a newly created var
537 LDAP_REWRITE_F (struct rewrite_var *)
538 rewrite_var_insert_f(
545 #define rewrite_var_insert(tree, name, value) \
546 rewrite_var_insert_f((tree), (name), (value), \
547 REWRITE_VAR_UPDATE|REWRITE_VAR_COPY_NAME|REWRITE_VAR_COPY_VALUE)
552 LDAP_REWRITE_F (struct rewrite_var *)
560 #define rewrite_var_set(tree, name, value, insert) \
561 rewrite_var_set_f((tree), (name), (value), \
562 REWRITE_VAR_UPDATE|REWRITE_VAR_COPY_NAME|REWRITE_VAR_COPY_VALUE|((insert)? REWRITE_VAR_INSERT : 0))
578 * Finds the context named rewriteContext in the context tree
580 LDAP_REWRITE_F (struct rewrite_context *)
581 rewrite_context_find(
582 struct rewrite_info *info,
583 const char *rewriteContext
587 * Creates a new context called rewriteContext and stores in into the tree
589 LDAP_REWRITE_F (struct rewrite_context *)
590 rewrite_context_create(
591 struct rewrite_info *info,
592 const char *rewriteContext
596 * Rewrites string according to context; may return:
597 * OK: fine; if *result != NULL rule matched and rewrite succeeded.
598 * STOP: fine, rule matched; stop processing following rules
599 * UNWILL: rule matched; force 'unwilling to perform'
602 rewrite_context_apply(
603 struct rewrite_info *info,
604 struct rewrite_op *op,
605 struct rewrite_context *context,
611 rewrite_context_destroy(
612 struct rewrite_context **context
615 LDAP_REWRITE_F (void)
616 rewrite_context_free(
620 #endif /* REWRITE_INT_H */