]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/modify.c
Add some initial BDB_INDEX code... needs much work.
[openldap] / servers / slapd / back-bdb / modify.c
1 /* modify.c - bdb backend modify routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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 #include <ac/string.h>
12 #include <ac/time.h>
13
14 #include "back-bdb.h"
15 #include "external.h"
16
17 static int add_values( Entry *e, Modification *mod, char *dn );
18 static int delete_values( Entry *e, Modification *mod, char *dn );
19 static int replace_values( Entry *e, Modification *mod, char *dn );
20
21 int bdb_modify_internal(
22         BackendDB *be,
23         Connection *conn,
24         Operation *op,
25         DB_TXN *tid,
26         Modifications *modlist,
27         Entry *e,
28         const char **text,
29         char *textbuf,
30         size_t textlen )
31 {
32         int rc, err;
33         Modification    *mod;
34         Modifications   *ml;
35         Attribute       *save_attrs;
36
37         Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
38                 e->e_id, e->e_dn, 0);
39
40         if ( !acl_check_modlist( be, conn, op, e, modlist )) {
41                 return LDAP_INSUFFICIENT_ACCESS;
42         }
43
44         save_attrs = e->e_attrs;
45         e->e_attrs = attrs_dup( e->e_attrs );
46
47         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
48                 mod = &ml->sml_mod;
49
50                 switch ( mod->sm_op ) {
51                 case LDAP_MOD_ADD:
52                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: add\n", 0, 0, 0);
53                         err = add_values( e, mod, op->o_ndn );
54
55                         if( err != LDAP_SUCCESS ) {
56                                 *text = "modify: add values failed";
57                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
58                                         err, *text, 0);
59                         }
60                         break;
61
62                 case LDAP_MOD_DELETE:
63                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: delete\n", 0, 0, 0);
64                         err = delete_values( e, mod, op->o_ndn );
65                         assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
66                         if( err != LDAP_SUCCESS ) {
67                                 *text = "modify: delete values failed";
68                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
69                                         err, *text, 0);
70                         }
71                         break;
72
73                 case LDAP_MOD_REPLACE:
74                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: replace\n", 0, 0, 0);
75                         err = replace_values( e, mod, op->o_ndn );
76                         assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
77                         if( err != LDAP_SUCCESS ) {
78                                 *text = "modify: replace values failed";
79                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
80                                         err, *text, 0);
81                         }
82                         break;
83
84                 case SLAP_MOD_SOFTADD:
85                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: softadd\n", 0, 0, 0);
86                         /* Avoid problems in index_add_mods()
87                          * We need to add index if necessary.
88                          */
89                         mod->sm_op = LDAP_MOD_ADD;
90                         err = add_values( e, mod, op->o_ndn );
91
92                         if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
93                                 err = LDAP_SUCCESS;
94                         }
95
96                         if( err != LDAP_SUCCESS ) {
97                                 *text = "modify: (soft)add values failed";
98                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
99                                         err, *text, 0);
100                         }
101                         break;
102
103                 default:
104                         Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
105                                 mod->sm_op, 0, 0);
106                         *text = "Invalid modify operation";
107                         err = LDAP_OTHER;
108                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
109                                 err, *text, 0);
110                 }
111
112                 if ( err != LDAP_SUCCESS ) {
113                         attrs_free( e->e_attrs );
114                         e->e_attrs = save_attrs;
115                         /* unlock entry, delete from cache */
116                         return err; 
117                 }
118         }
119
120         /* check that the entry still obeys the schema */
121         rc = entry_schema_check( e, save_attrs, text, textbuf, textlen );
122         if ( rc != LDAP_SUCCESS ) {
123                 attrs_free( e->e_attrs );
124                 e->e_attrs = save_attrs;
125                 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
126                         *text, 0, 0 );
127                 return rc;
128         }
129
130 #ifdef BDB_INDEX
131         /* delete indices for old attributes */
132         rc = bdb_index_entry_del( be, tid, e, save_attrs);
133         if ( rc != LDAP_SUCCESS ) {
134                 attrs_free( e->e_attrs );
135                 e->e_attrs = save_attrs;
136                 Debug( LDAP_DEBUG_ANY, "entry index delete failed: %s\n",
137                         *text, 0, 0 );
138                 return rc;
139         }
140
141         /* add indices for new attributes */
142         rc = bdb_index_entry_add( be, tid, e, e->e_attrs); 
143         if ( rc != LDAP_SUCCESS ) {
144                 attrs_free( e->e_attrs );
145                 e->e_attrs = save_attrs;
146                 Debug( LDAP_DEBUG_ANY, "entry index add failed: %s\n",
147                         *text, 0, 0 );
148                 return rc;
149         }
150 #endif
151
152         attrs_free( save_attrs );
153         return rc;
154 }
155
156
157 int
158 bdb_modify(
159         BackendDB       *be,
160         Connection      *conn,
161         Operation       *op,
162         const char      *dn,
163         const char      *ndn,
164         Modifications   *modlist )
165 {
166         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
167         int rc;
168         Entry           *matched;
169         Entry           *e;
170         int             manageDSAit = get_manageDSAit( op );
171         const char *text = NULL;
172         char textbuf[SLAP_TEXT_BUFLEN];
173         size_t textlen = sizeof textbuf;
174         DB_TXN  *ltid;
175         struct bdb_op_info opinfo;
176
177         Debug( LDAP_DEBUG_ARGS, "bdb_modify: %s\n", dn, 0, 0 );
178
179         if (0) {
180 retry:  /* transaction retry */
181                 Debug(LDAP_DEBUG_TRACE,
182                         "bdb_modify: retrying...\n", 0, 0, 0);
183                 rc = txn_abort( ltid );
184                 ltid = NULL;
185                 op->o_private = NULL;
186                 if( rc != 0 ) {
187                         rc = LDAP_OTHER;
188                         text = "internal error";
189                         goto return_results;
190                 }
191         }
192
193         /* begin transaction */
194         rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 0 );
195         text = NULL;
196         if( rc != 0 ) {
197                 Debug( LDAP_DEBUG_TRACE,
198                         "bdb_modify: txn_begin failed: %s (%d)\n",
199                         db_strerror(rc), rc, 0 );
200                 rc = LDAP_OTHER;
201                 text = "internal error";
202                 goto return_results;
203         }
204
205         opinfo.boi_bdb = be;
206         opinfo.boi_txn = ltid;
207         opinfo.boi_err = 0;
208         op->o_private = &opinfo;
209
210         /* get entry */
211         rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
212
213         if ( rc != 0 ) {
214                 Debug( LDAP_DEBUG_TRACE,
215                         "bdb_modify: dn2entry failed (%d)\n",
216                         rc, 0, 0 );
217                 switch( rc ) {
218                 case DB_LOCK_DEADLOCK:
219                 case DB_LOCK_NOTGRANTED:
220                         goto retry;
221                 case DB_NOTFOUND:
222                         break;
223                 default:
224                         rc = LDAP_OTHER;
225                 }
226                 text = "internal error";
227                 goto return_results;
228         }
229
230         /* acquire and lock entry */
231         if ( e == NULL ) {
232                 char* matched_dn = NULL;
233                 struct berval **refs = NULL;
234
235                 if ( matched != NULL ) {
236                         matched_dn = ch_strdup( matched->e_dn );
237                         refs = is_entry_referral( matched )
238                                 ? get_entry_referrals( be, conn, op, matched )
239                                 : NULL;
240                         bdb_entry_return( be, matched );
241                         matched = NULL;
242
243                 } else {
244                         refs = default_referral;
245                 }
246
247                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
248                         matched_dn, NULL, refs, NULL );
249
250                 if ( matched != NULL ) {
251                         ber_bvecfree( refs );
252                         free( matched_dn );
253                 }
254
255                 return rc;
256         }
257
258         if ( !manageDSAit && is_entry_referral( e ) ) {
259                 /* parent is a referral, don't allow add */
260                 /* parent is an alias, don't allow add */
261                 struct berval **refs = get_entry_referrals( be,
262                         conn, op, e );
263
264                 Debug( LDAP_DEBUG_TRACE,
265                         "bdb_modify: entry is referral\n",
266                         0, 0, 0 );
267
268                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
269                         e->e_dn, NULL, refs, NULL );
270
271                 ber_bvecfree( refs );
272                 goto done;
273         }
274         
275         /* Modify the entry */
276         rc = bdb_modify_internal( be, conn, op, ltid, modlist, e,
277                 &text, textbuf, textlen );
278
279         if( rc != LDAP_SUCCESS ) {
280                 Debug( LDAP_DEBUG_TRACE,
281                         "bdb_modify: modify failed (%d)\n",
282                         rc, 0, 0 );
283                 switch( rc ) {
284                 case DB_LOCK_DEADLOCK:
285                 case DB_LOCK_NOTGRANTED:
286                         goto retry;
287                 }
288                 goto return_results;
289         }
290
291         /* change the entry itself */
292         rc = bdb_id2entry_update( be, ltid, e );
293         if ( rc != 0 ) {
294                 Debug( LDAP_DEBUG_TRACE,
295                         "bdb_modify: id2entry update failed (%d)\n",
296                         rc, 0, 0 );
297                 switch( rc ) {
298                 case DB_LOCK_DEADLOCK:
299                 case DB_LOCK_NOTGRANTED:
300                         goto retry;
301                 }
302                 text = "entry update failed";
303                 goto return_results;
304         }
305
306         rc = txn_commit( ltid, 0 );
307         ltid = NULL;
308         op->o_private = NULL;
309
310         if( rc != 0 ) {
311                 Debug( LDAP_DEBUG_TRACE,
312                         "bdb_modify: txn_commit failed: %s (%d)\n",
313                         db_strerror(rc), rc, 0 );
314                 rc = LDAP_OTHER;
315                 text = "commit failed";
316         } else {
317                 Debug( LDAP_DEBUG_TRACE,
318                         "bdb_modify: updated id=%08x dn=\"%s\"\n",
319                         e->e_id, e->e_dn, 0 );
320                 rc = LDAP_SUCCESS;
321                 text = NULL;
322         }
323
324 return_results:
325         send_ldap_result( conn, op, rc,
326                 NULL, text, NULL, NULL );
327
328         if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
329                 ldap_pvt_thread_yield();
330                 txn_checkpoint( bdb->bi_dbenv,
331                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
332         }
333
334 done:
335         if( ltid != NULL ) {
336                 txn_abort( ltid );
337                 op->o_private = NULL;
338         }
339
340         if( e != NULL ) {
341                 bdb_entry_return( be, e );
342         }
343         return rc;
344 }
345
346 static int
347 add_values(
348         Entry   *e,
349         Modification    *mod,
350         char    *dn
351 )
352 {
353         int             i;
354         Attribute       *a;
355
356         /* char *desc = mod->sm_desc->ad_cname->bv_val; */
357         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
358
359         a = attr_find( e->e_attrs, mod->sm_desc );
360
361         /* check if the values we're adding already exist */
362         if ( a != NULL ) {
363                 if( mr == NULL || !mr->smr_match ) {
364                         /* do not allow add of additional attribute
365                                 if no equality rule exists */
366                         return LDAP_INAPPROPRIATE_MATCHING;
367                 }
368
369                 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
370                         int rc;
371                         int j;
372                         const char *text = NULL;
373                         struct berval *asserted;
374
375                         rc = value_normalize( mod->sm_desc,
376                                 SLAP_MR_EQUALITY,
377                                 mod->sm_bvalues[i],
378                                 &asserted,
379                                 &text );
380
381                         if( rc != LDAP_SUCCESS ) return rc;
382
383                         for ( j = 0; a->a_vals[j] != NULL; j++ ) {
384                                 int match;
385                                 int rc = value_match( &match, mod->sm_desc, mr,
386                                         SLAP_MR_MODIFY_MATCHING,
387                                         a->a_vals[j], asserted, &text );
388
389                                 if( rc == LDAP_SUCCESS && match == 0 ) {
390                                         ber_bvfree( asserted );
391                                         return LDAP_TYPE_OR_VALUE_EXISTS;
392                                 }
393                         }
394
395                         ber_bvfree( asserted );
396                 }
397         }
398
399         /* no - add them */
400         if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
401                 /* this should return result return of attr_merge */
402                 return LDAP_OTHER;
403         }
404
405         return LDAP_SUCCESS;
406 }
407
408 static int
409 delete_values(
410         Entry   *e,
411         Modification    *mod,
412         char    *dn
413 )
414 {
415         int             i, j, k, found;
416         Attribute       *a;
417         char *desc = mod->sm_desc->ad_cname->bv_val;
418         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
419
420         /* delete the entire attribute */
421         if ( mod->sm_bvalues == NULL ) {
422                 Debug( LDAP_DEBUG_TRACE,
423                         "bdb_modify_delete: removing entire attribute %s\n",
424                         desc, 0, 0 );
425                 return attr_delete( &e->e_attrs, mod->sm_desc )
426                         ? LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS;
427         }
428
429         if( mr == NULL || !mr->smr_match ) {
430                 /* disallow specific attributes from being deleted if
431                         no equality rule */
432                 return LDAP_INAPPROPRIATE_MATCHING;
433         }
434
435         /* delete specific values - find the attribute first */
436         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
437                 Debug( LDAP_DEBUG_TRACE,
438                         "bdb_modify_delete: could not find attribute %s\n",
439                         desc, 0, 0 );
440                 return LDAP_NO_SUCH_ATTRIBUTE;
441         }
442
443         /* find each value to delete */
444         for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
445                 int rc;
446                 const char *text = NULL;
447
448                 struct berval *asserted;
449
450                 rc = value_normalize( mod->sm_desc,
451                         SLAP_MR_EQUALITY,
452                         mod->sm_bvalues[i],
453                         &asserted,
454                         &text );
455
456                 if( rc != LDAP_SUCCESS ) return rc;
457
458                 found = 0;
459                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
460                         int match;
461                         int rc = value_match( &match, mod->sm_desc, mr,
462                                 SLAP_MR_MODIFY_MATCHING,
463                                 a->a_vals[j], asserted, &text );
464
465                         if( rc == LDAP_SUCCESS && match != 0 ) {
466                                 continue;
467                         }
468
469                         /* found a matching value */
470                         found = 1;
471
472                         /* delete it */
473                         ber_bvfree( a->a_vals[j] );
474                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
475                                 a->a_vals[k - 1] = a->a_vals[k];
476                         }
477                         a->a_vals[k - 1] = NULL;
478
479                         break;
480                 }
481
482                 ber_bvfree( asserted );
483
484                 /* looked through them all w/o finding it */
485                 if ( ! found ) {
486                         Debug( LDAP_DEBUG_TRACE,
487                                 "bdb_modify_delete: could not find value for attr %s\n",
488                                 desc, 0, 0 );
489                         return LDAP_NO_SUCH_ATTRIBUTE;
490                 }
491         }
492
493         /* if no values remain, delete the entire attribute */
494         if ( a->a_vals[0] == NULL ) {
495                 Debug( LDAP_DEBUG_TRACE,
496                         "bdb_modify_delete: removing entire attribute %s\n",
497                         desc, 0, 0 );
498                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
499                         return LDAP_NO_SUCH_ATTRIBUTE;
500                 }
501         }
502
503         return LDAP_SUCCESS;
504 }
505
506 static int
507 replace_values(
508         Entry   *e,
509         Modification    *mod,
510         char    *dn
511 )
512 {
513         int rc = attr_delete( &e->e_attrs, mod->sm_desc );
514
515         if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
516                 return rc;
517         }
518
519         if ( mod->sm_bvalues != NULL &&
520                 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )
521         {
522                 return LDAP_OTHER;
523         }
524
525         return LDAP_SUCCESS;
526 }