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(( "backend", LDAP_LEVEL_ENTRY,"ldbm_back_add: %s\n",
41 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", e->e_dn, 0, 0);
44 /* grab giant lock for writing */
45 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
47 if ( ( rc = dn2id( be, &e->e_nname, &id ) ) || id != NOID ) {
48 /* if (rc) something bad happened to ldbm cache */
49 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
50 send_ldap_result( conn, op,
51 rc ? LDAP_OPERATIONS_ERROR : LDAP_ALREADY_EXISTS,
52 NULL, NULL, NULL, NULL );
56 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
58 if ( rc != LDAP_SUCCESS ) {
59 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
62 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
63 "ldbm_back_add: entry (%s) failed schema check.\n",
66 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 if ( be_issuffix( be, &e->e_nname ) ) {
84 dnParent( &e->e_nname, &pdn );
88 Entry *matched = NULL;
90 /* get parent with writer lock */
91 if ( (p = dn2entry_w( be, &pdn, &matched )) == NULL ) {
92 char *matched_dn = NULL;
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 );
103 refs = referral_rewrite( default_referral,
104 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
107 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
110 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
111 "ldbm_back_add: Parent of (%s) does not exist.\n",
114 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
118 send_ldap_result( conn, op, LDAP_REFERRAL, matched_dn,
119 refs == NULL ? "parent does not exist" : "parent is referral",
122 ber_bvarray_free( refs );
128 if ( ! access_allowed( be, conn, op, p,
129 children, NULL, ACL_WRITE, NULL ) )
131 /* free parent and writer lock */
132 cache_return_entry_w( &li->li_cache, p );
133 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
136 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
137 "ldbm_back_add: No write access to parent (%s).\n",
140 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
144 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
145 NULL, "no write access to parent", NULL, NULL );
150 if ( is_entry_alias( p ) ) {
151 /* parent is an alias, don't allow add */
153 /* free parent and writer lock */
154 cache_return_entry_w( &li->li_cache, p );
155 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
158 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
159 "ldbm_back_add: Parent is an alias.\n"));
161 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
166 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
167 NULL, "parent is an alias", NULL, NULL );
172 if ( is_entry_referral( p ) ) {
173 /* parent is a referral, don't allow add */
174 char *matched_dn = ch_strdup( p->e_dn );
175 BerVarray refs = is_entry_referral( p )
176 ? get_entry_referrals( be, conn, op, p )
179 /* free parent and writer lock */
180 cache_return_entry_w( &li->li_cache, p );
181 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
184 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
185 "ldbm_back_add: Parent is referral.\n" ));
187 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
191 send_ldap_result( conn, op, LDAP_REFERRAL,
192 matched_dn, NULL, refs, NULL );
194 ber_bvarray_free( refs );
200 if(pdn.bv_val != NULL) {
201 assert( *pdn.bv_val == '\0' );
204 /* no parent, must be adding entry to root */
205 if ( !be_isroot( be, &op->o_ndn ) ) {
206 if ( be_issuffix( be, (struct berval *)&slap_empty_bv ) || be_isupdate( be, &op->o_ndn ) ) {
207 p = (Entry *)&slap_entry_root;
209 rc = access_allowed( be, conn, op, p,
210 children, NULL, ACL_WRITE, NULL );
214 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
217 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
218 "ldbm_back_add: No write "
219 "access to parent (\"\").\n" ));
221 Debug( LDAP_DEBUG_TRACE,
222 "no write access to parent\n",
226 send_ldap_result( conn, op,
227 LDAP_INSUFFICIENT_ACCESS,
229 "no write access to parent",
236 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
239 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
240 "ldbm_back_add: %s add denied.\n",
241 pdn.bv_val == NULL ? "suffix"
242 : "entry at root" ));
244 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
245 pdn.bv_val == NULL ? "suffix"
246 : "entry at root", 0, 0 );
249 send_ldap_result( conn, op,
250 LDAP_INSUFFICIENT_ACCESS,
251 NULL, NULL, NULL, NULL );
258 if ( next_id( be, &e->e_id ) ) {
260 /* free parent and writer lock */
261 cache_return_entry_w( &li->li_cache, p );
264 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
267 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
268 "ldbm_back_add: next_id failed.\n" ));
270 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
274 send_ldap_result( conn, op, LDAP_OTHER,
275 NULL, "next_id add failed", NULL, NULL );
281 * Try to add the entry to the cache, assign it a new dnid.
283 rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
287 /* free parent and writer lock */
288 cache_return_entry_w( &li->li_cache, p );
291 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
294 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
295 "ldbm_back_add: cache_add_entry_lock failed.\n" ));
297 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
301 send_ldap_result( conn, op,
302 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
303 NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
310 /* attribute indexes */
311 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
313 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
314 "ldbm_back_add: index_entry_add failed.\n" ));
316 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
320 send_ldap_result( conn, op, LDAP_OTHER,
321 NULL, "index generation failed", NULL, NULL );
327 if ( dn2id_add( be, &e->e_nname, e->e_id ) != 0 ) {
329 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
330 "ldbm_back_add: dn2id_add failed.\n" ));
332 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
335 /* FIXME: delete attr indices? */
337 send_ldap_result( conn, op, LDAP_OTHER,
338 NULL, "DN index generation failed", NULL, NULL );
344 if ( id2entry_add( be, e ) != 0 ) {
346 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
347 "ldbm_back_add: id2entry_add failed.\n" ));
349 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
353 /* FIXME: delete attr indices? */
354 (void) dn2id_delete( be, &e->e_nname, e->e_id );
356 send_ldap_result( conn, op, LDAP_OTHER,
357 NULL, "entry store failed", NULL, NULL );
362 send_ldap_result( conn, op, LDAP_SUCCESS,
363 NULL, NULL, NULL, NULL );
365 /* marks the entry as committed, so it is added to the cache;
366 * otherwise it is removed from the cache, but not destroyed;
367 * it will be destroyed by the caller */
369 cache_entry_commit( e );
373 /* free parent and writer lock */
374 cache_return_entry_w( &li->li_cache, p );
379 * in case of error, writer lock is freed
380 * and entry's private data is destroyed.
381 * otherwise, this is done when entry is released
383 cache_return_entry_w( &li->li_cache, e );
384 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);