1 /* add.c - ldap BerkeleyDB back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 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};
43 LDAPControl **postread_ctrl = NULL;
44 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
47 Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_add) ": %s\n",
48 op->oq_add.rs_e->e_name.bv_val, 0, 0);
52 /* add opattrs to shadow as well, only missing attrs will actually
53 * be added; helps compatibility with older OL versions */
54 slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
56 /* check entry's schema */
57 rs->sr_err = entry_schema_check( op, op->oq_add.rs_e, NULL,
58 get_manageDIT(op), &rs->sr_text, textbuf, textlen );
59 if ( rs->sr_err != LDAP_SUCCESS ) {
60 Debug( LDAP_DEBUG_TRACE,
61 LDAP_XSTRING(bdb_add) ": entry failed schema check: "
62 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
66 subentry = is_entry_subentry( op->oq_add.rs_e );
69 * acquire an ID outside of the operation transaction
70 * to avoid serializing adds.
72 rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
73 if( rs->sr_err != 0 ) {
74 Debug( LDAP_DEBUG_TRACE,
75 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
77 rs->sr_err = LDAP_OTHER;
78 rs->sr_text = "internal error";
83 retry: /* transaction retry */
85 /* free parent and reader lock */
86 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
89 rs->sr_err = TXN_ABORT( ltid );
92 op->o_do_not_cache = opinfo.boi_acl_cache;
93 if( rs->sr_err != 0 ) {
94 rs->sr_err = LDAP_OTHER;
95 rs->sr_text = "internal error";
98 if ( op->o_abandon ) {
99 rs->sr_err = SLAPD_ABANDON;
102 ldap_pvt_thread_yield();
103 bdb_trans_backoff( ++num_retries );
106 /* begin transaction */
107 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
108 bdb->bi_db_opflags );
110 if( rs->sr_err != 0 ) {
111 Debug( LDAP_DEBUG_TRACE,
112 LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
113 db_strerror(rs->sr_err), rs->sr_err, 0 );
114 rs->sr_err = LDAP_OTHER;
115 rs->sr_text = "internal error";
119 locker = TXN_ID ( ltid );
121 opinfo.boi_bdb = op->o_bd;
122 opinfo.boi_txn = ltid;
123 opinfo.boi_locker = locker;
125 opinfo.boi_acl_cache = op->o_do_not_cache;
126 op->o_private = &opinfo;
129 * Get the parent dn and see if the corresponding entry exists.
131 if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
134 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
137 /* get entry or parent */
138 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
140 switch( rs->sr_err ) {
142 rs->sr_err = LDAP_ALREADY_EXISTS;
146 case DB_LOCK_DEADLOCK:
147 case DB_LOCK_NOTGRANTED:
150 rs->sr_text = "ldap server busy";
153 rs->sr_err = LDAP_OTHER;
154 rs->sr_text = "internal error";
160 if ( !bvmatch( &pdn, &p->e_nname ) ) {
161 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
163 rs->sr_ref = is_entry_referral( p )
164 ? get_entry_referrals( op, p )
166 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
168 Debug( LDAP_DEBUG_TRACE,
169 LDAP_XSTRING(bdb_add) ": parent "
170 "does not exist\n", 0, 0, 0 );
172 rs->sr_err = LDAP_REFERRAL;
173 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
177 rs->sr_err = access_allowed( op, p,
178 children, NULL, ACL_WADD, NULL );
180 if ( ! rs->sr_err ) {
181 switch( opinfo.boi_err ) {
182 case DB_LOCK_DEADLOCK:
183 case DB_LOCK_NOTGRANTED:
187 Debug( LDAP_DEBUG_TRACE,
188 LDAP_XSTRING(bdb_add) ": no write access to parent\n",
190 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
191 rs->sr_text = "no write access to parent";
192 goto return_results;;
195 if ( is_entry_subentry( p ) ) {
196 /* parent is a subentry, don't allow add */
197 Debug( LDAP_DEBUG_TRACE,
198 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
200 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
201 rs->sr_text = "parent is a subentry";
202 goto return_results;;
204 if ( is_entry_alias( p ) ) {
205 /* parent is an alias, don't allow add */
206 Debug( LDAP_DEBUG_TRACE,
207 LDAP_XSTRING(bdb_add) ": parent is alias\n",
209 rs->sr_err = LDAP_ALIAS_PROBLEM;
210 rs->sr_text = "parent is an alias";
211 goto return_results;;
214 if ( is_entry_referral( p ) ) {
215 /* parent is a referral, don't allow add */
216 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
218 rs->sr_ref = get_entry_referrals( op, p );
219 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
221 Debug( LDAP_DEBUG_TRACE,
222 LDAP_XSTRING(bdb_add) ": parent is referral\n",
225 rs->sr_err = LDAP_REFERRAL;
226 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
232 /* parent must be an administrative point of the required kind */
235 /* free parent and reader lock */
236 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
242 * if not attempting to add entry at suffix or with parent ""
244 if ((( !be_isroot( op ) && !be_shadow_update(op) )
245 || pdn.bv_len > 0 ) && !is_entry_glue( op->oq_add.rs_e ))
247 Debug( LDAP_DEBUG_TRACE,
248 LDAP_XSTRING(bdb_add) ": %s denied\n",
249 pdn.bv_len == 0 ? "suffix" : "entry at root",
251 rs->sr_err = LDAP_NO_SUCH_OBJECT;
256 if ( get_assert( op ) &&
257 ( test_filter( op, op->oq_add.rs_e, get_assertion( op ))
258 != LDAP_COMPARE_TRUE ))
260 rs->sr_err = LDAP_ASSERTION_FAILED;
264 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
265 entry, NULL, ACL_WADD, NULL );
267 if ( ! rs->sr_err ) {
268 switch( opinfo.boi_err ) {
269 case DB_LOCK_DEADLOCK:
270 case DB_LOCK_NOTGRANTED:
274 Debug( LDAP_DEBUG_TRACE,
275 LDAP_XSTRING(bdb_add) ": no write access to entry\n",
277 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
278 rs->sr_text = "no write access to entry";
279 goto return_results;;
282 /* nested transaction */
283 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
284 bdb->bi_db_opflags );
286 if( rs->sr_err != 0 ) {
287 Debug( LDAP_DEBUG_TRACE,
288 LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
289 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
290 rs->sr_err = LDAP_OTHER;
291 rs->sr_text = "internal error";
296 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
297 if ( rs->sr_err != 0 ) {
298 Debug( LDAP_DEBUG_TRACE,
299 LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
300 db_strerror(rs->sr_err), rs->sr_err, 0 );
302 switch( rs->sr_err ) {
303 case DB_LOCK_DEADLOCK:
304 case DB_LOCK_NOTGRANTED:
307 rs->sr_err = LDAP_ALREADY_EXISTS;
310 rs->sr_err = LDAP_OTHER;
315 /* attribute indexes */
316 rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
317 if ( rs->sr_err != LDAP_SUCCESS ) {
318 Debug( LDAP_DEBUG_TRACE,
319 LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
321 switch( rs->sr_err ) {
322 case DB_LOCK_DEADLOCK:
323 case DB_LOCK_NOTGRANTED:
326 rs->sr_err = LDAP_OTHER;
328 rs->sr_text = "index generation failed";
333 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
334 if ( rs->sr_err != 0 ) {
335 Debug( LDAP_DEBUG_TRACE,
336 LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
338 switch( rs->sr_err ) {
339 case DB_LOCK_DEADLOCK:
340 case DB_LOCK_NOTGRANTED:
343 rs->sr_err = LDAP_OTHER;
345 rs->sr_text = "entry store failed";
349 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
350 rs->sr_err = LDAP_OTHER;
351 rs->sr_text = "txn_commit(2) failed";
356 if( op->o_postread ) {
357 if( postread_ctrl == NULL ) {
358 postread_ctrl = &ctrls[num_ctrls++];
359 ctrls[num_ctrls] = NULL;
361 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
362 &slap_post_read_bv, postread_ctrl ) )
364 Debug( LDAP_DEBUG_TRACE,
365 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
366 "failed!\n", 0, 0, 0 );
372 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
373 rs->sr_text = "txn_abort (no-op) failed";
375 rs->sr_err = LDAP_X_NO_OPERATION;
382 Entry *e = entry_dup( op->ora_e );
384 /* pick the RDN if not suffix; otherwise pick the entire DN */
386 nrdn.bv_val = e->e_nname.bv_val;
387 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
392 bdb_cache_add( bdb, ei, e, &nrdn, locker );
394 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
395 rs->sr_text = "txn_commit failed";
397 rs->sr_err = LDAP_SUCCESS;
402 op->o_private = NULL;
404 if ( rs->sr_err != LDAP_SUCCESS ) {
405 Debug( LDAP_DEBUG_TRACE,
406 LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
407 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
408 rs->sr_err = LDAP_OTHER;
412 Debug(LDAP_DEBUG_TRACE,
413 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
414 op->o_noop ? " (no-op)" : "",
415 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
418 if( num_ctrls ) rs->sr_ctrls = ctrls;
421 send_ldap_result( op, rs );
422 if ( !SLAP_SHADOW( op->o_bd ))
423 slap_graduate_commit_csn( op );
428 op->o_private = NULL;
430 if( postread_ctrl != NULL ) {
431 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
432 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
435 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
436 ldap_pvt_thread_yield();
437 TXN_CHECKPOINT( bdb->bi_dbenv,
438 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );