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