]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
More for -q tool mode
[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-2007 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;
36         struct bdb_op_info opinfo = {0};
37         int subentry;
38         BDB_LOCKER      locker = 0, rlocker = 0;
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->oq_add.rs_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
96         /* check entry's schema */
97         rs->sr_err = entry_schema_check( op, op->oq_add.rs_e, NULL,
98                 get_relax(op), 1, &rs->sr_text, textbuf, textlen );
99         if ( rs->sr_err != LDAP_SUCCESS ) {
100                 Debug( LDAP_DEBUG_TRACE,
101                         LDAP_XSTRING(bdb_add) ": entry failed schema check: "
102                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
103                 goto return_results;
104         }
105
106         /* add opattrs to shadow as well, only missing attrs will actually
107          * be added; helps compatibility with older OL versions */
108         rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
109         if ( rs->sr_err != LDAP_SUCCESS ) {
110                 Debug( LDAP_DEBUG_TRACE,
111                         LDAP_XSTRING(bdb_add) ": entry failed op attrs add: "
112                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
113                 goto return_results;
114         }
115
116         subentry = is_entry_subentry( op->oq_add.rs_e );
117
118         /*
119          * acquire an ID outside of the operation transaction
120          * to avoid serializing adds.
121          */
122         rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
123         if( rs->sr_err != 0 ) {
124                 Debug( LDAP_DEBUG_TRACE,
125                         LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
126                         rs->sr_err, 0, 0 );
127                 rs->sr_err = LDAP_OTHER;
128                 rs->sr_text = "internal error";
129                 goto return_results;
130         }
131
132         /* Get our thread locker ID */
133         rs->sr_err = LOCK_ID( bdb->bi_dbenv, &rlocker );
134
135         if( 0 ) {
136 retry:  /* transaction retry */
137                 if( p ) {
138                         /* free parent and reader lock */
139                         if ( p != (Entry *)&slap_entry_root ) {
140                                 bdb_unlocked_cache_return_entry_r( bdb, p );
141                         }
142                         p = NULL;
143                 }
144                 rs->sr_err = TXN_ABORT( ltid );
145                 ltid = NULL;
146                 op->o_private = NULL;
147                 op->o_do_not_cache = opinfo.boi_acl_cache;
148                 if( rs->sr_err != 0 ) {
149                         rs->sr_err = LDAP_OTHER;
150                         rs->sr_text = "internal error";
151                         goto return_results;
152                 }
153                 if ( op->o_abandon ) {
154                         rs->sr_err = SLAPD_ABANDON;
155                         goto return_results;
156                 }
157                 bdb_trans_backoff( ++num_retries );
158         }
159
160         /* begin transaction */
161         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
162                 bdb->bi_db_opflags );
163         rs->sr_text = NULL;
164         if( rs->sr_err != 0 ) {
165                 Debug( LDAP_DEBUG_TRACE,
166                         LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
167                         db_strerror(rs->sr_err), rs->sr_err, 0 );
168                 rs->sr_err = LDAP_OTHER;
169                 rs->sr_text = "internal error";
170                 goto return_results;
171         }
172
173         locker = TXN_ID ( ltid );
174
175         opinfo.boi_bdb = op->o_bd;
176         opinfo.boi_txn = ltid;
177         opinfo.boi_locker = locker;
178         opinfo.boi_err = 0;
179         opinfo.boi_acl_cache = op->o_do_not_cache;
180         op->o_private = &opinfo;
181         
182         /*
183          * Get the parent dn and see if the corresponding entry exists.
184          */
185         if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
186                 pdn = slap_empty_bv;
187         } else {
188                 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
189         }
190
191         /* get entry or parent */
192         rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
193                 1, locker, &lock );
194         switch( rs->sr_err ) {
195         case 0:
196                 rs->sr_err = LDAP_ALREADY_EXISTS;
197                 goto return_results;
198         case DB_NOTFOUND:
199                 break;
200         case DB_LOCK_DEADLOCK:
201         case DB_LOCK_NOTGRANTED:
202                 goto retry;
203         case LDAP_BUSY:
204                 rs->sr_text = "ldap server busy";
205                 goto return_results;
206         default:
207                 rs->sr_err = LDAP_OTHER;
208                 rs->sr_text = "internal error";
209                 goto return_results;
210         }
211
212         p = ei->bei_e;
213         if ( !p )
214                 p = (Entry *)&slap_entry_root;
215
216         if ( !bvmatch( &pdn, &p->e_nname ) ) {
217                 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
218                         op->o_tmpmemctx );
219                 rs->sr_ref = is_entry_referral( p )
220                         ? get_entry_referrals( op, p )
221                         : NULL;
222                 bdb_unlocked_cache_return_entry_r( bdb, p );
223                 p = NULL;
224                 Debug( LDAP_DEBUG_TRACE,
225                         LDAP_XSTRING(bdb_add) ": parent "
226                         "does not exist\n", 0, 0, 0 );
227
228                 rs->sr_err = LDAP_REFERRAL;
229                 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
230                 goto return_results;
231         }
232
233         rs->sr_err = access_allowed( op, p,
234                 children, NULL, ACL_WADD, NULL );
235
236         if ( ! rs->sr_err ) {
237                 switch( opinfo.boi_err ) {
238                 case DB_LOCK_DEADLOCK:
239                 case DB_LOCK_NOTGRANTED:
240                         goto retry;
241                 }
242
243                 Debug( LDAP_DEBUG_TRACE,
244                         LDAP_XSTRING(bdb_add) ": no write access to parent\n",
245                         0, 0, 0 );
246                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
247                 rs->sr_text = "no write access to parent";
248                 goto return_results;;
249         }
250
251         if ( p != (Entry *)&slap_entry_root ) {
252                 if ( is_entry_subentry( p ) ) {
253                         /* parent is a subentry, don't allow add */
254                         Debug( LDAP_DEBUG_TRACE,
255                                 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
256                                 0, 0, 0 );
257                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
258                         rs->sr_text = "parent is a subentry";
259                         goto return_results;;
260                 }
261
262                 if ( is_entry_alias( p ) ) {
263                         /* parent is an alias, don't allow add */
264                         Debug( LDAP_DEBUG_TRACE,
265                                 LDAP_XSTRING(bdb_add) ": parent is alias\n",
266                                 0, 0, 0 );
267                         rs->sr_err = LDAP_ALIAS_PROBLEM;
268                         rs->sr_text = "parent is an alias";
269                         goto return_results;;
270                 }
271
272                 if ( is_entry_referral( p ) ) {
273                         /* parent is a referral, don't allow add */
274                         rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
275                                 op->o_tmpmemctx );
276                         rs->sr_ref = get_entry_referrals( op, p );
277                         bdb_unlocked_cache_return_entry_r( bdb, p );
278                         p = NULL;
279                         Debug( LDAP_DEBUG_TRACE,
280                                 LDAP_XSTRING(bdb_add) ": parent is referral\n",
281                                 0, 0, 0 );
282
283                         rs->sr_err = LDAP_REFERRAL;
284                         rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
285                         goto return_results;
286                 }
287
288         }
289
290         if ( subentry ) {
291                 /* FIXME: */
292                 /* parent must be an administrative point of the required kind */
293         }
294
295         /* free parent and reader lock */
296         if ( p != (Entry *)&slap_entry_root ) {
297                 bdb_unlocked_cache_return_entry_r( bdb, p );
298         }
299         p = NULL;
300
301         rs->sr_err = access_allowed( op, op->oq_add.rs_e,
302                 entry, NULL, ACL_WADD, NULL );
303
304         if ( ! rs->sr_err ) {
305                 switch( opinfo.boi_err ) {
306                 case DB_LOCK_DEADLOCK:
307                 case DB_LOCK_NOTGRANTED:
308                         goto retry;
309                 }
310
311                 Debug( LDAP_DEBUG_TRACE,
312                         LDAP_XSTRING(bdb_add) ": no write access to entry\n",
313                         0, 0, 0 );
314                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
315                 rs->sr_text = "no write access to entry";
316                 goto return_results;;
317         }
318
319         /* nested transaction */
320         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, 
321                 bdb->bi_db_opflags );
322         rs->sr_text = NULL;
323         if( rs->sr_err != 0 ) {
324                 Debug( LDAP_DEBUG_TRACE,
325                         LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
326                         "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
327                 rs->sr_err = LDAP_OTHER;
328                 rs->sr_text = "internal error";
329                 goto return_results;
330         }
331
332         /* dn2id index */
333         rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
334         if ( rs->sr_err != 0 ) {
335                 Debug( LDAP_DEBUG_TRACE,
336                         LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
337                         db_strerror(rs->sr_err), rs->sr_err, 0 );
338
339                 switch( rs->sr_err ) {
340                 case DB_LOCK_DEADLOCK:
341                 case DB_LOCK_NOTGRANTED:
342                         goto retry;
343                 case DB_KEYEXIST:
344                         rs->sr_err = LDAP_ALREADY_EXISTS;
345                         break;
346                 default:
347                         rs->sr_err = LDAP_OTHER;
348                 }
349                 goto return_results;
350         }
351
352         /* attribute indexes */
353         rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
354         if ( rs->sr_err != LDAP_SUCCESS ) {
355                 Debug( LDAP_DEBUG_TRACE,
356                         LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
357                         0, 0, 0 );
358                 switch( rs->sr_err ) {
359                 case DB_LOCK_DEADLOCK:
360                 case DB_LOCK_NOTGRANTED:
361                         goto retry;
362                 default:
363                         rs->sr_err = LDAP_OTHER;
364                 }
365                 rs->sr_text = "index generation failed";
366                 goto return_results;
367         }
368
369         /* id2entry index */
370         rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
371         if ( rs->sr_err != 0 ) {
372                 Debug( LDAP_DEBUG_TRACE,
373                         LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
374                         0, 0, 0 );
375                 switch( rs->sr_err ) {
376                 case DB_LOCK_DEADLOCK:
377                 case DB_LOCK_NOTGRANTED:
378                         goto retry;
379                 default:
380                         rs->sr_err = LDAP_OTHER;
381                 }
382                 rs->sr_text = "entry store failed";
383                 goto return_results;
384         }
385
386         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
387                 rs->sr_err = LDAP_OTHER;
388                 rs->sr_text = "txn_commit(2) failed";
389                 goto return_results;
390         }
391
392         /* post-read */
393         if( op->o_postread ) {
394                 if( postread_ctrl == NULL ) {
395                         postread_ctrl = &ctrls[num_ctrls++];
396                         ctrls[num_ctrls] = NULL;
397                 }
398                 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
399                         &slap_post_read_bv, postread_ctrl ) )
400                 {
401                         Debug( LDAP_DEBUG_TRACE,
402                                 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
403                                 "failed!\n", 0, 0, 0 );
404                         if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
405                                 /* FIXME: is it correct to abort
406                                  * operation if control fails? */
407                                 goto return_results;
408                         }
409                 }
410         }
411
412         if ( op->o_noop ) {
413                 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
414                         rs->sr_text = "txn_abort (no-op) failed";
415                 } else {
416                         rs->sr_err = LDAP_X_NO_OPERATION;
417                         ltid = NULL;
418                         goto return_results;
419                 }
420
421         } else {
422                 struct berval nrdn;
423
424                 /* pick the RDN if not suffix; otherwise pick the entire DN */
425                 if (pdn.bv_len) {
426                         nrdn.bv_val = op->ora_e->e_nname.bv_val;
427                         nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
428                 } else {
429                         nrdn = op->ora_e->e_nname;
430                 }
431
432                 /* Use the thread locker here, outside the txn */
433                 bdb_cache_add( bdb, ei, op->ora_e, &nrdn, rlocker, &lock );
434
435                 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
436                         rs->sr_text = "txn_commit failed";
437                 } else {
438                         rs->sr_err = LDAP_SUCCESS;
439                 }
440         }
441
442         ltid = NULL;
443         op->o_private = NULL;
444
445         if ( rs->sr_err != LDAP_SUCCESS ) {
446                 Debug( LDAP_DEBUG_TRACE,
447                         LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
448                         rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
449                 rs->sr_err = LDAP_OTHER;
450                 goto return_results;
451         }
452
453         Debug(LDAP_DEBUG_TRACE,
454                 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
455                 op->o_noop ? " (no-op)" : "",
456                 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
457
458         rs->sr_text = NULL;
459         if( num_ctrls ) rs->sr_ctrls = ctrls;
460
461 return_results:
462         success = rs->sr_err;
463         send_ldap_result( op, rs );
464         slap_graduate_commit_csn( op );
465
466         if( ltid != NULL ) {
467                 TXN_ABORT( ltid );
468         }
469         op->o_private = NULL;
470
471         if( success == LDAP_SUCCESS ) {
472                 /* We own the entry now, and it can be purged at will
473                  * Check to make sure it's the same entry we entered with.
474                  * Possibly a callback may have mucked with it, although
475                  * in general callbacks should treat the entry as read-only.
476                  */
477                 bdb_cache_return_entry_r( bdb, oe, &lock );
478                 if ( op->ora_e == oe )
479                         op->ora_e = NULL;
480
481                 if ( bdb->bi_txn_cp_kbyte ) {
482                         TXN_CHECKPOINT( bdb->bi_dbenv,
483                                 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
484                 }
485         }
486
487         if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
488                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
489                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
490         }
491
492         return rs->sr_err;
493 }