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