1 /* modify.c - ldbm backend modify routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
21 #include <ac/string.h>
22 #include <ac/socket.h>
26 #include "back-ldbm.h"
27 #include "proto-back-ldbm.h"
29 /* We need this function because of LDAP modrdn. If we do not
30 * add this there would be a bunch of code replication here
31 * and there and of course the likelihood of bugs increases.
32 * Juan C. Gomez (gomez@engr.sgi.com) 05/18/99
34 int ldbm_modify_internal(
36 Modifications *modlist,
43 int rc = LDAP_SUCCESS;
46 Attribute *save_attrs;
50 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_modify_internal: %s\n", e->e_name.bv_val, 0, 0 );
52 Debug(LDAP_DEBUG_TRACE, "ldbm_modify_internal: %s\n", e->e_name.bv_val, 0, 0);
56 if ( !acl_check_modlist( op, e, modlist )) {
57 return LDAP_INSUFFICIENT_ACCESS;
60 save_attrs = e->e_attrs;
61 e->e_attrs = attrs_dup( e->e_attrs );
63 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
66 switch ( mod->sm_op ) {
69 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: add\n", 0, 0, 0);
71 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: add\n", 0, 0, 0);
74 rc = modify_add_values( e, mod, get_permissiveModify( op ),
75 text, textbuf, textlen );
76 if( rc != LDAP_SUCCESS ) {
78 LDAP_LOG( BACK_LDBM, INFO,
79 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
81 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
89 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: delete\n", 0,0,0);
91 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: delete\n", 0, 0, 0);
94 rc = modify_delete_values( e, mod, get_permissiveModify( op ),
95 text, textbuf, textlen );
96 assert( rc != LDAP_TYPE_OR_VALUE_EXISTS );
97 if( rc != LDAP_SUCCESS ) {
99 LDAP_LOG( BACK_LDBM, INFO,
100 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
102 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
108 case LDAP_MOD_REPLACE:
110 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: replace\n",0,0,0);
112 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: replace\n", 0, 0, 0);
115 rc = modify_replace_values( e, mod, get_permissiveModify( op ),
116 text, textbuf, textlen );
117 if( rc != LDAP_SUCCESS ) {
119 LDAP_LOG( BACK_LDBM, INFO,
120 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
122 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
128 case LDAP_MOD_INCREMENT:
130 LDAP_LOG( BACK_LDBM, DETAIL1,
131 "ldbm_modify_internal: increment\n",0,0,0);
133 Debug(LDAP_DEBUG_ARGS,
134 "ldbm_modify_internal: increment\n",0,0,0);
137 rc = modify_increment_values( e, mod, get_permissiveModify( op ),
138 text, textbuf, textlen );
139 if( rc != LDAP_SUCCESS ) {
141 LDAP_LOG( BACK_LDBM, INFO,
142 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
144 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
150 case SLAP_MOD_SOFTADD:
152 LDAP_LOG( BACK_LDBM, DETAIL1,
153 "ldbm_modify_internal: softadd\n", 0, 0, 0 );
155 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: softadd\n", 0, 0, 0);
158 /* Avoid problems in index_add_mods()
159 * We need to add index if necessary.
161 mod->sm_op = LDAP_MOD_ADD;
163 rc = modify_add_values( e, mod, get_permissiveModify( op ),
164 text, textbuf, textlen );
165 mod->sm_op = SLAP_MOD_SOFTADD;
166 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
170 if( rc != LDAP_SUCCESS ) {
172 LDAP_LOG( BACK_LDBM, INFO,
173 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
175 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
183 LDAP_LOG( BACK_LDBM, ERR,
184 "ldbm_modify_internal: invalid op %d\n", mod->sm_op, 0, 0 );
186 Debug(LDAP_DEBUG_ANY, "ldbm_modify_internal: invalid op %d\n",
191 *text = "Invalid modify operation";
193 LDAP_LOG( BACK_LDBM, INFO,
194 "ldbm_modify_internal: %d (%s)\n", rc, *text, 0 );
196 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
201 if ( rc != LDAP_SUCCESS ) {
205 /* If objectClass was modified, reset the flags */
206 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
210 /* check if modified attribute was indexed */
211 rc = index_is_indexed( op->o_bd, mod->sm_desc );
212 if ( rc == LDAP_SUCCESS ) {
213 ap = attr_find( save_attrs, mod->sm_desc );
214 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
216 ap = attr_find( e->e_attrs, mod->sm_desc );
217 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
221 /* check that the entry still obeys the schema */
222 rc = entry_schema_check( op->o_bd, e, save_attrs, text, textbuf, textlen );
224 if ( rc != LDAP_SUCCESS ) {
226 LDAP_LOG( BACK_LDBM, ERR,
227 "ldbm_modify_internal: entry failed schema check: %s\n",
230 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
237 /* check for abandon */
238 if ( op->o_abandon ) {
243 /* update the indices of the modified attributes */
245 /* start with deleting the old index entries */
246 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
247 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
248 rc = index_values( op, ap->a_desc,
250 e->e_id, SLAP_INDEX_DELETE_OP );
251 if ( rc != LDAP_SUCCESS ) {
253 LDAP_LOG( BACK_LDBM, ERR,
254 "ldbm_modify_internal: Attribute index delete failure\n",
257 Debug( LDAP_DEBUG_ANY,
258 "Attribute index delete failure",
263 ap->a_flags &= ~SLAP_ATTR_IXDEL;
267 /* add the new index entries */
268 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
269 if ( ap->a_flags & SLAP_ATTR_IXADD ) {
270 rc = index_values( op, ap->a_desc,
272 e->e_id, SLAP_INDEX_ADD_OP );
273 if ( rc != LDAP_SUCCESS ) {
275 LDAP_LOG( BACK_LDBM, ERR,
276 "ldbm_modify_internal: Attribute index add failure\n",
279 Debug( LDAP_DEBUG_ANY,
280 "Attribute index add failure",
285 ap->a_flags &= ~SLAP_ATTR_IXADD;
290 if ( rc == LDAP_SUCCESS ) {
291 attrs_free( save_attrs );
293 for ( ap = save_attrs; ap; ap = ap->a_next ) {
296 attrs_free( e->e_attrs );
297 e->e_attrs = save_attrs;
308 struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
311 int manageDSAit = get_manageDSAit( op );
312 char textbuf[SLAP_TEXT_BUFLEN];
313 size_t textlen = sizeof textbuf;
316 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_modify: enter\n", 0, 0, 0);
318 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
321 /* grab giant lock for writing */
322 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
324 /* acquire and lock entry */
325 e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
327 /* FIXME: dn2entry() should return non-glue entry */
328 if (( e == NULL ) || ( !manageDSAit && e && is_entry_glue( e ))) {
329 if ( matched != NULL ) {
330 rs->sr_matched = ch_strdup( matched->e_dn );
331 rs->sr_ref = is_entry_referral( matched )
332 ? get_entry_referrals( op, matched )
334 cache_return_entry_r( &li->li_cache, matched );
336 BerVarray deref = NULL;
337 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
339 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
341 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
342 ber_bvarray_add( &deref, &tmpbv );
345 deref = default_referral;
347 rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn,
348 LDAP_SCOPE_DEFAULT );
351 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
352 rs->sr_err = LDAP_REFERRAL;
353 send_ldap_result( op, rs );
355 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
356 free( (char *)rs->sr_matched );
359 rs->sr_matched = NULL;
363 if ( !manageDSAit && is_entry_referral( e ) )
365 /* parent is a referral, don't allow add */
366 /* parent is an alias, don't allow add */
367 rs->sr_ref = get_entry_referrals( op, e );
370 LDAP_LOG( BACK_LDBM, INFO,
371 "ldbm_back_modify: entry (%s) is referral\n", op->o_req_ndn.bv_val, 0, 0 );
373 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
377 rs->sr_err = LDAP_REFERRAL;
378 rs->sr_matched = e->e_name.bv_val;
379 send_ldap_result( op, rs );
381 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
383 rs->sr_matched = NULL;
387 /* Modify the entry */
388 rs->sr_err = ldbm_modify_internal( op, op->oq_modify.rs_modlist, e,
389 &rs->sr_text, textbuf, textlen );
391 if( rs->sr_err != LDAP_SUCCESS ) {
392 if( rs->sr_err != SLAPD_ABANDON ) {
393 send_ldap_result( op, rs );
399 /* change the entry itself */
400 if ( id2entry_add( op->o_bd, e ) != 0 ) {
401 send_ldap_error( op, rs, LDAP_OTHER,
402 "id2entry failure" );
403 rs->sr_err = LDAP_OTHER;
408 send_ldap_error( op, rs, LDAP_SUCCESS,
411 cache_return_entry_w( &li->li_cache, e );
412 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
417 cache_return_entry_w( &li->li_cache, e );
418 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);