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