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