1 /* ldapsync.c -- LDAP Content Sync Routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2005 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 ];
42 if ( BER_BVISNULL( csn )) {
46 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
51 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
52 "csn=%s", csn->bv_val );
54 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
55 "csn=%s,rid=%03d", csn->bv_val, rid );
58 ber_str2bv( cookiestr, strlen(cookiestr), 1, cookie );
62 slap_sync_cookie_free(
63 struct sync_cookie *cookie,
70 if ( !BER_BVISNULL( &cookie->ctxcsn )) {
71 ch_free( cookie->ctxcsn.bv_val );
72 BER_BVZERO( &cookie->ctxcsn );
75 if ( !BER_BVISNULL( &cookie->octet_str )) {
76 ch_free( cookie->octet_str.bv_val );
77 BER_BVZERO( &cookie->octet_str );
88 slap_parse_sync_cookie(
89 struct sync_cookie *cookie,
101 if ( cookie == NULL )
104 while (( csn_ptr = strstr( cookie->octet_str.bv_val, "csn=" )) != NULL ) {
105 AttributeDescription *ad = slap_schema.si_ad_modifyTimestamp;
106 slap_syntax_validate_func *validate;
109 csn_str = csn_ptr + STRLENOF("csn=");
110 cval = strchr( csn_str, ',' );
112 csn_str_len = cval - csn_str;
116 /* FIXME use csnValidate when it gets implemented */
117 csn_ptr = strchr( csn_str, '#' );
118 if ( !csn_ptr ) break;
120 stamp.bv_val = csn_str;
121 stamp.bv_len = csn_ptr - csn_str;
122 validate = ad->ad_type->sat_syntax->ssyn_validate;
123 if ( validate( ad->ad_type->sat_syntax, &stamp ) != LDAP_SUCCESS )
129 ber_str2bv_x( csn_str, csn_str_len, 1, &cookie->ctxcsn, memctx );
131 BER_BVZERO( &cookie->ctxcsn );
134 if (( rid_ptr = strstr( cookie->octet_str.bv_val, "rid=" )) != NULL ) {
135 rid_str = SLAP_STRNDUP( rid_ptr,
136 SLAP_SYNC_RID_SIZE + sizeof("rid=") - 1 );
137 if ( (cval = strchr( rid_str, ',' )) != NULL ) {
140 cookie->rid = atoi( rid_str + sizeof("rid=") - 1 );
149 slap_init_sync_cookie_ctxcsn(
150 struct sync_cookie *cookie
153 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE + 4 ];
154 struct berval octet_str = BER_BVNULL;
155 struct berval ctxcsn = BER_BVNULL;
157 if ( cookie == NULL )
160 octet_str.bv_len = snprintf( csnbuf, LDAP_LUTIL_CSNSTR_BUFSIZE + 4,
161 "csn=%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
162 1900, 1, 1, 0, 0, 0, 0, 0, 0 );
163 octet_str.bv_val = csnbuf;
164 ch_free( cookie->octet_str.bv_val );
165 ber_dupbv( &cookie->octet_str, &octet_str );
167 ctxcsn.bv_val = octet_str.bv_val + 4;
168 ctxcsn.bv_len = octet_str.bv_len - 4;
169 ber_dupbv( &cookie->ctxcsn, &ctxcsn );
175 slap_dup_sync_cookie(
176 struct sync_cookie *dst,
177 struct sync_cookie *src
180 struct sync_cookie *new;
186 ch_free( dst->ctxcsn.bv_val );
187 ch_free( dst->octet_str.bv_val );
188 BER_BVZERO( &dst->ctxcsn );
189 BER_BVZERO( &dst->octet_str );
192 new = ( struct sync_cookie * )
193 ch_calloc( 1, sizeof( struct sync_cookie ));
198 if ( !BER_BVISNULL( &src->ctxcsn )) {
199 ber_dupbv( &new->ctxcsn, &src->ctxcsn );
202 if ( !BER_BVISNULL( &src->octet_str )) {
203 ber_dupbv( &new->octet_str, &src->octet_str );