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