3 * LDAP Content Sync Routines
6 * Copyright 2003 The OpenLDAP Foundation, All Rights Reserved.
7 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
9 /* Copyright (c) 2003 by International Business Machines, Inc.
11 * International Business Machines, Inc. (hereinafter called IBM) grants
12 * permission under its copyrights to use, copy, modify, and distribute this
13 * Software with or without fee, provided that the above copyright notice and
14 * all paragraphs of this notice appear in all copies, and that the name of IBM
15 * not be used in connection with the marketing of any product incorporating
16 * the Software or modifications thereof, without specific, written prior
19 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
20 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
22 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
23 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
24 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
31 #include <ac/string.h>
32 #include <ac/socket.h>
37 #include "lutil_ldap.h"
40 slap_build_sync_state_ctrl(
53 const char *text = NULL;
55 BerElementBuffer berbuf;
56 BerElement *ber = (BerElement *)&berbuf;
58 struct berval entryuuid_bv = { 0, NULL };
60 ber_init2( ber, 0, LBER_USE_DER );
62 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
64 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
65 AttributeDescription *desc = a->a_desc;
66 if ( desc == slap_schema.si_ad_entryUUID ) {
67 ber_dupbv( &entryuuid_bv, &a->a_vals[0] );
71 if ( send_cookie && csn ) {
72 ber_printf( ber, "{eOON}",
73 entry_sync_state, &entryuuid_bv, csn );
75 ber_printf( ber, "{eON}",
76 entry_sync_state, &entryuuid_bv );
79 ch_free( entryuuid_bv.bv_val );
80 entryuuid_bv.bv_val = NULL;
82 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
83 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
84 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
90 LDAP_LOG ( OPERATION, RESULTS,
91 "slap_build_sync_ctrl: ber_flatten2 failed\n",
94 Debug( LDAP_DEBUG_TRACE,
95 "slap_build_sync_ctrl: ber_flatten2 failed\n",
98 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
106 slap_build_sync_done_ctrl(
115 BerElementBuffer berbuf;
116 BerElement *ber = (BerElement *)&berbuf;
118 ber_init2( ber, NULL, LBER_USE_DER );
120 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
122 if ( send_cookie && csn ) {
123 ber_printf( ber, "{ON}", csn );
125 ber_printf( ber, "{N}" );
128 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_DONE;
129 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
130 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
136 LDAP_LOG ( OPERATION, RESULTS,
137 "slap_build_sync_done_ctrl: ber_flatten2 failed\n",
140 Debug( LDAP_DEBUG_TRACE,
141 "slap_build_sync_done_ctrl: ber_flatten2 failed\n",
144 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
153 slap_build_sync_state_ctrl_from_slog(
156 struct slog_entry *slog_e,
157 int entry_sync_state,
166 const char *text = NULL;
168 BerElementBuffer berbuf;
169 BerElement *ber = (BerElement *)&berbuf;
171 struct berval entryuuid_bv = { 0, NULL };
173 ber_init2( ber, 0, LBER_USE_DER );
175 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
177 ber_dupbv( &entryuuid_bv, &slog_e->sl_uuid );
179 if ( send_cookie && csn ) {
180 ber_printf( ber, "{eOON}",
181 entry_sync_state, &entryuuid_bv, csn );
183 ber_printf( ber, "{eON}",
184 entry_sync_state, &entryuuid_bv );
187 ch_free( entryuuid_bv.bv_val );
188 entryuuid_bv.bv_val = NULL;
190 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
191 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
192 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
198 LDAP_LOG ( OPERATION, RESULTS,
199 "slap_build_sync_ctrl: ber_flatten2 failed\n",
202 Debug( LDAP_DEBUG_TRACE,
203 "slap_build_sync_ctrl: ber_flatten2 failed\n",
206 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
214 slap_compose_sync_cookie(
216 struct berval *cookie,
220 char cookiestr[ LDAP_LUTIL_CSNSTR_BUFSIZE + 10 ];
222 if ( csn->bv_val == NULL ) {
226 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 10,
229 } else if ( sid == -1 ) {
230 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 10,
231 "csn=%s", csn->bv_val );
233 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 10,
234 "csn=%s,sid=%03d", csn->bv_val, sid );
236 ber_str2bv( cookiestr, strlen(cookiestr), 1, cookie );
240 slap_sync_cookie_free(
241 struct sync_cookie *cookie,
245 if ( cookie == NULL )
248 if ( cookie->ctxcsn ) {
249 ber_bvarray_free( cookie->ctxcsn );
250 cookie->ctxcsn = NULL;
253 if ( cookie->octet_str ) {
254 ber_bvarray_free( cookie->octet_str );
255 cookie->octet_str = NULL;
266 slap_parse_sync_cookie(
267 struct sync_cookie *cookie
275 struct berval *ctxcsn;
277 if ( cookie == NULL )
280 if (( csn_ptr = strstr( cookie->octet_str[0].bv_val, "csn=" )) != NULL ) {
281 csn_str = (char *) strndup( csn_ptr, LDAP_LUTIL_CSNSTR_BUFSIZE );
282 if ( cval = strchr( csn_str, ',' )) {
285 ctxcsn = ber_str2bv( csn_str + 4, strlen(csn_str) - 4, 1, NULL );
287 ber_bvarray_add( &cookie->ctxcsn, ctxcsn );
290 cookie->ctxcsn = NULL;
293 if (( sid_ptr = strstr( cookie->octet_str->bv_val, "sid=" )) != NULL ) {
294 sid_str = (char *) strndup( sid_ptr, 7 );
295 if ( cval = strchr( sid_str, ',' )) {
298 cookie->sid = atoi( sid_str+4 );
306 slap_init_sync_cookie_ctxcsn(
307 struct sync_cookie *cookie
310 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE + 4 ];
311 struct berval octet_str = { 0, NULL };
312 struct berval ctxcsn = { 0, NULL };
313 struct berval ctxcsn_dup = { 0, NULL };
314 struct berval slap_syncCookie;
316 if ( cookie == NULL )
319 octet_str.bv_len = snprintf( csnbuf, LDAP_LUTIL_CSNSTR_BUFSIZE + 4,
320 "csn=%4d%02d%02d%02d:%02d:%02dZ#0x%04x#%d#%04x",
321 1900, 1, 1, 0, 0, 0, 0, 0, 0 );
322 octet_str.bv_val = csnbuf;
323 build_new_dn( &slap_syncCookie, &cookie->octet_str[0], &octet_str, NULL );
324 ber_bvarray_free( cookie->octet_str );
325 cookie->octet_str = NULL;
326 ber_bvarray_add( &cookie->octet_str, &slap_syncCookie );
328 ber_dupbv( &ctxcsn, &octet_str );
331 ber_dupbv( &ctxcsn_dup, &ctxcsn );
332 ch_free( ctxcsn.bv_val );
333 ber_bvarray_add( &cookie->ctxcsn, &ctxcsn_dup );
339 slap_dup_sync_cookie(
340 struct sync_cookie *dst,
341 struct sync_cookie *src
345 struct sync_cookie *new;
346 struct berval tmp_bv;
352 ber_bvarray_free( dst->ctxcsn );
353 ber_bvarray_free( dst->octet_str );
356 new = ( struct sync_cookie * )
357 ch_calloc( 1, sizeof( struct sync_cookie ));
363 for ( i=0; src->ctxcsn[i].bv_val; i++ ) {
364 ber_dupbv( &tmp_bv, &src->ctxcsn[i] );
365 ber_bvarray_add( &new->ctxcsn, &tmp_bv );
369 if ( src->octet_str ) {
370 for ( i=0; src->octet_str[i].bv_val; i++ ) {
371 ber_dupbv( &tmp_bv, &src->octet_str[i] );
372 ber_bvarray_add( &new->octet_str, &tmp_bv );