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