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