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};
45 LDAPControl **postread_ctrl = NULL;
46 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
49 Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_add) ": %s\n",
50 op->oq_add.rs_e->e_name.bv_val, 0, 0);
54 /* check entry's schema */
55 rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e,
56 NULL, &rs->sr_text, textbuf, textlen );
57 if ( rs->sr_err != LDAP_SUCCESS ) {
58 Debug( LDAP_DEBUG_TRACE,
59 LDAP_XSTRING(bdb_add) ": entry failed schema check: "
60 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
65 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 ldap_pvt_thread_yield();
99 bdb_trans_backoff( ++num_retries );
102 /* begin transaction */
103 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
104 bdb->bi_db_opflags );
106 if( rs->sr_err != 0 ) {
107 Debug( LDAP_DEBUG_TRACE,
108 LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
109 db_strerror(rs->sr_err), rs->sr_err, 0 );
110 rs->sr_err = LDAP_OTHER;
111 rs->sr_text = "internal error";
115 locker = TXN_ID ( ltid );
117 opinfo.boi_bdb = op->o_bd;
118 opinfo.boi_txn = ltid;
119 opinfo.boi_locker = locker;
121 opinfo.boi_acl_cache = op->o_do_not_cache;
122 op->o_private = &opinfo;
125 * Get the parent dn and see if the corresponding entry exists.
127 if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
130 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
133 /* get entry or parent */
134 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
136 switch( rs->sr_err ) {
138 rs->sr_err = LDAP_ALREADY_EXISTS;
142 case DB_LOCK_DEADLOCK:
143 case DB_LOCK_NOTGRANTED:
146 rs->sr_text = "ldap server busy";
149 rs->sr_err = LDAP_OTHER;
150 rs->sr_text = "internal error";
156 if ( !bvmatch( &pdn, &p->e_nname ) ) {
157 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
159 rs->sr_ref = is_entry_referral( p )
160 ? get_entry_referrals( op, p )
162 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
164 Debug( LDAP_DEBUG_TRACE,
165 LDAP_XSTRING(bdb_add) ": parent "
166 "does not exist\n", 0, 0, 0 );
168 rs->sr_err = LDAP_REFERRAL;
169 send_ldap_result( op, rs );
171 ber_bvarray_free( rs->sr_ref );
172 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
174 rs->sr_matched = NULL;
179 rs->sr_err = access_allowed( op, p,
180 children, NULL, ACL_WADD, NULL );
182 if ( ! rs->sr_err ) {
183 switch( opinfo.boi_err ) {
184 case DB_LOCK_DEADLOCK:
185 case DB_LOCK_NOTGRANTED:
189 Debug( LDAP_DEBUG_TRACE,
190 LDAP_XSTRING(bdb_add) ": no write access "
191 "to parent\n", 0, 0, 0 );
192 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
193 rs->sr_text = "no write access to parent";
194 goto return_results;;
197 #ifdef BDB_SUBENTRIES
198 if ( is_entry_subentry( p ) ) {
199 /* parent is a subentry, don't allow add */
200 Debug( LDAP_DEBUG_TRACE,
201 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
203 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
204 rs->sr_text = "parent is a subentry";
205 goto return_results;;
208 if ( is_entry_alias( p ) ) {
209 /* parent is an alias, don't allow add */
210 Debug( LDAP_DEBUG_TRACE,
211 LDAP_XSTRING(bdb_add) ": parent is alias\n",
213 rs->sr_err = LDAP_ALIAS_PROBLEM;
214 rs->sr_text = "parent is an alias";
215 goto return_results;;
218 if ( is_entry_referral( p ) ) {
219 /* parent is a referral, don't allow add */
220 rs->sr_matched = p->e_name.bv_val;
221 rs->sr_ref = get_entry_referrals( op, p );
223 Debug( LDAP_DEBUG_TRACE,
224 LDAP_XSTRING(bdb_add) ": parent is referral\n",
227 rs->sr_err = LDAP_REFERRAL;
228 send_ldap_result( op, rs );
230 ber_bvarray_free( rs->sr_ref );
231 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
233 rs->sr_matched = NULL;
238 #ifdef BDB_SUBENTRIES
241 /* parent must be an administrative point of the required kind */
245 /* free parent and reader lock */
246 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
252 * if not attempting to add entry at suffix or with parent ""
254 if ((( !be_isroot( op ) && !be_shadow_update(op) )
255 || pdn.bv_len > 0 ) && !is_entry_glue( op->oq_add.rs_e ))
257 Debug( LDAP_DEBUG_TRACE,
258 LDAP_XSTRING(bdb_add) ": %s denied\n",
259 pdn.bv_len == 0 ? "suffix" : "entry at root",
261 rs->sr_err = LDAP_NO_SUCH_OBJECT;
266 if ( get_assert( op ) &&
267 ( test_filter( op, op->oq_add.rs_e, get_assertion( op ))
268 != LDAP_COMPARE_TRUE ))
270 rs->sr_err = LDAP_ASSERTION_FAILED;
274 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
275 entry, NULL, ACL_WADD, NULL );
277 if ( ! rs->sr_err ) {
278 switch( opinfo.boi_err ) {
279 case DB_LOCK_DEADLOCK:
280 case DB_LOCK_NOTGRANTED:
284 Debug( LDAP_DEBUG_TRACE,
285 LDAP_XSTRING(bdb_add) ": no write access to entry\n",
287 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
288 rs->sr_text = "no write access to entry";
289 goto return_results;;
292 /* nested transaction */
293 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
294 bdb->bi_db_opflags );
296 if( rs->sr_err != 0 ) {
297 Debug( LDAP_DEBUG_TRACE,
298 LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
299 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
300 rs->sr_err = LDAP_OTHER;
301 rs->sr_text = "internal error";
306 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
307 if ( rs->sr_err != 0 ) {
308 Debug( LDAP_DEBUG_TRACE,
309 LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
310 db_strerror(rs->sr_err), rs->sr_err, 0 );
312 switch( rs->sr_err ) {
313 case DB_LOCK_DEADLOCK:
314 case DB_LOCK_NOTGRANTED:
317 rs->sr_err = LDAP_ALREADY_EXISTS;
320 rs->sr_err = LDAP_OTHER;
326 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
327 if ( rs->sr_err != 0 ) {
328 Debug( LDAP_DEBUG_TRACE,
329 LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
331 switch( rs->sr_err ) {
332 case DB_LOCK_DEADLOCK:
333 case DB_LOCK_NOTGRANTED:
336 rs->sr_err = LDAP_OTHER;
338 rs->sr_text = "entry store failed";
342 /* attribute indexes */
343 rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
344 if ( rs->sr_err != LDAP_SUCCESS ) {
345 Debug( LDAP_DEBUG_TRACE,
346 LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
348 switch( rs->sr_err ) {
349 case DB_LOCK_DEADLOCK:
350 case DB_LOCK_NOTGRANTED:
353 rs->sr_err = LDAP_OTHER;
355 rs->sr_text = "index generation failed";
358 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
359 rs->sr_err = LDAP_OTHER;
360 rs->sr_text = "txn_commit(2) failed";
365 if( op->o_postread ) {
366 if( postread_ctrl == NULL ) {
367 postread_ctrl = &ctrls[num_ctrls++];
368 ctrls[num_ctrls] = NULL;
370 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
371 &slap_post_read_bv, postread_ctrl ) )
373 Debug( LDAP_DEBUG_TRACE,
374 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
375 "failed!\n", 0, 0, 0 );
381 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
382 rs->sr_text = "txn_abort (no-op) failed";
384 rs->sr_err = LDAP_NO_OPERATION;
390 Entry *e = entry_dup( op->ora_e );
393 nrdn.bv_val = e->e_nname.bv_val;
394 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
399 bdb_cache_add( bdb, ei, e, &nrdn, locker );
401 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
402 rs->sr_text = "txn_commit failed";
404 rs->sr_err = LDAP_SUCCESS;
409 op->o_private = NULL;
411 if ( rs->sr_err != LDAP_SUCCESS ) {
412 Debug( LDAP_DEBUG_TRACE,
413 LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
414 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
415 rs->sr_err = LDAP_OTHER;
419 Debug(LDAP_DEBUG_TRACE,
420 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
421 op->o_noop ? " (no-op)" : "",
422 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
425 if( num_ctrls ) rs->sr_ctrls = ctrls;
428 send_ldap_result( op, rs );
430 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
431 ldap_pvt_thread_yield();
432 TXN_CHECKPOINT( bdb->bi_dbenv,
433 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
439 op->o_private = NULL;
442 if( postread_ctrl != NULL ) {
443 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
444 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );