1 /* OpenLDAP WiredTiger backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2002-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>.
17 * This work was developed by HAMANO Tsukasa <hamano@osstech.co.jp>
18 * based on back-bdb for inclusion in OpenLDAP Software.
19 * WiredTiger is a product of MongoDB Inc.
29 wt_add( Operation *op, SlapReply *rs )
31 struct wt_info *wi = (struct wt_info *) op->o_bd->be_private;
33 char textbuf[SLAP_TEXT_BUFLEN];
34 size_t textlen = sizeof textbuf;
35 AttributeDescription *children = slap_schema.si_ad_children;
36 AttributeDescription *entry = slap_schema.si_ad_entry;
40 LDAPControl **postread_ctrl = NULL;
41 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
49 Debug( LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(wt_add) ": %s\n",
50 op->ora_e->e_name.bv_val, 0, 0);
54 /* check entry's schema */
55 rs->sr_err = entry_schema_check(
57 get_relax(op), 1, NULL, &rs->sr_text, textbuf, textlen );
58 if ( rs->sr_err != LDAP_SUCCESS ) {
59 Debug( LDAP_DEBUG_TRACE,
61 ": entry failed schema check: %s (%d)\n",
62 rs->sr_text, rs->sr_err, 0 );
66 /* add opattrs to shadow as well, only missing attrs will actually
67 * be added; helps compatibility with older OL versions */
68 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
69 if ( rs->sr_err != LDAP_SUCCESS ) {
70 Debug( LDAP_DEBUG_TRACE,
72 ": entry failed op attrs add: %s (%d)\n",
73 rs->sr_text, rs->sr_err, 0 );
77 if ( get_assert( op ) &&
78 ( test_filter( op, op->ora_e, get_assertion( op ))
79 != LDAP_COMPARE_TRUE ))
81 rs->sr_err = LDAP_ASSERTION_FAILED;
86 * subentry = is_entry_subentry( op->ora_e );
90 * Get the parent dn and see if the corresponding entry exists.
92 if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
95 dnParent( &op->ora_e->e_nname, &pdn );
98 wc = wt_ctx_get(op, wi);
100 Debug( LDAP_DEBUG_ANY,
102 ": wt_ctx_get failed\n",
104 rs->sr_err = LDAP_OTHER;
105 rs->sr_text = "internal error";
106 send_ldap_result( op, rs );
110 rc = wt_dn2entry(op->o_bd, wc, &op->o_req_ndn, &e);
113 rs->sr_err = LDAP_ALREADY_EXISTS;
119 /* TODO: retry handling */
120 Debug( LDAP_DEBUG_ANY,
122 ": error at wt_dn2entry() rc=%d\n",
124 rs->sr_err = LDAP_OTHER;
125 rs->sr_text = "internal error";
129 /* get parent entry */
130 rc = wt_dn2pentry(op->o_bd, wc, &op->o_req_ndn, &p);
136 Debug( LDAP_DEBUG_ANY,
138 ": error at wt_dn2pentry() rc=%d\n",
140 rs->sr_err = LDAP_OTHER;
141 rs->sr_text = "internal error";
146 p = (Entry *)&slap_entry_root;
148 if ( !bvmatch( &pdn, &p->e_nname ) ) {
149 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
151 if ( p != (Entry *)&slap_entry_root ) {
152 rs->sr_ref = is_entry_referral( p )
153 ? get_entry_referrals( op, p )
155 wt_entry_return( p );
160 Debug( LDAP_DEBUG_TRACE,
161 LDAP_XSTRING(wt_add) ": parent "
162 "does not exist\n", 0, 0, 0 );
163 rs->sr_err = LDAP_REFERRAL;
164 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
168 rs->sr_err = access_allowed( op, p,
169 children, NULL, ACL_WADD, NULL );
170 if ( ! rs->sr_err ) {
172 if ( p != (Entry *)&slap_entry_root )
173 wt_entry_return( op, p );
177 Debug( LDAP_DEBUG_TRACE,
178 LDAP_XSTRING(wt_add) ": no write access to parent\n",
180 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
181 rs->sr_text = "no write access to parent";
182 goto return_results;;
185 if ( p != (Entry *)&slap_entry_root ) {
186 if ( is_entry_subentry( p ) ) {
187 wt_entry_return( p );
189 /* parent is a subentry, don't allow add */
190 Debug( LDAP_DEBUG_TRACE,
191 LDAP_XSTRING(wt_add) ": parent is subentry\n",
193 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
194 rs->sr_text = "parent is a subentry";
195 goto return_results;;
198 if ( is_entry_alias( p ) ) {
199 wt_entry_return( p );
201 /* parent is an alias, don't allow add */
202 Debug( LDAP_DEBUG_TRACE,
203 LDAP_XSTRING(wt_add) ": parent is alias\n",
205 rs->sr_err = LDAP_ALIAS_PROBLEM;
206 rs->sr_text = "parent is an alias";
207 goto return_results;;
210 if ( is_entry_referral( p ) ) {
211 BerVarray ref = get_entry_referrals( op, p );
212 /* parent is a referral, don't allow add */
213 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
215 rs->sr_ref = referral_rewrite( ref, &p->e_name,
216 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
217 ber_bvarray_free( ref );
218 wt_entry_return( p );
220 Debug( LDAP_DEBUG_TRACE,
221 LDAP_XSTRING(wt_add) ": parent is referral\n",
224 rs->sr_err = LDAP_REFERRAL;
225 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
233 /* parent must be an administrative point of the required kind */
238 if ( p != (Entry *)&slap_entry_root ) {
240 if ( p->e_nname.bv_len ) {
243 /* ITS#5326: use parent's DN if differs from provided one */
244 dnParent( &op->ora_e->e_name, &ppdn );
245 if ( !dn_match( &p->e_name, &ppdn ) ) {
249 dnRdn( &op->ora_e->e_name, &rdn );
251 build_new_dn( &newdn, &p->e_name, &rdn, NULL );
252 if ( op->ora_e->e_name.bv_val != op->o_req_dn.bv_val )
253 ber_memfree( op->ora_e->e_name.bv_val );
254 op->ora_e->e_name = newdn;
256 /* FIXME: should check whether
257 * dnNormalize(newdn) == e->e_nname ... */
261 wt_entry_return( p );
265 rs->sr_err = access_allowed( op, op->ora_e,
266 entry, NULL, ACL_WADD, NULL );
268 if ( ! rs->sr_err ) {
269 Debug( LDAP_DEBUG_TRACE,
270 LDAP_XSTRING(wt_add) ": no write access to entry\n",
272 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
273 rs->sr_text = "no write access to entry";
278 * Check ACL for attribute write access
280 if (!acl_check_modlist(op, op->ora_e, op->ora_modlist)) {
281 Debug( LDAP_DEBUG_TRACE,
282 LDAP_XSTRING(wt_add) ": no write access to attribute\n",
284 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
285 rs->sr_text = "no write access to attribute";
289 rc = wc->session->begin_transaction(wc->session, NULL);
291 Debug( LDAP_DEBUG_TRACE,
292 LDAP_XSTRING(wt_add) ": begin_transaction failed: %s (%d)\n",
293 wiredtiger_strerror(rc), rc, 0 );
294 rs->sr_err = LDAP_OTHER;
295 rs->sr_text = "begin_transaction failed";
298 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(wt_add) ": session id: %p\n",
301 wt_next_id( op->o_bd, &eid );
302 op->ora_e->e_id = eid;
304 rc = wt_dn2id_add( op, wc->session, pid, op->ora_e );
306 Debug( LDAP_DEBUG_TRACE,
308 ": dn2id_add failed: %s (%d)\n",
309 wiredtiger_strerror(rc), rc, 0 );
311 case WT_DUPLICATE_KEY:
312 rs->sr_err = LDAP_ALREADY_EXISTS;
315 rs->sr_err = LDAP_OTHER;
317 wc->session->rollback_transaction(wc->session, NULL);
321 rc = wt_id2entry_add( op, wc->session, op->ora_e );
323 Debug( LDAP_DEBUG_TRACE,
325 ": id2entry_add failed: %s (%d)\n",
326 wiredtiger_strerror(rc), rc, 0 );
327 if ( rc == LDAP_ADMINLIMIT_EXCEEDED ) {
328 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
329 rs->sr_text = "entry is too big";
331 rs->sr_err = LDAP_OTHER;
332 rs->sr_text = "entry store failed";
334 wc->session->rollback_transaction(wc->session, NULL);
339 rc = wt_index_entry_add( op, wc, op->ora_e );
341 Debug(LDAP_DEBUG_TRACE,
342 "<== " LDAP_XSTRING(wt_add)
343 ": index add failed: %s (%d)\n",
344 wiredtiger_strerror(rc), rc, 0 );
345 rs->sr_err = LDAP_OTHER;
346 rs->sr_text = "index add failed";
347 wc->session->rollback_transaction(wc->session, NULL);
351 rc = wc->session->commit_transaction(wc->session, NULL);
353 Debug( LDAP_DEBUG_TRACE,
354 "<== " LDAP_XSTRING(wt_add)
355 ": commit_transaction failed: %s (%d)\n",
356 wiredtiger_strerror(rc), rc, 0 );
357 rs->sr_err = LDAP_OTHER;
358 rs->sr_text = "commit_transaction failed";
362 rs->sr_err = LDAP_SUCCESS;
365 if( op->o_postread ) {
366 if( postread_ctrl == NULL ) {
367 postread_ctrl = &ctrls[num_ctrls++];
368 ctrls[num_ctrls] = NULL;
370 if ( slap_read_controls( op, rs, op->ora_e,
371 &slap_post_read_bv, postread_ctrl ) )
373 Debug( LDAP_DEBUG_TRACE,
374 "<=- " LDAP_XSTRING(wt_add) ": post-read "
375 "failed!\n", 0, 0, 0 );
376 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
377 /* FIXME: is it correct to abort
378 * operation if control fails? */
384 Debug(LDAP_DEBUG_TRACE,
385 LDAP_XSTRING(wt_add) ": added%s id=%08lx dn=\"%s\"\n",
386 op->o_noop ? " (no-op)" : "",
387 op->ora_e->e_id, op->ora_e->e_dn );
390 success = rs->sr_err;
391 send_ldap_result( op, rs );
393 slap_graduate_commit_csn( op );
395 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
396 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
397 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
404 * indent-tabs-mode: t