1 /* add.c - ldap ldbm back-end add routine */
4 * Copyright 1998-2003 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);
45 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
46 #else /* LDAP_CACHING */
47 if ( !op->o_caching_on ) {
48 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
52 #endif /* LDAP_CACHING */
54 if ( rc != LDAP_SUCCESS ) {
56 LDAP_LOG( BACK_LDBM, ERR,
57 "ldbm_back_add: entry (%s) failed schema check.\n", e->e_dn, 0, 0 );
59 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
63 send_ldap_result( conn, op, rc,
64 NULL, text, NULL, NULL );
69 if ( !op->o_caching_on ) {
70 #endif /* LDAP_CACHING */
71 if ( !access_allowed( be, conn, op, e,
72 entry, NULL, ACL_WRITE, NULL ) )
75 LDAP_LOG( BACK_LDBM, ERR,
76 "ldbm_back_add: No write access to entry (%s).\n",
79 Debug( LDAP_DEBUG_TRACE, "no write access to entry\n", 0,
83 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
84 NULL, "no write access to entry", NULL, NULL );
90 #endif /* LDAP_CACHING */
92 /* grab giant lock for writing */
93 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
95 if ( ( rc = dn2id( be, &e->e_nname, &id ) ) || id != NOID ) {
96 /* if (rc) something bad happened to ldbm cache */
97 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
98 send_ldap_result( conn, op,
99 rc ? LDAP_OTHER : LDAP_ALREADY_EXISTS,
100 NULL, NULL, NULL, NULL );
105 * Get the parent dn and see if the corresponding entry exists.
106 * If the parent does not exist, only allow the "root" user to
110 if ( be_issuffix( be, &e->e_nname ) ) {
113 dnParent( &e->e_nname, &pdn );
118 #else /* LDAP_CACHING */
119 if( pdn.bv_len && !op->o_caching_on )
120 #endif /* LDAP_CACHING */
122 Entry *matched = NULL;
124 /* get parent with writer lock */
125 if ( (p = dn2entry_w( be, &pdn, &matched )) == NULL ) {
126 char *matched_dn = NULL;
129 if ( matched != NULL ) {
130 matched_dn = ch_strdup( matched->e_dn );
131 refs = is_entry_referral( matched )
132 ? get_entry_referrals( be, conn, op, matched )
134 cache_return_entry_r( &li->li_cache, matched );
137 refs = referral_rewrite( default_referral,
138 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
141 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
144 LDAP_LOG( BACK_LDBM, ERR,
145 "ldbm_back_add: Parent of (%s) does not exist.\n",
148 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
152 send_ldap_result( conn, op, LDAP_REFERRAL, matched_dn,
153 refs == NULL ? "parent does not exist" : "parent is referral",
156 ber_bvarray_free( refs );
162 if ( ! access_allowed( be, conn, op, p,
163 children, NULL, ACL_WRITE, NULL ) )
165 /* free parent and writer lock */
166 cache_return_entry_w( &li->li_cache, p );
167 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
170 LDAP_LOG( BACK_LDBM, ERR,
171 "ldbm_back_add: No write access to parent (%s).\n",
174 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
178 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
179 NULL, "no write access to parent", NULL, NULL );
184 if ( is_entry_alias( p ) ) {
185 /* parent is an alias, don't allow add */
187 /* free parent and writer lock */
188 cache_return_entry_w( &li->li_cache, p );
189 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
192 LDAP_LOG(BACK_LDBM, ERR,
193 "ldbm_back_add: Parent is an alias.\n", 0, 0, 0 );
195 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
200 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
201 NULL, "parent is an alias", NULL, NULL );
206 if ( is_entry_referral( p ) ) {
207 /* parent is a referral, don't allow add */
208 char *matched_dn = ch_strdup( p->e_dn );
209 BerVarray refs = is_entry_referral( p )
210 ? get_entry_referrals( be, conn, op, p )
213 /* free parent and writer lock */
214 cache_return_entry_w( &li->li_cache, p );
215 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
218 LDAP_LOG( BACK_LDBM, ERR,
219 "ldbm_back_add: Parent is referral.\n", 0, 0, 0 );
221 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
225 send_ldap_result( conn, op, LDAP_REFERRAL,
226 matched_dn, NULL, refs, NULL );
228 ber_bvarray_free( refs );
235 if( pdn.bv_val != NULL )
236 #else /* LDAP_CACHING */
237 if( pdn.bv_val != NULL && !op->o_caching_on )
238 #endif /* LDAP_CACHING */
240 assert( *pdn.bv_val == '\0' );
243 /* no parent, must be adding entry to root */
245 if ( !be_isroot( be, &op->o_ndn ) )
246 #else /* LDAP_CACHING */
247 if ( !be_isroot( be, &op->o_ndn ) && !op->o_caching_on )
248 #endif /* LDAP_CACHING */
250 if ( be_issuffix( be, (struct berval *)&slap_empty_bv ) || be_isupdate( be, &op->o_ndn ) ) {
251 p = (Entry *)&slap_entry_root;
253 rc = access_allowed( be, conn, op, p,
254 children, NULL, ACL_WRITE, NULL );
258 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
261 LDAP_LOG( BACK_LDBM, ERR,
262 "ldbm_back_add: No write "
263 "access to parent (\"\").\n", 0, 0, 0 );
265 Debug( LDAP_DEBUG_TRACE,
266 "no write access to parent\n",
270 send_ldap_result( conn, op,
271 LDAP_INSUFFICIENT_ACCESS,
273 "no write access to parent",
280 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
283 LDAP_LOG( BACK_LDBM, ERR,
284 "ldbm_back_add: %s add denied.\n",
285 pdn.bv_val == NULL ? "suffix"
286 : "entry at root", 0, 0 );
288 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
289 pdn.bv_val == NULL ? "suffix"
290 : "entry at root", 0, 0 );
293 send_ldap_result( conn, op,
294 LDAP_INSUFFICIENT_ACCESS,
295 NULL, NULL, NULL, NULL );
302 if ( next_id( be, &e->e_id ) ) {
304 /* free parent and writer lock */
305 cache_return_entry_w( &li->li_cache, p );
308 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
311 LDAP_LOG( BACK_LDBM, ERR,
312 "ldbm_back_add: next_id failed.\n", 0, 0, 0 );
314 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
318 send_ldap_result( conn, op, LDAP_OTHER,
319 NULL, "next_id add failed", NULL, NULL );
325 * Try to add the entry to the cache, assign it a new dnid.
327 rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
331 /* free parent and writer lock */
332 cache_return_entry_w( &li->li_cache, p );
335 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
338 LDAP_LOG( BACK_LDBM, ERR,
339 "ldbm_back_add: cache_add_entry_lock failed.\n", 0, 0, 0 );
341 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
345 send_ldap_result( conn, op,
346 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
347 NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
354 /* attribute indexes */
355 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
357 LDAP_LOG( BACK_LDBM, ERR,
358 "ldbm_back_add: index_entry_add failed.\n", 0, 0, 0 );
360 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
364 send_ldap_result( conn, op, LDAP_OTHER,
365 NULL, "index generation failed", NULL, NULL );
371 if ( dn2id_add( be, &e->e_nname, e->e_id ) != 0 ) {
373 LDAP_LOG( BACK_LDBM, ERR,
374 "ldbm_back_add: dn2id_add failed.\n", 0, 0, 0 );
376 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
379 /* FIXME: delete attr indices? */
381 send_ldap_result( conn, op, LDAP_OTHER,
382 NULL, "DN index generation failed", NULL, NULL );
388 if ( id2entry_add( be, e ) != 0 ) {
390 LDAP_LOG( BACK_LDBM, ERR,
391 "ldbm_back_add: id2entry_add failed.\n", 0, 0, 0 );
393 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
397 /* FIXME: delete attr indices? */
398 (void) dn2id_delete( be, &e->e_nname, e->e_id );
400 send_ldap_result( conn, op, LDAP_OTHER,
401 NULL, "entry store failed", NULL, NULL );
406 send_ldap_result( conn, op, LDAP_SUCCESS,
407 NULL, NULL, NULL, NULL );
409 /* marks the entry as committed, so it is added to the cache;
410 * otherwise it is removed from the cache, but not destroyed;
411 * it will be destroyed by the caller */
413 cache_entry_commit( e );
417 /* free parent and writer lock */
418 cache_return_entry_w( &li->li_cache, p );
423 * in case of error, writer lock is freed
424 * and entry's private data is destroyed.
425 * otherwise, this is done when entry is released
427 cache_return_entry_w( &li->li_cache, e );
428 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);