1 /* modify.c - ldbm backend modify routine */
4 * Copyright 1998-2003 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(
27 Modifications *modlist,
34 int rc = LDAP_SUCCESS;
37 Attribute *save_attrs;
41 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_modify_internal: %s\n", e->e_name.bv_val, 0, 0 );
43 Debug(LDAP_DEBUG_TRACE, "ldbm_modify_internal: %s\n", e->e_name.bv_val, 0, 0);
47 if ( !acl_check_modlist( 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 ) {
60 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: add\n", 0, 0, 0);
62 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: add\n", 0, 0, 0);
65 rc = modify_add_values( e, mod, get_permissiveModify( op ),
66 text, textbuf, textlen );
67 if( rc != LDAP_SUCCESS ) {
69 LDAP_LOG( BACK_LDBM, INFO,
70 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
72 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
80 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: delete\n", 0,0,0);
82 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: delete\n", 0, 0, 0);
85 rc = modify_delete_values( e, mod, get_permissiveModify( op ),
86 text, textbuf, textlen );
87 assert( rc != LDAP_TYPE_OR_VALUE_EXISTS );
88 if( rc != LDAP_SUCCESS ) {
90 LDAP_LOG( BACK_LDBM, INFO,
91 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
93 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
99 case LDAP_MOD_REPLACE:
101 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: replace\n",0,0,0);
103 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: replace\n", 0, 0, 0);
106 rc = modify_replace_values( e, mod, get_permissiveModify( op ),
107 text, textbuf, textlen );
108 if( rc != LDAP_SUCCESS ) {
110 LDAP_LOG( BACK_LDBM, INFO,
111 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
113 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
119 case LDAP_MOD_INCREMENT:
121 LDAP_LOG( BACK_LDBM, DETAIL1,
122 "ldbm_modify_internal: increment\n",0,0,0);
124 Debug(LDAP_DEBUG_ARGS,
125 "ldbm_modify_internal: increment\n",0,0,0);
128 rc = modify_increment_values( e, mod, get_permissiveModify( op ),
129 text, textbuf, textlen );
130 if( rc != LDAP_SUCCESS ) {
132 LDAP_LOG( BACK_LDBM, INFO,
133 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
135 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
141 case SLAP_MOD_SOFTADD:
143 LDAP_LOG( BACK_LDBM, DETAIL1,
144 "ldbm_modify_internal: softadd\n", 0, 0, 0 );
146 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: softadd\n", 0, 0, 0);
149 /* Avoid problems in index_add_mods()
150 * We need to add index if necessary.
152 mod->sm_op = LDAP_MOD_ADD;
154 rc = modify_add_values( e, mod, get_permissiveModify( op ),
155 text, textbuf, textlen );
156 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
160 if( rc != LDAP_SUCCESS ) {
162 LDAP_LOG( BACK_LDBM, INFO,
163 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
165 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
173 LDAP_LOG( BACK_LDBM, ERR,
174 "ldbm_modify_internal: invalid op %d\n", mod->sm_op, 0, 0 );
176 Debug(LDAP_DEBUG_ANY, "ldbm_modify_internal: invalid op %d\n",
181 *text = "Invalid modify operation";
183 LDAP_LOG( BACK_LDBM, INFO,
184 "ldbm_modify_internal: %d (%s)\n", rc, *text, 0 );
186 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
191 if ( rc != LDAP_SUCCESS ) {
195 /* If objectClass was modified, reset the flags */
196 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
200 /* check if modified attribute was indexed */
201 rc = index_is_indexed( op->o_bd, mod->sm_desc );
202 if ( rc == LDAP_SUCCESS ) {
203 ap = attr_find( save_attrs, mod->sm_desc );
204 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
206 ap = attr_find( e->e_attrs, mod->sm_desc );
207 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
211 /* check that the entry still obeys the schema */
212 rc = entry_schema_check( op->o_bd, e, save_attrs, text, textbuf, textlen );
214 if ( rc != LDAP_SUCCESS ) {
216 LDAP_LOG( BACK_LDBM, ERR,
217 "ldbm_modify_internal: entry failed schema check: %s\n",
220 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
227 /* check for abandon */
228 if ( op->o_abandon ) {
233 /* update the indices of the modified attributes */
235 /* start with deleting the old index entries */
236 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
237 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
238 rc = index_values( op, ap->a_desc,
240 e->e_id, SLAP_INDEX_DELETE_OP );
241 if ( rc != LDAP_SUCCESS ) {
243 LDAP_LOG( BACK_LDBM, ERR,
244 "ldbm_modify_internal: Attribute index delete failure\n",
247 Debug( LDAP_DEBUG_ANY,
248 "Attribute index delete failure",
253 ap->a_flags &= ~SLAP_ATTR_IXDEL;
257 /* add the new index entries */
258 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
259 if ( ap->a_flags & SLAP_ATTR_IXADD ) {
260 rc = index_values( op, ap->a_desc,
262 e->e_id, SLAP_INDEX_ADD_OP );
263 if ( rc != LDAP_SUCCESS ) {
265 LDAP_LOG( BACK_LDBM, ERR,
266 "ldbm_modify_internal: Attribute index add failure\n",
269 Debug( LDAP_DEBUG_ANY,
270 "Attribute index add failure",
275 ap->a_flags &= ~SLAP_ATTR_IXADD;
280 if ( rc == LDAP_SUCCESS ) {
281 attrs_free( save_attrs );
283 for ( ap = save_attrs; ap; ap = ap->a_next ) {
286 attrs_free( e->e_attrs );
287 e->e_attrs = save_attrs;
298 struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
301 int manageDSAit = get_manageDSAit( op );
302 char textbuf[SLAP_TEXT_BUFLEN];
303 size_t textlen = sizeof textbuf;
306 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_modify: enter\n", 0, 0, 0);
308 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
311 /* grab giant lock for writing */
312 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
314 /* acquire and lock entry */
315 e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
317 /* FIXME: dn2entry() should return non-glue entry */
318 if (( e == NULL ) || ( !manageDSAit && e && is_entry_glue( e ))) {
319 if ( matched != NULL ) {
320 rs->sr_matched = ch_strdup( matched->e_dn );
321 rs->sr_ref = is_entry_referral( matched )
322 ? get_entry_referrals( op, matched )
324 cache_return_entry_r( &li->li_cache, matched );
326 BerVarray deref = op->o_bd->syncinfo ?
327 op->o_bd->syncinfo->provideruri_bv : default_referral;
328 rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
331 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
332 rs->sr_err = LDAP_REFERRAL;
333 send_ldap_result( op, rs );
335 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
336 free( (char *)rs->sr_matched );
339 rs->sr_matched = NULL;
343 if ( !manageDSAit && is_entry_referral( e ) )
345 /* parent is a referral, don't allow add */
346 /* parent is an alias, don't allow add */
347 rs->sr_ref = get_entry_referrals( op, e );
350 LDAP_LOG( BACK_LDBM, INFO,
351 "ldbm_back_modify: entry (%s) is referral\n", op->o_req_ndn.bv_val, 0, 0 );
353 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
357 rs->sr_err = LDAP_REFERRAL;
358 rs->sr_matched = e->e_name.bv_val;
359 send_ldap_result( op, rs );
361 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
363 rs->sr_matched = NULL;
367 /* Modify the entry */
368 rs->sr_err = ldbm_modify_internal( op, op->oq_modify.rs_modlist, e,
369 &rs->sr_text, textbuf, textlen );
371 if( rs->sr_err != LDAP_SUCCESS ) {
372 if( rs->sr_err != SLAPD_ABANDON ) {
373 send_ldap_result( op, rs );
379 /* change the entry itself */
380 if ( id2entry_add( op->o_bd, e ) != 0 ) {
381 send_ldap_error( op, rs, LDAP_OTHER,
382 "id2entry failure" );
383 rs->sr_err = LDAP_OTHER;
388 send_ldap_error( op, rs, LDAP_SUCCESS,
391 cache_return_entry_w( &li->li_cache, e );
392 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
397 cache_return_entry_w( &li->li_cache, e );
398 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);