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