]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/add.c
Use #ifdef, not #if
[openldap] / servers / slapd / back-bdb2 / add.c
1 /* add.c - ldap bdb2 back-end add routine */
2 /* $OpenLDAP$ */
3
4 #include "portable.h"
5
6 #include <stdio.h>
7
8 #include <ac/socket.h>
9 #include <ac/string.h>
10
11 #include "slap.h"
12 #include "back-bdb2.h"
13 #include "proto-back-bdb2.h"
14
15 static DB_LOCK         lock;
16
17
18 static int
19 bdb2i_back_add_internal(
20     BackendDB   *be,
21     Connection  *conn,
22     Operation   *op,
23     Entry       *e
24 )
25 {
26         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
27         char            *pdn;
28         Entry           *p = NULL;
29         int                     rc; 
30         struct timeval  time1;
31
32         Debug(LDAP_DEBUG_ARGS, "==> bdb2i_back_add: %s\n", e->e_dn, 0, 0);
33
34         if ( ( bdb2i_dn2id( be, e->e_ndn ) ) != NOID ) {
35                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
36                         NULL, NULL, NULL, NULL );
37                 return( -1 );
38         }
39
40         if ( schema_check_entry( e ) != 0 ) {
41                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n",
42                         0, 0, 0 );
43
44                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION,
45                         NULL, NULL, NULL, NULL );
46                 return( -1 );
47         }
48
49         /*
50          * Get the parent dn and see if the corresponding entry exists.
51          * If the parent does not exist, only allow the "root" user to
52          * add the entry.
53          */
54
55         pdn = dn_parent( be, e->e_ndn );
56
57         if( pdn != NULL && *pdn != '\0' ) {
58                 Entry *matched = NULL;
59
60                 assert( *pdn != '\0' );
61
62                 /* get parent with writer lock */
63                 if ( (p = bdb2i_dn2entry_w( be, pdn, &matched )) == NULL ) {
64                         char *matched_dn;
65                         struct berval **refs;
66
67                         if( matched != NULL ) {
68                                 matched_dn = ch_strdup( matched->e_dn );
69                                 refs = is_entry_referral( matched )
70                                         ? get_entry_referrals( be, conn, op, matched )
71                                         : NULL;
72
73                                 bdb2i_cache_return_entry_w( &li->li_cache, matched ); 
74
75                         } else {
76                                 matched_dn = NULL;
77                                 refs = default_referral;
78                         }
79
80                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
81                                 0, 0, 0 );
82
83                         send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
84                             matched_dn, NULL, NULL, NULL );
85
86                         if ( matched != NULL ) {
87                                 ber_bvecfree( refs );
88                                 free( matched_dn );
89                         }
90
91                         free( pdn );
92                         return -1;
93                 }
94
95                 free(pdn);
96
97                 if ( ! access_allowed( be, conn, op, p,
98                         "children", NULL, ACL_WRITE ) )
99                 {
100                         /* free parent and writer lock */
101                         bdb2i_cache_return_entry_w( &li->li_cache, p ); 
102
103                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
104                             0, 0 );
105                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
106                             NULL, NULL, NULL, NULL );
107
108                         return -1;
109                 }
110
111
112                 if ( is_entry_alias( p ) ) {
113                         /* parent is an alias, don't allow add */
114
115                         /* free parent and writer lock */
116                         bdb2i_cache_return_entry_w( &li->li_cache, p ); 
117
118                         Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
119                             0, 0 );
120                         send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
121                             NULL, NULL, NULL, NULL );
122
123                         return -1;
124                 }
125
126                 if ( is_entry_referral( p ) ) {
127                         /* parent is an referral, don't allow add */
128                         char *matched_dn = ch_strdup( matched->e_dn );
129                         struct berval **refs = is_entry_referral( matched )
130                                         ? get_entry_referrals( be, conn, op, matched )
131                                         : NULL;
132
133                         /* free parent and writer lock */
134                         bdb2i_cache_return_entry_w( &li->li_cache, p ); 
135
136                         Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
137                             0, 0 );
138                         send_ldap_result( conn, op, LDAP_REFERRAL,
139                             matched_dn, NULL, refs, NULL );
140
141                         ber_bvecfree( refs );
142                         free( matched_dn );
143                         return -1;
144                 }
145
146         } else {
147                 if(pdn != NULL) {
148                         assert( *pdn == '\0' );
149                         free(pdn);
150                 }
151
152                 /* no parent, must be adding entry to root */
153                 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix(be, "") ) {
154                         Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
155                                 pdn == NULL ? "suffix" : "entry at root",
156                                 0, 0 );
157
158                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
159                             NULL, NULL, NULL, NULL );
160
161                         return -1;
162                 }
163         }
164
165         e->e_id = bdb2i_next_id( be );
166
167         /*
168          * Try to add the entry to the cache, assign it a new dnid.
169          */
170         bdb2i_start_timing( be->bd_info, &time1 );
171
172         rc = bdb2i_cache_add_entry_rw( &li->li_cache, e, CACHE_WRITE_LOCK );
173
174         bdb2i_stop_timing( be->bd_info, time1, "ADD-CACHE", conn, op );
175
176         if ( rc != 0 ) {
177                 if( p != NULL) {
178                         /* free parent and writer lock */
179                         bdb2i_cache_return_entry_w( &li->li_cache, p ); 
180                 }
181
182                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
183                     0 );
184
185                 send_ldap_result( conn, op,
186                         rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OPERATIONS_ERROR,
187                         NULL, NULL, NULL, NULL );
188
189                 return( -1 );
190         }
191
192         rc = -1;
193
194         /*
195          * Add the entry to the attribute indexes, then add it to
196          * the id2entry and dn2id index.
197          */
198
199         bdb2i_start_timing( be->bd_info, &time1 );
200
201         /* attribute indexes */
202         if ( bdb2i_index_add_entry( be, e ) != 0 ) {
203                 Debug( LDAP_DEBUG_TRACE, "bdb2i_index_add_entry failed\n", 0,
204                     0, 0 );
205                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
206                         NULL, NULL, NULL, NULL );
207
208                 bdb2i_stop_timing( be->bd_info, time1, "ADD-INDEX", conn, op );
209
210                 goto return_results;
211         }
212
213         bdb2i_stop_timing( be->bd_info, time1, "ADD-INDEX", conn, op );
214
215         bdb2i_start_timing( be->bd_info, &time1 );
216
217         /* dn2id index */
218         if ( bdb2i_dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
219                 Debug( LDAP_DEBUG_TRACE, "bdb2i_dn2id_add failed\n", 0,
220                     0, 0 );
221                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
222                         NULL, NULL, NULL, NULL );
223
224                 bdb2i_stop_timing( be->bd_info, time1, "ADD-DN2ID", conn, op );
225
226                 goto return_results;
227         }
228
229         bdb2i_stop_timing( be->bd_info, time1, "ADD-DN2ID", conn, op );
230
231         bdb2i_start_timing( be->bd_info, &time1 );
232
233         /* id2entry index */
234         if ( bdb2i_id2entry_add( be, e ) != 0 ) {
235                 Debug( LDAP_DEBUG_TRACE, "bdb2i_id2entry_add failed\n", 0,
236                     0, 0 );
237                 (void) bdb2i_dn2id_delete( be, e->e_ndn, e->e_id );
238                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
239                         NULL, NULL, NULL, NULL );
240
241                 bdb2i_stop_timing( be->bd_info, time1, "ADD-ID2ENTRY", conn, op );
242
243                 goto return_results;
244         }
245
246         bdb2i_stop_timing( be->bd_info, time1, "ADD-ID2ENTRY", conn, op );
247
248         send_ldap_result( conn, op, LDAP_SUCCESS,
249                         NULL, NULL, NULL, NULL );
250         rc = 0;
251
252 return_results:;
253         if (p != NULL) {
254                 /* free parent and writer lock */
255                 bdb2i_cache_return_entry_w( &li->li_cache, p ); 
256         }
257
258         if ( rc ) {
259                 /* free entry and writer lock */
260                 bdb2i_cache_return_entry_w( &li->li_cache, e );
261         }
262
263         return( rc );
264 }
265
266
267 int
268 bdb2_back_add(
269     BackendDB   *be,
270     Connection  *conn,
271     Operation   *op,
272     Entry       *e
273 )
274 {
275         struct ldbminfo *li  = (struct ldbminfo *) be->be_private;
276         struct timeval  time1;
277         int             ret;
278
279         bdb2i_start_timing( be->bd_info, &time1 );
280
281         if ( bdb2i_enter_backend_w( &lock ) != 0 ) {
282                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
283                         NULL, NULL, NULL, NULL );
284                 return( -1 );
285         }
286
287         /*  check, if a new default attribute index will be created,
288                 in which case we have to open the index file BEFORE TP  */
289         switch ( slapMode & SLAP_MODE ) {
290                 case SLAP_SERVER_MODE:
291                 case SLAP_TOOL_MODE:
292                         bdb2i_check_default_attr_index_add( li, e );
293                         break;
294         }
295
296         ret = bdb2i_back_add_internal( be, conn, op, e );
297
298         /*  if the operation was successful, we will delay the unlock  */
299         if ( ret )
300                 (void) bdb2i_leave_backend_w( lock );
301
302         bdb2i_stop_timing( be->bd_info, time1, "ADD", conn, op );
303
304         return( ret );
305 }
306
307
308 int
309 bdb2i_release_add_lock( void )
310 {
311         (void) bdb2i_leave_backend_w( lock );
312         return 0;
313 }
314
315