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;
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( BACK_LDBM, ENTRY, "ldbm_back_add: %s\n", e->e_dn, 0, 0 );
40 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", e->e_dn, 0, 0);
43 /* grab giant lock for writing */
44 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
46 if ( ( rc = dn2id( be, &e->e_nname, &id ) ) || id != NOID ) {
47 /* if (rc) something bad happened to ldbm cache */
48 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
49 send_ldap_result( conn, op,
50 rc ? LDAP_OTHER : LDAP_ALREADY_EXISTS,
51 NULL, NULL, NULL, NULL );
55 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
57 if ( rc != LDAP_SUCCESS ) {
58 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
61 LDAP_LOG( BACK_LDBM, ERR,
62 "ldbm_back_add: entry (%s) failed schema check.\n", e->e_dn, 0, 0 );
64 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
68 send_ldap_result( conn, op, rc,
69 NULL, text, NULL, NULL );
74 * Get the parent dn and see if the corresponding entry exists.
75 * If the parent does not exist, only allow the "root" user to
79 if ( be_issuffix( be, &e->e_nname ) ) {
82 dnParent( &e->e_nname, &pdn );
86 Entry *matched = NULL;
88 /* get parent with writer lock */
89 if ( (p = dn2entry_w( be, &pdn, &matched )) == NULL ) {
90 char *matched_dn = NULL;
93 if ( matched != NULL ) {
94 matched_dn = ch_strdup( matched->e_dn );
95 refs = is_entry_referral( matched )
96 ? get_entry_referrals( be, conn, op, matched )
98 cache_return_entry_r( &li->li_cache, matched );
101 refs = referral_rewrite( default_referral,
102 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
105 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
108 LDAP_LOG( BACK_LDBM, ERR,
109 "ldbm_back_add: Parent of (%s) does not exist.\n",
112 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 ber_bvarray_free( refs );
126 if ( ! access_allowed( be, conn, op, p,
127 children, NULL, ACL_WRITE, NULL ) )
129 /* free parent and writer lock */
130 cache_return_entry_w( &li->li_cache, p );
131 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
134 LDAP_LOG( BACK_LDBM, ERR,
135 "ldbm_back_add: No write access to parent (%s).\n",
138 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
142 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
143 NULL, "no write access to parent", NULL, NULL );
148 if ( is_entry_alias( p ) ) {
149 /* parent is an alias, don't allow add */
151 /* free parent and writer lock */
152 cache_return_entry_w( &li->li_cache, p );
153 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
156 LDAP_LOG(BACK_LDBM, ERR,
157 "ldbm_back_add: Parent is an alias.\n", 0, 0, 0 );
159 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
164 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
165 NULL, "parent is an alias", NULL, NULL );
170 if ( is_entry_referral( p ) ) {
171 /* parent is a referral, don't allow add */
172 char *matched_dn = ch_strdup( p->e_dn );
173 BerVarray refs = is_entry_referral( p )
174 ? get_entry_referrals( be, conn, op, p )
177 /* free parent and writer lock */
178 cache_return_entry_w( &li->li_cache, p );
179 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
182 LDAP_LOG( BACK_LDBM, ERR,
183 "ldbm_back_add: Parent is referral.\n", 0, 0, 0 );
185 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
189 send_ldap_result( conn, op, LDAP_REFERRAL,
190 matched_dn, NULL, refs, NULL );
192 ber_bvarray_free( refs );
198 if(pdn.bv_val != NULL) {
199 assert( *pdn.bv_val == '\0' );
202 /* no parent, must be adding entry to root */
203 if ( !be_isroot( be, &op->o_ndn ) ) {
204 if ( be_issuffix( be, (struct berval *)&slap_empty_bv ) || be_isupdate( be, &op->o_ndn ) ) {
205 p = (Entry *)&slap_entry_root;
207 rc = access_allowed( be, conn, op, p,
208 children, NULL, ACL_WRITE, NULL );
212 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
215 LDAP_LOG( BACK_LDBM, ERR,
216 "ldbm_back_add: No write "
217 "access to parent (\"\").\n", 0, 0, 0 );
219 Debug( LDAP_DEBUG_TRACE,
220 "no write access to parent\n",
224 send_ldap_result( conn, op,
225 LDAP_INSUFFICIENT_ACCESS,
227 "no write access to parent",
234 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
237 LDAP_LOG( BACK_LDBM, ERR,
238 "ldbm_back_add: %s add denied.\n",
239 pdn.bv_val == NULL ? "suffix"
240 : "entry at root", 0, 0 );
242 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
243 pdn.bv_val == NULL ? "suffix"
244 : "entry at root", 0, 0 );
247 send_ldap_result( conn, op,
248 LDAP_INSUFFICIENT_ACCESS,
249 NULL, NULL, NULL, NULL );
256 if ( next_id( be, &e->e_id ) ) {
258 /* free parent and writer lock */
259 cache_return_entry_w( &li->li_cache, p );
262 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
265 LDAP_LOG( BACK_LDBM, ERR,
266 "ldbm_back_add: next_id failed.\n", 0, 0, 0 );
268 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
272 send_ldap_result( conn, op, LDAP_OTHER,
273 NULL, "next_id add failed", NULL, NULL );
279 * Try to add the entry to the cache, assign it a new dnid.
281 rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
285 /* free parent and writer lock */
286 cache_return_entry_w( &li->li_cache, p );
289 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
292 LDAP_LOG( BACK_LDBM, ERR,
293 "ldbm_back_add: cache_add_entry_lock failed.\n", 0, 0, 0 );
295 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
299 send_ldap_result( conn, op,
300 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
301 NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
308 /* attribute indexes */
309 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
311 LDAP_LOG( BACK_LDBM, ERR,
312 "ldbm_back_add: index_entry_add failed.\n", 0, 0, 0 );
314 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
318 send_ldap_result( conn, op, LDAP_OTHER,
319 NULL, "index generation failed", NULL, NULL );
325 if ( dn2id_add( be, &e->e_nname, e->e_id ) != 0 ) {
327 LDAP_LOG( BACK_LDBM, ERR,
328 "ldbm_back_add: dn2id_add failed.\n", 0, 0, 0 );
330 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
333 /* FIXME: delete attr indices? */
335 send_ldap_result( conn, op, LDAP_OTHER,
336 NULL, "DN index generation failed", NULL, NULL );
342 if ( id2entry_add( be, e ) != 0 ) {
344 LDAP_LOG( BACK_LDBM, ERR,
345 "ldbm_back_add: id2entry_add failed.\n", 0, 0, 0 );
347 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
351 /* FIXME: delete attr indices? */
352 (void) dn2id_delete( be, &e->e_nname, e->e_id );
354 send_ldap_result( conn, op, LDAP_OTHER,
355 NULL, "entry store failed", NULL, NULL );
360 send_ldap_result( conn, op, LDAP_SUCCESS,
361 NULL, NULL, NULL, NULL );
363 /* marks the entry as committed, so it is added to the cache;
364 * otherwise it is removed from the cache, but not destroyed;
365 * it will be destroyed by the caller */
367 cache_entry_commit( e );
371 /* free parent and writer lock */
372 cache_return_entry_w( &li->li_cache, p );
377 * in case of error, writer lock is freed
378 * and entry's private data is destroyed.
379 * otherwise, this is done when entry is released
381 cache_return_entry_w( &li->li_cache, e );
382 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);