1 /* add.c - ldap ldbm back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 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",
47 op->o_req_dn.bv_val, 0, 0 );
49 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n",
50 op->o_req_dn.bv_val, 0, 0);
53 rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e, NULL,
54 &rs->sr_text, textbuf, textlen );
56 if ( rs->sr_err != LDAP_SUCCESS ) {
58 LDAP_LOG( BACK_LDBM, ERR,
59 "ldbm_back_add: entry (%s) failed schema check.\n",
60 op->o_req_dn.bv_val, 0, 0 );
62 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
66 send_ldap_result( op, rs );
70 #ifdef LDBM_SUBENTRIES
71 subentry = is_entry_subentry( op->oq_add.rs_e );
74 if ( !access_allowed( op, op->oq_add.rs_e,
75 entry, NULL, ACL_WRITE, NULL ) )
78 LDAP_LOG( BACK_LDBM, ERR,
79 "ldbm_back_add: No write access to entry (%s).\n",
80 op->o_req_dn.bv_val, 0, 0 );
82 Debug( LDAP_DEBUG_TRACE, "no write access to entry\n", 0,
86 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
87 "no write access to entry" );
89 return LDAP_INSUFFICIENT_ACCESS;
92 /* grab giant lock for writing */
93 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
95 rs->sr_err = dn2id( op->o_bd, &op->o_req_ndn, &id );
96 if ( rs->sr_err || id != NOID ) {
97 /* if (rs->sr_err) something bad happened to ldbm cache */
98 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
99 rs->sr_err = rs->sr_err ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
100 send_ldap_result( op, rs );
105 * Get the parent dn and see if the corresponding entry exists.
106 * If the parent does not exist, only allow the "root" user to
110 if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
113 dnParent( &op->o_req_ndn, &pdn );
117 Entry *matched = NULL;
119 /* get parent with writer lock */
120 if ( (p = dn2entry_w( op->o_bd, &pdn, &matched )) == NULL ) {
121 if ( matched != NULL ) {
122 rs->sr_matched = ch_strdup( matched->e_dn );
123 rs->sr_ref = is_entry_referral( matched )
124 ? get_entry_referrals( op, matched )
126 cache_return_entry_r( &li->li_cache, matched );
129 rs->sr_ref = referral_rewrite( default_referral,
130 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
133 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
136 LDAP_LOG( BACK_LDBM, ERR,
137 "ldbm_back_add: Parent of (%s) does not exist.\n",
138 op->o_req_dn.bv_val, 0, 0 );
140 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
144 rs->sr_text = rs->sr_ref
145 ? "parent is referral" : "parent does not exist";
146 rs->sr_err = LDAP_REFERRAL;
147 send_ldap_result( op, rs );
149 ber_bvarray_free( rs->sr_ref );
150 free( (char *)rs->sr_matched );
152 rs->sr_matched = NULL;
156 if ( ! access_allowed( op, p, children, NULL, ACL_WRITE, NULL ) ) {
157 /* free parent and writer lock */
158 cache_return_entry_w( &li->li_cache, p );
159 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
162 LDAP_LOG( BACK_LDBM, ERR,
163 "ldbm_back_add: No write access to parent (%s).\n",
164 op->o_req_dn.bv_val, 0, 0 );
166 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
170 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
171 "no write access to parent" );
173 return LDAP_INSUFFICIENT_ACCESS;
176 #ifdef LDBM_SUBENTRIES
177 if ( is_entry_subentry( p )) {
179 LDAP_LOG( OPERATION, DETAIL1,
180 "bdb_add: parent is subentry\n", 0, 0, 0 );
182 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
185 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
186 rs->sr_text = "parent is a subentry";
191 if ( is_entry_alias( p ) ) {
192 /* parent is an alias, don't allow add */
194 /* free parent and writer lock */
195 cache_return_entry_w( &li->li_cache, p );
196 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
199 LDAP_LOG(BACK_LDBM, ERR,
200 "ldbm_back_add: Parent is an alias.\n", 0, 0, 0 );
202 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
206 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
207 "parent is an alias" );
209 return LDAP_ALIAS_PROBLEM;
212 if ( is_entry_referral( p ) ) {
213 /* parent is a referral, don't allow add */
214 rs->sr_matched = ch_strdup( p->e_dn );
215 rs->sr_ref = is_entry_referral( p )
216 ? get_entry_referrals( op, p )
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 referral.\n", 0, 0, 0 );
227 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
230 rs->sr_err = LDAP_REFERRAL;
231 send_ldap_result( op, rs );
233 ber_bvarray_free( rs->sr_ref );
234 free( (char *)rs->sr_matched );
236 rs->sr_matched = NULL;
240 #ifdef LDBM_SUBENTRIES
243 /* parent must be an administrative point of the required kind */
248 assert( pdn.bv_val == NULL || *pdn.bv_val == '\0' );
250 if ( !be_isroot( op )
251 && !is_entry_glue( op->oq_add.rs_e ))
253 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
256 LDAP_LOG( BACK_LDBM, ERR,
257 "ldbm_back_add: %s add denied.\n",
258 pdn.bv_val == NULL ? "suffix" : "entry at root", 0, 0 );
260 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
261 pdn.bv_val == NULL ? "suffix" : "entry at root", 0, 0 );
264 send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT, NULL );
265 return LDAP_NO_SUCH_OBJECT;
269 if ( next_id( op->o_bd, &op->oq_add.rs_e->e_id ) ) {
271 /* free parent and writer lock */
272 cache_return_entry_w( &li->li_cache, p );
275 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
278 LDAP_LOG( BACK_LDBM, ERR,
279 "ldbm_back_add: next_id failed.\n", 0, 0, 0 );
281 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
285 send_ldap_error( op, rs, LDAP_OTHER,
286 "next_id add failed" );
292 * Try to add the entry to the cache, assign it a new dnid.
294 rs->sr_err = cache_add_entry_rw( &li->li_cache, op->oq_add.rs_e,
297 if ( rs->sr_err != 0 ) {
299 /* free parent and writer lock */
300 cache_return_entry_w( &li->li_cache, p );
303 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
306 LDAP_LOG( BACK_LDBM, ERR,
307 "ldbm_back_add: cache_add_entry_lock failed.\n", 0, 0, 0 );
309 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
313 rs->sr_text = rs->sr_err > 0 ? NULL : "cache add failed";
314 rs->sr_err = rs->sr_err > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER;
315 send_ldap_result( op, rs );
322 /* attribute indexes */
323 if ( index_entry_add( op, op->oq_add.rs_e ) != LDAP_SUCCESS ) {
325 LDAP_LOG( BACK_LDBM, ERR,
326 "ldbm_back_add: index_entry_add failed.\n", 0, 0, 0 );
328 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
332 send_ldap_error( op, rs, LDAP_OTHER,
333 "index generation failed" );
339 if ( dn2id_add( op->o_bd, &op->oq_add.rs_e->e_nname,
340 op->oq_add.rs_e->e_id ) != 0 )
343 LDAP_LOG( BACK_LDBM, ERR,
344 "ldbm_back_add: dn2id_add failed.\n", 0, 0, 0 );
346 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
349 /* FIXME: delete attr indices? */
351 send_ldap_error( op, rs, LDAP_OTHER,
352 "DN index generation failed" );
358 if ( id2entry_add( op->o_bd, op->oq_add.rs_e ) != 0 ) {
360 LDAP_LOG( BACK_LDBM, ERR,
361 "ldbm_back_add: id2entry_add failed.\n", 0, 0, 0 );
363 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
367 /* FIXME: delete attr indices? */
368 (void) dn2id_delete( op->o_bd, &op->oq_add.rs_e->e_nname,
369 op->oq_add.rs_e->e_id );
371 send_ldap_error( op, rs, LDAP_OTHER,
372 "entry store failed" );
377 rs->sr_err = LDAP_SUCCESS;
379 send_ldap_result( op, rs );
381 /* marks the entry as committed, so it is added to the cache;
382 * otherwise it is removed from the cache, but not destroyed;
383 * it will be destroyed by the caller */
384 cache_entry_commit( op->oq_add.rs_e );
388 /* free parent and writer lock */
389 cache_return_entry_w( &li->li_cache, p );
394 * in case of error, writer lock is freed
395 * and entry's private data is destroyed.
396 * otherwise, this is done when entry is released
398 cache_return_entry_w( &li->li_cache, op->oq_add.rs_e );
399 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
402 return( rs->sr_err );