1 /* add.c - ldap BerkeleyDB back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-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>.
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};
43 LDAPControl **postread_ctrl = NULL;
44 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
51 Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_add) ": %s\n",
52 op->oq_add.rs_e->e_name.bv_val, 0, 0);
56 /* acquire connection lock */
57 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
58 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
59 rs->sr_text = "invalid transaction identifier";
60 rs->sr_err = LDAP_X_TXN_ID_INVALID;
62 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
67 if( op->o_conn->c_txn_backend == NULL ) {
68 op->o_conn->c_txn_backend = op->o_bd;
70 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
71 rs->sr_text = "transaction cannot span multiple database contexts";
72 rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
76 /* insert operation into transaction */
78 rs->sr_text = "transaction specified";
79 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
82 /* release connection lock */
83 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
86 send_ldap_result( op, rs );
94 /* add opattrs to shadow as well, only missing attrs will actually
95 * be added; helps compatibility with older OL versions */
96 slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
98 /* check entry's schema */
99 rs->sr_err = entry_schema_check( op, op->oq_add.rs_e, NULL,
100 get_manageDIT(op), &rs->sr_text, textbuf, textlen );
101 if ( rs->sr_err != LDAP_SUCCESS ) {
102 Debug( LDAP_DEBUG_TRACE,
103 LDAP_XSTRING(bdb_add) ": entry failed schema check: "
104 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
108 subentry = is_entry_subentry( op->oq_add.rs_e );
111 * acquire an ID outside of the operation transaction
112 * to avoid serializing adds.
114 rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
115 if( rs->sr_err != 0 ) {
116 Debug( LDAP_DEBUG_TRACE,
117 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
119 rs->sr_err = LDAP_OTHER;
120 rs->sr_text = "internal error";
125 retry: /* transaction retry */
127 /* free parent and reader lock */
128 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
131 rs->sr_err = TXN_ABORT( ltid );
133 op->o_private = NULL;
134 op->o_do_not_cache = opinfo.boi_acl_cache;
135 if( rs->sr_err != 0 ) {
136 rs->sr_err = LDAP_OTHER;
137 rs->sr_text = "internal error";
140 if ( op->o_abandon ) {
141 rs->sr_err = SLAPD_ABANDON;
144 bdb_trans_backoff( ++num_retries );
147 /* begin transaction */
148 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
149 bdb->bi_db_opflags );
151 if( rs->sr_err != 0 ) {
152 Debug( LDAP_DEBUG_TRACE,
153 LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
154 db_strerror(rs->sr_err), rs->sr_err, 0 );
155 rs->sr_err = LDAP_OTHER;
156 rs->sr_text = "internal error";
160 locker = TXN_ID ( ltid );
162 opinfo.boi_bdb = op->o_bd;
163 opinfo.boi_txn = ltid;
164 opinfo.boi_locker = locker;
166 opinfo.boi_acl_cache = op->o_do_not_cache;
167 op->o_private = &opinfo;
170 * Get the parent dn and see if the corresponding entry exists.
172 if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
175 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
178 /* get entry or parent */
179 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
181 switch( rs->sr_err ) {
183 rs->sr_err = LDAP_ALREADY_EXISTS;
187 case DB_LOCK_DEADLOCK:
188 case DB_LOCK_NOTGRANTED:
191 rs->sr_text = "ldap server busy";
194 rs->sr_err = LDAP_OTHER;
195 rs->sr_text = "internal error";
201 if ( !bvmatch( &pdn, &p->e_nname ) ) {
202 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
204 rs->sr_ref = is_entry_referral( p )
205 ? get_entry_referrals( op, p )
207 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
209 Debug( LDAP_DEBUG_TRACE,
210 LDAP_XSTRING(bdb_add) ": parent "
211 "does not exist\n", 0, 0, 0 );
213 rs->sr_err = LDAP_REFERRAL;
214 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
218 rs->sr_err = access_allowed( op, p,
219 children, NULL, ACL_WADD, NULL );
221 if ( ! rs->sr_err ) {
222 switch( opinfo.boi_err ) {
223 case DB_LOCK_DEADLOCK:
224 case DB_LOCK_NOTGRANTED:
228 Debug( LDAP_DEBUG_TRACE,
229 LDAP_XSTRING(bdb_add) ": no write access to parent\n",
231 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
232 rs->sr_text = "no write access to parent";
233 goto return_results;;
236 if ( is_entry_subentry( p ) ) {
237 /* parent is a subentry, don't allow add */
238 Debug( LDAP_DEBUG_TRACE,
239 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
241 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
242 rs->sr_text = "parent is a subentry";
243 goto return_results;;
245 if ( is_entry_alias( p ) ) {
246 /* parent is an alias, don't allow add */
247 Debug( LDAP_DEBUG_TRACE,
248 LDAP_XSTRING(bdb_add) ": parent is alias\n",
250 rs->sr_err = LDAP_ALIAS_PROBLEM;
251 rs->sr_text = "parent is an alias";
252 goto return_results;;
255 if ( is_entry_referral( p ) ) {
256 /* parent is a referral, don't allow add */
257 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
259 rs->sr_ref = get_entry_referrals( op, p );
260 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
262 Debug( LDAP_DEBUG_TRACE,
263 LDAP_XSTRING(bdb_add) ": parent is referral\n",
266 rs->sr_err = LDAP_REFERRAL;
267 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
273 /* parent must be an administrative point of the required kind */
276 /* free parent and reader lock */
277 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
283 * if not attempting to add entry at suffix or with parent ""
285 if ((( !be_isroot( op ) && !be_shadow_update(op) )
286 || pdn.bv_len > 0 ) && !is_entry_glue( op->oq_add.rs_e ))
288 Debug( LDAP_DEBUG_TRACE,
289 LDAP_XSTRING(bdb_add) ": %s denied\n",
290 pdn.bv_len == 0 ? "suffix" : "entry at root",
292 rs->sr_err = LDAP_NO_SUCH_OBJECT;
297 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
298 entry, NULL, ACL_WADD, NULL );
300 if ( ! rs->sr_err ) {
301 switch( opinfo.boi_err ) {
302 case DB_LOCK_DEADLOCK:
303 case DB_LOCK_NOTGRANTED:
307 Debug( LDAP_DEBUG_TRACE,
308 LDAP_XSTRING(bdb_add) ": no write access to entry\n",
310 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
311 rs->sr_text = "no write access to entry";
312 goto return_results;;
315 /* nested transaction */
316 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
317 bdb->bi_db_opflags );
319 if( rs->sr_err != 0 ) {
320 Debug( LDAP_DEBUG_TRACE,
321 LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
322 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
323 rs->sr_err = LDAP_OTHER;
324 rs->sr_text = "internal error";
329 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
330 if ( rs->sr_err != 0 ) {
331 Debug( LDAP_DEBUG_TRACE,
332 LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
333 db_strerror(rs->sr_err), rs->sr_err, 0 );
335 switch( rs->sr_err ) {
336 case DB_LOCK_DEADLOCK:
337 case DB_LOCK_NOTGRANTED:
340 rs->sr_err = LDAP_ALREADY_EXISTS;
343 rs->sr_err = LDAP_OTHER;
348 /* attribute indexes */
349 rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
350 if ( rs->sr_err != LDAP_SUCCESS ) {
351 Debug( LDAP_DEBUG_TRACE,
352 LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
354 switch( rs->sr_err ) {
355 case DB_LOCK_DEADLOCK:
356 case DB_LOCK_NOTGRANTED:
359 rs->sr_err = LDAP_OTHER;
361 rs->sr_text = "index generation failed";
366 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
367 if ( rs->sr_err != 0 ) {
368 Debug( LDAP_DEBUG_TRACE,
369 LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
371 switch( rs->sr_err ) {
372 case DB_LOCK_DEADLOCK:
373 case DB_LOCK_NOTGRANTED:
376 rs->sr_err = LDAP_OTHER;
378 rs->sr_text = "entry store failed";
382 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
383 rs->sr_err = LDAP_OTHER;
384 rs->sr_text = "txn_commit(2) failed";
389 if( op->o_postread ) {
390 if( postread_ctrl == NULL ) {
391 postread_ctrl = &ctrls[num_ctrls++];
392 ctrls[num_ctrls] = NULL;
394 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
395 &slap_post_read_bv, postread_ctrl ) )
397 Debug( LDAP_DEBUG_TRACE,
398 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
399 "failed!\n", 0, 0, 0 );
405 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
406 rs->sr_text = "txn_abort (no-op) failed";
408 rs->sr_err = LDAP_X_NO_OPERATION;
415 Entry *e = entry_dup( op->ora_e );
417 /* pick the RDN if not suffix; otherwise pick the entire DN */
419 nrdn.bv_val = e->e_nname.bv_val;
420 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
425 bdb_cache_add( bdb, ei, e, &nrdn, locker );
427 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
428 rs->sr_text = "txn_commit failed";
430 rs->sr_err = LDAP_SUCCESS;
435 op->o_private = NULL;
437 if ( rs->sr_err != LDAP_SUCCESS ) {
438 Debug( LDAP_DEBUG_TRACE,
439 LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
440 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
441 rs->sr_err = LDAP_OTHER;
445 Debug(LDAP_DEBUG_TRACE,
446 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
447 op->o_noop ? " (no-op)" : "",
448 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
451 if( num_ctrls ) rs->sr_ctrls = ctrls;
454 send_ldap_result( op, rs );
455 slap_graduate_commit_csn( op );
460 op->o_private = NULL;
462 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
463 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
464 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
467 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
468 TXN_CHECKPOINT( bdb->bi_dbenv,
469 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );