]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
HEADS UP: connections are forced to "anonymous" status upon receiving
[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            *pdn;
24         Entry           *p = NULL;
25         int                     rootlock = 0;
26         int                     rc; 
27
28         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", e->e_dn, 0, 0);
29
30         /* nobody else can add until we lock our parent */
31         ldap_pvt_thread_mutex_lock(&li->li_add_mutex);
32
33         if ( ( dn2id( be, e->e_ndn ) ) != NOID ) {
34                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
35                 entry_free( e );
36                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
37                 return( -1 );
38         }
39
40         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
41                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
42
43                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n",
44                         0, 0, 0 );
45
46                 entry_free( e );
47                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, "",
48                     "" );
49                 return( -1 );
50         }
51
52         /*
53          * Get the parent dn and see if the corresponding entry exists.
54          * If the parent does not exist, only allow the "root" user to
55          * add the entry.
56          */
57
58         pdn = dn_parent( be, e->e_ndn );
59
60         if( pdn != NULL && *pdn != '\0' && !be_issuffix(be, "") ) {
61                 char *matched = NULL;
62
63                 assert( *pdn != '\0' );
64
65                 /* get parent with writer lock */
66                 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
67                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
68                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n", 0,
69                             0, 0 );
70                         send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
71                             matched, "" );
72
73                         if ( matched != NULL ) {
74                                 free( matched );
75                         }
76
77                         entry_free( e );
78                         free( pdn );
79                         return -1;
80                 }
81
82                 /* don't need the add lock anymore */
83                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
84
85                 free(pdn);
86
87                 if ( matched != NULL ) {
88                         free( matched );
89                 }
90
91                 if ( ! access_allowed( be, conn, op, p,
92                         "children", NULL, ACL_WRITE ) )
93                 {
94                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
95                             0, 0 );
96                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
97                             "", "" );
98
99                         /* free parent and writer lock */
100                         cache_return_entry_w( &li->li_cache, p ); 
101
102                         entry_free( e );
103                         return -1;
104                 }
105
106         } else {
107                 if(pdn != NULL) {
108                         assert( *pdn == '\0' );
109                         free(pdn);
110                 }
111
112                 /* no parent, must be adding entry to root */
113                 if ( ! be_isroot( be, op->o_ndn ) ) {
114                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
115
116                         Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
117                                         pdn == NULL ? "suffix" : "entry at root",
118                                         0, 0 );
119
120                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
121                             "", "" );
122
123                         entry_free( e );
124                         return -1;
125                 }
126
127                 /*
128                  * no parent, acquire the root write lock
129                  * and release the add lock.
130                  */
131                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
132                 rootlock = 1;
133                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
134         }
135
136         e->e_id = next_id( be );
137
138         /*
139          * Try to add the entry to the cache, assign it a new dnid.
140          */
141         rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
142
143         if ( rc != 0 ) {
144                 if( p != NULL) {
145                         /* free parent and writer lock */
146                         cache_return_entry_w( &li->li_cache, p ); 
147                 }
148
149                 if ( rootlock ) {
150                         /* release root lock */
151                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
152                 }
153
154                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
155                     0 );
156
157                 /* return the id */
158                 next_id_return( be, e->e_id );
159
160                 /* free the entry */
161                 entry_free( e );
162
163                 if(rc > 0) {
164                         send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
165                 } else {
166                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
167                 }
168
169                 return( -1 );
170         }
171
172         rc = -1;
173
174         /*
175          * add it to the id2children index for the parent
176          */
177
178         if ( id2children_add( be, p, e ) != 0 ) {
179                 Debug( LDAP_DEBUG_TRACE, "id2children_add failed\n", 0,
180                     0, 0 );
181                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
182
183                 goto return_results;
184         }
185
186         /*
187          * Add the entry to the attribute indexes, then add it to
188          * the id2children index, dn2id index, and the id2entry index.
189          */
190
191         /* attribute indexes */
192         if ( index_add_entry( be, e ) != 0 ) {
193                 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
194                     0, 0 );
195                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
196
197                 goto return_results;
198         }
199
200         /* dn2id index */
201         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
202                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
203                     0, 0 );
204                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
205
206                 goto return_results;
207         }
208
209         /* id2entry index */
210         if ( id2entry_add( be, e ) != 0 ) {
211                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
212                     0, 0 );
213                 (void) dn2id_delete( be, e->e_ndn );
214                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
215
216                 goto return_results;
217         }
218
219         send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
220         rc = 0;
221
222 return_results:;
223         if (p != NULL) {
224                 /* free parent and writer lock */
225                 cache_return_entry_w( &li->li_cache, p ); 
226         }
227
228         if ( rootlock ) {
229                 /* release root lock */
230                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
231         }
232
233         if ( rc ) {
234                 /* free entry and writer lock */
235                 cache_return_entry_w( &li->li_cache, e );
236         }
237
238         return( rc );
239 }