1 /* modify.c - ldbm backend modify routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 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, e, save_attrs, get_manageDIT(op),
168 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 if ( !SLAP_SHADOW( op->o_bd ))
245 slap_mods_opattrs( op, &op->orm_modlist, 1 );
247 /* grab giant lock for writing */
248 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
250 /* acquire and lock entry */
251 e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
253 /* FIXME: dn2entry() should return non-glue entry */
254 if (( e == NULL ) || ( !manageDSAit && e && is_entry_glue( e ))) {
255 if ( matched != NULL ) {
256 rs->sr_matched = ber_strdup_x( matched->e_dn, op->o_tmpmemctx );
257 rs->sr_ref = is_entry_referral( matched )
258 ? get_entry_referrals( op, matched )
260 cache_return_entry_r( &li->li_cache, matched );
262 rs->sr_ref = referral_rewrite( default_referral, NULL,
263 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
266 rs->sr_err = LDAP_REFERRAL;
267 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
271 if ( !manageDSAit && is_entry_referral( e ) )
273 /* parent is a referral, don't allow add */
274 /* parent is an alias, don't allow add */
275 rs->sr_ref = get_entry_referrals( op, e );
277 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
280 rs->sr_err = LDAP_REFERRAL;
281 rs->sr_matched = ber_strdup_x( e->e_name.bv_val, op->o_tmpmemctx );
282 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
286 /* Modify the entry */
287 rs->sr_err = ldbm_modify_internal( op, op->oq_modify.rs_modlist, e,
288 &rs->sr_text, textbuf, textlen );
290 /* change the entry itself */
291 if( rs->sr_err == LDAP_SUCCESS ) {
292 if ( id2entry_add( op->o_bd, e ) != 0 ) {
293 rs->sr_err = LDAP_OTHER;
294 rs->sr_text = "id2entry failure";
299 cache_return_entry_w( &li->li_cache, e );
300 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
302 send_ldap_result( op, rs );
303 if ( !SLAP_SHADOW( op->o_bd ))
304 slap_graduate_commit_csn( op );