1 /* add.c - ldap BerkeleyDB back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 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>.
20 #include <ac/string.h>
25 bdb_add(Operation *op, SlapReply *rs )
27 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
31 char textbuf[SLAP_TEXT_BUFLEN];
32 size_t textlen = sizeof textbuf;
33 AttributeDescription *children = slap_schema.si_ad_children;
34 AttributeDescription *entry = slap_schema.si_ad_entry;
35 DB_TXN *ltid = NULL, *lt2;
36 struct bdb_op_info opinfo = {0};
45 LDAPControl **postread_ctrl = NULL;
46 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
49 Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_add) ": %s\n",
50 op->oq_add.rs_e->e_name.bv_val, 0, 0);
54 /* add opattrs to shadow as well, only missing attrs will actually
55 * be added; helps compatibility with older OL versions */
56 slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
58 /* check entry's schema */
59 rs->sr_err = entry_schema_check( op, op->oq_add.rs_e, NULL,
60 get_manageDIT(op), &rs->sr_text, textbuf, textlen );
61 if ( rs->sr_err != LDAP_SUCCESS ) {
62 Debug( LDAP_DEBUG_TRACE,
63 LDAP_XSTRING(bdb_add) ": entry failed schema check: "
64 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
69 subentry = is_entry_subentry( op->oq_add.rs_e );
73 * acquire an ID outside of the operation transaction
74 * to avoid serializing adds.
76 rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
77 if( rs->sr_err != 0 ) {
78 Debug( LDAP_DEBUG_TRACE,
79 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
81 rs->sr_err = LDAP_OTHER;
82 rs->sr_text = "internal error";
87 retry: /* transaction retry */
89 /* free parent and reader lock */
90 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
93 rs->sr_err = TXN_ABORT( ltid );
96 op->o_do_not_cache = opinfo.boi_acl_cache;
97 if( rs->sr_err != 0 ) {
98 rs->sr_err = LDAP_OTHER;
99 rs->sr_text = "internal error";
102 if ( op->o_abandon ) {
103 rs->sr_err = SLAPD_ABANDON;
106 ldap_pvt_thread_yield();
107 bdb_trans_backoff( ++num_retries );
110 /* begin transaction */
111 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
112 bdb->bi_db_opflags );
114 if( rs->sr_err != 0 ) {
115 Debug( LDAP_DEBUG_TRACE,
116 LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
117 db_strerror(rs->sr_err), rs->sr_err, 0 );
118 rs->sr_err = LDAP_OTHER;
119 rs->sr_text = "internal error";
123 locker = TXN_ID ( ltid );
125 opinfo.boi_bdb = op->o_bd;
126 opinfo.boi_txn = ltid;
127 opinfo.boi_locker = locker;
129 opinfo.boi_acl_cache = op->o_do_not_cache;
130 op->o_private = &opinfo;
133 * Get the parent dn and see if the corresponding entry exists.
135 if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
138 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
141 /* get entry or parent */
142 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
144 switch( rs->sr_err ) {
146 rs->sr_err = LDAP_ALREADY_EXISTS;
150 case DB_LOCK_DEADLOCK:
151 case DB_LOCK_NOTGRANTED:
154 rs->sr_text = "ldap server busy";
157 rs->sr_err = LDAP_OTHER;
158 rs->sr_text = "internal error";
164 if ( !bvmatch( &pdn, &p->e_nname ) ) {
165 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
167 rs->sr_ref = is_entry_referral( p )
168 ? get_entry_referrals( op, p )
170 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
172 Debug( LDAP_DEBUG_TRACE,
173 LDAP_XSTRING(bdb_add) ": parent "
174 "does not exist\n", 0, 0, 0 );
176 rs->sr_err = LDAP_REFERRAL;
177 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
181 rs->sr_err = access_allowed( op, p,
182 children, NULL, ACL_WADD, NULL );
184 if ( ! rs->sr_err ) {
185 switch( opinfo.boi_err ) {
186 case DB_LOCK_DEADLOCK:
187 case DB_LOCK_NOTGRANTED:
191 Debug( LDAP_DEBUG_TRACE,
192 LDAP_XSTRING(bdb_add) ": no write access to parent\n",
194 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
195 rs->sr_text = "no write access to parent";
196 goto return_results;;
199 #ifdef BDB_SUBENTRIES
200 if ( is_entry_subentry( p ) ) {
201 /* parent is a subentry, don't allow add */
202 Debug( LDAP_DEBUG_TRACE,
203 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
205 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
206 rs->sr_text = "parent is a subentry";
207 goto return_results;;
210 if ( is_entry_alias( p ) ) {
211 /* parent is an alias, don't allow add */
212 Debug( LDAP_DEBUG_TRACE,
213 LDAP_XSTRING(bdb_add) ": parent is alias\n",
215 rs->sr_err = LDAP_ALIAS_PROBLEM;
216 rs->sr_text = "parent is an alias";
217 goto return_results;;
220 if ( is_entry_referral( p ) ) {
221 /* parent is a referral, don't allow add */
222 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
224 rs->sr_ref = get_entry_referrals( op, p );
225 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
227 Debug( LDAP_DEBUG_TRACE,
228 LDAP_XSTRING(bdb_add) ": parent is referral\n",
231 rs->sr_err = LDAP_REFERRAL;
232 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
236 #ifdef BDB_SUBENTRIES
239 /* parent must be an administrative point of the required kind */
243 /* free parent and reader lock */
244 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
250 * if not attempting to add entry at suffix or with parent ""
252 if ((( !be_isroot( op ) && !be_shadow_update(op) )
253 || pdn.bv_len > 0 ) && !is_entry_glue( op->oq_add.rs_e ))
255 Debug( LDAP_DEBUG_TRACE,
256 LDAP_XSTRING(bdb_add) ": %s denied\n",
257 pdn.bv_len == 0 ? "suffix" : "entry at root",
259 rs->sr_err = LDAP_NO_SUCH_OBJECT;
264 if ( get_assert( op ) &&
265 ( test_filter( op, op->oq_add.rs_e, get_assertion( op ))
266 != LDAP_COMPARE_TRUE ))
268 rs->sr_err = LDAP_ASSERTION_FAILED;
272 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
273 entry, NULL, ACL_WADD, NULL );
275 if ( ! rs->sr_err ) {
276 switch( opinfo.boi_err ) {
277 case DB_LOCK_DEADLOCK:
278 case DB_LOCK_NOTGRANTED:
282 Debug( LDAP_DEBUG_TRACE,
283 LDAP_XSTRING(bdb_add) ": no write access to entry\n",
285 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
286 rs->sr_text = "no write access to entry";
287 goto return_results;;
290 /* nested transaction */
291 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
292 bdb->bi_db_opflags );
294 if( rs->sr_err != 0 ) {
295 Debug( LDAP_DEBUG_TRACE,
296 LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
297 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
298 rs->sr_err = LDAP_OTHER;
299 rs->sr_text = "internal error";
304 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
305 if ( rs->sr_err != 0 ) {
306 Debug( LDAP_DEBUG_TRACE,
307 LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
308 db_strerror(rs->sr_err), rs->sr_err, 0 );
310 switch( rs->sr_err ) {
311 case DB_LOCK_DEADLOCK:
312 case DB_LOCK_NOTGRANTED:
315 rs->sr_err = LDAP_ALREADY_EXISTS;
318 rs->sr_err = LDAP_OTHER;
324 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
325 if ( rs->sr_err != 0 ) {
326 Debug( LDAP_DEBUG_TRACE,
327 LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
329 switch( rs->sr_err ) {
330 case DB_LOCK_DEADLOCK:
331 case DB_LOCK_NOTGRANTED:
334 rs->sr_err = LDAP_OTHER;
336 rs->sr_text = "entry store failed";
340 /* attribute indexes */
341 rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
342 if ( rs->sr_err != LDAP_SUCCESS ) {
343 Debug( LDAP_DEBUG_TRACE,
344 LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
346 switch( rs->sr_err ) {
347 case DB_LOCK_DEADLOCK:
348 case DB_LOCK_NOTGRANTED:
351 rs->sr_err = LDAP_OTHER;
353 rs->sr_text = "index generation failed";
356 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
357 rs->sr_err = LDAP_OTHER;
358 rs->sr_text = "txn_commit(2) failed";
363 if( op->o_postread ) {
364 if( postread_ctrl == NULL ) {
365 postread_ctrl = &ctrls[num_ctrls++];
366 ctrls[num_ctrls] = NULL;
368 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
369 &slap_post_read_bv, postread_ctrl ) )
371 Debug( LDAP_DEBUG_TRACE,
372 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
373 "failed!\n", 0, 0, 0 );
379 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
380 rs->sr_text = "txn_abort (no-op) failed";
382 rs->sr_err = LDAP_X_NO_OPERATION;
389 Entry *e = entry_dup( op->ora_e );
391 /* pick the RDN if not suffix; otherwise pick the entire DN */
393 nrdn.bv_val = e->e_nname.bv_val;
394 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
399 bdb_cache_add( bdb, ei, e, &nrdn, locker );
401 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
402 rs->sr_text = "txn_commit failed";
404 rs->sr_err = LDAP_SUCCESS;
409 op->o_private = NULL;
411 if ( rs->sr_err != LDAP_SUCCESS ) {
412 Debug( LDAP_DEBUG_TRACE,
413 LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
414 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
415 rs->sr_err = LDAP_OTHER;
419 Debug(LDAP_DEBUG_TRACE,
420 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
421 op->o_noop ? " (no-op)" : "",
422 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
425 if( num_ctrls ) rs->sr_ctrls = ctrls;
428 send_ldap_result( op, rs );
429 if ( !SLAP_SHADOW( op->o_bd ))
430 slap_graduate_commit_csn( op );
435 op->o_private = NULL;
437 if( postread_ctrl != NULL ) {
438 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
439 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
442 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
443 ldap_pvt_thread_yield();
444 TXN_CHECKPOINT( bdb->bi_dbenv,
445 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );