1 /* add.c - ldap BerkeleyDB back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2014 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;
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 );
125 retry: /* transaction retry */
127 /* free parent and reader lock */
128 if ( p != (Entry *)&slap_entry_root ) {
129 bdb_unlocked_cache_return_entry_r( bdb, p );
133 rs->sr_err = TXN_ABORT( ltid );
135 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
136 opinfo.boi_oe.oe_key = NULL;
137 op->o_do_not_cache = opinfo.boi_acl_cache;
138 if( rs->sr_err != 0 ) {
139 rs->sr_err = LDAP_OTHER;
140 rs->sr_text = "internal error";
143 if ( op->o_abandon ) {
144 rs->sr_err = SLAPD_ABANDON;
147 bdb_trans_backoff( ++num_retries );
150 /* begin transaction */
151 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
152 bdb->bi_db_opflags );
154 if( rs->sr_err != 0 ) {
155 Debug( LDAP_DEBUG_TRACE,
156 LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
157 db_strerror(rs->sr_err), rs->sr_err, 0 );
158 rs->sr_err = LDAP_OTHER;
159 rs->sr_text = "internal error";
162 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_add) ": txn1 id: %x\n",
163 ltid->id(ltid), 0, 0 );
165 opinfo.boi_oe.oe_key = bdb;
166 opinfo.boi_txn = ltid;
168 opinfo.boi_acl_cache = op->o_do_not_cache;
169 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
172 * Get the parent dn and see if the corresponding entry exists.
174 if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
177 dnParent( &op->ora_e->e_nname, &pdn );
180 /* get entry or parent */
181 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
183 switch( rs->sr_err ) {
185 rs->sr_err = LDAP_ALREADY_EXISTS;
189 case DB_LOCK_DEADLOCK:
190 case DB_LOCK_NOTGRANTED:
193 rs->sr_text = "ldap server busy";
196 rs->sr_err = LDAP_OTHER;
197 rs->sr_text = "internal error";
203 p = (Entry *)&slap_entry_root;
205 if ( !bvmatch( &pdn, &p->e_nname ) ) {
206 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
208 rs->sr_ref = is_entry_referral( p )
209 ? get_entry_referrals( op, p )
211 if ( p != (Entry *)&slap_entry_root )
212 bdb_unlocked_cache_return_entry_r( bdb, p );
214 Debug( LDAP_DEBUG_TRACE,
215 LDAP_XSTRING(bdb_add) ": parent "
216 "does not exist\n", 0, 0, 0 );
218 rs->sr_err = LDAP_REFERRAL;
219 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
223 rs->sr_err = access_allowed( op, p,
224 children, NULL, ACL_WADD, NULL );
226 if ( ! rs->sr_err ) {
227 switch( opinfo.boi_err ) {
228 case DB_LOCK_DEADLOCK:
229 case DB_LOCK_NOTGRANTED:
233 if ( p != (Entry *)&slap_entry_root )
234 bdb_unlocked_cache_return_entry_r( bdb, p );
237 Debug( LDAP_DEBUG_TRACE,
238 LDAP_XSTRING(bdb_add) ": no write access to parent\n",
240 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
241 rs->sr_text = "no write access to parent";
242 goto return_results;;
245 if ( p != (Entry *)&slap_entry_root ) {
246 if ( is_entry_subentry( p ) ) {
247 bdb_unlocked_cache_return_entry_r( bdb, p );
249 /* parent is a subentry, don't allow add */
250 Debug( LDAP_DEBUG_TRACE,
251 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
253 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
254 rs->sr_text = "parent is a subentry";
255 goto return_results;;
258 if ( is_entry_alias( p ) ) {
259 bdb_unlocked_cache_return_entry_r( bdb, p );
261 /* parent is an alias, don't allow add */
262 Debug( LDAP_DEBUG_TRACE,
263 LDAP_XSTRING(bdb_add) ": parent is alias\n",
265 rs->sr_err = LDAP_ALIAS_PROBLEM;
266 rs->sr_text = "parent is an alias";
267 goto return_results;;
270 if ( is_entry_referral( p ) ) {
271 /* parent is a referral, don't allow add */
272 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
274 rs->sr_ref = get_entry_referrals( op, p );
275 bdb_unlocked_cache_return_entry_r( bdb, p );
277 Debug( LDAP_DEBUG_TRACE,
278 LDAP_XSTRING(bdb_add) ": parent is referral\n",
281 rs->sr_err = LDAP_REFERRAL;
282 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
290 /* parent must be an administrative point of the required kind */
293 /* free parent and reader lock */
294 if ( p != (Entry *)&slap_entry_root ) {
295 if ( p->e_nname.bv_len ) {
298 /* ITS#5326: use parent's DN if differs from provided one */
299 dnParent( &op->ora_e->e_name, &ppdn );
300 if ( !dn_match( &p->e_name, &ppdn ) ) {
304 dnRdn( &op->ora_e->e_name, &rdn );
306 build_new_dn( &newdn, &p->e_name, &rdn, NULL );
307 if ( op->ora_e->e_name.bv_val != op->o_req_dn.bv_val )
308 ber_memfree( op->ora_e->e_name.bv_val );
309 op->ora_e->e_name = newdn;
311 /* FIXME: should check whether
312 * dnNormalize(newdn) == e->e_nname ... */
316 bdb_unlocked_cache_return_entry_r( bdb, p );
320 rs->sr_err = access_allowed( op, op->ora_e,
321 entry, NULL, ACL_WADD, NULL );
323 if ( ! rs->sr_err ) {
324 switch( opinfo.boi_err ) {
325 case DB_LOCK_DEADLOCK:
326 case DB_LOCK_NOTGRANTED:
330 Debug( LDAP_DEBUG_TRACE,
331 LDAP_XSTRING(bdb_add) ": no write access to entry\n",
333 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
334 rs->sr_text = "no write access to entry";
335 goto return_results;;
339 * Check ACL for attribute write access
341 if (!acl_check_modlist(op, oe, op->ora_modlist)) {
342 switch( opinfo.boi_err ) {
343 case DB_LOCK_DEADLOCK:
344 case DB_LOCK_NOTGRANTED:
348 Debug( LDAP_DEBUG_TRACE,
349 LDAP_XSTRING(bdb_add) ": no write access to attribute\n",
351 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
352 rs->sr_text = "no write access to attribute";
353 goto return_results;;
357 rs->sr_err = bdb_next_id( op->o_bd, &eid );
358 if( rs->sr_err != 0 ) {
359 Debug( LDAP_DEBUG_TRACE,
360 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
362 rs->sr_err = LDAP_OTHER;
363 rs->sr_text = "internal error";
366 op->ora_e->e_id = eid;
369 /* nested transaction */
370 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
371 bdb->bi_db_opflags );
373 if( rs->sr_err != 0 ) {
374 Debug( LDAP_DEBUG_TRACE,
375 LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
376 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
377 rs->sr_err = LDAP_OTHER;
378 rs->sr_text = "internal error";
381 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_add) ": txn2 id: %x\n",
382 lt2->id(lt2), 0, 0 );
385 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->ora_e );
386 if ( rs->sr_err != 0 ) {
387 Debug( LDAP_DEBUG_TRACE,
388 LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
389 db_strerror(rs->sr_err), rs->sr_err, 0 );
391 switch( rs->sr_err ) {
392 case DB_LOCK_DEADLOCK:
393 case DB_LOCK_NOTGRANTED:
396 rs->sr_err = LDAP_ALREADY_EXISTS;
399 rs->sr_err = LDAP_OTHER;
404 /* attribute indexes */
405 rs->sr_err = bdb_index_entry_add( op, lt2, op->ora_e );
406 if ( rs->sr_err != LDAP_SUCCESS ) {
407 Debug( LDAP_DEBUG_TRACE,
408 LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
410 switch( rs->sr_err ) {
411 case DB_LOCK_DEADLOCK:
412 case DB_LOCK_NOTGRANTED:
415 rs->sr_err = LDAP_OTHER;
417 rs->sr_text = "index generation failed";
422 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->ora_e );
423 if ( rs->sr_err != 0 ) {
424 Debug( LDAP_DEBUG_TRACE,
425 LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
427 switch( rs->sr_err ) {
428 case DB_LOCK_DEADLOCK:
429 case DB_LOCK_NOTGRANTED:
432 rs->sr_err = LDAP_OTHER;
434 rs->sr_text = "entry store failed";
438 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
439 rs->sr_err = LDAP_OTHER;
440 rs->sr_text = "txn_commit(2) failed";
445 if( op->o_postread ) {
446 if( postread_ctrl == NULL ) {
447 postread_ctrl = &ctrls[num_ctrls++];
448 ctrls[num_ctrls] = NULL;
450 if ( slap_read_controls( op, rs, op->ora_e,
451 &slap_post_read_bv, postread_ctrl ) )
453 Debug( LDAP_DEBUG_TRACE,
454 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
455 "failed!\n", 0, 0, 0 );
456 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
457 /* FIXME: is it correct to abort
458 * operation if control fails? */
465 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
466 rs->sr_text = "txn_abort (no-op) failed";
468 rs->sr_err = LDAP_X_NO_OPERATION;
476 /* pick the RDN if not suffix; otherwise pick the entire DN */
478 nrdn.bv_val = op->ora_e->e_nname.bv_val;
479 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
481 nrdn = op->ora_e->e_nname;
484 bdb_cache_add( bdb, ei, op->ora_e, &nrdn, ltid, &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 );
520 if ( opinfo.boi_oe.oe_key ) {
521 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
524 if( success == LDAP_SUCCESS ) {
525 /* We own the entry now, and it can be purged at will
526 * Check to make sure it's the same entry we entered with.
527 * Possibly a callback may have mucked with it, although
528 * in general callbacks should treat the entry as read-only.
530 bdb_cache_deref( oe->e_private );
531 if ( op->ora_e == oe )
534 if ( bdb->bi_txn_cp_kbyte ) {
535 TXN_CHECKPOINT( bdb->bi_dbenv,
536 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
540 slap_graduate_commit_csn( op );
542 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
543 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
544 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );