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,
43 for (; !BER_BVISEMPTY( &csn[numcsn] ); numcsn++);
46 if ( numcsn == 0 || rid == -1 ) {
47 char cookiestr[ LDAP_LUTIL_CSNSTR_BUFSIZE + 20 ];
52 len = snprintf( cookiestr, sizeof( cookiestr ),
55 ber_str2bv_x( cookiestr, len, 1, cookie,
56 op ? op->o_tmpmemctx : NULL );
62 for ( i=0; i<numcsn; i++)
63 len += csn[i].bv_len + 1;
65 len += STRLENOF("rid=123,csn=");
66 cookie->bv_val = slap_sl_malloc( len, op ? op->o_tmpmemctx : NULL );
68 len = sprintf( cookie->bv_val, "rid=%03d,csn=", rid );
69 ptr = cookie->bv_val + len;
70 for ( i=0; i<numcsn; i++) {
71 ptr = lutil_strncopy( ptr, csn->bv_val, csn->bv_len );
76 cookie->bv_len = ptr - cookie->bv_val;
81 slap_sync_cookie_free(
82 struct sync_cookie *cookie,
90 ch_free( cookie->sids );
94 if ( cookie->ctxcsn ) {
95 ber_bvarray_free( cookie->ctxcsn );
96 cookie->ctxcsn = NULL;
99 if ( !BER_BVISNULL( &cookie->octet_str )) {
100 ch_free( cookie->octet_str.bv_val );
101 BER_BVZERO( &cookie->octet_str );
112 slap_parse_csn_sid( struct berval *csn )
117 p = memchr( csn->bv_val, '#', csn->bv_len );
119 p = strchr( p+1, '#' );
123 i = strtoul( p, &q, 10 );
124 if ( p == q || i > SLAP_SYNC_SID_MAX )
130 slap_parse_csn_sids( BerVarray csns, int numcsns )
135 ret = ch_malloc( numcsns * sizeof(int) );
136 for ( i=0; i<numcsns; i++ ) {
137 ret[i] = slap_parse_csn_sid( &csns[i] );
143 slap_parse_sync_cookie(
144 struct sync_cookie *cookie,
154 AttributeDescription *ad = slap_schema.si_ad_modifyTimestamp;
156 if ( cookie == NULL )
159 if ( cookie->octet_str.bv_len <= STRLENOF( "rid=" ) )
163 cookie->ctxcsn = NULL;
167 end = cookie->octet_str.bv_val + cookie->octet_str.bv_len;
169 for ( next=cookie->octet_str.bv_val; next < end; ) {
170 if ( !strncmp( next, "rid=", STRLENOF("rid=") )) {
172 cookie->rid = strtoul( &rid_ptr[ STRLENOF( "rid=" ) ], &next, 10 );
173 if ( next == rid_ptr || next > end || *next != ',' ) {
176 if ( *next == ',' ) {
184 if ( !strncmp( next, "csn=", STRLENOF("csn=") )) {
185 slap_syntax_validate_func *validate;
188 next += STRLENOF("csn=");
189 while ( next < end ) {
191 /* FIXME use csnValidate when it gets implemented */
192 csn_ptr = strchr( csn_str, '#' );
193 if ( !csn_ptr || csn_ptr > end )
195 /* ad will be NULL when called from main. we just
196 * want to parse the rid then. But we still iterate
197 * through the string to find the end.
200 stamp.bv_val = csn_str;
201 stamp.bv_len = csn_ptr - csn_str;
202 validate = ad->ad_type->sat_syntax->ssyn_validate;
203 if ( validate( ad->ad_type->sat_syntax, &stamp )
207 cval = strchr( csn_ptr, ';' );
209 cval = strchr(csn_ptr, ',' );
211 stamp.bv_len = cval - csn_str;
213 stamp.bv_len = end - csn_str;
215 value_add_one( &cookie->ctxcsn, &stamp );
231 if ( cookie->numcsns ) {
232 cookie->sids = slap_parse_csn_sids( cookie->ctxcsn, cookie->numcsns );
238 slap_init_sync_cookie_ctxcsn(
239 struct sync_cookie *cookie
242 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE + 4 ];
243 struct berval octet_str = BER_BVNULL;
244 struct berval ctxcsn = BER_BVNULL;
246 if ( cookie == NULL )
249 octet_str.bv_len = snprintf( csnbuf, LDAP_LUTIL_CSNSTR_BUFSIZE + 4,
250 "csn=%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
251 1900, 1, 1, 0, 0, 0, 0, 0, 0 );
252 octet_str.bv_val = csnbuf;
253 ch_free( cookie->octet_str.bv_val );
254 ber_dupbv( &cookie->octet_str, &octet_str );
256 ctxcsn.bv_val = octet_str.bv_val + 4;
257 ctxcsn.bv_len = octet_str.bv_len - 4;
258 cookie->ctxcsn = NULL;
259 value_add_one( &cookie->ctxcsn, &ctxcsn );
266 slap_dup_sync_cookie(
267 struct sync_cookie *dst,
268 struct sync_cookie *src
271 struct sync_cookie *new;
278 ber_bvarray_free( dst->ctxcsn );
281 ch_free( dst->octet_str.bv_val );
282 BER_BVZERO( &dst->octet_str );
285 new = ( struct sync_cookie * )
286 ch_calloc( 1, sizeof( struct sync_cookie ));
290 new->numcsns = src->numcsns;
292 if ( src->numcsns ) {
293 if ( ber_bvarray_dup_x( &new->ctxcsn, src->ctxcsn, NULL )) {
299 new->sids = ch_malloc( src->numcsns * sizeof(int) );
300 for (i=0; i<src->numcsns; i++)
301 new->sids[i] = src->sids[i];
304 if ( !BER_BVISNULL( &src->octet_str )) {
305 ber_dupbv( &new->octet_str, &src->octet_str );