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