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