]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
More changes to let BDB build without LDBM.
[openldap] / servers / slapd / back-bdb / add.c
1 /* add.c - ldap BerkeleyDB back-end add routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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         char            *pdn = NULL;
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
34         Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
35
36         /* check entry's schema */
37         rc = entry_schema_check( e, NULL, &text, textbuf, textlen );
38         if ( rc != LDAP_SUCCESS ) {
39                 Debug( LDAP_DEBUG_TRACE,
40                         "bdb_add: entry failed schema check: %s (%d)\n",
41                         text, rc, 0 );
42                 goto return_results;
43         }
44
45         /*
46          * acquire an ID outside of the operation transaction
47          * to avoid serializing adds.
48          */
49         rc = bdb_next_id( be, NULL, &e->e_id );
50         if( rc != 0 ) {
51                 Debug( LDAP_DEBUG_TRACE,
52                         "bdb_add: next_id failed (%d)\n",
53                         rc, 0, 0 );
54                 rc = LDAP_OTHER;
55                 text = "internal error";
56                 goto return_results;
57         }
58
59         if (0) {
60                 /* transaction retry */
61 retry:  rc = txn_abort( ltid );
62                 ltid = NULL;
63                 op->o_private = NULL;
64                 if( rc != 0 ) {
65                         rc = LDAP_OTHER;
66                         text = "internal error";
67                         goto return_results;
68                 }
69         }
70
71         /* begin transaction */
72         rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 0 );
73         text = NULL;
74         if( rc != 0 ) {
75                 Debug( LDAP_DEBUG_TRACE,
76                         "bdb_add: txn_begin failed: %s (%d)\n",
77                         db_strerror(rc), rc, 0 );
78                 rc = LDAP_OTHER;
79                 text = "internal error";
80                 goto return_results;
81         }
82
83         opinfo.boi_bdb = be;
84         opinfo.boi_txn = ltid;
85         opinfo.boi_err = 0;
86         op->o_private = &opinfo;
87         
88         /*
89          * Get the parent dn and see if the corresponding entry exists.
90          * If the parent does not exist, only allow the "root" user to
91          * add the entry.
92          */
93         pdn = dn_parent( be, e->e_ndn );
94
95         if( pdn != NULL && *pdn != '\0' ) {
96                 Entry *matched = NULL;
97
98                 /* get parent */
99                 rc = bdb_dn2entry( be, ltid, pdn, &p, &matched, 0 );
100                 ch_free( pdn );
101
102                 switch( rc ) {
103                 case 0:
104                 case DB_NOTFOUND:
105                         break;
106                 case DB_LOCK_DEADLOCK:
107                 case DB_LOCK_NOTGRANTED:
108                         goto retry;
109                 default:
110                         rc = LDAP_OTHER;
111                         text = "internal error";
112                         goto return_results;
113                 }
114
115                 if ( p == NULL ) {
116                         char *matched_dn;
117                         struct berval **refs;
118
119                         if ( matched != NULL ) {
120                                 matched_dn = ch_strdup( matched->e_dn );
121                                 refs = is_entry_referral( matched )
122                                         ? get_entry_referrals( be, conn, op, matched )
123                                         : NULL;
124                                 bdb_entry_return( be, matched );
125                                 matched = NULL;
126
127                         } else {
128                                 matched_dn = NULL;
129                                 refs = default_referral;
130                         }
131
132                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
133                                 0, 0, 0 );
134
135                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
136                                 matched_dn, NULL, refs, NULL );
137
138                         if( matched != NULL ) {
139                                 ber_bvecfree( refs );
140                                 ch_free( matched_dn );
141                         }
142
143                         goto done;
144                 }
145
146                 if ( ! access_allowed( be, conn, op, p,
147                         children, NULL, ACL_WRITE ) )
148                 {
149                         Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
150                                 0, 0, 0 );
151                         rc = LDAP_INSUFFICIENT_ACCESS;
152                         text = "no write access to parent", NULL, NULL;
153                         goto return_results;;
154                 }
155
156                 if ( is_entry_alias( p ) ) {
157                         /* parent is an alias, don't allow add */
158                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
159                                 0, 0, 0 );
160                         rc = LDAP_ALIAS_PROBLEM;
161                         text = "parent is an alias";
162                         goto return_results;;
163                 }
164
165                 if ( is_entry_referral( p ) ) {
166                         /* parent is a referral, don't allow add */
167                         char *matched_dn = ch_strdup( p->e_dn );
168                         struct berval **refs = is_entry_referral( p )
169                                 ? get_entry_referrals( be, conn, op, p )
170                                 : NULL;
171
172                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
173                                 0, 0, 0 );
174
175                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
176                                 matched_dn, NULL, refs, NULL );
177
178                         ber_bvecfree( refs );
179                         free( matched_dn );
180                         goto done;
181                 }
182
183                 /* free parent and writer lock */
184                 bdb_entry_return( be, p );
185                 p = NULL;
186
187         } else {
188                 if( pdn != NULL ) {
189                         free(pdn);
190                 }
191
192                 /*
193                  * no parent!
194                  *      must be adding entry to at suffix
195                  *  or with parent ""
196                  */
197                 if ( !be_isroot( be, op->o_ndn )) {
198                         Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
199                                 pdn == NULL ? "suffix" : "entry at root",
200                                 0, 0 );
201                         rc = LDAP_INSUFFICIENT_ACCESS;
202                         goto return_results;
203                 }
204         }
205
206         /* dn2id index */
207         rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
208         if ( rc != 0 ) {
209                 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
210                         db_strerror(rc), rc, 0 );
211
212                 switch( rc ) {
213                 case DB_LOCK_DEADLOCK:
214                 case DB_LOCK_NOTGRANTED:
215                         goto retry;
216                 case DB_KEYEXIST:
217                         rc = LDAP_ALREADY_EXISTS;
218                         break;
219                 default:
220                         rc = LDAP_OTHER;
221                 }
222                 goto return_results;
223         }
224
225         /* id2entry index */
226         rc = bdb_id2entry_add( be, ltid, e );
227         if ( rc != 0 ) {
228                 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
229                         0, 0, 0 );
230                 switch( rc ) {
231                 case DB_LOCK_DEADLOCK:
232                 case DB_LOCK_NOTGRANTED:
233                         goto retry;
234                 default:
235                         rc = LDAP_OTHER;
236                 }
237                 text = "entry store failed";
238                 goto return_results;
239         }
240
241 #ifdef BDB_INDEX
242         /* attribute indexes */
243         if ( bdb_index_entry_add( be, ltid, e, e->e_attrs ) != LDAP_SUCCESS ) {
244                 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
245                         0, 0, 0 );
246                 switch( rc ) {
247                 case DB_LOCK_DEADLOCK:
248                 case DB_LOCK_NOTGRANTED:
249                         goto retry;
250                 default:
251                         rc = LDAP_OTHER;
252                 }
253                 text = "index generation failed";
254                 goto return_results;
255         }
256 #endif
257
258         rc = txn_commit( ltid, 0 );
259         ltid = NULL;
260         op->o_private = NULL;
261
262         if( rc != 0 ) {
263                 Debug( LDAP_DEBUG_TRACE,
264                         "bdb_add: txn_commit failed: %s (%d)\n",
265                         db_strerror(rc), rc, 0 );
266                 rc = LDAP_OTHER;
267                 text = "commit failed";
268         } else {
269                 Debug( LDAP_DEBUG_TRACE,
270                         "bdb_add: added id=%08x dn=\"%s\"\n",
271                         e->e_id, e->e_dn, 0 );
272                 rc = LDAP_SUCCESS;
273                 text = NULL;
274         }
275
276 return_results:
277         send_ldap_result( conn, op, rc,
278                 NULL, text, NULL, NULL );
279
280         if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
281                 ldap_pvt_thread_yield();
282                 txn_checkpoint( bdb->bi_dbenv,
283                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
284         }
285
286 done:
287         if (p != NULL) {
288                 /* free parent and writer lock */
289                 bdb_entry_return( be, p ); 
290         }
291
292         if( ltid != NULL ) {
293                 txn_abort( ltid );
294                 op->o_private = NULL;
295         }
296
297         return rc;
298 }