]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/slapi/plugin.c
use slapi_pblock_delete_param()
[openldap] / servers / slapd / slapi / plugin.c
index 23ff7151d691cffd2d758843cffbc0d06c78c64f..abdd63d6cdc2f27a2430ed2b329fb55f77795574 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2002-2003 The OpenLDAP Foundation.
+ * Copyright 2002-2005 The OpenLDAP Foundation.
  * Portions Copyright 1997,2002-2003 IBM Corporation.
  * All rights reserved.
  *
@@ -24,6 +24,7 @@
 #include <ldap_pvt_thread.h>
 #include <slap.h>
 #include <slapi.h>
+#include <lutil.h>
 
 /*
  * Note: if ltdl.h is not available, slapi should not be compiled
@@ -35,8 +36,6 @@ static int slapi_int_load_plugin( Slapi_PBlock *, const char *, const char *, in
 
 /* pointer to link list of extended objects */
 static ExtendedOp *pGExtendedOps = NULL;
-/* global plugins not associated with a specific backend */
-static Slapi_PBlock *pGPlugins = NULL;
 
 /*********************************************************************
  * Function Name:      plugin_pblock_new
@@ -64,8 +63,6 @@ static Slapi_PBlock *pGPlugins = NULL;
 static Slapi_PBlock *
 plugin_pblock_new(
        int type, 
-       const char *path, 
-       const char *initfunc, 
        int argc, 
        char *argv[] ) 
 {
@@ -73,6 +70,9 @@ plugin_pblock_new(
        Slapi_PluginDesc *pPluginDesc = NULL;
        lt_dlhandle     hdLoadHandle;
        int             rc;
+       char **av2 = NULL, **ppPluginArgv;
+       char *path = argv[2];
+       char *initfunc = argv[3];
 
        pPlugin = slapi_pblock_new();
        if ( pPlugin == NULL ) {
@@ -90,12 +90,28 @@ plugin_pblock_new(
                goto done;
        }
 
-       rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_ARGV, (void *)argv );
+       av2 = ldap_charray_dup( argv );
+       if ( av2 == NULL ) {
+               rc = LDAP_NO_MEMORY;
+               goto done;
+       }
+
+       if ( argc > 0 ) {
+               ppPluginArgv = &av2[4];
+       } else {
+               ppPluginArgv = NULL;
+       }
+       rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_ARGV, (void *)ppPluginArgv );
        if ( rc != 0 ) { 
                goto done;
        }
 
-       rc = slapi_int_load_plugin( pPlugin, path, initfunc, TRUE, NULL, &hdLoadHandle );
+       rc = slapi_pblock_set( pPlugin, SLAPI_X_CONFIG_ARGV, (void *)av2 );
+       if ( rc != 0 ) { 
+               goto done;
+       }
+
+       rc = slapi_int_load_plugin( pPlugin, path, initfunc, 1, NULL, &hdLoadHandle );
        if ( rc != 0 ) {
                goto done;
        }
@@ -114,6 +130,9 @@ done:
        if ( rc != 0 && pPlugin != NULL ) {
                slapi_pblock_destroy( pPlugin );
                pPlugin = NULL;
+               if ( av2 != NULL ) {
+                       ldap_charray_free( av2 );
+               }
        }
 
        return pPlugin;
@@ -144,33 +163,23 @@ slapi_int_register_plugin(
        Slapi_PBlock *pSavePB;
        int    rc = LDAP_SUCCESS;
 
-       pTmpPB = ( be == NULL ) ? pGPlugins : (Slapi_PBlock *)(be->be_pb);
+       assert( be != NULL );
 
+       pTmpPB = SLAPI_BACKEND_PBLOCK( be );
        if ( pTmpPB == NULL ) {
-               if ( be != NULL )
-                       be->be_pb = (void *)pPB;
-               else
-                       pGPlugins = pPB;
+               SLAPI_BACKEND_PBLOCK( be ) = pPB;
        } else {
                while ( pTmpPB != NULL && rc == LDAP_SUCCESS ) {
                        pSavePB = pTmpPB;
-                       rc = slapi_pblock_get( pTmpPB, SLAPI_IBM_PBLOCK,
-                                       &pTmpPB );
-                       if ( rc != LDAP_SUCCESS ) {
-                               rc = LDAP_OTHER;
-                       }
+                       rc = slapi_pblock_get( pTmpPB, SLAPI_IBM_PBLOCK, &pTmpPB );
                }
 
                if ( rc == LDAP_SUCCESS ) { 
-                       rc = slapi_pblock_set( pSavePB, SLAPI_IBM_PBLOCK,
-                                       (void *)pPB ); 
-                       if ( rc != LDAP_SUCCESS ) {
-                               rc = LDAP_OTHER;
-                       }
+                       rc = slapi_pblock_set( pSavePB, SLAPI_IBM_PBLOCK, (void *)pPB ); 
                }
        }
      
-       return rc;
+       return ( rc != LDAP_SUCCESS ) ? LDAP_OTHER : LDAP_SUCCESS;
 }
        
 /*********************************************************************
@@ -202,51 +211,25 @@ slapi_int_get_plugins(
        int             numPB = 0;
        int             rc = LDAP_SUCCESS;
 
-       assert( ppFuncPtrs );
+       assert( ppFuncPtrs != NULL );
 
-       /*
-        * First, count the plugins associated with a specific
-        * backend.
-        */
-       if ( be != NULL ) {
-               pCurrentPB = (Slapi_PBlock *)be->be_pb;
-
-               while ( pCurrentPB != NULL && rc == LDAP_SUCCESS ) {
-                       rc = slapi_pblock_get( pCurrentPB, functype, &FuncPtr );
-                       if ( rc == LDAP_SUCCESS ) {
-                               if ( FuncPtr != NULL )  {
-                                       numPB++;
-                               }
-                               rc = slapi_pblock_get( pCurrentPB,
-                                               SLAPI_IBM_PBLOCK, &pCurrentPB );
-                       }
-               }
-       }
-
-       if ( rc != LDAP_SUCCESS ) {
+       if ( be == NULL ) {
                goto done;
        }
 
-       /*
-        * Then, count the global plugins.
-        */
-       pCurrentPB = pGPlugins;
+       pCurrentPB = SLAPI_BACKEND_PBLOCK( be );
 
-       while  ( pCurrentPB != NULL && rc == LDAP_SUCCESS ) {
+       while ( pCurrentPB != NULL && rc == LDAP_SUCCESS ) {
                rc = slapi_pblock_get( pCurrentPB, functype, &FuncPtr );
                if ( rc == LDAP_SUCCESS ) {
                        if ( FuncPtr != NULL )  {
                                numPB++;
                        }
                        rc = slapi_pblock_get( pCurrentPB,
-                                       SLAPI_IBM_PBLOCK, &pCurrentPB );
+                               SLAPI_IBM_PBLOCK, &pCurrentPB );
                }
        }
 
-       if ( rc != LDAP_SUCCESS ) {
-               goto done;
-       }
-
        if ( numPB == 0 ) {
                *ppFuncPtrs = NULL;
                rc = LDAP_SUCCESS;
@@ -264,23 +247,7 @@ slapi_int_get_plugins(
                goto done;
        }
 
-       if ( be != NULL ) {
-               pCurrentPB = (Slapi_PBlock *)be->be_pb;
-
-               while ( pCurrentPB != NULL && rc == LDAP_SUCCESS )  {
-                       rc = slapi_pblock_get( pCurrentPB, functype, &FuncPtr );
-                       if ( rc == LDAP_SUCCESS ) {
-                               if ( FuncPtr != NULL )  {
-                                       *pTmpFuncPtr = FuncPtr;
-                                       pTmpFuncPtr++;
-                               } 
-                               rc = slapi_pblock_get( pCurrentPB,
-                                               SLAPI_IBM_PBLOCK, &pCurrentPB );
-                       }
-               }
-       }
-
-       pCurrentPB = pGPlugins;
+       pCurrentPB = SLAPI_BACKEND_PBLOCK( be );
 
        while ( pCurrentPB != NULL && rc == LDAP_SUCCESS )  {
                rc = slapi_pblock_get( pCurrentPB, functype, &FuncPtr );
@@ -293,7 +260,9 @@ slapi_int_get_plugins(
                                        SLAPI_IBM_PBLOCK, &pCurrentPB );
                }
        }
-       *pTmpFuncPtr = NULL ;
+
+       *pTmpFuncPtr = NULL;
+
 
 done:
        if ( rc != LDAP_SUCCESS && *ppFuncPtrs != NULL ) {
@@ -589,7 +558,7 @@ slapi_int_load_plugin(
        int             rc = LDAP_SUCCESS;
        SLAPI_FUNC      fpInitFunc = NULL;
 
-       assert( pLdHandle );
+       assert( pLdHandle != NULL );
 
        if ( lt_dlinit() ) {
                return LDAP_LOCAL_ERROR;
@@ -607,7 +576,7 @@ slapi_int_load_plugin(
                return LDAP_LOCAL_ERROR;
        }
 
-       if ( doInit == TRUE ) {
+       if ( doInit ) {
                rc = ( *fpInitFunc )( pPlugin );
                if ( rc != LDAP_SUCCESS ) {
                        lt_dlclose( *pLdHandle );
@@ -681,7 +650,6 @@ slapi_int_read_config(
 {
        int             iType = -1;
        int             numPluginArgc = 0;
-       char            **ppPluginArgv = NULL;
 
        if ( argc < 4 ) {
                fprintf( stderr,
@@ -691,6 +659,14 @@ slapi_int_read_config(
                        fname, lineno );
                return 1;
        }
+
+       /* automatically instantiate overlay if necessary */
+       if ( !overlay_is_inst( be, SLAPI_OVERLAY_NAME ) ) {
+               if ( overlay_config( be, SLAPI_OVERLAY_NAME ) != 0 ) {
+                       fprintf( stderr, "Failed to instantiate SLAPI overlay\n");
+                       return -1;
+               }
+       }
        
        if ( strcasecmp( argv[1], "preoperation" ) == 0 ) {
                iType = SLAPI_PLUGIN_PREOPERATION;
@@ -707,11 +683,6 @@ slapi_int_read_config(
        }
        
        numPluginArgc = argc - 4;
-       if ( numPluginArgc > 0 ) {
-               ppPluginArgv = &argv[4];
-       } else {
-               ppPluginArgv = NULL;
-       }
 
        if ( iType == SLAPI_PLUGIN_PREOPERATION ||
                        iType == SLAPI_PLUGIN_EXTENDEDOP ||
@@ -720,8 +691,7 @@ slapi_int_read_config(
                int rc;
                Slapi_PBlock *pPlugin;
 
-               pPlugin = plugin_pblock_new( iType, argv[2], argv[3], 
-                                       numPluginArgc, ppPluginArgv );
+               pPlugin = plugin_pblock_new( iType, numPluginArgc, argv );
                if (pPlugin == NULL) {
                        return 1;
                }
@@ -747,6 +717,44 @@ slapi_int_read_config(
        return 0;
 }
 
+void
+slapi_int_plugin_unparse(
+       Backend *be,
+       BerVarray *out
+)
+{
+       Slapi_PBlock *pp;
+       int i, j;
+       char **argv, ibuf[32], *ptr;
+       struct berval idx, bv;
+
+       *out = NULL;
+       idx.bv_val = ibuf;
+       i = 0;
+
+       for ( pp = SLAPI_BACKEND_PBLOCK( be );
+             pp != NULL;
+             slapi_pblock_get( pp, SLAPI_IBM_PBLOCK, &pp ) )
+       {
+               slapi_pblock_get( pp, SLAPI_X_CONFIG_ARGV, &argv );
+               if ( argv == NULL ) /* could be dynamic plugin */
+                       continue;
+               idx.bv_len = sprintf( idx.bv_val, "{%d}", i );
+               bv.bv_len = idx.bv_len;
+               for (j=1; argv[j]; j++) {
+                       bv.bv_len += strlen(argv[j]);
+                       if ( j ) bv.bv_len++;
+               }
+               bv.bv_val = ch_malloc( bv.bv_len + 1 );
+               ptr = lutil_strcopy( bv.bv_val, ibuf );
+               for (j=1; argv[j]; j++) {
+                       if ( j ) *ptr++ = ' ';
+                       ptr = lutil_strcopy( ptr, argv[j] );
+               }
+               ber_bvarray_add( out, &bv );
+       }
+}
+
 int
 slapi_int_initialize(void)
 {
@@ -771,6 +779,5 @@ slapi_int_initialize(void)
                return -1;
        }
 
-       return 0;
+       return slapi_int_overlay_init();
 }
-