]> git.sur5r.net Git - openldap/blob - servers/slapd/ctxcsn.c
misc cleanup
[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 #ifdef LDAP_SYNC
37
38 struct berval *
39 slap_get_commit_csn( Operation *op )
40 {
41         struct berval *max_committed_csn = NULL;
42         struct slap_csn_entry *csne = NULL, *committed_csne = NULL;
43         int i = 0;
44
45         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
46
47         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
48                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
49                         break;
50         }
51
52         if ( csne ) {
53                 csne->state = SLAP_CSN_COMMIT;
54         }
55
56         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
57                 if ( csne->state == SLAP_CSN_COMMIT )
58                         committed_csne = csne;
59                 if ( csne->state == SLAP_CSN_PENDING )
60                         break;
61         }
62
63         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
64
65         if ( committed_csne ) {
66                 max_committed_csn = ber_dupbv( NULL, committed_csne->csn );
67         }
68
69         return max_committed_csn;
70 }
71
72 void
73 slap_rewind_commit_csn( Operation *op )
74 {
75         struct slap_csn_entry *csne = NULL;
76
77         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
78
79         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
80                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
81                         break;
82         }
83
84         if ( csne ) {
85                 csne->state = SLAP_CSN_PENDING;
86         }
87         
88         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
89 }
90
91 void
92 slap_graduate_commit_csn( Operation *op )
93 {
94         struct slap_csn_entry *csne = NULL;
95
96         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
97
98         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
99                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
100                         break;
101         }
102
103         if ( csne ) {
104                 LDAP_TAILQ_REMOVE( &op->o_bd->be_pending_csn_list, csne, csn_link );
105                 ch_free( csne->csn->bv_val );
106                 ch_free( csne->csn );
107                 ch_free( csne );
108         }
109
110         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
111
112         return;
113 }
114
115 Entry *
116 slap_create_context_csn_entry(
117         Backend *be,
118         struct berval *context_csn
119 )
120 {
121         Modifications *ml;
122         Modifications *mlnext;
123         Modifications *mod;
124         Modifications *modlist;
125         Modifications **modtail = &modlist;
126
127         struct berval* ocbva = NULL;
128         struct berval* socbva = NULL;
129         struct berval* cnbva = NULL;
130         struct berval* ssbva = NULL;
131         struct berval* scbva = NULL;
132
133         char substr[64];
134         char rdnstr[67];
135         const char      *text;
136         char txtbuf[SLAP_TEXT_BUFLEN];
137         size_t textlen = sizeof txtbuf;
138
139         Entry* e;
140         int rc;
141
142         struct berval sub_bv = { 0, NULL };
143         struct berval psubrdn = { 0, NULL };
144         
145         slap_callback cb;
146         SlapReply       rs = {REP_RESULT};
147
148         struct berval rdn = { 0, NULL };
149         int match = 0;
150         char *def_filter_str = NULL;
151
152         ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
153         socbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
154         cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
155         ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
156         scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
157
158         ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
159         ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
160         ber_str2bv( "syncProviderSubentry",
161                         strlen("syncProviderSubentry"), 1, &ocbva[2] );
162         ocbva[3].bv_len = 0;
163         ocbva[3].bv_val = NULL;
164
165         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
166         mod->sml_op = LDAP_MOD_REPLACE;
167         mod->sml_next = NULL;
168         mod->sml_desc = NULL;
169         ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
170         mod->sml_bvalues = ocbva;
171         mod->sml_nvalues = ocbva;
172         *modtail = mod;
173         modtail = &mod->sml_next;
174
175         ber_str2bv( "syncProviderSubentry",
176                         strlen("syncProviderSubentry"), 1, &socbva[0] );
177         socbva[1].bv_len = 0;
178         socbva[1].bv_val = NULL;
179
180         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
181         mod->sml_op = LDAP_MOD_REPLACE;
182         mod->sml_next = NULL;
183         mod->sml_desc = NULL;
184         ber_str2bv( "structuralObjectClass", strlen("structuralObjectClass"), 1, &mod->sml_type );
185         mod->sml_bvalues = socbva;
186         mod->sml_nvalues = socbva;
187         *modtail = mod;
188         modtail = &mod->sml_next;
189
190         sprintf( substr, "ldapsync" );
191         sprintf( rdnstr, "cn=%s", substr );
192         ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
193         ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
194         cnbva[1].bv_len = 0;
195         cnbva[1].bv_val = NULL;
196         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
197         mod->sml_op = LDAP_MOD_REPLACE;
198         mod->sml_next = NULL;
199         mod->sml_desc = NULL;
200         ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
201         mod->sml_bvalues = cnbva;
202         mod->sml_nvalues = cnbva;
203         *modtail = mod;
204         modtail = &mod->sml_next;
205
206         if ( context_csn ) {
207                 ber_dupbv( &scbva[0], context_csn );
208                 scbva[1].bv_len = 0;
209                 scbva[1].bv_val = NULL;
210                 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
211                 mod->sml_op = LDAP_MOD_REPLACE;
212                 mod->sml_next = NULL;
213                 mod->sml_desc = NULL;
214                 ber_str2bv( "contextCSN", strlen("contextCSN"), 1, &mod->sml_type );
215                 mod->sml_bvalues = scbva;
216                 mod->sml_nvalues = scbva;
217                 *modtail = mod;
218                 modtail = &mod->sml_next;
219         }
220
221         ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
222         ssbva[1].bv_len = 0;
223         ssbva[1].bv_val = NULL;
224         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
225         mod->sml_op = LDAP_MOD_REPLACE;
226         mod->sml_next = NULL;
227         mod->sml_desc = NULL;
228         ber_str2bv( "subtreeSpecification",
229                         strlen("subtreeSpecification"), 1, &mod->sml_type );
230         mod->sml_bvalues = ssbva;
231         mod->sml_nvalues = ssbva;
232         *modtail = mod;
233         modtail = &mod->sml_next;
234
235         rc = slap_mods_check( modlist, 1, &text, txtbuf, textlen, NULL );
236
237         if ( rc != LDAP_SUCCESS ) {
238 #ifdef NEW_LOGGING
239                 LDAP_LOG( OPERATION, ERR,
240                                 "create_context_csn_entry: mods check (%s)\n", text, 0, 0 );
241 #else
242                 Debug( LDAP_DEBUG_ANY, "create_context_csn_entry: mods check (%s)\n",
243                          text, 0, 0 );
244 #endif
245         }
246
247         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
248
249         build_new_dn( &sub_bv, &be->be_nsuffix[0], &psubrdn );
250         dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, NULL );
251         ch_free( sub_bv.bv_val );
252         ch_free( psubrdn.bv_val );
253
254         e->e_attrs = NULL;
255
256         rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
257
258         if( rc != LDAP_SUCCESS ) {
259 #ifdef NEW_LOGGING
260                 LDAP_LOG( OPERATION, ERR,
261                                 "create_context_csn_entry: mods2entry (%s)\n", text, 0, 0 );
262 #else
263                 Debug( LDAP_DEBUG_ANY, "create_context_csn_entry: mods2entry (%s)\n",
264                          text, 0, 0 );
265 #endif
266         }
267
268         return e;
269 }
270
271 static int
272 slap_contextcsn_callback(
273         Operation* op,
274         SlapReply* rs
275 )
276 {
277         if ( rs->sr_type != REP_SEARCH ) {
278                 *((int*)op->o_callback->sc_private) = 0;
279         } else {
280                 *((int*)op->o_callback->sc_private) = 1;
281         }
282         return LDAP_SUCCESS;
283 }
284
285 int
286 slap_get_csn(
287         Operation *op,
288         const char *csnbuf,
289         int     len,
290         struct berval *csn,
291         int manage_ctxcsn
292 )
293 {
294         struct  slap_csn_entry *pending;
295
296         if ( manage_ctxcsn ) {
297                 pending = (struct slap_csn_entry *) ch_calloc( 1, sizeof( struct slap_csn_entry ));
298         }
299
300         if ( csn == NULL )
301                 return LDAP_OTHER;
302
303         csn->bv_len = lutil_csnstr( csnbuf, len, 0, 0 );
304         csn->bv_val = csnbuf;
305
306         if ( manage_ctxcsn ) {
307                 ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
308                 pending->csn = ber_dupbv( NULL, csn );
309                 pending->connid = op->o_connid;
310                 pending->opid = op->o_opid;
311                 pending->state = SLAP_CSN_PENDING;
312                 LDAP_TAILQ_INSERT_TAIL( &op->o_bd->be_pending_csn_list, pending, csn_link );
313                 ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
314         }
315
316         return LDAP_SUCCESS;
317 }
318 #endif