plugin.c revision 1.1.1.4 1 1.1.1.3 lukem /* $NetBSD: plugin.c,v 1.1.1.4 2010/12/12 15:23:51 adam Exp $ */
2 1.1.1.3 lukem
3 1.1.1.4 adam /* OpenLDAP: pkg/ldap/servers/slapd/slapi/plugin.c,v 1.43.2.8 2010/04/13 20:23:50 kurt Exp */
4 1.1 lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 1.1 lukem *
6 1.1.1.4 adam * Copyright 2002-2010 The OpenLDAP Foundation.
7 1.1 lukem * Portions Copyright 1997,2002-2003 IBM Corporation.
8 1.1 lukem * All rights reserved.
9 1.1 lukem *
10 1.1 lukem * Redistribution and use in source and binary forms, with or without
11 1.1 lukem * modification, are permitted only as authorized by the OpenLDAP
12 1.1 lukem * Public License.
13 1.1 lukem *
14 1.1 lukem * A copy of this license is available in the file LICENSE in the
15 1.1 lukem * top-level directory of the distribution or, alternatively, at
16 1.1 lukem * <http://www.OpenLDAP.org/license.html>.
17 1.1 lukem */
18 1.1 lukem /* ACKNOWLEDGEMENTS:
19 1.1 lukem * This work was initially developed by IBM Corporation for use in
20 1.1 lukem * IBM products and subsequently ported to OpenLDAP Software by
21 1.1 lukem * Steve Omrani. Additional significant contributors include:
22 1.1 lukem * Luke Howard
23 1.1 lukem */
24 1.1 lukem
25 1.1 lukem #include "portable.h"
26 1.1.1.2 lukem #include "ldap_pvt_thread.h"
27 1.1.1.2 lukem #include "slap.h"
28 1.1.1.2 lukem #include "config.h"
29 1.1.1.2 lukem #include "slapi.h"
30 1.1.1.2 lukem #include "lutil.h"
31 1.1 lukem
32 1.1 lukem /*
33 1.1 lukem * Note: if ltdl.h is not available, slapi should not be compiled
34 1.1 lukem */
35 1.1 lukem #include <ltdl.h>
36 1.1 lukem
37 1.1 lukem static int slapi_int_load_plugin( Slapi_PBlock *, const char *, const char *, int,
38 1.1 lukem SLAPI_FUNC *, lt_dlhandle * );
39 1.1 lukem
40 1.1 lukem /* pointer to link list of extended objects */
41 1.1 lukem static ExtendedOp *pGExtendedOps = NULL;
42 1.1 lukem
43 1.1 lukem /*********************************************************************
44 1.1 lukem * Function Name: plugin_pblock_new
45 1.1 lukem *
46 1.1 lukem * Description: This routine creates a new Slapi_PBlock structure,
47 1.1 lukem * loads in the plugin module and executes the init
48 1.1 lukem * function provided by the module.
49 1.1 lukem *
50 1.1 lukem * Input: type - type of the plugin, such as SASL, database, etc.
51 1.1 lukem * path - the loadpath to load the module in
52 1.1 lukem * initfunc - name of the plugin function to execute first
53 1.1 lukem * argc - number of arguements
54 1.1 lukem * argv[] - an array of char pointers point to
55 1.1 lukem * the arguments passed in via
56 1.1 lukem * the configuration file.
57 1.1 lukem *
58 1.1 lukem * Output:
59 1.1 lukem *
60 1.1 lukem * Return Values: a pointer to a newly created Slapi_PBlock structrue or
61 1.1 lukem * NULL - function failed
62 1.1 lukem *
63 1.1 lukem * Messages: None
64 1.1 lukem *********************************************************************/
65 1.1 lukem
66 1.1 lukem static Slapi_PBlock *
67 1.1 lukem plugin_pblock_new(
68 1.1 lukem int type,
69 1.1 lukem int argc,
70 1.1 lukem char *argv[] )
71 1.1 lukem {
72 1.1 lukem Slapi_PBlock *pPlugin = NULL;
73 1.1 lukem Slapi_PluginDesc *pPluginDesc = NULL;
74 1.1 lukem lt_dlhandle hdLoadHandle;
75 1.1 lukem int rc;
76 1.1 lukem char **av2 = NULL, **ppPluginArgv;
77 1.1 lukem char *path = argv[2];
78 1.1 lukem char *initfunc = argv[3];
79 1.1 lukem
80 1.1 lukem pPlugin = slapi_pblock_new();
81 1.1 lukem if ( pPlugin == NULL ) {
82 1.1 lukem rc = LDAP_NO_MEMORY;
83 1.1 lukem goto done;
84 1.1 lukem }
85 1.1 lukem
86 1.1 lukem slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
87 1.1 lukem slapi_pblock_set( pPlugin, SLAPI_PLUGIN_ARGC, (void *)&argc );
88 1.1 lukem
89 1.1 lukem av2 = ldap_charray_dup( argv );
90 1.1 lukem if ( av2 == NULL ) {
91 1.1 lukem rc = LDAP_NO_MEMORY;
92 1.1 lukem goto done;
93 1.1 lukem }
94 1.1 lukem
95 1.1 lukem if ( argc > 0 ) {
96 1.1 lukem ppPluginArgv = &av2[4];
97 1.1 lukem } else {
98 1.1 lukem ppPluginArgv = NULL;
99 1.1 lukem }
100 1.1 lukem
101 1.1 lukem slapi_pblock_set( pPlugin, SLAPI_PLUGIN_ARGV, (void *)ppPluginArgv );
102 1.1 lukem slapi_pblock_set( pPlugin, SLAPI_X_CONFIG_ARGV, (void *)av2 );
103 1.1 lukem
104 1.1 lukem rc = slapi_int_load_plugin( pPlugin, path, initfunc, 1, NULL, &hdLoadHandle );
105 1.1 lukem if ( rc != 0 ) {
106 1.1 lukem goto done;
107 1.1 lukem }
108 1.1 lukem
109 1.1 lukem if ( slapi_pblock_get( pPlugin, SLAPI_PLUGIN_DESCRIPTION, (void **)&pPluginDesc ) == 0 &&
110 1.1 lukem pPluginDesc != NULL ) {
111 1.1 lukem slapi_log_error(SLAPI_LOG_TRACE, "plugin_pblock_new",
112 1.1 lukem "Registered plugin %s %s [%s] (%s)\n",
113 1.1 lukem pPluginDesc->spd_id,
114 1.1 lukem pPluginDesc->spd_version,
115 1.1 lukem pPluginDesc->spd_vendor,
116 1.1 lukem pPluginDesc->spd_description);
117 1.1 lukem }
118 1.1 lukem
119 1.1 lukem done:
120 1.1 lukem if ( rc != 0 && pPlugin != NULL ) {
121 1.1 lukem slapi_pblock_destroy( pPlugin );
122 1.1 lukem pPlugin = NULL;
123 1.1 lukem if ( av2 != NULL ) {
124 1.1 lukem ldap_charray_free( av2 );
125 1.1 lukem }
126 1.1 lukem }
127 1.1 lukem
128 1.1 lukem return pPlugin;
129 1.1 lukem }
130 1.1 lukem
131 1.1 lukem /*********************************************************************
132 1.1 lukem * Function Name: slapi_int_register_plugin
133 1.1 lukem *
134 1.1 lukem * Description: insert the slapi_pblock structure to the end of the plugin
135 1.1 lukem * list
136 1.1 lukem *
137 1.1 lukem * Input: a pointer to a plugin slapi_pblock structure to be added to
138 1.1 lukem * the list
139 1.1 lukem *
140 1.1 lukem * Output: none
141 1.1 lukem *
142 1.1 lukem * Return Values: LDAP_SUCCESS - successfully inserted.
143 1.1 lukem * LDAP_LOCAL_ERROR.
144 1.1 lukem *
145 1.1 lukem * Messages: None
146 1.1 lukem *********************************************************************/
147 1.1 lukem int
148 1.1 lukem slapi_int_register_plugin(
149 1.1 lukem Backend *be,
150 1.1 lukem Slapi_PBlock *pPB )
151 1.1 lukem {
152 1.1 lukem Slapi_PBlock *pTmpPB;
153 1.1 lukem Slapi_PBlock *pSavePB;
154 1.1 lukem int rc = LDAP_SUCCESS;
155 1.1 lukem
156 1.1 lukem assert( be != NULL );
157 1.1 lukem
158 1.1 lukem pTmpPB = SLAPI_BACKEND_PBLOCK( be );
159 1.1 lukem if ( pTmpPB == NULL ) {
160 1.1 lukem SLAPI_BACKEND_PBLOCK( be ) = pPB;
161 1.1 lukem } else {
162 1.1 lukem while ( pTmpPB != NULL && rc == LDAP_SUCCESS ) {
163 1.1 lukem pSavePB = pTmpPB;
164 1.1 lukem rc = slapi_pblock_get( pTmpPB, SLAPI_IBM_PBLOCK, &pTmpPB );
165 1.1 lukem }
166 1.1 lukem
167 1.1 lukem if ( rc == LDAP_SUCCESS ) {
168 1.1 lukem rc = slapi_pblock_set( pSavePB, SLAPI_IBM_PBLOCK, (void *)pPB );
169 1.1 lukem }
170 1.1 lukem }
171 1.1 lukem
172 1.1 lukem return ( rc != LDAP_SUCCESS ) ? LDAP_OTHER : LDAP_SUCCESS;
173 1.1 lukem }
174 1.1 lukem
175 1.1 lukem /*********************************************************************
176 1.1 lukem * Function Name: slapi_int_get_plugins
177 1.1 lukem *
178 1.1 lukem * Description: get the desired type of function pointers defined
179 1.1 lukem * in all the plugins
180 1.1 lukem *
181 1.1 lukem * Input: the type of the functions to get, such as pre-operation,etc.
182 1.1 lukem *
183 1.1 lukem * Output: none
184 1.1 lukem *
185 1.1 lukem * Return Values: this routine returns a pointer to an array of function
186 1.1 lukem * pointers containing backend-specific plugin functions
187 1.1 lukem * followed by global plugin functions
188 1.1 lukem *
189 1.1 lukem * Messages: None
190 1.1 lukem *********************************************************************/
191 1.1 lukem int
192 1.1 lukem slapi_int_get_plugins(
193 1.1 lukem Backend *be,
194 1.1 lukem int functype,
195 1.1 lukem SLAPI_FUNC **ppFuncPtrs )
196 1.1 lukem {
197 1.1 lukem
198 1.1 lukem Slapi_PBlock *pCurrentPB;
199 1.1 lukem SLAPI_FUNC FuncPtr;
200 1.1 lukem SLAPI_FUNC *pTmpFuncPtr;
201 1.1 lukem int numPB = 0;
202 1.1 lukem int rc = LDAP_SUCCESS;
203 1.1 lukem
204 1.1 lukem assert( ppFuncPtrs != NULL );
205 1.1 lukem
206 1.1 lukem if ( be == NULL ) {
207 1.1 lukem goto done;
208 1.1 lukem }
209 1.1 lukem
210 1.1 lukem pCurrentPB = SLAPI_BACKEND_PBLOCK( be );
211 1.1 lukem
212 1.1 lukem while ( pCurrentPB != NULL && rc == LDAP_SUCCESS ) {
213 1.1 lukem rc = slapi_pblock_get( pCurrentPB, functype, &FuncPtr );
214 1.1 lukem if ( rc == LDAP_SUCCESS ) {
215 1.1 lukem if ( FuncPtr != NULL ) {
216 1.1 lukem numPB++;
217 1.1 lukem }
218 1.1 lukem rc = slapi_pblock_get( pCurrentPB,
219 1.1 lukem SLAPI_IBM_PBLOCK, &pCurrentPB );
220 1.1 lukem }
221 1.1 lukem }
222 1.1 lukem
223 1.1 lukem if ( numPB == 0 ) {
224 1.1 lukem *ppFuncPtrs = NULL;
225 1.1 lukem rc = LDAP_SUCCESS;
226 1.1 lukem goto done;
227 1.1 lukem }
228 1.1 lukem
229 1.1 lukem /*
230 1.1 lukem * Now, build the function pointer array of backend-specific
231 1.1 lukem * plugins followed by global plugins.
232 1.1 lukem */
233 1.1 lukem *ppFuncPtrs = pTmpFuncPtr =
234 1.1 lukem (SLAPI_FUNC *)ch_malloc( ( numPB + 1 ) * sizeof(SLAPI_FUNC) );
235 1.1 lukem if ( ppFuncPtrs == NULL ) {
236 1.1 lukem rc = LDAP_NO_MEMORY;
237 1.1 lukem goto done;
238 1.1 lukem }
239 1.1 lukem
240 1.1 lukem pCurrentPB = SLAPI_BACKEND_PBLOCK( be );
241 1.1 lukem
242 1.1 lukem while ( pCurrentPB != NULL && rc == LDAP_SUCCESS ) {
243 1.1 lukem rc = slapi_pblock_get( pCurrentPB, functype, &FuncPtr );
244 1.1 lukem if ( rc == LDAP_SUCCESS ) {
245 1.1 lukem if ( FuncPtr != NULL ) {
246 1.1 lukem *pTmpFuncPtr = FuncPtr;
247 1.1 lukem pTmpFuncPtr++;
248 1.1 lukem }
249 1.1 lukem rc = slapi_pblock_get( pCurrentPB,
250 1.1 lukem SLAPI_IBM_PBLOCK, &pCurrentPB );
251 1.1 lukem }
252 1.1 lukem }
253 1.1 lukem
254 1.1 lukem *pTmpFuncPtr = NULL;
255 1.1 lukem
256 1.1 lukem
257 1.1 lukem done:
258 1.1 lukem if ( rc != LDAP_SUCCESS && *ppFuncPtrs != NULL ) {
259 1.1 lukem ch_free( *ppFuncPtrs );
260 1.1 lukem *ppFuncPtrs = NULL;
261 1.1 lukem }
262 1.1 lukem
263 1.1 lukem return rc;
264 1.1 lukem }
265 1.1 lukem
266 1.1 lukem /*********************************************************************
267 1.1 lukem * Function Name: createExtendedOp
268 1.1 lukem *
269 1.1 lukem * Description: Creates an extended operation structure and
270 1.1 lukem * initializes the fields
271 1.1 lukem *
272 1.1 lukem * Return value: A newly allocated structure or NULL
273 1.1 lukem ********************************************************************/
274 1.1 lukem ExtendedOp *
275 1.1 lukem createExtendedOp()
276 1.1 lukem {
277 1.1 lukem ExtendedOp *ret;
278 1.1 lukem
279 1.1 lukem ret = (ExtendedOp *)slapi_ch_malloc(sizeof(ExtendedOp));
280 1.1 lukem ret->ext_oid.bv_val = NULL;
281 1.1 lukem ret->ext_oid.bv_len = 0;
282 1.1 lukem ret->ext_func = NULL;
283 1.1 lukem ret->ext_be = NULL;
284 1.1 lukem ret->ext_next = NULL;
285 1.1 lukem
286 1.1 lukem return ret;
287 1.1 lukem }
288 1.1 lukem
289 1.1 lukem
290 1.1 lukem /*********************************************************************
291 1.1 lukem * Function Name: slapi_int_unregister_extop
292 1.1 lukem *
293 1.1 lukem * Description: This routine removes the ExtendedOp structures
294 1.1 lukem * asscoiated with a particular extended operation
295 1.1 lukem * plugin.
296 1.1 lukem *
297 1.1 lukem * Input: pBE - pointer to a backend structure
298 1.1 lukem * opList - pointer to a linked list of extended
299 1.1 lukem * operation structures
300 1.1 lukem * pPB - pointer to a slapi parameter block
301 1.1 lukem *
302 1.1 lukem * Output:
303 1.1 lukem *
304 1.1 lukem * Return Value: none
305 1.1 lukem *
306 1.1 lukem * Messages: None
307 1.1 lukem *********************************************************************/
308 1.1 lukem void
309 1.1 lukem slapi_int_unregister_extop(
310 1.1 lukem Backend *pBE,
311 1.1 lukem ExtendedOp **opList,
312 1.1 lukem Slapi_PBlock *pPB )
313 1.1 lukem {
314 1.1 lukem ExtendedOp *pTmpExtOp, *backExtOp;
315 1.1 lukem char **pTmpOIDs;
316 1.1 lukem int i;
317 1.1 lukem
318 1.1 lukem #if 0
319 1.1 lukem assert( pBE != NULL); /* unused */
320 1.1 lukem #endif /* 0 */
321 1.1 lukem assert( opList != NULL );
322 1.1 lukem assert( pPB != NULL );
323 1.1 lukem
324 1.1 lukem if ( *opList == NULL ) {
325 1.1 lukem return;
326 1.1 lukem }
327 1.1 lukem
328 1.1 lukem slapi_pblock_get( pPB, SLAPI_PLUGIN_EXT_OP_OIDLIST, &pTmpOIDs );
329 1.1 lukem if ( pTmpOIDs == NULL ) {
330 1.1 lukem return;
331 1.1 lukem }
332 1.1 lukem
333 1.1 lukem for ( i = 0; pTmpOIDs[i] != NULL; i++ ) {
334 1.1 lukem backExtOp = NULL;
335 1.1 lukem pTmpExtOp = *opList;
336 1.1 lukem for ( ; pTmpExtOp != NULL; pTmpExtOp = pTmpExtOp->ext_next) {
337 1.1 lukem int rc;
338 1.1 lukem rc = strcasecmp( pTmpExtOp->ext_oid.bv_val,
339 1.1 lukem pTmpOIDs[ i ] );
340 1.1 lukem if ( rc == 0 ) {
341 1.1 lukem if ( backExtOp == NULL ) {
342 1.1 lukem *opList = pTmpExtOp->ext_next;
343 1.1 lukem } else {
344 1.1 lukem backExtOp->ext_next
345 1.1 lukem = pTmpExtOp->ext_next;
346 1.1 lukem }
347 1.1 lukem
348 1.1 lukem ch_free( pTmpExtOp );
349 1.1 lukem break;
350 1.1 lukem }
351 1.1 lukem backExtOp = pTmpExtOp;
352 1.1 lukem }
353 1.1 lukem }
354 1.1 lukem }
355 1.1 lukem
356 1.1 lukem
357 1.1 lukem /*********************************************************************
358 1.1 lukem * Function Name: slapi_int_register_extop
359 1.1 lukem *
360 1.1 lukem * Description: This routine creates a new ExtendedOp structure, loads
361 1.1 lukem * in the extended op module and put the extended op function address
362 1.1 lukem * in the structure. The function will not be executed in
363 1.1 lukem * this routine.
364 1.1 lukem *
365 1.1 lukem * Input: pBE - pointer to a backend structure
366 1.1 lukem * opList - pointer to a linked list of extended
367 1.1 lukem * operation structures
368 1.1 lukem * pPB - pointer to a slapi parameter block
369 1.1 lukem *
370 1.1 lukem * Output:
371 1.1 lukem *
372 1.1 lukem * Return Value: an LDAP return code
373 1.1 lukem *
374 1.1 lukem * Messages: None
375 1.1 lukem *********************************************************************/
376 1.1 lukem int
377 1.1 lukem slapi_int_register_extop(
378 1.1 lukem Backend *pBE,
379 1.1 lukem ExtendedOp **opList,
380 1.1 lukem Slapi_PBlock *pPB )
381 1.1 lukem {
382 1.1 lukem ExtendedOp *pTmpExtOp = NULL;
383 1.1 lukem SLAPI_FUNC tmpFunc;
384 1.1 lukem char **pTmpOIDs;
385 1.1 lukem int rc = LDAP_OTHER;
386 1.1 lukem int i;
387 1.1 lukem
388 1.1 lukem if ( (*opList) == NULL ) {
389 1.1 lukem *opList = createExtendedOp();
390 1.1 lukem if ( (*opList) == NULL ) {
391 1.1 lukem rc = LDAP_NO_MEMORY;
392 1.1 lukem goto error_return;
393 1.1 lukem }
394 1.1 lukem pTmpExtOp = *opList;
395 1.1 lukem
396 1.1 lukem } else { /* Find the end of the list */
397 1.1 lukem for ( pTmpExtOp = *opList; pTmpExtOp->ext_next != NULL;
398 1.1 lukem pTmpExtOp = pTmpExtOp->ext_next )
399 1.1 lukem ; /* EMPTY */
400 1.1 lukem pTmpExtOp->ext_next = createExtendedOp();
401 1.1 lukem if ( pTmpExtOp->ext_next == NULL ) {
402 1.1 lukem rc = LDAP_NO_MEMORY;
403 1.1 lukem goto error_return;
404 1.1 lukem }
405 1.1 lukem pTmpExtOp = pTmpExtOp->ext_next;
406 1.1 lukem }
407 1.1 lukem
408 1.1 lukem rc = slapi_pblock_get( pPB,SLAPI_PLUGIN_EXT_OP_OIDLIST, &pTmpOIDs );
409 1.1 lukem if ( rc != 0 ) {
410 1.1 lukem rc = LDAP_OTHER;
411 1.1 lukem goto error_return;
412 1.1 lukem }
413 1.1 lukem
414 1.1 lukem rc = slapi_pblock_get(pPB,SLAPI_PLUGIN_EXT_OP_FN, &tmpFunc);
415 1.1 lukem if ( rc != 0 ) {
416 1.1 lukem rc = LDAP_OTHER;
417 1.1 lukem goto error_return;
418 1.1 lukem }
419 1.1 lukem
420 1.1 lukem if ( (pTmpOIDs == NULL) || (tmpFunc == NULL) ) {
421 1.1 lukem rc = LDAP_OTHER;
422 1.1 lukem goto error_return;
423 1.1 lukem }
424 1.1 lukem
425 1.1 lukem for ( i = 0; pTmpOIDs[i] != NULL; i++ ) {
426 1.1 lukem pTmpExtOp->ext_oid.bv_val = pTmpOIDs[i];
427 1.1 lukem pTmpExtOp->ext_oid.bv_len = strlen( pTmpOIDs[i] );
428 1.1 lukem pTmpExtOp->ext_func = tmpFunc;
429 1.1 lukem pTmpExtOp->ext_be = pBE;
430 1.1 lukem if ( pTmpOIDs[i + 1] != NULL ) {
431 1.1 lukem pTmpExtOp->ext_next = createExtendedOp();
432 1.1 lukem if ( pTmpExtOp->ext_next == NULL ) {
433 1.1 lukem rc = LDAP_NO_MEMORY;
434 1.1 lukem break;
435 1.1 lukem }
436 1.1 lukem pTmpExtOp = pTmpExtOp->ext_next;
437 1.1 lukem }
438 1.1 lukem }
439 1.1 lukem
440 1.1 lukem error_return:
441 1.1 lukem return rc;
442 1.1 lukem }
443 1.1 lukem
444 1.1 lukem /*********************************************************************
445 1.1 lukem * Function Name: slapi_int_get_extop_plugin
446 1.1 lukem *
447 1.1 lukem * Description: This routine gets the function address for a given function
448 1.1 lukem * name.
449 1.1 lukem *
450 1.1 lukem * Input:
451 1.1 lukem * funcName - name of the extended op function, ie. an OID.
452 1.1 lukem *
453 1.1 lukem * Output: pFuncAddr - the function address of the requested function name.
454 1.1 lukem *
455 1.1 lukem * Return Values: a pointer to a newly created ExtendOp structrue or
456 1.1 lukem * NULL - function failed
457 1.1 lukem *
458 1.1 lukem * Messages: None
459 1.1 lukem *********************************************************************/
460 1.1 lukem int
461 1.1 lukem slapi_int_get_extop_plugin(
462 1.1 lukem struct berval *reqoid,
463 1.1 lukem SLAPI_FUNC *pFuncAddr )
464 1.1 lukem {
465 1.1 lukem ExtendedOp *pTmpExtOp;
466 1.1 lukem
467 1.1 lukem assert( reqoid != NULL );
468 1.1 lukem assert( pFuncAddr != NULL );
469 1.1 lukem
470 1.1 lukem *pFuncAddr = NULL;
471 1.1 lukem
472 1.1 lukem if ( pGExtendedOps == NULL ) {
473 1.1 lukem return LDAP_OTHER;
474 1.1 lukem }
475 1.1 lukem
476 1.1 lukem pTmpExtOp = pGExtendedOps;
477 1.1 lukem while ( pTmpExtOp != NULL ) {
478 1.1 lukem int rc;
479 1.1 lukem
480 1.1 lukem rc = strcasecmp( reqoid->bv_val, pTmpExtOp->ext_oid.bv_val );
481 1.1 lukem if ( rc == 0 ) {
482 1.1 lukem *pFuncAddr = pTmpExtOp->ext_func;
483 1.1 lukem break;
484 1.1 lukem }
485 1.1 lukem pTmpExtOp = pTmpExtOp->ext_next;
486 1.1 lukem }
487 1.1 lukem
488 1.1 lukem return ( *pFuncAddr == NULL ? 1 : 0 );
489 1.1 lukem }
490 1.1 lukem
491 1.1 lukem /***************************************************************************
492 1.1 lukem * This function is similar to slapi_int_get_extop_plugin above. except it returns one OID
493 1.1 lukem * per call. It is called from root_dse_info (root_dse.c).
494 1.1 lukem * The function is a modified version of get_supported_extop (file extended.c).
495 1.1 lukem ***************************************************************************/
496 1.1 lukem struct berval *
497 1.1 lukem slapi_int_get_supported_extop( int index )
498 1.1 lukem {
499 1.1 lukem ExtendedOp *ext;
500 1.1 lukem
501 1.1 lukem for ( ext = pGExtendedOps ; ext != NULL && --index >= 0;
502 1.1 lukem ext = ext->ext_next) {
503 1.1 lukem ; /* empty */
504 1.1 lukem }
505 1.1 lukem
506 1.1 lukem if ( ext == NULL ) {
507 1.1 lukem return NULL;
508 1.1 lukem }
509 1.1 lukem
510 1.1 lukem return &ext->ext_oid ;
511 1.1 lukem }
512 1.1 lukem
513 1.1 lukem /*********************************************************************
514 1.1 lukem * Function Name: slapi_int_load_plugin
515 1.1 lukem *
516 1.1 lukem * Description: This routine loads the specified DLL, gets and executes the init function
517 1.1 lukem * if requested.
518 1.1 lukem *
519 1.1 lukem * Input:
520 1.1 lukem * pPlugin - a pointer to a Slapi_PBlock struct which will be passed to
521 1.1 lukem * the DLL init function.
522 1.1 lukem * path - path name of the DLL to be load.
523 1.1 lukem * initfunc - either the DLL initialization function or an OID of the
524 1.1 lukem * loaded extended operation.
525 1.1 lukem * doInit - if it is TRUE, execute the init function, otherwise, save the
526 1.1 lukem * function address but not execute it.
527 1.1 lukem *
528 1.1 lukem * Output: pInitFunc - the function address of the loaded function. This param
529 1.1 lukem * should be not be null if doInit is FALSE.
530 1.1 lukem * pLdHandle - handle returned by lt_dlopen()
531 1.1 lukem *
532 1.1 lukem * Return Values: LDAP_SUCCESS, LDAP_LOCAL_ERROR
533 1.1 lukem *
534 1.1 lukem * Messages: None
535 1.1 lukem *********************************************************************/
536 1.1 lukem
537 1.1 lukem static int
538 1.1 lukem slapi_int_load_plugin(
539 1.1 lukem Slapi_PBlock *pPlugin,
540 1.1 lukem const char *path,
541 1.1 lukem const char *initfunc,
542 1.1 lukem int doInit,
543 1.1 lukem SLAPI_FUNC *pInitFunc,
544 1.1 lukem lt_dlhandle *pLdHandle )
545 1.1 lukem {
546 1.1 lukem int rc = LDAP_SUCCESS;
547 1.1 lukem SLAPI_FUNC fpInitFunc = NULL;
548 1.1 lukem
549 1.1 lukem assert( pLdHandle != NULL );
550 1.1 lukem
551 1.1 lukem if ( lt_dlinit() ) {
552 1.1 lukem return LDAP_LOCAL_ERROR;
553 1.1 lukem }
554 1.1 lukem
555 1.1 lukem /* load in the module */
556 1.1 lukem *pLdHandle = lt_dlopen( path );
557 1.1 lukem if ( *pLdHandle == NULL ) {
558 1.1 lukem fprintf( stderr, "failed to load plugin %s: %s\n",
559 1.1 lukem path, lt_dlerror() );
560 1.1 lukem return LDAP_LOCAL_ERROR;
561 1.1 lukem }
562 1.1 lukem
563 1.1 lukem fpInitFunc = (SLAPI_FUNC)lt_dlsym( *pLdHandle, initfunc );
564 1.1 lukem if ( fpInitFunc == NULL ) {
565 1.1 lukem fprintf( stderr, "failed to find symbol %s in plugin %s: %s\n",
566 1.1 lukem initfunc, path, lt_dlerror() );
567 1.1 lukem lt_dlclose( *pLdHandle );
568 1.1 lukem return LDAP_LOCAL_ERROR;
569 1.1 lukem }
570 1.1 lukem
571 1.1 lukem if ( doInit ) {
572 1.1 lukem rc = ( *fpInitFunc )( pPlugin );
573 1.1 lukem if ( rc != LDAP_SUCCESS ) {
574 1.1 lukem lt_dlclose( *pLdHandle );
575 1.1 lukem }
576 1.1 lukem
577 1.1 lukem } else {
578 1.1 lukem *pInitFunc = fpInitFunc;
579 1.1 lukem }
580 1.1 lukem
581 1.1 lukem return rc;
582 1.1 lukem }
583 1.1 lukem
584 1.1 lukem /*
585 1.1 lukem * Special support for computed attribute plugins
586 1.1 lukem */
587 1.1 lukem int
588 1.1 lukem slapi_int_call_plugins(
589 1.1 lukem Backend *be,
590 1.1 lukem int funcType,
591 1.1 lukem Slapi_PBlock *pPB )
592 1.1 lukem {
593 1.1 lukem
594 1.1 lukem int rc = 0;
595 1.1 lukem SLAPI_FUNC *pGetPlugin = NULL, *tmpPlugin = NULL;
596 1.1 lukem
597 1.1 lukem if ( pPB == NULL ) {
598 1.1 lukem return 1;
599 1.1 lukem }
600 1.1 lukem
601 1.1 lukem rc = slapi_int_get_plugins( be, funcType, &tmpPlugin );
602 1.1 lukem if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
603 1.1 lukem /* Nothing to do, front-end should ignore. */
604 1.1 lukem return rc;
605 1.1 lukem }
606 1.1 lukem
607 1.1 lukem for ( pGetPlugin = tmpPlugin ; *pGetPlugin != NULL; pGetPlugin++ ) {
608 1.1 lukem rc = (*pGetPlugin)(pPB);
609 1.1 lukem
610 1.1 lukem /*
611 1.1 lukem * Only non-postoperation plugins abort processing on
612 1.1 lukem * failure (confirmed with SLAPI specification).
613 1.1 lukem */
614 1.1 lukem if ( !SLAPI_PLUGIN_IS_POST_FN( funcType ) && rc != 0 ) {
615 1.1 lukem /*
616 1.1 lukem * Plugins generally return negative error codes
617 1.1 lukem * to indicate failure, although in the case of
618 1.1 lukem * bind plugins they may return SLAPI_BIND_xxx
619 1.1 lukem */
620 1.1 lukem break;
621 1.1 lukem }
622 1.1 lukem }
623 1.1 lukem
624 1.1 lukem slapi_ch_free( (void **)&tmpPlugin );
625 1.1 lukem
626 1.1 lukem return rc;
627 1.1 lukem }
628 1.1 lukem
629 1.1 lukem int
630 1.1 lukem slapi_int_read_config(
631 1.1 lukem Backend *be,
632 1.1 lukem const char *fname,
633 1.1 lukem int lineno,
634 1.1 lukem int argc,
635 1.1 lukem char **argv )
636 1.1 lukem {
637 1.1 lukem int iType = -1;
638 1.1 lukem int numPluginArgc = 0;
639 1.1 lukem
640 1.1 lukem if ( argc < 4 ) {
641 1.1 lukem fprintf( stderr,
642 1.1 lukem "%s: line %d: missing arguments "
643 1.1 lukem "in \"plugin <plugin_type> <lib_path> "
644 1.1 lukem "<init_function> [<arguments>]\" line\n",
645 1.1 lukem fname, lineno );
646 1.1 lukem return 1;
647 1.1 lukem }
648 1.1 lukem
649 1.1 lukem /* automatically instantiate overlay if necessary */
650 1.1 lukem if ( !slapi_over_is_inst( be ) ) {
651 1.1.1.2 lukem ConfigReply cr = { 0 };
652 1.1.1.2 lukem if ( slapi_over_config( be, &cr ) != 0 ) {
653 1.1.1.2 lukem fprintf( stderr, "Failed to instantiate SLAPI overlay: "
654 1.1.1.2 lukem "err=%d msg=\"%s\"\n", cr.err, cr.msg );
655 1.1 lukem return -1;
656 1.1 lukem }
657 1.1 lukem }
658 1.1 lukem
659 1.1 lukem if ( strcasecmp( argv[1], "preoperation" ) == 0 ) {
660 1.1 lukem iType = SLAPI_PLUGIN_PREOPERATION;
661 1.1 lukem } else if ( strcasecmp( argv[1], "postoperation" ) == 0 ) {
662 1.1 lukem iType = SLAPI_PLUGIN_POSTOPERATION;
663 1.1 lukem } else if ( strcasecmp( argv[1], "extendedop" ) == 0 ) {
664 1.1 lukem iType = SLAPI_PLUGIN_EXTENDEDOP;
665 1.1 lukem } else if ( strcasecmp( argv[1], "object" ) == 0 ) {
666 1.1 lukem iType = SLAPI_PLUGIN_OBJECT;
667 1.1 lukem } else {
668 1.1 lukem fprintf( stderr, "%s: line %d: invalid plugin type \"%s\".\n",
669 1.1 lukem fname, lineno, argv[1] );
670 1.1 lukem return 1;
671 1.1 lukem }
672 1.1 lukem
673 1.1 lukem numPluginArgc = argc - 4;
674 1.1 lukem
675 1.1 lukem if ( iType == SLAPI_PLUGIN_PREOPERATION ||
676 1.1 lukem iType == SLAPI_PLUGIN_EXTENDEDOP ||
677 1.1 lukem iType == SLAPI_PLUGIN_POSTOPERATION ||
678 1.1 lukem iType == SLAPI_PLUGIN_OBJECT ) {
679 1.1 lukem int rc;
680 1.1 lukem Slapi_PBlock *pPlugin;
681 1.1 lukem
682 1.1 lukem pPlugin = plugin_pblock_new( iType, numPluginArgc, argv );
683 1.1 lukem if (pPlugin == NULL) {
684 1.1 lukem return 1;
685 1.1 lukem }
686 1.1 lukem
687 1.1 lukem if (iType == SLAPI_PLUGIN_EXTENDEDOP) {
688 1.1 lukem rc = slapi_int_register_extop(be, &pGExtendedOps, pPlugin);
689 1.1 lukem if ( rc != LDAP_SUCCESS ) {
690 1.1 lukem slapi_pblock_destroy( pPlugin );
691 1.1 lukem return 1;
692 1.1 lukem }
693 1.1 lukem }
694 1.1 lukem
695 1.1 lukem rc = slapi_int_register_plugin( be, pPlugin );
696 1.1 lukem if ( rc != LDAP_SUCCESS ) {
697 1.1 lukem if ( iType == SLAPI_PLUGIN_EXTENDEDOP ) {
698 1.1 lukem slapi_int_unregister_extop( be, &pGExtendedOps, pPlugin );
699 1.1 lukem }
700 1.1 lukem slapi_pblock_destroy( pPlugin );
701 1.1 lukem return 1;
702 1.1 lukem }
703 1.1 lukem }
704 1.1 lukem
705 1.1 lukem return 0;
706 1.1 lukem }
707 1.1 lukem
708 1.1 lukem void
709 1.1 lukem slapi_int_plugin_unparse(
710 1.1 lukem Backend *be,
711 1.1 lukem BerVarray *out
712 1.1 lukem )
713 1.1 lukem {
714 1.1 lukem Slapi_PBlock *pp;
715 1.1 lukem int i, j;
716 1.1 lukem char **argv, ibuf[32], *ptr;
717 1.1 lukem struct berval idx, bv;
718 1.1 lukem
719 1.1 lukem *out = NULL;
720 1.1 lukem idx.bv_val = ibuf;
721 1.1 lukem i = 0;
722 1.1 lukem
723 1.1 lukem for ( pp = SLAPI_BACKEND_PBLOCK( be );
724 1.1 lukem pp != NULL;
725 1.1 lukem slapi_pblock_get( pp, SLAPI_IBM_PBLOCK, &pp ) )
726 1.1 lukem {
727 1.1 lukem slapi_pblock_get( pp, SLAPI_X_CONFIG_ARGV, &argv );
728 1.1 lukem if ( argv == NULL ) /* could be dynamic plugin */
729 1.1 lukem continue;
730 1.1 lukem idx.bv_len = snprintf( idx.bv_val, sizeof( ibuf ), "{%d}", i );
731 1.1 lukem if ( idx.bv_len >= sizeof( ibuf ) ) {
732 1.1 lukem /* FIXME: just truncating by now */
733 1.1 lukem idx.bv_len = sizeof( ibuf ) - 1;
734 1.1 lukem }
735 1.1 lukem bv.bv_len = idx.bv_len;
736 1.1 lukem for (j=1; argv[j]; j++) {
737 1.1 lukem bv.bv_len += strlen(argv[j]);
738 1.1 lukem if ( j ) bv.bv_len++;
739 1.1 lukem }
740 1.1 lukem bv.bv_val = ch_malloc( bv.bv_len + 1 );
741 1.1 lukem ptr = lutil_strcopy( bv.bv_val, ibuf );
742 1.1 lukem for (j=1; argv[j]; j++) {
743 1.1 lukem if ( j ) *ptr++ = ' ';
744 1.1 lukem ptr = lutil_strcopy( ptr, argv[j] );
745 1.1 lukem }
746 1.1 lukem ber_bvarray_add( out, &bv );
747 1.1 lukem }
748 1.1 lukem }
749 1.1 lukem
750