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