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 */
/*
* 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 ) ) {
}
#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 );
/*
free( data );
}
-void *
+static void *
map_ldap_parse(
- struct rewrite_info *info,
const char *fname,
int lineno,
int argc,
struct ldap_map_data *data;
char *p, *uri;
- assert( info != NULL );
assert( fname != NULL );
assert( argv != NULL );
return ( void * )data;
}
-int
+static int
map_ldap_apply(
- struct rewrite_builtin_map *map,
+ void *private,
const char *filter,
struct berval *val
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 );
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
+};
+
#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,
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;
}
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 );
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;
+}
* 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;
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 */