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