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