]> git.sur5r.net Git - openldap/blob - libraries/librewrite/rule.c
format fix from HEAD
[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                         action->la_type = REWRITE_ACTION_STOP;
194                         break;
195                         
196                 case REWRITE_FLAG_UNWILLING:            /* '#' */
197                         /*
198                          * Matching objs will be marked as gone!
199                          */
200                         action = calloc( sizeof( struct rewrite_action ), 1 );
201                         if ( action == NULL ) {
202                                 /* cleanup ... */
203                                 return REWRITE_ERR;
204                         }
205                         
206                         mode &= ~REWRITE_RECURSE;
207                         mode |= REWRITE_EXEC_ONCE;
208                         action->la_type = REWRITE_ACTION_UNWILLING;
209                         break;
210
211                 case REWRITE_FLAG_GOTO:                         /* 'G' */
212                         /*
213                          * After applying rule, jump N rules
214                          */
215
216                 case REWRITE_FLAG_USER: {                       /* 'U' */
217                         /*
218                          * After applying rule, return user-defined
219                          * error code
220                          */
221                         char *next = NULL;
222                         int *d;
223                         
224                         if ( p[ 1 ] != '{' ) {
225                                 /* XXX Need to free stuff */
226                                 return REWRITE_ERR;
227                         }
228
229                         d = malloc( sizeof( int ) );
230                         if ( d == NULL ) {
231                                 /* XXX Need to free stuff */
232                                 return REWRITE_ERR;
233                         }
234
235                         d[ 0 ] = strtol( &p[ 2 ], &next, 0 );
236                         if ( next == NULL || next == &p[ 2 ] || next[0] != '}' ) {
237                                 /* XXX Need to free stuff */
238                                 return REWRITE_ERR;
239                         }
240
241                         action = calloc( sizeof( struct rewrite_action ), 1 );
242                         if ( action == NULL ) {
243                                 /* cleanup ... */       
244                                 return REWRITE_ERR;
245                         }
246                         switch ( p[ 0 ] ) {
247                         case REWRITE_FLAG_GOTO:
248                                 action->la_type = REWRITE_ACTION_GOTO;
249                                 break;
250
251                         case REWRITE_FLAG_USER:
252                                 action->la_type = REWRITE_ACTION_USER;
253                                 break;
254
255                         default:
256                                 assert(0);
257                         }
258
259                         action->la_args = (void *)d;
260
261                         p = next;       /* p is incremented by the for ... */
262                 
263                         break;
264                 }
265
266                 case REWRITE_FLAG_MAX_PASSES: {                 /* 'U' */
267                         /*
268                          * Set the number of max passes per rule
269                          */
270                         char *next = NULL;
271                         
272                         if ( p[ 1 ] != '{' ) {
273                                 /* XXX Need to free stuff */
274                                 return REWRITE_ERR;
275                         }
276
277                         max_passes = strtol( &p[ 2 ], &next, 0 );
278                         if ( next == NULL || next == &p[ 2 ] || next[0] != '}' ) {
279                                 /* XXX Need to free stuff */
280                                 return REWRITE_ERR;
281                         }
282
283                         if ( max_passes < 1 ) {
284                                 /* FIXME: nonsense ... */
285                                 max_passes = 1;
286                         }
287
288                         p = next;       /* p is incremented by the for ... */
289                 
290                         break;
291                 }
292
293                 case REWRITE_FLAG_IGNORE_ERR:               /* 'I' */
294                         /*
295                          * Ignore errors!
296                          */
297                         action = calloc( sizeof( struct rewrite_action ), 1 );
298                         if ( action == NULL ) {
299                                 /* cleanup ... */
300                                 return REWRITE_ERR;
301                         }
302                         
303                         action->la_type = REWRITE_ACTION_IGNORE_ERR;
304                         break;
305                         
306                 /*
307                  * Other flags ...
308                  */
309                 default:
310                         /*
311                          * Unimplemented feature (complain only)
312                          */
313                         break;
314                 }
315                 
316                 /*
317                  * Stupid way to append to a list ...
318                  */
319                 if ( action != NULL ) {
320                         append_action( &first_action, action );
321                         action = NULL;
322                 }
323         }
324         
325         /*
326          * Finally, rule allocation
327          */
328         rule = calloc( sizeof( struct rewrite_rule ), 1 );
329         if ( rule == NULL ) {
330                 /* charray_free( res ); */
331                 /*
332                  * XXX need to free the value subst stuff!
333                  */
334                 return REWRITE_ERR;
335         }
336         
337         /*
338          * REGEX compilation (luckily I don't need to take care of this ...)
339          */
340         if ( regcomp( &rule->lr_regex, ( char * )pattern, flags ) != 0 ) {
341                 /* charray_free( res ); */
342                 /*
343                  *XXX need to free the value subst stuff!
344                  */
345                 free( rule );
346                 return REWRITE_ERR;
347         }
348         
349         /*
350          * Just to remember them ...
351          */
352         rule->lr_pattern = strdup( pattern );
353         rule->lr_subststring = strdup( result );
354         rule->lr_flagstring = strdup( flagstring );
355         
356         /*
357          * Load compiled data into rule
358          */
359         rule->lr_subst = subst;
360
361         /*
362          * Set various parameters
363          */
364         rule->lr_flags = flags;         /* don't really need any longer ... */
365         rule->lr_mode = mode;
366         rule->lr_max_passes = max_passes;
367         rule->lr_action = first_action;
368         
369         /*
370          * Append rule at the end of the rewrite context
371          */
372         append_rule( context, rule );
373
374         return REWRITE_SUCCESS;
375 }
376
377 /*
378  * Rewrites string according to rule; may return:
379  *      OK:     fine; if *result != NULL rule matched and rewrite succeeded.
380  *      STOP:   fine, rule matched; stop processing following rules
381  *      UNWILL: rule matched; force 'unwilling to perform'
382  */
383 int
384 rewrite_rule_apply(
385                 struct rewrite_info *info,
386                 struct rewrite_op *op,
387                 struct rewrite_rule *rule,
388                 const char *arg,
389                 char **result
390                 )
391 {
392         size_t nmatch = REWRITE_MAX_MATCH;
393         regmatch_t match[ REWRITE_MAX_MATCH ];
394
395         int rc = REWRITE_SUCCESS;
396
397         char *string;
398         int strcnt = 0;
399         struct berval val = { 0, NULL };
400
401         assert( info != NULL );
402         assert( op != NULL );
403         assert( rule != NULL );
404         assert( arg != NULL );
405         assert( result != NULL );
406
407         *result = NULL;
408
409         string = (char *)arg;
410         
411         /*
412          * In case recursive match is required (default)
413          */
414 recurse:;
415
416         Debug( LDAP_DEBUG_TRACE, "==> rewrite_rule_apply"
417                         " rule='%s' string='%s' [%d pass(es)]\n", 
418                         rule->lr_pattern, string, strcnt + 1 );
419         
420         op->lo_num_passes++;
421         if ( regexec( &rule->lr_regex, string, nmatch, match, 0 ) != 0 ) {
422                 if ( *result == NULL && strcnt > 0 ) {
423                         free( string );
424                         string = NULL;
425                 }
426
427                 /*
428                  * No match is OK; *result = NULL means no match
429                  */
430                 return REWRITE_REGEXEC_OK;
431         }
432
433         rc = rewrite_subst_apply( info, op, rule->lr_subst, string,
434                         match, &val );
435
436         *result = val.bv_val;
437         val.bv_val = NULL;
438         if ( strcnt > 0 ) {
439                 free( string );
440                 string = NULL;
441         }
442
443         if ( rc != REWRITE_REGEXEC_OK ) {
444                 return rc;
445         }
446
447         if ( ( rule->lr_mode & REWRITE_RECURSE ) == REWRITE_RECURSE 
448                         && op->lo_num_passes < info->li_max_passes
449                         && ++strcnt < rule->lr_max_passes ) {
450                 string = *result;
451
452                 goto recurse;
453         }
454
455         return REWRITE_REGEXEC_OK;
456 }
457
458 int
459 rewrite_rule_destroy(
460                 struct rewrite_rule **prule
461                 )
462 {
463         struct rewrite_rule *rule;
464         struct rewrite_action *action;
465
466         assert( prule );
467         assert( *prule );
468
469         rule = *prule;
470
471         if ( rule->lr_pattern ) {
472                 free( rule->lr_pattern );
473                 rule->lr_pattern = NULL;
474         }
475
476         if ( rule->lr_subststring ) {
477                 free( rule->lr_subststring );
478                 rule->lr_subststring = NULL;
479         }
480
481         if ( rule->lr_flagstring ) {
482                 free( rule->lr_flagstring );
483                 rule->lr_flagstring = NULL;
484         }
485
486         if ( rule->lr_subst ) {
487                 rewrite_subst_destroy( &rule->lr_subst );
488         }
489
490         regfree( &rule->lr_regex );
491
492         for ( action = rule->lr_action; action; ) {
493                 struct rewrite_action *curraction = action;
494
495                 action = action->la_next;
496                 destroy_action( &curraction );
497         }
498
499         free( rule );
500         *prule = NULL;
501
502         return 0;
503 }
504