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,
42 int rc = LDAP_SUCCESS;
45 Attribute *save_attrs;
49 LDAP_LOG( BACK_LDBM, ENTRY,
50 "ldbm_modify_internal: %s\n",
52 get_permissiveModify(op) ? " (permissive)" : "",
55 Debug(LDAP_DEBUG_TRACE,
56 "ldbm_modify_internal: %s\n",
58 get_permissiveModify(op) ? " (permissive)" : "",
62 if ( !acl_check_modlist( op, e, modlist )) {
63 return LDAP_INSUFFICIENT_ACCESS;
66 save_attrs = e->e_attrs;
67 e->e_attrs = attrs_dup( e->e_attrs );
69 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
72 switch ( mod->sm_op ) {
75 LDAP_LOG( BACK_LDBM, DETAIL1,
76 "ldbm_modify_internal: add\n", 0, 0, 0);
78 Debug(LDAP_DEBUG_ARGS,
79 "ldbm_modify_internal: add\n", 0, 0, 0);
82 rc = modify_add_values( e, mod, get_permissiveModify( op ),
83 text, textbuf, textlen );
84 if( rc != LDAP_SUCCESS ) {
86 LDAP_LOG( BACK_LDBM, INFO,
87 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
89 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
97 LDAP_LOG( BACK_LDBM, DETAIL1,
98 "ldbm_modify_internal: delete\n", 0,0,0);
100 Debug(LDAP_DEBUG_ARGS,
101 "ldbm_modify_internal: delete\n", 0, 0, 0);
104 rc = modify_delete_values( e, mod, get_permissiveModify( op ),
105 text, textbuf, textlen );
106 assert( rc != LDAP_TYPE_OR_VALUE_EXISTS );
107 if( rc != LDAP_SUCCESS ) {
109 LDAP_LOG( BACK_LDBM, INFO,
110 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
112 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
118 case LDAP_MOD_REPLACE:
120 LDAP_LOG( BACK_LDBM, DETAIL1,
121 "ldbm_modify_internal: replace\n",0,0,0);
123 Debug(LDAP_DEBUG_ARGS,
124 "ldbm_modify_internal: replace\n", 0, 0, 0);
127 rc = modify_replace_values( e, mod, get_permissiveModify( op ),
128 text, textbuf, textlen );
129 if( rc != LDAP_SUCCESS ) {
131 LDAP_LOG( BACK_LDBM, INFO,
132 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
134 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
140 case LDAP_MOD_INCREMENT:
142 LDAP_LOG( BACK_LDBM, DETAIL1,
143 "ldbm_modify_internal: increment\n",0,0,0);
145 Debug(LDAP_DEBUG_ARGS,
146 "ldbm_modify_internal: increment\n",0,0,0);
149 rc = modify_increment_values( e, mod, get_permissiveModify( op ),
150 text, textbuf, textlen );
151 if( rc != LDAP_SUCCESS ) {
153 LDAP_LOG( BACK_LDBM, INFO,
154 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
156 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
162 case SLAP_MOD_SOFTADD:
164 LDAP_LOG( BACK_LDBM, DETAIL1,
165 "ldbm_modify_internal: softadd\n", 0, 0, 0 );
167 Debug(LDAP_DEBUG_ARGS,
168 "ldbm_modify_internal: softadd\n", 0, 0, 0);
171 /* Avoid problems in index_add_mods()
172 * We need to add index if necessary.
174 mod->sm_op = LDAP_MOD_ADD;
176 rc = modify_add_values( e, mod, get_permissiveModify( op ),
177 text, textbuf, textlen );
178 mod->sm_op = SLAP_MOD_SOFTADD;
179 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
183 if( rc != LDAP_SUCCESS ) {
185 LDAP_LOG( BACK_LDBM, INFO,
186 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
188 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
196 LDAP_LOG( BACK_LDBM, ERR,
197 "ldbm_modify_internal: invalid op %d\n", mod->sm_op, 0, 0 );
199 Debug(LDAP_DEBUG_ANY, "ldbm_modify_internal: invalid op %d\n",
204 *text = "Invalid modify operation";
206 LDAP_LOG( BACK_LDBM, INFO,
207 "ldbm_modify_internal: %d (%s)\n", rc, *text, 0 );
209 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
214 if ( rc != LDAP_SUCCESS ) {
218 /* If objectClass was modified, reset the flags */
219 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
223 /* check if modified attribute was indexed */
224 rc = index_is_indexed( op->o_bd, mod->sm_desc );
225 if ( rc == LDAP_SUCCESS ) {
226 ap = attr_find( save_attrs, mod->sm_desc );
227 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
229 ap = attr_find( e->e_attrs, mod->sm_desc );
230 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
234 /* check that the entry still obeys the schema */
235 rc = entry_schema_check( op->o_bd, e, save_attrs, text, textbuf, textlen );
237 if ( rc != LDAP_SUCCESS ) {
239 LDAP_LOG( BACK_LDBM, ERR,
240 "ldbm_modify_internal: entry failed schema check: %s\n",
243 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
250 /* check for abandon */
251 if ( op->o_abandon ) {
256 /* update the indices of the modified attributes */
258 /* start with deleting the old index entries */
259 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
260 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
261 rc = index_values( op, ap->a_desc,
263 e->e_id, SLAP_INDEX_DELETE_OP );
264 if ( rc != LDAP_SUCCESS ) {
266 LDAP_LOG( BACK_LDBM, ERR,
267 "ldbm_modify_internal: Attribute index delete failure\n",
270 Debug( LDAP_DEBUG_ANY,
271 "ldbm_modify_internal: Attribute index delete failure\n",
276 ap->a_flags &= ~SLAP_ATTR_IXDEL;
280 /* add the new index entries */
281 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
282 if ( ap->a_flags & SLAP_ATTR_IXADD ) {
283 rc = index_values( op, ap->a_desc,
285 e->e_id, SLAP_INDEX_ADD_OP );
286 if ( rc != LDAP_SUCCESS ) {
288 LDAP_LOG( BACK_LDBM, ERR,
289 "ldbm_modify_internal: Attribute index add failure\n",
292 Debug( LDAP_DEBUG_ANY,
293 "ldbm_modify_internal: Attribute index add failure\n",
298 ap->a_flags &= ~SLAP_ATTR_IXADD;
303 if ( rc == LDAP_SUCCESS ) {
304 attrs_free( save_attrs );
306 for ( ap = save_attrs; ap; ap = ap->a_next ) {
309 attrs_free( e->e_attrs );
310 e->e_attrs = save_attrs;
321 struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
324 int manageDSAit = get_manageDSAit( op );
325 char textbuf[SLAP_TEXT_BUFLEN];
326 size_t textlen = sizeof textbuf;
329 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_modify: enter\n", 0, 0, 0);
331 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
334 /* grab giant lock for writing */
335 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
337 /* acquire and lock entry */
338 e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
340 /* FIXME: dn2entry() should return non-glue entry */
341 if (( e == NULL ) || ( !manageDSAit && e && is_entry_glue( e ))) {
342 if ( matched != NULL ) {
343 rs->sr_matched = ch_strdup( matched->e_dn );
344 rs->sr_ref = is_entry_referral( matched )
345 ? get_entry_referrals( op, matched )
347 cache_return_entry_r( &li->li_cache, matched );
349 BerVarray deref = NULL;
350 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
352 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
354 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
355 ber_bvarray_add( &deref, &tmpbv );
358 deref = default_referral;
360 rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn,
361 LDAP_SCOPE_DEFAULT );
364 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
365 rs->sr_err = LDAP_REFERRAL;
366 send_ldap_result( op, rs );
368 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
369 free( (char *)rs->sr_matched );
372 rs->sr_matched = NULL;
376 if ( !manageDSAit && is_entry_referral( e ) )
378 /* parent is a referral, don't allow add */
379 /* parent is an alias, don't allow add */
380 rs->sr_ref = get_entry_referrals( op, e );
383 LDAP_LOG( BACK_LDBM, INFO,
384 "ldbm_back_modify: entry (%s) is referral\n", op->o_req_ndn.bv_val, 0, 0 );
386 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
390 rs->sr_err = LDAP_REFERRAL;
391 rs->sr_matched = e->e_name.bv_val;
392 send_ldap_result( op, rs );
394 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
396 rs->sr_matched = NULL;
400 /* Modify the entry */
401 rs->sr_err = ldbm_modify_internal( op, op->oq_modify.rs_modlist, e,
402 &rs->sr_text, textbuf, textlen );
404 if( rs->sr_err != LDAP_SUCCESS ) {
405 if( rs->sr_err != SLAPD_ABANDON ) {
406 send_ldap_result( op, rs );
412 /* change the entry itself */
413 if ( id2entry_add( op->o_bd, e ) != 0 ) {
414 send_ldap_error( op, rs, LDAP_OTHER,
415 "id2entry failure" );
416 rs->sr_err = LDAP_OTHER;
421 send_ldap_error( op, rs, LDAP_SUCCESS,
424 cache_return_entry_w( &li->li_cache, e );
425 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
430 cache_return_entry_w( &li->li_cache, e );
431 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);