]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
752c1ab4200e05030b8ccb6a53c4dbaa364b21d7
[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         const char      *text = NULL;
33         AttributeDescription *children = slap_schema.si_ad_children;
34
35
36 #ifdef NEW_LOGGING
37         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,"ldbm_back_add: %s\n",
38                    e->e_dn ));
39 #else
40         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", e->e_dn, 0, 0);
41 #endif
42
43
44         /* nobody else can add until we lock our parent */
45         ldap_pvt_thread_mutex_lock(&li->li_add_mutex);
46
47         if ( ( dn2id( be, e->e_ndn ) ) != NOID ) {
48                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
49                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
50                         NULL, NULL, NULL, NULL );
51                 return( -1 );
52         }
53
54         rc = entry_schema_check( e, NULL, &text );
55
56         if ( rc != LDAP_SUCCESS ) {
57                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
58
59 #ifdef NEW_LOGGING
60                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
61                            "ldbm_back_add: entry (%s) failed schema check.\n",
62                            e->e_dn ));
63 #else
64                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
65                         text, 0, 0 );
66 #endif
67
68
69                 send_ldap_result( conn, op, rc,
70                         NULL, text, NULL, NULL );
71                 return( -1 );
72         }
73
74         /*
75          * Get the parent dn and see if the corresponding entry exists.
76          * If the parent does not exist, only allow the "root" user to
77          * add the entry.
78          */
79
80         pdn = dn_parent( be, e->e_ndn );
81
82         if( pdn != NULL && *pdn != '\0' ) {
83                 Entry *matched = NULL;
84
85                 assert( *pdn != '\0' );
86
87                 /* get parent with writer lock */
88                 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
89                         char *matched_dn;
90                         struct berval **refs;
91
92                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
93
94                         if ( matched != NULL ) {
95                                 matched_dn = ch_strdup( matched->e_dn );
96                                 refs = is_entry_referral( matched )
97                                         ? get_entry_referrals( be, conn, op, matched )
98                                         : NULL;
99                                 cache_return_entry_r( &li->li_cache, matched );
100
101                         } else {
102                                 matched_dn = NULL;
103                                 refs = default_referral;
104                         }
105
106 #ifdef NEW_LOGGING
107                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
108                                    "ldbm_back_add: Parent of (%s) does not exist.\n",
109                                    e->e_dn ));
110 #else
111                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
112                                 0, 0, 0 );
113 #endif
114
115
116                         send_ldap_result( conn, op, LDAP_REFERRAL, matched_dn,
117                                 refs == NULL ? "parent does not exist" : "parent is referral",
118                                 refs, NULL );
119
120                         if( matched != NULL ) {
121                                 ber_bvecfree( refs );
122                                 free( matched_dn );
123                         }
124
125                         free( pdn );
126                         return -1;
127                 }
128
129                 /* don't need the add lock anymore */
130                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
131
132                 free(pdn);
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                         struct berval **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_bvecfree( refs );
199                         free( matched_dn );
200                         return -1;
201                 }
202
203         } else {
204                 if(pdn != NULL) {
205                         assert( *pdn == '\0' );
206                         free(pdn);
207                 }
208
209                 /* no parent, must be adding entry to root */
210                 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix( be, "" ) ) {
211                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
212
213 #ifdef NEW_LOGGING
214                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
215                                    "ldbm_back_add: %s add denied.\n",
216                                    pdn == NULL ? "suffix" : "entry at root" ));
217 #else
218                         Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
219                                         pdn == NULL ? "suffix" : "entry at root",
220                                         0, 0 );
221 #endif
222
223
224                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
225                             NULL, NULL, NULL, NULL );
226
227                         return -1;
228                 }
229
230                 /*
231                  * no parent, acquire the root write lock
232                  * and release the add lock.
233                  */
234                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
235                 rootlock = 1;
236                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
237         }
238
239         e->e_id = next_id( be );
240
241         if( e->e_id == NOID ) {
242                 if( p != NULL) {
243                         /* free parent and writer lock */
244                         cache_return_entry_w( &li->li_cache, p ); 
245                 }
246
247                 if ( rootlock ) {
248                         /* release root lock */
249                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
250                 }
251
252 #ifdef NEW_LOGGING
253                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
254                            "ldbm_back_add: next_id failed.\n" ));
255 #else
256                 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
257                         0, 0, 0 );
258 #endif
259
260
261                 send_ldap_result( conn, op, LDAP_OTHER,
262                         NULL, "next_id add failed", NULL, NULL );
263
264                 return( -1 );
265         }
266
267         /*
268          * Try to add the entry to the cache, assign it a new dnid.
269          */
270         rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
271
272         if ( rc != 0 ) {
273                 if( p != NULL) {
274                         /* free parent and writer lock */
275                         cache_return_entry_w( &li->li_cache, p ); 
276                 }
277
278                 if ( rootlock ) {
279                         /* release root lock */
280                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
281                 }
282
283 #ifdef NEW_LOGGING
284                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
285                            "ldbm_back_add: cache_add_entry_lock failed.\n" ));
286 #else
287                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
288                     0 );
289 #endif
290
291
292                 send_ldap_result( conn, op,
293                         rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
294                         NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
295
296                 return( -1 );
297         }
298
299         rc = -1;
300
301         /* attribute indexes */
302         if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
303 #ifdef NEW_LOGGING
304                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
305                            "ldbm_back_add: index_entry_add failed.\n" ));
306 #else
307                 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
308                     0, 0 );
309 #endif
310
311                 send_ldap_result( conn, op, LDAP_OTHER,
312                         NULL, "index generation failed", NULL, NULL );
313
314                 goto return_results;
315         }
316
317         /* dn2id index */
318         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
319 #ifdef NEW_LOGGING
320                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
321                            "ldbm_back_add: dn2id_add failed.\n" ));
322 #else
323                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
324                     0, 0 );
325 #endif
326
327                 send_ldap_result( conn, op, LDAP_OTHER,
328                         NULL, "DN index generation failed", NULL, NULL );
329
330                 goto return_results;
331         }
332
333         /* id2entry index */
334         if ( id2entry_add( be, e ) != 0 ) {
335 #ifdef NEW_LOGGING
336                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
337                            "ldbm_back_add: id2entry_add failed.\n" ));
338 #else
339                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
340                     0, 0 );
341 #endif
342
343                 (void) dn2id_delete( be, e->e_ndn, e->e_id );
344                 send_ldap_result( conn, op, LDAP_OTHER,
345                         NULL, "entry store failed", NULL, NULL );
346
347                 goto return_results;
348         }
349
350         send_ldap_result( conn, op, LDAP_SUCCESS,
351                 NULL, NULL, NULL, NULL );
352         rc = 0;
353
354 return_results:;
355         if (p != NULL) {
356                 /* free parent and writer lock */
357                 cache_return_entry_w( &li->li_cache, p ); 
358         }
359
360         if ( rootlock ) {
361                 /* release root lock */
362                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
363         }
364
365         if ( rc ) {
366                 /* free entry and writer lock */
367                 cache_return_entry_w( &li->li_cache, e );
368         }
369
370         return( rc );
371 }