1 /* add.c - ldap ldbm back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
21 #include <ac/socket.h>
22 #include <ac/string.h>
25 #include "back-ldbm.h"
26 #include "proto-back-ldbm.h"
33 struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
37 AttributeDescription *children = slap_schema.si_ad_children;
38 AttributeDescription *entry = slap_schema.si_ad_entry;
39 char textbuf[SLAP_TEXT_BUFLEN];
40 size_t textlen = sizeof textbuf;
41 #ifdef LDBM_SUBENTRIES
46 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_add: %s\n", op->o_req_dn.bv_val, 0, 0 );
48 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", op->o_req_dn.bv_val, 0, 0);
51 rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e, NULL, &rs->sr_text, textbuf, textlen );
53 if ( rs->sr_err != LDAP_SUCCESS ) {
55 LDAP_LOG( BACK_LDBM, ERR,
56 "ldbm_back_add: entry (%s) failed schema check.\n", op->o_req_dn.bv_val, 0, 0 );
58 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
62 send_ldap_result( op, rs );
66 #ifdef LDBM_SUBENTRIES
67 subentry = is_entry_subentry( op->oq_add.rs_e );
70 if ( !access_allowed( op, op->oq_add.rs_e,
71 entry, NULL, ACL_WRITE, NULL ) )
74 LDAP_LOG( BACK_LDBM, ERR,
75 "ldbm_back_add: No write access to entry (%s).\n",
76 op->o_req_dn.bv_val, 0, 0 );
78 Debug( LDAP_DEBUG_TRACE, "no write access to entry\n", 0,
82 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
83 "no write access to entry" );
85 return LDAP_INSUFFICIENT_ACCESS;
88 /* grab giant lock for writing */
89 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
91 if ( ( rs->sr_err = dn2id( op->o_bd, &op->o_req_ndn, &id ) ) || id != NOID ) {
92 /* if (rs->sr_err) something bad happened to ldbm cache */
93 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
94 rs->sr_err = rs->sr_err ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
95 send_ldap_result( op, rs );
100 * Get the parent dn and see if the corresponding entry exists.
101 * If the parent does not exist, only allow the "root" user to
105 if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
108 dnParent( &op->o_req_ndn, &pdn );
112 Entry *matched = NULL;
114 /* get parent with writer lock */
115 if ( (p = dn2entry_w( op->o_bd, &pdn, &matched )) == NULL ) {
116 if ( matched != NULL ) {
117 rs->sr_matched = ch_strdup( matched->e_dn );
118 rs->sr_ref = is_entry_referral( matched )
119 ? get_entry_referrals( op, matched )
121 cache_return_entry_r( &li->li_cache, matched );
124 rs->sr_ref = referral_rewrite( default_referral,
125 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
128 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
131 LDAP_LOG( BACK_LDBM, ERR,
132 "ldbm_back_add: Parent of (%s) does not exist.\n",
133 op->o_req_dn.bv_val, 0, 0 );
135 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
139 rs->sr_text = rs->sr_ref ? "parent is referral" : "parent does not exist";
140 rs->sr_err = LDAP_REFERRAL;
141 send_ldap_result( op, rs );
143 ber_bvarray_free( rs->sr_ref );
144 free( (char *)rs->sr_matched );
146 rs->sr_matched = NULL;
150 if ( ! access_allowed( op, p,
151 children, NULL, ACL_WRITE, NULL ) )
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( BACK_LDBM, ERR,
159 "ldbm_back_add: No write access to parent (%s).\n",
160 op->o_req_dn.bv_val, 0, 0 );
162 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
166 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
167 "no write access to parent" );
169 return LDAP_INSUFFICIENT_ACCESS;
172 #ifdef LDBM_SUBENTRIES
173 if ( is_entry_subentry( p )) {
175 LDAP_LOG( OPERATION, DETAIL1,
176 "bdb_add: parent is subentry\n", 0, 0, 0 );
178 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
181 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
182 rs->sr_text = "parent is a subentry";
187 if ( is_entry_alias( p ) ) {
188 /* parent is an alias, don't allow add */
190 /* free parent and writer lock */
191 cache_return_entry_w( &li->li_cache, p );
192 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
195 LDAP_LOG(BACK_LDBM, ERR,
196 "ldbm_back_add: Parent is an alias.\n", 0, 0, 0 );
198 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
202 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
203 "parent is an alias" );
205 return LDAP_ALIAS_PROBLEM;
208 if ( is_entry_referral( p ) ) {
209 /* parent is a referral, don't allow add */
210 rs->sr_matched = ch_strdup( p->e_dn );
211 rs->sr_ref = is_entry_referral( p )
212 ? get_entry_referrals( op, p )
215 /* free parent and writer lock */
216 cache_return_entry_w( &li->li_cache, p );
217 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
220 LDAP_LOG( BACK_LDBM, ERR,
221 "ldbm_back_add: Parent is referral.\n", 0, 0, 0 );
223 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
226 rs->sr_err = LDAP_REFERRAL;
227 send_ldap_result( op, rs );
229 ber_bvarray_free( rs->sr_ref );
230 free( (char *)rs->sr_matched );
232 rs->sr_matched = NULL;
236 #ifdef LDBM_SUBENTRIES
239 /* parent must be an administrative point of the required kind */
244 assert( pdn.bv_val == NULL || *pdn.bv_val == '\0' );
246 if ( !be_isroot( op->o_bd, &op->o_ndn )
247 && !is_entry_glue( op->oq_add.rs_e ))
249 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
252 LDAP_LOG( BACK_LDBM, ERR,
253 "ldbm_back_add: %s add denied.\n",
254 pdn.bv_val == NULL ? "suffix" : "entry at root", 0, 0 );
256 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
257 pdn.bv_val == NULL ? "suffix" : "entry at root", 0, 0 );
260 send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT, NULL );
261 return LDAP_NO_SUCH_OBJECT;
265 if ( next_id( op->o_bd, &op->oq_add.rs_e->e_id ) ) {
267 /* free parent and writer lock */
268 cache_return_entry_w( &li->li_cache, p );
271 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
274 LDAP_LOG( BACK_LDBM, ERR,
275 "ldbm_back_add: next_id failed.\n", 0, 0, 0 );
277 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
281 send_ldap_error( op, rs, LDAP_OTHER,
282 "next_id add failed" );
288 * Try to add the entry to the cache, assign it a new dnid.
290 rs->sr_err = cache_add_entry_rw(&li->li_cache, op->oq_add.rs_e, CACHE_WRITE_LOCK);
292 if ( rs->sr_err != 0 ) {
294 /* free parent and writer lock */
295 cache_return_entry_w( &li->li_cache, p );
298 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
301 LDAP_LOG( BACK_LDBM, ERR,
302 "ldbm_back_add: cache_add_entry_lock failed.\n", 0, 0, 0 );
304 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
308 rs->sr_text = rs->sr_err > 0 ? NULL : "cache add failed";
309 rs->sr_err = rs->sr_err > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER;
310 send_ldap_result( op, rs );
317 /* attribute indexes */
318 if ( index_entry_add( op, op->oq_add.rs_e ) != LDAP_SUCCESS ) {
320 LDAP_LOG( BACK_LDBM, ERR,
321 "ldbm_back_add: index_entry_add failed.\n", 0, 0, 0 );
323 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
327 send_ldap_error( op, rs, LDAP_OTHER,
328 "index generation failed" );
334 if ( dn2id_add( op->o_bd, &op->oq_add.rs_e->e_nname, op->oq_add.rs_e->e_id ) != 0 ) {
336 LDAP_LOG( BACK_LDBM, ERR,
337 "ldbm_back_add: dn2id_add failed.\n", 0, 0, 0 );
339 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
342 /* FIXME: delete attr indices? */
344 send_ldap_error( op, rs, LDAP_OTHER,
345 "DN index generation failed" );
351 if ( id2entry_add( op->o_bd, op->oq_add.rs_e ) != 0 ) {
353 LDAP_LOG( BACK_LDBM, ERR,
354 "ldbm_back_add: id2entry_add failed.\n", 0, 0, 0 );
356 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
360 /* FIXME: delete attr indices? */
361 (void) dn2id_delete( op->o_bd, &op->oq_add.rs_e->e_nname, op->oq_add.rs_e->e_id );
363 send_ldap_error( op, rs, LDAP_OTHER,
364 "entry store failed" );
369 rs->sr_err = LDAP_SUCCESS;
371 send_ldap_result( op, rs );
373 /* marks the entry as committed, so it is added to the cache;
374 * otherwise it is removed from the cache, but not destroyed;
375 * it will be destroyed by the caller */
376 cache_entry_commit( op->oq_add.rs_e );
380 /* free parent and writer lock */
381 cache_return_entry_w( &li->li_cache, p );
386 * in case of error, writer lock is freed
387 * and entry's private data is destroyed.
388 * otherwise, this is done when entry is released
390 cache_return_entry_w( &li->li_cache, op->oq_add.rs_e );
391 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
394 return( rs->sr_err );