]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
fix format for new logging
[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,
117                             matched_dn, NULL, refs, NULL );
118
119                         if( matched != NULL ) {
120                                 ber_bvecfree( refs );
121                                 free( matched_dn );
122                         }
123
124                         free( pdn );
125                         return -1;
126                 }
127
128                 /* don't need the add lock anymore */
129                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
130
131                 free(pdn);
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                         struct berval **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                         ber_bvecfree( refs );
198                         free( matched_dn );
199                         return -1;
200                 }
201
202         } else {
203                 if(pdn != NULL) {
204                         assert( *pdn == '\0' );
205                         free(pdn);
206                 }
207
208                 /* no parent, must be adding entry to root */
209                 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix( be, "" ) ) {
210                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
211
212 #ifdef NEW_LOGGING
213                         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
214                                    "ldbm_back_add: %s add denied.\n",
215                                    pdn == NULL ? "suffix" : "entry at root" ));
216 #else
217                         Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
218                                         pdn == NULL ? "suffix" : "entry at root",
219                                         0, 0 );
220 #endif
221
222
223                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
224                             NULL, NULL, NULL, NULL );
225
226                         return -1;
227                 }
228
229                 /*
230                  * no parent, acquire the root write lock
231                  * and release the add lock.
232                  */
233                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
234                 rootlock = 1;
235                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
236         }
237
238         e->e_id = next_id( be );
239
240         if( e->e_id == NOID ) {
241                 if( p != NULL) {
242                         /* free parent and writer lock */
243                         cache_return_entry_w( &li->li_cache, p ); 
244                 }
245
246                 if ( rootlock ) {
247                         /* release root lock */
248                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
249                 }
250
251 #ifdef NEW_LOGGING
252                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
253                            "ldbm_back_add: next_id failed.\n" ));
254 #else
255                 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
256                         0, 0, 0 );
257 #endif
258
259
260                 send_ldap_result( conn, op, LDAP_OTHER,
261                         NULL, "next_id add failed", NULL, NULL );
262
263                 return( -1 );
264         }
265
266         /*
267          * Try to add the entry to the cache, assign it a new dnid.
268          */
269         rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
270
271         if ( rc != 0 ) {
272                 if( p != NULL) {
273                         /* free parent and writer lock */
274                         cache_return_entry_w( &li->li_cache, p ); 
275                 }
276
277                 if ( rootlock ) {
278                         /* release root lock */
279                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
280                 }
281
282 #ifdef NEW_LOGGING
283                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
284                            "ldbm_back_add: cache_add_entry_lock failed.\n" ));
285 #else
286                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
287                     0 );
288 #endif
289
290
291                 send_ldap_result( conn, op,
292                         rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
293                         NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
294
295                 return( -1 );
296         }
297
298         rc = -1;
299
300         /* attribute indexes */
301         if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
302 #ifdef NEW_LOGGING
303                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
304                            "ldbm_back_add: index_entry_add failed.\n" ));
305 #else
306                 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
307                     0, 0 );
308 #endif
309
310                 send_ldap_result( conn, op, LDAP_OTHER,
311                         NULL, "index generation failed", NULL, NULL );
312
313                 goto return_results;
314         }
315
316         /* dn2id index */
317         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
318 #ifdef NEW_LOGGING
319                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
320                            "ldbm_back_add: dn2id_add failed.\n" ));
321 #else
322                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
323                     0, 0 );
324 #endif
325
326                 send_ldap_result( conn, op, LDAP_OTHER,
327                         NULL, "DN index generation failed", NULL, NULL );
328
329                 goto return_results;
330         }
331
332         /* id2entry index */
333         if ( id2entry_add( be, e ) != 0 ) {
334 #ifdef NEW_LOGGING
335                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
336                            "ldbm_back_add: id2entry_add failed.\n" ));
337 #else
338                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
339                     0, 0 );
340 #endif
341
342                 (void) dn2id_delete( be, e->e_ndn, e->e_id );
343                 send_ldap_result( conn, op, LDAP_OTHER,
344                         NULL, "entry store failed", NULL, NULL );
345
346                 goto return_results;
347         }
348
349         send_ldap_result( conn, op, LDAP_SUCCESS,
350                 NULL, NULL, NULL, NULL );
351         rc = 0;
352
353 return_results:;
354         if (p != NULL) {
355                 /* free parent and writer lock */
356                 cache_return_entry_w( &li->li_cache, p ); 
357         }
358
359         if ( rootlock ) {
360                 /* release root lock */
361                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
362         }
363
364         if ( rc ) {
365                 /* free entry and writer lock */
366                 cache_return_entry_w( &li->li_cache, e );
367         }
368
369         return( rc );
370 }