1 /* modify.c - ldbm backend modify routine */
4 * Copyright 1998-2002 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
25 int ldbm_modify_internal(
30 Modifications *modlist,
37 int rc = LDAP_SUCCESS;
40 Attribute *save_attrs;
44 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
45 "ldbm_modify_internal: %s\n", dn ));
47 Debug(LDAP_DEBUG_TRACE, "ldbm_modify_internal: %s\n", dn, 0, 0);
51 if ( !acl_check_modlist( be, conn, op, e, modlist )) {
52 return LDAP_INSUFFICIENT_ACCESS;
55 save_attrs = e->e_attrs;
56 e->e_attrs = attrs_dup( e->e_attrs );
58 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
61 switch ( mod->sm_op ) {
64 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
65 "ldbm_modify_internal: add\n" ));
67 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: add\n", 0, 0, 0);
70 rc = modify_add_values( e, mod, text, textbuf, textlen );
71 if( rc != LDAP_SUCCESS ) {
73 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
74 "ldbm_modify_internal: failed %d (%s)\n",
77 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
85 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
86 "ldbm_modify_internal: delete\n" ));
88 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: delete\n", 0, 0, 0);
91 rc = modify_delete_values( e, mod, text, textbuf, textlen );
92 assert( rc != LDAP_TYPE_OR_VALUE_EXISTS );
93 if( rc != LDAP_SUCCESS ) {
95 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
96 "ldbm_modify_internal: failed %d (%s)\n", rc, *text ));
98 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
104 case LDAP_MOD_REPLACE:
106 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
107 "ldbm_modify_internal: replace\n" ));
109 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: replace\n", 0, 0, 0);
112 rc = modify_replace_values( e, mod, text, textbuf, textlen );
113 assert( rc != LDAP_TYPE_OR_VALUE_EXISTS );
114 if( rc != LDAP_SUCCESS ) {
116 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
117 "ldbm_modify_internal: failed %d (%s)\n", rc, *text ));
119 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
125 case SLAP_MOD_SOFTADD:
127 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
128 "ldbm_modify_internal: softadd\n" ));
130 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: softadd\n", 0, 0, 0);
133 /* Avoid problems in index_add_mods()
134 * We need to add index if necessary.
136 mod->sm_op = LDAP_MOD_ADD;
138 rc = modify_add_values( e, mod, text, textbuf, textlen );
139 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
143 if( rc != LDAP_SUCCESS ) {
145 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
146 "ldbm_modify_internal: failed %d (%s)\n", rc, *text ));
148 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
156 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
157 "ldbm_modify_internal: invalid op %d\n", mod->sm_op ));
159 Debug(LDAP_DEBUG_ANY, "ldbm_modify_internal: invalid op %d\n",
164 *text = "Invalid modify operation";
166 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
167 "ldbm_modify_internal: %d (%s)\n", rc, *text ));
169 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
174 if ( rc != LDAP_SUCCESS ) {
178 /* If objectClass was modified, reset the flags */
179 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
183 /* check if modified attribute was indexed */
184 rc = index_is_indexed( be, mod->sm_desc );
185 if ( rc == LDAP_SUCCESS ) {
186 ap = attr_find( save_attrs, mod->sm_desc );
187 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
189 ap = attr_find( e->e_attrs, mod->sm_desc );
190 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
194 /* check that the entry still obeys the schema */
195 rc = entry_schema_check( be, e, save_attrs, text, textbuf, textlen );
196 if ( rc != LDAP_SUCCESS ) {
198 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
199 "ldbm_modify_internal: entry failed schema check: %s\n",
202 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
209 /* check for abandon */
210 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
212 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
218 /* update the indices of the modified attributes */
220 /* start with deleting the old index entries */
221 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
222 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
223 rc = index_values( be, ap->a_desc, ap->a_vals, e->e_id,
224 SLAP_INDEX_DELETE_OP );
225 if ( rc != LDAP_SUCCESS ) {
227 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
228 "ldbm_modify_internal: Attribute index delete failure\n" ));
230 Debug( LDAP_DEBUG_ANY,
231 "Attribute index delete failure",
236 ap->a_flags &= ~SLAP_ATTR_IXDEL;
240 /* add the new index entries */
241 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
242 if ( ap->a_flags & SLAP_ATTR_IXADD ) {
243 rc = index_values( be, ap->a_desc, ap->a_vals, e->e_id,
245 if ( rc != LDAP_SUCCESS ) {
247 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
248 "ldbm_modify_internal: Attribute index add failure\n" ));
250 Debug( LDAP_DEBUG_ANY,
251 "Attribute index add failure",
256 ap->a_flags &= ~SLAP_ATTR_IXADD;
261 if ( rc == LDAP_SUCCESS ) {
262 attrs_free( save_attrs );
264 for ( ap = save_attrs; ap; ap = ap->a_next ) {
267 attrs_free( e->e_attrs );
268 e->e_attrs = save_attrs;
281 Modifications *modlist
285 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
288 int manageDSAit = get_manageDSAit( op );
289 const char *text = NULL;
290 char textbuf[SLAP_TEXT_BUFLEN];
291 size_t textlen = sizeof textbuf;
294 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
295 "ldbm_back_modify: enter\n" ));
297 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
301 /* acquire and lock entry */
302 if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
303 char* matched_dn = NULL;
306 if ( matched != NULL ) {
307 matched_dn = ch_strdup( matched->e_dn );
308 refs = is_entry_referral( matched )
309 ? get_entry_referrals( be, conn, op, matched )
311 cache_return_entry_r( &li->li_cache, matched );
313 refs = referral_rewrite( default_referral,
314 NULL, dn, LDAP_SCOPE_DEFAULT );
317 send_ldap_result( conn, op, LDAP_REFERRAL,
318 matched_dn, NULL, refs, NULL );
320 if ( refs ) ber_bvarray_free( refs );
326 if ( !manageDSAit && is_entry_referral( e ) ) {
327 /* parent is a referral, don't allow add */
328 /* parent is an alias, don't allow add */
329 BerVarray refs = get_entry_referrals( be,
333 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
334 "ldbm_back_modify: entry (%s) is referral\n", ndn->bv_val ));
336 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
341 send_ldap_result( conn, op, LDAP_REFERRAL,
342 e->e_dn, NULL, refs, NULL );
344 if ( refs ) ber_bvarray_free( refs );
349 /* Modify the entry */
350 rc = ldbm_modify_internal( be, conn, op, ndn->bv_val, modlist, e,
351 &text, textbuf, textlen );
353 if( rc != LDAP_SUCCESS ) {
354 if( rc != SLAPD_ABANDON ) {
355 send_ldap_result( conn, op, rc,
356 NULL, text, NULL, NULL );
362 /* change the entry itself */
363 if ( id2entry_add( be, e ) != 0 ) {
364 send_ldap_result( conn, op, LDAP_OTHER,
365 NULL, "id2entry failure", NULL, NULL );
369 send_ldap_result( conn, op, LDAP_SUCCESS,
370 NULL, NULL, NULL, NULL );
372 cache_return_entry_w( &li->li_cache, e );
376 cache_return_entry_w( &li->li_cache, e );