]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
Used API signature from back-bdb; compiles and passes make test
[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         e->e_id = next_id( be );
244
245         if( e->e_id == NOID ) {
246                 if( p != NULL) {
247                         /* free parent and writer lock */
248                         cache_return_entry_w( &li->li_cache, p ); 
249                 }
250
251                 if ( rootlock ) {
252                         /* release root lock */
253                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
254                 }
255
256 #ifdef NEW_LOGGING
257                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
258                            "ldbm_back_add: next_id failed.\n" ));
259 #else
260                 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
261                         0, 0, 0 );
262 #endif
263
264
265                 send_ldap_result( conn, op, LDAP_OTHER,
266                         NULL, "next_id add failed", NULL, NULL );
267
268                 return( -1 );
269         }
270
271         /*
272          * Try to add the entry to the cache, assign it a new dnid.
273          */
274         rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
275
276         if ( rc != 0 ) {
277                 if( p != NULL) {
278                         /* free parent and writer lock */
279                         cache_return_entry_w( &li->li_cache, p ); 
280                 }
281
282                 if ( rootlock ) {
283                         /* release root lock */
284                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
285                 }
286
287 #ifdef NEW_LOGGING
288                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
289                            "ldbm_back_add: cache_add_entry_lock failed.\n" ));
290 #else
291                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
292                     0 );
293 #endif
294
295
296                 send_ldap_result( conn, op,
297                         rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
298                         NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
299
300                 return( -1 );
301         }
302
303         rc = -1;
304
305         /* attribute indexes */
306         if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
307 #ifdef NEW_LOGGING
308                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
309                            "ldbm_back_add: index_entry_add failed.\n" ));
310 #else
311                 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
312                     0, 0 );
313 #endif
314
315                 send_ldap_result( conn, op, LDAP_OTHER,
316                         NULL, "index generation failed", NULL, NULL );
317
318                 goto return_results;
319         }
320
321         /* dn2id index */
322         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
323 #ifdef NEW_LOGGING
324                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
325                            "ldbm_back_add: dn2id_add failed.\n" ));
326 #else
327                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
328                     0, 0 );
329 #endif
330
331                 send_ldap_result( conn, op, LDAP_OTHER,
332                         NULL, "DN index generation failed", NULL, NULL );
333
334                 goto return_results;
335         }
336
337         /* id2entry index */
338         if ( id2entry_add( be, e ) != 0 ) {
339 #ifdef NEW_LOGGING
340                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
341                            "ldbm_back_add: id2entry_add failed.\n" ));
342 #else
343                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
344                     0, 0 );
345 #endif
346
347                 (void) dn2id_delete( be, e->e_ndn, e->e_id );
348                 send_ldap_result( conn, op, LDAP_OTHER,
349                         NULL, "entry store failed", NULL, NULL );
350
351                 goto return_results;
352         }
353
354         send_ldap_result( conn, op, LDAP_SUCCESS,
355                 NULL, NULL, NULL, NULL );
356         rc = 0;
357
358 return_results:;
359         if (p != NULL) {
360                 /* free parent and writer lock */
361                 cache_return_entry_w( &li->li_cache, p ); 
362         }
363
364         if ( rootlock ) {
365                 /* release root lock */
366                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
367         }
368
369         if ( rc ) {
370                 /* free entry and writer lock */
371                 cache_return_entry_w( &li->li_cache, e );
372         }
373
374         return( rc );
375 }