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