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