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;
33 const char *text = NULL;
34 AttributeDescription *children = slap_schema.si_ad_children;
35 char textbuf[SLAP_TEXT_BUFLEN];
36 size_t textlen = sizeof textbuf;
39 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,"ldbm_back_add: %s\n",
42 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 ( ( rc = dn2id( be, e->e_ndn, &id ) ) || id != NOID ) {
49 /* if (rc) something bad happened to ldbm cache */
50 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
51 send_ldap_result( conn, op,
52 rc ? 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 ) {
92 char *matched_dn = 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,
101 e->e_dn, LDAP_SCOPE_DEFAULT )
103 cache_return_entry_r( &li->li_cache, matched );
106 refs = referral_rewrite( default_referral,
107 NULL, e->e_dn, LDAP_SCOPE_DEFAULT );
111 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
112 "ldbm_back_add: Parent of (%s) does not exist.\n",
115 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 ber_bvecfree( refs );
129 /* don't need the add lock anymore */
130 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
132 if ( ! access_allowed( be, conn, op, p,
133 children, NULL, ACL_WRITE ) )
135 /* free parent and writer lock */
136 cache_return_entry_w( &li->li_cache, p );
139 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
140 "ldbm_back_add: No write access to parent (%s).\n",
143 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
147 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
148 NULL, "no write access to parent", NULL, NULL );
154 if ( is_entry_alias( p ) ) {
155 /* parent is an alias, don't allow add */
157 /* free parent and writer lock */
158 cache_return_entry_w( &li->li_cache, p );
161 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
162 "ldbm_back_add: Parent is an alias.\n"));
164 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
169 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
170 NULL, "parent is an alias", NULL, NULL );
175 if ( is_entry_referral( p ) ) {
176 /* parent is a referral, don't allow add */
177 char *matched_dn = ch_strdup( p->e_dn );
178 struct berval **refs = is_entry_referral( p )
179 ? get_entry_referrals( be, conn, op, p,
180 e->e_dn, LDAP_SCOPE_DEFAULT )
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' );
207 /* no parent, must be adding entry to root */
208 if ( !be_isroot( be, op->o_ndn.bv_val ) ) {
209 if ( be_issuffix( be, "" )
210 || be_isupdate( be, op->o_ndn.bv_val ) ) {
211 p = (Entry *)&slap_entry_root;
213 rc = access_allowed( be, conn, op, p,
214 children, NULL, ACL_WRITE );
218 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
221 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
222 "ldbm_back_add: No write "
223 "access to parent (\"\").\n" ));
225 Debug( LDAP_DEBUG_TRACE,
226 "no write access to parent\n",
230 send_ldap_result( conn, op,
231 LDAP_INSUFFICIENT_ACCESS,
233 "no write access to parent",
240 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
243 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
244 "ldbm_back_add: %s add denied.\n",
245 pdn == NULL ? "suffix"
246 : "entry at root" ));
248 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
249 pdn == NULL ? "suffix"
250 : "entry at root", 0, 0 );
253 send_ldap_result( conn, op,
254 LDAP_INSUFFICIENT_ACCESS,
255 NULL, NULL, NULL, NULL );
262 * no parent, acquire the root write lock
263 * and release the add lock.
265 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
267 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
270 if ( next_id( be, &e->e_id ) ) {
272 /* free parent and writer lock */
273 cache_return_entry_w( &li->li_cache, p );
277 /* release root lock */
278 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
282 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
283 "ldbm_back_add: next_id failed.\n" ));
285 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
290 send_ldap_result( conn, op, LDAP_OTHER,
291 NULL, "next_id add failed", NULL, NULL );
297 * Try to add the entry to the cache, assign it a new dnid.
299 rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
303 /* free parent and writer lock */
304 cache_return_entry_w( &li->li_cache, p );
308 /* release root lock */
309 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
313 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
314 "ldbm_back_add: cache_add_entry_lock failed.\n" ));
316 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
320 send_ldap_result( conn, op,
321 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
322 NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
329 /* attribute indexes */
330 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
332 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
333 "ldbm_back_add: index_entry_add failed.\n" ));
335 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
339 send_ldap_result( conn, op, LDAP_OTHER,
340 NULL, "index generation failed", NULL, NULL );
346 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
348 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
349 "ldbm_back_add: dn2id_add failed.\n" ));
351 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
354 /* FIXME: delete attr indices? */
356 send_ldap_result( conn, op, LDAP_OTHER,
357 NULL, "DN index generation failed", NULL, NULL );
363 if ( id2entry_add( be, e ) != 0 ) {
365 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
366 "ldbm_back_add: id2entry_add failed.\n" ));
368 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
372 /* FIXME: delete attr indices? */
373 (void) dn2id_delete( be, e->e_ndn, e->e_id );
375 send_ldap_result( conn, op, LDAP_OTHER,
376 NULL, "entry store failed", NULL, NULL );
381 send_ldap_result( conn, op, LDAP_SUCCESS,
382 NULL, NULL, NULL, NULL );
384 /* marks the entry as committed, so it is added to the cache;
385 * otherwise it is removed from the cache, but not destroyed;
386 * it will be destroyed by the caller */
388 cache_entry_commit( e );
392 /* free parent and writer lock */
393 cache_return_entry_w( &li->li_cache, p );
397 /* release root lock */
398 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
402 /* in case of error, writer lock is freed
403 * and entry's private data is destroyed */
404 cache_return_entry_w( &li->li_cache, e );