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 retry: rc = txn_abort( ltid );
61 text = "internal error";
66 /* begin transaction */
67 rc = txn_begin( bdb->bi_dbenv, NULL, <id, 0 );
69 Debug( LDAP_DEBUG_TRACE,
70 "bdb_add: txn_begin failed: %s (%d)\n",
71 db_strerror(rc), rc, 0 );
73 text = "internal error";
80 * Get the parent dn and see if the corresponding entry exists.
81 * If the parent does not exist, only allow the "root" user to
84 pdn = dn_parent( be, e->e_ndn );
86 if( pdn != NULL && *pdn != '\0' ) {
87 Entry *matched = NULL;
89 /* get parent with reader lock */
90 rc = dn2entry_r( be, ltid, pdn, &p, &matched );
94 case DB_LOCK_DEADLOCK:
95 case DB_LOCK_NOTGRANTED:
99 text = "internal error";
105 struct berval **refs;
107 if ( matched != NULL ) {
108 matched_dn = ch_strdup( matched->e_dn );
109 refs = is_entry_referral( matched )
110 ? get_entry_referrals( be, conn, op, matched )
112 bdb_entry_return( be, matched );
116 refs = default_referral;
119 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
122 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
123 matched_dn, NULL, refs, NULL );
125 if( matched != NULL ) {
126 ber_bvecfree( refs );
127 ch_free( matched_dn );
133 if ( ! access_allowed( be, conn, op, p,
134 children, NULL, ACL_WRITE ) )
136 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
138 rc = LDAP_INSUFFICIENT_ACCESS;
139 text = "no write access to parent", NULL, NULL;
140 goto return_results;;
143 if ( is_entry_alias( p ) ) {
144 /* parent is an alias, don't allow add */
145 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
147 rc = LDAP_ALIAS_PROBLEM;
148 text = "parent is an alias";
149 goto return_results;;
152 if ( is_entry_referral( p ) ) {
153 /* parent is a referral, don't allow add */
154 char *matched_dn = ch_strdup( p->e_dn );
155 struct berval **refs = is_entry_referral( p )
156 ? get_entry_referrals( be, conn, op, p )
159 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
162 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
163 matched_dn, NULL, refs, NULL );
165 ber_bvecfree( refs );
170 /* free parent and writer lock */
171 bdb_entry_return( be, p );
181 * must be adding entry to at suffix
184 if ( !be_isroot( be, op->o_ndn )) {
185 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
186 pdn == NULL ? "suffix" : "entry at root",
188 rc = LDAP_INSUFFICIENT_ACCESS;
194 rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
196 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
197 db_strerror(rc), rc, 0 );
200 case DB_LOCK_DEADLOCK:
201 case DB_LOCK_NOTGRANTED:
204 rc = LDAP_ALREADY_EXISTS;
213 rc = bdb_id2entry_add( be, ltid, e );
215 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
218 text = "entry store failed";
223 /* attribute indexes */
224 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
225 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
228 text = "index generation failed";
233 rc = txn_commit( ltid, 0 );
235 op->o_private = NULL;
238 Debug( LDAP_DEBUG_TRACE,
239 "bdb_add: txn_commit failed: %s (%d)\n",
240 db_strerror(rc), rc, 0 );
242 text = "commit failed";
244 Debug( LDAP_DEBUG_TRACE,
245 "bdb_add: added id=%08x dn=\"%s\"\n",
246 e->e_id, e->e_dn, 0 );
252 send_ldap_result( conn, op, rc,
253 NULL, text, NULL, NULL );
257 /* free parent and writer lock */
258 bdb_entry_return( be, p );
263 op->o_private = NULL;