]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
0753f1611bf0eeeb96684fc79a3f23b92e19ce8d
[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 = NULL;
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,
123                                         matched, e->e_dn, LDAP_SCOPE_DEFAULT )
124                                         : NULL;
125                                 bdb_entry_return( be, matched );
126                                 matched = NULL;
127
128                         } else {
129                                 refs = referral_rewrite( default_referral,
130                                         NULL, e->e_dn, LDAP_SCOPE_DEFAULT );
131                         }
132
133                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
134                                 0, 0, 0 );
135
136                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
137                                 matched_dn, NULL, refs, NULL );
138
139                         ber_bvecfree( refs );
140                         ch_free( matched_dn );
141
142                         goto done;
143                 }
144
145                 if ( ! access_allowed( be, conn, op, p,
146                         children, NULL, ACL_WRITE ) )
147                 {
148                         Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
149                                 0, 0, 0 );
150                         rc = LDAP_INSUFFICIENT_ACCESS;
151                         text = "no write access to parent";
152                         goto return_results;;
153                 }
154
155                 if ( is_entry_alias( p ) ) {
156                         /* parent is an alias, don't allow add */
157                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
158                                 0, 0, 0 );
159                         rc = LDAP_ALIAS_PROBLEM;
160                         text = "parent is an alias";
161                         goto return_results;;
162                 }
163
164                 if ( is_entry_referral( p ) ) {
165                         /* parent is a referral, don't allow add */
166                         char *matched_dn = ch_strdup( p->e_dn );
167                         struct berval **refs = is_entry_referral( p )
168                                 ? get_entry_referrals( be, conn, op, p,
169                                         e->e_dn, LDAP_SCOPE_DEFAULT )
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                         if ( be_issuffix( be, "" ) ) {
199                                 static const Entry rootp = { NOID, "", "", NULL, NULL };
200
201                                 p = (Entry *)&rootp;
202
203                                 /* check parent for "children" acl */
204                                 rc = access_allowed( be, conn, op, p,
205                                         children, NULL, ACL_WRITE );
206                                 p = NULL;
207
208                                 if ( ! rc ) {
209                                         Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
210                                                 0, 0, 0 );
211                                         rc = LDAP_INSUFFICIENT_ACCESS;
212                                         text = "no write access to parent";
213                                         goto return_results;;
214                                 }
215
216                         } else {
217                                 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
218                                         pdn == NULL ? "suffix" : "entry at root",
219                                         0, 0 );
220                                 rc = LDAP_INSUFFICIENT_ACCESS;
221                                 goto return_results;
222                         }
223                 }
224         }
225
226         /* dn2id index */
227         rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
228         if ( rc != 0 ) {
229                 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
230                         db_strerror(rc), rc, 0 );
231
232                 switch( rc ) {
233                 case DB_LOCK_DEADLOCK:
234                 case DB_LOCK_NOTGRANTED:
235                         goto retry;
236                 case DB_KEYEXIST:
237                         rc = LDAP_ALREADY_EXISTS;
238                         break;
239                 default:
240                         rc = LDAP_OTHER;
241                 }
242                 goto return_results;
243         }
244
245         /* id2entry index */
246         rc = bdb_id2entry_add( be, ltid, e );
247         if ( rc != 0 ) {
248                 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
249                         0, 0, 0 );
250                 switch( rc ) {
251                 case DB_LOCK_DEADLOCK:
252                 case DB_LOCK_NOTGRANTED:
253                         goto retry;
254                 default:
255                         rc = LDAP_OTHER;
256                 }
257                 text = "entry store failed";
258                 goto return_results;
259         }
260
261         /* attribute indexes */
262         if ( bdb_index_entry_add( be, ltid, e, e->e_attrs ) != 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         rc = txn_commit( ltid, 0 );
277         ltid = NULL;
278         op->o_private = NULL;
279
280         if( rc != 0 ) {
281                 Debug( LDAP_DEBUG_TRACE,
282                         "bdb_add: txn_commit failed: %s (%d)\n",
283                         db_strerror(rc), rc, 0 );
284                 rc = LDAP_OTHER;
285                 text = "commit failed";
286         } else {
287                 Debug( LDAP_DEBUG_TRACE,
288                         "bdb_add: added id=%08lx dn=\"%s\"\n",
289                         e->e_id, e->e_dn, 0 );
290                 rc = LDAP_SUCCESS;
291                 text = NULL;
292         }
293
294 return_results:
295         send_ldap_result( conn, op, rc,
296                 NULL, text, NULL, NULL );
297
298         if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
299                 ldap_pvt_thread_yield();
300                 txn_checkpoint( bdb->bi_dbenv,
301                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
302         }
303
304 done:
305         if (p != NULL) {
306                 /* free parent and writer lock */
307                 bdb_entry_return( be, p ); 
308         }
309
310         if( ltid != NULL ) {
311                 txn_abort( ltid );
312                 op->o_private = NULL;
313         }
314
315         return rc;
316 }