1 /* add.c - ldap ldbm back-end add routine */
4 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
13 #include <ac/string.h>
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
27 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
33 #ifdef SLAPD_SCHEMA_NOT_COMPAT
34 static AttributeDescription *children = NULL;
36 static const char *children = "children";
40 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", e->e_dn, 0, 0);
42 /* nobody else can add until we lock our parent */
43 ldap_pvt_thread_mutex_lock(&li->li_add_mutex);
45 if ( ( dn2id( be, e->e_ndn ) ) != NOID ) {
46 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
48 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
49 NULL, NULL, NULL, NULL );
53 rc = entry_schema_check( e, NULL, &text );
55 if ( rc != LDAP_SUCCESS ) {
56 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
58 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
62 send_ldap_result( conn, op, rc,
63 NULL, text, NULL, NULL );
68 * Get the parent dn and see if the corresponding entry exists.
69 * If the parent does not exist, only allow the "root" user to
73 pdn = dn_parent( be, e->e_ndn );
75 if( pdn != NULL && *pdn != '\0' ) {
76 Entry *matched = NULL;
78 assert( *pdn != '\0' );
80 /* get parent with writer lock */
81 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
85 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
87 if ( matched != NULL ) {
88 matched_dn = ch_strdup( matched->e_dn );
89 refs = is_entry_referral( matched )
90 ? get_entry_referrals( be, conn, op, matched )
92 cache_return_entry_r( &li->li_cache, matched );
96 refs = default_referral;
99 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
102 send_ldap_result( conn, op, LDAP_REFERRAL,
103 matched_dn, NULL, refs, NULL );
105 if( matched != NULL ) {
106 ber_bvecfree( refs );
115 /* don't need the add lock anymore */
116 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
120 if ( ! access_allowed( be, conn, op, p,
121 children, NULL, ACL_WRITE ) )
123 /* free parent and writer lock */
124 cache_return_entry_w( &li->li_cache, p );
126 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
128 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
129 NULL, "no write access to parent", NULL, NULL );
136 if ( is_entry_alias( p ) ) {
137 /* parent is an alias, don't allow add */
139 /* free parent and writer lock */
140 cache_return_entry_w( &li->li_cache, p );
142 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
145 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
146 NULL, "parent is an alias", NULL, NULL );
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 /* free parent and writer lock */
160 cache_return_entry_w( &li->li_cache, p );
162 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
164 send_ldap_result( conn, op, LDAP_REFERRAL,
165 matched_dn, NULL, refs, NULL );
167 ber_bvecfree( refs );
175 assert( *pdn == '\0' );
179 /* no parent, must be adding entry to root */
180 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix( be, "" ) ) {
181 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
183 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
184 pdn == NULL ? "suffix" : "entry at root",
187 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
188 NULL, NULL, NULL, NULL );
195 * no parent, acquire the root write lock
196 * and release the add lock.
198 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
200 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
203 e->e_id = next_id( be );
206 * Try to add the entry to the cache, assign it a new dnid.
208 rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
212 /* free parent and writer lock */
213 cache_return_entry_w( &li->li_cache, p );
217 /* release root lock */
218 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
221 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
227 send_ldap_result( conn, op,
228 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
229 NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
237 * Add the entry to the attribute indexes, then add it to
238 * the id2children index, dn2id index, and the id2entry index.
241 /* attribute indexes */
242 if ( index_add_entry( be, e ) != 0 ) {
243 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
245 send_ldap_result( conn, op, LDAP_OTHER,
246 NULL, "index generation failed", NULL, NULL );
252 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
253 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
255 send_ldap_result( conn, op, LDAP_OTHER,
256 NULL, "DN index generation failed", NULL, NULL );
262 if ( id2entry_add( be, e ) != 0 ) {
263 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
265 (void) dn2id_delete( be, e->e_ndn, e->e_id );
266 send_ldap_result( conn, op, LDAP_OTHER,
267 NULL, "entry store failed", NULL, NULL );
272 send_ldap_result( conn, op, LDAP_SUCCESS,
273 NULL, NULL, NULL, NULL );
278 /* free parent and writer lock */
279 cache_return_entry_w( &li->li_cache, p );
283 /* release root lock */
284 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
288 /* free entry and writer lock */
289 cache_return_entry_w( &li->li_cache, e );