1 /* add.c - ldap BerkeleyDB back-end add routine */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
17 bdb_add(Operation *op, SlapReply *rs )
19 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
23 char textbuf[SLAP_TEXT_BUFLEN];
24 size_t textlen = sizeof textbuf;
25 AttributeDescription *children = slap_schema.si_ad_children;
26 AttributeDescription *entry = slap_schema.si_ad_entry;
27 DB_TXN *ltid = NULL, *lt2;
28 struct bdb_op_info opinfo;
36 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
41 LDAP_LOG ( OPERATION, ARGS, "==> bdb_add: %s\n", op->oq_add.rs_e->e_name.bv_val, 0, 0 );
43 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", op->oq_add.rs_e->e_name.bv_val, 0, 0);
46 /* check entry's schema */
47 rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e, NULL, &rs->sr_text, textbuf, textlen );
48 if ( rs->sr_err != LDAP_SUCCESS ) {
50 LDAP_LOG ( OPERATION, ERR,
51 "bdb_add: entry failed schema check: %s (%d)\n", rs->sr_text, rs->sr_err, 0 );
53 Debug( LDAP_DEBUG_TRACE,
54 "bdb_add: entry failed schema check: %s (%d)\n",
55 rs->sr_text, rs->sr_err, 0 );
61 subentry = is_entry_subentry( op->oq_add.rs_e );
65 * acquire an ID outside of the operation transaction
66 * to avoid serializing adds.
68 rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
69 if( rs->sr_err != 0 ) {
71 LDAP_LOG ( OPERATION, ERR,
72 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
74 Debug( LDAP_DEBUG_TRACE,
75 "bdb_add: next_id failed (%d)\n",
78 rs->sr_err = LDAP_OTHER;
79 rs->sr_text = "internal error";
84 retry: /* transaction retry */
86 /* free parent and reader lock */
87 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
90 rs->sr_err = TXN_ABORT( ltid );
93 op->o_do_not_cache = opinfo.boi_acl_cache;
94 if( rs->sr_err != 0 ) {
95 rs->sr_err = LDAP_OTHER;
96 rs->sr_text = "internal error";
99 ldap_pvt_thread_yield();
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 ) {
108 LDAP_LOG ( OPERATION, ERR,
109 "bdb_add: txn_begin failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
111 Debug( LDAP_DEBUG_TRACE,
112 "bdb_add: txn_begin failed: %s (%d)\n",
113 db_strerror(rs->sr_err), rs->sr_err, 0 );
115 rs->sr_err = LDAP_OTHER;
116 rs->sr_text = "internal error";
120 locker = TXN_ID ( ltid );
122 opinfo.boi_bdb = op->o_bd;
123 opinfo.boi_txn = ltid;
124 opinfo.boi_locker = locker;
126 opinfo.boi_acl_cache = op->o_do_not_cache;
127 op->o_private = &opinfo;
130 * Get the parent dn and see if the corresponding entry exists.
131 * If the parent does not exist, only allow the "root" user to
134 if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
137 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
140 /* get entry or parent */
141 rs->sr_err = bdb_dn2entry( op->o_bd, ltid, &op->ora_e->e_nname, &ei,
142 1, locker, &lock, op->o_tmpmemctx );
143 switch( rs->sr_err ) {
145 rs->sr_err = LDAP_ALREADY_EXISTS;
149 case DB_LOCK_DEADLOCK:
150 case DB_LOCK_NOTGRANTED:
153 rs->sr_text = "ldap server busy";
156 rs->sr_err = LDAP_OTHER;
157 rs->sr_text = "internal error";
163 if ( !bvmatch( &pdn, &p->e_nname ) ) {
164 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
166 rs->sr_ref = is_entry_referral( p )
167 ? get_entry_referrals( op, p )
169 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
172 LDAP_LOG ( OPERATION, DETAIL1,
173 "bdb_add: parent does not exist\n", 0, 0, 0 );
175 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
179 rs->sr_err = LDAP_REFERRAL;
180 send_ldap_result( op, rs );
182 ber_bvarray_free( rs->sr_ref );
183 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
185 rs->sr_matched = NULL;
190 rs->sr_err = access_allowed( op, p,
191 children, NULL, ACL_WRITE, NULL );
193 if ( ! rs->sr_err ) {
194 switch( opinfo.boi_err ) {
195 case DB_LOCK_DEADLOCK:
196 case DB_LOCK_NOTGRANTED:
201 LDAP_LOG ( OPERATION, DETAIL1,
202 "bdb_add: no write access to parent\n", 0, 0, 0 );
204 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
207 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
208 rs->sr_text = "no write access to parent";
209 goto return_results;;
212 #ifdef BDB_SUBENTRIES
213 if ( is_entry_subentry( p ) ) {
214 /* parent is a subentry, don't allow add */
216 LDAP_LOG ( OPERATION, DETAIL1,
217 "bdb_add: parent is subentry\n", 0, 0, 0 );
219 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
222 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
223 rs->sr_text = "parent is a subentry";
224 goto return_results;;
228 if ( is_entry_alias( p ) ) {
229 /* parent is an alias, don't allow add */
231 LDAP_LOG ( OPERATION, DETAIL1,
232 "bdb_add: parent is alias\n", 0, 0, 0 );
234 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
237 rs->sr_err = LDAP_ALIAS_PROBLEM;
238 rs->sr_text = "parent is an alias";
239 goto return_results;;
243 if ( is_entry_referral( p ) ) {
244 /* parent is a referral, don't allow add */
245 rs->sr_matched = p->e_name.bv_val;
246 rs->sr_ref = get_entry_referrals( op, p );
249 LDAP_LOG ( OPERATION, DETAIL1,
250 "bdb_add: parent is referral\n", 0, 0, 0 );
252 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
256 rs->sr_err = LDAP_REFERRAL;
257 send_ldap_result( op, rs );
259 ber_bvarray_free( rs->sr_ref );
260 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
262 rs->sr_matched = NULL;
267 #ifdef BDB_SUBENTRIES
270 /* parent must be an administrative point of the required kind */
274 /* free parent and reader lock */
275 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
281 * must be adding entry at suffix or with parent ""
283 if ( !be_isroot( op->o_bd, &op->o_ndn )) {
284 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
285 || be_isupdate( op->o_bd, &op->o_ndn ) )
287 p = (Entry *)&slap_entry_root;
289 /* check parent for "children" acl */
290 rs->sr_err = access_allowed( op, p,
291 children, NULL, ACL_WRITE, NULL );
295 if ( ! rs->sr_err ) {
296 switch( opinfo.boi_err ) {
297 case DB_LOCK_DEADLOCK:
298 case DB_LOCK_NOTGRANTED:
303 LDAP_LOG ( OPERATION, DETAIL1,
304 "bdb_add: no write access to parent\n", 0, 0, 0 );
306 Debug( LDAP_DEBUG_TRACE,
307 "bdb_add: no write access to parent\n",
310 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
311 rs->sr_text = "no write access to parent";
312 goto return_results;;
317 LDAP_LOG ( OPERATION, DETAIL1, "bdb_add: %s denied\n",
318 pdn.bv_len == 0 ? "suffix" : "entry at root", 0, 0 );
320 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
321 pdn.bv_len == 0 ? "suffix" : "entry at root",
324 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
329 #ifdef BDB_SUBENTRIES
332 LDAP_LOG ( OPERATION, DETAIL1,
333 "bdb_add: no parent, cannot add subentry\n", 0, 0, 0 );
335 Debug( LDAP_DEBUG_TRACE,
336 "bdb_add: no parent, cannot add subentry\n",
339 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
340 rs->sr_text = "no parent, cannot add subentry";
341 goto return_results;;
346 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
347 entry, NULL, ACL_WRITE, NULL );
349 if ( ! rs->sr_err ) {
350 switch( opinfo.boi_err ) {
351 case DB_LOCK_DEADLOCK:
352 case DB_LOCK_NOTGRANTED:
357 LDAP_LOG ( OPERATION, DETAIL1,
358 "bdb_add: no write access to entry\n", 0, 0, 0 );
360 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to entry\n",
363 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
364 rs->sr_text = "no write access to entry";
365 goto return_results;;
368 /* nested transaction */
369 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
370 bdb->bi_db_opflags );
372 if( rs->sr_err != 0 ) {
374 LDAP_LOG ( OPERATION, ERR,
375 "bdb_add: txn_begin(2) failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
377 Debug( LDAP_DEBUG_TRACE,
378 "bdb_add: txn_begin(2) failed: %s (%d)\n",
379 db_strerror(rs->sr_err), rs->sr_err, 0 );
381 rs->sr_err = LDAP_OTHER;
382 rs->sr_text = "internal error";
387 rs->sr_err = bdb_dn2id_add( op->o_bd, lt2, ei, op->oq_add.rs_e,
389 if ( rs->sr_err != 0 ) {
391 LDAP_LOG ( OPERATION, ERR,
392 "bdb_add: dn2id_add failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
394 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
395 db_strerror(rs->sr_err), rs->sr_err, 0 );
398 switch( rs->sr_err ) {
399 case DB_LOCK_DEADLOCK:
400 case DB_LOCK_NOTGRANTED:
403 rs->sr_err = LDAP_ALREADY_EXISTS;
406 rs->sr_err = LDAP_OTHER;
412 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
413 if ( rs->sr_err != 0 ) {
415 LDAP_LOG ( OPERATION, ERR, "bdb_add: id2entry_add failed\n", 0, 0, 0 );
417 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
420 switch( rs->sr_err ) {
421 case DB_LOCK_DEADLOCK:
422 case DB_LOCK_NOTGRANTED:
425 rs->sr_err = LDAP_OTHER;
427 rs->sr_text = "entry store failed";
431 /* attribute indexes */
432 rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
433 if ( rs->sr_err != LDAP_SUCCESS ) {
435 LDAP_LOG ( OPERATION, ERR,
436 "bdb_add: index_entry_add failed\n", 0, 0, 0 );
438 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
441 switch( rs->sr_err ) {
442 case DB_LOCK_DEADLOCK:
443 case DB_LOCK_NOTGRANTED:
446 rs->sr_err = LDAP_OTHER;
448 rs->sr_text = "index generation failed";
451 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
452 rs->sr_err = LDAP_OTHER;
453 rs->sr_text = "txn_commit(2) failed";
458 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
459 rs->sr_text = "txn_abort (no-op) failed";
462 rs->sr_err = LDAP_SUCCESS;
466 char gid[DB_XIDDATASIZE];
468 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
469 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
471 if (( rs->sr_err=TXN_PREPARE( ltid, gid )) != 0 ) {
472 rs->sr_text = "txn_prepare failed";
478 nrdn.bv_val = op->ora_e->e_nname.bv_val;
479 nrdn.bv_len = pdn.bv_val - nrdn.bv_val - 1;
481 nrdn = op->ora_e->e_nname;
484 bdb_cache_add(bdb, ei, op->oq_add.rs_e, &nrdn, locker );
486 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
487 rs->sr_text = "txn_commit failed";
489 rs->sr_err = LDAP_SUCCESS;
495 op->o_private = NULL;
497 if (rs->sr_err == LDAP_SUCCESS) {
499 LDAP_LOG ( OPERATION, RESULTS,
500 "bdb_add: added%s id=%08lx dn=\"%s\"\n",
501 op->o_noop ? " (no-op)" : "", op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
503 Debug(LDAP_DEBUG_TRACE, "bdb_add: added%s id=%08lx dn=\"%s\"\n",
504 op->o_noop ? " (no-op)" : "", op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
510 LDAP_LOG ( OPERATION, ERR,
511 "bdb_add: %s : %s (%d)\n", rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
513 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
514 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
516 rs->sr_err = LDAP_OTHER;
520 send_ldap_result( op, rs );
522 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
523 if ( rs->sr_err == LDAP_SUCCESS && !noop ) {
524 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
525 bdb_psearch( op, rs, ps_list, op->oq_add.rs_e, LDAP_PSEARCH_BY_ADD );
528 #endif /* LDAP_CLIENT_UPDATE */
530 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
531 ldap_pvt_thread_yield();
532 TXN_CHECKPOINT( bdb->bi_dbenv,
533 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
540 op->o_private = NULL;
543 return ( ( rs->sr_err == LDAP_SUCCESS ) ? noop : rs->sr_err );