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 /* We need this function because of LDAP modrdn. If we do not
21 * add this there would be a bunch of code replication here
22 * and there and of course the likelihood of bugs increases.
23 * Juan C. Gomez (gomez@engr.sgi.com) 05/18/99
26 int ldbm_modify_internal(
39 Attribute *save_attrs;
41 if ( !acl_check_modlist( be, conn, op, e, modlist )) {
42 return LDAP_INSUFFICIENT_ACCESS;
45 save_attrs = e->e_attrs;
46 e->e_attrs = attrs_dup( e->e_attrs );
48 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
51 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
53 err = add_values( e, mod, op->o_ndn );
57 err = delete_values( e, mod, op->o_ndn );
60 case LDAP_MOD_REPLACE:
61 err = replace_values( e, mod, op->o_ndn );
64 case LDAP_MOD_SOFTADD:
65 /* Avoid problems in index_add_mods()
66 * We need to add index if necessary.
68 mod->mod_op = LDAP_MOD_ADD;
69 if ( (err = add_values( e, mod, op->o_ndn ))
70 == LDAP_TYPE_OR_VALUE_EXISTS ) {
73 mod->mod_op = LDAP_MOD_SOFTADD;
79 if ( err != LDAP_SUCCESS ) {
80 attrs_free( e->e_attrs );
81 e->e_attrs = save_attrs;
82 /* unlock entry, delete from cache */
87 /* check for abandon */
88 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
89 if ( op->o_abandon ) {
90 attrs_free( e->e_attrs );
91 e->e_attrs = save_attrs;
92 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
95 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
97 /* check that the entry still obeys the schema */
98 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
99 attrs_free( e->e_attrs );
100 e->e_attrs = save_attrs;
101 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
102 return LDAP_OBJECT_CLASS_VIOLATION;
105 /* check for abandon */
106 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
107 if ( op->o_abandon ) {
108 attrs_free( e->e_attrs );
109 e->e_attrs = save_attrs;
110 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
111 return SLAPD_ABANDON;
113 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
115 /* remove old indices */
116 if( save_attrs != NULL ) {
117 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
119 if( ( mod->mod_op & ~LDAP_MOD_BVALUES )
120 == LDAP_MOD_REPLACE )
122 /* Need to remove all values from indexes */
123 a = attr_find( save_attrs, mod->mod_type );
126 (void) index_change_values( be,
130 SLAP_INDEX_DELETE_OP);
134 attrs_free( save_attrs );
138 if ( index_add_mods( be, modlist, e->e_id ) != 0 ) {
139 /* our indices are likely hosed */
140 return LDAP_OPERATIONS_ERROR;
158 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
161 int manageDSAit = get_manageDSAit( op );
163 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
165 /* acquire and lock entry */
166 if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
167 char* matched_dn = NULL;
168 struct berval **refs = NULL;
170 if ( matched != NULL ) {
171 matched_dn = ch_strdup( matched->e_dn );
172 refs = is_entry_referral( matched )
173 ? get_entry_referrals( be, conn, op, matched )
175 cache_return_entry_r( &li->li_cache, matched );
177 refs = default_referral;
180 send_ldap_result( conn, op, LDAP_REFERRAL,
181 matched_dn, NULL, refs, NULL );
183 if ( matched != NULL ) {
184 ber_bvecfree( refs );
191 if ( !manageDSAit && is_entry_referral( e ) ) {
192 /* parent is a referral, don't allow add */
193 /* parent is an alias, don't allow add */
194 struct berval **refs = get_entry_referrals( be,
197 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
200 send_ldap_result( conn, op, LDAP_REFERRAL,
201 e->e_dn, NULL, refs, NULL );
203 ber_bvecfree( refs );
208 /* Modify the entry */
209 rc = ldbm_modify_internal( be, conn, op, ndn, modlist, e );
211 if( rc != LDAP_SUCCESS ) {
212 if( rc != SLAPD_ABANDON ) {
213 send_ldap_result( conn, op, rc,
214 NULL, NULL, NULL, NULL );
220 /* change the entry itself */
221 if ( id2entry_add( be, e ) != 0 ) {
222 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
223 NULL, NULL, NULL, NULL );
227 send_ldap_result( conn, op, LDAP_SUCCESS,
228 NULL, NULL, NULL, NULL );
230 cache_return_entry_w( &li->li_cache, e );
234 cache_return_entry_w( &li->li_cache, e );
248 /* check if the values we're adding already exist */
249 if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
250 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
251 #ifdef SLAPD_SCHEMA_NOT_COMPAT
252 /* not yet implemented */
254 if ( value_find( a->a_vals, mod->mod_bvalues[i],
255 a->a_syntax, 3 ) == 0 ) {
256 return( LDAP_TYPE_OR_VALUE_EXISTS );
263 if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
264 return( LDAP_CONSTRAINT_VIOLATION );
267 return( LDAP_SUCCESS );
280 /* delete the entire attribute */
281 if ( mod->mod_bvalues == NULL ) {
282 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
283 mod->mod_type, 0, 0 );
284 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
285 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
288 /* delete specific values - find the attribute first */
289 if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
290 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
291 mod->mod_type, 0, 0 );
292 return( LDAP_NO_SUCH_ATTRIBUTE );
295 /* find each value to delete */
296 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
298 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
299 #ifdef SLAPD_SCHEMA_NOT_COMPAT
300 /* not yet implemented */
302 if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
303 a->a_syntax, 3 ) != 0 ) {
309 /* found a matching value - delete it */
310 ber_bvfree( a->a_vals[j] );
311 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
312 a->a_vals[k - 1] = a->a_vals[k];
314 a->a_vals[k - 1] = NULL;
316 /* delete the entire attribute, if no values remain */
317 if ( a->a_vals[0] == NULL) {
318 Debug( LDAP_DEBUG_ARGS,
319 "removing entire attribute %s\n",
320 mod->mod_type, 0, 0 );
321 if ( attr_delete( &e->e_attrs, mod->mod_type ) ) {
322 return LDAP_NO_SUCH_ATTRIBUTE;
329 /* looked through them all w/o finding it */
331 Debug( LDAP_DEBUG_ARGS,
332 "could not find value for attr %s\n",
333 mod->mod_type, 0, 0 );
334 return( LDAP_NO_SUCH_ATTRIBUTE );
338 return( LDAP_SUCCESS );
348 (void) attr_delete( &e->e_attrs, mod->mod_type );
350 if ( mod->mod_bvalues != NULL &&
351 attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 )
353 return( LDAP_CONSTRAINT_VIOLATION );
356 return( LDAP_SUCCESS );