1 /* txn.c - LDAP Transactions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2014 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
21 #include <ac/socket.h>
22 #include <ac/string.h>
23 #include <ac/unistd.h>
31 const struct berval slap_EXOP_TXN_START = BER_BVC(LDAP_EXOP_X_TXN_START);
32 const struct berval slap_EXOP_TXN_END = BER_BVC(LDAP_EXOP_X_TXN_END);
35 Operation *op, SlapReply *rs )
40 Statslog( LDAP_DEBUG_STATS, "%s TXN START\n",
41 op->o_log_prefix, 0, 0, 0, 0 );
43 if( op->ore_reqdata != NULL ) {
44 rs->sr_text = "no request data expected";
45 return LDAP_PROTOCOL_ERROR;
48 op->o_bd = op->o_conn->c_authz_backend;
49 if( backend_check_restrictions( op, rs,
50 (struct berval *)&slap_EXOP_TXN_START ) != LDAP_SUCCESS )
55 /* acquire connection lock */
56 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
58 if( op->o_conn->c_txn != CONN_TXN_INACTIVE ) {
59 rs->sr_text = "Too many transactions";
64 assert( op->o_conn->c_txn_backend == NULL );
65 op->o_conn->c_txn = CONN_TXN_SPECIFY;
67 bv = (struct berval *) ch_malloc( sizeof (struct berval) );
75 /* release connection lock */
76 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
81 Operation *op, SlapReply *rs, LDAPControl *ctrl )
83 if ( !ctrl->ldctl_iscritical ) {
84 rs->sr_text = "txnSpec control must be marked critical";
85 return LDAP_PROTOCOL_ERROR;
88 rs->sr_text = "txnSpec control provided multiple times";
89 return LDAP_PROTOCOL_ERROR;
92 if ( ctrl->ldctl_value.bv_val == NULL ) {
93 rs->sr_text = "no transaction identifier provided";
94 return LDAP_PROTOCOL_ERROR;
96 if ( ctrl->ldctl_value.bv_len != 0 ) {
97 rs->sr_text = "invalid transaction identifier";
98 return LDAP_X_TXN_ID_INVALID;
101 if ( op->o_preread ) { /* temporary limitation */
102 rs->sr_text = "cannot perform pre-read in transaction";
103 return LDAP_UNWILLING_TO_PERFORM;
105 if ( op->o_postread ) { /* temporary limitation */
106 rs->sr_text = "cannot perform post-read in transaction";
107 return LDAP_UNWILLING_TO_PERFORM;
110 op->o_txnSpec = SLAP_CONTROL_CRITICAL;
114 typedef struct txn_rctrls {
115 struct txn_rctrls *tr_next;
117 LDAPControl ** tr_ctrls;
120 static int txn_result( Operation *op, SlapReply *rs )
122 if ( rs->sr_ctrls ) {
123 txn_rctrls **t0, *tr;
124 for ( t0 = (txn_rctrls **) &op->o_callback->sc_private; *t0;
125 t0 = &(*t0)->tr_next )
127 tr = op->o_tmpalloc( sizeof( txn_rctrls ), op->o_tmpmemctx );
130 tr->tr_msgid = op->o_msgid;
131 tr->tr_ctrls = ldap_controls_dup( rs->sr_ctrls );
136 static int txn_put_ctrls( Operation *op, BerElement *ber, txn_rctrls *tr )
140 ber_printf( ber, "{" );
141 for ( ; tr; tr = next ) {
143 ber_printf( ber, "{it{", tr->tr_msgid, LDAP_TAG_CONTROLS );
144 for ( i = 0; tr->tr_ctrls[i]; i++ )
145 ldap_pvt_put_control( tr->tr_ctrls[i], ber );
146 ber_printf( ber, "}}" );
147 ldap_controls_free( tr->tr_ctrls );
148 op->o_tmpfree( tr, op->o_tmpmemctx );
150 ber_printf( ber, "}" );
155 Operation *op, SlapReply *rs )
158 BerElementBuffer berbuf;
159 BerElement *ber = (BerElement *)&berbuf;
165 Connection *c = op->o_conn;
167 Statslog( LDAP_DEBUG_STATS, "%s TXN END\n",
168 op->o_log_prefix, 0, 0, 0, 0 );
170 if( op->ore_reqdata == NULL ) {
171 rs->sr_text = "request data expected";
172 return LDAP_PROTOCOL_ERROR;
174 if( op->ore_reqdata->bv_len == 0 ) {
175 rs->sr_text = "empty request data";
176 return LDAP_PROTOCOL_ERROR;
179 op->o_bd = c->c_authz_backend;
180 if( backend_check_restrictions( op, rs,
181 (struct berval *)&slap_EXOP_TXN_END ) != LDAP_SUCCESS )
186 ber_init2( ber, op->ore_reqdata, 0 );
188 tag = ber_scanf( ber, "{" /*}*/ );
189 if( tag == LBER_ERROR ) {
190 rs->sr_text = "request data decoding error";
191 return LDAP_PROTOCOL_ERROR;
194 tag = ber_peek_tag( ber, &len );
195 if( tag == LBER_BOOLEAN ) {
196 tag = ber_scanf( ber, "b", &commit );
197 if( tag == LBER_ERROR ) {
198 rs->sr_text = "request data decoding error";
199 return LDAP_PROTOCOL_ERROR;
203 tag = ber_scanf( ber, /*{*/ "m}", &txnid );
204 if( tag == LBER_ERROR ) {
205 rs->sr_text = "request data decoding error";
206 return LDAP_PROTOCOL_ERROR;
210 rs->sr_text = "invalid transaction identifier";
211 return LDAP_X_TXN_ID_INVALID;
214 /* acquire connection lock */
215 ldap_pvt_thread_mutex_lock( &c->c_mutex );
217 if( c->c_txn != CONN_TXN_SPECIFY ) {
218 rs->sr_text = "invalid transaction identifier";
219 rc = LDAP_X_TXN_ID_INVALID;
222 c->c_txn = CONN_TXN_SETTLE;
225 slap_callback cb = {0};
227 if ( op->o_abandon ) {
231 if( LDAP_STAILQ_EMPTY(&c->c_txn_ops) ) {
232 /* no updates to commit */
233 rs->sr_text = "no updates to commit";
234 rc = LDAP_OPERATIONS_ERROR;
238 cb.sc_response = txn_result;
239 LDAP_STAILQ_FOREACH( o, &c->c_txn_ops, o_next ) {
240 o->o_bd = c->c_txn_backend;
243 rc = o->o_bd->bd_info->bi_op_txn(o, SLAP_TXN_BEGIN, &txn );
245 rs->sr_text = "couldn't start DB transaction";
250 LDAP_SLIST_INSERT_HEAD( &o->o_extra, txn, oe_next );
252 cb.sc_next = o->o_callback;
255 SlapReply rs = {REP_RESULT};
256 int opidx = slap_req2op( o->o_tag );
257 assert( opidx != SLAP_OP_LAST );
258 o->o_threadctx = op->o_threadctx;
259 o->o_tid = op->o_tid;
260 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
261 rc = (&o->o_bd->bd_info->bi_op_bind)[opidx]( o, &rs );
262 ldap_pvt_thread_mutex_lock( &c->c_mutex );
265 struct berval *bv = NULL;
266 BerElementBuffer berbuf;
267 BerElement *ber = (BerElement *)&berbuf;
269 ber_init_w_nullc( ber, LBER_USE_DER );
270 ber_printf( ber, "{i", o->o_msgid );
272 txn_put_ctrls( op, ber, cb.sc_private );
273 ber_printf( ber, "}" );
274 ber_flatten( ber, &bv );
277 o->o_bd->bd_info->bi_op_txn(o, SLAP_TXN_ABORT, &txn );
281 if ( cb.sc_private ) {
282 struct berval *bv = NULL;
283 BerElementBuffer berbuf;
284 BerElement *ber = (BerElement *)&berbuf;
286 ber_init_w_nullc( ber, LBER_USE_DER );
287 ber_printf( ber, "{" );
288 txn_put_ctrls( op, ber, cb.sc_private );
289 ber_printf( ber, "}" );
290 ber_flatten( ber, &bv );
295 rc = o->o_bd->bd_info->bi_op_txn(o, SLAP_TXN_COMMIT, &txn );
297 rs->sr_text = "transaction commit failed";
301 rs->sr_text = "transaction aborted";
306 /* drain txn ops list */
307 while (( o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL ) {
308 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
309 LDAP_STAILQ_NEXT( o, o_next ) = NULL;
310 slap_op_free( o, NULL );
314 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
315 assert( c->c_txn == CONN_TXN_SETTLE );
316 c->c_txn = CONN_TXN_INACTIVE;
317 c->c_txn_backend = NULL;
320 /* release connection lock */
321 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
326 int txn_preop( Operation *op, SlapReply *rs )
330 /* acquire connection lock */
331 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
332 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
333 rs->sr_text = "invalid transaction identifier";
334 rs->sr_err = LDAP_X_TXN_ID_INVALID;
336 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
341 if( op->o_conn->c_txn_backend == NULL ) {
342 op->o_conn->c_txn_backend = op->o_bd;
344 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
345 rs->sr_text = "transaction cannot span multiple database contexts";
346 rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
350 /* insert operation into transaction */
351 LDAP_STAILQ_REMOVE( &op->o_conn->c_ops, op, Operation, o_next );
352 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_txn_ops, op, o_next );
355 /* release connection lock */
356 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
359 send_ldap_result( op, rs );
361 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
364 return LDAP_SUCCESS; /* proceed with operation */
367 #endif /* LDAP_X_TXN */