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);
43 rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e, NULL, &rs->sr_text, textbuf, textlen );
44 #else /* LDAP_CACHING */
45 if ( !op->o_caching_on ) {
46 rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e, NULL, &rs->sr_text, textbuf, textlen );
48 rs->sr_err = LDAP_SUCCESS;
50 #endif /* LDAP_CACHING */
52 if ( rs->sr_err != LDAP_SUCCESS ) {
54 LDAP_LOG( BACK_LDBM, ERR,
55 "ldbm_back_add: entry (%s) failed schema check.\n", op->o_req_dn.bv_val, 0, 0 );
57 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
61 send_ldap_result( op, rs );
69 #ifdef LDBM_SUBENTRIES
70 subentry = is_entry_subentry( op->oq_add.rs_e );
74 if ( !op->o_caching_on ) {
75 #endif /* LDAP_CACHING */
76 if ( !access_allowed( op, op->oq_add.rs_e,
77 entry, NULL, ACL_WRITE, NULL ) )
80 LDAP_LOG( BACK_LDBM, ERR,
81 "ldbm_back_add: No write access to entry (%s).\n",
82 op->o_req_dn.bv_val, 0, 0 );
84 Debug( LDAP_DEBUG_TRACE, "no write access to entry\n", 0,
88 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
89 "no write access to entry" );
92 return LDAP_INSUFFICIENT_ACCESS;
99 #endif /* LDAP_CACHING */
101 /* grab giant lock for writing */
102 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
104 if ( ( rs->sr_err = dn2id( op->o_bd, &op->o_req_ndn, &id ) ) || id != NOID ) {
105 /* if (rs->sr_err) something bad happened to ldbm cache */
106 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
107 rs->sr_err = rs->sr_err ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
108 send_ldap_result( op, rs );
117 * Get the parent dn and see if the corresponding entry exists.
118 * If the parent does not exist, only allow the "root" user to
122 if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
125 dnParent( &op->o_req_ndn, &pdn );
130 #else /* LDAP_CACHING */
131 if( pdn.bv_len && !op->o_caching_on )
132 #endif /* LDAP_CACHING */
134 Entry *matched = NULL;
136 /* get parent with writer lock */
137 if ( (p = dn2entry_w( op->o_bd, &pdn, &matched )) == NULL ) {
138 if ( matched != NULL ) {
139 rs->sr_matched = ch_strdup( matched->e_dn );
140 rs->sr_ref = is_entry_referral( matched )
141 ? get_entry_referrals( op, matched )
143 cache_return_entry_r( &li->li_cache, matched );
146 rs->sr_ref = referral_rewrite( default_referral,
147 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
150 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
153 LDAP_LOG( BACK_LDBM, ERR,
154 "ldbm_back_add: Parent of (%s) does not exist.\n",
155 op->o_req_dn.bv_val, 0, 0 );
157 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
161 rs->sr_text = rs->sr_ref ? "parent is referral" : "parent does not exist";
162 rs->sr_err = LDAP_REFERRAL;
163 send_ldap_result( op, rs );
165 ber_bvarray_free( rs->sr_ref );
166 free( (char *)rs->sr_matched );
175 if ( ! access_allowed( op, p,
176 children, NULL, ACL_WRITE, NULL ) )
178 /* free parent and writer lock */
179 cache_return_entry_w( &li->li_cache, p );
180 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
183 LDAP_LOG( BACK_LDBM, ERR,
184 "ldbm_back_add: No write access to parent (%s).\n",
185 op->o_req_dn.bv_val, 0, 0 );
187 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
191 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
192 "no write access to parent" );
195 return LDAP_INSUFFICIENT_ACCESS;
201 #ifdef LDBM_SUBENTRIES
202 if ( is_entry_subentry( p )) {
204 LDAP_LOG( OPERATION, DETAIL1,
205 "bdb_add: parent is subentry\n", 0, 0, 0 );
207 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
210 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
211 rs->sr_text = "parent is a subentry";
216 if ( is_entry_alias( p ) ) {
217 /* parent is an alias, don't allow add */
219 /* free parent and writer lock */
220 cache_return_entry_w( &li->li_cache, p );
221 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
224 LDAP_LOG(BACK_LDBM, ERR,
225 "ldbm_back_add: Parent is an alias.\n", 0, 0, 0 );
227 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
232 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
233 "parent is an alias" );
236 return LDAP_ALIAS_PROBLEM;
242 if ( is_entry_referral( p ) ) {
243 /* parent is a referral, don't allow add */
244 rs->sr_matched = ch_strdup( p->e_dn );
245 rs->sr_ref = is_entry_referral( p )
246 ? get_entry_referrals( op, p )
249 /* free parent and writer lock */
250 cache_return_entry_w( &li->li_cache, p );
251 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
254 LDAP_LOG( BACK_LDBM, ERR,
255 "ldbm_back_add: Parent is referral.\n", 0, 0, 0 );
257 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
260 rs->sr_err = LDAP_REFERRAL;
261 send_ldap_result( op, rs );
263 ber_bvarray_free( rs->sr_ref );
264 free( (char *)rs->sr_matched );
272 #ifdef LDBM_SUBENTRIES
275 /* parent must be an administrative point of the required kind */
281 if( pdn.bv_val != NULL )
282 #else /* LDAP_CACHING */
283 if( pdn.bv_val != NULL && !op->o_caching_on )
284 #endif /* LDAP_CACHING */
286 assert( *pdn.bv_val == '\0' );
289 /* no parent, must be adding entry to root */
291 if ( !be_isroot( op->o_bd, &op->o_ndn ) )
292 #else /* LDAP_CACHING */
293 if ( !be_isroot( op->o_bd, &op->o_ndn ) && !op->o_caching_on )
294 #endif /* LDAP_CACHING */
296 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
297 p = (Entry *)&slap_entry_root;
299 rs->sr_err = access_allowed( op, p,
300 children, NULL, ACL_WRITE, NULL );
303 if ( ! rs->sr_err ) {
304 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
307 LDAP_LOG( BACK_LDBM, ERR,
308 "ldbm_back_add: No write "
309 "access to parent (\"\").\n", 0, 0, 0 );
311 Debug( LDAP_DEBUG_TRACE,
312 "no write access to parent\n",
316 send_ldap_error( op, rs,
317 LDAP_INSUFFICIENT_ACCESS,
318 "no write access to parent" );
321 return LDAP_INSUFFICIENT_ACCESS;
327 } else if ( !is_entry_glue( op->oq_add.rs_e )) {
331 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
334 LDAP_LOG( BACK_LDBM, ERR,
335 "ldbm_back_add: %s add denied.\n",
336 pdn.bv_val == NULL ? "suffix"
337 : "entry at root", 0, 0 );
339 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
340 pdn.bv_val == NULL ? "suffix"
341 : "entry at root", 0, 0 );
344 send_ldap_error( op, rs,
345 LDAP_NO_SUCH_OBJECT, NULL );
348 return LDAP_NO_SUCH_OBJECT;
355 #ifdef LDBM_SUBENTRIES
358 LDAP_LOG ( OPERATION, DETAIL1,
359 "bdb_add: no parent, cannot add subentry\n", 0, 0, 0 );
361 Debug( LDAP_DEBUG_TRACE,
362 "bdb_add: no parent, cannot add subentry\n", 0, 0, 0 );
364 rs->sr_err = LDAP_NO_SUCH_OBJECT;
365 rs->sr_text = "no parent, cannot add subentry";
372 if ( next_id( op->o_bd, &op->oq_add.rs_e->e_id ) ) {
374 /* free parent and writer lock */
375 cache_return_entry_w( &li->li_cache, p );
378 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
381 LDAP_LOG( BACK_LDBM, ERR,
382 "ldbm_back_add: next_id failed.\n", 0, 0, 0 );
384 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
388 send_ldap_error( op, rs, LDAP_OTHER,
389 "next_id add failed" );
399 * Try to add the entry to the cache, assign it a new dnid.
401 rs->sr_err = cache_add_entry_rw(&li->li_cache, op->oq_add.rs_e, CACHE_WRITE_LOCK);
403 if ( rs->sr_err != 0 ) {
405 /* free parent and writer lock */
406 cache_return_entry_w( &li->li_cache, p );
409 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
412 LDAP_LOG( BACK_LDBM, ERR,
413 "ldbm_back_add: cache_add_entry_lock failed.\n", 0, 0, 0 );
415 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
419 rs->sr_text = rs->sr_err > 0 ? NULL : "cache add failed";
420 rs->sr_err = rs->sr_err > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER;
421 send_ldap_result( op, rs );
432 /* attribute indexes */
433 if ( index_entry_add( op, op->oq_add.rs_e ) != LDAP_SUCCESS ) {
435 LDAP_LOG( BACK_LDBM, ERR,
436 "ldbm_back_add: index_entry_add failed.\n", 0, 0, 0 );
438 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
442 send_ldap_error( op, rs, LDAP_OTHER,
443 "index generation failed" );
449 if ( dn2id_add( op->o_bd, &op->oq_add.rs_e->e_nname, op->oq_add.rs_e->e_id ) != 0 ) {
451 LDAP_LOG( BACK_LDBM, ERR,
452 "ldbm_back_add: dn2id_add failed.\n", 0, 0, 0 );
454 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
457 /* FIXME: delete attr indices? */
459 send_ldap_error( op, rs, LDAP_OTHER,
460 "DN index generation failed" );
466 if ( id2entry_add( op->o_bd, op->oq_add.rs_e ) != 0 ) {
468 LDAP_LOG( BACK_LDBM, ERR,
469 "ldbm_back_add: id2entry_add failed.\n", 0, 0, 0 );
471 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
475 /* FIXME: delete attr indices? */
476 (void) dn2id_delete( op->o_bd, &op->oq_add.rs_e->e_nname, op->oq_add.rs_e->e_id );
478 send_ldap_error( op, rs, LDAP_OTHER,
479 "entry store failed" );
484 rs->sr_err = LDAP_SUCCESS;
485 send_ldap_result( op, rs );
487 /* marks the entry as committed, so it is added to the cache;
488 * otherwise it is removed from the cache, but not destroyed;
489 * it will be destroyed by the caller */
490 cache_entry_commit( op->oq_add.rs_e );
494 /* free parent and writer lock */
495 cache_return_entry_w( &li->li_cache, p );
500 * in case of error, writer lock is freed
501 * and entry's private data is destroyed.
502 * otherwise, this is done when entry is released
504 cache_return_entry_w( &li->li_cache, op->oq_add.rs_e );
505 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
508 return( rs->sr_err );