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