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 );
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 );
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 );
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 );
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, &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,
279 dn, LDAP_SCOPE_DEFAULT )
281 cache_return_entry_r( &li->li_cache, matched );
283 refs = referral_rewrite( default_referral,
284 NULL, dn, LDAP_SCOPE_DEFAULT );
287 send_ldap_result( conn, op, LDAP_REFERRAL,
288 matched_dn, NULL, refs, NULL );
290 ber_bvecfree( refs );
296 if ( !manageDSAit && is_entry_referral( e ) ) {
297 /* parent is a referral, don't allow add */
298 /* parent is an alias, don't allow add */
299 struct berval **refs = get_entry_referrals( be,
300 conn, op, e, dn, LDAP_SCOPE_DEFAULT );
303 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
304 "ldbm_back_modify: entry (%s) is referral\n", ndn ));
306 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
311 send_ldap_result( conn, op, LDAP_REFERRAL,
312 e->e_dn, NULL, refs, NULL );
314 ber_bvecfree( refs );
319 /* Modify the entry */
320 rc = ldbm_modify_internal( be, conn, op, ndn, modlist, e,
321 &text, textbuf, textlen );
323 if( rc != LDAP_SUCCESS ) {
324 if( rc != SLAPD_ABANDON ) {
325 send_ldap_result( conn, op, rc,
326 NULL, text, NULL, NULL );
332 /* change the entry itself */
333 if ( id2entry_add( be, e ) != 0 ) {
334 send_ldap_result( conn, op, LDAP_OTHER,
335 NULL, "id2entry failure", NULL, NULL );
339 send_ldap_result( conn, op, LDAP_SUCCESS,
340 NULL, NULL, NULL, NULL );
342 cache_return_entry_w( &li->li_cache, e );
346 cache_return_entry_w( &li->li_cache, e );
360 /* char *desc = mod->sm_desc->ad_cname.bv_val; */
361 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
363 a = attr_find( e->e_attrs, mod->sm_desc );
365 /* check if the values we're adding already exist */
367 if( mr == NULL || !mr->smr_match ) {
368 /* do not allow add of additional attribute
369 if no equality rule exists */
370 return LDAP_INAPPROPRIATE_MATCHING;
373 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
376 const char *text = NULL;
377 struct berval *asserted;
379 rc = value_normalize( mod->sm_desc,
385 if( rc != LDAP_SUCCESS ) return rc;
387 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
389 int rc = value_match( &match, mod->sm_desc, mr,
390 SLAP_MR_MODIFY_MATCHING,
391 a->a_vals[j], asserted, &text );
393 if( rc == LDAP_SUCCESS && match == 0 ) {
394 ber_bvfree( asserted );
395 return LDAP_TYPE_OR_VALUE_EXISTS;
399 ber_bvfree( asserted );
404 if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
405 /* this should return result return of attr_merge */
421 char *desc = mod->sm_desc->ad_cname.bv_val;
422 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
424 /* delete the entire attribute */
425 if ( mod->sm_bvalues == NULL ) {
427 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
428 "delete_values: removing entire attribute %s\n", desc ));
430 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
434 return( attr_delete( &e->e_attrs, mod->sm_desc ) ?
435 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
438 if( mr == NULL || !mr->smr_match ) {
439 /* disallow specific attributes from being deleted if
441 return LDAP_INAPPROPRIATE_MATCHING;
444 /* delete specific values - find the attribute first */
445 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
447 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
448 "ldap_modify_delete: Could not find attribute %s\n", desc ));
450 Debug( LDAP_DEBUG_ARGS, "ldap_modify_delete: "
451 "could not find attribute %s\n",
455 return( LDAP_NO_SUCH_ATTRIBUTE );
458 /* find each value to delete */
459 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
461 const char *text = NULL;
463 struct berval *asserted;
465 rc = value_normalize( mod->sm_desc,
471 if( rc != LDAP_SUCCESS ) return rc;
474 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
476 int rc = value_match( &match, mod->sm_desc, mr,
477 SLAP_MR_MODIFY_MATCHING,
478 a->a_vals[j], asserted, &text );
480 if( rc == LDAP_SUCCESS && match != 0 ) {
484 /* found a matching value */
488 ber_bvfree( a->a_vals[j] );
489 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
490 a->a_vals[k - 1] = a->a_vals[k];
492 a->a_vals[k - 1] = NULL;
497 ber_bvfree( asserted );
499 /* looked through them all w/o finding it */
502 LDAP_LOG(( "backend", LDAP_LEVEL_ARGS,
503 "delete_values: could not find value for attr %s\n", desc ));
505 Debug( LDAP_DEBUG_ARGS,
506 "ldbm_modify_delete: could not find value for attr %s\n",
510 return LDAP_NO_SUCH_ATTRIBUTE;
514 /* if no values remain, delete the entire attribute */
515 if ( a->a_vals[0] == NULL ) {
517 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
518 "delete_values: removing entire attribute %s\n", desc ));
520 Debug( LDAP_DEBUG_ARGS,
521 "removing entire attribute %s\n",
525 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
526 return LDAP_NO_SUCH_ATTRIBUTE;
540 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
542 if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
546 if ( mod->sm_bvalues != NULL &&
547 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )