1 /* modify.c - ldbm backend modify routine */
4 * Copyright 1998-2000 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 Debug(LDAP_DEBUG_TRACE, "ldbm_modify_internal:\n", 0, 0, 0);
47 if ( !acl_check_modlist( be, conn, op, e, modlist )) {
48 return LDAP_INSUFFICIENT_ACCESS;
51 save_attrs = e->e_attrs;
52 e->e_attrs = attrs_dup( e->e_attrs );
54 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
57 switch ( mod->sm_op ) {
59 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: add\n", 0, 0, 0);
60 err = add_values( e, mod, op->o_ndn );
62 if( err != LDAP_SUCCESS ) {
63 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
65 *text = "modify: add values failed";
70 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: delete\n", 0, 0, 0);
71 err = delete_values( e, mod, op->o_ndn );
72 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
73 if( err != LDAP_SUCCESS ) {
74 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
76 *text = "modify: delete values failed";
80 case LDAP_MOD_REPLACE:
81 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: replace\n", 0, 0, 0);
82 err = replace_values( e, mod, op->o_ndn );
83 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
84 if( err != LDAP_SUCCESS ) {
85 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
87 *text = "modify: replace values failed";
91 case SLAP_MOD_SOFTADD:
92 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: softadd\n", 0, 0, 0);
93 /* Avoid problems in index_add_mods()
94 * We need to add index if necessary.
96 mod->sm_op = LDAP_MOD_ADD;
97 err = add_values( e, mod, op->o_ndn );
99 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
103 if( err != LDAP_SUCCESS ) {
104 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
106 *text = "modify: (soft)add values failed";
111 Debug(LDAP_DEBUG_ANY, "ldbm_modify_internal: invalid op %d\n",
113 *text = "Invalid modify operation";
115 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
119 if ( err != LDAP_SUCCESS ) {
120 attrs_free( e->e_attrs );
121 e->e_attrs = save_attrs;
122 /* unlock entry, delete from cache */
127 /* check for abandon */
128 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
129 if ( op->o_abandon ) {
130 attrs_free( e->e_attrs );
131 e->e_attrs = save_attrs;
132 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
133 return SLAPD_ABANDON;
135 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
137 /* check that the entry still obeys the schema */
138 rc = entry_schema_check( e, save_attrs, text );
139 if ( rc != LDAP_SUCCESS ) {
140 attrs_free( e->e_attrs );
141 e->e_attrs = save_attrs;
142 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
147 /* check for abandon */
148 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
149 if ( op->o_abandon ) {
150 attrs_free( e->e_attrs );
151 e->e_attrs = save_attrs;
152 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
153 return SLAPD_ABANDON;
155 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
157 /* delete indices for old attributes */
158 index_entry_del( be, e, save_attrs);
160 /* add indices for new attributes */
161 index_entry_add( be, e, e->e_attrs);
163 attrs_free( save_attrs );
176 Modifications *modlist
180 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
183 int manageDSAit = get_manageDSAit( op );
184 const char *text = NULL;
186 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
188 /* acquire and lock entry */
189 if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
190 char* matched_dn = NULL;
191 struct berval **refs = NULL;
193 if ( matched != NULL ) {
194 matched_dn = ch_strdup( matched->e_dn );
195 refs = is_entry_referral( matched )
196 ? get_entry_referrals( be, conn, op, matched )
198 cache_return_entry_r( &li->li_cache, matched );
200 refs = default_referral;
203 send_ldap_result( conn, op, LDAP_REFERRAL,
204 matched_dn, NULL, refs, NULL );
206 if ( matched != NULL ) {
207 ber_bvecfree( refs );
214 if ( !manageDSAit && is_entry_referral( e ) ) {
215 /* parent is a referral, don't allow add */
216 /* parent is an alias, don't allow add */
217 struct berval **refs = get_entry_referrals( be,
220 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
223 send_ldap_result( conn, op, LDAP_REFERRAL,
224 e->e_dn, NULL, refs, NULL );
226 ber_bvecfree( refs );
231 /* Modify the entry */
232 rc = ldbm_modify_internal( be, conn, op, ndn, modlist, e, &text );
234 if( rc != LDAP_SUCCESS ) {
235 if( rc != SLAPD_ABANDON ) {
236 send_ldap_result( conn, op, rc,
237 NULL, text, NULL, NULL );
243 /* change the entry itself */
244 if ( id2entry_add( be, e ) != 0 ) {
245 send_ldap_result( conn, op, LDAP_OTHER,
246 NULL, "id2entry failure", NULL, NULL );
250 send_ldap_result( conn, op, LDAP_SUCCESS,
251 NULL, NULL, NULL, NULL );
253 cache_return_entry_w( &li->li_cache, e );
257 cache_return_entry_w( &li->li_cache, e );
271 /* char *desc = mod->sm_desc->ad_cname->bv_val; */
272 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
275 return LDAP_INAPPROPRIATE_MATCHING;
279 a = attr_find( e->e_attrs, mod->sm_desc );
281 /* check if the values we're adding already exist */
283 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
286 const char *text = NULL;
287 struct berval *asserted;
289 rc = value_normalize( mod->sm_desc,
295 if( rc != LDAP_SUCCESS ) return rc;
297 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
299 int rc = value_match( &match, mod->sm_desc, mr,
300 a->a_vals[j], asserted, &text );
302 if( rc == LDAP_SUCCESS && match == 0 ) {
303 ber_bvfree( asserted );
304 return LDAP_TYPE_OR_VALUE_EXISTS;
308 ber_bvfree( asserted );
313 if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
314 /* this should return result return of attr_merge */
330 char *desc = mod->sm_desc->ad_cname->bv_val;
331 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
333 if( mr == NULL || !mr->smr_match ) {
334 return LDAP_INAPPROPRIATE_MATCHING;
337 /* delete the entire attribute */
338 if ( mod->sm_bvalues == NULL ) {
339 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
341 return( attr_delete( &e->e_attrs, mod->sm_desc ) ?
342 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
345 /* delete specific values - find the attribute first */
346 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
347 Debug( LDAP_DEBUG_ARGS, "ldap_modify_delete: "
348 "could not find attribute %s\n",
350 return( LDAP_NO_SUCH_ATTRIBUTE );
353 /* find each value to delete */
354 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
356 const char *text = NULL;
358 struct berval *asserted;
360 rc = value_normalize( mod->sm_desc,
366 if( rc != LDAP_SUCCESS ) return rc;
369 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
371 int rc = value_match( &match, mod->sm_desc, mr,
372 a->a_vals[j], asserted, &text );
374 if( rc == LDAP_SUCCESS && match != 0 )
380 /* found a matching value - delete it */
381 ber_bvfree( a->a_vals[j] );
382 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
383 a->a_vals[k - 1] = a->a_vals[k];
385 a->a_vals[k - 1] = NULL;
387 /* delete the entire attribute, if no values remain */
388 if ( a->a_vals[0] == NULL) {
389 Debug( LDAP_DEBUG_ARGS,
390 "removing entire attribute %s\n",
392 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
393 ber_bvfree( asserted );
394 return LDAP_NO_SUCH_ATTRIBUTE;
401 ber_bvfree( asserted );
403 /* looked through them all w/o finding it */
405 Debug( LDAP_DEBUG_ARGS,
406 "ldbm_modify_delete: could not find value for attr %s\n",
408 return LDAP_NO_SUCH_ATTRIBUTE;
422 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
424 if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
428 if ( mod->sm_bvalues != NULL &&
429 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )