]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
Fix previous commit
[openldap] / servers / slapd / back-bdb / add.c
1 /* add.c - ldap BerkeleyDB back-end add routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14 #include "external.h"
15
16 int
17 bdb_add(
18         BackendDB       *be,
19         Connection      *conn,
20         Operation       *op,
21         Entry   *e )
22 {
23         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
24         struct berval   pdn;
25         Entry           *p = NULL;
26         int                     rc; 
27         const char      *text;
28         char textbuf[SLAP_TEXT_BUFLEN];
29         size_t textlen = sizeof textbuf;
30         AttributeDescription *children = slap_schema.si_ad_children;
31         DB_TXN          *ltid = NULL;
32         struct bdb_op_info opinfo;
33         int subentry;
34 #if 0
35         u_int32_t       lockid;
36         DB_LOCK         lock;
37 #endif
38
39         Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
40
41         /* check entry's schema */
42         rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
43         if ( rc != LDAP_SUCCESS ) {
44                 Debug( LDAP_DEBUG_TRACE,
45                         "bdb_add: entry failed schema check: %s (%d)\n",
46                         text, rc, 0 );
47                 goto return_results;
48         }
49
50         subentry = is_entry_subentry( e );
51
52         /*
53          * acquire an ID outside of the operation transaction
54          * to avoid serializing adds.
55          */
56         rc = bdb_next_id( be, NULL, &e->e_id );
57         if( rc != 0 ) {
58                 Debug( LDAP_DEBUG_TRACE,
59                         "bdb_add: next_id failed (%d)\n",
60                         rc, 0, 0 );
61                 rc = LDAP_OTHER;
62                 text = "internal error";
63                 goto return_results;
64         }
65
66         if( 0 ) {
67 retry:          /* transaction retry */
68                 rc = txn_abort( ltid );
69                 ltid = NULL;
70                 op->o_private = NULL;
71                 if( rc != 0 ) {
72                         rc = LDAP_OTHER;
73                         text = "internal error";
74                         goto return_results;
75                 }
76                 ldap_pvt_thread_yield();
77         }
78
79         /* begin transaction */
80         if( bdb->bi_txn ) {
81                 rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 
82                         bdb->bi_db_opflags );
83                 text = NULL;
84                 if( rc != 0 ) {
85                         Debug( LDAP_DEBUG_TRACE,
86                                 "bdb_add: txn_begin failed: %s (%d)\n",
87                                 db_strerror(rc), rc, 0 );
88                         rc = LDAP_OTHER;
89                         text = "internal error";
90                         goto return_results;
91                 }
92 #if 0
93                 lockid = TXN_ID( ltid );
94 #endif
95         }
96
97         opinfo.boi_bdb = be;
98         opinfo.boi_txn = ltid;
99         opinfo.boi_err = 0;
100         op->o_private = &opinfo;
101         
102         /*
103          * Get the parent dn and see if the corresponding entry exists.
104          * If the parent does not exist, only allow the "root" user to
105          * add the entry.
106          */
107         if ( be_issuffix( be, &e->e_nname ) ) {
108                 pdn = slap_empty_bv;
109         } else {
110                 rc = dnParent( &e->e_nname, &pdn );
111                 if ( rc != LDAP_SUCCESS ) {
112                         text = "internal error";
113                         goto return_results;
114                 }
115         }
116
117         if( pdn.bv_len != 0 ) {
118                 Entry *matched = NULL;
119
120 #if 0
121                 if ( ltid ) {
122                         DBT obj;
123                         obj.data = pdn.bv_val-1;
124                         obj.size = pdn.bv_len+1;
125                         rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
126                                 DB_LOCK_WRITE, &lock);
127                 }
128 #endif
129
130                 /* get parent */
131                 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, &matched, 0 );
132
133                 switch( rc ) {
134                 case 0:
135                 case DB_NOTFOUND:
136                         break;
137                 case DB_LOCK_DEADLOCK:
138                 case DB_LOCK_NOTGRANTED:
139                         goto retry;
140                 default:
141                         rc = LDAP_OTHER;
142                         text = "internal error";
143                         goto return_results;
144                 }
145
146                 if ( p == NULL ) {
147                         char *matched_dn = NULL;
148                         BerVarray refs;
149
150                         if ( matched != NULL ) {
151                                 matched_dn = ch_strdup( matched->e_dn );
152                                 refs = is_entry_referral( matched )
153                                         ? get_entry_referrals( be, conn, op, matched )
154                                         : NULL;
155                                 bdb_cache_return_entry_r(&bdb->bi_cache, matched);
156                                 matched = NULL;
157
158                         } else {
159                                 refs = referral_rewrite( default_referral,
160                                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
161                         }
162
163                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
164                                 0, 0, 0 );
165
166                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
167                                 matched_dn, NULL, refs, NULL );
168
169                         ber_bvarray_free( refs );
170                         ch_free( matched_dn );
171
172                         goto done;
173                 }
174
175                 rc = access_allowed( be, conn, op, p,
176                         children, NULL, ACL_WRITE );
177
178                 switch( opinfo.boi_err ) {
179                 case DB_LOCK_DEADLOCK:
180                 case DB_LOCK_NOTGRANTED:
181                         /* free parent and reader lock */
182                         bdb_cache_return_entry_r( &bdb->bi_cache, p );
183                         p = NULL;
184                         goto retry;
185                 }
186
187                 if ( ! rc ) {
188                         Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
189                                 0, 0, 0 );
190                         rc = LDAP_INSUFFICIENT_ACCESS;
191                         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, "bdb_add: parent is subentry\n",
198                                 0, 0, 0 );
199                         rc = LDAP_OBJECT_CLASS_VIOLATION;
200                         text = "parent is a subentry";
201                         goto return_results;;
202                 }
203
204                 if ( is_entry_alias( p ) ) {
205                         /* parent is an alias, don't allow add */
206                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
207                                 0, 0, 0 );
208                         rc = LDAP_ALIAS_PROBLEM;
209                         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                         char *matched_dn = p->e_dn;
216                         BerVarray refs = get_entry_referrals( be, conn, op, p );
217
218                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
219                                 0, 0, 0 );
220
221                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
222                                 matched_dn, NULL, refs, NULL );
223
224                         ber_bvarray_free( refs );
225                         bdb_cache_return_entry_r( &bdb->bi_cache, p );
226                         p = NULL;
227                         goto done;
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_cache_return_entry_r( &bdb->bi_cache, p );
237                 p = NULL;
238
239         } else {
240                 /*
241                  * no parent!
242                  *      must be adding entry at suffix or with parent ""
243                  */
244                 if ( !be_isroot( be, &op->o_ndn )) {
245                         if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
246                                 || be_isupdate( be, &op->o_ndn ) ) {
247                                 p = (Entry *)&slap_entry_root;
248
249                                 /* check parent for "children" acl */
250                                 rc = access_allowed( be, conn, op, p,
251                                         children, NULL, ACL_WRITE );
252                                 p = NULL;
253
254                                 switch( opinfo.boi_err ) {
255                                 case DB_LOCK_DEADLOCK:
256                                 case DB_LOCK_NOTGRANTED:
257                                         goto retry;
258                                 }
259
260                                 if ( ! rc ) {
261                                         Debug( LDAP_DEBUG_TRACE,
262                                                 "bdb_add: no write access to parent\n",
263                                                 0, 0, 0 );
264                                         rc = LDAP_INSUFFICIENT_ACCESS;
265                                         text = "no write access to parent";
266                                         goto return_results;;
267                                 }
268
269                         } else {
270                                 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
271                                         pdn.bv_len == 0 ? "suffix" : "entry at root",
272                                         0, 0 );
273                                 rc = LDAP_INSUFFICIENT_ACCESS;
274                                 goto return_results;
275                         }
276                 }
277
278                 if( subentry ) {
279                         Debug( LDAP_DEBUG_TRACE,
280                                 "bdb_add: no parent, cannot add subentry\n",
281                                 0, 0, 0 );
282                         rc = LDAP_INSUFFICIENT_ACCESS;
283                         text = "no parent, cannot add subentry";
284                         goto return_results;;
285                 }
286 #if 0
287                 if ( ltid ) {
288                         DBT obj;
289                         obj.data = ",";
290                         obj.size = 1;
291                         rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
292                                 DB_LOCK_WRITE, &lock);
293                 }
294 #endif
295         }
296
297         /* dn2id index */
298         rc = bdb_dn2id_add( be, ltid, &pdn, e );
299         if ( rc != 0 ) {
300                 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
301                         db_strerror(rc), rc, 0 );
302
303                 switch( rc ) {
304                 case DB_LOCK_DEADLOCK:
305                 case DB_LOCK_NOTGRANTED:
306                         goto retry;
307                 case DB_KEYEXIST:
308                         rc = LDAP_ALREADY_EXISTS;
309                         break;
310                 default:
311                         rc = LDAP_OTHER;
312                 }
313                 goto return_results;
314         }
315
316         /* id2entry index */
317         rc = bdb_id2entry_add( be, ltid, e );
318         if ( rc != 0 ) {
319                 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
320                         0, 0, 0 );
321                 switch( rc ) {
322                 case DB_LOCK_DEADLOCK:
323                 case DB_LOCK_NOTGRANTED:
324                         goto retry;
325                 default:
326                         rc = LDAP_OTHER;
327                 }
328                 text = "entry store failed";
329                 goto return_results;
330         }
331
332         /* attribute indexes */
333         rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
334         if ( rc != LDAP_SUCCESS ) {
335                 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
336                         0, 0, 0 );
337                 switch( rc ) {
338                 case DB_LOCK_DEADLOCK:
339                 case DB_LOCK_NOTGRANTED:
340                         goto retry;
341                 default:
342                         rc = LDAP_OTHER;
343                 }
344                 text = "index generation failed";
345                 goto return_results;
346         }
347
348         if( bdb->bi_txn ) {
349                 rc = txn_commit( ltid, 0 );
350         }
351         ltid = NULL;
352         op->o_private = NULL;
353
354         if( rc != 0 ) {
355                 Debug( LDAP_DEBUG_TRACE,
356                         "bdb_add: txn_commit failed: %s (%d)\n",
357                         db_strerror(rc), rc, 0 );
358                 rc = LDAP_OTHER;
359                 text = "commit failed";
360
361         } else {
362                 /* add the entry to the entry cache */
363                 /* we should add to cache only upon free of txn-abort */
364                 if (bdb_cache_add_entry_rw(&bdb->bi_cache, e, CACHE_WRITE_LOCK) != 0) {
365                         text = "cache add failed";
366                         goto return_results;
367                 }
368  
369                 Debug( LDAP_DEBUG_TRACE,
370                         "bdb_add: added id=%08lx dn=\"%s\"\n",
371                         e->e_id, e->e_dn, 0 );
372                 rc = LDAP_SUCCESS;
373                 text = NULL;
374         }
375
376         bdb_cache_entry_commit (e);
377
378 return_results:
379         send_ldap_result( conn, op, rc,
380                 NULL, text, NULL, NULL );
381
382         if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
383                 ldap_pvt_thread_yield();
384                 TXN_CHECKPOINT( bdb->bi_dbenv,
385                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
386         }
387
388 done:
389
390         if( ltid != NULL ) {
391                 txn_abort( ltid );
392                 op->o_private = NULL;
393         }
394
395         return rc;
396 }