1 /* txn.c - LDAP Transactions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 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>.
22 #include <ac/socket.h>
23 #include <ac/string.h>
24 #include <ac/unistd.h>
32 const struct berval slap_EXOP_TXN_START = BER_BVC(LDAP_EXOP_X_TXN_START);
33 const struct berval slap_EXOP_TXN_END = BER_BVC(LDAP_EXOP_X_TXN_END);
36 Operation *op, SlapReply *rs )
41 Statslog( LDAP_DEBUG_STATS, "%s TXN START\n",
42 op->o_log_prefix, 0, 0, 0, 0 );
44 if( op->ore_reqdata != NULL ) {
45 rs->sr_text = "no request data expected";
46 return LDAP_PROTOCOL_ERROR;
49 op->o_bd = op->o_conn->c_authz_backend;
50 if( backend_check_restrictions( op, rs,
51 (struct berval *)&slap_EXOP_TXN_START ) != LDAP_SUCCESS )
56 /* acquire connection lock */
57 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
59 if( op->o_conn->c_txn ) {
60 rs->sr_text = "Too many transactions";
65 assert( op->o_conn->c_txn_backend == NULL );
68 bv = (struct berval *) ch_malloc( sizeof (struct berval) );
76 /* release connection lock */
77 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
82 Operation *op, SlapReply *rs, LDAPControl *ctrl )
84 if ( !ctrl->ldctl_iscritical ) {
85 rs->sr_text = "txnSpec control must be marked critical";
86 return LDAP_PROTOCOL_ERROR;
89 rs->sr_text = "txnSpec control provided multiple times";
90 return LDAP_PROTOCOL_ERROR;
93 if ( ctrl->ldctl_value.bv_val == NULL ) {
94 rs->sr_text = "no transaction identifier provided";
95 return LDAP_PROTOCOL_ERROR;
97 if ( ctrl->ldctl_value.bv_len != 0 ) {
98 rs->sr_text = "invalid transaction identifier";
99 return LDAP_X_TXN_ID_INVALID;
102 if ( op->o_preread ) { /* temporary limitation */
103 rs->sr_text = "cannot perform pre-read in transaction";
104 return LDAP_UNWILLING_TO_PERFORM;
106 if ( op->o_postread ) { /* temporary limitation */
107 rs->sr_text = "cannot perform post-read in transaction";
108 return LDAP_UNWILLING_TO_PERFORM;
111 op->o_txnSpec = SLAP_CONTROL_CRITICAL;
116 Operation *op, SlapReply *rs )
119 BerElementBuffer berbuf;
120 BerElement *ber = (BerElement *)&berbuf;
126 Statslog( LDAP_DEBUG_STATS, "%s TXN END\n",
127 op->o_log_prefix, 0, 0, 0, 0 );
129 if( op->ore_reqdata == NULL ) {
130 rs->sr_text = "request data expected";
131 return LDAP_PROTOCOL_ERROR;
133 if( op->ore_reqdata->bv_len == 0 ) {
134 rs->sr_text = "empty request data";
135 return LDAP_PROTOCOL_ERROR;
138 op->o_bd = op->o_conn->c_authz_backend;
139 if( backend_check_restrictions( op, rs,
140 (struct berval *)&slap_EXOP_TXN_END ) != LDAP_SUCCESS )
145 ber_init2( ber, op->ore_reqdata, 0 );
147 tag = ber_scanf( ber, "{" /*}*/ );
148 if( tag == LBER_ERROR ) {
149 rs->sr_text = "request data decoding error";
150 return LDAP_PROTOCOL_ERROR;
153 tag = ber_peek_tag( ber, &len );
154 assert( tag == LBER_BOOLEAN );
155 if( tag == LBER_BOOLEAN ) {
156 tag = ber_scanf( ber, "b", &commit );
157 if( tag == LBER_ERROR ) {
158 rs->sr_text = "request data decoding error";
159 return LDAP_PROTOCOL_ERROR;
163 tag = ber_scanf( ber, /*{*/ "m}", &txnid );
164 if( tag == LBER_ERROR ) {
165 rs->sr_text = "request data decoding error";
166 return LDAP_PROTOCOL_ERROR;
170 rs->sr_text = "invalid transaction identifier";
171 return LDAP_X_TXN_ID_INVALID;
174 /* acquire connection lock */
175 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
177 if( op->o_conn->c_txn == 0 ) {
178 rs->sr_text = "invalid transaction identifier";
179 rc = LDAP_X_TXN_ID_INVALID;
184 rs->sr_text = "not yet implemented";
185 rc = LDAP_UNWILLING_TO_PERFORM;
188 rs->sr_text = "transaction aborted";
192 op->o_conn->c_txn = 0;
193 op->o_conn->c_txn_backend = NULL;
196 /* release connection lock */
197 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
202 #endif /* LDAP_X_TXN */