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