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 )
146 ret = ch_malloc( numcsns * sizeof(int) );
147 for ( i=0; i<numcsns; i++ ) {
148 ret[i] = slap_parse_csn_sid( &csns[i] );
154 slap_parse_sync_cookie(
155 struct sync_cookie *cookie,
165 AttributeDescription *ad = slap_schema.si_ad_modifyTimestamp;
167 if ( cookie == NULL )
170 if ( cookie->octet_str.bv_len <= STRLENOF( "rid=" ) )
175 cookie->ctxcsn = NULL;
179 end = cookie->octet_str.bv_val + cookie->octet_str.bv_len;
181 for ( next=cookie->octet_str.bv_val; next < end; ) {
182 if ( !strncmp( next, "rid=", STRLENOF("rid=") )) {
184 cookie->rid = strtoul( &rid_ptr[ STRLENOF( "rid=" ) ], &next, 10 );
185 if ( next == rid_ptr || next > end || *next != ',' ) {
188 if ( *next == ',' ) {
196 if ( !strncmp( next, "sid=", STRLENOF("sid=") )) {
198 cookie->sid = strtoul( &rid_ptr[ STRLENOF( "sid=" ) ], &next, 16 );
199 if ( next == rid_ptr || next > end || *next != ',' ) {
202 if ( *next == ',' ) {
207 if ( !strncmp( next, "csn=", STRLENOF("csn=") )) {
208 slap_syntax_validate_func *validate;
211 next += STRLENOF("csn=");
212 while ( next < end ) {
214 /* FIXME use csnValidate when it gets implemented */
215 csn_ptr = strchr( csn_str, '#' );
216 if ( !csn_ptr || csn_ptr > end )
218 /* ad will be NULL when called from main. we just
219 * want to parse the rid then. But we still iterate
220 * through the string to find the end.
223 stamp.bv_val = csn_str;
224 stamp.bv_len = csn_ptr - csn_str;
225 validate = ad->ad_type->sat_syntax->ssyn_validate;
226 if ( validate( ad->ad_type->sat_syntax, &stamp )
230 cval = strchr( csn_ptr, ';' );
232 cval = strchr(csn_ptr, ',' );
234 stamp.bv_len = cval - csn_str;
236 stamp.bv_len = end - csn_str;
238 value_add_one( &cookie->ctxcsn, &stamp );
254 if ( cookie->numcsns ) {
255 cookie->sids = slap_parse_csn_sids( cookie->ctxcsn, cookie->numcsns );
261 slap_init_sync_cookie_ctxcsn(
262 struct sync_cookie *cookie
265 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE + 4 ];
266 struct berval octet_str = BER_BVNULL;
267 struct berval ctxcsn = BER_BVNULL;
269 if ( cookie == NULL )
272 octet_str.bv_len = snprintf( csnbuf, LDAP_LUTIL_CSNSTR_BUFSIZE + 4,
273 "csn=%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
274 1900, 1, 1, 0, 0, 0, 0, 0, 0 );
275 octet_str.bv_val = csnbuf;
276 ch_free( cookie->octet_str.bv_val );
277 ber_dupbv( &cookie->octet_str, &octet_str );
279 ctxcsn.bv_val = octet_str.bv_val + 4;
280 ctxcsn.bv_len = octet_str.bv_len - 4;
281 cookie->ctxcsn = NULL;
282 value_add_one( &cookie->ctxcsn, &ctxcsn );
290 slap_dup_sync_cookie(
291 struct sync_cookie *dst,
292 struct sync_cookie *src
295 struct sync_cookie *new;
302 ber_bvarray_free( dst->ctxcsn );
305 ch_free( dst->octet_str.bv_val );
306 BER_BVZERO( &dst->octet_str );
309 new = ( struct sync_cookie * )
310 ch_calloc( 1, sizeof( struct sync_cookie ));
315 new->numcsns = src->numcsns;
317 if ( src->numcsns ) {
318 if ( ber_bvarray_dup_x( &new->ctxcsn, src->ctxcsn, NULL )) {
324 new->sids = ch_malloc( src->numcsns * sizeof(int) );
325 for (i=0; i<src->numcsns; i++)
326 new->sids[i] = src->sids[i];
329 if ( !BER_BVISNULL( &src->octet_str )) {
330 ber_dupbv( &new->octet_str, &src->octet_str );