1 /* ldapsync.c -- LDAP Content Sync Routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2004 The OpenLDAP Foundation.
6 * Portions Copyright 2003 IBM Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
22 #include <ac/string.h>
23 #include <ac/socket.h>
28 #include "../../libraries/liblber/lber-int.h" /* get ber_strndup() */
29 #include "lutil_ldap.h"
32 struct sync_cookie *slap_sync_cookie = NULL;
34 struct slap_sync_cookie_s slap_sync_cookie =
35 LDAP_STAILQ_HEAD_INITIALIZER( slap_sync_cookie );
39 slap_build_sync_state_ctrl(
47 struct berval *cookie)
52 const char *text = NULL;
54 BerElementBuffer berbuf;
55 BerElement *ber = (BerElement *)&berbuf;
57 struct berval entryuuid_bv = { 0, NULL };
59 ber_init2( ber, 0, LBER_USE_DER );
60 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
62 ctrls[num_ctrls] = sl_malloc ( sizeof ( LDAPControl ), op->o_tmpmemctx );
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_nvals[0] );
71 if ( send_cookie && cookie ) {
72 ber_printf( ber, "{eOON}",
73 entry_sync_state, &entryuuid_bv, cookie );
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(
112 struct berval *cookie,
116 BerElementBuffer berbuf;
117 BerElement *ber = (BerElement *)&berbuf;
119 ber_init2( ber, NULL, LBER_USE_DER );
120 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
122 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
124 ber_printf( ber, "{" );
125 if ( send_cookie && cookie ) {
126 ber_printf( ber, "O", cookie );
128 if ( refreshDeletes == LDAP_SYNC_REFRESH_DELETES ) {
129 ber_printf( ber, "b", refreshDeletes );
131 ber_printf( ber, "N}" );
133 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_DONE;
134 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
135 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
141 LDAP_LOG ( OPERATION, RESULTS,
142 "slap_build_sync_done_ctrl: ber_flatten2 failed\n",
145 Debug( LDAP_DEBUG_TRACE,
146 "slap_build_sync_done_ctrl: ber_flatten2 failed\n",
149 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
158 slap_build_sync_state_ctrl_from_slog(
161 struct slog_entry *slog_e,
162 int entry_sync_state,
166 struct berval *cookie)
171 const char *text = NULL;
173 BerElementBuffer berbuf;
174 BerElement *ber = (BerElement *)&berbuf;
176 struct berval entryuuid_bv = { 0, NULL };
178 ber_init2( ber, NULL, LBER_USE_DER );
179 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
181 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
183 ber_dupbv( &entryuuid_bv, &slog_e->sl_uuid );
185 if ( send_cookie && cookie ) {
186 ber_printf( ber, "{eOON}",
187 entry_sync_state, &entryuuid_bv, cookie );
189 ber_printf( ber, "{eON}",
190 entry_sync_state, &entryuuid_bv );
193 ch_free( entryuuid_bv.bv_val );
194 entryuuid_bv.bv_val = NULL;
196 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
197 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
198 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
204 LDAP_LOG ( OPERATION, RESULTS,
205 "slap_build_sync_ctrl: ber_flatten2 failed\n",
208 Debug( LDAP_DEBUG_TRACE,
209 "slap_build_sync_ctrl: ber_flatten2 failed\n",
212 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
224 struct berval *cookie,
229 BerElementBuffer berbuf;
230 BerElement *ber = (BerElement *)&berbuf;
231 struct berval rspdata;
235 ber_init2( ber, NULL, LBER_USE_DER );
236 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
240 case LDAP_TAG_SYNC_NEW_COOKIE:
241 ber_printf( ber, "tO", type, cookie );
243 case LDAP_TAG_SYNC_REFRESH_DELETE:
244 case LDAP_TAG_SYNC_REFRESH_PRESENT:
245 ber_printf( ber, "t{", type );
247 ber_printf( ber, "O", cookie );
249 if ( refreshDone == 0 ) {
250 ber_printf( ber, "b", refreshDone );
252 ber_printf( ber, "N}" );
254 case LDAP_TAG_SYNC_ID_SET:
255 ber_printf( ber, "t{", type );
257 ber_printf( ber, "O", cookie );
259 if ( refreshDeletes == 1 ) {
260 ber_printf( ber, "b", refreshDeletes );
262 ber_printf( ber, "[W]", syncUUIDs );
263 ber_printf( ber, "N}" );
267 LDAP_LOG ( OPERATION, RESULTS,
268 "slap_send_syncinfo: invalid syncinfo type (%d)\n",
271 Debug( LDAP_DEBUG_TRACE,
272 "slap_send_syncinfo: invalid syncinfo type (%d)\n",
279 ret = ber_flatten2( ber, &rspdata, 0 );
283 LDAP_LOG ( OPERATION, RESULTS,
284 "slap_send_syncinfo: ber_flatten2 failed\n",
287 Debug( LDAP_DEBUG_TRACE,
288 "slap_send_syncinfo: ber_flatten2 failed\n",
291 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
295 rs->sr_rspdata = &rspdata;
296 send_ldap_intermediate( op, rs );
297 rs->sr_rspdata = NULL;
304 slap_compose_sync_cookie(
306 struct berval *cookie,
311 char cookiestr[ LDAP_LUTIL_CSNSTR_BUFSIZE + 20 ];
313 if ( csn->bv_val == NULL ) {
318 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
323 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
326 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
327 "sid=%03d,rid=%03d", sid, rid );
333 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
334 "csn=%s", csn->bv_val );
336 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
337 "csn=%s,rid=%03d", csn->bv_val, rid );
341 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
342 "csn=%s,sid=%03d", csn->bv_val, sid );
344 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
345 "csn=%s,sid=%03d,rid=%03d", csn->bv_val, sid, rid );
349 ber_str2bv( cookiestr, strlen(cookiestr), 1, cookie );
353 slap_sync_cookie_free(
354 struct sync_cookie *cookie,
358 if ( cookie == NULL )
361 if ( cookie->ctxcsn ) {
362 ber_bvarray_free( cookie->ctxcsn );
363 cookie->ctxcsn = NULL;
366 if ( cookie->octet_str ) {
367 ber_bvarray_free( cookie->octet_str );
368 cookie->octet_str = NULL;
379 slap_parse_sync_cookie(
380 struct sync_cookie *cookie
391 struct berval *ctxcsn;
393 if ( cookie == NULL )
396 if (( csn_ptr = strstr( cookie->octet_str[0].bv_val, "csn=" )) != NULL ) {
397 csn_str = SLAP_STRNDUP( csn_ptr, LDAP_LUTIL_CSNSTR_BUFSIZE );
398 if ( (cval = strchr( csn_str, ',' )) != NULL ) {
400 csn_str_len = cval - csn_str - (sizeof("csn=") - 1);
402 csn_str_len = cookie->octet_str[0].bv_len -
403 (csn_ptr - cookie->octet_str[0].bv_val) -
404 (sizeof("csn=") - 1);
406 ctxcsn = ber_str2bv( csn_str + (sizeof("csn=")-1),
407 csn_str_len, 1, NULL );
409 ber_bvarray_add( &cookie->ctxcsn, ctxcsn );
412 cookie->ctxcsn = NULL;
415 if (( sid_ptr = strstr( cookie->octet_str->bv_val, "sid=" )) != NULL ) {
416 sid_str = SLAP_STRNDUP( sid_ptr,
417 SLAP_SYNC_SID_SIZE + sizeof("sid=") - 1 );
418 if ( (cval = strchr( sid_str, ',' )) != NULL ) {
421 cookie->sid = atoi( sid_str + sizeof("sid=") - 1 );
427 if (( rid_ptr = strstr( cookie->octet_str->bv_val, "rid=" )) != NULL ) {
428 rid_str = SLAP_STRNDUP( rid_ptr,
429 SLAP_SYNC_RID_SIZE + sizeof("rid=") - 1 );
430 if ( (cval = strchr( rid_str, ',' )) != NULL ) {
433 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 );