]> git.sur5r.net Git - openldap/blob - servers/slapd/ctxcsn.c
92c80fa96c075255fc65a5dd053e0193b1cc3569
[openldap] / servers / slapd / ctxcsn.c
1 /* $OpenLDAP$ */
2 /*
3  * Context CSN Management Routines
4  */
5 /* Copyright (c) 2003 by International Business Machines, Inc.
6  *
7  * International Business Machines, Inc. (hereinafter called IBM) grants
8  * permission under its copyrights to use, copy, modify, and distribute this
9  * Software with or without fee, provided that the above copyright notice and
10  * all paragraphs of this notice appear in all copies, and that the name of IBM
11  * not be used in connection with the marketing of any product incorporating
12  * the Software or modifications thereof, without specific, written prior
13  * permission.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
18  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
19  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
20  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28 #include <ac/socket.h>
29 #include <db.h>
30
31 #include "ldap_pvt.h"
32 #include "lutil.h"
33 #include "slap.h"
34 #include "lutil_ldap.h"
35
36 struct berval *
37 slap_get_commit_csn( Operation *op )
38 {
39         struct berval *max_committed_csn = NULL;
40         struct slap_csn_entry *csne = NULL, *committed_csne = NULL;
41         int i = 0;
42
43         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
44
45         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
46                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
47                         break;
48         }
49
50         if ( csne ) {
51                 csne->state = SLAP_CSN_COMMIT;
52         }
53
54         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
55                 if ( csne->state == SLAP_CSN_COMMIT )
56                         committed_csne = csne;
57                 if ( csne->state == SLAP_CSN_PENDING )
58                         break;
59         }
60
61         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
62
63         if ( committed_csne ) {
64                 max_committed_csn = ber_dupbv( NULL, committed_csne->csn );
65         }
66
67         return max_committed_csn;
68 }
69
70 void
71 slap_rewind_commit_csn( Operation *op )
72 {
73         struct slap_csn_entry *csne = NULL;
74
75         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
76
77         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
78                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
79                         break;
80         }
81
82         if ( csne ) {
83                 csne->state = SLAP_CSN_PENDING;
84         }
85         
86         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
87 }
88
89 void
90 slap_graduate_commit_csn( Operation *op )
91 {
92         struct slap_csn_entry *csne = NULL;
93
94         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
95
96         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
97                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
98                         break;
99         }
100
101         if ( csne ) {
102                 LDAP_TAILQ_REMOVE( &op->o_bd->be_pending_csn_list, csne, csn_link );
103                 ch_free( csne->csn->bv_val );
104                 ch_free( csne->csn );
105                 ch_free( csne );
106         }
107
108         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
109
110         return;
111 }
112
113 Entry *
114 slap_create_context_csn_entry(
115         Backend *be,
116         struct berval *context_csn
117 )
118 {
119         Modifications *ml;
120         Modifications *mlnext;
121         Modifications *mod;
122         Modifications *modlist;
123         Modifications **modtail = &modlist;
124
125         struct berval* ocbva = NULL;
126         struct berval* socbva = NULL;
127         struct berval* cnbva = NULL;
128         struct berval* ssbva = NULL;
129         struct berval* scbva = NULL;
130
131         char substr[64];
132         char rdnstr[67];
133         const char      *text;
134         char txtbuf[SLAP_TEXT_BUFLEN];
135         size_t textlen = sizeof txtbuf;
136
137         Entry* e;
138         int rc;
139
140         struct berval sub_bv = { 0, NULL };
141         struct berval psubrdn = { 0, NULL };
142         
143         slap_callback cb;
144         SlapReply       rs = {REP_RESULT};
145
146         struct berval rdn = { 0, NULL };
147         int match = 0;
148         char *def_filter_str = NULL;
149
150         ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
151         socbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
152         cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
153         ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
154         scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
155
156         ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
157         ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
158         ber_str2bv( "syncProviderSubentry",
159                         strlen("syncProviderSubentry"), 1, &ocbva[2] );
160         ocbva[3].bv_len = 0;
161         ocbva[3].bv_val = NULL;
162
163         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
164         mod->sml_op = LDAP_MOD_REPLACE;
165         mod->sml_next = NULL;
166         mod->sml_desc = NULL;
167         ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
168         mod->sml_bvalues = ocbva;
169         mod->sml_nvalues = NULL;
170         *modtail = mod;
171         modtail = &mod->sml_next;
172
173         ber_str2bv( "syncProviderSubentry",
174                         strlen("syncProviderSubentry"), 1, &socbva[0] );
175         socbva[1].bv_len = 0;
176         socbva[1].bv_val = NULL;
177
178         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
179         mod->sml_op = LDAP_MOD_REPLACE;
180         mod->sml_next = NULL;
181         mod->sml_desc = NULL;
182         ber_str2bv( "structuralObjectClass", strlen("structuralObjectClass"), 1, &mod->sml_type );
183         mod->sml_bvalues = socbva;
184         mod->sml_nvalues = NULL;
185         *modtail = mod;
186         modtail = &mod->sml_next;
187
188         sprintf( substr, "ldapsync" );
189         sprintf( rdnstr, "cn=%s", substr );
190         ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
191         ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
192         cnbva[1].bv_len = 0;
193         cnbva[1].bv_val = NULL;
194         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
195         mod->sml_op = LDAP_MOD_REPLACE;
196         mod->sml_next = NULL;
197         mod->sml_desc = NULL;
198         ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
199         mod->sml_bvalues = cnbva;
200         mod->sml_nvalues = NULL;
201         *modtail = mod;
202         modtail = &mod->sml_next;
203
204         if ( context_csn ) {
205                 ber_dupbv( &scbva[0], context_csn );
206                 scbva[1].bv_len = 0;
207                 scbva[1].bv_val = NULL;
208                 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
209                 mod->sml_op = LDAP_MOD_REPLACE;
210                 mod->sml_next = NULL;
211                 mod->sml_desc = NULL;
212                 ber_str2bv( "contextCSN", strlen("contextCSN"), 1, &mod->sml_type );
213                 mod->sml_bvalues = scbva;
214                 mod->sml_nvalues = NULL;
215                 *modtail = mod;
216                 modtail = &mod->sml_next;
217         }
218
219         ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
220         ssbva[1].bv_len = 0;
221         ssbva[1].bv_val = NULL;
222         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
223         mod->sml_op = LDAP_MOD_REPLACE;
224         mod->sml_next = NULL;
225         mod->sml_desc = NULL;
226         ber_str2bv( "subtreeSpecification",
227                         strlen("subtreeSpecification"), 1, &mod->sml_type );
228         mod->sml_bvalues = ssbva;
229         mod->sml_nvalues = NULL;
230         *modtail = mod;
231         modtail = &mod->sml_next;
232
233         rc = slap_mods_check( modlist, 1, &text, txtbuf, textlen, NULL );
234
235         if ( rc != LDAP_SUCCESS ) {
236 #ifdef NEW_LOGGING
237                 LDAP_LOG( OPERATION, ERR,
238                                 "create_context_csn_entry: mods check (%s)\n", text, 0, 0 );
239 #else
240                 Debug( LDAP_DEBUG_ANY, "create_context_csn_entry: mods check (%s)\n",
241                          text, 0, 0 );
242 #endif
243         }
244
245         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
246
247         build_new_dn( &sub_bv, &be->be_nsuffix[0], &psubrdn );
248         dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, NULL );
249         ch_free( sub_bv.bv_val );
250         ch_free( psubrdn.bv_val );
251
252         e->e_attrs = NULL;
253
254         rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
255
256         if( rc != LDAP_SUCCESS ) {
257 #ifdef NEW_LOGGING
258                 LDAP_LOG( OPERATION, ERR,
259                                 "create_context_csn_entry: mods2entry (%s)\n", text, 0, 0 );
260 #else
261                 Debug( LDAP_DEBUG_ANY, "create_context_csn_entry: mods2entry (%s)\n",
262                          text, 0, 0 );
263 #endif
264         }
265
266         return e;
267 }
268
269 static int
270 slap_contextcsn_callback(
271         Operation* op,
272         SlapReply* rs
273 )
274 {
275         if ( rs->sr_type != REP_SEARCH ) {
276                 *((int*)op->o_callback->sc_private) = 0;
277         } else {
278                 *((int*)op->o_callback->sc_private) = 1;
279         }
280         return LDAP_SUCCESS;
281 }
282
283 int
284 slap_get_csn(
285         Operation *op,
286         const char *csnbuf,
287         int     len,
288         struct berval *csn,
289         int manage_ctxcsn
290 )
291 {
292         struct  slap_csn_entry *pending;
293
294         if ( manage_ctxcsn ) {
295                 pending = (struct slap_csn_entry *) ch_calloc( 1, sizeof( struct slap_csn_entry ));
296         }
297
298         if ( csn == NULL )
299                 return LDAP_OTHER;
300
301         csn->bv_len = lutil_csnstr( csnbuf, len, 0, 0 );
302         csn->bv_val = csnbuf;
303
304         if ( manage_ctxcsn ) {
305                 ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
306                 pending->csn = ber_dupbv( NULL, csn );
307                 pending->connid = op->o_connid;
308                 pending->opid = op->o_opid;
309                 pending->state = SLAP_CSN_PENDING;
310                 LDAP_TAILQ_INSERT_TAIL( &op->o_bd->be_pending_csn_list, pending, csn_link );
311                 ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
312         }
313
314         return LDAP_SUCCESS;
315 }