1 /* add.c - ldap BerkeleyDB back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2009 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
20 #include <ac/string.h>
25 bdb_add(Operation *op, SlapReply *rs )
27 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
29 Entry *p = NULL, *oe = op->ora_e;
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;
37 struct bdb_op_info opinfo = {{{ 0 }}};
44 LDAPControl **postread_ctrl = NULL;
45 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
52 Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_add) ": %s\n",
53 op->ora_e->e_name.bv_val, 0, 0);
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;
63 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
68 if( op->o_conn->c_txn_backend == NULL ) {
69 op->o_conn->c_txn_backend = op->o_bd;
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;
77 /* insert operation into transaction */
79 rs->sr_text = "transaction specified";
80 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
83 /* release connection lock */
84 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
87 send_ldap_result( op, rs );
95 /* check entry's schema */
96 rs->sr_err = entry_schema_check( op, op->ora_e, NULL,
97 get_relax(op), 1, NULL, &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 );
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 );
115 if ( get_assert( op ) &&
116 ( test_filter( op, op->ora_e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
118 rs->sr_err = LDAP_ASSERTION_FAILED;
122 subentry = is_entry_subentry( op->ora_e );
124 /* Get our reader TXN */
125 rs->sr_err = bdb_reader_get( op, bdb->bi_dbenv, &rtxn );
128 retry: /* transaction retry */
130 /* free parent and reader lock */
131 if ( p != (Entry *)&slap_entry_root ) {
132 bdb_unlocked_cache_return_entry_r( bdb, p );
136 rs->sr_err = TXN_ABORT( ltid );
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";
146 if ( op->o_abandon ) {
147 rs->sr_err = SLAPD_ABANDON;
150 bdb_trans_backoff( ++num_retries );
153 /* begin transaction */
154 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
155 bdb->bi_db_opflags );
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";
166 opinfo.boi_oe.oe_key = bdb;
167 opinfo.boi_txn = ltid;
169 opinfo.boi_acl_cache = op->o_do_not_cache;
170 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
173 * Get the parent dn and see if the corresponding entry exists.
175 if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
178 dnParent( &op->ora_e->e_nname, &pdn );
181 /* get entry or parent */
182 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
184 switch( rs->sr_err ) {
186 rs->sr_err = LDAP_ALREADY_EXISTS;
190 case DB_LOCK_DEADLOCK:
191 case DB_LOCK_NOTGRANTED:
194 rs->sr_text = "ldap server busy";
197 rs->sr_err = LDAP_OTHER;
198 rs->sr_text = "internal error";
204 p = (Entry *)&slap_entry_root;
206 if ( !bvmatch( &pdn, &p->e_nname ) ) {
207 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
209 rs->sr_ref = is_entry_referral( p )
210 ? get_entry_referrals( op, p )
212 if ( p != (Entry *)&slap_entry_root )
213 bdb_unlocked_cache_return_entry_r( bdb, p );
215 Debug( LDAP_DEBUG_TRACE,
216 LDAP_XSTRING(bdb_add) ": parent "
217 "does not exist\n", 0, 0, 0 );
219 rs->sr_err = LDAP_REFERRAL;
220 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
224 rs->sr_err = access_allowed( op, p,
225 children, NULL, ACL_WADD, NULL );
227 if ( ! rs->sr_err ) {
228 switch( opinfo.boi_err ) {
229 case DB_LOCK_DEADLOCK:
230 case DB_LOCK_NOTGRANTED:
234 if ( p != (Entry *)&slap_entry_root )
235 bdb_unlocked_cache_return_entry_r( bdb, p );
238 Debug( LDAP_DEBUG_TRACE,
239 LDAP_XSTRING(bdb_add) ": no write access to parent\n",
241 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
242 rs->sr_text = "no write access to parent";
243 goto return_results;;
246 if ( p != (Entry *)&slap_entry_root ) {
247 if ( is_entry_subentry( p ) ) {
248 bdb_unlocked_cache_return_entry_r( bdb, p );
250 /* parent is a subentry, don't allow add */
251 Debug( LDAP_DEBUG_TRACE,
252 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
254 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
255 rs->sr_text = "parent is a subentry";
256 goto return_results;;
259 if ( is_entry_alias( p ) ) {
260 bdb_unlocked_cache_return_entry_r( bdb, p );
262 /* parent is an alias, don't allow add */
263 Debug( LDAP_DEBUG_TRACE,
264 LDAP_XSTRING(bdb_add) ": parent is alias\n",
266 rs->sr_err = LDAP_ALIAS_PROBLEM;
267 rs->sr_text = "parent is an alias";
268 goto return_results;;
271 if ( is_entry_referral( p ) ) {
272 /* parent is a referral, don't allow add */
273 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
275 rs->sr_ref = get_entry_referrals( op, p );
276 bdb_unlocked_cache_return_entry_r( bdb, p );
278 Debug( LDAP_DEBUG_TRACE,
279 LDAP_XSTRING(bdb_add) ": parent is referral\n",
282 rs->sr_err = LDAP_REFERRAL;
283 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
291 /* parent must be an administrative point of the required kind */
294 /* free parent and reader lock */
295 if ( p != (Entry *)&slap_entry_root ) {
296 if ( p->e_nname.bv_len ) {
299 /* ITS#5326: use parent's DN if differs from provided one */
300 dnParent( &op->ora_e->e_name, &ppdn );
301 if ( !dn_match( &p->e_name, &ppdn ) ) {
305 dnRdn( &op->ora_e->e_name, &rdn );
307 build_new_dn( &newdn, &p->e_name, &rdn, NULL );
308 if ( op->ora_e->e_name.bv_val != op->o_req_dn.bv_val )
309 ber_memfree( op->ora_e->e_name.bv_val );
310 op->ora_e->e_name = newdn;
312 /* FIXME: should check whether
313 * dnNormalize(newdn) == e->e_nname ... */
317 bdb_unlocked_cache_return_entry_r( bdb, p );
321 rs->sr_err = access_allowed( op, op->ora_e,
322 entry, NULL, ACL_WADD, NULL );
324 if ( ! rs->sr_err ) {
325 switch( opinfo.boi_err ) {
326 case DB_LOCK_DEADLOCK:
327 case DB_LOCK_NOTGRANTED:
331 Debug( LDAP_DEBUG_TRACE,
332 LDAP_XSTRING(bdb_add) ": no write access to entry\n",
334 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
335 rs->sr_text = "no write access to entry";
336 goto return_results;;
340 * Check ACL for attribute write access
342 if (!acl_check_modlist(op, oe, op->ora_modlist)) {
343 switch( opinfo.boi_err ) {
344 case DB_LOCK_DEADLOCK:
345 case DB_LOCK_NOTGRANTED:
349 Debug( LDAP_DEBUG_TRACE,
350 LDAP_XSTRING(bdb_add) ": no write access to attribute\n",
352 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
353 rs->sr_text = "no write access to attribute";
354 goto return_results;;
358 rs->sr_err = bdb_next_id( op->o_bd, &eid );
359 if( rs->sr_err != 0 ) {
360 Debug( LDAP_DEBUG_TRACE,
361 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
363 rs->sr_err = LDAP_OTHER;
364 rs->sr_text = "internal error";
367 op->ora_e->e_id = eid;
370 /* nested transaction */
371 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
372 bdb->bi_db_opflags );
374 if( rs->sr_err != 0 ) {
375 Debug( LDAP_DEBUG_TRACE,
376 LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
377 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
378 rs->sr_err = LDAP_OTHER;
379 rs->sr_text = "internal error";
384 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->ora_e );
385 if ( rs->sr_err != 0 ) {
386 Debug( LDAP_DEBUG_TRACE,
387 LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
388 db_strerror(rs->sr_err), rs->sr_err, 0 );
390 switch( rs->sr_err ) {
391 case DB_LOCK_DEADLOCK:
392 case DB_LOCK_NOTGRANTED:
395 rs->sr_err = LDAP_ALREADY_EXISTS;
398 rs->sr_err = LDAP_OTHER;
403 /* attribute indexes */
404 rs->sr_err = bdb_index_entry_add( op, lt2, op->ora_e );
405 if ( rs->sr_err != LDAP_SUCCESS ) {
406 Debug( LDAP_DEBUG_TRACE,
407 LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
409 switch( rs->sr_err ) {
410 case DB_LOCK_DEADLOCK:
411 case DB_LOCK_NOTGRANTED:
414 rs->sr_err = LDAP_OTHER;
416 rs->sr_text = "index generation failed";
421 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->ora_e );
422 if ( rs->sr_err != 0 ) {
423 Debug( LDAP_DEBUG_TRACE,
424 LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
426 switch( rs->sr_err ) {
427 case DB_LOCK_DEADLOCK:
428 case DB_LOCK_NOTGRANTED:
431 rs->sr_err = LDAP_OTHER;
433 rs->sr_text = "entry store failed";
437 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
438 rs->sr_err = LDAP_OTHER;
439 rs->sr_text = "txn_commit(2) failed";
444 if( op->o_postread ) {
445 if( postread_ctrl == NULL ) {
446 postread_ctrl = &ctrls[num_ctrls++];
447 ctrls[num_ctrls] = NULL;
449 if ( slap_read_controls( op, rs, op->ora_e,
450 &slap_post_read_bv, postread_ctrl ) )
452 Debug( LDAP_DEBUG_TRACE,
453 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
454 "failed!\n", 0, 0, 0 );
455 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
456 /* FIXME: is it correct to abort
457 * operation if control fails? */
464 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
465 rs->sr_text = "txn_abort (no-op) failed";
467 rs->sr_err = LDAP_X_NO_OPERATION;
475 /* pick the RDN if not suffix; otherwise pick the entire DN */
477 nrdn.bv_val = op->ora_e->e_nname.bv_val;
478 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
480 nrdn = op->ora_e->e_nname;
483 /* Use the reader txn here, outside the add txn */
484 bdb_cache_add( bdb, ei, op->ora_e, &nrdn, rtxn, &lock );
486 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
487 rs->sr_text = "txn_commit failed";
489 rs->sr_err = LDAP_SUCCESS;
494 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
495 opinfo.boi_oe.oe_key = NULL;
497 if ( rs->sr_err != LDAP_SUCCESS ) {
498 Debug( LDAP_DEBUG_TRACE,
499 LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
500 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
501 rs->sr_err = LDAP_OTHER;
505 Debug(LDAP_DEBUG_TRACE,
506 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
507 op->o_noop ? " (no-op)" : "",
508 op->ora_e->e_id, op->ora_e->e_dn );
511 if( num_ctrls ) rs->sr_ctrls = ctrls;
514 success = rs->sr_err;
515 send_ldap_result( op, rs );
516 slap_graduate_commit_csn( op );
521 if ( opinfo.boi_oe.oe_key ) {
522 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
525 if( success == LDAP_SUCCESS ) {
526 /* We own the entry now, and it can be purged at will
527 * Check to make sure it's the same entry we entered with.
528 * Possibly a callback may have mucked with it, although
529 * in general callbacks should treat the entry as read-only.
531 bdb_cache_return_entry_r( bdb, oe, &lock );
532 if ( op->ora_e == oe )
535 if ( bdb->bi_txn_cp_kbyte ) {
536 TXN_CHECKPOINT( bdb->bi_dbenv,
537 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
541 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
542 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
543 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );