1 /* add.c - ldap BerkeleyDB back-end add routine */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
23 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
28 char textbuf[SLAP_TEXT_BUFLEN];
29 size_t textlen = sizeof textbuf;
30 AttributeDescription *children = slap_schema.si_ad_children;
32 struct bdb_op_info opinfo;
42 LDAP_LOG (( "add", LDAP_LEVEL_ARGS, "==> bdb_add: %s\n", e->e_dn ));
44 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
47 /* check entry's schema */
48 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
49 if ( rc != LDAP_SUCCESS ) {
51 LDAP_LOG (( "add", LDAP_LEVEL_ERR, "bdb_add: entry failed schema check: %s (%d)\n", text, rc ));
53 Debug( LDAP_DEBUG_TRACE,
54 "bdb_add: entry failed schema check: %s (%d)\n",
61 subentry = is_entry_subentry( e );
65 * acquire an ID outside of the operation transaction
66 * to avoid serializing adds.
68 rc = bdb_next_id( be, NULL, &e->e_id );
71 LDAP_LOG (( "add", LDAP_LEVEL_ERR, "bdb_add: next_id failed (%d)\n", rc ));
73 Debug( LDAP_DEBUG_TRACE,
74 "bdb_add: next_id failed (%d)\n",
78 text = "internal error";
83 retry: /* transaction retry */
84 rc = TXN_ABORT( ltid );
89 text = "internal error";
92 ldap_pvt_thread_yield();
95 /* begin transaction */
96 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
101 LDAP_LOG (( "add", LDAP_LEVEL_ERR, "bdb_add: txn_begin failed: %s (%d)\n", db_strerror(rc), rc ));
103 Debug( LDAP_DEBUG_TRACE,
104 "bdb_add: txn_begin failed: %s (%d)\n",
105 db_strerror(rc), rc, 0 );
108 text = "internal error";
112 lockid = TXN_ID( ltid );
116 opinfo.boi_txn = ltid;
118 op->o_private = &opinfo;
121 * Get the parent dn and see if the corresponding entry exists.
122 * If the parent does not exist, only allow the "root" user to
125 if ( be_issuffix( be, &e->e_nname ) ) {
128 dnParent( &e->e_nname, &pdn );
131 if( pdn.bv_len != 0 ) {
132 Entry *matched = NULL;
137 obj.data = pdn.bv_val-1;
138 obj.size = pdn.bv_len+1;
139 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
140 DB_LOCK_WRITE, &lock);
145 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, &matched, 0 );
151 case DB_LOCK_DEADLOCK:
152 case DB_LOCK_NOTGRANTED:
156 text = "internal error";
161 char *matched_dn = NULL;
164 if ( matched != NULL ) {
165 matched_dn = ch_strdup( matched->e_dn );
166 refs = is_entry_referral( matched )
167 ? get_entry_referrals( be, conn, op, matched )
169 bdb_cache_return_entry_r(&bdb->bi_cache, matched);
173 refs = referral_rewrite( default_referral,
174 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
178 LDAP_LOG (( "add", LDAP_LEVEL_DETAIL1, "bdb_add: parent does not exist\n" ));
180 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
184 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
185 matched_dn, NULL, refs, NULL );
187 ber_bvarray_free( refs );
188 ch_free( matched_dn );
193 rc = access_allowed( be, conn, op, p,
194 children, NULL, ACL_WRITE, NULL );
196 switch( opinfo.boi_err ) {
197 case DB_LOCK_DEADLOCK:
198 case DB_LOCK_NOTGRANTED:
199 /* free parent and reader lock */
200 bdb_cache_return_entry_r( &bdb->bi_cache, p );
207 LDAP_LOG (( "add", LDAP_LEVEL_DETAIL1, "bdb_add: no write access to parent\n" ));
209 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
212 rc = LDAP_INSUFFICIENT_ACCESS;
213 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 (( "add", LDAP_LEVEL_DETAIL1, "bdb_add: parent is subentry\n" ));
223 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
226 rc = LDAP_OBJECT_CLASS_VIOLATION;
227 text = "parent is a subentry";
228 goto return_results;;
232 if ( is_entry_alias( p ) ) {
233 /* parent is an alias, don't allow add */
235 LDAP_LOG (( "add", LDAP_LEVEL_DETAIL1, "bdb_add: parent is alias\n" ));
237 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
240 rc = LDAP_ALIAS_PROBLEM;
241 text = "parent is an alias";
242 goto return_results;;
246 if ( is_entry_referral( p ) ) {
247 /* parent is a referral, don't allow add */
248 char *matched_dn = p->e_dn;
249 BerVarray refs = get_entry_referrals( be, conn, op, p );
252 LDAP_LOG (( "add", LDAP_LEVEL_DETAIL1, "bdb_add: parent is referral\n" ));
254 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
258 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
259 matched_dn, NULL, refs, NULL );
261 ber_bvarray_free( refs );
262 bdb_cache_return_entry_r( &bdb->bi_cache, p );
267 #ifdef BDB_SUBENTRIES
270 /* parent must be an administrative point of the required kind */
274 /* free parent and reader lock */
275 bdb_cache_return_entry_r( &bdb->bi_cache, p );
281 * must be adding entry at suffix or with parent ""
283 if ( !be_isroot( be, &op->o_ndn )) {
284 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
285 || be_isupdate( be, &op->o_ndn ) ) {
286 p = (Entry *)&slap_entry_root;
288 /* check parent for "children" acl */
289 rc = access_allowed( be, conn, op, p,
290 children, NULL, ACL_WRITE, NULL );
293 switch( opinfo.boi_err ) {
294 case DB_LOCK_DEADLOCK:
295 case DB_LOCK_NOTGRANTED:
301 LDAP_LOG (( "add", LDAP_LEVEL_DETAIL1, "bdb_add: no write access to parent\n" ));
303 Debug( LDAP_DEBUG_TRACE,
304 "bdb_add: no write access to parent\n",
307 rc = LDAP_INSUFFICIENT_ACCESS;
308 text = "no write access to parent";
309 goto return_results;;
314 LDAP_LOG (( "add", LDAP_LEVEL_DETAIL1, "bdb_add: %s denied\n", pdn.bv_len == 0 ? "suffix" : "entry at root" ));
316 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
317 pdn.bv_len == 0 ? "suffix" : "entry at root",
320 rc = LDAP_INSUFFICIENT_ACCESS;
325 #ifdef BDB_SUBENTRIES
328 LDAP_LOG (( "add", LDAP_LEVEL_DETAIL1, "bdb_add: no parent, cannot add subentry\n" ));
330 Debug( LDAP_DEBUG_TRACE,
331 "bdb_add: no parent, cannot add subentry\n",
334 rc = LDAP_INSUFFICIENT_ACCESS;
335 text = "no parent, cannot add subentry";
336 goto return_results;;
344 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
345 DB_LOCK_WRITE, &lock);
351 rc = bdb_dn2id_add( be, ltid, &pdn, e );
354 LDAP_LOG (( "add", LDAP_LEVEL_ERR, "bdb_add: dn2id_add failed: %s (%d)\n", db_strerror(rc), rc ));
356 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
357 db_strerror(rc), rc, 0 );
361 case DB_LOCK_DEADLOCK:
362 case DB_LOCK_NOTGRANTED:
365 rc = LDAP_ALREADY_EXISTS;
374 rc = bdb_id2entry_add( be, ltid, e );
377 LDAP_LOG (( "add", LDAP_LEVEL_ERR, "bdb_add: id2entry_add failed\n" ));
379 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
383 case DB_LOCK_DEADLOCK:
384 case DB_LOCK_NOTGRANTED:
389 text = "entry store failed";
393 /* attribute indexes */
394 rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
395 if ( rc != LDAP_SUCCESS ) {
397 LDAP_LOG (( "add", LDAP_LEVEL_ERR, "bdb_add: index_entry_add failed\n" ));
399 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
403 case DB_LOCK_DEADLOCK:
404 case DB_LOCK_NOTGRANTED:
409 text = "index generation failed";
414 if (( rc=TXN_ABORT( ltid )) != 0 ) {
415 text = "txn_abort (no-op) failed";
421 char gid[DB_XIDDATASIZE];
423 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
424 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
426 if (( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
427 text = "txn_prepare failed";
430 if ( bdb_cache_add_entry_rw(&bdb->bi_cache,
431 e, CACHE_WRITE_LOCK) != 0 )
433 if(( rc=TXN_ABORT( ltid )) != 0 ) {
434 text = "cache add & txn_abort failed";
437 text = "cache add failed";
440 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
441 text = "txn_commit failed";
450 op->o_private = NULL;
452 if (rc == LDAP_SUCCESS) {
454 LDAP_LOG (( "add", LDAP_LEVEL_RESULTS, "bdb_add: added%s id=%08lx dn=\"%s\"\n", op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn ));
456 Debug(LDAP_DEBUG_TRACE, "bdb_add: added%s id=%08lx dn=\"%s\"\n",
457 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
460 bdb_cache_entry_commit( e );
464 LDAP_LOG (( "add", LDAP_LEVEL_ERR, "bdb_add: %s : %s (%d)\n", text, db_strerror(rc), rc ));
466 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
467 text, db_strerror(rc), rc );
473 send_ldap_result( conn, op, rc,
474 NULL, text, NULL, NULL );
476 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
477 ldap_pvt_thread_yield();
478 TXN_CHECKPOINT( bdb->bi_dbenv,
479 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
486 op->o_private = NULL;