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"
31 struct sync_cookie *slap_sync_cookie = NULL;
33 struct slap_sync_cookie_s slap_sync_cookie =
34 LDAP_STAILQ_HEAD_INITIALIZER( slap_sync_cookie );
38 slap_compose_sync_cookie(
40 struct berval *cookie,
44 char cookiestr[ LDAP_LUTIL_CSNSTR_BUFSIZE + 20 ];
46 if ( csn->bv_val == NULL ) {
50 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
55 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
56 "csn=%s", csn->bv_val );
58 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
59 "csn=%s,rid=%03d", csn->bv_val, rid );
62 ber_str2bv( cookiestr, strlen(cookiestr), 1, cookie );
66 slap_sync_cookie_free(
67 struct sync_cookie *cookie,
74 if ( !BER_BVISNULL( &cookie->ctxcsn )) {
75 ch_free( cookie->ctxcsn.bv_val );
76 BER_BVZERO( &cookie->ctxcsn );
79 if ( !BER_BVISNULL( &cookie->octet_str )) {
80 ch_free( cookie->octet_str.bv_val );
81 BER_BVZERO( &cookie->octet_str );
92 slap_parse_sync_cookie(
93 struct sync_cookie *cookie
104 if ( cookie == NULL )
107 while (( csn_ptr = strstr( cookie->octet_str.bv_val, "csn=" )) != NULL ) {
108 AttributeDescription *ad = slap_schema.si_ad_modifyTimestamp;
109 slap_syntax_validate_func *validate;
112 csn_str = csn_ptr + STRLENOF("csn=");
113 cval = strchr( csn_str, ',' );
115 csn_str_len = cval - csn_str;
119 /* FIXME use csnValidate when it gets implemented */
120 csn_ptr = strchr( csn_str, '#' );
121 if ( !csn_ptr ) break;
123 stamp.bv_val = csn_str;
124 stamp.bv_len = csn_ptr - csn_str;
125 validate = ad->ad_type->sat_syntax->ssyn_validate;
126 if ( validate( ad->ad_type->sat_syntax, &stamp ) != LDAP_SUCCESS )
132 ber_str2bv( csn_str, csn_str_len, 1, &cookie->ctxcsn );
134 BER_BVZERO( &cookie->ctxcsn );
137 if (( rid_ptr = strstr( cookie->octet_str.bv_val, "rid=" )) != NULL ) {
138 rid_str = SLAP_STRNDUP( rid_ptr,
139 SLAP_SYNC_RID_SIZE + sizeof("rid=") - 1 );
140 if ( (cval = strchr( rid_str, ',' )) != NULL ) {
143 cookie->rid = atoi( rid_str + sizeof("rid=") - 1 );
152 slap_init_sync_cookie_ctxcsn(
153 struct sync_cookie *cookie
156 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE + 4 ];
157 struct berval octet_str = BER_BVNULL;
158 struct berval ctxcsn = BER_BVNULL;
160 if ( cookie == NULL )
163 octet_str.bv_len = snprintf( csnbuf, LDAP_LUTIL_CSNSTR_BUFSIZE + 4,
164 "csn=%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
165 1900, 1, 1, 0, 0, 0, 0, 0, 0 );
166 octet_str.bv_val = csnbuf;
167 ch_free( cookie->octet_str.bv_val );
168 ber_dupbv( &cookie->octet_str, &octet_str );
170 ctxcsn.bv_val = octet_str.bv_val + 4;
171 ctxcsn.bv_len = octet_str.bv_len - 4;
172 ber_dupbv( &cookie->ctxcsn, &ctxcsn );
178 slap_dup_sync_cookie(
179 struct sync_cookie *dst,
180 struct sync_cookie *src
184 struct sync_cookie *new;
185 struct berval tmp_bv;
191 ch_free( dst->ctxcsn.bv_val );
192 ch_free( dst->octet_str.bv_val );
193 BER_BVZERO( &dst->ctxcsn );
194 BER_BVZERO( &dst->octet_str );
197 new = ( struct sync_cookie * )
198 ch_calloc( 1, sizeof( struct sync_cookie ));
203 if ( !BER_BVISNULL( &src->ctxcsn )) {
204 ber_dupbv( &new->ctxcsn, &src->ctxcsn );
207 if ( !BER_BVISNULL( &src->octet_str )) {
208 ber_dupbv( &new->octet_str, &src->octet_str );