]> git.sur5r.net Git - openldap/blob - libraries/librewrite/rule.c
Happy new year
[openldap] / libraries / librewrite / rule.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2000-2004 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 #include <portable.h>
21
22 #include "rewrite-int.h"
23
24 /*
25  * Appends a rule to the double linked list of rules
26  * Helper for rewrite_rule_compile
27  */
28 static int
29 append_rule(
30                 struct rewrite_context *context,
31                 struct rewrite_rule *rule
32 )
33 {
34         struct rewrite_rule *r;
35
36         assert( context != NULL );
37         assert( context->lc_rule != NULL );
38         assert( rule != NULL );
39
40         for ( r = context->lc_rule; r->lr_next != NULL; r = r->lr_next );
41         r->lr_next = rule;
42         rule->lr_prev = r;
43         
44         return REWRITE_SUCCESS;
45 }
46
47 /*
48  * Appends an action to the linked list of actions
49  * Helper for rewrite_rule_compile
50  */
51 static int
52 append_action(
53                 struct rewrite_action **pbase,
54                 struct rewrite_action *action
55 )
56 {
57         struct rewrite_action **pa;
58
59         assert( pbase != NULL );
60         assert( action != NULL );
61         
62         for ( pa = pbase; *pa != NULL; pa = &(*pa)->la_next );
63         *pa = action;
64         
65         return REWRITE_SUCCESS;
66 }
67
68 static int
69 destroy_action(
70                 struct rewrite_action **paction
71 )
72 {
73         struct rewrite_action   *action;
74
75         assert( paction );
76         assert( *paction );
77
78         action = *paction;
79
80         /* do something */
81         switch ( action->la_type ) {
82         case REWRITE_FLAG_GOTO:
83         case REWRITE_FLAG_USER: {
84                 int *pi = (int *)action->la_args;
85
86                 if ( pi ) {
87                         free( pi );
88                 }
89                 break;
90         }
91
92         default:
93                 break;
94         }
95         
96         free( action );
97         *paction = NULL;
98         
99         return 0;
100 }
101
102 /*
103  * In case of error it returns NULL and does not free all the memory
104  * it allocated; as this is a once only phase, and an error at this stage
105  * would require the server to stop, there is no need to be paranoid
106  * about memory allocation
107  */
108 int
109 rewrite_rule_compile(
110                 struct rewrite_info *info,
111                 struct rewrite_context *context,
112                 const char *pattern,
113                 const char *result,
114                 const char *flagstring
115 )
116 {
117         int flags = REWRITE_REGEX_EXTENDED | REWRITE_REGEX_ICASE;
118         int mode = REWRITE_RECURSE;
119         int max_passes = info->li_max_passes_per_rule;
120
121         struct rewrite_rule *rule = NULL;
122         struct rewrite_subst *subst = NULL;
123         struct rewrite_action *action = NULL, *first_action = NULL;
124
125         const char *p;
126
127         assert( info != NULL );
128         assert( context != NULL );
129         assert( pattern != NULL );
130         assert( result != NULL );
131
132         /*
133          * A null flagstring should be allowed
134          */
135
136         /*
137          * Take care of substitution string
138          */
139         subst = rewrite_subst_compile( info, result );
140         if ( subst == NULL ) {
141                 return REWRITE_ERR;
142         }
143
144         /*
145          * Take care of flags
146          */
147         for ( p = flagstring; p[ 0 ] != '\0'; p++ ) {
148                 switch( p[ 0 ] ) {
149                         
150                 /*
151                  * REGEX flags
152                  */
153                 case REWRITE_FLAG_HONORCASE:            /* 'C' */
154                         /*
155                          * Honor case (default is case insensitive)
156                          */
157                         flags &= ~REWRITE_REGEX_ICASE;
158                         break;
159                         
160                 case REWRITE_FLAG_BASICREGEX:           /* 'R' */
161                         /*
162                          * Use POSIX Basic Regular Expression syntax
163                          * instead of POSIX Extended Regular Expression 
164                          * syntax (default)
165                          */
166                         flags &= ~REWRITE_REGEX_EXTENDED;
167                         break;
168                         
169                 /*
170                  * Execution mode flags
171                  */
172                 case REWRITE_FLAG_EXECONCE:             /* ':' */
173                         /*
174                          * Apply rule once only
175                          */
176                         mode &= ~REWRITE_RECURSE;
177                         mode |= REWRITE_EXEC_ONCE;
178                         break;
179                 
180                 /*
181                  * Special action flags
182                  */
183                 case REWRITE_FLAG_STOP:                 /* '@' */
184                         /*
185                          * Bail out after applying rule
186                          */
187                         action = calloc( sizeof( struct rewrite_action ), 1 );
188                         if ( action == NULL ) {
189                                 /* cleanup ... */
190                                 return REWRITE_ERR;
191                         }
192                         
193                         //mode &= ~REWRITE_RECURSE;
194                         //mode |= REWRITE_EXEC_ONCE;
195                         action->la_type = REWRITE_ACTION_STOP;
196                         break;
197                         
198                 case REWRITE_FLAG_UNWILLING:            /* '#' */
199                         /*
200                          * Matching objs will be marked as gone!
201                          */
202                         action = calloc( sizeof( struct rewrite_action ), 1 );
203                         if ( action == NULL ) {
204                                 /* cleanup ... */
205                                 return REWRITE_ERR;
206                         }
207                         
208                         mode &= ~REWRITE_RECURSE;
209                         mode |= REWRITE_EXEC_ONCE;
210                         action->la_type = REWRITE_ACTION_UNWILLING;
211                         break;
212
213                 case REWRITE_FLAG_GOTO:                         /* 'G' */
214                         /*
215                          * After applying rule, jump N rules
216                          */
217
218                 case REWRITE_FLAG_USER: {                       /* 'U' */
219                         /*
220                          * After applying rule, return user-defined
221                          * error code
222                          */
223                         char *next = NULL;
224                         int *d;
225                         
226                         if ( p[ 1 ] != '{' ) {
227                                 /* XXX Need to free stuff */
228                                 return REWRITE_ERR;
229                         }
230
231                         d = malloc( sizeof( int ) );
232                         if ( d == NULL ) {
233                                 /* XXX Need to free stuff */
234                                 return REWRITE_ERR;
235                         }
236
237                         d[ 0 ] = strtol( &p[ 2 ], &next, 0 );
238                         if ( next == NULL || next == &p[ 2 ] || next[0] != '}' ) {
239                                 /* XXX Need to free stuff */
240                                 return REWRITE_ERR;
241                         }
242
243                         action = calloc( sizeof( struct rewrite_action ), 1 );
244                         if ( action == NULL ) {
245                                 /* cleanup ... */       
246                                 return REWRITE_ERR;
247                         }
248                         switch ( p[ 0 ] ) {
249                         case REWRITE_FLAG_GOTO:
250                                 action->la_type = REWRITE_ACTION_GOTO;
251                                 break;
252
253                         case REWRITE_FLAG_USER:
254                                 action->la_type = REWRITE_ACTION_USER;
255                                 break;
256
257                         default:
258                                 assert(0);
259                         }
260
261                         action->la_args = (void *)d;
262
263                         p = next;       /* p is incremented by the for ... */
264                 
265                         break;
266                 }
267
268                 case REWRITE_FLAG_MAX_PASSES: {                 /* 'U' */
269                         /*
270                          * Set the number of max passes per rule
271                          */
272                         char *next = NULL;
273                         
274                         if ( p[ 1 ] != '{' ) {
275                                 /* XXX Need to free stuff */
276                                 return REWRITE_ERR;
277                         }
278
279                         max_passes = strtol( &p[ 2 ], &next, 0 );
280                         if ( next == NULL || next == &p[ 2 ] || next[0] != '}' ) {
281                                 /* XXX Need to free stuff */
282                                 return REWRITE_ERR;
283                         }
284
285                         if ( max_passes < 1 ) {
286                                 /* FIXME: nonsense ... */
287                                 max_passes = 1;
288                         }
289
290                         p = next;       /* p is incremented by the for ... */
291                 
292                         break;
293                 }
294
295                 case REWRITE_FLAG_IGNORE_ERR:               /* 'I' */
296                         /*
297                          * Ignore errors!
298                          */
299                         action = calloc( sizeof( struct rewrite_action ), 1 );
300                         if ( action == NULL ) {
301                                 /* cleanup ... */
302                                 return REWRITE_ERR;
303                         }
304                         
305                         action->la_type = REWRITE_ACTION_IGNORE_ERR;
306                         break;
307                         
308                 /*
309                  * Other flags ...
310                  */
311                 default:
312                         /*
313                          * Unimplemented feature (complain only)
314                          */
315                         break;
316                 }
317                 
318                 /*
319                  * Stupid way to append to a list ...
320                  */
321                 if ( action != NULL ) {
322                         append_action( &first_action, action );
323                         action = NULL;
324                 }
325         }
326         
327         /*
328          * Finally, rule allocation
329          */
330         rule = calloc( sizeof( struct rewrite_rule ), 1 );
331         if ( rule == NULL ) {
332                 /* charray_free( res ); */
333                 /*
334                  * XXX need to free the value subst stuff!
335                  */
336                 return REWRITE_ERR;
337         }
338         
339         /*
340          * REGEX compilation (luckily I don't need to take care of this ...)
341          */
342         if ( regcomp( &rule->lr_regex, ( char * )pattern, flags ) != 0 ) {
343                 /* charray_free( res ); */
344                 /*
345                  *XXX need to free the value subst stuff!
346                  */
347                 free( rule );
348                 return REWRITE_ERR;
349         }
350         
351         /*
352          * Just to remember them ...
353          */
354         rule->lr_pattern = strdup( pattern );
355         rule->lr_subststring = strdup( result );
356         rule->lr_flagstring = strdup( flagstring );
357         
358         /*
359          * Load compiled data into rule
360          */
361         rule->lr_subst = subst;
362
363         /*
364          * Set various parameters
365          */
366         rule->lr_flags = flags;         /* don't really need any longer ... */
367         rule->lr_mode = mode;
368         rule->lr_max_passes = max_passes;
369         rule->lr_action = first_action;
370         
371         /*
372          * Append rule at the end of the rewrite context
373          */
374         append_rule( context, rule );
375
376         return REWRITE_SUCCESS;
377 }
378
379 /*
380  * Rewrites string according to rule; may return:
381  *      OK:     fine; if *result != NULL rule matched and rewrite succeeded.
382  *      STOP:   fine, rule matched; stop processing following rules
383  *      UNWILL: rule matched; force 'unwilling to perform'
384  */
385 int
386 rewrite_rule_apply(
387                 struct rewrite_info *info,
388                 struct rewrite_op *op,
389                 struct rewrite_rule *rule,
390                 const char *arg,
391                 char **result
392                 )
393 {
394         size_t nmatch = REWRITE_MAX_MATCH;
395         regmatch_t match[ REWRITE_MAX_MATCH ];
396
397         int rc = REWRITE_SUCCESS;
398
399         char *string;
400         int strcnt = 0;
401         struct berval val = { 0, NULL };
402
403         assert( info != NULL );
404         assert( op != NULL );
405         assert( rule != NULL );
406         assert( arg != NULL );
407         assert( result != NULL );
408
409         *result = NULL;
410
411         string = (char *)arg;
412         
413         /*
414          * In case recursive match is required (default)
415          */
416 recurse:;
417
418         Debug( LDAP_DEBUG_TRACE, "==> rewrite_rule_apply"
419                         " rule='%s' string='%s' [%s pass(es)]\n", 
420                         rule->lr_pattern, string, strcnt + 1 );
421         
422         op->lo_num_passes++;
423         if ( regexec( &rule->lr_regex, string, nmatch, match, 0 ) != 0 ) {
424                 if ( *result == NULL && strcnt > 0 ) {
425                         free( string );
426                         string = NULL;
427                 }
428
429                 /*
430                  * No match is OK; *result = NULL means no match
431                  */
432                 return REWRITE_REGEXEC_OK;
433         }
434
435         rc = rewrite_subst_apply( info, op, rule->lr_subst, string,
436                         match, &val );
437
438         *result = val.bv_val;
439         val.bv_val = NULL;
440         if ( strcnt > 0 ) {
441                 free( string );
442                 string = NULL;
443         }
444
445         if ( rc != REWRITE_REGEXEC_OK ) {
446                 return rc;
447         }
448
449         if ( ( rule->lr_mode & REWRITE_RECURSE ) == REWRITE_RECURSE 
450                         && op->lo_num_passes < info->li_max_passes
451                         && ++strcnt < rule->lr_max_passes ) {
452                 string = *result;
453
454                 goto recurse;
455         }
456
457         return REWRITE_REGEXEC_OK;
458 }
459
460 int
461 rewrite_rule_destroy(
462                 struct rewrite_rule **prule
463                 )
464 {
465         struct rewrite_rule *rule;
466         struct rewrite_action *action;
467
468         assert( prule );
469         assert( *prule );
470
471         rule = *prule;
472
473         if ( rule->lr_pattern ) {
474                 free( rule->lr_pattern );
475                 rule->lr_pattern = NULL;
476         }
477
478         if ( rule->lr_subststring ) {
479                 free( rule->lr_subststring );
480                 rule->lr_subststring = NULL;
481         }
482
483         if ( rule->lr_flagstring ) {
484                 free( rule->lr_flagstring );
485                 rule->lr_flagstring = NULL;
486         }
487
488         if ( rule->lr_subst ) {
489                 rewrite_subst_destroy( &rule->lr_subst );
490         }
491
492         regfree( &rule->lr_regex );
493
494         for ( action = rule->lr_action; action; ) {
495                 struct rewrite_action *curraction = action;
496
497                 action = action->la_next;
498                 destroy_action( &curraction );
499         }
500
501         free( rule );
502         *prule = NULL;
503
504         return 0;
505 }
506