]> git.sur5r.net Git - openldap/blob - servers/slapd/ldapsync.c
4e96b22539ca2a95e845f7e10f7f15c265bc1e82
[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 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( cookiestr, strlen(cookiestr), 1, cookie );
59 }
60
61 void
62 slap_sync_cookie_free(
63         struct sync_cookie *cookie,
64         int free_cookie
65 )
66 {
67         if ( cookie == NULL )
68                 return;
69
70         if ( !BER_BVISNULL( &cookie->ctxcsn )) {
71                 ch_free( cookie->ctxcsn.bv_val );
72                 BER_BVZERO( &cookie->ctxcsn );
73         }
74
75         if ( !BER_BVISNULL( &cookie->octet_str )) {
76                 ch_free( cookie->octet_str.bv_val );
77                 BER_BVZERO( &cookie->octet_str );
78         }
79
80         if ( free_cookie ) {
81                 ch_free( cookie );
82         }
83
84         return;
85 }
86
87 int
88 slap_parse_sync_cookie(
89         struct sync_cookie *cookie
90 )
91 {
92         char *csn_ptr;
93         char *csn_str;
94         int csn_str_len;
95         int valid = 0;
96         char *rid_ptr;
97         char *rid_str;
98         char *cval;
99
100         if ( cookie == NULL )
101                 return -1;
102
103         while (( csn_ptr = strstr( cookie->octet_str.bv_val, "csn=" )) != NULL ) {
104                 AttributeDescription *ad = slap_schema.si_ad_modifyTimestamp;
105                 slap_syntax_validate_func *validate;
106                 struct berval stamp;
107
108                 csn_str = csn_ptr + STRLENOF("csn=");
109                 cval = strchr( csn_str, ',' );
110                 if ( cval )
111                         csn_str_len = cval - csn_str;
112                 else
113                         csn_str_len = 0;
114
115                 /* FIXME use csnValidate when it gets implemented */
116                 csn_ptr = strchr( csn_str, '#' );
117                 if ( !csn_ptr ) break;
118
119                 stamp.bv_val = csn_str;
120                 stamp.bv_len = csn_ptr - csn_str;
121                 validate = ad->ad_type->sat_syntax->ssyn_validate;
122                 if ( validate( ad->ad_type->sat_syntax, &stamp ) != LDAP_SUCCESS )
123                         break;
124                 valid = 1;
125                 break;
126         }
127         if ( valid ) {
128                 ber_str2bv( csn_str, csn_str_len, 1, &cookie->ctxcsn );
129         } else {
130                 BER_BVZERO( &cookie->ctxcsn );
131         }
132
133         if (( rid_ptr = strstr( cookie->octet_str.bv_val, "rid=" )) != NULL ) {
134                 rid_str = SLAP_STRNDUP( rid_ptr,
135                                                         SLAP_SYNC_RID_SIZE + sizeof("rid=") - 1 );
136                 if ( (cval = strchr( rid_str, ',' )) != NULL ) {
137                         *cval = '\0';
138                 }
139                 cookie->rid = atoi( rid_str + sizeof("rid=") - 1 );
140                 ch_free( rid_str );
141         } else {
142                 cookie->rid = -1;
143         }
144         return 0;
145 }
146
147 int
148 slap_init_sync_cookie_ctxcsn(
149         struct sync_cookie *cookie
150 )
151 {
152         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE + 4 ];
153         struct berval octet_str = BER_BVNULL;
154         struct berval ctxcsn = BER_BVNULL;
155
156         if ( cookie == NULL )
157                 return -1;
158
159         octet_str.bv_len = snprintf( csnbuf, LDAP_LUTIL_CSNSTR_BUFSIZE + 4,
160                                         "csn=%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
161                                         1900, 1, 1, 0, 0, 0, 0, 0, 0 );
162         octet_str.bv_val = csnbuf;
163         ch_free( cookie->octet_str.bv_val );
164         ber_dupbv( &cookie->octet_str, &octet_str );
165
166         ctxcsn.bv_val = octet_str.bv_val + 4;
167         ctxcsn.bv_len = octet_str.bv_len - 4;
168         ber_dupbv( &cookie->ctxcsn, &ctxcsn );
169
170         return 0;
171 }
172
173 struct sync_cookie *
174 slap_dup_sync_cookie(
175         struct sync_cookie *dst,
176         struct sync_cookie *src
177 )
178 {
179         struct sync_cookie *new;
180
181         if ( src == NULL )
182                 return NULL;
183
184         if ( dst ) {
185                 ch_free( dst->ctxcsn.bv_val );
186                 ch_free( dst->octet_str.bv_val );
187                 BER_BVZERO( &dst->ctxcsn );
188                 BER_BVZERO( &dst->octet_str );
189                 new = dst;
190         } else {
191                 new = ( struct sync_cookie * )
192                                 ch_calloc( 1, sizeof( struct sync_cookie ));
193         }
194
195         new->rid = src->rid;
196
197         if ( !BER_BVISNULL( &src->ctxcsn )) {
198                 ber_dupbv( &new->ctxcsn, &src->ctxcsn );
199         }
200
201         if ( !BER_BVISNULL( &src->octet_str )) {
202                 ber_dupbv( &new->octet_str, &src->octet_str );
203         }
204
205         return new;
206 }
207