]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
33a2e2f08eb63b878394b637e0a990ebe4900d2b
[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
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, matched )
125                                         : NULL;
126                                 bdb_entry_return( be, matched );
127                                 matched = NULL;
128
129                         } else {
130                                 refs = referral_rewrite( default_referral,
131                                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
132                         }
133
134                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
135                                 0, 0, 0 );
136
137                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
138                                 matched_dn, NULL, refs, NULL );
139
140                         ber_bvecfree( refs );
141                         ch_free( matched_dn );
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";
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                 /*
189                  * no parent!
190                  *      must be adding entry to at suffix
191                  *  or with parent ""
192                  */
193                 if ( !be_isroot( be, &op->o_ndn )) {
194                         if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
195                                 p = (Entry *)&slap_entry_root;
196
197                                 /* check parent for "children" acl */
198                                 rc = access_allowed( be, conn, op, p,
199                                         children, NULL, ACL_WRITE );
200                                 p = NULL;
201
202                                 if ( ! rc ) {
203                                         Debug( LDAP_DEBUG_TRACE,
204                                                 "bdb_add: no write access to parent\n",
205                                                 0, 0, 0 );
206                                         rc = LDAP_INSUFFICIENT_ACCESS;
207                                         text = "no write access to parent";
208                                         goto return_results;;
209                                 }
210
211                         } else {
212                                 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
213                                         pdn == NULL ? "suffix" : "entry at root",
214                                         0, 0 );
215                                 rc = LDAP_INSUFFICIENT_ACCESS;
216                                 goto return_results;
217                         }
218                 }
219         }
220
221         /* dn2id index */
222         rc = bdb_dn2id_add( be, ltid, pdn, e );
223         if ( rc != 0 ) {
224                 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
225                         db_strerror(rc), rc, 0 );
226
227                 switch( rc ) {
228                 case DB_LOCK_DEADLOCK:
229                 case DB_LOCK_NOTGRANTED:
230                         goto retry;
231                 case DB_KEYEXIST:
232                         rc = LDAP_ALREADY_EXISTS;
233                         break;
234                 default:
235                         rc = LDAP_OTHER;
236                 }
237                 goto return_results;
238         }
239
240         /* id2entry index */
241         rc = bdb_id2entry_add( be, ltid, e );
242         if ( rc != 0 ) {
243                 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
244                         0, 0, 0 );
245                 switch( rc ) {
246                 case DB_LOCK_DEADLOCK:
247                 case DB_LOCK_NOTGRANTED:
248                         goto retry;
249                 default:
250                         rc = LDAP_OTHER;
251                 }
252                 text = "entry store failed";
253                 goto return_results;
254         }
255
256         /* attribute indexes */
257         rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
258         if ( rc != LDAP_SUCCESS ) {
259                 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
260                         0, 0, 0 );
261                 switch( rc ) {
262                 case DB_LOCK_DEADLOCK:
263                 case DB_LOCK_NOTGRANTED:
264                         goto retry;
265                 default:
266                         rc = LDAP_OTHER;
267                 }
268                 text = "index generation failed";
269                 goto return_results;
270         }
271
272         if( bdb->bi_txn ) {
273                 rc = txn_commit( ltid, 0 );
274         }
275         ltid = NULL;
276         op->o_private = NULL;
277
278         if( rc != 0 ) {
279                 Debug( LDAP_DEBUG_TRACE,
280                         "bdb_add: txn_commit failed: %s (%d)\n",
281                         db_strerror(rc), rc, 0 );
282                 rc = LDAP_OTHER;
283                 text = "commit failed";
284
285         } else {
286                 Debug( LDAP_DEBUG_TRACE,
287                         "bdb_add: added id=%08lx dn=\"%s\"\n",
288                         e->e_id, e->e_dn, 0 );
289                 rc = LDAP_SUCCESS;
290                 text = NULL;
291         }
292
293 return_results:
294         send_ldap_result( conn, op, rc,
295                 NULL, text, NULL, NULL );
296
297         if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
298                 ldap_pvt_thread_yield();
299                 TXN_CHECKPOINT( bdb->bi_dbenv,
300                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
301         }
302
303 done:
304         if (p != NULL) {
305                 /* free parent and writer lock */
306                 bdb_entry_return( be, p ); 
307         }
308
309         if( ltid != NULL ) {
310                 txn_abort( ltid );
311                 op->o_private = NULL;
312         }
313
314         return rc;
315 }