slapi_ext.c 9.22 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 2003-2021 The OpenLDAP Foundation.
5
6
 * All rights reserved.
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted only as authorized by the OpenLDAP
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * 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>.
 */
/* (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.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by Luke Howard for inclusion
 * in OpenLDAP Software.
24
25
26
27
28
29
30
31
32
 */

#include "portable.h"

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

Luke Howard's avatar
Luke Howard committed
33
34
#ifdef LDAP_SLAPI

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
#include <slap.h>
#include <slapi.h>

/*
 * 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;
};

Luke Howard's avatar
Luke Howard committed
61
static int get_extension_block(int objecttype, void *object, struct slapi_extension_block **eblock, void **parent)
62
63
64
65
66
67
68
{
	switch ((slapi_extension_t) objecttype) {
	case SLAPI_X_EXT_CONNECTION:
		*eblock = ((Connection *)object)->c_extensions;
		*parent = NULL;
		break;	
	case SLAPI_X_EXT_OPERATION:
69
		*eblock = ((Operation *)object)->o_hdr->oh_extensions;
70
71
72
73
74
75
76
77
78
79
80
81
82
83
		*parent = ((Operation *)object)->o_conn;
		break;	
	default:
		return -1;
		break;
	}

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

	return 0;
}

Luke Howard's avatar
Luke Howard committed
84
static int map_extension_type(const char *objectname, slapi_extension_t *type)
85
86
87
88
89
90
91
92
93
94
95
96
{
	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;
}

Luke Howard's avatar
Luke Howard committed
97
98
99
static void new_extension(struct slapi_extension_block *eblock,
	int objecttype, void *object, void *parent,
	int extensionhandle )
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
{
	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;
	}
}

Luke Howard's avatar
Luke Howard committed
118
static void free_extension(struct slapi_extension_block *eblock, int objecttype, void *object, void *parent, int extensionhandle )
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
{
	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;
	}
}

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

Luke Howard's avatar
Luke Howard committed
140
	if ( get_extension_block( objecttype, object, &eblock, &parent ) != 0 ) {
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
		return NULL;
	}

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

	return NULL;
}

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

Luke Howard's avatar
Luke Howard committed
156
	if ( get_extension_block( objecttype, object, &eblock, &parent ) != 0 ) {
157
158
159
160
161
		return;
	}

	if ( extensionhandle < registered_extensions.extensions[objecttype].count ) {
		/* free the old one */
Luke Howard's avatar
Luke Howard committed
162
		free_extension( eblock, objecttype, object, parent, extensionhandle );
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

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

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)
{
	int rc;
	slapi_extension_t type;
	struct slapi_registered_extension *re;

	ldap_pvt_thread_mutex_lock( &registered_extensions.mutex );

Luke Howard's avatar
Luke Howard committed
183
	rc = map_extension_type( objectname, &type );
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
	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;
}

226
int slapi_int_create_object_extensions(int objecttype, void *object)
227
{
228
	int i;
229
230
231
232
233
234
235
236
237
238
	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:
239
		peblock = &(((Operation *)object)->o_hdr->oh_extensions);
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
		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++ ) {
Luke Howard's avatar
Luke Howard committed
264
			new_extension( eblock, objecttype, object, parent, i );
265
266
267
268
269
270
271
272
273
274
		}
	} else {
		eblock->extensions = NULL;
	}

	*peblock = eblock;

	return 0;
}

275
int slapi_int_free_object_extensions(int objecttype, void *object)
276
{
277
	int i;
278
279
280
281
282
283
284
285
286
287
	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:
288
		peblock = &(((Operation *)object)->o_hdr->oh_extensions);
289
290
291
292
293
294
295
296
297
		parent = ((Operation *)object)->o_conn;
		break;	
	default:
		return -1;
		break;
	}

	eblock = (struct slapi_extension_block *)*peblock;

298
	if ( eblock != NULL && eblock->extensions != NULL ) {
299
		for ( i = registered_extensions.extensions[objecttype].count - 1; i >= 0; --i ) {
Luke Howard's avatar
Luke Howard committed
300
			free_extension( eblock, objecttype, object, parent, i );
301
302
303
304
305
306
307
308
309
310
311
		}

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

	slapi_ch_free( peblock );

	return 0;
}

/* for reusable object types */
312
int slapi_int_clear_object_extensions(int objecttype, void *object)
313
{
314
	int i;
315
316
317
	struct slapi_extension_block *eblock;
	void *parent;

Luke Howard's avatar
Luke Howard committed
318
	if ( get_extension_block( objecttype, object, &eblock, &parent ) != 0 ) {
319
320
321
322
323
324
325
326
327
		return -1;
	}

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

	for ( i = registered_extensions.extensions[objecttype].count - 1; i >= 0; --i ) {
Luke Howard's avatar
Luke Howard committed
328
		free_extension( eblock, objecttype, object, parent, i );
329
330
331
	}

	for ( i = 0; i < registered_extensions.extensions[objecttype].count; i++ ) {
Luke Howard's avatar
Luke Howard committed
332
		new_extension( eblock, objecttype, object, parent, i );
333
334
335
336
337
	}

	return 0;
}

338
int slapi_int_init_object_extensions(void)
339
340
341
342
343
344
345
346
347
{
	memset( &registered_extensions, 0, sizeof( registered_extensions ) );

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

	return 0;
}
Luke Howard's avatar
Luke Howard committed
348
349

#endif /* LDAP_SLAPI */