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,
312 char cookiestr[ LDAP_LUTIL_CSNSTR_BUFSIZE + 20 ];
314 if ( csn->bv_val == NULL ) {
319 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
324 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
327 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
328 "sid=%03d,rid=%03d", sid, rid );
334 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
335 "csn=%s", csn->bv_val );
337 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
338 "csn=%s,rid=%03d", csn->bv_val, rid );
342 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
343 "csn=%s,sid=%03d", csn->bv_val, sid );
345 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
346 "csn=%s,sid=%03d,rid=%03d", csn->bv_val, sid, rid );
350 ber_str2bv( cookiestr, strlen(cookiestr), 1, cookie );
354 slap_sync_cookie_free(
355 struct sync_cookie *cookie,
359 if ( cookie == NULL )
362 if ( cookie->ctxcsn ) {
363 ber_bvarray_free( cookie->ctxcsn );
364 cookie->ctxcsn = NULL;
367 if ( cookie->octet_str ) {
368 ber_bvarray_free( cookie->octet_str );
369 cookie->octet_str = NULL;
380 slap_parse_sync_cookie(
381 struct sync_cookie *cookie
392 struct berval *ctxcsn;
394 if ( cookie == NULL )
397 if (( csn_ptr = strstr( cookie->octet_str[0].bv_val, "csn=" )) != NULL ) {
398 csn_str = (char *) SLAP_STRNDUP( csn_ptr, LDAP_LUTIL_CSNSTR_BUFSIZE );
399 if ( cval = strchr( csn_str, ',' )) {
401 csn_str_len = cval - csn_str - (sizeof("csn=") - 1);
403 csn_str_len = cookie->octet_str[0].bv_len -
404 (csn_ptr - cookie->octet_str[0].bv_val) -
405 (sizeof("csn=") - 1);
407 ctxcsn = ber_str2bv( csn_str + (sizeof("csn=")-1),
408 csn_str_len, 1, NULL );
410 ber_bvarray_add( &cookie->ctxcsn, ctxcsn );
413 cookie->ctxcsn = NULL;
416 if (( sid_ptr = strstr( cookie->octet_str->bv_val, "sid=" )) != NULL ) {
417 sid_str = (char *) SLAP_STRNDUP( sid_ptr,
418 SLAP_SYNC_SID_SIZE + sizeof("sid=") - 1 );
419 if ( cval = strchr( sid_str, ',' )) {
422 cookie->sid = atoi( sid_str + sizeof("sid=") - 1 );
428 if (( rid_ptr = strstr( cookie->octet_str->bv_val, "rid=" )) != NULL ) {
429 rid_str = (char *) SLAP_STRNDUP( rid_ptr,
430 SLAP_SYNC_RID_SIZE + sizeof("rid=") - 1 );
431 if ( cval = strchr( rid_str, ',' )) {
434 cookie->rid = atoi( rid_str + sizeof("rid=") - 1 );
442 slap_init_sync_cookie_ctxcsn(
443 struct sync_cookie *cookie
446 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE + 4 ];
447 struct berval octet_str = { 0, NULL };
448 struct berval ctxcsn = { 0, NULL };
449 struct berval ctxcsn_dup = { 0, NULL };
450 struct berval slap_syncCookie;
452 if ( cookie == NULL )
455 octet_str.bv_len = snprintf( csnbuf, LDAP_LUTIL_CSNSTR_BUFSIZE + 4,
456 "csn=%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
457 1900, 1, 1, 0, 0, 0, 0, 0, 0 );
458 octet_str.bv_val = csnbuf;
459 build_new_dn( &slap_syncCookie, &cookie->octet_str[0], &octet_str, NULL );
460 ber_bvarray_free( cookie->octet_str );
461 cookie->octet_str = NULL;
462 ber_bvarray_add( &cookie->octet_str, &slap_syncCookie );
464 ber_dupbv( &ctxcsn, &octet_str );
467 ber_dupbv( &ctxcsn_dup, &ctxcsn );
468 ch_free( ctxcsn.bv_val );
469 ber_bvarray_add( &cookie->ctxcsn, &ctxcsn_dup );
475 slap_dup_sync_cookie(
476 struct sync_cookie *dst,
477 struct sync_cookie *src
481 struct sync_cookie *new;
482 struct berval tmp_bv;
488 ber_bvarray_free( dst->ctxcsn );
489 ber_bvarray_free( dst->octet_str );
492 new = ( struct sync_cookie * )
493 ch_calloc( 1, sizeof( struct sync_cookie ));
500 for ( i=0; src->ctxcsn[i].bv_val; i++ ) {
501 ber_dupbv( &tmp_bv, &src->ctxcsn[i] );
502 ber_bvarray_add( &new->ctxcsn, &tmp_bv );
506 if ( src->octet_str ) {
507 for ( i=0; src->octet_str[i].bv_val; i++ ) {
508 ber_dupbv( &tmp_bv, &src->octet_str[i] );
509 ber_bvarray_add( &new->octet_str, &tmp_bv );
517 slap_build_syncUUID_set(
526 struct berval entryuuid_bv = { 0, NULL };
528 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
529 AttributeDescription *desc = a->a_desc;
530 if ( desc == slap_schema.si_ad_entryUUID ) {
531 ber_dupbv_x( &entryuuid_bv, &a->a_nvals[0], op->o_tmpmemctx );
535 ret = ber_bvarray_add_x( set, &entryuuid_bv, op->o_tmpmemctx );