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