1 /* add.c - ldap BerkeleyDB back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2011 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";
163 opinfo.boi_oe.oe_key = bdb;
164 opinfo.boi_txn = ltid;
166 opinfo.boi_acl_cache = op->o_do_not_cache;
167 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
170 * Get the parent dn and see if the corresponding entry exists.
172 if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
175 dnParent( &op->ora_e->e_nname, &pdn );
178 /* get entry or parent */
179 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
181 switch( rs->sr_err ) {
183 rs->sr_err = LDAP_ALREADY_EXISTS;
187 case DB_LOCK_DEADLOCK:
188 case DB_LOCK_NOTGRANTED:
191 rs->sr_text = "ldap server busy";
194 rs->sr_err = LDAP_OTHER;
195 rs->sr_text = "internal error";
201 p = (Entry *)&slap_entry_root;
203 if ( !bvmatch( &pdn, &p->e_nname ) ) {
204 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
206 rs->sr_ref = is_entry_referral( p )
207 ? get_entry_referrals( op, p )
209 if ( p != (Entry *)&slap_entry_root )
210 bdb_unlocked_cache_return_entry_r( bdb, p );
212 Debug( LDAP_DEBUG_TRACE,
213 LDAP_XSTRING(bdb_add) ": parent "
214 "does not exist\n", 0, 0, 0 );
216 rs->sr_err = LDAP_REFERRAL;
217 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
221 rs->sr_err = access_allowed( op, p,
222 children, NULL, ACL_WADD, NULL );
224 if ( ! rs->sr_err ) {
225 switch( opinfo.boi_err ) {
226 case DB_LOCK_DEADLOCK:
227 case DB_LOCK_NOTGRANTED:
231 if ( p != (Entry *)&slap_entry_root )
232 bdb_unlocked_cache_return_entry_r( bdb, p );
235 Debug( LDAP_DEBUG_TRACE,
236 LDAP_XSTRING(bdb_add) ": no write access to parent\n",
238 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
239 rs->sr_text = "no write access to parent";
240 goto return_results;;
243 if ( p != (Entry *)&slap_entry_root ) {
244 if ( is_entry_subentry( p ) ) {
245 bdb_unlocked_cache_return_entry_r( bdb, p );
247 /* parent is a subentry, don't allow add */
248 Debug( LDAP_DEBUG_TRACE,
249 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
251 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
252 rs->sr_text = "parent is a subentry";
253 goto return_results;;
256 if ( is_entry_alias( p ) ) {
257 bdb_unlocked_cache_return_entry_r( bdb, p );
259 /* parent is an alias, don't allow add */
260 Debug( LDAP_DEBUG_TRACE,
261 LDAP_XSTRING(bdb_add) ": parent is alias\n",
263 rs->sr_err = LDAP_ALIAS_PROBLEM;
264 rs->sr_text = "parent is an alias";
265 goto return_results;;
268 if ( is_entry_referral( p ) ) {
269 /* parent is a referral, don't allow add */
270 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
272 rs->sr_ref = get_entry_referrals( op, p );
273 bdb_unlocked_cache_return_entry_r( bdb, p );
275 Debug( LDAP_DEBUG_TRACE,
276 LDAP_XSTRING(bdb_add) ": parent is referral\n",
279 rs->sr_err = LDAP_REFERRAL;
280 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
288 /* parent must be an administrative point of the required kind */
291 /* free parent and reader lock */
292 if ( p != (Entry *)&slap_entry_root ) {
293 if ( p->e_nname.bv_len ) {
296 /* ITS#5326: use parent's DN if differs from provided one */
297 dnParent( &op->ora_e->e_name, &ppdn );
298 if ( !dn_match( &p->e_name, &ppdn ) ) {
302 dnRdn( &op->ora_e->e_name, &rdn );
304 build_new_dn( &newdn, &p->e_name, &rdn, NULL );
305 ber_memfree( op->ora_e->e_name.bv_val );
306 op->ora_e->e_name = newdn;
308 /* FIXME: should check whether
309 * dnNormalize(newdn) == e->e_nname ... */
313 bdb_unlocked_cache_return_entry_r( bdb, p );
317 rs->sr_err = access_allowed( op, op->ora_e,
318 entry, NULL, ACL_WADD, NULL );
320 if ( ! rs->sr_err ) {
321 switch( opinfo.boi_err ) {
322 case DB_LOCK_DEADLOCK:
323 case DB_LOCK_NOTGRANTED:
327 Debug( LDAP_DEBUG_TRACE,
328 LDAP_XSTRING(bdb_add) ": no write access to entry\n",
330 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
331 rs->sr_text = "no write access to entry";
332 goto return_results;;
336 * Check ACL for attribute write access
338 if (!acl_check_modlist(op, oe, op->ora_modlist)) {
339 switch( opinfo.boi_err ) {
340 case DB_LOCK_DEADLOCK:
341 case DB_LOCK_NOTGRANTED:
345 Debug( LDAP_DEBUG_TRACE,
346 LDAP_XSTRING(bdb_add) ": no write access to attribute\n",
348 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
349 rs->sr_text = "no write access to attribute";
350 goto return_results;;
354 rs->sr_err = bdb_next_id( op->o_bd, &eid );
355 if( rs->sr_err != 0 ) {
356 Debug( LDAP_DEBUG_TRACE,
357 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
359 rs->sr_err = LDAP_OTHER;
360 rs->sr_text = "internal error";
363 op->ora_e->e_id = eid;
366 /* nested transaction */
367 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
368 bdb->bi_db_opflags );
370 if( rs->sr_err != 0 ) {
371 Debug( LDAP_DEBUG_TRACE,
372 LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
373 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
374 rs->sr_err = LDAP_OTHER;
375 rs->sr_text = "internal error";
380 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->ora_e );
381 if ( rs->sr_err != 0 ) {
382 Debug( LDAP_DEBUG_TRACE,
383 LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
384 db_strerror(rs->sr_err), rs->sr_err, 0 );
386 switch( rs->sr_err ) {
387 case DB_LOCK_DEADLOCK:
388 case DB_LOCK_NOTGRANTED:
391 rs->sr_err = LDAP_ALREADY_EXISTS;
394 rs->sr_err = LDAP_OTHER;
399 /* attribute indexes */
400 rs->sr_err = bdb_index_entry_add( op, lt2, op->ora_e );
401 if ( rs->sr_err != LDAP_SUCCESS ) {
402 Debug( LDAP_DEBUG_TRACE,
403 LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
405 switch( rs->sr_err ) {
406 case DB_LOCK_DEADLOCK:
407 case DB_LOCK_NOTGRANTED:
410 rs->sr_err = LDAP_OTHER;
412 rs->sr_text = "index generation failed";
417 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->ora_e );
418 if ( rs->sr_err != 0 ) {
419 Debug( LDAP_DEBUG_TRACE,
420 LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
422 switch( rs->sr_err ) {
423 case DB_LOCK_DEADLOCK:
424 case DB_LOCK_NOTGRANTED:
427 rs->sr_err = LDAP_OTHER;
429 rs->sr_text = "entry store failed";
433 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
434 rs->sr_err = LDAP_OTHER;
435 rs->sr_text = "txn_commit(2) failed";
440 if( op->o_postread ) {
441 if( postread_ctrl == NULL ) {
442 postread_ctrl = &ctrls[num_ctrls++];
443 ctrls[num_ctrls] = NULL;
445 if ( slap_read_controls( op, rs, op->ora_e,
446 &slap_post_read_bv, postread_ctrl ) )
448 Debug( LDAP_DEBUG_TRACE,
449 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
450 "failed!\n", 0, 0, 0 );
451 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
452 /* FIXME: is it correct to abort
453 * operation if control fails? */
460 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
461 rs->sr_text = "txn_abort (no-op) failed";
463 rs->sr_err = LDAP_X_NO_OPERATION;
471 /* pick the RDN if not suffix; otherwise pick the entire DN */
473 nrdn.bv_val = op->ora_e->e_nname.bv_val;
474 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
476 nrdn = op->ora_e->e_nname;
479 bdb_cache_add( bdb, ei, op->ora_e, &nrdn, ltid, &lock );
481 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
482 rs->sr_text = "txn_commit failed";
484 rs->sr_err = LDAP_SUCCESS;
489 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
490 opinfo.boi_oe.oe_key = NULL;
492 if ( rs->sr_err != LDAP_SUCCESS ) {
493 Debug( LDAP_DEBUG_TRACE,
494 LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
495 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
496 rs->sr_err = LDAP_OTHER;
500 Debug(LDAP_DEBUG_TRACE,
501 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
502 op->o_noop ? " (no-op)" : "",
503 op->ora_e->e_id, op->ora_e->e_dn );
506 if( num_ctrls ) rs->sr_ctrls = ctrls;
509 success = rs->sr_err;
510 send_ldap_result( op, rs );
515 if ( opinfo.boi_oe.oe_key ) {
516 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
519 if( success == LDAP_SUCCESS ) {
520 /* We own the entry now, and it can be purged at will
521 * Check to make sure it's the same entry we entered with.
522 * Possibly a callback may have mucked with it, although
523 * in general callbacks should treat the entry as read-only.
525 bdb_cache_deref( oe->e_private );
526 if ( op->ora_e == oe )
529 if ( bdb->bi_txn_cp_kbyte ) {
530 TXN_CHECKPOINT( bdb->bi_dbenv,
531 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
535 slap_graduate_commit_csn( op );
537 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
538 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
539 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );