1 /* add.c - ldap mdb back-end add routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2000-2021 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
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #include "back-mdb.h"
23
24 int
mdb_add(Operation * op,SlapReply * rs)25 mdb_add(Operation *op, SlapReply *rs )
26 {
27 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
28 struct berval pdn;
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;
34 MDB_txn *txn = NULL;
35 MDB_cursor *mc = NULL;
36 MDB_cursor *mcd;
37 ID eid, pid = 0;
38 mdb_op_info opinfo = {{{ 0 }}}, *moi = &opinfo;
39 int subentry;
40 int numads = mdb->mi_numads;
41
42 int success;
43
44 LDAPControl **postread_ctrl = NULL;
45 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
46 int num_ctrls = 0;
47
48 #ifdef LDAP_X_TXN
49 int settle = 0;
50 #endif
51
52 Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(mdb_add) ": %s\n",
53 op->ora_e->e_name.bv_val, 0, 0);
54
55 #ifdef LDAP_X_TXN
56 if( op->o_txnSpec ) {
57 /* acquire connection lock */
58 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
59 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
60 rs->sr_text = "invalid transaction identifier";
61 rs->sr_err = LDAP_X_TXN_ID_INVALID;
62 goto txnReturn;
63 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
64 settle=1;
65 goto txnReturn;
66 }
67
68 if( op->o_conn->c_txn_backend == NULL ) {
69 op->o_conn->c_txn_backend = op->o_bd;
70
71 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
72 rs->sr_text = "transaction cannot span multiple database contexts";
73 rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
74 goto txnReturn;
75 }
76
77 /* insert operation into transaction */
78
79 rs->sr_text = "transaction specified";
80 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
81
82 txnReturn:
83 /* release connection lock */
84 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
85
86 if( !settle ) {
87 send_ldap_result( op, rs );
88 return rs->sr_err;
89 }
90 }
91 #endif
92
93 ctrls[num_ctrls] = 0;
94
95 /* check entry's schema */
96 rs->sr_err = entry_schema_check( op, op->ora_e, NULL,
97 get_relax(op), 1, NULL, &rs->sr_text, textbuf, textlen );
98 if ( rs->sr_err != LDAP_SUCCESS ) {
99 Debug( LDAP_DEBUG_TRACE,
100 LDAP_XSTRING(mdb_add) ": entry failed schema check: "
101 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
102 goto return_results;
103 }
104
105 /* begin transaction */
106 rs->sr_err = mdb_opinfo_get( op, mdb, 0, &moi );
107 rs->sr_text = NULL;
108 if( rs->sr_err != 0 ) {
109 Debug( LDAP_DEBUG_TRACE,
110 LDAP_XSTRING(mdb_add) ": txn_begin failed: %s (%d)\n",
111 mdb_strerror(rs->sr_err), rs->sr_err, 0 );
112 rs->sr_err = LDAP_OTHER;
113 rs->sr_text = "internal error";
114 goto return_results;
115 }
116 txn = moi->moi_txn;
117
118 /* add opattrs to shadow as well, only missing attrs will actually
119 * be added; helps compatibility with older OL versions */
120 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
121 if ( rs->sr_err != LDAP_SUCCESS ) {
122 Debug( LDAP_DEBUG_TRACE,
123 LDAP_XSTRING(mdb_add) ": entry failed op attrs add: "
124 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
125 goto return_results;
126 }
127
128 if ( get_assert( op ) &&
129 ( test_filter( op, op->ora_e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
130 {
131 rs->sr_err = LDAP_ASSERTION_FAILED;
132 goto return_results;
133 }
134
135 subentry = is_entry_subentry( op->ora_e );
136
137 /*
138 * Get the parent dn and see if the corresponding entry exists.
139 */
140 if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
141 pdn = slap_empty_bv;
142 } else {
143 dnParent( &op->ora_e->e_nname, &pdn );
144 }
145
146 rs->sr_err = mdb_cursor_open( txn, mdb->mi_dn2id, &mcd );
147 if( rs->sr_err != 0 ) {
148 Debug( LDAP_DEBUG_TRACE,
149 LDAP_XSTRING(mdb_add) ": mdb_cursor_open failed (%d)\n",
150 rs->sr_err, 0, 0 );
151 rs->sr_err = LDAP_OTHER;
152 rs->sr_text = "internal error";
153 goto return_results;
154 }
155
156 /* get entry or parent */
157 rs->sr_err = mdb_dn2entry( op, txn, mcd, &op->ora_e->e_nname, &p, NULL, 1 );
158 switch( rs->sr_err ) {
159 case 0:
160 rs->sr_err = LDAP_ALREADY_EXISTS;
161 mdb_entry_return( op, p );
162 p = NULL;
163 goto return_results;
164 case MDB_NOTFOUND:
165 break;
166 case LDAP_BUSY:
167 rs->sr_text = "ldap server busy";
168 goto return_results;
169 default:
170 rs->sr_err = LDAP_OTHER;
171 rs->sr_text = "internal error";
172 goto return_results;
173 }
174
175 if ( !p )
176 p = (Entry *)&slap_entry_root;
177
178 if ( !bvmatch( &pdn, &p->e_nname ) ) {
179 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
180 op->o_tmpmemctx );
181 if ( p != (Entry *)&slap_entry_root && is_entry_referral( p )) {
182 BerVarray ref = get_entry_referrals( op, p );
183 rs->sr_ref = referral_rewrite( ref, &p->e_name,
184 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
185 ber_bvarray_free( ref );
186 } else {
187 rs->sr_ref = NULL;
188 }
189 if ( p != (Entry *)&slap_entry_root )
190 mdb_entry_return( op, p );
191 p = NULL;
192 Debug( LDAP_DEBUG_TRACE,
193 LDAP_XSTRING(mdb_add) ": parent "
194 "does not exist\n", 0, 0, 0 );
195
196 rs->sr_err = LDAP_REFERRAL;
197 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
198 goto return_results;
199 }
200
201 rs->sr_err = access_allowed( op, p,
202 children, NULL, ACL_WADD, NULL );
203
204 if ( ! rs->sr_err ) {
205 if ( p != (Entry *)&slap_entry_root )
206 mdb_entry_return( op, p );
207 p = NULL;
208
209 Debug( LDAP_DEBUG_TRACE,
210 LDAP_XSTRING(mdb_add) ": no write access to parent\n",
211 0, 0, 0 );
212 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
213 rs->sr_text = "no write access to parent";
214 goto return_results;;
215 }
216
217 if ( p != (Entry *)&slap_entry_root ) {
218 if ( is_entry_subentry( p ) ) {
219 mdb_entry_return( op, p );
220 p = NULL;
221 /* parent is a subentry, don't allow add */
222 Debug( LDAP_DEBUG_TRACE,
223 LDAP_XSTRING(mdb_add) ": parent is subentry\n",
224 0, 0, 0 );
225 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
226 rs->sr_text = "parent is a subentry";
227 goto return_results;;
228 }
229
230 if ( is_entry_alias( p ) ) {
231 mdb_entry_return( op, p );
232 p = NULL;
233 /* parent is an alias, don't allow add */
234 Debug( LDAP_DEBUG_TRACE,
235 LDAP_XSTRING(mdb_add) ": parent is alias\n",
236 0, 0, 0 );
237 rs->sr_err = LDAP_ALIAS_PROBLEM;
238 rs->sr_text = "parent is an alias";
239 goto return_results;;
240 }
241
242 if ( is_entry_referral( p ) ) {
243 BerVarray ref = get_entry_referrals( op, p );
244 /* parent is a referral, don't allow add */
245 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
246 op->o_tmpmemctx );
247 rs->sr_ref = referral_rewrite( ref, &p->e_name,
248 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
249 ber_bvarray_free( ref );
250 mdb_entry_return( op, p );
251 p = NULL;
252 Debug( LDAP_DEBUG_TRACE,
253 LDAP_XSTRING(mdb_add) ": parent is referral\n",
254 0, 0, 0 );
255
256 rs->sr_err = LDAP_REFERRAL;
257 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
258 goto return_results;
259 }
260
261 }
262
263 if ( subentry ) {
264 /* FIXME: */
265 /* parent must be an administrative point of the required kind */
266 }
267
268 /* free parent */
269 if ( p != (Entry *)&slap_entry_root ) {
270 pid = p->e_id;
271 if ( p->e_nname.bv_len ) {
272 struct berval ppdn;
273
274 /* ITS#5326: use parent's DN if differs from provided one */
275 dnParent( &op->ora_e->e_name, &ppdn );
276 if ( !dn_match( &p->e_name, &ppdn ) ) {
277 struct berval rdn;
278 struct berval newdn;
279
280 dnRdn( &op->ora_e->e_name, &rdn );
281
282 build_new_dn( &newdn, &p->e_name, &rdn, NULL );
283 if ( op->ora_e->e_name.bv_val != op->o_req_dn.bv_val )
284 ber_memfree( op->ora_e->e_name.bv_val );
285 op->ora_e->e_name = newdn;
286
287 /* FIXME: should check whether
288 * dnNormalize(newdn) == e->e_nname ... */
289 }
290 }
291
292 mdb_entry_return( op, p );
293 }
294 p = NULL;
295
296 rs->sr_err = access_allowed( op, op->ora_e,
297 entry, NULL, ACL_WADD, NULL );
298
299 if ( ! rs->sr_err ) {
300 Debug( LDAP_DEBUG_TRACE,
301 LDAP_XSTRING(mdb_add) ": no write access to entry\n",
302 0, 0, 0 );
303 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
304 rs->sr_text = "no write access to entry";
305 goto return_results;;
306 }
307
308 /*
309 * Check ACL for attribute write access
310 */
311 if (!acl_check_modlist(op, oe, op->ora_modlist)) {
312 Debug( LDAP_DEBUG_TRACE,
313 LDAP_XSTRING(mdb_add) ": no write access to attribute\n",
314 0, 0, 0 );
315 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
316 rs->sr_text = "no write access to attribute";
317 goto return_results;;
318 }
319
320 rs->sr_err = mdb_cursor_open( txn, mdb->mi_id2entry, &mc );
321 if( rs->sr_err != 0 ) {
322 Debug( LDAP_DEBUG_TRACE,
323 LDAP_XSTRING(mdb_add) ": mdb_cursor_open failed (%d)\n",
324 rs->sr_err, 0, 0 );
325 rs->sr_err = LDAP_OTHER;
326 rs->sr_text = "internal error";
327 goto return_results;
328 }
329
330 rs->sr_err = mdb_next_id( op->o_bd, mc, &eid );
331 if( rs->sr_err != 0 ) {
332 Debug( LDAP_DEBUG_TRACE,
333 LDAP_XSTRING(mdb_add) ": next_id failed (%d)\n",
334 rs->sr_err, 0, 0 );
335 rs->sr_err = LDAP_OTHER;
336 rs->sr_text = "internal error";
337 goto return_results;
338 }
339 op->ora_e->e_id = eid;
340
341 /* dn2id index */
342 rs->sr_err = mdb_dn2id_add( op, mcd, mcd, pid, 1, 1, op->ora_e );
343 mdb_cursor_close( mcd );
344 if ( rs->sr_err != 0 ) {
345 Debug( LDAP_DEBUG_TRACE,
346 LDAP_XSTRING(mdb_add) ": dn2id_add failed: %s (%d)\n",
347 mdb_strerror(rs->sr_err), rs->sr_err, 0 );
348
349 switch( rs->sr_err ) {
350 case MDB_KEYEXIST:
351 rs->sr_err = LDAP_ALREADY_EXISTS;
352 break;
353 default:
354 rs->sr_err = LDAP_OTHER;
355 }
356 goto return_results;
357 }
358
359 /* attribute indexes */
360 rs->sr_err = mdb_index_entry_add( op, txn, op->ora_e );
361 if ( rs->sr_err != LDAP_SUCCESS ) {
362 Debug( LDAP_DEBUG_TRACE,
363 LDAP_XSTRING(mdb_add) ": index_entry_add failed\n",
364 0, 0, 0 );
365 rs->sr_err = LDAP_OTHER;
366 rs->sr_text = "index generation failed";
367 goto return_results;
368 }
369
370 /* id2entry index */
371 rs->sr_err = mdb_id2entry_add( op, txn, mc, op->ora_e );
372 if ( rs->sr_err != 0 ) {
373 Debug( LDAP_DEBUG_TRACE,
374 LDAP_XSTRING(mdb_add) ": id2entry_add failed\n",
375 0, 0, 0 );
376 rs->sr_err = LDAP_OTHER;
377 rs->sr_text = "entry store failed";
378 goto return_results;
379 }
380
381 /* post-read */
382 if( op->o_postread ) {
383 if( postread_ctrl == NULL ) {
384 postread_ctrl = &ctrls[num_ctrls++];
385 ctrls[num_ctrls] = NULL;
386 }
387 if ( slap_read_controls( op, rs, op->ora_e,
388 &slap_post_read_bv, postread_ctrl ) )
389 {
390 Debug( LDAP_DEBUG_TRACE,
391 "<=- " LDAP_XSTRING(mdb_add) ": post-read "
392 "failed!\n", 0, 0, 0 );
393 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
394 /* FIXME: is it correct to abort
395 * operation if control fails? */
396 goto return_results;
397 }
398 }
399 }
400
401 if ( moi == &opinfo ) {
402 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
403 opinfo.moi_oe.oe_key = NULL;
404 if ( op->o_noop ) {
405 mdb->mi_numads = numads;
406 mdb_txn_abort( txn );
407 rs->sr_err = LDAP_X_NO_OPERATION;
408 txn = NULL;
409 goto return_results;
410 }
411
412 rs->sr_err = mdb_txn_commit( txn );
413 txn = NULL;
414 if ( rs->sr_err != 0 ) {
415 mdb->mi_numads = numads;
416 rs->sr_text = "txn_commit failed";
417 Debug( LDAP_DEBUG_ANY,
418 LDAP_XSTRING(mdb_add) ": %s : %s (%d)\n",
419 rs->sr_text, mdb_strerror(rs->sr_err), rs->sr_err );
420 rs->sr_err = LDAP_OTHER;
421 goto return_results;
422 }
423 }
424
425 Debug(LDAP_DEBUG_TRACE,
426 LDAP_XSTRING(mdb_add) ": added%s id=%08lx dn=\"%s\"\n",
427 op->o_noop ? " (no-op)" : "",
428 op->ora_e->e_id, op->ora_e->e_dn );
429
430 rs->sr_text = NULL;
431 if( num_ctrls ) rs->sr_ctrls = ctrls;
432
433 return_results:
434 success = rs->sr_err;
435 send_ldap_result( op, rs );
436
437 if( moi == &opinfo ) {
438 if( txn != NULL ) {
439 mdb->mi_numads = numads;
440 mdb_txn_abort( txn );
441 }
442 if ( opinfo.moi_oe.oe_key ) {
443 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
444 }
445 } else {
446 moi->moi_ref--;
447 }
448
449 if( success == LDAP_SUCCESS ) {
450 #if 0
451 if ( mdb->bi_txn_cp_kbyte ) {
452 TXN_CHECKPOINT( mdb->bi_dbenv,
453 mdb->bi_txn_cp_kbyte, mdb->bi_txn_cp_min, 0 );
454 }
455 #endif
456 }
457
458 slap_graduate_commit_csn( op );
459
460 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
461 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
462 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
463 }
464 return rs->sr_err;
465 }
466