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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
//! LDAP specific operations handling components. This is where LDAP operations
//! are sent to for processing.

use std::collections::BTreeSet;
use std::iter;

use async_std::task;
use kanidm_proto::v1::{ApiToken, OperationError, UserAuthToken};
use ldap3_proto::simple::*;
use regex::Regex;
use tracing::trace;
use uuid::Uuid;

use crate::event::SearchEvent;
use crate::idm::event::{LdapAuthEvent, LdapTokenAuthEvent};
use crate::idm::server::{IdmServer, IdmServerTransaction};
use crate::prelude::*;

// Clippy doesn't like Bind here. But proto needs unboxed ldapmsg,
// and ldapboundtoken is moved. Really, it's not too bad, every message here is pretty sucky.
#[allow(clippy::large_enum_variant)]
pub enum LdapResponseState {
    Unbind,
    Disconnect(LdapMsg),
    Bind(LdapBoundToken, LdapMsg),
    Respond(LdapMsg),
    MultiPartResponse(Vec<LdapMsg>),
    BindMultiPartResponse(LdapBoundToken, Vec<LdapMsg>),
}

#[derive(Debug, Clone, PartialEq)]
pub enum LdapSession {
    // Maps through and provides anon read, but allows us to check the validity
    // of the account still.
    UnixBind(Uuid),
    UserAuthToken(UserAuthToken),
    ApiToken(ApiToken),
}

#[derive(Debug, Clone)]
pub struct LdapBoundToken {
    // Used to help ID the user doing the action, makes logging nicer.
    pub spn: String,
    pub session_id: Uuid,
    // This is the effective session permission. This is generated from either:
    // * A valid anonymous bind
    // * A valid unix pw bind
    // * A valid ApiToken
    // In a way, this is a stepping stone to an "ident" but allows us to check
    // the session is still "valid" depending on it's origin.
    pub effective_session: LdapSession,
}

pub struct LdapServer {
    rootdse: LdapSearchResultEntry,
    basedn: String,
    dnre: Regex,
    binddnre: Regex,
}

impl LdapServer {
    pub fn new(idms: &IdmServer) -> Result<Self, OperationError> {
        // let ct = duration_from_epoch_now();
        let idms_prox_read = task::block_on(idms.proxy_read_async());
        // This is the rootdse path.
        // get the domain_info item
        let domain_entry = idms_prox_read
            .qs_read
            .internal_search_uuid(&UUID_DOMAIN_INFO)?;

        let domain_name = domain_entry
            .get_ava_single_iname("domain_name")
            .map(|s| s.to_string())
            .ok_or(OperationError::InvalidEntryState)?;

        let basedn = ldap_domain_to_dc(domain_name.as_str());

        let dnre = Regex::new(format!("^((?P<attr>[^=]+)=(?P<val>[^=]+),)?{}$", basedn).as_str())
            .map_err(|_| OperationError::InvalidEntryState)?;

        let binddnre = Regex::new(format!("^(([^=,]+)=)?(?P<val>[^=,]+)(,{})?$", basedn).as_str())
            .map_err(|_| OperationError::InvalidEntryState)?;

        let rootdse = LdapSearchResultEntry {
            dn: "".to_string(),
            attributes: vec![
                LdapPartialAttribute {
                    atype: "objectClass".to_string(),
                    vals: vec!["top".to_string()],
                },
                LdapPartialAttribute {
                    atype: "vendorName".to_string(),
                    vals: vec!["Kanidm Project".to_string()],
                },
                LdapPartialAttribute {
                    atype: "vendorVersion".to_string(),
                    vals: vec!["kanidm_ldap_1.0.0".to_string()],
                },
                LdapPartialAttribute {
                    atype: "supportedLDAPVersion".to_string(),
                    vals: vec!["3".to_string()],
                },
                LdapPartialAttribute {
                    atype: "supportedExtension".to_string(),
                    vals: vec!["1.3.6.1.4.1.4203.1.11.3".to_string()],
                },
                LdapPartialAttribute {
                    atype: "supportedFeatures".to_string(),
                    vals: vec!["1.3.6.1.4.1.4203.1.5.1".to_string()],
                },
                LdapPartialAttribute {
                    atype: "defaultnamingcontext".to_string(),
                    vals: vec![basedn.clone()],
                },
            ],
        };

        Ok(LdapServer {
            rootdse,
            basedn,
            dnre,
            binddnre,
        })
    }

    #[instrument(level = "debug", skip_all)]
    async fn do_search(
        &self,
        idms: &IdmServer,
        sr: &SearchRequest,
        uat: &LdapBoundToken,
        // eventid: &Uuid,
    ) -> Result<Vec<LdapMsg>, OperationError> {
        admin_info!("Attempt LDAP Search for {}", uat.spn);
        // If the request is "", Base, Present("objectclass"), [], then we want the rootdse.
        if sr.base.is_empty() && sr.scope == LdapSearchScope::Base {
            admin_info!("LDAP Search success - RootDSE");
            Ok(vec![
                sr.gen_result_entry(self.rootdse.clone()),
                sr.gen_success(),
            ])
        } else {
            // We want something else apparently. Need to do some more work ...
            // Parse the operation and make sure it's sane before we start the txn.

            // This scoping returns an extra filter component.

            let (opt_attr, opt_value) = match self.dnre.captures(sr.base.as_str()) {
                Some(caps) => (
                    caps.name("attr").map(|v| v.as_str().to_string()),
                    caps.name("val").map(|v| v.as_str().to_string()),
                ),
                None => {
                    request_error!("LDAP Search failure - invalid basedn");
                    return Err(OperationError::InvalidRequestState);
                }
            };

            let req_dn = match (opt_attr, opt_value) {
                (Some(a), Some(v)) => Some((a, v)),
                (None, None) => None,
                _ => {
                    request_error!("LDAP Search failure - invalid rdn");
                    return Err(OperationError::InvalidRequestState);
                }
            };

            trace!(rdn = ?req_dn);

            // Map the Some(a,v) to ...?

            let ext_filter = match (&sr.scope, req_dn) {
                (LdapSearchScope::OneLevel, Some(_r)) => return Ok(vec![sr.gen_success()]),
                (LdapSearchScope::OneLevel, None) => {
                    // exclude domain_info
                    Some(LdapFilter::Not(Box::new(LdapFilter::Equality(
                        "uuid".to_string(),
                        STR_UUID_DOMAIN_INFO.to_string(),
                    ))))
                }
                (LdapSearchScope::Base, Some((a, v))) => Some(LdapFilter::Equality(a, v)),
                (LdapSearchScope::Base, None) => {
                    // domain_info
                    Some(LdapFilter::Equality(
                        "uuid".to_string(),
                        STR_UUID_DOMAIN_INFO.to_string(),
                    ))
                }
                (LdapSearchScope::Subtree, Some((a, v))) => Some(LdapFilter::Equality(a, v)),
                (LdapSearchScope::Subtree, None) => {
                    // No filter changes needed.
                    None
                }
            };

            let mut all_attrs = false;
            let mut all_op_attrs = false;

            // TODO #67: limit the number of attributes here!
            if sr.attrs.is_empty() {
                // If [], then "all" attrs
                all_attrs = true;
            } else {
                sr.attrs.iter().for_each(|a| {
                    if a == "*" {
                        all_attrs = true;
                    } else if a == "+" {
                        // This forces the BE to get all the attrs so we can
                        // map all vattrs.
                        all_attrs = true;
                        all_op_attrs = true;
                    }
                })
            }

            // We need to retain this to know what the client requested.
            let (k_attrs, l_attrs) = if all_op_attrs {
                // We need all attrs, and we do a full v_attr map.
                (None, ldap_all_vattrs())
            } else if all_attrs {
                (None, Vec::new())
            } else {
                // What the client requested, in LDAP forms.
                let req_attrs: Vec<String> = sr
                    .attrs
                    .iter()
                    .filter_map(|a| {
                        if a == "*" || a == "+" {
                            None
                        } else {
                            Some(a.to_lowercase())
                        }
                    })
                    .collect();
                // This is what the client requested, but mapped to kanidm forms.
                // NOTE: All req_attrs are lowercase at this point.
                let mapped_attrs: BTreeSet<_> = req_attrs
                    .iter()
                    .filter_map(|a| {
                        // EntryDN and DN have special handling in to_ldap in Entry. We don't
                        // need these here, we know they will be returned as part of the transform.
                        if a == "entrydn" || a == "dn" {
                            None
                        } else {
                            Some(AttrString::from(ldap_vattr_map(a)))
                        }
                    })
                    .collect();

                (Some(mapped_attrs), req_attrs)
            };

            admin_info!(attr = ?l_attrs, "LDAP Search Request LDAP Attrs");
            admin_info!(attr = ?k_attrs, "LDAP Search Request Mapped Attrs");

            let ct = duration_from_epoch_now();
            let idm_read = idms.proxy_read_async().await;
            // Now start the txn - we need it for resolving filter components.

            // join the filter, with ext_filter
            let lfilter = match ext_filter {
                Some(ext) => LdapFilter::And(vec![
                    sr.filter.clone(),
                    ext,
                    LdapFilter::Not(Box::new(LdapFilter::Or(vec![
                        LdapFilter::Equality("class".to_string(), "classtype".to_string()),
                        LdapFilter::Equality("class".to_string(), "attributetype".to_string()),
                        LdapFilter::Equality(
                            "class".to_string(),
                            "access_control_profile".to_string(),
                        ),
                    ]))),
                ]),
                None => LdapFilter::And(vec![
                    sr.filter.clone(),
                    LdapFilter::Not(Box::new(LdapFilter::Or(vec![
                        LdapFilter::Equality("class".to_string(), "classtype".to_string()),
                        LdapFilter::Equality("class".to_string(), "attributetype".to_string()),
                        LdapFilter::Equality(
                            "class".to_string(),
                            "access_control_profile".to_string(),
                        ),
                    ]))),
                ]),
            };

            admin_info!(filter = ?lfilter, "LDAP Search Filter");

            // Build the event, with the permissions from effective_session
            //
            // ! Remember, searchEvent wraps to ignore hidden for us.
            let ident = idm_read
                .validate_ldap_session(&uat.effective_session, ct)
                .map_err(|e| {
                    admin_error!("Invalid identity: {:?}", e);
                    e
                })?;
            let se =
                SearchEvent::new_ext_impersonate_uuid(&idm_read.qs_read, ident, &lfilter, k_attrs)
                    .map_err(|e| {
                        admin_error!("failed to create search event -> {:?}", e);
                        e
                    })?;

            let res = idm_read.qs_read.search_ext(&se).map_err(|e| {
                admin_error!("search failure {:?}", e);
                e
            })?;

            // These have already been fully reduced (access controls applied),
            // so we can just transform the values and open palm slam them into
            // the result structure.
            let lres: Result<Vec<_>, _> = res
                .into_iter()
                .map(|e| {
                    e.to_ldap(&idm_read.qs_read, self.basedn.as_str(), all_attrs, &l_attrs)
                        // if okay, wrap in a ldap msg.
                        .map(|r| sr.gen_result_entry(r))
                })
                .chain(iter::once(Ok(sr.gen_success())))
                .collect();

            let lres = lres.map_err(|e| {
                admin_error!("entry resolve failure {:?}", e);
                e
            })?;

            admin_info!(
                nentries = %lres.len(),
                "LDAP Search Success -> number of entries"
            );

            Ok(lres)
        }
    }

    async fn do_bind(
        &self,
        idms: &IdmServer,
        dn: &str,
        pw: &str,
    ) -> Result<Option<LdapBoundToken>, OperationError> {
        security_info!(
            "Attempt LDAP Bind for {}",
            if dn.is_empty() { "anonymous" } else { dn }
        );
        let ct = duration_from_epoch_now();

        let mut idm_auth = idms.auth_async().await;

        let target_uuid: Uuid = if dn.is_empty() {
            if pw.is_empty() {
                security_info!("✅ LDAP Bind success anonymous");
                UUID_ANONYMOUS
            } else {
                // This is the path to access api-token logins.
                let lae = LdapTokenAuthEvent::from_parts(pw.to_string())?;
                return idm_auth.token_auth_ldap(&lae, ct).await.and_then(|r| {
                    idm_auth.commit().map(|_| {
                        if r.is_some() {
                            security_info!(%dn, "✅ LDAP Bind success");
                        } else {
                            security_info!(%dn, "❌ LDAP Bind failure");
                        };
                        r
                    })
                });
            }
        } else {
            let rdn = match self
                .binddnre
                .captures(dn)
                .and_then(|caps| caps.name("val").map(|v| v.as_str().to_string()))
            {
                Some(r) => r,
                None => return Err(OperationError::NoMatchingEntries),
            };

            trace!(?rdn, "relative dn");

            if rdn.is_empty() {
                // That's weird ...
                return Err(OperationError::NoMatchingEntries);
            }

            idm_auth.qs_read.name_to_uuid(rdn.as_str()).map_err(|e| {
                request_error!(err = ?e, ?rdn, "Error resolving rdn to target");
                e
            })?
        };

        let lae = LdapAuthEvent::from_parts(target_uuid, pw.to_string())?;
        idm_auth.auth_ldap(&lae, ct).await.and_then(|r| {
            idm_auth.commit().map(|_| {
                if r.is_some() {
                    security_info!(%dn, "✅ LDAP Bind success");
                } else {
                    security_info!(%dn, "❌ LDAP Bind failure");
                };
                r
            })
        })
    }

    pub async fn do_op(
        &self,
        idms: &IdmServer,
        server_op: ServerOps,
        uat: Option<LdapBoundToken>,
        eventid: &Uuid,
    ) -> Result<LdapResponseState, OperationError> {
        match server_op {
            ServerOps::SimpleBind(sbr) => self
                .do_bind(idms, sbr.dn.as_str(), sbr.pw.as_str())
                .await
                .map(|r| match r {
                    Some(lbt) => LdapResponseState::Bind(lbt, sbr.gen_success()),
                    None => LdapResponseState::Respond(sbr.gen_invalid_cred()),
                })
                .or_else(|e| {
                    let (rc, msg) = operationerr_to_ldapresultcode(e);
                    Ok(LdapResponseState::Respond(sbr.gen_error(rc, msg)))
                }),
            ServerOps::Search(sr) => match uat {
                Some(u) => self
                    .do_search(idms, &sr, &u)
                    .await
                    .map(LdapResponseState::MultiPartResponse)
                    .or_else(|e| {
                        let (rc, msg) = operationerr_to_ldapresultcode(e);
                        Ok(LdapResponseState::Respond(sr.gen_error(rc, msg)))
                    }),
                None => {
                    // Search can occur without a bind, so bind first.
                    let lbt = match self.do_bind(idms, "", "").await {
                        Ok(Some(lbt)) => lbt,
                        Ok(None) => {
                            return Ok(LdapResponseState::Respond(
                                sr.gen_error(LdapResultCode::InvalidCredentials, "".to_string()),
                            ))
                        }
                        Err(e) => {
                            let (rc, msg) = operationerr_to_ldapresultcode(e);
                            return Ok(LdapResponseState::Respond(sr.gen_error(rc, msg)));
                        }
                    };
                    // If okay, do the search.
                    self.do_search(idms, &sr, &lbt)
                        .await
                        .map(|r| LdapResponseState::BindMultiPartResponse(lbt, r))
                        .or_else(|e| {
                            let (rc, msg) = operationerr_to_ldapresultcode(e);
                            Ok(LdapResponseState::Respond(sr.gen_error(rc, msg)))
                        })
                }
            },
            ServerOps::Unbind(_) => {
                // No need to notify on unbind (per rfc4511)
                Ok(LdapResponseState::Unbind)
            }
            ServerOps::Whoami(wr) => match uat {
                Some(u) => Ok(LdapResponseState::Respond(
                    wr.gen_success(format!("u: {}", u.spn).as_str()),
                )),
                None => Ok(LdapResponseState::Respond(wr.gen_operror(
                    format!("Unbound Connection {:?}", &eventid).as_str(),
                ))),
            },
        } // end match server op
    }
}

fn ldap_domain_to_dc(input: &str) -> String {
    let mut output: String = String::new();
    input.split('.').for_each(|dc| {
        output.push_str("dc=");
        output.push_str(dc);
        #[allow(clippy::single_char_pattern, clippy::single_char_add_str)]
        output.push_str(",");
    });
    // Remove the last ','
    output.pop();
    output
}

fn operationerr_to_ldapresultcode(e: OperationError) -> (LdapResultCode, String) {
    match e {
        OperationError::InvalidRequestState => {
            (LdapResultCode::ConstraintViolation, "".to_string())
        }
        OperationError::InvalidAttributeName(s) | OperationError::InvalidAttribute(s) => {
            (LdapResultCode::InvalidAttributeSyntax, s)
        }
        OperationError::SchemaViolation(se) => {
            (LdapResultCode::UnwillingToPerform, format!("{:?}", se))
        }
        e => (LdapResultCode::Other, format!("{:?}", e)),
    }
}

#[inline]
pub(crate) fn ldap_all_vattrs() -> Vec<String> {
    vec![
        "entryuuid".to_string(),
        "objectclass".to_string(),
        "entrydn".to_string(),
        "email".to_string(),
        "emailaddress".to_string(),
        "keys".to_string(),
        "sshpublickey".to_string(),
        "cn".to_string(),
        "uidnumber".to_string(),
    ]
}

#[inline]
pub(crate) fn ldap_vattr_map(input: &str) -> &str {
    // ⚠️  WARNING ⚠️
    // If you modify this list you MUST add these values to
    // corresponding phantom attributes in the schema to prevent
    // incorrect future or duplicate usage.
    //
    //   LDAP NAME     KANI ATTR SOURCE NAME
    match input {
        "entryuuid" => "uuid",
        "objectclass" => "class",
        "email" => "mail",
        "emailaddress" => "mail",
        "keys" => "ssh_publickey",
        "sshpublickey" => "ssh_publickey",
        "cn" => "name",
        "uidnumber" => "gidnumber",
        a => a,
    }
}

#[inline]
pub(crate) fn ldap_attr_filter_map(input: &str) -> AttrString {
    AttrString::from(ldap_vattr_map(&input.to_lowercase()))
}

#[cfg(test)]
mod tests {
    // use crate::prelude::*;
    use std::str::FromStr;

    use async_std::task;
    use compact_jwt::{Jws, JwsUnverified};
    use hashbrown::HashSet;
    use kanidm_proto::v1::ApiToken;
    use ldap3_proto::proto::{LdapFilter, LdapOp, LdapSearchScope};
    use ldap3_proto::simple::*;

    use crate::event::{CreateEvent, ModifyEvent};
    use crate::idm::event::UnixPasswordChangeEvent;
    use crate::idm::serviceaccount::GenerateApiTokenEvent;
    use crate::ldap::{LdapServer, LdapSession};

    const TEST_PASSWORD: &'static str = "ntaoeuntnaoeuhraohuercahu😍";

    #[test]
    fn test_ldap_simple_bind() {
        run_idm_test!(
            |_qs: &QueryServer, idms: &IdmServer, _idms_delayed: &IdmServerDelayed| {
                let ldaps = LdapServer::new(idms).expect("failed to start ldap");

                let mut idms_prox_write = idms.proxy_write(duration_from_epoch_now());
                // make the admin a valid posix account
                let me_posix = unsafe {
                    ModifyEvent::new_internal_invalid(
                        filter!(f_eq("name", PartialValue::new_iname("admin"))),
                        ModifyList::new_list(vec![
                            Modify::Present(
                                AttrString::from("class"),
                                Value::new_class("posixaccount"),
                            ),
                            Modify::Present(AttrString::from("gidnumber"), Value::new_uint32(2001)),
                        ]),
                    )
                };
                assert!(idms_prox_write.qs_write.modify(&me_posix).is_ok());

                let pce = UnixPasswordChangeEvent::new_internal(&UUID_ADMIN, TEST_PASSWORD);

                assert!(idms_prox_write.set_unix_account_password(&pce).is_ok());
                assert!(idms_prox_write.commit().is_ok());

                let anon_t = task::block_on(ldaps.do_bind(idms, "", ""))
                    .unwrap()
                    .unwrap();
                assert!(anon_t.effective_session == LdapSession::UnixBind(UUID_ANONYMOUS));
                assert!(
                    task::block_on(ldaps.do_bind(idms, "", "test")).unwrap_err()
                        == OperationError::NotAuthenticated
                );

                // Now test the admin and various DN's
                let admin_t = task::block_on(ldaps.do_bind(idms, "admin", TEST_PASSWORD))
                    .unwrap()
                    .unwrap();
                assert!(admin_t.effective_session == LdapSession::UnixBind(UUID_ADMIN));
                let admin_t =
                    task::block_on(ldaps.do_bind(idms, "admin@example.com", TEST_PASSWORD))
                        .unwrap()
                        .unwrap();
                assert!(admin_t.effective_session == LdapSession::UnixBind(UUID_ADMIN));
                let admin_t = task::block_on(ldaps.do_bind(idms, STR_UUID_ADMIN, TEST_PASSWORD))
                    .unwrap()
                    .unwrap();
                assert!(admin_t.effective_session == LdapSession::UnixBind(UUID_ADMIN));
                let admin_t = task::block_on(ldaps.do_bind(
                    idms,
                    "name=admin,dc=example,dc=com",
                    TEST_PASSWORD,
                ))
                .unwrap()
                .unwrap();
                assert!(admin_t.effective_session == LdapSession::UnixBind(UUID_ADMIN));
                let admin_t = task::block_on(ldaps.do_bind(
                    idms,
                    "spn=admin@example.com,dc=example,dc=com",
                    TEST_PASSWORD,
                ))
                .unwrap()
                .unwrap();
                assert!(admin_t.effective_session == LdapSession::UnixBind(UUID_ADMIN));
                let admin_t = task::block_on(ldaps.do_bind(
                    idms,
                    format!("uuid={},dc=example,dc=com", STR_UUID_ADMIN).as_str(),
                    TEST_PASSWORD,
                ))
                .unwrap()
                .unwrap();
                assert!(admin_t.effective_session == LdapSession::UnixBind(UUID_ADMIN));

                let admin_t = task::block_on(ldaps.do_bind(idms, "name=admin", TEST_PASSWORD))
                    .unwrap()
                    .unwrap();
                assert!(admin_t.effective_session == LdapSession::UnixBind(UUID_ADMIN));
                let admin_t =
                    task::block_on(ldaps.do_bind(idms, "spn=admin@example.com", TEST_PASSWORD))
                        .unwrap()
                        .unwrap();
                assert!(admin_t.effective_session == LdapSession::UnixBind(UUID_ADMIN));
                let admin_t = task::block_on(ldaps.do_bind(
                    idms,
                    format!("uuid={}", STR_UUID_ADMIN).as_str(),
                    TEST_PASSWORD,
                ))
                .unwrap()
                .unwrap();
                assert!(admin_t.effective_session == LdapSession::UnixBind(UUID_ADMIN));

                let admin_t =
                    task::block_on(ldaps.do_bind(idms, "admin,dc=example,dc=com", TEST_PASSWORD))
                        .unwrap()
                        .unwrap();
                assert!(admin_t.effective_session == LdapSession::UnixBind(UUID_ADMIN));
                let admin_t = task::block_on(ldaps.do_bind(
                    idms,
                    "admin@example.com,dc=example,dc=com",
                    TEST_PASSWORD,
                ))
                .unwrap()
                .unwrap();
                assert!(admin_t.effective_session == LdapSession::UnixBind(UUID_ADMIN));
                let admin_t = task::block_on(ldaps.do_bind(
                    idms,
                    format!("{},dc=example,dc=com", STR_UUID_ADMIN).as_str(),
                    TEST_PASSWORD,
                ))
                .unwrap()
                .unwrap();
                assert!(admin_t.effective_session == LdapSession::UnixBind(UUID_ADMIN));

                // Bad password, check last to prevent softlocking of the admin account.
                assert!(task::block_on(ldaps.do_bind(idms, "admin", "test"))
                    .unwrap()
                    .is_none());

                // Non-existant and invalid DNs
                assert!(task::block_on(ldaps.do_bind(
                    idms,
                    "spn=admin@example.com,dc=clownshoes,dc=example,dc=com",
                    TEST_PASSWORD
                ))
                .is_err());
                assert!(task::block_on(ldaps.do_bind(
                    idms,
                    "spn=claire@example.com,dc=example,dc=com",
                    TEST_PASSWORD
                ))
                .is_err());
                assert!(
                    task::block_on(ldaps.do_bind(idms, ",dc=example,dc=com", TEST_PASSWORD))
                        .is_err()
                );
                assert!(
                    task::block_on(ldaps.do_bind(idms, "dc=example,dc=com", TEST_PASSWORD))
                        .is_err()
                );

                assert!(task::block_on(ldaps.do_bind(idms, "claire", "test")).is_err());
            }
        )
    }

    macro_rules! assert_entry_contains {
        (
            $e:expr,
            $dn:expr,
            $($item:expr),*
        ) => {{
            assert!($e.dn == $dn);
            // Build a set from the attrs.
            let mut attrs = HashSet::new();
            for a in $e.attributes.iter() {
                for v in a.vals.iter() {
                    attrs.insert((a.atype.as_str(), v.as_str()));
                }
            };
            $(
                assert!(attrs.contains(&(
                    $item.0, $item.1
                )));
            )*

        }};
    }

    #[test]
    fn test_ldap_virtual_attribute_generation() {
        run_idm_test!(
            |_qs: &QueryServer, idms: &IdmServer, _idms_delayed: &IdmServerDelayed| {
                let ldaps = LdapServer::new(idms).expect("failed to start ldap");

                let ssh_ed25519 = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAeGW1P6Pc2rPq0XqbRaDKBcXZUPRklo0L1EyR30CwoP william@amethyst";

                // Setup a user we want to check.
                {
                    let e1 = entry_init!(
                        ("class", Value::new_class("object")),
                        ("class", Value::new_class("person")),
                        ("class", Value::new_class("account")),
                        ("class", Value::new_class("posixaccount")),
                        ("name", Value::new_iname("testperson1")),
                        (
                            "uuid",
                            Value::new_uuids("cc8e95b4-c24f-4d68-ba54-8bed76f63930").expect("uuid")
                        ),
                        ("description", Value::new_utf8s("testperson1")),
                        ("displayname", Value::new_utf8s("testperson1")),
                        ("gidnumber", Value::new_uint32(12345678)),
                        ("loginshell", Value::new_iutf8("/bin/zsh")),
                        ("ssh_publickey", Value::new_sshkey_str("test", ssh_ed25519))
                    );

                    let server_txn = idms.proxy_write(duration_from_epoch_now());
                    let ce = CreateEvent::new_internal(vec![e1]);
                    assert!(server_txn
                        .qs_write
                        .create(&ce)
                        .and_then(|_| server_txn.commit())
                        .is_ok());
                }

                // Setup the anonymous login.
                let anon_t = task::block_on(ldaps.do_bind(idms, "", ""))
                    .unwrap()
                    .unwrap();
                assert!(anon_t.effective_session == LdapSession::UnixBind(UUID_ANONYMOUS));

                // Check that when we request *, we get default list.
                let sr = SearchRequest {
                    msgid: 1,
                    base: "dc=example,dc=com".to_string(),
                    scope: LdapSearchScope::Subtree,
                    filter: LdapFilter::Equality("name".to_string(), "testperson1".to_string()),
                    attrs: vec!["*".to_string()],
                };
                let r1 = task::block_on(ldaps.do_search(idms, &sr, &anon_t)).unwrap();

                // The result, and the ldap proto success msg.
                assert!(r1.len() == 2);
                match &r1[0].op {
                    LdapOp::SearchResultEntry(lsre) => {
                        assert_entry_contains!(
                            lsre,
                            "spn=testperson1@example.com,dc=example,dc=com",
                            ("class", "object"),
                            ("class", "person"),
                            ("class", "account"),
                            ("class", "posixaccount"),
                            ("displayname", "testperson1"),
                            ("name", "testperson1"),
                            ("gidnumber", "12345678"),
                            ("loginshell", "/bin/zsh"),
                            ("ssh_publickey", ssh_ed25519),
                            ("uuid", "cc8e95b4-c24f-4d68-ba54-8bed76f63930")
                        );
                    }
                    _ => assert!(false),
                };

                // Check that when we request +, we get all attrs and the vattrs
                let sr = SearchRequest {
                    msgid: 1,
                    base: "dc=example,dc=com".to_string(),
                    scope: LdapSearchScope::Subtree,
                    filter: LdapFilter::Equality("name".to_string(), "testperson1".to_string()),
                    attrs: vec!["+".to_string()],
                };
                let r1 = task::block_on(ldaps.do_search(idms, &sr, &anon_t)).unwrap();

                // The result, and the ldap proto success msg.
                assert!(r1.len() == 2);
                match &r1[0].op {
                    LdapOp::SearchResultEntry(lsre) => {
                        assert_entry_contains!(
                            lsre,
                            "spn=testperson1@example.com,dc=example,dc=com",
                            ("objectclass", "object"),
                            ("objectclass", "person"),
                            ("objectclass", "account"),
                            ("objectclass", "posixaccount"),
                            ("displayname", "testperson1"),
                            ("name", "testperson1"),
                            ("gidnumber", "12345678"),
                            ("loginshell", "/bin/zsh"),
                            ("ssh_publickey", ssh_ed25519),
                            ("entryuuid", "cc8e95b4-c24f-4d68-ba54-8bed76f63930"),
                            ("entrydn", "spn=testperson1@example.com,dc=example,dc=com"),
                            ("uidnumber", "12345678"),
                            ("cn", "testperson1"),
                            ("keys", ssh_ed25519)
                        );
                    }
                    _ => assert!(false),
                };

                // Check that when we request an attr by name, we get all of them correctly.
                let sr = SearchRequest {
                    msgid: 1,
                    base: "dc=example,dc=com".to_string(),
                    scope: LdapSearchScope::Subtree,
                    filter: LdapFilter::Equality("name".to_string(), "testperson1".to_string()),
                    attrs: vec![
                        "name".to_string(),
                        "entrydn".to_string(),
                        "keys".to_string(),
                        "uidnumber".to_string(),
                    ],
                };
                let r1 = task::block_on(ldaps.do_search(idms, &sr, &anon_t)).unwrap();

                // The result, and the ldap proto success msg.
                assert!(r1.len() == 2);
                match &r1[0].op {
                    LdapOp::SearchResultEntry(lsre) => {
                        assert_entry_contains!(
                            lsre,
                            "spn=testperson1@example.com,dc=example,dc=com",
                            ("name", "testperson1"),
                            ("entrydn", "spn=testperson1@example.com,dc=example,dc=com"),
                            ("uidnumber", "12345678"),
                            ("keys", ssh_ed25519)
                        );
                    }
                    _ => assert!(false),
                };
            }
        )
    }

    #[test]
    fn test_ldap_token_privilege_granting() {
        run_idm_test!(
            |_qs: &QueryServer, idms: &IdmServer, _idms_delayed: &IdmServerDelayed| {
                // Setup the ldap server
                let ldaps = LdapServer::new(idms).expect("failed to start ldap");

                // Prebuild the search req we'll be using this test.
                let sr = SearchRequest {
                    msgid: 1,
                    base: "dc=example,dc=com".to_string(),
                    scope: LdapSearchScope::Subtree,
                    filter: LdapFilter::Equality("name".to_string(), "testperson1".to_string()),
                    attrs: vec!["name".to_string(), "mail".to_string()],
                };

                let sa_uuid = uuid::uuid!("cc8e95b4-c24f-4d68-ba54-8bed76f63930");

                // Configure the user account that will have the tokens issued.
                // Should be a SERVICE account.
                let apitoken = {
                    // Create a service account,

                    let e1 = entry_init!(
                        ("class", Value::new_class("object")),
                        ("class", Value::new_class("service_account")),
                        ("class", Value::new_class("account")),
                        ("uuid", Value::new_uuid(sa_uuid)),
                        ("name", Value::new_iname("service_permission_test")),
                        ("displayname", Value::new_utf8s("service_permission_test"))
                    );

                    // Setup a person with an email
                    let e2 = entry_init!(
                        ("class", Value::new_class("object")),
                        ("class", Value::new_class("person")),
                        ("class", Value::new_class("account")),
                        ("class", Value::new_class("posixaccount")),
                        ("name", Value::new_iname("testperson1")),
                        (
                            "mail",
                            Value::EmailAddress("testperson1@example.com".to_string(), true)
                        ),
                        ("description", Value::new_utf8s("testperson1")),
                        ("displayname", Value::new_utf8s("testperson1")),
                        ("gidnumber", Value::new_uint32(12345678)),
                        ("loginshell", Value::new_iutf8("/bin/zsh"))
                    );

                    // Setup an access control for the service account to view mail attrs.

                    let ct = duration_from_epoch_now();

                    let server_txn = idms.proxy_write(ct);
                    let ce = CreateEvent::new_internal(vec![e1, e2]);
                    assert!(server_txn.qs_write.create(&ce).is_ok());

                    // idm_people_read_priv
                    let me = unsafe {
                        ModifyEvent::new_internal_invalid(
                            filter!(f_eq(
                                "name",
                                PartialValue::new_iname("idm_people_read_priv")
                            )),
                            ModifyList::new_list(vec![Modify::Present(
                                AttrString::from("member"),
                                Value::new_refer(sa_uuid),
                            )]),
                        )
                    };
                    assert!(server_txn.qs_write.modify(&me).is_ok());

                    // Issue a token
                    // make it purpose = ldap <- currently purpose isn't supported,
                    // it's an idea for future.
                    let gte = GenerateApiTokenEvent::new_internal(sa_uuid, "TestToken", None);

                    let apitoken = server_txn
                        .service_account_generate_api_token(&gte, ct)
                        .expect("Failed to create new apitoken");

                    assert!(server_txn.commit().is_ok());

                    apitoken
                };

                // assert the token fails on non-ldap events token-xchg <- currently
                // we don't have purpose so this isn't tested.

                // Bind with anonymous, search and show mail attr isn't accessible.
                let anon_lbt = task::block_on(ldaps.do_bind(idms, "", ""))
                    .unwrap()
                    .unwrap();
                assert!(anon_lbt.effective_session == LdapSession::UnixBind(UUID_ANONYMOUS));

                let r1 = task::block_on(ldaps.do_search(idms, &sr, &anon_lbt)).unwrap();
                assert!(r1.len() == 2);
                match &r1[0].op {
                    LdapOp::SearchResultEntry(lsre) => {
                        assert_entry_contains!(
                            lsre,
                            "spn=testperson1@example.com,dc=example,dc=com",
                            ("name", "testperson1")
                        );
                    }
                    _ => assert!(false),
                };

                // Inspect the token to get its uuid out.
                let apitoken_unverified =
                    JwsUnverified::from_str(&apitoken).expect("Failed to parse apitoken");

                let apitoken_inner: Jws<ApiToken> = apitoken_unverified
                    .validate_embeded()
                    .expect("Embedded jwk not found");

                let apitoken_inner = apitoken_inner.into_inner();

                // Bind using the token
                let sa_lbt = task::block_on(ldaps.do_bind(idms, "", &apitoken))
                    .unwrap()
                    .unwrap();
                assert!(sa_lbt.effective_session == LdapSession::ApiToken(apitoken_inner.clone()));

                // Search and retrieve mail that's now accessible.
                let r1 = task::block_on(ldaps.do_search(idms, &sr, &sa_lbt)).unwrap();
                assert!(r1.len() == 2);
                match &r1[0].op {
                    LdapOp::SearchResultEntry(lsre) => {
                        assert_entry_contains!(
                            lsre,
                            "spn=testperson1@example.com,dc=example,dc=com",
                            ("name", "testperson1"),
                            ("mail", "testperson1@example.com")
                        );
                    }
                    _ => assert!(false),
                };
            }
        )
    }
}