]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
Catch nextid failures.
[openldap] / servers / slapd / back-ldbm / add.c
1 /* add.c - ldap ldbm 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
12 #include <ac/socket.h>
13 #include <ac/string.h>
14
15 #include "slap.h"
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
18
19 int
20 ldbm_back_add(
21     Backend     *be,
22     Connection  *conn,
23     Operation   *op,
24     Entry       *e
25 )
26 {
27         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
28         char            *pdn;
29         Entry           *p = NULL;
30         int                     rootlock = 0;
31         int                     rc; 
32         const char      *text = NULL;
33         AttributeDescription *children = slap_schema.si_ad_children;
34
35
36         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", e->e_dn, 0, 0);
37
38         /* nobody else can add until we lock our parent */
39         ldap_pvt_thread_mutex_lock(&li->li_add_mutex);
40
41         if ( ( dn2id( be, e->e_ndn ) ) != NOID ) {
42                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
43                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
44                         NULL, NULL, NULL, NULL );
45                 return( -1 );
46         }
47
48         rc = entry_schema_check( e, NULL, &text );
49
50         if ( rc != LDAP_SUCCESS ) {
51                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
52
53                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
54                         text, 0, 0 );
55
56                 send_ldap_result( conn, op, rc,
57                         NULL, text, NULL, NULL );
58                 return( -1 );
59         }
60
61         /*
62          * Get the parent dn and see if the corresponding entry exists.
63          * If the parent does not exist, only allow the "root" user to
64          * add the entry.
65          */
66
67         pdn = dn_parent( be, e->e_ndn );
68
69         if( pdn != NULL && *pdn != '\0' ) {
70                 Entry *matched = NULL;
71
72                 assert( *pdn != '\0' );
73
74                 /* get parent with writer lock */
75                 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
76                         char *matched_dn;
77                         struct berval **refs;
78
79                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
80
81                         if ( matched != NULL ) {
82                                 matched_dn = ch_strdup( matched->e_dn );
83                                 refs = is_entry_referral( matched )
84                                         ? get_entry_referrals( be, conn, op, matched )
85                                         : NULL;
86                                 cache_return_entry_r( &li->li_cache, matched );
87
88                         } else {
89                                 matched_dn = NULL;
90                                 refs = default_referral;
91                         }
92
93                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
94                                 0, 0, 0 );
95
96                         send_ldap_result( conn, op, LDAP_REFERRAL,
97                             matched_dn, NULL, refs, NULL );
98
99                         if( matched != NULL ) {
100                                 ber_bvecfree( refs );
101                                 free( matched_dn );
102                         }
103
104                         free( pdn );
105                         return -1;
106                 }
107
108                 /* don't need the add lock anymore */
109                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
110
111                 free(pdn);
112
113                 if ( ! access_allowed( be, conn, op, p,
114                         children, NULL, ACL_WRITE ) )
115                 {
116                         /* free parent and writer lock */
117                         cache_return_entry_w( &li->li_cache, p ); 
118
119                         Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
120                             0, 0 );
121                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
122                             NULL, "no write access to parent", NULL, NULL );
123
124
125                         return -1;
126                 }
127
128                 if ( is_entry_alias( p ) ) {
129                         /* parent is an alias, don't allow add */
130
131                         /* free parent and writer lock */
132                         cache_return_entry_w( &li->li_cache, p );
133
134                         Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
135                             0, 0 );
136
137                         send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
138                             NULL, "parent is an alias", NULL, NULL );
139
140                         return -1;
141                 }
142
143                 if ( is_entry_referral( p ) ) {
144                         /* parent is a referral, don't allow add */
145                         char *matched_dn = ch_strdup( p->e_dn );
146                         struct berval **refs = is_entry_referral( p )
147                                 ? get_entry_referrals( be, conn, op, p )
148                                 : NULL;
149
150                         /* free parent and writer lock */
151                         cache_return_entry_w( &li->li_cache, p );
152
153                         Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
154                             0, 0 );
155                         send_ldap_result( conn, op, LDAP_REFERRAL,
156                             matched_dn, NULL, refs, NULL );
157
158                         ber_bvecfree( refs );
159                         free( matched_dn );
160                         return -1;
161                 }
162
163         } else {
164                 if(pdn != NULL) {
165                         assert( *pdn == '\0' );
166                         free(pdn);
167                 }
168
169                 /* no parent, must be adding entry to root */
170                 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix( be, "" ) ) {
171                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
172
173                         Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
174                                         pdn == NULL ? "suffix" : "entry at root",
175                                         0, 0 );
176
177                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
178                             NULL, NULL, NULL, NULL );
179
180                         return -1;
181                 }
182
183                 /*
184                  * no parent, acquire the root write lock
185                  * and release the add lock.
186                  */
187                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
188                 rootlock = 1;
189                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
190         }
191
192         e->e_id = next_id( be );
193
194         if( e->e_id == NOID ) {
195                 if( p != NULL) {
196                         /* free parent and writer lock */
197                         cache_return_entry_w( &li->li_cache, p ); 
198                 }
199
200                 if ( rootlock ) {
201                         /* release root lock */
202                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
203                 }
204
205                 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
206                         0, 0, 0 );
207
208                 send_ldap_result( conn, op, LDAP_OTHER,
209                         NULL, "next_id add failed", NULL, NULL );
210
211                 return( -1 );
212         }
213
214         /*
215          * Try to add the entry to the cache, assign it a new dnid.
216          */
217         rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
218
219         if ( rc != 0 ) {
220                 if( p != NULL) {
221                         /* free parent and writer lock */
222                         cache_return_entry_w( &li->li_cache, p ); 
223                 }
224
225                 if ( rootlock ) {
226                         /* release root lock */
227                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
228                 }
229
230                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
231                     0 );
232
233                 send_ldap_result( conn, op,
234                         rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
235                         NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
236
237                 return( -1 );
238         }
239
240         rc = -1;
241
242         /* attribute indexes */
243         if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
244                 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
245                     0, 0 );
246                 send_ldap_result( conn, op, LDAP_OTHER,
247                         NULL, "index generation failed", NULL, NULL );
248
249                 goto return_results;
250         }
251
252         /* dn2id index */
253         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
254                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
255                     0, 0 );
256                 send_ldap_result( conn, op, LDAP_OTHER,
257                         NULL, "DN index generation failed", NULL, NULL );
258
259                 goto return_results;
260         }
261
262         /* id2entry index */
263         if ( id2entry_add( be, e ) != 0 ) {
264                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
265                     0, 0 );
266                 (void) dn2id_delete( be, e->e_ndn, e->e_id );
267                 send_ldap_result( conn, op, LDAP_OTHER,
268                         NULL, "entry store failed", NULL, NULL );
269
270                 goto return_results;
271         }
272
273         send_ldap_result( conn, op, LDAP_SUCCESS,
274                 NULL, NULL, NULL, NULL );
275         rc = 0;
276
277 return_results:;
278         if (p != NULL) {
279                 /* free parent and writer lock */
280                 cache_return_entry_w( &li->li_cache, p ); 
281         }
282
283         if ( rootlock ) {
284                 /* release root lock */
285                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
286         }
287
288         if ( rc ) {
289                 /* free entry and writer lock */
290                 cache_return_entry_w( &li->li_cache, e );
291         }
292
293         return( rc );
294 }