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 if ( p != (Entry *)&slap_entry_root ) {
129 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
133 rs->sr_err = TXN_ABORT( ltid );
135 op->o_private = NULL;
136 op->o_do_not_cache = opinfo.boi_acl_cache;
137 if( rs->sr_err != 0 ) {
138 rs->sr_err = LDAP_OTHER;
139 rs->sr_text = "internal error";
142 if ( op->o_abandon ) {
143 rs->sr_err = SLAPD_ABANDON;
146 bdb_trans_backoff( ++num_retries );
149 /* begin transaction */
150 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
151 bdb->bi_db_opflags );
153 if( rs->sr_err != 0 ) {
154 Debug( LDAP_DEBUG_TRACE,
155 LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
156 db_strerror(rs->sr_err), rs->sr_err, 0 );
157 rs->sr_err = LDAP_OTHER;
158 rs->sr_text = "internal error";
162 locker = TXN_ID ( ltid );
164 opinfo.boi_bdb = op->o_bd;
165 opinfo.boi_txn = ltid;
166 opinfo.boi_locker = locker;
168 opinfo.boi_acl_cache = op->o_do_not_cache;
169 op->o_private = &opinfo;
172 * Get the parent dn and see if the corresponding entry exists.
174 if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
177 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
180 /* get entry or parent */
181 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
183 switch( rs->sr_err ) {
185 rs->sr_err = LDAP_ALREADY_EXISTS;
189 case DB_LOCK_DEADLOCK:
190 case DB_LOCK_NOTGRANTED:
193 rs->sr_text = "ldap server busy";
196 rs->sr_err = LDAP_OTHER;
197 rs->sr_text = "internal error";
203 p = (Entry *)&slap_entry_root;
205 if ( !bvmatch( &pdn, &p->e_nname ) ) {
206 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
208 rs->sr_ref = is_entry_referral( p )
209 ? get_entry_referrals( op, p )
211 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
213 Debug( LDAP_DEBUG_TRACE,
214 LDAP_XSTRING(bdb_add) ": parent "
215 "does not exist\n", 0, 0, 0 );
217 rs->sr_err = LDAP_REFERRAL;
218 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
222 rs->sr_err = access_allowed( op, p,
223 children, NULL, ACL_WADD, NULL );
225 if ( ! rs->sr_err ) {
226 switch( opinfo.boi_err ) {
227 case DB_LOCK_DEADLOCK:
228 case DB_LOCK_NOTGRANTED:
232 Debug( LDAP_DEBUG_TRACE,
233 LDAP_XSTRING(bdb_add) ": no write access to parent\n",
235 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
236 rs->sr_text = "no write access to parent";
237 goto return_results;;
240 if ( is_entry_subentry( p ) ) {
241 /* parent is a subentry, don't allow add */
242 Debug( LDAP_DEBUG_TRACE,
243 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
245 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
246 rs->sr_text = "parent is a subentry";
247 goto return_results;;
249 if ( is_entry_alias( p ) ) {
250 /* parent is an alias, don't allow add */
251 Debug( LDAP_DEBUG_TRACE,
252 LDAP_XSTRING(bdb_add) ": parent is alias\n",
254 rs->sr_err = LDAP_ALIAS_PROBLEM;
255 rs->sr_text = "parent is an alias";
256 goto return_results;;
259 if ( is_entry_referral( p ) ) {
260 /* parent is a referral, don't allow add */
261 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
263 rs->sr_ref = get_entry_referrals( op, p );
264 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
266 Debug( LDAP_DEBUG_TRACE,
267 LDAP_XSTRING(bdb_add) ": parent is referral\n",
270 rs->sr_err = LDAP_REFERRAL;
271 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
277 /* parent must be an administrative point of the required kind */
280 /* free parent and reader lock */
281 if ( p != (Entry *)&slap_entry_root ) {
282 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
286 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
287 entry, NULL, ACL_WADD, NULL );
289 if ( ! rs->sr_err ) {
290 switch( opinfo.boi_err ) {
291 case DB_LOCK_DEADLOCK:
292 case DB_LOCK_NOTGRANTED:
296 Debug( LDAP_DEBUG_TRACE,
297 LDAP_XSTRING(bdb_add) ": no write access to entry\n",
299 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
300 rs->sr_text = "no write access to entry";
301 goto return_results;;
304 /* nested transaction */
305 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
306 bdb->bi_db_opflags );
308 if( rs->sr_err != 0 ) {
309 Debug( LDAP_DEBUG_TRACE,
310 LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
311 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
312 rs->sr_err = LDAP_OTHER;
313 rs->sr_text = "internal error";
318 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
319 if ( rs->sr_err != 0 ) {
320 Debug( LDAP_DEBUG_TRACE,
321 LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
322 db_strerror(rs->sr_err), rs->sr_err, 0 );
324 switch( rs->sr_err ) {
325 case DB_LOCK_DEADLOCK:
326 case DB_LOCK_NOTGRANTED:
329 rs->sr_err = LDAP_ALREADY_EXISTS;
332 rs->sr_err = LDAP_OTHER;
337 /* attribute indexes */
338 rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
339 if ( rs->sr_err != LDAP_SUCCESS ) {
340 Debug( LDAP_DEBUG_TRACE,
341 LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
343 switch( rs->sr_err ) {
344 case DB_LOCK_DEADLOCK:
345 case DB_LOCK_NOTGRANTED:
348 rs->sr_err = LDAP_OTHER;
350 rs->sr_text = "index generation failed";
355 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
356 if ( rs->sr_err != 0 ) {
357 Debug( LDAP_DEBUG_TRACE,
358 LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
360 switch( rs->sr_err ) {
361 case DB_LOCK_DEADLOCK:
362 case DB_LOCK_NOTGRANTED:
365 rs->sr_err = LDAP_OTHER;
367 rs->sr_text = "entry store failed";
371 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
372 rs->sr_err = LDAP_OTHER;
373 rs->sr_text = "txn_commit(2) failed";
378 if( op->o_postread ) {
379 if( postread_ctrl == NULL ) {
380 postread_ctrl = &ctrls[num_ctrls++];
381 ctrls[num_ctrls] = NULL;
383 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
384 &slap_post_read_bv, postread_ctrl ) )
386 Debug( LDAP_DEBUG_TRACE,
387 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
388 "failed!\n", 0, 0, 0 );
394 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
395 rs->sr_text = "txn_abort (no-op) failed";
397 rs->sr_err = LDAP_X_NO_OPERATION;
404 Entry *e = entry_dup( op->ora_e );
406 /* pick the RDN if not suffix; otherwise pick the entire DN */
408 nrdn.bv_val = e->e_nname.bv_val;
409 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
414 bdb_cache_add( bdb, ei, e, &nrdn, locker );
416 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
417 rs->sr_text = "txn_commit failed";
419 rs->sr_err = LDAP_SUCCESS;
424 op->o_private = NULL;
426 if ( rs->sr_err != LDAP_SUCCESS ) {
427 Debug( LDAP_DEBUG_TRACE,
428 LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
429 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
430 rs->sr_err = LDAP_OTHER;
434 Debug(LDAP_DEBUG_TRACE,
435 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
436 op->o_noop ? " (no-op)" : "",
437 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
440 if( num_ctrls ) rs->sr_ctrls = ctrls;
443 send_ldap_result( op, rs );
444 slap_graduate_commit_csn( op );
449 op->o_private = NULL;
451 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
452 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
453 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
456 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
457 TXN_CHECKPOINT( bdb->bi_dbenv,
458 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );