]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
New indexer/filter codes (test suite works) with cheats
[openldap] / servers / slapd / back-ldbm / add.c
1 /* add.c - ldap ldbm back-end add routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/socket.h>
13 #include <ac/string.h>
14
15 #include "slap.h"
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
18
19 int
20 ldbm_back_add(
21     Backend     *be,
22     Connection  *conn,
23     Operation   *op,
24     Entry       *e
25 )
26 {
27         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
28         char            *pdn;
29         Entry           *p = NULL;
30         int                     rootlock = 0;
31         int                     rc; 
32         const char      *text = NULL;
33 #ifdef SLAPD_SCHEMA_NOT_COMPAT
34         AttributeDescription *children = slap_schema.si_ad_children;
35 #else
36         static const char *children = "children";
37 #endif
38
39
40         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", e->e_dn, 0, 0);
41
42         /* nobody else can add until we lock our parent */
43         ldap_pvt_thread_mutex_lock(&li->li_add_mutex);
44
45         if ( ( dn2id( be, e->e_ndn ) ) != NOID ) {
46                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
47                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
48                         NULL, NULL, NULL, NULL );
49                 return( -1 );
50         }
51
52         rc = entry_schema_check( e, NULL, &text );
53
54         if ( rc != LDAP_SUCCESS ) {
55                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
56
57                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
58                         text, 0, 0 );
59
60                 send_ldap_result( conn, op, rc,
61                         NULL, text, NULL, NULL );
62                 return( -1 );
63         }
64
65         /*
66          * Get the parent dn and see if the corresponding entry exists.
67          * If the parent does not exist, only allow the "root" user to
68          * add the entry.
69          */
70
71         pdn = dn_parent( be, e->e_ndn );
72
73         if( pdn != NULL && *pdn != '\0' ) {
74                 Entry *matched = NULL;
75
76                 assert( *pdn != '\0' );
77
78                 /* get parent with writer lock */
79                 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
80                         char *matched_dn;
81                         struct berval **refs;
82
83                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
84
85                         if ( matched != NULL ) {
86                                 matched_dn = ch_strdup( matched->e_dn );
87                                 refs = is_entry_referral( matched )
88                                         ? get_entry_referrals( be, conn, op, matched )
89                                         : NULL;
90                                 cache_return_entry_r( &li->li_cache, matched );
91
92                         } else {
93                                 matched_dn = NULL;
94                                 refs = default_referral;
95                         }
96
97                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
98                                 0, 0, 0 );
99
100                         send_ldap_result( conn, op, LDAP_REFERRAL,
101                             matched_dn, NULL, refs, NULL );
102
103                         if( matched != NULL ) {
104                                 ber_bvecfree( refs );
105                                 free( matched_dn );
106                         }
107
108                         free( pdn );
109                         return -1;
110                 }
111
112                 /* don't need the add lock anymore */
113                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
114
115                 free(pdn);
116
117                 if ( ! access_allowed( be, conn, op, p,
118                         children, NULL, ACL_WRITE ) )
119                 {
120                         /* free parent and writer lock */
121                         cache_return_entry_w( &li->li_cache, p ); 
122
123                         Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
124                             0, 0 );
125                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
126                             NULL, "no write access to parent", NULL, NULL );
127
128
129                         return -1;
130                 }
131
132                 if ( is_entry_alias( p ) ) {
133                         /* parent is an alias, don't allow add */
134
135                         /* free parent and writer lock */
136                         cache_return_entry_w( &li->li_cache, p );
137
138                         Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
139                             0, 0 );
140
141                         send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
142                             NULL, "parent is an alias", NULL, NULL );
143
144                         return -1;
145                 }
146
147                 if ( is_entry_referral( p ) ) {
148                         /* parent is a referral, don't allow add */
149                         char *matched_dn = ch_strdup( p->e_dn );
150                         struct berval **refs = is_entry_referral( p )
151                                 ? get_entry_referrals( be, conn, op, p )
152                                 : NULL;
153
154                         /* free parent and writer lock */
155                         cache_return_entry_w( &li->li_cache, p );
156
157                         Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
158                             0, 0 );
159                         send_ldap_result( conn, op, LDAP_REFERRAL,
160                             matched_dn, NULL, refs, NULL );
161
162                         ber_bvecfree( refs );
163                         free( matched_dn );
164                         return -1;
165                 }
166
167         } else {
168                 if(pdn != NULL) {
169                         assert( *pdn == '\0' );
170                         free(pdn);
171                 }
172
173                 /* no parent, must be adding entry to root */
174                 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix( be, "" ) ) {
175                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
176
177                         Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
178                                         pdn == NULL ? "suffix" : "entry at root",
179                                         0, 0 );
180
181                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
182                             NULL, NULL, NULL, NULL );
183
184                         return -1;
185                 }
186
187                 /*
188                  * no parent, acquire the root write lock
189                  * and release the add lock.
190                  */
191                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
192                 rootlock = 1;
193                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
194         }
195
196         e->e_id = next_id( be );
197
198         /*
199          * Try to add the entry to the cache, assign it a new dnid.
200          */
201         rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
202
203         if ( rc != 0 ) {
204                 if( p != NULL) {
205                         /* free parent and writer lock */
206                         cache_return_entry_w( &li->li_cache, p ); 
207                 }
208
209                 if ( rootlock ) {
210                         /* release root lock */
211                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
212                 }
213
214                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
215                     0 );
216
217                 send_ldap_result( conn, op,
218                         rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
219                         NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
220
221                 return( -1 );
222         }
223
224         rc = -1;
225
226         /* attribute indexes */
227         if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
228                 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
229                     0, 0 );
230                 send_ldap_result( conn, op, LDAP_OTHER,
231                         NULL, "index generation failed", NULL, NULL );
232
233                 goto return_results;
234         }
235
236         /* dn2id index */
237         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
238                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
239                     0, 0 );
240                 send_ldap_result( conn, op, LDAP_OTHER,
241                         NULL, "DN index generation failed", NULL, NULL );
242
243                 goto return_results;
244         }
245
246         /* id2entry index */
247         if ( id2entry_add( be, e ) != 0 ) {
248                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
249                     0, 0 );
250                 (void) dn2id_delete( be, e->e_ndn, e->e_id );
251                 send_ldap_result( conn, op, LDAP_OTHER,
252                         NULL, "entry store failed", NULL, NULL );
253
254                 goto return_results;
255         }
256
257         send_ldap_result( conn, op, LDAP_SUCCESS,
258                 NULL, NULL, NULL, NULL );
259         rc = 0;
260
261 return_results:;
262         if (p != NULL) {
263                 /* free parent and writer lock */
264                 cache_return_entry_w( &li->li_cache, p ); 
265         }
266
267         if ( rootlock ) {
268                 /* release root lock */
269                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
270         }
271
272         if ( rc ) {
273                 /* free entry and writer lock */
274                 cache_return_entry_w( &li->li_cache, e );
275         }
276
277         return( rc );
278 }