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