Next, you need to write an initialization function for your server plug-in. Your initialization function should do the following:
Specify the plug-in version.
Specify information about the plug-in.
Register your plug-in functions.
Return a value to the Directory Server.
Your initialization function should have the following prototype:
int my_init_function( Slapi_PBlock pb );
Directory Server passes a single argument of type Slapi_PBlock when calling your initialization function.
If you are writing an initialization function for a plug-in on Windows NT, make sure to export the initialization functions. Use the following prototype:
__declspec( dllexport ) int my_init_function( Slapi_PBlock pb );
and specify the initialization function names in a .def file.
Note. You do not need to export all plug-in functions. Any plug-in functions that you
specify in the pblock (see "Registering Your Plug-In Functions" on page 36) do
not need to be exported. You only need to export the initialization functions
and any functions that are not registered by initialization functions (such as the
entry store and entry fetch plug-in functions, which are described in
Chapter 10, "Writing Entry Store/Fetch Plug-Ins").
Specifying the Plug-In Version
You need to specify the version of your plug-in so that the Directory Server can determine whether or not it supports the plug-in.
To specify the plug-in version, call the slapi_pblock_set() function, and set the SLAPI_PLUGIN_VERSION parameter to the version number. For example:
slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_02);
Plug-in version 1 (SLAPI_PLUGIN_VERSION_01) is compatible with versions 3.x and 4.x of the Netscape Directory Server. Plug-in version 2 (SLAPI_PLUGIN_VERSION_02) is compatible with version 4.x of the Netscape Directory Server.
Specifying Information about the Plug-In
You can specify information about your plug-in (such as a description of the plug-in) in an Slapi_PluginDesc structure. Call the slapi_pblock_set() function, and set the SLAPI_PLUGIN_DESCRIPTION parameter to this structure. For example:
/* Specify information about the plug-in */
Slapi_PluginDesc mypdesc = { "test-plugin", "Airius.com", "0.5", "sample
pre-operation plugin" };
...
/* Set this information in the parameter block */
slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION, (void *)&mypdesc );
In this example, the following information about the server plug-in is specified:
Registering Your Plug-In Functions
The way in which you register your plug-in function depends on the type of the plug-in function.
For some plug-in types, you do not need to register the plug-in function from within the initialization function. (For example, you register entry store and entry fetch plug-ins by specifying the function names in the plugin directive in the slapd.conf configuration file.)
For other plug-in types (such as pre-operation plug-ins, post-operation plug-ins, and database plug-ins), the Directory Server gets the pointer to your function from a parameter in the parameter block. You register plug-in functions by calling the slapi_pblock_set() function to specify the name of your plug-in function.
These parameters that you can use to specify plug-in functions are listed in "Parameters for Registering Plug-In Functions" on page 354. For example, if you want to register searchdn_preop_search() as a pre-operation search function, include the following code in your initialization function:
slapi_pblock_set( pb, SLAPI_PLUGIN_PRE_SEARCH_FN,
(void *) searchdn_preop_search )
SLAPI_PLUGIN_PRE_SEARCH_FN is the parameter that specifies the pre-operation plug-in function for the LDAP search operation.
Important. If you do not register your plug-in functions, the Directory Server will not call
your plug-in functions. Make sure to register your plug-in functions.
Note that you can register more than one plug-in function in your initialization function. (In other words, you do not need to define an initialization function for each plug-in function. You should, however, define a different initialization function for each type of plug-in.)
Returning a Value to the Directory Server
If the initialization function is successful, it should return 0. If an error occurs, it should return -1. If the initialization function returns -1, the Directory Server will exit.
Example of an Initialization Function
The following is an example of an initialization function that registers a pre-operation plug-in function searchdn_preop_search(). After the initialization function is done, the server will call searchdn_preop_search() before each LDAP search operation is executed.
#include "slapi-plugin.h"
...
#ifdef _WIN32
__declspec(dllexport)
#endif
searchdn_preop_init( Slapi_PBlock *pb )
{
/* Specify the version of the plug-in (set this to "01") */
if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
SLAPI_PLUGIN_VERSION_01 ) != 0 ||
/* Set up the server to call searchdn_preop_search() before
each LDAP search operation. */
slapi_pblock_set( pb, SLAPI_PLUGIN_PRE_SEARCH_FN,
(void *) searchdn_preop_search ) !=0 ) {
/* If a problem occurs, log an error message and
return -1. */
slapi_log_error( SLAPI_LOG_PLUGIN, "searchdn_preop_init",
"Error registering function.\n" );
return( -1 );
}
/* If the plug-in was successfully registered, log a
message and return 0. */
slapi_log_error( SLAPI_LOG_PLUGIN, "searchdn_preop_init",
"Plug-in successfully registered.\n" );
return( 0 );
}