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
|
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2021 Red Hat, Inc.
* All rights reserved.
*
* License: GPL (version 3 or any later version).
* See LICENSE for details.
* END COPYRIGHT BLOCK **/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "cb.h"
/*
** Controls that can't be forwarded due to the current implementation
*/
static char *unsupported_ctrls[] = {LDAP_CONTROL_PERSISTENTSEARCH, NULL};
int
cb_is_control_forwardable(cb_backend *cb __attribute__((unused)), char *controloid)
{
return (!(charray_inlist(unsupported_ctrls, controloid)));
}
void
cb_register_supported_control(cb_backend *cb, char *controloid, unsigned long controlops __attribute__((unused)))
{
/* For now, ignore controlops */
if (controloid != NULL) {
slapi_rwlock_wrlock(cb->config.rwl_config_lock);
charray_add(&cb->config.forward_ctrls, slapi_ch_strdup(controloid));
slapi_rwlock_unlock(cb->config.rwl_config_lock);
}
}
void
cb_unregister_all_supported_control(cb_backend *cb)
{
slapi_rwlock_wrlock(cb->config.rwl_config_lock);
charray_free(cb->config.forward_ctrls);
cb->config.forward_ctrls = NULL;
slapi_rwlock_unlock(cb->config.rwl_config_lock);
}
void
cb_unregister_supported_control(cb_backend *cb, char *controloid, unsigned long controlops)
{
/* For now, ignore controlops */
if (controloid != NULL) {
int i;
slapi_rwlock_wrlock(cb->config.rwl_config_lock);
for (i = 0; cb->config.forward_ctrls != NULL && cb->config.forward_ctrls[i] != NULL; ++i) {
if (strcmp(cb->config.forward_ctrls[i], controloid) == 0) {
break;
}
}
if (cb->config.forward_ctrls == NULL || cb->config.forward_ctrls[i] == NULL) {
slapi_rwlock_unlock(cb->config.rwl_config_lock);
return;
}
if (controlops == 0) {
charray_remove(cb->config.forward_ctrls, controloid, 0 /* free it */);
}
slapi_rwlock_unlock(cb->config.rwl_config_lock);
}
}
int
cb_create_loop_control(
const ber_int_t hops,
LDAPControl **ctrlp)
{
BerElement *ber;
int rc;
if ((ber = ber_alloc()) == NULL)
return -1;
if (ber_printf(ber, "i", hops) < 0) {
ber_free(ber, 1);
return -1;
}
rc = slapi_build_control(CB_LDAP_CONTROL_CHAIN_SERVER, ber, 0, ctrlp);
ber_free(ber, 1);
return rc;
}
/*
** Return the controls to be passed to the remote
** farm server and the LDAP error to return.
**
** Add the Proxied Authorization control when impersonation
** is enabled. Other controls present in the request are added
** to the control list
**
** #622885 .abandon should not inherit the to-be-abandoned-operation's controls
** .controls attached to abandon should not be critical
*/
int
cb_update_controls(Slapi_PBlock *pb,
LDAP *ld,
LDAPControl ***controls,
int ctrl_flags)
{
int cCount = 0;
int dCount = 0;
char *proxyDN = NULL;
LDAPControl **reqControls = NULL;
LDAPControl **ctrls = NULL;
cb_backend_instance *cb;
cb_backend *cbb;
Slapi_Backend *be;
int rc = LDAP_SUCCESS;
ber_int_t hops = 0;
int useloop = 0;
int addauth = (ctrl_flags & CB_UPDATE_CONTROLS_ADDAUTH);
int isabandon = (ctrl_flags & CB_UPDATE_CONTROLS_ISABANDON);
int op_type = 0;
size_t i;
*controls = NULL;
slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &op_type);
if (!isabandon || op_type == SLAPI_OPERATION_ABANDON) {
/* if not abandon or abandon sent by client */
slapi_pblock_get(pb, SLAPI_REQCONTROLS, &reqControls);
}
slapi_pblock_get(pb, SLAPI_BACKEND, &be);
slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &cbb);
cb = cb_get_instance(be);
/*****************************************/
/* First, check for unsupported controls */
/* Return an error if critical control */
/* else remove it from the control list */
/*****************************************/
for (cCount = 0; reqControls && reqControls[cCount]; cCount++)
;
ctrls = (LDAPControl **)slapi_ch_calloc(1, sizeof(LDAPControl *) * (cCount + 3));
slapi_rwlock_rdlock(cbb->config.rwl_config_lock);
for (cCount = 0; reqControls && reqControls[cCount]; cCount++) {
/* XXXSD CASCADING */
/* For now, allow PROXY_AUTH control forwarding only when */
/* local acl evaluation to prevent unauthorized access */
if (!strcmp(reqControls[cCount]->ldctl_oid, LDAP_CONTROL_PROXYAUTH)) {
/* we have to force remote acl checking if the associated backend to this
chaining backend is disabled - disabled == no acl check possible */
if (!cb->local_acl && !cb->associated_be_is_disabled) {
slapi_log_err(SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
"local aci check required to handle proxied auth control. Deny access.\n");
rc = LDAP_INSUFFICIENT_ACCESS;
break;
}
/* XXXSD Not safe to use proxied authorization with Directory Manager */
/* checked earlier when impersonation is on */
if (!cb->impersonate) {
char *requestor, *rootdn;
char *requestorCopy = NULL;
rootdn = cb_get_rootdn();
slapi_pblock_get(pb, SLAPI_REQUESTOR_NDN, &requestor);
requestorCopy = slapi_ch_strdup(requestor);
slapi_dn_ignore_case(requestorCopy);
if (!strcmp(requestorCopy, rootdn)) { /* UTF8- aware */
slapi_log_err(SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
"Use of user <%s> incompatible with proxied auth. control\n",
rootdn);
rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
slapi_ch_free_string(&rootdn);
slapi_ch_free_string(&requestorCopy);
break;
}
slapi_ch_free_string(&rootdn);
slapi_ch_free_string(&requestorCopy);
}
addauth = 0;
ctrls[dCount] = slapi_dup_control(reqControls[cCount]);
dCount++;
} else if (!strcmp(reqControls[cCount]->ldctl_oid, CB_LDAP_CONTROL_CHAIN_SERVER) &&
BV_HAS_DATA((&(reqControls[cCount]->ldctl_value)))) {
/* Max hop count reached ? */
/* Checked earlier by a call to cb_forward_operation() */
BerElement *ber = NULL;
ber = ber_init(&(reqControls[cCount]->ldctl_value));
if (LBER_ERROR == ber_scanf(ber, "i", &hops)) {
slapi_log_err(SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
"Unable to get number of hops from the chaining control\n");
}
ber_free(ber, 1);
useloop = 1;
/* Add to the control list later */
} else {
for (i = 0; cbb->config.forward_ctrls != NULL && cbb->config.forward_ctrls[i] != NULL; ++i) {
if (strcmp(cbb->config.forward_ctrls[i], reqControls[cCount]->ldctl_oid) == 0) {
break;
}
}
/* For now, ignore optype */
if (cbb->config.forward_ctrls == NULL || cbb->config.forward_ctrls[i] == NULL) {
if (reqControls[cCount]->ldctl_iscritical) {
rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
break;
}
/* Skip it */
} else {
ctrls[dCount] = slapi_dup_control(reqControls[cCount]);
dCount++;
}
}
}
slapi_rwlock_unlock(cbb->config.rwl_config_lock);
if (LDAP_SUCCESS != rc) {
ldap_controls_free(ctrls);
return rc;
}
/***************************************/
/* add impersonation control if needed */
/***************************************/
if (!(cb->impersonate)) {
/* don't add proxy control */
addauth = 0;
}
if (addauth) {
slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &proxyDN);
if (slapi_ldap_create_proxyauth_control(ld, proxyDN, isabandon ? 0 : 1, 0, &ctrls[dCount])) {
ldap_controls_free(ctrls);
slapi_log_err(SLAPI_LOG_PLUGIN, CB_PLUGIN_SUBSYSTEM,
"LDAP_CONTROL_PROXYAUTH control encoding failed.\n");
return LDAP_OPERATIONS_ERROR;
}
dCount++;
}
/***********************************************************/
/* add loop control if needed */
/* Don't add it if not in the list of forwardable controls */
/***********************************************************/
if (!useloop) {
for (i = 0; cbb->config.forward_ctrls != NULL && cbb->config.forward_ctrls[i] != NULL; ++i) {
if (strcmp(cbb->config.forward_ctrls[i],
CB_LDAP_CONTROL_CHAIN_SERVER) == 0) {
break;
}
}
}
if (useloop || (cbb->config.forward_ctrls != NULL && cbb->config.forward_ctrls[i] != NULL)) {
if (hops > 0) {
hops--;
} else {
hops = cb->hoplimit;
}
/* loop control's critical flag is 0;
* no special treatment is needed for abandon */
cb_create_loop_control(hops, &ctrls[dCount]);
dCount++;
}
if (dCount == 0) {
ldap_controls_free(ctrls);
} else {
*controls = ctrls;
}
return LDAP_SUCCESS;
}
|