1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use std::collections::HashSet;

use crate::controls::{ControlParser, MakeCritical, RawControl};
use crate::ResultEntry;

use bytes::BytesMut;

use lber::common::TagClass;
use lber::parse::{parse_tag, parse_uint};
use lber::structure::{StructureTag, PL};
use lber::structures::{ASNTag, Boolean, Enumerated, OctetString, Sequence, Tag};
use lber::universal::Types;
use lber::{write, IResult};

pub const SYNC_REQUEST_OID: &str = "1.3.6.1.4.1.4203.1.9.1.1";
pub const SYNC_STATE_OID: &str = "1.3.6.1.4.1.4203.1.9.1.2";
pub const SYNC_DONE_OID: &str = "1.3.6.1.4.1.4203.1.9.1.3";
const SYNC_INFO_OID: &str = "1.3.6.1.4.1.4203.1.9.1.4";

/// Sync Request control ([RFC 4533](https://tools.ietf.org/html/rfc4533)).
#[derive(Clone, Debug, Default)]
pub struct SyncRequest {
    pub mode: RefreshMode,
    pub cookie: Option<Vec<u8>>,
    pub reload_hint: bool,
}

/// Content refresh mode.
///
/// See the Content Synchronization specification
/// ([RFC 4533](https://tools.ietf.org/html/rfc4533)).
#[derive(Clone, Debug)]
pub enum RefreshMode {
    RefreshOnly,
    RefreshAndPersist,
}

impl Default for RefreshMode {
    fn default() -> Self {
        RefreshMode::RefreshOnly
    }
}

impl From<RefreshMode> for i64 {
    fn from(mode: RefreshMode) -> i64 {
        match mode {
            RefreshMode::RefreshOnly => 1,
            RefreshMode::RefreshAndPersist => 3,
        }
    }
}

impl MakeCritical for SyncRequest {}

impl From<SyncRequest> for RawControl {
    fn from(sr: SyncRequest) -> RawControl {
        let mut cap_est = 16; // covers sequence, selector and hint if any
        let mut tags = vec![Tag::Enumerated(Enumerated {
            inner: i64::from(sr.mode),
            ..Default::default()
        })];
        if let Some(cookie) = sr.cookie {
            cap_est += cookie.len();
            tags.push(Tag::OctetString(OctetString {
                inner: cookie,
                ..Default::default()
            }));
        }
        if sr.reload_hint {
            tags.push(Tag::Boolean(Boolean {
                inner: sr.reload_hint,
                ..Default::default()
            }));
        }
        let sreq = Tag::Sequence(Sequence {
            inner: tags,
            ..Default::default()
        })
        .into_structure();
        let mut buf = BytesMut::with_capacity(cap_est);
        write::encode_into(&mut buf, sreq).expect("encoded");
        RawControl {
            ctype: SYNC_REQUEST_OID.to_owned(),
            crit: false,
            val: Some(Vec::from(&buf[..])),
        }
    }
}

/// Sync State response control ([RFC 4533](https://tools.ietf.org/html/rfc4533)).
#[derive(Debug)]
pub struct SyncState {
    pub state: EntryState,
    pub entry_uuid: Vec<u8>,
    pub cookie: Option<Vec<u8>>,
}

/// Possible states for the Sync State control.
#[derive(Debug)]
pub enum EntryState {
    Present,
    Add,
    Modify,
    Delete,
}

impl ControlParser for SyncState {
    fn parse(val: &[u8]) -> Self {
        let mut tags = match parse_tag(val) {
            IResult::Ok((_, tag)) => tag,
            _ => panic!("syncstate: failed to parse tag"),
        }
        .expect_constructed()
        .expect("syncstate: elements")
        .into_iter();
        let state = match match parse_uint(
            tags.next()
                .expect("syncstate: element 1")
                .match_class(TagClass::Universal)
                .and_then(|t| t.match_id(Types::Enumerated as u64))
                .and_then(|t| t.expect_primitive())
                .expect("syncstate: state")
                .as_slice(),
        ) {
            Ok((_, state)) => state,
            _ => panic!("syncstate: failed to parse state"),
        } {
            0 => EntryState::Present,
            1 => EntryState::Add,
            2 => EntryState::Modify,
            3 => EntryState::Delete,
            _ => panic!("syncstate: unknown state"),
        };
        let entry_uuid = tags
            .next()
            .expect("syncstate: element 2")
            .expect_primitive()
            .expect("syncstate: entryUUID");
        let cookie = tags
            .next()
            .map(|tag| tag.expect_primitive().expect("syncstate: synCookie"));
        SyncState {
            state,
            entry_uuid,
            cookie,
        }
    }
}

/// Sync Done response control ([RFC 4533](https://tools.ietf.org/html/rfc4533)).
#[derive(Debug)]
pub struct SyncDone {
    pub cookie: Option<Vec<u8>>,
    pub refresh_deletes: bool,
}

impl ControlParser for SyncDone {
    fn parse(val: &[u8]) -> Self {
        let tags = match parse_tag(val) {
            Ok((_, tag)) => tag,
            _ => panic!("syncdone: failed to parse tag"),
        }
        .expect_constructed()
        .expect("syncdone: elements")
        .into_iter();
        let mut cookie = None;
        let mut refresh_deletes = false;
        for tag in tags {
            match tag {
                StructureTag { id, payload, .. } if id == Types::OctetString as u64 => {
                    cookie = Some(match payload {
                        PL::P(ostr) => ostr,
                        PL::C(_) => panic!("syncdone: constructed octet string?"),
                    });
                }
                StructureTag { id, payload, .. } if id == Types::Boolean as u64 => {
                    refresh_deletes = match payload {
                        PL::P(ostr) => ostr[0] != 0,
                        PL::C(_) => panic!("syncdone: constructed boolean?"),
                    };
                }
                _ => panic!("syncdone: unrecognized component"),
            }
        }
        SyncDone {
            cookie,
            refresh_deletes,
        }
    }
}

/// Values of the Sync Info intermediate message ([RFC 4533](https://tools.ietf.org/html/rfc4533)).
#[derive(Clone, Debug)]
pub enum SyncInfo {
    NewCookie(Vec<u8>),
    RefreshDelete {
        cookie: Option<Vec<u8>>,
        refresh_done: bool,
    },
    RefreshPresent {
        cookie: Option<Vec<u8>>,
        refresh_done: bool,
    },
    SyncIdSet {
        cookie: Option<Vec<u8>>,
        refresh_deletes: bool,
        sync_uuids: HashSet<Vec<u8>>,
    },
}

/// Parse the Sync Info value from the Search result entry.
pub fn parse_syncinfo(entry: ResultEntry) -> SyncInfo {
    let mut tags = entry
        .0
        .match_id(25)
        .and_then(|t| t.expect_constructed())
        .expect("intermediate seq")
        .into_iter();
    loop {
        match tags.next() {
            None => panic!("syncinfo: out of tags"),
            Some(tag) if tag.id == 0 => {
                let oid = String::from_utf8(tag.expect_primitive().expect("octet string"))
                    .expect("intermediate oid");
                if oid != SYNC_INFO_OID {
                    panic!("syncinfo: oid mismatch");
                }
            }
            Some(tag) if tag.id == 1 => {
                let syncinfo_val =
                    match parse_tag(tag.expect_primitive().expect("octet string").as_ref()) {
                        Ok((_, tag)) => tag,
                        _ => panic!("syncinfo: error parsing value"),
                    };
                return match syncinfo_val {
                    StructureTag { id, class, payload } if class == TagClass::Context && id < 4 => {
                        match id {
                            0 => {
                                let cookie = match payload {
                                    PL::P(payload) => payload,
                                    PL::C(_) => panic!("syncinfo: [0] not primitive"),
                                };
                                SyncInfo::NewCookie(cookie)
                            }
                            1 | 2 | 3 => {
                                let mut syncinfo_val = match payload {
                                    PL::C(payload) => payload,
                                    PL::P(_) => panic!("syncinfo: [1,2,3] not a sequence"),
                                }
                                .into_iter();
                                let mut sync_cookie = None;
                                let mut flag = id != 3;
                                let mut uuids = HashSet::new();
                                let mut pass = 1;
                                'it: loop {
                                    match syncinfo_val.next() {
                                        None => break 'it,
                                        Some(comp) => match comp {
                                            StructureTag { id, class, .. }
                                                if class == TagClass::Universal
                                                    && id == Types::OctetString as u64
                                                    && pass <= 1 =>
                                            {
                                                sync_cookie = comp.expect_primitive();
                                            }
                                            StructureTag { id, class, .. }
                                                if class == TagClass::Universal
                                                    && id == Types::Boolean as u64
                                                    && pass <= 2 =>
                                            {
                                                flag = comp
                                                    .expect_primitive()
                                                    .expect("octet string")[0]
                                                    != 0;
                                            }
                                            StructureTag { id, class, .. }
                                                if class == TagClass::Universal
                                                    && id == Types::Set as u64
                                                    && pass <= 3 =>
                                            {
                                                uuids = comp
                                                    .expect_constructed()
                                                    .expect("uuid set")
                                                    .into_iter()
                                                    .map(|u| {
                                                        u.expect_primitive().expect("octet string")
                                                    })
                                                    .collect();
                                            }
                                            _ => panic!(),
                                        },
                                    }
                                    pass += 1;
                                }
                                match id {
                                    1 => SyncInfo::RefreshDelete {
                                        cookie: sync_cookie,
                                        refresh_done: flag,
                                    },
                                    2 => SyncInfo::RefreshPresent {
                                        cookie: sync_cookie,
                                        refresh_done: flag,
                                    },
                                    3 => SyncInfo::SyncIdSet {
                                        cookie: sync_cookie,
                                        refresh_deletes: flag,
                                        sync_uuids: uuids,
                                    },
                                    _ => panic!("syncinfo: got id > 3"),
                                }
                            }
                            _ => panic!("syncinfo: got id > 3"),
                        }
                    }
                    _ => panic!("syncinfo: got id > 3"),
                };
            }
            _ => panic!("syncinfo: unrecognized tag"),
        }
    }
}