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