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.
22 #include "rewrite-int.h"
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.
33 struct rewrite_context *rewrite_int_curr_context = NULL;
43 struct rewrite_info *info;
44 struct rewrite_context *context;
47 case REWRITE_MODE_ERR:
49 case REWRITE_MODE_COPY_INPUT:
50 case REWRITE_MODE_USE_DEFAULT:
53 mode = REWRITE_MODE_USE_DEFAULT;
59 * Resets the running context for parsing ...
61 rewrite_int_curr_context = NULL;
63 info = calloc( sizeof( struct rewrite_info ), 1 );
68 info->li_state = REWRITE_DEFAULT;
69 info->li_max_passes = REWRITE_MAX_PASSES;
70 info->li_rewrite_mode = mode;
73 * Add the default (empty) rule
75 context = rewrite_context_create( info, REWRITE_DEFAULT_CONTEXT );
76 if ( context == NULL ) {
81 #ifdef USE_REWRITE_LDAP_PVT_THREADS
82 if ( ldap_pvt_thread_rdwr_init( &info->li_cookies_mutex ) ) {
86 if ( ldap_pvt_thread_rdwr_init( &info->li_params_mutex ) ) {
90 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
96 * Cleans up the info structure
100 struct rewrite_info **pinfo
103 struct rewrite_info *info;
105 assert( pinfo != NULL );
106 assert( *pinfo != NULL );
110 if ( info->li_context ) {
111 avl_free( info->li_context, rewrite_context_free );
113 info->li_context = NULL;
115 if ( info->li_maps ) {
116 avl_free( info->li_maps, rewrite_builtin_map_free );
118 info->li_context = NULL;
120 rewrite_session_destroy( info );
122 #ifdef USE_REWRITE_LDAP_PVT_THREADS
123 ldap_pvt_thread_rdwr_destroy( &info->li_cookies_mutex );
124 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
126 rewrite_param_destroy( info );
128 #ifdef USE_REWRITE_LDAP_PVT_THREADS
129 ldap_pvt_thread_rdwr_destroy( &info->li_params_mutex );
130 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
135 return REWRITE_SUCCESS;
139 * Rewrites a string according to context.
140 * If the engine is off, OK is returned, but the return string will be NULL.
141 * In case of 'unwilling to perform', UNWILLING is returned, and the
142 * return string will also be null. The same in case of error.
143 * Otherwise, OK is returned, and result will hold a newly allocated string
144 * with the rewriting.
146 * What to do in case of non-existing rewrite context is still an issue.
147 * Four possibilities:
149 * - ok with NULL result,
150 * - ok with copy of string as result,
151 * - use the default rewrite context.
155 struct rewrite_info *info,
156 const char *rewriteContext,
161 return rewrite_session( info, rewriteContext,
162 string, NULL, result );
167 struct rewrite_info *info,
168 const char *rewriteContext,
174 struct rewrite_context *context;
175 struct rewrite_op op = { 0, 0, NULL, NULL, NULL };
178 assert( info != NULL );
179 assert( rewriteContext != NULL );
180 assert( string != NULL );
181 assert( result != NULL );
184 * cookie can be null; means: don't care about session stuff
188 op.lo_cookie = cookie;
191 * Engine not on means no failure, but explicit no rewriting
193 if ( info->li_state != REWRITE_ON ) {
194 rc = REWRITE_REGEXEC_OK;
199 * Undefined context means no rewriting also
200 * (conservative, are we sure it's what we want?)
202 context = rewrite_context_find( info, rewriteContext );
203 if ( context == NULL ) {
204 switch ( info->li_rewrite_mode ) {
205 case REWRITE_MODE_ERR:
206 rc = REWRITE_REGEXEC_ERR;
209 case REWRITE_MODE_OK:
210 rc = REWRITE_REGEXEC_OK;
213 case REWRITE_MODE_COPY_INPUT:
214 *result = strdup( string );
215 rc = REWRITE_REGEXEC_OK;
218 case REWRITE_MODE_USE_DEFAULT:
219 context = rewrite_context_find( info,
220 REWRITE_DEFAULT_CONTEXT );
225 #if 0 /* FIXME: not used anywhere! (debug? then, why strdup?) */
226 op.lo_string = strdup( string );
227 if ( op.lo_string == NULL ) {
228 rc = REWRITE_REGEXEC_ERR;
234 * Applies rewrite context
236 rc = rewrite_context_apply( info, &op, context, string, result );
237 assert( op.lo_depth == 0 );
239 #if 0 /* FIXME: not used anywhere! (debug? then, why strdup?) */
240 free( op.lo_string );
247 case REWRITE_REGEXEC_OK:
248 case REWRITE_REGEXEC_STOP:
250 * If rewrite succeeded return OK regardless of how
251 * the successful rewriting was obtained!
253 rc = REWRITE_REGEXEC_OK;
258 * Internal or forced error, return = NULL; rc already OK.
260 case REWRITE_REGEXEC_UNWILLING:
261 case REWRITE_REGEXEC_ERR:
263 if ( *result != NULL ) {
271 rewrite_var_delete( op.lo_vars );