1 /* ldapsync.c -- LDAP Content Sync Routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2014 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>
27 #include "../../libraries/liblber/lber-int.h" /* get ber_strndup() */
28 #include "lutil_ldap.h"
30 struct slap_sync_cookie_s slap_sync_cookie =
31 LDAP_STAILQ_HEAD_INITIALIZER( slap_sync_cookie );
34 slap_compose_sync_cookie(
36 struct berval *cookie,
44 for (; !BER_BVISNULL( &csn[numcsn] ); numcsn++);
47 if ( numcsn == 0 || rid == -1 ) {
48 char cookiestr[ LDAP_PVT_CSNSTR_BUFSIZE + 20 ];
53 len = snprintf( cookiestr, sizeof( cookiestr ),
56 len += sprintf( cookiestr+len, ",sid=%03x", sid );
59 ber_str2bv_x( cookiestr, len, 1, cookie,
60 op ? op->o_tmpmemctx : NULL );
66 for ( i=0; i<numcsn; i++)
67 len += csn[i].bv_len + 1;
69 len += STRLENOF("rid=123,csn=");
71 len += STRLENOF("sid=xxx,");
73 cookie->bv_val = slap_sl_malloc( len, op ? op->o_tmpmemctx : NULL );
75 len = sprintf( cookie->bv_val, "rid=%03d,", rid );
76 ptr = cookie->bv_val + len;
78 ptr += sprintf( ptr, "sid=%03x,", sid );
80 ptr = lutil_strcopy( ptr, "csn=" );
81 for ( i=0; i<numcsn; i++) {
82 ptr = lutil_strncopy( ptr, csn[i].bv_val, csn[i].bv_len );
87 cookie->bv_len = ptr - cookie->bv_val;
92 slap_sync_cookie_free(
93 struct sync_cookie *cookie,
100 if ( cookie->sids ) {
101 ch_free( cookie->sids );
105 if ( cookie->ctxcsn ) {
106 ber_bvarray_free( cookie->ctxcsn );
107 cookie->ctxcsn = NULL;
110 if ( !BER_BVISNULL( &cookie->octet_str )) {
111 ch_free( cookie->octet_str.bv_val );
112 BER_BVZERO( &cookie->octet_str );
123 slap_parse_csn_sid( struct berval *csnp )
126 struct berval csn = *csnp;
129 p = ber_bvchr( &csn, '#' );
133 csn.bv_len -= p - csn.bv_val;
136 p = ber_bvchr( &csn, '#' );
140 csn.bv_len -= p - csn.bv_val;
143 q = ber_bvchr( &csn, '#' );
149 i = strtol( p, &q, 16 );
150 if ( p == q || q != p + csn.bv_len || i < 0 || i > SLAP_SYNC_SID_MAX ) {
158 slap_parse_csn_sids( BerVarray csns, int numcsns, void *memctx )
162 ret = slap_sl_malloc( numcsns * sizeof(int), memctx );
163 for ( i=0; i<numcsns; i++ ) {
164 ret[i] = slap_parse_csn_sid( &csns[i] );
169 static slap_mr_match_func sidsort_cmp;
171 static const MatchingRule sidsort_mr = {
177 NULL, NULL, NULL, sidsort_cmp
179 static const AttributeType sidsort_at = {
182 NULL, NULL, (MatchingRule *)&sidsort_mr,
183 NULL, NULL, NULL, NULL, NULL, NULL, NULL, SLAP_AT_SORTED_VAL
185 static const AttributeDescription sidsort_ad = {
187 (AttributeType *)&sidsort_at
199 struct berval *b2 = v2;
200 *matchp = b1->bv_len - b2->bv_len;
204 /* sort CSNs by SID. Use a fake Attribute with our own
205 * syntax and matching rule, which sorts the nvals by
206 * bv_len order. Stuff our sids into the bv_len.
209 slap_sort_csn_sids( BerVarray csns, int *sids, int numcsns, void *memctx )
215 a.a_desc = (AttributeDescription *)&sidsort_ad;
216 a.a_nvals = slap_sl_malloc( numcsns * sizeof(struct berval), memctx );
217 for ( i=0; i<numcsns; i++ ) {
218 a.a_nvals[i].bv_len = sids[i];
219 a.a_nvals[i].bv_val = NULL;
222 a.a_numvals = numcsns;
224 rc = slap_sort_vals( (Modifications *)&a, &text, &i, memctx );
225 for ( i=0; i<numcsns; i++ )
226 sids[i] = a.a_nvals[i].bv_len;
227 slap_sl_free( a.a_nvals, memctx );
232 slap_insert_csn_sids(
233 struct sync_cookie *ck,
241 ck->ctxcsn = ch_realloc( ck->ctxcsn,
242 (ck->numcsns+1) * sizeof(struct berval));
243 BER_BVZERO( &ck->ctxcsn[ck->numcsns] );
244 ck->sids = ch_realloc( ck->sids, ck->numcsns * sizeof(int));
245 for ( i = ck->numcsns-1; i > pos; i-- ) {
246 ck->ctxcsn[i] = ck->ctxcsn[i-1];
247 ck->sids[i] = ck->sids[i-1];
250 ber_dupbv( &ck->ctxcsn[i], csn );
254 slap_parse_sync_cookie(
255 struct sync_cookie *cookie,
263 AttributeDescription *ad = slap_schema.si_ad_entryCSN;
265 if ( cookie == NULL )
268 if ( cookie->octet_str.bv_len <= STRLENOF( "rid=" ) )
273 cookie->ctxcsn = NULL;
277 end = cookie->octet_str.bv_val + cookie->octet_str.bv_len;
279 for ( next=cookie->octet_str.bv_val; next < end; ) {
280 if ( !strncmp( next, "rid=", STRLENOF("rid=") )) {
281 char *rid_ptr = next;
282 cookie->rid = strtol( &rid_ptr[ STRLENOF( "rid=" ) ], &next, 10 );
283 if ( next == rid_ptr ||
285 ( *next && *next != ',' ) ||
287 cookie->rid > SLAP_SYNC_RID_MAX )
291 if ( *next == ',' ) {
299 if ( !strncmp( next, "sid=", STRLENOF("sid=") )) {
300 char *sid_ptr = next;
302 cookie->sid = strtol( &sid_ptr[ STRLENOF( "sid=" ) ], &next, 16 );
303 if ( next == sid_ptr ||
305 ( *next && *next != ',' ) ||
307 cookie->sid > SLAP_SYNC_SID_MAX )
311 if ( *next == ',' ) {
316 if ( !strncmp( next, "csn=", STRLENOF("csn=") )) {
319 next += STRLENOF("csn=");
320 while ( next < end ) {
322 csn_ptr = strchr( csn_str, '#' );
323 if ( !csn_ptr || csn_ptr > end )
325 /* ad will be NULL when called from main. we just
326 * want to parse the rid then. But we still iterate
327 * through the string to find the end.
329 cval = strchr( csn_ptr, ';' );
331 cval = strchr(csn_ptr, ',' );
333 stamp.bv_len = cval - csn_str;
335 stamp.bv_len = end - csn_str;
338 stamp.bv_val = csn_str;
339 if ( ad->ad_type->sat_syntax->ssyn_validate(
340 ad->ad_type->sat_syntax, &stamp ) != LDAP_SUCCESS )
342 if ( ad->ad_type->sat_equality->smr_normalize(
343 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
344 ad->ad_type->sat_syntax,
345 ad->ad_type->sat_equality,
346 &stamp, &bv, memctx ) != LDAP_SUCCESS )
348 ber_bvarray_add_x( &cookie->ctxcsn, &bv, memctx );
364 if ( cookie->numcsns ) {
365 cookie->sids = slap_parse_csn_sids( cookie->ctxcsn, cookie->numcsns,
367 if ( cookie->numcsns > 1 )
368 slap_sort_csn_sids( cookie->ctxcsn, cookie->sids, cookie->numcsns, memctx );
373 /* count the numcsns and regenerate the list of SIDs in a recomposed cookie */
375 slap_reparse_sync_cookie(
376 struct sync_cookie *cookie,
379 if ( cookie->ctxcsn ) {
380 for (; !BER_BVISNULL( &cookie->ctxcsn[cookie->numcsns] ); cookie->numcsns++);
382 if ( cookie->numcsns ) {
383 cookie->sids = slap_parse_csn_sids( cookie->ctxcsn, cookie->numcsns, NULL );
384 if ( cookie->numcsns > 1 )
385 slap_sort_csn_sids( cookie->ctxcsn, cookie->sids, cookie->numcsns, memctx );
390 slap_init_sync_cookie_ctxcsn(
391 struct sync_cookie *cookie
394 char csnbuf[ LDAP_PVT_CSNSTR_BUFSIZE + 4 ];
395 struct berval octet_str = BER_BVNULL;
396 struct berval ctxcsn = BER_BVNULL;
398 if ( cookie == NULL )
401 octet_str.bv_len = snprintf( csnbuf, LDAP_PVT_CSNSTR_BUFSIZE + 4,
402 "csn=%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
403 1900, 1, 1, 0, 0, 0, 0, 0, 0 );
404 octet_str.bv_val = csnbuf;
405 ch_free( cookie->octet_str.bv_val );
406 ber_dupbv( &cookie->octet_str, &octet_str );
408 ctxcsn.bv_val = octet_str.bv_val + 4;
409 ctxcsn.bv_len = octet_str.bv_len - 4;
410 cookie->ctxcsn = NULL;
411 value_add_one( &cookie->ctxcsn, &ctxcsn );
419 slap_dup_sync_cookie(
420 struct sync_cookie *dst,
421 struct sync_cookie *src
424 struct sync_cookie *new;
431 ber_bvarray_free( dst->ctxcsn );
434 ch_free( dst->octet_str.bv_val );
435 BER_BVZERO( &dst->octet_str );
438 new = ( struct sync_cookie * )
439 ch_calloc( 1, sizeof( struct sync_cookie ));
444 new->numcsns = src->numcsns;
446 if ( src->numcsns ) {
447 if ( ber_bvarray_dup_x( &new->ctxcsn, src->ctxcsn, NULL )) {
453 new->sids = ch_malloc( src->numcsns * sizeof(int) );
454 for (i=0; i<src->numcsns; i++)
455 new->sids[i] = src->sids[i];
458 if ( !BER_BVISNULL( &src->octet_str )) {
459 ber_dupbv( &new->octet_str, &src->octet_str );