1 /* add.c - ldap BerkeleyDB back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 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;
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;
36 struct bdb_op_info opinfo = {0};
47 EntryInfo *suffix_ei = NULL;
51 LDAPControl **postread_ctrl = NULL;
52 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
55 Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_add) ": %s\n",
56 op->oq_add.rs_e->e_name.bv_val, 0, 0);
60 /* check entry's schema */
61 rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e,
62 NULL, &rs->sr_text, textbuf, textlen );
63 if ( rs->sr_err != LDAP_SUCCESS ) {
64 Debug( LDAP_DEBUG_TRACE,
65 LDAP_XSTRING(bdb_add) ": entry failed schema check: "
66 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
71 subentry = is_entry_subentry( op->oq_add.rs_e );
75 * acquire an ID outside of the operation transaction
76 * to avoid serializing adds.
78 rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
79 if( rs->sr_err != 0 ) {
80 Debug( LDAP_DEBUG_TRACE,
81 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
83 rs->sr_err = LDAP_OTHER;
84 rs->sr_text = "internal error";
89 retry: /* transaction retry */
91 /* free parent and reader lock */
92 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
95 rs->sr_err = TXN_ABORT( ltid );
98 op->o_do_not_cache = opinfo.boi_acl_cache;
99 if( rs->sr_err != 0 ) {
100 rs->sr_err = LDAP_OTHER;
101 rs->sr_text = "internal error";
104 ldap_pvt_thread_yield();
105 bdb_trans_backoff( ++num_retries );
108 /* begin transaction */
109 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
110 bdb->bi_db_opflags );
112 if( rs->sr_err != 0 ) {
113 Debug( LDAP_DEBUG_TRACE,
114 LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
115 db_strerror(rs->sr_err), rs->sr_err, 0 );
116 rs->sr_err = LDAP_OTHER;
117 rs->sr_text = "internal error";
121 locker = TXN_ID ( ltid );
123 opinfo.boi_bdb = op->o_bd;
124 opinfo.boi_txn = ltid;
125 opinfo.boi_locker = locker;
127 opinfo.boi_acl_cache = op->o_do_not_cache;
128 op->o_private = &opinfo;
131 * Get the parent dn and see if the corresponding entry exists.
133 if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
136 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
139 /* get entry or parent */
140 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
142 switch( rs->sr_err ) {
144 rs->sr_err = LDAP_ALREADY_EXISTS;
148 case DB_LOCK_DEADLOCK:
149 case DB_LOCK_NOTGRANTED:
152 rs->sr_text = "ldap server busy";
155 rs->sr_err = LDAP_OTHER;
156 rs->sr_text = "internal error";
162 if ( !bvmatch( &pdn, &p->e_nname ) ) {
163 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
165 rs->sr_ref = is_entry_referral( p )
166 ? get_entry_referrals( op, p )
168 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
170 Debug( LDAP_DEBUG_TRACE,
171 LDAP_XSTRING(bdb_add) ": parent "
172 "does not exist\n", 0, 0, 0 );
174 rs->sr_err = LDAP_REFERRAL;
175 send_ldap_result( op, rs );
177 ber_bvarray_free( rs->sr_ref );
178 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
180 rs->sr_matched = NULL;
185 rs->sr_err = access_allowed( op, p,
186 children, NULL, ACL_WRITE, NULL );
188 if ( ! rs->sr_err ) {
189 switch( opinfo.boi_err ) {
190 case DB_LOCK_DEADLOCK:
191 case DB_LOCK_NOTGRANTED:
195 Debug( LDAP_DEBUG_TRACE,
196 LDAP_XSTRING(bdb_add) ": no write access "
197 "to parent\n", 0, 0, 0 );
198 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
199 rs->sr_text = "no write access to parent";
200 goto return_results;;
203 #ifdef BDB_SUBENTRIES
204 if ( is_entry_subentry( p ) ) {
205 /* parent is a subentry, don't allow add */
206 Debug( LDAP_DEBUG_TRACE,
207 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
209 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
210 rs->sr_text = "parent is a subentry";
211 goto return_results;;
214 if ( is_entry_alias( p ) ) {
215 /* parent is an alias, don't allow add */
216 Debug( LDAP_DEBUG_TRACE,
217 LDAP_XSTRING(bdb_add) ": parent is alias\n",
219 rs->sr_err = LDAP_ALIAS_PROBLEM;
220 rs->sr_text = "parent is an alias";
221 goto return_results;;
224 if ( is_entry_referral( p ) ) {
225 /* parent is a referral, don't allow add */
226 rs->sr_matched = p->e_name.bv_val;
227 rs->sr_ref = get_entry_referrals( op, p );
229 Debug( LDAP_DEBUG_TRACE,
230 LDAP_XSTRING(bdb_add) ": parent is referral\n",
233 rs->sr_err = LDAP_REFERRAL;
234 send_ldap_result( op, rs );
236 ber_bvarray_free( rs->sr_ref );
237 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
239 rs->sr_matched = NULL;
244 #ifdef BDB_SUBENTRIES
247 /* parent must be an administrative point of the required kind */
251 /* free parent and reader lock */
252 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
258 * if not attempting to add entry at suffix or with parent ""
260 if ((( !be_isroot( op ) && !be_shadow_update(op) )
261 || pdn.bv_len > 0 ) && !is_entry_glue( op->oq_add.rs_e ))
263 Debug( LDAP_DEBUG_TRACE,
264 LDAP_XSTRING(bdb_add) ": %s denied\n",
265 pdn.bv_len == 0 ? "suffix" : "entry at root",
267 rs->sr_err = LDAP_NO_SUCH_OBJECT;
272 if ( get_assert( op ) &&
273 ( test_filter( op, op->oq_add.rs_e, get_assertion( op ))
274 != LDAP_COMPARE_TRUE ))
276 rs->sr_err = LDAP_ASSERTION_FAILED;
280 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
281 entry, NULL, ACL_WRITE, NULL );
283 if ( ! rs->sr_err ) {
284 switch( opinfo.boi_err ) {
285 case DB_LOCK_DEADLOCK:
286 case DB_LOCK_NOTGRANTED:
290 Debug( LDAP_DEBUG_TRACE,
291 LDAP_XSTRING(bdb_add) ": no write access to entry\n",
293 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
294 rs->sr_text = "no write access to entry";
295 goto return_results;;
298 /* nested transaction */
299 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
300 bdb->bi_db_opflags );
302 if( rs->sr_err != 0 ) {
303 Debug( LDAP_DEBUG_TRACE,
304 LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
305 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
306 rs->sr_err = LDAP_OTHER;
307 rs->sr_text = "internal error";
312 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
313 if ( rs->sr_err != 0 ) {
314 Debug( LDAP_DEBUG_TRACE,
315 LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
316 db_strerror(rs->sr_err), rs->sr_err, 0 );
318 switch( rs->sr_err ) {
319 case DB_LOCK_DEADLOCK:
320 case DB_LOCK_NOTGRANTED:
323 rs->sr_err = LDAP_ALREADY_EXISTS;
326 rs->sr_err = LDAP_OTHER;
332 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
333 if ( rs->sr_err != 0 ) {
334 Debug( LDAP_DEBUG_TRACE,
335 LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
337 switch( rs->sr_err ) {
338 case DB_LOCK_DEADLOCK:
339 case DB_LOCK_NOTGRANTED:
342 rs->sr_err = LDAP_OTHER;
344 rs->sr_text = "entry store failed";
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";
364 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
365 rs->sr_err = LDAP_OTHER;
366 rs->sr_text = "txn_commit(2) failed";
371 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
372 rc = bdb_csn_commit( op, rs, ltid, ei, &suffix_ei,
373 &ctxcsn_e, &ctxcsn_added, locker );
384 if( op->o_postread ) {
385 if( postread_ctrl == NULL ) {
386 postread_ctrl = &ctrls[num_ctrls++];
387 ctrls[num_ctrls] = NULL;
389 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
390 &slap_post_read_bv, postread_ctrl ) )
392 Debug( LDAP_DEBUG_TRACE,
393 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
394 "failed!\n", 0, 0, 0 );
400 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
401 rs->sr_text = "txn_abort (no-op) failed";
403 rs->sr_err = LDAP_NO_OPERATION;
409 Entry *e = entry_dup( op->ora_e );
412 nrdn.bv_val = e->e_nname.bv_val;
413 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
418 bdb_cache_add( bdb, ei, e, &nrdn, locker );
420 if ( suffix_ei == NULL ) {
425 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
426 if ( ctxcsn_added ) {
427 bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
428 (struct berval *)&slap_ldapsync_cn_bv, locker );
432 if ( rs->sr_err == LDAP_SUCCESS && !op->o_no_psearch ) {
433 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
435 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
436 rc = bdb_psearch( op, rs, ps_list, e, LDAP_PSEARCH_BY_ADD );
438 Debug( LDAP_DEBUG_TRACE,
439 LDAP_XSTRING(bdb_add)
440 ": persistent search failed "
445 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
449 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
450 rs->sr_text = "txn_commit failed";
452 rs->sr_err = LDAP_SUCCESS;
457 op->o_private = NULL;
459 if ( rs->sr_err != LDAP_SUCCESS ) {
460 Debug( LDAP_DEBUG_TRACE,
461 LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
462 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
463 rs->sr_err = LDAP_OTHER;
467 Debug(LDAP_DEBUG_TRACE,
468 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
469 op->o_noop ? " (no-op)" : "",
470 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
473 if( num_ctrls ) rs->sr_ctrls = ctrls;
476 send_ldap_result( op, rs );
478 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
479 ldap_pvt_thread_yield();
480 TXN_CHECKPOINT( bdb->bi_dbenv,
481 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
487 op->o_private = NULL;
490 if( postread_ctrl != NULL ) {
491 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
492 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );