2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2003 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 '%'
64 #define REWRITE_FLAG_HONORCASE 'C'
65 #define REWRITE_FLAG_BASICREGEX 'R'
70 #define REWRITE_FLAG_EXECONCE ':'
71 #define REWRITE_FLAG_STOP '@'
72 #define REWRITE_FLAG_UNWILLING '#'
73 #define REWRITE_FLAG_GOTO 'G' /* requires an arg */
74 #define REWRITE_FLAG_USER 'U' /* requires an arg */
75 #define REWRITE_FLAG_IGNORE_ERR 'I'
80 #define REWRITE_OPERATOR_SUBCONTEXT '>'
81 #define REWRITE_OPERATOR_COMMAND '|'
82 #define REWRITE_OPERATOR_VARIABLE_SET '&'
83 #define REWRITE_OPERATOR_VARIABLE_GET '*'
84 #define REWRITE_OPERATOR_PARAM_GET '$'
94 struct rewrite_action {
95 struct rewrite_action *la_next;
97 #define REWRITE_ACTION_STOP 0x0001
98 #define REWRITE_ACTION_UNWILLING 0x0002
99 #define REWRITE_ACTION_GOTO 0x0003
100 #define REWRITE_ACTION_IGNORE_ERR 0x0004
101 #define REWRITE_ACTION_USER 0x0005
114 #define REWRITE_MAP_XFILEMAP 0x0001 /* Rough implementation! */
115 #define REWRITE_MAP_XPWDMAP 0x0002 /* uid -> gecos */
116 #define REWRITE_MAP_XLDAPMAP 0x0003 /* Not implemented yet! */
121 #define REWRITE_MAP_SUBCONTEXT 0x0101
123 #define REWRITE_MAP_SET_OP_VAR 0x0102
124 #define REWRITE_MAP_SETW_OP_VAR 0x0103
125 #define REWRITE_MAP_GET_OP_VAR 0x0104
126 #define REWRITE_MAP_SET_SESN_VAR 0x0105
127 #define REWRITE_MAP_SETW_SESN_VAR 0x0106
128 #define REWRITE_MAP_GET_SESN_VAR 0x0107
129 #define REWRITE_MAP_GET_PARAM 0x0108
130 #define REWRITE_MAP_BUILTIN 0x0109
137 * Old maps store private data in _lm_args;
138 * new maps store the substitution pattern in _lm_subst
142 struct rewrite_subst *_lm_subst;
144 #define lm_args lm_union._lm_args
145 #define lm_subst lm_union._lm_subst
147 #ifdef USE_REWRITE_LDAP_PVT_THREADS
148 ldap_pvt_thread_mutex_t lm_mutex;
149 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
155 struct rewrite_builtin_map {
156 #define REWRITE_BUILTIN_MAP_LDAP 0x0201
161 #ifdef USE_REWRITE_LDAP_PVT_THREADS
162 ldap_pvt_thread_mutex_t lb_mutex;
163 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
167 * Submatch substitution
169 struct rewrite_submatch {
170 #define REWRITE_SUBMATCH_ASIS 0x0000
171 #define REWRITE_SUBMATCH_XMAP 0x0001
172 #define REWRITE_SUBMATCH_MAP_W_ARG 0x0002
174 struct rewrite_map *ls_map;
177 * The first one represents the index of the submatch in case
178 * the map has single submatch as argument;
179 * the latter represents the map argument scheme in case
180 * the map has substitution string argument form
185 * Pattern substitution
187 struct rewrite_subst {
189 struct berval *lt_subs;
192 struct rewrite_submatch *lt_submatch;
198 struct rewrite_rule {
199 struct rewrite_rule *lr_next;
200 struct rewrite_rule *lr_prev;
203 char *lr_subststring;
208 * I was thinking about some kind of per-rule mutex, but there's
209 * probably no need, because rules after compilation are only read;
210 * however, I need to check whether regexec is reentrant ...
213 struct rewrite_subst *lr_subst;
215 #define REWRITE_REGEX_ICASE REG_ICASE
216 #define REWRITE_REGEX_EXTENDED REG_EXTENDED
219 #define REWRITE_RECURSE 0x0001
220 #define REWRITE_EXEC_ONCE 0x0002
223 struct rewrite_action *lr_action;
227 * Rewrite Context (set of rules)
229 struct rewrite_context {
231 struct rewrite_context *lc_alias;
232 struct rewrite_rule *lc_rule;
238 struct rewrite_session {
241 #ifdef USE_REWRITE_LDAP_PVT_THREADS
242 ldap_pvt_thread_rdwr_t ls_vars_mutex;
243 ldap_pvt_thread_mutex_t ls_mutex;
244 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
254 struct berval lv_value;
263 #if 0 /* FIXME: not used anywhere! (debug? then, why strdup?) */
268 const void *lo_cookie;
279 struct rewrite_info {
283 * No global mutex because maps are read only at
290 #ifdef USE_REWRITE_LDAP_PVT_THREADS
291 ldap_pvt_thread_rdwr_t li_params_mutex;
292 ldap_pvt_thread_rdwr_t li_cookies_mutex;
293 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
297 * use `rewriteEngine {on|off}' directive to alter
302 * Defaults to REWRITE_MAXPASSES;
303 * use `rewriteMaxPasses numPasses' directive to alter
305 #define REWRITE_MAXPASSES 100
309 * Behavior in case a NULL or non-existent context is required
318 LDAP_REWRITE_V (struct rewrite_context*) rewrite_int_curr_context;
325 * Parses a map (also in legacy 'x' version)
327 LDAP_REWRITE_F (struct rewrite_map *)
329 struct rewrite_info *info,
334 LDAP_REWRITE_F (struct rewrite_map *)
336 struct rewrite_info *info,
342 * Resolves key in val by means of map (also in legacy 'x' version)
346 struct rewrite_info *info,
347 struct rewrite_op *op,
348 struct rewrite_map *map,
355 struct rewrite_info *info,
356 struct rewrite_op *op,
357 struct rewrite_map *map,
364 struct rewrite_map **map
368 rewrite_xmap_destroy(
369 struct rewrite_map **map
372 LDAP_REWRITE_F (void)
373 rewrite_builtin_map_free(
377 * Submatch substitution
381 * Compiles a substitution pattern
383 LDAP_REWRITE_F (struct rewrite_subst *)
384 rewrite_subst_compile(
385 struct rewrite_info *info,
390 * Substitutes a portion of rewritten string according to substitution
391 * pattern using submatches
395 struct rewrite_info *info,
396 struct rewrite_op *op,
397 struct rewrite_subst *subst,
399 const regmatch_t *match,
404 rewrite_subst_destroy(
405 struct rewrite_subst **subst
414 * Compiles the rule and appends it at the running context
417 rewrite_rule_compile(
418 struct rewrite_info *info,
419 struct rewrite_context *context,
422 const char *flagstring
426 * Rewrites string according to rule; may return:
427 * REWRITE_REGEXEC_OK: fine; if *result != NULL rule matched
428 * and rewrite succeeded.
429 * REWRITE_REGEXEC_STOP: fine, rule matched; stop processing
431 * REWRITE_REGEXEC_UNWILL: rule matched; force 'unwilling to perform'
432 * REWRITE_REGEXEC_ERR: an error occurred
436 struct rewrite_info *info,
437 struct rewrite_op *op,
438 struct rewrite_rule *rule,
444 rewrite_rule_destroy(
445 struct rewrite_rule **rule
453 * Fetches a struct rewrite_session
455 LDAP_REWRITE_F (struct rewrite_session *)
456 rewrite_session_find(
457 struct rewrite_info *info,
462 * Defines and inits a variable with session scope
465 rewrite_session_var_set_f(
466 struct rewrite_info *info,
474 * Gets a var with session scope
477 rewrite_session_var_get(
478 struct rewrite_info *info,
488 rewrite_session_delete(
489 struct rewrite_info *info,
494 * Destroys the cookie tree
497 rewrite_session_destroy(
498 struct rewrite_info *info
509 LDAP_REWRITE_F (struct rewrite_var *)
516 * Replaces the value of a variable
520 struct rewrite_var *var,
526 * Inserts a newly created var
528 LDAP_REWRITE_F (struct rewrite_var *)
529 rewrite_var_insert_f(
536 #define rewrite_var_insert(tree, name, value) \
537 rewrite_var_insert_f((tree), (name), (value), \
538 REWRITE_VAR_UPDATE|REWRITE_VAR_COPY_NAME|REWRITE_VAR_COPY_VALUE)
543 LDAP_REWRITE_F (struct rewrite_var *)
551 #define rewrite_var_set(tree, name, value, insert) \
552 rewrite_var_set_f((tree), (name), (value), \
553 REWRITE_VAR_UPDATE|REWRITE_VAR_COPY_NAME|REWRITE_VAR_COPY_VALUE|((insert)? REWRITE_VAR_INSERT : 0))
569 * Finds the context named rewriteContext in the context tree
571 LDAP_REWRITE_F (struct rewrite_context *)
572 rewrite_context_find(
573 struct rewrite_info *info,
574 const char *rewriteContext
578 * Creates a new context called rewriteContext and stores in into the tree
580 LDAP_REWRITE_F (struct rewrite_context *)
581 rewrite_context_create(
582 struct rewrite_info *info,
583 const char *rewriteContext
587 * Rewrites string according to context; may return:
588 * OK: fine; if *result != NULL rule matched and rewrite succeeded.
589 * STOP: fine, rule matched; stop processing following rules
590 * UNWILL: rule matched; force 'unwilling to perform'
593 rewrite_context_apply(
594 struct rewrite_info *info,
595 struct rewrite_op *op,
596 struct rewrite_context *context,
602 rewrite_context_destroy(
603 struct rewrite_context **context
606 LDAP_REWRITE_F (void)
607 rewrite_context_free(
611 #endif /* REWRITE_INT_H */