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