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