]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
Update copyright statements
[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
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.bv_val = dn_parent( be, e->e_ndn );
97         if (pdn.bv_val && *pdn.bv_val)
98                 pdn.bv_len = e->e_nname.bv_len - (pdn.bv_val - e->e_ndn);
99         else
100                 pdn.bv_len = 0;
101
102         if( pdn.bv_len != 0 ) {
103                 Entry *matched = NULL;
104
105                 /* get parent */
106                 rc = bdb_dn2entry( be, ltid, &pdn, &p, &matched, 0 );
107
108                 switch( rc ) {
109                 case 0:
110                 case DB_NOTFOUND:
111                         break;
112                 case DB_LOCK_DEADLOCK:
113                 case DB_LOCK_NOTGRANTED:
114                         goto retry;
115                 default:
116                         rc = LDAP_OTHER;
117                         text = "internal error";
118                         goto return_results;
119                 }
120
121                 if ( p == NULL ) {
122                         char *matched_dn = NULL;
123                         BVarray refs;
124
125                         if ( matched != NULL ) {
126                                 matched_dn = ch_strdup( matched->e_dn );
127                                 refs = is_entry_referral( matched )
128                                         ? get_entry_referrals( be, conn, op, matched )
129                                         : NULL;
130                                 bdb_entry_return( be, matched );
131                                 matched = NULL;
132
133                         } else {
134                                 refs = referral_rewrite( default_referral,
135                                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
136                         }
137
138                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
139                                 0, 0, 0 );
140
141                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
142                                 matched_dn, NULL, refs, NULL );
143
144                         bvarray_free( refs );
145                         ch_free( matched_dn );
146
147                         goto done;
148                 }
149
150                 if ( ! access_allowed( be, conn, op, p,
151                         children, NULL, ACL_WRITE ) )
152                 {
153                         Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
154                                 0, 0, 0 );
155                         rc = LDAP_INSUFFICIENT_ACCESS;
156                         text = "no write access to parent";
157                         goto return_results;;
158                 }
159
160                 if ( is_entry_alias( p ) ) {
161                         /* parent is an alias, don't allow add */
162                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
163                                 0, 0, 0 );
164                         rc = LDAP_ALIAS_PROBLEM;
165                         text = "parent is an alias";
166                         goto return_results;;
167                 }
168
169                 if ( is_entry_referral( p ) ) {
170                         /* parent is a referral, don't allow add */
171                         char *matched_dn = ch_strdup( p->e_dn );
172                         BVarray refs = is_entry_referral( p )
173                                 ? get_entry_referrals( be, conn, op, p )
174                                 : NULL;
175
176                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
177                                 0, 0, 0 );
178
179                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
180                                 matched_dn, NULL, refs, NULL );
181
182                         bvarray_free( refs );
183                         free( matched_dn );
184                         goto done;
185                 }
186
187                 /* free parent and writer lock */
188                 bdb_entry_return( be, p );
189                 p = NULL;
190
191         } else {
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                         if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
199                                 p = (Entry *)&slap_entry_root;
200
201                                 /* check parent for "children" acl */
202                                 rc = access_allowed( be, conn, op, p,
203                                         children, NULL, ACL_WRITE );
204                                 p = NULL;
205
206                                 if ( ! rc ) {
207                                         Debug( LDAP_DEBUG_TRACE,
208                                                 "bdb_add: no write access to parent\n",
209                                                 0, 0, 0 );
210                                         rc = LDAP_INSUFFICIENT_ACCESS;
211                                         text = "no write access to parent";
212                                         goto return_results;;
213                                 }
214
215                         } else {
216                                 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
217                                         pdn.bv_len == 0 ? "suffix" : "entry at root",
218                                         0, 0 );
219                                 rc = LDAP_INSUFFICIENT_ACCESS;
220                                 goto return_results;
221                         }
222                 }
223         }
224
225         /* dn2id index */
226         rc = bdb_dn2id_add( be, ltid, &pdn, e );
227         if ( rc != 0 ) {
228                 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
229                         db_strerror(rc), rc, 0 );
230
231                 switch( rc ) {
232                 case DB_LOCK_DEADLOCK:
233                 case DB_LOCK_NOTGRANTED:
234                         goto retry;
235                 case DB_KEYEXIST:
236                         rc = LDAP_ALREADY_EXISTS;
237                         break;
238                 default:
239                         rc = LDAP_OTHER;
240                 }
241                 goto return_results;
242         }
243
244         /* id2entry index */
245         rc = bdb_id2entry_add( be, ltid, e );
246         if ( rc != 0 ) {
247                 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
248                         0, 0, 0 );
249                 switch( rc ) {
250                 case DB_LOCK_DEADLOCK:
251                 case DB_LOCK_NOTGRANTED:
252                         goto retry;
253                 default:
254                         rc = LDAP_OTHER;
255                 }
256                 text = "entry store failed";
257                 goto return_results;
258         }
259
260         /* attribute indexes */
261         rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
262         if ( rc != LDAP_SUCCESS ) {
263                 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
264                         0, 0, 0 );
265                 switch( rc ) {
266                 case DB_LOCK_DEADLOCK:
267                 case DB_LOCK_NOTGRANTED:
268                         goto retry;
269                 default:
270                         rc = LDAP_OTHER;
271                 }
272                 text = "index generation failed";
273                 goto return_results;
274         }
275
276         if( bdb->bi_txn ) {
277                 rc = txn_commit( ltid, 0 );
278         }
279         ltid = NULL;
280         op->o_private = NULL;
281
282         if( rc != 0 ) {
283                 Debug( LDAP_DEBUG_TRACE,
284                         "bdb_add: txn_commit failed: %s (%d)\n",
285                         db_strerror(rc), rc, 0 );
286                 rc = LDAP_OTHER;
287                 text = "commit failed";
288
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 }