1 /* modify.c - ldbm backend modify routine */
11 #include "back-ldbm.h"
12 #include "proto-back-ldbm.h"
14 static int add_values(Entry *e, LDAPMod *mod, char *dn);
15 static int delete_values(Entry *e, LDAPMod *mod, char *dn);
16 static int replace_values(Entry *e, LDAPMod *mod, char *dn);
18 /* We need this function because of LDAP modrdn. If we do not
19 * add this there would be a bunch of code replication here
20 * and there and of course the likelihood of bugs increases.
21 * Juan C. Gomez (gomez@engr.sgi.com) 05/18/99
24 int ldbm_internal_modify(
36 Attribute *save_attrs;
38 if ( (err = acl_check_mods( be, conn, op, e, mods )) != LDAP_SUCCESS ) {
39 send_ldap_result( conn, op, err, NULL, NULL );
43 save_attrs = e->e_attrs;
44 e->e_attrs = attrs_dup( e->e_attrs );
46 for ( mod = mods; mod != NULL; mod = mod->mod_next ) {
47 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
49 err = add_values( e, mod, op->o_ndn );
53 err = delete_values( e, mod, op->o_ndn );
56 case LDAP_MOD_REPLACE:
57 err = replace_values( e, mod, op->o_ndn );
60 case LDAP_MOD_SOFTADD:
61 /* Avoid problems in index_add_mods()
62 * We need to add index if necessary.
64 mod->mod_op = LDAP_MOD_ADD;
65 if ( (err = add_values( e, mod, op->o_ndn ))
66 == LDAP_TYPE_OR_VALUE_EXISTS ) {
69 mod->mod_op = LDAP_MOD_SOFTADD;
75 if ( err != LDAP_SUCCESS ) {
76 attrs_free( e->e_attrs );
77 e->e_attrs = save_attrs;
78 /* unlock entry, delete from cache */
79 send_ldap_result( conn, op, err, NULL, NULL );
84 /* check for abandon */
85 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
86 if ( op->o_abandon ) {
87 attrs_free( e->e_attrs );
88 e->e_attrs = save_attrs;
89 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
92 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
94 /* check that the entry still obeys the schema */
95 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
96 attrs_free( e->e_attrs );
97 e->e_attrs = save_attrs;
98 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
99 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, NULL, NULL );
103 /* check for abandon */
104 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
105 if ( op->o_abandon ) {
106 attrs_free( e->e_attrs );
107 e->e_attrs = save_attrs;
108 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
111 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
113 /* remove old indices */
114 if( save_attrs != NULL ) {
115 for ( mod = mods; mod != NULL; mod = mod->mod_next ) {
116 if( ( mod->mod_op & ~LDAP_MOD_BVALUES )
117 == LDAP_MOD_REPLACE )
119 /* Need to remove all values from indexes */
120 a = attr_find( save_attrs, mod->mod_type );
123 (void) index_change_values( be,
132 attrs_free( save_attrs );
136 if ( index_add_mods( be, mods, e->e_id ) != 0 ) {
137 /* our indices are likely hosed */
138 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
143 /* check for abandon */
144 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
145 if ( op->o_abandon ) {
146 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
149 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
164 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
168 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
170 /* acquire and lock entry */
171 if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
172 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched,
174 if ( matched != NULL ) {
180 /* Modify the entry */
181 if ( ldbm_internal_modify( be, conn, op, dn, mods, e ) != 0 ) {
187 /* change the entry itself */
188 if ( id2entry_add( be, e ) != 0 ) {
190 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
194 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
195 cache_return_entry_w( &li->li_cache, e );
199 cache_return_entry_w( &li->li_cache, e );
213 /* check if the values we're adding already exist */
214 if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
215 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
216 if ( value_find( a->a_vals, mod->mod_bvalues[i],
217 a->a_syntax, 3 ) == 0 ) {
218 return( LDAP_TYPE_OR_VALUE_EXISTS );
224 if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
225 return( LDAP_CONSTRAINT_VIOLATION );
228 return( LDAP_SUCCESS );
241 /* delete the entire attribute */
242 if ( mod->mod_bvalues == NULL ) {
243 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
244 mod->mod_type, 0, 0 );
245 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
246 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
249 /* delete specific values - find the attribute first */
250 if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
251 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
252 mod->mod_type, 0, 0 );
253 return( LDAP_NO_SUCH_ATTRIBUTE );
256 /* find each value to delete */
257 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
259 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
260 if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
261 a->a_syntax, 3 ) != 0 ) {
266 /* found a matching value - delete it */
267 ber_bvfree( a->a_vals[j] );
268 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
269 a->a_vals[k - 1] = a->a_vals[k];
271 a->a_vals[k - 1] = NULL;
273 /* delete the entire attribute, if no values remain */
274 if ( a->a_vals[0] == NULL) {
275 Debug( LDAP_DEBUG_ARGS,
276 "removing entire attribute %s\n",
277 mod->mod_type, 0, 0 );
278 if ( attr_delete( &e->e_attrs, mod->mod_type ) ) {
279 return LDAP_NO_SUCH_ATTRIBUTE;
286 /* looked through them all w/o finding it */
288 Debug( LDAP_DEBUG_ARGS,
289 "could not find value for attr %s\n",
290 mod->mod_type, 0, 0 );
291 return( LDAP_NO_SUCH_ATTRIBUTE );
295 return( LDAP_SUCCESS );
305 (void) attr_delete( &e->e_attrs, mod->mod_type );
307 if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
308 return( LDAP_CONSTRAINT_VIOLATION );
311 return( LDAP_SUCCESS );