]> git.sur5r.net Git - openldap/blob - servers/slapd/ldapsync.c
Fallout from ITS#4986 - remove unused param of select_backend()
[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 ||
184                                 next > end ||
185                                 ( *next && *next != ',' ) )
186                         {
187                                 return -1;
188                         }
189                         if ( *next == ',' ) {
190                                 next++;
191                         }
192                         if ( !ad ) {
193                                 break;
194                         }
195                         continue;
196                 }
197                 if ( !strncmp( next, "sid=", STRLENOF("sid=") )) {
198                         rid_ptr = next;
199                         cookie->sid = strtoul( &rid_ptr[ STRLENOF( "sid=" ) ], &next, 16 );
200                         if ( next == rid_ptr ||
201                                 next > end ||
202                                 ( *next && *next != ',' ) )
203                         {
204                                 return -1;
205                         }
206                         if ( *next == ',' ) {
207                                 next++;
208                         }
209                         continue;
210                 }
211                 if ( !strncmp( next, "csn=", STRLENOF("csn=") )) {
212                         slap_syntax_validate_func *validate;
213                         struct berval stamp;
214
215                         next += STRLENOF("csn=");
216                         while ( next < end ) {
217                                 csn_str = next;
218                                 /* FIXME use csnValidate when it gets implemented */
219                                 csn_ptr = strchr( csn_str, '#' );
220                                 if ( !csn_ptr || csn_ptr > end )
221                                         break;
222                                 /* ad will be NULL when called from main. we just
223                                  * want to parse the rid then. But we still iterate
224                                  * through the string to find the end.
225                                  */
226                                 if ( ad ) {
227                                         stamp.bv_val = csn_str;
228                                         stamp.bv_len = csn_ptr - csn_str;
229                                         validate = ad->ad_type->sat_syntax->ssyn_validate;
230                                         if ( validate( ad->ad_type->sat_syntax, &stamp )
231                                                 != LDAP_SUCCESS )
232                                                 break;
233                                 }
234                                 cval = strchr( csn_ptr, ';' );
235                                 if ( !cval )
236                                         cval = strchr(csn_ptr, ',' );
237                                 if ( cval )
238                                         stamp.bv_len = cval - csn_str;
239                                 else
240                                         stamp.bv_len = end - csn_str;
241                                 if ( ad ) {
242                                         struct berval bv;
243                                         ber_dupbv_x( &bv, &stamp, memctx );
244                                         ber_bvarray_add_x( &cookie->ctxcsn, &bv, memctx );
245                                         cookie->numcsns++;
246                                 }
247                                 if ( cval ) {
248                                         next = cval + 1;
249                                         if ( *cval != ';' )
250                                                 break;
251                                 } else {
252                                         next = end;
253                                         break;
254                                 }
255                         }
256                         continue;
257                 }
258                 next++;
259         }
260         if ( cookie->numcsns ) {
261                 cookie->sids = slap_parse_csn_sids( cookie->ctxcsn, cookie->numcsns,
262                         memctx );
263         }
264         return 0;
265 }
266
267 int
268 slap_init_sync_cookie_ctxcsn(
269         struct sync_cookie *cookie
270 )
271 {
272         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE + 4 ];
273         struct berval octet_str = BER_BVNULL;
274         struct berval ctxcsn = BER_BVNULL;
275
276         if ( cookie == NULL )
277                 return -1;
278
279         octet_str.bv_len = snprintf( csnbuf, LDAP_LUTIL_CSNSTR_BUFSIZE + 4,
280                                         "csn=%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
281                                         1900, 1, 1, 0, 0, 0, 0, 0, 0 );
282         octet_str.bv_val = csnbuf;
283         ch_free( cookie->octet_str.bv_val );
284         ber_dupbv( &cookie->octet_str, &octet_str );
285
286         ctxcsn.bv_val = octet_str.bv_val + 4;
287         ctxcsn.bv_len = octet_str.bv_len - 4;
288         cookie->ctxcsn = NULL;
289         value_add_one( &cookie->ctxcsn, &ctxcsn );
290         cookie->numcsns = 1;
291         cookie->sid = -1;
292
293         return 0;
294 }
295
296 struct sync_cookie *
297 slap_dup_sync_cookie(
298         struct sync_cookie *dst,
299         struct sync_cookie *src
300 )
301 {
302         struct sync_cookie *new;
303         int i;
304
305         if ( src == NULL )
306                 return NULL;
307
308         if ( dst ) {
309                 ber_bvarray_free( dst->ctxcsn );
310                 dst->ctxcsn = NULL;
311                 dst->sids = NULL;
312                 ch_free( dst->octet_str.bv_val );
313                 BER_BVZERO( &dst->octet_str );
314                 new = dst;
315         } else {
316                 new = ( struct sync_cookie * )
317                                 ch_calloc( 1, sizeof( struct sync_cookie ));
318         }
319
320         new->rid = src->rid;
321         new->sid = src->sid;
322         new->numcsns = src->numcsns;
323
324         if ( src->numcsns ) {
325                 if ( ber_bvarray_dup_x( &new->ctxcsn, src->ctxcsn, NULL )) {
326                         if ( !dst ) {
327                                 ch_free( new );
328                         }
329                         return NULL;
330                 }
331                 new->sids = ch_malloc( src->numcsns * sizeof(int) );
332                 for (i=0; i<src->numcsns; i++)
333                         new->sids[i] = src->sids[i];
334         }
335
336         if ( !BER_BVISNULL( &src->octet_str )) {
337                 ber_dupbv( &new->octet_str, &src->octet_str );
338         }
339
340         return new;
341 }
342