]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modify.c
ITS#2368 - fix deleting key from range IDL
[openldap] / servers / slapd / back-ldbm / modify.c
1 /* modify.c - ldbm backend modify routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14 #include <ac/time.h>
15
16 #include "slap.h"
17 #include "back-ldbm.h"
18 #include "proto-back-ldbm.h"
19
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
24  */ 
25 int ldbm_modify_internal(
26     Backend     *be,
27     Connection  *conn,
28     Operation   *op,
29     const char  *dn,
30     Modifications       *modlist,
31     Entry       *e,
32         const char **text,
33         char *textbuf,
34         size_t textlen
35 )
36 {
37         int rc = LDAP_SUCCESS;
38         Modification    *mod;
39         Modifications   *ml;
40         Attribute       *save_attrs;
41         Attribute       *ap;
42
43 #ifdef NEW_LOGGING
44         LDAP_LOG( BACK_LDBM, ENTRY,  "ldbm_modify_internal: %s\n", dn, 0, 0 );
45 #else
46         Debug(LDAP_DEBUG_TRACE, "ldbm_modify_internal: %s\n", dn, 0, 0);
47 #endif
48
49
50         if ( !acl_check_modlist( be, conn, op, e, modlist )) {
51                 return LDAP_INSUFFICIENT_ACCESS;
52         }
53
54         save_attrs = e->e_attrs;
55         e->e_attrs = attrs_dup( e->e_attrs );
56
57         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
58                 mod = &ml->sml_mod;
59
60                 switch ( mod->sm_op ) {
61                 case LDAP_MOD_ADD:
62 #ifdef NEW_LOGGING
63                         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: add\n", 0, 0, 0);
64 #else
65                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: add\n", 0, 0, 0);
66 #endif
67
68                         rc = modify_add_values( e, mod, get_permissiveModify( op ),
69                                                 text, textbuf, textlen );
70                         if( rc != LDAP_SUCCESS ) {
71 #ifdef NEW_LOGGING
72                                 LDAP_LOG( BACK_LDBM, INFO, 
73                                         "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
74 #else
75                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
76                                         rc, *text, 0);
77 #endif
78                         }
79                         break;
80
81                 case LDAP_MOD_DELETE:
82 #ifdef NEW_LOGGING
83                         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: delete\n", 0,0,0);
84 #else
85                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: delete\n", 0, 0, 0);
86 #endif
87
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 ) {
92 #ifdef NEW_LOGGING
93                                 LDAP_LOG( BACK_LDBM, INFO, 
94                                         "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
95 #else
96                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
97                                         rc, *text, 0);
98 #endif
99                         }
100                         break;
101
102                 case LDAP_MOD_REPLACE:
103 #ifdef NEW_LOGGING
104                         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal:  replace\n",0,0,0);
105 #else
106                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: replace\n", 0, 0, 0);
107 #endif
108
109                         rc = modify_replace_values( e, mod, get_permissiveModify( op ),
110                                                         text, textbuf, textlen );
111                         if( rc != LDAP_SUCCESS ) {
112 #ifdef NEW_LOGGING
113                                 LDAP_LOG( BACK_LDBM, INFO, 
114                                         "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
115 #else
116                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
117                                         rc, *text, 0);
118 #endif
119                         }
120                         break;
121
122                 case SLAP_MOD_SOFTADD:
123 #ifdef NEW_LOGGING
124                         LDAP_LOG( BACK_LDBM, DETAIL1, 
125                                 "ldbm_modify_internal: softadd\n", 0, 0, 0 );
126 #else
127                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: softadd\n", 0, 0, 0);
128 #endif
129
130                         /* Avoid problems in index_add_mods()
131                          * We need to add index if necessary.
132                          */
133                         mod->sm_op = LDAP_MOD_ADD;
134
135                         rc = modify_add_values( e, mod, get_permissiveModify( op ),
136                                                 text, textbuf, textlen );
137                         if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
138                                 rc = LDAP_SUCCESS;
139                         }
140
141                         if( rc != LDAP_SUCCESS ) {
142 #ifdef NEW_LOGGING
143                                 LDAP_LOG( BACK_LDBM, INFO, 
144                                            "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
145 #else
146                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
147                                         rc, *text, 0);
148 #endif
149                         }
150                         break;
151
152                 default:
153 #ifdef NEW_LOGGING
154                         LDAP_LOG( BACK_LDBM, ERR, 
155                                 "ldbm_modify_internal: invalid op %d\n", mod->sm_op, 0, 0 );
156 #else
157                         Debug(LDAP_DEBUG_ANY, "ldbm_modify_internal: invalid op %d\n",
158                                 mod->sm_op, 0, 0);
159 #endif
160
161                         rc = LDAP_OTHER;
162                         *text = "Invalid modify operation";
163 #ifdef NEW_LOGGING
164                         LDAP_LOG( BACK_LDBM, INFO, 
165                                 "ldbm_modify_internal: %d (%s)\n", rc, *text, 0 );
166 #else
167                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
168                                 rc, *text, 0);
169 #endif
170                 }
171
172                 if ( rc != LDAP_SUCCESS ) {
173                         goto exit;
174                 }
175
176                 /* If objectClass was modified, reset the flags */
177                 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
178                         e->e_ocflags = 0;
179                 }
180
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;
186
187                         ap = attr_find( e->e_attrs, mod->sm_desc );
188                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
189                 }
190         }
191
192         /* check that the entry still obeys the schema */
193 #ifndef LDAP_CACHING
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 );
199         } else {
200                 rc = LDAP_SUCCESS; 
201         }
202 #endif /* LDAP_CACHING */
203
204         if ( rc != LDAP_SUCCESS ) {
205 #ifdef NEW_LOGGING
206                 LDAP_LOG( BACK_LDBM, ERR, 
207                         "ldbm_modify_internal: entry failed schema check: %s\n", 
208                         *text, 0, 0 );
209 #else
210                 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
211                         *text, 0, 0 );
212 #endif
213
214                 goto exit;
215         }
216
217         /* check for abandon */
218         if ( op->o_abandon ) {
219                 rc = SLAPD_ABANDON;
220                 goto exit;
221         }
222
223         /* update the indices of the modified attributes */
224
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, ap->a_vals, e->e_id,
229                                            SLAP_INDEX_DELETE_OP );
230                         if ( rc != LDAP_SUCCESS ) {
231 #ifdef NEW_LOGGING
232                                 LDAP_LOG( BACK_LDBM, ERR,
233                                         "ldbm_modify_internal: Attribute index delete failure\n",
234                                         0, 0, 0 );
235 #else
236                                 Debug( LDAP_DEBUG_ANY,
237                                        "Attribute index delete failure",
238                                        0, 0, 0 );
239 #endif
240                                 goto exit;
241                         }
242                         ap->a_flags &= ~SLAP_ATTR_IXDEL;
243                 }
244         }
245
246         /* add the new index entries */
247         for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
248                 if ( ap->a_flags & SLAP_ATTR_IXADD ) {
249                         rc = index_values( be, ap->a_desc, ap->a_vals, e->e_id,
250                                            SLAP_INDEX_ADD_OP );
251                         if ( rc != LDAP_SUCCESS ) {
252 #ifdef NEW_LOGGING
253                                 LDAP_LOG( BACK_LDBM, ERR,
254                                         "ldbm_modify_internal: Attribute index add failure\n",
255                                         0, 0, 0 );
256 #else
257                                 Debug( LDAP_DEBUG_ANY,
258                                        "Attribute index add failure",
259                                        0, 0, 0 );
260 #endif
261                                 goto exit;
262                         }
263                         ap->a_flags &= ~SLAP_ATTR_IXADD;
264                 }
265         }
266
267 exit:
268         if ( rc == LDAP_SUCCESS ) {
269                 attrs_free( save_attrs );
270         } else {
271                 for ( ap = save_attrs; ap; ap = ap->a_next ) {
272                         ap->a_flags = 0;
273                 }
274                 attrs_free( e->e_attrs );
275                 e->e_attrs = save_attrs;
276         }
277
278         return rc;
279 }
280
281 int
282 ldbm_back_modify(
283     Backend     *be,
284     Connection  *conn,
285     Operation   *op,
286     struct berval       *dn,
287     struct berval       *ndn,
288     Modifications       *modlist
289 )
290 {
291         int rc;
292         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
293         Entry           *matched;
294         Entry           *e;
295         int             manageDSAit = get_manageDSAit( op );
296         const char *text = NULL;
297         char textbuf[SLAP_TEXT_BUFLEN];
298         size_t textlen = sizeof textbuf;
299
300 #ifdef NEW_LOGGING
301         LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_modify: enter\n", 0, 0, 0);
302 #else
303         Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
304 #endif
305
306         /* grab giant lock for writing */
307         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
308
309         /* acquire and lock entry */
310         if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
311                 char* matched_dn = NULL;
312                 BerVarray refs;
313
314                 if ( matched != NULL ) {
315                         matched_dn = ch_strdup( matched->e_dn );
316                         refs = is_entry_referral( matched )
317                                 ? get_entry_referrals( be, conn, op, matched )
318                                 : NULL;
319                         cache_return_entry_r( &li->li_cache, matched );
320                 } else {
321                         refs = referral_rewrite( default_referral,
322                                 NULL, dn, LDAP_SCOPE_DEFAULT );
323                 }
324
325                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
326                 send_ldap_result( conn, op, LDAP_REFERRAL,
327                         matched_dn, NULL, refs, NULL );
328
329                 if ( refs ) ber_bvarray_free( refs );
330                 free( matched_dn );
331
332                 return( -1 );
333         }
334
335 #ifndef LDAP_CACHING
336         if ( !manageDSAit && is_entry_referral( e ) )
337 #else /* LDAP_CACHING */
338         if ( !op->o_caching_on && !manageDSAit && is_entry_referral( e ) )
339 #endif /* LDAP_CACHING */
340         {
341                 /* parent is a referral, don't allow add */
342                 /* parent is an alias, don't allow add */
343                 BerVarray refs = get_entry_referrals( be,
344                         conn, op, e );
345
346 #ifdef NEW_LOGGING
347                 LDAP_LOG( BACK_LDBM, INFO, 
348                            "ldbm_back_modify: entry (%s) is referral\n", ndn->bv_val, 0, 0 );
349 #else
350                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
351                     0, 0 );
352 #endif
353
354
355                 send_ldap_result( conn, op, LDAP_REFERRAL,
356                     e->e_dn, NULL, refs, NULL );
357
358                 if ( refs ) ber_bvarray_free( refs );
359
360                 goto error_return;
361         }
362         
363         /* Modify the entry */
364         rc = ldbm_modify_internal( be, conn, op, ndn->bv_val, modlist, e,
365                 &text, textbuf, textlen );
366
367         if( rc != LDAP_SUCCESS ) {
368                 if( rc != SLAPD_ABANDON ) {
369                         send_ldap_result( conn, op, rc,
370                                 NULL, text, NULL, NULL );
371                 }
372
373                 goto error_return;
374         }
375
376         /* change the entry itself */
377         if ( id2entry_add( be, e ) != 0 ) {
378                 send_ldap_result( conn, op, LDAP_OTHER,
379                         NULL, "id2entry failure", NULL, NULL );
380                 goto error_return;
381         }
382
383         send_ldap_result( conn, op, LDAP_SUCCESS,
384                 NULL, NULL, NULL, NULL );
385
386         cache_return_entry_w( &li->li_cache, e );
387         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
388         return( 0 );
389
390 error_return:;
391         cache_return_entry_w( &li->li_cache, e );
392         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
393         return( -1 );
394 }