yamldap 0.0.8

A lightweight LDAP server that serves directory data from YAML files
Documentation
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use bytes::BytesMut;
use std::collections::HashMap;
use tokio_util::codec::{Decoder, Encoder};

pub type LdapMessageId = u32;

#[derive(Debug, Clone, PartialEq)]
pub struct LdapMessage {
    pub message_id: LdapMessageId,
    pub protocol_op: LdapProtocolOp,
}

#[derive(Debug, Clone, PartialEq)]
pub enum LdapProtocolOp {
    BindRequest {
        version: u8,
        dn: String,
        authentication: BindAuthentication,
    },
    BindResponse {
        result: LdapResult,
    },
    UnbindRequest,
    SearchRequest {
        base_dn: String,
        scope: SearchScope,
        deref_aliases: DerefAliases,
        size_limit: u32,
        time_limit: u32,
        types_only: bool,
        filter: String, // Simplified - store as string for now
        attributes: Vec<String>,
    },
    SearchResultEntry {
        dn: String,
        attributes: HashMap<String, Vec<String>>,
    },
    SearchResultDone {
        result: LdapResult,
    },
    CompareRequest {
        dn: String,
        attribute: String,
        value: String,
    },
    CompareResponse {
        result: LdapResult,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub enum BindAuthentication {
    Simple(String), // password
    Anonymous,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SearchScope {
    BaseObject = 0,
    SingleLevel = 1,
    WholeSubtree = 2,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum DerefAliases {
    NeverDerefAliases = 0,
    DerefInSearching = 1,
    DerefFindingBaseObj = 2,
    DerefAlways = 3,
}

#[derive(Debug, Clone, PartialEq)]
pub struct LdapResult {
    pub result_code: LdapResultCode,
    pub matched_dn: String,
    pub diagnostic_message: String,
}

#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(u8)]
pub enum LdapResultCode {
    Success = 0,
    OperationsError = 1,
    ProtocolError = 2,
    TimeLimitExceeded = 3,
    SizeLimitExceeded = 4,
    CompareFalse = 5,
    CompareTrue = 6,
    AuthMethodNotSupported = 7,
    StrongerAuthRequired = 8,
    NoSuchAttribute = 16,
    UndefinedAttributeType = 17,
    InappropriateMatching = 18,
    ConstraintViolation = 19,
    AttributeOrValueExists = 20,
    InvalidAttributeSyntax = 21,
    NoSuchObject = 32,
    AliasProblem = 33,
    InvalidDNSyntax = 34,
    InvalidCredentials = 49,
    InsufficientAccessRights = 50,
    Busy = 51,
    Unavailable = 52,
    UnwillingToPerform = 53,
    LoopDetect = 54,
    NamingViolation = 64,
    ObjectClassViolation = 65,
    NotAllowedOnNonLeaf = 66,
    NotAllowedOnRDN = 67,
    EntryAlreadyExists = 68,
    ObjectClassModsProhibited = 69,
    Other = 80,
}

impl LdapResult {
    pub fn success() -> Self {
        Self {
            result_code: LdapResultCode::Success,
            matched_dn: String::new(),
            diagnostic_message: String::new(),
        }
    }

    pub fn error(code: LdapResultCode, message: String) -> Self {
        Self {
            result_code: code,
            matched_dn: String::new(),
            diagnostic_message: message,
        }
    }
}

pub struct LdapCodec;

impl Decoder for LdapCodec {
    type Item = LdapMessage;
    type Error = std::io::Error;

    fn decode(&mut self, _src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
        // This is a simplified decoder - in a real implementation,
        // we would use proper ASN.1 BER decoding
        // For now, return None to indicate we need more data
        // The actual implementation would parse LDAP messages here
        Ok(None)
    }
}

impl Encoder<LdapMessage> for LdapCodec {
    type Error = std::io::Error;

    fn encode(&mut self, _item: LdapMessage, dst: &mut BytesMut) -> Result<(), Self::Error> {
        // This is a simplified encoder - in a real implementation,
        // we would use proper ASN.1 BER encoding
        // For now, just write a placeholder
        dst.extend_from_slice(b"LDAP");
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ldap_result_success() {
        let result = LdapResult::success();
        assert_eq!(result.result_code, LdapResultCode::Success);
        assert_eq!(result.matched_dn, "");
        assert_eq!(result.diagnostic_message, "");
    }

    #[test]
    fn test_ldap_result_error() {
        let result = LdapResult::error(
            LdapResultCode::InvalidDNSyntax,
            "Invalid DN format".to_string(),
        );
        assert_eq!(result.result_code, LdapResultCode::InvalidDNSyntax);
        assert_eq!(result.matched_dn, "");
        assert_eq!(result.diagnostic_message, "Invalid DN format");
    }

    #[test]
    fn test_search_scope_values() {
        assert_eq!(SearchScope::BaseObject as u8, 0);
        assert_eq!(SearchScope::SingleLevel as u8, 1);
        assert_eq!(SearchScope::WholeSubtree as u8, 2);
    }

    #[test]
    fn test_deref_aliases_values() {
        assert_eq!(DerefAliases::NeverDerefAliases as u8, 0);
        assert_eq!(DerefAliases::DerefInSearching as u8, 1);
        assert_eq!(DerefAliases::DerefFindingBaseObj as u8, 2);
        assert_eq!(DerefAliases::DerefAlways as u8, 3);
    }

    #[test]
    fn test_ldap_result_code_values() {
        assert_eq!(LdapResultCode::Success as u8, 0);
        assert_eq!(LdapResultCode::OperationsError as u8, 1);
        assert_eq!(LdapResultCode::ProtocolError as u8, 2);
        assert_eq!(LdapResultCode::CompareFalse as u8, 5);
        assert_eq!(LdapResultCode::CompareTrue as u8, 6);
        assert_eq!(LdapResultCode::NoSuchAttribute as u8, 16);
        assert_eq!(LdapResultCode::NoSuchObject as u8, 32);
        assert_eq!(LdapResultCode::InvalidDNSyntax as u8, 34);
        assert_eq!(LdapResultCode::InvalidCredentials as u8, 49);
        assert_eq!(LdapResultCode::InsufficientAccessRights as u8, 50);
        assert_eq!(LdapResultCode::Other as u8, 80);
    }

    #[test]
    fn test_bind_authentication_variants() {
        let simple = BindAuthentication::Simple("password".to_string());
        match simple {
            BindAuthentication::Simple(pwd) => assert_eq!(pwd, "password"),
            _ => panic!("Expected Simple authentication"),
        }

        let anon = BindAuthentication::Anonymous;
        match anon {
            BindAuthentication::Anonymous => {}
            _ => panic!("Expected Anonymous authentication"),
        }
    }

    #[test]
    fn test_ldap_message_structure() {
        let msg = LdapMessage {
            message_id: 42,
            protocol_op: LdapProtocolOp::UnbindRequest,
        };
        assert_eq!(msg.message_id, 42);
        match msg.protocol_op {
            LdapProtocolOp::UnbindRequest => {}
            _ => panic!("Expected UnbindRequest"),
        }
    }

    #[test]
    fn test_ldap_protocol_op_bind_request() {
        let op = LdapProtocolOp::BindRequest {
            version: 3,
            dn: "cn=admin,dc=example,dc=com".to_string(),
            authentication: BindAuthentication::Simple("secret".to_string()),
        };

        match op {
            LdapProtocolOp::BindRequest {
                version,
                dn,
                authentication,
            } => {
                assert_eq!(version, 3);
                assert_eq!(dn, "cn=admin,dc=example,dc=com");
                match authentication {
                    BindAuthentication::Simple(pwd) => assert_eq!(pwd, "secret"),
                    _ => panic!("Expected Simple authentication"),
                }
            }
            _ => panic!("Expected BindRequest"),
        }
    }

    #[test]
    fn test_ldap_protocol_op_search_request() {
        let op = LdapProtocolOp::SearchRequest {
            base_dn: "dc=example,dc=com".to_string(),
            scope: SearchScope::WholeSubtree,
            deref_aliases: DerefAliases::NeverDerefAliases,
            size_limit: 100,
            time_limit: 60,
            types_only: false,
            filter: "(objectClass=*)".to_string(),
            attributes: vec!["cn".to_string(), "mail".to_string()],
        };

        match op {
            LdapProtocolOp::SearchRequest {
                base_dn,
                scope,
                deref_aliases,
                size_limit,
                time_limit,
                types_only,
                filter,
                attributes,
            } => {
                assert_eq!(base_dn, "dc=example,dc=com");
                assert_eq!(scope, SearchScope::WholeSubtree);
                assert_eq!(deref_aliases, DerefAliases::NeverDerefAliases);
                assert_eq!(size_limit, 100);
                assert_eq!(time_limit, 60);
                assert!(!types_only);
                assert_eq!(filter, "(objectClass=*)");
                assert_eq!(attributes, vec!["cn", "mail"]);
            }
            _ => panic!("Expected SearchRequest"),
        }
    }

    #[test]
    fn test_ldap_protocol_op_search_result_entry() {
        let mut attrs = HashMap::new();
        attrs.insert("cn".to_string(), vec!["John Doe".to_string()]);
        attrs.insert("mail".to_string(), vec!["john@example.com".to_string()]);

        let op = LdapProtocolOp::SearchResultEntry {
            dn: "cn=John Doe,dc=example,dc=com".to_string(),
            attributes: attrs.clone(),
        };

        match op {
            LdapProtocolOp::SearchResultEntry { dn, attributes } => {
                assert_eq!(dn, "cn=John Doe,dc=example,dc=com");
                assert_eq!(attributes, attrs);
            }
            _ => panic!("Expected SearchResultEntry"),
        }
    }

    #[test]
    fn test_ldap_protocol_op_compare_request() {
        let op = LdapProtocolOp::CompareRequest {
            dn: "cn=user,dc=example,dc=com".to_string(),
            attribute: "userPassword".to_string(),
            value: "secret".to_string(),
        };

        match op {
            LdapProtocolOp::CompareRequest {
                dn,
                attribute,
                value,
            } => {
                assert_eq!(dn, "cn=user,dc=example,dc=com");
                assert_eq!(attribute, "userPassword");
                assert_eq!(value, "secret");
            }
            _ => panic!("Expected CompareRequest"),
        }
    }

    #[test]
    fn test_ldap_codec_encoder() {
        let mut codec = LdapCodec;
        let mut buf = BytesMut::new();

        let msg = LdapMessage {
            message_id: 1,
            protocol_op: LdapProtocolOp::UnbindRequest,
        };

        // Test that encoding doesn't error
        let result = codec.encode(msg, &mut buf);
        assert!(result.is_ok());
        assert!(!buf.is_empty());
        assert_eq!(&buf[..], b"LDAP"); // Our placeholder implementation
    }

    #[test]
    fn test_ldap_codec_decoder() {
        let mut codec = LdapCodec;
        let mut buf = BytesMut::new();

        // Test that decoding returns None (needs more data)
        let result = codec.decode(&mut buf);
        assert!(result.is_ok());
        assert!(result.unwrap().is_none());
    }

    #[test]
    fn test_equality_traits() {
        let result1 = LdapResult::success();
        let result2 = LdapResult::success();
        assert_eq!(result1, result2);

        let result3 = LdapResult::error(LdapResultCode::NoSuchObject, "Not found".to_string());
        assert_ne!(result1, result3);

        let msg1 = LdapMessage {
            message_id: 1,
            protocol_op: LdapProtocolOp::UnbindRequest,
        };
        let msg2 = LdapMessage {
            message_id: 1,
            protocol_op: LdapProtocolOp::UnbindRequest,
        };
        assert_eq!(msg1, msg2);
    }

    #[test]
    fn test_clone_traits() {
        let result = LdapResult::success();
        let result_clone = result.clone();
        assert_eq!(result, result_clone);

        let msg = LdapMessage {
            message_id: 42,
            protocol_op: LdapProtocolOp::BindResponse {
                result: LdapResult::success(),
            },
        };
        let msg_clone = msg.clone();
        assert_eq!(msg, msg_clone);
    }
}