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"
23 #include "rewrite-map.h"
29 rewrite_parse_builtin_map(
30 struct rewrite_info *info,
38 * Parses a config line and takes actions to fit content in rewrite structure;
39 * lines handled are of the form:
41 * rewriteEngine {on|off}
42 * rewriteMaxPasses numPasses
43 * rewriteContext contextName [alias aliasedContextName]
44 * rewriteRule pattern substPattern [ruleFlags]
45 * rewriteMap mapType mapName [mapArgs]
46 * rewriteParam paramName paramValue
50 struct rewrite_info *info,
59 assert( info != NULL );
60 assert( fname != NULL );
61 assert( argv != NULL );
65 * Switch on the rewrite engine
67 if ( strcasecmp( argv[ 0 ], "rewriteEngine" ) == 0 ) {
69 Debug( LDAP_DEBUG_ANY,
70 "[%s:%d] rewriteEngine needs 'state'\n%s",
74 } else if ( argc > 2 ) {
75 Debug( LDAP_DEBUG_ANY,
76 "[%s:%d] extra fields in rewriteEngine"
77 " will be discarded\n%s",
81 if ( strcasecmp( argv[ 1 ], "on" ) == 0 ) {
82 info->li_state = REWRITE_ON;
84 } else if ( strcasecmp( argv[ 1 ], "off" ) == 0 ) {
85 info->li_state = REWRITE_OFF;
88 Debug( LDAP_DEBUG_ANY,
89 "[%s:%d] unknown 'state' in rewriteEngine;"
92 info->li_state = REWRITE_ON;
99 } else if ( strcasecmp( argv[ 0 ], "rewriteMaxPasses" ) == 0 ) {
101 Debug( LDAP_DEBUG_ANY,
102 "[%s:%d] rewriteMaxPasses needs 'value'\n%s",
106 info->li_max_passes = atoi( argv[ 1 ] );
107 rc = REWRITE_SUCCESS;
110 * Start a new rewrite context and set current context
112 } else if ( strcasecmp( argv[ 0 ], "rewriteContext" ) == 0 ) {
114 Debug( LDAP_DEBUG_ANY,
115 "[%s:%d] rewriteContext needs 'name'\n%s",
121 * Checks for existence (lots of contexts should be
122 * available by default ...)
124 rewrite_int_curr_context = rewrite_context_find( info, argv[ 1 ] );
125 if ( rewrite_int_curr_context == NULL ) {
126 rewrite_int_curr_context = rewrite_context_create( info,
129 if ( rewrite_int_curr_context == NULL ) {
136 * A context can alias another (e.g., the `builtin'
137 * contexts for backend operations, if not defined,
138 * alias the `default' rewrite context (with the
139 * notable exception of the searchResult context,
140 * which can be undefined)
142 if ( strcasecmp( argv[ 2 ], "alias" ) == 0 ) {
143 struct rewrite_context *aliased;
146 Debug( LDAP_DEBUG_ANY,
147 "[%s:%d] rewriteContext"
148 " needs 'name' after"
153 } else if ( argc > 4 ) {
154 Debug( LDAP_DEBUG_ANY,
155 "[%s:%d] extra fields in"
157 " after aliased name"
163 aliased = rewrite_context_find( info,
165 if ( aliased == NULL ) {
166 Debug( LDAP_DEBUG_ANY,
168 " rewriteContext '%s'"
169 " does not exists\n",
175 rewrite_int_curr_context->lc_alias = aliased;
176 rewrite_int_curr_context = aliased;
179 Debug( LDAP_DEBUG_ANY,
180 "[%s:%d] extra fields"
182 " will be discarded\n%s",
186 rc = REWRITE_SUCCESS;
189 * Compile a rule in current context
191 } else if ( strcasecmp( argv[ 0 ], "rewriteRule" ) == 0 ) {
193 Debug( LDAP_DEBUG_ANY,
194 "[%s:%d] rewriteRule needs 'pattern'"
195 " 'subst' ['flags']\n%s",
199 } else if ( argc > 4 ) {
200 Debug( LDAP_DEBUG_ANY,
201 "[%s:%d] extra fields in rewriteRule"
202 " will be discarded\n%s",
206 if ( rewrite_int_curr_context == NULL ) {
207 Debug( LDAP_DEBUG_ANY,
208 "[%s:%d] rewriteRule outside a"
209 " context; will add to default\n%s",
211 rewrite_int_curr_context = rewrite_context_find( info,
212 REWRITE_DEFAULT_CONTEXT );
215 * Default context MUST exist in a properly initialized
216 * struct rewrite_info
218 assert( rewrite_int_curr_context != NULL );
221 rc = rewrite_rule_compile( info, rewrite_int_curr_context, argv[ 1 ],
222 argv[ 2 ], ( argc == 4 ? argv[ 3 ] : "" ) );
225 * Add a plugin map to the map tree
227 } else if ( strcasecmp( argv[ 0 ], "rewriteMap" ) == 0 ) {
229 Debug( LDAP_DEBUG_ANY,
230 "[%s:%d] rewriteMap needs at least 'type'"
231 " and 'name' ['args']\n%s",
236 rc = rewrite_parse_builtin_map( info, fname, lineno,
240 * Set the value of a global scope parameter
242 } else if ( strcasecmp( argv[ 0 ], "rewriteParam" ) == 0 ) {
244 Debug( LDAP_DEBUG_ANY,
245 "[%s:%d] rewriteParam needs 'name'"
251 rc = rewrite_param_set( info, argv[ 1 ], argv[ 2 ] );
257 Debug( LDAP_DEBUG_ANY,
258 "[%s:%d] unknown command '%s'\n",
270 rewrite_builtin_map_cmp(
275 const struct rewrite_builtin_map *m1, *m2;
277 m1 = ( const struct rewrite_builtin_map * )c1;
278 m2 = ( const struct rewrite_builtin_map * )c2;
280 assert( m1 != NULL );
281 assert( m2 != NULL );
282 assert( m1->lb_name != NULL );
283 assert( m2->lb_name != NULL );
285 return strcasecmp( m1->lb_name, m2->lb_name );
292 rewrite_builtin_map_dup(
297 struct rewrite_builtin_map *m1, *m2;
299 m1 = ( struct rewrite_builtin_map * )c1;
300 m2 = ( struct rewrite_builtin_map * )c2;
302 assert( m1 != NULL );
303 assert( m2 != NULL );
304 assert( m1->lb_name != NULL );
305 assert( m2->lb_name != NULL );
307 return ( strcasecmp( m1->lb_name, m2->lb_name ) == 0 ? -1 : 0 );
311 * Adds a map to the info map tree
314 rewrite_builtin_map_insert(
315 struct rewrite_info *info,
316 struct rewrite_builtin_map *map
322 return avl_insert( &info->li_maps, ( caddr_t )map,
323 rewrite_builtin_map_cmp,
324 rewrite_builtin_map_dup );
330 struct rewrite_builtin_map *
331 rewrite_builtin_map_find(
332 struct rewrite_info *info,
336 struct rewrite_builtin_map tmp;
338 assert( info != NULL );
339 assert( name != NULL );
341 tmp.lb_name = ( char * )name;
343 return ( struct rewrite_builtin_map * )avl_find( info->li_maps,
344 ( caddr_t )&tmp, rewrite_builtin_map_cmp );
348 * Parses a plugin map
351 rewrite_parse_builtin_map(
352 struct rewrite_info *info,
359 struct rewrite_builtin_map *map;
364 assert( info != NULL );
365 assert( fname != NULL );
367 assert( argv != NULL );
368 assert( strcasecmp( argv[ 0 ], "rewriteMap" ) == 0 );
370 map = calloc( sizeof( struct rewrite_builtin_map ), 1 );
375 map->lb_name = strdup( argv[ MAP_NAME ] );
376 if ( map->lb_name == NULL ) {
384 if ( strcasecmp( argv[ MAP_TYPE ], "ldap" ) == 0 ) {
385 map->lb_type = REWRITE_BUILTIN_MAP_LDAP;
387 #ifdef USE_REWRITE_LDAP_PVT_THREADS
388 if ( ldap_pvt_thread_mutex_init( & map->lb_mutex ) ) {
389 free( map->lb_name );
393 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
395 map->lb_private = map_ldap_parse( info, fname, lineno,
396 argc - 3, argv + 3 );
402 Debug( LDAP_DEBUG_ANY, "[%s:%d] unknown map type\n%s",
407 return rewrite_builtin_map_insert( info, map );