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