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