]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
Reworked again the caching in case of failure.
[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         ID               id = NOID;
33         const char      *text = NULL;
34         AttributeDescription *children = slap_schema.si_ad_children;
35         char textbuf[SLAP_TEXT_BUFLEN];
36         size_t textlen = sizeof textbuf;
37
38 #ifdef NEW_LOGGING
39         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,"ldbm_back_add: %s\n",
40                    e->e_dn ));
41 #else
42         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", e->e_dn, 0, 0);
43 #endif
44
45
46         /* nobody else can add until we lock our parent */
47         ldap_pvt_thread_mutex_lock(&li->li_add_mutex);
48
49         if ( ( rc = dn2id( be, e->e_ndn, &id ) ) || id != NOID ) {
50                 /* if (rc) something bad happened to ldbm cache */
51                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
52                 send_ldap_result( conn, op, 
53                         rc ? LDAP_OPERATIONS_ERROR : LDAP_ALREADY_EXISTS,
54                         NULL, NULL, NULL, NULL );
55                 return( -1 );
56         }
57
58         rc = entry_schema_check( e, NULL, &text, textbuf, textlen );
59
60         if ( rc != LDAP_SUCCESS ) {
61                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
62
63 #ifdef NEW_LOGGING
64                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
65                            "ldbm_back_add: entry (%s) failed schema check.\n",
66                            e->e_dn ));
67 #else
68                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
69                         text, 0, 0 );
70 #endif
71
72
73                 send_ldap_result( conn, op, rc,
74                         NULL, text, NULL, NULL );
75                 return( -1 );
76         }
77
78         /*
79          * Get the parent dn and see if the corresponding entry exists.
80          * If the parent does not exist, only allow the "root" user to
81          * add the entry.
82          */
83
84         pdn = dn_parent( be, e->e_ndn );
85
86         if( pdn != NULL && *pdn != '\0' ) {
87                 Entry *matched = NULL;
88
89                 assert( *pdn != '\0' );
90
91                 /* get parent with writer lock */
92                 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
93                         char *matched_dn;
94                         struct berval **refs;
95
96                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
97
98                         if ( matched != NULL ) {
99                                 matched_dn = ch_strdup( matched->e_dn );
100                                 refs = is_entry_referral( matched )
101                                         ? get_entry_referrals( be, conn, op, matched )
102                                         : NULL;
103                                 cache_return_entry_r( &li->li_cache, matched );
104
105                         } else {
106                                 matched_dn = NULL;
107                                 refs = default_referral;
108                         }
109
110 #ifdef NEW_LOGGING
111                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
112                                    "ldbm_back_add: Parent of (%s) does not exist.\n",
113                                    e->e_dn ));
114 #else
115                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
116                                 0, 0, 0 );
117 #endif
118
119
120                         send_ldap_result( conn, op, LDAP_REFERRAL, matched_dn,
121                                 refs == NULL ? "parent does not exist" : "parent is referral",
122                                 refs, NULL );
123
124                         if( matched != NULL ) {
125                                 ber_bvecfree( refs );
126                                 free( matched_dn );
127                         }
128
129                         free( pdn );
130                         return -1;
131                 }
132
133                 /* don't need the add lock anymore */
134                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
135
136                 free(pdn);
137
138                 if ( ! access_allowed( be, conn, op, p,
139                         children, NULL, ACL_WRITE ) )
140                 {
141                         /* free parent and writer lock */
142                         cache_return_entry_w( &li->li_cache, p ); 
143
144 #ifdef NEW_LOGGING
145                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
146                                    "ldbm_back_add: No write access to parent (%s).\n",
147                                    e->e_dn ));
148 #else
149                         Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
150                             0, 0 );
151 #endif
152
153                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
154                             NULL, "no write access to parent", NULL, NULL );
155
156
157                         return -1;
158                 }
159
160                 if ( is_entry_alias( p ) ) {
161                         /* parent is an alias, don't allow add */
162
163                         /* free parent and writer lock */
164                         cache_return_entry_w( &li->li_cache, p );
165
166 #ifdef NEW_LOGGING
167                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
168                                    "ldbm_back_add:  Parent is an alias.\n"));
169 #else
170                         Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
171                             0, 0 );
172 #endif
173
174
175                         send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
176                             NULL, "parent is an alias", NULL, NULL );
177
178                         return -1;
179                 }
180
181                 if ( is_entry_referral( p ) ) {
182                         /* parent is a referral, don't allow add */
183                         char *matched_dn = ch_strdup( p->e_dn );
184                         struct berval **refs = is_entry_referral( p )
185                                 ? get_entry_referrals( be, conn, op, p )
186                                 : NULL;
187
188                         /* free parent and writer lock */
189                         cache_return_entry_w( &li->li_cache, p );
190
191 #ifdef NEW_LOGGING
192                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
193                                    "ldbm_back_add: Parent is referral.\n" ));
194 #else
195                         Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
196                             0, 0 );
197 #endif
198
199                         send_ldap_result( conn, op, LDAP_REFERRAL,
200                             matched_dn, NULL, refs, NULL );
201
202                         ber_bvecfree( refs );
203                         free( matched_dn );
204                         return -1;
205                 }
206
207         } else {
208                 if(pdn != NULL) {
209                         assert( *pdn == '\0' );
210                         free(pdn);
211                 }
212
213                 /* no parent, must be adding entry to root */
214                 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix( be, "" ) ) {
215                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
216
217 #ifdef NEW_LOGGING
218                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
219                                    "ldbm_back_add: %s add denied.\n",
220                                    pdn == NULL ? "suffix" : "entry at root" ));
221 #else
222                         Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
223                                         pdn == NULL ? "suffix" : "entry at root",
224                                         0, 0 );
225 #endif
226
227
228                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
229                             NULL, NULL, NULL, NULL );
230
231                         return -1;
232                 }
233
234                 /*
235                  * no parent, acquire the root write lock
236                  * and release the add lock.
237                  */
238                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
239                 rootlock = 1;
240                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
241         }
242
243         if ( next_id( be, &e->e_id ) ) {
244                 if( p != NULL) {
245                         /* free parent and writer lock */
246                         cache_return_entry_w( &li->li_cache, p ); 
247                 }
248
249                 if ( rootlock ) {
250                         /* release root lock */
251                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
252                 }
253
254 #ifdef NEW_LOGGING
255                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
256                            "ldbm_back_add: next_id failed.\n" ));
257 #else
258                 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
259                         0, 0, 0 );
260 #endif
261
262
263                 send_ldap_result( conn, op, LDAP_OTHER,
264                         NULL, "next_id add failed", NULL, NULL );
265
266                 return( -1 );
267         }
268
269         /*
270          * Try to add the entry to the cache, assign it a new dnid.
271          */
272         rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
273
274         if ( rc != 0 ) {
275                 if( p != NULL) {
276                         /* free parent and writer lock */
277                         cache_return_entry_w( &li->li_cache, p ); 
278                 }
279
280                 if ( rootlock ) {
281                         /* release root lock */
282                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
283                 }
284
285 #ifdef NEW_LOGGING
286                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
287                            "ldbm_back_add: cache_add_entry_lock failed.\n" ));
288 #else
289                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
290                     0 );
291 #endif
292
293
294                 send_ldap_result( conn, op,
295                         rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
296                         NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
297
298                 return( -1 );
299         }
300
301         rc = -1;
302
303         /* attribute indexes */
304         if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
305 #ifdef NEW_LOGGING
306                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
307                            "ldbm_back_add: index_entry_add failed.\n" ));
308 #else
309                 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
310                     0, 0 );
311 #endif
312                 
313                 send_ldap_result( conn, op, LDAP_OTHER,
314                         NULL, "index generation failed", NULL, NULL );
315
316                 goto return_results;
317         }
318
319         /* dn2id index */
320         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
321 #ifdef NEW_LOGGING
322                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
323                            "ldbm_back_add: dn2id_add failed.\n" ));
324 #else
325                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
326                     0, 0 );
327 #endif
328                 /* FIXME: delete attr indices? */
329
330                 send_ldap_result( conn, op, LDAP_OTHER,
331                         NULL, "DN index generation failed", NULL, NULL );
332
333                 goto return_results;
334         }
335
336         /* id2entry index */
337         if ( id2entry_add( be, e ) != 0 ) {
338 #ifdef NEW_LOGGING
339                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
340                            "ldbm_back_add: id2entry_add failed.\n" ));
341 #else
342                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
343                     0, 0 );
344 #endif
345
346                 /* FIXME: delete attr indices? */
347                 (void) dn2id_delete( be, e->e_ndn, e->e_id );
348                 
349                 send_ldap_result( conn, op, LDAP_OTHER,
350                         NULL, "entry store failed", NULL, NULL );
351
352                 goto return_results;
353         }
354
355         send_ldap_result( conn, op, LDAP_SUCCESS,
356                 NULL, NULL, NULL, NULL );
357
358         /* marks the entry as committed, so it is added to the cache;
359          * otherwise it is removed from the cache, but not destroyed;
360          * it will be destroyed by the caller */
361         rc = 0;
362         cache_entry_commit( e );
363
364 return_results:;
365         if (p != NULL) {
366                 /* free parent and writer lock */
367                 cache_return_entry_w( &li->li_cache, p ); 
368         }
369
370         if ( rootlock ) {
371                 /* release root lock */
372                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
373         }
374
375         if ( rc ) {
376                 /* in case of error, writer lock is freed 
377                  * and entry's private data is destroyed */
378                 cache_return_entry_w( &li->li_cache, e );
379         }
380
381         return( rc );
382 }