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