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