2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2002-2005 The OpenLDAP Foundation.
5 * Portions Copyright 1997,2002-2003 IBM Corporation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by IBM Corporation for use in
18 * IBM products and subsequently ported to OpenLDAP Software by
19 * Steve Omrani. Additional significant contributors include:
24 #include <ldap_pvt_thread.h>
30 * Note: if ltdl.h is not available, slapi should not be compiled
34 static int slapi_int_load_plugin( Slapi_PBlock *, const char *, const char *, int,
35 SLAPI_FUNC *, lt_dlhandle * );
37 /* pointer to link list of extended objects */
38 static ExtendedOp *pGExtendedOps = NULL;
40 /*********************************************************************
41 * Function Name: plugin_pblock_new
43 * Description: This routine creates a new Slapi_PBlock structure,
44 * loads in the plugin module and executes the init
45 * function provided by the module.
47 * Input: type - type of the plugin, such as SASL, database, etc.
48 * path - the loadpath to load the module in
49 * initfunc - name of the plugin function to execute first
50 * argc - number of arguements
51 * argv[] - an array of char pointers point to
52 * the arguments passed in via
53 * the configuration file.
57 * Return Values: a pointer to a newly created Slapi_PBlock structrue or
58 * NULL - function failed
61 *********************************************************************/
69 Slapi_PBlock *pPlugin = NULL;
70 Slapi_PluginDesc *pPluginDesc = NULL;
71 lt_dlhandle hdLoadHandle;
73 char **av2 = NULL, **ppPluginArgv;
75 char *initfunc = argv[3];
77 pPlugin = slapi_pblock_new();
78 if ( pPlugin == NULL ) {
83 slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
84 slapi_pblock_set( pPlugin, SLAPI_PLUGIN_ARGC, (void *)&argc );
86 av2 = ldap_charray_dup( argv );
93 ppPluginArgv = &av2[4];
98 slapi_pblock_set( pPlugin, SLAPI_PLUGIN_ARGV, (void *)ppPluginArgv );
99 slapi_pblock_set( pPlugin, SLAPI_X_CONFIG_ARGV, (void *)av2 );
101 rc = slapi_int_load_plugin( pPlugin, path, initfunc, 1, NULL, &hdLoadHandle );
106 if ( slapi_pblock_get( pPlugin, SLAPI_PLUGIN_DESCRIPTION, (void **)&pPluginDesc ) == 0 &&
107 pPluginDesc != NULL ) {
108 slapi_log_error(SLAPI_LOG_TRACE, "plugin_pblock_new",
109 "Registered plugin %s %s [%s] (%s)\n",
111 pPluginDesc->spd_version,
112 pPluginDesc->spd_vendor,
113 pPluginDesc->spd_description);
117 if ( rc != 0 && pPlugin != NULL ) {
118 slapi_pblock_destroy( pPlugin );
121 ldap_charray_free( av2 );
128 /*********************************************************************
129 * Function Name: slapi_int_register_plugin
131 * Description: insert the slapi_pblock structure to the end of the plugin
134 * Input: a pointer to a plugin slapi_pblock structure to be added to
139 * Return Values: LDAP_SUCCESS - successfully inserted.
143 *********************************************************************/
145 slapi_int_register_plugin(
149 Slapi_PBlock *pTmpPB;
150 Slapi_PBlock *pSavePB;
151 int rc = LDAP_SUCCESS;
153 assert( be != NULL );
155 pTmpPB = SLAPI_BACKEND_PBLOCK( be );
156 if ( pTmpPB == NULL ) {
157 SLAPI_BACKEND_PBLOCK( be ) = pPB;
159 while ( pTmpPB != NULL && rc == LDAP_SUCCESS ) {
161 rc = slapi_pblock_get( pTmpPB, SLAPI_IBM_PBLOCK, &pTmpPB );
164 if ( rc == LDAP_SUCCESS ) {
165 rc = slapi_pblock_set( pSavePB, SLAPI_IBM_PBLOCK, (void *)pPB );
169 return ( rc != LDAP_SUCCESS ) ? LDAP_OTHER : LDAP_SUCCESS;
172 /*********************************************************************
173 * Function Name: slapi_int_get_plugins
175 * Description: get the desired type of function pointers defined
178 * Input: the type of the functions to get, such as pre-operation,etc.
182 * Return Values: this routine returns a pointer to an array of function
183 * pointers containing backend-specific plugin functions
184 * followed by global plugin functions
187 *********************************************************************/
189 slapi_int_get_plugins(
192 SLAPI_FUNC **ppFuncPtrs )
195 Slapi_PBlock *pCurrentPB;
197 SLAPI_FUNC *pTmpFuncPtr;
199 int rc = LDAP_SUCCESS;
201 assert( ppFuncPtrs != NULL );
207 pCurrentPB = SLAPI_BACKEND_PBLOCK( be );
209 while ( pCurrentPB != NULL && rc == LDAP_SUCCESS ) {
210 rc = slapi_pblock_get( pCurrentPB, functype, &FuncPtr );
211 if ( rc == LDAP_SUCCESS ) {
212 if ( FuncPtr != NULL ) {
215 rc = slapi_pblock_get( pCurrentPB,
216 SLAPI_IBM_PBLOCK, &pCurrentPB );
227 * Now, build the function pointer array of backend-specific
228 * plugins followed by global plugins.
230 *ppFuncPtrs = pTmpFuncPtr =
231 (SLAPI_FUNC *)ch_malloc( ( numPB + 1 ) * sizeof(SLAPI_FUNC) );
232 if ( ppFuncPtrs == NULL ) {
237 pCurrentPB = SLAPI_BACKEND_PBLOCK( be );
239 while ( pCurrentPB != NULL && rc == LDAP_SUCCESS ) {
240 rc = slapi_pblock_get( pCurrentPB, functype, &FuncPtr );
241 if ( rc == LDAP_SUCCESS ) {
242 if ( FuncPtr != NULL ) {
243 *pTmpFuncPtr = FuncPtr;
246 rc = slapi_pblock_get( pCurrentPB,
247 SLAPI_IBM_PBLOCK, &pCurrentPB );
255 if ( rc != LDAP_SUCCESS && *ppFuncPtrs != NULL ) {
256 ch_free( *ppFuncPtrs );
263 /*********************************************************************
264 * Function Name: createExtendedOp
266 * Description: Creates an extended operation structure and
267 * initializes the fields
269 * Return value: A newly allocated structure or NULL
270 ********************************************************************/
276 ret = (ExtendedOp *)slapi_ch_malloc(sizeof(ExtendedOp));
277 ret->ext_oid.bv_val = NULL;
278 ret->ext_oid.bv_len = 0;
279 ret->ext_func = NULL;
281 ret->ext_next = NULL;
287 /*********************************************************************
288 * Function Name: slapi_int_unregister_extop
290 * Description: This routine removes the ExtendedOp structures
291 * asscoiated with a particular extended operation
294 * Input: pBE - pointer to a backend structure
295 * opList - pointer to a linked list of extended
296 * operation structures
297 * pPB - pointer to a slapi parameter block
304 *********************************************************************/
306 slapi_int_unregister_extop(
311 ExtendedOp *pTmpExtOp, *backExtOp;
316 assert( pBE != NULL); /* unused */
318 assert( opList != NULL );
319 assert( pPB != NULL );
321 if ( *opList == NULL ) {
325 slapi_pblock_get( pPB, SLAPI_PLUGIN_EXT_OP_OIDLIST, &pTmpOIDs );
326 if ( pTmpOIDs == NULL ) {
330 for ( i = 0; pTmpOIDs[i] != NULL; i++ ) {
333 for ( ; pTmpExtOp != NULL; pTmpExtOp = pTmpExtOp->ext_next) {
335 rc = strcasecmp( pTmpExtOp->ext_oid.bv_val,
338 if ( backExtOp == NULL ) {
339 *opList = pTmpExtOp->ext_next;
342 = pTmpExtOp->ext_next;
345 ch_free( pTmpExtOp );
348 backExtOp = pTmpExtOp;
354 /*********************************************************************
355 * Function Name: slapi_int_register_extop
357 * Description: This routine creates a new ExtendedOp structure, loads
358 * in the extended op module and put the extended op function address
359 * in the structure. The function will not be executed in
362 * Input: pBE - pointer to a backend structure
363 * opList - pointer to a linked list of extended
364 * operation structures
365 * pPB - pointer to a slapi parameter block
369 * Return Value: an LDAP return code
372 *********************************************************************/
374 slapi_int_register_extop(
379 ExtendedOp *pTmpExtOp = NULL;
385 if ( (*opList) == NULL ) {
386 *opList = createExtendedOp();
387 if ( (*opList) == NULL ) {
393 } else { /* Find the end of the list */
394 for ( pTmpExtOp = *opList; pTmpExtOp->ext_next != NULL;
395 pTmpExtOp = pTmpExtOp->ext_next )
397 pTmpExtOp->ext_next = createExtendedOp();
398 if ( pTmpExtOp->ext_next == NULL ) {
402 pTmpExtOp = pTmpExtOp->ext_next;
405 rc = slapi_pblock_get( pPB,SLAPI_PLUGIN_EXT_OP_OIDLIST, &pTmpOIDs );
411 rc = slapi_pblock_get(pPB,SLAPI_PLUGIN_EXT_OP_FN, &tmpFunc);
417 if ( (pTmpOIDs == NULL) || (tmpFunc == NULL) ) {
422 for ( i = 0; pTmpOIDs[i] != NULL; i++ ) {
423 pTmpExtOp->ext_oid.bv_val = pTmpOIDs[i];
424 pTmpExtOp->ext_oid.bv_len = strlen( pTmpOIDs[i] );
425 pTmpExtOp->ext_func = tmpFunc;
426 pTmpExtOp->ext_be = pBE;
427 if ( pTmpOIDs[i + 1] != NULL ) {
428 pTmpExtOp->ext_next = createExtendedOp();
429 if ( pTmpExtOp->ext_next == NULL ) {
433 pTmpExtOp = pTmpExtOp->ext_next;
441 /*********************************************************************
442 * Function Name: slapi_int_get_extop_plugin
444 * Description: This routine gets the function address for a given function
448 * funcName - name of the extended op function, ie. an OID.
450 * Output: pFuncAddr - the function address of the requested function name.
452 * Return Values: a pointer to a newly created ExtendOp structrue or
453 * NULL - function failed
456 *********************************************************************/
458 slapi_int_get_extop_plugin(
459 struct berval *reqoid,
460 SLAPI_FUNC *pFuncAddr )
462 ExtendedOp *pTmpExtOp;
464 assert( reqoid != NULL );
465 assert( pFuncAddr != NULL );
469 if ( pGExtendedOps == NULL ) {
473 pTmpExtOp = pGExtendedOps;
474 while ( pTmpExtOp != NULL ) {
477 rc = strcasecmp( reqoid->bv_val, pTmpExtOp->ext_oid.bv_val );
479 *pFuncAddr = pTmpExtOp->ext_func;
482 pTmpExtOp = pTmpExtOp->ext_next;
485 return ( *pFuncAddr == NULL ? 1 : 0 );
488 /***************************************************************************
489 * This function is similar to slapi_int_get_extop_plugin above. except it returns one OID
490 * per call. It is called from root_dse_info (root_dse.c).
491 * The function is a modified version of get_supported_extop (file extended.c).
492 ***************************************************************************/
494 slapi_int_get_supported_extop( int index )
498 for ( ext = pGExtendedOps ; ext != NULL && --index >= 0;
499 ext = ext->ext_next) {
507 return &ext->ext_oid ;
510 /*********************************************************************
511 * Function Name: slapi_int_load_plugin
513 * Description: This routine loads the specified DLL, gets and executes the init function
517 * pPlugin - a pointer to a Slapi_PBlock struct which will be passed to
518 * the DLL init function.
519 * path - path name of the DLL to be load.
520 * initfunc - either the DLL initialization function or an OID of the
521 * loaded extended operation.
522 * doInit - if it is TRUE, execute the init function, otherwise, save the
523 * function address but not execute it.
525 * Output: pInitFunc - the function address of the loaded function. This param
526 * should be not be null if doInit is FALSE.
527 * pLdHandle - handle returned by lt_dlopen()
529 * Return Values: LDAP_SUCCESS, LDAP_LOCAL_ERROR
532 *********************************************************************/
535 slapi_int_load_plugin(
536 Slapi_PBlock *pPlugin,
538 const char *initfunc,
540 SLAPI_FUNC *pInitFunc,
541 lt_dlhandle *pLdHandle )
543 int rc = LDAP_SUCCESS;
544 SLAPI_FUNC fpInitFunc = NULL;
546 assert( pLdHandle != NULL );
549 return LDAP_LOCAL_ERROR;
552 /* load in the module */
553 *pLdHandle = lt_dlopen( path );
554 if ( *pLdHandle == NULL ) {
555 fprintf( stderr, "failed to load plugin %s: %s\n",
556 path, lt_dlerror() );
557 return LDAP_LOCAL_ERROR;
560 fpInitFunc = (SLAPI_FUNC)lt_dlsym( *pLdHandle, initfunc );
561 if ( fpInitFunc == NULL ) {
562 fprintf( stderr, "failed to find symbol %s in plugin %s: %s\n",
563 initfunc, path, lt_dlerror() );
564 lt_dlclose( *pLdHandle );
565 return LDAP_LOCAL_ERROR;
569 rc = ( *fpInitFunc )( pPlugin );
570 if ( rc != LDAP_SUCCESS ) {
571 lt_dlclose( *pLdHandle );
575 *pInitFunc = fpInitFunc;
582 * Special support for computed attribute plugins
585 slapi_int_call_plugins(
592 SLAPI_FUNC *pGetPlugin = NULL, *tmpPlugin = NULL;
598 rc = slapi_int_get_plugins( be, funcType, &tmpPlugin );
599 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
600 /* Nothing to do, front-end should ignore. */
604 for ( pGetPlugin = tmpPlugin ; *pGetPlugin != NULL; pGetPlugin++ ) {
605 rc = (*pGetPlugin)(pPB);
608 * Only non-postoperation plugins abort processing on
609 * failure (confirmed with SLAPI specification).
611 if ( !SLAPI_PLUGIN_IS_POST_FN( funcType ) && rc != 0 ) {
613 * Plugins generally return negative error codes
614 * to indicate failure, although in the case of
615 * bind plugins they may return SLAPI_BIND_xxx
621 slapi_ch_free( (void **)&tmpPlugin );
627 slapi_int_read_config(
635 int numPluginArgc = 0;
639 "%s: line %d: missing arguments "
640 "in \"plugin <plugin_type> <lib_path> "
641 "<init_function> [<arguments>]\" line\n",
646 /* automatically instantiate overlay if necessary */
647 if ( !slapi_over_is_inst( be ) ) {
648 if ( slapi_over_config( be ) != 0 ) {
649 fprintf( stderr, "Failed to instantiate SLAPI overlay\n");
654 if ( strcasecmp( argv[1], "preoperation" ) == 0 ) {
655 iType = SLAPI_PLUGIN_PREOPERATION;
656 } else if ( strcasecmp( argv[1], "postoperation" ) == 0 ) {
657 iType = SLAPI_PLUGIN_POSTOPERATION;
658 } else if ( strcasecmp( argv[1], "extendedop" ) == 0 ) {
659 iType = SLAPI_PLUGIN_EXTENDEDOP;
660 } else if ( strcasecmp( argv[1], "object" ) == 0 ) {
661 iType = SLAPI_PLUGIN_OBJECT;
663 fprintf( stderr, "%s: line %d: invalid plugin type \"%s\".\n",
664 fname, lineno, argv[1] );
668 numPluginArgc = argc - 4;
670 if ( iType == SLAPI_PLUGIN_PREOPERATION ||
671 iType == SLAPI_PLUGIN_EXTENDEDOP ||
672 iType == SLAPI_PLUGIN_POSTOPERATION ||
673 iType == SLAPI_PLUGIN_OBJECT ) {
675 Slapi_PBlock *pPlugin;
677 pPlugin = plugin_pblock_new( iType, numPluginArgc, argv );
678 if (pPlugin == NULL) {
682 if (iType == SLAPI_PLUGIN_EXTENDEDOP) {
683 rc = slapi_int_register_extop(be, &pGExtendedOps, pPlugin);
684 if ( rc != LDAP_SUCCESS ) {
685 slapi_pblock_destroy( pPlugin );
690 rc = slapi_int_register_plugin( be, pPlugin );
691 if ( rc != LDAP_SUCCESS ) {
692 if ( iType == SLAPI_PLUGIN_EXTENDEDOP ) {
693 slapi_int_unregister_extop( be, &pGExtendedOps, pPlugin );
695 slapi_pblock_destroy( pPlugin );
704 slapi_int_plugin_unparse(
711 char **argv, ibuf[32], *ptr;
712 struct berval idx, bv;
718 for ( pp = SLAPI_BACKEND_PBLOCK( be );
720 slapi_pblock_get( pp, SLAPI_IBM_PBLOCK, &pp ) )
722 slapi_pblock_get( pp, SLAPI_X_CONFIG_ARGV, &argv );
723 if ( argv == NULL ) /* could be dynamic plugin */
725 idx.bv_len = sprintf( idx.bv_val, "{%d}", i );
726 bv.bv_len = idx.bv_len;
727 for (j=1; argv[j]; j++) {
728 bv.bv_len += strlen(argv[j]);
729 if ( j ) bv.bv_len++;
731 bv.bv_val = ch_malloc( bv.bv_len + 1 );
732 ptr = lutil_strcopy( bv.bv_val, ibuf );
733 for (j=1; argv[j]; j++) {
734 if ( j ) *ptr++ = ' ';
735 ptr = lutil_strcopy( ptr, argv[j] );
737 ber_bvarray_add( out, &bv );