]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
Preliminary Fixes for ITS#24, ITS#26, and ldbm_back_add race condition.
[openldap] / servers / slapd / back-ldbm / add.c
1 /* add.c - ldap ldbm back-end add routine */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/socket.h>
8 #include <ac/string.h>
9
10 #include "slap.h"
11 #include "back-ldbm.h"
12 #include "proto-back-ldbm.h"
13
14 int
15 ldbm_back_add(
16     Backend     *be,
17     Connection  *conn,
18     Operation   *op,
19     Entry       *e
20 )
21 {
22         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
23         char            *dn = NULL, *pdn = NULL;
24         Entry           *p = NULL;
25         int                     rc;
26
27         dn = dn_normalize( ch_strdup( e->e_dn ) );
28
29         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", dn, 0, 0);
30
31         pthread_mutex_lock(&li->li_add_mutex);
32
33         if ( ( dn2id( be, dn ) ) != NOID ) {
34                 pthread_mutex_unlock(&li->li_add_mutex);
35                 entry_free( e );
36                 free( dn );
37                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
38                 return( -1 );
39         }
40
41         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
42                 pthread_mutex_unlock(&li->li_add_mutex);
43
44                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n",
45                         0, 0, 0 );
46
47                 entry_free( e );
48                 free( dn );
49                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, "",
50                     "" );
51                 return( -1 );
52         }
53
54         /*
55          * Get the parent dn and see if the corresponding entry exists.
56          * If the parent does not exist, only allow the "root" user to
57          * add the entry.
58          */
59
60         if ( (pdn = dn_parent( be, dn )) != NULL ) {
61                 char *matched;
62                 /* no parent */
63
64                 /* get entry with reader lock */
65                 if ( (p = dn2entry_r( be, pdn, &matched )) == NULL ) {
66                         pthread_mutex_unlock(&li->li_add_mutex);
67                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n", 0,
68                             0, 0 );
69                         send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
70                             matched, "" );
71
72                         if ( matched != NULL ) {
73                                 free( matched );
74                         }
75
76                         entry_free( e );
77                         free( dn );
78                         return -1;
79                 }
80
81                 if ( ! access_allowed( be, conn, op, p, "children", NULL,
82                     op->o_dn, ACL_WRITE ) )
83                 {
84                         pthread_mutex_unlock(&li->li_add_mutex);
85                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
86                             0, 0 );
87                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
88                             "", "" );
89
90                         entry_free( e );
91                         free( dn );
92                         return -1;
93                 }
94
95         } else {
96                 if ( ! be_isroot( be, op->o_dn ) ) {
97                         pthread_mutex_unlock(&li->li_add_mutex);
98                         Debug( LDAP_DEBUG_TRACE, "no parent & not root\n", 0,
99                             0, 0 );
100                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
101                             "", "" );
102
103                         entry_free( e );
104                         free( dn );
105                         return -1;
106                 }
107         }
108
109         /*
110          * Try to add the entry to the cache, assign it a new dnid
111          * and mark it locked.  This should only fail if the entry
112          * already exists.
113          */
114
115         e->e_id = next_id( be );
116         if ( cache_add_entry_lock( &li->li_cache, e, ENTRY_STATE_CREATING ) != 0 ) {
117                 pthread_mutex_unlock(&li->li_add_mutex);
118
119                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
120                     0 );
121                 next_id_return( be, e->e_id );
122                 
123                 /* XXX this should be ok, no other thread should have access
124                  * because e hasn't been added to the cache yet
125                  */
126                 entry_free( e );
127                 free( dn );
128                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
129                 return( -1 );
130         }
131
132         /*
133          * add it to the id2children index for the parent
134          */
135
136         if ( id2children_add( be, p, e ) != 0 ) {
137                 Debug( LDAP_DEBUG_TRACE, "id2children_add failed\n", 0,
138                     0, 0 );
139                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
140
141                 rc = -1;
142                 goto return_results;
143         }
144
145         /*
146          * Add the entry to the attribute indexes, then add it to
147          * the id2children index, dn2id index, and the id2entry index.
148          */
149
150         /* attribute indexes */
151         if ( index_add_entry( be, e ) != 0 ) {
152                 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
153                     0, 0 );
154                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
155
156                 rc = -1;
157                 goto return_results;
158         }
159
160         /* dn2id index */
161         if ( dn2id_add( be, dn, e->e_id ) != 0 ) {
162                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
163                     0, 0 );
164                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
165
166                 rc = -1;
167                 goto return_results;
168         }
169
170         /* acquire writer lock */
171         entry_rdwr_lock(e, 1);
172
173         /* id2entry index */
174         if ( id2entry_add( be, e ) != 0 ) {
175                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
176                     0, 0 );
177                 (void) dn2id_delete( be, dn );
178                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
179
180                 rc = -1;
181                 goto return_results;
182         }
183
184         send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
185         rc = 0;
186
187 return_results:;
188
189         if ( dn != NULL )
190                 free( dn );
191         if ( pdn != NULL )
192                 free( pdn );
193
194         cache_set_state( &li->li_cache, e, 0 );
195
196         /* free entry and writer lock */
197         cache_return_entry_w( &li->li_cache, e ); 
198
199         /* free entry and reader lock */
200         if (p != NULL) {
201                 cache_return_entry_r( &li->li_cache, p ); 
202         }
203
204         /* it might actually be okay to release this lock sooner */
205         pthread_mutex_unlock(&li->li_add_mutex);
206
207         return( rc );
208 }