1 /* add.c - ldap ldbm back-end add routine */
4 * Copyright 1998-2000 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;
32 const char *text = NULL;
33 AttributeDescription *children = slap_schema.si_ad_children;
37 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,"ldbm_back_add: %s\n",
40 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", e->e_dn, 0, 0);
44 /* nobody else can add until we lock our parent */
45 ldap_pvt_thread_mutex_lock(&li->li_add_mutex);
47 if ( ( dn2id( be, e->e_ndn ) ) != NOID ) {
48 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
49 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
50 NULL, NULL, NULL, NULL );
54 rc = entry_schema_check( e, NULL, &text );
56 if ( rc != LDAP_SUCCESS ) {
57 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
60 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
61 "ldbm_back_add: entry (%s) failed schema check.\n",
64 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
69 send_ldap_result( conn, op, rc,
70 NULL, text, NULL, NULL );
75 * Get the parent dn and see if the corresponding entry exists.
76 * If the parent does not exist, only allow the "root" user to
80 pdn = dn_parent( be, e->e_ndn );
82 if( pdn != NULL && *pdn != '\0' ) {
83 Entry *matched = NULL;
85 assert( *pdn != '\0' );
87 /* get parent with writer lock */
88 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
92 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
94 if ( matched != NULL ) {
95 matched_dn = ch_strdup( matched->e_dn );
96 refs = is_entry_referral( matched )
97 ? get_entry_referrals( be, conn, op, matched )
99 cache_return_entry_r( &li->li_cache, matched );
103 refs = default_referral;
107 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
108 "ldbm_back_add: Parent of (%s) does not exist.\n",
111 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
116 send_ldap_result( conn, op, LDAP_REFERRAL,
117 matched_dn, NULL, refs, NULL );
119 if( matched != NULL ) {
120 ber_bvecfree( refs );
128 /* don't need the add lock anymore */
129 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
133 if ( ! access_allowed( be, conn, op, p,
134 children, NULL, ACL_WRITE ) )
136 /* free parent and writer lock */
137 cache_return_entry_w( &li->li_cache, p );
140 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
141 "ldbm_back_add: No write access to parent (%s).\n",
144 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
148 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
149 NULL, "no write access to parent", NULL, NULL );
155 if ( is_entry_alias( p ) ) {
156 /* parent is an alias, don't allow add */
158 /* free parent and writer lock */
159 cache_return_entry_w( &li->li_cache, p );
162 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
163 "ldbm_back_add: Parent is an alias.\n"));
165 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
170 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
171 NULL, "parent is an alias", NULL, NULL );
176 if ( is_entry_referral( p ) ) {
177 /* parent is a referral, don't allow add */
178 char *matched_dn = ch_strdup( p->e_dn );
179 struct berval **refs = is_entry_referral( p )
180 ? get_entry_referrals( be, conn, op, p )
183 /* free parent and writer lock */
184 cache_return_entry_w( &li->li_cache, p );
187 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
188 "ldbm_back_add: Parent is referral.\n" ));
190 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
194 send_ldap_result( conn, op, LDAP_REFERRAL,
195 matched_dn, NULL, refs, NULL );
197 ber_bvecfree( refs );
204 assert( *pdn == '\0' );
208 /* no parent, must be adding entry to root */
209 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix( be, "" ) ) {
210 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
213 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
214 "ldbm_back_add: %s add denied.\n",
215 pdn == NULL ? "suffix" : "entry at root" ));
217 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
218 pdn == NULL ? "suffix" : "entry at root",
223 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
224 NULL, NULL, NULL, NULL );
230 * no parent, acquire the root write lock
231 * and release the add lock.
233 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
235 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
238 e->e_id = next_id( be );
240 if( e->e_id == NOID ) {
242 /* free parent and writer lock */
243 cache_return_entry_w( &li->li_cache, p );
247 /* release root lock */
248 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
252 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
253 "ldbm_back_add: next_id failed.\n" ));
255 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
260 send_ldap_result( conn, op, LDAP_OTHER,
261 NULL, "next_id add failed", NULL, NULL );
267 * Try to add the entry to the cache, assign it a new dnid.
269 rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
273 /* free parent and writer lock */
274 cache_return_entry_w( &li->li_cache, p );
278 /* release root lock */
279 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
283 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
284 "ldbm_back_add: cache_add_entry_lock failed.\n" ));
286 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
291 send_ldap_result( conn, op,
292 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
293 NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
300 /* attribute indexes */
301 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
303 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
304 "ldbm_back_add: index_entry_add failed.\n" ));
306 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
310 send_ldap_result( conn, op, LDAP_OTHER,
311 NULL, "index generation failed", NULL, NULL );
317 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
319 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
320 "ldbm_back_add: dn2id_add failed.\n" ));
322 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
326 send_ldap_result( conn, op, LDAP_OTHER,
327 NULL, "DN index generation failed", NULL, NULL );
333 if ( id2entry_add( be, e ) != 0 ) {
335 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
336 "ldbm_back_add: id2entry_add failed.\n" ));
338 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
342 (void) dn2id_delete( be, e->e_ndn, e->e_id );
343 send_ldap_result( conn, op, LDAP_OTHER,
344 NULL, "entry store failed", NULL, NULL );
349 send_ldap_result( conn, op, LDAP_SUCCESS,
350 NULL, NULL, NULL, NULL );
355 /* free parent and writer lock */
356 cache_return_entry_w( &li->li_cache, p );
360 /* release root lock */
361 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
365 /* free entry and writer lock */
366 cache_return_entry_w( &li->li_cache, e );