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