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 mod->sm_op = SLAP_MOD_SOFTADD;
157 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
161 if( rc != LDAP_SUCCESS ) {
163 LDAP_LOG( BACK_LDBM, INFO,
164 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
166 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
174 LDAP_LOG( BACK_LDBM, ERR,
175 "ldbm_modify_internal: invalid op %d\n", mod->sm_op, 0, 0 );
177 Debug(LDAP_DEBUG_ANY, "ldbm_modify_internal: invalid op %d\n",
182 *text = "Invalid modify operation";
184 LDAP_LOG( BACK_LDBM, INFO,
185 "ldbm_modify_internal: %d (%s)\n", rc, *text, 0 );
187 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
192 if ( rc != LDAP_SUCCESS ) {
196 /* If objectClass was modified, reset the flags */
197 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
201 /* check if modified attribute was indexed */
202 rc = index_is_indexed( op->o_bd, mod->sm_desc );
203 if ( rc == LDAP_SUCCESS ) {
204 ap = attr_find( save_attrs, mod->sm_desc );
205 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
207 ap = attr_find( e->e_attrs, mod->sm_desc );
208 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
212 /* check that the entry still obeys the schema */
213 rc = entry_schema_check( op->o_bd, e, save_attrs, text, textbuf, textlen );
215 if ( rc != LDAP_SUCCESS ) {
217 LDAP_LOG( BACK_LDBM, ERR,
218 "ldbm_modify_internal: entry failed schema check: %s\n",
221 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
228 /* check for abandon */
229 if ( op->o_abandon ) {
234 /* update the indices of the modified attributes */
236 /* start with deleting the old index entries */
237 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
238 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
239 rc = index_values( op, ap->a_desc,
241 e->e_id, SLAP_INDEX_DELETE_OP );
242 if ( rc != LDAP_SUCCESS ) {
244 LDAP_LOG( BACK_LDBM, ERR,
245 "ldbm_modify_internal: Attribute index delete failure\n",
248 Debug( LDAP_DEBUG_ANY,
249 "Attribute index delete failure",
254 ap->a_flags &= ~SLAP_ATTR_IXDEL;
258 /* add the new index entries */
259 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
260 if ( ap->a_flags & SLAP_ATTR_IXADD ) {
261 rc = index_values( op, ap->a_desc,
263 e->e_id, SLAP_INDEX_ADD_OP );
264 if ( rc != LDAP_SUCCESS ) {
266 LDAP_LOG( BACK_LDBM, ERR,
267 "ldbm_modify_internal: Attribute index add failure\n",
270 Debug( LDAP_DEBUG_ANY,
271 "Attribute index add failure",
276 ap->a_flags &= ~SLAP_ATTR_IXADD;
281 if ( rc == LDAP_SUCCESS ) {
282 attrs_free( save_attrs );
284 for ( ap = save_attrs; ap; ap = ap->a_next ) {
287 attrs_free( e->e_attrs );
288 e->e_attrs = save_attrs;
299 struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
302 int manageDSAit = get_manageDSAit( op );
303 char textbuf[SLAP_TEXT_BUFLEN];
304 size_t textlen = sizeof textbuf;
307 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_modify: enter\n", 0, 0, 0);
309 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
312 /* grab giant lock for writing */
313 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
315 /* acquire and lock entry */
316 e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
318 /* FIXME: dn2entry() should return non-glue entry */
319 if (( e == NULL ) || ( !manageDSAit && e && is_entry_glue( e ))) {
320 if ( matched != NULL ) {
321 rs->sr_matched = ch_strdup( matched->e_dn );
322 rs->sr_ref = is_entry_referral( matched )
323 ? get_entry_referrals( op, matched )
325 cache_return_entry_r( &li->li_cache, matched );
327 BerVarray deref = NULL;
328 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
330 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
332 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
333 ber_bvarray_add( &deref, &tmpbv );
336 deref = default_referral;
338 rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn,
339 LDAP_SCOPE_DEFAULT );
342 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
343 rs->sr_err = LDAP_REFERRAL;
344 send_ldap_result( op, rs );
346 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
347 free( (char *)rs->sr_matched );
350 rs->sr_matched = NULL;
354 if ( !manageDSAit && is_entry_referral( e ) )
356 /* parent is a referral, don't allow add */
357 /* parent is an alias, don't allow add */
358 rs->sr_ref = get_entry_referrals( op, e );
361 LDAP_LOG( BACK_LDBM, INFO,
362 "ldbm_back_modify: entry (%s) is referral\n", op->o_req_ndn.bv_val, 0, 0 );
364 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
368 rs->sr_err = LDAP_REFERRAL;
369 rs->sr_matched = e->e_name.bv_val;
370 send_ldap_result( op, rs );
372 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
374 rs->sr_matched = NULL;
378 /* Modify the entry */
379 rs->sr_err = ldbm_modify_internal( op, op->oq_modify.rs_modlist, e,
380 &rs->sr_text, textbuf, textlen );
382 if( rs->sr_err != LDAP_SUCCESS ) {
383 if( rs->sr_err != SLAPD_ABANDON ) {
384 send_ldap_result( op, rs );
390 /* change the entry itself */
391 if ( id2entry_add( op->o_bd, e ) != 0 ) {
392 send_ldap_error( op, rs, LDAP_OTHER,
393 "id2entry failure" );
394 rs->sr_err = LDAP_OTHER;
399 send_ldap_error( op, rs, LDAP_SUCCESS,
402 cache_return_entry_w( &li->li_cache, e );
403 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
408 cache_return_entry_w( &li->li_cache, e );
409 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);