1 /* ldapsync.c -- LDAP Content Sync Routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2006 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,
40 char cookiestr[ LDAP_LUTIL_CSNSTR_BUFSIZE + 20 ];
43 if ( BER_BVISNULL( csn )) {
48 len = snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
52 char *end = cookiestr + sizeof(cookiestr);
53 char *ptr = lutil_strcopy( cookiestr, "csn=" );
55 if ( ptr + len >= end )
57 ptr = lutil_strncopy( ptr, csn->bv_val, len );
58 if ( rid != -1 && ptr < end - STRLENOF(",rid=xxx") ) {
59 ptr += sprintf( ptr, ",rid=%03d", rid );
61 len = ptr - cookiestr;
63 ber_str2bv_x( cookiestr, len, 1, cookie,
64 op ? op->o_tmpmemctx : NULL );
68 slap_sync_cookie_free(
69 struct sync_cookie *cookie,
76 if ( !BER_BVISNULL( &cookie->ctxcsn )) {
77 ch_free( cookie->ctxcsn.bv_val );
78 BER_BVZERO( &cookie->ctxcsn );
81 if ( !BER_BVISNULL( &cookie->octet_str )) {
82 ch_free( cookie->octet_str.bv_val );
83 BER_BVZERO( &cookie->octet_str );
94 slap_parse_sync_cookie(
95 struct sync_cookie *cookie,
107 if ( cookie == NULL )
110 if ( cookie->octet_str.bv_len <= STRLENOF( "rid=" ) )
114 /* FIXME: may read past end of cookie->octet_str.bv_val */
115 rid_ptr = strstr( cookie->octet_str.bv_val, "rid=" );
117 || rid_ptr > &cookie->octet_str.bv_val[ cookie->octet_str.bv_len - STRLENOF( "rid=" ) ] )
122 if ( rid_ptr[ STRLENOF( "rid=" ) ] == '-' ) {
125 cookie->rid = strtoul( &rid_ptr[ STRLENOF( "rid=" ) ], &next, 10 );
126 if ( next == &rid_ptr[ STRLENOF( "rid=" ) ] || ( next[ 0 ] != ',' && next[ 0 ] != '\0' ) ) {
130 while (( csn_ptr = strstr( cookie->octet_str.bv_val, "csn=" )) != NULL ) {
131 AttributeDescription *ad = slap_schema.si_ad_modifyTimestamp;
132 slap_syntax_validate_func *validate;
135 /* This only happens when called from main */
139 if ( csn_ptr >= &cookie->octet_str.bv_val[ cookie->octet_str.bv_len - STRLENOF( "csn=" ) ] ) {
143 csn_str = csn_ptr + STRLENOF("csn=");
144 cval = strchr( csn_str, ',' );
145 if ( cval && cval < &cookie->octet_str.bv_val[ cookie->octet_str.bv_len ] )
146 csn_str_len = cval - csn_str;
150 /* FIXME use csnValidate when it gets implemented */
151 csn_ptr = strchr( csn_str, '#' );
152 if ( !csn_ptr || csn_str >= &cookie->octet_str.bv_val[ cookie->octet_str.bv_len ] ) break;
154 stamp.bv_val = csn_str;
155 stamp.bv_len = csn_ptr - csn_str;
156 validate = ad->ad_type->sat_syntax->ssyn_validate;
157 if ( validate( ad->ad_type->sat_syntax, &stamp ) != LDAP_SUCCESS )
163 ber_str2bv_x( csn_str, csn_str_len, 1, &cookie->ctxcsn, memctx );
165 BER_BVZERO( &cookie->ctxcsn );
172 slap_init_sync_cookie_ctxcsn(
173 struct sync_cookie *cookie
176 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE + 4 ];
177 struct berval octet_str = BER_BVNULL;
178 struct berval ctxcsn = BER_BVNULL;
180 if ( cookie == NULL )
183 octet_str.bv_len = snprintf( csnbuf, LDAP_LUTIL_CSNSTR_BUFSIZE + 4,
184 "csn=%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
185 1900, 1, 1, 0, 0, 0, 0, 0, 0 );
186 octet_str.bv_val = csnbuf;
187 ch_free( cookie->octet_str.bv_val );
188 ber_dupbv( &cookie->octet_str, &octet_str );
190 ctxcsn.bv_val = octet_str.bv_val + 4;
191 ctxcsn.bv_len = octet_str.bv_len - 4;
192 ber_dupbv( &cookie->ctxcsn, &ctxcsn );
198 slap_dup_sync_cookie(
199 struct sync_cookie *dst,
200 struct sync_cookie *src
203 struct sync_cookie *new;
209 ch_free( dst->ctxcsn.bv_val );
210 ch_free( dst->octet_str.bv_val );
211 BER_BVZERO( &dst->ctxcsn );
212 BER_BVZERO( &dst->octet_str );
215 new = ( struct sync_cookie * )
216 ch_calloc( 1, sizeof( struct sync_cookie ));
221 if ( !BER_BVISNULL( &src->ctxcsn )) {
222 ber_dupbv( &new->ctxcsn, &src->ctxcsn );
225 if ( !BER_BVISNULL( &src->octet_str )) {
226 ber_dupbv( &new->octet_str, &src->octet_str );