]> git.sur5r.net Git - openldap/blobdiff - libraries/librewrite/map.c
Merge remote-tracking branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_4
[openldap] / libraries / librewrite / map.c
index c40456aa9cc6d5a366c078428fce44392b79d74a..c467e471f6ad10ad2a8cb46ea2a6dd17bf795d00 100644 (file)
-/******************************************************************************
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright (C) 2000 Pierangelo Masarati, <ando@sys-net.it>
+ * Copyright 2000-2013 The OpenLDAP Foundation.
  * All rights reserved.
  *
- * Permission is granted to anyone to use this software for any purpose
- * on any computer system, and to alter it and redistribute it, subject
- * to the following restrictions:
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
  *
- * 1. The author is not responsible for the consequences of use of this
- * software, no matter how awful, even if they arise from flaws in it.
- *
- * 2. The origin of this software must not be misrepresented, either by
- * explicit claim or by omission.  Since few users ever read sources,
- * credits should appear in the documentation.
- *
- * 3. Altered versions must be plainly marked as such, and must not be
- * misrepresented as being the original software.  Since few users
- * ever read sources, credits should appear in the documentation.
- * 
- * 4. This notice may not be removed or altered.
- *
- ******************************************************************************/
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* ACKNOWLEDGEMENT:
+ * This work was initially developed by Pierangelo Masarati for
+ * inclusion in OpenLDAP Software.
+ */
 
 #include <portable.h>
 
+#include <stdio.h>
+
+#ifdef HAVE_PWD_H
 #include <pwd.h>
+#endif
 
 #include "rewrite-int.h"
 #include "rewrite-map.h"
 
-/*
- * Global data
- */
-#ifdef USE_REWRITE_LDAP_PVT_THREADS
-ldap_pvt_thread_mutex_t xpasswd_mutex;
-static int xpasswd_mutex_init = 0;
-#endif /* USE_REWRITE_LDAP_PVT_THREADS */
-
-/*
- * Map parsing
- * NOTE: these are old-fashion maps; new maps will be parsed on separate
- * config lines, and referred by name.
- */
-struct rewrite_map *
-rewrite_xmap_parse(
-               struct rewrite_info *info,
-               const char *s,
-               const char **currpos
-)
-{
-       struct rewrite_map *map;
-
-       assert( info != NULL );
-       assert( s != NULL );
-       assert( currpos != NULL );
-
-       Debug( LDAP_DEBUG_ARGS, "rewrite_xmap_parse: %s\n%s%s",
-                       s, "", "" );
-
-       *currpos = NULL;
-
-       map = calloc( sizeof( struct rewrite_map ), 1 );
-       if ( map == NULL ) {
-               Debug( LDAP_DEBUG_ANY, "rewrite_xmap_parse:"
-                               " calloc failed\n%s%s%s", "", "", "" );
-               return NULL;
-       }
-
-       /*
-        * Experimental passwd map:
-        * replaces the uid with the matching gecos from /etc/passwd file 
-        */
-       if ( strncasecmp(s, "xpasswd", 7 ) == 0 ) {
-               map->lm_type = REWRITE_MAP_XPWDMAP;
-               map->lm_name = strdup( "xpasswd" );
-
-               assert( s[7] == '}' );
-               *currpos = s + 8;
-
-#ifdef USE_REWRITE_LDAP_PVT_THREADS
-               if ( !xpasswd_mutex_init ) {
-                       xpasswd_mutex_init = 1;
-                       if ( ldap_pvt_thread_mutex_init( &xpasswd_mutex ) ) {
-                               free( map );
-                               return NULL;
-                       }
-               }
-#endif /* USE_REWRITE_LDAP_PVT_THREADS */
-
-               /* Don't really care if fails */
-               return map;
-       
-       /*
-        * Experimental file map:
-        * looks up key in a `key value' ascii file
-        */
-       } else if ( strncasecmp(s, "xfile", 5 ) == 0 ) {
-               char *filename;
-               const char *p;
-               int l;
-               int c = 5;
-               
-               map->lm_type = REWRITE_MAP_XFILEMAP;
-               
-               if ( s[ c ] != '(' ) {
-                       free( map );
-                       return NULL;
-               }
-
-               /* Must start with '/' for security concerns */
-               c++;
-               if ( s[ c ] != '/' ) {
-                       free( map );
-                       return NULL;
-               }
-
-               for ( p = s + c; p[ 0 ] != '\0' && p[ 0 ] != ')'; p++ );
-               if ( p[ 0 ] != ')' ) {
-                       free( map );
-                       return NULL;
-               }
-
-               l = p - s - c;
-               filename = calloc( sizeof( char ), l + 1 );
-               strncpy( filename, s + c, l );
-               filename[ l ] = '\0';
-               
-               map->lm_args = ( void * )fopen( filename, "r" );
-               free( filename );
-
-               if ( map->lm_args == NULL ) {
-                       free( map );
-                       return NULL;
-               }
-
-               *currpos = p + 1;
-
-#ifdef USE_REWRITE_LDAP_PVT_THREADS
-                if ( ldap_pvt_thread_mutex_init( &map->lm_mutex ) ) {
-                       fclose( ( FILE * )map->lm_args );
-                       free( map );
-                       return NULL;
-               }
-#endif /* USE_REWRITE_LDAP_PVT_THREADS */      
-               
-               return map;
-
-       /*
-         * Experimental ldap map:
-         * looks up key on the fly (not implemented!)
-         */
-        } else if ( strncasecmp(s, "xldap", 5 ) == 0 ) {
-               char *p;
-               char *url;
-               int l, rc;
-               int c = 5;
-               LDAPURLDesc *lud;
-
-               if ( s[ c ] != '(' ) {
-                       free( map );
-                       return NULL;
-               }
-               c++;
-               
-               p = strchr( s, '}' );
-               if ( p == NULL ) {
-                       free( map );
-                       return NULL;
-               }
-               p--;
-
-               *currpos = p + 2;
-       
-               /*
-                * Add two bytes for urlencoding of '%s'
-                */
-               l = p - s - c;
-               url = calloc( sizeof( char ), l + 3 );
-               strncpy( url, s + c, l );
-               url[ l ] = '\0';
-
-               /*
-                * Urlencodes the '%s' for ldap_url_parse
-                */
-               p = strchr( url, '%' );
-               if ( p != NULL ) {
-                       memmove( p + 3, p + 1, strlen( p + 1 ) + 1 );
-                       p[ 1 ] = '2';
-                       p[ 2 ] = '5';
-               }
-
-               rc =  ldap_url_parse( url, &lud );
-               free( url );
-
-               if ( rc != LDAP_SUCCESS ) {
-                       free( map );
-                       return NULL;
-               }
-               assert( lud != NULL );
-
-               map->lm_args = ( void * )lud;
-               map->lm_type = REWRITE_MAP_XLDAPMAP;
-
-#ifdef USE_REWRITE_LDAP_PVT_THREADS
-                if ( ldap_pvt_thread_mutex_init( &map->lm_mutex ) ) {
-                       ldap_free_urldesc( lud );
-                       free( map );
-                       return NULL;
-               }
-#endif /* USE_REWRITE_LDAP_PVT_THREADS */
-
-               return map;
-       
-       /* Unhandled map */
-       }
-       
-       return NULL;
-}
+static int num_mappers;
+static const rewrite_mapper **mappers;
+#define        MAPPER_ALLOC    8
 
 struct rewrite_map *
 rewrite_map_parse(
@@ -229,7 +43,7 @@ rewrite_map_parse(
        struct rewrite_subst *subst = NULL;
        char *s, *begin = NULL, *end;
        const char *p;
-       int l, cnt;
+       int l, cnt, mtx = 0, rc = 0;
 
        assert( info != NULL );
        assert( string != NULL );
@@ -241,21 +55,25 @@ rewrite_map_parse(
         * Go to the end of the map invocation (the right closing brace)
         */
        for ( p = string, cnt = 1; p[ 0 ] != '\0' && cnt > 0; p++ ) {
-               if ( p[ 0 ] == REWRITE_SUBMATCH_ESCAPE ) {
+               if ( IS_REWRITE_SUBMATCH_ESCAPE( p[ 0 ] ) ) {
                        /*
-                        * '\' marks the beginning of a new map
+                        * '%' marks the beginning of a new map
                         */
                        if ( p[ 1 ] == '{' ) {
                                cnt++;
                        /*
-                        * '\' followed by a digit may mark the beginning
+                        * '%' followed by a digit may mark the beginning
                         * of an old map
                         */
-                       } else if ( isdigit( p[ 1 ] ) && p[ 2 ] == '{' ) {
+                       } else if ( isdigit( (unsigned char) p[ 1 ] ) && p[ 2 ] == '{' ) {
                                cnt++;
                                p++;
                        }
-                       p++;
+
+                       if ( p[ 1 ] != '\0' ) {
+                               p++;
+                       }
+
                } else if ( p[ 0 ] == '}' ) {
                        cnt--;
                }
@@ -270,7 +88,10 @@ rewrite_map_parse(
         */
        l = p - string - 1;
        s = calloc( sizeof( char ), l + 1 );
-       strncpy( s, string, l );
+       if ( s == NULL ) {
+               return NULL;
+       }
+       AC_MEMCPY( s, string, l );
        s[ l ] = 0;
 
        /*
@@ -280,11 +101,12 @@ rewrite_map_parse(
        case REWRITE_OPERATOR_VARIABLE_GET:
        case REWRITE_OPERATOR_PARAM_GET:
                break;
+
        default:
                begin = strchr( s, '(' );
                if ( begin == NULL ) {
-                       free( s );
-                       return NULL;
+                       rc = -1;
+                       goto cleanup;
                }
                begin[ 0 ] = '\0';
                begin++;
@@ -327,14 +149,14 @@ rewrite_map_parse(
        /*
         * Check the syntax of the variable name
         */
-       if ( !isalpha( p[ 0 ] ) ) {
-               free( s );
-               return NULL;
+       if ( !isalpha( (unsigned char) p[ 0 ] ) ) {
+               rc = -1;
+               goto cleanup;
        }
        for ( p++; p[ 0 ] != '\0'; p++ ) {
-               if ( !isalnum( p[ 0 ] ) ) {
-                       free( s );
-                       return NULL;
+               if ( !isalnum( (unsigned char) p[ 0 ] ) ) {
+                       rc = -1;
+                       goto cleanup;
                }
        }
 
@@ -345,11 +167,12 @@ rewrite_map_parse(
        case REWRITE_OPERATOR_VARIABLE_GET:
        case REWRITE_OPERATOR_PARAM_GET:
                break;
+
        default:
                end = strrchr( begin, ')' );
                if ( end == NULL ) {
-                       free( s );
-                       return NULL;
+                       rc = -1;
+                       goto cleanup;
                }
                end[ 0 ] = '\0';
 
@@ -358,8 +181,8 @@ rewrite_map_parse(
                 */
                subst = rewrite_subst_compile( info, begin );
                if ( subst == NULL ) {
-                       free( s );
-                       return NULL;
+                       rc = -1;
+                       goto cleanup;
                }
                break;
        }
@@ -369,22 +192,17 @@ rewrite_map_parse(
         */
        map = calloc( sizeof( struct rewrite_map ), 1 );
        if ( map == NULL ) {
-               if ( subst != NULL ) {
-                       free( subst );
-               }
-               free( s );
-               return NULL;
+               rc = -1;
+               goto cleanup;
        }
+       memset( map, 0, sizeof( struct rewrite_map ) );
        
 #ifdef USE_REWRITE_LDAP_PVT_THREADS
         if ( ldap_pvt_thread_mutex_init( &map->lm_mutex ) ) {
-               if ( subst != NULL ) {
-                       free( subst );
-               }
-               free( s );
-               free( map );
-               return NULL;
+               rc = -1;
+               goto cleanup;
        }
+       ++mtx;
 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
                        
        /*
@@ -394,6 +212,7 @@ rewrite_map_parse(
        case REWRITE_OPERATOR_VARIABLE_GET:
        case REWRITE_OPERATOR_PARAM_GET:
                break;
+
        default:
                map->lm_subst = subst;
                break;
@@ -415,21 +234,23 @@ rewrite_map_parse(
                 */
                map->lm_type = REWRITE_MAP_SUBCONTEXT;
                map->lm_name = strdup( s + 1 );
+               if ( map->lm_name == NULL ) {
+                       rc = -1;
+                       goto cleanup;
+               }
                map->lm_data = rewrite_context_find( info, s + 1 );
                if ( map->lm_data == NULL ) {
-                       free( s );
-                       free( map );
-                       return NULL;
+                       rc = -1;
+                       goto cleanup;
                }
                break;
 
        /*
-        * External command
+        * External command (not implemented yet)
         */
        case REWRITE_OPERATOR_COMMAND:          /* '|' */
-               free( map );
-               map = NULL;
-               break;
+               rc = -1;
+               goto cleanup;
        
        /*
         * Variable set
@@ -452,6 +273,10 @@ rewrite_map_parse(
                                map->lm_name = strdup( s + 1 );
                        }
                }
+               if ( map->lm_name == NULL ) {
+                       rc = -1;
+                       goto cleanup;
+               }
                break;
        
        /*
@@ -465,6 +290,10 @@ rewrite_map_parse(
                        map->lm_type = REWRITE_MAP_GET_OP_VAR;
                        map->lm_name = strdup( s + 1 );
                }
+               if ( map->lm_name == NULL ) {
+                       rc = -1;
+                       goto cleanup;
+               }
                break;
        
        /*
@@ -473,6 +302,10 @@ rewrite_map_parse(
        case REWRITE_OPERATOR_PARAM_GET:                /* '$' */
                map->lm_type = REWRITE_MAP_GET_PARAM;
                map->lm_name = strdup( s + 1 );
+               if ( map->lm_name == NULL ) {
+                       rc = -1;
+                       goto cleanup;
+               }
                break;
        
        /*
@@ -481,223 +314,42 @@ rewrite_map_parse(
        default:
                map->lm_type = REWRITE_MAP_BUILTIN;
                map->lm_name = strdup( s );
+               if ( map->lm_name == NULL ) {
+                       rc = -1;
+                       goto cleanup;
+               }
                map->lm_data = rewrite_builtin_map_find( info, s );
                if ( map->lm_data == NULL ) {
-                       return NULL;
+                       rc = -1;
+                       goto cleanup;
                }
                break;
 
        }
-       
-       free( s );
-       return map;
-}
 
-/*
- * Map key -> value resolution
- * NOTE: these are old-fashion maps; new maps will be parsed on separate
- * config lines, and referred by name.
- */
-int
-rewrite_xmap_apply(
-               struct rewrite_info *info,
-               struct rewrite_op *op,
-               struct rewrite_map *map,
-               struct berval *key,
-               struct berval *val
-)
-{
-       int rc = REWRITE_SUCCESS;
-       
-       assert( info != NULL );
-       assert( op != NULL );
-       assert( map != NULL );
-       assert( key != NULL );
-       assert( val != NULL );
-       
-       val->bv_val = NULL;
-       val->bv_len = 0;
-       
-       switch ( map->lm_type ) {
-       case REWRITE_MAP_XPWDMAP: {
-               struct passwd *pwd;
-
-#ifdef USE_REWRITE_LDAP_PVT_THREADS
-               ldap_pvt_thread_mutex_lock( &xpasswd_mutex );
-#endif /* USE_REWRITE_LDAP_PVT_THREADS */
-               
-               pwd = getpwnam( key->bv_val );
-               if ( pwd == NULL ) {
-
-#ifdef USE_REWRITE_LDAP_PVT_THREADS
-                       ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
-#endif /* USE_REWRITE_LDAP_PVT_THREADS */
-
-                       rc = REWRITE_NO_SUCH_OBJECT;
-                       break;
+cleanup:
+       free( s );
+       if ( rc ) {
+               if ( subst != NULL ) {
+                       free( subst );
                }
-
-               if ( pwd->pw_gecos != NULL && pwd->pw_gecos[0] != '\0' ) {
-                       int l = strlen( pwd->pw_gecos );
-                       
-                       val->bv_val = strdup( pwd->pw_gecos );
-                       if ( val->bv_val == NULL ) {
-
+               if ( map ) {
 #ifdef USE_REWRITE_LDAP_PVT_THREADS
-                               ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
-#endif /* USE_REWRITE_LDAP_PVT_THREADS */
-
-                               rc = REWRITE_ERR;
-                               break;
+                       if ( mtx ) {
+                               ldap_pvt_thread_mutex_destroy( &map->lm_mutex );
                        }
-                       val->bv_len = l;
-               } else {
-                       val->bv_val = strdup( key->bv_val );
-                       val->bv_len = key->bv_len;
-               }
-
-#ifdef USE_REWRITE_LDAP_PVT_THREADS
-               ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
-#endif /* USE_REWRITE_LDAP_PVT_THREADS */
-                       
-               break;
-       }
-       
-       case REWRITE_MAP_XFILEMAP: {
-               char buf[1024];
-               
-               if ( map->lm_args == NULL ) {
-                       rc = REWRITE_ERR;
-                       break;
-               }
-               
-#ifdef USE_REWRITE_LDAP_PVT_THREADS
-               ldap_pvt_thread_mutex_lock( &map->lm_mutex );
 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
 
-               rewind( ( FILE * )map->lm_args );
-               
-               while ( fgets( buf, sizeof( buf ), ( FILE * )map->lm_args ) ) {
-                       char *p;
-                       int blen;
-                       
-                       blen = strlen( buf );
-                       if ( buf[ blen - 1 ] == '\n' ) {
-                               buf[ blen - 1 ] = '\0';
+                       if ( map->lm_name ) {
+                               free( map->lm_name );
+                               map->lm_name = NULL;
                        }
-                       
-                       p = strtok( buf, " " );
-                       if ( p == NULL ) {
-#ifdef USE_REWRITE_LDAP_PVT_THREADS
-                               ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
-#endif /* USE_REWRITE_LDAP_PVT_THREADS */
-                               rc = REWRITE_ERR;
-                               goto rc_return;
-                       }
-                       if ( strcasecmp( p, key->bv_val ) == 0 
-                                       && ( p = strtok( NULL, "" ) ) ) {
-                               val->bv_val = strdup( p );
-                               if ( val->bv_val == NULL ) {
-                                       return REWRITE_ERR;
-                               }
-
-                               val->bv_len = strlen( p );
-                               
-#ifdef USE_REWRITE_LDAP_PVT_THREADS
-                               ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
-#endif /* USE_REWRITE_LDAP_PVT_THREADS */
-                               
-                               goto rc_return;
-                       }
-               }
-
-#ifdef USE_REWRITE_LDAP_PVT_THREADS
-               ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
-#endif /* USE_REWRITE_LDAP_PVT_THREADS */
-
-               rc = REWRITE_ERR;
-               
-               break;
-       }
-
-       case REWRITE_MAP_XLDAPMAP: {
-               LDAP *ld;
-               char filter[ LDAP_FILT_MAXSIZ ];
-               LDAPMessage *res = NULL, *entry;
-               LDAPURLDesc *lud = ( LDAPURLDesc * )map->lm_args;
-               int attrsonly = 0;
-               char **values;
-
-               assert( lud != NULL );
-
-               /*
-                * No mutex because there is no write on the map data
-                */
-               
-               ld = ldap_init( lud->lud_host, lud->lud_port );
-               if ( ld == NULL ) {
-                       rc = REWRITE_ERR;
-                       goto rc_return;
-               }
-
-               snprintf( filter, sizeof( filter ), lud->lud_filter,
-                               key->bv_val );
-
-               if ( strcasecmp( lud->lud_attrs[ 0 ], "dn" ) == 0 ) {
-                       attrsonly = 1;
-               }
-               rc = ldap_search_s( ld, lud->lud_dn, lud->lud_scope,
-                               filter, lud->lud_attrs, attrsonly, &res );
-               if ( rc != LDAP_SUCCESS ) {
-                       ldap_unbind( ld );
-                       rc = REWRITE_ERR;
-                       goto rc_return;
-               }
-
-               if ( ldap_count_entries( ld, res ) != 1 ) {
-                       ldap_unbind( ld );
-                       rc = REWRITE_ERR;
-                       goto rc_return;
-               }
-
-               entry = ldap_first_entry( ld, res );
-               if ( entry == NULL ) {
-                       ldap_msgfree( res );
-                       ldap_unbind( ld );
-                       rc = REWRITE_ERR;
-                       goto rc_return;
-               }
-               if ( attrsonly == 1 ) {
-                       val->bv_val = ldap_get_dn( ld, entry );
-                       if ( val->bv_val == NULL ) {
-                               ldap_msgfree( res );
-                                ldap_unbind( ld );
-                                rc = REWRITE_ERR;
-                                goto rc_return;
-                        }
-               } else {
-                       values = ldap_get_values( ld, entry,
-                                       lud->lud_attrs[0] );
-                       if ( values == NULL ) {
-                               ldap_msgfree( res );
-                               ldap_unbind( ld );
-                               rc = REWRITE_ERR;
-                               goto rc_return;
-                       }
-                       val->bv_val = strdup( values[ 0 ] );
-                       ldap_value_free( values );
+                       free( map );
+                       map = NULL;
                }
-               val->bv_len = strlen( val->bv_val );
-
-               ldap_msgfree( res );
-               ldap_unbind( ld );
-               
-               rc = REWRITE_SUCCESS;
-       }
        }
 
-rc_return:;
-       return rc;
+       return map;
 }
 
 /*
@@ -729,7 +381,12 @@ rewrite_map_apply(
                                ( struct rewrite_context * )map->lm_data,
                                key->bv_val, &val->bv_val );
                if ( val->bv_val != NULL ) {
-                       val->bv_len = strlen( val->bv_val );
+                       if ( val->bv_val == key->bv_val ) {
+                               val->bv_len = key->bv_len;
+                               key->bv_val = NULL;
+                       } else {
+                               val->bv_len = strlen( val->bv_val );
+                       }
                }
                break;
 
@@ -738,11 +395,16 @@ rewrite_map_apply(
                rc = rewrite_var_set( &op->lo_vars, map->lm_name,
                                key->bv_val, 1 )
                        ? REWRITE_SUCCESS : REWRITE_ERR;
-               if ( map->lm_type == REWRITE_MAP_SET_OP_VAR ) {
-                       val->bv_val = strdup( "" );
-               } else {
-                       val->bv_val = strdup( key->bv_val );
-                       val->bv_len = key->bv_len;
+               if ( rc == REWRITE_SUCCESS ) {
+                       if ( map->lm_type == REWRITE_MAP_SET_OP_VAR ) {
+                               val->bv_val = strdup( "" );
+                       } else {
+                               val->bv_val = strdup( key->bv_val );
+                               val->bv_len = key->bv_len;
+                       }
+                       if ( val->bv_val == NULL ) {
+                               rc = REWRITE_ERR;
+                       }
                }
                break;
        
@@ -755,6 +417,9 @@ rewrite_map_apply(
                } else {
                        val->bv_val = strdup( var->lv_value.bv_val );
                        val->bv_len = var->lv_value.bv_len;
+                       if ( val->bv_val == NULL ) {
+                               rc = REWRITE_ERR;
+                       }
                }
                break;  
        }
@@ -767,11 +432,16 @@ rewrite_map_apply(
                }
                rc = rewrite_session_var_set( info, op->lo_cookie, 
                                map->lm_name, key->bv_val );
-               if ( map->lm_type == REWRITE_MAP_SET_SESN_VAR ) {
-                       val->bv_val = strdup( "" );
-               } else {
-                       val->bv_val = strdup( key->bv_val );
-                       val->bv_len = key->bv_len;
+               if ( rc == REWRITE_SUCCESS ) {
+                       if ( map->lm_type == REWRITE_MAP_SET_SESN_VAR ) {
+                               val->bv_val = strdup( "" );
+                       } else {
+                               val->bv_val = strdup( key->bv_val );
+                               val->bv_len = key->bv_len;
+                       }
+                       if ( val->bv_val == NULL ) {
+                               rc = REWRITE_ERR;
+                       }
                }
                break;
 
@@ -786,14 +456,13 @@ rewrite_map_apply(
 
        case REWRITE_MAP_BUILTIN: {
                struct rewrite_builtin_map *bmap = map->lm_data;
-               switch ( bmap->lb_type ) {
-               case REWRITE_BUILTIN_MAP_LDAP:
-                       rc = map_ldap_apply( bmap, key->bv_val, val );
-                       break;
-               default:
+
+               if ( bmap->lb_mapper && bmap->lb_mapper->rm_apply )
+                       rc = bmap->lb_mapper->rm_apply( bmap->lb_private, key->bv_val,
+                               val );
+               else
                        rc = REWRITE_ERR;
                        break;
-               }
                break;
        }
 
@@ -805,3 +474,110 @@ rewrite_map_apply(
        return rc;
 }
 
+void
+rewrite_builtin_map_free(
+               void *tmp
+)
+{
+       struct rewrite_builtin_map *map = ( struct rewrite_builtin_map * )tmp;
+
+       assert( map != NULL );
+
+       if ( map->lb_mapper && map->lb_mapper->rm_destroy )
+               map->lb_mapper->rm_destroy( map->lb_private );
+
+       free( map->lb_name );
+       free( map );
+}
+
+int
+rewrite_map_destroy(
+               struct rewrite_map **pmap
+)
+{
+       struct rewrite_map *map;
+       
+       assert( pmap != NULL );
+       assert( *pmap != NULL );
+
+       map = *pmap;
+
+#ifdef USE_REWRITE_LDAP_PVT_THREADS
+       ldap_pvt_thread_mutex_lock( &map->lm_mutex );
+#endif /* USE_REWRITE_LDAP_PVT_THREADS */
+
+       if ( map->lm_name ) {
+               free( map->lm_name );
+               map->lm_name = NULL;
+       }
+
+       if ( map->lm_subst ) {
+               rewrite_subst_destroy( &map->lm_subst );
+       }
+
+#ifdef USE_REWRITE_LDAP_PVT_THREADS
+       ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
+       ldap_pvt_thread_mutex_destroy( &map->lm_mutex );
+#endif /* USE_REWRITE_LDAP_PVT_THREADS */
+
+       free( map );
+       *pmap = NULL;
+       
+       return 0;
+}
+
+/* ldapmap.c */
+extern const rewrite_mapper rewrite_ldap_mapper;
+
+const rewrite_mapper *
+rewrite_mapper_find(
+       const char *name
+)
+{
+       int i;
+
+       if ( !strcasecmp( name, "ldap" ))
+               return &rewrite_ldap_mapper;
+
+       for (i=0; i<num_mappers; i++)
+               if ( !strcasecmp( name, mappers[i]->rm_name ))
+                       return mappers[i];
+       return NULL;
+}
+
+int
+rewrite_mapper_register(
+       const rewrite_mapper *map
+)
+{
+       if ( num_mappers % MAPPER_ALLOC == 0 ) {
+               const rewrite_mapper **mnew;
+               mnew = realloc( mappers, (num_mappers + MAPPER_ALLOC) *
+                       sizeof( rewrite_mapper * ));
+               if ( mnew )
+                       mappers = mnew;
+               else
+                       return -1;
+       }
+       mappers[num_mappers++] = map;
+       return 0;
+}
+
+int
+rewrite_mapper_unregister(
+       const rewrite_mapper *map
+)
+{
+       int i;
+
+       for (i = 0; i<num_mappers; i++) {
+               if ( mappers[i] == map ) {
+                       num_mappers--;
+                       mappers[i] = mappers[num_mappers];
+                       mappers[num_mappers] = NULL;
+                       return 0;
+               }
+       }
+       /* not found */
+       return -1;
+}