]> git.sur5r.net Git - openldap/blob - libraries/librewrite/rewrite-int.h
3f14b0c16030f63b3f8c3fbe582089708f998212
[openldap] / libraries / librewrite / rewrite-int.h
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2000-2003 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15 /* ACKNOWLEDGEMENT:
16  * This work was initially developed by Pierangelo Masarati for
17  * inclusion in OpenLDAP Software.
18  */
19
20 #ifndef REWRITE_INT_H
21 #define REWRITE_INT_H
22
23 /*
24  * These are required by every file of the library, so they're included here
25  */
26 #include <ac/stdlib.h>
27 #include <ac/string.h>
28 #include <ac/syslog.h>
29 #include <ac/regex.h>
30 #include <ac/socket.h>
31 #include <ac/unistd.h>
32 #include <ac/ctype.h>
33
34 #include <lber.h>
35 #include <ldap.h>
36 #include "../libldap/ldap-int.h"
37
38 #include <avl.h>
39
40 #include <rewrite.h>
41
42 /* Uncomment to use ldap pvt threads */
43 #define USE_REWRITE_LDAP_PVT_THREADS
44 #include <ldap_pvt_thread.h>
45
46 /*
47  * For details, see RATIONALE.
48  */
49
50 #define REWRITE_MAX_MATCH       11      /* 0: overall string; 1-9: submatches */
51 #define REWRITE_MAX_PASSES      100
52
53 /*
54  * Submatch escape char
55  */
56 /* the '\' conflicts with slapd.conf parsing */
57 /* #define REWRITE_SUBMATCH_ESCAPE                      '\\' */
58 #define REWRITE_SUBMATCH_ESCAPE                 '%'
59
60 /*
61  * REGEX flags
62  */
63
64 #define REWRITE_FLAG_HONORCASE                  'C'
65 #define REWRITE_FLAG_BASICREGEX                 'R'
66
67 /*
68  * Action flags
69  */
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_IGNORE_ERR                 'I'
75
76 /*
77  * Map operators
78  */
79 #define REWRITE_OPERATOR_SUBCONTEXT             '>'
80 #define REWRITE_OPERATOR_COMMAND                '|'
81 #define REWRITE_OPERATOR_VARIABLE_SET           '&'
82 #define REWRITE_OPERATOR_VARIABLE_GET           '*'
83 #define REWRITE_OPERATOR_PARAM_GET              '$'
84
85
86 /***********
87  * PRIVATE *
88  ***********/
89
90 /*
91  * Action
92  */
93 struct rewrite_action {
94         struct rewrite_action          *la_next;
95         
96 #define REWRITE_ACTION_STOP             0x0001
97 #define REWRITE_ACTION_UNWILLING        0x0002
98 #define REWRITE_ACTION_GOTO             0x0003
99 #define REWRITE_ACTION_IGNORE_ERR       0x0004
100         int                             la_type;
101         void                           *la_args;
102 };
103
104 /*
105  * Map
106  */
107 struct rewrite_map {
108
109         /*
110          * Legacy stuff
111          */
112 #define REWRITE_MAP_XFILEMAP            0x0001  /* Rough implementation! */
113 #define REWRITE_MAP_XPWDMAP             0x0002  /* uid -> gecos */
114 #define REWRITE_MAP_XLDAPMAP            0x0003  /* Not implemented yet! */
115
116         /*
117          * Maps with args
118          */
119 #define REWRITE_MAP_SUBCONTEXT          0x0101
120         
121 #define REWRITE_MAP_SET_OP_VAR          0x0102
122 #define REWRITE_MAP_SETW_OP_VAR         0x0103
123 #define REWRITE_MAP_GET_OP_VAR          0x0104
124 #define REWRITE_MAP_SET_SESN_VAR        0x0105
125 #define REWRITE_MAP_SETW_SESN_VAR       0x0106
126 #define REWRITE_MAP_GET_SESN_VAR        0x0107
127 #define REWRITE_MAP_GET_PARAM           0x0108
128 #define REWRITE_MAP_BUILTIN             0x0109
129         int                             lm_type;
130
131         char                           *lm_name;
132         void                           *lm_data;
133
134         /*
135          * Old maps store private data in _lm_args;
136          * new maps store the substitution pattern in _lm_subst
137          */
138         union {         
139                 void                   *_lm_args;
140                 struct rewrite_subst   *_lm_subst;
141         } lm_union;
142 #define lm_args lm_union._lm_args
143 #define lm_subst lm_union._lm_subst
144
145 #ifdef USE_REWRITE_LDAP_PVT_THREADS
146         ldap_pvt_thread_mutex_t         lm_mutex;
147 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
148 };
149
150 /*
151  * Builtin maps
152  */
153 struct rewrite_builtin_map {
154 #define REWRITE_BUILTIN_MAP_LDAP        0x0201
155         int                             lb_type;
156         char                           *lb_name;
157         void                           *lb_private;
158
159 #ifdef USE_REWRITE_LDAP_PVT_THREADS
160         ldap_pvt_thread_mutex_t         lb_mutex;
161 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
162 };
163
164 /*
165  * Submatch substitution
166  */
167 struct rewrite_submatch {
168 #define REWRITE_SUBMATCH_ASIS           0x0000
169 #define REWRITE_SUBMATCH_XMAP           0x0001
170 #define REWRITE_SUBMATCH_MAP_W_ARG      0x0002
171         int                             ls_type;
172         struct rewrite_map             *ls_map;
173         int                             ls_submatch;
174         /*
175          * The first one represents the index of the submatch in case
176          * the map has single submatch as argument;
177          * the latter represents the map argument scheme in case
178          * the map has substitution string argument form
179          */
180 };
181
182 /*
183  * Pattern substitution
184  */
185 struct rewrite_subst {
186         size_t                          lt_subs_len;
187         struct berval                  *lt_subs;
188         
189         int                             lt_num_submatch;
190         struct rewrite_submatch        *lt_submatch;
191 };
192
193 /*
194  * Rule
195  */
196 struct rewrite_rule {
197         struct rewrite_rule            *lr_next;
198         struct rewrite_rule            *lr_prev;
199
200         char                           *lr_pattern;
201         char                           *lr_subststring;
202         char                           *lr_flagstring;
203         regex_t                         lr_regex;
204
205         /*
206          * I was thinking about some kind of per-rule mutex, but there's
207          * probably no need, because rules after compilation are only read;
208          * however, I need to check whether regexec is reentrant ...
209          */
210
211         struct rewrite_subst           *lr_subst;
212         
213 #define REWRITE_REGEX_ICASE             REG_ICASE
214 #define REWRITE_REGEX_EXTENDED          REG_EXTENDED    
215         int                             lr_flags;
216
217 #define REWRITE_RECURSE                 0x0001
218 #define REWRITE_EXEC_ONCE               0x0002
219         int                             lr_mode;
220
221         struct rewrite_action          *lr_action;
222 };
223
224 /*
225  * Rewrite Context (set of rules)
226  */
227 struct rewrite_context {
228         char                           *lc_name;
229         struct rewrite_context         *lc_alias;
230         struct rewrite_rule            *lc_rule;
231 };
232
233 /*
234  * Session
235  */
236 struct rewrite_session {
237         void                           *ls_cookie;
238         Avlnode                        *ls_vars;
239 #ifdef USE_REWRITE_LDAP_PVT_THREADS
240         ldap_pvt_thread_rdwr_t          ls_vars_mutex;
241         ldap_pvt_thread_mutex_t         ls_mutex;
242 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
243         int                             ls_count;
244 };
245
246 /*
247  * Variable
248  */
249 struct rewrite_var {
250         char                           *lv_name;
251         struct berval                   lv_value;
252 };
253
254 /*
255  * Operation
256  */
257 struct rewrite_op {
258         int                             lo_num_passes;
259         int                             lo_depth;
260 #if 0 /* FIXME: not used anywhere! (debug? then, why strdup?) */
261         char                           *lo_string;
262 #endif
263         char                           *lo_result;
264         Avlnode                        *lo_vars;
265         const void                     *lo_cookie;
266 };
267
268
269 /**********
270  * PUBLIC *
271  **********/
272
273 /*
274  * Rewrite info
275  */
276 struct rewrite_info {
277         Avlnode                        *li_context;
278         Avlnode                        *li_maps;
279         /*
280          * No global mutex because maps are read only at 
281          * config time
282          */
283         Avlnode                        *li_params;
284         Avlnode                        *li_cookies;
285         int                             li_num_cookies;
286
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 */
291
292         /*
293          * Default to `off';
294          * use `rewriteEngine {on|off}' directive to alter
295          */
296         int                             li_state;
297
298         /*
299          * Defaults to REWRITE_MAXPASSES;
300          * use `rewriteMaxPasses numPasses' directive to alter
301          */
302 #define REWRITE_MAXPASSES               100
303         int                             li_max_passes;
304
305         /*
306          * Behavior in case a NULL or non-existent context is required
307          */
308         int                             li_rewrite_mode;
309 };
310
311 /***********
312  * PRIVATE *
313  ***********/
314
315 LDAP_REWRITE_V (struct rewrite_context*) rewrite_int_curr_context;
316
317 /*
318  * Maps
319  */
320
321 /*
322  * Parses a map (also in legacy 'x' version)
323  */
324 LDAP_REWRITE_F (struct rewrite_map *)
325 rewrite_map_parse(
326                 struct rewrite_info *info,
327                 const char *s,
328                 const char **end
329 );
330
331 LDAP_REWRITE_F (struct rewrite_map *)
332 rewrite_xmap_parse(
333                 struct rewrite_info *info,
334                 const char *s,
335                 const char **end
336 );
337
338 /*
339  * Resolves key in val by means of map (also in legacy 'x' version)
340  */
341 LDAP_REWRITE_F (int)
342 rewrite_map_apply(
343                 struct rewrite_info *info,
344                 struct rewrite_op *op,
345                 struct rewrite_map *map,
346                 struct berval *key,
347                 struct berval *val
348 );
349
350 LDAP_REWRITE_F (int)
351 rewrite_xmap_apply(
352                 struct rewrite_info *info,
353                 struct rewrite_op *op,
354                 struct rewrite_map *map,
355                 struct berval *key,
356                 struct berval *val
357 );
358
359 LDAP_REWRITE_F (int)
360 rewrite_map_destroy(
361                 struct rewrite_map **map
362 );
363
364 LDAP_REWRITE_F (int)
365 rewrite_xmap_destroy(
366                 struct rewrite_map **map
367 );
368
369 LDAP_REWRITE_F (void)
370 rewrite_builtin_map_free(
371                 void *map
372 );
373 /*
374  * Submatch substitution
375  */
376
377 /*
378  * Compiles a substitution pattern
379  */
380 LDAP_REWRITE_F (struct rewrite_subst *)
381 rewrite_subst_compile(
382                 struct rewrite_info *info,
383                 const char *result
384 );
385
386 /*
387  * Substitutes a portion of rewritten string according to substitution
388  * pattern using submatches
389  */
390 LDAP_REWRITE_F (int)
391 rewrite_subst_apply(
392                 struct rewrite_info *info,
393                 struct rewrite_op *op,
394                 struct rewrite_subst *subst,
395                 const char *string,
396                 const regmatch_t *match,
397                 struct berval *val
398 );
399
400 LDAP_REWRITE_F (int)
401 rewrite_subst_destroy(
402                 struct rewrite_subst **subst
403 );
404
405
406 /*
407  * Rules
408  */
409
410 /*
411  * Compiles the rule and appends it at the running context
412  */
413 LDAP_REWRITE_F (int)
414 rewrite_rule_compile(
415                 struct rewrite_info *info,
416                 struct rewrite_context *context,
417                 const char *pattern,
418                 const char *result,
419                 const char *flagstring
420 );
421
422 /*
423  * Rewrites string according to rule; may return:
424  *      REWRITE_REGEXEC_OK:     fine; if *result != NULL rule matched
425  *                              and rewrite succeeded.
426  *      REWRITE_REGEXEC_STOP:   fine, rule matched; stop processing
427  *                              following rules
428  *      REWRITE_REGEXEC_UNWILL: rule matched; force 'unwilling to perform'
429  *      REWRITE_REGEXEC_ERR:    an error occurred
430  */
431 LDAP_REWRITE_F (int)
432 rewrite_rule_apply(
433                 struct rewrite_info *info,
434                 struct rewrite_op *op,
435                 struct rewrite_rule *rule,
436                 const char *string,
437                 char **result
438 );
439
440 LDAP_REWRITE_F (int)
441 rewrite_rule_destroy(
442                 struct rewrite_rule **rule
443 );
444
445 /*
446  * Sessions
447  */
448
449 /*
450  * Fetches a struct rewrite_session
451  */
452 LDAP_REWRITE_F (struct rewrite_session *)
453 rewrite_session_find(
454                 struct rewrite_info *info,
455                 const void *cookie
456 );
457
458 /*
459  * Defines and inits a variable with session scope
460  */
461 LDAP_REWRITE_F (int)
462 rewrite_session_var_set(
463                 struct rewrite_info *info,
464                 const void *cookie,
465                 const char *name,
466                 const char *value
467 );
468
469 /*
470  * Gets a var with session scope
471  */
472 LDAP_REWRITE_F (int)
473 rewrite_session_var_get(
474                 struct rewrite_info *info,
475                 const void *cookie,
476                 const char *name,
477                 struct berval *val
478 );
479
480 /*
481  * Deletes a session
482  */
483 LDAP_REWRITE_F (int)
484 rewrite_session_delete(
485                 struct rewrite_info *info,
486                 const void *cookie
487 );
488
489 /*
490  * Destroys the cookie tree
491  */
492 LDAP_REWRITE_F (int)
493 rewrite_session_destroy(
494                 struct rewrite_info *info
495 );
496
497
498 /*
499  * Vars
500  */
501
502 /*
503  * Finds a var
504  */
505 LDAP_REWRITE_F (struct rewrite_var *)
506 rewrite_var_find(
507                 Avlnode *tree,
508                 const char *name
509 );
510
511 /*
512  * Inserts a newly created var
513  */
514 LDAP_REWRITE_F (struct rewrite_var *)
515 rewrite_var_insert(
516                 Avlnode **tree,
517                 const char *name,
518                 const char *value
519 );
520
521 /*
522  * Sets/inserts a var
523  */
524 LDAP_REWRITE_F (struct rewrite_var *)
525 rewrite_var_set(
526                 Avlnode **tree,
527                 const char *name,
528                 const char *value,
529                 int insert
530 );
531
532 /*
533  * Deletes a var tree
534  */
535 LDAP_REWRITE_F (int)
536 rewrite_var_delete(
537                 Avlnode *tree
538 );
539
540
541 /*
542  * Contexts
543  */
544
545 /*
546  * Finds the context named rewriteContext in the context tree
547  */
548 LDAP_REWRITE_F (struct rewrite_context *)
549 rewrite_context_find(
550                 struct rewrite_info *info,
551                 const char *rewriteContext
552 );
553
554 /*
555  * Creates a new context called rewriteContext and stores in into the tree
556  */
557 LDAP_REWRITE_F (struct rewrite_context *)
558 rewrite_context_create(
559                 struct rewrite_info *info,
560                 const char *rewriteContext
561 );
562
563 /*
564  * Rewrites string according to context; may return:
565  *      OK:     fine; if *result != NULL rule matched and rewrite succeeded.
566  *      STOP:   fine, rule matched; stop processing following rules
567  *      UNWILL: rule matched; force 'unwilling to perform'
568  */
569 LDAP_REWRITE_F (int)
570 rewrite_context_apply(
571                 struct rewrite_info *info,
572                 struct rewrite_op *op,
573                 struct rewrite_context *context,
574                 const char *string,
575                 char **result
576 );
577
578 LDAP_REWRITE_F (int)
579 rewrite_context_destroy(
580                 struct rewrite_context **context
581 );
582
583 LDAP_REWRITE_F (void)
584 rewrite_context_free(
585                 void *tmp
586 );
587
588 #endif /* REWRITE_INT_H */
589