1 /* ldapsync.c -- LDAP Content Sync Routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2007 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_LUTIL_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 *csn )
128 p = memchr( csn->bv_val, '#', csn->bv_len );
130 p = strchr( p+1, '#' );
134 i = strtoul( p, &q, 10 );
135 if ( p == q || i > SLAP_SYNC_SID_MAX )
141 slap_parse_csn_sids( BerVarray csns, int numcsns, void *memctx )
145 ret = slap_sl_malloc( numcsns * sizeof(int), memctx );
146 for ( i=0; i<numcsns; i++ ) {
147 ret[i] = slap_parse_csn_sid( &csns[i] );
153 slap_parse_sync_cookie(
154 struct sync_cookie *cookie,
163 AttributeDescription *ad = slap_schema.si_ad_modifyTimestamp;
165 if ( cookie == NULL )
168 if ( cookie->octet_str.bv_len <= STRLENOF( "rid=" ) )
173 cookie->ctxcsn = NULL;
177 end = cookie->octet_str.bv_val + cookie->octet_str.bv_len;
179 for ( next=cookie->octet_str.bv_val; next < end; ) {
180 if ( !strncmp( next, "rid=", STRLENOF("rid=") )) {
182 cookie->rid = strtoul( &rid_ptr[ STRLENOF( "rid=" ) ], &next, 10 );
183 if ( next == rid_ptr || next > end || *next != ',' ) {
186 if ( *next == ',' ) {
194 if ( !strncmp( next, "sid=", STRLENOF("sid=") )) {
196 cookie->sid = strtoul( &rid_ptr[ STRLENOF( "sid=" ) ], &next, 16 );
197 if ( next == rid_ptr || next > end || *next != ',' ) {
200 if ( *next == ',' ) {
205 if ( !strncmp( next, "csn=", STRLENOF("csn=") )) {
206 slap_syntax_validate_func *validate;
209 next += STRLENOF("csn=");
210 while ( next < end ) {
212 /* FIXME use csnValidate when it gets implemented */
213 csn_ptr = strchr( csn_str, '#' );
214 if ( !csn_ptr || csn_ptr > end )
216 /* ad will be NULL when called from main. we just
217 * want to parse the rid then. But we still iterate
218 * through the string to find the end.
221 stamp.bv_val = csn_str;
222 stamp.bv_len = csn_ptr - csn_str;
223 validate = ad->ad_type->sat_syntax->ssyn_validate;
224 if ( validate( ad->ad_type->sat_syntax, &stamp )
228 cval = strchr( csn_ptr, ';' );
230 cval = strchr(csn_ptr, ',' );
232 stamp.bv_len = cval - csn_str;
234 stamp.bv_len = end - csn_str;
237 ber_dupbv_x( &bv, &stamp, memctx );
238 ber_bvarray_add_x( &cookie->ctxcsn, &bv, memctx );
254 if ( cookie->numcsns ) {
255 cookie->sids = slap_parse_csn_sids( cookie->ctxcsn, cookie->numcsns,
262 slap_init_sync_cookie_ctxcsn(
263 struct sync_cookie *cookie
266 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE + 4 ];
267 struct berval octet_str = BER_BVNULL;
268 struct berval ctxcsn = BER_BVNULL;
270 if ( cookie == NULL )
273 octet_str.bv_len = snprintf( csnbuf, LDAP_LUTIL_CSNSTR_BUFSIZE + 4,
274 "csn=%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
275 1900, 1, 1, 0, 0, 0, 0, 0, 0 );
276 octet_str.bv_val = csnbuf;
277 ch_free( cookie->octet_str.bv_val );
278 ber_dupbv( &cookie->octet_str, &octet_str );
280 ctxcsn.bv_val = octet_str.bv_val + 4;
281 ctxcsn.bv_len = octet_str.bv_len - 4;
282 cookie->ctxcsn = NULL;
283 value_add_one( &cookie->ctxcsn, &ctxcsn );
291 slap_dup_sync_cookie(
292 struct sync_cookie *dst,
293 struct sync_cookie *src
296 struct sync_cookie *new;
303 ber_bvarray_free( dst->ctxcsn );
306 ch_free( dst->octet_str.bv_val );
307 BER_BVZERO( &dst->octet_str );
310 new = ( struct sync_cookie * )
311 ch_calloc( 1, sizeof( struct sync_cookie ));
316 new->numcsns = src->numcsns;
318 if ( src->numcsns ) {
319 if ( ber_bvarray_dup_x( &new->ctxcsn, src->ctxcsn, NULL )) {
325 new->sids = ch_malloc( src->numcsns * sizeof(int) );
326 for (i=0; i<src->numcsns; i++)
327 new->sids[i] = src->sids[i];
330 if ( !BER_BVISNULL( &src->octet_str )) {
331 ber_dupbv( &new->octet_str, &src->octet_str );