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