]> git.sur5r.net Git - openldap/blob - servers/slapd/ldapsync.c
parse unsigned as unsigned (ITS#5122)
[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 *csnp )
124 {
125         char *p, *q;
126         struct berval csn = *csnp;
127         int i;
128
129         p = ber_bvchr( &csn, '#' );
130         if ( !p )
131                 return -1;
132         p++;
133         csn.bv_len -= p - csn.bv_val;
134         csn.bv_val = p;
135
136         p = ber_bvchr( &csn, '#' );
137         if ( !p )
138                 return -1;
139         p++;
140         csn.bv_len -= p - csn.bv_val;
141         csn.bv_val = p;
142
143         q = ber_bvchr( &csn, '#' );
144         if ( !q )
145                 return -1;
146
147         csn.bv_len = q - p;
148
149         i = (int)strtoul( p, &q, 16 );
150         if ( p == q || q != p + csn.bv_len || i > SLAP_SYNC_SID_MAX ) {
151                 i = -1;
152         }
153
154         return i;
155 }
156
157 int *
158 slap_parse_csn_sids( BerVarray csns, int numcsns, void *memctx )
159 {
160         int i, *ret;
161
162         ret = slap_sl_malloc( numcsns * sizeof(int), memctx );
163         for ( i=0; i<numcsns; i++ ) {
164                 ret[i] = slap_parse_csn_sid( &csns[i] );
165         }
166         return ret;
167 }
168
169 int
170 slap_parse_sync_cookie(
171         struct sync_cookie *cookie,
172         void *memctx
173 )
174 {
175         char *csn_ptr;
176         char *csn_str;
177         char *rid_ptr;
178         char *cval;
179         char *next, *end;
180         AttributeDescription *ad = slap_schema.si_ad_modifyTimestamp;
181
182         if ( cookie == NULL )
183                 return -1;
184
185         if ( cookie->octet_str.bv_len <= STRLENOF( "rid=" ) )
186                 return -1;
187
188         cookie->rid = -1;
189         cookie->sid = -1;
190         cookie->ctxcsn = NULL;
191         cookie->sids = NULL;
192         cookie->numcsns = 0;
193
194         end = cookie->octet_str.bv_val + cookie->octet_str.bv_len;
195
196         for ( next=cookie->octet_str.bv_val; next < end; ) {
197                 if ( !strncmp( next, "rid=", STRLENOF("rid=") )) {
198                         rid_ptr = next;
199                         cookie->rid = strtoul( &rid_ptr[ STRLENOF( "rid=" ) ], &next, 10 );
200                         if ( next == rid_ptr ||
201                                 next > end ||
202                                 ( *next && *next != ',' ) )
203                         {
204                                 return -1;
205                         }
206                         if ( *next == ',' ) {
207                                 next++;
208                         }
209                         if ( !ad ) {
210                                 break;
211                         }
212                         continue;
213                 }
214                 if ( !strncmp( next, "sid=", STRLENOF("sid=") )) {
215                         rid_ptr = next;
216                         cookie->sid = strtoul( &rid_ptr[ STRLENOF( "sid=" ) ], &next, 16 );
217                         if ( next == rid_ptr ||
218                                 next > end ||
219                                 ( *next && *next != ',' ) )
220                         {
221                                 return -1;
222                         }
223                         if ( *next == ',' ) {
224                                 next++;
225                         }
226                         continue;
227                 }
228                 if ( !strncmp( next, "csn=", STRLENOF("csn=") )) {
229                         slap_syntax_validate_func *validate;
230                         struct berval stamp;
231
232                         next += STRLENOF("csn=");
233                         while ( next < end ) {
234                                 csn_str = next;
235                                 /* FIXME use csnValidate when it gets implemented */
236                                 csn_ptr = strchr( csn_str, '#' );
237                                 if ( !csn_ptr || csn_ptr > end )
238                                         break;
239                                 /* ad will be NULL when called from main. we just
240                                  * want to parse the rid then. But we still iterate
241                                  * through the string to find the end.
242                                  */
243                                 if ( ad ) {
244                                         stamp.bv_val = csn_str;
245                                         stamp.bv_len = csn_ptr - csn_str;
246                                         validate = ad->ad_type->sat_syntax->ssyn_validate;
247                                         if ( validate( ad->ad_type->sat_syntax, &stamp )
248                                                 != LDAP_SUCCESS )
249                                                 break;
250                                 }
251                                 cval = strchr( csn_ptr, ';' );
252                                 if ( !cval )
253                                         cval = strchr(csn_ptr, ',' );
254                                 if ( cval )
255                                         stamp.bv_len = cval - csn_str;
256                                 else
257                                         stamp.bv_len = end - csn_str;
258                                 if ( ad ) {
259                                         struct berval bv;
260                                         ber_dupbv_x( &bv, &stamp, memctx );
261                                         ber_bvarray_add_x( &cookie->ctxcsn, &bv, memctx );
262                                         cookie->numcsns++;
263                                 }
264                                 if ( cval ) {
265                                         next = cval + 1;
266                                         if ( *cval != ';' )
267                                                 break;
268                                 } else {
269                                         next = end;
270                                         break;
271                                 }
272                         }
273                         continue;
274                 }
275                 next++;
276         }
277         if ( cookie->numcsns ) {
278                 cookie->sids = slap_parse_csn_sids( cookie->ctxcsn, cookie->numcsns,
279                         memctx );
280         }
281         return 0;
282 }
283
284 int
285 slap_init_sync_cookie_ctxcsn(
286         struct sync_cookie *cookie
287 )
288 {
289         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE + 4 ];
290         struct berval octet_str = BER_BVNULL;
291         struct berval ctxcsn = BER_BVNULL;
292
293         if ( cookie == NULL )
294                 return -1;
295
296         octet_str.bv_len = snprintf( csnbuf, LDAP_LUTIL_CSNSTR_BUFSIZE + 4,
297                                         "csn=%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
298                                         1900, 1, 1, 0, 0, 0, 0, 0, 0 );
299         octet_str.bv_val = csnbuf;
300         ch_free( cookie->octet_str.bv_val );
301         ber_dupbv( &cookie->octet_str, &octet_str );
302
303         ctxcsn.bv_val = octet_str.bv_val + 4;
304         ctxcsn.bv_len = octet_str.bv_len - 4;
305         cookie->ctxcsn = NULL;
306         value_add_one( &cookie->ctxcsn, &ctxcsn );
307         cookie->numcsns = 1;
308         cookie->sid = -1;
309
310         return 0;
311 }
312
313 struct sync_cookie *
314 slap_dup_sync_cookie(
315         struct sync_cookie *dst,
316         struct sync_cookie *src
317 )
318 {
319         struct sync_cookie *new;
320         int i;
321
322         if ( src == NULL )
323                 return NULL;
324
325         if ( dst ) {
326                 ber_bvarray_free( dst->ctxcsn );
327                 dst->ctxcsn = NULL;
328                 dst->sids = NULL;
329                 ch_free( dst->octet_str.bv_val );
330                 BER_BVZERO( &dst->octet_str );
331                 new = dst;
332         } else {
333                 new = ( struct sync_cookie * )
334                                 ch_calloc( 1, sizeof( struct sync_cookie ));
335         }
336
337         new->rid = src->rid;
338         new->sid = src->sid;
339         new->numcsns = src->numcsns;
340
341         if ( src->numcsns ) {
342                 if ( ber_bvarray_dup_x( &new->ctxcsn, src->ctxcsn, NULL )) {
343                         if ( !dst ) {
344                                 ch_free( new );
345                         }
346                         return NULL;
347                 }
348                 new->sids = ch_malloc( src->numcsns * sizeof(int) );
349                 for (i=0; i<src->numcsns; i++)
350                         new->sids[i] = src->sids[i];
351         }
352
353         if ( !BER_BVISNULL( &src->octet_str )) {
354                 ber_dupbv( &new->octet_str, &src->octet_str );
355         }
356
357         return new;
358 }
359