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
409
410
411
412
413
414
use super::entry::LdapEntry;
use super::index::{AttributeIndex, ObjectClassIndex};
use crate::yaml::{YamlDirectory, YamlSchema};
use dashmap::DashMap;
use std::sync::Arc;

#[derive(Debug, Clone)]
pub struct Directory {
    pub base_dn: String,
    entries: Arc<DashMap<String, LdapEntry>>,
    pub schema: YamlSchema,
    // Indexes for fast lookups
    uid_index: AttributeIndex,
    cn_index: AttributeIndex,
    objectclass_index: ObjectClassIndex,
}

impl Directory {
    pub fn new(base_dn: String, schema: YamlSchema) -> Self {
        Self {
            base_dn,
            entries: Arc::new(DashMap::new()),
            schema,
            uid_index: AttributeIndex::new(),
            cn_index: AttributeIndex::new(),
            objectclass_index: ObjectClassIndex::new(),
        }
    }

    pub fn from_yaml(yaml_dir: YamlDirectory, schema: YamlSchema) -> Self {
        let dir = Self::new(yaml_dir.directory.base_dn, schema);

        for yaml_entry in yaml_dir.entries {
            let entry: LdapEntry = yaml_entry.into();
            dir.add_entry(entry);
        }

        dir
    }

    pub fn add_entry(&self, entry: LdapEntry) {
        let dn_lower = entry.dn.to_lowercase();

        // Update indexes
        if let Some(uid_attr) = entry.get_attribute("uid") {
            for value in &uid_attr.values {
                self.uid_index.insert("uid", &value.as_string(), &dn_lower);
            }
        }

        if let Some(cn_attr) = entry.get_attribute("cn") {
            for value in &cn_attr.values {
                self.cn_index.insert("cn", &value.as_string(), &dn_lower);
            }
        }

        for oc in &entry.object_classes {
            self.objectclass_index.insert(oc, &dn_lower);
        }

        self.entries.insert(dn_lower, entry);
    }

    pub fn get_entry(&self, dn: &str) -> Option<LdapEntry> {
        self.entries.get(&dn.to_lowercase()).map(|e| e.clone())
    }

    pub fn search_entries<F>(&self, base_dn: &str, scope: SearchScope, filter: F) -> Vec<LdapEntry>
    where
        F: Fn(&LdapEntry) -> bool,
    {
        let base_dn_lower = base_dn.to_lowercase();
        let mut results = Vec::new();

        for entry in self.entries.iter() {
            let entry_dn_lower = entry.key().to_lowercase();

            // Check if entry is in scope
            let in_scope = match scope {
                SearchScope::BaseObject => entry_dn_lower == base_dn_lower,
                SearchScope::SingleLevel => {
                    entry_dn_lower != base_dn_lower
                        && is_direct_child(&entry_dn_lower, &base_dn_lower)
                }
                SearchScope::WholeSubtree => {
                    entry_dn_lower == base_dn_lower
                        || is_descendant(&entry_dn_lower, &base_dn_lower)
                }
            };

            if in_scope && filter(&entry) {
                results.push(entry.clone());
            }
        }

        results
    }

    pub fn entry_exists(&self, dn: &str) -> bool {
        self.entries.contains_key(&dn.to_lowercase())
    }
}

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

fn is_direct_child(child_dn: &str, parent_dn: &str) -> bool {
    if parent_dn.is_empty() || !child_dn.ends_with(parent_dn) {
        return false;
    }

    let prefix = &child_dn[..child_dn.len() - parent_dn.len()];
    if prefix.is_empty() {
        return false;
    }

    // Remove trailing comma if present
    let prefix = prefix.trim_end_matches(',');

    // Check if there's only one RDN component
    !prefix.contains(',')
}

fn is_descendant(child_dn: &str, parent_dn: &str) -> bool {
    child_dn.ends_with(parent_dn) && child_dn.len() > parent_dn.len()
}

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

    #[test]
    fn test_is_direct_child() {
        assert!(is_direct_child(
            "uid=john,ou=users,dc=example,dc=com",
            "ou=users,dc=example,dc=com"
        ));

        assert!(!is_direct_child(
            "uid=john,ou=admins,ou=users,dc=example,dc=com",
            "ou=users,dc=example,dc=com"
        ));

        assert!(!is_direct_child(
            "ou=users,dc=example,dc=com",
            "ou=users,dc=example,dc=com"
        ));
    }

    #[test]
    fn test_is_descendant() {
        assert!(is_descendant(
            "uid=john,ou=users,dc=example,dc=com",
            "dc=example,dc=com"
        ));

        assert!(is_descendant(
            "uid=john,ou=admins,ou=users,dc=example,dc=com",
            "dc=example,dc=com"
        ));

        assert!(!is_descendant("dc=example,dc=com", "dc=example,dc=com"));
    }

    #[test]
    fn test_directory_new() {
        let schema = YamlSchema::default();
        let directory = Directory::new("dc=test,dc=com".to_string(), schema.clone());

        assert_eq!(directory.base_dn, "dc=test,dc=com");
        assert_eq!(directory.entries.len(), 0);
    }

    #[test]
    fn test_directory_add_and_get_entry() {
        let schema = YamlSchema::default();
        let directory = Directory::new("dc=test,dc=com".to_string(), schema);

        let mut entry = LdapEntry::new("cn=test,dc=test,dc=com".to_string());
        entry.add_attribute(
            "cn".to_string(),
            vec![crate::directory::entry::AttributeValue::String(
                "test".to_string(),
            )],
            crate::directory::entry::AttributeSyntax::String,
        );

        directory.add_entry(entry.clone());

        // Test get_entry
        let retrieved = directory.get_entry("cn=test,dc=test,dc=com");
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().dn, "cn=test,dc=test,dc=com");

        // Test case insensitive lookup
        let retrieved = directory.get_entry("CN=TEST,DC=TEST,DC=COM");
        assert!(retrieved.is_some());

        // Test entry_exists
        assert!(directory.entry_exists("cn=test,dc=test,dc=com"));
        assert!(directory.entry_exists("CN=TEST,DC=TEST,DC=COM"));
        assert!(!directory.entry_exists("cn=nonexistent,dc=test,dc=com"));
    }

    #[test]
    fn test_directory_search_base_object() {
        let schema = YamlSchema::default();
        let directory = Directory::new("dc=test,dc=com".to_string(), schema);

        let mut entry = LdapEntry::new("cn=test,dc=test,dc=com".to_string());
        entry.add_attribute(
            "cn".to_string(),
            vec![crate::directory::entry::AttributeValue::String(
                "test".to_string(),
            )],
            crate::directory::entry::AttributeSyntax::String,
        );
        directory.add_entry(entry);

        // Search for exact DN
        let results =
            directory.search_entries("cn=test,dc=test,dc=com", SearchScope::BaseObject, |_| true);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].dn, "cn=test,dc=test,dc=com");

        // Search for non-existent DN
        let results =
            directory.search_entries("cn=other,dc=test,dc=com", SearchScope::BaseObject, |_| true);
        assert_eq!(results.len(), 0);
    }

    #[test]
    fn test_directory_search_single_level() {
        let schema = YamlSchema::default();
        let directory = Directory::new("dc=test,dc=com".to_string(), schema);

        // Add parent
        let parent = LdapEntry::new("ou=users,dc=test,dc=com".to_string());
        directory.add_entry(parent);

        // Add direct child
        let child1 = LdapEntry::new("cn=user1,ou=users,dc=test,dc=com".to_string());
        directory.add_entry(child1);

        // Add grandchild (should not be included)
        let grandchild = LdapEntry::new("cn=sub,cn=user1,ou=users,dc=test,dc=com".to_string());
        directory.add_entry(grandchild);

        let results =
            directory.search_entries("ou=users,dc=test,dc=com", SearchScope::SingleLevel, |_| {
                true
            });

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].dn, "cn=user1,ou=users,dc=test,dc=com");
    }

    #[test]
    fn test_directory_search_whole_subtree() {
        let schema = YamlSchema::default();
        let directory = Directory::new("dc=test,dc=com".to_string(), schema);

        // Add base
        let base = LdapEntry::new("dc=test,dc=com".to_string());
        directory.add_entry(base);

        // Add child
        let child = LdapEntry::new("ou=users,dc=test,dc=com".to_string());
        directory.add_entry(child);

        // Add grandchild
        let grandchild = LdapEntry::new("cn=user1,ou=users,dc=test,dc=com".to_string());
        directory.add_entry(grandchild);

        let results =
            directory.search_entries("dc=test,dc=com", SearchScope::WholeSubtree, |_| true);

        assert_eq!(results.len(), 3);
        let dns: Vec<String> = results.iter().map(|e| e.dn.clone()).collect();
        assert!(dns.contains(&"dc=test,dc=com".to_string()));
        assert!(dns.contains(&"ou=users,dc=test,dc=com".to_string()));
        assert!(dns.contains(&"cn=user1,ou=users,dc=test,dc=com".to_string()));
    }

    #[test]
    fn test_directory_search_with_filter() {
        let schema = YamlSchema::default();
        let directory = Directory::new("dc=test,dc=com".to_string(), schema);

        let mut entry1 = LdapEntry::new("cn=user1,dc=test,dc=com".to_string());
        entry1.add_attribute(
            "uid".to_string(),
            vec![crate::directory::entry::AttributeValue::String(
                "user1".to_string(),
            )],
            crate::directory::entry::AttributeSyntax::String,
        );
        directory.add_entry(entry1);

        let mut entry2 = LdapEntry::new("cn=user2,dc=test,dc=com".to_string());
        entry2.add_attribute(
            "uid".to_string(),
            vec![crate::directory::entry::AttributeValue::String(
                "user2".to_string(),
            )],
            crate::directory::entry::AttributeSyntax::String,
        );
        directory.add_entry(entry2);

        // Filter for user1 only
        let results =
            directory.search_entries("dc=test,dc=com", SearchScope::WholeSubtree, |entry| {
                entry
                    .get_attribute("uid")
                    .map(|attr| attr.values.iter().any(|v| v.as_string() == "user1"))
                    .unwrap_or(false)
            });

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].dn, "cn=user1,dc=test,dc=com");
    }

    #[test]
    fn test_directory_from_yaml() {
        let schema = YamlSchema::default();

        let yaml_dir = crate::yaml::YamlDirectory {
            directory: crate::yaml::schema::DirectoryConfig {
                base_dn: "dc=yaml,dc=com".to_string(),
            },
            schema: None,
            entries: vec![crate::yaml::YamlEntry {
                dn: "cn=test,dc=yaml,dc=com".to_string(),
                object_class: vec!["person".to_string()],
                attributes: [(
                    "cn".to_string(),
                    serde_yaml::Value::String("test".to_string()),
                )]
                .into_iter()
                .collect(),
            }],
        };

        let directory = Directory::from_yaml(yaml_dir, schema);

        assert_eq!(directory.base_dn, "dc=yaml,dc=com");
        assert!(directory.entry_exists("cn=test,dc=yaml,dc=com"));

        let entry = directory.get_entry("cn=test,dc=yaml,dc=com").unwrap();
        assert!(entry.has_attribute("cn"));
        assert!(entry.has_attribute("objectClass"));
    }

    #[test]
    fn test_directory_indexing() {
        let schema = YamlSchema::default();
        let directory = Directory::new("dc=test,dc=com".to_string(), schema);

        let mut entry = LdapEntry::new("cn=indexed,dc=test,dc=com".to_string());
        entry.add_attribute(
            "uid".to_string(),
            vec![crate::directory::entry::AttributeValue::String(
                "testuid".to_string(),
            )],
            crate::directory::entry::AttributeSyntax::String,
        );
        entry.add_attribute(
            "cn".to_string(),
            vec![crate::directory::entry::AttributeValue::String(
                "Indexed User".to_string(),
            )],
            crate::directory::entry::AttributeSyntax::String,
        );
        entry.object_classes = vec!["person".to_string(), "top".to_string()];

        directory.add_entry(entry);

        // The indexes should have been updated
        // This is tested indirectly through search functionality
        let results = directory.search_entries("dc=test,dc=com", SearchScope::WholeSubtree, |e| {
            e.has_attribute("uid")
        });

        assert_eq!(results.len(), 1);
    }

    #[test]
    fn test_is_direct_child_edge_cases() {
        // Test with empty parent
        assert!(!is_direct_child("cn=test", ""));

        // Test with child same as parent
        assert!(!is_direct_child("dc=com", "dc=com"));

        // Test with trailing comma
        assert!(is_direct_child("cn=test,dc=com", "dc=com"));

        // Test with multiple levels
        assert!(!is_direct_child("cn=test,ou=users,dc=com", "dc=com"));
    }

    #[test]
    fn test_is_descendant_edge_cases() {
        // Test with unrelated DNs
        assert!(!is_descendant("dc=other,dc=com", "dc=example,dc=com"));

        // Test with partial match
        assert!(!is_descendant("dc=com", "dc=example,dc=com"));
    }
}