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 ) ) != NOID ) {
49 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
50 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
51 NULL, NULL, NULL, NULL );
55 rc = entry_schema_check( e, NULL, &text, textbuf, textlen );
57 if ( rc != LDAP_SUCCESS ) {
58 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
61 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
62 "ldbm_back_add: entry (%s) failed schema check.\n",
65 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
70 send_ldap_result( conn, op, rc,
71 NULL, text, NULL, NULL );
76 * Get the parent dn and see if the corresponding entry exists.
77 * If the parent does not exist, only allow the "root" user to
81 pdn = dn_parent( be, e->e_ndn );
83 if( pdn != NULL && *pdn != '\0' ) {
84 Entry *matched = NULL;
86 assert( *pdn != '\0' );
88 /* get parent with writer lock */
89 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
93 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
95 if ( matched != NULL ) {
96 matched_dn = ch_strdup( matched->e_dn );
97 refs = is_entry_referral( matched )
98 ? get_entry_referrals( be, conn, op, matched )
100 cache_return_entry_r( &li->li_cache, matched );
104 refs = default_referral;
108 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
109 "ldbm_back_add: Parent of (%s) does not exist.\n",
112 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
117 send_ldap_result( conn, op, LDAP_REFERRAL, matched_dn,
118 refs == NULL ? "parent does not exist" : "parent is referral",
121 if( matched != NULL ) {
122 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 )
185 /* free parent and writer lock */
186 cache_return_entry_w( &li->li_cache, p );
189 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
190 "ldbm_back_add: Parent is referral.\n" ));
192 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
196 send_ldap_result( conn, op, LDAP_REFERRAL,
197 matched_dn, NULL, refs, NULL );
199 ber_bvecfree( refs );
206 assert( *pdn == '\0' );
210 /* no parent, must be adding entry to root */
211 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix( be, "" ) ) {
212 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
215 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
216 "ldbm_back_add: %s add denied.\n",
217 pdn == NULL ? "suffix" : "entry at root" ));
219 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
220 pdn == NULL ? "suffix" : "entry at root",
225 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
226 NULL, NULL, NULL, NULL );
232 * no parent, acquire the root write lock
233 * and release the add lock.
235 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
237 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
240 e->e_id = next_id( be );
242 if( e->e_id == NOID ) {
244 /* free parent and writer lock */
245 cache_return_entry_w( &li->li_cache, p );
249 /* release root lock */
250 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
254 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
255 "ldbm_back_add: next_id failed.\n" ));
257 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
262 send_ldap_result( conn, op, LDAP_OTHER,
263 NULL, "next_id add failed", NULL, NULL );
269 * Try to add the entry to the cache, assign it a new dnid.
271 rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
275 /* free parent and writer lock */
276 cache_return_entry_w( &li->li_cache, p );
280 /* release root lock */
281 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
285 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
286 "ldbm_back_add: cache_add_entry_lock failed.\n" ));
288 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
293 send_ldap_result( conn, op,
294 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
295 NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
302 /* attribute indexes */
303 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
305 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
306 "ldbm_back_add: index_entry_add failed.\n" ));
308 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
312 send_ldap_result( conn, op, LDAP_OTHER,
313 NULL, "index generation failed", NULL, NULL );
319 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
321 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
322 "ldbm_back_add: dn2id_add failed.\n" ));
324 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
328 send_ldap_result( conn, op, LDAP_OTHER,
329 NULL, "DN index generation failed", NULL, NULL );
335 if ( id2entry_add( be, e ) != 0 ) {
337 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
338 "ldbm_back_add: id2entry_add failed.\n" ));
340 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
344 (void) dn2id_delete( be, e->e_ndn, e->e_id );
345 send_ldap_result( conn, op, LDAP_OTHER,
346 NULL, "entry store failed", NULL, NULL );
351 send_ldap_result( conn, op, LDAP_SUCCESS,
352 NULL, NULL, NULL, NULL );
357 /* free parent and writer lock */
358 cache_return_entry_w( &li->li_cache, p );
362 /* release root lock */
363 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
367 /* free entry and writer lock */
368 cache_return_entry_w( &li->li_cache, e );