1 /* modify.c - ldbm backend modify routine */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
17 #include "back-ldbm.h"
18 #include "proto-back-ldbm.h"
20 static int add_values LDAP_P(( Entry *e, Modification *mod, char *dn ));
21 static int delete_values LDAP_P(( Entry *e, Modification *mod, char *dn ));
22 static int replace_values LDAP_P(( Entry *e, Modification *mod, char *dn ));
24 /* We need this function because of LDAP modrdn. If we do not
25 * add this there would be a bunch of code replication here
26 * and there and of course the likelihood of bugs increases.
27 * Juan C. Gomez (gomez@engr.sgi.com) 05/18/99
29 int ldbm_modify_internal(
34 Modifications *modlist,
41 int rc = LDAP_SUCCESS;
44 Attribute *save_attrs;
48 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
49 "ldbm_modify_internal: %s\n", dn ));
51 Debug(LDAP_DEBUG_TRACE, "ldbm_modify_internal: %s\n", dn, 0, 0);
55 if ( !acl_check_modlist( be, conn, op, e, modlist )) {
56 return LDAP_INSUFFICIENT_ACCESS;
59 save_attrs = e->e_attrs;
60 e->e_attrs = attrs_dup( e->e_attrs );
62 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
65 switch ( mod->sm_op ) {
68 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
69 "ldbm_modify_internal: add\n" ));
71 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: add\n", 0, 0, 0);
74 rc = add_values( e, mod, op->o_ndn.bv_val );
76 if( rc != LDAP_SUCCESS ) {
77 *text = "modify: add values failed";
79 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
80 "ldbm_modify_internal: failed %d (%s)\n",
83 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
91 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
92 "ldbm_modify_internal: delete\n" ));
94 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: delete\n", 0, 0, 0);
97 rc = delete_values( e, mod, op->o_ndn.bv_val );
98 assert( rc != LDAP_TYPE_OR_VALUE_EXISTS );
99 if( rc != LDAP_SUCCESS ) {
100 *text = "modify: delete values failed";
102 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
103 "ldbm_modify_internal: failed %d (%s)\n", rc, *text ));
105 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
111 case LDAP_MOD_REPLACE:
113 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
114 "ldbm_modify_internal: replace\n" ));
116 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: replace\n", 0, 0, 0);
119 rc = replace_values( e, mod, op->o_ndn.bv_val );
120 assert( rc != LDAP_TYPE_OR_VALUE_EXISTS );
121 if( rc != LDAP_SUCCESS ) {
122 *text = "modify: replace values failed";
124 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
125 "ldbm_modify_internal: failed %d (%s)\n", rc, *text ));
127 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
134 case SLAP_MOD_SOFTADD:
136 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
137 "ldbm_modify_internal: softadd\n" ));
139 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: softadd\n", 0, 0, 0);
142 /* Avoid problems in index_add_mods()
143 * We need to add index if necessary.
145 mod->sm_op = LDAP_MOD_ADD;
146 rc = add_values( e, mod, op->o_ndn.bv_val );
148 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
152 if( rc != LDAP_SUCCESS ) {
153 *text = "modify: (soft)add values failed";
155 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
156 "ldbm_modify_internal: failed %d (%s)\n", rc, *text ));
158 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
167 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
168 "ldbm_modify_internal: invalid op %d\n", mod->sm_op ));
170 Debug(LDAP_DEBUG_ANY, "ldbm_modify_internal: invalid op %d\n",
175 *text = "Invalid modify operation";
177 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
178 "ldbm_modify_internal: %d (%s)\n", rc, *text ));
180 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
186 if ( rc != LDAP_SUCCESS ) {
190 /* check if modified attribute was indexed */
191 rc = index_is_indexed( be, mod->sm_desc );
192 if ( rc == LDAP_SUCCESS ) {
193 ap = attr_find( save_attrs, mod->sm_desc );
194 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
196 ap = attr_find( e->e_attrs, mod->sm_desc );
197 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
201 /* check that the entry still obeys the schema */
202 rc = entry_schema_check( be, e, save_attrs, text, textbuf, textlen );
203 if ( rc != LDAP_SUCCESS ) {
205 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
206 "ldbm_modify_internal: entry failed schema check: %s\n",
209 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
216 /* check for abandon */
217 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
219 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
225 /* update the indices of the modified attributes */
227 /* start with deleting the old index entries */
228 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
229 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
230 rc = index_values( be, ap->a_desc, ap->a_vals, e->e_id,
231 SLAP_INDEX_DELETE_OP );
232 if ( rc != LDAP_SUCCESS ) {
234 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
235 "ldbm_modify_internal: Attribute index delete failure\n" ));
237 Debug( LDAP_DEBUG_ANY,
238 "Attribute index delete failure",
243 ap->a_flags &= ~SLAP_ATTR_IXDEL;
247 /* add the new index entries */
248 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
249 if ( ap->a_flags & SLAP_ATTR_IXADD ) {
250 rc = index_values( be, ap->a_desc, ap->a_vals, e->e_id,
252 if ( rc != LDAP_SUCCESS ) {
254 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
255 "ldbm_modify_internal: Attribute index add failure\n" ));
257 Debug( LDAP_DEBUG_ANY,
258 "Attribute index add failure",
263 ap->a_flags &= ~SLAP_ATTR_IXADD;
268 if ( rc == LDAP_SUCCESS ) {
269 attrs_free( save_attrs );
271 for ( ap = save_attrs; ap; ap = ap->a_next ) {
274 attrs_free( e->e_attrs );
275 e->e_attrs = save_attrs;
288 Modifications *modlist
292 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
295 int manageDSAit = get_manageDSAit( op );
296 const char *text = NULL;
297 char textbuf[SLAP_TEXT_BUFLEN];
298 size_t textlen = sizeof textbuf;
301 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
302 "ldbm_back_modify: enter\n" ));
304 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
308 /* acquire and lock entry */
309 if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
310 char* matched_dn = NULL;
313 if ( matched != NULL ) {
314 matched_dn = ch_strdup( matched->e_dn );
315 refs = is_entry_referral( matched )
316 ? get_entry_referrals( be, conn, op, matched )
318 cache_return_entry_r( &li->li_cache, matched );
320 refs = referral_rewrite( default_referral,
321 NULL, dn, LDAP_SCOPE_DEFAULT );
324 send_ldap_result( conn, op, LDAP_REFERRAL,
325 matched_dn, NULL, refs, NULL );
327 if ( refs ) ber_bvarray_free( refs );
333 if ( !manageDSAit && is_entry_referral( e ) ) {
334 /* parent is a referral, don't allow add */
335 /* parent is an alias, don't allow add */
336 BerVarray refs = get_entry_referrals( be,
340 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
341 "ldbm_back_modify: entry (%s) is referral\n", ndn->bv_val ));
343 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
348 send_ldap_result( conn, op, LDAP_REFERRAL,
349 e->e_dn, NULL, refs, NULL );
351 if ( refs ) ber_bvarray_free( refs );
356 /* Modify the entry */
357 rc = ldbm_modify_internal( be, conn, op, ndn->bv_val, modlist, e,
358 &text, textbuf, textlen );
360 if( rc != LDAP_SUCCESS ) {
361 if( rc != SLAPD_ABANDON ) {
362 send_ldap_result( conn, op, rc,
363 NULL, text, NULL, NULL );
369 /* change the entry itself */
370 if ( id2entry_add( be, e ) != 0 ) {
371 send_ldap_result( conn, op, LDAP_OTHER,
372 NULL, "id2entry failure", NULL, NULL );
376 send_ldap_result( conn, op, LDAP_SUCCESS,
377 NULL, NULL, NULL, NULL );
379 cache_return_entry_w( &li->li_cache, e );
383 cache_return_entry_w( &li->li_cache, e );
397 /* char *desc = mod->sm_desc->ad_cname.bv_val; */
398 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
400 a = attr_find( e->e_attrs, mod->sm_desc );
402 /* check if the values we're adding already exist */
404 if( mr == NULL || !mr->smr_match ) {
405 /* do not allow add of additional attribute
406 if no equality rule exists */
407 return LDAP_INAPPROPRIATE_MATCHING;
410 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
413 const char *text = NULL;
414 struct berval asserted;
416 rc = value_normalize( mod->sm_desc,
422 if( rc != LDAP_SUCCESS ) return rc;
424 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
426 int rc = value_match( &match, mod->sm_desc, mr,
427 SLAP_MR_VALUE_SYNTAX_MATCH,
428 &a->a_vals[j], &asserted, &text );
430 if( rc == LDAP_SUCCESS && match == 0 ) {
431 free( asserted.bv_val );
432 return LDAP_TYPE_OR_VALUE_EXISTS;
436 free( asserted.bv_val );
441 if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
442 /* this should return result return of attr_merge */
458 char *desc = mod->sm_desc->ad_cname.bv_val;
459 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
461 /* delete the entire attribute */
462 if ( mod->sm_bvalues == NULL ) {
464 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
465 "delete_values: removing entire attribute %s\n", desc ));
467 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
471 return( attr_delete( &e->e_attrs, mod->sm_desc ) ?
472 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
475 if( mr == NULL || !mr->smr_match ) {
476 /* disallow specific attributes from being deleted if
478 return LDAP_INAPPROPRIATE_MATCHING;
481 /* delete specific values - find the attribute first */
482 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
484 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
485 "ldap_modify_delete: Could not find attribute %s\n", desc ));
487 Debug( LDAP_DEBUG_ARGS, "ldap_modify_delete: "
488 "could not find attribute %s\n",
492 return( LDAP_NO_SUCH_ATTRIBUTE );
495 /* find each value to delete */
496 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
498 const char *text = NULL;
500 struct berval asserted;
502 rc = value_normalize( mod->sm_desc,
508 if( rc != LDAP_SUCCESS ) return rc;
511 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
513 int rc = value_match( &match, mod->sm_desc, mr,
514 SLAP_MR_VALUE_SYNTAX_MATCH,
515 &a->a_vals[j], &asserted, &text );
517 if( rc == LDAP_SUCCESS && match != 0 ) {
521 /* found a matching value */
525 free( a->a_vals[j].bv_val );
526 for ( k = j + 1; a->a_vals[k].bv_val != NULL; k++ ) {
527 a->a_vals[k - 1] = a->a_vals[k];
529 a->a_vals[k - 1].bv_val = NULL;
534 free( asserted.bv_val );
536 /* looked through them all w/o finding it */
539 LDAP_LOG(( "backend", LDAP_LEVEL_ARGS,
540 "delete_values: could not find value for attr %s\n", desc ));
542 Debug( LDAP_DEBUG_ARGS,
543 "ldbm_modify_delete: could not find value for attr %s\n",
547 return LDAP_NO_SUCH_ATTRIBUTE;
551 /* if no values remain, delete the entire attribute */
552 if ( a->a_vals[0].bv_val == NULL ) {
554 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
555 "delete_values: removing entire attribute %s\n", desc ));
557 Debug( LDAP_DEBUG_ARGS,
558 "removing entire attribute %s\n",
562 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
563 return LDAP_NO_SUCH_ATTRIBUTE;
577 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
579 if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
583 if ( mod->sm_bvalues != NULL &&
584 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )