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