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;
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 if( pdn.bv_len != 0 ) {
141 Entry *matched = NULL;
144 rs->sr_err = bdb_dn2entry_r( op->o_bd, ltid, &pdn, &p, &matched, 0, locker, &lock );
146 switch( rs->sr_err ) {
150 case DB_LOCK_DEADLOCK:
151 case DB_LOCK_NOTGRANTED:
154 rs->sr_text = "ldap server busy";
157 rs->sr_err = LDAP_OTHER;
158 rs->sr_text = "internal error";
163 if ( matched != NULL ) {
164 rs->sr_matched = ch_strdup( matched->e_dn );
165 rs->sr_ref = is_entry_referral( matched )
166 ? get_entry_referrals( op, matched )
168 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, matched );
172 rs->sr_ref = referral_rewrite( default_referral,
173 NULL, &op->oq_add.rs_e->e_name, LDAP_SCOPE_DEFAULT );
177 LDAP_LOG ( OPERATION, DETAIL1,
178 "bdb_add: parent does not exist\n", 0, 0, 0 );
180 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
184 rs->sr_err = LDAP_REFERRAL;
185 send_ldap_result( op, rs );
187 ber_bvarray_free( rs->sr_ref );
188 ch_free( (char *)rs->sr_matched );
190 rs->sr_matched = NULL;
195 rs->sr_err = access_allowed( op, p,
196 children, NULL, ACL_WRITE, NULL );
198 if ( ! rs->sr_err ) {
199 switch( opinfo.boi_err ) {
200 case DB_LOCK_DEADLOCK:
201 case DB_LOCK_NOTGRANTED:
206 LDAP_LOG ( OPERATION, DETAIL1,
207 "bdb_add: no write access to parent\n", 0, 0, 0 );
209 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
212 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
213 rs->sr_text = "no write access to parent";
214 goto return_results;;
217 #ifdef BDB_SUBENTRIES
218 if ( is_entry_subentry( p ) ) {
219 /* parent is a subentry, don't allow add */
221 LDAP_LOG ( OPERATION, DETAIL1,
222 "bdb_add: parent is subentry\n", 0, 0, 0 );
224 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
227 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
228 rs->sr_text = "parent is a subentry";
229 goto return_results;;
233 if ( is_entry_alias( p ) ) {
234 /* parent is an alias, don't allow add */
236 LDAP_LOG ( OPERATION, DETAIL1,
237 "bdb_add: parent is alias\n", 0, 0, 0 );
239 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
242 rs->sr_err = LDAP_ALIAS_PROBLEM;
243 rs->sr_text = "parent is an alias";
244 goto return_results;;
248 if ( is_entry_referral( p ) ) {
249 /* parent is a referral, don't allow add */
250 rs->sr_matched = p->e_name.bv_val;
251 rs->sr_ref = get_entry_referrals( op, p );
254 LDAP_LOG ( OPERATION, DETAIL1,
255 "bdb_add: parent is referral\n", 0, 0, 0 );
257 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
261 rs->sr_err = LDAP_REFERRAL;
262 send_ldap_result( op, rs );
264 ber_bvarray_free( rs->sr_ref );
265 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
267 rs->sr_matched = NULL;
272 #ifdef BDB_SUBENTRIES
275 /* parent must be an administrative point of the required kind */
279 /* free parent and reader lock */
280 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
286 * must be adding entry at suffix or with parent ""
288 if ( !be_isroot( op->o_bd, &op->o_ndn )) {
289 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
290 || be_isupdate( op->o_bd, &op->o_ndn ) )
292 p = (Entry *)&slap_entry_root;
294 /* check parent for "children" acl */
295 rs->sr_err = access_allowed( op, p,
296 children, NULL, ACL_WRITE, NULL );
300 if ( ! rs->sr_err ) {
301 switch( opinfo.boi_err ) {
302 case DB_LOCK_DEADLOCK:
303 case DB_LOCK_NOTGRANTED:
308 LDAP_LOG ( OPERATION, DETAIL1,
309 "bdb_add: no write access to parent\n", 0, 0, 0 );
311 Debug( LDAP_DEBUG_TRACE,
312 "bdb_add: no write access to parent\n",
315 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
316 rs->sr_text = "no write access to parent";
317 goto return_results;;
322 LDAP_LOG ( OPERATION, DETAIL1, "bdb_add: %s denied\n",
323 pdn.bv_len == 0 ? "suffix" : "entry at root", 0, 0 );
325 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
326 pdn.bv_len == 0 ? "suffix" : "entry at root",
329 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
334 #ifdef BDB_SUBENTRIES
337 LDAP_LOG ( OPERATION, DETAIL1,
338 "bdb_add: no parent, cannot add subentry\n", 0, 0, 0 );
340 Debug( LDAP_DEBUG_TRACE,
341 "bdb_add: no parent, cannot add subentry\n",
344 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
345 rs->sr_text = "no parent, cannot add subentry";
346 goto return_results;;
351 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
352 entry, NULL, ACL_WRITE, NULL );
354 if ( ! rs->sr_err ) {
355 switch( opinfo.boi_err ) {
356 case DB_LOCK_DEADLOCK:
357 case DB_LOCK_NOTGRANTED:
362 LDAP_LOG ( OPERATION, DETAIL1,
363 "bdb_add: no write access to entry\n", 0, 0, 0 );
365 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to entry\n",
368 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
369 rs->sr_text = "no write access to entry";
370 goto return_results;;
374 rs->sr_err = bdb_dn2id_add( op->o_bd, ltid, &pdn, op->oq_add.rs_e );
375 if ( rs->sr_err != 0 ) {
377 LDAP_LOG ( OPERATION, ERR,
378 "bdb_add: dn2id_add failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
380 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
381 db_strerror(rs->sr_err), rs->sr_err, 0 );
384 switch( rs->sr_err ) {
385 case DB_LOCK_DEADLOCK:
386 case DB_LOCK_NOTGRANTED:
389 rs->sr_err = LDAP_ALREADY_EXISTS;
392 rs->sr_err = LDAP_OTHER;
398 rs->sr_err = bdb_id2entry_add( op->o_bd, ltid, op->oq_add.rs_e );
399 if ( rs->sr_err != 0 ) {
401 LDAP_LOG ( OPERATION, ERR, "bdb_add: id2entry_add failed\n", 0, 0, 0 );
403 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
406 switch( rs->sr_err ) {
407 case DB_LOCK_DEADLOCK:
408 case DB_LOCK_NOTGRANTED:
411 rs->sr_err = LDAP_OTHER;
413 rs->sr_text = "entry store failed";
417 /* attribute indexes */
418 rs->sr_err = bdb_index_entry_add( op->o_bd, ltid, op->oq_add.rs_e );
419 if ( rs->sr_err != LDAP_SUCCESS ) {
421 LDAP_LOG ( OPERATION, ERR,
422 "bdb_add: index_entry_add failed\n", 0, 0, 0 );
424 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
427 switch( rs->sr_err ) {
428 case DB_LOCK_DEADLOCK:
429 case DB_LOCK_NOTGRANTED:
432 rs->sr_err = LDAP_OTHER;
434 rs->sr_text = "index generation failed";
440 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
441 rs->sr_text = "txn_abort (no-op) failed";
444 rs->sr_err = LDAP_SUCCESS;
448 char gid[DB_XIDDATASIZE];
450 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
451 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
453 if (( rs->sr_err=TXN_PREPARE( ltid, gid )) != 0 ) {
454 rs->sr_text = "txn_prepare failed";
457 int ret = bdb_cache_add_entry_rw(bdb->bi_dbenv,
458 &bdb->bi_cache, op->oq_add.rs_e, CACHE_WRITE_LOCK,
463 case DB_LOCK_DEADLOCK:
464 case DB_LOCK_NOTGRANTED:
471 if(( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
472 rs->sr_text = "cache add & txn_abort failed";
474 rs->sr_err = LDAP_OTHER;
475 rs->sr_text = "cache add failed";
478 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
479 rs->sr_text = "txn_commit failed";
481 rs->sr_err = LDAP_SUCCESS;
488 op->o_private = NULL;
490 if (rs->sr_err == LDAP_SUCCESS) {
492 LDAP_LOG ( OPERATION, RESULTS,
493 "bdb_add: added%s id=%08lx dn=\"%s\"\n",
494 op->o_noop ? " (no-op)" : "", op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
496 Debug(LDAP_DEBUG_TRACE, "bdb_add: added%s id=%08lx dn=\"%s\"\n",
497 op->o_noop ? " (no-op)" : "", op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
501 bdb_cache_entry_commit( op->oq_add.rs_e );
506 LDAP_LOG ( OPERATION, ERR,
507 "bdb_add: %s : %s (%d)\n", rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
509 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
510 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
512 rs->sr_err = LDAP_OTHER;
516 send_ldap_result( op, rs );
518 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
519 if ( rs->sr_err == LDAP_SUCCESS && !noop ) {
520 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
521 bdb_psearch( op, rs, ps_list, op->oq_add.rs_e, LDAP_PSEARCH_BY_ADD );
524 #endif /* LDAP_CLIENT_UPDATE */
526 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
527 ldap_pvt_thread_yield();
528 TXN_CHECKPOINT( bdb->bi_dbenv,
529 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
536 op->o_private = NULL;
539 return ( ( rs->sr_err == LDAP_SUCCESS ) ? noop : rs->sr_err );