1 /* add.c - ldap ldbm back-end add routine */
4 * Copyright 1998-2002 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_nname, &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( be, 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 if ( be_issuffix( be, &e->e_nname ) ) {
86 rc = dnParent( &e->e_nname, &pdn );
87 /* dnParent always returns success */
91 Entry *matched = NULL;
93 /* get parent with writer lock */
94 if ( (p = dn2entry_w( be, &pdn, &matched )) == NULL ) {
95 char *matched_dn = NULL;
98 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
100 if ( matched != NULL ) {
101 matched_dn = ch_strdup( matched->e_dn );
102 refs = is_entry_referral( matched )
103 ? get_entry_referrals( be, conn, op, matched )
105 cache_return_entry_r( &li->li_cache, matched );
108 refs = referral_rewrite( default_referral,
109 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
113 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
114 "ldbm_back_add: Parent of (%s) does not exist.\n",
117 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
121 send_ldap_result( conn, op, LDAP_REFERRAL, matched_dn,
122 refs == NULL ? "parent does not exist" : "parent is referral",
125 ber_bvarray_free( refs );
131 /* don't need the add lock anymore */
132 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
134 if ( ! access_allowed( be, conn, op, p,
135 children, NULL, ACL_WRITE ) )
137 /* free parent and writer lock */
138 cache_return_entry_w( &li->li_cache, p );
141 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
142 "ldbm_back_add: No write access to parent (%s).\n",
145 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
149 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
150 NULL, "no write access to parent", NULL, NULL );
156 if ( is_entry_alias( p ) ) {
157 /* parent is an alias, don't allow add */
159 /* free parent and writer lock */
160 cache_return_entry_w( &li->li_cache, p );
163 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
164 "ldbm_back_add: Parent is an alias.\n"));
166 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
171 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
172 NULL, "parent is an alias", NULL, NULL );
177 if ( is_entry_referral( p ) ) {
178 /* parent is a referral, don't allow add */
179 char *matched_dn = ch_strdup( p->e_dn );
180 BerVarray refs = is_entry_referral( p )
181 ? get_entry_referrals( be, conn, op, p )
184 /* free parent and writer lock */
185 cache_return_entry_w( &li->li_cache, p );
188 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
189 "ldbm_back_add: Parent is referral.\n" ));
191 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
195 send_ldap_result( conn, op, LDAP_REFERRAL,
196 matched_dn, NULL, refs, NULL );
198 ber_bvarray_free( refs );
204 if(pdn.bv_val != NULL) {
205 assert( *pdn.bv_val == '\0' );
208 /* no parent, must be adding entry to root */
209 if ( !be_isroot( be, &op->o_ndn ) ) {
210 if ( be_issuffix( be, (struct berval *)&slap_empty_bv ) || be_isupdate( be, &op->o_ndn ) ) {
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.bv_val == NULL ? "suffix"
246 : "entry at root" ));
248 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
249 pdn.bv_val == 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_nname, 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_nname, 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 );