1 /* modify.c - ldbm backend modify routine */
4 * Copyright 1998-2000 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
30 int ldbm_modify_internal(
35 Modifications *modlist,
45 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 err = add_values( e, mod, op->o_ndn.bv_val );
76 if( err != 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 err = delete_values( e, mod, op->o_ndn.bv_val );
98 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
99 if( err != LDAP_SUCCESS ) {
100 *text = "modify: delete values failed";
102 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
103 "ldbm_modify_internal: failed %d (%s)\n", err, *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 err = replace_values( e, mod, op->o_ndn.bv_val );
120 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
121 if( err != LDAP_SUCCESS ) {
122 *text = "modify: replace values failed";
124 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
125 "ldbm_modify_internal: failed %d (%s)\n", err, *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 err = add_values( e, mod, op->o_ndn.bv_val );
148 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
152 if( err != LDAP_SUCCESS ) {
153 *text = "modify: (soft)add values failed";
155 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
156 "ldbm_modify_internal: failed %d (%s)\n", err, *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", err, *text ));
180 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
186 if ( err != LDAP_SUCCESS ) {
187 attrs_free( e->e_attrs );
188 e->e_attrs = save_attrs;
189 /* unlock entry, delete from cache */
194 /* check for abandon */
195 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
196 if ( op->o_abandon ) {
197 attrs_free( e->e_attrs );
198 e->e_attrs = save_attrs;
199 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
200 return SLAPD_ABANDON;
202 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
204 /* check that the entry still obeys the schema */
205 rc = entry_schema_check( e, save_attrs, text, textbuf, textlen );
206 if ( rc != LDAP_SUCCESS ) {
207 attrs_free( e->e_attrs );
208 e->e_attrs = save_attrs;
210 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
211 "ldbm_modify_internal: entry failed schema check: %s\n",
214 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
221 /* check for abandon */
222 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
223 if ( op->o_abandon ) {
224 attrs_free( e->e_attrs );
225 e->e_attrs = save_attrs;
226 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
227 return SLAPD_ABANDON;
229 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
231 /* delete indices for old attributes */
232 index_entry_del( be, e, save_attrs);
234 /* add indices for new attributes */
235 index_entry_add( be, e, e->e_attrs);
237 attrs_free( save_attrs );
250 Modifications *modlist
254 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
257 int manageDSAit = get_manageDSAit( op );
258 const char *text = NULL;
259 char textbuf[SLAP_TEXT_BUFLEN];
260 size_t textlen = sizeof textbuf;
263 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
264 "ldbm_back_modify: enter\n" ));
266 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
270 /* acquire and lock entry */
271 if ( (e = dn2entry_w( be, ndn->bv_val, &matched )) == NULL ) {
272 char* matched_dn = NULL;
273 struct berval **refs;
275 if ( matched != NULL ) {
276 matched_dn = ch_strdup( matched->e_dn );
277 refs = is_entry_referral( matched )
278 ? get_entry_referrals( be, conn, op, matched )
280 cache_return_entry_r( &li->li_cache, matched );
282 refs = referral_rewrite( default_referral,
283 NULL, dn, LDAP_SCOPE_DEFAULT );
286 send_ldap_result( conn, op, LDAP_REFERRAL,
287 matched_dn, NULL, refs, NULL );
289 ber_bvecfree( refs );
295 if ( !manageDSAit && is_entry_referral( e ) ) {
296 /* parent is a referral, don't allow add */
297 /* parent is an alias, don't allow add */
298 struct berval **refs = get_entry_referrals( be,
302 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
303 "ldbm_back_modify: entry (%s) is referral\n", ndn->bv_val ));
305 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
310 send_ldap_result( conn, op, LDAP_REFERRAL,
311 e->e_dn, NULL, refs, NULL );
313 ber_bvecfree( refs );
318 /* Modify the entry */
319 rc = ldbm_modify_internal( be, conn, op, ndn->bv_val, modlist, e,
320 &text, textbuf, textlen );
322 if( rc != LDAP_SUCCESS ) {
323 if( rc != SLAPD_ABANDON ) {
324 send_ldap_result( conn, op, rc,
325 NULL, text, NULL, NULL );
331 /* change the entry itself */
332 if ( id2entry_add( be, e ) != 0 ) {
333 send_ldap_result( conn, op, LDAP_OTHER,
334 NULL, "id2entry failure", NULL, NULL );
338 send_ldap_result( conn, op, LDAP_SUCCESS,
339 NULL, NULL, NULL, NULL );
341 cache_return_entry_w( &li->li_cache, e );
345 cache_return_entry_w( &li->li_cache, e );
359 /* char *desc = mod->sm_desc->ad_cname.bv_val; */
360 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
362 a = attr_find( e->e_attrs, mod->sm_desc );
364 /* check if the values we're adding already exist */
366 if( mr == NULL || !mr->smr_match ) {
367 /* do not allow add of additional attribute
368 if no equality rule exists */
369 return LDAP_INAPPROPRIATE_MATCHING;
372 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
375 const char *text = NULL;
376 struct berval asserted;
378 rc = value_normalize( mod->sm_desc,
384 if( rc != LDAP_SUCCESS ) return rc;
386 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
388 int rc = value_match( &match, mod->sm_desc, mr,
389 SLAP_MR_VALUE_SYNTAX_MATCH,
390 a->a_vals[j], &asserted, &text );
392 if( rc == LDAP_SUCCESS && match == 0 ) {
393 free( asserted.bv_val );
394 return LDAP_TYPE_OR_VALUE_EXISTS;
398 free( asserted.bv_val );
403 if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
404 /* this should return result return of attr_merge */
420 char *desc = mod->sm_desc->ad_cname.bv_val;
421 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
423 /* delete the entire attribute */
424 if ( mod->sm_bvalues == NULL ) {
426 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
427 "delete_values: removing entire attribute %s\n", desc ));
429 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
433 return( attr_delete( &e->e_attrs, mod->sm_desc ) ?
434 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
437 if( mr == NULL || !mr->smr_match ) {
438 /* disallow specific attributes from being deleted if
440 return LDAP_INAPPROPRIATE_MATCHING;
443 /* delete specific values - find the attribute first */
444 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
446 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
447 "ldap_modify_delete: Could not find attribute %s\n", desc ));
449 Debug( LDAP_DEBUG_ARGS, "ldap_modify_delete: "
450 "could not find attribute %s\n",
454 return( LDAP_NO_SUCH_ATTRIBUTE );
457 /* find each value to delete */
458 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
460 const char *text = NULL;
462 struct berval asserted;
464 rc = value_normalize( mod->sm_desc,
470 if( rc != LDAP_SUCCESS ) return rc;
473 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
475 int rc = value_match( &match, mod->sm_desc, mr,
476 SLAP_MR_VALUE_SYNTAX_MATCH,
477 a->a_vals[j], &asserted, &text );
479 if( rc == LDAP_SUCCESS && match != 0 ) {
483 /* found a matching value */
487 ber_bvfree( a->a_vals[j] );
488 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
489 a->a_vals[k - 1] = a->a_vals[k];
491 a->a_vals[k - 1] = NULL;
496 free( asserted.bv_val );
498 /* looked through them all w/o finding it */
501 LDAP_LOG(( "backend", LDAP_LEVEL_ARGS,
502 "delete_values: could not find value for attr %s\n", desc ));
504 Debug( LDAP_DEBUG_ARGS,
505 "ldbm_modify_delete: could not find value for attr %s\n",
509 return LDAP_NO_SUCH_ATTRIBUTE;
513 /* if no values remain, delete the entire attribute */
514 if ( a->a_vals[0] == NULL ) {
516 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
517 "delete_values: removing entire attribute %s\n", desc ));
519 Debug( LDAP_DEBUG_ARGS,
520 "removing entire attribute %s\n",
524 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
525 return LDAP_NO_SUCH_ATTRIBUTE;
539 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
541 if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
545 if ( mod->sm_bvalues != NULL &&
546 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )