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);
46 /* nobody else can add until we lock our parent */
47 ldap_pvt_thread_mutex_lock(&li->li_add_mutex);
49 if ( ( rc = dn2id( be, e->e_ndn, &id ) ) || id != NOID ) {
50 /* if (rc) something bad happened to ldbm cache */
51 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
52 send_ldap_result( conn, op,
53 rc ? LDAP_OPERATIONS_ERROR : LDAP_ALREADY_EXISTS,
54 NULL, NULL, NULL, NULL );
58 rc = entry_schema_check( e, NULL, &text, textbuf, textlen );
60 if ( rc != LDAP_SUCCESS ) {
61 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
64 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
65 "ldbm_back_add: entry (%s) failed schema check.\n",
68 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
73 send_ldap_result( conn, op, rc,
74 NULL, text, NULL, NULL );
79 * Get the parent dn and see if the corresponding entry exists.
80 * If the parent does not exist, only allow the "root" user to
84 pdn = dn_parent( be, e->e_ndn );
86 if( pdn != NULL && *pdn != '\0' ) {
87 Entry *matched = NULL;
89 assert( *pdn != '\0' );
91 /* get parent with writer lock */
92 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
96 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
98 if ( matched != NULL ) {
99 matched_dn = ch_strdup( matched->e_dn );
100 refs = is_entry_referral( matched )
101 ? get_entry_referrals( be, conn, op, matched )
103 cache_return_entry_r( &li->li_cache, matched );
107 refs = default_referral;
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",
120 send_ldap_result( conn, op, LDAP_REFERRAL, matched_dn,
121 refs == NULL ? "parent does not exist" : "parent is referral",
124 if( matched != NULL ) {
125 ber_bvecfree( refs );
133 /* don't need the add lock anymore */
134 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
138 if ( ! access_allowed( be, conn, op, p,
139 children, NULL, ACL_WRITE ) )
141 /* free parent and writer lock */
142 cache_return_entry_w( &li->li_cache, p );
145 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
146 "ldbm_back_add: No write access to parent (%s).\n",
149 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
153 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
154 NULL, "no write access to parent", NULL, NULL );
160 if ( is_entry_alias( p ) ) {
161 /* parent is an alias, don't allow add */
163 /* free parent and writer lock */
164 cache_return_entry_w( &li->li_cache, p );
167 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
168 "ldbm_back_add: Parent is an alias.\n"));
170 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
175 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
176 NULL, "parent is an alias", NULL, NULL );
181 if ( is_entry_referral( p ) ) {
182 /* parent is a referral, don't allow add */
183 char *matched_dn = ch_strdup( p->e_dn );
184 struct berval **refs = is_entry_referral( p )
185 ? get_entry_referrals( be, conn, op, p )
188 /* free parent and writer lock */
189 cache_return_entry_w( &li->li_cache, p );
192 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
193 "ldbm_back_add: Parent is referral.\n" ));
195 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
199 send_ldap_result( conn, op, LDAP_REFERRAL,
200 matched_dn, NULL, refs, NULL );
202 ber_bvecfree( refs );
209 assert( *pdn == '\0' );
213 /* no parent, must be adding entry to root */
214 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix( be, "" ) ) {
215 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
218 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
219 "ldbm_back_add: %s add denied.\n",
220 pdn == NULL ? "suffix" : "entry at root" ));
222 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
223 pdn == NULL ? "suffix" : "entry at root",
228 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
229 NULL, NULL, NULL, NULL );
235 * no parent, acquire the root write lock
236 * and release the add lock.
238 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
240 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
243 if ( next_id( be, &e->e_id ) ) {
245 /* free parent and writer lock */
246 cache_return_entry_w( &li->li_cache, p );
250 /* release root lock */
251 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
255 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
256 "ldbm_back_add: next_id failed.\n" ));
258 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
263 send_ldap_result( conn, op, LDAP_OTHER,
264 NULL, "next_id add failed", NULL, NULL );
270 * Try to add the entry to the cache, assign it a new dnid.
272 rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
276 /* free parent and writer lock */
277 cache_return_entry_w( &li->li_cache, p );
281 /* release root lock */
282 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
286 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
287 "ldbm_back_add: cache_add_entry_lock failed.\n" ));
289 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
294 send_ldap_result( conn, op,
295 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
296 NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
303 /* attribute indexes */
304 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
306 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
307 "ldbm_back_add: index_entry_add failed.\n" ));
309 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
313 send_ldap_result( conn, op, LDAP_OTHER,
314 NULL, "index generation failed", NULL, NULL );
320 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
322 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
323 "ldbm_back_add: dn2id_add failed.\n" ));
325 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
328 /* FIXME: delete attr indices? */
330 send_ldap_result( conn, op, LDAP_OTHER,
331 NULL, "DN index generation failed", NULL, NULL );
337 if ( id2entry_add( be, e ) != 0 ) {
339 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
340 "ldbm_back_add: id2entry_add failed.\n" ));
342 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
346 /* FIXME: delete attr indices? */
347 (void) dn2id_delete( be, e->e_ndn, e->e_id );
349 send_ldap_result( conn, op, LDAP_OTHER,
350 NULL, "entry store failed", NULL, NULL );
355 send_ldap_result( conn, op, LDAP_SUCCESS,
356 NULL, NULL, NULL, NULL );
358 /* marks the entry as committed, so it is added to the cache;
359 * otherwise it is removed from the cache, but not destroyed;
360 * it will be destroyed by the caller */
362 cache_entry_commit( e );
366 /* free parent and writer lock */
367 cache_return_entry_w( &li->li_cache, p );
371 /* release root lock */
372 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
376 /* in case of error, writer lock is freed
377 * and entry's private data is destroyed */
378 cache_return_entry_w( &li->li_cache, e );