1 /* modify.c - ldbm backend modify routine */
12 #include "back-ldbm.h"
13 #include "proto-back-ldbm.h"
15 /* We need this function because of LDAP modrdn. If we do not
16 * add this there would be a bunch of code replication here
17 * and there and of course the likelihood of bugs increases.
18 * Juan C. Gomez (gomez@engr.sgi.com) 05/18/99
21 int ldbm_modify_internal(
34 Attribute *save_attrs;
36 if ( (err = acl_check_modlist( be, conn, op, e, modlist ))
39 send_ldap_result( conn, op, err,
40 NULL, NULL, NULL, NULL );
44 save_attrs = e->e_attrs;
45 e->e_attrs = attrs_dup( e->e_attrs );
47 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
50 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
52 err = add_values( e, mod, op->o_ndn );
56 err = delete_values( e, mod, op->o_ndn );
59 case LDAP_MOD_REPLACE:
60 err = replace_values( e, mod, op->o_ndn );
63 case LDAP_MOD_SOFTADD:
64 /* Avoid problems in index_add_mods()
65 * We need to add index if necessary.
67 mod->mod_op = LDAP_MOD_ADD;
68 if ( (err = add_values( e, mod, op->o_ndn ))
69 == LDAP_TYPE_OR_VALUE_EXISTS ) {
72 mod->mod_op = LDAP_MOD_SOFTADD;
78 if ( err != LDAP_SUCCESS ) {
79 attrs_free( e->e_attrs );
80 e->e_attrs = save_attrs;
81 /* unlock entry, delete from cache */
82 send_ldap_result( conn, op, err,
83 NULL, NULL, NULL, NULL );
88 /* check for abandon */
89 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
90 if ( op->o_abandon ) {
91 attrs_free( e->e_attrs );
92 e->e_attrs = save_attrs;
93 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
96 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
98 /* check that the entry still obeys the schema */
99 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
100 attrs_free( e->e_attrs );
101 e->e_attrs = save_attrs;
102 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
103 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION,
104 NULL, NULL, NULL, NULL );
108 /* check for abandon */
109 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
110 if ( op->o_abandon ) {
111 attrs_free( e->e_attrs );
112 e->e_attrs = save_attrs;
113 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
116 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
119 if ( index_add_mods( be, modlist, e->e_id ) != 0 ) {
120 attrs_free( e->e_attrs );
121 e->e_attrs = save_attrs;
122 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
123 NULL, NULL, NULL, NULL );
127 if( save_attrs != NULL ) {
128 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
130 if( ( mod->mod_op & ~LDAP_MOD_BVALUES )
131 == LDAP_MOD_REPLACE )
133 /* Need to remove all values from indexes */
134 a = attr_find( save_attrs, mod->mod_type );
137 (void) index_change_values( be,
145 attrs_free( save_attrs );
148 /* check for abandon */
149 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
150 if ( op->o_abandon ) {
151 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
154 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
169 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
172 int manageDSAit = get_manageDSAit( op );
174 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
176 /* acquire and lock entry */
177 if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
178 char* matched_dn = NULL;
179 struct berval **refs = NULL;
181 if ( matched != NULL ) {
182 matched_dn = ch_strdup( matched->e_dn );
183 refs = is_entry_referral( matched )
184 ? get_entry_referrals( be, conn, op, matched )
186 cache_return_entry_r( &li->li_cache, matched );
188 refs = default_referral;
191 send_ldap_result( conn, op, LDAP_REFERRAL,
192 matched_dn, NULL, refs, NULL );
194 if ( matched != NULL ) {
195 ber_bvecfree( refs );
202 if ( !manageDSAit && is_entry_referral( e ) ) {
203 /* parent is a referral, don't allow add */
204 /* parent is an alias, don't allow add */
205 struct berval **refs = get_entry_referrals( be,
208 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
211 send_ldap_result( conn, op, LDAP_REFERRAL,
212 e->e_dn, NULL, refs, NULL );
214 ber_bvecfree( refs );
219 /* Modify the entry */
220 if ( ldbm_modify_internal( be, conn, op, dn, modlist, e ) != 0 ) {
224 /* change the entry itself */
225 if ( id2entry_add( be, e ) != 0 ) {
226 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
227 NULL, NULL, NULL, NULL );
231 send_ldap_result( conn, op, LDAP_SUCCESS,
232 NULL, NULL, NULL, NULL );
233 cache_return_entry_w( &li->li_cache, e );
237 cache_return_entry_w( &li->li_cache, e );
251 /* check if the values we're adding already exist */
252 if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
253 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
254 if ( value_find( a->a_vals, mod->mod_bvalues[i],
255 a->a_syntax, 3 ) == 0 ) {
256 return( LDAP_TYPE_OR_VALUE_EXISTS );
262 if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
263 return( LDAP_CONSTRAINT_VIOLATION );
266 return( LDAP_SUCCESS );
279 /* delete the entire attribute */
280 if ( mod->mod_bvalues == NULL ) {
281 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
282 mod->mod_type, 0, 0 );
283 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
284 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
287 /* delete specific values - find the attribute first */
288 if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
289 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
290 mod->mod_type, 0, 0 );
291 return( LDAP_NO_SUCH_ATTRIBUTE );
294 /* find each value to delete */
295 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
297 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
298 if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
299 a->a_syntax, 3 ) != 0 ) {
304 /* found a matching value - delete it */
305 ber_bvfree( a->a_vals[j] );
306 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
307 a->a_vals[k - 1] = a->a_vals[k];
309 a->a_vals[k - 1] = NULL;
311 /* delete the entire attribute, if no values remain */
312 if ( a->a_vals[0] == NULL) {
313 Debug( LDAP_DEBUG_ARGS,
314 "removing entire attribute %s\n",
315 mod->mod_type, 0, 0 );
316 if ( attr_delete( &e->e_attrs, mod->mod_type ) ) {
317 return LDAP_NO_SUCH_ATTRIBUTE;
324 /* looked through them all w/o finding it */
326 Debug( LDAP_DEBUG_ARGS,
327 "could not find value for attr %s\n",
328 mod->mod_type, 0, 0 );
329 return( LDAP_NO_SUCH_ATTRIBUTE );
333 return( LDAP_SUCCESS );
343 (void) attr_delete( &e->e_attrs, mod->mod_type );
345 if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
346 return( LDAP_CONSTRAINT_VIOLATION );
349 return( LDAP_SUCCESS );