]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
1a8a66acc50139537a8266acfe7e74b86f128f26
[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                 dnParent( &e->e_nname, &pdn );
111         }
112
113         if( pdn.bv_len != 0 ) {
114                 Entry *matched = NULL;
115
116 #if 0
117                 if ( ltid ) {
118                         DBT obj;
119                         obj.data = pdn.bv_val-1;
120                         obj.size = pdn.bv_len+1;
121                         rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
122                                 DB_LOCK_WRITE, &lock);
123                 }
124 #endif
125
126                 /* get parent */
127                 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, &matched, 0 );
128
129                 switch( rc ) {
130                 case 0:
131                 case DB_NOTFOUND:
132                         break;
133                 case DB_LOCK_DEADLOCK:
134                 case DB_LOCK_NOTGRANTED:
135                         goto retry;
136                 default:
137                         rc = LDAP_OTHER;
138                         text = "internal error";
139                         goto return_results;
140                 }
141
142                 if ( p == NULL ) {
143                         char *matched_dn = NULL;
144                         BerVarray refs;
145
146                         if ( matched != NULL ) {
147                                 matched_dn = ch_strdup( matched->e_dn );
148                                 refs = is_entry_referral( matched )
149                                         ? get_entry_referrals( be, conn, op, matched )
150                                         : NULL;
151                                 bdb_cache_return_entry_r(&bdb->bi_cache, matched);
152                                 matched = NULL;
153
154                         } else {
155                                 refs = referral_rewrite( default_referral,
156                                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
157                         }
158
159                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
160                                 0, 0, 0 );
161
162                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
163                                 matched_dn, NULL, refs, NULL );
164
165                         ber_bvarray_free( refs );
166                         ch_free( matched_dn );
167
168                         goto done;
169                 }
170
171                 rc = access_allowed( be, conn, op, p,
172                         children, NULL, ACL_WRITE );
173
174                 switch( opinfo.boi_err ) {
175                 case DB_LOCK_DEADLOCK:
176                 case DB_LOCK_NOTGRANTED:
177                         /* free parent and reader lock */
178                         bdb_cache_return_entry_r( &bdb->bi_cache, p );
179                         p = NULL;
180                         goto retry;
181                 }
182
183                 if ( ! rc ) {
184                         Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
185                                 0, 0, 0 );
186                         rc = LDAP_INSUFFICIENT_ACCESS;
187                         text = "no write access to parent";
188                         goto return_results;;
189                 }
190
191                 if ( is_entry_subentry( p ) ) {
192                         /* parent is a subentry, don't allow add */
193                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
194                                 0, 0, 0 );
195                         rc = LDAP_OBJECT_CLASS_VIOLATION;
196                         text = "parent is a subentry";
197                         goto return_results;;
198                 }
199
200                 if ( is_entry_alias( p ) ) {
201                         /* parent is an alias, don't allow add */
202                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
203                                 0, 0, 0 );
204                         rc = LDAP_ALIAS_PROBLEM;
205                         text = "parent is an alias";
206                         goto return_results;;
207                 }
208
209                 if ( is_entry_referral( p ) ) {
210                         /* parent is a referral, don't allow add */
211                         char *matched_dn = p->e_dn;
212                         BerVarray refs = get_entry_referrals( be, conn, op, p );
213
214                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
215                                 0, 0, 0 );
216
217                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
218                                 matched_dn, NULL, refs, NULL );
219
220                         ber_bvarray_free( refs );
221                         bdb_cache_return_entry_r( &bdb->bi_cache, p );
222                         p = NULL;
223                         goto done;
224                 }
225
226                 if ( subentry ) {
227                         /* FIXME: */
228                         /* parent must be an administrative point of the required kind */
229                 }
230
231                 /* free parent and reader lock */
232                 bdb_cache_return_entry_r( &bdb->bi_cache, p );
233                 p = NULL;
234
235         } else {
236                 /*
237                  * no parent!
238                  *      must be adding entry at suffix or with parent ""
239                  */
240                 if ( !be_isroot( be, &op->o_ndn )) {
241                         if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
242                                 || be_isupdate( be, &op->o_ndn ) ) {
243                                 p = (Entry *)&slap_entry_root;
244
245                                 /* check parent for "children" acl */
246                                 rc = access_allowed( be, conn, op, p,
247                                         children, NULL, ACL_WRITE );
248                                 p = NULL;
249
250                                 switch( opinfo.boi_err ) {
251                                 case DB_LOCK_DEADLOCK:
252                                 case DB_LOCK_NOTGRANTED:
253                                         goto retry;
254                                 }
255
256                                 if ( ! rc ) {
257                                         Debug( LDAP_DEBUG_TRACE,
258                                                 "bdb_add: no write access to parent\n",
259                                                 0, 0, 0 );
260                                         rc = LDAP_INSUFFICIENT_ACCESS;
261                                         text = "no write access to parent";
262                                         goto return_results;;
263                                 }
264
265                         } else {
266                                 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
267                                         pdn.bv_len == 0 ? "suffix" : "entry at root",
268                                         0, 0 );
269                                 rc = LDAP_INSUFFICIENT_ACCESS;
270                                 goto return_results;
271                         }
272                 }
273
274                 if( subentry ) {
275                         Debug( LDAP_DEBUG_TRACE,
276                                 "bdb_add: no parent, cannot add subentry\n",
277                                 0, 0, 0 );
278                         rc = LDAP_INSUFFICIENT_ACCESS;
279                         text = "no parent, cannot add subentry";
280                         goto return_results;;
281                 }
282 #if 0
283                 if ( ltid ) {
284                         DBT obj;
285                         obj.data = ",";
286                         obj.size = 1;
287                         rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
288                                 DB_LOCK_WRITE, &lock);
289                 }
290 #endif
291         }
292
293         /* dn2id index */
294         rc = bdb_dn2id_add( be, ltid, &pdn, e );
295         if ( rc != 0 ) {
296                 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
297                         db_strerror(rc), rc, 0 );
298
299                 switch( rc ) {
300                 case DB_LOCK_DEADLOCK:
301                 case DB_LOCK_NOTGRANTED:
302                         goto retry;
303                 case DB_KEYEXIST:
304                         rc = LDAP_ALREADY_EXISTS;
305                         break;
306                 default:
307                         rc = LDAP_OTHER;
308                 }
309                 goto return_results;
310         }
311
312         /* id2entry index */
313         rc = bdb_id2entry_add( be, ltid, e );
314         if ( rc != 0 ) {
315                 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
316                         0, 0, 0 );
317                 switch( rc ) {
318                 case DB_LOCK_DEADLOCK:
319                 case DB_LOCK_NOTGRANTED:
320                         goto retry;
321                 default:
322                         rc = LDAP_OTHER;
323                 }
324                 text = "entry store failed";
325                 goto return_results;
326         }
327
328         /* attribute indexes */
329         rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
330         if ( rc != LDAP_SUCCESS ) {
331                 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
332                         0, 0, 0 );
333                 switch( rc ) {
334                 case DB_LOCK_DEADLOCK:
335                 case DB_LOCK_NOTGRANTED:
336                         goto retry;
337                 default:
338                         rc = LDAP_OTHER;
339                 }
340                 text = "index generation failed";
341                 goto return_results;
342         }
343
344         if( bdb->bi_txn ) {
345                 rc = txn_commit( ltid, 0 );
346         }
347         ltid = NULL;
348         op->o_private = NULL;
349
350         if( rc != 0 ) {
351                 Debug( LDAP_DEBUG_TRACE,
352                         "bdb_add: txn_commit failed: %s (%d)\n",
353                         db_strerror(rc), rc, 0 );
354                 rc = LDAP_OTHER;
355                 text = "commit failed";
356
357         } else {
358                 /* add the entry to the entry cache */
359                 /* we should add to cache only upon free of txn-abort */
360                 if (bdb_cache_add_entry_rw(&bdb->bi_cache, e, CACHE_WRITE_LOCK) != 0) {
361                         text = "cache add failed";
362                         goto return_results;
363                 }
364  
365                 Debug( LDAP_DEBUG_TRACE,
366                         "bdb_add: added id=%08lx dn=\"%s\"\n",
367                         e->e_id, e->e_dn, 0 );
368                 rc = LDAP_SUCCESS;
369                 text = NULL;
370         }
371
372         bdb_cache_entry_commit (e);
373
374 return_results:
375         send_ldap_result( conn, op, rc,
376                 NULL, text, NULL, NULL );
377
378         if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
379                 ldap_pvt_thread_yield();
380                 TXN_CHECKPOINT( bdb->bi_dbenv,
381                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
382         }
383
384 done:
385
386         if( ltid != NULL ) {
387                 txn_abort( ltid );
388                 op->o_private = NULL;
389         }
390
391         return rc;
392 }