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;
34 char textbuf[SLAP_TEXT_BUFLEN];
35 size_t textlen = sizeof textbuf;
38 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,"ldbm_back_add: %s\n",
41 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", e->e_dn, 0, 0);
45 /* nobody else can add until we lock our parent */
46 ldap_pvt_thread_mutex_lock(&li->li_add_mutex);
48 if ( ( dn2id( be, e->e_ndn, &rc_id ) ) != NOID || rc_id ) {
49 /* if (rc_id) something bad happened to ldbm cache */
50 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
51 send_ldap_result( conn, op,
52 rc_id ? LDAP_OPERATIONS_ERROR : LDAP_ALREADY_EXISTS,
53 NULL, NULL, NULL, NULL );
57 rc = entry_schema_check( e, NULL, &text, textbuf, textlen );
59 if ( rc != LDAP_SUCCESS ) {
60 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
63 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
64 "ldbm_back_add: entry (%s) failed schema check.\n",
67 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
72 send_ldap_result( conn, op, rc,
73 NULL, text, NULL, NULL );
78 * Get the parent dn and see if the corresponding entry exists.
79 * If the parent does not exist, only allow the "root" user to
83 pdn = dn_parent( be, e->e_ndn );
85 if( pdn != NULL && *pdn != '\0' ) {
86 Entry *matched = NULL;
88 assert( *pdn != '\0' );
90 /* get parent with writer lock */
91 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
95 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
97 if ( matched != NULL ) {
98 matched_dn = ch_strdup( matched->e_dn );
99 refs = is_entry_referral( matched )
100 ? get_entry_referrals( be, conn, op, matched )
102 cache_return_entry_r( &li->li_cache, matched );
106 refs = default_referral;
110 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
111 "ldbm_back_add: Parent of (%s) does not exist.\n",
114 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
119 send_ldap_result( conn, op, LDAP_REFERRAL, matched_dn,
120 refs == NULL ? "parent does not exist" : "parent is referral",
123 if( matched != NULL ) {
124 ber_bvecfree( refs );
132 /* don't need the add lock anymore */
133 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
137 if ( ! access_allowed( be, conn, op, p,
138 children, NULL, ACL_WRITE ) )
140 /* free parent and writer lock */
141 cache_return_entry_w( &li->li_cache, p );
144 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
145 "ldbm_back_add: No write access to parent (%s).\n",
148 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
152 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
153 NULL, "no write access to parent", NULL, NULL );
159 if ( is_entry_alias( p ) ) {
160 /* parent is an alias, don't allow add */
162 /* free parent and writer lock */
163 cache_return_entry_w( &li->li_cache, p );
166 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
167 "ldbm_back_add: Parent is an alias.\n"));
169 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
174 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
175 NULL, "parent is an alias", NULL, NULL );
180 if ( is_entry_referral( p ) ) {
181 /* parent is a referral, don't allow add */
182 char *matched_dn = ch_strdup( p->e_dn );
183 struct berval **refs = is_entry_referral( p )
184 ? get_entry_referrals( be, conn, op, p )
187 /* free parent and writer lock */
188 cache_return_entry_w( &li->li_cache, p );
191 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
192 "ldbm_back_add: Parent is referral.\n" ));
194 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
198 send_ldap_result( conn, op, LDAP_REFERRAL,
199 matched_dn, NULL, refs, NULL );
201 ber_bvecfree( refs );
208 assert( *pdn == '\0' );
212 /* no parent, must be adding entry to root */
213 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix( be, "" ) ) {
214 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
217 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
218 "ldbm_back_add: %s add denied.\n",
219 pdn == NULL ? "suffix" : "entry at root" ));
221 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
222 pdn == NULL ? "suffix" : "entry at root",
227 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
228 NULL, NULL, NULL, NULL );
234 * no parent, acquire the root write lock
235 * and release the add lock.
237 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
239 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
242 e->e_id = next_id( be );
244 if( e->e_id == NOID ) {
246 /* free parent and writer lock */
247 cache_return_entry_w( &li->li_cache, p );
251 /* release root lock */
252 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
256 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
257 "ldbm_back_add: next_id failed.\n" ));
259 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
264 send_ldap_result( conn, op, LDAP_OTHER,
265 NULL, "next_id add failed", NULL, NULL );
271 * Try to add the entry to the cache, assign it a new dnid.
273 rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
277 /* free parent and writer lock */
278 cache_return_entry_w( &li->li_cache, p );
282 /* release root lock */
283 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
287 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
288 "ldbm_back_add: cache_add_entry_lock failed.\n" ));
290 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
295 send_ldap_result( conn, op,
296 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
297 NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
304 /* attribute indexes */
305 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
307 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
308 "ldbm_back_add: index_entry_add failed.\n" ));
310 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
314 send_ldap_result( conn, op, LDAP_OTHER,
315 NULL, "index generation failed", NULL, NULL );
321 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
323 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
324 "ldbm_back_add: dn2id_add failed.\n" ));
326 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
330 send_ldap_result( conn, op, LDAP_OTHER,
331 NULL, "DN index generation failed", NULL, NULL );
337 if ( id2entry_add( be, e ) != 0 ) {
339 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
340 "ldbm_back_add: id2entry_add failed.\n" ));
342 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
346 (void) dn2id_delete( be, e->e_ndn, e->e_id );
347 send_ldap_result( conn, op, LDAP_OTHER,
348 NULL, "entry store failed", NULL, NULL );
353 send_ldap_result( conn, op, LDAP_SUCCESS,
354 NULL, NULL, NULL, NULL );
359 /* free parent and writer lock */
360 cache_return_entry_w( &li->li_cache, p );
364 /* release root lock */
365 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
369 /* free entry and writer lock */
370 cache_return_entry_w( &li->li_cache, e );