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 ID eid = NOID, pid = 0;
36 mdb_op_info opinfo = {{{ 0 }}}, *moi = &opinfo;
41 LDAPControl **postread_ctrl = NULL;
42 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
49 Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(mdb_add) ": %s\n",
50 op->ora_e->e_name.bv_val, 0, 0);
54 /* acquire connection lock */
55 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
56 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
57 rs->sr_text = "invalid transaction identifier";
58 rs->sr_err = LDAP_X_TXN_ID_INVALID;
60 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
65 if( op->o_conn->c_txn_backend == NULL ) {
66 op->o_conn->c_txn_backend = op->o_bd;
68 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
69 rs->sr_text = "transaction cannot span multiple database contexts";
70 rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
74 /* insert operation into transaction */
76 rs->sr_text = "transaction specified";
77 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
80 /* release connection lock */
81 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
84 send_ldap_result( op, rs );
92 /* check entry's schema */
93 rs->sr_err = entry_schema_check( op, op->ora_e, NULL,
94 get_relax(op), 1, NULL, &rs->sr_text, textbuf, textlen );
95 if ( rs->sr_err != LDAP_SUCCESS ) {
96 Debug( LDAP_DEBUG_TRACE,
97 LDAP_XSTRING(mdb_add) ": entry failed schema check: "
98 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
102 /* add opattrs to shadow as well, only missing attrs will actually
103 * be added; helps compatibility with older OL versions */
104 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
105 if ( rs->sr_err != LDAP_SUCCESS ) {
106 Debug( LDAP_DEBUG_TRACE,
107 LDAP_XSTRING(mdb_add) ": entry failed op attrs add: "
108 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
112 if ( get_assert( op ) &&
113 ( test_filter( op, op->ora_e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
115 rs->sr_err = LDAP_ASSERTION_FAILED;
119 subentry = is_entry_subentry( op->ora_e );
121 /* begin transaction */
122 rs->sr_err = mdb_opinfo_get( op, mdb, 0, &moi );
124 if( rs->sr_err != 0 ) {
125 Debug( LDAP_DEBUG_TRACE,
126 LDAP_XSTRING(mdb_add) ": txn_begin failed: %s (%d)\n",
127 mdb_strerror(rs->sr_err), rs->sr_err, 0 );
128 rs->sr_err = LDAP_OTHER;
129 rs->sr_text = "internal error";
136 * Get the parent dn and see if the corresponding entry exists.
138 if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
141 dnParent( &op->ora_e->e_nname, &pdn );
144 /* get entry or parent */
145 rs->sr_err = mdb_dn2entry( op, txn, &op->ora_e->e_nname, &p, 1 );
146 switch( rs->sr_err ) {
148 rs->sr_err = LDAP_ALREADY_EXISTS;
149 mdb_entry_return( p );
155 rs->sr_text = "ldap server busy";
158 rs->sr_err = LDAP_OTHER;
159 rs->sr_text = "internal error";
164 p = (Entry *)&slap_entry_root;
166 if ( !bvmatch( &pdn, &p->e_nname ) ) {
167 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
169 rs->sr_ref = p != (Entry *)&slap_entry_root &&
170 is_entry_referral( p ) ? get_entry_referrals( op, p )
172 if ( p != (Entry *)&slap_entry_root )
173 mdb_entry_return( p );
175 Debug( LDAP_DEBUG_TRACE,
176 LDAP_XSTRING(mdb_add) ": parent "
177 "does not exist\n", 0, 0, 0 );
179 rs->sr_err = LDAP_REFERRAL;
180 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
184 rs->sr_err = access_allowed( op, p,
185 children, NULL, ACL_WADD, NULL );
187 if ( ! rs->sr_err ) {
188 if ( p != (Entry *)&slap_entry_root )
189 mdb_entry_return( p );
192 Debug( LDAP_DEBUG_TRACE,
193 LDAP_XSTRING(mdb_add) ": no write access to parent\n",
195 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
196 rs->sr_text = "no write access to parent";
197 goto return_results;;
200 if ( p != (Entry *)&slap_entry_root ) {
201 if ( is_entry_subentry( p ) ) {
202 mdb_entry_return( p );
204 /* parent is a subentry, don't allow add */
205 Debug( LDAP_DEBUG_TRACE,
206 LDAP_XSTRING(mdb_add) ": parent is subentry\n",
208 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
209 rs->sr_text = "parent is a subentry";
210 goto return_results;;
213 if ( is_entry_alias( p ) ) {
214 mdb_entry_return( p );
216 /* parent is an alias, don't allow add */
217 Debug( LDAP_DEBUG_TRACE,
218 LDAP_XSTRING(mdb_add) ": parent is alias\n",
220 rs->sr_err = LDAP_ALIAS_PROBLEM;
221 rs->sr_text = "parent is an alias";
222 goto return_results;;
225 if ( is_entry_referral( p ) ) {
226 /* parent is a referral, don't allow add */
227 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
229 rs->sr_ref = get_entry_referrals( op, p );
230 mdb_entry_return( p );
232 Debug( LDAP_DEBUG_TRACE,
233 LDAP_XSTRING(mdb_add) ": parent is referral\n",
236 rs->sr_err = LDAP_REFERRAL;
237 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
245 /* parent must be an administrative point of the required kind */
249 if ( p != (Entry *)&slap_entry_root ) {
251 if ( p->e_nname.bv_len ) {
254 /* ITS#5326: use parent's DN if differs from provided one */
255 dnParent( &op->ora_e->e_name, &ppdn );
256 if ( !dn_match( &p->e_name, &ppdn ) ) {
260 dnRdn( &op->ora_e->e_name, &rdn );
262 build_new_dn( &newdn, &p->e_name, &rdn, NULL );
263 if ( op->ora_e->e_name.bv_val != op->o_req_dn.bv_val )
264 ber_memfree( op->ora_e->e_name.bv_val );
265 op->ora_e->e_name = newdn;
267 /* FIXME: should check whether
268 * dnNormalize(newdn) == e->e_nname ... */
272 mdb_entry_return( p );
276 rs->sr_err = access_allowed( op, op->ora_e,
277 entry, NULL, ACL_WADD, NULL );
279 if ( ! rs->sr_err ) {
280 Debug( LDAP_DEBUG_TRACE,
281 LDAP_XSTRING(mdb_add) ": no write access to entry\n",
283 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
284 rs->sr_text = "no write access to entry";
285 goto return_results;;
289 * Check ACL for attribute write access
291 if (!acl_check_modlist(op, oe, op->ora_modlist)) {
292 Debug( LDAP_DEBUG_TRACE,
293 LDAP_XSTRING(mdb_add) ": no write access to attribute\n",
295 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
296 rs->sr_text = "no write access to attribute";
297 goto return_results;;
301 rs->sr_err = mdb_next_id( op->o_bd, txn, &eid );
302 if( rs->sr_err != 0 ) {
303 Debug( LDAP_DEBUG_TRACE,
304 LDAP_XSTRING(mdb_add) ": next_id failed (%d)\n",
306 rs->sr_err = LDAP_OTHER;
307 rs->sr_text = "internal error";
310 op->ora_e->e_id = eid;
314 rs->sr_err = mdb_dn2id_add( op, txn, pid, op->ora_e );
315 if ( rs->sr_err != 0 ) {
316 Debug( LDAP_DEBUG_TRACE,
317 LDAP_XSTRING(mdb_add) ": dn2id_add failed: %s (%d)\n",
318 mdb_strerror(rs->sr_err), rs->sr_err, 0 );
320 switch( rs->sr_err ) {
322 rs->sr_err = LDAP_ALREADY_EXISTS;
325 rs->sr_err = LDAP_OTHER;
330 /* attribute indexes */
331 rs->sr_err = mdb_index_entry_add( op, txn, op->ora_e );
332 if ( rs->sr_err != LDAP_SUCCESS ) {
333 Debug( LDAP_DEBUG_TRACE,
334 LDAP_XSTRING(mdb_add) ": index_entry_add failed\n",
336 rs->sr_err = LDAP_OTHER;
337 rs->sr_text = "index generation failed";
342 rs->sr_err = mdb_id2entry_add( op, txn, op->ora_e );
343 if ( rs->sr_err != 0 ) {
344 Debug( LDAP_DEBUG_TRACE,
345 LDAP_XSTRING(mdb_add) ": id2entry_add failed\n",
347 rs->sr_err = LDAP_OTHER;
348 rs->sr_text = "entry store failed";
353 if( op->o_postread ) {
354 if( postread_ctrl == NULL ) {
355 postread_ctrl = &ctrls[num_ctrls++];
356 ctrls[num_ctrls] = NULL;
358 if ( slap_read_controls( op, rs, op->ora_e,
359 &slap_post_read_bv, postread_ctrl ) )
361 Debug( LDAP_DEBUG_TRACE,
362 "<=- " LDAP_XSTRING(mdb_add) ": post-read "
363 "failed!\n", 0, 0, 0 );
364 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
365 /* FIXME: is it correct to abort
366 * operation if control fails? */
372 if ( moi == &opinfo ) {
373 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
374 opinfo.moi_oe.oe_key = NULL;
376 mdb_txn_abort( txn );
377 rs->sr_err = LDAP_X_NO_OPERATION;
382 if (( rs->sr_err = mdb_txn_commit( txn )) != 0 ) {
383 rs->sr_text = "txn_commit failed";
384 Debug( LDAP_DEBUG_TRACE,
385 LDAP_XSTRING(mdb_add) ": %s : %s (%d)\n",
386 rs->sr_text, mdb_strerror(rs->sr_err), rs->sr_err );
387 rs->sr_err = LDAP_OTHER;
393 Debug(LDAP_DEBUG_TRACE,
394 LDAP_XSTRING(mdb_add) ": added%s id=%08lx dn=\"%s\"\n",
395 op->o_noop ? " (no-op)" : "",
396 op->ora_e->e_id, op->ora_e->e_dn );
399 if( num_ctrls ) rs->sr_ctrls = ctrls;
402 success = rs->sr_err;
403 send_ldap_result( op, rs );
405 if( moi == &opinfo ) {
407 mdb_txn_abort( txn );
409 if ( opinfo.moi_oe.oe_key ) {
410 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
414 if( success == LDAP_SUCCESS ) {
416 if ( mdb->bi_txn_cp_kbyte ) {
417 TXN_CHECKPOINT( mdb->bi_dbenv,
418 mdb->bi_txn_cp_kbyte, mdb->bi_txn_cp_min, 0 );
423 slap_graduate_commit_csn( op );
425 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
426 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
427 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );