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(
30 Modifications *modlist,
37 int rc = LDAP_SUCCESS;
40 Attribute *save_attrs;
44 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_modify_internal: %s\n", dn, 0, 0 );
46 Debug(LDAP_DEBUG_TRACE, "ldbm_modify_internal: %s\n", dn, 0, 0);
50 if ( !acl_check_modlist( be, conn, op, e, modlist )) {
51 return LDAP_INSUFFICIENT_ACCESS;
54 save_attrs = e->e_attrs;
55 e->e_attrs = attrs_dup( e->e_attrs );
57 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
60 switch ( mod->sm_op ) {
63 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: add\n", 0, 0, 0);
65 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: add\n", 0, 0, 0);
68 rc = modify_add_values( e, mod, get_permissiveModify( op ),
69 text, textbuf, textlen );
70 if( rc != LDAP_SUCCESS ) {
72 LDAP_LOG( BACK_LDBM, INFO,
73 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
75 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
83 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: delete\n", 0,0,0);
85 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: delete\n", 0, 0, 0);
88 rc = modify_delete_values( e, mod, get_permissiveModify( op ),
89 text, textbuf, textlen );
90 assert( rc != LDAP_TYPE_OR_VALUE_EXISTS );
91 if( rc != LDAP_SUCCESS ) {
93 LDAP_LOG( BACK_LDBM, INFO,
94 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
96 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
102 case LDAP_MOD_REPLACE:
104 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: replace\n",0,0,0);
106 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: replace\n", 0, 0, 0);
109 rc = modify_replace_values( e, mod, get_permissiveModify( op ),
110 text, textbuf, textlen );
111 if( rc != LDAP_SUCCESS ) {
113 LDAP_LOG( BACK_LDBM, INFO,
114 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
116 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
122 case SLAP_MOD_SOFTADD:
124 LDAP_LOG( BACK_LDBM, DETAIL1,
125 "ldbm_modify_internal: softadd\n", 0, 0, 0 );
127 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: softadd\n", 0, 0, 0);
130 /* Avoid problems in index_add_mods()
131 * We need to add index if necessary.
133 mod->sm_op = LDAP_MOD_ADD;
135 rc = modify_add_values( e, mod, get_permissiveModify( op ),
136 text, textbuf, textlen );
137 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
141 if( rc != LDAP_SUCCESS ) {
143 LDAP_LOG( BACK_LDBM, INFO,
144 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
146 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
154 LDAP_LOG( BACK_LDBM, ERR,
155 "ldbm_modify_internal: invalid op %d\n", mod->sm_op, 0, 0 );
157 Debug(LDAP_DEBUG_ANY, "ldbm_modify_internal: invalid op %d\n",
162 *text = "Invalid modify operation";
164 LDAP_LOG( BACK_LDBM, INFO,
165 "ldbm_modify_internal: %d (%s)\n", rc, *text, 0 );
167 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
172 if ( rc != LDAP_SUCCESS ) {
176 /* If objectClass was modified, reset the flags */
177 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
181 /* check if modified attribute was indexed */
182 rc = index_is_indexed( be, mod->sm_desc );
183 if ( rc == LDAP_SUCCESS ) {
184 ap = attr_find( save_attrs, mod->sm_desc );
185 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
187 ap = attr_find( e->e_attrs, mod->sm_desc );
188 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
192 /* check that the entry still obeys the schema */
194 rc = entry_schema_check( be, e, save_attrs, text, textbuf, textlen );
195 #else /* LDAP_CACHING */
196 if ( !op->o_caching_on ) {
197 rc = entry_schema_check( be, e, save_attrs,
198 text, textbuf, textlen );
202 #endif /* LDAP_CACHING */
204 if ( rc != LDAP_SUCCESS ) {
206 LDAP_LOG( BACK_LDBM, ERR,
207 "ldbm_modify_internal: entry failed schema check: %s\n",
210 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
217 /* check for abandon */
218 if ( op->o_abandon ) {
223 /* update the indices of the modified attributes */
225 /* start with deleting the old index entries */
226 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
227 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
228 rc = index_values( be, ap->a_desc,
230 ap->a_nvals ? ap->a_nvals : ap->a_vals,
234 e->e_id, SLAP_INDEX_DELETE_OP );
235 if ( rc != LDAP_SUCCESS ) {
237 LDAP_LOG( BACK_LDBM, ERR,
238 "ldbm_modify_internal: Attribute index delete failure\n",
241 Debug( LDAP_DEBUG_ANY,
242 "Attribute index delete failure",
247 ap->a_flags &= ~SLAP_ATTR_IXDEL;
251 /* add the new index entries */
252 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
253 if ( ap->a_flags & SLAP_ATTR_IXADD ) {
254 rc = index_values( be, ap->a_desc,
256 ap->a_nvals ? ap->a_nvals : ap->a_vals,
260 e->e_id, SLAP_INDEX_ADD_OP );
261 if ( rc != LDAP_SUCCESS ) {
263 LDAP_LOG( BACK_LDBM, ERR,
264 "ldbm_modify_internal: Attribute index add failure\n",
267 Debug( LDAP_DEBUG_ANY,
268 "Attribute index add failure",
273 ap->a_flags &= ~SLAP_ATTR_IXADD;
278 if ( rc == LDAP_SUCCESS ) {
279 attrs_free( save_attrs );
281 for ( ap = save_attrs; ap; ap = ap->a_next ) {
284 attrs_free( e->e_attrs );
285 e->e_attrs = save_attrs;
298 Modifications *modlist
302 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
305 int manageDSAit = get_manageDSAit( op );
306 const char *text = NULL;
307 char textbuf[SLAP_TEXT_BUFLEN];
308 size_t textlen = sizeof textbuf;
311 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_modify: enter\n", 0, 0, 0);
313 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
316 /* grab giant lock for writing */
317 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
319 /* acquire and lock entry */
320 if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
321 char* matched_dn = NULL;
324 if ( matched != NULL ) {
325 matched_dn = ch_strdup( matched->e_dn );
326 refs = is_entry_referral( matched )
327 ? get_entry_referrals( be, conn, op, matched )
329 cache_return_entry_r( &li->li_cache, matched );
331 refs = referral_rewrite( default_referral,
332 NULL, dn, LDAP_SCOPE_DEFAULT );
335 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
336 send_ldap_result( conn, op, LDAP_REFERRAL,
337 matched_dn, NULL, refs, NULL );
339 if ( refs ) ber_bvarray_free( refs );
346 if ( !manageDSAit && is_entry_referral( e ) )
347 #else /* LDAP_CACHING */
348 if ( !op->o_caching_on && !manageDSAit && is_entry_referral( e ) )
349 #endif /* LDAP_CACHING */
351 /* parent is a referral, don't allow add */
352 /* parent is an alias, don't allow add */
353 BerVarray refs = get_entry_referrals( be,
357 LDAP_LOG( BACK_LDBM, INFO,
358 "ldbm_back_modify: entry (%s) is referral\n", ndn->bv_val, 0, 0 );
360 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
365 send_ldap_result( conn, op, LDAP_REFERRAL,
366 e->e_dn, NULL, refs, NULL );
368 if ( refs ) ber_bvarray_free( refs );
373 /* Modify the entry */
374 rc = ldbm_modify_internal( be, conn, op, ndn->bv_val, modlist, e,
375 &text, textbuf, textlen );
377 if( rc != LDAP_SUCCESS ) {
378 if( rc != SLAPD_ABANDON ) {
379 send_ldap_result( conn, op, rc,
380 NULL, text, NULL, NULL );
386 /* change the entry itself */
387 if ( id2entry_add( be, e ) != 0 ) {
388 send_ldap_result( conn, op, LDAP_OTHER,
389 NULL, "id2entry failure", NULL, NULL );
393 send_ldap_result( conn, op, LDAP_SUCCESS,
394 NULL, NULL, NULL, NULL );
396 cache_return_entry_w( &li->li_cache, e );
397 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
401 cache_return_entry_w( &li->li_cache, e );
402 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);