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 /* transaction retry */
68 retry: 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.bv_val ) ) {
111 rc = dnParent( e->e_nname.bv_val, (const char **)&pdn.bv_val );
112 if ( rc != LDAP_SUCCESS ) {
113 text = "internal error";
116 pdn.bv_len = e->e_nname.bv_len - (pdn.bv_val - e->e_nname.bv_val);
119 if( pdn.bv_len != 0 ) {
120 Entry *matched = NULL;
125 obj.data = pdn.bv_val-1;
126 obj.size = pdn.bv_len+1;
127 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
128 DB_LOCK_WRITE, &lock);
133 rc = bdb_dn2entry( be, ltid, &pdn, &p, &matched, 0 );
139 case DB_LOCK_DEADLOCK:
140 case DB_LOCK_NOTGRANTED:
144 text = "internal error";
149 char *matched_dn = NULL;
152 if ( matched != NULL ) {
153 matched_dn = ch_strdup( matched->e_dn );
154 refs = is_entry_referral( matched )
155 ? get_entry_referrals( be, conn, op, matched )
157 bdb_entry_return( be, matched );
161 refs = referral_rewrite( default_referral,
162 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
165 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
168 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
169 matched_dn, NULL, refs, NULL );
171 ber_bvarray_free( refs );
172 ch_free( matched_dn );
177 rc = access_allowed( be, conn, op, p,
178 children, NULL, ACL_WRITE );
180 switch( opinfo.boi_err ) {
181 case DB_LOCK_DEADLOCK:
182 case DB_LOCK_NOTGRANTED:
183 /* free parent and writer lock */
184 bdb_entry_return( be, p );
190 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
192 rc = LDAP_INSUFFICIENT_ACCESS;
193 text = "no write access to parent";
194 goto return_results;;
197 if ( is_entry_subentry( p ) ) {
198 /* parent is a subentry, don't allow add */
199 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
201 rc = LDAP_OBJECT_CLASS_VIOLATION;
202 text = "parent is a subentry";
203 goto return_results;;
206 if ( is_entry_alias( p ) ) {
207 /* parent is an alias, don't allow add */
208 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
210 rc = LDAP_ALIAS_PROBLEM;
211 text = "parent is an alias";
212 goto return_results;;
215 if ( is_entry_referral( p ) ) {
216 /* parent is a referral, don't allow add */
217 char *matched_dn = ch_strdup( p->e_dn );
218 BerVarray refs = is_entry_referral( p )
219 ? get_entry_referrals( be, conn, op, p )
222 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
225 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
226 matched_dn, NULL, refs, NULL );
228 ber_bvarray_free( refs );
235 /* parent must be an administrative point of the required kind */
238 /* free parent and writer lock */
239 bdb_entry_return( be, p );
245 * must be adding entry at suffix or with parent ""
247 if ( !be_isroot( be, &op->o_ndn )) {
248 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
249 p = (Entry *)&slap_entry_root;
251 /* check parent for "children" acl */
252 rc = access_allowed( be, conn, op, p,
253 children, NULL, ACL_WRITE );
256 switch( opinfo.boi_err ) {
257 case DB_LOCK_DEADLOCK:
258 case DB_LOCK_NOTGRANTED:
263 Debug( LDAP_DEBUG_TRACE,
264 "bdb_add: no write access to parent\n",
266 rc = LDAP_INSUFFICIENT_ACCESS;
267 text = "no write access to parent";
268 goto return_results;;
272 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
273 pdn.bv_len == 0 ? "suffix" : "entry at root",
275 rc = LDAP_INSUFFICIENT_ACCESS;
281 Debug( LDAP_DEBUG_TRACE,
282 "bdb_add: no parent, cannot add subentry\n",
284 rc = LDAP_INSUFFICIENT_ACCESS;
285 text = "no parent, cannot add subentry";
286 goto return_results;;
293 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
294 DB_LOCK_WRITE, &lock);
300 rc = bdb_dn2id_add( be, ltid, &pdn, e );
302 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
303 db_strerror(rc), rc, 0 );
306 case DB_LOCK_DEADLOCK:
307 case DB_LOCK_NOTGRANTED:
310 rc = LDAP_ALREADY_EXISTS;
319 rc = bdb_id2entry_add( be, ltid, e );
321 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
324 case DB_LOCK_DEADLOCK:
325 case DB_LOCK_NOTGRANTED:
330 text = "entry store failed";
334 /* attribute indexes */
335 rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
336 if ( rc != LDAP_SUCCESS ) {
337 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
340 case DB_LOCK_DEADLOCK:
341 case DB_LOCK_NOTGRANTED:
346 text = "index generation failed";
351 rc = txn_commit( ltid, 0 );
354 op->o_private = NULL;
357 Debug( LDAP_DEBUG_TRACE,
358 "bdb_add: txn_commit failed: %s (%d)\n",
359 db_strerror(rc), rc, 0 );
361 text = "commit failed";
364 Debug( LDAP_DEBUG_TRACE,
365 "bdb_add: added id=%08lx dn=\"%s\"\n",
366 e->e_id, e->e_dn, 0 );
372 send_ldap_result( conn, op, rc,
373 NULL, text, NULL, NULL );
375 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
376 ldap_pvt_thread_yield();
377 TXN_CHECKPOINT( bdb->bi_dbenv,
378 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
383 /* free parent and writer lock */
384 bdb_entry_return( be, p );
389 op->o_private = NULL;