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