]> git.sur5r.net Git - openldap/commitdiff
Framework for loadable mapper implementations
authorHoward Chu <hyc@openldap.org>
Fri, 12 Jan 2007 23:20:17 +0000 (23:20 +0000)
committerHoward Chu <hyc@openldap.org>
Fri, 12 Jan 2007 23:20:17 +0000 (23:20 +0000)
include/rewrite.h
libraries/librewrite/config.c
libraries/librewrite/ldapmap.c
libraries/librewrite/map.c
libraries/librewrite/rewrite-int.h
libraries/librewrite/rewrite-map.h

index ebd361700230116c3a203a4ba82f0d5a358cd62f..4709b078dbd526a9bfab72c1a6adb7ab6a04221d 100644 (file)
@@ -253,6 +253,46 @@ rewrite_param_destroy(
                 struct rewrite_info *info
 );
 
+/*
+ * Mapping implementations
+ */
+
+struct rewrite_mapper;
+
+typedef void * (rewrite_mapper_config)(
+       const char *fname,
+       int lineno,
+       int argc,
+       char **argv );
+
+typedef int (rewrite_mapper_apply)(
+       void *ctx,
+       const char *arg,
+       struct berval *retval );
+
+typedef int (rewrite_mapper_destroy)(
+       void *ctx );
+
+typedef struct rewrite_mapper {
+       char *rm_name;
+       rewrite_mapper_config *rm_config;
+       rewrite_mapper_apply *rm_apply;
+       rewrite_mapper_destroy *rm_destroy;
+} rewrite_mapper;
+
+/* For dynamic loading and unloading of mappers */
+LDAP_REWRITE_F (int)
+rewrite_mapper_register(
+       const rewrite_mapper *map );
+
+LDAP_REWRITE_F (int)
+rewrite_mapper_unregister(
+       const rewrite_mapper *map );
+
+LDAP_REWRITE_F (const rewrite_mapper *)
+rewrite_mapper_find(
+       const char *name );
+
 LDAP_END_DECL
 
 #endif /* REWRITE_H */
index d23e53c90e6cc325ccfe266c97d7a22d555acb0f..60fc6b9b52a1aa2a242723ee1a3affd9866b217a 100644 (file)
@@ -413,8 +413,8 @@ rewrite_parse_builtin_map(
        /*
         * Built-in ldap map
         */
-       if ( strcasecmp( argv[ MAP_TYPE ], "ldap" ) == 0 ) {
-               map->lb_type = REWRITE_BUILTIN_MAP_LDAP;
+       if (( map->lb_mapper = rewrite_mapper_find( argv[ MAP_TYPE ] ))) {
+               map->lb_type = REWRITE_BUILTIN_MAP;
 
 #ifdef USE_REWRITE_LDAP_PVT_THREADS
                if ( ldap_pvt_thread_mutex_init( & map->lb_mutex ) ) {
@@ -424,7 +424,7 @@ rewrite_parse_builtin_map(
                }
 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
                
-               map->lb_private = map_ldap_parse( info, fname, lineno,
+               map->lb_private = map->lb_mapper->rm_config( fname, lineno,
                                argc - 3, argv + 3 );
                
        /* 
index 882ba302f88d9df60235d6b64cae575f311f5217..86c1f7582f76b891cf5bde006554843296588064 100644 (file)
@@ -85,9 +85,8 @@ map_ldap_free(
        free( data );
 }
 
-void *
+static void *
 map_ldap_parse(
-               struct rewrite_info *info,
                const char *fname,
                int lineno,
                int argc,
@@ -97,7 +96,6 @@ map_ldap_parse(
        struct ldap_map_data *data;
        char *p, *uri;
 
-       assert( info != NULL );
        assert( fname != NULL );
        assert( argv != NULL );
 
@@ -285,9 +283,9 @@ map_ldap_parse(
        return ( void * )data;
 }
 
-int
+static int
 map_ldap_apply(
-               struct rewrite_builtin_map *map,
+               void *private,
                const char *filter,
                struct berval *val
 
@@ -296,14 +294,12 @@ map_ldap_apply(
        LDAP *ld;
        LDAPMessage *res = NULL, *entry;
        int rc;
-       struct ldap_map_data *data = ( struct ldap_map_data * )map->lb_private;
+       struct ldap_map_data *data = private;
        LDAPURLDesc *lud = data->lm_lud;
        
        int first_try = 1, set_version = 0;
 
-       assert( map != NULL );
-       assert( map->lb_type == REWRITE_BUILTIN_MAP_LDAP );
-       assert( map->lb_private != NULL );
+       assert( private != NULL );
        assert( filter != NULL );
        assert( val != NULL );
 
@@ -435,22 +431,24 @@ rc_return:;
        return rc;
 }
 
-int
+static int
 map_ldap_destroy(
-               struct rewrite_builtin_map **pmap
+               void *private
 )
 {
-       struct ldap_map_data *data;
+       struct ldap_map_data *data = private;
 
-       assert( pmap != NULL );
-       assert( *pmap != NULL );
+       assert( private != NULL );
        
-       data = ( struct ldap_map_data * )(*pmap)->lb_private;
-
        map_ldap_free( data );
 
-       (*pmap)->lb_private = NULL;
-
        return 0;
 }
 
+const rewrite_mapper rewrite_ldap_mapper = {
+       "ldap",
+       map_ldap_parse,
+       map_ldap_apply,
+       map_ldap_destroy
+};
+
index 68bc44d60e101c4c827428ac1418ac5c57cad876..d1a98a55a871170b1386343c7caf268429921e16 100644 (file)
 #include "rewrite-int.h"
 #include "rewrite-map.h"
 
+static int num_mappers;
+static const rewrite_mapper **mappers;
+#define        MAPPER_ALLOC    8
+
 struct rewrite_map *
 rewrite_map_parse(
                struct rewrite_info *info,
@@ -417,14 +421,12 @@ 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;
        }
 
@@ -445,15 +447,8 @@ rewrite_builtin_map_free(
 
        assert( map != NULL );
 
-       switch ( map->lb_type ) {
-       case REWRITE_BUILTIN_MAP_LDAP:
-               map_ldap_destroy( &map );
-               break;
-
-       default:
-               assert(0);
-               break;
-       }
+       if ( map->lb_mapper && map->lb_mapper->rm_destroy )
+               map->lb_mapper->rm_destroy( map->lb_private );
 
        free( map->lb_name );
        free( map );
@@ -495,3 +490,58 @@ rewrite_map_destroy(
        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;
+}
index aee711dabc6c426e6807d2921e7734121ece377a..ca8873b06af622c219e893e0d8774ea8fc74773f 100644 (file)
@@ -157,10 +157,11 @@ struct rewrite_map {
  * Builtin maps
  */
 struct rewrite_builtin_map {
-#define REWRITE_BUILTIN_MAP_LDAP       0x0201
+#define REWRITE_BUILTIN_MAP    0x0200
        int                             lb_type;
        char                           *lb_name;
        void                           *lb_private;
+       const rewrite_mapper               *lb_mapper;
 
 #ifdef USE_REWRITE_LDAP_PVT_THREADS
        ldap_pvt_thread_mutex_t         lb_mutex;
index 0f0675c4f1c9e8d35e60ef395e0be557aaacc82d..44a061f379e827cf604c1c16d2f57d6d988f8921 100644 (file)
@@ -29,26 +29,4 @@ rewrite_builtin_map_find(
                 const char *name
 );
 
-
-/*
- * LDAP map
- */
-LDAP_REWRITE_F (void  *)
-map_ldap_parse(
-               struct rewrite_info *info,
-               const char *fname,
-               int lineno,
-               int argc,
-               char **argv
-);
-
-LDAP_REWRITE_F (int)
-map_ldap_apply( struct rewrite_builtin_map *map,
-               const char *filter,
-               struct berval *val
-);
-
-LDAP_REWRITE_F (int)
-map_ldap_destroy( struct rewrite_builtin_map **map );
-
 #endif /* MAP_H */