]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
Add o_no_schema_check to operation structure
[openldap] / servers / slapd / back-ldbm / add.c
1 /* add.c - ldap ldbm back-end add routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/socket.h>
22 #include <ac/string.h>
23
24 #include "slap.h"
25 #include "back-ldbm.h"
26 #include "proto-back-ldbm.h"
27
28 int
29 ldbm_back_add(
30     Operation   *op,
31     SlapReply   *rs )
32 {
33         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
34         struct berval   pdn;
35         Entry           *p = NULL;
36         ID               id = NOID;
37         AttributeDescription *children = slap_schema.si_ad_children;
38         AttributeDescription *entry = slap_schema.si_ad_entry;
39         char textbuf[SLAP_TEXT_BUFLEN];
40         size_t textlen = sizeof textbuf;
41 #ifdef LDBM_SUBENTRIES
42         int subentry;
43 #endif
44
45         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n",
46                 op->o_req_dn.bv_val, 0, 0);
47         
48         rs->sr_err = entry_schema_check( op, op->oq_add.rs_e, NULL,
49                 get_manageDIT(op), &rs->sr_text, textbuf, textlen );
50
51         if ( rs->sr_err != LDAP_SUCCESS ) {
52                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
53                         rs->sr_text, 0, 0 );
54
55                 send_ldap_result( op, rs );
56                 return rs->sr_err;
57         }
58         rs->sr_text = NULL;
59
60 #ifdef LDBM_SUBENTRIES
61         subentry = is_entry_subentry( op->oq_add.rs_e );
62 #endif
63
64         if ( !access_allowed( op, op->oq_add.rs_e,
65                                 entry, NULL, ACL_WADD, NULL ) )
66         {
67                 Debug( LDAP_DEBUG_TRACE, "no write access to entry\n", 0,
68                     0, 0 );
69
70                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
71                     "no write access to entry" );
72
73                 return LDAP_INSUFFICIENT_ACCESS;
74         }
75
76         /* grab giant lock for writing */
77         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
78
79         rs->sr_err = dn2id( op->o_bd, &op->o_req_ndn, &id );
80         if ( rs->sr_err || id != NOID ) {
81                 /* if (rs->sr_err) something bad happened to ldbm cache */
82                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
83                 rs->sr_err = rs->sr_err ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
84                 send_ldap_result( op, rs );
85                 return rs->sr_err;
86         }
87
88         /*
89          * Get the parent dn and see if the corresponding entry exists.
90          * If the parent does not exist, only allow the "root" user to
91          * add the entry.
92          */
93
94         if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
95                 pdn = slap_empty_bv;
96         } else {
97                 dnParent( &op->o_req_ndn, &pdn );
98         }
99
100         if( pdn.bv_len ) {
101                 Entry *matched = NULL;
102
103                 /* get parent with writer lock */
104                 if ( (p = dn2entry_w( op->o_bd, &pdn, &matched )) == NULL ) {
105                         if ( matched != NULL ) {
106                                 rs->sr_matched = ch_strdup( matched->e_dn );
107                                 rs->sr_ref = is_entry_referral( matched )
108                                         ? get_entry_referrals( op, matched )
109                                         : NULL;
110                                 cache_return_entry_r( &li->li_cache, matched );
111
112                         } else {
113                                 rs->sr_ref = referral_rewrite( default_referral,
114                                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
115                         }
116
117                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
118
119                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
120                                 0, 0, 0 );
121
122                         rs->sr_text = rs->sr_ref
123                                 ? "parent is referral" : "parent does not exist";
124                         rs->sr_err = LDAP_REFERRAL;
125                         send_ldap_result( op, rs );
126
127                         ber_bvarray_free( rs->sr_ref );
128                         free( (char *)rs->sr_matched );
129                         rs->sr_ref = NULL;
130                         rs->sr_matched = NULL;
131                         return rs->sr_err;
132                 }
133
134                 if ( ! access_allowed( op, p, children, NULL, ACL_WADD, NULL ) ) {
135                         /* free parent and writer lock */
136                         cache_return_entry_w( &li->li_cache, p ); 
137                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
138
139                         Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
140                             0, 0 );
141
142                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
143                             "no write access to parent" );
144
145                         return LDAP_INSUFFICIENT_ACCESS;
146                 }
147
148 #ifdef LDBM_SUBENTRIES
149                 if ( is_entry_subentry( p )) {
150                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
151                                 0, 0, 0 );
152                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
153                         rs->sr_text = "parent is a subentry";
154                         goto return_results;
155                 }
156 #endif
157
158                 if ( is_entry_alias( p ) ) {
159                         /* parent is an alias, don't allow add */
160
161                         /* free parent and writer lock */
162                         cache_return_entry_w( &li->li_cache, p );
163                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
164
165                         Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
166                             0, 0 );
167
168                         send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
169                             "parent is an alias" );
170
171                         return LDAP_ALIAS_PROBLEM;
172                 }
173
174                 if ( is_entry_referral( p ) ) {
175                         /* parent is a referral, don't allow add */
176                         rs->sr_matched = ch_strdup( p->e_dn );
177                         rs->sr_ref = is_entry_referral( p )
178                                 ? get_entry_referrals( op, p )
179                                 : NULL;
180
181                         /* free parent and writer lock */
182                         cache_return_entry_w( &li->li_cache, p );
183                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
184
185                         Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
186                             0, 0 );
187                         rs->sr_err = LDAP_REFERRAL;
188                         send_ldap_result( op, rs );
189
190                         ber_bvarray_free( rs->sr_ref );
191                         free( (char *)rs->sr_matched );
192                         rs->sr_ref = NULL;
193                         rs->sr_matched = NULL;
194                         return rs->sr_err;
195                 }
196
197 #ifdef LDBM_SUBENTRIES
198                 if ( subentry ) {
199                         /* FIXME: */
200                         /* parent must be an administrative point of the required kind */
201                 }
202 #endif
203
204         } else {
205                 assert( pdn.bv_val == NULL || *pdn.bv_val == '\0' );
206
207                 if (( !be_isroot(op) && !be_shadow_update(op) )
208                         && !is_entry_glue( op->oq_add.rs_e ))
209                 {
210                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
211
212                         Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
213                                 pdn.bv_val == NULL ? "suffix" : "entry at root", 0, 0 );
214
215                         send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT, NULL );
216                         return LDAP_NO_SUCH_OBJECT;
217                 }
218         }
219
220         if ( next_id( op->o_bd, &op->oq_add.rs_e->e_id ) ) {
221                 if( p != NULL) {
222                         /* free parent and writer lock */
223                         cache_return_entry_w( &li->li_cache, p ); 
224                 }
225
226                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
227
228                 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
229                         0, 0, 0 );
230
231                 send_ldap_error( op, rs, LDAP_OTHER,
232                         "next_id add failed" );
233
234                 return LDAP_OTHER;
235         }
236
237         /*
238          * Try to add the entry to the cache, assign it a new dnid.
239          */
240         rs->sr_err = cache_add_entry_rw( &li->li_cache, op->oq_add.rs_e,
241                 CACHE_WRITE_LOCK );
242
243         if ( rs->sr_err != 0 ) {
244                 if( p != NULL) {
245                         /* free parent and writer lock */
246                         cache_return_entry_w( &li->li_cache, p ); 
247                 }
248
249                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
250
251                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
252                     0 );
253
254                 rs->sr_text = rs->sr_err > 0 ? NULL : "cache add failed";
255                 rs->sr_err = rs->sr_err > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER;
256                 send_ldap_result( op, rs );
257
258                 return rs->sr_err;
259         }
260
261         rs->sr_err = -1;
262
263         /* attribute indexes */
264         if ( index_entry_add( op, op->oq_add.rs_e ) != LDAP_SUCCESS ) {
265                 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
266                     0, 0 );
267                 
268                 send_ldap_error( op, rs, LDAP_OTHER,
269                         "index generation failed" );
270
271                 goto return_results;
272         }
273
274         /* dn2id index */
275         if ( dn2id_add( op->o_bd, &op->oq_add.rs_e->e_nname,
276                 op->oq_add.rs_e->e_id ) != 0 )
277         {
278                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
279                     0, 0 );
280                 /* FIXME: delete attr indices? */
281
282                 send_ldap_error( op, rs, LDAP_OTHER,
283                         "DN index generation failed" );
284
285                 goto return_results;
286         }
287
288         /* id2entry index */
289         if ( id2entry_add( op->o_bd, op->oq_add.rs_e ) != 0 ) {
290                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
291                     0, 0 );
292
293                 /* FIXME: delete attr indices? */
294                 (void) dn2id_delete( op->o_bd, &op->oq_add.rs_e->e_nname,
295                         op->oq_add.rs_e->e_id );
296                 
297                 send_ldap_error( op, rs, LDAP_OTHER,
298                         "entry store failed" );
299
300                 goto return_results;
301         }
302
303         rs->sr_err = LDAP_SUCCESS;
304         rs->sr_text = NULL;
305         send_ldap_result( op, rs );
306
307         /* marks the entry as committed, so it is added to the cache;
308          * otherwise it is removed from the cache, but not destroyed;
309          * it will be destroyed by the caller */
310         cache_entry_commit( op->oq_add.rs_e );
311
312 return_results:;
313         if (p != NULL) {
314                 /* free parent and writer lock */
315                 cache_return_entry_w( &li->li_cache, p ); 
316         }
317
318         if ( rs->sr_err ) {
319                 /*
320                  * in case of error, writer lock is freed 
321                  * and entry's private data is destroyed.
322                  * otherwise, this is done when entry is released
323                  */
324                 cache_return_entry_w( &li->li_cache, op->oq_add.rs_e );
325                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
326         }
327
328         return( rs->sr_err );
329 }