]> git.sur5r.net Git - openldap/blob - servers/slapd/ldapsync.c
Fix ITS#3424
[openldap] / servers / slapd / ldapsync.c
1 /* ldapsync.c -- LDAP Content Sync Routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003-2004 The OpenLDAP Foundation.
6  * Portions Copyright 2003 IBM Corporation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
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>.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21
22 #include <ac/string.h>
23 #include <ac/socket.h>
24
25 #include "lutil.h"
26 #include "slap.h"
27 #include "../../libraries/liblber/lber-int.h" /* get ber_strndup() */
28 #include "lutil_ldap.h"
29
30 #if 0
31 struct sync_cookie *slap_sync_cookie = NULL;
32 #else
33 struct slap_sync_cookie_s slap_sync_cookie =
34         LDAP_STAILQ_HEAD_INITIALIZER( slap_sync_cookie );
35 #endif
36
37 void
38 slap_compose_sync_cookie(
39         Operation *op,
40         struct berval *cookie,
41         struct berval *csn,
42         int sid,
43         int rid )
44 {
45         char cookiestr[ LDAP_LUTIL_CSNSTR_BUFSIZE + 20 ];
46
47         if ( csn->bv_val == NULL ) {
48                 if ( sid == -1 ) {
49                         if ( rid == -1 ) {
50                                 cookiestr[0] = '\0';
51                         } else {
52                                 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
53                                                 "rid=%03d", rid );
54                         }
55                 } else {
56                         if ( rid == -1 ) {
57                                 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
58                                                 "sid=%03d", sid );
59                         } else {
60                                 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
61                                                 "sid=%03d,rid=%03d", sid, rid );
62                         }
63                 }
64         } else {
65                 if ( sid == -1 ) {
66                         if ( rid == -1 ) {
67                                 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
68                                                 "csn=%s", csn->bv_val );
69                         } else {
70                                 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
71                                                 "csn=%s,rid=%03d", csn->bv_val, rid );
72                         }
73                 } else {
74                         if ( rid == -1 ) {
75                                 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
76                                                 "csn=%s,sid=%03d", csn->bv_val, sid );
77                         } else {
78                                 snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
79                                                 "csn=%s,sid=%03d,rid=%03d", csn->bv_val, sid, rid );
80                         }
81                 }
82         }
83         ber_str2bv( cookiestr, strlen(cookiestr), 1, cookie );
84 }
85
86 void
87 slap_sync_cookie_free(
88         struct sync_cookie *cookie,
89         int free_cookie
90 )
91 {
92         if ( cookie == NULL )
93                 return;
94
95         if ( cookie->ctxcsn ) {
96                 ber_bvarray_free( cookie->ctxcsn );
97                 cookie->ctxcsn = NULL;
98         }
99
100         if ( cookie->octet_str ) {
101                 ber_bvarray_free( cookie->octet_str );
102                 cookie->octet_str = NULL;
103         }
104
105         if ( free_cookie ) {
106                 ch_free( cookie );
107         }
108
109         return;
110 }
111
112 int
113 slap_parse_sync_cookie(
114         struct sync_cookie *cookie
115 )
116 {
117         char *csn_ptr;
118         char *csn_str;
119         int csn_str_len;
120         char *sid_ptr;
121         char *sid_str;
122         char *rid_ptr;
123         char *rid_str;
124         char *cval;
125         struct berval *ctxcsn;
126
127         if ( cookie == NULL )
128                 return -1;
129
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 ) {
133                         *cval = '\0';
134                         csn_str_len = cval - csn_str - (sizeof("csn=") - 1);
135                 } else {
136                         csn_str_len = cookie->octet_str[0].bv_len -
137                                                         (csn_ptr - cookie->octet_str[0].bv_val) -
138                                                         (sizeof("csn=") - 1);
139                 }
140                 ctxcsn = ber_str2bv( csn_str + (sizeof("csn=")-1),
141                                                          csn_str_len, 1, NULL );
142                 ch_free( csn_str );
143                 ber_bvarray_add( &cookie->ctxcsn, ctxcsn );
144                 ch_free( ctxcsn );
145         } else {
146                 cookie->ctxcsn = NULL;
147         }
148
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 ) {
153                         *cval = '\0';
154                 }
155                 cookie->sid = atoi( sid_str + sizeof("sid=") - 1 );
156                 ch_free( sid_str );
157         } else {
158                 cookie->sid = -1;
159         }
160
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 ) {
165                         *cval = '\0';
166                 }
167                 cookie->rid = atoi( rid_str + sizeof("rid=") - 1 );
168                 ch_free( rid_str );
169         } else {
170                 cookie->rid = -1;
171         }
172         return 0;
173 }
174
175 int
176 slap_init_sync_cookie_ctxcsn(
177         struct sync_cookie *cookie
178 )
179 {
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;
185
186         if ( cookie == NULL )
187                 return -1;
188
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 );
197
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 );
202
203         return 0;
204 }
205
206 struct sync_cookie *
207 slap_dup_sync_cookie(
208         struct sync_cookie *dst,
209         struct sync_cookie *src
210 )
211 {
212         int i;
213         struct sync_cookie *new;
214         struct berval tmp_bv;
215
216         if ( src == NULL )
217                 return NULL;
218
219         if ( dst ) {
220                 ber_bvarray_free( dst->ctxcsn );
221                 ber_bvarray_free( dst->octet_str );
222                 new = dst;
223         } else {
224                 new = ( struct sync_cookie * )
225                                 ch_calloc( 1, sizeof( struct sync_cookie ));
226         }
227
228         new->sid = src->sid;
229         new->rid = src->rid;
230
231         if ( src->ctxcsn ) {
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 );
235                 }
236         }
237
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 );
242                 }
243         }
244
245         return new;
246 }
247