1 /* add.c - ldap BerkeleyDB back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2008 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->oq_add.rs_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->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 );
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 subentry = is_entry_subentry( op->oq_add.rs_e );
117 /* Get our reader TXN */
118 rs->sr_err = bdb_reader_get( op, bdb->bi_dbenv, &rtxn );
121 retry: /* transaction retry */
123 /* free parent and reader lock */
124 if ( p != (Entry *)&slap_entry_root ) {
125 bdb_unlocked_cache_return_entry_r( bdb, p );
129 rs->sr_err = TXN_ABORT( ltid );
131 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
132 opinfo.boi_oe.oe_key = NULL;
133 op->o_do_not_cache = opinfo.boi_acl_cache;
134 if( rs->sr_err != 0 ) {
135 rs->sr_err = LDAP_OTHER;
136 rs->sr_text = "internal error";
139 if ( op->o_abandon ) {
140 rs->sr_err = SLAPD_ABANDON;
143 bdb_trans_backoff( ++num_retries );
146 /* begin transaction */
147 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
148 bdb->bi_db_opflags );
150 if( rs->sr_err != 0 ) {
151 Debug( LDAP_DEBUG_TRACE,
152 LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
153 db_strerror(rs->sr_err), rs->sr_err, 0 );
154 rs->sr_err = LDAP_OTHER;
155 rs->sr_text = "internal error";
159 opinfo.boi_oe.oe_key = bdb;
160 opinfo.boi_txn = ltid;
162 opinfo.boi_acl_cache = op->o_do_not_cache;
163 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
166 * Get the parent dn and see if the corresponding entry exists.
168 if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
171 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
174 /* get entry or parent */
175 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
177 switch( rs->sr_err ) {
179 rs->sr_err = LDAP_ALREADY_EXISTS;
183 case DB_LOCK_DEADLOCK:
184 case DB_LOCK_NOTGRANTED:
187 rs->sr_text = "ldap server busy";
190 rs->sr_err = LDAP_OTHER;
191 rs->sr_text = "internal error";
197 p = (Entry *)&slap_entry_root;
199 if ( !bvmatch( &pdn, &p->e_nname ) ) {
200 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
202 rs->sr_ref = is_entry_referral( p )
203 ? get_entry_referrals( op, p )
205 bdb_unlocked_cache_return_entry_r( bdb, p );
207 Debug( LDAP_DEBUG_TRACE,
208 LDAP_XSTRING(bdb_add) ": parent "
209 "does not exist\n", 0, 0, 0 );
211 rs->sr_err = LDAP_REFERRAL;
212 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
216 rs->sr_err = access_allowed( op, p,
217 children, NULL, ACL_WADD, NULL );
219 if ( ! rs->sr_err ) {
220 switch( opinfo.boi_err ) {
221 case DB_LOCK_DEADLOCK:
222 case DB_LOCK_NOTGRANTED:
226 Debug( LDAP_DEBUG_TRACE,
227 LDAP_XSTRING(bdb_add) ": no write access to parent\n",
229 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
230 rs->sr_text = "no write access to parent";
231 goto return_results;;
234 if ( p != (Entry *)&slap_entry_root ) {
235 if ( is_entry_subentry( p ) ) {
236 /* parent is a subentry, don't allow add */
237 Debug( LDAP_DEBUG_TRACE,
238 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
240 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
241 rs->sr_text = "parent is a subentry";
242 goto return_results;;
245 if ( is_entry_alias( p ) ) {
246 /* parent is an alias, don't allow add */
247 Debug( LDAP_DEBUG_TRACE,
248 LDAP_XSTRING(bdb_add) ": parent is alias\n",
250 rs->sr_err = LDAP_ALIAS_PROBLEM;
251 rs->sr_text = "parent is an alias";
252 goto return_results;;
255 if ( is_entry_referral( p ) ) {
256 /* parent is a referral, don't allow add */
257 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
259 rs->sr_ref = get_entry_referrals( op, p );
260 bdb_unlocked_cache_return_entry_r( bdb, p );
262 Debug( LDAP_DEBUG_TRACE,
263 LDAP_XSTRING(bdb_add) ": parent is referral\n",
266 rs->sr_err = LDAP_REFERRAL;
267 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
275 /* parent must be an administrative point of the required kind */
278 /* free parent and reader lock */
279 if ( p != (Entry *)&slap_entry_root ) {
280 bdb_unlocked_cache_return_entry_r( bdb, p );
284 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
285 entry, NULL, ACL_WADD, NULL );
287 if ( ! rs->sr_err ) {
288 switch( opinfo.boi_err ) {
289 case DB_LOCK_DEADLOCK:
290 case DB_LOCK_NOTGRANTED:
294 Debug( LDAP_DEBUG_TRACE,
295 LDAP_XSTRING(bdb_add) ": no write access to entry\n",
297 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
298 rs->sr_text = "no write access to entry";
299 goto return_results;;
303 rs->sr_err = bdb_next_id( op->o_bd, &eid );
304 if( rs->sr_err != 0 ) {
305 Debug( LDAP_DEBUG_TRACE,
306 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
308 rs->sr_err = LDAP_OTHER;
309 rs->sr_text = "internal error";
312 op->oq_add.rs_e->e_id = eid;
315 /* nested transaction */
316 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
317 bdb->bi_db_opflags );
319 if( rs->sr_err != 0 ) {
320 Debug( LDAP_DEBUG_TRACE,
321 LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
322 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
323 rs->sr_err = LDAP_OTHER;
324 rs->sr_text = "internal error";
329 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
330 if ( rs->sr_err != 0 ) {
331 Debug( LDAP_DEBUG_TRACE,
332 LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
333 db_strerror(rs->sr_err), rs->sr_err, 0 );
335 switch( rs->sr_err ) {
336 case DB_LOCK_DEADLOCK:
337 case DB_LOCK_NOTGRANTED:
340 rs->sr_err = LDAP_ALREADY_EXISTS;
343 rs->sr_err = LDAP_OTHER;
348 /* attribute indexes */
349 rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
350 if ( rs->sr_err != LDAP_SUCCESS ) {
351 Debug( LDAP_DEBUG_TRACE,
352 LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
354 switch( rs->sr_err ) {
355 case DB_LOCK_DEADLOCK:
356 case DB_LOCK_NOTGRANTED:
359 rs->sr_err = LDAP_OTHER;
361 rs->sr_text = "index generation failed";
366 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
367 if ( rs->sr_err != 0 ) {
368 Debug( LDAP_DEBUG_TRACE,
369 LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
371 switch( rs->sr_err ) {
372 case DB_LOCK_DEADLOCK:
373 case DB_LOCK_NOTGRANTED:
376 rs->sr_err = LDAP_OTHER;
378 rs->sr_text = "entry store failed";
382 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
383 rs->sr_err = LDAP_OTHER;
384 rs->sr_text = "txn_commit(2) failed";
389 if( op->o_postread ) {
390 if( postread_ctrl == NULL ) {
391 postread_ctrl = &ctrls[num_ctrls++];
392 ctrls[num_ctrls] = NULL;
394 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
395 &slap_post_read_bv, postread_ctrl ) )
397 Debug( LDAP_DEBUG_TRACE,
398 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
399 "failed!\n", 0, 0, 0 );
400 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
401 /* FIXME: is it correct to abort
402 * operation if control fails? */
409 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
410 rs->sr_text = "txn_abort (no-op) failed";
412 rs->sr_err = LDAP_X_NO_OPERATION;
420 /* pick the RDN if not suffix; otherwise pick the entire DN */
422 nrdn.bv_val = op->ora_e->e_nname.bv_val;
423 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
425 nrdn = op->ora_e->e_nname;
428 /* Use the reader txn here, outside the add txn */
429 bdb_cache_add( bdb, ei, op->ora_e, &nrdn, rtxn, &lock );
431 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
432 rs->sr_text = "txn_commit failed";
434 rs->sr_err = LDAP_SUCCESS;
439 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
440 opinfo.boi_oe.oe_key = NULL;
442 if ( rs->sr_err != LDAP_SUCCESS ) {
443 Debug( LDAP_DEBUG_TRACE,
444 LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
445 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
446 rs->sr_err = LDAP_OTHER;
450 Debug(LDAP_DEBUG_TRACE,
451 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
452 op->o_noop ? " (no-op)" : "",
453 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
456 if( num_ctrls ) rs->sr_ctrls = ctrls;
459 success = rs->sr_err;
460 send_ldap_result( op, rs );
461 slap_graduate_commit_csn( op );
466 if ( opinfo.boi_oe.oe_key ) {
467 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
470 if( success == LDAP_SUCCESS ) {
471 /* We own the entry now, and it can be purged at will
472 * Check to make sure it's the same entry we entered with.
473 * Possibly a callback may have mucked with it, although
474 * in general callbacks should treat the entry as read-only.
476 bdb_cache_return_entry_r( bdb, oe, &lock );
477 if ( op->ora_e == oe )
480 if ( bdb->bi_txn_cp_kbyte ) {
481 TXN_CHECKPOINT( bdb->bi_dbenv,
482 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
486 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
487 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
488 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );