ldap-acis 0.2.1

LDAP Access Control Instructions (ACI) system built on acls-rs
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//! ACI text format generators for 389-ds and OpenLDAP formats.

use crate::aci::{Aci, BindRule, Scope, TargetAttrFilter, TargetAttrFilterOp, TargetFilter};
use crate::operation::{OperationType, PermissionSlice};
use std::fmt;

/// Error type for ACI generation failures.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum GenerateError {
    /// Unsupported feature for this format
    UnsupportedFeature(String),
    /// Invalid ACI structure
    InvalidAci(String),
}

impl fmt::Display for GenerateError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            GenerateError::UnsupportedFeature(msg) => write!(f, "Unsupported feature: {}", msg),
            GenerateError::InvalidAci(msg) => write!(f, "Invalid ACI: {}", msg),
        }
    }
}

impl std::error::Error for GenerateError {}

/// Trait for ACI text format generators.
pub trait AciGenerator {
    /// Generate text representation of ACIs.
    fn generate(acis: &[Aci]) -> Result<String, GenerateError>;

    /// Generator name for error messages.
    fn name() -> &'static str;
}

/// 389-ds / FreeIPA ACI format generator.
pub struct Ds389;

impl AciGenerator for Ds389 {
    fn generate(acis: &[Aci]) -> Result<String, GenerateError> {
        if acis.is_empty() {
            return Ok(String::new());
        }

        acis.iter()
            .map(generate_389ds_aci_impl)
            .collect::<Result<Vec<_>, _>>()
            .map(|lines| lines.join("\n"))
    }

    fn name() -> &'static str {
        "389-ds"
    }
}

/// OpenLDAP access control format generator.
pub struct OpenLdap;

impl AciGenerator for OpenLdap {
    fn generate(acis: &[Aci]) -> Result<String, GenerateError> {
        generate_openldap_access_impl(acis)
    }

    fn name() -> &'static str {
        "OpenLDAP"
    }
}

/// Generic generate function using the AciGenerator trait.
///
/// # Examples
///
/// ```
/// use ldap_acis::prelude::*;
///
/// let aci = AciBuilder::new("test")
///     .target_attribute("cn")
///     .permission(OperationType::Read)
///     .bind_rule(BindRule::Anyone)
///     .build();
///
/// // Generate 389-ds format
/// let text = generate::<Ds389Gen>(&[aci.clone()]).expect("generate should succeed");
/// assert!(text.contains("version 3.0"));
///
/// // Generate OpenLDAP format
/// let text = generate::<OpenLdapGen>(&[aci]).expect("generate should succeed");
/// assert!(text.contains("by *"));
/// ```
pub fn generate<F: AciGenerator>(acis: &[Aci]) -> Result<String, GenerateError> {
    F::generate(acis)
}

// Internal implementation functions

fn generate_389ds_aci_impl(aci: &Aci) -> Result<String, GenerateError> {
    let mut parts = Vec::new();

    // Target DN
    if let Some(ref dn) = aci.target_dn {
        parts.push(format!("(target = \"ldap:///{}\")", dn));
    }

    // Target attributes
    if !aci.target_attributes.is_empty() {
        let attrs = aci.target_attributes.join(" || ");
        let op = if aci.target_attributes_excluded {
            "!="
        } else {
            "="
        };
        parts.push(format!("(targetattr {} \"{}\")", op, attrs));
    }

    // Target attribute filters (targattrfilters)
    if !aci.target_attr_filters.is_empty() {
        parts.push(format!(
            "(targattrfilters = \"{}\")",
            format_targattrfilters(&aci.target_attr_filters)
        ));
    }

    // Target from/to (ModDN restrictions)
    if let Some(ref from_dn) = aci.target_from {
        parts.push(format!("(target_from = \"ldap:///{}\")", from_dn));
    }
    if let Some(ref to_dn) = aci.target_to {
        parts.push(format!("(target_to = \"ldap:///{}\")", to_dn));
    }

    // Target filter
    match &aci.target_filter {
        TargetFilter::All => {
            // No filter needed for "all"
        }
        TargetFilter::DnPattern(_) => {
            // DN patterns go in target, not targetfilter
        }
        filter => {
            let filter_str = format_target_filter(filter)?;
            parts.push(format!("(targetfilter = \"({})\")", filter_str));
        }
    }

    // Permission statement
    let grant_deny = if aci.grant { "allow" } else { "deny" };
    let perms = format_permissions_389ds(&aci.permissions)?;
    let bind_rule = format_bind_rule_389ds(&aci.bind_rule)?;

    parts.push(format!(
        "(version 3.0;acl \"{}\";{} ({}) {};)",
        aci.name, grant_deny, perms, bind_rule
    ));

    Ok(parts.join(""))
}

fn format_targattrfilters(filters: &[TargetAttrFilter]) -> String {
    let mut add_parts = Vec::new();
    let mut del_parts = Vec::new();

    for f in filters {
        let entry = format!("{}:({})", f.attr, f.filter);
        match f.op {
            TargetAttrFilterOp::Add => add_parts.push(entry),
            TargetAttrFilterOp::Del => del_parts.push(entry),
        }
    }

    let mut sections = Vec::new();
    if !add_parts.is_empty() {
        sections.push(format!("add={}", add_parts.join(" && ")));
    }
    if !del_parts.is_empty() {
        sections.push(format!("del={}", del_parts.join(" && ")));
    }
    sections.join(", ")
}

fn format_target_filter(filter: &TargetFilter) -> Result<String, GenerateError> {
    match filter {
        TargetFilter::All => Ok("objectclass=*".to_string()),
        TargetFilter::DnPattern(_) => Err(GenerateError::UnsupportedFeature(
            "DN patterns should be in target, not targetfilter".to_string(),
        )),
        TargetFilter::ObjectClass(oc) => Ok(format!("objectclass={}", oc)),
        TargetFilter::HasAttribute(attr) => Ok(format!("{}=*", attr)),
        TargetFilter::And(filters) => {
            let inner = filters
                .iter()
                .map(|f| format_target_filter(f).map(|s| format!("({})", s)))
                .collect::<Result<Vec<_>, _>>()?
                .join("");
            Ok(format!("&{}", inner))
        }
        TargetFilter::Or(filters) => {
            let inner = filters
                .iter()
                .map(|f| format_target_filter(f).map(|s| format!("({})", s)))
                .collect::<Result<Vec<_>, _>>()?
                .join("");
            Ok(format!("|{}", inner))
        }
        TargetFilter::Not(inner) => {
            let inner_str = format_target_filter(inner)?;
            Ok(format!(
                "!{}",
                if inner_str.starts_with('(') {
                    inner_str
                } else {
                    format!("({})", inner_str)
                }
            ))
        }
        TargetFilter::Raw(s) => Ok(s.clone()),
    }
}

fn format_permissions_389ds(perms: &[OperationType]) -> Result<String, GenerateError> {
    if perms.is_empty() {
        return Err(GenerateError::InvalidAci(
            "ACI must have at least one permission".to_string(),
        ));
    }

    let perm_strs: Vec<_> = perms
        .iter()
        .map(|p| match p {
            OperationType::Read => "read",
            OperationType::Search => "search",
            OperationType::Compare => "compare",
            OperationType::Modify => "write",
            OperationType::Add => "add",
            OperationType::Delete => "delete",
            OperationType::ModifyDn => "moddn",
            OperationType::Bind => "proxy",
            OperationType::All => "all",
            OperationType::SelfWrite => "selfwrite",
        })
        .collect();

    Ok(perm_strs.join(","))
}

fn format_bind_rule_389ds(bind_rule: &BindRule) -> Result<String, GenerateError> {
    match bind_rule {
        BindRule::Anyone => Ok("userdn = \"ldap:///anyone\"".to_string()),
        BindRule::Authenticated => Ok("userdn = \"ldap:///all\"".to_string()),
        BindRule::SelfUser => Ok("userdn = \"ldap:///self\"".to_string()),
        BindRule::UserDn(dn) => Ok(format!("userdn = \"ldap:///{}\"", dn)),
        BindRule::GroupDn(dn) => Ok(format!("groupdn = \"ldap:///{}\"", dn)),
        BindRule::RoleAttribute(attr, value) => Ok(format!("userattr = \"{}#{}\"", attr, value)),
        BindRule::ParentDn => Ok("userdn = \"ldap:///parent\"".to_string()),
        BindRule::RoleDn(dn) => Ok(format!("roledn = \"ldap:///{}\"", dn)),
        BindRule::UserDnAttr(attr) => Ok(format!("userdnattr = \"{}\"", attr)),
        BindRule::GroupDnAttr(attr) => Ok(format!("groupdnattr = \"{}\"", attr)),
        BindRule::Ip(val) => Ok(format!("ip = \"{}\"", val)),
        BindRule::Dns(val) => Ok(format!("dns = \"{}\"", val)),
        BindRule::AuthMethod(val) => Ok(format!("authmethod = \"{}\"", val)),
        BindRule::DayOfWeek(val) => Ok(format!("dayofweek = \"{}\"", val)),
        BindRule::TimeOfDay(op, val) => Ok(format!("timeofday {} \"{}\"", op, val)),
        BindRule::Ssf(op, val) => Ok(format!("ssf {} \"{}\"", op, val)),
        BindRule::Or(rules) => {
            let parts: Vec<String> = rules
                .iter()
                .map(format_bind_rule_389ds)
                .collect::<Result<_, _>>()?;
            Ok(parts.join(" or "))
        }
        BindRule::And(rules) => {
            let parts: Vec<String> = rules
                .iter()
                .map(format_bind_rule_389ds)
                .collect::<Result<_, _>>()?;
            Ok(parts.join(" and "))
        }
        BindRule::Not(inner) => {
            let inner_str = match inner.as_ref() {
                BindRule::UserDn(dn) => format!("userdn != \"ldap:///{}\"", dn),
                BindRule::Anyone => "userdn != \"ldap:///anyone\"".to_string(),
                BindRule::Authenticated => "userdn != \"ldap:///all\"".to_string(),
                BindRule::SelfUser => "userdn != \"ldap:///self\"".to_string(),
                BindRule::GroupDn(dn) => format!("groupdn != \"ldap:///{}\"", dn),
                BindRule::RoleDn(dn) => format!("roledn != \"ldap:///{}\"", dn),
                BindRule::Ip(val) => format!("ip != \"{}\"", val),
                BindRule::Dns(val) => format!("dns != \"{}\"", val),
                _ => {
                    return Err(GenerateError::UnsupportedFeature(
                        "Nested NOT in 389-ds bind rule".to_string(),
                    ))
                }
            };
            Ok(inner_str)
        }
    }
}

fn generate_openldap_access_impl(acis: &[Aci]) -> Result<String, GenerateError> {
    if acis.is_empty() {
        return Ok(String::new());
    }

    // Group ACIs by target (DN, scope, attributes, filter)
    // Each unique target becomes one "to" clause with multiple "by" clauses
    let mut target_groups: Vec<(String, Vec<&Aci>)> = Vec::new();

    for aci in acis {
        let target_key = format_openldap_target(aci)?;

        if let Some((_, acis)) = target_groups.iter_mut().find(|(k, _)| k == &target_key) {
            acis.push(aci);
        } else {
            target_groups.push((target_key, vec![aci]));
        }
    }

    // Generate each target group
    let mut rules = Vec::new();
    for (target_spec, acis) in target_groups {
        let mut lines = vec![target_spec];

        for aci in acis {
            let who = format_who_clause_openldap(&aci.bind_rule)?;
            let access = format_access_level_openldap(&aci.permissions, aci.grant)?;
            lines.push(format!("  by {} {}", who, access));
        }

        rules.push(lines.join("\n"));
    }

    Ok(rules.join("\n\n"))
}

fn format_openldap_target(aci: &Aci) -> Result<String, GenerateError> {
    let mut parts = Vec::new();

    // DN and scope
    if let Some(ref dn) = aci.target_dn {
        let scope_str = match aci.scope {
            Scope::Base => "base",
            Scope::OneLevel => "one",
            Scope::Subtree => "subtree",
        };
        parts.push(format!("dn.{}=\"{}\"", scope_str, dn));
    } else {
        parts.push("*".to_string());
    }

    // Attributes
    if !aci.target_attributes.is_empty() {
        let attrs = aci.target_attributes.join(",");
        parts.push(format!("attrs={}", attrs));
    }

    // Filter
    match &aci.target_filter {
        TargetFilter::All => {
            // No filter needed for "all"
        }
        TargetFilter::DnPattern(_) => {
            // DN patterns handled in target
        }
        filter => {
            let filter_str = format_target_filter(filter)?;
            parts.push(format!("filter=\"({})\"", filter_str));
        }
    }

    Ok(format!("to {}", parts.join(" ")))
}

fn format_who_clause_openldap(bind_rule: &BindRule) -> Result<String, GenerateError> {
    match bind_rule {
        BindRule::Anyone => Ok("*".to_string()),
        BindRule::Authenticated => Ok("users".to_string()),
        BindRule::SelfUser => Ok("self".to_string()),
        BindRule::UserDn(dn) => Ok(format!("dn.exact=\"{}\"", dn)),
        BindRule::GroupDn(dn) => Ok(format!("group.exact=\"{}\"", dn)),
        BindRule::RoleAttribute(attr, value) => Err(GenerateError::UnsupportedFeature(format!(
            "Role attribute {}={} not directly supported in OpenLDAP",
            attr, value
        ))),
        BindRule::And(_) | BindRule::Or(_) | BindRule::Not(_) => {
            Err(GenerateError::UnsupportedFeature(
                "Complex boolean bind rules not yet supported in OpenLDAP generator".to_string(),
            ))
        }
        BindRule::ParentDn
        | BindRule::RoleDn(_)
        | BindRule::UserDnAttr(_)
        | BindRule::GroupDnAttr(_)
        | BindRule::Ip(_)
        | BindRule::Dns(_)
        | BindRule::AuthMethod(_)
        | BindRule::DayOfWeek(_)
        | BindRule::TimeOfDay(_, _)
        | BindRule::Ssf(_, _) => Err(GenerateError::UnsupportedFeature(
            "389-ds specific bind rule not supported in OpenLDAP generator".to_string(),
        )),
    }
}

fn format_access_level_openldap(
    perms: &[OperationType],
    grant: bool,
) -> Result<String, GenerateError> {
    if !grant {
        return Ok("none".to_string());
    }

    if perms.is_empty() {
        return Ok("none".to_string());
    }

    // Determine access level based on permissions (All grants everything)
    let has_all = perms.contains(&OperationType::All);

    // Highest level wins
    if has_all || perms.grants_write() {
        Ok("write".to_string())
    } else if perms.grants(&OperationType::Read) {
        Ok("read".to_string())
    } else if perms.grants(&OperationType::Search) {
        Ok("search".to_string())
    } else if perms.grants(&OperationType::Compare) {
        Ok("compare".to_string())
    } else {
        Ok("none".to_string())
    }
}

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

    #[test]
    fn test_generate_389ds_simple() {
        let aci = AciBuilder::new("test read")
            .target_attribute("cn")
            .target_attribute("mail")
            .permission(OperationType::Read)
            .permission(OperationType::Search)
            .bind_rule(BindRule::Anyone)
            .build();

        let result = generate::<Ds389>(&[aci]).unwrap();
        assert!(result.contains("targetattr = \"cn || mail\""));
        assert!(result.contains("version 3.0"));
        assert!(result.contains("acl \"test read\""));
        assert!(result.contains("allow (read,search)"));
        assert!(result.contains("userdn = \"ldap:///anyone\""));
    }

    #[test]
    fn test_generate_389ds_with_target() {
        let aci = AciBuilder::new("self modify")
            .target_dn("uid=*,ou=people,dc=example,dc=com")
            .target_attribute("description")
            .permission(OperationType::Modify)
            .bind_rule(BindRule::SelfUser)
            .build();

        let result = generate::<Ds389>(&[aci]).unwrap();
        assert!(result.contains("target = \"ldap:///uid=*,ou=people,dc=example,dc=com\""));
        assert!(result.contains("targetattr = \"description\""));
        assert!(result.contains("allow (write)"));
        assert!(result.contains("userdn = \"ldap:///self\""));
    }

    #[test]
    fn test_generate_389ds_deny() {
        let aci = AciBuilder::new("deny password")
            .target_attribute("userPassword")
            .permission(OperationType::Read)
            .permission(OperationType::Compare)
            .bind_rule(BindRule::Authenticated)
            .deny()
            .build();

        let result = generate::<Ds389>(&[aci]).unwrap();
        assert!(result.contains("deny (read,compare)"));
        assert!(result.contains("userdn = \"ldap:///all\""));
    }

    #[test]
    fn test_generate_openldap_simple() {
        let aci1 = AciBuilder::new("self write")
            .target_dn("ou=people,dc=example,dc=com")
            .target_scope(Scope::Subtree)
            .target_attribute("cn")
            .target_attribute("mail")
            .permission(OperationType::Modify)
            .bind_rule(BindRule::SelfUser)
            .build();

        let aci2 = AciBuilder::new("public read")
            .target_dn("ou=people,dc=example,dc=com")
            .target_scope(Scope::Subtree)
            .target_attribute("cn")
            .target_attribute("mail")
            .permission(OperationType::Read)
            .bind_rule(BindRule::Anyone)
            .build();

        let result = generate::<OpenLdap>(&[aci1, aci2]).unwrap();
        assert!(result.contains("to dn.subtree=\"ou=people,dc=example,dc=com\" attrs=cn,mail"));
        assert!(result.contains("by self write"));
        assert!(result.contains("by * read"));
    }

    #[test]
    fn test_generate_openldap_with_group() {
        let aci = AciBuilder::new("admin write")
            .target_dn("dc=example,dc=com")
            .target_scope(Scope::Base)
            .permission(OperationType::Modify)
            .bind_rule(BindRule::GroupDn(
                "cn=admins,ou=groups,dc=example,dc=com".to_string(),
            ))
            .build();

        let result = generate::<OpenLdap>(&[aci]).unwrap();
        assert!(result.contains("to dn.base=\"dc=example,dc=com\""));
        assert!(result.contains("by group.exact=\"cn=admins,ou=groups,dc=example,dc=com\" write"));
    }

    #[test]
    fn test_round_trip_389ds() {
        use crate::parser::{parse, Ds389 as Ds389Parser};

        let original = r#"(targetattr = "cn || mail")(version 3.0;acl "test";allow (read,search) userdn = "ldap:///anyone";)"#;

        let parsed = parse::<Ds389Parser>(original).unwrap();
        let generated = generate::<Ds389>(&parsed).unwrap();
        let re_parsed = parse::<Ds389Parser>(&generated).unwrap();

        assert_eq!(parsed.len(), re_parsed.len());
        assert_eq!(parsed[0].name, re_parsed[0].name);
        assert_eq!(parsed[0].target_attributes, re_parsed[0].target_attributes);
        assert_eq!(parsed[0].permissions, re_parsed[0].permissions);
    }

    #[test]
    fn test_round_trip_389ds_compound_or() {
        use crate::parser::{parse, Ds389 as Ds389Parser};

        let original = r#"(targetattr = "member")(version 3.0;acl "compound or";allow (write) userattr = "memberManager#USERDN" or userattr = "memberManager#GROUPDN";)"#;

        let parsed = parse::<Ds389Parser>(original).unwrap();
        assert!(matches!(&parsed[0].bind_rule, BindRule::Or(_)));

        let generated = generate::<Ds389>(&parsed).unwrap();
        let re_parsed = parse::<Ds389Parser>(&generated).unwrap();

        assert_eq!(parsed[0].name, re_parsed[0].name);
        assert_eq!(parsed[0].bind_rule, re_parsed[0].bind_rule);
    }

    #[test]
    fn test_generate_389ds_not() {
        let aci = AciBuilder::new("deny anon")
            .target_attribute("cn")
            .permission(OperationType::Read)
            .bind_rule(BindRule::Not(Box::new(BindRule::Anyone)))
            .build();

        let result = generate::<Ds389>(&[aci]).unwrap();
        assert!(result.contains(r#"userdn != "ldap:///anyone""#));
    }
}