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