1 /* ldapsync.c -- LDAP Content Sync Routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2004 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,
45 char cookiestr[ LDAP_LUTIL_CSNSTR_BUFSIZE + 20 ];
47 if ( csn->bv_val == NULL ) {
52 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
57 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
60 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
61 "sid=%03d,rid=%03d", sid, rid );
67 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
68 "csn=%s", csn->bv_val );
70 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
71 "csn=%s,rid=%03d", csn->bv_val, rid );
75 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
76 "csn=%s,sid=%03d", csn->bv_val, sid );
78 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
79 "csn=%s,sid=%03d,rid=%03d", csn->bv_val, sid, rid );
83 ber_str2bv( cookiestr, strlen(cookiestr), 1, cookie );
87 slap_sync_cookie_free(
88 struct sync_cookie *cookie,
95 if ( cookie->ctxcsn ) {
96 ber_bvarray_free( cookie->ctxcsn );
97 cookie->ctxcsn = NULL;
100 if ( cookie->octet_str ) {
101 ber_bvarray_free( cookie->octet_str );
102 cookie->octet_str = NULL;
113 slap_parse_sync_cookie(
114 struct sync_cookie *cookie
125 struct berval *ctxcsn;
127 if ( cookie == NULL )
130 if (( csn_ptr = strstr( cookie->octet_str[0].bv_val, "csn=" )) != NULL ) {
131 csn_str = SLAP_STRNDUP( csn_ptr, LDAP_LUTIL_CSNSTR_BUFSIZE );
132 if ( (cval = strchr( csn_str, ',' )) != NULL ) {
134 csn_str_len = cval - csn_str - (sizeof("csn=") - 1);
136 csn_str_len = cookie->octet_str[0].bv_len -
137 (csn_ptr - cookie->octet_str[0].bv_val) -
138 (sizeof("csn=") - 1);
140 ctxcsn = ber_str2bv( csn_str + (sizeof("csn=")-1),
141 csn_str_len, 1, NULL );
143 ber_bvarray_add( &cookie->ctxcsn, ctxcsn );
146 cookie->ctxcsn = NULL;
149 if (( sid_ptr = strstr( cookie->octet_str->bv_val, "sid=" )) != NULL ) {
150 sid_str = SLAP_STRNDUP( sid_ptr,
151 SLAP_SYNC_SID_SIZE + sizeof("sid=") - 1 );
152 if ( (cval = strchr( sid_str, ',' )) != NULL ) {
155 cookie->sid = atoi( sid_str + sizeof("sid=") - 1 );
161 if (( rid_ptr = strstr( cookie->octet_str->bv_val, "rid=" )) != NULL ) {
162 rid_str = SLAP_STRNDUP( rid_ptr,
163 SLAP_SYNC_RID_SIZE + sizeof("rid=") - 1 );
164 if ( (cval = strchr( rid_str, ',' )) != NULL ) {
167 cookie->rid = atoi( rid_str + sizeof("rid=") - 1 );
176 slap_init_sync_cookie_ctxcsn(
177 struct sync_cookie *cookie
180 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE + 4 ];
181 struct berval octet_str = BER_BVNULL;
182 struct berval ctxcsn = BER_BVNULL;
183 struct berval ctxcsn_dup = BER_BVNULL;
184 struct berval slap_syncCookie;
186 if ( cookie == NULL )
189 octet_str.bv_len = snprintf( csnbuf, LDAP_LUTIL_CSNSTR_BUFSIZE + 4,
190 "csn=%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
191 1900, 1, 1, 0, 0, 0, 0, 0, 0 );
192 octet_str.bv_val = csnbuf;
193 build_new_dn( &slap_syncCookie, &cookie->octet_str[0], &octet_str, NULL );
194 ber_bvarray_free( cookie->octet_str );
195 cookie->octet_str = NULL;
196 ber_bvarray_add( &cookie->octet_str, &slap_syncCookie );
198 ctxcsn.bv_val = octet_str.bv_val + 4;
199 ctxcsn.bv_len = octet_str.bv_len - 4;
200 ber_dupbv( &ctxcsn_dup, &ctxcsn );
201 ber_bvarray_add( &cookie->ctxcsn, &ctxcsn_dup );
207 slap_dup_sync_cookie(
208 struct sync_cookie *dst,
209 struct sync_cookie *src
213 struct sync_cookie *new;
214 struct berval tmp_bv;
220 ber_bvarray_free( dst->ctxcsn );
221 ber_bvarray_free( dst->octet_str );
224 new = ( struct sync_cookie * )
225 ch_calloc( 1, sizeof( struct sync_cookie ));
232 for ( i=0; src->ctxcsn[i].bv_val; i++ ) {
233 ber_dupbv( &tmp_bv, &src->ctxcsn[i] );
234 ber_bvarray_add( &new->ctxcsn, &tmp_bv );
238 if ( src->octet_str ) {
239 for ( i=0; src->octet_str[i].bv_val; i++ ) {
240 ber_dupbv( &tmp_bv, &src->octet_str[i] );
241 ber_bvarray_add( &new->octet_str, &tmp_bv );