]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
cleanup & more
[openldap] / servers / slapd / back-bdb / add.c
1 /* add.c - ldap BerkeleyDB 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 #include <ac/string.h>
12
13 #include "back-bdb.h"
14 #include "external.h"
15
16 int
17 bdb_add(Operation *op, SlapReply *rs )
18 {
19         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
20         struct berval   pdn;
21         Entry           *p = NULL;
22         const char      *text;
23         char textbuf[SLAP_TEXT_BUFLEN];
24         size_t textlen = sizeof textbuf;
25         AttributeDescription *children = slap_schema.si_ad_children;
26         AttributeDescription *entry = slap_schema.si_ad_entry;
27         DB_TXN          *ltid = NULL;
28         struct bdb_op_info opinfo;
29 #ifdef BDB_SUBENTRIES
30         int subentry;
31 #endif
32         u_int32_t       locker = 0;
33         DB_LOCK         lock;
34         int             noop = 0;
35
36 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
37         Operation* ps_list;
38 #endif
39
40 #ifdef NEW_LOGGING
41         LDAP_LOG ( OPERATION, ARGS, "==> bdb_add: %s\n", op->oq_add.rs_e->e_name.bv_val, 0, 0 );
42 #else
43         Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", op->oq_add.rs_e->e_name.bv_val, 0, 0);
44 #endif
45
46         /* check entry's schema */
47         rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e, NULL, &rs->sr_text, textbuf, textlen );
48         if ( rs->sr_err != LDAP_SUCCESS ) {
49 #ifdef NEW_LOGGING
50         LDAP_LOG ( OPERATION, ERR, 
51                 "bdb_add: entry failed schema check: %s (%d)\n", rs->sr_text, rs->sr_err, 0 );
52 #else
53                 Debug( LDAP_DEBUG_TRACE,
54                         "bdb_add: entry failed schema check: %s (%d)\n",
55                         rs->sr_text, rs->sr_err, 0 );
56 #endif
57                 goto return_results;
58         }
59
60 #ifdef BDB_SUBENTRIES
61         subentry = is_entry_subentry( op->oq_add.rs_e );
62 #endif
63
64         /*
65          * acquire an ID outside of the operation transaction
66          * to avoid serializing adds.
67          */
68         rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
69         if( rs->sr_err != 0 ) {
70 #ifdef NEW_LOGGING
71                 LDAP_LOG ( OPERATION, ERR, 
72                         "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
73 #else
74                 Debug( LDAP_DEBUG_TRACE,
75                         "bdb_add: next_id failed (%d)\n",
76                         rs->sr_err, 0, 0 );
77 #endif
78                 rs->sr_err = LDAP_OTHER;
79                 rs->sr_text = "internal error";
80                 goto return_results;
81         }
82
83         if( 0 ) {
84 retry:  /* transaction retry */
85                 if( p ) {
86                         /* free parent and reader lock */
87                         bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
88                         p = NULL;
89                 }
90                 rs->sr_err = TXN_ABORT( ltid );
91                 ltid = NULL;
92                 op->o_private = NULL;
93                 op->o_do_not_cache = opinfo.boi_acl_cache;
94                 if( rs->sr_err != 0 ) {
95                         rs->sr_err = LDAP_OTHER;
96                         rs->sr_text = "internal error";
97                         goto return_results;
98                 }
99                 ldap_pvt_thread_yield();
100         }
101
102         /* begin transaction */
103         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
104                 bdb->bi_db_opflags );
105         rs->sr_text = NULL;
106         if( rs->sr_err != 0 ) {
107 #ifdef NEW_LOGGING
108                 LDAP_LOG ( OPERATION, ERR, 
109                         "bdb_add: txn_begin failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
110 #else
111                 Debug( LDAP_DEBUG_TRACE,
112                         "bdb_add: txn_begin failed: %s (%d)\n",
113                         db_strerror(rs->sr_err), rs->sr_err, 0 );
114 #endif
115                 rs->sr_err = LDAP_OTHER;
116                 rs->sr_text = "internal error";
117                 goto return_results;
118         }
119
120         locker = TXN_ID ( ltid );
121
122         opinfo.boi_bdb = op->o_bd;
123         opinfo.boi_txn = ltid;
124         opinfo.boi_locker = locker;
125         opinfo.boi_err = 0;
126         opinfo.boi_acl_cache = op->o_do_not_cache;
127         op->o_private = &opinfo;
128         
129         /*
130          * Get the parent dn and see if the corresponding entry exists.
131          * If the parent does not exist, only allow the "root" user to
132          * add the entry.
133          */
134         if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
135                 pdn = slap_empty_bv;
136         } else {
137                 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
138         }
139
140         if( pdn.bv_len != 0 ) {
141                 Entry *matched = NULL;
142
143                 /* get parent */
144                 rs->sr_err = bdb_dn2entry_r( op->o_bd, ltid, &pdn, &p, &matched, 0, locker, &lock );
145
146                 switch( rs->sr_err ) {
147                 case 0:
148                 case DB_NOTFOUND:
149                         break;
150                 case DB_LOCK_DEADLOCK:
151                 case DB_LOCK_NOTGRANTED:
152                         goto retry;
153                 case LDAP_BUSY:
154                         rs->sr_text = "ldap server busy";
155                         goto return_results;
156                 default:
157                         rs->sr_err = LDAP_OTHER;
158                         rs->sr_text = "internal error";
159                         goto return_results;
160                 }
161
162                 if ( p == NULL ) {
163                         if ( matched != NULL ) {
164                                 rs->sr_matched = ch_strdup( matched->e_dn );
165                                 rs->sr_ref = is_entry_referral( matched )
166                                         ? get_entry_referrals( op, matched )
167                                         : NULL;
168                                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, matched );
169                                 matched = NULL;
170
171                         } else {
172                                 rs->sr_ref = referral_rewrite( default_referral,
173                                         NULL, &op->oq_add.rs_e->e_name, LDAP_SCOPE_DEFAULT );
174                         }
175
176 #ifdef NEW_LOGGING
177                         LDAP_LOG ( OPERATION, DETAIL1, 
178                                 "bdb_add: parent does not exist\n", 0, 0, 0 );
179 #else
180                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
181                                 0, 0, 0 );
182 #endif
183
184                         rs->sr_err = LDAP_REFERRAL;
185                         send_ldap_result( op, rs );
186
187                         ber_bvarray_free( rs->sr_ref );
188                         ch_free( (char *)rs->sr_matched );
189                         rs->sr_ref = NULL;
190                         rs->sr_matched = NULL;
191
192                         goto done;
193                 }
194
195                 rs->sr_err = access_allowed( op, p,
196                         children, NULL, ACL_WRITE, NULL );
197
198                 if ( ! rs->sr_err ) {
199                         switch( opinfo.boi_err ) {
200                         case DB_LOCK_DEADLOCK:
201                         case DB_LOCK_NOTGRANTED:
202                                 goto retry;
203                         }
204
205 #ifdef NEW_LOGGING
206                         LDAP_LOG ( OPERATION, DETAIL1, 
207                                 "bdb_add: no write access to parent\n", 0, 0, 0 );
208 #else
209                         Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
210                                 0, 0, 0 );
211 #endif
212                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
213                         rs->sr_text = "no write access to parent";
214                         goto return_results;;
215                 }
216
217 #ifdef BDB_SUBENTRIES
218                 if ( is_entry_subentry( p ) ) {
219                         /* parent is a subentry, don't allow add */
220 #ifdef NEW_LOGGING
221                         LDAP_LOG ( OPERATION, DETAIL1, 
222                                 "bdb_add: parent is subentry\n", 0, 0, 0 );
223 #else
224                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
225                                 0, 0, 0 );
226 #endif
227                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
228                         rs->sr_text = "parent is a subentry";
229                         goto return_results;;
230                 }
231 #endif
232 #ifdef BDB_ALIASES
233                 if ( is_entry_alias( p ) ) {
234                         /* parent is an alias, don't allow add */
235 #ifdef NEW_LOGGING
236                         LDAP_LOG ( OPERATION, DETAIL1, 
237                                 "bdb_add: parent is alias\n", 0, 0, 0 );
238 #else
239                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
240                                 0, 0, 0 );
241 #endif
242                         rs->sr_err = LDAP_ALIAS_PROBLEM;
243                         rs->sr_text = "parent is an alias";
244                         goto return_results;;
245                 }
246 #endif
247
248                 if ( is_entry_referral( p ) ) {
249                         /* parent is a referral, don't allow add */
250                         rs->sr_matched = p->e_name.bv_val;
251                         rs->sr_ref = get_entry_referrals( op, p );
252
253 #ifdef NEW_LOGGING
254                         LDAP_LOG ( OPERATION, DETAIL1, 
255                                 "bdb_add: parent is referral\n", 0, 0, 0 );
256 #else
257                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
258                                 0, 0, 0 );
259 #endif
260
261                         rs->sr_err = LDAP_REFERRAL;
262                         send_ldap_result( op, rs );
263
264                         ber_bvarray_free( rs->sr_ref );
265                         bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
266                         rs->sr_ref = NULL;
267                         rs->sr_matched = NULL;
268                         p = NULL;
269                         goto done;
270                 }
271
272 #ifdef BDB_SUBENTRIES
273                 if ( subentry ) {
274                         /* FIXME: */
275                         /* parent must be an administrative point of the required kind */
276                 }
277 #endif
278
279                 /* free parent and reader lock */
280                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
281                 p = NULL;
282
283         } else {
284                 /*
285                  * no parent!
286                  *      must be adding entry at suffix or with parent ""
287                  */
288                 if ( !be_isroot( op->o_bd, &op->o_ndn )) {
289                         if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
290                                 || be_isupdate( op->o_bd, &op->o_ndn ) )
291                         {
292                                 p = (Entry *)&slap_entry_root;
293
294                                 /* check parent for "children" acl */
295                                 rs->sr_err = access_allowed( op, p,
296                                         children, NULL, ACL_WRITE, NULL );
297
298                                 p = NULL;
299
300                                 if ( ! rs->sr_err ) {
301                                         switch( opinfo.boi_err ) {
302                                         case DB_LOCK_DEADLOCK:
303                                         case DB_LOCK_NOTGRANTED:
304                                                 goto retry;
305                                         }
306
307 #ifdef NEW_LOGGING
308                                         LDAP_LOG ( OPERATION, DETAIL1, 
309                                                 "bdb_add: no write access to parent\n", 0, 0, 0 );
310 #else
311                                         Debug( LDAP_DEBUG_TRACE,
312                                                 "bdb_add: no write access to parent\n",
313                                                 0, 0, 0 );
314 #endif
315                                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
316                                         rs->sr_text = "no write access to parent";
317                                         goto return_results;;
318                                 }
319
320                         } else {
321 #ifdef NEW_LOGGING
322                                 LDAP_LOG ( OPERATION, DETAIL1, "bdb_add: %s denied\n", 
323                                         pdn.bv_len == 0 ? "suffix" : "entry at root", 0, 0 );
324 #else
325                                 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
326                                         pdn.bv_len == 0 ? "suffix" : "entry at root",
327                                         0, 0 );
328 #endif
329                                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
330                                 goto return_results;
331                         }
332                 }
333
334 #ifdef BDB_SUBENTRIES
335                 if( subentry ) {
336 #ifdef NEW_LOGGING
337                         LDAP_LOG ( OPERATION, DETAIL1, 
338                                 "bdb_add: no parent, cannot add subentry\n", 0, 0, 0 );
339 #else
340                         Debug( LDAP_DEBUG_TRACE,
341                                 "bdb_add: no parent, cannot add subentry\n",
342                                 0, 0, 0 );
343 #endif
344                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
345                         rs->sr_text = "no parent, cannot add subentry";
346                         goto return_results;;
347                 }
348 #endif
349         }
350
351         rs->sr_err = access_allowed( op, op->oq_add.rs_e,
352                 entry, NULL, ACL_WRITE, NULL );
353
354         if ( ! rs->sr_err ) {
355                 switch( opinfo.boi_err ) {
356                 case DB_LOCK_DEADLOCK:
357                 case DB_LOCK_NOTGRANTED:
358                         goto retry;
359                 }
360
361 #ifdef NEW_LOGGING
362                 LDAP_LOG ( OPERATION, DETAIL1, 
363                         "bdb_add: no write access to entry\n", 0, 0, 0 );
364 #else
365                 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to entry\n",
366                         0, 0, 0 );
367 #endif
368                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
369                 rs->sr_text = "no write access to entry";
370                 goto return_results;;
371         }
372
373         /* dn2id index */
374         rs->sr_err = bdb_dn2id_add( op->o_bd, ltid, &pdn, op->oq_add.rs_e );
375         if ( rs->sr_err != 0 ) {
376 #ifdef NEW_LOGGING
377                 LDAP_LOG ( OPERATION, ERR, 
378                         "bdb_add: dn2id_add failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
379 #else
380                 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
381                         db_strerror(rs->sr_err), rs->sr_err, 0 );
382 #endif
383
384                 switch( rs->sr_err ) {
385                 case DB_LOCK_DEADLOCK:
386                 case DB_LOCK_NOTGRANTED:
387                         goto retry;
388                 case DB_KEYEXIST:
389                         rs->sr_err = LDAP_ALREADY_EXISTS;
390                         break;
391                 default:
392                         rs->sr_err = LDAP_OTHER;
393                 }
394                 goto return_results;
395         }
396
397         /* id2entry index */
398         rs->sr_err = bdb_id2entry_add( op->o_bd, ltid, op->oq_add.rs_e );
399         if ( rs->sr_err != 0 ) {
400 #ifdef NEW_LOGGING
401                 LDAP_LOG ( OPERATION, ERR, "bdb_add: id2entry_add failed\n", 0, 0, 0 );
402 #else
403                 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
404                         0, 0, 0 );
405 #endif
406                 switch( rs->sr_err ) {
407                 case DB_LOCK_DEADLOCK:
408                 case DB_LOCK_NOTGRANTED:
409                         goto retry;
410                 default:
411                         rs->sr_err = LDAP_OTHER;
412                 }
413                 rs->sr_text = "entry store failed";
414                 goto return_results;
415         }
416
417         /* attribute indexes */
418         rs->sr_err = bdb_index_entry_add( op->o_bd, ltid, op->oq_add.rs_e );
419         if ( rs->sr_err != LDAP_SUCCESS ) {
420 #ifdef NEW_LOGGING
421                 LDAP_LOG ( OPERATION, ERR, 
422                         "bdb_add: index_entry_add failed\n", 0, 0, 0 );
423 #else
424                 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
425                         0, 0, 0 );
426 #endif
427                 switch( rs->sr_err ) {
428                 case DB_LOCK_DEADLOCK:
429                 case DB_LOCK_NOTGRANTED:
430                         goto retry;
431                 default:
432                         rs->sr_err = LDAP_OTHER;
433                 }
434                 rs->sr_text = "index generation failed";
435                 goto return_results;
436         }
437
438
439         if( op->o_noop ) {
440                 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
441                         rs->sr_text = "txn_abort (no-op) failed";
442                 } else {
443                         noop = 1;
444                         rs->sr_err = LDAP_SUCCESS;
445                 }
446
447         } else {
448                 char gid[DB_XIDDATASIZE];
449
450                 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
451                         bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
452
453                 if (( rs->sr_err=TXN_PREPARE( ltid, gid )) != 0 ) {
454                         rs->sr_text = "txn_prepare failed";
455
456                 } else {
457                         int ret = bdb_cache_add_entry_rw(bdb->bi_dbenv,
458                                         &bdb->bi_cache, op->oq_add.rs_e, CACHE_WRITE_LOCK,
459                                         locker, &lock);
460                         switch ( ret ) {
461                         case 0:
462                                 break;
463                         case DB_LOCK_DEADLOCK:
464                         case DB_LOCK_NOTGRANTED:
465                                 goto retry;
466                         default:
467                                 ret = LDAP_OTHER;
468                         }
469
470                         if ( ret ) {
471                                 if(( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
472                                         rs->sr_text = "cache add & txn_abort failed";
473                                 } else {
474                                         rs->sr_err = LDAP_OTHER;
475                                         rs->sr_text = "cache add failed";
476                                 }
477                         } else {
478                                 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
479                                         rs->sr_text = "txn_commit failed";
480                                 } else {
481                                         rs->sr_err = LDAP_SUCCESS;
482                                 }
483                         }
484                 }
485         }
486
487         ltid = NULL;
488         op->o_private = NULL;
489
490         if (rs->sr_err == LDAP_SUCCESS) {
491 #ifdef NEW_LOGGING
492                 LDAP_LOG ( OPERATION, RESULTS, 
493                         "bdb_add: added%s id=%08lx dn=\"%s\"\n", 
494                         op->o_noop ? " (no-op)" : "", op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
495 #else
496                 Debug(LDAP_DEBUG_TRACE, "bdb_add: added%s id=%08lx dn=\"%s\"\n",
497                         op->o_noop ? " (no-op)" : "", op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
498 #endif
499                 rs->sr_text = NULL;
500                 if ( !noop ) {
501                         bdb_cache_entry_commit( op->oq_add.rs_e );
502                 }
503         }
504         else {
505 #ifdef NEW_LOGGING
506                 LDAP_LOG ( OPERATION, ERR, 
507                         "bdb_add: %s : %s (%d)\n",  rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
508 #else
509                 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
510                         rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
511 #endif
512                 rs->sr_err = LDAP_OTHER;
513         }
514
515 return_results:
516         send_ldap_result( op, rs );
517
518 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
519         if ( rs->sr_err == LDAP_SUCCESS && !noop ) {
520                 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
521                         bdb_psearch( op, rs, ps_list, op->oq_add.rs_e, LDAP_PSEARCH_BY_ADD );
522                 }
523         }
524 #endif /* LDAP_CLIENT_UPDATE */
525
526         if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
527                 ldap_pvt_thread_yield();
528                 TXN_CHECKPOINT( bdb->bi_dbenv,
529                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
530         }
531
532 done:
533
534         if( ltid != NULL ) {
535                 TXN_ABORT( ltid );
536                 op->o_private = NULL;
537         }
538
539         return ( ( rs->sr_err == LDAP_SUCCESS ) ? noop : rs->sr_err );
540 }
541