1 /* add.c - ldap BerkeleyDB back-end add routine */
4 * Copyright 1998-2000 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;
34 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
36 /* check entry's schema */
37 rc = entry_schema_check( e, NULL, &text, textbuf, textlen );
38 if ( rc != LDAP_SUCCESS ) {
39 Debug( LDAP_DEBUG_TRACE,
40 "bdb_add: entry failed schema check: %s (%d)\n",
46 * acquire an ID outside of the operation transaction
47 * to avoid serializing adds.
49 rc = bdb_next_id( be, NULL, &e->e_id );
51 Debug( LDAP_DEBUG_TRACE,
52 "bdb_add: next_id failed (%d)\n",
55 text = "internal error";
60 /* transaction retry */
61 retry: rc = txn_abort( ltid );
66 text = "internal error";
71 /* begin transaction */
73 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
77 Debug( LDAP_DEBUG_TRACE,
78 "bdb_add: txn_begin failed: %s (%d)\n",
79 db_strerror(rc), rc, 0 );
81 text = "internal error";
87 opinfo.boi_txn = ltid;
89 op->o_private = &opinfo;
92 * Get the parent dn and see if the corresponding entry exists.
93 * If the parent does not exist, only allow the "root" user to
96 pdn = dn_parent( be, e->e_ndn );
98 if( pdn != NULL && *pdn != '\0' ) {
99 Entry *matched = NULL;
102 rc = bdb_dn2entry( be, ltid, pdn, &p, &matched, 0 );
109 case DB_LOCK_DEADLOCK:
110 case DB_LOCK_NOTGRANTED:
114 text = "internal error";
119 char *matched_dn = NULL;
120 struct berval **refs;
122 if ( matched != NULL ) {
123 matched_dn = ch_strdup( matched->e_dn );
124 refs = is_entry_referral( matched )
125 ? get_entry_referrals( be, conn, op,
126 matched, e->e_dn, LDAP_SCOPE_DEFAULT )
128 bdb_entry_return( be, matched );
132 refs = referral_rewrite( default_referral,
133 NULL, e->e_dn, LDAP_SCOPE_DEFAULT );
136 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
139 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
140 matched_dn, NULL, refs, NULL );
142 ber_bvecfree( refs );
143 ch_free( matched_dn );
148 if ( ! access_allowed( be, conn, op, p,
149 children, NULL, ACL_WRITE ) )
151 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
153 rc = LDAP_INSUFFICIENT_ACCESS;
154 text = "no write access to parent";
155 goto return_results;;
158 if ( is_entry_alias( p ) ) {
159 /* parent is an alias, don't allow add */
160 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
162 rc = LDAP_ALIAS_PROBLEM;
163 text = "parent is an alias";
164 goto return_results;;
167 if ( is_entry_referral( p ) ) {
168 /* parent is a referral, don't allow add */
169 char *matched_dn = ch_strdup( p->e_dn );
170 struct berval **refs = is_entry_referral( p )
171 ? get_entry_referrals( be, conn, op, p,
172 e->e_dn, LDAP_SCOPE_DEFAULT )
175 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
178 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
179 matched_dn, NULL, refs, NULL );
181 ber_bvecfree( refs );
186 /* free parent and writer lock */
187 bdb_entry_return( be, p );
197 * must be adding entry to at suffix
200 if ( !be_isroot( be, op->o_ndn )) {
201 if ( be_issuffix( be, "" ) || be_isupdate( be, op->o_ndn ) ) {
203 p = (Entry *)&slap_entry_root;
205 /* check parent for "children" acl */
206 rc = access_allowed( be, conn, op, p,
207 children, NULL, ACL_WRITE );
211 Debug( LDAP_DEBUG_TRACE,
212 "bdb_add: no write access to parent\n",
214 rc = LDAP_INSUFFICIENT_ACCESS;
215 text = "no write access to parent";
216 goto return_results;;
220 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
221 pdn == NULL ? "suffix" : "entry at root",
223 rc = LDAP_INSUFFICIENT_ACCESS;
230 rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
232 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
233 db_strerror(rc), rc, 0 );
236 case DB_LOCK_DEADLOCK:
237 case DB_LOCK_NOTGRANTED:
240 rc = LDAP_ALREADY_EXISTS;
249 rc = bdb_id2entry_add( be, ltid, e );
251 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
254 case DB_LOCK_DEADLOCK:
255 case DB_LOCK_NOTGRANTED:
260 text = "entry store failed";
264 /* attribute indexes */
265 rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
266 if ( rc != LDAP_SUCCESS ) {
267 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
270 case DB_LOCK_DEADLOCK:
271 case DB_LOCK_NOTGRANTED:
276 text = "index generation failed";
281 rc = txn_commit( ltid, 0 );
284 op->o_private = NULL;
287 Debug( LDAP_DEBUG_TRACE,
288 "bdb_add: txn_commit failed: %s (%d)\n",
289 db_strerror(rc), rc, 0 );
291 text = "commit failed";
294 Debug( LDAP_DEBUG_TRACE,
295 "bdb_add: added id=%08lx dn=\"%s\"\n",
296 e->e_id, e->e_dn, 0 );
302 send_ldap_result( conn, op, rc,
303 NULL, text, NULL, NULL );
305 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
306 ldap_pvt_thread_yield();
307 txn_checkpoint( bdb->bi_dbenv,
308 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
313 /* free parent and writer lock */
314 bdb_entry_return( be, p );
319 op->o_private = NULL;