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