1 /* modify.c - ldbm backend modify routine */
4 * Copyright 1998-2002 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, text, textbuf, textlen );
69 if( rc != LDAP_SUCCESS ) {
71 LDAP_LOG( BACK_LDBM, INFO,
72 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
74 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
82 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: delete\n", 0,0,0);
84 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: delete\n", 0, 0, 0);
87 rc = modify_delete_values( e, mod, text, textbuf, textlen );
88 assert( rc != LDAP_TYPE_OR_VALUE_EXISTS );
89 if( rc != LDAP_SUCCESS ) {
91 LDAP_LOG( BACK_LDBM, INFO,
92 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
94 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
100 case LDAP_MOD_REPLACE:
102 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: replace\n",0,0,0);
104 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: replace\n", 0, 0, 0);
107 rc = modify_replace_values( e, mod, text, textbuf, textlen );
108 if( rc != LDAP_SUCCESS ) {
110 LDAP_LOG( BACK_LDBM, INFO,
111 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
113 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
119 case SLAP_MOD_SOFTADD:
121 LDAP_LOG( BACK_LDBM, DETAIL1,
122 "ldbm_modify_internal: softadd\n", 0, 0, 0 );
124 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: softadd\n", 0, 0, 0);
127 /* Avoid problems in index_add_mods()
128 * We need to add index if necessary.
130 mod->sm_op = LDAP_MOD_ADD;
132 rc = modify_add_values( e, mod, text, textbuf, textlen );
133 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
137 if( rc != LDAP_SUCCESS ) {
139 LDAP_LOG( BACK_LDBM, INFO,
140 "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
142 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
150 LDAP_LOG( BACK_LDBM, ERR,
151 "ldbm_modify_internal: invalid op %d\n", mod->sm_op, 0, 0 );
153 Debug(LDAP_DEBUG_ANY, "ldbm_modify_internal: invalid op %d\n",
158 *text = "Invalid modify operation";
160 LDAP_LOG( BACK_LDBM, INFO,
161 "ldbm_modify_internal: %d (%s)\n", rc, *text, 0 );
163 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
168 if ( rc != LDAP_SUCCESS ) {
172 /* If objectClass was modified, reset the flags */
173 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
177 /* check if modified attribute was indexed */
178 rc = index_is_indexed( be, mod->sm_desc );
179 if ( rc == LDAP_SUCCESS ) {
180 ap = attr_find( save_attrs, mod->sm_desc );
181 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
183 ap = attr_find( e->e_attrs, mod->sm_desc );
184 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
188 /* check that the entry still obeys the schema */
189 rc = entry_schema_check( be, e, save_attrs, text, textbuf, textlen );
190 if ( rc != LDAP_SUCCESS ) {
192 LDAP_LOG( BACK_LDBM, ERR,
193 "ldbm_modify_internal: entry failed schema check: %s\n",
196 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
203 /* check for abandon */
204 if ( op->o_abandon ) {
209 /* update the indices of the modified attributes */
211 /* start with deleting the old index entries */
212 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
213 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
214 rc = index_values( be, ap->a_desc, ap->a_vals, e->e_id,
215 SLAP_INDEX_DELETE_OP );
216 if ( rc != LDAP_SUCCESS ) {
218 LDAP_LOG( BACK_LDBM, ERR,
219 "ldbm_modify_internal: Attribute index delete failure\n",
222 Debug( LDAP_DEBUG_ANY,
223 "Attribute index delete failure",
228 ap->a_flags &= ~SLAP_ATTR_IXDEL;
232 /* add the new index entries */
233 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
234 if ( ap->a_flags & SLAP_ATTR_IXADD ) {
235 rc = index_values( be, ap->a_desc, ap->a_vals, e->e_id,
237 if ( rc != LDAP_SUCCESS ) {
239 LDAP_LOG( BACK_LDBM, ERR,
240 "ldbm_modify_internal: Attribute index add failure\n",
243 Debug( LDAP_DEBUG_ANY,
244 "Attribute index add failure",
249 ap->a_flags &= ~SLAP_ATTR_IXADD;
254 if ( rc == LDAP_SUCCESS ) {
255 attrs_free( save_attrs );
257 for ( ap = save_attrs; ap; ap = ap->a_next ) {
260 attrs_free( e->e_attrs );
261 e->e_attrs = save_attrs;
274 Modifications *modlist
278 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
281 int manageDSAit = get_manageDSAit( op );
282 const char *text = NULL;
283 char textbuf[SLAP_TEXT_BUFLEN];
284 size_t textlen = sizeof textbuf;
287 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_modify: enter\n", 0, 0, 0);
289 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
292 /* grab giant lock for writing */
293 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
295 /* acquire and lock entry */
296 if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
297 char* matched_dn = NULL;
300 if ( matched != NULL ) {
301 matched_dn = ch_strdup( matched->e_dn );
302 refs = is_entry_referral( matched )
303 ? get_entry_referrals( be, conn, op, matched )
305 cache_return_entry_r( &li->li_cache, matched );
307 refs = referral_rewrite( default_referral,
308 NULL, dn, LDAP_SCOPE_DEFAULT );
311 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
312 send_ldap_result( conn, op, LDAP_REFERRAL,
313 matched_dn, NULL, refs, NULL );
315 if ( refs ) ber_bvarray_free( refs );
321 if ( !manageDSAit && is_entry_referral( e ) ) {
322 /* parent is a referral, don't allow add */
323 /* parent is an alias, don't allow add */
324 BerVarray refs = get_entry_referrals( be,
328 LDAP_LOG( BACK_LDBM, INFO,
329 "ldbm_back_modify: entry (%s) is referral\n", ndn->bv_val, 0, 0 );
331 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
336 send_ldap_result( conn, op, LDAP_REFERRAL,
337 e->e_dn, NULL, refs, NULL );
339 if ( refs ) ber_bvarray_free( refs );
344 /* Modify the entry */
345 rc = ldbm_modify_internal( be, conn, op, ndn->bv_val, modlist, e,
346 &text, textbuf, textlen );
348 if( rc != LDAP_SUCCESS ) {
349 if( rc != SLAPD_ABANDON ) {
350 send_ldap_result( conn, op, rc,
351 NULL, text, NULL, NULL );
357 /* change the entry itself */
358 if ( id2entry_add( be, e ) != 0 ) {
359 send_ldap_result( conn, op, LDAP_OTHER,
360 NULL, "id2entry failure", NULL, NULL );
364 send_ldap_result( conn, op, LDAP_SUCCESS,
365 NULL, NULL, NULL, NULL );
367 cache_return_entry_w( &li->li_cache, e );
368 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
372 cache_return_entry_w( &li->li_cache, e );
373 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);