1 /* add.c - ldap mdb back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2017 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;
40 int numads = mdb->mi_numads;
44 LDAPControl **postread_ctrl = NULL;
45 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
48 Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(mdb_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(mdb_add) ": entry failed schema check: "
64 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
68 /* begin transaction */
69 rs->sr_err = mdb_opinfo_get( op, mdb, 0, &moi );
71 if( rs->sr_err != 0 ) {
72 Debug( LDAP_DEBUG_TRACE,
73 LDAP_XSTRING(mdb_add) ": txn_begin failed: %s (%d)\n",
74 mdb_strerror(rs->sr_err), rs->sr_err, 0 );
75 rs->sr_err = LDAP_OTHER;
76 rs->sr_text = "internal error";
81 /* add opattrs to shadow as well, only missing attrs will actually
82 * be added; helps compatibility with older OL versions */
83 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
84 if ( rs->sr_err != LDAP_SUCCESS ) {
85 Debug( LDAP_DEBUG_TRACE,
86 LDAP_XSTRING(mdb_add) ": entry failed op attrs add: "
87 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
91 if ( get_assert( op ) &&
92 ( test_filter( op, op->ora_e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
94 rs->sr_err = LDAP_ASSERTION_FAILED;
98 subentry = is_entry_subentry( op->ora_e );
101 * Get the parent dn and see if the corresponding entry exists.
103 if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
106 dnParent( &op->ora_e->e_nname, &pdn );
109 rs->sr_err = mdb_cursor_open( txn, mdb->mi_dn2id, &mcd );
110 if( rs->sr_err != 0 ) {
111 Debug( LDAP_DEBUG_TRACE,
112 LDAP_XSTRING(mdb_add) ": mdb_cursor_open failed (%d)\n",
114 rs->sr_err = LDAP_OTHER;
115 rs->sr_text = "internal error";
119 /* get entry or parent */
120 rs->sr_err = mdb_dn2entry( op, txn, mcd, &op->ora_e->e_nname, &p, NULL, 1 );
121 switch( rs->sr_err ) {
123 rs->sr_err = LDAP_ALREADY_EXISTS;
124 mdb_entry_return( op, p );
130 rs->sr_text = "ldap server busy";
133 rs->sr_err = LDAP_OTHER;
134 rs->sr_text = "internal error";
139 p = (Entry *)&slap_entry_root;
141 if ( !bvmatch( &pdn, &p->e_nname ) ) {
142 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
144 if ( p != (Entry *)&slap_entry_root && is_entry_referral( p )) {
145 BerVarray ref = get_entry_referrals( op, p );
146 rs->sr_ref = referral_rewrite( ref, &p->e_name,
147 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
148 ber_bvarray_free( ref );
152 if ( p != (Entry *)&slap_entry_root )
153 mdb_entry_return( op, p );
155 Debug( LDAP_DEBUG_TRACE,
156 LDAP_XSTRING(mdb_add) ": parent "
157 "does not exist\n", 0, 0, 0 );
159 rs->sr_err = LDAP_REFERRAL;
160 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
164 rs->sr_err = access_allowed( op, p,
165 children, NULL, ACL_WADD, NULL );
167 if ( ! rs->sr_err ) {
168 if ( p != (Entry *)&slap_entry_root )
169 mdb_entry_return( op, p );
172 Debug( LDAP_DEBUG_TRACE,
173 LDAP_XSTRING(mdb_add) ": no write access to parent\n",
175 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
176 rs->sr_text = "no write access to parent";
177 goto return_results;;
180 if ( p != (Entry *)&slap_entry_root ) {
181 if ( is_entry_subentry( p ) ) {
182 mdb_entry_return( op, p );
184 /* parent is a subentry, don't allow add */
185 Debug( LDAP_DEBUG_TRACE,
186 LDAP_XSTRING(mdb_add) ": parent is subentry\n",
188 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
189 rs->sr_text = "parent is a subentry";
190 goto return_results;;
193 if ( is_entry_alias( p ) ) {
194 mdb_entry_return( op, p );
196 /* parent is an alias, don't allow add */
197 Debug( LDAP_DEBUG_TRACE,
198 LDAP_XSTRING(mdb_add) ": parent is alias\n",
200 rs->sr_err = LDAP_ALIAS_PROBLEM;
201 rs->sr_text = "parent is an alias";
202 goto return_results;;
205 if ( is_entry_referral( p ) ) {
206 BerVarray ref = get_entry_referrals( op, p );
207 /* parent is a referral, don't allow add */
208 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
210 rs->sr_ref = referral_rewrite( ref, &p->e_name,
211 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
212 ber_bvarray_free( ref );
213 mdb_entry_return( op, p );
215 Debug( LDAP_DEBUG_TRACE,
216 LDAP_XSTRING(mdb_add) ": parent is referral\n",
219 rs->sr_err = LDAP_REFERRAL;
220 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
228 /* parent must be an administrative point of the required kind */
232 if ( p != (Entry *)&slap_entry_root ) {
234 if ( p->e_nname.bv_len ) {
237 /* ITS#5326: use parent's DN if differs from provided one */
238 dnParent( &op->ora_e->e_name, &ppdn );
239 if ( !dn_match( &p->e_name, &ppdn ) ) {
243 dnRdn( &op->ora_e->e_name, &rdn );
245 build_new_dn( &newdn, &p->e_name, &rdn, NULL );
246 if ( op->ora_e->e_name.bv_val != op->o_req_dn.bv_val )
247 ber_memfree( op->ora_e->e_name.bv_val );
248 op->ora_e->e_name = newdn;
250 /* FIXME: should check whether
251 * dnNormalize(newdn) == e->e_nname ... */
255 mdb_entry_return( op, p );
259 rs->sr_err = access_allowed( op, op->ora_e,
260 entry, NULL, ACL_WADD, NULL );
262 if ( ! rs->sr_err ) {
263 Debug( LDAP_DEBUG_TRACE,
264 LDAP_XSTRING(mdb_add) ": no write access to entry\n",
266 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
267 rs->sr_text = "no write access to entry";
268 goto return_results;;
272 * Check ACL for attribute write access
274 if (!acl_check_modlist(op, oe, op->ora_modlist)) {
275 Debug( LDAP_DEBUG_TRACE,
276 LDAP_XSTRING(mdb_add) ": no write access to attribute\n",
278 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
279 rs->sr_text = "no write access to attribute";
280 goto return_results;;
283 rs->sr_err = mdb_cursor_open( txn, mdb->mi_id2entry, &mc );
284 if( rs->sr_err != 0 ) {
285 Debug( LDAP_DEBUG_TRACE,
286 LDAP_XSTRING(mdb_add) ": mdb_cursor_open failed (%d)\n",
288 rs->sr_err = LDAP_OTHER;
289 rs->sr_text = "internal error";
293 rs->sr_err = mdb_next_id( op->o_bd, mc, &eid );
294 if( rs->sr_err != 0 ) {
295 Debug( LDAP_DEBUG_TRACE,
296 LDAP_XSTRING(mdb_add) ": next_id failed (%d)\n",
298 rs->sr_err = LDAP_OTHER;
299 rs->sr_text = "internal error";
302 op->ora_e->e_id = eid;
305 rs->sr_err = mdb_dn2id_add( op, mcd, mcd, pid, 1, 1, op->ora_e );
306 mdb_cursor_close( mcd );
307 if ( rs->sr_err != 0 ) {
308 Debug( LDAP_DEBUG_TRACE,
309 LDAP_XSTRING(mdb_add) ": dn2id_add failed: %s (%d)\n",
310 mdb_strerror(rs->sr_err), rs->sr_err, 0 );
312 switch( rs->sr_err ) {
314 rs->sr_err = LDAP_ALREADY_EXISTS;
317 rs->sr_err = LDAP_OTHER;
322 /* attribute indexes */
323 rs->sr_err = mdb_index_entry_add( op, txn, op->ora_e );
324 if ( rs->sr_err != LDAP_SUCCESS ) {
325 Debug( LDAP_DEBUG_TRACE,
326 LDAP_XSTRING(mdb_add) ": index_entry_add failed\n",
328 rs->sr_err = LDAP_OTHER;
329 rs->sr_text = "index generation failed";
334 rs->sr_err = mdb_id2entry_add( op, txn, mc, op->ora_e );
335 if ( rs->sr_err != 0 ) {
336 Debug( LDAP_DEBUG_TRACE,
337 LDAP_XSTRING(mdb_add) ": id2entry_add failed\n",
339 if ( rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ) {
340 rs->sr_text = "entry is too big";
342 rs->sr_err = LDAP_OTHER;
343 rs->sr_text = "entry store failed";
349 if( op->o_postread ) {
350 if( postread_ctrl == NULL ) {
351 postread_ctrl = &ctrls[num_ctrls++];
352 ctrls[num_ctrls] = NULL;
354 if ( slap_read_controls( op, rs, op->ora_e,
355 &slap_post_read_bv, postread_ctrl ) )
357 Debug( LDAP_DEBUG_TRACE,
358 "<=- " LDAP_XSTRING(mdb_add) ": post-read "
359 "failed!\n", 0, 0, 0 );
360 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
361 /* FIXME: is it correct to abort
362 * operation if control fails? */
368 if ( moi == &opinfo ) {
369 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
370 opinfo.moi_oe.oe_key = NULL;
372 mdb->mi_numads = numads;
373 mdb_txn_abort( txn );
374 rs->sr_err = LDAP_X_NO_OPERATION;
379 rs->sr_err = mdb_txn_commit( txn );
381 if ( rs->sr_err != 0 ) {
382 mdb->mi_numads = numads;
383 rs->sr_text = "txn_commit failed";
384 Debug( LDAP_DEBUG_ANY,
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;
392 Debug(LDAP_DEBUG_TRACE,
393 LDAP_XSTRING(mdb_add) ": added%s id=%08lx dn=\"%s\"\n",
394 op->o_noop ? " (no-op)" : "",
395 op->ora_e->e_id, op->ora_e->e_dn );
398 if( num_ctrls ) rs->sr_ctrls = ctrls;
401 success = rs->sr_err;
402 send_ldap_result( op, rs );
404 if( moi == &opinfo ) {
406 mdb->mi_numads = numads;
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 );
416 if( success == LDAP_SUCCESS ) {
418 if ( mdb->bi_txn_cp_kbyte ) {
419 TXN_CHECKPOINT( mdb->bi_dbenv,
420 mdb->bi_txn_cp_kbyte, mdb->bi_txn_cp_min, 0 );
425 slap_graduate_commit_csn( op );
427 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
428 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
429 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );