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(
48 struct berval *cookie)
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 );
61 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
63 ctrls[num_ctrls] = sl_malloc ( sizeof ( LDAPControl ), op->o_tmpmemctx );
65 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
66 AttributeDescription *desc = a->a_desc;
67 if ( desc == slap_schema.si_ad_entryUUID ) {
68 ber_dupbv( &entryuuid_bv, &a->a_nvals[0] );
72 if ( send_cookie && cookie ) {
73 ber_printf( ber, "{eOON}",
74 entry_sync_state, &entryuuid_bv, cookie );
76 ber_printf( ber, "{eON}",
77 entry_sync_state, &entryuuid_bv );
80 ch_free( entryuuid_bv.bv_val );
81 entryuuid_bv.bv_val = NULL;
83 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
84 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
85 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
91 LDAP_LOG ( OPERATION, RESULTS,
92 "slap_build_sync_ctrl: ber_flatten2 failed\n",
95 Debug( LDAP_DEBUG_TRACE,
96 "slap_build_sync_ctrl: ber_flatten2 failed\n",
99 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
107 slap_build_sync_done_ctrl(
113 struct berval *cookie,
117 BerElementBuffer berbuf;
118 BerElement *ber = (BerElement *)&berbuf;
120 ber_init2( ber, NULL, LBER_USE_DER );
121 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
123 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
125 ber_printf( ber, "{" );
126 if ( send_cookie && cookie ) {
127 ber_printf( ber, "O", cookie );
129 if ( refreshDeletes == LDAP_SYNC_REFRESH_DELETES ) {
130 ber_printf( ber, "b", refreshDeletes );
132 ber_printf( ber, "N}" );
134 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_DONE;
135 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
136 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
142 LDAP_LOG ( OPERATION, RESULTS,
143 "slap_build_sync_done_ctrl: ber_flatten2 failed\n",
146 Debug( LDAP_DEBUG_TRACE,
147 "slap_build_sync_done_ctrl: ber_flatten2 failed\n",
150 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
159 slap_build_sync_state_ctrl_from_slog(
162 struct slog_entry *slog_e,
163 int entry_sync_state,
167 struct berval *cookie)
172 const char *text = NULL;
174 BerElementBuffer berbuf;
175 BerElement *ber = (BerElement *)&berbuf;
177 struct berval entryuuid_bv = { 0, NULL };
179 ber_init2( ber, NULL, LBER_USE_DER );
180 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
182 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
184 ber_dupbv( &entryuuid_bv, &slog_e->sl_uuid );
186 if ( send_cookie && cookie ) {
187 ber_printf( ber, "{eOON}",
188 entry_sync_state, &entryuuid_bv, cookie );
190 ber_printf( ber, "{eON}",
191 entry_sync_state, &entryuuid_bv );
194 ch_free( entryuuid_bv.bv_val );
195 entryuuid_bv.bv_val = NULL;
197 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
198 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
199 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
205 LDAP_LOG ( OPERATION, RESULTS,
206 "slap_build_sync_ctrl: ber_flatten2 failed\n",
209 Debug( LDAP_DEBUG_TRACE,
210 "slap_build_sync_ctrl: ber_flatten2 failed\n",
213 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
225 struct berval *cookie,
230 BerElementBuffer berbuf;
231 BerElement *ber = (BerElement *)&berbuf;
232 struct berval rspdata;
236 ber_init2( ber, NULL, LBER_USE_DER );
237 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
241 case LDAP_TAG_SYNC_NEW_COOKIE:
242 ber_printf( ber, "tO", type, cookie );
244 case LDAP_TAG_SYNC_REFRESH_DELETE:
245 case LDAP_TAG_SYNC_REFRESH_PRESENT:
246 ber_printf( ber, "t{", type );
248 ber_printf( ber, "O", cookie );
250 if ( refreshDone == 0 ) {
251 ber_printf( ber, "b", refreshDone );
253 ber_printf( ber, "N}" );
255 case LDAP_TAG_SYNC_ID_SET:
256 ber_printf( ber, "t{", type );
258 ber_printf( ber, "O", cookie );
260 if ( refreshDeletes == 1 ) {
261 ber_printf( ber, "b", refreshDeletes );
263 ber_printf( ber, "[W]", syncUUIDs );
264 ber_printf( ber, "N}" );
268 LDAP_LOG ( OPERATION, RESULTS,
269 "slap_send_syncinfo: invalid syncinfo type (%d)\n",
272 Debug( LDAP_DEBUG_TRACE,
273 "slap_send_syncinfo: invalid syncinfo type (%d)\n",
280 ret = ber_flatten2( ber, &rspdata, 0 );
284 LDAP_LOG ( OPERATION, RESULTS,
285 "slap_send_syncinfo: ber_flatten2 failed\n",
288 Debug( LDAP_DEBUG_TRACE,
289 "slap_send_syncinfo: ber_flatten2 failed\n",
292 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
296 rs->sr_rspdata = &rspdata;
297 send_ldap_intermediate( op, rs );
298 rs->sr_rspdata = NULL;
305 slap_compose_sync_cookie(
307 struct berval *cookie,
311 char cookiestr[ LDAP_LUTIL_CSNSTR_BUFSIZE + 10 ];
313 if ( csn->bv_val == NULL ) {
317 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 10,
320 } else if ( sid == -1 ) {
321 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 10,
322 "csn=%s", csn->bv_val );
324 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 10,
325 "csn=%s,sid=%03d", csn->bv_val, sid );
327 ber_str2bv( cookiestr, strlen(cookiestr), 1, cookie );
331 slap_sync_cookie_free(
332 struct sync_cookie *cookie,
336 if ( cookie == NULL )
339 if ( cookie->ctxcsn ) {
340 ber_bvarray_free( cookie->ctxcsn );
341 cookie->ctxcsn = NULL;
344 if ( cookie->octet_str ) {
345 ber_bvarray_free( cookie->octet_str );
346 cookie->octet_str = NULL;
357 slap_parse_sync_cookie(
358 struct sync_cookie *cookie
367 struct berval *ctxcsn;
369 if ( cookie == NULL )
372 if (( csn_ptr = strstr( cookie->octet_str[0].bv_val, "csn=" )) != NULL ) {
373 csn_str = (char *) SLAP_STRNDUP( csn_ptr, LDAP_LUTIL_CSNSTR_BUFSIZE );
374 if ( cval = strchr( csn_str, ',' )) {
376 csn_str_len = cval - csn_str - (sizeof("csn=") - 1);
378 csn_str_len = cookie->octet_str[0].bv_len -
379 (csn_ptr - cookie->octet_str[0].bv_val) -
380 (sizeof("csn=") - 1);
382 ctxcsn = ber_str2bv( csn_str + (sizeof("csn=")-1),
383 csn_str_len, 1, NULL );
385 ber_bvarray_add( &cookie->ctxcsn, ctxcsn );
388 cookie->ctxcsn = NULL;
391 if (( sid_ptr = strstr( cookie->octet_str->bv_val, "sid=" )) != NULL ) {
392 sid_str = (char *) SLAP_STRNDUP( sid_ptr,
393 SLAP_SYNC_SID_SIZE + sizeof("sid=") - 1 );
394 if ( cval = strchr( sid_str, ',' )) {
397 cookie->sid = atoi( sid_str + sizeof("sid=") - 1 );
405 slap_init_sync_cookie_ctxcsn(
406 struct sync_cookie *cookie
409 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE + 4 ];
410 struct berval octet_str = { 0, NULL };
411 struct berval ctxcsn = { 0, NULL };
412 struct berval ctxcsn_dup = { 0, NULL };
413 struct berval slap_syncCookie;
415 if ( cookie == NULL )
418 octet_str.bv_len = snprintf( csnbuf, LDAP_LUTIL_CSNSTR_BUFSIZE + 4,
419 "csn=%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
420 1900, 1, 1, 0, 0, 0, 0, 0, 0 );
421 octet_str.bv_val = csnbuf;
422 build_new_dn( &slap_syncCookie, &cookie->octet_str[0], &octet_str, NULL );
423 ber_bvarray_free( cookie->octet_str );
424 cookie->octet_str = NULL;
425 ber_bvarray_add( &cookie->octet_str, &slap_syncCookie );
427 ber_dupbv( &ctxcsn, &octet_str );
430 ber_dupbv( &ctxcsn_dup, &ctxcsn );
431 ch_free( ctxcsn.bv_val );
432 ber_bvarray_add( &cookie->ctxcsn, &ctxcsn_dup );
438 slap_dup_sync_cookie(
439 struct sync_cookie *dst,
440 struct sync_cookie *src
444 struct sync_cookie *new;
445 struct berval tmp_bv;
451 ber_bvarray_free( dst->ctxcsn );
452 ber_bvarray_free( dst->octet_str );
455 new = ( struct sync_cookie * )
456 ch_calloc( 1, sizeof( struct sync_cookie ));
462 for ( i=0; src->ctxcsn[i].bv_val; i++ ) {
463 ber_dupbv( &tmp_bv, &src->ctxcsn[i] );
464 ber_bvarray_add( &new->ctxcsn, &tmp_bv );
468 if ( src->octet_str ) {
469 for ( i=0; src->octet_str[i].bv_val; i++ ) {
470 ber_dupbv( &tmp_bv, &src->octet_str[i] );
471 ber_bvarray_add( &new->octet_str, &tmp_bv );
479 slap_build_syncUUID_set(
488 struct berval entryuuid_bv = { 0, NULL };
490 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
491 AttributeDescription *desc = a->a_desc;
492 if ( desc == slap_schema.si_ad_entryUUID ) {
493 ber_dupbv_x( &entryuuid_bv, &a->a_nvals[0], op->o_tmpmemctx );
497 ret = ber_bvarray_add_x( set, &entryuuid_bv, op->o_tmpmemctx );