2 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * (C) Copyright IBM Corp. 1997,2002
7 * Redistribution and use in source and binary forms are permitted
8 * provided that this notice is preserved and that due credit is
9 * given to IBM Corporation. This software is provided ``as is''
10 * without express or implied warranty.
14 #include "slapi_common.h"
15 #include <ldap_pvt_thread.h>
20 * Note: if ltdl.h is not available, slapi should not be compiled
24 static int loadPlugin( Slapi_PBlock *, const char *, const char *, int,
25 SLAPI_FUNC *, lt_dlhandle * );
27 /* pointer to link list of extended objects */
28 static ExtendedOp *pGExtendedOps = NULL;
30 /*********************************************************************
31 * Function Name: newPlugin
33 * Description: This routine creates a new Slapi_PBlock structure,
34 * loads in the plugin module and executes the init
35 * function provided by the module.
37 * Input: type - type of the plugin, such as SASL, database, etc.
38 * path - the loadpath to load the module in
39 * initfunc - name of the plugin function to execute first
40 * argc - number of arguements
41 * argv[] - an array of char pointers point to
42 * the arguments passed in via
43 * the configuration file.
47 * Return Values: a pointer to a newly created Slapi_PBlock structrue or
48 * NULL - function failed
51 *********************************************************************/
61 Slapi_PBlock *pPlugin = NULL;
62 lt_dlhandle hdLoadHandle;
65 pPlugin = slapi_pblock_new();
66 if ( pPlugin == NULL ) {
71 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)type );
72 if ( rc != LDAP_SUCCESS ) {
76 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_ARGC, (void *)argc );
77 if ( rc != LDAP_SUCCESS ) {
81 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_ARGV, (void *)argv );
82 if ( rc != LDAP_SUCCESS ) {
86 rc = loadPlugin( pPlugin, path, initfunc, TRUE, NULL, &hdLoadHandle );
89 if ( rc != LDAP_SUCCESS && pPlugin != NULL ) {
90 slapi_pblock_destroy( pPlugin );
97 /*********************************************************************
98 * Function Name: insertPlugin
100 * Description: insert the slapi_pblock structure to the end of the plugin
103 * Input: a pointer to a plugin slapi_pblock structure to be added to
108 * Return Values: LDAP_SUCCESS - successfully inserted.
112 *********************************************************************/
118 Slapi_PBlock *pTmpPB;
119 Slapi_PBlock *pSavePB;
120 int rc = LDAP_SUCCESS;
122 pTmpPB = (Slapi_PBlock *)(be->be_pb);
124 if ( pTmpPB == NULL ) {
125 be->be_pb = (void *)pPB;
127 while ( pTmpPB != NULL && rc == LDAP_SUCCESS ) {
129 rc = slapi_pblock_get( pTmpPB, SLAPI_IBM_PBLOCK,
131 if ( rc != LDAP_SUCCESS ) {
136 if ( rc == LDAP_SUCCESS ) {
137 rc = slapi_pblock_set( pSavePB, SLAPI_IBM_PBLOCK,
139 if ( rc != LDAP_SUCCESS ) {
148 /*********************************************************************
149 * Function Name: getAllPluginFuncs
151 * Description: get the desired type of function pointers defined
154 * Input: the type of the functions to get, such as pre-operation,etc.
158 * Return Values: this routine returns a pointer to an array of function
162 *********************************************************************/
167 SLAPI_FUNC **ppFuncPtrs )
170 Slapi_PBlock *pCurrentPB;
172 SLAPI_FUNC *pTmpFuncPtr;
174 int rc = LDAP_SUCCESS;
177 assert( ppFuncPtrs );
179 pCurrentPB = (Slapi_PBlock *)(be->be_pb);
181 if ( pCurrentPB == NULL ) {
183 * LDAP_OTHER is returned if no plugins are installed
189 while ( pCurrentPB != NULL && rc == LDAP_SUCCESS ) {
190 rc = slapi_pblock_get( pCurrentPB, functype, &FuncPtr );
191 if ( rc == LDAP_SUCCESS ) {
192 if ( FuncPtr != NULL ) {
195 rc = slapi_pblock_get( pCurrentPB,
196 SLAPI_IBM_PBLOCK, &pCurrentPB );
200 if ( rc != LDAP_SUCCESS ) {
210 *ppFuncPtrs = pTmpFuncPtr =
211 (SLAPI_FUNC *)ch_malloc( ( numPB + 1 ) * sizeof(SLAPI_FUNC) );
212 if ( ppFuncPtrs == NULL ) {
217 pCurrentPB = (Slapi_PBlock *)(be->be_pb);
218 while ( pCurrentPB != NULL && rc == LDAP_SUCCESS ) {
219 rc = slapi_pblock_get( pCurrentPB, functype, &FuncPtr );
220 if ( rc == LDAP_SUCCESS ) {
221 if ( FuncPtr != NULL ) {
222 *pTmpFuncPtr = FuncPtr;
225 rc = slapi_pblock_get( pCurrentPB,
226 SLAPI_IBM_PBLOCK, &pCurrentPB );
229 *pTmpFuncPtr = NULL ;
232 if ( rc != LDAP_SUCCESS && *ppFuncPtrs != NULL ) {
233 ch_free( *ppFuncPtrs );
240 /*********************************************************************
241 * Function Name: createExtendedOp
243 * Description: Creates an extended operation structure and
244 * initializes the fields
246 * Return value: A newly allocated structure or NULL
247 ********************************************************************/
253 ret = (ExtendedOp *)ch_malloc(sizeof(ExtendedOp));
255 ret->ext_oid.bv_val = NULL;
256 ret->ext_oid.bv_len = 0;
257 ret->ext_func = NULL;
259 ret->ext_next = NULL;
266 /*********************************************************************
267 * Function Name: removeExtendedOp
269 * Description: This routine removes the ExtendedOp structures
270 * asscoiated with a particular extended operation
273 * Input: pBE - pointer to a backend structure
274 * opList - pointer to a linked list of extended
275 * operation structures
276 * pPB - pointer to a slapi parameter block
283 *********************************************************************/
290 ExtendedOp *pTmpExtOp, *backExtOp;
295 assert( pBE != NULL); /* unused */
297 assert( opList != NULL );
298 assert( pPB != NULL );
300 if ( *opList == NULL ) {
304 slapi_pblock_get( pPB, SLAPI_PLUGIN_EXT_OP_OIDLIST, &pTmpOIDs );
305 if ( pTmpOIDs == NULL ) {
309 for ( i = 0; pTmpOIDs[i] != NULL; i++ ) {
312 for ( ; pTmpExtOp != NULL; pTmpExtOp = pTmpExtOp->ext_next) {
314 rc = strcasecmp( pTmpExtOp->ext_oid.bv_val,
317 if ( backExtOp == NULL ) {
318 *opList = pTmpExtOp->ext_next;
321 = pTmpExtOp->ext_next;
324 ch_free( pTmpExtOp );
327 backExtOp = pTmpExtOp;
333 /*********************************************************************
334 * Function Name: newExtendedOp
336 * Description: This routine creates a new ExtendedOp structure, loads
337 * in the extended op module and put the extended op function address
338 * in the structure. The function will not be executed in
341 * Input: pBE - pointer to a backend structure
342 * opList - pointer to a linked list of extended
343 * operation structures
344 * pPB - pointer to a slapi parameter block
348 * Return Value: an LDAP return code
351 *********************************************************************/
358 ExtendedOp *pTmpExtOp = NULL;
364 if ( (*opList) == NULL ) {
365 *opList = createExtendedOp();
366 if ( (*opList) == NULL ) {
372 } else { /* Find the end of the list */
373 for ( pTmpExtOp = *opList; pTmpExtOp->ext_next != NULL;
374 pTmpExtOp = pTmpExtOp->ext_next )
376 pTmpExtOp->ext_next = createExtendedOp();
377 if ( pTmpExtOp->ext_next == NULL ) {
381 pTmpExtOp = pTmpExtOp->ext_next;
384 rc = slapi_pblock_get( pPB,SLAPI_PLUGIN_EXT_OP_OIDLIST, &pTmpOIDs );
385 if ( rc != LDAP_SUCCESS ) {
390 rc = slapi_pblock_get(pPB,SLAPI_PLUGIN_EXT_OP_FN, &tmpFunc);
396 if ( (pTmpOIDs == NULL) || (tmpFunc == NULL) ) {
401 for ( i = 0; pTmpOIDs[i] != NULL; i++ ) {
402 pTmpExtOp->ext_oid.bv_val = pTmpOIDs[i];
403 pTmpExtOp->ext_oid.bv_len = strlen( pTmpOIDs[i] );
404 pTmpExtOp->ext_func = tmpFunc;
405 pTmpExtOp->ext_be = pBE;
406 if ( pTmpOIDs[i + 1] != NULL ) {
407 pTmpExtOp->ext_next = createExtendedOp();
408 if ( pTmpExtOp->ext_next == NULL ) {
412 pTmpExtOp = pTmpExtOp->ext_next;
420 /*********************************************************************
421 * Function Name: getPluginFunc
423 * Description: This routine gets the function address for a given function
427 * funcName - name of the extended op function, ie. an OID.
429 * Output: pFuncAddr - the function address of the requested function name.
431 * Return Values: a pointer to a newly created ExtendOp structrue or
432 * NULL - function failed
435 *********************************************************************/
438 struct berval *reqoid,
439 SLAPI_FUNC *pFuncAddr )
441 ExtendedOp *pTmpExtOp;
443 assert( reqoid != NULL );
444 assert( pFuncAddr != NULL );
448 if ( pGExtendedOps == NULL ) {
452 pTmpExtOp = pGExtendedOps;
453 while ( pTmpExtOp != NULL ) {
456 rc = strcasecmp( reqoid->bv_val, pTmpExtOp->ext_oid.bv_val );
458 *pFuncAddr = pTmpExtOp->ext_func;
461 pTmpExtOp = pTmpExtOp->ext_next;
464 return ( *pFuncAddr == NULL ? 1 : 0 );
467 /***************************************************************************
468 * This function is similar to getPluginFunc above. except it returns one OID
469 * per call. It is called from root_dse_info (root_dse.c).
470 * The function is a modified version of get_supported_extop (file extended.c).
471 ***************************************************************************/
473 ns_get_supported_extop( int index )
477 for ( ext = pGExtendedOps ; ext != NULL && --index >= 0;
478 ext = ext->ext_next) {
486 return &ext->ext_oid ;
489 /*********************************************************************
490 * Function Name: loadPlugin
492 * Description: This routine loads the specified DLL, gets and executes the init function
496 * pPlugin - a pointer to a Slapi_PBlock struct which will be passed to
497 * the DLL init function.
498 * path - path name of the DLL to be load.
499 * initfunc - either the DLL initialization function or an OID of the
500 * loaded extended operation.
501 * doInit - if it is TRUE, execute the init function, otherwise, save the
502 * function address but not execute it.
504 * Output: pInitFunc - the function address of the loaded function. This param
505 * should be not be null if doInit is FALSE.
506 * pLdHandle - handle returned by lt_dlopen()
508 * Return Values: LDAP_SUCCESS, LDAP_LOCAL_ERROR
511 *********************************************************************/
515 Slapi_PBlock *pPlugin,
517 const char *initfunc,
519 SLAPI_FUNC *pInitFunc,
520 lt_dlhandle *pLdHandle )
522 int rc = LDAP_SUCCESS;
523 SLAPI_FUNC fpInitFunc = NULL;
528 return LDAP_LOCAL_ERROR;
531 /* load in the module */
532 *pLdHandle = lt_dlopen( path );
533 if ( *pLdHandle == NULL ) {
534 return LDAP_LOCAL_ERROR;
537 fpInitFunc = (SLAPI_FUNC)lt_dlsym( *pLdHandle, initfunc );
538 if ( fpInitFunc == NULL ) {
539 lt_dlclose( *pLdHandle );
540 return LDAP_LOCAL_ERROR;
543 if ( doInit == TRUE ) {
544 rc = ( *fpInitFunc )( pPlugin );
545 if ( rc != LDAP_SUCCESS ) {
546 lt_dlclose( *pLdHandle );
550 *pInitFunc = fpInitFunc;
564 int rc = LDAP_SUCCESS;
565 SLAPI_FUNC *pGetPlugin = NULL, *tmpPlugin = NULL;
567 rc = getAllPluginFuncs(be, funcType, &tmpPlugin );
568 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
572 for ( pGetPlugin = tmpPlugin ; *pGetPlugin != NULL; pGetPlugin++ ) {
574 * FIXME: operation stops at first non-success
576 * FIXME: we should provide here a sort of sandbox,
577 * to protect from plugin faults; e.g. trap signals
578 * and longjump here, marking the plugin as unsafe for
579 * later executions ...
581 rc = (*pGetPlugin)(pPB);
583 if ( rc != LDAP_SUCCESS ) {
588 ch_free( tmpPlugin );
602 int numPluginArgc = 0;
603 char **ppPluginArgv = NULL;
607 "%s: line %d: missing arguments "
608 "in \"plugin <plugin_type> <lib_path> "
609 "<init_function> [<arguments>]\" line\n",
614 if ( strcasecmp( argv[1], "preoperation" ) == 0 ) {
615 iType = SLAPI_PLUGIN_PREOPERATION;
616 } else if ( strcasecmp( argv[1], "postoperation" ) == 0 ) {
617 iType = SLAPI_PLUGIN_POSTOPERATION;
618 } else if ( strcasecmp( argv[1], "extendedop" ) == 0 ) {
619 iType = SLAPI_PLUGIN_EXTENDEDOP;
621 fprintf( stderr, "%s: line %d: invalid plugin type \"%s\".\n",
622 fname, lineno, argv[1] );
626 numPluginArgc = argc - 4;
627 if ( numPluginArgc > 0 ) {
628 ppPluginArgv = &argv[4];
633 if ( iType == SLAPI_PLUGIN_PREOPERATION ||
634 iType == SLAPI_PLUGIN_EXTENDEDOP ||
635 iType == SLAPI_PLUGIN_POSTOPERATION ) {
637 Slapi_PBlock *pPlugin;
639 pPlugin = newPlugin( iType, argv[2], argv[3],
640 numPluginArgc, ppPluginArgv );
641 if (pPlugin == NULL) {
645 if (iType == SLAPI_PLUGIN_EXTENDEDOP) {
646 rc = newExtendedOp(be, &pGExtendedOps, pPlugin);
647 if ( rc != LDAP_SUCCESS ) {
648 slapi_pblock_destroy( pPlugin );
653 rc = insertPlugin( be, pPlugin );
654 if ( rc != LDAP_SUCCESS ) {
655 if ( iType == SLAPI_PLUGIN_EXTENDEDOP ) {
656 removeExtendedOp( be, &pGExtendedOps, pPlugin );
658 slapi_pblock_destroy( pPlugin );
669 if ( ldap_pvt_thread_mutex_init( &slapi_hn_mutex ) ) {
673 if ( ldap_pvt_thread_mutex_init( &slapi_time_mutex ) ) {
677 if ( ldap_pvt_thread_mutex_init( &slapi_printmessage_mutex ) ) {
681 slapi_log_file = ch_strdup( LDAP_RUNDIR LDAP_DIRSEP "errors" );
682 if ( slapi_log_file == NULL ) {