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