]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
Set lock detector to DEFAULT, not NORUN.
[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                 /* transaction retry */
68 retry:  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( 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_entry_return( be, 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 writer lock */
184                         bdb_entry_return( be, 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 = ch_strdup( p->e_dn );
218                         BerVarray refs = is_entry_referral( p )
219                                 ? get_entry_referrals( be, conn, op, p )
220                                 : NULL;
221
222                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
223                                 0, 0, 0 );
224
225                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
226                                 matched_dn, NULL, refs, NULL );
227
228                         ber_bvarray_free( refs );
229                         free( matched_dn );
230                         goto done;
231                 }
232
233                 if ( subentry ) {
234                         /* FIXME: */
235                         /* parent must be an administrative point of the required kind */
236                 }
237
238                 /* free parent and writer lock */
239                 bdb_entry_return( be, p );
240                 p = NULL;
241
242         } else {
243                 /*
244                  * no parent!
245                  *      must be adding entry at suffix or with parent ""
246                  */
247                 if ( !be_isroot( be, &op->o_ndn )) {
248                         if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
249                                 p = (Entry *)&slap_entry_root;
250
251                                 /* check parent for "children" acl */
252                                 rc = access_allowed( be, conn, op, p,
253                                         children, NULL, ACL_WRITE );
254                                 p = NULL;
255
256                                 switch( opinfo.boi_err ) {
257                                 case DB_LOCK_DEADLOCK:
258                                 case DB_LOCK_NOTGRANTED:
259                                         goto retry;
260                                 }
261
262                                 if ( ! rc ) {
263                                         Debug( LDAP_DEBUG_TRACE,
264                                                 "bdb_add: no write access to parent\n",
265                                                 0, 0, 0 );
266                                         rc = LDAP_INSUFFICIENT_ACCESS;
267                                         text = "no write access to parent";
268                                         goto return_results;;
269                                 }
270
271                         } else {
272                                 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
273                                         pdn.bv_len == 0 ? "suffix" : "entry at root",
274                                         0, 0 );
275                                 rc = LDAP_INSUFFICIENT_ACCESS;
276                                 goto return_results;
277                         }
278                 }
279
280                 if( subentry ) {
281                         Debug( LDAP_DEBUG_TRACE,
282                                 "bdb_add: no parent, cannot add subentry\n",
283                                 0, 0, 0 );
284                         rc = LDAP_INSUFFICIENT_ACCESS;
285                         text = "no parent, cannot add subentry";
286                         goto return_results;;
287                 }
288 #if 0
289                 if ( ltid ) {
290                         DBT obj;
291                         obj.data = ",";
292                         obj.size = 1;
293                         rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
294                                 DB_LOCK_WRITE, &lock);
295                 }
296 #endif
297         }
298
299         /* dn2id index */
300         rc = bdb_dn2id_add( be, ltid, &pdn, e );
301         if ( rc != 0 ) {
302                 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
303                         db_strerror(rc), rc, 0 );
304
305                 switch( rc ) {
306                 case DB_LOCK_DEADLOCK:
307                 case DB_LOCK_NOTGRANTED:
308                         goto retry;
309                 case DB_KEYEXIST:
310                         rc = LDAP_ALREADY_EXISTS;
311                         break;
312                 default:
313                         rc = LDAP_OTHER;
314                 }
315                 goto return_results;
316         }
317
318         /* id2entry index */
319         rc = bdb_id2entry_add( be, ltid, e );
320         if ( rc != 0 ) {
321                 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
322                         0, 0, 0 );
323                 switch( rc ) {
324                 case DB_LOCK_DEADLOCK:
325                 case DB_LOCK_NOTGRANTED:
326                         goto retry;
327                 default:
328                         rc = LDAP_OTHER;
329                 }
330                 text = "entry store failed";
331                 goto return_results;
332         }
333
334         /* attribute indexes */
335         rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
336         if ( rc != LDAP_SUCCESS ) {
337                 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
338                         0, 0, 0 );
339                 switch( rc ) {
340                 case DB_LOCK_DEADLOCK:
341                 case DB_LOCK_NOTGRANTED:
342                         goto retry;
343                 default:
344                         rc = LDAP_OTHER;
345                 }
346                 text = "index generation failed";
347                 goto return_results;
348         }
349
350         if( bdb->bi_txn ) {
351                 rc = txn_commit( ltid, 0 );
352         }
353         ltid = NULL;
354         op->o_private = NULL;
355
356         if( rc != 0 ) {
357                 Debug( LDAP_DEBUG_TRACE,
358                         "bdb_add: txn_commit failed: %s (%d)\n",
359                         db_strerror(rc), rc, 0 );
360                 rc = LDAP_OTHER;
361                 text = "commit failed";
362
363         } else {
364                 Debug( LDAP_DEBUG_TRACE,
365                         "bdb_add: added id=%08lx dn=\"%s\"\n",
366                         e->e_id, e->e_dn, 0 );
367                 rc = LDAP_SUCCESS;
368                 text = NULL;
369         }
370
371 return_results:
372         send_ldap_result( conn, op, rc,
373                 NULL, text, NULL, NULL );
374
375         if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
376                 ldap_pvt_thread_yield();
377                 TXN_CHECKPOINT( bdb->bi_dbenv,
378                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
379         }
380
381 done:
382         if (p != NULL) {
383                 /* free parent and writer lock */
384                 bdb_entry_return( be, p ); 
385         }
386
387         if( ltid != NULL ) {
388                 txn_abort( ltid );
389                 op->o_private = NULL;
390         }
391
392         return rc;
393 }