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