1 /* add.c - ldap BerkeleyDB back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2014 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;
29 Entry *p = NULL, *oe = op->ora_e;
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;
37 struct bdb_op_info opinfo = {{{ 0 }}};
44 LDAPControl **postread_ctrl = NULL;
45 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
48 Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_add) ": %s\n",
49 op->ora_e->e_name.bv_val, 0, 0);
52 if( op->o_txnSpec && txn_preop( op, rs ))
58 /* check entry's schema */
59 rs->sr_err = entry_schema_check( op, op->ora_e, NULL,
60 get_relax(op), 1, NULL, &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 );
68 /* add opattrs to shadow as well, only missing attrs will actually
69 * be added; helps compatibility with older OL versions */
70 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
71 if ( rs->sr_err != LDAP_SUCCESS ) {
72 Debug( LDAP_DEBUG_TRACE,
73 LDAP_XSTRING(bdb_add) ": entry failed op attrs add: "
74 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
78 if ( get_assert( op ) &&
79 ( test_filter( op, op->ora_e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
81 rs->sr_err = LDAP_ASSERTION_FAILED;
85 subentry = is_entry_subentry( op->ora_e );
88 retry: /* transaction retry */
90 /* free parent and reader lock */
91 if ( p != (Entry *)&slap_entry_root ) {
92 bdb_unlocked_cache_return_entry_r( bdb, p );
96 rs->sr_err = TXN_ABORT( ltid );
98 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
99 opinfo.boi_oe.oe_key = NULL;
100 op->o_do_not_cache = opinfo.boi_acl_cache;
101 if( rs->sr_err != 0 ) {
102 rs->sr_err = LDAP_OTHER;
103 rs->sr_text = "internal error";
106 if ( op->o_abandon ) {
107 rs->sr_err = SLAPD_ABANDON;
110 bdb_trans_backoff( ++num_retries );
113 /* begin transaction */
115 int tflags = bdb->bi_db_opflags;
116 if ( get_lazyCommit( op ))
117 tflags |= DB_TXN_NOSYNC;
118 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id, tflags );
121 if( rs->sr_err != 0 ) {
122 Debug( LDAP_DEBUG_TRACE,
123 LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
124 db_strerror(rs->sr_err), rs->sr_err, 0 );
125 rs->sr_err = LDAP_OTHER;
126 rs->sr_text = "internal error";
129 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_add) ": txn1 id: %x\n",
130 ltid->id(ltid), 0, 0 );
132 opinfo.boi_oe.oe_key = bdb;
133 opinfo.boi_txn = ltid;
135 opinfo.boi_acl_cache = op->o_do_not_cache;
136 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
139 * Get the parent dn and see if the corresponding entry exists.
141 if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
144 dnParent( &op->ora_e->e_nname, &pdn );
147 /* get entry or parent */
148 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
150 switch( rs->sr_err ) {
152 rs->sr_err = LDAP_ALREADY_EXISTS;
156 case DB_LOCK_DEADLOCK:
157 case DB_LOCK_NOTGRANTED:
160 rs->sr_text = "ldap server busy";
163 rs->sr_err = LDAP_OTHER;
164 rs->sr_text = "internal error";
170 p = (Entry *)&slap_entry_root;
172 if ( !bvmatch( &pdn, &p->e_nname ) ) {
173 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
175 rs->sr_ref = is_entry_referral( p )
176 ? get_entry_referrals( op, p )
178 if ( p != (Entry *)&slap_entry_root )
179 bdb_unlocked_cache_return_entry_r( bdb, p );
181 Debug( LDAP_DEBUG_TRACE,
182 LDAP_XSTRING(bdb_add) ": parent "
183 "does not exist\n", 0, 0, 0 );
185 rs->sr_err = LDAP_REFERRAL;
186 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
190 rs->sr_err = access_allowed( op, p,
191 children, NULL, ACL_WADD, NULL );
193 if ( ! rs->sr_err ) {
194 switch( opinfo.boi_err ) {
195 case DB_LOCK_DEADLOCK:
196 case DB_LOCK_NOTGRANTED:
200 if ( p != (Entry *)&slap_entry_root )
201 bdb_unlocked_cache_return_entry_r( bdb, p );
204 Debug( LDAP_DEBUG_TRACE,
205 LDAP_XSTRING(bdb_add) ": no write access to parent\n",
207 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
208 rs->sr_text = "no write access to parent";
209 goto return_results;;
212 if ( p != (Entry *)&slap_entry_root ) {
213 if ( is_entry_subentry( p ) ) {
214 bdb_unlocked_cache_return_entry_r( bdb, p );
216 /* parent is a subentry, don't allow add */
217 Debug( LDAP_DEBUG_TRACE,
218 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
220 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
221 rs->sr_text = "parent is a subentry";
222 goto return_results;;
225 if ( is_entry_alias( p ) ) {
226 bdb_unlocked_cache_return_entry_r( bdb, p );
228 /* parent is an alias, don't allow add */
229 Debug( LDAP_DEBUG_TRACE,
230 LDAP_XSTRING(bdb_add) ": parent is alias\n",
232 rs->sr_err = LDAP_ALIAS_PROBLEM;
233 rs->sr_text = "parent is an alias";
234 goto return_results;;
237 if ( is_entry_referral( p ) ) {
238 /* parent is a referral, don't allow add */
239 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
241 rs->sr_ref = get_entry_referrals( op, p );
242 bdb_unlocked_cache_return_entry_r( bdb, p );
244 Debug( LDAP_DEBUG_TRACE,
245 LDAP_XSTRING(bdb_add) ": parent is referral\n",
248 rs->sr_err = LDAP_REFERRAL;
249 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
257 /* parent must be an administrative point of the required kind */
260 /* free parent and reader lock */
261 if ( p != (Entry *)&slap_entry_root ) {
262 if ( p->e_nname.bv_len ) {
265 /* ITS#5326: use parent's DN if differs from provided one */
266 dnParent( &op->ora_e->e_name, &ppdn );
267 if ( !dn_match( &p->e_name, &ppdn ) ) {
271 dnRdn( &op->ora_e->e_name, &rdn );
273 build_new_dn( &newdn, &p->e_name, &rdn, NULL );
274 if ( op->ora_e->e_name.bv_val != op->o_req_dn.bv_val )
275 ber_memfree( op->ora_e->e_name.bv_val );
276 op->ora_e->e_name = newdn;
278 /* FIXME: should check whether
279 * dnNormalize(newdn) == e->e_nname ... */
283 bdb_unlocked_cache_return_entry_r( bdb, p );
287 rs->sr_err = access_allowed( op, op->ora_e,
288 entry, NULL, ACL_WADD, NULL );
290 if ( ! rs->sr_err ) {
291 switch( opinfo.boi_err ) {
292 case DB_LOCK_DEADLOCK:
293 case DB_LOCK_NOTGRANTED:
297 Debug( LDAP_DEBUG_TRACE,
298 LDAP_XSTRING(bdb_add) ": no write access to entry\n",
300 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
301 rs->sr_text = "no write access to entry";
302 goto return_results;;
306 * Check ACL for attribute write access
308 if (!acl_check_modlist(op, oe, op->ora_modlist)) {
309 switch( opinfo.boi_err ) {
310 case DB_LOCK_DEADLOCK:
311 case DB_LOCK_NOTGRANTED:
315 Debug( LDAP_DEBUG_TRACE,
316 LDAP_XSTRING(bdb_add) ": no write access to attribute\n",
318 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
319 rs->sr_text = "no write access to attribute";
320 goto return_results;;
324 rs->sr_err = bdb_next_id( op->o_bd, &eid );
325 if( rs->sr_err != 0 ) {
326 Debug( LDAP_DEBUG_TRACE,
327 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
329 rs->sr_err = LDAP_OTHER;
330 rs->sr_text = "internal error";
333 op->ora_e->e_id = eid;
336 /* nested transaction */
337 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
338 bdb->bi_db_opflags );
340 if( rs->sr_err != 0 ) {
341 Debug( LDAP_DEBUG_TRACE,
342 LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
343 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
344 rs->sr_err = LDAP_OTHER;
345 rs->sr_text = "internal error";
348 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_add) ": txn2 id: %x\n",
349 lt2->id(lt2), 0, 0 );
352 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->ora_e );
353 if ( rs->sr_err != 0 ) {
354 Debug( LDAP_DEBUG_TRACE,
355 LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
356 db_strerror(rs->sr_err), rs->sr_err, 0 );
358 switch( rs->sr_err ) {
359 case DB_LOCK_DEADLOCK:
360 case DB_LOCK_NOTGRANTED:
363 rs->sr_err = LDAP_ALREADY_EXISTS;
366 rs->sr_err = LDAP_OTHER;
371 /* attribute indexes */
372 rs->sr_err = bdb_index_entry_add( op, lt2, op->ora_e );
373 if ( rs->sr_err != LDAP_SUCCESS ) {
374 Debug( LDAP_DEBUG_TRACE,
375 LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
377 switch( rs->sr_err ) {
378 case DB_LOCK_DEADLOCK:
379 case DB_LOCK_NOTGRANTED:
382 rs->sr_err = LDAP_OTHER;
384 rs->sr_text = "index generation failed";
389 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->ora_e );
390 if ( rs->sr_err != 0 ) {
391 Debug( LDAP_DEBUG_TRACE,
392 LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
394 switch( rs->sr_err ) {
395 case DB_LOCK_DEADLOCK:
396 case DB_LOCK_NOTGRANTED:
399 rs->sr_err = LDAP_OTHER;
401 rs->sr_text = "entry store failed";
405 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
406 rs->sr_err = LDAP_OTHER;
407 rs->sr_text = "txn_commit(2) failed";
412 if( op->o_postread ) {
413 if( postread_ctrl == NULL ) {
414 postread_ctrl = &ctrls[num_ctrls++];
415 ctrls[num_ctrls] = NULL;
417 if ( slap_read_controls( op, rs, op->ora_e,
418 &slap_post_read_bv, postread_ctrl ) )
420 Debug( LDAP_DEBUG_TRACE,
421 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
422 "failed!\n", 0, 0, 0 );
423 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
424 /* FIXME: is it correct to abort
425 * operation if control fails? */
432 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
433 rs->sr_text = "txn_abort (no-op) failed";
435 rs->sr_err = LDAP_X_NO_OPERATION;
443 /* pick the RDN if not suffix; otherwise pick the entire DN */
445 nrdn.bv_val = op->ora_e->e_nname.bv_val;
446 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
448 nrdn = op->ora_e->e_nname;
451 bdb_cache_add( bdb, ei, op->ora_e, &nrdn, ltid, &lock );
453 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
454 rs->sr_text = "txn_commit failed";
456 rs->sr_err = LDAP_SUCCESS;
461 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
462 opinfo.boi_oe.oe_key = NULL;
464 if ( rs->sr_err != LDAP_SUCCESS ) {
465 Debug( LDAP_DEBUG_TRACE,
466 LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
467 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
468 rs->sr_err = LDAP_OTHER;
472 Debug(LDAP_DEBUG_TRACE,
473 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
474 op->o_noop ? " (no-op)" : "",
475 op->ora_e->e_id, op->ora_e->e_dn );
478 if( num_ctrls ) rs->sr_ctrls = ctrls;
481 success = rs->sr_err;
482 send_ldap_result( op, rs );
487 if ( opinfo.boi_oe.oe_key ) {
488 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
491 if( success == LDAP_SUCCESS ) {
492 /* We own the entry now, and it can be purged at will
493 * Check to make sure it's the same entry we entered with.
494 * Possibly a callback may have mucked with it, although
495 * in general callbacks should treat the entry as read-only.
497 bdb_cache_deref( oe->e_private );
498 if ( op->ora_e == oe )
501 if ( bdb->bi_txn_cp_kbyte ) {
502 TXN_CHECKPOINT( bdb->bi_dbenv,
503 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
507 slap_graduate_commit_csn( op );
509 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
510 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
511 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );