1 /* add.c - ldap mdb back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2011 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 mdb_add(Operation *op, SlapReply *rs )
27 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
29 Entry *p = NULL, *oe = op->ora_e;
30 char textbuf[SLAP_TEXT_BUFLEN];
31 size_t textlen = sizeof textbuf;
32 AttributeDescription *children = slap_schema.si_ad_children;
33 AttributeDescription *entry = slap_schema.si_ad_entry;
35 MDB_cursor *mc = NULL;
38 mdb_op_info opinfo = {{{ 0 }}}, *moi = &opinfo;
43 LDAPControl **postread_ctrl = NULL;
44 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
51 Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(mdb_add) ": %s\n",
52 op->ora_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 /* check entry's schema */
95 rs->sr_err = entry_schema_check( op, op->ora_e, NULL,
96 get_relax(op), 1, NULL, &rs->sr_text, textbuf, textlen );
97 if ( rs->sr_err != LDAP_SUCCESS ) {
98 Debug( LDAP_DEBUG_TRACE,
99 LDAP_XSTRING(mdb_add) ": entry failed schema check: "
100 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
104 /* add opattrs to shadow as well, only missing attrs will actually
105 * be added; helps compatibility with older OL versions */
106 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
107 if ( rs->sr_err != LDAP_SUCCESS ) {
108 Debug( LDAP_DEBUG_TRACE,
109 LDAP_XSTRING(mdb_add) ": entry failed op attrs add: "
110 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
114 if ( get_assert( op ) &&
115 ( test_filter( op, op->ora_e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
117 rs->sr_err = LDAP_ASSERTION_FAILED;
121 subentry = is_entry_subentry( op->ora_e );
123 /* begin transaction */
124 rs->sr_err = mdb_opinfo_get( op, mdb, 0, &moi );
126 if( rs->sr_err != 0 ) {
127 Debug( LDAP_DEBUG_TRACE,
128 LDAP_XSTRING(mdb_add) ": txn_begin failed: %s (%d)\n",
129 mdb_strerror(rs->sr_err), rs->sr_err, 0 );
130 rs->sr_err = LDAP_OTHER;
131 rs->sr_text = "internal error";
138 * Get the parent dn and see if the corresponding entry exists.
140 if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
143 dnParent( &op->ora_e->e_nname, &pdn );
146 rs->sr_err = mdb_cursor_open( txn, mdb->mi_dn2id, &mcd );
147 if( rs->sr_err != 0 ) {
148 Debug( LDAP_DEBUG_TRACE,
149 LDAP_XSTRING(mdb_add) ": mdb_cursor_open failed (%d)\n",
151 rs->sr_err = LDAP_OTHER;
152 rs->sr_text = "internal error";
156 /* get entry or parent */
157 rs->sr_err = mdb_dn2entry( op, txn, mcd, &op->ora_e->e_nname, &p, 1 );
158 switch( rs->sr_err ) {
160 rs->sr_err = LDAP_ALREADY_EXISTS;
161 mdb_entry_return( op, p );
167 rs->sr_text = "ldap server busy";
170 rs->sr_err = LDAP_OTHER;
171 rs->sr_text = "internal error";
176 p = (Entry *)&slap_entry_root;
178 if ( !bvmatch( &pdn, &p->e_nname ) ) {
179 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
181 if ( p != (Entry *)&slap_entry_root && is_entry_referral( p )) {
182 BerVarray ref = get_entry_referrals( op, p );
183 rs->sr_ref = referral_rewrite( ref, &p->e_name,
184 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
185 ber_bvarray_free( ref );
189 if ( p != (Entry *)&slap_entry_root )
190 mdb_entry_return( op, p );
192 Debug( LDAP_DEBUG_TRACE,
193 LDAP_XSTRING(mdb_add) ": parent "
194 "does not exist\n", 0, 0, 0 );
196 rs->sr_err = LDAP_REFERRAL;
197 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
201 rs->sr_err = access_allowed( op, p,
202 children, NULL, ACL_WADD, NULL );
204 if ( ! rs->sr_err ) {
205 if ( p != (Entry *)&slap_entry_root )
206 mdb_entry_return( op, p );
209 Debug( LDAP_DEBUG_TRACE,
210 LDAP_XSTRING(mdb_add) ": no write access to parent\n",
212 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
213 rs->sr_text = "no write access to parent";
214 goto return_results;;
217 if ( p != (Entry *)&slap_entry_root ) {
218 if ( is_entry_subentry( p ) ) {
219 mdb_entry_return( op, p );
221 /* parent is a subentry, don't allow add */
222 Debug( LDAP_DEBUG_TRACE,
223 LDAP_XSTRING(mdb_add) ": parent is subentry\n",
225 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
226 rs->sr_text = "parent is a subentry";
227 goto return_results;;
230 if ( is_entry_alias( p ) ) {
231 mdb_entry_return( op, p );
233 /* parent is an alias, don't allow add */
234 Debug( LDAP_DEBUG_TRACE,
235 LDAP_XSTRING(mdb_add) ": parent is alias\n",
237 rs->sr_err = LDAP_ALIAS_PROBLEM;
238 rs->sr_text = "parent is an alias";
239 goto return_results;;
242 if ( is_entry_referral( p ) ) {
243 BerVarray ref = get_entry_referrals( op, p );
244 /* parent is a referral, don't allow add */
245 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
247 rs->sr_ref = referral_rewrite( ref, &p->e_name,
248 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
249 ber_bvarray_free( ref );
250 mdb_entry_return( op, p );
252 Debug( LDAP_DEBUG_TRACE,
253 LDAP_XSTRING(mdb_add) ": parent is referral\n",
256 rs->sr_err = LDAP_REFERRAL;
257 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
265 /* parent must be an administrative point of the required kind */
269 if ( p != (Entry *)&slap_entry_root ) {
271 if ( p->e_nname.bv_len ) {
274 /* ITS#5326: use parent's DN if differs from provided one */
275 dnParent( &op->ora_e->e_name, &ppdn );
276 if ( !dn_match( &p->e_name, &ppdn ) ) {
280 dnRdn( &op->ora_e->e_name, &rdn );
282 build_new_dn( &newdn, &p->e_name, &rdn, NULL );
283 if ( op->ora_e->e_name.bv_val != op->o_req_dn.bv_val )
284 ber_memfree( op->ora_e->e_name.bv_val );
285 op->ora_e->e_name = newdn;
287 /* FIXME: should check whether
288 * dnNormalize(newdn) == e->e_nname ... */
292 mdb_entry_return( op, p );
296 rs->sr_err = access_allowed( op, op->ora_e,
297 entry, NULL, ACL_WADD, NULL );
299 if ( ! rs->sr_err ) {
300 Debug( LDAP_DEBUG_TRACE,
301 LDAP_XSTRING(mdb_add) ": no write access to entry\n",
303 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
304 rs->sr_text = "no write access to entry";
305 goto return_results;;
309 * Check ACL for attribute write access
311 if (!acl_check_modlist(op, oe, op->ora_modlist)) {
312 Debug( LDAP_DEBUG_TRACE,
313 LDAP_XSTRING(mdb_add) ": no write access to attribute\n",
315 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
316 rs->sr_text = "no write access to attribute";
317 goto return_results;;
320 rs->sr_err = mdb_cursor_open( txn, mdb->mi_id2entry, &mc );
321 if( rs->sr_err != 0 ) {
322 Debug( LDAP_DEBUG_TRACE,
323 LDAP_XSTRING(mdb_add) ": mdb_cursor_open failed (%d)\n",
325 rs->sr_err = LDAP_OTHER;
326 rs->sr_text = "internal error";
330 rs->sr_err = mdb_next_id( op->o_bd, mc, &eid );
331 if( rs->sr_err != 0 ) {
332 Debug( LDAP_DEBUG_TRACE,
333 LDAP_XSTRING(mdb_add) ": next_id failed (%d)\n",
335 rs->sr_err = LDAP_OTHER;
336 rs->sr_text = "internal error";
339 op->ora_e->e_id = eid;
342 rs->sr_err = mdb_dn2id_add( op, mcd, mcd, pid, op->ora_e );
343 mdb_cursor_close( mcd );
344 if ( rs->sr_err != 0 ) {
345 Debug( LDAP_DEBUG_TRACE,
346 LDAP_XSTRING(mdb_add) ": dn2id_add failed: %s (%d)\n",
347 mdb_strerror(rs->sr_err), rs->sr_err, 0 );
349 switch( rs->sr_err ) {
351 rs->sr_err = LDAP_ALREADY_EXISTS;
354 rs->sr_err = LDAP_OTHER;
359 /* attribute indexes */
360 rs->sr_err = mdb_index_entry_add( op, txn, op->ora_e );
361 if ( rs->sr_err != LDAP_SUCCESS ) {
362 Debug( LDAP_DEBUG_TRACE,
363 LDAP_XSTRING(mdb_add) ": index_entry_add failed\n",
365 rs->sr_err = LDAP_OTHER;
366 rs->sr_text = "index generation failed";
371 rs->sr_err = mdb_id2entry_add( op, txn, mc, op->ora_e );
372 if ( rs->sr_err != 0 ) {
373 Debug( LDAP_DEBUG_TRACE,
374 LDAP_XSTRING(mdb_add) ": id2entry_add failed\n",
376 rs->sr_err = LDAP_OTHER;
377 rs->sr_text = "entry store failed";
382 if( op->o_postread ) {
383 if( postread_ctrl == NULL ) {
384 postread_ctrl = &ctrls[num_ctrls++];
385 ctrls[num_ctrls] = NULL;
387 if ( slap_read_controls( op, rs, op->ora_e,
388 &slap_post_read_bv, postread_ctrl ) )
390 Debug( LDAP_DEBUG_TRACE,
391 "<=- " LDAP_XSTRING(mdb_add) ": post-read "
392 "failed!\n", 0, 0, 0 );
393 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
394 /* FIXME: is it correct to abort
395 * operation if control fails? */
401 if ( moi == &opinfo ) {
402 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
403 opinfo.moi_oe.oe_key = NULL;
405 mdb_txn_abort( txn );
406 rs->sr_err = LDAP_X_NO_OPERATION;
411 if (( rs->sr_err = mdb_txn_commit( txn )) != 0 ) {
412 rs->sr_text = "txn_commit failed";
413 Debug( LDAP_DEBUG_TRACE,
414 LDAP_XSTRING(mdb_add) ": %s : %s (%d)\n",
415 rs->sr_text, mdb_strerror(rs->sr_err), rs->sr_err );
416 rs->sr_err = LDAP_OTHER;
422 Debug(LDAP_DEBUG_TRACE,
423 LDAP_XSTRING(mdb_add) ": added%s id=%08lx dn=\"%s\"\n",
424 op->o_noop ? " (no-op)" : "",
425 op->ora_e->e_id, op->ora_e->e_dn );
428 if( num_ctrls ) rs->sr_ctrls = ctrls;
431 success = rs->sr_err;
432 send_ldap_result( op, rs );
434 if( moi == &opinfo ) {
436 mdb_txn_abort( txn );
438 if ( opinfo.moi_oe.oe_key ) {
439 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
443 if( success == LDAP_SUCCESS ) {
445 if ( mdb->bi_txn_cp_kbyte ) {
446 TXN_CHECKPOINT( mdb->bi_dbenv,
447 mdb->bi_txn_cp_kbyte, mdb->bi_txn_cp_min, 0 );
452 slap_graduate_commit_csn( op );
454 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
455 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
456 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );