1 /* backglue.c - backend glue */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2001-2021 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 
17 /*
18  * Functions to glue a bunch of other backends into a single tree.
19  * All of the glued backends must share a common suffix. E.g., you
20  * can glue o=foo and ou=bar,o=foo but you can't glue o=foo and o=bar.
21  *
22  * The purpose of these functions is to allow you to split a single database
23  * into pieces (for load balancing purposes, whatever) but still be able
24  * to treat it as a single database after it's been split. As such, each
25  * of the glued backends should have identical rootdn.
26  *  -- Howard Chu
27  */
28 
29 #include "portable.h"
30 
31 #include <stdio.h>
32 
33 #include <ac/string.h>
34 #include <ac/socket.h>
35 
36 #define SLAPD_TOOLS
37 #include "slap.h"
38 #include "lutil.h"
39 #include "slap-config.h"
40 
41 typedef struct gluenode {
42 	BackendDB *gn_be;
43 	struct berval gn_pdn;
44 } gluenode;
45 
46 typedef struct glueinfo {
47 	int gi_nodes;
48 	struct berval gi_pdn;
49 	gluenode gi_n[1];
50 } glueinfo;
51 
52 static slap_overinst	glue;
53 
54 static int glueMode;
55 static BackendDB *glueBack;
56 static BackendDB glueBackDone;
57 #define GLUEBACK_DONE (&glueBackDone)
58 
59 static slap_overinst * glue_tool_inst( BackendInfo *bi);
60 
61 static slap_response glue_op_response;
62 
63 /* Just like select_backend, but only for our backends */
64 static BackendDB *
glue_back_select(BackendDB * be,struct berval * dn)65 glue_back_select (
66 	BackendDB *be,
67 	struct berval *dn
68 )
69 {
70 	slap_overinst	*on = (slap_overinst *)be->bd_info;
71 	glueinfo		*gi = (glueinfo *)on->on_bi.bi_private;
72 	int i;
73 
74 	for (i = gi->gi_nodes-1; i >= 0; i--) {
75 		assert( gi->gi_n[i].gn_be->be_nsuffix != NULL );
76 
77 		if (dnIsSuffix(dn, &gi->gi_n[i].gn_be->be_nsuffix[0])) {
78 			return gi->gi_n[i].gn_be;
79 		}
80 	}
81 	be->bd_info = on->on_info->oi_orig;
82 	return be;
83 }
84 
85 
86 typedef struct glue_state {
87 	char *matched;
88 	BerVarray refs;
89 	LDAPControl **ctrls;
90 	int err;
91 	int matchlen;
92 	int nrefs;
93 	int nctrls;
94 } glue_state;
95 
96 static int
glue_op_cleanup(Operation * op,SlapReply * rs)97 glue_op_cleanup( Operation *op, SlapReply *rs )
98 {
99 	/* This is not a final result */
100 	if (rs->sr_type == REP_RESULT )
101 		rs->sr_type = REP_GLUE_RESULT;
102 	return SLAP_CB_CONTINUE;
103 }
104 
105 static int
glue_op_response(Operation * op,SlapReply * rs)106 glue_op_response ( Operation *op, SlapReply *rs )
107 {
108 	glue_state *gs = op->o_callback->sc_private;
109 
110 	switch(rs->sr_type) {
111 	case REP_SEARCH:
112 	case REP_SEARCHREF:
113 	case REP_INTERMEDIATE:
114 		return SLAP_CB_CONTINUE;
115 
116 	default:
117 		if (rs->sr_err == LDAP_SUCCESS ||
118 			rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ||
119 			rs->sr_err == LDAP_TIMELIMIT_EXCEEDED ||
120 			rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
121 			rs->sr_err == LDAP_NO_SUCH_OBJECT ||
122 			gs->err != LDAP_SUCCESS)
123 			gs->err = rs->sr_err;
124 		if (gs->err == LDAP_SUCCESS && gs->matched) {
125 			ch_free (gs->matched);
126 			gs->matched = NULL;
127 			gs->matchlen = 0;
128 		}
129 		if (gs->err != LDAP_SUCCESS && rs->sr_matched) {
130 			int len;
131 			len = strlen (rs->sr_matched);
132 			if (len > gs->matchlen) {
133 				if (gs->matched)
134 					ch_free (gs->matched);
135 				gs->matched = ch_strdup (rs->sr_matched);
136 				gs->matchlen = len;
137 			}
138 		}
139 		if (rs->sr_ref) {
140 			int i, j, k;
141 			BerVarray new;
142 
143 			for (i=0; rs->sr_ref[i].bv_val; i++);
144 
145 			j = gs->nrefs;
146 			if (!j) {
147 				new = ch_malloc ((i+1)*sizeof(struct berval));
148 			} else {
149 				new = ch_realloc(gs->refs,
150 					(j+i+1)*sizeof(struct berval));
151 			}
152 			for (k=0; k<i; j++,k++) {
153 				ber_dupbv( &new[j], &rs->sr_ref[k] );
154 			}
155 			new[j].bv_val = NULL;
156 			gs->nrefs = j;
157 			gs->refs = new;
158 		}
159 		if (rs->sr_ctrls) {
160 			int i, j, k;
161 			LDAPControl **newctrls;
162 
163 			for (i=0; rs->sr_ctrls[i]; i++);
164 
165 			j = gs->nctrls;
166 			if (!j) {
167 				newctrls = op->o_tmpalloc((i+1)*sizeof(LDAPControl *),
168 					op->o_tmpmemctx);
169 			} else {
170 				/* Forget old pagedResults response if we're sending
171 				 * a new one now
172 				 */
173 				if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
174 					int newpage = 0;
175 					for ( k=0; k<i; k++ ) {
176 						if ( !strcmp(rs->sr_ctrls[k]->ldctl_oid,
177 							LDAP_CONTROL_PAGEDRESULTS )) {
178 							newpage = 1;
179 							break;
180 						}
181 					}
182 					if ( newpage ) {
183 						for ( k=0; k<j; k++ ) {
184 							if ( !strcmp(gs->ctrls[k]->ldctl_oid,
185 								LDAP_CONTROL_PAGEDRESULTS ))
186 							{
187 								op->o_tmpfree(gs->ctrls[k], op->o_tmpmemctx);
188 								gs->ctrls[k] = gs->ctrls[--j];
189 								gs->ctrls[j] = NULL;
190 								break;
191 							}
192 						}
193 					}
194 				}
195 				newctrls = op->o_tmprealloc(gs->ctrls,
196 					(j+i+1)*sizeof(LDAPControl *), op->o_tmpmemctx);
197 			}
198 			for (k=0; k<i; j++,k++) {
199 				ber_len_t oidlen = strlen( rs->sr_ctrls[k]->ldctl_oid );
200 				newctrls[j] = op->o_tmpalloc(sizeof(LDAPControl) + oidlen + 1 + rs->sr_ctrls[k]->ldctl_value.bv_len + 1,
201 					op->o_tmpmemctx);
202 				newctrls[j]->ldctl_iscritical = rs->sr_ctrls[k]->ldctl_iscritical;
203 				newctrls[j]->ldctl_oid = (char *)&newctrls[j][1];
204 				lutil_strcopy( newctrls[j]->ldctl_oid, rs->sr_ctrls[k]->ldctl_oid );
205 				if ( !BER_BVISNULL( &rs->sr_ctrls[k]->ldctl_value ) ) {
206 					newctrls[j]->ldctl_value.bv_val = &newctrls[j]->ldctl_oid[oidlen + 1];
207 					newctrls[j]->ldctl_value.bv_len = rs->sr_ctrls[k]->ldctl_value.bv_len;
208 					lutil_memcopy( newctrls[j]->ldctl_value.bv_val,
209 						rs->sr_ctrls[k]->ldctl_value.bv_val,
210 						rs->sr_ctrls[k]->ldctl_value.bv_len + 1 );
211 				} else {
212 					BER_BVZERO( &newctrls[j]->ldctl_value );
213 				}
214 			}
215 			newctrls[j] = NULL;
216 			gs->nctrls = j;
217 			gs->ctrls = newctrls;
218 		}
219 	}
220 	return 0;
221 }
222 
223 static int
glue_op_func(Operation * op,SlapReply * rs)224 glue_op_func ( Operation *op, SlapReply *rs )
225 {
226 	slap_overinst	*on = (slap_overinst *)op->o_bd->bd_info;
227 	BackendDB *b0 = op->o_bd;
228 	BackendInfo *bi0 = op->o_bd->bd_info, *bi1;
229 	slap_operation_t which = op_bind;
230 	int rc;
231 
232 	op->o_bd = glue_back_select (b0, &op->o_req_ndn);
233 
234 	/* If we're on the primary backend, let overlay framework handle it */
235 	if ( op->o_bd == b0 )
236 		return SLAP_CB_CONTINUE;
237 
238 	b0->bd_info = on->on_info->oi_orig;
239 
240 	switch(op->o_tag) {
241 	case LDAP_REQ_ADD: which = op_add; break;
242 	case LDAP_REQ_DELETE: which = op_delete; break;
243 	case LDAP_REQ_MODIFY: which = op_modify; break;
244 	case LDAP_REQ_MODRDN: which = op_modrdn; break;
245 	case LDAP_REQ_EXTENDED: which = op_extended; break;
246 	default: assert( 0 ); break;
247 	}
248 
249 	bi1 = op->o_bd->bd_info;
250 	rc = (&bi1->bi_op_bind)[ which ] ?
251 		(&bi1->bi_op_bind)[ which ]( op, rs ) : SLAP_CB_BYPASS;
252 
253 	op->o_bd = b0;
254 	op->o_bd->bd_info = bi0;
255 	return rc;
256 }
257 
258 static int
glue_op_abandon(Operation * op,SlapReply * rs)259 glue_op_abandon( Operation *op, SlapReply *rs )
260 {
261 	slap_overinst	*on = (slap_overinst *)op->o_bd->bd_info;
262 	glueinfo		*gi = (glueinfo *)on->on_bi.bi_private;
263 	BackendDB *b0 = op->o_bd;
264 	BackendInfo *bi0 = op->o_bd->bd_info;
265 	int i;
266 
267 	b0->bd_info = on->on_info->oi_orig;
268 
269 	for (i = gi->gi_nodes-1; i >= 0; i--) {
270 		assert( gi->gi_n[i].gn_be->be_nsuffix != NULL );
271 		op->o_bd = gi->gi_n[i].gn_be;
272 		if ( op->o_bd == b0 )
273 			continue;
274 		if ( op->o_bd->bd_info->bi_op_abandon )
275 			op->o_bd->bd_info->bi_op_abandon( op, rs );
276 	}
277 	op->o_bd = b0;
278 	op->o_bd->bd_info = bi0;
279 	return SLAP_CB_CONTINUE;
280 }
281 
282 static int
glue_response(Operation * op,SlapReply * rs)283 glue_response ( Operation *op, SlapReply *rs )
284 {
285 	BackendDB *be = op->o_bd;
286 	be = glue_back_select (op->o_bd, &op->o_req_ndn);
287 
288 	/* If we're on the primary backend, let overlay framework handle it.
289 	 * Otherwise, bail out.
290 	 */
291 	return ( op->o_bd == be ) ? SLAP_CB_CONTINUE : SLAP_CB_BYPASS;
292 }
293 
294 static int
glue_chk_referrals(Operation * op,SlapReply * rs)295 glue_chk_referrals ( Operation *op, SlapReply *rs )
296 {
297 	slap_overinst	*on = (slap_overinst *)op->o_bd->bd_info;
298 	BackendDB *b0 = op->o_bd;
299 	BackendInfo *bi0 = op->o_bd->bd_info;
300 	int rc;
301 
302 	op->o_bd = glue_back_select (b0, &op->o_req_ndn);
303 	if ( op->o_bd == b0 )
304 		return SLAP_CB_CONTINUE;
305 
306 	b0->bd_info = on->on_info->oi_orig;
307 
308 	if ( op->o_bd->bd_info->bi_chk_referrals )
309 		rc = ( *op->o_bd->bd_info->bi_chk_referrals )( op, rs );
310 	else
311 		rc = SLAP_CB_CONTINUE;
312 
313 	op->o_bd = b0;
314 	op->o_bd->bd_info = bi0;
315 	return rc;
316 }
317 
318 static int
glue_chk_controls(Operation * op,SlapReply * rs)319 glue_chk_controls ( Operation *op, SlapReply *rs )
320 {
321 	slap_overinst	*on = (slap_overinst *)op->o_bd->bd_info;
322 	BackendDB *b0 = op->o_bd;
323 	BackendInfo *bi0 = op->o_bd->bd_info;
324 	int rc = SLAP_CB_CONTINUE;
325 
326 	op->o_bd = glue_back_select (b0, &op->o_req_ndn);
327 	if ( op->o_bd == b0 )
328 		return SLAP_CB_CONTINUE;
329 
330 	b0->bd_info = on->on_info->oi_orig;
331 
332 	/* if the subordinate database has overlays, the bi_chk_controls()
333 	 * hook is actually over_aux_chk_controls(); in case it actually
334 	 * wraps a missing hok, we need to mimic the behavior
335 	 * of the frontend applied to that database */
336 	if ( op->o_bd->bd_info->bi_chk_controls ) {
337 		rc = ( *op->o_bd->bd_info->bi_chk_controls )( op, rs );
338 	}
339 
340 
341 	if ( rc == SLAP_CB_CONTINUE ) {
342 		rc = backend_check_controls( op, rs );
343 	}
344 
345 	op->o_bd = b0;
346 	op->o_bd->bd_info = bi0;
347 	return rc;
348 }
349 
350 /* ITS#4615 - overlays configured above the glue overlay should be
351  * invoked for the entire glued tree. Overlays configured below the
352  * glue overlay should only be invoked on the primary backend.
353  * So, if we're searching on any subordinates, we need to force the
354  * current overlay chain to stop processing, without stopping the
355  * overall callback flow.
356  */
357 static int
glue_sub_search(Operation * op,SlapReply * rs,BackendDB * b0,slap_overinst * on)358 glue_sub_search( Operation *op, SlapReply *rs, BackendDB *b0,
359 	slap_overinst *on )
360 {
361 	/* Process any overlays on the primary backend */
362 	if ( op->o_bd == b0 && on->on_next ) {
363 		BackendInfo *bi = op->o_bd->bd_info;
364 		int rc = SLAP_CB_CONTINUE;
365 		for ( on=on->on_next; on; on=on->on_next ) {
366 			op->o_bd->bd_info = (BackendInfo *)on;
367 			if ( on->on_bi.bi_op_search ) {
368 				rc = on->on_bi.bi_op_search( op, rs );
369 				if ( rc != SLAP_CB_CONTINUE )
370 					break;
371 			}
372 		}
373 		op->o_bd->bd_info = bi;
374 		if ( rc != SLAP_CB_CONTINUE )
375 			return rc;
376 	}
377 	return op->o_bd->be_search( op, rs );
378 }
379 
380 static const ID glueID = NOID;
381 static const struct berval gluecookie = { sizeof( glueID ), (char *)&glueID };
382 
383 static int
glue_op_search(Operation * op,SlapReply * rs)384 glue_op_search ( Operation *op, SlapReply *rs )
385 {
386 	slap_overinst	*on = (slap_overinst *)op->o_bd->bd_info;
387 	glueinfo		*gi = (glueinfo *)on->on_bi.bi_private;
388 	BackendDB *b0 = op->o_bd;
389 	BackendDB *b1 = NULL, *btmp;
390 	BackendInfo *bi0 = op->o_bd->bd_info;
391 	int i;
392 	long stoptime = 0, starttime;
393 	glue_state gs = {NULL, NULL, NULL, 0, 0, 0, 0};
394 	slap_callback cb = { NULL, glue_op_response, glue_op_cleanup, NULL };
395 	int scope0, tlimit0;
396 	struct berval dn, ndn, *pdn;
397 
398 	cb.sc_private = &gs;
399 
400 	cb.sc_next = op->o_callback;
401 
402 	starttime = op->o_time;
403 	stoptime = slap_get_time () + op->ors_tlimit;
404 
405 	/* reset dummy cookie used to keep paged results going across databases */
406 	if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED
407 		&& bvmatch( &((PagedResultsState *)op->o_pagedresults_state)->ps_cookieval, &gluecookie ) )
408 	{
409 		PagedResultsState *ps = op->o_pagedresults_state;
410 		BerElementBuffer berbuf;
411 		BerElement *ber = (BerElement *)&berbuf;
412 		struct berval cookie = BER_BVC(""), value;
413 		int c;
414 
415 		for (c = 0; op->o_ctrls[c] != NULL; c++) {
416 			if (strcmp(op->o_ctrls[c]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS) == 0)
417 				break;
418 		}
419 
420 		assert( op->o_ctrls[c] != NULL );
421 
422 		ber_init2( ber, NULL, LBER_USE_DER );
423 		ber_printf( ber, "{iO}", ps->ps_size, &cookie );
424 		ber_flatten2( ber, &value, 0 );
425 		assert( op->o_ctrls[c]->ldctl_value.bv_len >= value.bv_len );
426 		op->o_ctrls[c]->ldctl_value.bv_len = value.bv_len;
427 		lutil_memcopy( op->o_ctrls[c]->ldctl_value.bv_val,
428 			value.bv_val, value.bv_len );
429 		ber_free_buf( ber );
430 
431 		ps->ps_cookie = (PagedResultsCookie)0;
432 		BER_BVZERO( &ps->ps_cookieval );
433 	}
434 
435 	op->o_bd = glue_back_select (b0, &op->o_req_ndn);
436 	b0->bd_info = on->on_info->oi_orig;
437 
438 	switch (op->ors_scope) {
439 	case LDAP_SCOPE_BASE:
440 		if ( op->o_bd == b0 )
441 			return SLAP_CB_CONTINUE;
442 
443 		if (op->o_bd && op->o_bd->be_search) {
444 			rs->sr_err = op->o_bd->be_search( op, rs );
445 		} else {
446 			rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
447 		}
448 		return rs->sr_err;
449 
450 	case LDAP_SCOPE_ONELEVEL:
451 	case LDAP_SCOPE_SUBTREE:
452 	case LDAP_SCOPE_SUBORDINATE: /* FIXME */
453 		op->o_callback = &cb;
454 		rs->sr_err = gs.err = LDAP_UNWILLING_TO_PERFORM;
455 		scope0 = op->ors_scope;
456 		tlimit0 = op->ors_tlimit;
457 		dn = op->o_req_dn;
458 		ndn = op->o_req_ndn;
459 		b1 = op->o_bd;
460 
461 		/*
462 		 * Execute in reverse order, most specific first
463 		 */
464 		for (i = gi->gi_nodes; i >= 0; i--) {
465 			if ( i == gi->gi_nodes ) {
466 				btmp = b0;
467 				pdn = &gi->gi_pdn;
468 			} else {
469 				btmp = gi->gi_n[i].gn_be;
470 				pdn = &gi->gi_n[i].gn_pdn;
471 			}
472 			if (!btmp || !btmp->be_search)
473 				continue;
474 			if (!dnIsSuffix(&btmp->be_nsuffix[0], &b1->be_nsuffix[0]))
475 				continue;
476 			if (get_no_subordinate_glue(op) && btmp != b1)
477 				continue;
478 			/* If we remembered which backend we were on before,
479 			 * skip down to it now
480 			 */
481 			if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED &&
482 				op->o_conn->c_pagedresults_state.ps_be &&
483 				op->o_conn->c_pagedresults_state.ps_be != btmp )
484 				continue;
485 
486 			if (tlimit0 != SLAP_NO_LIMIT) {
487 				op->o_time = slap_get_time();
488 				op->ors_tlimit = stoptime - op->o_time;
489 				if (op->ors_tlimit <= 0) {
490 					rs->sr_err = gs.err = LDAP_TIMELIMIT_EXCEEDED;
491 					break;
492 				}
493 			}
494 			rs->sr_err = 0;
495 			/*
496 			 * check for abandon
497 			 */
498 			if (op->o_abandon) {
499 				goto end_of_loop;
500 			}
501 			op->o_bd = btmp;
502 
503 			assert( op->o_bd->be_suffix != NULL );
504 			assert( op->o_bd->be_nsuffix != NULL );
505 
506 			if (scope0 == LDAP_SCOPE_ONELEVEL &&
507 				dn_match(pdn, &ndn))
508 			{
509 				struct berval mdn, mndn;
510 				op->ors_scope = LDAP_SCOPE_BASE;
511 				mdn = op->o_req_dn = op->o_bd->be_suffix[0];
512 				mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
513 				rs->sr_err = op->o_bd->be_search(op, rs);
514 				if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
515 					gs.err = LDAP_SUCCESS;
516 				}
517 				op->ors_scope = LDAP_SCOPE_ONELEVEL;
518 				if ( op->o_req_dn.bv_val == mdn.bv_val )
519 					op->o_req_dn = dn;
520 				if ( op->o_req_ndn.bv_val == mndn.bv_val )
521 					op->o_req_ndn = ndn;
522 
523 			} else if (scope0 == LDAP_SCOPE_SUBTREE &&
524 				dn_match(&op->o_bd->be_nsuffix[0], &ndn))
525 			{
526 				rs->sr_err = glue_sub_search( op, rs, b0, on );
527 
528 			} else if (scope0 == LDAP_SCOPE_SUBTREE &&
529 				dnIsSuffix(&op->o_bd->be_nsuffix[0], &ndn))
530 			{
531 				struct berval mdn, mndn;
532 				mdn = op->o_req_dn = op->o_bd->be_suffix[0];
533 				mndn = op->o_req_ndn = op->o_bd->be_nsuffix[0];
534 				rs->sr_err = glue_sub_search( op, rs, b0, on );
535 				if ( rs->sr_err == LDAP_NO_SUCH_OBJECT ) {
536 					gs.err = LDAP_SUCCESS;
537 				}
538 				if ( op->o_req_dn.bv_val == mdn.bv_val )
539 					op->o_req_dn = dn;
540 				if ( op->o_req_ndn.bv_val == mndn.bv_val )
541 					op->o_req_ndn = ndn;
542 
543 			} else if (dnIsSuffix(&ndn, &op->o_bd->be_nsuffix[0])) {
544 				rs->sr_err = glue_sub_search( op, rs, b0, on );
545 			}
546 
547 			switch ( gs.err ) {
548 
549 			/*
550 			 * Add errors that should result in dropping
551 			 * the search
552 			 */
553 			case LDAP_SIZELIMIT_EXCEEDED:
554 			case LDAP_TIMELIMIT_EXCEEDED:
555 			case LDAP_ADMINLIMIT_EXCEEDED:
556 			case LDAP_NO_SUCH_OBJECT:
557 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
558 			case LDAP_X_CANNOT_CHAIN:
559 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
560 				goto end_of_loop;
561 
562 			case LDAP_SUCCESS:
563 				if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
564 					PagedResultsState *ps = op->o_pagedresults_state;
565 
566 					/* Assume this backend can be forgotten now */
567 					op->o_conn->c_pagedresults_state.ps_be = NULL;
568 
569 					/* If we have a full page, exit the loop. We may
570 					 * need to remember this backend so we can continue
571 					 * from here on a subsequent request.
572 					 */
573 					if ( rs->sr_nentries >= ps->ps_size ) {
574 						PagedResultsState *cps = &op->o_conn->c_pagedresults_state;
575 
576 						/* Don't bother to remember the first backend.
577 						 * Only remember the last one if there's more state left.
578 						 */
579 						if ( op->o_bd != b0 &&
580 							( cps->ps_cookie != NOID
581 								|| !BER_BVISNULL( &cps->ps_cookieval )
582 								|| op->o_bd != gi->gi_n[0].gn_be ) )
583 						{
584 							op->o_conn->c_pagedresults_state.ps_be = op->o_bd;
585 						}
586 
587 						/* Check whether the cookie is empty,
588 						 * and give remaining databases a chance
589 						 */
590 						if ( op->o_bd != gi->gi_n[0].gn_be || cps->ps_cookie == NOID ) {
591 							int		c;
592 
593 							for ( c = 0; gs.ctrls[c] != NULL; c++ ) {
594 								if ( strcmp( gs.ctrls[c]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 ) {
595 									break;
596 								}
597 							}
598 
599 							if ( gs.ctrls[c] != NULL ) {
600 								BerElementBuffer berbuf;
601 								BerElement	*ber = (BerElement *)&berbuf;
602 								ber_tag_t	tag;
603 								ber_int_t	size;
604 								struct berval	cookie, value;
605 
606 								ber_init2( ber, &gs.ctrls[c]->ldctl_value, LBER_USE_DER );
607 
608 								tag = ber_scanf( ber, "{im}", &size, &cookie );
609 								assert( tag != LBER_ERROR );
610 
611 								if ( BER_BVISEMPTY( &cookie ) && op->o_bd != gi->gi_n[0].gn_be ) {
612 									/* delete old, create new cookie with NOID */
613 									PagedResultsCookie respcookie = (PagedResultsCookie)NOID;
614 									ber_len_t oidlen = strlen( gs.ctrls[c]->ldctl_oid );
615 									LDAPControl *newctrl;
616 
617 									/* it's next database's turn */
618 									if ( btmp == b0 ) {
619 										op->o_conn->c_pagedresults_state.ps_be = gi->gi_n[gi->gi_nodes - 1].gn_be;
620 
621 									} else {
622 										op->o_conn->c_pagedresults_state.ps_be = gi->gi_n[(i > 0 ? i - 1: 0)].gn_be;
623 									}
624 
625 									cookie.bv_val = (char *)&respcookie;
626 									cookie.bv_len = sizeof( PagedResultsCookie );
627 
628 									ber_init2( ber, NULL, LBER_USE_DER );
629 									ber_printf( ber, "{iO}", 0, &cookie );
630 									ber_flatten2( ber, &value, 0 );
631 
632 									newctrl = op->o_tmprealloc( gs.ctrls[c],
633 										sizeof(LDAPControl) + oidlen + 1 + value.bv_len + 1,
634 										op->o_tmpmemctx);
635 									newctrl->ldctl_iscritical = gs.ctrls[c]->ldctl_iscritical;
636 									newctrl->ldctl_oid = (char *)&newctrl[1];
637 									lutil_strcopy( newctrl->ldctl_oid, gs.ctrls[c]->ldctl_oid );
638 									newctrl->ldctl_value.bv_len = value.bv_len;
639 									lutil_memcopy( newctrl->ldctl_value.bv_val,
640 										value.bv_val, value.bv_len );
641 
642 									gs.ctrls[c] = newctrl;
643 
644 									ber_free_buf( ber );
645 
646 								} else if ( !BER_BVISEMPTY( &cookie ) && op->o_bd != b0 ) {
647 									/* if cookie not empty, it's again this database's turn */
648 									op->o_conn->c_pagedresults_state.ps_be = op->o_bd;
649 								}
650 							}
651 						}
652 
653 						goto end_of_loop;
654 					}
655 
656 					/* This backend has run out of entries, but more responses
657 					 * can fit in the page. Fake a reset of the state so the
658 					 * next backend will start up properly. Only back-[bh]db
659 					 * and back-sql look at this state info.
660 					 */
661 					ps->ps_cookie = (PagedResultsCookie)0;
662 					BER_BVZERO( &ps->ps_cookieval );
663 
664 					{
665 						/* change the size of the page in the request
666 						 * that will be propagated, and reset the cookie */
667 						BerElementBuffer berbuf;
668 						BerElement *ber = (BerElement *)&berbuf;
669 						int size = ps->ps_size - rs->sr_nentries;
670 						struct berval cookie = BER_BVC(""), value;
671 						int c;
672 
673 						for (c = 0; op->o_ctrls[c] != NULL; c++) {
674 							if (strcmp(op->o_ctrls[c]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS) == 0)
675 								break;
676 						}
677 
678 						assert( op->o_ctrls[c] != NULL );
679 
680 						ber_init2( ber, NULL, LBER_USE_DER );
681 						ber_printf( ber, "{iO}", size, &cookie );
682 						ber_flatten2( ber, &value, 0 );
683 						assert( op->o_ctrls[c]->ldctl_value.bv_len >= value.bv_len );
684 						op->o_ctrls[c]->ldctl_value.bv_len = value.bv_len;
685 						lutil_memcopy( op->o_ctrls[c]->ldctl_value.bv_val,
686 							value.bv_val, value.bv_len );
687 						ber_free_buf( ber );
688 					}
689 				}
690 
691 			default:
692 				break;
693 			}
694 		}
695 end_of_loop:;
696 		op->ors_scope = scope0;
697 		op->ors_tlimit = tlimit0;
698 		op->o_time = starttime;
699 
700 		break;
701 	}
702 
703 	op->o_callback = cb.sc_next;
704 	if ( op->o_abandon ) {
705 		rs->sr_err = SLAPD_ABANDON;
706 	} else {
707 		rs->sr_err = gs.err;
708 		rs->sr_matched = gs.matched;
709 		rs->sr_ref = gs.refs;
710 	}
711 	rs->sr_ctrls = gs.ctrls;
712 
713 	send_ldap_result( op, rs );
714 
715 	op->o_bd = b0;
716 	op->o_bd->bd_info = bi0;
717 	if (gs.matched)
718 		free (gs.matched);
719 	if (gs.refs)
720 		ber_bvarray_free(gs.refs);
721 	if (gs.ctrls) {
722 		for (i = gs.nctrls; --i >= 0; ) {
723 			op->o_tmpfree(gs.ctrls[i], op->o_tmpmemctx);
724 		}
725 		op->o_tmpfree(gs.ctrls, op->o_tmpmemctx);
726 	}
727 	return rs->sr_err;
728 }
729 
730 static BackendDB toolDB;
731 
732 static int
glue_tool_entry_open(BackendDB * b0,int mode)733 glue_tool_entry_open (
734 	BackendDB *b0,
735 	int mode
736 )
737 {
738 	slap_overinfo	*oi = (slap_overinfo *)b0->bd_info;
739 
740 	/* We don't know which backend to talk to yet, so just
741 	 * remember the mode and move on...
742 	 */
743 
744 	glueMode = mode;
745 	glueBack = NULL;
746 	toolDB = *b0;
747 	toolDB.bd_info = oi->oi_orig;
748 
749 	/* Sanity checks */
750 	{
751 		slap_overinst *on = glue_tool_inst( b0->bd_info );
752 		glueinfo	*gi = on->on_bi.bi_private;
753 
754 		int i;
755 		for (i = 0; i < gi->gi_nodes; i++) {
756 			BackendDB *bd;
757 			struct berval pdn;
758 
759 			dnParent( &gi->gi_n[i].gn_be->be_nsuffix[0], &pdn );
760 			bd = select_backend( &pdn, 0 );
761 			if ( bd ) {
762 				ID id;
763 				BackendDB db;
764 
765 				if ( overlay_is_over( bd ) ) {
766 					slap_overinfo *oi = (slap_overinfo *)bd->bd_info;
767 					db = *bd;
768 					db.bd_info = oi->oi_orig;
769 					bd = &db;
770 				}
771 
772 				if ( !bd->bd_info->bi_tool_dn2id_get
773 					|| !bd->bd_info->bi_tool_entry_open
774 					|| !bd->bd_info->bi_tool_entry_close )
775 				{
776 					continue;
777 				}
778 
779 				bd->bd_info->bi_tool_entry_open( bd, 0 );
780 				id = bd->bd_info->bi_tool_dn2id_get( bd, &gi->gi_n[i].gn_be->be_nsuffix[0] );
781 				bd->bd_info->bi_tool_entry_close( bd );
782 				if ( id != NOID ) {
783 					Debug( LDAP_DEBUG_ANY,
784 						"glue_tool_entry_open: subordinate database suffix entry DN=\"%s\" also present in superior database rooted at DN=\"%s\"\n",
785 						gi->gi_n[i].gn_be->be_suffix[0].bv_val, bd->be_suffix[0].bv_val );
786 					return LDAP_OTHER;
787 				}
788 			}
789 		}
790 	}
791 
792 	return 0;
793 }
794 
795 static int
glue_tool_entry_close(BackendDB * b0)796 glue_tool_entry_close (
797 	BackendDB *b0
798 )
799 {
800 	int rc = 0;
801 
802 	if (glueBack && glueBack != GLUEBACK_DONE) {
803 		if (!glueBack->be_entry_close)
804 			return 0;
805 		rc = glueBack->be_entry_close (glueBack);
806 	}
807 	return rc;
808 }
809 
810 static slap_overinst *
glue_tool_inst(BackendInfo * bi)811 glue_tool_inst(
812 	BackendInfo *bi
813 )
814 {
815 	slap_overinfo	*oi = (slap_overinfo *)bi;
816 	slap_overinst	*on;
817 
818 	for ( on = oi->oi_list; on; on=on->on_next ) {
819 		if ( !strcmp( on->on_bi.bi_type, glue.on_bi.bi_type ))
820 			return on;
821 	}
822 	return NULL;
823 }
824 
825 /* This function will only be called in tool mode */
826 static int
glue_open(BackendInfo * bi)827 glue_open (
828 	BackendInfo *bi
829 )
830 {
831 	slap_overinst *on = glue_tool_inst( bi );
832 	glueinfo		*gi = on->on_bi.bi_private;
833 	static int glueOpened = 0;
834 	int i, j, same, bsame = 0, rc = 0;
835 	ConfigReply cr = {0};
836 
837 	if (glueOpened) return 0;
838 
839 	glueOpened = 1;
840 
841 	/* If we were invoked in tool mode, open all the underlying backends */
842 	if (slapMode & SLAP_TOOL_MODE) {
843 		for (i = 0; i<gi->gi_nodes; i++) {
844 			same = 0;
845 			/* Same bi_open as our main backend? */
846 			if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
847 				on->on_info->oi_orig->bi_open )
848 				bsame = 1;
849 
850 			/* Loop thru the bd_info's and make sure we only
851 			 * invoke their bi_open functions once each.
852 			 */
853 			for ( j = 0; j<i; j++ ) {
854 				if ( gi->gi_n[i].gn_be->bd_info->bi_open ==
855 					gi->gi_n[j].gn_be->bd_info->bi_open ) {
856 					same = 1;
857 					break;
858 				}
859 			}
860 			/* OK, it's unique and non-NULL, call it. */
861 			if ( !same && gi->gi_n[i].gn_be->bd_info->bi_open )
862 				rc = gi->gi_n[i].gn_be->bd_info->bi_open(
863 					gi->gi_n[i].gn_be->bd_info );
864 			/* Let backend.c take care of the rest of startup */
865 			if ( !rc )
866 				rc = backend_startup_one( gi->gi_n[i].gn_be, &cr );
867 			if ( rc ) break;
868 		}
869 		if ( !rc && !bsame && on->on_info->oi_orig->bi_open )
870 			rc = on->on_info->oi_orig->bi_open( on->on_info->oi_orig );
871 
872 	} /* other case is impossible */
873 	return rc;
874 }
875 
876 /* This function will only be called in tool mode */
877 static int
glue_close(BackendInfo * bi)878 glue_close (
879 	BackendInfo *bi
880 )
881 {
882 	static int glueClosed = 0;
883 	int rc = 0;
884 
885 	if (glueClosed) return 0;
886 
887 	glueClosed = 1;
888 
889 	if (slapMode & SLAP_TOOL_MODE) {
890 		rc = backend_shutdown( NULL );
891 	}
892 	return rc;
893 }
894 
895 static int
glue_entry_get_rw(Operation * op,struct berval * dn,ObjectClass * oc,AttributeDescription * ad,int rw,Entry ** e)896 glue_entry_get_rw (
897 	Operation		*op,
898 	struct berval	*dn,
899 	ObjectClass		*oc,
900 	AttributeDescription	*ad,
901 	int	rw,
902 	Entry	**e )
903 {
904 	int rc;
905 	BackendDB *b0 = op->o_bd;
906 	op->o_bd = glue_back_select( b0, dn );
907 
908 	if ( op->o_bd->be_fetch ) {
909 		rc = op->o_bd->be_fetch( op, dn, oc, ad, rw, e );
910 	} else {
911 		rc = LDAP_UNWILLING_TO_PERFORM;
912 	}
913 	op->o_bd =b0;
914 	return rc;
915 }
916 
917 static int
glue_entry_release_rw(Operation * op,Entry * e,int rw)918 glue_entry_release_rw (
919 	Operation *op,
920 	Entry *e,
921 	int rw
922 )
923 {
924 	BackendDB *b0 = op->o_bd;
925 	int rc = -1;
926 
927 	op->o_bd = glue_back_select (b0, &e->e_nname);
928 
929 	if ( op->o_bd->be_release ) {
930 		rc = op->o_bd->be_release( op, e, rw );
931 
932 	} else {
933 		/* FIXME: mimic be_entry_release_rw
934 		 * when no be_release() available */
935 		/* free entry */
936 		entry_free( e );
937 		rc = 0;
938 	}
939 	op->o_bd = b0;
940 	return rc;
941 }
942 
943 static struct berval *glue_base;
944 static int glue_scope;
945 static Filter *glue_filter;
946 
947 static ID
glue_tool_entry_first(BackendDB * b0)948 glue_tool_entry_first (
949 	BackendDB *b0
950 )
951 {
952 	slap_overinst	*on = glue_tool_inst( b0->bd_info );
953 	glueinfo		*gi = on->on_bi.bi_private;
954 	int i;
955 	ID rc;
956 
957 	/* If we're starting from scratch, start at the most general */
958 	if (!glueBack) {
959 		if ( toolDB.be_entry_open && toolDB.be_entry_first ) {
960 			glueBack = &toolDB;
961 		} else {
962 			for (i = gi->gi_nodes-1; i >= 0; i--) {
963 				if (gi->gi_n[i].gn_be->be_entry_open &&
964 					gi->gi_n[i].gn_be->be_entry_first) {
965 						glueBack = gi->gi_n[i].gn_be;
966 					break;
967 				}
968 			}
969 		}
970 	}
971 	if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first ||
972 		glueBack->be_entry_open (glueBack, glueMode) != 0)
973 		return NOID;
974 
975 	rc = glueBack->be_entry_first (glueBack);
976 	while ( rc == NOID ) {
977 		if ( glueBack && glueBack->be_entry_close )
978 			glueBack->be_entry_close (glueBack);
979 		for (i=0; i<gi->gi_nodes; i++) {
980 			if (gi->gi_n[i].gn_be == glueBack)
981 				break;
982 		}
983 		if (i == 0) {
984 			glueBack = GLUEBACK_DONE;
985 			break;
986 		} else {
987 			glueBack = gi->gi_n[i-1].gn_be;
988 			rc = glue_tool_entry_first (b0);
989 			if ( glueBack == GLUEBACK_DONE ) {
990 				break;
991 			}
992 		}
993 	}
994 	return rc;
995 }
996 
997 static ID
glue_tool_entry_first_x(BackendDB * b0,struct berval * base,int scope,Filter * f)998 glue_tool_entry_first_x (
999 	BackendDB *b0,
1000 	struct berval *base,
1001 	int scope,
1002 	Filter *f
1003 )
1004 {
1005 	slap_overinst	*on = glue_tool_inst( b0->bd_info );
1006 	glueinfo		*gi = on->on_bi.bi_private;
1007 	int i;
1008 	ID rc;
1009 
1010 	glue_base = base;
1011 	glue_scope = scope;
1012 	glue_filter = f;
1013 
1014 	/* If we're starting from scratch, start at the most general */
1015 	if (!glueBack) {
1016 		if ( toolDB.be_entry_open && toolDB.be_entry_first_x ) {
1017 			glueBack = &toolDB;
1018 		} else {
1019 			for (i = gi->gi_nodes-1; i >= 0; i--) {
1020 				if (gi->gi_n[i].gn_be->be_entry_open &&
1021 					gi->gi_n[i].gn_be->be_entry_first_x)
1022 				{
1023 					glueBack = gi->gi_n[i].gn_be;
1024 					break;
1025 				}
1026 			}
1027 		}
1028 	}
1029 	if (!glueBack || !glueBack->be_entry_open || !glueBack->be_entry_first_x ||
1030 		glueBack->be_entry_open (glueBack, glueMode) != 0)
1031 		return NOID;
1032 
1033 	rc = glueBack->be_entry_first_x (glueBack,
1034 		glue_base, glue_scope, glue_filter);
1035 	while ( rc == NOID ) {
1036 		if ( glueBack && glueBack->be_entry_close )
1037 			glueBack->be_entry_close (glueBack);
1038 		for (i=0; i<gi->gi_nodes; i++) {
1039 			if (gi->gi_n[i].gn_be == glueBack)
1040 				break;
1041 		}
1042 		if (i == 0) {
1043 			glueBack = GLUEBACK_DONE;
1044 			break;
1045 		} else {
1046 			glueBack = gi->gi_n[i-1].gn_be;
1047 			rc = glue_tool_entry_first_x (b0,
1048 				glue_base, glue_scope, glue_filter);
1049 			if ( glueBack == GLUEBACK_DONE ) {
1050 				break;
1051 			}
1052 		}
1053 	}
1054 	return rc;
1055 }
1056 
1057 static ID
glue_tool_entry_next(BackendDB * b0)1058 glue_tool_entry_next (
1059 	BackendDB *b0
1060 )
1061 {
1062 	slap_overinst	*on = glue_tool_inst( b0->bd_info );
1063 	glueinfo		*gi = on->on_bi.bi_private;
1064 	int i;
1065 	ID rc;
1066 
1067 	if (!glueBack || !glueBack->be_entry_next)
1068 		return NOID;
1069 
1070 	rc = glueBack->be_entry_next (glueBack);
1071 
1072 	/* If we ran out of entries in one database, move on to the next */
1073 	while (rc == NOID) {
1074 		if ( glueBack && glueBack->be_entry_close )
1075 			glueBack->be_entry_close (glueBack);
1076 		for (i=0; i<gi->gi_nodes; i++) {
1077 			if (gi->gi_n[i].gn_be == glueBack)
1078 				break;
1079 		}
1080 		if (i == 0) {
1081 			glueBack = GLUEBACK_DONE;
1082 			break;
1083 		} else {
1084 			glueBack = gi->gi_n[i-1].gn_be;
1085 			if ( glue_base || glue_filter ) {
1086 				/* using entry_first_x() */
1087 				rc = glue_tool_entry_first_x (b0,
1088 					glue_base, glue_scope, glue_filter);
1089 
1090 			} else {
1091 				/* using entry_first() */
1092 				rc = glue_tool_entry_first (b0);
1093 			}
1094 			if ( glueBack == GLUEBACK_DONE ) {
1095 				break;
1096 			}
1097 		}
1098 	}
1099 	return rc;
1100 }
1101 
1102 static ID
glue_tool_dn2id_get(BackendDB * b0,struct berval * dn)1103 glue_tool_dn2id_get (
1104 	BackendDB *b0,
1105 	struct berval *dn
1106 )
1107 {
1108 	BackendDB *be, b2;
1109 	int rc = -1;
1110 
1111 	b2 = *b0;
1112 	b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
1113 	be = glue_back_select (&b2, dn);
1114 	if ( be == &b2 ) be = &toolDB;
1115 
1116 	if (!be->be_dn2id_get)
1117 		return NOID;
1118 
1119 	if (!glueBack) {
1120 		if ( be->be_entry_open ) {
1121 			rc = be->be_entry_open (be, glueMode);
1122 		}
1123 		if (rc != 0) {
1124 			return NOID;
1125 		}
1126 	} else if (be != glueBack) {
1127 		/* If this entry belongs in a different branch than the
1128 		 * previous one, close the current database and open the
1129 		 * new one.
1130 		 */
1131 		if ( glueBack->be_entry_close ) {
1132 			glueBack->be_entry_close (glueBack);
1133 		}
1134 		if ( be->be_entry_open ) {
1135 			rc = be->be_entry_open (be, glueMode);
1136 		}
1137 		if (rc != 0) {
1138 			return NOID;
1139 		}
1140 	}
1141 	glueBack = be;
1142 	return be->be_dn2id_get (be, dn);
1143 }
1144 
1145 static Entry *
glue_tool_entry_get(BackendDB * b0,ID id)1146 glue_tool_entry_get (
1147 	BackendDB *b0,
1148 	ID id
1149 )
1150 {
1151 	if (!glueBack || !glueBack->be_entry_get)
1152 		return NULL;
1153 
1154 	return glueBack->be_entry_get (glueBack, id);
1155 }
1156 
1157 static ID
glue_tool_entry_put(BackendDB * b0,Entry * e,struct berval * text)1158 glue_tool_entry_put (
1159 	BackendDB *b0,
1160 	Entry *e,
1161 	struct berval *text
1162 )
1163 {
1164 	BackendDB *be, b2;
1165 	int rc = -1;
1166 
1167 	b2 = *b0;
1168 	b2.bd_info = (BackendInfo *)glue_tool_inst( b0->bd_info );
1169 	be = glue_back_select (&b2, &e->e_nname);
1170 	if ( be == &b2 ) be = &toolDB;
1171 
1172 	if (!be->be_entry_put)
1173 		return NOID;
1174 
1175 	if (!glueBack) {
1176 		if ( be->be_entry_open ) {
1177 			rc = be->be_entry_open (be, glueMode);
1178 		}
1179 		if (rc != 0) {
1180 			return NOID;
1181 		}
1182 	} else if (be != glueBack) {
1183 		/* If this entry belongs in a different branch than the
1184 		 * previous one, close the current database and open the
1185 		 * new one.
1186 		 */
1187 		if ( glueBack->be_entry_close ) {
1188 			glueBack->be_entry_close (glueBack);
1189 		}
1190 		if ( be->be_entry_open ) {
1191 			rc = be->be_entry_open (be, glueMode);
1192 		}
1193 		if (rc != 0) {
1194 			return NOID;
1195 		}
1196 	}
1197 	glueBack = be;
1198 	return be->be_entry_put (be, e, text);
1199 }
1200 
1201 static ID
glue_tool_entry_modify(BackendDB * b0,Entry * e,struct berval * text)1202 glue_tool_entry_modify (
1203 	BackendDB *b0,
1204 	Entry *e,
1205 	struct berval *text
1206 )
1207 {
1208 	if (!glueBack || !glueBack->be_entry_modify)
1209 		return NOID;
1210 
1211 	return glueBack->be_entry_modify (glueBack, e, text);
1212 }
1213 
1214 static int
glue_tool_entry_reindex(BackendDB * b0,ID id,AttributeDescription ** adv)1215 glue_tool_entry_reindex (
1216 	BackendDB *b0,
1217 	ID id,
1218 	AttributeDescription **adv
1219 )
1220 {
1221 	if (!glueBack || !glueBack->be_entry_reindex)
1222 		return -1;
1223 
1224 	return glueBack->be_entry_reindex (glueBack, id, adv);
1225 }
1226 
1227 static int
glue_tool_sync(BackendDB * b0)1228 glue_tool_sync (
1229 	BackendDB *b0
1230 )
1231 {
1232 	slap_overinst	*on = glue_tool_inst( b0->bd_info );
1233 	glueinfo		*gi = on->on_bi.bi_private;
1234 	BackendInfo		*bi = b0->bd_info;
1235 	int i;
1236 
1237 	/* just sync everyone */
1238 	for (i = 0; i<gi->gi_nodes; i++)
1239 		if (gi->gi_n[i].gn_be->be_sync)
1240 			gi->gi_n[i].gn_be->be_sync (gi->gi_n[i].gn_be);
1241 	b0->bd_info = on->on_info->oi_orig;
1242 	if ( b0->be_sync )
1243 		b0->be_sync( b0 );
1244 	b0->bd_info = bi;
1245 	return 0;
1246 }
1247 
1248 typedef struct glue_Addrec {
1249 	struct glue_Addrec *ga_next;
1250 	BackendDB *ga_be;
1251 } glue_Addrec;
1252 
1253 /* List of added subordinates */
1254 static glue_Addrec *ga_list;
1255 static int ga_adding;
1256 
1257 static int
glue_db_init(BackendDB * be,ConfigReply * cr)1258 glue_db_init(
1259 	BackendDB *be,
1260 	ConfigReply *cr
1261 )
1262 {
1263 	slap_overinst	*on = (slap_overinst *)be->bd_info;
1264 	slap_overinfo	*oi = on->on_info;
1265 	BackendInfo	*bi = oi->oi_orig;
1266 	glueinfo *gi;
1267 
1268 	if ( SLAP_GLUE_SUBORDINATE( be )) {
1269 		Debug( LDAP_DEBUG_ANY, "glue: backend %s is already subordinate, "
1270 			"cannot have glue overlay!\n",
1271 			be->be_suffix[0].bv_val );
1272 		return LDAP_OTHER;
1273 	}
1274 
1275 	gi = ch_calloc( 1, sizeof(glueinfo));
1276 	on->on_bi.bi_private = gi;
1277 	dnParent( be->be_nsuffix, &gi->gi_pdn );
1278 
1279 	/* Currently the overlay framework doesn't handle these entry points
1280 	 * but we need them....
1281 	 */
1282 	oi->oi_bi.bi_open = glue_open;
1283 	oi->oi_bi.bi_close = glue_close;
1284 
1285 	/* Only advertise these if the root DB supports them */
1286 	if ( bi->bi_tool_entry_open )
1287 		oi->oi_bi.bi_tool_entry_open = glue_tool_entry_open;
1288 	if ( bi->bi_tool_entry_close )
1289 		oi->oi_bi.bi_tool_entry_close = glue_tool_entry_close;
1290 	if ( bi->bi_tool_entry_first )
1291 		oi->oi_bi.bi_tool_entry_first = glue_tool_entry_first;
1292 	/* FIXME: check whether all support bi_tool_entry_first_x() ? */
1293 	if ( bi->bi_tool_entry_first_x )
1294 		oi->oi_bi.bi_tool_entry_first_x = glue_tool_entry_first_x;
1295 	if ( bi->bi_tool_entry_next )
1296 		oi->oi_bi.bi_tool_entry_next = glue_tool_entry_next;
1297 	if ( bi->bi_tool_entry_get )
1298 		oi->oi_bi.bi_tool_entry_get = glue_tool_entry_get;
1299 	if ( bi->bi_tool_dn2id_get )
1300 		oi->oi_bi.bi_tool_dn2id_get = glue_tool_dn2id_get;
1301 	if ( bi->bi_tool_entry_put )
1302 		oi->oi_bi.bi_tool_entry_put = glue_tool_entry_put;
1303 	if ( bi->bi_tool_entry_reindex )
1304 		oi->oi_bi.bi_tool_entry_reindex = glue_tool_entry_reindex;
1305 	if ( bi->bi_tool_entry_modify )
1306 		oi->oi_bi.bi_tool_entry_modify = glue_tool_entry_modify;
1307 	if ( bi->bi_tool_sync )
1308 		oi->oi_bi.bi_tool_sync = glue_tool_sync;
1309 
1310 	SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_INSTANCE;
1311 
1312 	if ( ga_list && ( slapMode & SLAP_SERVER_MODE ) ) {
1313 		be->bd_info = (BackendInfo *)oi;
1314 		glue_sub_attach( 1 );
1315 	}
1316 
1317 	return 0;
1318 }
1319 
1320 static int
glue_db_destroy(BackendDB * be,ConfigReply * cr)1321 glue_db_destroy (
1322 	BackendDB *be,
1323 	ConfigReply *cr
1324 )
1325 {
1326 	slap_overinst	*on = (slap_overinst *)be->bd_info;
1327 	glueinfo		*gi = (glueinfo *)on->on_bi.bi_private;
1328 
1329 	free (gi);
1330 	return 0;
1331 }
1332 
1333 static int
glue_db_close(BackendDB * be,ConfigReply * cr)1334 glue_db_close(
1335 	BackendDB *be,
1336 	ConfigReply *cr
1337 )
1338 {
1339 	slap_overinst	*on = (slap_overinst *)be->bd_info;
1340 
1341 	on->on_info->oi_bi.bi_db_close = 0;
1342 	return 0;
1343 }
1344 
1345 int
glue_sub_del(BackendDB * b0)1346 glue_sub_del( BackendDB *b0 )
1347 {
1348 	BackendDB *be;
1349 	int rc = 0;
1350 
1351 	/* Find the top backend for this subordinate */
1352 	be = b0;
1353 	while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1354 		slap_overinfo *oi;
1355 		slap_overinst *on;
1356 		glueinfo *gi;
1357 		int i;
1358 
1359 		if ( SLAP_GLUE_SUBORDINATE( be ))
1360 			continue;
1361 		if ( !SLAP_GLUE_INSTANCE( be ))
1362 			continue;
1363 		if ( !dnIsSuffix( &b0->be_nsuffix[0], &be->be_nsuffix[0] ))
1364 			continue;
1365 
1366 		/* OK, got the right backend, find the overlay */
1367 		oi = (slap_overinfo *)be->bd_info;
1368 		for ( on=oi->oi_list; on; on=on->on_next ) {
1369 			if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1370 				break;
1371 		}
1372 		assert( on != NULL );
1373 		gi = on->on_bi.bi_private;
1374 		for ( i=0; i < gi->gi_nodes; i++ ) {
1375 			if ( gi->gi_n[i].gn_be == b0 ) {
1376 				int j;
1377 
1378 				for (j=i+1; j < gi->gi_nodes; j++)
1379 					gi->gi_n[j-1] = gi->gi_n[j];
1380 
1381 				gi->gi_nodes--;
1382 			}
1383 		}
1384 	}
1385 	if ( be == NULL )
1386 		rc = LDAP_NO_SUCH_OBJECT;
1387 
1388 	return rc;
1389 }
1390 
1391 
1392 /* Attach all the subordinate backends to their superior */
1393 int
glue_sub_attach(int online)1394 glue_sub_attach( int online )
1395 {
1396 	glue_Addrec *ga, *gnext = NULL;
1397 	int rc = 0;
1398 
1399 	if ( ga_adding )
1400 		return 0;
1401 
1402 	ga_adding = 1;
1403 
1404 	/* For all the subordinate backends */
1405 	for ( ga=ga_list; ga != NULL; ga = gnext ) {
1406 		BackendDB *be;
1407 
1408 		gnext = ga->ga_next;
1409 
1410 		/* Find the top backend for this subordinate */
1411 		be = ga->ga_be;
1412 		while ( (be=LDAP_STAILQ_NEXT( be, be_next )) != NULL ) {
1413 			slap_overinfo *oi;
1414 			slap_overinst *on;
1415 			glueinfo *gi;
1416 
1417 			if ( SLAP_GLUE_SUBORDINATE( be ))
1418 				continue;
1419 			if ( !dnIsSuffix( &ga->ga_be->be_nsuffix[0], &be->be_nsuffix[0] ))
1420 				continue;
1421 
1422 			/* If it's not already configured, set up the overlay */
1423 			if ( !SLAP_GLUE_INSTANCE( be )) {
1424 				rc = overlay_config( be, glue.on_bi.bi_type, -1, NULL, NULL);
1425 				if ( rc )
1426 					break;
1427 			}
1428 			/* Find the overlay instance */
1429 			oi = (slap_overinfo *)be->bd_info;
1430 			for ( on=oi->oi_list; on; on=on->on_next ) {
1431 				if ( on->on_bi.bi_type == glue.on_bi.bi_type )
1432 					break;
1433 			}
1434 			assert( on != NULL );
1435 			gi = on->on_bi.bi_private;
1436 			gi = (glueinfo *)ch_realloc( gi, sizeof(glueinfo) +
1437 				gi->gi_nodes * sizeof(gluenode));
1438 			gi->gi_n[gi->gi_nodes].gn_be = ga->ga_be;
1439 			dnParent( &ga->ga_be->be_nsuffix[0],
1440 				&gi->gi_n[gi->gi_nodes].gn_pdn );
1441 			gi->gi_nodes++;
1442 			on->on_bi.bi_private = gi;
1443 			ga->ga_be->be_flags |= SLAP_DBFLAG_GLUE_LINKED;
1444 			break;
1445 		}
1446 		if ( !be ) {
1447 			Debug( LDAP_DEBUG_ANY, "glue: no superior found for sub %s!\n",
1448 				ga->ga_be->be_suffix[0].bv_val );
1449 			/* allow this for now, assume a superior will
1450 			 * be added later
1451 			 */
1452 			if ( online ) {
1453 				rc = 0;
1454 				gnext = ga_list;
1455 				break;
1456 			}
1457 			rc = LDAP_NO_SUCH_OBJECT;
1458 		}
1459 		ch_free( ga );
1460 		if ( rc ) break;
1461 	}
1462 
1463 	ga_list = gnext;
1464 
1465 	ga_adding = 0;
1466 
1467 	return rc;
1468 }
1469 
1470 int
glue_sub_add(BackendDB * be,int advert,int online)1471 glue_sub_add( BackendDB *be, int advert, int online )
1472 {
1473 	glue_Addrec *ga;
1474 	int rc = 0;
1475 
1476 	if ( overlay_is_inst( be, "glue" )) {
1477 		Debug( LDAP_DEBUG_ANY, "glue: backend %s already has glue overlay, "
1478 			"cannot be a subordinate!\n",
1479 			be->be_suffix[0].bv_val );
1480 		return LDAP_OTHER;
1481 	}
1482 	SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
1483 	if ( advert )
1484 		SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLUE_ADVERTISE;
1485 
1486 	ga = ch_malloc( sizeof( glue_Addrec ));
1487 	ga->ga_next = ga_list;
1488 	ga->ga_be = be;
1489 	ga_list = ga;
1490 
1491 	if ( online )
1492 		rc = glue_sub_attach( online );
1493 
1494 	return rc;
1495 }
1496 
1497 static int
glue_access_allowed(Operation * op,Entry * e,AttributeDescription * desc,struct berval * val,slap_access_t access,AccessControlState * state,slap_mask_t * maskp)1498 glue_access_allowed(
1499 	Operation		*op,
1500 	Entry			*e,
1501 	AttributeDescription	*desc,
1502 	struct berval		*val,
1503 	slap_access_t		access,
1504 	AccessControlState	*state,
1505 	slap_mask_t		*maskp )
1506 {
1507 	BackendDB *b0, *be = glue_back_select( op->o_bd, &e->e_nname );
1508 	int rc;
1509 
1510 	if ( be == NULL || be == op->o_bd || be->bd_info->bi_access_allowed == NULL )
1511 		return SLAP_CB_CONTINUE;
1512 
1513 	b0 = op->o_bd;
1514 	op->o_bd = be;
1515 	rc = be->bd_info->bi_access_allowed ( op, e, desc, val, access, state, maskp );
1516 	op->o_bd = b0;
1517 	return rc;
1518 }
1519 
1520 int
glue_sub_init()1521 glue_sub_init()
1522 {
1523 	glue.on_bi.bi_type = "glue";
1524 
1525 	glue.on_bi.bi_db_init = glue_db_init;
1526 	glue.on_bi.bi_db_close = glue_db_close;
1527 	glue.on_bi.bi_db_destroy = glue_db_destroy;
1528 
1529 	glue.on_bi.bi_op_search = glue_op_search;
1530 	glue.on_bi.bi_op_modify = glue_op_func;
1531 	glue.on_bi.bi_op_modrdn = glue_op_func;
1532 	glue.on_bi.bi_op_add = glue_op_func;
1533 	glue.on_bi.bi_op_delete = glue_op_func;
1534 	glue.on_bi.bi_op_abandon = glue_op_abandon;
1535 	glue.on_bi.bi_extended = glue_op_func;
1536 
1537 	glue.on_bi.bi_chk_referrals = glue_chk_referrals;
1538 	glue.on_bi.bi_chk_controls = glue_chk_controls;
1539 	glue.on_bi.bi_entry_get_rw = glue_entry_get_rw;
1540 	glue.on_bi.bi_entry_release_rw = glue_entry_release_rw;
1541 	glue.on_bi.bi_access_allowed = glue_access_allowed;
1542 
1543 	glue.on_response = glue_response;
1544 
1545 	return overlay_register( &glue );
1546 }
1547