]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
Import experimental referral implementation from OPENLDAP_DEVEL_REFERRALS.
[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                         NULL, NULL, NULL, NULL );
38                 return( -1 );
39         }
40
41         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
42                 ldap_pvt_thread_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                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION,
49                         NULL, NULL, NULL, NULL );
50                 return( -1 );
51         }
52
53         /*
54          * Get the parent dn and see if the corresponding entry exists.
55          * If the parent does not exist, only allow the "root" user to
56          * add the entry.
57          */
58
59         pdn = dn_parent( be, e->e_ndn );
60
61         if( pdn != NULL && *pdn != '\0' && !be_issuffix(be, "") ) {
62                 Entry *matched = NULL;
63
64                 assert( *pdn != '\0' );
65
66                 /* get parent with writer lock */
67                 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
68                         char *matched_dn;
69                         struct berval **refs;
70
71                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
72
73                         if ( matched != NULL ) {
74                                 matched_dn = ch_strdup( matched->e_dn );
75                                 refs = is_entry_referral( matched )
76                                         ? get_entry_referrals( be, conn, op, matched )
77                                         : NULL;
78                                 cache_return_entry_r( &li->li_cache, matched );
79
80                         } else {
81                                 matched_dn = NULL;
82                                 refs = default_referral;
83                         }
84
85                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
86                                 0, 0, 0 );
87
88                         send_ldap_result( conn, op, LDAP_REFERRAL,
89                             matched_dn, NULL, refs, NULL );
90
91                         if( matched != NULL ) {
92                                 ber_bvecfree( refs );
93                                 free( matched_dn );
94                         }
95
96                         entry_free( e );
97                         free( pdn );
98                         return -1;
99                 }
100
101                 /* don't need the add lock anymore */
102                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
103
104                 free(pdn);
105
106                 if ( ! access_allowed( be, conn, op, p,
107                         "children", NULL, ACL_WRITE ) )
108                 {
109                         /* free parent and writer lock */
110                         cache_return_entry_w( &li->li_cache, p ); 
111
112                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
113                             0, 0 );
114                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
115                             NULL, NULL, NULL, NULL );
116
117
118                         entry_free( e );
119                         return -1;
120                 }
121
122                 if ( is_entry_alias( p ) ) {
123                         /* parent is an alias, don't allow add */
124
125                         /* free parent and writer lock */
126                         cache_return_entry_w( &li->li_cache, p );
127
128                         Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
129                             0, 0 );
130
131                         send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
132                             NULL, NULL, NULL, NULL );
133
134                         entry_free( e );
135                         return -1;
136                 }
137
138                 if ( is_entry_referral( p ) ) {
139                         /* parent is a referral, don't allow add */
140                         char *matched_dn = ch_strdup( p->e_dn );
141                         struct berval **refs = is_entry_referral( p )
142                                 ? get_entry_referrals( be, conn, op, p )
143                                 : NULL;
144
145                         /* free parent and writer lock */
146                         cache_return_entry_w( &li->li_cache, p );
147
148                         Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
149                             0, 0 );
150                         send_ldap_result( conn, op, LDAP_REFERRAL,
151                             matched_dn, NULL, refs, NULL );
152
153                         ber_bvecfree( refs );
154                         free( matched_dn );
155                         entry_free( e );
156                         return -1;
157                 }
158
159         } else {
160                 if(pdn != NULL) {
161                         assert( *pdn == '\0' );
162                         free(pdn);
163                 }
164
165                 /* no parent, must be adding entry to root */
166                 if ( ! be_isroot( be, op->o_ndn ) ) {
167                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
168
169                         Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
170                                         pdn == NULL ? "suffix" : "entry at root",
171                                         0, 0 );
172
173                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
174                             NULL, NULL, NULL, NULL );
175
176                         entry_free( e );
177                         return -1;
178                 }
179
180                 /*
181                  * no parent, acquire the root write lock
182                  * and release the add lock.
183                  */
184                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
185                 rootlock = 1;
186                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
187         }
188
189         e->e_id = next_id( be );
190
191         /*
192          * Try to add the entry to the cache, assign it a new dnid.
193          */
194         rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
195
196         if ( rc != 0 ) {
197                 if( p != NULL) {
198                         /* free parent and writer lock */
199                         cache_return_entry_w( &li->li_cache, p ); 
200                 }
201
202                 if ( rootlock ) {
203                         /* release root lock */
204                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
205                 }
206
207                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
208                     0 );
209
210                 /* return the id */
211                 next_id_return( be, e->e_id );
212
213                 /* free the entry */
214                 entry_free( e );
215
216                 send_ldap_result( conn, op,
217                         rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OPERATIONS_ERROR,
218                         NULL, NULL, NULL, NULL );
219
220                 return( -1 );
221         }
222
223         rc = -1;
224
225         /*
226          * add it to the id2children index for the parent
227          */
228
229         if ( id2children_add( be, p, e ) != 0 ) {
230                 Debug( LDAP_DEBUG_TRACE, "id2children_add failed\n", 0,
231                     0, 0 );
232                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
233                         NULL, NULL, NULL, NULL );
234
235                 goto return_results;
236         }
237
238         /*
239          * Add the entry to the attribute indexes, then add it to
240          * the id2children index, dn2id index, and the id2entry index.
241          */
242
243         /* attribute indexes */
244         if ( index_add_entry( be, e ) != 0 ) {
245                 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
246                     0, 0 );
247                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
248                         NULL, NULL, NULL, NULL );
249
250                 goto return_results;
251         }
252
253         /* dn2id index */
254         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
255                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
256                     0, 0 );
257                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
258                         NULL, NULL, NULL, NULL );
259
260                 goto return_results;
261         }
262
263         /* id2entry index */
264         if ( id2entry_add( be, e ) != 0 ) {
265                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
266                     0, 0 );
267                 (void) dn2id_delete( be, e->e_ndn );
268                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
269                         NULL, NULL, NULL, NULL );
270
271                 goto return_results;
272         }
273
274         send_ldap_result( conn, op, LDAP_SUCCESS,
275                 NULL, NULL, NULL, NULL );
276         rc = 0;
277
278 return_results:;
279         if (p != NULL) {
280                 /* free parent and writer lock */
281                 cache_return_entry_w( &li->li_cache, p ); 
282         }
283
284         if ( rootlock ) {
285                 /* release root lock */
286                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
287         }
288
289         if ( rc ) {
290                 /* free entry and writer lock */
291                 cache_return_entry_w( &li->li_cache, e );
292         }
293
294         return( rc );
295 }