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;
39 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
41 /* check entry's schema */
42 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
43 if ( rc != LDAP_SUCCESS ) {
44 Debug( LDAP_DEBUG_TRACE,
45 "bdb_add: entry failed schema check: %s (%d)\n",
50 subentry = is_entry_subentry( e );
53 * acquire an ID outside of the operation transaction
54 * to avoid serializing adds.
56 rc = bdb_next_id( be, NULL, &e->e_id );
58 Debug( LDAP_DEBUG_TRACE,
59 "bdb_add: next_id failed (%d)\n",
62 text = "internal error";
67 retry: /* transaction retry */
68 rc = txn_abort( ltid );
73 text = "internal error";
76 ldap_pvt_thread_yield();
79 /* begin transaction */
81 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
85 Debug( LDAP_DEBUG_TRACE,
86 "bdb_add: txn_begin failed: %s (%d)\n",
87 db_strerror(rc), rc, 0 );
89 text = "internal error";
93 lockid = TXN_ID( ltid );
98 opinfo.boi_txn = ltid;
100 op->o_private = &opinfo;
103 * Get the parent dn and see if the corresponding entry exists.
104 * If the parent does not exist, only allow the "root" user to
107 if ( be_issuffix( be, &e->e_nname ) ) {
110 dnParent( &e->e_nname, &pdn );
113 if( pdn.bv_len != 0 ) {
114 Entry *matched = NULL;
119 obj.data = pdn.bv_val-1;
120 obj.size = pdn.bv_len+1;
121 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
122 DB_LOCK_WRITE, &lock);
127 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, &matched, 0 );
133 case DB_LOCK_DEADLOCK:
134 case DB_LOCK_NOTGRANTED:
138 text = "internal error";
143 char *matched_dn = NULL;
146 if ( matched != NULL ) {
147 matched_dn = ch_strdup( matched->e_dn );
148 refs = is_entry_referral( matched )
149 ? get_entry_referrals( be, conn, op, matched )
151 bdb_cache_return_entry_r(&bdb->bi_cache, matched);
155 refs = referral_rewrite( default_referral,
156 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
159 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
162 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
163 matched_dn, NULL, refs, NULL );
165 ber_bvarray_free( refs );
166 ch_free( matched_dn );
171 rc = access_allowed( be, conn, op, p,
172 children, NULL, ACL_WRITE );
174 switch( opinfo.boi_err ) {
175 case DB_LOCK_DEADLOCK:
176 case DB_LOCK_NOTGRANTED:
177 /* free parent and reader lock */
178 bdb_cache_return_entry_r( &bdb->bi_cache, p );
184 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
186 rc = LDAP_INSUFFICIENT_ACCESS;
187 text = "no write access to parent";
188 goto return_results;;
191 if ( is_entry_subentry( p ) ) {
192 /* parent is a subentry, don't allow add */
193 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
195 rc = LDAP_OBJECT_CLASS_VIOLATION;
196 text = "parent is a subentry";
197 goto return_results;;
200 if ( is_entry_alias( p ) ) {
201 /* parent is an alias, don't allow add */
202 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
204 rc = LDAP_ALIAS_PROBLEM;
205 text = "parent is an alias";
206 goto return_results;;
209 if ( is_entry_referral( p ) ) {
210 /* parent is a referral, don't allow add */
211 char *matched_dn = p->e_dn;
212 BerVarray refs = get_entry_referrals( be, conn, op, p );
214 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
217 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
218 matched_dn, NULL, refs, NULL );
220 ber_bvarray_free( refs );
221 bdb_cache_return_entry_r( &bdb->bi_cache, p );
228 /* parent must be an administrative point of the required kind */
231 /* free parent and reader lock */
232 bdb_cache_return_entry_r( &bdb->bi_cache, p );
238 * must be adding entry at suffix or with parent ""
240 if ( !be_isroot( be, &op->o_ndn )) {
241 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
242 || be_isupdate( be, &op->o_ndn ) ) {
243 p = (Entry *)&slap_entry_root;
245 /* check parent for "children" acl */
246 rc = access_allowed( be, conn, op, p,
247 children, NULL, ACL_WRITE );
250 switch( opinfo.boi_err ) {
251 case DB_LOCK_DEADLOCK:
252 case DB_LOCK_NOTGRANTED:
257 Debug( LDAP_DEBUG_TRACE,
258 "bdb_add: no write access to parent\n",
260 rc = LDAP_INSUFFICIENT_ACCESS;
261 text = "no write access to parent";
262 goto return_results;;
266 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
267 pdn.bv_len == 0 ? "suffix" : "entry at root",
269 rc = LDAP_INSUFFICIENT_ACCESS;
275 Debug( LDAP_DEBUG_TRACE,
276 "bdb_add: no parent, cannot add subentry\n",
278 rc = LDAP_INSUFFICIENT_ACCESS;
279 text = "no parent, cannot add subentry";
280 goto return_results;;
287 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
288 DB_LOCK_WRITE, &lock);
294 rc = bdb_dn2id_add( be, ltid, &pdn, e );
296 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
297 db_strerror(rc), rc, 0 );
300 case DB_LOCK_DEADLOCK:
301 case DB_LOCK_NOTGRANTED:
304 rc = LDAP_ALREADY_EXISTS;
313 rc = bdb_id2entry_add( be, ltid, e );
315 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
318 case DB_LOCK_DEADLOCK:
319 case DB_LOCK_NOTGRANTED:
324 text = "entry store failed";
328 /* attribute indexes */
329 rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
330 if ( rc != LDAP_SUCCESS ) {
331 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
334 case DB_LOCK_DEADLOCK:
335 case DB_LOCK_NOTGRANTED:
340 text = "index generation failed";
345 rc = txn_commit( ltid, 0 );
348 op->o_private = NULL;
351 Debug( LDAP_DEBUG_TRACE,
352 "bdb_add: txn_commit failed: %s (%d)\n",
353 db_strerror(rc), rc, 0 );
355 text = "commit failed";
358 /* add the entry to the entry cache */
359 /* we should add to cache only upon free of txn-abort */
360 if (bdb_cache_add_entry_rw(&bdb->bi_cache, e, CACHE_WRITE_LOCK) != 0) {
361 text = "cache add failed";
365 Debug( LDAP_DEBUG_TRACE,
366 "bdb_add: added id=%08lx dn=\"%s\"\n",
367 e->e_id, e->e_dn, 0 );
372 bdb_cache_entry_commit (e);
375 send_ldap_result( conn, op, rc,
376 NULL, text, NULL, NULL );
378 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
379 ldap_pvt_thread_yield();
380 TXN_CHECKPOINT( bdb->bi_dbenv,
381 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
388 op->o_private = NULL;