]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modify.c
#unifdef -DSLAP_NVALUES_ON_DISK
[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     Operation   *op,
27     Modifications       *modlist,
28     Entry       *e,
29         const char **text,
30         char *textbuf,
31         size_t textlen
32 )
33 {
34         int rc = LDAP_SUCCESS;
35         Modification    *mod;
36         Modifications   *ml;
37         Attribute       *save_attrs;
38         Attribute       *ap;
39
40 #ifdef NEW_LOGGING
41         LDAP_LOG( BACK_LDBM, ENTRY,  "ldbm_modify_internal: %s\n", e->e_name.bv_val, 0, 0 );
42 #else
43         Debug(LDAP_DEBUG_TRACE, "ldbm_modify_internal: %s\n", e->e_name.bv_val, 0, 0);
44 #endif
45
46
47         if ( !acl_check_modlist( op, e, modlist )) {
48                 return LDAP_INSUFFICIENT_ACCESS;
49         }
50
51         save_attrs = e->e_attrs;
52         e->e_attrs = attrs_dup( e->e_attrs );
53
54         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
55                 mod = &ml->sml_mod;
56
57                 switch ( mod->sm_op ) {
58                 case LDAP_MOD_ADD:
59 #ifdef NEW_LOGGING
60                         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: add\n", 0, 0, 0);
61 #else
62                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: add\n", 0, 0, 0);
63 #endif
64
65                         rc = modify_add_values( e, mod, get_permissiveModify( op ),
66                                                 text, textbuf, textlen );
67                         if( rc != LDAP_SUCCESS ) {
68 #ifdef NEW_LOGGING
69                                 LDAP_LOG( BACK_LDBM, INFO, 
70                                         "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
71 #else
72                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
73                                         rc, *text, 0);
74 #endif
75                         }
76                         break;
77
78                 case LDAP_MOD_DELETE:
79 #ifdef NEW_LOGGING
80                         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: delete\n", 0,0,0);
81 #else
82                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: delete\n", 0, 0, 0);
83 #endif
84
85                         rc = modify_delete_values( e, mod, get_permissiveModify( op ),
86                                                         text, textbuf, textlen );
87                         assert( rc != LDAP_TYPE_OR_VALUE_EXISTS );
88                         if( rc != LDAP_SUCCESS ) {
89 #ifdef NEW_LOGGING
90                                 LDAP_LOG( BACK_LDBM, INFO, 
91                                         "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
92 #else
93                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
94                                         rc, *text, 0);
95 #endif
96                         }
97                         break;
98
99                 case LDAP_MOD_REPLACE:
100 #ifdef NEW_LOGGING
101                         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal:  replace\n",0,0,0);
102 #else
103                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: replace\n", 0, 0, 0);
104 #endif
105
106                         rc = modify_replace_values( e, mod, get_permissiveModify( op ),
107                                                         text, textbuf, textlen );
108                         if( rc != LDAP_SUCCESS ) {
109 #ifdef NEW_LOGGING
110                                 LDAP_LOG( BACK_LDBM, INFO, 
111                                         "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
112 #else
113                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
114                                         rc, *text, 0);
115 #endif
116                         }
117                         break;
118
119                 case SLAP_MOD_SOFTADD:
120 #ifdef NEW_LOGGING
121                         LDAP_LOG( BACK_LDBM, DETAIL1, 
122                                 "ldbm_modify_internal: softadd\n", 0, 0, 0 );
123 #else
124                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: softadd\n", 0, 0, 0);
125 #endif
126
127                         /* Avoid problems in index_add_mods()
128                          * We need to add index if necessary.
129                          */
130                         mod->sm_op = LDAP_MOD_ADD;
131
132                         rc = modify_add_values( e, mod, get_permissiveModify( op ),
133                                                 text, textbuf, textlen );
134                         if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
135                                 rc = LDAP_SUCCESS;
136                         }
137
138                         if( rc != LDAP_SUCCESS ) {
139 #ifdef NEW_LOGGING
140                                 LDAP_LOG( BACK_LDBM, INFO, 
141                                            "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
142 #else
143                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
144                                         rc, *text, 0);
145 #endif
146                         }
147                         break;
148
149                 default:
150 #ifdef NEW_LOGGING
151                         LDAP_LOG( BACK_LDBM, ERR, 
152                                 "ldbm_modify_internal: invalid op %d\n", mod->sm_op, 0, 0 );
153 #else
154                         Debug(LDAP_DEBUG_ANY, "ldbm_modify_internal: invalid op %d\n",
155                                 mod->sm_op, 0, 0);
156 #endif
157
158                         rc = LDAP_OTHER;
159                         *text = "Invalid modify operation";
160 #ifdef NEW_LOGGING
161                         LDAP_LOG( BACK_LDBM, INFO, 
162                                 "ldbm_modify_internal: %d (%s)\n", rc, *text, 0 );
163 #else
164                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
165                                 rc, *text, 0);
166 #endif
167                 }
168
169                 if ( rc != LDAP_SUCCESS ) {
170                         goto exit;
171                 }
172
173                 /* If objectClass was modified, reset the flags */
174                 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
175                         e->e_ocflags = 0;
176                 }
177
178                 /* check if modified attribute was indexed */
179                 rc = index_is_indexed( op->o_bd, mod->sm_desc );
180                 if ( rc == LDAP_SUCCESS ) {
181                         ap = attr_find( save_attrs, mod->sm_desc );
182                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
183
184                         ap = attr_find( e->e_attrs, mod->sm_desc );
185                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
186                 }
187         }
188
189         /* check that the entry still obeys the schema */
190 #ifndef LDAP_CACHING
191         rc = entry_schema_check( op->o_bd, e, save_attrs, text, textbuf, textlen );
192 #else /* LDAP_CACHING */
193         if ( !op->o_caching_on ) {
194                 rc = entry_schema_check( op->o_bd, e, save_attrs,
195                                 text, textbuf, textlen );
196         } else {
197                 rc = LDAP_SUCCESS; 
198         }
199 #endif /* LDAP_CACHING */
200
201         if ( rc != LDAP_SUCCESS ) {
202 #ifdef NEW_LOGGING
203                 LDAP_LOG( BACK_LDBM, ERR, 
204                         "ldbm_modify_internal: entry failed schema check: %s\n", 
205                         *text, 0, 0 );
206 #else
207                 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
208                         *text, 0, 0 );
209 #endif
210
211                 goto exit;
212         }
213
214         /* check for abandon */
215         if ( op->o_abandon ) {
216                 rc = SLAPD_ABANDON;
217                 goto exit;
218         }
219
220         /* update the indices of the modified attributes */
221
222         /* start with deleting the old index entries */
223         for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
224                 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
225                         rc = index_values( op->o_bd, ap->a_desc,
226 #ifdef SLAP_NVALUES
227                                 ap->a_nvals,
228 #else
229                                 ap->a_vals,
230 #endif
231                                 e->e_id, SLAP_INDEX_DELETE_OP );
232                         if ( rc != LDAP_SUCCESS ) {
233 #ifdef NEW_LOGGING
234                                 LDAP_LOG( BACK_LDBM, ERR,
235                                         "ldbm_modify_internal: Attribute index delete failure\n",
236                                         0, 0, 0 );
237 #else
238                                 Debug( LDAP_DEBUG_ANY,
239                                        "Attribute index delete failure",
240                                        0, 0, 0 );
241 #endif
242                                 goto exit;
243                         }
244                         ap->a_flags &= ~SLAP_ATTR_IXDEL;
245                 }
246         }
247
248         /* add the new index entries */
249         for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
250                 if ( ap->a_flags & SLAP_ATTR_IXADD ) {
251                         rc = index_values( op->o_bd, ap->a_desc,
252 #ifdef SLAP_NVALUES
253                                 ap->a_nvals,
254 #else
255                                 ap->a_vals,
256 #endif
257                                 e->e_id, SLAP_INDEX_ADD_OP );
258                         if ( rc != LDAP_SUCCESS ) {
259 #ifdef NEW_LOGGING
260                                 LDAP_LOG( BACK_LDBM, ERR,
261                                         "ldbm_modify_internal: Attribute index add failure\n",
262                                         0, 0, 0 );
263 #else
264                                 Debug( LDAP_DEBUG_ANY,
265                                        "Attribute index add failure",
266                                        0, 0, 0 );
267 #endif
268                                 goto exit;
269                         }
270                         ap->a_flags &= ~SLAP_ATTR_IXADD;
271                 }
272         }
273
274 exit:
275         if ( rc == LDAP_SUCCESS ) {
276                 attrs_free( save_attrs );
277         } else {
278                 for ( ap = save_attrs; ap; ap = ap->a_next ) {
279                         ap->a_flags = 0;
280                 }
281                 attrs_free( e->e_attrs );
282                 e->e_attrs = save_attrs;
283         }
284
285         return rc;
286 }
287
288 int
289 ldbm_back_modify(
290     Operation   *op,
291     SlapReply   *rs )
292 {
293         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
294         Entry           *matched;
295         Entry           *e;
296         int             manageDSAit = get_manageDSAit( op );
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( op->o_bd, &op->o_req_ndn, &matched )) == NULL ) {
311                 if ( matched != NULL ) {
312                         rs->sr_matched = ch_strdup( matched->e_dn );
313                         rs->sr_ref = is_entry_referral( matched )
314                                 ? get_entry_referrals( op, matched )
315                                 : NULL;
316                         cache_return_entry_r( &li->li_cache, matched );
317                 } else {
318                         rs->sr_ref = referral_rewrite( default_referral,
319                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
320                 }
321
322                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
323                 rs->sr_err = LDAP_REFERRAL;
324                 send_ldap_result( op, rs );
325
326                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
327                 free( (char *)rs->sr_matched );
328
329                 return( -1 );
330         }
331
332 #ifndef LDAP_CACHING
333         if ( !manageDSAit && is_entry_referral( e ) )
334 #else /* LDAP_CACHING */
335         if ( !op->o_caching_on && !manageDSAit && is_entry_referral( e ) )
336 #endif /* LDAP_CACHING */
337         {
338                 /* parent is a referral, don't allow add */
339                 /* parent is an alias, don't allow add */
340                 rs->sr_ref = get_entry_referrals( op, e );
341
342 #ifdef NEW_LOGGING
343                 LDAP_LOG( BACK_LDBM, INFO, 
344                            "ldbm_back_modify: entry (%s) is referral\n", op->o_req_ndn.bv_val, 0, 0 );
345 #else
346                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
347                     0, 0 );
348 #endif
349
350                 rs->sr_err = LDAP_REFERRAL;
351                 rs->sr_matched = e->e_name.bv_val;
352                 send_ldap_result( op, rs );
353
354                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
355
356                 goto error_return;
357         }
358         
359         /* Modify the entry */
360         rs->sr_err = ldbm_modify_internal( op, op->oq_modify.rs_modlist, e,
361                 &rs->sr_text, textbuf, textlen );
362
363         if( rs->sr_err != LDAP_SUCCESS ) {
364                 if( rs->sr_err != SLAPD_ABANDON ) {
365                         send_ldap_result( op, rs );
366                 }
367
368                 goto error_return;
369         }
370
371         /* change the entry itself */
372         if ( id2entry_add( op->o_bd, e ) != 0 ) {
373                 send_ldap_error( op, rs, LDAP_OTHER,
374                         "id2entry failure" );
375                 goto error_return;
376         }
377
378         send_ldap_error( op, rs, LDAP_SUCCESS,
379                 NULL );
380
381         cache_return_entry_w( &li->li_cache, e );
382         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
383         return( 0 );
384
385 error_return:;
386         cache_return_entry_w( &li->li_cache, e );
387         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
388         return( -1 );
389 }