1 /* modify.c - ldbm backend modify routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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;
48 Debug(LDAP_DEBUG_TRACE,
49 "ldbm_modify_internal: %s\n",
51 get_permissiveModify(op) ? " (permissive)" : "",
54 if ( !acl_check_modlist( op, e, modlist )) {
55 return LDAP_INSUFFICIENT_ACCESS;
58 save_attrs = e->e_attrs;
59 e->e_attrs = attrs_dup( e->e_attrs );
61 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
64 switch ( mod->sm_op ) {
66 Debug(LDAP_DEBUG_ARGS,
67 "ldbm_modify_internal: add\n", 0, 0, 0);
69 rc = modify_add_values( e, mod, get_permissiveModify( op ),
70 text, textbuf, textlen );
71 if( rc != LDAP_SUCCESS ) {
72 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
78 Debug(LDAP_DEBUG_ARGS,
79 "ldbm_modify_internal: delete\n", 0, 0, 0);
81 rc = modify_delete_values( e, mod, get_permissiveModify( op ),
82 text, textbuf, textlen );
83 assert( rc != LDAP_TYPE_OR_VALUE_EXISTS );
84 if( rc != LDAP_SUCCESS ) {
85 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
90 case LDAP_MOD_REPLACE:
91 Debug(LDAP_DEBUG_ARGS,
92 "ldbm_modify_internal: replace\n", 0, 0, 0);
94 rc = modify_replace_values( e, mod, get_permissiveModify( op ),
95 text, textbuf, textlen );
96 if( rc != LDAP_SUCCESS ) {
97 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
102 case LDAP_MOD_INCREMENT:
103 Debug(LDAP_DEBUG_ARGS,
104 "ldbm_modify_internal: increment\n",0,0,0);
106 rc = modify_increment_values( e, mod, get_permissiveModify( op ),
107 text, textbuf, textlen );
108 if( rc != LDAP_SUCCESS ) {
109 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
114 case SLAP_MOD_SOFTADD:
115 Debug(LDAP_DEBUG_ARGS,
116 "ldbm_modify_internal: softadd\n", 0, 0, 0);
118 /* Avoid problems in index_add_mods()
119 * We need to add index if necessary.
121 mod->sm_op = LDAP_MOD_ADD;
123 rc = modify_add_values( e, mod, get_permissiveModify( op ),
124 text, textbuf, textlen );
125 mod->sm_op = SLAP_MOD_SOFTADD;
126 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
130 if( rc != LDAP_SUCCESS ) {
131 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
137 Debug(LDAP_DEBUG_ANY, "ldbm_modify_internal: invalid op %d\n",
141 *text = "Invalid modify operation";
142 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
146 if ( rc != LDAP_SUCCESS ) {
150 /* If objectClass was modified, reset the flags */
151 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
155 /* check if modified attribute was indexed */
156 rc = index_is_indexed( op->o_bd, mod->sm_desc );
157 if ( rc == LDAP_SUCCESS ) {
158 ap = attr_find( save_attrs, mod->sm_desc );
159 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
161 ap = attr_find( e->e_attrs, mod->sm_desc );
162 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
166 /* check that the entry still obeys the schema */
167 rc = entry_schema_check( op->o_bd, e, save_attrs, text, textbuf, textlen );
169 if ( rc != LDAP_SUCCESS ) {
170 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
176 /* check for abandon */
177 if ( op->o_abandon ) {
182 /* update the indices of the modified attributes */
184 /* start with deleting the old index entries */
185 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
186 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
187 rc = index_values( op, ap->a_desc,
189 e->e_id, SLAP_INDEX_DELETE_OP );
190 if ( rc != LDAP_SUCCESS ) {
191 Debug( LDAP_DEBUG_ANY,
192 "ldbm_modify_internal: Attribute index delete failure\n",
196 ap->a_flags &= ~SLAP_ATTR_IXDEL;
200 /* add the new index entries */
201 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
202 if ( ap->a_flags & SLAP_ATTR_IXADD ) {
203 rc = index_values( op, ap->a_desc,
205 e->e_id, SLAP_INDEX_ADD_OP );
206 if ( rc != LDAP_SUCCESS ) {
207 Debug( LDAP_DEBUG_ANY,
208 "ldbm_modify_internal: Attribute index add failure\n",
212 ap->a_flags &= ~SLAP_ATTR_IXADD;
217 if ( rc == LDAP_SUCCESS ) {
218 attrs_free( save_attrs );
220 for ( ap = save_attrs; ap; ap = ap->a_next ) {
223 attrs_free( e->e_attrs );
224 e->e_attrs = save_attrs;
235 struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
238 int manageDSAit = get_manageDSAit( op );
239 char textbuf[SLAP_TEXT_BUFLEN];
240 size_t textlen = sizeof textbuf;
242 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
244 /* grab giant lock for writing */
245 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
247 /* acquire and lock entry */
248 e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
250 /* FIXME: dn2entry() should return non-glue entry */
251 if (( e == NULL ) || ( !manageDSAit && e && is_entry_glue( e ))) {
252 BerVarray deref = NULL;
253 if ( matched != NULL ) {
254 rs->sr_matched = ch_strdup( matched->e_dn );
255 rs->sr_ref = is_entry_referral( matched )
256 ? get_entry_referrals( op, matched )
258 cache_return_entry_r( &li->li_cache, matched );
260 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
262 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
264 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
265 ber_bvarray_add( &deref, &tmpbv );
268 deref = default_referral;
270 rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn,
271 LDAP_SCOPE_DEFAULT );
274 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
275 rs->sr_err = LDAP_REFERRAL;
276 send_ldap_result( op, rs );
278 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
279 if ( deref != default_referral ) {
280 ber_bvarray_free( deref );
282 free( (char *)rs->sr_matched );
285 rs->sr_matched = NULL;
289 if ( !manageDSAit && is_entry_referral( e ) )
291 /* parent is a referral, don't allow add */
292 /* parent is an alias, don't allow add */
293 rs->sr_ref = get_entry_referrals( op, e );
295 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
298 rs->sr_err = LDAP_REFERRAL;
299 rs->sr_matched = e->e_name.bv_val;
300 send_ldap_result( op, rs );
302 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
304 rs->sr_matched = NULL;
308 /* Modify the entry */
309 rs->sr_err = ldbm_modify_internal( op, op->oq_modify.rs_modlist, e,
310 &rs->sr_text, textbuf, textlen );
312 if( rs->sr_err != LDAP_SUCCESS ) {
313 if( rs->sr_err != SLAPD_ABANDON ) {
314 send_ldap_result( op, rs );
320 /* change the entry itself */
321 if ( id2entry_add( op->o_bd, e ) != 0 ) {
322 send_ldap_error( op, rs, LDAP_OTHER,
323 "id2entry failure" );
324 rs->sr_err = LDAP_OTHER;
329 send_ldap_error( op, rs, LDAP_SUCCESS,
332 cache_return_entry_w( &li->li_cache, e );
333 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
338 cache_return_entry_w( &li->li_cache, e );
339 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);