Debian Codegraph @

servers/slapd/back-mdb/add.c

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