summaryrefslogtreecommitdiffstats
path: root/src/backend.c
blob: 28f62755361211cba9675ed7d5d156a552f1d499 (plain)
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
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
424
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
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif

#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <nspr.h>
#include <secport.h>
#include <plarenas.h>
#include <dirsrv/slapi-plugin.h>

#include "backend.h"
#include "plugin.h"
#include "map.h"
#include "defaults.h"

/* The data we ask the map cache to keep, for us, for each map. */
struct backend_map_entry_cb_data {
	struct plugin_state *state;
	char *domain, *map, *key_attr, *value_fmt;
};

static struct backend_map_entry_cb_data *
backend_copy_cb_data(const struct backend_map_entry_cb_data *data)
{
	struct backend_map_entry_cb_data *ret;
	ret = malloc(sizeof(*ret));
	if (ret == NULL) {
		return NULL;
	}
	ret->state = data->state;
	ret->domain = strdup(data->domain);
	ret->map = strdup(data->map);
	ret->key_attr = strdup(data->key_attr);
	ret->value_fmt = strdup(data->value_fmt);
	if ((ret->domain == NULL) ||
	    (ret->map == NULL) ||
	    (ret->key_attr == NULL) ||
	    (ret->value_fmt == NULL)) {
		free(ret->domain);
		free(ret->map);
		free(ret->key_attr);
		free(ret->value_fmt);
		free(ret);
		return NULL;
	}
	return ret;
}

static void
backend_free_cb_data(void *data)
{
	struct backend_map_entry_cb_data *cb_data = data;
	if (cb_data != NULL) {
		free(cb_data->domain);
		free(cb_data->map);
		free(cb_data->key_attr);
		free(cb_data->value_fmt);
		free(cb_data);
	}
}

/* Given a map-entry directory entry, determine which keys it should have,
 * determine which value should be associated with those keys, and add them to
 * the map cache. */
static int
backend_map_entry_cb(Slapi_Entry *e, void *callback_data)
{
	struct backend_map_entry_cb_data *data;
	char *key, *value, *dn, **visited_dn;
	int i, j;
	data = callback_data;
	/* Pull out the name of the attribute which holds the key. */
	key = slapi_entry_attr_get_charptr(e, data->key_attr);
	/* Pull out the value format string and generate the value. XXX */
	value = slapi_entry_attr_get_charptr(e, data->value_fmt);
	/* Pull together the DN of this entry and the others we peeked at. */
	dn = slapi_entry_get_dn(e);
	visited_dn = NULL;
	/* If we actually generated a value, then set it for all keys. */
	if ((key != NULL) && (value != NULL)) {
		/* For each key, set the value. */
		slapi_log_error(SLAPI_LOG_PLUGIN,
				data->state->plugin_desc->spd_id,
				"setting domain/map/key/value "
				"\"%s\"/\"%s\"/\"%s\"(\"%s\")=\"%s\"\n",
				data->domain, data->map,
				key, dn, value);
		map_data_set_entry(data->state, data->domain, data->map,
				   dn, (const char **) visited_dn,
				   -1, key,
				   -1, value);
	} else {
		slapi_log_error(SLAPI_LOG_PLUGIN,
				data->state->plugin_desc->spd_id,
				"no value for %s\n", dn);
		slapi_log_error(SLAPI_LOG_PLUGIN,
				data->state->plugin_desc->spd_id,
				"unsetting domain/map/id"
				"\"%s\"/\"%s\"/(\"%s\")\n",
				data->domain, data->map, dn);
		map_data_unset_entry_id(data->state,
					data->domain, data->map,
					dn);
	}
	slapi_ch_free_string(&value);
	slapi_ch_free_string(&key);
	return 0;
}

/*
 * Generate a copy of the filter string, with specific sequences replaced:
 * %d -> name of the domain
 * %m -> name of the map
 * %% -> literal '%'
 */
static char *
backend_map_config_filter(const char *fmt, const char *domain, const char *map)
{
	char *ret;
	int i, j, l;
	l = 0;
	for (i = 0; fmt[i] != '\0'; i++) {
		if (fmt[i] == '%') {
			switch (fmt[i + 1]) {
			case 'd':
				l += strlen(domain);
				i++;
				break;
			case 'm':
				l += strlen(map);
				i++;
				break;
			case '%':
				l++;
				i++;
				break;
			default:
				l++;
				break;
			}
		} else {
			l++;
		}
	}
	ret = malloc(l + 1);
	for (i = j = 0; fmt[i] != '\0'; i++) {
		if (fmt[i] == '%') {
			switch (fmt[i + 1]) {
			case 'd':
				strcpy(ret + j, domain);
				i++;
				j += strlen(domain);
				break;
			case 'm':
				strcpy(ret + j, map);
				i++;
				j += strlen(map);
				break;
			case '%':
				i++;
				ret[j++] = fmt[i];
				break;
			default:
				ret[j++] = fmt[i];
				break;
			}
		} else {
			ret[j++] = fmt[i];
		}
	}
	ret[j] = '\0';
	return ret;
}

/* Given a directory server entry which represents a map's configuration, set
 * up and populate the map. */
static int
backend_map_config_entry(struct plugin_state *state, Slapi_Entry *e,
			 const char *domain, const char *map)
{
	Slapi_PBlock *pb;
	char **base;
	int i;
	char *entry_filter, *use_entry_filter, *key_attribute, *value_attribute;
	char *use_key_attribute, *use_value_attribute;
	char *use_attrs[3]; /* XXX */
	struct backend_map_entry_cb_data cb_data;

	pb = slapi_pblock_new();
	/* Read the NIS entry search base, entry search filter, and the names
	 * of the key and value attributes. */
	base = slapi_entry_attr_get_charray(e, "base");
	entry_filter = slapi_entry_attr_get_charptr(e, "filter");
	key_attribute = slapi_entry_attr_get_charptr(e, "key");
	value_attribute = slapi_entry_attr_get_charptr(e, "value");
	/* Perform substitutions on the filter, so that it can include the map
	 * name without having to explicitly list it.  We need this because
	 * RFC2307bis actually stores the map name in each entry, so it's
	 * useful to be able to filter on it. */
	use_entry_filter = backend_map_config_filter(entry_filter ?
						     entry_filter :
						     DEFAULT_ENTRY_FILTER,
						     domain, map);
	/* If the key and value attributes aren't set, we'll use compiled-in
	 * defaults. */
	use_key_attribute = key_attribute ?
			    key_attribute :
			    DEFAULT_KEY_ATTRIBUTE;
	use_value_attribute = value_attribute ?
			      value_attribute :
			      DEFAULT_VALUE_ATTRIBUTE;
	/* Now get ready to search for entries which need to be in the map. */
	use_attrs[0] = use_key_attribute; /* XXX */
	use_attrs[1] = use_value_attribute; /* XXX */
	use_attrs[2] = NULL; /* XXX */
	cb_data.state = state;
	cb_data.domain = (char *) domain;
	cb_data.map = (char *) map;
	cb_data.key_attr = use_key_attribute;
	cb_data.value_fmt = use_value_attribute;
	slapi_log_error(SLAPI_LOG_PLUGIN,
			state->plugin_desc->spd_id,
			"initializing map %s in %s (2)\n",
			map, domain);
	map_data_set_map(state, domain, map,
			 backend_copy_cb_data(&cb_data),
			 &backend_free_cb_data);
	map_data_clear_map(state, domain, map);
	/* Search under each base in turn, adding the matching directory
	 * entries to the NIS maps. */
	for (i = 0; (base != NULL) && (base[i] != NULL); i++) {
		slapi_log_error(SLAPI_LOG_PLUGIN,
				state->plugin_desc->spd_id,
				"searching %s for %s\n",
				base[i], use_entry_filter);
		slapi_search_internal_set_pb(pb,
					     base[i],
					     LDAP_SCOPE_SUB,
					     use_entry_filter,
					     use_attrs, 0,
					     NULL,
					     NULL,
					     state->plugin_identity,
					     0);
		slapi_search_internal_callback_pb(pb, &cb_data,
						  NULL,
						  backend_map_entry_cb,
						  NULL);
		slapi_free_search_results_internal(pb);
	}
	/* Clean up. */
	free(use_entry_filter);
	slapi_ch_free_string(&value_attribute);
	slapi_ch_free_string(&key_attribute);
	slapi_ch_free_string(&entry_filter);
	slapi_ch_array_free(base);
	slapi_pblock_destroy(pb);
	return 0;
}

/* Process a map configuration directory entry.  Pull out the domain and map
 * names which are valid for this configuration and configure such a map for
 * each in turn. */
static int
backend_map_config_entry_cb(Slapi_Entry *e, void *callback_data)
{
	char **domains, **maps;
	int i, j, ret;
	struct plugin_state *state;

	state = callback_data;
	domains = slapi_entry_attr_get_charray(e, "domain");
	maps = slapi_entry_attr_get_charray(e, "map");
	for (i = 0; (domains != NULL) && (domains[i] != NULL); i++) {
		for (j = 0; (maps != NULL) && (maps[j] != NULL); j++) {
			slapi_log_error(SLAPI_LOG_PLUGIN,
					state->plugin_desc->spd_id,
					"initializing map %s in %s\n",
					maps[j], domains[i]);
			ret = backend_map_config_entry(state, e,
						       domains[i],
						       maps[j]);
		}
	}
	slapi_ch_array_free(maps);
	slapi_ch_array_free(domains);
	return 0;
}

/* Scan for the list of configured domains and maps. */
void
backend_startup(struct plugin_state *state)
{
	Slapi_PBlock *pb;
	char *attrs = NULL;
	pb = slapi_pblock_new();
	slapi_log_error(SLAPI_LOG_PLUGIN,
			state->plugin_desc->spd_id,
			"searching \"%s\" for maps\n", state->plugin_base);
	slapi_search_internal_set_pb(pb,
				     state->plugin_base,
				     LDAP_SCOPE_ONE,
				     "(objectclass=*)",
				     NULL, 0,
				     NULL,
				     NULL,
				     state->plugin_identity,
				     0);
	slapi_search_internal_callback_pb(pb, state,
					  NULL,
					  backend_map_config_entry_cb,
					  NULL);
	slapi_free_search_results_internal(pb);
	slapi_pblock_destroy(pb);
}

/* Our postop callbacks. */

/* Add any map entries which correspond to a directory server entry in this
 * map. */
static PRBool
backend_add_entry_cb(const char *domain, const char *map, void *backend_data,
		     void *cbdata)
{
	char *dn;
	struct backend_map_entry_cb_data *data;
	data = backend_data;
	dn = cbdata;
	return PR_TRUE;
}

int
backend_add_cb(Slapi_PBlock *pb)
{
	struct plugin_state *state;
	char *dn;
	slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &state);
	slapi_pblock_get(pb, SLAPI_TARGET_DN, &dn);
	slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
			"added \"%s\"\n", dn);
	/* Add map entries which corresponded to this directory server
	 * entry. */
	if (!map_data_foreach_map(state, NULL, backend_add_entry_cb, dn)) {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"error adding map entries corresponding to "
				"\"%s\"\n", dn);
	}
	return 0;
}

int
backend_modify_cb(Slapi_PBlock *pb)
{
	struct plugin_state *state;
	char *dn, *id;
	slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &state);
	slapi_pblock_get(pb, SLAPI_TARGET_DN, &dn);
	slapi_pblock_get(pb, SLAPI_TARGET_UNIQUEID, &id);
	slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
			"modified \"%s\" (\"%s\")\n", dn, id);
	return 0;
}

int
backend_modrdn_cb(Slapi_PBlock *pb)
{
	struct plugin_state *state;
	char *dn, *id;
	slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &state);
	slapi_pblock_get(pb, SLAPI_TARGET_DN, &dn);
	slapi_pblock_get(pb, SLAPI_TARGET_UNIQUEID, &id);
	slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
			"renamed \"%s\" (\"%s\")\n", dn, id);
	return 0;
}

/* Delete any map entries which correspond to a directory server entry in this
 * map. */
static PRBool
backend_delete_entry_cb(const char *domain, const char *map, void *backend_data,
			void *cbdata)
{
	char *dn;
	struct backend_map_entry_cb_data *data;
	data = backend_data;
	dn = cbdata;
	map_data_unset_entry_id(data->state, domain, map, dn);
	return PR_TRUE;
}

/* Called by the server when a directory server entry is deleted. */
int
backend_delete_cb(Slapi_PBlock *pb)
{
	struct plugin_state *state;
	char *dn;
	slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &state);
	slapi_pblock_get(pb, SLAPI_TARGET_DN, &dn);
	slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
			"deleted \"%s\" (\"%s\")\n", dn);
	/* Remove map entries which corresponded to this directory server
	 * entry. */
	if (!map_data_foreach_map(state, NULL, backend_delete_entry_cb, dn)) {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"error removing map entries corresponding to "
				"\"%s\"\n", dn);
	}
	/* If it's a map configuration entry, remove the map. XXX */
	return 0;
}

/* Set our post-op callbacks. */
void
backend_init(Slapi_PBlock *pb, struct plugin_state *state)
{
	slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
			"hooking up postoperation callbacks\n");
	if (slapi_pblock_set(pb, SLAPI_PLUGIN_POST_ADD_FN,
			     backend_add_cb) != 0) {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"error hooking up add callback\n");
	}
	if (slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODIFY_FN,
			     backend_modify_cb) != 0) {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"error hooking up modify callback\n");
	}
	if (slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODRDN_FN,
			     backend_modrdn_cb) != 0) {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"error hooking up modrdn callback\n");
	}
	if (slapi_pblock_set(pb, SLAPI_PLUGIN_POST_DELETE_FN,
			     backend_delete_cb) != 0) {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"error hooking up delete callback\n");
	}
}

void *
xmemdup(char *region, int size)
{
	char *ret;
	ret = malloc(size + 1);
	if (ret != NULL) {
		memcpy(ret, region, size);
		ret[size] = '\0';
	}
	return ret;
}