1 /* modify.c - ldbm backend modify routine */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
12 #include <ac/socket.h>
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
19 /* We need this function because of LDAP modrdn. If we do not
20 * add this there would be a bunch of code replication here
21 * and there and of course the likelihood of bugs increases.
22 * Juan C. Gomez (gomez@engr.sgi.com) 05/18/99
25 int ldbm_modify_internal(
38 Attribute *save_attrs;
40 if ( (err = acl_check_modlist( be, conn, op, e, modlist ))
43 send_ldap_result( conn, op, err,
44 NULL, NULL, NULL, NULL );
48 save_attrs = e->e_attrs;
49 e->e_attrs = attrs_dup( e->e_attrs );
51 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
54 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
56 err = add_values( e, mod, op->o_ndn );
60 err = delete_values( e, mod, op->o_ndn );
63 case LDAP_MOD_REPLACE:
64 err = replace_values( e, mod, op->o_ndn );
67 case LDAP_MOD_SOFTADD:
68 /* Avoid problems in index_add_mods()
69 * We need to add index if necessary.
71 mod->mod_op = LDAP_MOD_ADD;
72 if ( (err = add_values( e, mod, op->o_ndn ))
73 == LDAP_TYPE_OR_VALUE_EXISTS ) {
76 mod->mod_op = LDAP_MOD_SOFTADD;
82 if ( err != LDAP_SUCCESS ) {
83 attrs_free( e->e_attrs );
84 e->e_attrs = save_attrs;
85 /* unlock entry, delete from cache */
86 send_ldap_result( conn, op, err,
87 NULL, NULL, NULL, NULL );
92 /* check for abandon */
93 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
94 if ( op->o_abandon ) {
95 attrs_free( e->e_attrs );
96 e->e_attrs = save_attrs;
97 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
100 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
102 /* check that the entry still obeys the schema */
103 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
104 attrs_free( e->e_attrs );
105 e->e_attrs = save_attrs;
106 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
107 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION,
108 NULL, NULL, NULL, NULL );
112 /* check for abandon */
113 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
114 if ( op->o_abandon ) {
115 attrs_free( e->e_attrs );
116 e->e_attrs = save_attrs;
117 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
120 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
122 /* remove old indices */
123 if( save_attrs != NULL ) {
124 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
126 if( ( mod->mod_op & ~LDAP_MOD_BVALUES )
127 == LDAP_MOD_REPLACE )
129 /* Need to remove all values from indexes */
130 a = attr_find( save_attrs, mod->mod_type );
133 (void) index_change_values( be,
137 SLAP_INDEX_DELETE_OP);
141 attrs_free( save_attrs );
145 if ( index_add_mods( be, modlist, e->e_id ) != 0 ) {
146 /* our indices are likely hosed */
147 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
148 NULL, NULL, NULL, NULL );
152 /* check for abandon */
153 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
154 if ( op->o_abandon ) {
155 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
158 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
173 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
176 int manageDSAit = get_manageDSAit( op );
178 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
180 /* acquire and lock entry */
181 if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
182 char* matched_dn = NULL;
183 struct berval **refs = NULL;
185 if ( matched != NULL ) {
186 matched_dn = ch_strdup( matched->e_dn );
187 refs = is_entry_referral( matched )
188 ? get_entry_referrals( be, conn, op, matched )
190 cache_return_entry_r( &li->li_cache, matched );
192 refs = default_referral;
195 send_ldap_result( conn, op, LDAP_REFERRAL,
196 matched_dn, NULL, refs, NULL );
198 if ( matched != NULL ) {
199 ber_bvecfree( refs );
206 if ( !manageDSAit && is_entry_referral( e ) ) {
207 /* parent is a referral, don't allow add */
208 /* parent is an alias, don't allow add */
209 struct berval **refs = get_entry_referrals( be,
212 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
215 send_ldap_result( conn, op, LDAP_REFERRAL,
216 e->e_dn, NULL, refs, NULL );
218 ber_bvecfree( refs );
223 /* Modify the entry */
224 if ( ldbm_modify_internal( be, conn, op, dn, modlist, e ) != 0 ) {
228 /* change the entry itself */
229 if ( id2entry_add( be, e ) != 0 ) {
230 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
231 NULL, NULL, NULL, NULL );
235 send_ldap_result( conn, op, LDAP_SUCCESS,
236 NULL, NULL, NULL, NULL );
237 cache_return_entry_w( &li->li_cache, e );
241 cache_return_entry_w( &li->li_cache, e );
255 /* check if the values we're adding already exist */
256 if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
257 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
258 if ( value_find( a->a_vals, mod->mod_bvalues[i],
259 a->a_syntax, 3 ) == 0 ) {
260 return( LDAP_TYPE_OR_VALUE_EXISTS );
266 if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
267 return( LDAP_CONSTRAINT_VIOLATION );
270 return( LDAP_SUCCESS );
283 /* delete the entire attribute */
284 if ( mod->mod_bvalues == NULL ) {
285 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
286 mod->mod_type, 0, 0 );
287 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
288 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
291 /* delete specific values - find the attribute first */
292 if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
293 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
294 mod->mod_type, 0, 0 );
295 return( LDAP_NO_SUCH_ATTRIBUTE );
298 /* find each value to delete */
299 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
301 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
302 if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
303 a->a_syntax, 3 ) != 0 ) {
308 /* found a matching value - delete it */
309 ber_bvfree( a->a_vals[j] );
310 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
311 a->a_vals[k - 1] = a->a_vals[k];
313 a->a_vals[k - 1] = NULL;
315 /* delete the entire attribute, if no values remain */
316 if ( a->a_vals[0] == NULL) {
317 Debug( LDAP_DEBUG_ARGS,
318 "removing entire attribute %s\n",
319 mod->mod_type, 0, 0 );
320 if ( attr_delete( &e->e_attrs, mod->mod_type ) ) {
321 return LDAP_NO_SUCH_ATTRIBUTE;
328 /* looked through them all w/o finding it */
330 Debug( LDAP_DEBUG_ARGS,
331 "could not find value for attr %s\n",
332 mod->mod_type, 0, 0 );
333 return( LDAP_NO_SUCH_ATTRIBUTE );
337 return( LDAP_SUCCESS );
347 (void) attr_delete( &e->e_attrs, mod->mod_type );
349 if ( mod->mod_bvalues != NULL &&
350 attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 )
352 return( LDAP_CONSTRAINT_VIOLATION );
355 return( LDAP_SUCCESS );