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