1 /* modify.c - ldbm backend modify routine */
4 * Copyright 1998-1999 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,
42 Attribute *save_attrs;
44 if ( !acl_check_modlist( be, conn, op, e, modlist )) {
45 return LDAP_INSUFFICIENT_ACCESS;
48 save_attrs = e->e_attrs;
49 e->e_attrs = attrs_dup( e->e_attrs );
51 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
54 #ifdef SLAPD_SCHEMA_NOT_COMPAT
57 switch ( mod->mod_op )
61 err = add_values( e, mod, op->o_ndn );
65 err = delete_values( e, mod, op->o_ndn );
68 case LDAP_MOD_REPLACE:
69 err = replace_values( e, mod, op->o_ndn );
72 case SLAP_MOD_SOFTADD:
73 /* Avoid problems in index_add_mods()
74 * We need to add index if necessary.
76 #ifdef SLAPD_SCHEMA_NOT_COMPAT
77 mod->sm_op = LDAP_MOD_ADD;
79 mod->mod_op = LDAP_MOD_ADD;
81 err = add_values( e, mod, op->o_ndn );
83 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
89 if ( err != LDAP_SUCCESS ) {
90 attrs_free( e->e_attrs );
91 e->e_attrs = save_attrs;
92 /* unlock entry, delete from cache */
97 /* check for abandon */
98 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
99 if ( op->o_abandon ) {
100 attrs_free( e->e_attrs );
101 e->e_attrs = save_attrs;
102 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
103 return SLAPD_ABANDON;
105 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
107 /* check that the entry still obeys the schema */
108 if ( schema_check_entry( e ) != 0 ) {
109 attrs_free( e->e_attrs );
110 e->e_attrs = save_attrs;
111 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
112 return LDAP_OBJECT_CLASS_VIOLATION;
115 /* check for abandon */
116 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
117 if ( op->o_abandon ) {
118 attrs_free( e->e_attrs );
119 e->e_attrs = save_attrs;
120 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
121 return SLAPD_ABANDON;
123 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
125 /* run through the attributes removing old indices */
126 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
129 #ifdef SLAPD_SCHEMA_NOT_COMPAT
130 switch ( mod->sm_op )
132 switch ( mod->mod_op )
135 case LDAP_MOD_REPLACE: {
136 #ifdef SLAPD_SCHEMA_NOT_COMPAT
137 /* not yet implemented */
139 /* Need to remove all values from indexes */
140 Attribute *a = save_attrs
141 ? attr_find( save_attrs, mod->mod_type )
145 (void) index_change_values( be,
149 SLAP_INDEX_DELETE_OP );
154 case LDAP_MOD_DELETE:
155 #ifdef SLAPD_SCHEMA_NOT_COMPAT
156 /* not yet implemented */
158 /* remove deleted values */
159 (void) index_change_values( be,
163 SLAP_INDEX_DELETE_OP );
169 attrs_free( save_attrs );
171 /* run through the attributes adding new indices */
172 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
175 switch ( mod->mod_op ) {
176 case LDAP_MOD_REPLACE:
178 #ifdef SLAPD_SCHEMA_NOT_COMPAT
179 /* not yet implemented */
181 (void) index_change_values( be,
189 case LDAP_MOD_DELETE: {
190 #ifdef SLAPD_SCHEMA_NOT_COMPAT
191 /* not yet implemented */
193 /* Need to add all remaining values */
194 Attribute *a = e->e_attrs
195 ? attr_find( e->e_attrs, mod->mod_type )
200 (void) index_change_values( be,
221 Modifications *modlist
225 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
228 int manageDSAit = get_manageDSAit( op );
230 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
232 /* acquire and lock entry */
233 if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
234 char* matched_dn = NULL;
235 struct berval **refs = NULL;
237 if ( matched != NULL ) {
238 matched_dn = ch_strdup( matched->e_dn );
239 refs = is_entry_referral( matched )
240 ? get_entry_referrals( be, conn, op, matched )
242 cache_return_entry_r( &li->li_cache, matched );
244 refs = default_referral;
247 send_ldap_result( conn, op, LDAP_REFERRAL,
248 matched_dn, NULL, refs, NULL );
250 if ( matched != NULL ) {
251 ber_bvecfree( refs );
258 if ( !manageDSAit && is_entry_referral( e ) ) {
259 /* parent is a referral, don't allow add */
260 /* parent is an alias, don't allow add */
261 struct berval **refs = get_entry_referrals( be,
264 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
267 send_ldap_result( conn, op, LDAP_REFERRAL,
268 e->e_dn, NULL, refs, NULL );
270 ber_bvecfree( refs );
275 /* Modify the entry */
276 rc = ldbm_modify_internal( be, conn, op, ndn, modlist, e );
278 if( rc != LDAP_SUCCESS ) {
279 if( rc != SLAPD_ABANDON ) {
280 send_ldap_result( conn, op, rc,
281 NULL, NULL, NULL, NULL );
287 /* change the entry itself */
288 if ( id2entry_add( be, e ) != 0 ) {
289 send_ldap_result( conn, op, LDAP_OTHER,
290 NULL, NULL, NULL, NULL );
294 send_ldap_result( conn, op, LDAP_SUCCESS,
295 NULL, NULL, NULL, NULL );
297 cache_return_entry_w( &li->li_cache, e );
301 cache_return_entry_w( &li->li_cache, e );
312 #ifdef SLAPD_SCHEMA_NOT_COMPAT
313 /* not yet implemented */
318 /* check if the values we're adding already exist */
319 if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
320 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
321 if ( value_find( a->a_vals, mod->mod_bvalues[i],
322 a->a_syntax, 3 ) == 0 ) {
323 return( LDAP_TYPE_OR_VALUE_EXISTS );
329 if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
330 return( LDAP_CONSTRAINT_VIOLATION );
334 return( LDAP_SUCCESS );
344 #ifdef SLAPD_SCHEMA_NOT_COMPAT
345 /* not yet implemented */
350 /* delete the entire attribute */
351 if ( mod->mod_bvalues == NULL ) {
352 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
353 mod->mod_type, 0, 0 );
354 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
355 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
358 /* delete specific values - find the attribute first */
359 if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
360 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
361 mod->mod_type, 0, 0 );
362 return( LDAP_NO_SUCH_ATTRIBUTE );
365 /* find each value to delete */
366 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
368 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
369 if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
370 a->a_syntax, 3 ) != 0 ) {
375 /* found a matching value - delete it */
376 ber_bvfree( a->a_vals[j] );
377 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
378 a->a_vals[k - 1] = a->a_vals[k];
380 a->a_vals[k - 1] = NULL;
382 /* delete the entire attribute, if no values remain */
383 if ( a->a_vals[0] == NULL) {
384 Debug( LDAP_DEBUG_ARGS,
385 "removing entire attribute %s\n",
386 mod->mod_type, 0, 0 );
387 if ( attr_delete( &e->e_attrs, mod->mod_type ) ) {
388 return LDAP_NO_SUCH_ATTRIBUTE;
395 /* looked through them all w/o finding it */
397 Debug( LDAP_DEBUG_ARGS,
398 "could not find value for attr %s\n",
399 mod->mod_type, 0, 0 );
400 return( LDAP_NO_SUCH_ATTRIBUTE );
405 return( LDAP_SUCCESS );
415 #ifdef SLAPD_SCHEMA_NOT_COMPAT
416 /* not yet implemented */
418 (void) attr_delete( &e->e_attrs, mod->mod_type );
420 if ( mod->mod_bvalues != NULL &&
421 attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 )
423 return( LDAP_CONSTRAINT_VIOLATION );
427 return( LDAP_SUCCESS );