]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/modify.c
BDB_INDEX code does no harm (but no good yet, not used by filters yet).
[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         /* delete indices for old attributes */
131         rc = bdb_index_entry_del( be, tid, e, save_attrs);
132         if ( rc != LDAP_SUCCESS ) {
133                 attrs_free( e->e_attrs );
134                 e->e_attrs = save_attrs;
135                 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
136                         0, 0, 0 );
137                 return rc;
138         }
139
140         /* add indices for new attributes */
141         rc = bdb_index_entry_add( be, tid, e, e->e_attrs); 
142         if ( rc != LDAP_SUCCESS ) {
143                 attrs_free( e->e_attrs );
144                 e->e_attrs = save_attrs;
145                 Debug( LDAP_DEBUG_ANY, "entry index add failed!\n",
146                         0, 0, 0 );
147                 return rc;
148         }
149
150         attrs_free( save_attrs );
151         return rc;
152 }
153
154
155 int
156 bdb_modify(
157         BackendDB       *be,
158         Connection      *conn,
159         Operation       *op,
160         const char      *dn,
161         const char      *ndn,
162         Modifications   *modlist )
163 {
164         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
165         int rc;
166         Entry           *matched;
167         Entry           *e;
168         int             manageDSAit = get_manageDSAit( op );
169         const char *text = NULL;
170         char textbuf[SLAP_TEXT_BUFLEN];
171         size_t textlen = sizeof textbuf;
172         DB_TXN  *ltid;
173         struct bdb_op_info opinfo;
174
175         Debug( LDAP_DEBUG_ARGS, "bdb_modify: %s\n", dn, 0, 0 );
176
177         if (0) {
178 retry:  /* transaction retry */
179                 Debug(LDAP_DEBUG_TRACE,
180                         "bdb_modify: retrying...\n", 0, 0, 0);
181                 rc = txn_abort( ltid );
182                 ltid = NULL;
183                 op->o_private = NULL;
184                 if( rc != 0 ) {
185                         rc = LDAP_OTHER;
186                         text = "internal error";
187                         goto return_results;
188                 }
189         }
190
191         /* begin transaction */
192         rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 0 );
193         text = NULL;
194         if( rc != 0 ) {
195                 Debug( LDAP_DEBUG_TRACE,
196                         "bdb_modify: txn_begin failed: %s (%d)\n",
197                         db_strerror(rc), rc, 0 );
198                 rc = LDAP_OTHER;
199                 text = "internal error";
200                 goto return_results;
201         }
202
203         opinfo.boi_bdb = be;
204         opinfo.boi_txn = ltid;
205         opinfo.boi_err = 0;
206         op->o_private = &opinfo;
207
208         /* get entry */
209         rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
210
211         if ( rc != 0 ) {
212                 Debug( LDAP_DEBUG_TRACE,
213                         "bdb_modify: dn2entry failed (%d)\n",
214                         rc, 0, 0 );
215                 switch( rc ) {
216                 case DB_LOCK_DEADLOCK:
217                 case DB_LOCK_NOTGRANTED:
218                         goto retry;
219                 case DB_NOTFOUND:
220                         break;
221                 default:
222                         rc = LDAP_OTHER;
223                 }
224                 text = "internal error";
225                 goto return_results;
226         }
227
228         /* acquire and lock entry */
229         if ( e == NULL ) {
230                 char* matched_dn = NULL;
231                 struct berval **refs = NULL;
232
233                 if ( matched != NULL ) {
234                         matched_dn = ch_strdup( matched->e_dn );
235                         refs = is_entry_referral( matched )
236                                 ? get_entry_referrals( be, conn, op, matched )
237                                 : NULL;
238                         bdb_entry_return( be, matched );
239                         matched = NULL;
240
241                 } else {
242                         refs = default_referral;
243                 }
244
245                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
246                         matched_dn, NULL, refs, NULL );
247
248                 if ( matched != NULL ) {
249                         ber_bvecfree( refs );
250                         free( matched_dn );
251                 }
252
253                 return rc;
254         }
255
256         if ( !manageDSAit && is_entry_referral( e ) ) {
257                 /* parent is a referral, don't allow add */
258                 /* parent is an alias, don't allow add */
259                 struct berval **refs = get_entry_referrals( be,
260                         conn, op, e );
261
262                 Debug( LDAP_DEBUG_TRACE,
263                         "bdb_modify: entry is referral\n",
264                         0, 0, 0 );
265
266                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
267                         e->e_dn, NULL, refs, NULL );
268
269                 ber_bvecfree( refs );
270                 goto done;
271         }
272         
273         /* Modify the entry */
274         rc = bdb_modify_internal( be, conn, op, ltid, modlist, e,
275                 &text, textbuf, textlen );
276
277         if( rc != LDAP_SUCCESS ) {
278                 Debug( LDAP_DEBUG_TRACE,
279                         "bdb_modify: modify failed (%d)\n",
280                         rc, 0, 0 );
281                 switch( rc ) {
282                 case DB_LOCK_DEADLOCK:
283                 case DB_LOCK_NOTGRANTED:
284                         goto retry;
285                 }
286                 goto return_results;
287         }
288
289         /* change the entry itself */
290         rc = bdb_id2entry_update( be, ltid, e );
291         if ( rc != 0 ) {
292                 Debug( LDAP_DEBUG_TRACE,
293                         "bdb_modify: id2entry update failed (%d)\n",
294                         rc, 0, 0 );
295                 switch( rc ) {
296                 case DB_LOCK_DEADLOCK:
297                 case DB_LOCK_NOTGRANTED:
298                         goto retry;
299                 }
300                 text = "entry update failed";
301                 goto return_results;
302         }
303
304         rc = txn_commit( ltid, 0 );
305         ltid = NULL;
306         op->o_private = NULL;
307
308         if( rc != 0 ) {
309                 Debug( LDAP_DEBUG_TRACE,
310                         "bdb_modify: txn_commit failed: %s (%d)\n",
311                         db_strerror(rc), rc, 0 );
312                 rc = LDAP_OTHER;
313                 text = "commit failed";
314         } else {
315                 Debug( LDAP_DEBUG_TRACE,
316                         "bdb_modify: updated id=%08x dn=\"%s\"\n",
317                         e->e_id, e->e_dn, 0 );
318                 rc = LDAP_SUCCESS;
319                 text = NULL;
320         }
321
322 return_results:
323         send_ldap_result( conn, op, rc,
324                 NULL, text, NULL, NULL );
325
326         if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
327                 ldap_pvt_thread_yield();
328                 txn_checkpoint( bdb->bi_dbenv,
329                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
330         }
331
332 done:
333         if( ltid != NULL ) {
334                 txn_abort( ltid );
335                 op->o_private = NULL;
336         }
337
338         if( e != NULL ) {
339                 bdb_entry_return( be, e );
340         }
341         return rc;
342 }
343
344 static int
345 add_values(
346         Entry   *e,
347         Modification    *mod,
348         char    *dn
349 )
350 {
351         int             i;
352         Attribute       *a;
353
354         /* char *desc = mod->sm_desc->ad_cname->bv_val; */
355         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
356
357         a = attr_find( e->e_attrs, mod->sm_desc );
358
359         /* check if the values we're adding already exist */
360         if ( a != NULL ) {
361                 if( mr == NULL || !mr->smr_match ) {
362                         /* do not allow add of additional attribute
363                                 if no equality rule exists */
364                         return LDAP_INAPPROPRIATE_MATCHING;
365                 }
366
367                 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
368                         int rc;
369                         int j;
370                         const char *text = NULL;
371                         struct berval *asserted;
372
373                         rc = value_normalize( mod->sm_desc,
374                                 SLAP_MR_EQUALITY,
375                                 mod->sm_bvalues[i],
376                                 &asserted,
377                                 &text );
378
379                         if( rc != LDAP_SUCCESS ) return rc;
380
381                         for ( j = 0; a->a_vals[j] != NULL; j++ ) {
382                                 int match;
383                                 int rc = value_match( &match, mod->sm_desc, mr,
384                                         SLAP_MR_MODIFY_MATCHING,
385                                         a->a_vals[j], asserted, &text );
386
387                                 if( rc == LDAP_SUCCESS && match == 0 ) {
388                                         ber_bvfree( asserted );
389                                         return LDAP_TYPE_OR_VALUE_EXISTS;
390                                 }
391                         }
392
393                         ber_bvfree( asserted );
394                 }
395         }
396
397         /* no - add them */
398         if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
399                 /* this should return result return of attr_merge */
400                 return LDAP_OTHER;
401         }
402
403         return LDAP_SUCCESS;
404 }
405
406 static int
407 delete_values(
408         Entry   *e,
409         Modification    *mod,
410         char    *dn
411 )
412 {
413         int             i, j, k, found;
414         Attribute       *a;
415         char *desc = mod->sm_desc->ad_cname->bv_val;
416         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
417
418         /* delete the entire attribute */
419         if ( mod->sm_bvalues == NULL ) {
420                 Debug( LDAP_DEBUG_TRACE,
421                         "bdb_modify_delete: removing entire attribute %s\n",
422                         desc, 0, 0 );
423                 return attr_delete( &e->e_attrs, mod->sm_desc )
424                         ? LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS;
425         }
426
427         if( mr == NULL || !mr->smr_match ) {
428                 /* disallow specific attributes from being deleted if
429                         no equality rule */
430                 return LDAP_INAPPROPRIATE_MATCHING;
431         }
432
433         /* delete specific values - find the attribute first */
434         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
435                 Debug( LDAP_DEBUG_TRACE,
436                         "bdb_modify_delete: could not find attribute %s\n",
437                         desc, 0, 0 );
438                 return LDAP_NO_SUCH_ATTRIBUTE;
439         }
440
441         /* find each value to delete */
442         for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
443                 int rc;
444                 const char *text = NULL;
445
446                 struct berval *asserted;
447
448                 rc = value_normalize( mod->sm_desc,
449                         SLAP_MR_EQUALITY,
450                         mod->sm_bvalues[i],
451                         &asserted,
452                         &text );
453
454                 if( rc != LDAP_SUCCESS ) return rc;
455
456                 found = 0;
457                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
458                         int match;
459                         int rc = value_match( &match, mod->sm_desc, mr,
460                                 SLAP_MR_MODIFY_MATCHING,
461                                 a->a_vals[j], asserted, &text );
462
463                         if( rc == LDAP_SUCCESS && match != 0 ) {
464                                 continue;
465                         }
466
467                         /* found a matching value */
468                         found = 1;
469
470                         /* delete it */
471                         ber_bvfree( a->a_vals[j] );
472                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
473                                 a->a_vals[k - 1] = a->a_vals[k];
474                         }
475                         a->a_vals[k - 1] = NULL;
476
477                         break;
478                 }
479
480                 ber_bvfree( asserted );
481
482                 /* looked through them all w/o finding it */
483                 if ( ! found ) {
484                         Debug( LDAP_DEBUG_TRACE,
485                                 "bdb_modify_delete: could not find value for attr %s\n",
486                                 desc, 0, 0 );
487                         return LDAP_NO_SUCH_ATTRIBUTE;
488                 }
489         }
490
491         /* if no values remain, delete the entire attribute */
492         if ( a->a_vals[0] == NULL ) {
493                 Debug( LDAP_DEBUG_TRACE,
494                         "bdb_modify_delete: removing entire attribute %s\n",
495                         desc, 0, 0 );
496                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
497                         return LDAP_NO_SUCH_ATTRIBUTE;
498                 }
499         }
500
501         return LDAP_SUCCESS;
502 }
503
504 static int
505 replace_values(
506         Entry   *e,
507         Modification    *mod,
508         char    *dn
509 )
510 {
511         int rc = attr_delete( &e->e_attrs, mod->sm_desc );
512
513         if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
514                 return rc;
515         }
516
517         if ( mod->sm_bvalues != NULL &&
518                 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )
519         {
520                 return LDAP_OTHER;
521         }
522
523         return LDAP_SUCCESS;
524 }