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 );
57 #ifdef LDBM_SUBENTRIES
58 subentry = is_entry_subentry( op->oq_add.rs_e );
61 if ( !access_allowed( op, op->oq_add.rs_e,
62 entry, NULL, ACL_WRITE, NULL ) )
65 LDAP_LOG( BACK_LDBM, ERR,
66 "ldbm_back_add: No write access to entry (%s).\n",
67 op->o_req_dn.bv_val, 0, 0 );
69 Debug( LDAP_DEBUG_TRACE, "no write access to entry\n", 0,
73 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
74 "no write access to entry" );
76 return LDAP_INSUFFICIENT_ACCESS;
79 /* grab giant lock for writing */
80 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
82 if ( ( rs->sr_err = dn2id( op->o_bd, &op->o_req_ndn, &id ) ) || id != NOID ) {
83 /* if (rs->sr_err) something bad happened to ldbm cache */
84 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
85 rs->sr_err = rs->sr_err ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
86 send_ldap_result( op, rs );
91 * Get the parent dn and see if the corresponding entry exists.
92 * If the parent does not exist, only allow the "root" user to
96 if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
99 dnParent( &op->o_req_ndn, &pdn );
104 Entry *matched = NULL;
106 /* get parent with writer lock */
107 if ( (p = dn2entry_w( op->o_bd, &pdn, &matched )) == NULL ) {
108 if ( matched != NULL ) {
109 rs->sr_matched = ch_strdup( matched->e_dn );
110 rs->sr_ref = is_entry_referral( matched )
111 ? get_entry_referrals( op, matched )
113 cache_return_entry_r( &li->li_cache, matched );
116 rs->sr_ref = referral_rewrite( default_referral,
117 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
120 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
123 LDAP_LOG( BACK_LDBM, ERR,
124 "ldbm_back_add: Parent of (%s) does not exist.\n",
125 op->o_req_dn.bv_val, 0, 0 );
127 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
131 rs->sr_text = rs->sr_ref ? "parent is referral" : "parent does not exist";
132 rs->sr_err = LDAP_REFERRAL;
133 send_ldap_result( op, rs );
135 ber_bvarray_free( rs->sr_ref );
136 free( (char *)rs->sr_matched );
138 rs->sr_matched = NULL;
142 if ( ! access_allowed( 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",
152 op->o_req_dn.bv_val, 0, 0 );
154 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
158 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
159 "no write access to parent" );
161 return LDAP_INSUFFICIENT_ACCESS;
164 #ifdef LDBM_SUBENTRIES
165 if ( is_entry_subentry( p )) {
167 LDAP_LOG( OPERATION, DETAIL1,
168 "bdb_add: parent is subentry\n", 0, 0, 0 );
170 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
173 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
174 rs->sr_text = "parent is a subentry";
179 if ( is_entry_alias( p ) ) {
180 /* parent is an alias, don't allow add */
182 /* free parent and writer lock */
183 cache_return_entry_w( &li->li_cache, p );
184 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
187 LDAP_LOG(BACK_LDBM, ERR,
188 "ldbm_back_add: Parent is an alias.\n", 0, 0, 0 );
190 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
195 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
196 "parent is an alias" );
198 return LDAP_ALIAS_PROBLEM;
201 if ( is_entry_referral( p ) ) {
202 /* parent is a referral, don't allow add */
203 rs->sr_matched = ch_strdup( p->e_dn );
204 rs->sr_ref = is_entry_referral( p )
205 ? get_entry_referrals( op, p )
208 /* free parent and writer lock */
209 cache_return_entry_w( &li->li_cache, p );
210 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
213 LDAP_LOG( BACK_LDBM, ERR,
214 "ldbm_back_add: Parent is referral.\n", 0, 0, 0 );
216 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
219 rs->sr_err = LDAP_REFERRAL;
220 send_ldap_result( op, rs );
222 ber_bvarray_free( rs->sr_ref );
223 free( (char *)rs->sr_matched );
225 rs->sr_matched = NULL;
229 #ifdef LDBM_SUBENTRIES
232 /* parent must be an administrative point of the required kind */
237 if( pdn.bv_val != NULL ) {
238 assert( *pdn.bv_val == '\0' );
242 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) ||
243 be_isupdate( op->o_bd, &op->o_ndn ) )
245 p = (Entry *)&slap_entry_root;
247 rs->sr_err = access_allowed( op, p,
248 children, NULL, ACL_WRITE, NULL );
251 if ( ! rs->sr_err ) {
252 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
255 LDAP_LOG( BACK_LDBM, ERR,
256 "ldbm_back_add: No write "
257 "access to parent (\"\").\n", 0, 0, 0 );
259 Debug( LDAP_DEBUG_TRACE,
260 "no write access to parent\n", 0, 0, 0 );
263 send_ldap_error( op, rs,
264 LDAP_INSUFFICIENT_ACCESS,
265 "no write access to parent" );
267 return LDAP_INSUFFICIENT_ACCESS;
269 } else if ( !is_entry_glue( op->oq_add.rs_e )) {
270 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
273 LDAP_LOG( BACK_LDBM, ERR,
274 "ldbm_back_add: %s add denied.\n",
275 pdn.bv_val == NULL ? "suffix" : "entry at root", 0, 0 );
277 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
278 pdn.bv_val == NULL ? "suffix" : "entry at root", 0, 0 );
281 send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT, NULL );
282 return LDAP_NO_SUCH_OBJECT;
286 if ( next_id( op->o_bd, &op->oq_add.rs_e->e_id ) ) {
288 /* free parent and writer lock */
289 cache_return_entry_w( &li->li_cache, p );
292 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
295 LDAP_LOG( BACK_LDBM, ERR,
296 "ldbm_back_add: next_id failed.\n", 0, 0, 0 );
298 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
302 send_ldap_error( op, rs, LDAP_OTHER,
303 "next_id add failed" );
309 * Try to add the entry to the cache, assign it a new dnid.
311 rs->sr_err = cache_add_entry_rw(&li->li_cache, op->oq_add.rs_e, CACHE_WRITE_LOCK);
313 if ( rs->sr_err != 0 ) {
315 /* free parent and writer lock */
316 cache_return_entry_w( &li->li_cache, p );
319 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
322 LDAP_LOG( BACK_LDBM, ERR,
323 "ldbm_back_add: cache_add_entry_lock failed.\n", 0, 0, 0 );
325 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
329 rs->sr_text = rs->sr_err > 0 ? NULL : "cache add failed";
330 rs->sr_err = rs->sr_err > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER;
331 send_ldap_result( op, rs );
338 /* attribute indexes */
339 if ( index_entry_add( op, op->oq_add.rs_e ) != LDAP_SUCCESS ) {
341 LDAP_LOG( BACK_LDBM, ERR,
342 "ldbm_back_add: index_entry_add failed.\n", 0, 0, 0 );
344 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
348 send_ldap_error( op, rs, LDAP_OTHER,
349 "index generation failed" );
355 if ( dn2id_add( op->o_bd, &op->oq_add.rs_e->e_nname, op->oq_add.rs_e->e_id ) != 0 ) {
357 LDAP_LOG( BACK_LDBM, ERR,
358 "ldbm_back_add: dn2id_add failed.\n", 0, 0, 0 );
360 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
363 /* FIXME: delete attr indices? */
365 send_ldap_error( op, rs, LDAP_OTHER,
366 "DN index generation failed" );
372 if ( id2entry_add( op->o_bd, op->oq_add.rs_e ) != 0 ) {
374 LDAP_LOG( BACK_LDBM, ERR,
375 "ldbm_back_add: id2entry_add failed.\n", 0, 0, 0 );
377 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
381 /* FIXME: delete attr indices? */
382 (void) dn2id_delete( op->o_bd, &op->oq_add.rs_e->e_nname, op->oq_add.rs_e->e_id );
384 send_ldap_error( op, rs, LDAP_OTHER,
385 "entry store failed" );
390 rs->sr_err = LDAP_SUCCESS;
392 send_ldap_result( op, rs );
394 /* marks the entry as committed, so it is added to the cache;
395 * otherwise it is removed from the cache, but not destroyed;
396 * it will be destroyed by the caller */
397 cache_entry_commit( op->oq_add.rs_e );
401 /* free parent and writer lock */
402 cache_return_entry_w( &li->li_cache, p );
407 * in case of error, writer lock is freed
408 * and entry's private data is destroyed.
409 * otherwise, this is done when entry is released
411 cache_return_entry_w( &li->li_cache, op->oq_add.rs_e );
412 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
415 return( rs->sr_err );