]> git.sur5r.net Git - openldap/blob - servers/slapd/txn.c
happy new year
[openldap] / servers / slapd / txn.c
1 /* txn.c - LDAP Transactions */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-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
21 #include <ac/krb.h>
22 #include <ac/socket.h>
23 #include <ac/string.h>
24 #include <ac/unistd.h>
25
26 #include "slap.h"
27
28 #include <lber_pvt.h>
29 #include <lutil.h>
30
31 #ifdef LDAP_X_TXN
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);
34
35 int txn_start_extop(
36         Operation *op, SlapReply *rs )
37 {
38         int rc;
39         struct berval *bv;
40
41         Statslog( LDAP_DEBUG_STATS, "%s TXN START\n",
42                 op->o_log_prefix, 0, 0, 0, 0 );
43
44         if( op->ore_reqdata != NULL ) {
45                 rs->sr_text = "no request data expected";
46                 return LDAP_PROTOCOL_ERROR;
47         }
48
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 )
52         {
53                 return rs->sr_err;
54         }
55
56         /* acquire connection lock */
57         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
58
59         if( op->o_conn->c_txn != CONN_TXN_INACTIVE ) {
60                 rs->sr_text = "Too many transactions";
61                 rc = LDAP_BUSY;
62                 goto done;
63         }
64
65         assert( op->o_conn->c_txn_backend == NULL );
66         op->o_conn->c_txn = CONN_TXN_SPECIFY;
67
68         bv = (struct berval *) ch_malloc( sizeof (struct berval) );
69         bv->bv_len = 0;
70         bv->bv_val = NULL;
71
72         rs->sr_rspdata = bv;
73         rc = LDAP_SUCCESS;
74
75 done:
76         /* release connection lock */
77         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
78         return rc;
79 }
80
81 int txn_spec_ctrl(
82         Operation *op, SlapReply *rs, LDAPControl *ctrl )
83 {
84         if ( !ctrl->ldctl_iscritical ) {
85                 rs->sr_text = "txnSpec control must be marked critical";
86                 return LDAP_PROTOCOL_ERROR;
87         }
88         if( op->o_txnSpec ) {
89                 rs->sr_text = "txnSpec control provided multiple times";
90                 return LDAP_PROTOCOL_ERROR;
91         }
92
93         if ( ctrl->ldctl_value.bv_val == NULL ) {
94                 rs->sr_text = "no transaction identifier provided";
95                 return LDAP_PROTOCOL_ERROR;
96         }
97         if ( ctrl->ldctl_value.bv_len != 0 ) {
98                 rs->sr_text = "invalid transaction identifier";
99                 return LDAP_X_TXN_ID_INVALID;
100         }
101
102         if ( op->o_preread ) { /* temporary limitation */
103                 rs->sr_text = "cannot perform pre-read in transaction";
104                 return LDAP_UNWILLING_TO_PERFORM;
105         } 
106         if ( op->o_postread ) { /* temporary limitation */
107                 rs->sr_text = "cannot perform post-read in transaction";
108                 return LDAP_UNWILLING_TO_PERFORM;
109         }
110
111         op->o_txnSpec = SLAP_CONTROL_CRITICAL;
112         return LDAP_SUCCESS;
113 }
114
115 int txn_end_extop(
116         Operation *op, SlapReply *rs )
117 {
118         int rc;
119         BerElementBuffer berbuf;
120         BerElement *ber = (BerElement *)&berbuf;
121         ber_tag_t tag;
122         ber_len_t len;
123         ber_int_t commit=1;
124         struct berval txnid;
125
126         Statslog( LDAP_DEBUG_STATS, "%s TXN END\n",
127                 op->o_log_prefix, 0, 0, 0, 0 );
128
129         if( op->ore_reqdata == NULL ) {
130                 rs->sr_text = "request data expected";
131                 return LDAP_PROTOCOL_ERROR;
132         }
133         if( op->ore_reqdata->bv_len == 0 ) {
134                 rs->sr_text = "empty request data";
135                 return LDAP_PROTOCOL_ERROR;
136         }
137
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 )
141         {
142                 return rs->sr_err;
143         }
144
145         ber_init2( ber, op->ore_reqdata, 0 );
146
147         tag = ber_scanf( ber, "{" /*}*/ );
148         if( tag == LBER_ERROR ) {
149                 rs->sr_text = "request data decoding error";
150                 return LDAP_PROTOCOL_ERROR;
151         }
152
153         tag = ber_peek_tag( ber, &len );
154         if( tag == LBER_BOOLEAN ) {
155                 tag = ber_scanf( ber, "b", &commit );
156                 if( tag == LBER_ERROR ) {
157                         rs->sr_text = "request data decoding error";
158                         return LDAP_PROTOCOL_ERROR;
159                 }
160         }
161
162         tag = ber_scanf( ber, /*{*/ "m}", &txnid );
163         if( tag == LBER_ERROR ) {
164                 rs->sr_text = "request data decoding error";
165                 return LDAP_PROTOCOL_ERROR;
166         }
167
168         if( txnid.bv_len ) {
169                 rs->sr_text = "invalid transaction identifier";
170                 return LDAP_X_TXN_ID_INVALID;
171         }
172
173         /* acquire connection lock */
174         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
175
176         if( op->o_conn->c_txn != CONN_TXN_SPECIFY ) {
177                 rs->sr_text = "invalid transaction identifier";
178                 rc = LDAP_X_TXN_ID_INVALID;
179                 goto done;
180         }
181         op->o_conn->c_txn = CONN_TXN_SETTLE;
182
183         if( commit ) {
184                 if ( op->o_abandon ) {
185                 }
186
187                 if( LDAP_STAILQ_EMPTY(&op->o_conn->c_txn_ops) ) {
188                         /* no updates to commit */
189                         rs->sr_text = "no updates to commit";
190                         rc = LDAP_OPERATIONS_ERROR;
191                         goto settled;
192                 }
193
194                 rs->sr_text = "not yet implemented";
195                 rc = LDAP_UNWILLING_TO_PERFORM;
196
197         } else {
198                 rs->sr_text = "transaction aborted";
199                 rc = LDAP_SUCCESS;;
200         }
201
202 drain:
203         /* drain txn ops list */
204
205 settled:
206         assert( LDAP_STAILQ_EMPTY(&op->o_conn->c_txn_ops) );
207         assert( op->o_conn->c_txn == CONN_TXN_SETTLE );
208         op->o_conn->c_txn = CONN_TXN_INACTIVE;
209         op->o_conn->c_txn_backend = NULL;
210
211 done:
212         /* release connection lock */
213         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
214
215         return rc;
216 }
217
218 #endif /* LDAP_X_TXN */