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>
22 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
26 const char *text = NULL;
27 AttributeDescription *children = slap_schema.si_ad_children;
30 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
32 /* check entry's schema */
33 rc = entry_schema_check( e, NULL, &text );
34 if ( rc != LDAP_SUCCESS ) {
35 Debug( LDAP_DEBUG_TRACE,
36 "bdb_add: entry failed schema check: %s (%d)\n",
42 * acquire an ID outside of the operation transaction
43 * to avoid serializing adds.
45 rc = bdb_next_id( be, NULL, &e->e_id );
47 Debug( LDAP_DEBUG_TRACE,
48 "bdb_add: next_id failed (%d)\n",
51 text = "internal error";
56 /* transaction retry */
57 retry: rc = txn_abort( ltid );
62 text = "internal error";
67 /* begin transaction */
68 rc = txn_begin( bdb->bi_dbenv, NULL, <id, 0 );
70 Debug( LDAP_DEBUG_TRACE,
71 "bdb_add: txn_begin failed: %s (%d)\n",
72 db_strerror(rc), rc, 0 );
74 text = "internal error";
81 * Get the parent dn and see if the corresponding entry exists.
82 * If the parent does not exist, only allow the "root" user to
85 pdn = dn_parent( be, e->e_ndn );
87 if( pdn != NULL && *pdn != '\0' ) {
88 Entry *matched = NULL;
90 /* get parent with reader lock */
91 rc = dn2entry_r( be, ltid, pdn, &p, &matched );
98 case DB_LOCK_DEADLOCK:
99 case DB_LOCK_NOTGRANTED:
103 text = "internal error";
109 struct berval **refs;
111 if ( matched != NULL ) {
112 matched_dn = ch_strdup( matched->e_dn );
113 refs = is_entry_referral( matched )
114 ? get_entry_referrals( be, conn, op, matched )
116 bdb_entry_return( be, matched );
120 refs = default_referral;
123 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
126 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
127 matched_dn, NULL, refs, NULL );
129 if( matched != NULL ) {
130 ber_bvecfree( refs );
131 ch_free( matched_dn );
137 if ( ! access_allowed( be, conn, op, p,
138 children, NULL, ACL_WRITE ) )
140 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
142 rc = LDAP_INSUFFICIENT_ACCESS;
143 text = "no write access to parent", NULL, NULL;
144 goto return_results;;
147 if ( is_entry_alias( p ) ) {
148 /* parent is an alias, don't allow add */
149 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
151 rc = LDAP_ALIAS_PROBLEM;
152 text = "parent is an alias";
153 goto return_results;;
156 if ( is_entry_referral( p ) ) {
157 /* parent is a referral, don't allow add */
158 char *matched_dn = ch_strdup( p->e_dn );
159 struct berval **refs = is_entry_referral( p )
160 ? get_entry_referrals( be, conn, op, p )
163 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
166 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
167 matched_dn, NULL, refs, NULL );
169 ber_bvecfree( refs );
174 /* free parent and writer lock */
175 bdb_entry_return( be, p );
185 * must be adding entry to at suffix
188 if ( !be_isroot( be, op->o_ndn )) {
189 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
190 pdn == NULL ? "suffix" : "entry at root",
192 rc = LDAP_INSUFFICIENT_ACCESS;
198 rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
200 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
201 db_strerror(rc), rc, 0 );
204 case DB_LOCK_DEADLOCK:
205 case DB_LOCK_NOTGRANTED:
208 rc = LDAP_ALREADY_EXISTS;
217 rc = bdb_id2entry_add( be, ltid, e );
219 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
222 case DB_LOCK_DEADLOCK:
223 case DB_LOCK_NOTGRANTED:
228 text = "entry store failed";
233 /* attribute indexes */
234 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
235 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
238 case DB_LOCK_DEADLOCK:
239 case DB_LOCK_NOTGRANTED:
244 text = "index generation failed";
249 rc = txn_commit( ltid, 0 );
251 op->o_private = NULL;
254 Debug( LDAP_DEBUG_TRACE,
255 "bdb_add: txn_commit failed: %s (%d)\n",
256 db_strerror(rc), rc, 0 );
258 text = "commit failed";
260 Debug( LDAP_DEBUG_TRACE,
261 "bdb_add: added id=%08x dn=\"%s\"\n",
262 e->e_id, e->e_dn, 0 );
268 send_ldap_result( conn, op, rc,
269 NULL, text, NULL, NULL );
273 /* free parent and writer lock */
274 bdb_entry_return( be, p );
279 op->o_private = NULL;