]> git.sur5r.net Git - openldap/blob - servers/slapd/ldapsync.c
large X (for version.sh)
[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-2008 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         struct berval *csn,
38         int rid )
39 {
40         char cookiestr[ LDAP_LUTIL_CSNSTR_BUFSIZE + 20 ];
41         int len;
42
43         if ( BER_BVISNULL( csn )) {
44                 if ( rid == -1 ) {
45                         cookiestr[0] = '\0';
46                         len = 0;
47                 } else {
48                         len = snprintf( cookiestr, LDAP_LUTIL_CSNSTR_BUFSIZE + 20,
49                                         "rid=%03d", rid );
50                 }
51         } else {
52                 char *end = cookiestr + sizeof(cookiestr);
53                 char *ptr = lutil_strcopy( cookiestr, "csn=" );
54                 len = csn->bv_len;
55                 if ( ptr + len >= end )
56                         len = end - ptr;
57                 ptr = lutil_strncopy( ptr, csn->bv_val, len );
58                 if ( rid != -1 && ptr < end - STRLENOF(",rid=xxx") ) {
59                         ptr += sprintf( ptr, ",rid=%03d", rid );
60                 }
61                 len = ptr - cookiestr;
62         }
63         ber_str2bv_x( cookiestr, len, 1, cookie,
64                 op ? op->o_tmpmemctx : NULL );
65 }
66
67 void
68 slap_sync_cookie_free(
69         struct sync_cookie *cookie,
70         int free_cookie
71 )
72 {
73         if ( cookie == NULL )
74                 return;
75
76         if ( !BER_BVISNULL( &cookie->ctxcsn )) {
77                 ch_free( cookie->ctxcsn.bv_val );
78                 BER_BVZERO( &cookie->ctxcsn );
79         }
80
81         if ( !BER_BVISNULL( &cookie->octet_str )) {
82                 ch_free( cookie->octet_str.bv_val );
83                 BER_BVZERO( &cookie->octet_str );
84         }
85
86         if ( free_cookie ) {
87                 ch_free( cookie );
88         }
89
90         return;
91 }
92
93 int
94 slap_parse_sync_cookie(
95         struct sync_cookie *cookie,
96         void *memctx
97 )
98 {
99         char *csn_ptr;
100         char *csn_str;
101         int csn_str_len;
102         int valid = 0;
103         char *rid_ptr;
104         char *cval;
105         char *next;
106
107         if ( cookie == NULL )
108                 return -1;
109
110         if ( cookie->octet_str.bv_len <= STRLENOF( "rid=" ) )
111                 return -1;
112
113         cookie->rid = -1;
114         /* FIXME: may read past end of cookie->octet_str.bv_val */
115         rid_ptr = strstr( cookie->octet_str.bv_val, "rid=" );
116         if ( rid_ptr == NULL 
117                 || rid_ptr > &cookie->octet_str.bv_val[ cookie->octet_str.bv_len - STRLENOF( "rid=" ) ] )
118         {
119                 return -1;
120         }
121
122         if ( rid_ptr[ STRLENOF( "rid=" ) ] == '-' ) {
123                 return -1;
124         }
125
126         cookie->rid = strtoul( &rid_ptr[ STRLENOF( "rid=" ) ], &next, 10 );
127         if ( next == &rid_ptr[ STRLENOF( "rid=" ) ] || ( next[ 0 ] != ',' && next[ 0 ] != '\0' ) ) {
128                 return -1;
129         }
130
131         while (( csn_ptr = strstr( cookie->octet_str.bv_val, "csn=" )) != NULL ) {
132                 AttributeDescription *ad = slap_schema.si_ad_modifyTimestamp;
133                 slap_syntax_validate_func *validate;
134                 struct berval stamp;
135
136                 /* This only happens when called from main */
137                 if ( ad == NULL )
138                         break;
139
140                 if ( csn_ptr >= &cookie->octet_str.bv_val[ cookie->octet_str.bv_len - STRLENOF( "csn=" ) ] ) {
141                         return -1;
142                 }
143
144                 csn_str = csn_ptr + STRLENOF("csn=");
145                 cval = strchr( csn_str, ',' );
146                 if ( cval && cval < &cookie->octet_str.bv_val[ cookie->octet_str.bv_len ] )
147                         csn_str_len = cval - csn_str;
148                 else
149                         csn_str_len = 0;
150
151                 /* FIXME use csnValidate when it gets implemented */
152                 csn_ptr = strchr( csn_str, '#' );
153                 if ( !csn_ptr || csn_str >= &cookie->octet_str.bv_val[ cookie->octet_str.bv_len ] ) break;
154
155                 stamp.bv_val = csn_str;
156                 stamp.bv_len = csn_ptr - csn_str;
157                 validate = ad->ad_type->sat_syntax->ssyn_validate;
158                 if ( validate( ad->ad_type->sat_syntax, &stamp ) != LDAP_SUCCESS )
159                         break;
160                 valid = 1;
161                 break;
162         }
163         if ( valid ) {
164                 ber_str2bv_x( csn_str, csn_str_len, 1, &cookie->ctxcsn, memctx );
165         } else {
166                 BER_BVZERO( &cookie->ctxcsn );
167         }
168
169         return 0;
170 }
171
172 int
173 slap_init_sync_cookie_ctxcsn(
174         struct sync_cookie *cookie
175 )
176 {
177         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE + 4 ];
178         struct berval octet_str = BER_BVNULL;
179         struct berval ctxcsn = BER_BVNULL;
180
181         if ( cookie == NULL )
182                 return -1;
183
184         octet_str.bv_len = snprintf( csnbuf, LDAP_LUTIL_CSNSTR_BUFSIZE + 4,
185                                         "csn=%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
186                                         1900, 1, 1, 0, 0, 0, 0, 0, 0 );
187         octet_str.bv_val = csnbuf;
188         ch_free( cookie->octet_str.bv_val );
189         ber_dupbv( &cookie->octet_str, &octet_str );
190
191         ctxcsn.bv_val = octet_str.bv_val + 4;
192         ctxcsn.bv_len = octet_str.bv_len - 4;
193         ber_dupbv( &cookie->ctxcsn, &ctxcsn );
194
195         return 0;
196 }
197
198 struct sync_cookie *
199 slap_dup_sync_cookie(
200         struct sync_cookie *dst,
201         struct sync_cookie *src
202 )
203 {
204         struct sync_cookie *new;
205
206         if ( src == NULL )
207                 return NULL;
208
209         if ( dst ) {
210                 ch_free( dst->ctxcsn.bv_val );
211                 ch_free( dst->octet_str.bv_val );
212                 BER_BVZERO( &dst->ctxcsn );
213                 BER_BVZERO( &dst->octet_str );
214                 new = dst;
215         } else {
216                 new = ( struct sync_cookie * )
217                                 ch_calloc( 1, sizeof( struct sync_cookie ));
218         }
219
220         new->rid = src->rid;
221
222         if ( !BER_BVISNULL( &src->ctxcsn )) {
223                 ber_dupbv( &new->ctxcsn, &src->ctxcsn );
224         }
225
226         if ( !BER_BVISNULL( &src->octet_str )) {
227                 ber_dupbv( &new->octet_str, &src->octet_str );
228         }
229
230         return new;
231 }
232