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 = BER_BVNULL;
59 ber_init2( ber, 0, LBER_USE_DER );
60 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
62 ctrls[num_ctrls] = slap_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 );
89 Debug( LDAP_DEBUG_TRACE,
90 "slap_build_sync_ctrl: ber_flatten2 failed\n",
92 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
100 slap_build_sync_done_ctrl(
106 struct berval *cookie,
110 BerElementBuffer berbuf;
111 BerElement *ber = (BerElement *)&berbuf;
113 ber_init2( ber, NULL, LBER_USE_DER );
114 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
116 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
118 ber_printf( ber, "{" );
119 if ( send_cookie && cookie ) {
120 ber_printf( ber, "O", cookie );
122 if ( refreshDeletes == LDAP_SYNC_REFRESH_DELETES ) {
123 ber_printf( ber, "b", refreshDeletes );
125 ber_printf( ber, "N}" );
127 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_DONE;
128 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
129 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
134 Debug( LDAP_DEBUG_TRACE,
135 "slap_build_sync_done_ctrl: ber_flatten2 failed\n",
137 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
146 slap_build_sync_state_ctrl_from_slog(
149 struct slog_entry *slog_e,
150 int entry_sync_state,
154 struct berval *cookie)
159 const char *text = NULL;
161 BerElementBuffer berbuf;
162 BerElement *ber = (BerElement *)&berbuf;
164 struct berval entryuuid_bv = BER_BVNULL;
166 ber_init2( ber, NULL, LBER_USE_DER );
167 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
169 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
171 ber_dupbv( &entryuuid_bv, &slog_e->sl_uuid );
173 if ( send_cookie && cookie ) {
174 ber_printf( ber, "{eOON}",
175 entry_sync_state, &entryuuid_bv, cookie );
177 ber_printf( ber, "{eON}",
178 entry_sync_state, &entryuuid_bv );
181 ch_free( entryuuid_bv.bv_val );
182 entryuuid_bv.bv_val = NULL;
184 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
185 ctrls[num_ctrls]->ldctl_iscritical = op->o_sync;
186 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
191 Debug( LDAP_DEBUG_TRACE,
192 "slap_build_sync_ctrl: ber_flatten2 failed\n",
194 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
206 struct berval *cookie,
211 BerElementBuffer berbuf;
212 BerElement *ber = (BerElement *)&berbuf;
213 struct berval rspdata;
217 ber_init2( ber, NULL, LBER_USE_DER );
218 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
222 case LDAP_TAG_SYNC_NEW_COOKIE:
223 ber_printf( ber, "tO", type, cookie );
225 case LDAP_TAG_SYNC_REFRESH_DELETE:
226 case LDAP_TAG_SYNC_REFRESH_PRESENT:
227 ber_printf( ber, "t{", type );
229 ber_printf( ber, "O", cookie );
231 if ( refreshDone == 0 ) {
232 ber_printf( ber, "b", refreshDone );
234 ber_printf( ber, "N}" );
236 case LDAP_TAG_SYNC_ID_SET:
237 ber_printf( ber, "t{", type );
239 ber_printf( ber, "O", cookie );
241 if ( refreshDeletes == 1 ) {
242 ber_printf( ber, "b", refreshDeletes );
244 ber_printf( ber, "[W]", syncUUIDs );
245 ber_printf( ber, "N}" );
248 Debug( LDAP_DEBUG_TRACE,
249 "slap_send_syncinfo: invalid syncinfo type (%d)\n",
255 ret = ber_flatten2( ber, &rspdata, 0 );
258 Debug( LDAP_DEBUG_TRACE,
259 "slap_send_syncinfo: ber_flatten2 failed\n",
261 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
265 rs->sr_rspdata = &rspdata;
266 send_ldap_intermediate( op, rs );
267 rs->sr_rspdata = NULL;
274 slap_compose_sync_cookie(
276 struct berval *cookie,
281 char cookiestr[ LDAP_LUTIL_CSNSTR_BUFSIZE + 20 ];
283 if ( csn->bv_val == NULL ) {
288 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
293 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
296 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
297 "sid=%03d,rid=%03d", sid, rid );
303 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
304 "csn=%s", csn->bv_val );
306 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
307 "csn=%s,rid=%03d", csn->bv_val, rid );
311 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
312 "csn=%s,sid=%03d", csn->bv_val, sid );
314 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
315 "csn=%s,sid=%03d,rid=%03d", csn->bv_val, sid, rid );
319 ber_str2bv( cookiestr, strlen(cookiestr), 1, cookie );
323 slap_sync_cookie_free(
324 struct sync_cookie *cookie,
328 if ( cookie == NULL )
331 if ( cookie->ctxcsn ) {
332 ber_bvarray_free( cookie->ctxcsn );
333 cookie->ctxcsn = NULL;
336 if ( cookie->octet_str ) {
337 ber_bvarray_free( cookie->octet_str );
338 cookie->octet_str = NULL;
349 slap_parse_sync_cookie(
350 struct sync_cookie *cookie
361 struct berval *ctxcsn;
363 if ( cookie == NULL )
366 if (( csn_ptr = strstr( cookie->octet_str[0].bv_val, "csn=" )) != NULL ) {
367 csn_str = SLAP_STRNDUP( csn_ptr, LDAP_LUTIL_CSNSTR_BUFSIZE );
368 if ( (cval = strchr( csn_str, ',' )) != NULL ) {
370 csn_str_len = cval - csn_str - (sizeof("csn=") - 1);
372 csn_str_len = cookie->octet_str[0].bv_len -
373 (csn_ptr - cookie->octet_str[0].bv_val) -
374 (sizeof("csn=") - 1);
376 ctxcsn = ber_str2bv( csn_str + (sizeof("csn=")-1),
377 csn_str_len, 1, NULL );
379 ber_bvarray_add( &cookie->ctxcsn, ctxcsn );
382 cookie->ctxcsn = NULL;
385 if (( sid_ptr = strstr( cookie->octet_str->bv_val, "sid=" )) != NULL ) {
386 sid_str = SLAP_STRNDUP( sid_ptr,
387 SLAP_SYNC_SID_SIZE + sizeof("sid=") - 1 );
388 if ( (cval = strchr( sid_str, ',' )) != NULL ) {
391 cookie->sid = atoi( sid_str + sizeof("sid=") - 1 );
397 if (( rid_ptr = strstr( cookie->octet_str->bv_val, "rid=" )) != NULL ) {
398 rid_str = SLAP_STRNDUP( rid_ptr,
399 SLAP_SYNC_RID_SIZE + sizeof("rid=") - 1 );
400 if ( (cval = strchr( rid_str, ',' )) != NULL ) {
403 cookie->rid = atoi( rid_str + sizeof("rid=") - 1 );
412 slap_init_sync_cookie_ctxcsn(
413 struct sync_cookie *cookie
416 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE + 4 ];
417 struct berval octet_str = BER_BVNULL;
418 struct berval ctxcsn = BER_BVNULL;
419 struct berval ctxcsn_dup = BER_BVNULL;
420 struct berval slap_syncCookie;
422 if ( cookie == NULL )
425 octet_str.bv_len = snprintf( csnbuf, LDAP_LUTIL_CSNSTR_BUFSIZE + 4,
426 "csn=%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
427 1900, 1, 1, 0, 0, 0, 0, 0, 0 );
428 octet_str.bv_val = csnbuf;
429 build_new_dn( &slap_syncCookie, &cookie->octet_str[0], &octet_str, NULL );
430 ber_bvarray_free( cookie->octet_str );
431 cookie->octet_str = NULL;
432 ber_bvarray_add( &cookie->octet_str, &slap_syncCookie );
434 ber_dupbv( &ctxcsn, &octet_str );
437 ber_dupbv( &ctxcsn_dup, &ctxcsn );
438 ch_free( ctxcsn.bv_val );
439 ber_bvarray_add( &cookie->ctxcsn, &ctxcsn_dup );
445 slap_dup_sync_cookie(
446 struct sync_cookie *dst,
447 struct sync_cookie *src
451 struct sync_cookie *new;
452 struct berval tmp_bv;
458 ber_bvarray_free( dst->ctxcsn );
459 ber_bvarray_free( dst->octet_str );
462 new = ( struct sync_cookie * )
463 ch_calloc( 1, sizeof( struct sync_cookie ));
470 for ( i=0; src->ctxcsn[i].bv_val; i++ ) {
471 ber_dupbv( &tmp_bv, &src->ctxcsn[i] );
472 ber_bvarray_add( &new->ctxcsn, &tmp_bv );
476 if ( src->octet_str ) {
477 for ( i=0; src->octet_str[i].bv_val; i++ ) {
478 ber_dupbv( &tmp_bv, &src->octet_str[i] );
479 ber_bvarray_add( &new->octet_str, &tmp_bv );
487 slap_build_syncUUID_set(
496 struct berval entryuuid_bv = BER_BVNULL;
498 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
499 AttributeDescription *desc = a->a_desc;
500 if ( desc == slap_schema.si_ad_entryUUID ) {
501 ber_dupbv_x( &entryuuid_bv, &a->a_nvals[0], op->o_tmpmemctx );
505 ret = ber_bvarray_add_x( set, &entryuuid_bv, op->o_tmpmemctx );