| 1 |
/* extended.c - ldap backend extended routines */ |
| 2 |
/* $OpenLDAP$ */ |
| 3 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 4 |
* |
| 5 |
* Copyright 2003-2024 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 |
/* ACKNOWLEDGEMENTS: |
| 17 |
* This work was initially developed by the Howard Chu for inclusion |
| 18 |
* in OpenLDAP Software and subsequently enhanced by Pierangelo |
| 19 |
* Masarati. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "portable.h" |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <ac/string.h> |
| 26 |
|
| 27 |
#include "slap.h" |
| 28 |
#include "back-ldap.h" |
| 29 |
#include "lber_pvt.h" |
| 30 |
|
| 31 |
typedef int (ldap_back_exop_f)( Operation *op, SlapReply *rs, ldapconn_t **lc ); |
| 32 |
|
| 33 |
static ldap_back_exop_f ldap_back_exop_passwd; |
| 34 |
static ldap_back_exop_f ldap_back_exop_generic; |
| 35 |
|
| 36 |
static struct exop { |
| 37 |
struct berval oid; |
| 38 |
ldap_back_exop_f *extended; |
| 39 |
} exop_table[] = { |
| 40 |
{ BER_BVC(LDAP_EXOP_MODIFY_PASSWD), ldap_back_exop_passwd }, |
| 41 |
{ BER_BVNULL, NULL } |
| 42 |
}; |
| 43 |
|
| 44 |
static int |
| 45 |
ldap_back_extended_one( Operation *op, SlapReply *rs, ldap_back_exop_f exop ) |
| 46 |
{ |
| 47 |
ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private; |
| 48 |
|
| 49 |
ldapconn_t *lc = NULL; |
| 50 |
LDAPControl **ctrls = NULL, **oldctrls = NULL; |
| 51 |
int rc; |
| 52 |
|
| 53 |
/* FIXME: this needs to be called here, so it is |
| 54 |
* called twice; maybe we could avoid the |
| 55 |
* ldap_back_dobind() call inside each extended() |
| 56 |
* call ... */ |
| 57 |
if ( !ldap_back_dobind( &lc, op, rs, LDAP_BACK_DONTSEND ) ) { |
| 58 |
return rs->sr_err; |
| 59 |
} |
| 60 |
|
| 61 |
ctrls = oldctrls = op->o_ctrls; |
| 62 |
if ( ldap_back_controls_add( op, rs, lc, &ctrls ) ) |
| 63 |
{ |
| 64 |
op->o_ctrls = oldctrls; |
| 65 |
rc = rs->sr_err; |
| 66 |
goto done; |
| 67 |
} |
| 68 |
|
| 69 |
op->o_ctrls = ctrls; |
| 70 |
rc = exop( op, rs, &lc ); |
| 71 |
|
| 72 |
op->o_ctrls = oldctrls; |
| 73 |
(void)ldap_back_controls_free( op, rs, &ctrls ); |
| 74 |
|
| 75 |
done:; |
| 76 |
if ( lc != NULL ) { |
| 77 |
ldap_back_release_conn( li, lc ); |
| 78 |
} |
| 79 |
|
| 80 |
return rc; |
| 81 |
} |
| 82 |
|
| 83 |
int |
| 84 |
ldap_back_extended( |
| 85 |
Operation *op, |
| 86 |
SlapReply *rs ) |
| 87 |
{ |
| 88 |
int i; |
| 89 |
|
| 90 |
RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) ); |
| 91 |
rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */ |
| 92 |
|
| 93 |
for ( i = 0; exop_table[i].extended != NULL; i++ ) { |
| 94 |
if ( bvmatch( &exop_table[i].oid, &op->oq_extended.rs_reqoid ) ) |
| 95 |
{ |
| 96 |
return ldap_back_extended_one( op, rs, exop_table[i].extended ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/* if we get here, the exop is known; the best that we can do |
| 101 |
* is pass it thru as is */ |
| 102 |
/* FIXME: maybe a list of OIDs to pass thru would be safer */ |
| 103 |
return ldap_back_extended_one( op, rs, ldap_back_exop_generic ); |
| 104 |
} |
| 105 |
|
| 106 |
static int |
| 107 |
ldap_back_exop_passwd( |
| 108 |
Operation *op, |
| 109 |
SlapReply *rs, |
| 110 |
ldapconn_t **lcp ) |
| 111 |
{ |
| 112 |
ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private; |
| 113 |
|
| 114 |
ldapconn_t *lc = *lcp; |
| 115 |
req_pwdexop_s *qpw = &op->oq_pwdexop; |
| 116 |
LDAPMessage *res; |
| 117 |
ber_int_t msgid; |
| 118 |
int rc, isproxy, freedn = 0; |
| 119 |
int do_retry = 1; |
| 120 |
char *text = NULL; |
| 121 |
struct berval dn = op->o_req_dn, |
| 122 |
ndn = op->o_req_ndn; |
| 123 |
|
| 124 |
assert( lc != NULL ); |
| 125 |
assert( rs->sr_ctrls == NULL ); |
| 126 |
|
| 127 |
if ( BER_BVISNULL( &ndn ) && op->ore_reqdata != NULL ) { |
| 128 |
/* NOTE: most of this code is mutated |
| 129 |
* from slap_passwd_parse(); |
| 130 |
* But here we only need |
| 131 |
* the first berval... */ |
| 132 |
|
| 133 |
ber_tag_t tag; |
| 134 |
ber_len_t len = -1; |
| 135 |
BerElementBuffer berbuf; |
| 136 |
BerElement *ber = (BerElement *)&berbuf; |
| 137 |
|
| 138 |
struct berval tmpid = BER_BVNULL; |
| 139 |
|
| 140 |
if ( op->ore_reqdata->bv_len == 0 ) { |
| 141 |
return LDAP_PROTOCOL_ERROR; |
| 142 |
} |
| 143 |
|
| 144 |
/* ber_init2 uses reqdata directly, doesn't allocate new buffers */ |
| 145 |
ber_init2( ber, op->ore_reqdata, 0 ); |
| 146 |
|
| 147 |
tag = ber_scanf( ber, "{" /*}*/ ); |
| 148 |
|
| 149 |
if ( tag == LBER_ERROR ) { |
| 150 |
return LDAP_PROTOCOL_ERROR; |
| 151 |
} |
| 152 |
|
| 153 |
tag = ber_peek_tag( ber, &len ); |
| 154 |
if ( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) { |
| 155 |
tag = ber_get_stringbv( ber, &tmpid, LBER_BV_NOTERM ); |
| 156 |
|
| 157 |
if ( tag == LBER_ERROR ) { |
| 158 |
return LDAP_PROTOCOL_ERROR; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if ( !BER_BVISEMPTY( &tmpid ) ) { |
| 163 |
char idNull = tmpid.bv_val[tmpid.bv_len]; |
| 164 |
tmpid.bv_val[tmpid.bv_len] = '\0'; |
| 165 |
rs->sr_err = dnPrettyNormal( NULL, &tmpid, &dn, |
| 166 |
&ndn, op->o_tmpmemctx ); |
| 167 |
tmpid.bv_val[tmpid.bv_len] = idNull; |
| 168 |
if ( rs->sr_err != LDAP_SUCCESS ) { |
| 169 |
/* should have been successfully parsed earlier! */ |
| 170 |
return rs->sr_err; |
| 171 |
} |
| 172 |
freedn = 1; |
| 173 |
|
| 174 |
} else { |
| 175 |
dn = op->o_dn; |
| 176 |
ndn = op->o_ndn; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
isproxy = ber_bvcmp( &ndn, &op->o_ndn ); |
| 181 |
|
| 182 |
Debug( LDAP_DEBUG_ARGS, "==> ldap_back_exop_passwd(\"%s\")%s\n", |
| 183 |
dn.bv_val, isproxy ? " (proxy)" : "" ); |
| 184 |
|
| 185 |
retry: |
| 186 |
rc = ldap_passwd( lc->lc_ld, &dn, |
| 187 |
qpw->rs_old.bv_val ? &qpw->rs_old : NULL, |
| 188 |
qpw->rs_new.bv_val ? &qpw->rs_new : NULL, |
| 189 |
op->o_ctrls, NULL, &msgid ); |
| 190 |
|
| 191 |
if ( rc == LDAP_SUCCESS ) { |
| 192 |
/* TODO: set timeout? */ |
| 193 |
/* by now, make sure no timeout is used (ITS#6282) */ |
| 194 |
struct timeval tv = { -1, 0 }; |
| 195 |
if ( ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res ) == -1 ) { |
| 196 |
ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER, &rc ); |
| 197 |
rs->sr_err = rc; |
| 198 |
|
| 199 |
} else { |
| 200 |
/* only touch when activity actually took place... */ |
| 201 |
if ( li->li_idle_timeout ) { |
| 202 |
lc->lc_time = op->o_time; |
| 203 |
} |
| 204 |
|
| 205 |
/* sigh. parse twice, because parse_passwd |
| 206 |
* doesn't give us the err / match / msg info. |
| 207 |
*/ |
| 208 |
rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err, |
| 209 |
(char **)&rs->sr_matched, |
| 210 |
&text, |
| 211 |
NULL, &rs->sr_ctrls, 0 ); |
| 212 |
|
| 213 |
if ( rc == LDAP_SUCCESS ) { |
| 214 |
if ( rs->sr_err == LDAP_SUCCESS ) { |
| 215 |
struct berval newpw; |
| 216 |
|
| 217 |
/* this never happens because |
| 218 |
* the frontend is generating |
| 219 |
* the new password, so when |
| 220 |
* the passwd exop is proxied, |
| 221 |
* it never delegates password |
| 222 |
* generation to the remote server |
| 223 |
*/ |
| 224 |
rc = ldap_parse_passwd( lc->lc_ld, res, |
| 225 |
&newpw ); |
| 226 |
if ( rc == LDAP_SUCCESS && |
| 227 |
!BER_BVISNULL( &newpw ) ) |
| 228 |
{ |
| 229 |
rs->sr_type = REP_EXTENDED; |
| 230 |
rs->sr_rspdata = slap_passwd_return( &newpw ); |
| 231 |
free( newpw.bv_val ); |
| 232 |
} |
| 233 |
|
| 234 |
} else { |
| 235 |
rc = rs->sr_err; |
| 236 |
} |
| 237 |
} |
| 238 |
ldap_msgfree( res ); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
if ( text ) { |
| 243 |
/* copy to tmpmem, doesn't need to be freed */ |
| 244 |
rs->sr_text = op->o_tmpalloc( strlen( text ) + 1, op->o_tmpmemctx ); |
| 245 |
strcpy( rs->sr_text, text ); |
| 246 |
ch_free( text ); |
| 247 |
} |
| 248 |
if ( rs->sr_matched ) |
| 249 |
rs->sr_flags |= REP_MATCHED_MUSTBEFREED; |
| 250 |
if ( rs->sr_ctrls ) |
| 251 |
rs->sr_flags |= REP_CTRLS_MUSTBEFREED; |
| 252 |
|
| 253 |
if ( rc != LDAP_SUCCESS ) { |
| 254 |
rs->sr_err = slap_map_api2result( rs ); |
| 255 |
if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) { |
| 256 |
do_retry = 0; |
| 257 |
if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_DONTSEND ) ) { |
| 258 |
goto retry; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
if ( LDAP_BACK_QUARANTINE( li ) ) { |
| 263 |
ldap_back_quarantine( op, rs ); |
| 264 |
} |
| 265 |
|
| 266 |
} else if ( LDAP_BACK_QUARANTINE( li ) ) { |
| 267 |
ldap_back_quarantine( op, rs ); |
| 268 |
} |
| 269 |
|
| 270 |
ldap_pvt_thread_mutex_lock( &li->li_counter_mutex ); |
| 271 |
ldap_pvt_mp_add_ulong( li->li_ops_completed[ SLAP_OP_EXTENDED ], 1 ); |
| 272 |
ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex ); |
| 273 |
|
| 274 |
if ( freedn ) { |
| 275 |
op->o_tmpfree( dn.bv_val, op->o_tmpmemctx ); |
| 276 |
op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx ); |
| 277 |
} |
| 278 |
|
| 279 |
/* in case, cleanup handler */ |
| 280 |
if ( lc == NULL ) { |
| 281 |
*lcp = NULL; |
| 282 |
} |
| 283 |
|
| 284 |
return rc; |
| 285 |
} |
| 286 |
|
| 287 |
static int |
| 288 |
ldap_back_exop_generic( |
| 289 |
Operation *op, |
| 290 |
SlapReply *rs, |
| 291 |
ldapconn_t **lcp ) |
| 292 |
{ |
| 293 |
ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private; |
| 294 |
|
| 295 |
ldapconn_t *lc = *lcp; |
| 296 |
LDAPMessage *res; |
| 297 |
ber_int_t msgid; |
| 298 |
int rc; |
| 299 |
int do_retry = 1; |
| 300 |
char *text = NULL; |
| 301 |
|
| 302 |
Debug( LDAP_DEBUG_ARGS, "==> ldap_back_exop_generic(%s, \"%s\")\n", |
| 303 |
op->ore_reqoid.bv_val, op->o_req_dn.bv_val ); |
| 304 |
assert( lc != NULL ); |
| 305 |
assert( rs->sr_ctrls == NULL ); |
| 306 |
|
| 307 |
retry: |
| 308 |
rc = ldap_extended_operation( lc->lc_ld, |
| 309 |
op->ore_reqoid.bv_val, op->ore_reqdata, |
| 310 |
op->o_ctrls, NULL, &msgid ); |
| 311 |
|
| 312 |
if ( rc == LDAP_SUCCESS ) { |
| 313 |
/* TODO: set timeout? */ |
| 314 |
/* by now, make sure no timeout is used (ITS#6282) */ |
| 315 |
struct timeval tv = { -1, 0 }; |
| 316 |
if ( ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res ) == -1 ) { |
| 317 |
ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER, &rc ); |
| 318 |
rs->sr_err = rc; |
| 319 |
|
| 320 |
} else { |
| 321 |
/* only touch when activity actually took place... */ |
| 322 |
if ( li->li_idle_timeout ) { |
| 323 |
lc->lc_time = op->o_time; |
| 324 |
} |
| 325 |
|
| 326 |
/* sigh. parse twice, because parse_passwd |
| 327 |
* doesn't give us the err / match / msg info. |
| 328 |
*/ |
| 329 |
rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err, |
| 330 |
(char **)&rs->sr_matched, |
| 331 |
&text, |
| 332 |
NULL, &rs->sr_ctrls, 0 ); |
| 333 |
if ( rc == LDAP_SUCCESS ) { |
| 334 |
if ( rs->sr_err == LDAP_SUCCESS ) { |
| 335 |
rc = ldap_parse_extended_result( lc->lc_ld, res, |
| 336 |
(char **)&rs->sr_rspoid, &rs->sr_rspdata, 0 ); |
| 337 |
if ( rc == LDAP_SUCCESS ) { |
| 338 |
rs->sr_type = REP_EXTENDED; |
| 339 |
} |
| 340 |
|
| 341 |
} else { |
| 342 |
rc = rs->sr_err; |
| 343 |
} |
| 344 |
} |
| 345 |
ldap_msgfree( res ); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
if ( rc != LDAP_SUCCESS ) { |
| 350 |
rs->sr_err = slap_map_api2result( rs ); |
| 351 |
if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) { |
| 352 |
do_retry = 0; |
| 353 |
if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_DONTSEND ) ) { |
| 354 |
goto retry; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
if ( LDAP_BACK_QUARANTINE( li ) ) { |
| 359 |
ldap_back_quarantine( op, rs ); |
| 360 |
} |
| 361 |
} else if ( LDAP_BACK_QUARANTINE( li ) ) { |
| 362 |
ldap_back_quarantine( op, rs ); |
| 363 |
} |
| 364 |
|
| 365 |
ldap_pvt_thread_mutex_lock( &li->li_counter_mutex ); |
| 366 |
ldap_pvt_mp_add_ulong( li->li_ops_completed[ SLAP_OP_EXTENDED ], 1 ); |
| 367 |
ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex ); |
| 368 |
|
| 369 |
if ( text ) { |
| 370 |
/* copy to tmpmem, doesn't need to be freed */ |
| 371 |
rs->sr_text = op->o_tmpalloc( strlen( text ) + 1, op->o_tmpmemctx ); |
| 372 |
strcpy( rs->sr_text, text ); |
| 373 |
ch_free( text ); |
| 374 |
} |
| 375 |
if ( rs->sr_matched ) |
| 376 |
rs->sr_flags |= REP_MATCHED_MUSTBEFREED; |
| 377 |
if ( rs->sr_ctrls ) |
| 378 |
rs->sr_flags |= REP_CTRLS_MUSTBEFREED; |
| 379 |
|
| 380 |
/* in case, cleanup handler */ |
| 381 |
if ( lc == NULL ) { |
| 382 |
*lcp = NULL; |
| 383 |
} |
| 384 |
|
| 385 |
return rc; |
| 386 |
} |