]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
More for ITS#6045: clear o_callback as well.
[openldap] / servers / slapd / back-bdb / add.c
1 /* add.c - ldap BerkeleyDB back-end add routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2009 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 #include <ac/string.h>
21
22 #include "back-bdb.h"
23
24 int
25 bdb_add(Operation *op, SlapReply *rs )
26 {
27         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
28         struct berval   pdn;
29         Entry           *p = NULL, *oe = op->ora_e;
30         EntryInfo       *ei;
31         char textbuf[SLAP_TEXT_BUFLEN];
32         size_t textlen = sizeof textbuf;
33         AttributeDescription *children = slap_schema.si_ad_children;
34         AttributeDescription *entry = slap_schema.si_ad_entry;
35         DB_TXN          *ltid = NULL, *lt2, *rtxn;
36         ID eid = NOID;
37         struct bdb_op_info opinfo = {{{ 0 }}};
38         int subentry;
39         DB_LOCK         lock;
40
41         int             num_retries = 0;
42         int             success;
43
44         LDAPControl **postread_ctrl = NULL;
45         LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
46         int num_ctrls = 0;
47
48 #ifdef LDAP_X_TXN
49         int settle = 0;
50 #endif
51
52         Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_add) ": %s\n",
53                 op->ora_e->e_name.bv_val, 0, 0);
54
55 #ifdef LDAP_X_TXN
56         if( op->o_txnSpec ) {
57                 /* acquire connection lock */
58                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
59                 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
60                         rs->sr_text = "invalid transaction identifier";
61                         rs->sr_err = LDAP_X_TXN_ID_INVALID;
62                         goto txnReturn;
63                 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
64                         settle=1;
65                         goto txnReturn;
66                 }
67
68                 if( op->o_conn->c_txn_backend == NULL ) {
69                         op->o_conn->c_txn_backend = op->o_bd;
70
71                 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
72                         rs->sr_text = "transaction cannot span multiple database contexts";
73                         rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
74                         goto txnReturn;
75                 }
76
77                 /* insert operation into transaction */
78
79                 rs->sr_text = "transaction specified";
80                 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
81
82 txnReturn:
83                 /* release connection lock */
84                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
85
86                 if( !settle ) {
87                         send_ldap_result( op, rs );
88                         return rs->sr_err;
89                 }
90         }
91 #endif
92
93         ctrls[num_ctrls] = 0;
94
95         /* check entry's schema */
96         rs->sr_err = entry_schema_check( op, op->ora_e, NULL,
97                 get_relax(op), 1, NULL, &rs->sr_text, textbuf, textlen );
98         if ( rs->sr_err != LDAP_SUCCESS ) {
99                 Debug( LDAP_DEBUG_TRACE,
100                         LDAP_XSTRING(bdb_add) ": entry failed schema check: "
101                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
102                 goto return_results;
103         }
104
105         /* add opattrs to shadow as well, only missing attrs will actually
106          * be added; helps compatibility with older OL versions */
107         rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
108         if ( rs->sr_err != LDAP_SUCCESS ) {
109                 Debug( LDAP_DEBUG_TRACE,
110                         LDAP_XSTRING(bdb_add) ": entry failed op attrs add: "
111                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
112                 goto return_results;
113         }
114
115         if ( get_assert( op ) &&
116                 ( test_filter( op, op->ora_e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
117         {
118                 rs->sr_err = LDAP_ASSERTION_FAILED;
119                 goto return_results;
120         }
121
122         subentry = is_entry_subentry( op->ora_e );
123
124         /* Get our reader TXN */
125         rs->sr_err = bdb_reader_get( op, bdb->bi_dbenv, &rtxn );
126
127         if( 0 ) {
128 retry:  /* transaction retry */
129                 if( p ) {
130                         /* free parent and reader lock */
131                         if ( p != (Entry *)&slap_entry_root ) {
132                                 bdb_unlocked_cache_return_entry_r( bdb, p );
133                         }
134                         p = NULL;
135                 }
136                 rs->sr_err = TXN_ABORT( ltid );
137                 ltid = NULL;
138                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
139                 opinfo.boi_oe.oe_key = NULL;
140                 op->o_do_not_cache = opinfo.boi_acl_cache;
141                 if( rs->sr_err != 0 ) {
142                         rs->sr_err = LDAP_OTHER;
143                         rs->sr_text = "internal error";
144                         goto return_results;
145                 }
146                 if ( op->o_abandon ) {
147                         rs->sr_err = SLAPD_ABANDON;
148                         goto return_results;
149                 }
150                 bdb_trans_backoff( ++num_retries );
151         }
152
153         /* begin transaction */
154         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
155                 bdb->bi_db_opflags );
156         rs->sr_text = NULL;
157         if( rs->sr_err != 0 ) {
158                 Debug( LDAP_DEBUG_TRACE,
159                         LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
160                         db_strerror(rs->sr_err), rs->sr_err, 0 );
161                 rs->sr_err = LDAP_OTHER;
162                 rs->sr_text = "internal error";
163                 goto return_results;
164         }
165
166         opinfo.boi_oe.oe_key = bdb;
167         opinfo.boi_txn = ltid;
168         opinfo.boi_err = 0;
169         opinfo.boi_acl_cache = op->o_do_not_cache;
170         LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
171
172         /*
173          * Get the parent dn and see if the corresponding entry exists.
174          */
175         if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
176                 pdn = slap_empty_bv;
177         } else {
178                 dnParent( &op->ora_e->e_nname, &pdn );
179         }
180
181         /* get entry or parent */
182         rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
183                 1, &lock );
184         switch( rs->sr_err ) {
185         case 0:
186                 rs->sr_err = LDAP_ALREADY_EXISTS;
187                 goto return_results;
188         case DB_NOTFOUND:
189                 break;
190         case DB_LOCK_DEADLOCK:
191         case DB_LOCK_NOTGRANTED:
192                 goto retry;
193         case LDAP_BUSY:
194                 rs->sr_text = "ldap server busy";
195                 goto return_results;
196         default:
197                 rs->sr_err = LDAP_OTHER;
198                 rs->sr_text = "internal error";
199                 goto return_results;
200         }
201
202         p = ei->bei_e;
203         if ( !p )
204                 p = (Entry *)&slap_entry_root;
205
206         if ( !bvmatch( &pdn, &p->e_nname ) ) {
207                 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
208                         op->o_tmpmemctx );
209                 rs->sr_ref = is_entry_referral( p )
210                         ? get_entry_referrals( op, p )
211                         : NULL;
212                 if ( p != (Entry *)&slap_entry_root )
213                         bdb_unlocked_cache_return_entry_r( bdb, p );
214                 p = NULL;
215                 Debug( LDAP_DEBUG_TRACE,
216                         LDAP_XSTRING(bdb_add) ": parent "
217                         "does not exist\n", 0, 0, 0 );
218
219                 rs->sr_err = LDAP_REFERRAL;
220                 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
221                 goto return_results;
222         }
223
224         rs->sr_err = access_allowed( op, p,
225                 children, NULL, ACL_WADD, NULL );
226
227         if ( ! rs->sr_err ) {
228                 switch( opinfo.boi_err ) {
229                 case DB_LOCK_DEADLOCK:
230                 case DB_LOCK_NOTGRANTED:
231                         goto retry;
232                 }
233
234                 if ( p != (Entry *)&slap_entry_root )
235                         bdb_unlocked_cache_return_entry_r( bdb, p );
236                 p = NULL;
237
238                 Debug( LDAP_DEBUG_TRACE,
239                         LDAP_XSTRING(bdb_add) ": no write access to parent\n",
240                         0, 0, 0 );
241                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
242                 rs->sr_text = "no write access to parent";
243                 goto return_results;;
244         }
245
246         if ( p != (Entry *)&slap_entry_root ) {
247                 if ( is_entry_subentry( p ) ) {
248                         bdb_unlocked_cache_return_entry_r( bdb, p );
249                         p = NULL;
250                         /* parent is a subentry, don't allow add */
251                         Debug( LDAP_DEBUG_TRACE,
252                                 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
253                                 0, 0, 0 );
254                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
255                         rs->sr_text = "parent is a subentry";
256                         goto return_results;;
257                 }
258
259                 if ( is_entry_alias( p ) ) {
260                         bdb_unlocked_cache_return_entry_r( bdb, p );
261                         p = NULL;
262                         /* parent is an alias, don't allow add */
263                         Debug( LDAP_DEBUG_TRACE,
264                                 LDAP_XSTRING(bdb_add) ": parent is alias\n",
265                                 0, 0, 0 );
266                         rs->sr_err = LDAP_ALIAS_PROBLEM;
267                         rs->sr_text = "parent is an alias";
268                         goto return_results;;
269                 }
270
271                 if ( is_entry_referral( p ) ) {
272                         /* parent is a referral, don't allow add */
273                         rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
274                                 op->o_tmpmemctx );
275                         rs->sr_ref = get_entry_referrals( op, p );
276                         bdb_unlocked_cache_return_entry_r( bdb, p );
277                         p = NULL;
278                         Debug( LDAP_DEBUG_TRACE,
279                                 LDAP_XSTRING(bdb_add) ": parent is referral\n",
280                                 0, 0, 0 );
281
282                         rs->sr_err = LDAP_REFERRAL;
283                         rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
284                         goto return_results;
285                 }
286
287         }
288
289         if ( subentry ) {
290                 /* FIXME: */
291                 /* parent must be an administrative point of the required kind */
292         }
293
294         /* free parent and reader lock */
295         if ( p != (Entry *)&slap_entry_root ) {
296                 if ( p->e_nname.bv_len ) {
297                         struct berval ppdn;
298
299                         /* ITS#5326: use parent's DN if differs from provided one */
300                         dnParent( &op->ora_e->e_name, &ppdn );
301                         if ( !dn_match( &p->e_name, &ppdn ) ) {
302                                 struct berval rdn;
303                                 struct berval newdn;
304
305                                 dnRdn( &op->ora_e->e_name, &rdn );
306
307                                 build_new_dn( &newdn, &p->e_name, &rdn, NULL ); 
308                                 ber_memfree( op->ora_e->e_name.bv_val );
309                                 op->ora_e->e_name = newdn;
310
311                                 /* FIXME: should check whether
312                                  * dnNormalize(newdn) == e->e_nname ... */
313                         }
314                 }
315
316                 bdb_unlocked_cache_return_entry_r( bdb, p );
317         }
318         p = NULL;
319
320         rs->sr_err = access_allowed( op, op->ora_e,
321                 entry, NULL, ACL_WADD, NULL );
322
323         if ( ! rs->sr_err ) {
324                 switch( opinfo.boi_err ) {
325                 case DB_LOCK_DEADLOCK:
326                 case DB_LOCK_NOTGRANTED:
327                         goto retry;
328                 }
329
330                 Debug( LDAP_DEBUG_TRACE,
331                         LDAP_XSTRING(bdb_add) ": no write access to entry\n",
332                         0, 0, 0 );
333                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
334                 rs->sr_text = "no write access to entry";
335                 goto return_results;;
336         }
337
338         /* 
339          * Check ACL for attribute write access
340          */
341         if (!acl_check_modlist(op, oe, op->ora_modlist)) {
342                 switch( opinfo.boi_err ) {
343                 case DB_LOCK_DEADLOCK:
344                 case DB_LOCK_NOTGRANTED:
345                         goto retry;
346                 }
347
348                 Debug( LDAP_DEBUG_TRACE,
349                         LDAP_XSTRING(bdb_add) ": no write access to attribute\n",
350                         0, 0, 0 );
351                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
352                 rs->sr_text = "no write access to attribute";
353                 goto return_results;;
354         }
355
356         if ( eid == NOID ) {
357                 rs->sr_err = bdb_next_id( op->o_bd, &eid );
358                 if( rs->sr_err != 0 ) {
359                         Debug( LDAP_DEBUG_TRACE,
360                                 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
361                                 rs->sr_err, 0, 0 );
362                         rs->sr_err = LDAP_OTHER;
363                         rs->sr_text = "internal error";
364                         goto return_results;
365                 }
366                 op->ora_e->e_id = eid;
367         }
368
369         /* nested transaction */
370         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, 
371                 bdb->bi_db_opflags );
372         rs->sr_text = NULL;
373         if( rs->sr_err != 0 ) {
374                 Debug( LDAP_DEBUG_TRACE,
375                         LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
376                         "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
377                 rs->sr_err = LDAP_OTHER;
378                 rs->sr_text = "internal error";
379                 goto return_results;
380         }
381
382         /* dn2id index */
383         rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->ora_e );
384         if ( rs->sr_err != 0 ) {
385                 Debug( LDAP_DEBUG_TRACE,
386                         LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
387                         db_strerror(rs->sr_err), rs->sr_err, 0 );
388
389                 switch( rs->sr_err ) {
390                 case DB_LOCK_DEADLOCK:
391                 case DB_LOCK_NOTGRANTED:
392                         goto retry;
393                 case DB_KEYEXIST:
394                         rs->sr_err = LDAP_ALREADY_EXISTS;
395                         break;
396                 default:
397                         rs->sr_err = LDAP_OTHER;
398                 }
399                 goto return_results;
400         }
401
402         /* attribute indexes */
403         rs->sr_err = bdb_index_entry_add( op, lt2, op->ora_e );
404         if ( rs->sr_err != LDAP_SUCCESS ) {
405                 Debug( LDAP_DEBUG_TRACE,
406                         LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
407                         0, 0, 0 );
408                 switch( rs->sr_err ) {
409                 case DB_LOCK_DEADLOCK:
410                 case DB_LOCK_NOTGRANTED:
411                         goto retry;
412                 default:
413                         rs->sr_err = LDAP_OTHER;
414                 }
415                 rs->sr_text = "index generation failed";
416                 goto return_results;
417         }
418
419         /* id2entry index */
420         rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->ora_e );
421         if ( rs->sr_err != 0 ) {
422                 Debug( LDAP_DEBUG_TRACE,
423                         LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
424                         0, 0, 0 );
425                 switch( rs->sr_err ) {
426                 case DB_LOCK_DEADLOCK:
427                 case DB_LOCK_NOTGRANTED:
428                         goto retry;
429                 default:
430                         rs->sr_err = LDAP_OTHER;
431                 }
432                 rs->sr_text = "entry store failed";
433                 goto return_results;
434         }
435
436         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
437                 rs->sr_err = LDAP_OTHER;
438                 rs->sr_text = "txn_commit(2) failed";
439                 goto return_results;
440         }
441
442         /* post-read */
443         if( op->o_postread ) {
444                 if( postread_ctrl == NULL ) {
445                         postread_ctrl = &ctrls[num_ctrls++];
446                         ctrls[num_ctrls] = NULL;
447                 }
448                 if ( slap_read_controls( op, rs, op->ora_e,
449                         &slap_post_read_bv, postread_ctrl ) )
450                 {
451                         Debug( LDAP_DEBUG_TRACE,
452                                 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
453                                 "failed!\n", 0, 0, 0 );
454                         if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
455                                 /* FIXME: is it correct to abort
456                                  * operation if control fails? */
457                                 goto return_results;
458                         }
459                 }
460         }
461
462         if ( op->o_noop ) {
463                 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
464                         rs->sr_text = "txn_abort (no-op) failed";
465                 } else {
466                         rs->sr_err = LDAP_X_NO_OPERATION;
467                         ltid = NULL;
468                         goto return_results;
469                 }
470
471         } else {
472                 struct berval nrdn;
473
474                 /* pick the RDN if not suffix; otherwise pick the entire DN */
475                 if (pdn.bv_len) {
476                         nrdn.bv_val = op->ora_e->e_nname.bv_val;
477                         nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
478                 } else {
479                         nrdn = op->ora_e->e_nname;
480                 }
481
482                 /* Use the reader txn here, outside the add txn */
483                 bdb_cache_add( bdb, ei, op->ora_e, &nrdn, rtxn, &lock );
484
485                 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
486                         rs->sr_text = "txn_commit failed";
487                 } else {
488                         rs->sr_err = LDAP_SUCCESS;
489                 }
490         }
491
492         ltid = NULL;
493         LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
494         opinfo.boi_oe.oe_key = NULL;
495
496         if ( rs->sr_err != LDAP_SUCCESS ) {
497                 Debug( LDAP_DEBUG_TRACE,
498                         LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
499                         rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
500                 rs->sr_err = LDAP_OTHER;
501                 goto return_results;
502         }
503
504         Debug(LDAP_DEBUG_TRACE,
505                 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
506                 op->o_noop ? " (no-op)" : "",
507                 op->ora_e->e_id, op->ora_e->e_dn );
508
509         rs->sr_text = NULL;
510         if( num_ctrls ) rs->sr_ctrls = ctrls;
511
512 return_results:
513         success = rs->sr_err;
514         send_ldap_result( op, rs );
515         slap_graduate_commit_csn( op );
516
517         if( ltid != NULL ) {
518                 TXN_ABORT( ltid );
519         }
520         if ( opinfo.boi_oe.oe_key ) {
521                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
522         }
523
524         if( success == LDAP_SUCCESS ) {
525                 /* We own the entry now, and it can be purged at will
526                  * Check to make sure it's the same entry we entered with.
527                  * Possibly a callback may have mucked with it, although
528                  * in general callbacks should treat the entry as read-only.
529                  */
530                 bdb_cache_return_entry_r( bdb, oe, &lock );
531                 if ( op->ora_e == oe )
532                         op->ora_e = NULL;
533
534                 if ( bdb->bi_txn_cp_kbyte ) {
535                         TXN_CHECKPOINT( bdb->bi_dbenv,
536                                 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
537                 }
538         }
539
540         if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
541                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
542                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
543         }
544
545         return rs->sr_err;
546 }