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