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