add.c 10.4 KB
Newer Older
1
2
3
4
/* OpenLDAP WiredTiger backend */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
5
 * Copyright 2002-2021 The OpenLDAP Foundation.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was developed by HAMANO Tsukasa <hamano@osstech.co.jp>
 * based on back-bdb for inclusion in OpenLDAP Software.
 * WiredTiger is a product of MongoDB Inc.
 */

#include "portable.h"

#include <stdio.h>
#include "back-wt.h"
#include "config.h"

int
wt_add( Operation *op, SlapReply *rs )
{
    struct wt_info *wi = (struct wt_info *) op->o_bd->be_private;
	struct berval   pdn;
	char textbuf[SLAP_TEXT_BUFLEN];
	size_t textlen = sizeof textbuf;
	AttributeDescription *children = slap_schema.si_ad_children;
	AttributeDescription *entry = slap_schema.si_ad_entry;
	ID eid;
	int num_retries = 0;
	int success;
	LDAPControl **postread_ctrl = NULL;
	LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
	int num_ctrls = 0;
	wt_ctx *wc;
	Entry *e = NULL;
	Entry *p = NULL;
	ID pid;
	int rc;

    Debug( LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(wt_add) ": %s\n",
50
		   op->ora_e->e_name.bv_val );
51
52
53
54
55
56
57
58
59
60
61

	ctrls[num_ctrls] = 0;

	/* check entry's schema */
	rs->sr_err = entry_schema_check(
		op, op->ora_e, NULL,
		get_relax(op), 1, NULL, &rs->sr_text, textbuf, textlen );
    if ( rs->sr_err != LDAP_SUCCESS ) {
        Debug( LDAP_DEBUG_TRACE,
			   LDAP_XSTRING(wt_add)
			   ": entry failed schema check: %s (%d)\n",
62
			   rs->sr_text, rs->sr_err );
63
64
65
66
67
68
69
70
71
72
        goto return_results;
    }

    /* add opattrs to shadow as well, only missing attrs will actually
     * be added; helps compatibility with older OL versions */
    rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
    if ( rs->sr_err != LDAP_SUCCESS ) {
        Debug( LDAP_DEBUG_TRACE,
			   LDAP_XSTRING(wt_add)
			   ": entry failed op attrs add: %s (%d)\n",
73
			   rs->sr_text, rs->sr_err );
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
        goto return_results;
    }

    if ( get_assert( op ) &&
		 ( test_filter( op, op->ora_e, get_assertion( op ))
		   != LDAP_COMPARE_TRUE ))
    {
        rs->sr_err = LDAP_ASSERTION_FAILED;
        goto return_results;
    }

	/* Not used
	 * subentry = is_entry_subentry( op->ora_e );
	 */

    /*
     * Get the parent dn and see if the corresponding entry exists.
     */
    if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
        pdn = slap_empty_bv;
    } else {
        dnParent( &op->ora_e->e_nname, &pdn );
    }

	wc = wt_ctx_get(op, wi);
	if( !wc ){
        Debug( LDAP_DEBUG_ANY,
			   LDAP_XSTRING(wt_add)
102
			   ": wt_ctx_get failed\n" );
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
		rs->sr_err = LDAP_OTHER;
		rs->sr_text = "internal error";
        send_ldap_result( op, rs );
        return rs->sr_err;
	}

	rc = wt_dn2entry(op->o_bd, wc, &op->o_req_ndn, &e);
	switch( rc ) {
	case 0:
		rs->sr_err = LDAP_ALREADY_EXISTS;
		goto return_results;
		break;
	case WT_NOTFOUND:
		break;
	default:
		/* TODO: retry handling */
        Debug( LDAP_DEBUG_ANY,
			   LDAP_XSTRING(wt_add)
			   ": error at wt_dn2entry() rc=%d\n",
122
			   rc );
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
		rs->sr_err = LDAP_OTHER;
		rs->sr_text = "internal error";
		goto return_results;
	}

    /* get parent entry */
	rc = wt_dn2pentry(op->o_bd, wc, &op->o_req_ndn, &p);
	switch( rc ){
	case 0:
	case WT_NOTFOUND:
		break;
	default:
        Debug( LDAP_DEBUG_ANY,
			   LDAP_XSTRING(wt_add)
			   ": error at wt_dn2pentry() rc=%d\n",
138
			   rc );
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
		rs->sr_err = LDAP_OTHER;
		rs->sr_text = "internal error";
		goto return_results;
	}

	if ( !p )
		p = (Entry *)&slap_entry_root;

	if ( !bvmatch( &pdn, &p->e_nname ) ) {
		rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
									   op->o_tmpmemctx );
		if ( p != (Entry *)&slap_entry_root ) {
			rs->sr_ref = is_entry_referral( p )
				? get_entry_referrals( op, p )
				: NULL;
			wt_entry_return( p );
		} else {
			rs->sr_ref = NULL;
		}
		p = NULL;
        Debug( LDAP_DEBUG_TRACE,
			   LDAP_XSTRING(wt_add) ": parent "
161
			   "does not exist\n" );
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
        rs->sr_err = LDAP_REFERRAL;
        rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
        goto return_results;
	}

	rs->sr_err = access_allowed( op, p,
								 children, NULL, ACL_WADD, NULL );
	if ( ! rs->sr_err ) {
		/*
		if ( p != (Entry *)&slap_entry_root )
			wt_entry_return( op, p );
		*/
		p = NULL;

		Debug( LDAP_DEBUG_TRACE,
177
			   LDAP_XSTRING(wt_add) ": no write access to parent\n" );
178
179
180
181
182
183
184
185
186
187
188
		rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
		rs->sr_text = "no write access to parent";
		goto return_results;;
	}

	if ( p != (Entry *)&slap_entry_root ) {
		if ( is_entry_subentry( p ) ) {
			wt_entry_return( p );
			p = NULL;
			/* parent is a subentry, don't allow add */
			Debug( LDAP_DEBUG_TRACE,
189
				   LDAP_XSTRING(wt_add) ": parent is subentry\n" );
190
191
192
193
194
195
196
197
198
199
			rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
			rs->sr_text = "parent is a subentry";
			goto return_results;;
		}

		if ( is_entry_alias( p ) ) {
			wt_entry_return( p );
			p = NULL;
			/* parent is an alias, don't allow add */
			Debug( LDAP_DEBUG_TRACE,
200
				   LDAP_XSTRING(wt_add) ": parent is alias\n" );
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
			rs->sr_err = LDAP_ALIAS_PROBLEM;
			rs->sr_text = "parent is an alias";
			goto return_results;;
		}

		if ( is_entry_referral( p ) ) {
			BerVarray ref = get_entry_referrals( op, p );
			/* parent is a referral, don't allow add */
			rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
										   op->o_tmpmemctx );
			rs->sr_ref = referral_rewrite( ref, &p->e_name,
										   &op->o_req_dn, LDAP_SCOPE_DEFAULT );
			ber_bvarray_free( ref );
			wt_entry_return( p );
			p = NULL;
			Debug( LDAP_DEBUG_TRACE,
217
				   LDAP_XSTRING(wt_add) ": parent is referral\n" );
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

			rs->sr_err = LDAP_REFERRAL;
			rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
			goto return_results;
		}
	}

#if 0
	if ( subentry ) {
		/* FIXME: */
		/* parent must be an administrative point of the required kind */
	}
#endif

	/* free parent */
	if ( p != (Entry *)&slap_entry_root ) {
		pid = p->e_id;
		if ( p->e_nname.bv_len ) {
			struct berval ppdn;

			/* ITS#5326: use parent's DN if differs from provided one */
			dnParent( &op->ora_e->e_name, &ppdn );
			if ( !dn_match( &p->e_name, &ppdn ) ) {
				struct berval rdn;
				struct berval newdn;

				dnRdn( &op->ora_e->e_name, &rdn );

				build_new_dn( &newdn, &p->e_name, &rdn, NULL );
				if ( op->ora_e->e_name.bv_val != op->o_req_dn.bv_val )
					ber_memfree( op->ora_e->e_name.bv_val );
				op->ora_e->e_name = newdn;

				/* FIXME: should check whether
                 * dnNormalize(newdn) == e->e_nname ... */
			}
		}

		wt_entry_return( p );
	}
	p = NULL;

	rs->sr_err = access_allowed( op, op->ora_e,
								 entry, NULL, ACL_WADD, NULL );

	if ( ! rs->sr_err ) {
		Debug( LDAP_DEBUG_TRACE,
265
			   LDAP_XSTRING(wt_add) ": no write access to entry\n" );
266
267
268
269
270
271
272
273
274
275
		rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
		rs->sr_text = "no write access to entry";
		goto return_results;
	}

	/*
	 * Check ACL for attribute write access
	 */
	if (!acl_check_modlist(op, op->ora_e, op->ora_modlist)) {
		Debug( LDAP_DEBUG_TRACE,
276
			   LDAP_XSTRING(wt_add) ": no write access to attribute\n" );
277
278
279
280
281
282
283
284
285
		rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
		rs->sr_text = "no write access to attribute";
		goto return_results;
	}

	rc = wc->session->begin_transaction(wc->session, NULL);
	if( rc ) {
		Debug( LDAP_DEBUG_TRACE,
			   LDAP_XSTRING(wt_add) ": begin_transaction failed: %s (%d)\n",
286
			   wiredtiger_strerror(rc), rc );
287
288
289
290
291
		rs->sr_err = LDAP_OTHER;
		rs->sr_text = "begin_transaction failed";
		goto return_results;
	}
	Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(wt_add) ": session id: %p\n",
292
		   wc->session );
293
294
295
296
297
298
299
300
301

	wt_next_id( op->o_bd, &eid );
	op->ora_e->e_id = eid;

	rc = wt_dn2id_add( op, wc->session, pid, op->ora_e );
	if( rc ){
		Debug( LDAP_DEBUG_TRACE,
			   LDAP_XSTRING(wt_add)
			   ": dn2id_add failed: %s (%d)\n",
302
			   wiredtiger_strerror(rc), rc );
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
		switch( rc ) {
		case WT_DUPLICATE_KEY:
			rs->sr_err = LDAP_ALREADY_EXISTS;
			break;
		default:
			rs->sr_err = LDAP_OTHER;
		}
		wc->session->rollback_transaction(wc->session, NULL);
		goto return_results;
	}

	rc = wt_id2entry_add( op, wc->session, op->ora_e );
	if ( rc ) {
		Debug( LDAP_DEBUG_TRACE,
			   LDAP_XSTRING(wt_add)
			   ": id2entry_add failed: %s (%d)\n",
319
			   wiredtiger_strerror(rc), rc );
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
		if ( rc == LDAP_ADMINLIMIT_EXCEEDED ) {
			rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
			rs->sr_text = "entry is too big";
		} else {
			rs->sr_err = LDAP_OTHER;
			rs->sr_text = "entry store failed";
		}
		wc->session->rollback_transaction(wc->session, NULL);
		goto return_results;
	}

	/* add indices */
	rc = wt_index_entry_add( op, wc, op->ora_e );
	if ( rc ) {
		Debug(LDAP_DEBUG_TRACE,
			  "<== " LDAP_XSTRING(wt_add)
			  ": index add failed: %s (%d)\n",
337
			  wiredtiger_strerror(rc), rc );
338
339
340
341
342
343
344
345
346
347
348
		rs->sr_err = LDAP_OTHER;
		rs->sr_text = "index add failed";
		wc->session->rollback_transaction(wc->session, NULL);
		goto return_results;
	}

	rc = wc->session->commit_transaction(wc->session, NULL);
	if( rc ) {
		Debug( LDAP_DEBUG_TRACE,
			   "<== " LDAP_XSTRING(wt_add)
			   ": commit_transaction failed: %s (%d)\n",
349
			   wiredtiger_strerror(rc), rc );
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
		rs->sr_err = LDAP_OTHER;
		rs->sr_text = "commit_transaction failed";
		goto return_results;
	}

	rs->sr_err = LDAP_SUCCESS;

	/* post-read */
	if( op->o_postread ) {
		if( postread_ctrl == NULL ) {
			postread_ctrl = &ctrls[num_ctrls++];
			ctrls[num_ctrls] = NULL;
		}
		if ( slap_read_controls( op, rs, op->ora_e,
								 &slap_post_read_bv, postread_ctrl ) )
		{
			Debug( LDAP_DEBUG_TRACE,
				   "<=- " LDAP_XSTRING(wt_add) ": post-read "
368
				   "failed!\n" );
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
			if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
				/* FIXME: is it correct to abort
                 * operation if control fails? */
				goto return_results;
			}
		}
	}

	Debug(LDAP_DEBUG_TRACE,
		  LDAP_XSTRING(wt_add) ": added%s id=%08lx dn=\"%s\"\n",
		  op->o_noop ? " (no-op)" : "",
		  op->ora_e->e_id, op->ora_e->e_dn );

return_results:
	success = rs->sr_err;
	send_ldap_result( op, rs );

	slap_graduate_commit_csn( op );

	if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
        slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
        slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
    }
    return rs->sr_err;
}

/*
 * Local variables:
 * indent-tabs-mode: t
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 */