From: Howard Chu Date: Fri, 12 Jan 2007 23:20:17 +0000 (+0000) Subject: Framework for loadable mapper implementations X-Git-Tag: OPENLDAP_REL_ENG_2_4_4ALPHA~8^2~189 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=26a47e7bd02a7097cbebac1a7da6a695d1985b41;p=openldap Framework for loadable mapper implementations --- diff --git a/include/rewrite.h b/include/rewrite.h index ebd3617002..4709b078db 100644 --- a/include/rewrite.h +++ b/include/rewrite.h @@ -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 */ diff --git a/libraries/librewrite/config.c b/libraries/librewrite/config.c index d23e53c90e..60fc6b9b52 100644 --- a/libraries/librewrite/config.c +++ b/libraries/librewrite/config.c @@ -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 ); /* diff --git a/libraries/librewrite/ldapmap.c b/libraries/librewrite/ldapmap.c index 882ba302f8..86c1f7582f 100644 --- a/libraries/librewrite/ldapmap.c +++ b/libraries/librewrite/ldapmap.c @@ -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 +}; + diff --git a/libraries/librewrite/map.c b/libraries/librewrite/map.c index 68bc44d60e..d1a98a55a8 100644 --- a/libraries/librewrite/map.c +++ b/libraries/librewrite/map.c @@ -28,6 +28,10 @@ #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; irm_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