]> 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-2005 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;
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         struct bdb_op_info opinfo = {0};
37 #ifdef BDB_SUBENTRIES
38         int subentry;
39 #endif
40         u_int32_t       locker = 0;
41         DB_LOCK         lock;
42
43         int             num_retries = 0;
44
45         Operation* ps_list;
46         int             rc;
47         EntryInfo       *suffix_ei = NULL;
48         Entry           *ctxcsn_e;
49         int                     ctxcsn_added = 0;
50
51         LDAPControl **postread_ctrl = NULL;
52         LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
53         int num_ctrls = 0;
54
55         Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_add) ": %s\n",
56                 op->oq_add.rs_e->e_name.bv_val, 0, 0);
57
58         ctrls[num_ctrls] = 0;
59
60         /* check entry's schema */
61         rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e,
62                 NULL, &rs->sr_text, textbuf, textlen );
63         if ( rs->sr_err != LDAP_SUCCESS ) {
64                 Debug( LDAP_DEBUG_TRACE,
65                         LDAP_XSTRING(bdb_add) ": entry failed schema check: "
66                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
67                 goto return_results;
68         }
69
70 #ifdef BDB_SUBENTRIES
71         subentry = is_entry_subentry( op->oq_add.rs_e );
72 #endif
73
74         /*
75          * acquire an ID outside of the operation transaction
76          * to avoid serializing adds.
77          */
78         rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
79         if( rs->sr_err != 0 ) {
80                 Debug( LDAP_DEBUG_TRACE,
81                         LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
82                         rs->sr_err, 0, 0 );
83                 rs->sr_err = LDAP_OTHER;
84                 rs->sr_text = "internal error";
85                 goto return_results;
86         }
87
88         if( 0 ) {
89 retry:  /* transaction retry */
90                 if( p ) {
91                         /* free parent and reader lock */
92                         bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
93                         p = NULL;
94                 }
95                 rs->sr_err = TXN_ABORT( ltid );
96                 ltid = NULL;
97                 op->o_private = NULL;
98                 op->o_do_not_cache = opinfo.boi_acl_cache;
99                 if( rs->sr_err != 0 ) {
100                         rs->sr_err = LDAP_OTHER;
101                         rs->sr_text = "internal error";
102                         goto return_results;
103                 }
104                 ldap_pvt_thread_yield();
105                 bdb_trans_backoff( ++num_retries );
106         }
107
108         /* begin transaction */
109         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
110                 bdb->bi_db_opflags );
111         rs->sr_text = NULL;
112         if( rs->sr_err != 0 ) {
113                 Debug( LDAP_DEBUG_TRACE,
114                         LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
115                         db_strerror(rs->sr_err), rs->sr_err, 0 );
116                 rs->sr_err = LDAP_OTHER;
117                 rs->sr_text = "internal error";
118                 goto return_results;
119         }
120
121         locker = TXN_ID ( ltid );
122
123         opinfo.boi_bdb = op->o_bd;
124         opinfo.boi_txn = ltid;
125         opinfo.boi_locker = locker;
126         opinfo.boi_err = 0;
127         opinfo.boi_acl_cache = op->o_do_not_cache;
128         op->o_private = &opinfo;
129         
130         /*
131          * Get the parent dn and see if the corresponding entry exists.
132          */
133         if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
134                 pdn = slap_empty_bv;
135         } else {
136                 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
137         }
138
139         /* get entry or parent */
140         rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
141                 1, locker, &lock );
142         switch( rs->sr_err ) {
143         case 0:
144                 rs->sr_err = LDAP_ALREADY_EXISTS;
145                 goto return_results;
146         case DB_NOTFOUND:
147                 break;
148         case DB_LOCK_DEADLOCK:
149         case DB_LOCK_NOTGRANTED:
150                 goto retry;
151         case LDAP_BUSY:
152                 rs->sr_text = "ldap server busy";
153                 goto return_results;
154         default:
155                 rs->sr_err = LDAP_OTHER;
156                 rs->sr_text = "internal error";
157                 goto return_results;
158         }
159
160         p = ei->bei_e;
161         if ( p ) {
162                 if ( !bvmatch( &pdn, &p->e_nname ) ) {
163                         rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
164                                 op->o_tmpmemctx );
165                         rs->sr_ref = is_entry_referral( p )
166                                 ? get_entry_referrals( op, p )
167                                 : NULL;
168                         bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
169                         p = NULL;
170                         Debug( LDAP_DEBUG_TRACE,
171                                 LDAP_XSTRING(bdb_add) ": parent "
172                                 "does not exist\n", 0, 0, 0 );
173
174                         rs->sr_err = LDAP_REFERRAL;
175                         send_ldap_result( op, rs );
176
177                         ber_bvarray_free( rs->sr_ref );
178                         op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
179                         rs->sr_ref = NULL;
180                         rs->sr_matched = NULL;
181
182                         goto done;
183                 }
184
185                 rs->sr_err = access_allowed( op, p,
186                         children, NULL, ACL_WRITE, NULL );
187
188                 if ( ! rs->sr_err ) {
189                         switch( opinfo.boi_err ) {
190                         case DB_LOCK_DEADLOCK:
191                         case DB_LOCK_NOTGRANTED:
192                                 goto retry;
193                         }
194
195                         Debug( LDAP_DEBUG_TRACE,
196                                 LDAP_XSTRING(bdb_add) ": no write access "
197                                 "to parent\n", 0, 0, 0 );
198                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
199                         rs->sr_text = "no write access to parent";
200                         goto return_results;;
201                 }
202
203 #ifdef BDB_SUBENTRIES
204                 if ( is_entry_subentry( p ) ) {
205                         /* parent is a subentry, don't allow add */
206                         Debug( LDAP_DEBUG_TRACE,
207                                 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
208                                 0, 0, 0 );
209                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
210                         rs->sr_text = "parent is a subentry";
211                         goto return_results;;
212                 }
213 #endif
214                 if ( is_entry_alias( p ) ) {
215                         /* parent is an alias, don't allow add */
216                         Debug( LDAP_DEBUG_TRACE,
217                                 LDAP_XSTRING(bdb_add) ": parent is alias\n",
218                                 0, 0, 0 );
219                         rs->sr_err = LDAP_ALIAS_PROBLEM;
220                         rs->sr_text = "parent is an alias";
221                         goto return_results;;
222                 }
223
224                 if ( is_entry_referral( p ) ) {
225                         /* parent is a referral, don't allow add */
226                         rs->sr_matched = p->e_name.bv_val;
227                         rs->sr_ref = get_entry_referrals( op, p );
228
229                         Debug( LDAP_DEBUG_TRACE,
230                                 LDAP_XSTRING(bdb_add) ": parent is referral\n",
231                                 0, 0, 0 );
232
233                         rs->sr_err = LDAP_REFERRAL;
234                         send_ldap_result( op, rs );
235
236                         ber_bvarray_free( rs->sr_ref );
237                         bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
238                         rs->sr_ref = NULL;
239                         rs->sr_matched = NULL;
240                         p = NULL;
241                         goto done;
242                 }
243
244 #ifdef BDB_SUBENTRIES
245                 if ( subentry ) {
246                         /* FIXME: */
247                         /* parent must be an administrative point of the required kind */
248                 }
249 #endif
250
251                 /* free parent and reader lock */
252                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
253                 p = NULL;
254
255         } else {
256                 /*
257                  * no parent!
258                  *  if not attempting to add entry at suffix or with parent ""
259                  */
260                 if ((( !be_isroot( op ) && !be_shadow_update(op) )
261                         || pdn.bv_len > 0 ) && !is_entry_glue( op->oq_add.rs_e ))
262                 {
263                         Debug( LDAP_DEBUG_TRACE,
264                                 LDAP_XSTRING(bdb_add) ": %s denied\n",
265                                 pdn.bv_len == 0 ? "suffix" : "entry at root",
266                                 0, 0 );
267                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
268                         goto return_results;
269                 }
270         }
271
272         if ( get_assert( op ) &&
273                 ( test_filter( op, op->oq_add.rs_e, get_assertion( op ))
274                         != LDAP_COMPARE_TRUE ))
275         {
276                 rs->sr_err = LDAP_ASSERTION_FAILED;
277                 goto return_results;
278         }
279
280         rs->sr_err = access_allowed( op, op->oq_add.rs_e,
281                 entry, NULL, ACL_WRITE, NULL );
282
283         if ( ! rs->sr_err ) {
284                 switch( opinfo.boi_err ) {
285                 case DB_LOCK_DEADLOCK:
286                 case DB_LOCK_NOTGRANTED:
287                         goto retry;
288                 }
289
290                 Debug( LDAP_DEBUG_TRACE,
291                         LDAP_XSTRING(bdb_add) ": no write access to entry\n",
292                         0, 0, 0 );
293                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
294                 rs->sr_text = "no write access to entry";
295                 goto return_results;;
296         }
297
298         /* nested transaction */
299         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, 
300                 bdb->bi_db_opflags );
301         rs->sr_text = NULL;
302         if( rs->sr_err != 0 ) {
303                 Debug( LDAP_DEBUG_TRACE,
304                         LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
305                         "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
306                 rs->sr_err = LDAP_OTHER;
307                 rs->sr_text = "internal error";
308                 goto return_results;
309         }
310
311         /* dn2id index */
312         rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
313         if ( rs->sr_err != 0 ) {
314                 Debug( LDAP_DEBUG_TRACE,
315                         LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
316                         db_strerror(rs->sr_err), rs->sr_err, 0 );
317
318                 switch( rs->sr_err ) {
319                 case DB_LOCK_DEADLOCK:
320                 case DB_LOCK_NOTGRANTED:
321                         goto retry;
322                 case DB_KEYEXIST:
323                         rs->sr_err = LDAP_ALREADY_EXISTS;
324                         break;
325                 default:
326                         rs->sr_err = LDAP_OTHER;
327                 }
328                 goto return_results;
329         }
330
331         /* id2entry index */
332         rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
333         if ( rs->sr_err != 0 ) {
334                 Debug( LDAP_DEBUG_TRACE,
335                         LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
336                         0, 0, 0 );
337                 switch( rs->sr_err ) {
338                 case DB_LOCK_DEADLOCK:
339                 case DB_LOCK_NOTGRANTED:
340                         goto retry;
341                 default:
342                         rs->sr_err = LDAP_OTHER;
343                 }
344                 rs->sr_text = "entry store failed";
345                 goto return_results;
346         }
347
348         /* attribute indexes */
349         rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
350         if ( rs->sr_err != LDAP_SUCCESS ) {
351                 Debug( LDAP_DEBUG_TRACE,
352                         LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
353                         0, 0, 0 );
354                 switch( rs->sr_err ) {
355                 case DB_LOCK_DEADLOCK:
356                 case DB_LOCK_NOTGRANTED:
357                         goto retry;
358                 default:
359                         rs->sr_err = LDAP_OTHER;
360                 }
361                 rs->sr_text = "index generation failed";
362                 goto return_results;
363         }
364         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
365                 rs->sr_err = LDAP_OTHER;
366                 rs->sr_text = "txn_commit(2) failed";
367                 goto return_results;
368         }
369
370 #ifdef BDB_PSEARCH
371         if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
372                 rc = bdb_csn_commit( op, rs, ltid, ei, &suffix_ei,
373                         &ctxcsn_e, &ctxcsn_added, locker );
374                 switch ( rc ) {
375                 case BDB_CSN_ABORT :
376                         goto return_results;
377                 case BDB_CSN_RETRY :
378                         goto retry;
379                 }
380         }
381 #endif
382
383         /* post-read */
384         if( op->o_postread ) {
385                 if( postread_ctrl == NULL ) {
386                         postread_ctrl = &ctrls[num_ctrls++];
387                         ctrls[num_ctrls] = NULL;
388                 }
389                 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
390                         &slap_post_read_bv, postread_ctrl ) )
391                 {
392                         Debug( LDAP_DEBUG_TRACE,
393                                 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
394                                 "failed!\n", 0, 0, 0 );
395                         goto return_results;
396                 }
397         }
398
399         if ( op->o_noop ) {
400                 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
401                         rs->sr_text = "txn_abort (no-op) failed";
402                 } else {
403                         rs->sr_err = LDAP_NO_OPERATION;
404                         goto return_results;
405                 }
406
407         } else {
408                 struct berval nrdn;
409                 Entry *e = entry_dup( op->ora_e );
410
411                 if (pdn.bv_len) {
412                         nrdn.bv_val = e->e_nname.bv_val;
413                         nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
414                 } else {
415                         nrdn = e->e_nname;
416                 }
417
418                 bdb_cache_add( bdb, ei, e, &nrdn, locker );
419
420                 if ( suffix_ei == NULL ) {
421                         suffix_ei = BEI(e);
422                 }
423
424 #ifdef BDB_PSEARCH
425                 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
426                         if ( ctxcsn_added ) {
427                                 bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
428                                         (struct berval *)&slap_ldapsync_cn_bv, locker );
429                         }
430                 }
431
432                 if ( rs->sr_err == LDAP_SUCCESS && !op->o_no_psearch ) {
433                         ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
434                         assert( BEI(e) );
435                         LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
436                                 rc = bdb_psearch( op, rs, ps_list, e, LDAP_PSEARCH_BY_ADD );
437                                 if ( rc ) {
438                                         Debug( LDAP_DEBUG_TRACE,
439                                                 LDAP_XSTRING(bdb_add)
440                                                 ": persistent search failed "
441                                                 "(%d,%d)\n",
442                                                 rc, rs->sr_err, 0 );
443                                 }
444                         }
445                         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
446                 }
447 #endif
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         op->o_private = NULL;
458
459         if ( rs->sr_err != LDAP_SUCCESS ) {
460                 Debug( LDAP_DEBUG_TRACE,
461                         LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
462                         rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
463                 rs->sr_err = LDAP_OTHER;
464                 goto return_results;
465         }
466
467         Debug(LDAP_DEBUG_TRACE,
468                 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
469                 op->o_noop ? " (no-op)" : "",
470                 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
471
472         rs->sr_text = NULL;
473         if( num_ctrls ) rs->sr_ctrls = ctrls;
474
475 return_results:
476         send_ldap_result( op, rs );
477
478         if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
479                 ldap_pvt_thread_yield();
480                 TXN_CHECKPOINT( bdb->bi_dbenv,
481                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
482         }
483
484 done:
485         if( ltid != NULL ) {
486                 TXN_ABORT( ltid );
487                 op->o_private = NULL;
488         }
489
490         if( postread_ctrl != NULL ) {
491                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
492                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
493         }
494         return rs->sr_err;
495 }