plugin.c 20.7 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
/* $OpenLDAP$ */
2
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
4
 * Copyright 2002-2021 The OpenLDAP Foundation.
5
6
7
 * Portions Copyright 1997,2002-2003 IBM Corporation.
 * All rights reserved.
 *
8
9
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
10
11
12
13
14
15
16
17
18
19
20
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by IBM Corporation for use in
 * IBM products and subsequently ported to OpenLDAP Software by
 * Steve Omrani.  Additional significant contributors include:
 *    Luke Howard
21
22
23
 */

#include "portable.h"
24
25
26
27
28
29

/*
 * Note: if ltdl.h is not available, slapi should not be compiled
 */

#ifdef HAVE_LTDL_H
30
31
#include "ldap_pvt_thread.h"
#include "slap.h"
32
#include "slap-config.h"
33
34
#include "slapi.h"
#include "lutil.h"
35

Howard Chu's avatar
Howard Chu committed
36
37
#include <ltdl.h>

Luke Howard's avatar
Luke Howard committed
38
static int slapi_int_load_plugin( Slapi_PBlock *, const char *, const char *, int, 
39
40
41
42
43
44
	SLAPI_FUNC *, lt_dlhandle * );

/* pointer to link list of extended objects */
static ExtendedOp *pGExtendedOps = NULL;

/*********************************************************************
Luke Howard's avatar
Luke Howard committed
45
 * Function Name:      plugin_pblock_new
46
47
48
49
50
51
52
53
 *
 * Description:        This routine creates a new Slapi_PBlock structure,
 *                     loads in the plugin module and executes the init
 *                     function provided by the module.
 *
 * Input:              type - type of the plugin, such as SASL, database, etc.
 *                     path - the loadpath to load the module in
 *                     initfunc - name of the plugin function to execute first
54
 *                     argc - number of arguments
55
56
57
58
59
60
 *                     argv[] - an array of char pointers point to
 *                              the arguments passed in via
 *                              the configuration file.
 *
 * Output:             
 *
Josh Soref's avatar
Josh Soref committed
61
 * Return Values:      a pointer to a newly created Slapi_PBlock structure or
62
63
64
65
66
 *                     NULL - function failed 
 *
 * Messages:           None
 *********************************************************************/

Luke Howard's avatar
Luke Howard committed
67
68
static Slapi_PBlock *
plugin_pblock_new(
69
70
71
72
73
	int type, 
	int argc, 
	char *argv[] ) 
{
	Slapi_PBlock	*pPlugin = NULL; 
Luke Howard's avatar
Luke Howard committed
74
	Slapi_PluginDesc *pPluginDesc = NULL;
75
76
	lt_dlhandle	hdLoadHandle;
	int		rc;
Luke Howard's avatar
Luke Howard committed
77
78
79
	char		**av2 = NULL, **ppPluginArgv;
	char		*path = argv[2];
	char		*initfunc = argv[3];
80
81
82
83
84
85
86

	pPlugin = slapi_pblock_new();
	if ( pPlugin == NULL ) {
		rc = LDAP_NO_MEMORY;
		goto done;
	}

87
88
	slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
	slapi_pblock_set( pPlugin, SLAPI_PLUGIN_ARGC, (void *)&argc );
89

90
	av2 = ldap_charray_dup( argv );
91
	if ( av2 == NULL ) {
92
93
94
95
96
97
98
99
100
101
		rc = LDAP_NO_MEMORY;
		goto done;
	}

	if ( argc > 0 ) {
		ppPluginArgv = &av2[4];
	} else {
		ppPluginArgv = NULL;
	}

102
103
	slapi_pblock_set( pPlugin, SLAPI_PLUGIN_ARGV, (void *)ppPluginArgv );
	slapi_pblock_set( pPlugin, SLAPI_X_CONFIG_ARGV, (void *)av2 );
104

Luke Howard's avatar
Luke Howard committed
105
	rc = slapi_int_load_plugin( pPlugin, path, initfunc, 1, NULL, &hdLoadHandle );
106
107
108
	if ( rc != 0 ) {
		goto done;
	}
109

Luke Howard's avatar
Luke Howard committed
110
111
	if ( slapi_pblock_get( pPlugin, SLAPI_PLUGIN_DESCRIPTION, (void **)&pPluginDesc ) == 0 &&
	     pPluginDesc != NULL ) {
Luke Howard's avatar
Luke Howard committed
112
		slapi_log_error(SLAPI_LOG_TRACE, "plugin_pblock_new",
Luke Howard's avatar
Luke Howard committed
113
114
115
116
117
118
119
				"Registered plugin %s %s [%s] (%s)\n",
				pPluginDesc->spd_id,
				pPluginDesc->spd_version,
				pPluginDesc->spd_vendor,
				pPluginDesc->spd_description);
	}

120
done:
Luke Howard's avatar
Luke Howard committed
121
	if ( rc != 0 && pPlugin != NULL ) {
122
123
		slapi_pblock_destroy( pPlugin );
		pPlugin = NULL;
124
		if ( av2 != NULL ) {
125
126
			ldap_charray_free( av2 );
		}
127
128
129
130
131
132
	}

	return pPlugin;
} 

/*********************************************************************
Luke Howard's avatar
Luke Howard committed
133
 * Function Name:      slapi_int_register_plugin
134
 *
135
 * Description:        insert the slapi_pblock structure to a given position the end of the plugin
136
137
138
139
140
141
142
143
144
145
146
147
148
 *                     list 
 *
 * Input:              a pointer to a plugin slapi_pblock structure to be added to 
 *                     the list
 *
 * Output:             none
 *
 * Return Values:      LDAP_SUCCESS - successfully inserted.
 *                     LDAP_LOCAL_ERROR.
 *
 * Messages:           None
 *********************************************************************/
int 
149
slapi_int_register_plugin_index(
150
	Backend *be, 
151
152
	Slapi_PBlock *pPB,
	int index )
153
{ 
Luke Howard's avatar
Luke Howard committed
154
155
	Slapi_PBlock	*pTmpPB;
	Slapi_PBlock	*pSavePB;
156
	int		pos = 0, rc = LDAP_SUCCESS;
157

158
	assert( be != NULL );
159

160
	pTmpPB = SLAPI_BACKEND_PBLOCK( be );
161
	if ( pTmpPB == NULL || index == 0 ) {
162
		SLAPI_BACKEND_PBLOCK( be ) = pPB;
163
	} else {
164
165
		while ( pTmpPB != NULL && rc == LDAP_SUCCESS &&
				( index < 0 || pos++ < index ) ) {
166
			pSavePB = pTmpPB;
167
			rc = slapi_pblock_get( pTmpPB, SLAPI_IBM_PBLOCK, &pTmpPB );
168
169
170
		}

		if ( rc == LDAP_SUCCESS ) { 
171
			rc = slapi_pblock_set( pSavePB, SLAPI_IBM_PBLOCK, (void *)pPB ); 
172
173
		}
	}
174
175
176
177

	if ( index >= 0 && rc == LDAP_SUCCESS ) {
		rc = slapi_pblock_set( pPB, SLAPI_IBM_PBLOCK, (void *)pTmpPB );
	}
178
     
179
	return ( rc != LDAP_SUCCESS ) ? LDAP_OTHER : LDAP_SUCCESS;
180
}
181
182
183
184
185
186
187
188
189

int
slapi_int_register_plugin(
	Backend *be,
	Slapi_PBlock *pPB )
{
	return slapi_int_register_plugin_index( be, pPB, -1 );
}

190
/*********************************************************************
Luke Howard's avatar
Luke Howard committed
191
 * Function Name:      slapi_int_get_plugins
192
193
194
195
196
197
198
199
200
 *
 * Description:        get the desired type of function pointers defined 
 *                     in all the plugins 
 *
 * Input:              the type of the functions to get, such as pre-operation,etc.
 *
 * Output:             none
 *
 * Return Values:      this routine returns a pointer to an array of function
201
202
 *                     pointers containing backend-specific plugin functions
 *                     followed by global plugin functions
203
204
205
206
 *
 * Messages:           None
 *********************************************************************/
int 
Luke Howard's avatar
Luke Howard committed
207
slapi_int_get_plugins(
208
209
210
211
212
213
214
215
216
217
218
	Backend *be, 		
	int functype, 
	SLAPI_FUNC **ppFuncPtrs )
{
 
	Slapi_PBlock	*pCurrentPB; 
	SLAPI_FUNC	FuncPtr;
	SLAPI_FUNC	*pTmpFuncPtr;
	int		numPB = 0;
	int		rc = LDAP_SUCCESS;

219
	assert( ppFuncPtrs != NULL );
220
	*ppFuncPtrs = NULL;
221
222
223
224

	if ( be == NULL ) {
		goto done;
	}
225

226
	pCurrentPB = SLAPI_BACKEND_PBLOCK( be );
227

228
	while ( pCurrentPB != NULL && rc == LDAP_SUCCESS ) {
229
230
231
232
233
234
		rc = slapi_pblock_get( pCurrentPB, functype, &FuncPtr );
		if ( rc == LDAP_SUCCESS ) {
			if ( FuncPtr != NULL )  {
				numPB++;
			}
			rc = slapi_pblock_get( pCurrentPB,
235
				SLAPI_IBM_PBLOCK, &pCurrentPB );
236
237
238
239
240
241
242
243
		}
	}

	if ( numPB == 0 ) {
		rc = LDAP_SUCCESS;
		goto done;
	}

244
245
246
247
	/*
	 * Now, build the function pointer array of backend-specific
	 * plugins followed by global plugins.
	 */
248
249
250
251
252
253
254
	*ppFuncPtrs = pTmpFuncPtr = 
		(SLAPI_FUNC *)ch_malloc( ( numPB + 1 ) * sizeof(SLAPI_FUNC) ); 
	if ( ppFuncPtrs == NULL ) {
		rc = LDAP_NO_MEMORY;
		goto done;
	}

255
	pCurrentPB = SLAPI_BACKEND_PBLOCK( be );
256

257
258
259
260
261
262
263
264
265
266
267
	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 );
		}
	}
268

269
270
	*pTmpFuncPtr = NULL;

271
272
273
274
275
276
277
278
279

done:
	if ( rc != LDAP_SUCCESS && *ppFuncPtrs != NULL ) {
		ch_free( *ppFuncPtrs );
		*ppFuncPtrs = NULL;
	}

	return rc;
}
280

281
282
283
284
285
286
287
288
289
290
291
292
293
/*********************************************************************
 * Function Name:      createExtendedOp
 *
 * Description: Creates an extended operation structure and
 *              initializes the fields
 *
 * Return value: A newly allocated structure or NULL
 ********************************************************************/
ExtendedOp *
createExtendedOp()
{
	ExtendedOp *ret;

Luke Howard's avatar
Luke Howard committed
294
295
296
297
298
299
	ret = (ExtendedOp *)slapi_ch_malloc(sizeof(ExtendedOp));
	ret->ext_oid.bv_val = NULL;
	ret->ext_oid.bv_len = 0;
	ret->ext_func = NULL;
	ret->ext_be = NULL;
	ret->ext_next = NULL;
300
301
302
303
304
305

	return ret;
}


/*********************************************************************
Luke Howard's avatar
Luke Howard committed
306
 * Function Name:      slapi_int_unregister_extop
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
 *
 * Description:        This routine removes the ExtendedOp structures 
 *					   asscoiated with a particular extended operation 
 *					   plugin.
 *
 * Input:              pBE - pointer to a backend structure
 *                     opList - pointer to a linked list of extended
 *                              operation structures
 *                     pPB - pointer to a slapi parameter block
 *
 * Output:
 *
 * Return Value:       none
 *
 * Messages:           None
 *********************************************************************/
void
Luke Howard's avatar
Luke Howard committed
324
slapi_int_unregister_extop(
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
	Backend *pBE, 
	ExtendedOp **opList, 
	Slapi_PBlock *pPB )
{
	ExtendedOp	*pTmpExtOp, *backExtOp;
	char		**pTmpOIDs;
	int		i;

#if 0
	assert( pBE != NULL); /* unused */
#endif /* 0 */
	assert( opList != NULL );
	assert( pPB != NULL );

	if ( *opList == NULL ) {
		return;
	}

	slapi_pblock_get( pPB, SLAPI_PLUGIN_EXT_OP_OIDLIST, &pTmpOIDs );
	if ( pTmpOIDs == NULL ) {
		return;
	}

	for ( i = 0; pTmpOIDs[i] != NULL; i++ ) {
		backExtOp = NULL;
		pTmpExtOp = *opList;
		for ( ; pTmpExtOp != NULL; pTmpExtOp = pTmpExtOp->ext_next) {
			int	rc;
			rc = strcasecmp( pTmpExtOp->ext_oid.bv_val,
					pTmpOIDs[ i ] );
			if ( rc == 0 ) {
				if ( backExtOp == NULL ) {
					*opList = pTmpExtOp->ext_next;
				} else {
					backExtOp->ext_next
						= pTmpExtOp->ext_next;
				}

				ch_free( pTmpExtOp );
				break;
			}
			backExtOp = pTmpExtOp;
		}
	}
}


/*********************************************************************
Luke Howard's avatar
Luke Howard committed
373
 * Function Name:      slapi_int_register_extop
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
 *
 * Description:        This routine creates a new ExtendedOp structure, loads
 *                     in the extended op module and put the extended op function address
 *                     in the structure. The function will not be executed in
 *                     this routine.
 *
 * Input:              pBE - pointer to a backend structure
 *                     opList - pointer to a linked list of extended
 *                              operation structures
 *                     pPB - pointer to a slapi parameter block
 *
 * Output:
 *
 * Return Value:       an LDAP return code
 *
 * Messages:           None
 *********************************************************************/
int 
Luke Howard's avatar
Luke Howard committed
392
slapi_int_register_extop(
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
	Backend *pBE, 	
	ExtendedOp **opList, 
	Slapi_PBlock *pPB )
{
	ExtendedOp	*pTmpExtOp = NULL;
	SLAPI_FUNC	tmpFunc;
	char		**pTmpOIDs;
	int		rc = LDAP_OTHER;
	int		i;

	if ( (*opList) == NULL ) { 
		*opList = createExtendedOp();
		if ( (*opList) == NULL ) {
			rc = LDAP_NO_MEMORY;
			goto error_return;
		}
		pTmpExtOp = *opList;
		
	} else {                        /* Find the end of the list */
		for ( pTmpExtOp = *opList; pTmpExtOp->ext_next != NULL;
				pTmpExtOp = pTmpExtOp->ext_next )
			; /* EMPTY */
		pTmpExtOp->ext_next = createExtendedOp();
		if ( pTmpExtOp->ext_next == NULL ) {
			rc = LDAP_NO_MEMORY;
			goto error_return;
		}
		pTmpExtOp = pTmpExtOp->ext_next;
	}

	rc = slapi_pblock_get( pPB,SLAPI_PLUGIN_EXT_OP_OIDLIST, &pTmpOIDs );
424
	if ( rc != 0 ) {
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
		rc = LDAP_OTHER;
		goto error_return;
	}

	rc = slapi_pblock_get(pPB,SLAPI_PLUGIN_EXT_OP_FN, &tmpFunc);
	if ( rc != 0 ) {
		rc = LDAP_OTHER;
		goto error_return;
	}

	if ( (pTmpOIDs == NULL) || (tmpFunc == NULL) ) {
		rc = LDAP_OTHER;
		goto error_return;
	}

	for ( i = 0; pTmpOIDs[i] != NULL; i++ ) {
		pTmpExtOp->ext_oid.bv_val = pTmpOIDs[i];
		pTmpExtOp->ext_oid.bv_len = strlen( pTmpOIDs[i] );
		pTmpExtOp->ext_func = tmpFunc;
		pTmpExtOp->ext_be = pBE;
		if ( pTmpOIDs[i + 1] != NULL ) {
			pTmpExtOp->ext_next = createExtendedOp();
			if ( pTmpExtOp->ext_next == NULL ) {
				rc = LDAP_NO_MEMORY;
				break;
			}
			pTmpExtOp = pTmpExtOp->ext_next;
		}
	}

error_return:
	return rc;
}

/*********************************************************************
Luke Howard's avatar
Luke Howard committed
460
 * Function Name:      slapi_int_get_extop_plugin
461
462
463
464
465
466
467
468
469
 *
 * Description:        This routine gets the function address for a given function
 *                     name.
 *
 * Input:
 *                     funcName - name of the extended op function, ie. an OID.
 *
 * Output:             pFuncAddr - the function address of the requested function name.
 *
Josh Soref's avatar
Josh Soref committed
470
 * Return Values:      a pointer to a newly created ExtendOp structure or
471
472
473
474
475
 *                     NULL - function failed
 *
 * Messages:           None
 *********************************************************************/
int 
Luke Howard's avatar
Luke Howard committed
476
slapi_int_get_extop_plugin(
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
	struct berval *reqoid, 		
	SLAPI_FUNC *pFuncAddr ) 
{
	ExtendedOp	*pTmpExtOp;

	assert( reqoid != NULL );
	assert( pFuncAddr != NULL );

	*pFuncAddr = NULL;

	if ( pGExtendedOps == NULL ) {
		return LDAP_OTHER;
	}

	pTmpExtOp = pGExtendedOps;
	while ( pTmpExtOp != NULL ) {
		int	rc;
		
		rc = strcasecmp( reqoid->bv_val, pTmpExtOp->ext_oid.bv_val );
		if ( rc == 0 ) {
			*pFuncAddr = pTmpExtOp->ext_func;
			break;
		}
		pTmpExtOp = pTmpExtOp->ext_next;
	}

	return ( *pFuncAddr == NULL ? 1 : 0 );
}

/***************************************************************************
Luke Howard's avatar
Luke Howard committed
507
 * This function is similar to slapi_int_get_extop_plugin above. except it returns one OID
508
509
510
511
 * per call. It is called from root_dse_info (root_dse.c).
 * The function is a modified version of get_supported_extop (file extended.c).
 ***************************************************************************/
struct berval *
Luke Howard's avatar
Luke Howard committed
512
slapi_int_get_supported_extop( int index )
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
{
        ExtendedOp	*ext;

        for ( ext = pGExtendedOps ; ext != NULL && --index >= 0;
			ext = ext->ext_next) {
                ; /* empty */
        }

        if ( ext == NULL ) {
		return NULL;
	}

        return &ext->ext_oid ;
}

/*********************************************************************
Luke Howard's avatar
Luke Howard committed
529
 * Function Name:      slapi_int_load_plugin
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
 *
 * Description:        This routine loads the specified DLL, gets and executes the init function
 *                     if requested.
 *
 * Input:
 *                     pPlugin - a pointer to a Slapi_PBlock struct which will be passed to
 *                               the DLL init function.
 *                     path - path name of the DLL to be load.
 *                     initfunc - either the DLL initialization function or an OID of the
 *                                loaded extended operation.
 *                     doInit - if it is TRUE, execute the init function, otherwise, save the
 *                              function address but not execute it.
 *
 * Output:             pInitFunc - the function address of the loaded function. This param
 *                                 should be not be null if doInit is FALSE.
 *                     pLdHandle - handle returned by lt_dlopen()
 *
 * Return Values:      LDAP_SUCCESS, LDAP_LOCAL_ERROR
 *
 * Messages:           None
 *********************************************************************/

static int 
Luke Howard's avatar
Luke Howard committed
553
slapi_int_load_plugin(
554
555
556
557
558
559
560
561
562
563
	Slapi_PBlock	*pPlugin,
	const char	*path,
	const char	*initfunc, 
	int		doInit,
	SLAPI_FUNC	*pInitFunc,
	lt_dlhandle	*pLdHandle ) 
{
	int		rc = LDAP_SUCCESS;
	SLAPI_FUNC	fpInitFunc = NULL;

564
	assert( pLdHandle != NULL );
565
566
567
568
569
570
571
572

	if ( lt_dlinit() ) {
		return LDAP_LOCAL_ERROR;
	}

	/* load in the module */
	*pLdHandle = lt_dlopen( path );
	if ( *pLdHandle == NULL ) {
573
574
		fprintf( stderr, "failed to load plugin %s: %s\n",
			 path, lt_dlerror() );
575
576
577
578
579
		return LDAP_LOCAL_ERROR;
	}

	fpInitFunc = (SLAPI_FUNC)lt_dlsym( *pLdHandle, initfunc );
	if ( fpInitFunc == NULL ) {
580
581
		fprintf( stderr, "failed to find symbol %s in plugin %s: %s\n",
			 initfunc, path, lt_dlerror() );
582
583
584
585
		lt_dlclose( *pLdHandle );
		return LDAP_LOCAL_ERROR;
	}

Luke Howard's avatar
Luke Howard committed
586
	if ( doInit ) {
587
588
589
590
591
592
593
594
595
596
597
598
		rc = ( *fpInitFunc )( pPlugin );
		if ( rc != LDAP_SUCCESS ) {
			lt_dlclose( *pLdHandle );
		}

	} else {
		*pInitFunc = fpInitFunc;
	}

	return rc;
}

599
600
601
/*
 * Special support for computed attribute plugins
 */
602
int 
Luke Howard's avatar
Luke Howard committed
603
slapi_int_call_plugins(
604
605
606
607
608
	Backend		*be, 	
	int		funcType, 
	Slapi_PBlock	*pPB )
{

609
	int rc = 0;
610
611
	SLAPI_FUNC *pGetPlugin = NULL, *tmpPlugin = NULL; 

612
	if ( pPB == NULL ) {
613
		return 1;
614
615
	}

Luke Howard's avatar
Luke Howard committed
616
	rc = slapi_int_get_plugins( be, funcType, &tmpPlugin );
617
	if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
618
		/* Nothing to do, front-end should ignore. */
619
		return rc;
620
621
622
623
624
	}

	for ( pGetPlugin = tmpPlugin ; *pGetPlugin != NULL; pGetPlugin++ ) {
		rc = (*pGetPlugin)(pPB);

625
626
627
628
629
		/*
		 * Only non-postoperation plugins abort processing on
		 * failure (confirmed with SLAPI specification).
		 */
		if ( !SLAPI_PLUGIN_IS_POST_FN( funcType ) && rc != 0 ) {
630
631
632
633
634
			/*
			 * Plugins generally return negative error codes
			 * to indicate failure, although in the case of
			 * bind plugins they may return SLAPI_BIND_xxx
			 */
635
636
637
638
			break;
		}
	}

639
640
	slapi_ch_free( (void **)&tmpPlugin );

641
642
643
644
	return rc;
}

int
Luke Howard's avatar
Luke Howard committed
645
slapi_int_read_config(
646
647
648
649
	Backend		*be, 		
	const char	*fname, 
	int		lineno, 
	int		argc, 
650
651
	char		**argv,
	int		index )
652
653
654
655
656
657
658
659
660
661
662
663
{
	int		iType = -1;
	int		numPluginArgc = 0;

	if ( argc < 4 ) {
		fprintf( stderr,
			"%s: line %d: missing arguments "
			"in \"plugin <plugin_type> <lib_path> "
			"<init_function> [<arguments>]\" line\n",
			fname, lineno );
		return 1;
	}
664
665

	/* automatically instantiate overlay if necessary */
666
	if ( !slapi_over_is_inst( be ) ) {
667
668
669
670
		ConfigReply cr = { 0 };
		if ( slapi_over_config( be, &cr ) != 0 ) {
			fprintf( stderr, "Failed to instantiate SLAPI overlay: "
				"err=%d msg=\"%s\"\n", cr.err, cr.msg );
671
672
673
			return -1;
		}
	}
674
675
676
677
678
679
680
	
	if ( strcasecmp( argv[1], "preoperation" ) == 0 ) {
		iType = SLAPI_PLUGIN_PREOPERATION;
	} else if ( strcasecmp( argv[1], "postoperation" ) == 0 ) {
		iType = SLAPI_PLUGIN_POSTOPERATION;
	} else if ( strcasecmp( argv[1], "extendedop" ) == 0 ) {
		iType = SLAPI_PLUGIN_EXTENDEDOP;
681
682
	} else if ( strcasecmp( argv[1], "object" ) == 0 ) {
		iType = SLAPI_PLUGIN_OBJECT;
683
684
685
686
687
688
689
690
691
692
	} else {
		fprintf( stderr, "%s: line %d: invalid plugin type \"%s\".\n",
				fname, lineno, argv[1] );
		return 1;
	}
	
	numPluginArgc = argc - 4;

	if ( iType == SLAPI_PLUGIN_PREOPERATION ||
		  	iType == SLAPI_PLUGIN_EXTENDEDOP ||
Luke Howard's avatar
Luke Howard committed
693
694
			iType == SLAPI_PLUGIN_POSTOPERATION ||
			iType == SLAPI_PLUGIN_OBJECT ) {
695
696
697
		int rc;
		Slapi_PBlock *pPlugin;

698
		pPlugin = plugin_pblock_new( iType, numPluginArgc, argv );
699
700
701
702
703
		if (pPlugin == NULL) {
			return 1;
		}

		if (iType == SLAPI_PLUGIN_EXTENDEDOP) {
Luke Howard's avatar
Luke Howard committed
704
			rc = slapi_int_register_extop(be, &pGExtendedOps, pPlugin);
705
706
707
708
709
710
			if ( rc != LDAP_SUCCESS ) {
				slapi_pblock_destroy( pPlugin );
				return 1;
			}
		}

711
		rc = slapi_int_register_plugin_index( be, pPlugin, index );
712
713
		if ( rc != LDAP_SUCCESS ) {
			if ( iType == SLAPI_PLUGIN_EXTENDEDOP ) {
Luke Howard's avatar
Luke Howard committed
714
				slapi_int_unregister_extop( be, &pGExtendedOps, pPlugin );
715
716
717
718
719
720
721
722
723
			}
			slapi_pblock_destroy( pPlugin );
			return 1;
		}
	}

	return 0;
}

724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
int
slapi_int_unregister_plugin(
	Backend *be,
	Slapi_PBlock *pPlugin,
	Slapi_PBlock *pPrev
)
{
	int type;

	assert( pPlugin != NULL );

	slapi_pblock_get( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
	if ( type == SLAPI_PLUGIN_EXTENDEDOP ) {
		slapi_int_unregister_extop( be, &pGExtendedOps, pPlugin );
	}

	if ( pPrev != NULL ) {
		Slapi_PBlock *pNext = NULL;

		slapi_pblock_get( pPlugin, SLAPI_IBM_PBLOCK, &pNext );
		slapi_pblock_set( pPrev, SLAPI_IBM_PBLOCK, &pNext );
	}
	slapi_pblock_destroy( pPlugin );

	return LDAP_SUCCESS;
}

int
slapi_int_unregister_plugins(
	Backend *be,
	int index
)
{
	Slapi_PBlock	*pTmpPB = NULL;
	Slapi_PBlock	*pSavePB = NULL;
	int rc = LDAP_SUCCESS;

	pTmpPB = SLAPI_BACKEND_PBLOCK( be );
	if ( pTmpPB == NULL ) {
		return ( index < 0 ) ? LDAP_SUCCESS : LDAP_OTHER;
	}

	if ( index < 0 ) {
		/* All plugins must go */
		while ( pTmpPB != NULL && rc == LDAP_SUCCESS ) {
			pSavePB = pTmpPB;
			rc = slapi_pblock_get( pTmpPB, SLAPI_IBM_PBLOCK, &pTmpPB );
			if ( pSavePB != NULL ) {
				slapi_int_unregister_plugin( be, pSavePB, NULL );
			}
		}
	} else if ( index == 0 ) {
		slapi_pblock_get( pTmpPB, SLAPI_IBM_PBLOCK, &pSavePB );
		SLAPI_BACKEND_PBLOCK( be ) = pSavePB;
		slapi_int_unregister_plugin( be, pTmpPB, NULL );
	} else {
		int pos = -1;
		while ( pTmpPB != NULL && rc == LDAP_SUCCESS && ++pos < index ) {
			pSavePB = pTmpPB;
			rc = slapi_pblock_get( pTmpPB, SLAPI_IBM_PBLOCK, &pTmpPB );
		}
		if ( pos == index ) {
			slapi_int_unregister_plugin( be, pTmpPB, pSavePB );
		}
	}
	return rc;
}

792
793
794
795
796
797
798
void
slapi_int_plugin_unparse(
	Backend *be,
	BerVarray *out
)
{
	Slapi_PBlock *pp;
799
	int i, j;
800
801
802
803
804
805
	char **argv, ibuf[32], *ptr;
	struct berval idx, bv;

	*out = NULL;
	idx.bv_val = ibuf;
	i = 0;
806

807
	for ( pp = SLAPI_BACKEND_PBLOCK( be );
808
809
810
	      pp != NULL;
	      slapi_pblock_get( pp, SLAPI_IBM_PBLOCK, &pp ) )
	{
811
		slapi_pblock_get( pp, SLAPI_X_CONFIG_ARGV, &argv );
812
813
		if ( argv == NULL ) /* could be dynamic plugin */
			continue;
814
815
816
817
818
		idx.bv_len = snprintf( idx.bv_val, sizeof( ibuf ), "{%d}", i );
		if ( idx.bv_len >= sizeof( ibuf ) ) {
			/* FIXME: just truncating by now */
			idx.bv_len = sizeof( ibuf ) - 1;
		}
819
		bv.bv_len = idx.bv_len;
Howard Chu's avatar
Howard Chu committed
820
		for (j=1; argv[j]; j++) {
821
822
823
824
825
			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 );
Howard Chu's avatar
Howard Chu committed
826
		for (j=1; argv[j]; j++) {
827
828
829
830
831
832
			if ( j ) *ptr++ = ' ';
			ptr = lutil_strcopy( ptr, argv[j] );
		}
		ber_bvarray_add( out, &bv );
	}
}
833
#endif /* HAVE_LTDL_H */