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"
24 struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
28 AttributeDescription *children = slap_schema.si_ad_children;
29 AttributeDescription *entry = slap_schema.si_ad_entry;
30 char textbuf[SLAP_TEXT_BUFLEN];
31 size_t textlen = sizeof textbuf;
32 #ifdef LDBM_SUBENTRIES
37 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_add: %s\n", op->o_req_dn.bv_val, 0, 0 );
39 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", op->o_req_dn.bv_val, 0, 0);
42 rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e, NULL, &rs->sr_text, textbuf, textlen );
44 if ( rs->sr_err != LDAP_SUCCESS ) {
46 LDAP_LOG( BACK_LDBM, ERR,
47 "ldbm_back_add: entry (%s) failed schema check.\n", op->o_req_dn.bv_val, 0, 0 );
49 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
53 send_ldap_result( op, rs );
61 #ifdef LDBM_SUBENTRIES
62 subentry = is_entry_subentry( op->oq_add.rs_e );
65 if ( !access_allowed( op, op->oq_add.rs_e,
66 entry, NULL, ACL_WRITE, NULL ) )
69 LDAP_LOG( BACK_LDBM, ERR,
70 "ldbm_back_add: No write access to entry (%s).\n",
71 op->o_req_dn.bv_val, 0, 0 );
73 Debug( LDAP_DEBUG_TRACE, "no write access to entry\n", 0,
77 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
78 "no write access to entry" );
81 return LDAP_INSUFFICIENT_ACCESS;
87 /* grab giant lock for writing */
88 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
90 if ( ( rs->sr_err = dn2id( op->o_bd, &op->o_req_ndn, &id ) ) || id != NOID ) {
91 /* if (rs->sr_err) something bad happened to ldbm cache */
92 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
93 rs->sr_err = rs->sr_err ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
94 send_ldap_result( op, rs );
103 * Get the parent dn and see if the corresponding entry exists.
104 * If the parent does not exist, only allow the "root" user to
108 if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
111 dnParent( &op->o_req_ndn, &pdn );
116 Entry *matched = NULL;
118 /* get parent with writer lock */
119 if ( (p = dn2entry_w( op->o_bd, &pdn, &matched )) == NULL ) {
120 if ( matched != NULL ) {
121 rs->sr_matched = ch_strdup( matched->e_dn );
122 rs->sr_ref = is_entry_referral( matched )
123 ? get_entry_referrals( op, matched )
125 cache_return_entry_r( &li->li_cache, matched );
128 rs->sr_ref = referral_rewrite( default_referral,
129 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
132 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
135 LDAP_LOG( BACK_LDBM, ERR,
136 "ldbm_back_add: Parent of (%s) does not exist.\n",
137 op->o_req_dn.bv_val, 0, 0 );
139 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
143 rs->sr_text = rs->sr_ref ? "parent is referral" : "parent does not exist";
144 rs->sr_err = LDAP_REFERRAL;
145 send_ldap_result( op, rs );
147 ber_bvarray_free( rs->sr_ref );
148 free( (char *)rs->sr_matched );
157 if ( ! access_allowed( op, p,
158 children, NULL, ACL_WRITE, NULL ) )
160 /* free parent and writer lock */
161 cache_return_entry_w( &li->li_cache, p );
162 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
165 LDAP_LOG( BACK_LDBM, ERR,
166 "ldbm_back_add: No write access to parent (%s).\n",
167 op->o_req_dn.bv_val, 0, 0 );
169 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
173 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
174 "no write access to parent" );
177 return LDAP_INSUFFICIENT_ACCESS;
183 #ifdef LDBM_SUBENTRIES
184 if ( is_entry_subentry( p )) {
186 LDAP_LOG( OPERATION, DETAIL1,
187 "bdb_add: parent is subentry\n", 0, 0, 0 );
189 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
192 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
193 rs->sr_text = "parent is a subentry";
198 if ( is_entry_alias( p ) ) {
199 /* parent is an alias, don't allow add */
201 /* free parent and writer lock */
202 cache_return_entry_w( &li->li_cache, p );
203 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
206 LDAP_LOG(BACK_LDBM, ERR,
207 "ldbm_back_add: Parent is an alias.\n", 0, 0, 0 );
209 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
214 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
215 "parent is an alias" );
218 return LDAP_ALIAS_PROBLEM;
224 if ( is_entry_referral( p ) ) {
225 /* parent is a referral, don't allow add */
226 rs->sr_matched = ch_strdup( p->e_dn );
227 rs->sr_ref = is_entry_referral( p )
228 ? get_entry_referrals( op, p )
231 /* free parent and writer lock */
232 cache_return_entry_w( &li->li_cache, p );
233 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
236 LDAP_LOG( BACK_LDBM, ERR,
237 "ldbm_back_add: Parent is referral.\n", 0, 0, 0 );
239 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
242 rs->sr_err = LDAP_REFERRAL;
243 send_ldap_result( op, rs );
245 ber_bvarray_free( rs->sr_ref );
246 free( (char *)rs->sr_matched );
254 #ifdef LDBM_SUBENTRIES
257 /* parent must be an administrative point of the required kind */
262 if( pdn.bv_val != NULL )
264 assert( *pdn.bv_val == '\0' );
267 /* no parent, must be adding entry to root */
268 if ( !be_isroot( op->o_bd, &op->o_ndn ) )
270 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
271 p = (Entry *)&slap_entry_root;
273 rs->sr_err = access_allowed( op, p,
274 children, NULL, ACL_WRITE, NULL );
277 if ( ! rs->sr_err ) {
278 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
281 LDAP_LOG( BACK_LDBM, ERR,
282 "ldbm_back_add: No write "
283 "access to parent (\"\").\n", 0, 0, 0 );
285 Debug( LDAP_DEBUG_TRACE,
286 "no write access to parent\n",
290 send_ldap_error( op, rs,
291 LDAP_INSUFFICIENT_ACCESS,
292 "no write access to parent" );
295 return LDAP_INSUFFICIENT_ACCESS;
301 } else if ( !is_entry_glue( op->oq_add.rs_e )) {
305 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
308 LDAP_LOG( BACK_LDBM, ERR,
309 "ldbm_back_add: %s add denied.\n",
310 pdn.bv_val == NULL ? "suffix"
311 : "entry at root", 0, 0 );
313 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
314 pdn.bv_val == NULL ? "suffix"
315 : "entry at root", 0, 0 );
318 send_ldap_error( op, rs,
319 LDAP_NO_SUCH_OBJECT, NULL );
322 return LDAP_NO_SUCH_OBJECT;
329 #ifdef LDBM_SUBENTRIES
332 LDAP_LOG ( OPERATION, DETAIL1,
333 "bdb_add: no parent, cannot add subentry\n", 0, 0, 0 );
335 Debug( LDAP_DEBUG_TRACE,
336 "bdb_add: no parent, cannot add subentry\n", 0, 0, 0 );
338 rs->sr_err = LDAP_NO_SUCH_OBJECT;
339 rs->sr_text = "no parent, cannot add subentry";
346 if ( next_id( op->o_bd, &op->oq_add.rs_e->e_id ) ) {
348 /* free parent and writer lock */
349 cache_return_entry_w( &li->li_cache, p );
352 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
355 LDAP_LOG( BACK_LDBM, ERR,
356 "ldbm_back_add: next_id failed.\n", 0, 0, 0 );
358 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
362 send_ldap_error( op, rs, LDAP_OTHER,
363 "next_id add failed" );
373 * Try to add the entry to the cache, assign it a new dnid.
375 rs->sr_err = cache_add_entry_rw(&li->li_cache, op->oq_add.rs_e, CACHE_WRITE_LOCK);
377 if ( rs->sr_err != 0 ) {
379 /* free parent and writer lock */
380 cache_return_entry_w( &li->li_cache, p );
383 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
386 LDAP_LOG( BACK_LDBM, ERR,
387 "ldbm_back_add: cache_add_entry_lock failed.\n", 0, 0, 0 );
389 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
393 rs->sr_text = rs->sr_err > 0 ? NULL : "cache add failed";
394 rs->sr_err = rs->sr_err > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER;
395 send_ldap_result( op, rs );
406 /* attribute indexes */
407 if ( index_entry_add( op, op->oq_add.rs_e ) != LDAP_SUCCESS ) {
409 LDAP_LOG( BACK_LDBM, ERR,
410 "ldbm_back_add: index_entry_add failed.\n", 0, 0, 0 );
412 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
416 send_ldap_error( op, rs, LDAP_OTHER,
417 "index generation failed" );
423 if ( dn2id_add( op->o_bd, &op->oq_add.rs_e->e_nname, op->oq_add.rs_e->e_id ) != 0 ) {
425 LDAP_LOG( BACK_LDBM, ERR,
426 "ldbm_back_add: dn2id_add failed.\n", 0, 0, 0 );
428 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
431 /* FIXME: delete attr indices? */
433 send_ldap_error( op, rs, LDAP_OTHER,
434 "DN index generation failed" );
440 if ( id2entry_add( op->o_bd, op->oq_add.rs_e ) != 0 ) {
442 LDAP_LOG( BACK_LDBM, ERR,
443 "ldbm_back_add: id2entry_add failed.\n", 0, 0, 0 );
445 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
449 /* FIXME: delete attr indices? */
450 (void) dn2id_delete( op->o_bd, &op->oq_add.rs_e->e_nname, op->oq_add.rs_e->e_id );
452 send_ldap_error( op, rs, LDAP_OTHER,
453 "entry store failed" );
458 rs->sr_err = LDAP_SUCCESS;
459 send_ldap_result( op, rs );
461 /* marks the entry as committed, so it is added to the cache;
462 * otherwise it is removed from the cache, but not destroyed;
463 * it will be destroyed by the caller */
464 cache_entry_commit( op->oq_add.rs_e );
468 /* free parent and writer lock */
469 cache_return_entry_w( &li->li_cache, p );
474 * in case of error, writer lock is freed
475 * and entry's private data is destroyed.
476 * otherwise, this is done when entry is released
478 cache_return_entry_w( &li->li_cache, op->oq_add.rs_e );
479 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
482 return( rs->sr_err );