xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-relay/op.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: op.c,v 1.1.1.4 2014/05/28 09:58:51 tron Exp $	*/
2 
3 /* op.c - relay backend operations */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 2004-2014 The OpenLDAP Foundation.
8  * Portions Copyright 2004 Pierangelo Masarati.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted only as authorized by the OpenLDAP
13  * Public License.
14  *
15  * A copy of this license is available in the file LICENSE in the
16  * top-level directory of the distribution or, alternatively, at
17  * <http://www.OpenLDAP.org/license.html>.
18  */
19 /* ACKNOWLEDGEMENTS:
20  * This work was initially developed by Pierangelo Masarati for inclusion
21  * in OpenLDAP Software.
22  */
23 
24 #include "portable.h"
25 
26 #include <stdio.h>
27 
28 #include "slap.h"
29 #include "back-relay.h"
30 
31 /* Results when no real database (.rf_bd) or operation handler (.rf_op) */
32 static const struct relay_fail_modes_s {
33 	slap_mask_t	rf_bd, rf_op;
34 #define RB_ERR_MASK	0x0000FFFFU /* bitmask for default return value */
35 #define RB_BDERR	0x80000000U /* use .rf_bd's default return value */
36 #define RB_OPERR	0x40000000U /* set rs->sr_err = .rf_op return value */
37 #define RB_REF		0x20000000U /* use default_referral if available */
38 #define RB_SEND		0x10000000U /* send result; RB_??ERR is also set */
39 #define RB_SENDREF	0/*unused*/ /* like RB_SEND when referral found */
40 #define RB_NO_BIND	(RB_OPERR | LDAP_INVALID_CREDENTIALS)
41 #define RB_NOT_SUPP	(RB_OPERR | LDAP_UNWILLING_TO_PERFORM)
42 #define RB_NO_OBJ	(RB_REF | LDAP_NO_SUCH_OBJECT)
43 #define RB_CHK_REF	(RB_REF | RB_SENDREF | LDAP_SUCCESS)
44 } relay_fail_modes[relay_op_last] = {
45 	/* .rf_bd is unused when zero, otherwise both fields have RB_BDERR */
46 #	define RB_OP(b, o)	{ (b) | RB_BD2ERR(b), (o) | RB_BD2ERR(b) }
47 #	define RB_BD2ERR(b)	((b) ? RB_BDERR : 0)
48 	/* indexed by slap_operation_t: */
49 	RB_OP(RB_NO_BIND|RB_SEND, RB_NO_BIND  |RB_SEND), /* Bind           */
50 	RB_OP(0,                  LDAP_SUCCESS),         /* Unbind: unused */
51 	RB_OP(RB_NO_OBJ |RB_SEND, RB_NOT_SUPP |RB_SEND), /* Search         */
52 	RB_OP(RB_NO_OBJ |RB_SEND, SLAP_CB_CONTINUE),     /* Compare        */
53 	RB_OP(RB_NO_OBJ |RB_SEND, RB_NOT_SUPP |RB_SEND), /* Modify         */
54 	RB_OP(RB_NO_OBJ |RB_SEND, RB_NOT_SUPP |RB_SEND), /* Modrdn         */
55 	RB_OP(RB_NO_OBJ |RB_SEND, RB_NOT_SUPP |RB_SEND), /* Add            */
56 	RB_OP(RB_NO_OBJ |RB_SEND, RB_NOT_SUPP |RB_SEND), /* Delete         */
57 	RB_OP(0,                  LDAP_SUCCESS),         /* Abandon:unused */
58 	RB_OP(RB_NO_OBJ,          RB_NOT_SUPP),          /* Extended       */
59 	RB_OP(0,                  SLAP_CB_CONTINUE),     /* Cancel: unused */
60 	RB_OP(0,                  LDAP_SUCCESS),    /* operational         */
61 	RB_OP(RB_CHK_REF,         LDAP_SUCCESS),    /* chk_referrals:unused*/
62 	RB_OP(0,                  SLAP_CB_CONTINUE),/* chk_controls:unused */
63 	/* additional relay_operation_t indexes from back-relay.h: */
64 	RB_OP(0,                  0/*unused*/),     /* entry_get = op_last */
65 	RB_OP(0,                  0/*unused*/),     /* entry_release       */
66 	RB_OP(0,                  0/*unused*/),     /* has_subordinates    */
67 };
68 
69 /*
70  * Callbacks: Caller changed op->o_bd from Relay to underlying
71  * BackendDB.  sc_response sets it to Relay BackendDB, sc_cleanup puts
72  * back underlying BackendDB.  Caller will restore Relay BackendDB.
73  */
74 
75 typedef struct relay_callback {
76 	slap_callback rcb_sc;
77 	BackendDB *rcb_bd;
78 } relay_callback;
79 
80 static int
81 relay_back_cleanup_cb( Operation *op, SlapReply *rs )
82 {
83 	op->o_bd = ((relay_callback *) op->o_callback)->rcb_bd;
84 	return SLAP_CB_CONTINUE;
85 }
86 
87 static int
88 relay_back_response_cb( Operation *op, SlapReply *rs )
89 {
90 	relay_callback	*rcb = (relay_callback *) op->o_callback;
91 
92 	rcb->rcb_sc.sc_cleanup = relay_back_cleanup_cb;
93 	rcb->rcb_bd = op->o_bd;
94 	op->o_bd = op->o_callback->sc_private;
95 	return SLAP_CB_CONTINUE;
96 }
97 
98 #define relay_back_add_cb( rcb, op ) {				\
99 		(rcb)->rcb_sc.sc_next = (op)->o_callback;	\
100 		(rcb)->rcb_sc.sc_response = relay_back_response_cb; \
101 		(rcb)->rcb_sc.sc_cleanup = 0;			\
102 		(rcb)->rcb_sc.sc_private = (op)->o_bd;		\
103 		(op)->o_callback = (slap_callback *) (rcb);	\
104 }
105 
106 #define relay_back_remove_cb( rcb, op ) {			\
107 		slap_callback	**sc = &(op)->o_callback;	\
108 		for ( ;; sc = &(*sc)->sc_next )			\
109 			if ( *sc == (slap_callback *) (rcb) ) {	\
110 				*sc = (*sc)->sc_next; break;	\
111 			} else if ( *sc == NULL ) break;	\
112 }
113 
114 /*
115  * Select the backend database with the operation's DN.  On failure,
116  * set/send results depending on operation type <which>'s fail_modes.
117  */
118 static BackendDB *
119 relay_back_select_backend( Operation *op, SlapReply *rs, int which )
120 {
121 	OpExtra		*oex;
122 	char		*key = (char *) op->o_bd->be_private;
123 	BackendDB	*bd  = ((relay_back_info *) key)->ri_bd;
124 	slap_mask_t	fail_mode = relay_fail_modes[which].rf_bd;
125 	int		useDN = 0, rc = ( fail_mode & RB_ERR_MASK );
126 
127 	if ( bd == NULL && !BER_BVISNULL( &op->o_req_ndn ) ) {
128 		useDN = 1;
129 		bd = select_backend( &op->o_req_ndn, 1 );
130 	}
131 
132 	if ( bd != NULL ) {
133 		key += which; /* <relay, op type> key from RELAY_WRAP_OP() */
134 		LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
135 			if ( oex->oe_key == key )
136 				break;
137 		}
138 		if ( oex == NULL ) {
139 			return bd;
140 		}
141 
142 		Debug( LDAP_DEBUG_ANY,
143 			"%s: back-relay for DN=\"%s\" would call self.\n",
144 			op->o_log_prefix, op->o_req_dn.bv_val, 0 );
145 
146 	} else if ( useDN && ( fail_mode & RB_REF ) && default_referral ) {
147 		rc = LDAP_REFERRAL;
148 
149 		/* if we set sr_err to LDAP_REFERRAL, we must provide one */
150 		rs->sr_ref = referral_rewrite(
151 			default_referral, NULL, &op->o_req_dn,
152 			op->o_tag == LDAP_REQ_SEARCH ?
153 			op->ors_scope : LDAP_SCOPE_DEFAULT );
154 		if ( rs->sr_ref != NULL ) {
155 			rs->sr_flags |= REP_REF_MUSTBEFREED;
156 		} else {
157 			rs->sr_ref = default_referral;
158 		}
159 
160 		if ( fail_mode & RB_SENDREF )
161 			fail_mode = (RB_BDERR | RB_SEND);
162 	}
163 
164 	if ( fail_mode & RB_BDERR ) {
165 		rs->sr_err = rc;
166 		if ( fail_mode & RB_SEND ) {
167 			send_ldap_result( op, rs );
168 		}
169 	}
170 
171 	return NULL;
172 }
173 
174 /*
175  * Forward <act> on <op> to database <bd>, with <relay, op type>-specific
176  * key in op->o_extra so relay_back_select_backend() can catch recursion.
177  */
178 #define RELAY_WRAP_OP( op, bd, which, act ) { \
179 	OpExtraDB wrap_oex; \
180 	BackendDB *const wrap_bd = (op)->o_bd; \
181 	wrap_oex.oe_db = wrap_bd; \
182 	wrap_oex.oe.oe_key = (char *) wrap_bd->be_private + (which); \
183 	LDAP_SLIST_INSERT_HEAD( &(op)->o_extra, &wrap_oex.oe, oe_next ); \
184 	(op)->o_bd = (bd); \
185 	act; \
186 	(op)->o_bd = wrap_bd; \
187 	LDAP_SLIST_REMOVE( &(op)->o_extra, &wrap_oex.oe, OpExtra, oe_next ); \
188 }
189 
190 /*
191  * Forward backend function #<which> on <op> to operation DN's database
192  * like RELAY_WRAP_OP, after setting up callbacks. If no database or no
193  * backend function, set/send results depending on <which>'s fail_modes.
194  */
195 static int
196 relay_back_op( Operation *op, SlapReply *rs, int which )
197 {
198 	BackendDB	*bd;
199 	BI_op_bind	*func;
200 	slap_mask_t	fail_mode = relay_fail_modes[which].rf_op;
201 	int		rc = ( fail_mode & RB_ERR_MASK );
202 
203 	bd = relay_back_select_backend( op, rs, which );
204 	if ( bd == NULL ) {
205 		if ( fail_mode & RB_BDERR )
206 			return rs->sr_err;	/* sr_err was set above */
207 
208 	} else if ( (func = (&bd->be_bind)[which]) != 0 ) {
209 		relay_callback	rcb;
210 
211 		relay_back_add_cb( &rcb, op );
212 		RELAY_WRAP_OP( op, bd, which, {
213 			rc = func( op, rs );
214 		});
215 		relay_back_remove_cb( &rcb, op );
216 
217 	} else if ( fail_mode & RB_OPERR ) {
218 		rs->sr_err = rc;
219 		if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
220 			rs->sr_text = "operation not supported within naming context";
221 		}
222 
223 		if ( fail_mode & RB_SEND ) {
224 			send_ldap_result( op, rs );
225 		}
226 	}
227 
228 	return rc;
229 }
230 
231 
232 int
233 relay_back_op_bind( Operation *op, SlapReply *rs )
234 {
235 	/* allow rootdn as a means to auth without the need to actually
236  	 * contact the proxied DSA */
237 	switch ( be_rootdn_bind( op, rs ) ) {
238 	case SLAP_CB_CONTINUE:
239 		break;
240 
241 	default:
242 		return rs->sr_err;
243 	}
244 
245 	return relay_back_op( op, rs, op_bind );
246 }
247 
248 #define RELAY_DEFOP(func, which) \
249 	int func( Operation *op, SlapReply *rs ) \
250 	{ return relay_back_op( op, rs, which ); }
251 
252 RELAY_DEFOP( relay_back_op_search,		op_search )
253 RELAY_DEFOP( relay_back_op_compare,		op_compare )
254 RELAY_DEFOP( relay_back_op_modify,		op_modify )
255 RELAY_DEFOP( relay_back_op_modrdn,		op_modrdn )
256 RELAY_DEFOP( relay_back_op_add,			op_add )
257 RELAY_DEFOP( relay_back_op_delete,		op_delete )
258 RELAY_DEFOP( relay_back_op_extended,	op_extended )
259 RELAY_DEFOP( relay_back_operational,	op_aux_operational )
260 
261 /* Abandon, Cancel, Unbind and some DN-less calls like be_connection_init
262  * need no extra handling:  slapd already calls them for all databases.
263  */
264 
265 
266 int
267 relay_back_entry_release_rw( Operation *op, Entry *e, int rw )
268 {
269 	BackendDB		*bd;
270 	int			rc = LDAP_UNWILLING_TO_PERFORM;
271 
272 	bd = relay_back_select_backend( op, NULL, relay_op_entry_release );
273 	if ( bd && bd->be_release ) {
274 		RELAY_WRAP_OP( op, bd, relay_op_entry_release, {
275 			rc = bd->be_release( op, e, rw );
276 		});
277 	} else if ( e->e_private == NULL ) {
278 		entry_free( e );
279 		rc = LDAP_SUCCESS;
280 	}
281 
282 	return rc;
283 }
284 
285 int
286 relay_back_entry_get_rw( Operation *op, struct berval *ndn,
287 	ObjectClass *oc, AttributeDescription *at, int rw, Entry **e )
288 {
289 	BackendDB		*bd;
290 	int			rc = LDAP_NO_SUCH_OBJECT;
291 
292 	bd = relay_back_select_backend( op, NULL, relay_op_entry_get );
293 	if ( bd && bd->be_fetch ) {
294 		RELAY_WRAP_OP( op, bd, relay_op_entry_get, {
295 			rc = bd->be_fetch( op, ndn, oc, at, rw, e );
296 		});
297 	}
298 
299 	return rc;
300 }
301 
302 #if 0 /* Give the RB_SENDREF flag a nonzero value if implementing this */
303 /*
304  * NOTE: even the existence of this function is questionable: we cannot
305  * pass the bi_chk_referrals() call thru the rwm overlay because there
306  * is no way to rewrite the req_dn back; but then relay_back_chk_referrals()
307  * is passing the target database a DN that likely does not belong to its
308  * naming context... mmmh.
309  */
310 RELAY_DEFOP( relay_back_chk_referrals, op_aux_chk_referrals )
311 #endif /*0*/
312 
313 int
314 relay_back_has_subordinates( Operation *op, Entry *e, int *hasSubs )
315 {
316 	BackendDB		*bd;
317 	int			rc = LDAP_OTHER;
318 
319 	bd = relay_back_select_backend( op, NULL, relay_op_has_subordinates );
320 	if ( bd && bd->be_has_subordinates ) {
321 		RELAY_WRAP_OP( op, bd, relay_op_has_subordinates, {
322 			rc = bd->be_has_subordinates( op, e, hasSubs );
323 		});
324 	}
325 
326 	return rc;
327 }
328 
329 
330 /*
331  * FIXME: must implement tools as well
332  */
333