]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
Happy New Year
[openldap] / servers / slapd / back-bdb / add.c
1 /* add.c - ldap BerkeleyDB 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-bdb.h"
23
24 int
25 bdb_add(Operation *op, SlapReply *rs )
26 {
27         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
28         struct berval   pdn;
29         Entry           *p = NULL, *oe = op->ora_e;
30         EntryInfo       *ei;
31         char textbuf[SLAP_TEXT_BUFLEN];
32         size_t textlen = sizeof textbuf;
33         AttributeDescription *children = slap_schema.si_ad_children;
34         AttributeDescription *entry = slap_schema.si_ad_entry;
35         DB_TXN          *ltid = NULL, *lt2;
36         ID eid = NOID;
37         struct bdb_op_info opinfo = {{{ 0 }}};
38         int subentry;
39         DB_LOCK         lock;
40
41         int             num_retries = 0;
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(bdb_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(bdb_add) ": entry failed schema check: "
64                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
65                 goto return_results;
66         }
67
68         /* add opattrs to shadow as well, only missing attrs will actually
69          * be added; helps compatibility with older OL versions */
70         rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
71         if ( rs->sr_err != LDAP_SUCCESS ) {
72                 Debug( LDAP_DEBUG_TRACE,
73                         LDAP_XSTRING(bdb_add) ": entry failed op attrs add: "
74                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
75                 goto return_results;
76         }
77
78         if ( get_assert( op ) &&
79                 ( test_filter( op, op->ora_e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
80         {
81                 rs->sr_err = LDAP_ASSERTION_FAILED;
82                 goto return_results;
83         }
84
85         subentry = is_entry_subentry( op->ora_e );
86
87         if( 0 ) {
88 retry:  /* transaction retry */
89                 if( p ) {
90                         /* free parent and reader lock */
91                         if ( p != (Entry *)&slap_entry_root ) {
92                                 bdb_unlocked_cache_return_entry_r( bdb, p );
93                         }
94                         p = NULL;
95                 }
96                 rs->sr_err = TXN_ABORT( ltid );
97                 ltid = NULL;
98                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
99                 opinfo.boi_oe.oe_key = NULL;
100                 op->o_do_not_cache = opinfo.boi_acl_cache;
101                 if( rs->sr_err != 0 ) {
102                         rs->sr_err = LDAP_OTHER;
103                         rs->sr_text = "internal error";
104                         goto return_results;
105                 }
106                 if ( op->o_abandon ) {
107                         rs->sr_err = SLAPD_ABANDON;
108                         goto return_results;
109                 }
110                 bdb_trans_backoff( ++num_retries );
111         }
112
113         /* begin transaction */
114         {
115                 int tflags = bdb->bi_db_opflags;
116                 if ( get_lazyCommit( op ))
117                         tflags |= DB_TXN_NOSYNC;
118                 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, tflags );
119         }
120         rs->sr_text = NULL;
121         if( rs->sr_err != 0 ) {
122                 Debug( LDAP_DEBUG_TRACE,
123                         LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
124                         db_strerror(rs->sr_err), rs->sr_err, 0 );
125                 rs->sr_err = LDAP_OTHER;
126                 rs->sr_text = "internal error";
127                 goto return_results;
128         }
129         Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_add) ": txn1 id: %x\n",
130                 ltid->id(ltid), 0, 0 );
131
132         opinfo.boi_oe.oe_key = bdb;
133         opinfo.boi_txn = ltid;
134         opinfo.boi_err = 0;
135         opinfo.boi_acl_cache = op->o_do_not_cache;
136         LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
137
138         /*
139          * Get the parent dn and see if the corresponding entry exists.
140          */
141         if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
142                 pdn = slap_empty_bv;
143         } else {
144                 dnParent( &op->ora_e->e_nname, &pdn );
145         }
146
147         /* get entry or parent */
148         rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
149                 1, &lock );
150         switch( rs->sr_err ) {
151         case 0:
152                 rs->sr_err = LDAP_ALREADY_EXISTS;
153                 goto return_results;
154         case DB_NOTFOUND:
155                 break;
156         case DB_LOCK_DEADLOCK:
157         case DB_LOCK_NOTGRANTED:
158                 goto retry;
159         case LDAP_BUSY:
160                 rs->sr_text = "ldap server busy";
161                 goto return_results;
162         default:
163                 rs->sr_err = LDAP_OTHER;
164                 rs->sr_text = "internal error";
165                 goto return_results;
166         }
167
168         p = ei->bei_e;
169         if ( !p )
170                 p = (Entry *)&slap_entry_root;
171
172         if ( !bvmatch( &pdn, &p->e_nname ) ) {
173                 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
174                         op->o_tmpmemctx );
175                 rs->sr_ref = is_entry_referral( p )
176                         ? get_entry_referrals( op, p )
177                         : NULL;
178                 if ( p != (Entry *)&slap_entry_root )
179                         bdb_unlocked_cache_return_entry_r( bdb, p );
180                 p = NULL;
181                 Debug( LDAP_DEBUG_TRACE,
182                         LDAP_XSTRING(bdb_add) ": parent "
183                         "does not exist\n", 0, 0, 0 );
184
185                 rs->sr_err = LDAP_REFERRAL;
186                 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
187                 goto return_results;
188         }
189
190         rs->sr_err = access_allowed( op, p,
191                 children, NULL, ACL_WADD, NULL );
192
193         if ( ! rs->sr_err ) {
194                 switch( opinfo.boi_err ) {
195                 case DB_LOCK_DEADLOCK:
196                 case DB_LOCK_NOTGRANTED:
197                         goto retry;
198                 }
199
200                 if ( p != (Entry *)&slap_entry_root )
201                         bdb_unlocked_cache_return_entry_r( bdb, p );
202                 p = NULL;
203
204                 Debug( LDAP_DEBUG_TRACE,
205                         LDAP_XSTRING(bdb_add) ": no write access to parent\n",
206                         0, 0, 0 );
207                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
208                 rs->sr_text = "no write access to parent";
209                 goto return_results;;
210         }
211
212         if ( p != (Entry *)&slap_entry_root ) {
213                 if ( is_entry_subentry( p ) ) {
214                         bdb_unlocked_cache_return_entry_r( bdb, p );
215                         p = NULL;
216                         /* parent is a subentry, don't allow add */
217                         Debug( LDAP_DEBUG_TRACE,
218                                 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
219                                 0, 0, 0 );
220                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
221                         rs->sr_text = "parent is a subentry";
222                         goto return_results;;
223                 }
224
225                 if ( is_entry_alias( p ) ) {
226                         bdb_unlocked_cache_return_entry_r( bdb, p );
227                         p = NULL;
228                         /* parent is an alias, don't allow add */
229                         Debug( LDAP_DEBUG_TRACE,
230                                 LDAP_XSTRING(bdb_add) ": parent is alias\n",
231                                 0, 0, 0 );
232                         rs->sr_err = LDAP_ALIAS_PROBLEM;
233                         rs->sr_text = "parent is an alias";
234                         goto return_results;;
235                 }
236
237                 if ( is_entry_referral( p ) ) {
238                         /* parent is a referral, don't allow add */
239                         rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
240                                 op->o_tmpmemctx );
241                         rs->sr_ref = get_entry_referrals( op, p );
242                         bdb_unlocked_cache_return_entry_r( bdb, p );
243                         p = NULL;
244                         Debug( LDAP_DEBUG_TRACE,
245                                 LDAP_XSTRING(bdb_add) ": parent is referral\n",
246                                 0, 0, 0 );
247
248                         rs->sr_err = LDAP_REFERRAL;
249                         rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
250                         goto return_results;
251                 }
252
253         }
254
255         if ( subentry ) {
256                 /* FIXME: */
257                 /* parent must be an administrative point of the required kind */
258         }
259
260         /* free parent and reader lock */
261         if ( p != (Entry *)&slap_entry_root ) {
262                 if ( p->e_nname.bv_len ) {
263                         struct berval ppdn;
264
265                         /* ITS#5326: use parent's DN if differs from provided one */
266                         dnParent( &op->ora_e->e_name, &ppdn );
267                         if ( !dn_match( &p->e_name, &ppdn ) ) {
268                                 struct berval rdn;
269                                 struct berval newdn;
270
271                                 dnRdn( &op->ora_e->e_name, &rdn );
272
273                                 build_new_dn( &newdn, &p->e_name, &rdn, NULL ); 
274                                 if ( op->ora_e->e_name.bv_val != op->o_req_dn.bv_val )
275                                         ber_memfree( op->ora_e->e_name.bv_val );
276                                 op->ora_e->e_name = newdn;
277
278                                 /* FIXME: should check whether
279                                  * dnNormalize(newdn) == e->e_nname ... */
280                         }
281                 }
282
283                 bdb_unlocked_cache_return_entry_r( bdb, p );
284         }
285         p = NULL;
286
287         rs->sr_err = access_allowed( op, op->ora_e,
288                 entry, NULL, ACL_WADD, NULL );
289
290         if ( ! rs->sr_err ) {
291                 switch( opinfo.boi_err ) {
292                 case DB_LOCK_DEADLOCK:
293                 case DB_LOCK_NOTGRANTED:
294                         goto retry;
295                 }
296
297                 Debug( LDAP_DEBUG_TRACE,
298                         LDAP_XSTRING(bdb_add) ": no write access to entry\n",
299                         0, 0, 0 );
300                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
301                 rs->sr_text = "no write access to entry";
302                 goto return_results;;
303         }
304
305         /* 
306          * Check ACL for attribute write access
307          */
308         if (!acl_check_modlist(op, oe, op->ora_modlist)) {
309                 switch( opinfo.boi_err ) {
310                 case DB_LOCK_DEADLOCK:
311                 case DB_LOCK_NOTGRANTED:
312                         goto retry;
313                 }
314
315                 Debug( LDAP_DEBUG_TRACE,
316                         LDAP_XSTRING(bdb_add) ": no write access to attribute\n",
317                         0, 0, 0 );
318                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
319                 rs->sr_text = "no write access to attribute";
320                 goto return_results;;
321         }
322
323         if ( eid == NOID ) {
324                 rs->sr_err = bdb_next_id( op->o_bd, &eid );
325                 if( rs->sr_err != 0 ) {
326                         Debug( LDAP_DEBUG_TRACE,
327                                 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
328                                 rs->sr_err, 0, 0 );
329                         rs->sr_err = LDAP_OTHER;
330                         rs->sr_text = "internal error";
331                         goto return_results;
332                 }
333                 op->ora_e->e_id = eid;
334         }
335
336         /* nested transaction */
337         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, 
338                 bdb->bi_db_opflags );
339         rs->sr_text = NULL;
340         if( rs->sr_err != 0 ) {
341                 Debug( LDAP_DEBUG_TRACE,
342                         LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
343                         "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
344                 rs->sr_err = LDAP_OTHER;
345                 rs->sr_text = "internal error";
346                 goto return_results;
347         }
348         Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_add) ": txn2 id: %x\n",
349                 lt2->id(lt2), 0, 0 );
350
351         /* dn2id index */
352         rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->ora_e );
353         if ( rs->sr_err != 0 ) {
354                 Debug( LDAP_DEBUG_TRACE,
355                         LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
356                         db_strerror(rs->sr_err), rs->sr_err, 0 );
357
358                 switch( rs->sr_err ) {
359                 case DB_LOCK_DEADLOCK:
360                 case DB_LOCK_NOTGRANTED:
361                         goto retry;
362                 case DB_KEYEXIST:
363                         rs->sr_err = LDAP_ALREADY_EXISTS;
364                         break;
365                 default:
366                         rs->sr_err = LDAP_OTHER;
367                 }
368                 goto return_results;
369         }
370
371         /* attribute indexes */
372         rs->sr_err = bdb_index_entry_add( op, lt2, op->ora_e );
373         if ( rs->sr_err != LDAP_SUCCESS ) {
374                 Debug( LDAP_DEBUG_TRACE,
375                         LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
376                         0, 0, 0 );
377                 switch( rs->sr_err ) {
378                 case DB_LOCK_DEADLOCK:
379                 case DB_LOCK_NOTGRANTED:
380                         goto retry;
381                 default:
382                         rs->sr_err = LDAP_OTHER;
383                 }
384                 rs->sr_text = "index generation failed";
385                 goto return_results;
386         }
387
388         /* id2entry index */
389         rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->ora_e );
390         if ( rs->sr_err != 0 ) {
391                 Debug( LDAP_DEBUG_TRACE,
392                         LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
393                         0, 0, 0 );
394                 switch( rs->sr_err ) {
395                 case DB_LOCK_DEADLOCK:
396                 case DB_LOCK_NOTGRANTED:
397                         goto retry;
398                 default:
399                         rs->sr_err = LDAP_OTHER;
400                 }
401                 rs->sr_text = "entry store failed";
402                 goto return_results;
403         }
404
405         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
406                 rs->sr_err = LDAP_OTHER;
407                 rs->sr_text = "txn_commit(2) failed";
408                 goto return_results;
409         }
410
411         /* post-read */
412         if( op->o_postread ) {
413                 if( postread_ctrl == NULL ) {
414                         postread_ctrl = &ctrls[num_ctrls++];
415                         ctrls[num_ctrls] = NULL;
416                 }
417                 if ( slap_read_controls( op, rs, op->ora_e,
418                         &slap_post_read_bv, postread_ctrl ) )
419                 {
420                         Debug( LDAP_DEBUG_TRACE,
421                                 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
422                                 "failed!\n", 0, 0, 0 );
423                         if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
424                                 /* FIXME: is it correct to abort
425                                  * operation if control fails? */
426                                 goto return_results;
427                         }
428                 }
429         }
430
431         if ( op->o_noop ) {
432                 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
433                         rs->sr_text = "txn_abort (no-op) failed";
434                 } else {
435                         rs->sr_err = LDAP_X_NO_OPERATION;
436                         ltid = NULL;
437                         goto return_results;
438                 }
439
440         } else {
441                 struct berval nrdn;
442
443                 /* pick the RDN if not suffix; otherwise pick the entire DN */
444                 if (pdn.bv_len) {
445                         nrdn.bv_val = op->ora_e->e_nname.bv_val;
446                         nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
447                 } else {
448                         nrdn = op->ora_e->e_nname;
449                 }
450
451                 bdb_cache_add( bdb, ei, op->ora_e, &nrdn, ltid, &lock );
452
453                 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
454                         rs->sr_text = "txn_commit failed";
455                 } else {
456                         rs->sr_err = LDAP_SUCCESS;
457                 }
458         }
459
460         ltid = NULL;
461         LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
462         opinfo.boi_oe.oe_key = NULL;
463
464         if ( rs->sr_err != LDAP_SUCCESS ) {
465                 Debug( LDAP_DEBUG_TRACE,
466                         LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
467                         rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
468                 rs->sr_err = LDAP_OTHER;
469                 goto return_results;
470         }
471
472         Debug(LDAP_DEBUG_TRACE,
473                 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
474                 op->o_noop ? " (no-op)" : "",
475                 op->ora_e->e_id, op->ora_e->e_dn );
476
477         rs->sr_text = NULL;
478         if( num_ctrls ) rs->sr_ctrls = ctrls;
479
480 return_results:
481         success = rs->sr_err;
482         send_ldap_result( op, rs );
483
484         if( ltid != NULL ) {
485                 TXN_ABORT( ltid );
486         }
487         if ( opinfo.boi_oe.oe_key ) {
488                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
489         }
490
491         if( success == LDAP_SUCCESS ) {
492                 /* We own the entry now, and it can be purged at will
493                  * Check to make sure it's the same entry we entered with.
494                  * Possibly a callback may have mucked with it, although
495                  * in general callbacks should treat the entry as read-only.
496                  */
497                 bdb_cache_deref( oe->e_private );
498                 if ( op->ora_e == oe )
499                         op->ora_e = NULL;
500
501                 if ( bdb->bi_txn_cp_kbyte ) {
502                         TXN_CHECKPOINT( bdb->bi_dbenv,
503                                 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
504                 }
505         }
506
507         slap_graduate_commit_csn( op );
508
509         if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
510                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
511                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
512         }
513         return rs->sr_err;
514 }