Debian Codegraph @

servers/slapd/back-wt/add.c

raw
1 /* OpenLDAP WiredTiger backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2002-2024 The OpenLDAP Foundation.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
11 *
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>.
15 */
16 /* ACKNOWLEDGEMENTS:
17 * This work was developed by HAMANO Tsukasa <[email protected]>
18 * based on back-bdb for inclusion in OpenLDAP Software.
19 * WiredTiger is a product of MongoDB Inc.
20 */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include "back-wt.h"
26 #include "slap-config.h"
27
28 int
29 wt_add( Operation *op, SlapReply *rs )
30 {
31 struct wt_info *wi = (struct wt_info *) op->o_bd->be_private;
32 struct berval pdn;
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;
37 ID eid = NOID;
38 LDAPControl **postread_ctrl = NULL;
39 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
40 int num_ctrls = 0;
41 wt_ctx *wc;
42 Entry *e = NULL;
43 Entry *p = NULL;
44 ID pid = NOID;
45 int rc;
46
47 Debug( LDAP_DEBUG_ARGS, "==> wt_add: %s\n", op->ora_e->e_name.bv_val );
48
49 ctrls[num_ctrls] = 0;
50
51 /* check entry's schema */
52 rs->sr_err = entry_schema_check(
53 op, op->ora_e, NULL,
54 get_relax(op), 1, NULL, &rs->sr_text, textbuf, textlen );
55 if ( rs->sr_err != LDAP_SUCCESS ) {
56 Debug( LDAP_DEBUG_TRACE,
57 "wt_add: entry failed schema check: %s (%d)\n",
58 rs->sr_text, rs->sr_err );
59 goto return_results;
60 }
61
62 /* add opattrs to shadow as well, only missing attrs will actually
63 * be added; helps compatibility with older OL versions */
64 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
65 if ( rs->sr_err != LDAP_SUCCESS ) {
66 Debug( LDAP_DEBUG_TRACE,
67 "wt_add: entry failed op attrs add: %s (%d)\n",
68 rs->sr_text, rs->sr_err );
69 goto return_results;
70 }
71
72 if ( get_assert( op ) &&
73 ( test_filter( op, op->ora_e, get_assertion( op ))
74 != LDAP_COMPARE_TRUE ))
75 {
76 rs->sr_err = LDAP_ASSERTION_FAILED;
77 goto return_results;
78 }
79
80 /* Not used
81 * subentry = is_entry_subentry( op->ora_e );
82 */
83
84 /*
85 * Get the parent dn and see if the corresponding entry exists.
86 */
87 if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
88 pdn = slap_empty_bv;
89 } else {
90 dnParent( &op->ora_e->e_nname, &pdn );
91 }
92
93 wc = wt_ctx_get(op, wi);
94 if( !wc ){
95 Debug( LDAP_DEBUG_ANY, "wt_add: wt_ctx_get failed\n" );
96 rs->sr_err = LDAP_OTHER;
97 rs->sr_text = "internal error";
98 send_ldap_result( op, rs );
99 return rs->sr_err;
100 }
101
102 rc = wt_dn2entry(op->o_bd, wc, &op->o_req_ndn, &e);
103 switch( rc ) {
104 case 0:
105 rs->sr_err = LDAP_ALREADY_EXISTS;
106 goto return_results;
107 break;
108 case WT_NOTFOUND:
109 break;
110 default:
111 /* TODO: retry handling */
112 Debug( LDAP_DEBUG_ANY,
113 "wt_add: error at wt_dn2entry() rc=%d\n", rc );
114 rs->sr_err = LDAP_OTHER;
115 rs->sr_text = "internal error";
116 goto return_results;
117 }
118
119 /* get parent entry */
120 rc = wt_dn2pentry(op->o_bd, wc, &op->o_req_ndn, &p);
121 switch( rc ){
122 case 0:
123 case WT_NOTFOUND:
124 break;
125 default:
126 Debug( LDAP_DEBUG_ANY,
127 "wt_add: error at wt_dn2pentry() rc=%d\n", rc );
128 rs->sr_err = LDAP_OTHER;
129 rs->sr_text = "internal error";
130 goto return_results;
131 }
132
133 if ( !p )
134 p = (Entry *)&slap_entry_root;
135
136 if ( !bvmatch( &pdn, &p->e_nname ) ) {
137 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
138 op->o_tmpmemctx );
139 if ( p != (Entry *)&slap_entry_root ) {
140 rs->sr_ref = is_entry_referral( p )
141 ? get_entry_referrals( op, p )
142 : NULL;
143 wt_entry_return( p );
144 } else {
145 rs->sr_ref = NULL;
146 }
147 p = NULL;
148 Debug( LDAP_DEBUG_TRACE, "wt_add: parent does not exist\n" );
149 rs->sr_err = LDAP_REFERRAL;
150 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
151 goto return_results;
152 }
153
154 rs->sr_err = access_allowed( op, p,
155 children, NULL, ACL_WADD, NULL );
156 if ( ! rs->sr_err ) {
157 /*
158 if ( p != (Entry *)&slap_entry_root )
159 wt_entry_return( op, p );
160 */
161 p = NULL;
162
163 Debug( LDAP_DEBUG_TRACE, "wt_add: no write access to parent\n" );
164 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
165 rs->sr_text = "no write access to parent";
166 goto return_results;;
167 }
168
169 if ( p != (Entry *)&slap_entry_root ) {
170 if ( is_entry_subentry( p ) ) {
171 wt_entry_return( p );
172 p = NULL;
173 /* parent is a subentry, don't allow add */
174 Debug( LDAP_DEBUG_TRACE, "wt_add: parent is subentry\n" );
175 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
176 rs->sr_text = "parent is a subentry";
177 goto return_results;;
178 }
179
180 if ( is_entry_alias( p ) ) {
181 wt_entry_return( p );
182 p = NULL;
183 /* parent is an alias, don't allow add */
184 Debug( LDAP_DEBUG_TRACE, "wt_add: parent is alias\n" );
185 rs->sr_err = LDAP_ALIAS_PROBLEM;
186 rs->sr_text = "parent is an alias";
187 goto return_results;;
188 }
189
190 if ( is_entry_referral( p ) ) {
191 BerVarray ref = get_entry_referrals( op, p );
192 /* parent is a referral, don't allow add */
193 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
194 op->o_tmpmemctx );
195 rs->sr_ref = referral_rewrite( ref, &p->e_name,
196 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
197 ber_bvarray_free( ref );
198 wt_entry_return( p );
199 p = NULL;
200 Debug( LDAP_DEBUG_TRACE, "wt_add: parent is referral\n" );
201
202 rs->sr_err = LDAP_REFERRAL;
203 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
204 goto return_results;
205 }
206 }
207
208 #if 0
209 if ( subentry ) {
210 /* FIXME: */
211 /* parent must be an administrative point of the required kind */
212 }
213 #endif
214
215 /* free parent */
216 if ( p != (Entry *)&slap_entry_root ) {
217 pid = p->e_id;
218 if ( p->e_nname.bv_len ) {
219 struct berval ppdn;
220
221 /* ITS#5326: use parent's DN if differs from provided one */
222 dnParent( &op->ora_e->e_name, &ppdn );
223 if ( !dn_match( &p->e_name, &ppdn ) ) {
224 struct berval rdn;
225 struct berval newdn;
226
227 dnRdn( &op->ora_e->e_name, &rdn );
228
229 build_new_dn( &newdn, &p->e_name, &rdn, NULL );
230 if ( op->ora_e->e_name.bv_val != op->o_req_dn.bv_val )
231 ber_memfree( op->ora_e->e_name.bv_val );
232 op->ora_e->e_name = newdn;
233
234 /* FIXME: should check whether
235 * dnNormalize(newdn) == e->e_nname ... */
236 }
237 }
238
239 wt_entry_return( p );
240 }
241 p = NULL;
242
243 rs->sr_err = access_allowed( op, op->ora_e,
244 entry, NULL, ACL_WADD, NULL );
245
246 if ( ! rs->sr_err ) {
247 Debug( LDAP_DEBUG_TRACE, "wt_add: no write access to entry\n" );
248 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
249 rs->sr_text = "no write access to entry";
250 goto return_results;
251 }
252
253 /*
254 * Check ACL for attribute write access
255 */
256 if (!acl_check_modlist(op, op->ora_e, op->ora_modlist)) {
257 Debug( LDAP_DEBUG_TRACE, "wt_add: no write access to attribute\n" );
258 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
259 rs->sr_text = "no write access to attribute";
260 goto return_results;
261 }
262
263 rc = wc->session->begin_transaction(wc->session, "isolation=read-uncommitted");
264 if( rc ) {
265 Debug( LDAP_DEBUG_TRACE, "wt_add: begin_transaction failed: %s (%d)\n",
266 wiredtiger_strerror(rc), rc );
267 rs->sr_err = LDAP_OTHER;
268 rs->sr_text = "begin_transaction failed";
269 goto return_results;
270 }
271 Debug( LDAP_DEBUG_TRACE, "wt_add: session id: %p\n", wc->session );
272
273 wt_next_id( op->o_bd, &eid );
274 op->ora_e->e_id = eid;
275
276 rc = wt_dn2id_add( op, wc, pid, op->ora_e );
277 if( rc ){
278 Debug( LDAP_DEBUG_TRACE,
279 "wt_add: dn2id_add failed: %s (%d)\n",
280 wiredtiger_strerror(rc), rc );
281 switch( rc ) {
282 case WT_DUPLICATE_KEY:
283 rs->sr_err = LDAP_ALREADY_EXISTS;
284 break;
285 default:
286 rs->sr_err = LDAP_OTHER;
287 }
288 wc->session->rollback_transaction(wc->session, NULL);
289 goto return_results;
290 }
291
292 rc = wt_id2entry_add( op, wc, op->ora_e );
293 if ( rc ) {
294 Debug( LDAP_DEBUG_TRACE,
295 "wt_add: id2entry_add failed: %s (%d)\n",
296 wiredtiger_strerror(rc), rc );
297 if ( rc == LDAP_ADMINLIMIT_EXCEEDED ) {
298 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
299 rs->sr_text = "entry is too big";
300 } else {
301 rs->sr_err = LDAP_OTHER;
302 rs->sr_text = "entry store failed";
303 }
304 wc->session->rollback_transaction(wc->session, NULL);
305 goto return_results;
306 }
307
308 /* add indices */
309 rc = wt_index_entry_add( op, wc, op->ora_e );
310 if ( rc ) {
311 Debug(LDAP_DEBUG_TRACE,
312 "<== wt_add: index add failed: %s (%d)\n",
313 wiredtiger_strerror(rc), rc );
314 rs->sr_err = LDAP_OTHER;
315 rs->sr_text = "index add failed";
316 wc->session->rollback_transaction(wc->session, NULL);
317 goto return_results;
318 }
319
320 rc = wc->session->commit_transaction(wc->session, NULL);
321 if( rc ) {
322 Debug( LDAP_DEBUG_TRACE,
323 "<== wt_add: commit_transaction failed: %s (%d)\n",
324 wiredtiger_strerror(rc), rc );
325 rs->sr_err = LDAP_OTHER;
326 rs->sr_text = "commit_transaction failed";
327 goto return_results;
328 }
329
330 rs->sr_err = LDAP_SUCCESS;
331
332 /* post-read */
333 if( op->o_postread ) {
334 if( postread_ctrl == NULL ) {
335 postread_ctrl = &ctrls[num_ctrls++];
336 ctrls[num_ctrls] = NULL;
337 }
338 if ( slap_read_controls( op, rs, op->ora_e,
339 &slap_post_read_bv, postread_ctrl ) )
340 {
341 Debug( LDAP_DEBUG_TRACE, "<=- wt_add: post-read failed!\n" );
342 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
343 /* FIXME: is it correct to abort
344 * operation if control fails? */
345 goto return_results;
346 }
347 }
348 }
349
350 Debug(LDAP_DEBUG_TRACE,
351 "wt_add: added%s id=%08lx dn=\"%s\"\n",
352 op->o_noop ? " (no-op)" : "",
353 op->ora_e->e_id, op->ora_e->e_dn );
354
355 return_results:
356 send_ldap_result( op, rs );
357
358 slap_graduate_commit_csn( op );
359
360 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
361 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
362 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
363 }
364 return rs->sr_err;
365 }
366
367 /*
368 * Local variables:
369 * indent-tabs-mode: t
370 * tab-width: 4
371 * c-basic-offset: 4
372 * End:
373 */