]> git.sur5r.net Git - openldap/blob - servers/slapd/ldapsync.c
652622d0c9f6faa6df18b479a2972fe31e6f4ff0
[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-2007 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 struct slap_sync_cookie_s slap_sync_cookie =
31         LDAP_STAILQ_HEAD_INITIALIZER( slap_sync_cookie );
32
33 void
34 slap_compose_sync_cookie(
35         Operation *op,
36         struct berval *cookie,
37         BerVarray csn,
38         int rid )
39 {
40         int len, numcsn = 0;
41
42         if ( csn ) {
43                 for (; !BER_BVISEMPTY( &csn[numcsn] ); numcsn++);
44         }
45
46         if ( numcsn == 0 || rid == -1 ) {
47                 char cookiestr[ LDAP_LUTIL_CSNSTR_BUFSIZE + 20 ];
48                 if ( rid == -1 ) {
49                         cookiestr[0] = '\0';
50                         len = 0;
51                 } else {
52                         len = snprintf( cookiestr, sizeof( cookiestr ),
53                                         "rid=%03d", rid );
54                 }
55                 ber_str2bv_x( cookiestr, len, 1, cookie, 
56                         op ? op->o_tmpmemctx : NULL );
57         } else {
58                 char *ptr;
59                 int i;
60
61                 len = 0;
62                 for ( i=0; i<numcsn; i++)
63                         len += csn[i].bv_len + 1;
64
65                 len += STRLENOF("rid=123,csn=");
66                 cookie->bv_val = slap_sl_malloc( len, op ? op->o_tmpmemctx : NULL );
67
68                 len = sprintf( cookie->bv_val, "rid=%03d,csn=", rid );
69                 ptr = cookie->bv_val + len;
70                 for ( i=0; i<numcsn; i++) {
71                         ptr = lutil_strncopy( ptr, csn->bv_val, csn->bv_len );
72                         *ptr++ = ';';
73                 }
74                 ptr--;
75                 *ptr = '\0';
76                 cookie->bv_len = ptr - cookie->bv_val;
77         }
78 }
79
80 void
81 slap_sync_cookie_free(
82         struct sync_cookie *cookie,
83         int free_cookie
84 )
85 {
86         if ( cookie == NULL )
87                 return;
88
89         if ( cookie->sids ) {
90                 ch_free( cookie->sids );
91                 cookie->sids = NULL;
92         }
93
94         if ( cookie->ctxcsn ) {
95                 ber_bvarray_free( cookie->ctxcsn );
96                 cookie->ctxcsn = NULL;
97         }
98         cookie->numcsns = 0;
99         if ( !BER_BVISNULL( &cookie->octet_str )) {
100                 ch_free( cookie->octet_str.bv_val );
101                 BER_BVZERO( &cookie->octet_str );
102         }
103
104         if ( free_cookie ) {
105                 ch_free( cookie );
106         }
107
108         return;
109 }
110
111 int
112 slap_parse_csn_sid( struct berval *csn )
113 {
114         char *p, *q;
115         int i;
116
117         p = memchr( csn->bv_val, '#', csn->bv_len );
118         if ( p )
119                 p = strchr( p+1, '#' );
120         if ( !p )
121                 return -1;
122         p++;
123         i = strtoul( p, &q, 10 );
124         if ( p == q || i > SLAP_SYNC_SID_MAX )
125                 i = -1;
126         return i;
127 }
128
129 int *
130 slap_parse_csn_sids( BerVarray csns, int numcsns )
131 {
132         int i, *ret;
133         char *p, *q;
134
135         ret = ch_malloc( numcsns * sizeof(int) );
136         for ( i=0; i<numcsns; i++ ) {
137                 ret[i] = slap_parse_csn_sid( &csns[i] );
138         }
139         return ret;
140 }
141
142 int
143 slap_parse_sync_cookie(
144         struct sync_cookie *cookie,
145         void *memctx
146 )
147 {
148         char *csn_ptr;
149         char *csn_str;
150         int csn_str_len;
151         char *rid_ptr;
152         char *cval;
153         char *next, *end;
154         AttributeDescription *ad = slap_schema.si_ad_modifyTimestamp;
155
156         if ( cookie == NULL )
157                 return -1;
158
159         if ( cookie->octet_str.bv_len <= STRLENOF( "rid=" ) )
160                 return -1;
161
162         cookie->rid = -1;
163         cookie->ctxcsn = NULL;
164         cookie->sids = NULL;
165         cookie->numcsns = 0;
166
167         end = cookie->octet_str.bv_val + cookie->octet_str.bv_len;
168
169         for ( next=cookie->octet_str.bv_val; next < end; ) {
170                 if ( !strncmp( next, "rid=", STRLENOF("rid=") )) {
171                         rid_ptr = next;
172                         cookie->rid = strtoul( &rid_ptr[ STRLENOF( "rid=" ) ], &next, 10 );
173                         if ( next == rid_ptr || next > end || *next != ',' ) {
174                                 return -1;
175                         }
176                         if ( *next == ',' ) {
177                                 next++;
178                         }
179                         if ( !ad ) {
180                                 break;
181                         }
182                         continue;
183                 }
184                 if ( !strncmp( next, "csn=", STRLENOF("csn=") )) {
185                         slap_syntax_validate_func *validate;
186                         struct berval stamp;
187
188                         csn_str = next + STRLENOF("csn=");
189                         while ( next < end ) {
190                                 /* FIXME use csnValidate when it gets implemented */
191                                 csn_ptr = strchr( csn_str, '#' );
192                                 if ( !csn_ptr || csn_ptr > end )
193                                         break;
194                                 /* ad will be NULL when called from main. we just
195                                  * want to parse the rid then. But we still iterate
196                                  * through the string to find the end.
197                                  */
198                                 if ( ad ) {
199                                         stamp.bv_val = csn_str;
200                                         stamp.bv_len = csn_ptr - csn_str;
201                                         validate = ad->ad_type->sat_syntax->ssyn_validate;
202                                         if ( validate( ad->ad_type->sat_syntax, &stamp )
203                                                 != LDAP_SUCCESS )
204                                                 break;
205                                 }
206                                 cval = strchr( csn_ptr, ';' );
207                                 if ( !cval )
208                                         cval = strchr(csn_ptr, ',' );
209                                 if ( cval )
210                                         stamp.bv_len = cval - csn_str;
211                                 else
212                                         stamp.bv_len = end - csn_str;
213                                 if ( ad ) {
214                                         value_add_one( &cookie->ctxcsn, &stamp );
215                                         cookie->numcsns++;
216                                 }
217                                 if ( cval ) {
218                                         next = cval + 1;
219                                         if ( *cval != ';' )
220                                                 break;
221                                 } else {
222                                         next = end;
223                                         break;
224                                 }
225                         }
226                         continue;
227                 }
228                 next++;
229         }
230         if ( cookie->numcsns ) {
231                 cookie->sids = slap_parse_csn_sids( cookie->ctxcsn, cookie->numcsns );
232         }
233         return 0;
234 }
235
236 int
237 slap_init_sync_cookie_ctxcsn(
238         struct sync_cookie *cookie
239 )
240 {
241         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE + 4 ];
242         struct berval octet_str = BER_BVNULL;
243         struct berval ctxcsn = BER_BVNULL;
244
245         if ( cookie == NULL )
246                 return -1;
247
248         octet_str.bv_len = snprintf( csnbuf, LDAP_LUTIL_CSNSTR_BUFSIZE + 4,
249                                         "csn=%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
250                                         1900, 1, 1, 0, 0, 0, 0, 0, 0 );
251         octet_str.bv_val = csnbuf;
252         ch_free( cookie->octet_str.bv_val );
253         ber_dupbv( &cookie->octet_str, &octet_str );
254
255         ctxcsn.bv_val = octet_str.bv_val + 4;
256         ctxcsn.bv_len = octet_str.bv_len - 4;
257         cookie->ctxcsn = NULL;
258         value_add_one( &cookie->ctxcsn, &ctxcsn );
259         cookie->numcsns = 1;
260
261         return 0;
262 }
263
264 struct sync_cookie *
265 slap_dup_sync_cookie(
266         struct sync_cookie *dst,
267         struct sync_cookie *src
268 )
269 {
270         struct sync_cookie *new;
271         int i;
272
273         if ( src == NULL )
274                 return NULL;
275
276         if ( dst ) {
277                 ber_bvarray_free( dst->ctxcsn );
278                 dst->ctxcsn = NULL;
279                 dst->sids = NULL;
280                 ch_free( dst->octet_str.bv_val );
281                 BER_BVZERO( &dst->octet_str );
282                 new = dst;
283         } else {
284                 new = ( struct sync_cookie * )
285                                 ch_calloc( 1, sizeof( struct sync_cookie ));
286         }
287
288         new->rid = src->rid;
289         new->numcsns = src->numcsns;
290
291         if ( src->numcsns ) {
292                 if ( ber_bvarray_dup_x( &new->ctxcsn, src->ctxcsn, NULL )) {
293                         if ( !dst ) {
294                                 ch_free( new );
295                         }
296                         return NULL;
297                 }
298                 new->sids = ch_malloc( src->numcsns * sizeof(int) );
299                 for (i=0; i<src->numcsns; i++)
300                         new->sids[i] = src->sids[i];
301         }
302
303         if ( !BER_BVISNULL( &src->octet_str )) {
304                 ber_dupbv( &new->octet_str, &src->octet_str );
305         }
306
307         return new;
308 }
309