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 );
130 /* don't need the add lock anymore */
131 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
135 if ( ! access_allowed( be, conn, op, p,
136 children, NULL, ACL_WRITE ) )
138 /* free parent and writer lock */
139 cache_return_entry_w( &li->li_cache, p );
142 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
143 "ldbm_back_add: No write access to parent (%s).\n",
146 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
150 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
151 NULL, "no write access to parent", NULL, NULL );
157 if ( is_entry_alias( p ) ) {
158 /* parent is an alias, don't allow add */
160 /* free parent and writer lock */
161 cache_return_entry_w( &li->li_cache, p );
164 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
165 "ldbm_back_add: Parent is an alias.\n"));
167 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
172 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
173 NULL, "parent is an alias", NULL, NULL );
178 if ( is_entry_referral( p ) ) {
179 /* parent is a referral, don't allow add */
180 char *matched_dn = ch_strdup( p->e_dn );
181 struct berval **refs = is_entry_referral( p )
182 ? get_entry_referrals( be, conn, op, p,
183 e->e_dn, LDAP_SCOPE_DEFAULT )
186 /* free parent and writer lock */
187 cache_return_entry_w( &li->li_cache, p );
190 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
191 "ldbm_back_add: Parent is referral.\n" ));
193 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
197 send_ldap_result( conn, op, LDAP_REFERRAL,
198 matched_dn, NULL, refs, NULL );
200 ber_bvecfree( refs );
207 assert( *pdn == '\0' );
211 /* no parent, must be adding entry to root */
212 if ( !be_isroot( be, op->o_ndn ) ) {
213 if ( be_issuffix( be, "" )
214 || be_isupdate( be, op->o_ndn ) ) {
215 static const Entry rootp = { NOID, "", "", NULL, NULL };
218 rc = access_allowed( be, conn, op, p,
219 children, NULL, ACL_WRITE );
223 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
226 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
227 "ldbm_back_add: No write "
228 "access to parent (\"\").\n" ));
230 Debug( LDAP_DEBUG_TRACE,
231 "no write access to parent\n",
235 send_ldap_result( conn, op,
236 LDAP_INSUFFICIENT_ACCESS,
238 "no write access to parent",
245 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
248 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
249 "ldbm_back_add: %s add denied.\n",
250 pdn == NULL ? "suffix"
251 : "entry at root" ));
253 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
254 pdn == NULL ? "suffix"
255 : "entry at root", 0, 0 );
258 send_ldap_result( conn, op,
259 LDAP_INSUFFICIENT_ACCESS,
260 NULL, NULL, NULL, NULL );
267 * no parent, acquire the root write lock
268 * and release the add lock.
270 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
272 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
275 if ( next_id( be, &e->e_id ) ) {
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: next_id failed.\n" ));
290 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
295 send_ldap_result( conn, op, LDAP_OTHER,
296 NULL, "next_id add failed", NULL, NULL );
302 * Try to add the entry to the cache, assign it a new dnid.
304 rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
308 /* free parent and writer lock */
309 cache_return_entry_w( &li->li_cache, p );
313 /* release root lock */
314 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
318 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
319 "ldbm_back_add: cache_add_entry_lock failed.\n" ));
321 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
325 send_ldap_result( conn, op,
326 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
327 NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
334 /* attribute indexes */
335 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
337 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
338 "ldbm_back_add: index_entry_add failed.\n" ));
340 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
344 send_ldap_result( conn, op, LDAP_OTHER,
345 NULL, "index generation failed", NULL, NULL );
351 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
353 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
354 "ldbm_back_add: dn2id_add failed.\n" ));
356 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
359 /* FIXME: delete attr indices? */
361 send_ldap_result( conn, op, LDAP_OTHER,
362 NULL, "DN index generation failed", NULL, NULL );
368 if ( id2entry_add( be, e ) != 0 ) {
370 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
371 "ldbm_back_add: id2entry_add failed.\n" ));
373 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
377 /* FIXME: delete attr indices? */
378 (void) dn2id_delete( be, e->e_ndn, e->e_id );
380 send_ldap_result( conn, op, LDAP_OTHER,
381 NULL, "entry store failed", NULL, NULL );
386 send_ldap_result( conn, op, LDAP_SUCCESS,
387 NULL, NULL, NULL, NULL );
389 /* marks the entry as committed, so it is added to the cache;
390 * otherwise it is removed from the cache, but not destroyed;
391 * it will be destroyed by the caller */
393 cache_entry_commit( e );
397 /* free parent and writer lock */
398 cache_return_entry_w( &li->li_cache, p );
402 /* release root lock */
403 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
407 /* in case of error, writer lock is freed
408 * and entry's private data is destroyed */
409 cache_return_entry_w( &li->li_cache, e );