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 AttributeDescription *entry = slap_schema.si_ad_entry;
35 char textbuf[SLAP_TEXT_BUFLEN];
36 size_t textlen = sizeof textbuf;
39 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_add: %s\n", e->e_dn, 0, 0 );
41 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", e->e_dn, 0, 0);
44 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
45 if ( rc != LDAP_SUCCESS ) {
47 LDAP_LOG( BACK_LDBM, ERR,
48 "ldbm_back_add: entry (%s) failed schema check.\n", e->e_dn, 0, 0 );
50 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
54 send_ldap_result( conn, op, rc,
55 NULL, text, NULL, NULL );
59 if ( ! access_allowed( be, conn, op, e,
60 entry, NULL, ACL_WRITE, NULL ) )
63 LDAP_LOG( BACK_LDBM, ERR,
64 "ldbm_back_add: No write access to entry (%s).\n",
67 Debug( LDAP_DEBUG_TRACE, "no write access to entry\n", 0,
71 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
72 NULL, "no write access to entry", NULL, NULL );
77 /* grab giant lock for writing */
78 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
80 if ( ( rc = dn2id( be, &e->e_nname, &id ) ) || id != NOID ) {
81 /* if (rc) something bad happened to ldbm cache */
82 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
83 send_ldap_result( conn, op,
84 rc ? LDAP_OTHER : LDAP_ALREADY_EXISTS,
85 NULL, NULL, NULL, NULL );
90 * Get the parent dn and see if the corresponding entry exists.
91 * If the parent does not exist, only allow the "root" user to
95 if ( be_issuffix( be, &e->e_nname ) ) {
98 dnParent( &e->e_nname, &pdn );
102 Entry *matched = NULL;
104 /* get parent with writer lock */
105 if ( (p = dn2entry_w( be, &pdn, &matched )) == NULL ) {
106 char *matched_dn = NULL;
109 if ( matched != NULL ) {
110 matched_dn = ch_strdup( matched->e_dn );
111 refs = is_entry_referral( matched )
112 ? get_entry_referrals( be, conn, op, matched )
114 cache_return_entry_r( &li->li_cache, matched );
117 refs = referral_rewrite( default_referral,
118 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
121 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
124 LDAP_LOG( BACK_LDBM, ERR,
125 "ldbm_back_add: Parent of (%s) does not exist.\n",
128 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
132 send_ldap_result( conn, op, LDAP_REFERRAL, matched_dn,
133 refs == NULL ? "parent does not exist" : "parent is referral",
136 ber_bvarray_free( refs );
142 if ( ! access_allowed( be, conn, op, p,
143 children, NULL, ACL_WRITE, NULL ) )
145 /* free parent and writer lock */
146 cache_return_entry_w( &li->li_cache, p );
147 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
150 LDAP_LOG( BACK_LDBM, ERR,
151 "ldbm_back_add: No write access to parent (%s).\n",
154 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
158 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
159 NULL, "no write access to parent", NULL, NULL );
164 if ( is_entry_alias( p ) ) {
165 /* parent is an alias, don't allow add */
167 /* free parent and writer lock */
168 cache_return_entry_w( &li->li_cache, p );
169 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
172 LDAP_LOG(BACK_LDBM, ERR,
173 "ldbm_back_add: Parent is an alias.\n", 0, 0, 0 );
175 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
180 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
181 NULL, "parent is an alias", NULL, NULL );
186 if ( is_entry_referral( p ) ) {
187 /* parent is a referral, don't allow add */
188 char *matched_dn = ch_strdup( p->e_dn );
189 BerVarray refs = is_entry_referral( p )
190 ? get_entry_referrals( be, conn, op, p )
193 /* free parent and writer lock */
194 cache_return_entry_w( &li->li_cache, p );
195 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
198 LDAP_LOG( BACK_LDBM, ERR,
199 "ldbm_back_add: Parent is referral.\n", 0, 0, 0 );
201 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
205 send_ldap_result( conn, op, LDAP_REFERRAL,
206 matched_dn, NULL, refs, NULL );
208 ber_bvarray_free( refs );
214 if(pdn.bv_val != NULL) {
215 assert( *pdn.bv_val == '\0' );
218 /* no parent, must be adding entry to root */
219 if ( !be_isroot( be, &op->o_ndn ) ) {
220 if ( be_issuffix( be, (struct berval *)&slap_empty_bv ) || be_isupdate( be, &op->o_ndn ) ) {
221 p = (Entry *)&slap_entry_root;
223 rc = access_allowed( be, conn, op, p,
224 children, NULL, ACL_WRITE, NULL );
228 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
231 LDAP_LOG( BACK_LDBM, ERR,
232 "ldbm_back_add: No write "
233 "access to parent (\"\").\n", 0, 0, 0 );
235 Debug( LDAP_DEBUG_TRACE,
236 "no write access to parent\n",
240 send_ldap_result( conn, op,
241 LDAP_INSUFFICIENT_ACCESS,
243 "no write access to parent",
250 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
253 LDAP_LOG( BACK_LDBM, ERR,
254 "ldbm_back_add: %s add denied.\n",
255 pdn.bv_val == NULL ? "suffix"
256 : "entry at root", 0, 0 );
258 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
259 pdn.bv_val == NULL ? "suffix"
260 : "entry at root", 0, 0 );
263 send_ldap_result( conn, op,
264 LDAP_INSUFFICIENT_ACCESS,
265 NULL, NULL, NULL, NULL );
272 if ( next_id( be, &e->e_id ) ) {
274 /* free parent and writer lock */
275 cache_return_entry_w( &li->li_cache, p );
278 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
281 LDAP_LOG( BACK_LDBM, ERR,
282 "ldbm_back_add: next_id failed.\n", 0, 0, 0 );
284 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
288 send_ldap_result( conn, op, LDAP_OTHER,
289 NULL, "next_id add failed", NULL, NULL );
295 * Try to add the entry to the cache, assign it a new dnid.
297 rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
301 /* free parent and writer lock */
302 cache_return_entry_w( &li->li_cache, p );
305 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
308 LDAP_LOG( BACK_LDBM, ERR,
309 "ldbm_back_add: cache_add_entry_lock failed.\n", 0, 0, 0 );
311 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
315 send_ldap_result( conn, op,
316 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
317 NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
324 /* attribute indexes */
325 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
327 LDAP_LOG( BACK_LDBM, ERR,
328 "ldbm_back_add: index_entry_add failed.\n", 0, 0, 0 );
330 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
334 send_ldap_result( conn, op, LDAP_OTHER,
335 NULL, "index generation failed", NULL, NULL );
341 if ( dn2id_add( be, &e->e_nname, e->e_id ) != 0 ) {
343 LDAP_LOG( BACK_LDBM, ERR,
344 "ldbm_back_add: dn2id_add failed.\n", 0, 0, 0 );
346 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
349 /* FIXME: delete attr indices? */
351 send_ldap_result( conn, op, LDAP_OTHER,
352 NULL, "DN index generation failed", NULL, NULL );
358 if ( id2entry_add( be, e ) != 0 ) {
360 LDAP_LOG( BACK_LDBM, ERR,
361 "ldbm_back_add: id2entry_add failed.\n", 0, 0, 0 );
363 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
367 /* FIXME: delete attr indices? */
368 (void) dn2id_delete( be, &e->e_nname, e->e_id );
370 send_ldap_result( conn, op, LDAP_OTHER,
371 NULL, "entry store failed", NULL, NULL );
376 send_ldap_result( conn, op, LDAP_SUCCESS,
377 NULL, NULL, NULL, NULL );
379 /* marks the entry as committed, so it is added to the cache;
380 * otherwise it is removed from the cache, but not destroyed;
381 * it will be destroyed by the caller */
383 cache_entry_commit( e );
387 /* free parent and writer lock */
388 cache_return_entry_w( &li->li_cache, p );
393 * in case of error, writer lock is freed
394 * and entry's private data is destroyed.
395 * otherwise, this is done when entry is released
397 cache_return_entry_w( &li->li_cache, e );
398 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);