1 /* add.c - ldap ldbm back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 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 op->o_callback = op->o_callback->sc_next;
34 slap_graduate_commit_csn( op );
35 return SLAP_CB_CONTINUE;
43 struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
47 AttributeDescription *children = slap_schema.si_ad_children;
48 AttributeDescription *entry = slap_schema.si_ad_entry;
49 char textbuf[SLAP_TEXT_BUFLEN];
50 size_t textlen = sizeof textbuf;
51 slap_callback cb = { NULL };
52 #ifdef LDBM_SUBENTRIES
56 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n",
57 op->o_req_dn.bv_val, 0, 0);
59 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
60 if ( rs->sr_err != LDAP_SUCCESS ) {
61 Debug( LDAP_DEBUG_TRACE,
62 "entry failed op attrs add: %s (%d)\n",
63 rs->sr_text, rs->sr_err, 0 );
67 cb.sc_cleanup = ldbm_csn_cb;
68 cb.sc_next = op->o_callback;
71 rs->sr_err = entry_schema_check( op, op->oq_add.rs_e, NULL,
72 get_manageDIT(op), &rs->sr_text, textbuf, textlen );
74 if ( rs->sr_err != LDAP_SUCCESS ) {
75 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
78 send_ldap_result( op, rs );
83 #ifdef LDBM_SUBENTRIES
84 subentry = is_entry_subentry( op->oq_add.rs_e );
87 if ( !access_allowed( op, op->oq_add.rs_e,
88 entry, NULL, ACL_WADD, NULL ) )
90 Debug( LDAP_DEBUG_TRACE, "no write access to entry\n", 0,
93 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
94 "no write access to entry" );
96 return LDAP_INSUFFICIENT_ACCESS;
99 /* grab giant lock for writing */
100 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
102 rs->sr_err = dn2id( op->o_bd, &op->o_req_ndn, &id );
103 if ( rs->sr_err || id != NOID ) {
104 /* if (rs->sr_err) something bad happened to ldbm cache */
105 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
106 rs->sr_err = rs->sr_err ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
107 send_ldap_result( op, rs );
112 * Get the parent dn and see if the corresponding entry exists.
113 * If the parent does not exist, only allow the "root" user to
117 if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
120 dnParent( &op->o_req_ndn, &pdn );
124 Entry *matched = NULL;
126 /* get parent with writer lock */
127 if ( (p = dn2entry_w( op->o_bd, &pdn, &matched )) == NULL ) {
128 if ( matched != NULL ) {
129 rs->sr_matched = ch_strdup( matched->e_dn );
130 rs->sr_ref = is_entry_referral( matched )
131 ? get_entry_referrals( op, matched )
133 cache_return_entry_r( &li->li_cache, matched );
136 rs->sr_ref = referral_rewrite( default_referral,
137 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
140 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
142 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
145 rs->sr_text = rs->sr_ref
146 ? "parent is referral" : "parent does not exist";
147 rs->sr_err = LDAP_REFERRAL;
148 send_ldap_result( op, rs );
150 ber_bvarray_free( rs->sr_ref );
151 free( (char *)rs->sr_matched );
153 rs->sr_matched = NULL;
157 if ( ! access_allowed( op, p, children, NULL, ACL_WADD, NULL ) ) {
158 /* free parent and writer lock */
159 cache_return_entry_w( &li->li_cache, p );
160 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
162 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
165 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
166 "no write access to parent" );
168 return LDAP_INSUFFICIENT_ACCESS;
171 #ifdef LDBM_SUBENTRIES
172 if ( is_entry_subentry( p )) {
173 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
175 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
176 rs->sr_text = "parent is a subentry";
181 if ( is_entry_alias( p ) ) {
182 /* parent is an alias, don't allow add */
184 /* free parent and writer lock */
185 cache_return_entry_w( &li->li_cache, p );
186 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
188 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
191 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
192 "parent is an alias" );
194 return LDAP_ALIAS_PROBLEM;
197 if ( is_entry_referral( p ) ) {
198 /* parent is a referral, don't allow add */
199 rs->sr_matched = ch_strdup( p->e_dn );
200 rs->sr_ref = is_entry_referral( p )
201 ? get_entry_referrals( op, p )
204 /* free parent and writer lock */
205 cache_return_entry_w( &li->li_cache, p );
206 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
208 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
210 rs->sr_err = LDAP_REFERRAL;
211 send_ldap_result( op, rs );
213 ber_bvarray_free( rs->sr_ref );
214 free( (char *)rs->sr_matched );
216 rs->sr_matched = NULL;
220 #ifdef LDBM_SUBENTRIES
223 /* parent must be an administrative point of the required kind */
228 assert( pdn.bv_val == NULL || *pdn.bv_val == '\0' );
230 if (( !be_isroot(op) && !be_shadow_update(op) )
231 && !is_entry_glue( op->oq_add.rs_e ))
233 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
235 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
236 pdn.bv_val == NULL ? "suffix" : "entry at root", 0, 0 );
238 send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT, NULL );
239 return LDAP_NO_SUCH_OBJECT;
243 if ( next_id( op->o_bd, &op->oq_add.rs_e->e_id ) ) {
245 /* free parent and writer lock */
246 cache_return_entry_w( &li->li_cache, p );
249 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
251 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
254 send_ldap_error( op, rs, LDAP_OTHER,
255 "next_id add failed" );
261 * Try to add the entry to the cache, assign it a new dnid.
263 rs->sr_err = cache_add_entry_rw( &li->li_cache, op->oq_add.rs_e,
266 if ( rs->sr_err != 0 ) {
268 /* free parent and writer lock */
269 cache_return_entry_w( &li->li_cache, p );
272 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
274 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
277 rs->sr_text = rs->sr_err > 0 ? NULL : "cache add failed";
278 rs->sr_err = rs->sr_err > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER;
279 send_ldap_result( op, rs );
286 /* attribute indexes */
287 if ( index_entry_add( op, op->oq_add.rs_e ) != LDAP_SUCCESS ) {
288 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
291 send_ldap_error( op, rs, LDAP_OTHER,
292 "index generation failed" );
298 if ( dn2id_add( op->o_bd, &op->oq_add.rs_e->e_nname,
299 op->oq_add.rs_e->e_id ) != 0 )
301 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
303 /* FIXME: delete attr indices? */
305 send_ldap_error( op, rs, LDAP_OTHER,
306 "DN index generation failed" );
312 if ( id2entry_add( op->o_bd, op->oq_add.rs_e ) != 0 ) {
313 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
316 /* FIXME: delete attr indices? */
317 (void) dn2id_delete( op->o_bd, &op->oq_add.rs_e->e_nname,
318 op->oq_add.rs_e->e_id );
320 send_ldap_error( op, rs, LDAP_OTHER,
321 "entry store failed" );
326 rs->sr_err = LDAP_SUCCESS;
328 send_ldap_result( op, rs );
330 /* marks the entry as committed, so it is added to the cache;
331 * otherwise it is removed from the cache, but not destroyed;
332 * it will be destroyed by the caller */
333 cache_entry_commit( op->oq_add.rs_e );
337 /* free parent and writer lock */
338 cache_return_entry_w( &li->li_cache, p );
343 * in case of error, writer lock is freed
344 * and entry's private data is destroyed.
345 * otherwise, this is done when entry is released
347 cache_return_entry_w( &li->li_cache, op->oq_add.rs_e );
348 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
351 return( rs->sr_err );