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,
43 Attribute *save_attrs;
45 if ( !acl_check_modlist( be, conn, op, e, modlist )) {
46 return LDAP_INSUFFICIENT_ACCESS;
49 save_attrs = e->e_attrs;
50 e->e_attrs = attrs_dup( e->e_attrs );
52 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
55 #ifdef SLAPD_SCHEMA_NOT_COMPAT
58 switch ( mod->mod_op )
62 err = add_values( e, mod, op->o_ndn );
66 err = delete_values( e, mod, op->o_ndn );
69 case LDAP_MOD_REPLACE:
70 err = replace_values( e, mod, op->o_ndn );
73 case SLAP_MOD_SOFTADD:
74 /* Avoid problems in index_add_mods()
75 * We need to add index if necessary.
77 #ifdef SLAPD_SCHEMA_NOT_COMPAT
78 mod->sm_op = LDAP_MOD_ADD;
80 mod->mod_op = LDAP_MOD_ADD;
82 err = add_values( e, mod, op->o_ndn );
84 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
90 if ( err != LDAP_SUCCESS ) {
91 attrs_free( e->e_attrs );
92 e->e_attrs = save_attrs;
93 /* unlock entry, delete from cache */
98 /* check for abandon */
99 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
100 if ( op->o_abandon ) {
101 attrs_free( e->e_attrs );
102 e->e_attrs = save_attrs;
103 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
104 return SLAPD_ABANDON;
106 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
108 /* check that the entry still obeys the schema */
109 rc = entry_schema_check( e, save_attrs, &text );
110 if ( rc != LDAP_SUCCESS ) {
111 attrs_free( e->e_attrs );
112 e->e_attrs = save_attrs;
113 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
118 /* check for abandon */
119 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
120 if ( op->o_abandon ) {
121 attrs_free( e->e_attrs );
122 e->e_attrs = save_attrs;
123 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
124 return SLAPD_ABANDON;
126 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
128 /* run through the attributes removing old indices */
129 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
132 #ifdef SLAPD_SCHEMA_NOT_COMPAT
133 switch ( mod->sm_op )
135 switch ( mod->mod_op )
138 case LDAP_MOD_REPLACE: {
139 #ifdef SLAPD_SCHEMA_NOT_COMPAT
140 /* not yet implemented */
142 /* Need to remove all values from indexes */
143 Attribute *a = save_attrs
144 ? attr_find( save_attrs, mod->mod_type )
148 (void) index_change_values( be,
152 SLAP_INDEX_DELETE_OP );
157 case LDAP_MOD_DELETE:
158 #ifdef SLAPD_SCHEMA_NOT_COMPAT
159 /* not yet implemented */
161 /* remove deleted values */
162 (void) index_change_values( be,
166 SLAP_INDEX_DELETE_OP );
172 attrs_free( save_attrs );
174 /* run through the attributes adding new indices */
175 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
178 switch ( mod->mod_op ) {
179 case LDAP_MOD_REPLACE:
181 #ifdef SLAPD_SCHEMA_NOT_COMPAT
182 /* not yet implemented */
184 (void) index_change_values( be,
192 case LDAP_MOD_DELETE: {
193 #ifdef SLAPD_SCHEMA_NOT_COMPAT
194 /* not yet implemented */
196 /* Need to add all remaining values */
197 Attribute *a = e->e_attrs
198 ? attr_find( e->e_attrs, mod->mod_type )
203 (void) index_change_values( be,
224 Modifications *modlist
228 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
231 int manageDSAit = get_manageDSAit( op );
233 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
235 /* acquire and lock entry */
236 if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
237 char* matched_dn = NULL;
238 struct berval **refs = NULL;
240 if ( matched != NULL ) {
241 matched_dn = ch_strdup( matched->e_dn );
242 refs = is_entry_referral( matched )
243 ? get_entry_referrals( be, conn, op, matched )
245 cache_return_entry_r( &li->li_cache, matched );
247 refs = default_referral;
250 send_ldap_result( conn, op, LDAP_REFERRAL,
251 matched_dn, NULL, refs, NULL );
253 if ( matched != NULL ) {
254 ber_bvecfree( refs );
261 if ( !manageDSAit && is_entry_referral( e ) ) {
262 /* parent is a referral, don't allow add */
263 /* parent is an alias, don't allow add */
264 struct berval **refs = get_entry_referrals( be,
267 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
270 send_ldap_result( conn, op, LDAP_REFERRAL,
271 e->e_dn, NULL, refs, NULL );
273 ber_bvecfree( refs );
278 /* Modify the entry */
279 rc = ldbm_modify_internal( be, conn, op, ndn, modlist, e );
281 if( rc != LDAP_SUCCESS ) {
282 if( rc != SLAPD_ABANDON ) {
283 send_ldap_result( conn, op, rc,
284 NULL, NULL, NULL, NULL );
290 /* change the entry itself */
291 if ( id2entry_add( be, e ) != 0 ) {
292 send_ldap_result( conn, op, LDAP_OTHER,
293 NULL, NULL, NULL, NULL );
297 send_ldap_result( conn, op, LDAP_SUCCESS,
298 NULL, NULL, NULL, NULL );
300 cache_return_entry_w( &li->li_cache, e );
304 cache_return_entry_w( &li->li_cache, e );
315 #ifdef SLAPD_SCHEMA_NOT_COMPAT
316 /* not yet implemented */
321 /* check if the values we're adding already exist */
322 if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
323 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
324 if ( value_find( a->a_vals, mod->mod_bvalues[i],
325 a->a_syntax, 3 ) == 0 ) {
326 return( LDAP_TYPE_OR_VALUE_EXISTS );
332 if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
333 return( LDAP_CONSTRAINT_VIOLATION );
337 return( LDAP_SUCCESS );
347 #ifdef SLAPD_SCHEMA_NOT_COMPAT
348 /* not yet implemented */
353 /* delete the entire attribute */
354 if ( mod->mod_bvalues == NULL ) {
355 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
356 mod->mod_type, 0, 0 );
357 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
358 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
361 /* delete specific values - find the attribute first */
362 if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
363 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
364 mod->mod_type, 0, 0 );
365 return( LDAP_NO_SUCH_ATTRIBUTE );
368 /* find each value to delete */
369 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
371 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
372 if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
373 a->a_syntax, 3 ) != 0 ) {
378 /* found a matching value - delete it */
379 ber_bvfree( a->a_vals[j] );
380 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
381 a->a_vals[k - 1] = a->a_vals[k];
383 a->a_vals[k - 1] = NULL;
385 /* delete the entire attribute, if no values remain */
386 if ( a->a_vals[0] == NULL) {
387 Debug( LDAP_DEBUG_ARGS,
388 "removing entire attribute %s\n",
389 mod->mod_type, 0, 0 );
390 if ( attr_delete( &e->e_attrs, mod->mod_type ) ) {
391 return LDAP_NO_SUCH_ATTRIBUTE;
398 /* looked through them all w/o finding it */
400 Debug( LDAP_DEBUG_ARGS,
401 "could not find value for attr %s\n",
402 mod->mod_type, 0, 0 );
403 return LDAP_NO_SUCH_ATTRIBUTE;
408 return( LDAP_SUCCESS );
418 #ifdef SLAPD_SCHEMA_NOT_COMPAT
419 /* not yet implemented */
421 (void) attr_delete( &e->e_attrs, mod->mod_type );
423 if ( mod->mod_bvalues != NULL &&
424 attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 )
426 return( LDAP_CONSTRAINT_VIOLATION );
430 return( LDAP_SUCCESS );