]> git.sur5r.net Git - openldap/blob - servers/slapd/ldapsync.c
Cleanup
[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_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
101         if ( cookie == NULL )
102                 return -1;
103
104         cookie->rid = -1;
105         if (( rid_ptr = strstr( cookie->octet_str.bv_val, "rid=" )) != NULL ) {
106                 if ( (cval = strchr( rid_ptr, ',' )) != NULL ) {
107                         *cval = '\0';
108                 }
109                 cookie->rid = atoi( rid_ptr + sizeof("rid=") - 1 );
110                 if ( cval != NULL ) {
111                         *cval = ',';
112                 }
113         } else {
114                 return -1;
115         }
116
117         while (( csn_ptr = strstr( cookie->octet_str.bv_val, "csn=" )) != NULL ) {
118                 AttributeDescription *ad = slap_schema.si_ad_modifyTimestamp;
119                 slap_syntax_validate_func *validate;
120                 struct berval stamp;
121
122                 /* This only happens when called from main */
123                 if ( ad == NULL )
124                         break;
125
126                 csn_str = csn_ptr + STRLENOF("csn=");
127                 cval = strchr( csn_str, ',' );
128                 if ( cval )
129                         csn_str_len = cval - csn_str;
130                 else
131                         csn_str_len = 0;
132
133                 /* FIXME use csnValidate when it gets implemented */
134                 csn_ptr = strchr( csn_str, '#' );
135                 if ( !csn_ptr ) break;
136
137                 stamp.bv_val = csn_str;
138                 stamp.bv_len = csn_ptr - csn_str;
139                 validate = ad->ad_type->sat_syntax->ssyn_validate;
140                 if ( validate( ad->ad_type->sat_syntax, &stamp ) != LDAP_SUCCESS )
141                         break;
142                 valid = 1;
143                 break;
144         }
145         if ( valid ) {
146                 ber_str2bv_x( csn_str, csn_str_len, 1, &cookie->ctxcsn, memctx );
147         } else {
148                 BER_BVZERO( &cookie->ctxcsn );
149         }
150
151         return 0;
152 }
153
154 int
155 slap_init_sync_cookie_ctxcsn(
156         struct sync_cookie *cookie
157 )
158 {
159         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE + 4 ];
160         struct berval octet_str = BER_BVNULL;
161         struct berval ctxcsn = BER_BVNULL;
162
163         if ( cookie == NULL )
164                 return -1;
165
166         octet_str.bv_len = snprintf( csnbuf, LDAP_LUTIL_CSNSTR_BUFSIZE + 4,
167                                         "csn=%4d%02d%02d%02d%02d%02dZ#%06x#%02x#%06x",
168                                         1900, 1, 1, 0, 0, 0, 0, 0, 0 );
169         octet_str.bv_val = csnbuf;
170         ch_free( cookie->octet_str.bv_val );
171         ber_dupbv( &cookie->octet_str, &octet_str );
172
173         ctxcsn.bv_val = octet_str.bv_val + 4;
174         ctxcsn.bv_len = octet_str.bv_len - 4;
175         ber_dupbv( &cookie->ctxcsn, &ctxcsn );
176
177         return 0;
178 }
179
180 struct sync_cookie *
181 slap_dup_sync_cookie(
182         struct sync_cookie *dst,
183         struct sync_cookie *src
184 )
185 {
186         struct sync_cookie *new;
187
188         if ( src == NULL )
189                 return NULL;
190
191         if ( dst ) {
192                 ch_free( dst->ctxcsn.bv_val );
193                 ch_free( dst->octet_str.bv_val );
194                 BER_BVZERO( &dst->ctxcsn );
195                 BER_BVZERO( &dst->octet_str );
196                 new = dst;
197         } else {
198                 new = ( struct sync_cookie * )
199                                 ch_calloc( 1, sizeof( struct sync_cookie ));
200         }
201
202         new->rid = src->rid;
203
204         if ( !BER_BVISNULL( &src->ctxcsn )) {
205                 ber_dupbv( &new->ctxcsn, &src->ctxcsn );
206         }
207
208         if ( !BER_BVISNULL( &src->octet_str )) {
209                 ber_dupbv( &new->octet_str, &src->octet_str );
210         }
211
212         return new;
213 }
214