]> git.sur5r.net Git - openldap/blob - libraries/librewrite/info.c
faa2424c3fd92935961dcd93bbffb5e26143ac84
[openldap] / libraries / librewrite / info.c
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 #include <portable.h>
21
22 #include "rewrite-int.h"
23
24 /*
25  * Global data
26  */
27
28 /*
29  * This becomes the running context for subsequent calls to
30  * rewrite_parse; it can be altered only by a 
31  * rewriteContext config line or by a change in info.
32  */
33 struct rewrite_context *rewrite_int_curr_context = NULL;
34
35 /*
36  * Inits the info
37  */
38 struct rewrite_info *
39 rewrite_info_init(
40                 int mode
41 )
42 {
43         struct rewrite_info *info;
44         struct rewrite_context *context;
45
46         switch ( mode ) {
47         case REWRITE_MODE_ERR:
48         case REWRITE_MODE_OK:
49         case REWRITE_MODE_COPY_INPUT:
50         case REWRITE_MODE_USE_DEFAULT:
51                 break;
52         default:
53                 mode = REWRITE_MODE_USE_DEFAULT;
54                 break;
55                 /* return NULL */
56         }
57
58         /*
59          * Resets the running context for parsing ...
60          */
61         rewrite_int_curr_context = NULL;
62
63         info = calloc( sizeof( struct rewrite_info ), 1 );
64         if ( info == NULL ) {
65                 return NULL;
66         }
67
68         info->li_state = REWRITE_DEFAULT;
69         info->li_max_passes = REWRITE_MAX_PASSES;
70         info->li_max_passes_per_rule = REWRITE_MAX_PASSES;
71         info->li_rewrite_mode = mode;
72
73         /*
74          * Add the default (empty) rule
75          */
76         context = rewrite_context_create( info, REWRITE_DEFAULT_CONTEXT );
77         if ( context == NULL ) {
78                 free( info );
79                 return NULL;
80         }
81
82 #ifdef USE_REWRITE_LDAP_PVT_THREADS
83         if ( ldap_pvt_thread_rdwr_init( &info->li_cookies_mutex ) ) {
84                 free( info );
85                 return NULL;
86         }
87         if ( ldap_pvt_thread_rdwr_init( &info->li_params_mutex ) ) {
88                 free( info );
89                 return NULL;
90         }
91 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
92         
93         return info;
94 }
95
96 /*
97  * Cleans up the info structure
98  */
99 int
100 rewrite_info_delete(
101                 struct rewrite_info **pinfo
102 )
103 {
104         struct rewrite_info     *info;
105
106         assert( pinfo != NULL );
107         assert( *pinfo != NULL );
108
109         info = *pinfo;
110         
111         if ( info->li_context ) {
112                 avl_free( info->li_context, rewrite_context_free );
113         }
114         info->li_context = NULL;
115
116         if ( info->li_maps ) {
117                 avl_free( info->li_maps, rewrite_builtin_map_free );
118         }
119         info->li_context = NULL;
120
121         rewrite_session_destroy( info );
122
123 #ifdef USE_REWRITE_LDAP_PVT_THREADS
124         ldap_pvt_thread_rdwr_destroy( &info->li_cookies_mutex );
125 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
126
127         rewrite_param_destroy( info );
128         
129 #ifdef USE_REWRITE_LDAP_PVT_THREADS
130         ldap_pvt_thread_rdwr_destroy( &info->li_params_mutex );
131 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
132
133         free( info );
134         *pinfo = NULL;
135
136         return REWRITE_SUCCESS;
137 }
138
139 /*
140  * Rewrites a string according to context.
141  * If the engine is off, OK is returned, but the return string will be NULL.
142  * In case of 'unwilling to perform', UNWILLING is returned, and the
143  * return string will also be null. The same in case of error.
144  * Otherwise, OK is returned, and result will hold a newly allocated string
145  * with the rewriting.
146  * 
147  * What to do in case of non-existing rewrite context is still an issue.
148  * Four possibilities:
149  *      - error, 
150  *      - ok with NULL result, 
151  *      - ok with copy of string as result,
152  *      - use the default rewrite context.
153  */
154 int
155 rewrite(
156                 struct rewrite_info *info,
157                 const char *rewriteContext,
158                 const char *string,
159                 char **result
160 )
161 {
162         return rewrite_session( info, rewriteContext, 
163                         string, NULL, result );
164 }
165
166 int
167 rewrite_session(
168                 struct rewrite_info *info,
169                 const char *rewriteContext,
170                 const char *string,
171                 const void *cookie,
172                 char **result
173 )
174 {
175         struct rewrite_context *context;
176         struct rewrite_op op = { 0, 0, NULL, NULL, NULL };
177         int rc;
178         
179         assert( info != NULL );
180         assert( rewriteContext != NULL );
181         assert( string != NULL );
182         assert( result != NULL );
183
184         /*
185          * cookie can be null; means: don't care about session stuff
186          */
187
188         *result = NULL;
189         op.lo_cookie = cookie;
190         
191         /*
192          * Engine not on means no failure, but explicit no rewriting
193          */
194         if ( info->li_state != REWRITE_ON ) {
195                 rc = REWRITE_REGEXEC_OK;
196                 goto rc_return;
197         }
198         
199         /*
200          * Undefined context means no rewriting also
201          * (conservative, are we sure it's what we want?)
202          */
203         context = rewrite_context_find( info, rewriteContext );
204         if ( context == NULL ) {
205                 switch ( info->li_rewrite_mode ) {
206                 case REWRITE_MODE_ERR:
207                         rc = REWRITE_REGEXEC_ERR;
208                         goto rc_return;
209                         
210                 case REWRITE_MODE_OK:
211                         rc = REWRITE_REGEXEC_OK;
212                         goto rc_return;
213
214                 case REWRITE_MODE_COPY_INPUT:
215                         *result = strdup( string );
216                         rc = REWRITE_REGEXEC_OK;
217                         goto rc_return;
218
219                 case REWRITE_MODE_USE_DEFAULT:
220                         context = rewrite_context_find( info,
221                                         REWRITE_DEFAULT_CONTEXT );
222                         break;
223                 }
224         }
225
226 #if 0 /* FIXME: not used anywhere! (debug? then, why strdup?) */
227         op.lo_string = strdup( string );
228         if ( op.lo_string == NULL ) {
229                 rc = REWRITE_REGEXEC_ERR;
230                 goto rc_return;
231         }
232 #endif
233         
234         /*
235          * Applies rewrite context
236          */
237         rc = rewrite_context_apply( info, &op, context, string, result );
238         assert( op.lo_depth == 0 );
239
240 #if 0 /* FIXME: not used anywhere! (debug? then, why strdup?) */        
241         free( op.lo_string );
242 #endif
243         
244         switch ( rc ) {
245         /*
246          * Success
247          */
248         case REWRITE_REGEXEC_OK:
249         case REWRITE_REGEXEC_STOP:
250                 /*
251                  * If rewrite succeeded return OK regardless of how
252                  * the successful rewriting was obtained!
253                  */
254                 rc = REWRITE_REGEXEC_OK;
255                 break;
256                 
257         
258         /*
259          * Internal or forced error, return = NULL; rc already OK.
260          */
261         case REWRITE_REGEXEC_UNWILLING:
262         case REWRITE_REGEXEC_ERR:
263                 if ( *result != NULL ) {
264                         free( *result );
265                         *result = NULL;
266                 }
267
268         default:
269                 break;
270         }
271
272 rc_return:;
273         if ( op.lo_vars ) {
274                 rewrite_var_delete( op.lo_vars );
275         }
276         
277         return rc;
278 }
279