]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
separate ID return value form return status in dn2id (back-ldbm/dn2id.c)
[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, rc_id; 
32         const char      *text = NULL;
33         AttributeDescription *children = slap_schema.si_ad_children;
34         char textbuf[SLAP_TEXT_BUFLEN];
35         size_t textlen = sizeof textbuf;
36
37 #ifdef NEW_LOGGING
38         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,"ldbm_back_add: %s\n",
39                    e->e_dn ));
40 #else
41         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", e->e_dn, 0, 0);
42 #endif
43
44
45         /* nobody else can add until we lock our parent */
46         ldap_pvt_thread_mutex_lock(&li->li_add_mutex);
47
48         if ( ( dn2id( be, e->e_ndn, &rc_id ) ) != NOID || rc_id ) {
49                 /* if (rc_id) something bad happened to ldbm cache */
50                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
51                 send_ldap_result( conn, op, 
52                         rc_id ? 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;
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                                 matched_dn = NULL;
106                                 refs = default_referral;
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
119                         send_ldap_result( conn, op, LDAP_REFERRAL, matched_dn,
120                                 refs == NULL ? "parent does not exist" : "parent is referral",
121                                 refs, NULL );
122
123                         if( matched != NULL ) {
124                                 ber_bvecfree( refs );
125                                 free( matched_dn );
126                         }
127
128                         free( pdn );
129                         return -1;
130                 }
131
132                 /* don't need the add lock anymore */
133                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
134
135                 free(pdn);
136
137                 if ( ! access_allowed( be, conn, op, p,
138                         children, NULL, ACL_WRITE ) )
139                 {
140                         /* free parent and writer lock */
141                         cache_return_entry_w( &li->li_cache, p ); 
142
143 #ifdef NEW_LOGGING
144                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
145                                    "ldbm_back_add: No write access to parent (%s).\n",
146                                    e->e_dn ));
147 #else
148                         Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
149                             0, 0 );
150 #endif
151
152                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
153                             NULL, "no write access to parent", NULL, NULL );
154
155
156                         return -1;
157                 }
158
159                 if ( is_entry_alias( p ) ) {
160                         /* parent is an alias, don't allow add */
161
162                         /* free parent and writer lock */
163                         cache_return_entry_w( &li->li_cache, p );
164
165 #ifdef NEW_LOGGING
166                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
167                                    "ldbm_back_add:  Parent is an alias.\n"));
168 #else
169                         Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
170                             0, 0 );
171 #endif
172
173
174                         send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
175                             NULL, "parent is an alias", NULL, NULL );
176
177                         return -1;
178                 }
179
180                 if ( is_entry_referral( p ) ) {
181                         /* parent is a referral, don't allow add */
182                         char *matched_dn = ch_strdup( p->e_dn );
183                         struct berval **refs = is_entry_referral( p )
184                                 ? get_entry_referrals( be, conn, op, p )
185                                 : NULL;
186
187                         /* free parent and writer lock */
188                         cache_return_entry_w( &li->li_cache, p );
189
190 #ifdef NEW_LOGGING
191                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
192                                    "ldbm_back_add: Parent is referral.\n" ));
193 #else
194                         Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
195                             0, 0 );
196 #endif
197
198                         send_ldap_result( conn, op, LDAP_REFERRAL,
199                             matched_dn, NULL, refs, NULL );
200
201                         ber_bvecfree( refs );
202                         free( matched_dn );
203                         return -1;
204                 }
205
206         } else {
207                 if(pdn != NULL) {
208                         assert( *pdn == '\0' );
209                         free(pdn);
210                 }
211
212                 /* no parent, must be adding entry to root */
213                 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix( be, "" ) ) {
214                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
215
216 #ifdef NEW_LOGGING
217                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
218                                    "ldbm_back_add: %s add denied.\n",
219                                    pdn == NULL ? "suffix" : "entry at root" ));
220 #else
221                         Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
222                                         pdn == NULL ? "suffix" : "entry at root",
223                                         0, 0 );
224 #endif
225
226
227                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
228                             NULL, NULL, NULL, NULL );
229
230                         return -1;
231                 }
232
233                 /*
234                  * no parent, acquire the root write lock
235                  * and release the add lock.
236                  */
237                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
238                 rootlock = 1;
239                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
240         }
241
242         e->e_id = next_id( be );
243
244         if( e->e_id == NOID ) {
245                 if( p != NULL) {
246                         /* free parent and writer lock */
247                         cache_return_entry_w( &li->li_cache, p ); 
248                 }
249
250                 if ( rootlock ) {
251                         /* release root lock */
252                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
253                 }
254
255 #ifdef NEW_LOGGING
256                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
257                            "ldbm_back_add: next_id failed.\n" ));
258 #else
259                 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
260                         0, 0, 0 );
261 #endif
262
263
264                 send_ldap_result( conn, op, LDAP_OTHER,
265                         NULL, "next_id add failed", NULL, NULL );
266
267                 return( -1 );
268         }
269
270         /*
271          * Try to add the entry to the cache, assign it a new dnid.
272          */
273         rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
274
275         if ( rc != 0 ) {
276                 if( p != NULL) {
277                         /* free parent and writer lock */
278                         cache_return_entry_w( &li->li_cache, p ); 
279                 }
280
281                 if ( rootlock ) {
282                         /* release root lock */
283                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
284                 }
285
286 #ifdef NEW_LOGGING
287                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
288                            "ldbm_back_add: cache_add_entry_lock failed.\n" ));
289 #else
290                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
291                     0 );
292 #endif
293
294
295                 send_ldap_result( conn, op,
296                         rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
297                         NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
298
299                 return( -1 );
300         }
301
302         rc = -1;
303
304         /* attribute indexes */
305         if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
306 #ifdef NEW_LOGGING
307                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
308                            "ldbm_back_add: index_entry_add failed.\n" ));
309 #else
310                 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
311                     0, 0 );
312 #endif
313
314                 send_ldap_result( conn, op, LDAP_OTHER,
315                         NULL, "index generation failed", NULL, NULL );
316
317                 goto return_results;
318         }
319
320         /* dn2id index */
321         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
322 #ifdef NEW_LOGGING
323                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
324                            "ldbm_back_add: dn2id_add failed.\n" ));
325 #else
326                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
327                     0, 0 );
328 #endif
329
330                 send_ldap_result( conn, op, LDAP_OTHER,
331                         NULL, "DN index generation failed", NULL, NULL );
332
333                 goto return_results;
334         }
335
336         /* id2entry index */
337         if ( id2entry_add( be, e ) != 0 ) {
338 #ifdef NEW_LOGGING
339                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
340                            "ldbm_back_add: id2entry_add failed.\n" ));
341 #else
342                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
343                     0, 0 );
344 #endif
345
346                 (void) dn2id_delete( be, e->e_ndn, e->e_id );
347                 send_ldap_result( conn, op, LDAP_OTHER,
348                         NULL, "entry store failed", NULL, NULL );
349
350                 goto return_results;
351         }
352
353         send_ldap_result( conn, op, LDAP_SUCCESS,
354                 NULL, NULL, NULL, NULL );
355         rc = 0;
356
357 return_results:;
358         if (p != NULL) {
359                 /* free parent and writer lock */
360                 cache_return_entry_w( &li->li_cache, p ); 
361         }
362
363         if ( rootlock ) {
364                 /* release root lock */
365                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
366         }
367
368         if ( rc ) {
369                 /* free entry and writer lock */
370                 cache_return_entry_w( &li->li_cache, e );
371         }
372
373         return( rc );
374 }