1 /* add.c - ldap mdb back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2015 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];
47 Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(mdb_add) ": %s\n",
48 op->ora_e->e_name.bv_val, 0, 0);
51 if( op->o_txnSpec && txn_preop( op, rs ))
57 /* check entry's schema */
58 rs->sr_err = entry_schema_check( op, op->ora_e, NULL,
59 get_relax(op), 1, NULL, &rs->sr_text, textbuf, textlen );
60 if ( rs->sr_err != LDAP_SUCCESS ) {
61 Debug( LDAP_DEBUG_TRACE,
62 LDAP_XSTRING(mdb_add) ": entry failed schema check: "
63 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
67 /* begin transaction */
68 rs->sr_err = mdb_opinfo_get( op, mdb, 0, &moi );
70 if( rs->sr_err != 0 ) {
71 Debug( LDAP_DEBUG_TRACE,
72 LDAP_XSTRING(mdb_add) ": txn_begin failed: %s (%d)\n",
73 mdb_strerror(rs->sr_err), rs->sr_err, 0 );
74 rs->sr_err = LDAP_OTHER;
75 rs->sr_text = "internal error";
80 /* add opattrs to shadow as well, only missing attrs will actually
81 * be added; helps compatibility with older OL versions */
82 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
83 if ( rs->sr_err != LDAP_SUCCESS ) {
84 Debug( LDAP_DEBUG_TRACE,
85 LDAP_XSTRING(mdb_add) ": entry failed op attrs add: "
86 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
90 if ( get_assert( op ) &&
91 ( test_filter( op, op->ora_e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
93 rs->sr_err = LDAP_ASSERTION_FAILED;
97 subentry = is_entry_subentry( op->ora_e );
100 * Get the parent dn and see if the corresponding entry exists.
102 if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
105 dnParent( &op->ora_e->e_nname, &pdn );
108 rs->sr_err = mdb_cursor_open( txn, mdb->mi_dn2id, &mcd );
109 if( rs->sr_err != 0 ) {
110 Debug( LDAP_DEBUG_TRACE,
111 LDAP_XSTRING(mdb_add) ": mdb_cursor_open failed (%d)\n",
113 rs->sr_err = LDAP_OTHER;
114 rs->sr_text = "internal error";
118 /* get entry or parent */
119 rs->sr_err = mdb_dn2entry( op, txn, mcd, &op->ora_e->e_nname, &p, NULL, 1 );
120 switch( rs->sr_err ) {
122 rs->sr_err = LDAP_ALREADY_EXISTS;
123 mdb_entry_return( op, p );
129 rs->sr_text = "ldap server busy";
132 rs->sr_err = LDAP_OTHER;
133 rs->sr_text = "internal error";
138 p = (Entry *)&slap_entry_root;
140 if ( !bvmatch( &pdn, &p->e_nname ) ) {
141 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
143 if ( p != (Entry *)&slap_entry_root && is_entry_referral( p )) {
144 BerVarray ref = get_entry_referrals( op, p );
145 rs->sr_ref = referral_rewrite( ref, &p->e_name,
146 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
147 ber_bvarray_free( ref );
151 if ( p != (Entry *)&slap_entry_root )
152 mdb_entry_return( op, p );
154 Debug( LDAP_DEBUG_TRACE,
155 LDAP_XSTRING(mdb_add) ": parent "
156 "does not exist\n", 0, 0, 0 );
158 rs->sr_err = LDAP_REFERRAL;
159 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
163 rs->sr_err = access_allowed( op, p,
164 children, NULL, ACL_WADD, NULL );
166 if ( ! rs->sr_err ) {
167 if ( p != (Entry *)&slap_entry_root )
168 mdb_entry_return( op, p );
171 Debug( LDAP_DEBUG_TRACE,
172 LDAP_XSTRING(mdb_add) ": no write access to parent\n",
174 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
175 rs->sr_text = "no write access to parent";
176 goto return_results;;
179 if ( p != (Entry *)&slap_entry_root ) {
180 if ( is_entry_subentry( p ) ) {
181 mdb_entry_return( op, p );
183 /* parent is a subentry, don't allow add */
184 Debug( LDAP_DEBUG_TRACE,
185 LDAP_XSTRING(mdb_add) ": parent is subentry\n",
187 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
188 rs->sr_text = "parent is a subentry";
189 goto return_results;;
192 if ( is_entry_alias( p ) ) {
193 mdb_entry_return( op, p );
195 /* parent is an alias, don't allow add */
196 Debug( LDAP_DEBUG_TRACE,
197 LDAP_XSTRING(mdb_add) ": parent is alias\n",
199 rs->sr_err = LDAP_ALIAS_PROBLEM;
200 rs->sr_text = "parent is an alias";
201 goto return_results;;
204 if ( is_entry_referral( p ) ) {
205 BerVarray ref = get_entry_referrals( op, p );
206 /* parent is a referral, don't allow add */
207 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
209 rs->sr_ref = referral_rewrite( ref, &p->e_name,
210 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
211 ber_bvarray_free( ref );
212 mdb_entry_return( op, p );
214 Debug( LDAP_DEBUG_TRACE,
215 LDAP_XSTRING(mdb_add) ": parent is referral\n",
218 rs->sr_err = LDAP_REFERRAL;
219 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
227 /* parent must be an administrative point of the required kind */
231 if ( p != (Entry *)&slap_entry_root ) {
233 if ( p->e_nname.bv_len ) {
236 /* ITS#5326: use parent's DN if differs from provided one */
237 dnParent( &op->ora_e->e_name, &ppdn );
238 if ( !dn_match( &p->e_name, &ppdn ) ) {
242 dnRdn( &op->ora_e->e_name, &rdn );
244 build_new_dn( &newdn, &p->e_name, &rdn, NULL );
245 if ( op->ora_e->e_name.bv_val != op->o_req_dn.bv_val )
246 ber_memfree( op->ora_e->e_name.bv_val );
247 op->ora_e->e_name = newdn;
249 /* FIXME: should check whether
250 * dnNormalize(newdn) == e->e_nname ... */
254 mdb_entry_return( op, p );
258 rs->sr_err = access_allowed( op, op->ora_e,
259 entry, NULL, ACL_WADD, NULL );
261 if ( ! rs->sr_err ) {
262 Debug( LDAP_DEBUG_TRACE,
263 LDAP_XSTRING(mdb_add) ": no write access to entry\n",
265 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
266 rs->sr_text = "no write access to entry";
267 goto return_results;;
271 * Check ACL for attribute write access
273 if (!acl_check_modlist(op, oe, op->ora_modlist)) {
274 Debug( LDAP_DEBUG_TRACE,
275 LDAP_XSTRING(mdb_add) ": no write access to attribute\n",
277 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
278 rs->sr_text = "no write access to attribute";
279 goto return_results;;
282 rs->sr_err = mdb_cursor_open( txn, mdb->mi_id2entry, &mc );
283 if( rs->sr_err != 0 ) {
284 Debug( LDAP_DEBUG_TRACE,
285 LDAP_XSTRING(mdb_add) ": mdb_cursor_open failed (%d)\n",
287 rs->sr_err = LDAP_OTHER;
288 rs->sr_text = "internal error";
292 rs->sr_err = mdb_next_id( op->o_bd, mc, &eid );
293 if( rs->sr_err != 0 ) {
294 Debug( LDAP_DEBUG_TRACE,
295 LDAP_XSTRING(mdb_add) ": next_id failed (%d)\n",
297 rs->sr_err = LDAP_OTHER;
298 rs->sr_text = "internal error";
301 op->ora_e->e_id = eid;
304 rs->sr_err = mdb_dn2id_add( op, mcd, mcd, pid, 1, 1, op->ora_e );
305 mdb_cursor_close( mcd );
306 if ( rs->sr_err != 0 ) {
307 Debug( LDAP_DEBUG_TRACE,
308 LDAP_XSTRING(mdb_add) ": dn2id_add failed: %s (%d)\n",
309 mdb_strerror(rs->sr_err), rs->sr_err, 0 );
311 switch( rs->sr_err ) {
313 rs->sr_err = LDAP_ALREADY_EXISTS;
316 rs->sr_err = LDAP_OTHER;
321 /* attribute indexes */
322 rs->sr_err = mdb_index_entry_add( op, txn, op->ora_e );
323 if ( rs->sr_err != LDAP_SUCCESS ) {
324 Debug( LDAP_DEBUG_TRACE,
325 LDAP_XSTRING(mdb_add) ": index_entry_add failed\n",
327 rs->sr_err = LDAP_OTHER;
328 rs->sr_text = "index generation failed";
333 rs->sr_err = mdb_id2entry_add( op, txn, mc, op->ora_e );
334 if ( rs->sr_err != 0 ) {
335 Debug( LDAP_DEBUG_TRACE,
336 LDAP_XSTRING(mdb_add) ": id2entry_add failed\n",
338 if ( rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ) {
339 rs->sr_text = "entry is too big";
341 rs->sr_err = LDAP_OTHER;
342 rs->sr_text = "entry store failed";
348 if( op->o_postread ) {
349 if( postread_ctrl == NULL ) {
350 postread_ctrl = &ctrls[num_ctrls++];
351 ctrls[num_ctrls] = NULL;
353 if ( slap_read_controls( op, rs, op->ora_e,
354 &slap_post_read_bv, postread_ctrl ) )
356 Debug( LDAP_DEBUG_TRACE,
357 "<=- " LDAP_XSTRING(mdb_add) ": post-read "
358 "failed!\n", 0, 0, 0 );
359 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
360 /* FIXME: is it correct to abort
361 * operation if control fails? */
367 if ( moi == &opinfo ) {
368 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
369 opinfo.moi_oe.oe_key = NULL;
371 mdb_txn_abort( txn );
372 rs->sr_err = LDAP_X_NO_OPERATION;
377 rs->sr_err = mdb_txn_commit( txn );
379 if ( rs->sr_err != 0 ) {
380 rs->sr_text = "txn_commit failed";
381 Debug( LDAP_DEBUG_ANY,
382 LDAP_XSTRING(mdb_add) ": %s : %s (%d)\n",
383 rs->sr_text, mdb_strerror(rs->sr_err), rs->sr_err );
384 rs->sr_err = LDAP_OTHER;
389 Debug(LDAP_DEBUG_TRACE,
390 LDAP_XSTRING(mdb_add) ": added%s id=%08lx dn=\"%s\"\n",
391 op->o_noop ? " (no-op)" : "",
392 op->ora_e->e_id, op->ora_e->e_dn );
395 if( num_ctrls ) rs->sr_ctrls = ctrls;
398 success = rs->sr_err;
399 send_ldap_result( op, rs );
401 if( moi == &opinfo ) {
403 mdb_txn_abort( txn );
405 if ( opinfo.moi_oe.oe_key ) {
406 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
412 if( success == LDAP_SUCCESS ) {
414 if ( mdb->bi_txn_cp_kbyte ) {
415 TXN_CHECKPOINT( mdb->bi_dbenv,
416 mdb->bi_txn_cp_kbyte, mdb->bi_txn_cp_min, 0 );
421 slap_graduate_commit_csn( op );
423 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
424 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
425 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );