slapi_ext.c 8.91 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
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
/*
 * (C) Copyright PADL Software Pty Ltd. 2003
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that this notice is preserved
 * and that due credit is given to PADL Software Pty Ltd. This software
 * is provided ``as is'' without express or implied warranty.
 */

#include "portable.h"

#include <ac/string.h>
#include <ac/stdarg.h>
#include <ac/ctype.h>
#include <ac/unistd.h>
#include <ldap_pvt.h>

#include <slap.h>
#include <slapi.h>

#ifdef LDAP_SLAPI
/*
 * Object extensions
 *
 * We only support two types -- connection and operation extensions.
 * Define more types in slapi.h
 */

/* global state */
struct slapi_registered_extension_set {
	ldap_pvt_thread_mutex_t mutex;
	struct slapi_registered_extension {
		int active;
		int count;
		slapi_extension_constructor_fnptr *constructors;
		slapi_extension_destructor_fnptr *destructors;
	} extensions[SLAPI_X_EXT_MAX];
} registered_extensions;

/* per-object state */
struct slapi_extension_block {
	void **extensions;
};

static int getExtensionBlock(int objecttype, void *object, struct slapi_extension_block **eblock, void **parent)
{
	switch ((slapi_extension_t) objecttype) {
	case SLAPI_X_EXT_CONNECTION:
		*eblock = ((Connection *)object)->c_extensions;
		*parent = NULL;
		break;	
	case SLAPI_X_EXT_OPERATION:
		*eblock = ((Operation *)object)->o_extensions;
		*parent = ((Operation *)object)->o_conn;
		break;	
	default:
		return -1;
		break;
	}

	if ( *eblock == NULL ) {
		return -1;
	}

	return 0;
}

static int mapExtensionType(const char *objectname, slapi_extension_t *type)
{
	if ( strcasecmp( objectname, SLAPI_EXT_CONNECTION ) == 0 ) {
		*type = SLAPI_X_EXT_CONNECTION;
	} else if ( strcasecmp( objectname, SLAPI_EXT_OPERATION ) == 0 ) {
		*type = SLAPI_X_EXT_OPERATION;
	} else {
		return -1;
	}

	return 0;
}

static void newExtension(struct slapi_extension_block *eblock, int objecttype, void *object, void *parent, int extensionhandle )
{
	slapi_extension_constructor_fnptr constructor;

	assert( objecttype < SLAPI_X_EXT_MAX );
	assert( extensionhandle < registered_extensions.extensions[objecttype].count );

	assert( registered_extensions.extensions[objecttype].constructors != NULL );
	constructor = registered_extensions.extensions[objecttype].constructors[extensionhandle];

	assert( eblock->extensions[extensionhandle] == NULL );

	if ( constructor != NULL ) {
		eblock->extensions[extensionhandle] = (*constructor)( object, parent );
	} else {
		eblock->extensions[extensionhandle] = NULL;
	}
}

static void freeExtension(struct slapi_extension_block *eblock, int objecttype, void *object, void *parent, int extensionhandle )
{
	slapi_extension_destructor_fnptr destructor;

	assert( objecttype < SLAPI_X_EXT_MAX );
	assert( extensionhandle < registered_extensions.extensions[objecttype].count );

	if ( eblock->extensions[extensionhandle] != NULL ) {
		assert( registered_extensions.extensions[objecttype].destructors != NULL );
		destructor = registered_extensions.extensions[objecttype].destructors[extensionhandle];
		if ( destructor != NULL ) {
			(*destructor)( eblock->extensions[extensionhandle], object, parent );
		}
		eblock->extensions[extensionhandle] = NULL;
	}
}
#endif /* LDAP_SLAPI */

void *slapi_get_object_extension(int objecttype, void *object, int extensionhandle)
{
#ifdef LDAP_SLAPI
	struct slapi_extension_block *eblock;
	void *parent;

	if ( getExtensionBlock( objecttype, object, &eblock, &parent ) != 0 ) {
		return NULL;
	}

	if ( extensionhandle < registered_extensions.extensions[objecttype].count ) {
		return eblock->extensions[extensionhandle];
	}

	return NULL;
#else
	return NULL;
#endif /* LDAP_SLAPI */
}

void slapi_set_object_extension(int objecttype, void *object, int extensionhandle, void *extension)
{
#ifdef LDAP_SLAPI
	struct slapi_extension_block *eblock;
	void *parent;

	if ( getExtensionBlock( objecttype, object, &eblock, &parent ) != 0 ) {
		return;
	}

	if ( extensionhandle < registered_extensions.extensions[objecttype].count ) {
		/* free the old one */
		freeExtension( eblock, objecttype, object, parent, extensionhandle );

		/* constructed by caller */
		eblock->extensions[extensionhandle] = extension;
	}
#endif /* LDAP_SLAPI */
}

int slapi_register_object_extension(
	const char *pluginname,
	const char *objectname,
	slapi_extension_constructor_fnptr constructor,
	slapi_extension_destructor_fnptr destructor,
	int *objecttype,
	int *extensionhandle)
{
#ifdef LDAP_SLAPI
	int rc;
	slapi_extension_t type;
	struct slapi_registered_extension *re;

	ldap_pvt_thread_mutex_lock( &registered_extensions.mutex );

	rc = mapExtensionType( objectname, &type );
	if ( rc != 0 ) {
		ldap_pvt_thread_mutex_unlock( &registered_extensions.mutex );
		return rc;
	}

	*objecttype = (int)type;

	re = &registered_extensions.extensions[*objecttype];

	*extensionhandle = re->count;

	if ( re->active ) {
		/* can't add new extensions after objects have been created */
		ldap_pvt_thread_mutex_unlock( &registered_extensions.mutex );
		return -1;
	}

	re->count++;

	if ( re->constructors == NULL ) {
		re->constructors = (slapi_extension_constructor_fnptr *)slapi_ch_calloc( re->count,
			sizeof( slapi_extension_constructor_fnptr ) );
	} else {
		re->constructors = (slapi_extension_constructor_fnptr *)slapi_ch_realloc( (char *)re->constructors,
			re->count * sizeof( slapi_extension_constructor_fnptr ) );
	}
	re->constructors[*extensionhandle] = constructor;

	if ( re->destructors == NULL ) {
		re->destructors = (slapi_extension_destructor_fnptr *)slapi_ch_calloc( re->count,
			sizeof( slapi_extension_destructor_fnptr ) );
	} else {
		re->destructors = (slapi_extension_destructor_fnptr *)slapi_ch_realloc( (char *)re->destructors,
			re->count * sizeof( slapi_extension_destructor_fnptr ) );
	}
	re->destructors[*extensionhandle] = destructor;

	ldap_pvt_thread_mutex_unlock( &registered_extensions.mutex );

	return 0;
#else
	return -1;
#endif /* LDAP_SLAPI */
}

int slapi_x_create_object_extensions(int objecttype, void *object)
{
#ifdef LDAP_SLAPI
	int i, rc;
	struct slapi_extension_block *eblock;
	void **peblock;
	void *parent;

	switch ((slapi_extension_t) objecttype) {
	case SLAPI_X_EXT_CONNECTION:
		peblock = &(((Connection *)object)->c_extensions);
		parent = NULL;
		break;	
	case SLAPI_X_EXT_OPERATION:
		peblock = &(((Operation *)object)->o_extensions);
		parent = ((Operation *)object)->o_conn;
		break;
	default:
		return -1;
		break;
	}

	*peblock = NULL;

	ldap_pvt_thread_mutex_lock( &registered_extensions.mutex );
	if ( registered_extensions.extensions[objecttype].active == 0 ) {
		/*
		 * once we've created some extensions, no new extensions can
		 * be registered.
		 */
		registered_extensions.extensions[objecttype].active = 1;
	}
	ldap_pvt_thread_mutex_unlock( &registered_extensions.mutex );

	eblock = (struct slapi_extension_block *)slapi_ch_calloc( 1, sizeof(*eblock) );

	if ( registered_extensions.extensions[objecttype].count ) {
		eblock->extensions = (void **)slapi_ch_calloc( registered_extensions.extensions[objecttype].count, sizeof(void *) );
		for ( i = 0; i < registered_extensions.extensions[objecttype].count; i++ ) {
			newExtension( eblock, objecttype, object, parent, i );
		}
	} else {
		eblock->extensions = NULL;
	}

	*peblock = eblock;

	return 0;
#else
	return -1;
#endif
}

int slapi_x_free_object_extensions(int objecttype, void *object)
{
#ifdef LDAP_SLAPI
	int i, rc;
	struct slapi_extension_block *eblock;
	void **peblock;
	void *parent;

	switch ((slapi_extension_t) objecttype) {
	case SLAPI_X_EXT_CONNECTION:
		peblock = &(((Connection *)object)->c_extensions);
		parent = NULL;
		break;	
	case SLAPI_X_EXT_OPERATION:
		peblock = &(((Operation *)object)->o_extensions);
		parent = ((Operation *)object)->o_conn;
		break;	
	default:
		return -1;
		break;
	}

	eblock = (struct slapi_extension_block *)*peblock;

	if ( eblock->extensions != NULL ) {
		for ( i = registered_extensions.extensions[objecttype].count - 1; i >= 0; --i ) {
			freeExtension( eblock, objecttype, object, parent, i );
		}

		slapi_ch_free( (void **)&eblock->extensions );
	}

	slapi_ch_free( peblock );

	return 0;
#else
	return -1;
#endif
}

/* for reusable object types */
int slapi_x_clear_object_extensions(int objecttype, void *object)
{
#ifdef LDAP_SLAPI
	int i, rc;
	struct slapi_extension_block *eblock;
	void *parent;

	if ( getExtensionBlock( objecttype, object, &eblock, &parent ) != 0 ) {
		return -1;
	}

	if ( eblock->extensions == NULL ) {
		/* no extensions */
		return 0;
	}

	for ( i = registered_extensions.extensions[objecttype].count - 1; i >= 0; --i ) {
		freeExtension( eblock, objecttype, object, parent, i );
	}

	for ( i = 0; i < registered_extensions.extensions[objecttype].count; i++ ) {
		 newExtension( eblock, objecttype, object, parent, i );
	}

	return 0;
#else
	return -1;
#endif
}

int slapi_x_init_object_extensions(void)
{
#ifdef LDAP_SLAPI
	memset( &registered_extensions, 0, sizeof( registered_extensions ) );

	if ( ldap_pvt_thread_mutex_init( &registered_extensions.mutex ) != 0 ) {
		return -1;
	}

	return 0;
#else
	return -1;
#endif
}