| 1 |
/* modify.c - mdb backend modify routine */ |
| 2 |
/* $OpenLDAP$ */ |
| 3 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 4 |
* |
| 5 |
* Copyright 2000-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 |
|
| 17 |
#include "portable.h" |
| 18 |
|
| 19 |
#include <stdio.h> |
| 20 |
#include <ac/string.h> |
| 21 |
#include <ac/time.h> |
| 22 |
|
| 23 |
#include "back-mdb.h" |
| 24 |
|
| 25 |
static struct berval scbva[] = { |
| 26 |
BER_BVC("glue"), |
| 27 |
BER_BVNULL |
| 28 |
}; |
| 29 |
|
| 30 |
#define CHECK_ADD 1 |
| 31 |
#define CHECK_DEL 2 |
| 32 |
|
| 33 |
static void |
| 34 |
mdb_modify_idxflags( |
| 35 |
Operation *op, |
| 36 |
AttributeDescription *desc, |
| 37 |
int ixcheck, |
| 38 |
Attribute *newattrs, |
| 39 |
Attribute *oldattrs ) |
| 40 |
{ |
| 41 |
struct berval ix_at; |
| 42 |
AttrInfo *ai; |
| 43 |
|
| 44 |
/* check if modified attribute was indexed |
| 45 |
* but not in case of NOOP... */ |
| 46 |
ai = mdb_index_mask( op->o_bd, desc, &ix_at ); |
| 47 |
if ( ai ) { |
| 48 |
if ( ixcheck & CHECK_DEL ) { |
| 49 |
Attribute *ap; |
| 50 |
struct berval ix2; |
| 51 |
|
| 52 |
ap = attr_find( oldattrs, desc ); |
| 53 |
if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL; |
| 54 |
|
| 55 |
/* ITS#8678 FIXME |
| 56 |
* If using 32bit hashes, or substring index, must account for |
| 57 |
* possible index collisions. If no substring index, and using |
| 58 |
* 64bit hashes, assume we don't need to check for collisions. |
| 59 |
* |
| 60 |
* In 2.5 use refcounts and avoid all of this mess. |
| 61 |
*/ |
| 62 |
if (!slap_hash64(-1) || (ai->ai_indexmask & SLAP_INDEX_SUBSTR)) { |
| 63 |
/* Find all other attrs that index to same slot */ |
| 64 |
for ( ap = newattrs; ap; ap = ap->a_next ) { |
| 65 |
ai = mdb_index_mask( op->o_bd, ap->a_desc, &ix2 ); |
| 66 |
if ( ai && ix2.bv_val == ix_at.bv_val ) |
| 67 |
ap->a_flags |= SLAP_ATTR_IXADD; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
} |
| 72 |
if ( ixcheck & CHECK_ADD ) { |
| 73 |
Attribute *ap; |
| 74 |
|
| 75 |
ap = attr_find( newattrs, desc ); |
| 76 |
if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
int mdb_modify_internal( |
| 82 |
Operation *op, |
| 83 |
MDB_txn *tid, |
| 84 |
Modifications *modlist, |
| 85 |
Entry *e, |
| 86 |
const char **text, |
| 87 |
char *textbuf, |
| 88 |
size_t textlen ) |
| 89 |
{ |
| 90 |
struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private; |
| 91 |
int rc, err; |
| 92 |
Modification *mod; |
| 93 |
Modifications *ml; |
| 94 |
Attribute *save_attrs; |
| 95 |
Attribute *ap, *aold, *anew; |
| 96 |
int glue_attr_delete = 0; |
| 97 |
int softop, chkpresent; |
| 98 |
int ixcheck; |
| 99 |
int a_flags; |
| 100 |
MDB_cursor *mvc = NULL; |
| 101 |
|
| 102 |
Debug( LDAP_DEBUG_TRACE, "mdb_modify_internal: 0x%08lx: %s\n", |
| 103 |
e->e_id, e->e_dn ); |
| 104 |
|
| 105 |
if ( !acl_check_modlist( op, e, modlist )) { |
| 106 |
return LDAP_INSUFFICIENT_ACCESS; |
| 107 |
} |
| 108 |
|
| 109 |
/* save_attrs will be disposed of by caller */ |
| 110 |
save_attrs = e->e_attrs; |
| 111 |
e->e_attrs = attrs_dup( e->e_attrs ); |
| 112 |
|
| 113 |
for ( ml = modlist; ml != NULL; ml = ml->sml_next ) { |
| 114 |
int match; |
| 115 |
mod = &ml->sml_mod; |
| 116 |
switch( mod->sm_op ) { |
| 117 |
case LDAP_MOD_ADD: |
| 118 |
case LDAP_MOD_REPLACE: |
| 119 |
if ( mod->sm_desc == slap_schema.si_ad_structuralObjectClass ) { |
| 120 |
value_match( &match, slap_schema.si_ad_structuralObjectClass, |
| 121 |
slap_schema.si_ad_structuralObjectClass-> |
| 122 |
ad_type->sat_equality, |
| 123 |
SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX, |
| 124 |
&mod->sm_values[0], &scbva[0], text ); |
| 125 |
if ( !match ) glue_attr_delete = 1; |
| 126 |
} |
| 127 |
} |
| 128 |
if ( glue_attr_delete ) |
| 129 |
break; |
| 130 |
} |
| 131 |
|
| 132 |
if ( glue_attr_delete ) { |
| 133 |
Attribute **app = &e->e_attrs; |
| 134 |
while ( *app != NULL ) { |
| 135 |
if ( !is_at_operational( (*app)->a_desc->ad_type )) { |
| 136 |
Attribute *save = *app; |
| 137 |
*app = (*app)->a_next; |
| 138 |
attr_free( save ); |
| 139 |
continue; |
| 140 |
} |
| 141 |
app = &(*app)->a_next; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
for ( ml = modlist; ml != NULL; ml = ml->sml_next ) { |
| 146 |
mod = &ml->sml_mod; |
| 147 |
ixcheck = 0; |
| 148 |
|
| 149 |
aold = attr_find( e->e_attrs, mod->sm_desc ); |
| 150 |
if (aold) |
| 151 |
a_flags = aold->a_flags; |
| 152 |
else |
| 153 |
a_flags = 0; |
| 154 |
|
| 155 |
switch ( mod->sm_op ) { |
| 156 |
case LDAP_MOD_ADD: |
| 157 |
softop = 0; |
| 158 |
chkpresent = 0; |
| 159 |
Debug(LDAP_DEBUG_ARGS, |
| 160 |
"mdb_modify_internal: add %s\n", |
| 161 |
mod->sm_desc->ad_cname.bv_val ); |
| 162 |
|
| 163 |
do_add: |
| 164 |
err = modify_add_values( e, mod, get_permissiveModify(op), |
| 165 |
text, textbuf, textlen ); |
| 166 |
|
| 167 |
if( softop ) { |
| 168 |
mod->sm_op = SLAP_MOD_SOFTADD; |
| 169 |
if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) |
| 170 |
err = LDAP_SUCCESS; |
| 171 |
} |
| 172 |
if( chkpresent ) { |
| 173 |
mod->sm_op = SLAP_MOD_ADD_IF_NOT_PRESENT; |
| 174 |
} |
| 175 |
|
| 176 |
if( err != LDAP_SUCCESS ) { |
| 177 |
Debug(LDAP_DEBUG_ARGS, "mdb_modify_internal: %d %s\n", |
| 178 |
err, *text ); |
| 179 |
} else { |
| 180 |
unsigned hi; |
| 181 |
if (!aold) |
| 182 |
anew = attr_find( e->e_attrs, mod->sm_desc ); |
| 183 |
else |
| 184 |
anew = aold; |
| 185 |
mdb_attr_multi_thresh( mdb, mod->sm_desc, &hi, NULL ); |
| 186 |
/* check for big multivalued attrs */ |
| 187 |
if ( anew->a_numvals > hi ) |
| 188 |
anew->a_flags |= SLAP_ATTR_BIG_MULTI; |
| 189 |
if ( anew->a_flags & SLAP_ATTR_BIG_MULTI ) { |
| 190 |
if (!mvc) { |
| 191 |
err = mdb_cursor_open( tid, mdb->mi_dbis[MDB_ID2VAL], &mvc ); |
| 192 |
if (err) { |
| 193 |
mval_fail: strncpy( textbuf, mdb_strerror( err ), textlen ); |
| 194 |
err = LDAP_OTHER; |
| 195 |
break; |
| 196 |
} |
| 197 |
} |
| 198 |
/* if prev was set, just add new values */ |
| 199 |
if (a_flags & SLAP_ATTR_BIG_MULTI ) { |
| 200 |
anew = (Attribute *)mod; |
| 201 |
/* Tweak nvals */ |
| 202 |
if (!anew->a_nvals) |
| 203 |
anew->a_nvals = anew->a_vals; |
| 204 |
} |
| 205 |
err = mdb_mval_put(op, mvc, e->e_id, anew); |
| 206 |
if (a_flags & SLAP_ATTR_BIG_MULTI ) { |
| 207 |
/* Undo nvals tweak */ |
| 208 |
if (anew->a_nvals == anew->a_vals) |
| 209 |
anew->a_nvals = NULL; |
| 210 |
} |
| 211 |
if ( err ) |
| 212 |
goto mval_fail; |
| 213 |
} |
| 214 |
ixcheck |= CHECK_ADD; |
| 215 |
} |
| 216 |
break; |
| 217 |
|
| 218 |
case LDAP_MOD_DELETE: |
| 219 |
if ( glue_attr_delete ) { |
| 220 |
err = LDAP_SUCCESS; |
| 221 |
break; |
| 222 |
} |
| 223 |
|
| 224 |
softop = 0; |
| 225 |
Debug(LDAP_DEBUG_ARGS, |
| 226 |
"mdb_modify_internal: delete %s\n", |
| 227 |
mod->sm_desc->ad_cname.bv_val ); |
| 228 |
do_del: |
| 229 |
err = modify_delete_values( e, mod, get_permissiveModify(op), |
| 230 |
text, textbuf, textlen ); |
| 231 |
|
| 232 |
if (softop) { |
| 233 |
mod->sm_op = SLAP_MOD_SOFTDEL; |
| 234 |
if ( err == LDAP_NO_SUCH_ATTRIBUTE ) { |
| 235 |
err = LDAP_SUCCESS; |
| 236 |
softop = 2; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if( err != LDAP_SUCCESS ) { |
| 241 |
Debug(LDAP_DEBUG_ARGS, "mdb_modify_internal: %d %s\n", |
| 242 |
err, *text ); |
| 243 |
} else { |
| 244 |
if (softop != 2) |
| 245 |
ixcheck |= CHECK_DEL; |
| 246 |
/* check for big multivalued attrs */ |
| 247 |
if (a_flags & SLAP_ATTR_BIG_MULTI) { |
| 248 |
Attribute a_dummy; |
| 249 |
if (!mvc) { |
| 250 |
err = mdb_cursor_open( tid, mdb->mi_dbis[MDB_ID2VAL], &mvc ); |
| 251 |
if (err) |
| 252 |
goto mval_fail; |
| 253 |
} |
| 254 |
if ( mod->sm_numvals ) { |
| 255 |
anew = attr_find( e->e_attrs, mod->sm_desc ); |
| 256 |
if ( anew ) { |
| 257 |
unsigned lo; |
| 258 |
mdb_attr_multi_thresh( mdb, mod->sm_desc, NULL, &lo ); |
| 259 |
if ( anew->a_numvals < lo ) { |
| 260 |
anew->a_flags ^= SLAP_ATTR_BIG_MULTI; |
| 261 |
anew = NULL; |
| 262 |
} else { |
| 263 |
anew = (Attribute *)mod; |
| 264 |
} |
| 265 |
} |
| 266 |
} else { |
| 267 |
anew = NULL; |
| 268 |
} |
| 269 |
if (!anew) { |
| 270 |
/* delete all values */ |
| 271 |
anew = &a_dummy; |
| 272 |
anew->a_desc = mod->sm_desc; |
| 273 |
anew->a_numvals = 0; |
| 274 |
} |
| 275 |
err = mdb_mval_del( op, mvc, e->e_id, anew ); |
| 276 |
if ( err ) |
| 277 |
goto mval_fail; |
| 278 |
} |
| 279 |
} |
| 280 |
break; |
| 281 |
|
| 282 |
case LDAP_MOD_REPLACE: |
| 283 |
Debug(LDAP_DEBUG_ARGS, |
| 284 |
"mdb_modify_internal: replace %s\n", |
| 285 |
mod->sm_desc->ad_cname.bv_val ); |
| 286 |
err = modify_replace_values( e, mod, get_permissiveModify(op), |
| 287 |
text, textbuf, textlen ); |
| 288 |
if( err != LDAP_SUCCESS ) { |
| 289 |
Debug(LDAP_DEBUG_ARGS, "mdb_modify_internal: %d %s\n", |
| 290 |
err, *text ); |
| 291 |
} else { |
| 292 |
unsigned hi; |
| 293 |
ixcheck = CHECK_DEL; |
| 294 |
if ( mod->sm_numvals ) |
| 295 |
ixcheck |= CHECK_ADD; |
| 296 |
if (a_flags & SLAP_ATTR_BIG_MULTI) { |
| 297 |
Attribute a_dummy; |
| 298 |
if (!mvc) { |
| 299 |
err = mdb_cursor_open( tid, mdb->mi_dbis[MDB_ID2VAL], &mvc ); |
| 300 |
if (err) |
| 301 |
goto mval_fail; |
| 302 |
} |
| 303 |
/* delete all values */ |
| 304 |
anew = &a_dummy; |
| 305 |
anew->a_desc = mod->sm_desc; |
| 306 |
anew->a_numvals = 0; |
| 307 |
err = mdb_mval_del( op, mvc, e->e_id, anew ); |
| 308 |
if (err) |
| 309 |
goto mval_fail; |
| 310 |
} |
| 311 |
anew = attr_find( e->e_attrs, mod->sm_desc ); |
| 312 |
mdb_attr_multi_thresh( mdb, mod->sm_desc, &hi, NULL ); |
| 313 |
if (mod->sm_numvals > hi) { |
| 314 |
anew->a_flags |= SLAP_ATTR_BIG_MULTI; |
| 315 |
if (!mvc) { |
| 316 |
err = mdb_cursor_open( tid, mdb->mi_dbis[MDB_ID2VAL], &mvc ); |
| 317 |
if (err) |
| 318 |
goto mval_fail; |
| 319 |
} |
| 320 |
err = mdb_mval_put(op, mvc, e->e_id, anew); |
| 321 |
if (err) |
| 322 |
goto mval_fail; |
| 323 |
} else if (anew) { |
| 324 |
/* revert back to normal attr */ |
| 325 |
anew->a_flags &= ~SLAP_ATTR_BIG_MULTI; |
| 326 |
} |
| 327 |
} |
| 328 |
break; |
| 329 |
|
| 330 |
case LDAP_MOD_INCREMENT: |
| 331 |
Debug(LDAP_DEBUG_ARGS, |
| 332 |
"mdb_modify_internal: increment %s\n", |
| 333 |
mod->sm_desc->ad_cname.bv_val ); |
| 334 |
err = modify_increment_values( e, mod, get_permissiveModify(op), |
| 335 |
text, textbuf, textlen ); |
| 336 |
if( err != LDAP_SUCCESS ) { |
| 337 |
Debug(LDAP_DEBUG_ARGS, |
| 338 |
"mdb_modify_internal: %d %s\n", |
| 339 |
err, *text ); |
| 340 |
} else { |
| 341 |
ixcheck = CHECK_ADD|CHECK_DEL; |
| 342 |
} |
| 343 |
break; |
| 344 |
|
| 345 |
case SLAP_MOD_SOFTADD: |
| 346 |
Debug(LDAP_DEBUG_ARGS, |
| 347 |
"mdb_modify_internal: softadd %s\n", |
| 348 |
mod->sm_desc->ad_cname.bv_val ); |
| 349 |
/* Avoid problems in index_add_mods() |
| 350 |
* We need to add index if necessary. |
| 351 |
*/ |
| 352 |
mod->sm_op = LDAP_MOD_ADD; |
| 353 |
softop = 1; |
| 354 |
chkpresent = 0; |
| 355 |
goto do_add; |
| 356 |
|
| 357 |
case SLAP_MOD_SOFTDEL: |
| 358 |
Debug(LDAP_DEBUG_ARGS, |
| 359 |
"mdb_modify_internal: softdel %s\n", |
| 360 |
mod->sm_desc->ad_cname.bv_val ); |
| 361 |
/* Avoid problems in index_delete_mods() |
| 362 |
* We need to add index if necessary. |
| 363 |
*/ |
| 364 |
mod->sm_op = LDAP_MOD_DELETE; |
| 365 |
softop = 1; |
| 366 |
goto do_del; |
| 367 |
|
| 368 |
case SLAP_MOD_ADD_IF_NOT_PRESENT: |
| 369 |
if ( attr_find( e->e_attrs, mod->sm_desc ) != NULL ) { |
| 370 |
/* skip */ |
| 371 |
err = LDAP_SUCCESS; |
| 372 |
break; |
| 373 |
} |
| 374 |
|
| 375 |
Debug(LDAP_DEBUG_ARGS, |
| 376 |
"mdb_modify_internal: add_if_not_present %s\n", |
| 377 |
mod->sm_desc->ad_cname.bv_val ); |
| 378 |
/* Avoid problems in index_add_mods() |
| 379 |
* We need to add index if necessary. |
| 380 |
*/ |
| 381 |
mod->sm_op = LDAP_MOD_ADD; |
| 382 |
softop = 0; |
| 383 |
chkpresent = 1; |
| 384 |
goto do_add; |
| 385 |
|
| 386 |
default: |
| 387 |
Debug(LDAP_DEBUG_ANY, "mdb_modify_internal: invalid op %d\n", |
| 388 |
mod->sm_op ); |
| 389 |
*text = "Invalid modify operation"; |
| 390 |
err = LDAP_OTHER; |
| 391 |
Debug(LDAP_DEBUG_ARGS, "mdb_modify_internal: %d %s\n", |
| 392 |
err, *text ); |
| 393 |
} |
| 394 |
|
| 395 |
if ( err != LDAP_SUCCESS ) { |
| 396 |
attrs_free( e->e_attrs ); |
| 397 |
e->e_attrs = save_attrs; |
| 398 |
/* unlock entry, delete from cache */ |
| 399 |
return err; |
| 400 |
} |
| 401 |
|
| 402 |
/* If objectClass was modified, reset the flags */ |
| 403 |
if ( mod->sm_desc == slap_schema.si_ad_objectClass ) { |
| 404 |
e->e_ocflags = 0; |
| 405 |
} |
| 406 |
|
| 407 |
if ( glue_attr_delete ) e->e_ocflags = 0; |
| 408 |
|
| 409 |
|
| 410 |
/* check if modified attribute was indexed |
| 411 |
* but not in case of NOOP... */ |
| 412 |
if ( !op->o_noop ) { |
| 413 |
mdb_modify_idxflags( op, mod->sm_desc, ixcheck, e->e_attrs, save_attrs ); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
/* check that the entry still obeys the schema */ |
| 418 |
ap = NULL; |
| 419 |
rc = entry_schema_check( op, e, save_attrs, get_relax(op), 0, &ap, |
| 420 |
text, textbuf, textlen ); |
| 421 |
if ( rc != LDAP_SUCCESS || op->o_noop ) { |
| 422 |
attrs_free( e->e_attrs ); |
| 423 |
/* clear the indexing flags */ |
| 424 |
for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) { |
| 425 |
ap->a_flags &= ~(SLAP_ATTR_IXADD|SLAP_ATTR_IXDEL); |
| 426 |
} |
| 427 |
e->e_attrs = save_attrs; |
| 428 |
|
| 429 |
if ( rc != LDAP_SUCCESS ) { |
| 430 |
Debug( LDAP_DEBUG_ANY, |
| 431 |
"entry failed schema check: %s\n", |
| 432 |
*text ); |
| 433 |
} |
| 434 |
|
| 435 |
/* if NOOP then silently revert to saved attrs */ |
| 436 |
return rc; |
| 437 |
} |
| 438 |
|
| 439 |
/* structuralObjectClass modified! */ |
| 440 |
if ( ap ) { |
| 441 |
assert( ap->a_desc == slap_schema.si_ad_structuralObjectClass ); |
| 442 |
if ( !op->o_noop ) { |
| 443 |
mdb_modify_idxflags( op, slap_schema.si_ad_structuralObjectClass, |
| 444 |
CHECK_ADD|CHECK_DEL, e->e_attrs, save_attrs ); |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
/* update the indices of the modified attributes */ |
| 449 |
|
| 450 |
/* start with deleting the old index entries */ |
| 451 |
for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) { |
| 452 |
if ( ap->a_flags & SLAP_ATTR_IXDEL ) { |
| 453 |
struct berval *vals; |
| 454 |
Attribute *a2; |
| 455 |
ap->a_flags &= ~SLAP_ATTR_IXDEL; |
| 456 |
a2 = attr_find( e->e_attrs, ap->a_desc ); |
| 457 |
if ( a2 ) { |
| 458 |
/* need to detect which values were deleted */ |
| 459 |
int i, j, k; |
| 460 |
/* let add know there were deletes */ |
| 461 |
if ( a2->a_flags & SLAP_ATTR_IXADD ) |
| 462 |
a2->a_flags |= SLAP_ATTR_IXDEL; |
| 463 |
vals = op->o_tmpalloc( (ap->a_numvals + 1) * |
| 464 |
sizeof(struct berval), op->o_tmpmemctx ); |
| 465 |
j = 0; |
| 466 |
for ( i=k=0; i < ap->a_numvals; i++ ) { |
| 467 |
char found = 0; |
| 468 |
BerValue* current = &ap->a_nvals[i]; |
| 469 |
int k2 = k; |
| 470 |
for (k2 = k ; k2 < a2->a_numvals; k2 ++) { |
| 471 |
int match = -1, rc; |
| 472 |
const char *text; |
| 473 |
|
| 474 |
rc = ordered_value_match( &match, a2->a_desc, |
| 475 |
ap->a_desc->ad_type->sat_equality, 0, |
| 476 |
&a2->a_nvals[k2], current, &text ); |
| 477 |
if ( rc == LDAP_SUCCESS && match == 0 ) { |
| 478 |
found = 1; |
| 479 |
break; |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
if (!found) { |
| 484 |
vals[j++] = *current; |
| 485 |
} else { |
| 486 |
k = k2 + 1; |
| 487 |
} |
| 488 |
} |
| 489 |
BER_BVZERO(vals+j); |
| 490 |
} else { |
| 491 |
/* attribute was completely deleted */ |
| 492 |
vals = ap->a_nvals; |
| 493 |
} |
| 494 |
rc = 0; |
| 495 |
if ( !BER_BVISNULL( vals )) { |
| 496 |
rc = mdb_index_values( op, tid, ap->a_desc, |
| 497 |
vals, e->e_id, SLAP_INDEX_DELETE_OP ); |
| 498 |
if ( rc != LDAP_SUCCESS ) { |
| 499 |
Debug( LDAP_DEBUG_ANY, |
| 500 |
"%s: attribute \"%s\" index delete failure\n", |
| 501 |
op->o_log_prefix, ap->a_desc->ad_cname.bv_val ); |
| 502 |
attrs_free( e->e_attrs ); |
| 503 |
e->e_attrs = save_attrs; |
| 504 |
} |
| 505 |
} |
| 506 |
if ( vals != ap->a_nvals ) |
| 507 |
op->o_tmpfree( vals, op->o_tmpmemctx ); |
| 508 |
if ( rc ) return rc; |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
/* add the new index entries */ |
| 513 |
for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) { |
| 514 |
if (ap->a_flags & SLAP_ATTR_IXADD) { |
| 515 |
ap->a_flags &= ~SLAP_ATTR_IXADD; |
| 516 |
if ( ap->a_flags & SLAP_ATTR_IXDEL ) { |
| 517 |
/* if any values were deleted, we must readd index |
| 518 |
* for all remaining values. |
| 519 |
*/ |
| 520 |
ap->a_flags &= ~SLAP_ATTR_IXDEL; |
| 521 |
rc = mdb_index_values( op, tid, ap->a_desc, |
| 522 |
ap->a_nvals, |
| 523 |
e->e_id, SLAP_INDEX_ADD_OP ); |
| 524 |
} else { |
| 525 |
int found = 0; |
| 526 |
/* if this was only an add, we only need to index |
| 527 |
* the added values. |
| 528 |
*/ |
| 529 |
for ( ml = modlist; ml != NULL; ml = ml->sml_next ) { |
| 530 |
struct berval *vals; |
| 531 |
if ( ml->sml_desc != ap->a_desc || !ml->sml_numvals ) |
| 532 |
continue; |
| 533 |
found = 1; |
| 534 |
switch( ml->sml_op ) { |
| 535 |
case LDAP_MOD_ADD: |
| 536 |
case LDAP_MOD_REPLACE: |
| 537 |
case LDAP_MOD_INCREMENT: |
| 538 |
case SLAP_MOD_SOFTADD: |
| 539 |
case SLAP_MOD_ADD_IF_NOT_PRESENT: |
| 540 |
if ( ml->sml_op == LDAP_MOD_INCREMENT ) |
| 541 |
vals = ap->a_nvals; |
| 542 |
else if ( ml->sml_nvalues ) |
| 543 |
vals = ml->sml_nvalues; |
| 544 |
else |
| 545 |
vals = ml->sml_values; |
| 546 |
rc = mdb_index_values( op, tid, ap->a_desc, |
| 547 |
vals, e->e_id, SLAP_INDEX_ADD_OP ); |
| 548 |
break; |
| 549 |
} |
| 550 |
if ( rc ) |
| 551 |
break; |
| 552 |
} |
| 553 |
/* This attr was affected by a modify of a subtype, so |
| 554 |
* there was no direct match in the modlist. Just readd |
| 555 |
* all of its values. |
| 556 |
*/ |
| 557 |
if ( !found ) { |
| 558 |
rc = mdb_index_values( op, tid, ap->a_desc, |
| 559 |
ap->a_nvals, |
| 560 |
e->e_id, SLAP_INDEX_ADD_OP ); |
| 561 |
} |
| 562 |
} |
| 563 |
if ( rc != LDAP_SUCCESS ) { |
| 564 |
Debug( LDAP_DEBUG_ANY, |
| 565 |
"%s: attribute \"%s\" index add failure\n", |
| 566 |
op->o_log_prefix, ap->a_desc->ad_cname.bv_val ); |
| 567 |
attrs_free( e->e_attrs ); |
| 568 |
e->e_attrs = save_attrs; |
| 569 |
return rc; |
| 570 |
} |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
return rc; |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
int |
| 579 |
mdb_modify( Operation *op, SlapReply *rs ) |
| 580 |
{ |
| 581 |
struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private; |
| 582 |
Entry *e = NULL; |
| 583 |
int manageDSAit = get_manageDSAit( op ); |
| 584 |
char textbuf[SLAP_TEXT_BUFLEN]; |
| 585 |
size_t textlen = sizeof textbuf; |
| 586 |
MDB_txn *txn = NULL; |
| 587 |
mdb_op_info opinfo = {{{ 0 }}}, *moi = &opinfo; |
| 588 |
Entry dummy = {0}; |
| 589 |
|
| 590 |
LDAPControl **preread_ctrl = NULL; |
| 591 |
LDAPControl **postread_ctrl = NULL; |
| 592 |
LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS]; |
| 593 |
int num_ctrls = 0; |
| 594 |
int numads = mdb->mi_numads; |
| 595 |
|
| 596 |
Debug( LDAP_DEBUG_ARGS, LDAP_XSTRING(mdb_modify) ": %s\n", |
| 597 |
op->o_req_dn.bv_val ); |
| 598 |
|
| 599 |
ctrls[num_ctrls] = NULL; |
| 600 |
|
| 601 |
/* begin transaction */ |
| 602 |
rs->sr_err = mdb_opinfo_get( op, mdb, 0, &moi ); |
| 603 |
rs->sr_text = NULL; |
| 604 |
if( rs->sr_err != 0 ) { |
| 605 |
Debug( LDAP_DEBUG_TRACE, |
| 606 |
LDAP_XSTRING(mdb_modify) ": txn_begin failed: " |
| 607 |
"%s (%d)\n", mdb_strerror(rs->sr_err), rs->sr_err ); |
| 608 |
rs->sr_err = LDAP_OTHER; |
| 609 |
rs->sr_text = "internal error"; |
| 610 |
goto return_results; |
| 611 |
} |
| 612 |
txn = moi->moi_txn; |
| 613 |
|
| 614 |
/* Don't touch the opattrs, if this is a contextCSN update |
| 615 |
* initiated from updatedn */ |
| 616 |
if ( !be_isupdate(op) || !op->orm_modlist || op->orm_modlist->sml_next || |
| 617 |
op->orm_modlist->sml_desc != slap_schema.si_ad_contextCSN ) { |
| 618 |
|
| 619 |
slap_mods_opattrs( op, &op->orm_modlist, 1 ); |
| 620 |
} |
| 621 |
|
| 622 |
/* get entry or ancestor */ |
| 623 |
rs->sr_err = mdb_dn2entry( op, txn, NULL, &op->o_req_ndn, &e, NULL, 1 ); |
| 624 |
|
| 625 |
if ( rs->sr_err != 0 ) { |
| 626 |
Debug( LDAP_DEBUG_TRACE, |
| 627 |
LDAP_XSTRING(mdb_modify) ": dn2entry failed (%d)\n", |
| 628 |
rs->sr_err ); |
| 629 |
switch( rs->sr_err ) { |
| 630 |
case MDB_NOTFOUND: |
| 631 |
break; |
| 632 |
case LDAP_BUSY: |
| 633 |
rs->sr_text = "ldap server busy"; |
| 634 |
goto return_results; |
| 635 |
default: |
| 636 |
rs->sr_err = LDAP_OTHER; |
| 637 |
rs->sr_text = "internal error"; |
| 638 |
goto return_results; |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
/* acquire and lock entry */ |
| 643 |
/* FIXME: dn2entry() should return non-glue entry */ |
| 644 |
if (( rs->sr_err == MDB_NOTFOUND ) || |
| 645 |
( !manageDSAit && e && is_entry_glue( e ))) |
| 646 |
{ |
| 647 |
if ( e != NULL ) { |
| 648 |
rs->sr_matched = ch_strdup( e->e_dn ); |
| 649 |
if ( is_entry_referral( e )) { |
| 650 |
BerVarray ref = get_entry_referrals( op, e ); |
| 651 |
rs->sr_ref = referral_rewrite( ref, &e->e_name, |
| 652 |
&op->o_req_dn, LDAP_SCOPE_DEFAULT ); |
| 653 |
ber_bvarray_free( ref ); |
| 654 |
} else { |
| 655 |
rs->sr_ref = NULL; |
| 656 |
} |
| 657 |
mdb_entry_return( op, e ); |
| 658 |
e = NULL; |
| 659 |
|
| 660 |
} else { |
| 661 |
rs->sr_ref = referral_rewrite( default_referral, NULL, |
| 662 |
&op->o_req_dn, LDAP_SCOPE_DEFAULT ); |
| 663 |
} |
| 664 |
|
| 665 |
rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED; |
| 666 |
rs->sr_err = LDAP_REFERRAL; |
| 667 |
send_ldap_result( op, rs ); |
| 668 |
goto done; |
| 669 |
} |
| 670 |
|
| 671 |
if ( !manageDSAit && is_entry_referral( e ) ) { |
| 672 |
/* entry is a referral, don't allow modify */ |
| 673 |
rs->sr_ref = get_entry_referrals( op, e ); |
| 674 |
|
| 675 |
Debug( LDAP_DEBUG_TRACE, |
| 676 |
LDAP_XSTRING(mdb_modify) ": entry is referral\n" ); |
| 677 |
|
| 678 |
rs->sr_err = LDAP_REFERRAL; |
| 679 |
rs->sr_matched = e->e_name.bv_val; |
| 680 |
rs->sr_flags = REP_REF_MUSTBEFREED; |
| 681 |
send_ldap_result( op, rs ); |
| 682 |
rs->sr_matched = NULL; |
| 683 |
goto done; |
| 684 |
} |
| 685 |
|
| 686 |
if ( get_assert( op ) && |
| 687 |
( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE )) |
| 688 |
{ |
| 689 |
rs->sr_err = LDAP_ASSERTION_FAILED; |
| 690 |
goto return_results; |
| 691 |
} |
| 692 |
|
| 693 |
if( op->o_preread ) { |
| 694 |
if( preread_ctrl == NULL ) { |
| 695 |
preread_ctrl = &ctrls[num_ctrls++]; |
| 696 |
ctrls[num_ctrls] = NULL; |
| 697 |
} |
| 698 |
if ( slap_read_controls( op, rs, e, |
| 699 |
&slap_pre_read_bv, preread_ctrl ) ) |
| 700 |
{ |
| 701 |
Debug( LDAP_DEBUG_TRACE, |
| 702 |
"<=- " LDAP_XSTRING(mdb_modify) ": pre-read " |
| 703 |
"failed!\n" ); |
| 704 |
if ( op->o_preread & SLAP_CONTROL_CRITICAL ) { |
| 705 |
/* FIXME: is it correct to abort |
| 706 |
* operation if control fails? */ |
| 707 |
goto return_results; |
| 708 |
} |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
/* Modify the entry */ |
| 713 |
dummy = *e; |
| 714 |
rs->sr_err = mdb_modify_internal( op, txn, op->orm_modlist, |
| 715 |
&dummy, &rs->sr_text, textbuf, textlen ); |
| 716 |
|
| 717 |
if( rs->sr_err != LDAP_SUCCESS ) { |
| 718 |
Debug( LDAP_DEBUG_TRACE, |
| 719 |
LDAP_XSTRING(mdb_modify) ": modify failed (%d)\n", |
| 720 |
rs->sr_err ); |
| 721 |
/* Only free attrs if they were dup'd. */ |
| 722 |
if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL; |
| 723 |
goto return_results; |
| 724 |
} |
| 725 |
|
| 726 |
/* change the entry itself */ |
| 727 |
rs->sr_err = mdb_id2entry_update( op, txn, NULL, &dummy ); |
| 728 |
if ( rs->sr_err != 0 ) { |
| 729 |
Debug( LDAP_DEBUG_TRACE, |
| 730 |
LDAP_XSTRING(mdb_modify) ": id2entry update failed " "(%d)\n", |
| 731 |
rs->sr_err ); |
| 732 |
if ( rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ) { |
| 733 |
rs->sr_text = "entry too big"; |
| 734 |
} else { |
| 735 |
rs->sr_err = LDAP_OTHER; |
| 736 |
rs->sr_text = "entry update failed"; |
| 737 |
} |
| 738 |
goto return_results; |
| 739 |
} |
| 740 |
|
| 741 |
if( op->o_postread ) { |
| 742 |
if( postread_ctrl == NULL ) { |
| 743 |
postread_ctrl = &ctrls[num_ctrls++]; |
| 744 |
ctrls[num_ctrls] = NULL; |
| 745 |
} |
| 746 |
if( slap_read_controls( op, rs, &dummy, |
| 747 |
&slap_post_read_bv, postread_ctrl ) ) |
| 748 |
{ |
| 749 |
Debug( LDAP_DEBUG_TRACE, |
| 750 |
"<=- " LDAP_XSTRING(mdb_modify) |
| 751 |
": post-read failed!\n" ); |
| 752 |
if ( op->o_postread & SLAP_CONTROL_CRITICAL ) { |
| 753 |
/* FIXME: is it correct to abort |
| 754 |
* operation if control fails? */ |
| 755 |
goto return_results; |
| 756 |
} |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
/* Only free attrs if they were dup'd. */ |
| 761 |
if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL; |
| 762 |
if( moi == &opinfo ) { |
| 763 |
LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next ); |
| 764 |
opinfo.moi_oe.oe_key = NULL; |
| 765 |
if( op->o_noop ) { |
| 766 |
mdb->mi_numads = numads; |
| 767 |
mdb_txn_abort( txn ); |
| 768 |
rs->sr_err = LDAP_X_NO_OPERATION; |
| 769 |
txn = NULL; |
| 770 |
goto return_results; |
| 771 |
} else { |
| 772 |
rs->sr_err = mdb_txn_commit( txn ); |
| 773 |
if ( rs->sr_err ) |
| 774 |
mdb->mi_numads = numads; |
| 775 |
txn = NULL; |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
if( rs->sr_err != 0 ) { |
| 780 |
Debug( LDAP_DEBUG_ANY, |
| 781 |
LDAP_XSTRING(mdb_modify) ": txn_%s failed: %s (%d)\n", |
| 782 |
op->o_noop ? "abort (no-op)" : "commit", |
| 783 |
mdb_strerror(rs->sr_err), rs->sr_err ); |
| 784 |
rs->sr_err = LDAP_OTHER; |
| 785 |
rs->sr_text = "commit failed"; |
| 786 |
|
| 787 |
goto return_results; |
| 788 |
} |
| 789 |
|
| 790 |
Debug( LDAP_DEBUG_TRACE, |
| 791 |
LDAP_XSTRING(mdb_modify) ": updated%s id=%08lx dn=\"%s\"\n", |
| 792 |
op->o_noop ? " (no-op)" : "", |
| 793 |
dummy.e_id, op->o_req_dn.bv_val ); |
| 794 |
|
| 795 |
rs->sr_err = LDAP_SUCCESS; |
| 796 |
rs->sr_text = NULL; |
| 797 |
if( num_ctrls ) rs->sr_ctrls = ctrls; |
| 798 |
|
| 799 |
return_results: |
| 800 |
if( dummy.e_attrs ) { |
| 801 |
attrs_free( dummy.e_attrs ); |
| 802 |
} |
| 803 |
send_ldap_result( op, rs ); |
| 804 |
|
| 805 |
#if 0 |
| 806 |
if( rs->sr_err == LDAP_SUCCESS && mdb->bi_txn_cp_kbyte ) { |
| 807 |
TXN_CHECKPOINT( mdb->bi_dbenv, |
| 808 |
mdb->bi_txn_cp_kbyte, mdb->bi_txn_cp_min, 0 ); |
| 809 |
} |
| 810 |
#endif |
| 811 |
|
| 812 |
done: |
| 813 |
slap_graduate_commit_csn( op ); |
| 814 |
|
| 815 |
if( moi == &opinfo ) { |
| 816 |
if( txn != NULL ) { |
| 817 |
mdb->mi_numads = numads; |
| 818 |
mdb_txn_abort( txn ); |
| 819 |
} |
| 820 |
if ( opinfo.moi_oe.oe_key ) { |
| 821 |
LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next ); |
| 822 |
} |
| 823 |
} else { |
| 824 |
moi->moi_ref--; |
| 825 |
} |
| 826 |
|
| 827 |
if( e != NULL ) { |
| 828 |
mdb_entry_return( op, e ); |
| 829 |
} |
| 830 |
|
| 831 |
if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) { |
| 832 |
slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx ); |
| 833 |
slap_sl_free( *preread_ctrl, op->o_tmpmemctx ); |
| 834 |
} |
| 835 |
if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) { |
| 836 |
slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx ); |
| 837 |
slap_sl_free( *postread_ctrl, op->o_tmpmemctx ); |
| 838 |
} |
| 839 |
|
| 840 |
rs->sr_text = NULL; |
| 841 |
|
| 842 |
return rs->sr_err; |
| 843 |
} |