]> git.sur5r.net Git - openldap/blob - servers/slapd/ctxcsn.c
c74c14f0961b4ebf3dcbf482892065f5a2cb5393
[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
161         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
162         mod->sml_op = LDAP_MOD_REPLACE;
163         ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
164         mod->sml_bvalues = ocbva;
165         *modtail = mod;
166         modtail = &mod->sml_next;
167
168         ber_str2bv( "subentry",
169                         strlen("subentry"), 1, &socbva[0] );
170
171         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
172         mod->sml_op = LDAP_MOD_REPLACE;
173         ber_str2bv( "structuralObjectClass", strlen("structuralObjectClass"), 1, &mod->sml_type );
174         mod->sml_bvalues = socbva;
175         *modtail = mod;
176         modtail = &mod->sml_next;
177
178         sprintf( substr, "ldapsync" );
179         sprintf( rdnstr, "cn=%s", substr );
180         ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
181         ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
182         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
183         mod->sml_op = LDAP_MOD_REPLACE;
184         ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
185         mod->sml_bvalues = cnbva;
186         *modtail = mod;
187         modtail = &mod->sml_next;
188
189         if ( context_csn ) {
190                 ber_dupbv( &scbva[0], context_csn );
191                 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
192                 mod->sml_op = LDAP_MOD_REPLACE;
193                 ber_str2bv( "contextCSN", strlen("contextCSN"), 1, &mod->sml_type );
194                 mod->sml_bvalues = scbva;
195                 *modtail = mod;
196                 modtail = &mod->sml_next;
197         }
198
199         ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
200         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
201         mod->sml_op = LDAP_MOD_REPLACE;
202         ber_str2bv( "subtreeSpecification",
203                         strlen("subtreeSpecification"), 1, &mod->sml_type );
204         mod->sml_bvalues = ssbva;
205         *modtail = mod;
206         modtail = &mod->sml_next;
207
208         rc = slap_mods_check( modlist, 1, &text, txtbuf, textlen, NULL );
209
210         if ( rc != LDAP_SUCCESS ) {
211 #ifdef NEW_LOGGING
212                 LDAP_LOG( OPERATION, ERR,
213                                 "create_context_csn_entry: mods check (%s)\n", text, 0, 0 );
214 #else
215                 Debug( LDAP_DEBUG_ANY, "create_context_csn_entry: mods check (%s)\n",
216                          text, 0, 0 );
217 #endif
218         }
219
220         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
221
222         build_new_dn( &sub_bv, &be->be_nsuffix[0], &psubrdn );
223         dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, NULL );
224         ch_free( sub_bv.bv_val );
225         ch_free( psubrdn.bv_val );
226
227         e->e_attrs = NULL;
228
229         rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
230
231         if( rc != LDAP_SUCCESS ) {
232 #ifdef NEW_LOGGING
233                 LDAP_LOG( OPERATION, ERR,
234                                 "create_context_csn_entry: mods2entry (%s)\n", text, 0, 0 );
235 #else
236                 Debug( LDAP_DEBUG_ANY, "create_context_csn_entry: mods2entry (%s)\n",
237                          text, 0, 0 );
238 #endif
239         }
240
241         for ( ml = modlist; ml != NULL; ml = mlnext ) {
242                 mlnext = ml->sml_next;
243                 free( ml );
244         }
245
246         return e;
247 }
248
249 static int
250 slap_contextcsn_callback(
251         Operation* op,
252         SlapReply* rs
253 )
254 {
255         if ( rs->sr_type != REP_SEARCH ) {
256                 *((int*)op->o_callback->sc_private) = 0;
257         } else {
258                 *((int*)op->o_callback->sc_private) = 1;
259         }
260         return LDAP_SUCCESS;
261 }
262
263 int
264 slap_get_csn(
265         Operation *op,
266         char *csnbuf,
267         int     len,
268         struct berval *csn,
269         int manage_ctxcsn
270 )
271 {
272         struct  slap_csn_entry *pending;
273
274         if ( manage_ctxcsn ) {
275                 pending = (struct slap_csn_entry *) ch_calloc( 1, sizeof( struct slap_csn_entry ));
276         }
277
278         if ( csn == NULL )
279                 return LDAP_OTHER;
280
281         csn->bv_len = lutil_csnstr( csnbuf, len, 0, 0 );
282         csn->bv_val = csnbuf;
283
284         if ( manage_ctxcsn ) {
285                 ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
286                 pending->csn = ber_dupbv( NULL, csn );
287                 pending->connid = op->o_connid;
288                 pending->opid = op->o_opid;
289                 pending->state = SLAP_CSN_PENDING;
290                 LDAP_TAILQ_INSERT_TAIL( &op->o_bd->be_pending_csn_list, pending, csn_link );
291                 ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
292         }
293
294         return LDAP_SUCCESS;
295 }