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, matched_dn,
117 refs == NULL ? "parent does not exist" : "parent is referral",
120 if( matched != NULL ) {
121 ber_bvecfree( refs );
129 /* don't need the add lock anymore */
130 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 struct berval **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_bvecfree( refs );
205 assert( *pdn == '\0' );
209 /* no parent, must be adding entry to root */
210 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix( be, "" ) ) {
211 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
214 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
215 "ldbm_back_add: %s add denied.\n",
216 pdn == NULL ? "suffix" : "entry at root" ));
218 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
219 pdn == NULL ? "suffix" : "entry at root",
224 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
225 NULL, NULL, NULL, NULL );
231 * no parent, acquire the root write lock
232 * and release the add lock.
234 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
236 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
239 e->e_id = next_id( be );
241 if( e->e_id == NOID ) {
243 /* free parent and writer lock */
244 cache_return_entry_w( &li->li_cache, p );
248 /* release root lock */
249 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
253 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
254 "ldbm_back_add: next_id failed.\n" ));
256 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
261 send_ldap_result( conn, op, LDAP_OTHER,
262 NULL, "next_id add failed", NULL, NULL );
268 * Try to add the entry to the cache, assign it a new dnid.
270 rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
274 /* free parent and writer lock */
275 cache_return_entry_w( &li->li_cache, p );
279 /* release root lock */
280 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
284 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
285 "ldbm_back_add: cache_add_entry_lock failed.\n" ));
287 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
292 send_ldap_result( conn, op,
293 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
294 NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
301 /* attribute indexes */
302 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
304 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
305 "ldbm_back_add: index_entry_add failed.\n" ));
307 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
311 send_ldap_result( conn, op, LDAP_OTHER,
312 NULL, "index generation failed", NULL, NULL );
318 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
320 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
321 "ldbm_back_add: dn2id_add failed.\n" ));
323 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
327 send_ldap_result( conn, op, LDAP_OTHER,
328 NULL, "DN index generation failed", NULL, NULL );
334 if ( id2entry_add( be, e ) != 0 ) {
336 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
337 "ldbm_back_add: id2entry_add failed.\n" ));
339 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
343 (void) dn2id_delete( be, e->e_ndn, e->e_id );
344 send_ldap_result( conn, op, LDAP_OTHER,
345 NULL, "entry store failed", NULL, NULL );
350 send_ldap_result( conn, op, LDAP_SUCCESS,
351 NULL, NULL, NULL, NULL );
356 /* free parent and writer lock */
357 cache_return_entry_w( &li->li_cache, p );
361 /* release root lock */
362 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
366 /* free entry and writer lock */
367 cache_return_entry_w( &li->li_cache, e );