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