2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2014 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>.
24 typedef int (*MODULE_INIT_FN)(
27 typedef int (*MODULE_LOAD_FN)(
29 const char *filename);
30 typedef int (*MODULE_TERM_FN)(void);
33 struct module_regtable_t {
36 } module_regtable[] = {
37 { "null", load_null_module },
38 #ifdef SLAPD_EXTERNAL_EXTENSIONS
39 { "extension", load_extop_module },
44 typedef struct module_loaded_t {
45 struct module_loaded_t *next;
50 module_loaded_t *module_list = NULL;
52 static int module_int_unload (module_loaded_t *module);
55 static char ebuf[BUFSIZ];
58 int module_init (void)
61 const char *error = lt_dlerror();
63 strcpy( ebuf, error );
67 Debug(LDAP_DEBUG_ANY, "lt_dlinit failed: %s\n", error, 0, 0);
72 return module_path( LDAP_MODULEDIR );
75 int module_kill (void)
77 /* unload all modules before shutdown */
78 while (module_list != NULL) {
79 module_int_unload(module_list);
83 const char *error = lt_dlerror();
85 strcpy( ebuf, error );
89 Debug(LDAP_DEBUG_ANY, "lt_dlexit failed: %s\n", error, 0, 0);
96 void * module_handle( const char *file_name )
98 module_loaded_t *module;
100 for ( module = module_list; module; module= module->next ) {
101 if ( !strcmp( module->name, file_name )) {
108 int module_unload( const char *file_name )
110 module_loaded_t *module;
112 module = module_handle( file_name );
114 module_int_unload( module );
117 return -1; /* not found */
120 int module_load(const char* file_name, int argc, char *argv[])
122 module_loaded_t *module;
125 MODULE_INIT_FN initialize;
129 #define file file_name
132 module = module_handle( file_name );
134 Debug( LDAP_DEBUG_ANY, "module_load: (%s) already loaded\n",
139 /* If loading a backend, see if we already have it */
140 if ( !strncasecmp( file_name, "back_", 5 )) {
141 char *name = (char *)file_name + 5;
142 char *dot = strchr( name, '.');
143 if (dot) *dot = '\0';
144 rc = backend_info( name ) != NULL;
147 Debug( LDAP_DEBUG_CONFIG, "module_load: (%s) already present (static)\n",
152 /* check for overlays too */
153 char *dot = strchr( file_name, '.' );
154 if ( dot ) *dot = '\0';
155 rc = overlay_find( file_name ) != NULL;
156 if ( dot ) *dot = '.';
158 Debug( LDAP_DEBUG_CONFIG, "module_load: (%s) already present (static)\n",
164 module = (module_loaded_t *)ch_calloc(1, sizeof(module_loaded_t) +
166 if (module == NULL) {
167 Debug(LDAP_DEBUG_ANY, "module_load failed: (%s) out of memory\n", file_name,
172 strcpy( module->name, file_name );
175 strcpy( file, file_name );
179 * The result of lt_dlerror(), when called, must be cached prior
180 * to calling Debug. This is because Debug is a macro that expands
181 * into multiple function calls.
183 if ((module->lib = lt_dlopenext(file)) == NULL) {
184 error = lt_dlerror();
186 strcpy( ebuf, error );
190 Debug(LDAP_DEBUG_ANY, "lt_dlopenext failed: (%s) %s\n", file_name,
197 Debug(LDAP_DEBUG_CONFIG, "loaded module %s\n", file_name, 0, 0);
201 #pragma convlit(suspend)
203 if ((initialize = lt_dlsym(module->lib, "init_module")) == NULL) {
205 #pragma convlit(resume)
207 Debug(LDAP_DEBUG_CONFIG, "module %s: no init_module() function found\n",
210 lt_dlclose(module->lib);
215 /* The imported init_module() routine passes back the type of
216 * module (i.e., which part of slapd it should be hooked into)
217 * or -1 for error. If it passes back 0, then you get the
218 * old behavior (i.e., the library is loaded and not hooked
221 * It might be better if the conf file could specify the type
222 * of module. That way, a single module could support multiple
223 * type of hooks. This could be done by using something like:
225 * moduleload extension /usr/local/openldap/whatever.so
227 * then we'd search through module_regtable for a matching
228 * module type, and hook in there.
230 rc = initialize(argc, argv);
232 Debug(LDAP_DEBUG_CONFIG, "module %s: init_module() failed\n",
235 lt_dlclose(module->lib);
240 if (rc >= (int)(sizeof(module_regtable) / sizeof(struct module_regtable_t))
241 || module_regtable[rc].proc == NULL)
243 Debug(LDAP_DEBUG_CONFIG, "module %s: unknown registration type (%d)\n",
246 module_int_unload(module);
250 rc = (module_regtable[rc].proc)(module, file_name);
252 Debug(LDAP_DEBUG_CONFIG, "module %s: %s module could not be registered\n",
253 file_name, module_regtable[rc].type, 0);
255 module_int_unload(module);
259 module->next = module_list;
260 module_list = module;
262 Debug(LDAP_DEBUG_CONFIG, "module %s: %s module registered\n",
263 file_name, module_regtable[rc].type, 0);
268 int module_path(const char *path)
275 return lt_dlsetsearchpath( path );
278 void *module_resolve (const void *module, const char *name)
285 if (module == NULL || name == NULL)
287 return(lt_dlsym(((module_loaded_t *)module)->lib, name));
290 static int module_int_unload (module_loaded_t *module)
292 module_loaded_t *mod;
293 MODULE_TERM_FN terminate;
295 if (module != NULL) {
296 /* remove module from tracking list */
297 if (module_list == module) {
298 module_list = module->next;
300 for (mod = module_list; mod; mod = mod->next) {
301 if (mod->next == module) {
302 mod->next = module->next;
308 /* call module's terminate routine, if present */
310 #pragma convlit(suspend)
312 if ((terminate = lt_dlsym(module->lib, "term_module"))) {
314 #pragma convlit(resume)
319 /* close the library and free the memory */
320 lt_dlclose(module->lib);
326 int load_null_module (const void *module, const char *file_name)
331 #ifdef SLAPD_EXTERNAL_EXTENSIONS
335 const char *file_name
338 SLAP_EXTOP_MAIN_FN *ext_main;
339 SLAP_EXTOP_GETOID_FN *ext_getoid;
343 ext_main = (SLAP_EXTOP_MAIN_FN *)module_resolve(module, "ext_main");
344 if (ext_main == NULL) {
348 ext_getoid = module_resolve(module, "ext_getoid");
349 if (ext_getoid == NULL) {
353 rc = (ext_getoid)(0, &oid, 256);
357 if (oid.bv_val == NULL || oid.bv_len == 0) {
361 /* FIXME: this is broken, and no longer needed,
362 * as a module can call load_extop() itself... */
363 rc = load_extop( &oid, ext_main );
366 #endif /* SLAPD_EXTERNAL_EXTENSIONS */
367 #endif /* SLAPD_MODULES */