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;
41 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
43 /* check entry's schema */
44 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
45 if ( rc != LDAP_SUCCESS ) {
46 Debug( LDAP_DEBUG_TRACE,
47 "bdb_add: entry failed schema check: %s (%d)\n",
53 subentry = is_entry_subentry( e );
57 * acquire an ID outside of the operation transaction
58 * to avoid serializing adds.
60 rc = bdb_next_id( be, NULL, &e->e_id );
62 Debug( LDAP_DEBUG_TRACE,
63 "bdb_add: next_id failed (%d)\n",
66 text = "internal error";
71 retry: /* transaction retry */
72 rc = TXN_ABORT( ltid );
77 text = "internal error";
80 ldap_pvt_thread_yield();
83 /* begin transaction */
84 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
88 Debug( LDAP_DEBUG_TRACE,
89 "bdb_add: txn_begin failed: %s (%d)\n",
90 db_strerror(rc), rc, 0 );
92 text = "internal error";
96 lockid = TXN_ID( ltid );
100 opinfo.boi_txn = ltid;
102 op->o_private = &opinfo;
105 * Get the parent dn and see if the corresponding entry exists.
106 * If the parent does not exist, only allow the "root" user to
109 if ( be_issuffix( be, &e->e_nname ) ) {
112 dnParent( &e->e_nname, &pdn );
115 if( pdn.bv_len != 0 ) {
116 Entry *matched = NULL;
121 obj.data = pdn.bv_val-1;
122 obj.size = pdn.bv_len+1;
123 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
124 DB_LOCK_WRITE, &lock);
129 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, &matched, 0 );
135 case DB_LOCK_DEADLOCK:
136 case DB_LOCK_NOTGRANTED:
140 text = "internal error";
145 char *matched_dn = NULL;
148 if ( matched != NULL ) {
149 matched_dn = ch_strdup( matched->e_dn );
150 refs = is_entry_referral( matched )
151 ? get_entry_referrals( be, conn, op, matched )
153 bdb_cache_return_entry_r(&bdb->bi_cache, matched);
157 refs = referral_rewrite( default_referral,
158 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
161 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
164 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
165 matched_dn, NULL, refs, NULL );
167 ber_bvarray_free( refs );
168 ch_free( matched_dn );
173 rc = access_allowed( be, conn, op, p,
174 children, NULL, ACL_WRITE, NULL );
176 switch( opinfo.boi_err ) {
177 case DB_LOCK_DEADLOCK:
178 case DB_LOCK_NOTGRANTED:
179 /* free parent and reader lock */
180 bdb_cache_return_entry_r( &bdb->bi_cache, p );
186 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
188 rc = LDAP_INSUFFICIENT_ACCESS;
189 text = "no write access to parent";
190 goto return_results;;
193 #ifdef BDB_SUBENTRIES
194 if ( is_entry_subentry( p ) ) {
195 /* parent is a subentry, don't allow add */
196 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
198 rc = LDAP_OBJECT_CLASS_VIOLATION;
199 text = "parent is a subentry";
200 goto return_results;;
204 if ( is_entry_alias( p ) ) {
205 /* parent is an alias, don't allow add */
206 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
208 rc = LDAP_ALIAS_PROBLEM;
209 text = "parent is an alias";
210 goto return_results;;
214 if ( is_entry_referral( p ) ) {
215 /* parent is a referral, don't allow add */
216 char *matched_dn = p->e_dn;
217 BerVarray refs = get_entry_referrals( be, conn, op, p );
219 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
222 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
223 matched_dn, NULL, refs, NULL );
225 ber_bvarray_free( refs );
226 bdb_cache_return_entry_r( &bdb->bi_cache, p );
231 #ifdef BDB_SUBENTRIES
234 /* parent must be an administrative point of the required kind */
238 /* free parent and reader lock */
239 bdb_cache_return_entry_r( &bdb->bi_cache, p );
245 * must be adding entry at suffix or with parent ""
247 if ( !be_isroot( be, &op->o_ndn )) {
248 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
249 || be_isupdate( be, &op->o_ndn ) ) {
250 p = (Entry *)&slap_entry_root;
252 /* check parent for "children" acl */
253 rc = access_allowed( be, conn, op, p,
254 children, NULL, ACL_WRITE, NULL );
257 switch( opinfo.boi_err ) {
258 case DB_LOCK_DEADLOCK:
259 case DB_LOCK_NOTGRANTED:
264 Debug( LDAP_DEBUG_TRACE,
265 "bdb_add: no write access to parent\n",
267 rc = LDAP_INSUFFICIENT_ACCESS;
268 text = "no write access to parent";
269 goto return_results;;
273 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
274 pdn.bv_len == 0 ? "suffix" : "entry at root",
276 rc = LDAP_INSUFFICIENT_ACCESS;
281 #ifdef BDB_SUBENTRIES
283 Debug( LDAP_DEBUG_TRACE,
284 "bdb_add: no parent, cannot add subentry\n",
286 rc = LDAP_INSUFFICIENT_ACCESS;
287 text = "no parent, cannot add subentry";
288 goto return_results;;
296 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
297 DB_LOCK_WRITE, &lock);
303 rc = bdb_dn2id_add( be, ltid, &pdn, e );
305 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
306 db_strerror(rc), rc, 0 );
309 case DB_LOCK_DEADLOCK:
310 case DB_LOCK_NOTGRANTED:
313 rc = LDAP_ALREADY_EXISTS;
322 rc = bdb_id2entry_add( be, ltid, e );
324 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
327 case DB_LOCK_DEADLOCK:
328 case DB_LOCK_NOTGRANTED:
333 text = "entry store failed";
337 /* attribute indexes */
338 rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
339 if ( rc != LDAP_SUCCESS ) {
340 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
343 case DB_LOCK_DEADLOCK:
344 case DB_LOCK_NOTGRANTED:
349 text = "index generation failed";
354 if (( rc=TXN_ABORT( ltid )) != 0 ) {
355 text = "txn_abort (no-op) failed";
361 char gid[DB_XIDDATASIZE];
363 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
364 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
366 if (( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
367 text = "txn_prepare failed";
370 if ( bdb_cache_add_entry_rw(&bdb->bi_cache,
371 e, CACHE_WRITE_LOCK) != 0 )
373 if(( rc=TXN_ABORT( ltid )) != 0 ) {
374 text = "cache add & txn_abort failed";
377 text = "cache add failed";
380 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
381 text = "txn_commit failed";
390 op->o_private = NULL;
392 if (rc == LDAP_SUCCESS) {
393 Debug(LDAP_DEBUG_TRACE, "bdb_add: added%s id=%08lx dn=\"%s\"\n",
394 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
396 bdb_cache_entry_commit( e );
399 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
400 text, db_strerror(rc), rc );
405 send_ldap_result( conn, op, rc,
406 NULL, text, NULL, NULL );
408 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
409 ldap_pvt_thread_yield();
410 TXN_CHECKPOINT( bdb->bi_dbenv,
411 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
418 op->o_private = NULL;