]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
honor RFC4528 assertion control for LDAP add operations (ITS#5861)
[openldap] / servers / slapd / back-bdb / add.c
1 /* add.c - ldap BerkeleyDB back-end add routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2008 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #include "back-bdb.h"
23
24 int
25 bdb_add(Operation *op, SlapReply *rs )
26 {
27         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
28         struct berval   pdn;
29         Entry           *p = NULL, *oe = op->ora_e;
30         EntryInfo       *ei;
31         char textbuf[SLAP_TEXT_BUFLEN];
32         size_t textlen = sizeof textbuf;
33         AttributeDescription *children = slap_schema.si_ad_children;
34         AttributeDescription *entry = slap_schema.si_ad_entry;
35         DB_TXN          *ltid = NULL, *lt2, *rtxn;
36         ID eid = NOID;
37         struct bdb_op_info opinfo = {{{ 0 }}};
38         int subentry;
39         DB_LOCK         lock;
40
41         int             num_retries = 0;
42         int             success;
43
44         LDAPControl **postread_ctrl = NULL;
45         LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
46         int num_ctrls = 0;
47
48 #ifdef LDAP_X_TXN
49         int settle = 0;
50 #endif
51
52         Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_add) ": %s\n",
53                 op->oq_add.rs_e->e_name.bv_val, 0, 0);
54
55 #ifdef LDAP_X_TXN
56         if( op->o_txnSpec ) {
57                 /* acquire connection lock */
58                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
59                 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
60                         rs->sr_text = "invalid transaction identifier";
61                         rs->sr_err = LDAP_X_TXN_ID_INVALID;
62                         goto txnReturn;
63                 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
64                         settle=1;
65                         goto txnReturn;
66                 }
67
68                 if( op->o_conn->c_txn_backend == NULL ) {
69                         op->o_conn->c_txn_backend = op->o_bd;
70
71                 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
72                         rs->sr_text = "transaction cannot span multiple database contexts";
73                         rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
74                         goto txnReturn;
75                 }
76
77                 /* insert operation into transaction */
78
79                 rs->sr_text = "transaction specified";
80                 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
81
82 txnReturn:
83                 /* release connection lock */
84                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
85
86                 if( !settle ) {
87                         send_ldap_result( op, rs );
88                         return rs->sr_err;
89                 }
90         }
91 #endif
92
93         ctrls[num_ctrls] = 0;
94
95         /* check entry's schema */
96         rs->sr_err = entry_schema_check( op, op->oq_add.rs_e, NULL,
97                 get_relax(op), 1, &rs->sr_text, textbuf, textlen );
98         if ( rs->sr_err != LDAP_SUCCESS ) {
99                 Debug( LDAP_DEBUG_TRACE,
100                         LDAP_XSTRING(bdb_add) ": entry failed schema check: "
101                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
102                 goto return_results;
103         }
104
105         /* add opattrs to shadow as well, only missing attrs will actually
106          * be added; helps compatibility with older OL versions */
107         rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
108         if ( rs->sr_err != LDAP_SUCCESS ) {
109                 Debug( LDAP_DEBUG_TRACE,
110                         LDAP_XSTRING(bdb_add) ": entry failed op attrs add: "
111                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
112                 goto return_results;
113         }
114
115         if ( get_assert( op ) &&
116                 ( test_filter( op, op->ora_e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
117         {
118                 rs->sr_err = LDAP_ASSERTION_FAILED;
119                 goto return_results;
120         }
121
122         subentry = is_entry_subentry( op->oq_add.rs_e );
123
124         /* Get our reader TXN */
125         rs->sr_err = bdb_reader_get( op, bdb->bi_dbenv, &rtxn );
126
127         if( 0 ) {
128 retry:  /* transaction retry */
129                 if( p ) {
130                         /* free parent and reader lock */
131                         if ( p != (Entry *)&slap_entry_root ) {
132                                 bdb_unlocked_cache_return_entry_r( bdb, p );
133                         }
134                         p = NULL;
135                 }
136                 rs->sr_err = TXN_ABORT( ltid );
137                 ltid = NULL;
138                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
139                 opinfo.boi_oe.oe_key = NULL;
140                 op->o_do_not_cache = opinfo.boi_acl_cache;
141                 if( rs->sr_err != 0 ) {
142                         rs->sr_err = LDAP_OTHER;
143                         rs->sr_text = "internal error";
144                         goto return_results;
145                 }
146                 if ( op->o_abandon ) {
147                         rs->sr_err = SLAPD_ABANDON;
148                         goto return_results;
149                 }
150                 bdb_trans_backoff( ++num_retries );
151         }
152
153         /* begin transaction */
154         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
155                 bdb->bi_db_opflags );
156         rs->sr_text = NULL;
157         if( rs->sr_err != 0 ) {
158                 Debug( LDAP_DEBUG_TRACE,
159                         LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
160                         db_strerror(rs->sr_err), rs->sr_err, 0 );
161                 rs->sr_err = LDAP_OTHER;
162                 rs->sr_text = "internal error";
163                 goto return_results;
164         }
165
166         opinfo.boi_oe.oe_key = bdb;
167         opinfo.boi_txn = ltid;
168         opinfo.boi_err = 0;
169         opinfo.boi_acl_cache = op->o_do_not_cache;
170         LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
171
172         /*
173          * Get the parent dn and see if the corresponding entry exists.
174          */
175         if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
176                 pdn = slap_empty_bv;
177         } else {
178                 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
179         }
180
181         /* get entry or parent */
182         rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
183                 1, &lock );
184         switch( rs->sr_err ) {
185         case 0:
186                 rs->sr_err = LDAP_ALREADY_EXISTS;
187                 goto return_results;
188         case DB_NOTFOUND:
189                 break;
190         case DB_LOCK_DEADLOCK:
191         case DB_LOCK_NOTGRANTED:
192                 goto retry;
193         case LDAP_BUSY:
194                 rs->sr_text = "ldap server busy";
195                 goto return_results;
196         default:
197                 rs->sr_err = LDAP_OTHER;
198                 rs->sr_text = "internal error";
199                 goto return_results;
200         }
201
202         p = ei->bei_e;
203         if ( !p )
204                 p = (Entry *)&slap_entry_root;
205
206         if ( !bvmatch( &pdn, &p->e_nname ) ) {
207                 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
208                         op->o_tmpmemctx );
209                 rs->sr_ref = is_entry_referral( p )
210                         ? get_entry_referrals( op, p )
211                         : NULL;
212                 bdb_unlocked_cache_return_entry_r( bdb, p );
213                 p = NULL;
214                 Debug( LDAP_DEBUG_TRACE,
215                         LDAP_XSTRING(bdb_add) ": parent "
216                         "does not exist\n", 0, 0, 0 );
217
218                 rs->sr_err = LDAP_REFERRAL;
219                 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
220                 goto return_results;
221         }
222
223         rs->sr_err = access_allowed( op, p,
224                 children, NULL, ACL_WADD, NULL );
225
226         if ( ! rs->sr_err ) {
227                 switch( opinfo.boi_err ) {
228                 case DB_LOCK_DEADLOCK:
229                 case DB_LOCK_NOTGRANTED:
230                         goto retry;
231                 }
232
233                 Debug( LDAP_DEBUG_TRACE,
234                         LDAP_XSTRING(bdb_add) ": no write access to parent\n",
235                         0, 0, 0 );
236                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
237                 rs->sr_text = "no write access to parent";
238                 goto return_results;;
239         }
240
241         if ( p != (Entry *)&slap_entry_root ) {
242                 if ( is_entry_subentry( p ) ) {
243                         /* parent is a subentry, don't allow add */
244                         Debug( LDAP_DEBUG_TRACE,
245                                 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
246                                 0, 0, 0 );
247                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
248                         rs->sr_text = "parent is a subentry";
249                         goto return_results;;
250                 }
251
252                 if ( is_entry_alias( p ) ) {
253                         /* parent is an alias, don't allow add */
254                         Debug( LDAP_DEBUG_TRACE,
255                                 LDAP_XSTRING(bdb_add) ": parent is alias\n",
256                                 0, 0, 0 );
257                         rs->sr_err = LDAP_ALIAS_PROBLEM;
258                         rs->sr_text = "parent is an alias";
259                         goto return_results;;
260                 }
261
262                 if ( is_entry_referral( p ) ) {
263                         /* parent is a referral, don't allow add */
264                         rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
265                                 op->o_tmpmemctx );
266                         rs->sr_ref = get_entry_referrals( op, p );
267                         bdb_unlocked_cache_return_entry_r( bdb, p );
268                         p = NULL;
269                         Debug( LDAP_DEBUG_TRACE,
270                                 LDAP_XSTRING(bdb_add) ": parent is referral\n",
271                                 0, 0, 0 );
272
273                         rs->sr_err = LDAP_REFERRAL;
274                         rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
275                         goto return_results;
276                 }
277
278         }
279
280         if ( subentry ) {
281                 /* FIXME: */
282                 /* parent must be an administrative point of the required kind */
283         }
284
285         /* free parent and reader lock */
286         if ( p != (Entry *)&slap_entry_root ) {
287                 bdb_unlocked_cache_return_entry_r( bdb, p );
288         }
289         p = NULL;
290
291         rs->sr_err = access_allowed( op, op->oq_add.rs_e,
292                 entry, NULL, ACL_WADD, NULL );
293
294         if ( ! rs->sr_err ) {
295                 switch( opinfo.boi_err ) {
296                 case DB_LOCK_DEADLOCK:
297                 case DB_LOCK_NOTGRANTED:
298                         goto retry;
299                 }
300
301                 Debug( LDAP_DEBUG_TRACE,
302                         LDAP_XSTRING(bdb_add) ": no write access to entry\n",
303                         0, 0, 0 );
304                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
305                 rs->sr_text = "no write access to entry";
306                 goto return_results;;
307         }
308
309         /* 
310          * Check ACL for attribute write access
311          */
312         if (!acl_check_modlist(op, oe, op->ora_modlist)) {
313                 switch( opinfo.boi_err ) {
314                 case DB_LOCK_DEADLOCK:
315                 case DB_LOCK_NOTGRANTED:
316                         goto retry;
317                 }
318
319                 Debug( LDAP_DEBUG_TRACE,
320                         LDAP_XSTRING(bdb_add) ": no write access to attribute\n",
321                         0, 0, 0 );
322                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
323                 rs->sr_text = "no write access to attribute";
324                 goto return_results;;
325         }
326
327         if ( eid == NOID ) {
328                 rs->sr_err = bdb_next_id( op->o_bd, &eid );
329                 if( rs->sr_err != 0 ) {
330                         Debug( LDAP_DEBUG_TRACE,
331                                 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
332                                 rs->sr_err, 0, 0 );
333                         rs->sr_err = LDAP_OTHER;
334                         rs->sr_text = "internal error";
335                         goto return_results;
336                 }
337                 op->oq_add.rs_e->e_id = eid;
338         }
339
340         /* nested transaction */
341         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, 
342                 bdb->bi_db_opflags );
343         rs->sr_text = NULL;
344         if( rs->sr_err != 0 ) {
345                 Debug( LDAP_DEBUG_TRACE,
346                         LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
347                         "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
348                 rs->sr_err = LDAP_OTHER;
349                 rs->sr_text = "internal error";
350                 goto return_results;
351         }
352
353         /* dn2id index */
354         rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
355         if ( rs->sr_err != 0 ) {
356                 Debug( LDAP_DEBUG_TRACE,
357                         LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
358                         db_strerror(rs->sr_err), rs->sr_err, 0 );
359
360                 switch( rs->sr_err ) {
361                 case DB_LOCK_DEADLOCK:
362                 case DB_LOCK_NOTGRANTED:
363                         goto retry;
364                 case DB_KEYEXIST:
365                         rs->sr_err = LDAP_ALREADY_EXISTS;
366                         break;
367                 default:
368                         rs->sr_err = LDAP_OTHER;
369                 }
370                 goto return_results;
371         }
372
373         /* attribute indexes */
374         rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
375         if ( rs->sr_err != LDAP_SUCCESS ) {
376                 Debug( LDAP_DEBUG_TRACE,
377                         LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
378                         0, 0, 0 );
379                 switch( rs->sr_err ) {
380                 case DB_LOCK_DEADLOCK:
381                 case DB_LOCK_NOTGRANTED:
382                         goto retry;
383                 default:
384                         rs->sr_err = LDAP_OTHER;
385                 }
386                 rs->sr_text = "index generation failed";
387                 goto return_results;
388         }
389
390         /* id2entry index */
391         rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
392         if ( rs->sr_err != 0 ) {
393                 Debug( LDAP_DEBUG_TRACE,
394                         LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
395                         0, 0, 0 );
396                 switch( rs->sr_err ) {
397                 case DB_LOCK_DEADLOCK:
398                 case DB_LOCK_NOTGRANTED:
399                         goto retry;
400                 default:
401                         rs->sr_err = LDAP_OTHER;
402                 }
403                 rs->sr_text = "entry store failed";
404                 goto return_results;
405         }
406
407         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
408                 rs->sr_err = LDAP_OTHER;
409                 rs->sr_text = "txn_commit(2) failed";
410                 goto return_results;
411         }
412
413         /* post-read */
414         if( op->o_postread ) {
415                 if( postread_ctrl == NULL ) {
416                         postread_ctrl = &ctrls[num_ctrls++];
417                         ctrls[num_ctrls] = NULL;
418                 }
419                 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
420                         &slap_post_read_bv, postread_ctrl ) )
421                 {
422                         Debug( LDAP_DEBUG_TRACE,
423                                 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
424                                 "failed!\n", 0, 0, 0 );
425                         if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
426                                 /* FIXME: is it correct to abort
427                                  * operation if control fails? */
428                                 goto return_results;
429                         }
430                 }
431         }
432
433         if ( op->o_noop ) {
434                 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
435                         rs->sr_text = "txn_abort (no-op) failed";
436                 } else {
437                         rs->sr_err = LDAP_X_NO_OPERATION;
438                         ltid = NULL;
439                         goto return_results;
440                 }
441
442         } else {
443                 struct berval nrdn;
444
445                 /* pick the RDN if not suffix; otherwise pick the entire DN */
446                 if (pdn.bv_len) {
447                         nrdn.bv_val = op->ora_e->e_nname.bv_val;
448                         nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
449                 } else {
450                         nrdn = op->ora_e->e_nname;
451                 }
452
453                 /* Use the reader txn here, outside the add txn */
454                 bdb_cache_add( bdb, ei, op->ora_e, &nrdn, rtxn, &lock );
455
456                 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
457                         rs->sr_text = "txn_commit failed";
458                 } else {
459                         rs->sr_err = LDAP_SUCCESS;
460                 }
461         }
462
463         ltid = NULL;
464         LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
465         opinfo.boi_oe.oe_key = NULL;
466
467         if ( rs->sr_err != LDAP_SUCCESS ) {
468                 Debug( LDAP_DEBUG_TRACE,
469                         LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
470                         rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
471                 rs->sr_err = LDAP_OTHER;
472                 goto return_results;
473         }
474
475         Debug(LDAP_DEBUG_TRACE,
476                 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
477                 op->o_noop ? " (no-op)" : "",
478                 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
479
480         rs->sr_text = NULL;
481         if( num_ctrls ) rs->sr_ctrls = ctrls;
482
483 return_results:
484         success = rs->sr_err;
485         send_ldap_result( op, rs );
486         slap_graduate_commit_csn( op );
487
488         if( ltid != NULL ) {
489                 TXN_ABORT( ltid );
490         }
491         if ( opinfo.boi_oe.oe_key ) {
492                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
493         }
494
495         if( success == LDAP_SUCCESS ) {
496                 /* We own the entry now, and it can be purged at will
497                  * Check to make sure it's the same entry we entered with.
498                  * Possibly a callback may have mucked with it, although
499                  * in general callbacks should treat the entry as read-only.
500                  */
501                 bdb_cache_return_entry_r( bdb, oe, &lock );
502                 if ( op->ora_e == oe )
503                         op->ora_e = NULL;
504
505                 if ( bdb->bi_txn_cp_kbyte ) {
506                         TXN_CHECKPOINT( bdb->bi_dbenv,
507                                 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
508                 }
509         }
510
511         if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
512                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
513                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
514         }
515
516         return rs->sr_err;
517 }