]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
Rewrote entry_encode/entry_decode again, uses 50% less disk space.
[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         if( bdb->bi_txn ) {
73                 rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 
74                         bdb->bi_db_opflags );
75                 text = NULL;
76                 if( rc != 0 ) {
77                         Debug( LDAP_DEBUG_TRACE,
78                                 "bdb_add: txn_begin failed: %s (%d)\n",
79                                 db_strerror(rc), rc, 0 );
80                         rc = LDAP_OTHER;
81                         text = "internal error";
82                         goto return_results;
83                 }
84         }
85
86         opinfo.boi_bdb = be;
87         opinfo.boi_txn = ltid;
88         opinfo.boi_err = 0;
89         op->o_private = &opinfo;
90         
91         /*
92          * Get the parent dn and see if the corresponding entry exists.
93          * If the parent does not exist, only allow the "root" user to
94          * add the entry.
95          */
96         pdn = dn_parent( be, e->e_ndn );
97
98         if( pdn != NULL && *pdn != '\0' ) {
99                 Entry *matched = NULL;
100
101                 /* get parent */
102                 rc = bdb_dn2entry( be, ltid, pdn, &p, &matched, 0 );
103                 ch_free( pdn );
104
105                 switch( rc ) {
106                 case 0:
107                 case DB_NOTFOUND:
108                         break;
109                 case DB_LOCK_DEADLOCK:
110                 case DB_LOCK_NOTGRANTED:
111                         goto retry;
112                 default:
113                         rc = LDAP_OTHER;
114                         text = "internal error";
115                         goto return_results;
116                 }
117
118                 if ( p == NULL ) {
119                         char *matched_dn = NULL;
120                         struct berval **refs;
121
122                         if ( matched != NULL ) {
123                                 matched_dn = ch_strdup( matched->e_dn );
124                                 refs = is_entry_referral( matched )
125                                         ? get_entry_referrals( be, conn, op,
126                                         matched, e->e_dn, LDAP_SCOPE_DEFAULT )
127                                         : NULL;
128                                 bdb_entry_return( be, matched );
129                                 matched = NULL;
130
131                         } else {
132                                 refs = referral_rewrite( default_referral,
133                                         NULL, e->e_dn, LDAP_SCOPE_DEFAULT );
134                         }
135
136                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
137                                 0, 0, 0 );
138
139                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
140                                 matched_dn, NULL, refs, NULL );
141
142                         ber_bvecfree( refs );
143                         ch_free( matched_dn );
144
145                         goto done;
146                 }
147
148                 if ( ! access_allowed( be, conn, op, p,
149                         children, NULL, ACL_WRITE ) )
150                 {
151                         Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
152                                 0, 0, 0 );
153                         rc = LDAP_INSUFFICIENT_ACCESS;
154                         text = "no write access to parent";
155                         goto return_results;;
156                 }
157
158                 if ( is_entry_alias( p ) ) {
159                         /* parent is an alias, don't allow add */
160                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
161                                 0, 0, 0 );
162                         rc = LDAP_ALIAS_PROBLEM;
163                         text = "parent is an alias";
164                         goto return_results;;
165                 }
166
167                 if ( is_entry_referral( p ) ) {
168                         /* parent is a referral, don't allow add */
169                         char *matched_dn = ch_strdup( p->e_dn );
170                         struct berval **refs = is_entry_referral( p )
171                                 ? get_entry_referrals( be, conn, op, p,
172                                         e->e_dn, LDAP_SCOPE_DEFAULT )
173                                 : NULL;
174
175                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
176                                 0, 0, 0 );
177
178                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
179                                 matched_dn, NULL, refs, NULL );
180
181                         ber_bvecfree( refs );
182                         free( matched_dn );
183                         goto done;
184                 }
185
186                 /* free parent and writer lock */
187                 bdb_entry_return( be, p );
188                 p = NULL;
189
190         } else {
191                 if( pdn != NULL ) {
192                         free(pdn);
193                 }
194
195                 /*
196                  * no parent!
197                  *      must be adding entry to at suffix
198                  *  or with parent ""
199                  */
200                 if ( !be_isroot( be, op->o_ndn )) {
201                         if ( be_issuffix( be, "" ) || be_isupdate( be, op->o_ndn ) ) {
202
203                                 p = (Entry *)&slap_entry_root;
204
205                                 /* check parent for "children" acl */
206                                 rc = access_allowed( be, conn, op, p,
207                                         children, NULL, ACL_WRITE );
208                                 p = NULL;
209
210                                 if ( ! rc ) {
211                                         Debug( LDAP_DEBUG_TRACE,
212                                                 "bdb_add: no write access to parent\n",
213                                                 0, 0, 0 );
214                                         rc = LDAP_INSUFFICIENT_ACCESS;
215                                         text = "no write access to parent";
216                                         goto return_results;;
217                                 }
218
219                         } else {
220                                 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
221                                         pdn == NULL ? "suffix" : "entry at root",
222                                         0, 0 );
223                                 rc = LDAP_INSUFFICIENT_ACCESS;
224                                 goto return_results;
225                         }
226                 }
227         }
228
229         /* dn2id index */
230         rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
231         if ( rc != 0 ) {
232                 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
233                         db_strerror(rc), rc, 0 );
234
235                 switch( rc ) {
236                 case DB_LOCK_DEADLOCK:
237                 case DB_LOCK_NOTGRANTED:
238                         goto retry;
239                 case DB_KEYEXIST:
240                         rc = LDAP_ALREADY_EXISTS;
241                         break;
242                 default:
243                         rc = LDAP_OTHER;
244                 }
245                 goto return_results;
246         }
247
248         /* id2entry index */
249         rc = bdb_id2entry_add( be, ltid, e );
250         if ( rc != 0 ) {
251                 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
252                         0, 0, 0 );
253                 switch( rc ) {
254                 case DB_LOCK_DEADLOCK:
255                 case DB_LOCK_NOTGRANTED:
256                         goto retry;
257                 default:
258                         rc = LDAP_OTHER;
259                 }
260                 text = "entry store failed";
261                 goto return_results;
262         }
263
264         /* attribute indexes */
265         rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
266         if ( rc != LDAP_SUCCESS ) {
267                 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
268                         0, 0, 0 );
269                 switch( rc ) {
270                 case DB_LOCK_DEADLOCK:
271                 case DB_LOCK_NOTGRANTED:
272                         goto retry;
273                 default:
274                         rc = LDAP_OTHER;
275                 }
276                 text = "index generation failed";
277                 goto return_results;
278         }
279
280         if( bdb->bi_txn ) {
281                 rc = txn_commit( ltid, 0 );
282         }
283         ltid = NULL;
284         op->o_private = NULL;
285
286         if( rc != 0 ) {
287                 Debug( LDAP_DEBUG_TRACE,
288                         "bdb_add: txn_commit failed: %s (%d)\n",
289                         db_strerror(rc), rc, 0 );
290                 rc = LDAP_OTHER;
291                 text = "commit failed";
292
293         } else {
294                 Debug( LDAP_DEBUG_TRACE,
295                         "bdb_add: added id=%08lx dn=\"%s\"\n",
296                         e->e_id, e->e_dn, 0 );
297                 rc = LDAP_SUCCESS;
298                 text = NULL;
299         }
300
301 return_results:
302         send_ldap_result( conn, op, rc,
303                 NULL, text, NULL, NULL );
304
305         if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
306                 ldap_pvt_thread_yield();
307                 txn_checkpoint( bdb->bi_dbenv,
308                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
309         }
310
311 done:
312         if (p != NULL) {
313                 /* free parent and writer lock */
314                 bdb_entry_return( be, p ); 
315         }
316
317         if( ltid != NULL ) {
318                 txn_abort( ltid );
319                 op->o_private = NULL;
320         }
321
322         return rc;
323 }