Skip to main content

ldap_client/
error.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3use ldap_client_proto::ResultCode;
4
5#[derive(Debug, thiserror::Error)]
6#[non_exhaustive]
7pub enum Error {
8    #[error("I/O error: {0}")]
9    Io(#[from] std::io::Error),
10
11    #[error("BER error: {0}")]
12    Ber(#[from] ldap_client_ber::BerError),
13
14    #[error("protocol error: {0}")]
15    Proto(#[from] ldap_client_proto::ProtoError),
16
17    #[error("TLS error: {0}")]
18    Tls(#[from] rustls::Error),
19
20    #[error("invalid URL: {0}")]
21    InvalidUrl(String),
22
23    #[error("LDAP error: {code} - {message}")]
24    Ldap {
25        code: ResultCode,
26        message: String,
27        matched_dn: String,
28    },
29
30    #[error("referral to {}", urls.join(", "))]
31    Referral {
32        result: ldap_client_proto::LdapResult,
33        urls: Vec<String>,
34    },
35
36    #[error("connection closed")]
37    ConnectionClosed,
38
39    #[error("timeout")]
40    Timeout,
41
42    #[error("StartTLS failed: {0}")]
43    StartTls(String),
44
45    #[error("search returned multiple results when at most one was expected")]
46    MultipleResults,
47
48    #[error("referral hop limit exceeded")]
49    ReferralHopLimitExceeded,
50
51    #[error("search result entry limit exceeded ({0})")]
52    SearchEntryLimitExceeded(usize),
53
54    #[error("refusing an unauthenticated bind: the DN is set and the password is empty")]
55    UnauthenticatedBind,
56}
57
58impl Error {
59    pub fn ldap(result: &ldap_client_proto::LdapResult) -> Self {
60        Self::Ldap {
61            code: result.code,
62            message: result.diagnostic_message.clone(),
63            matched_dn: result.matched_dn.clone(),
64        }
65    }
66
67    /// A referral for a referral result, an LDAP error for any other.
68    pub(crate) fn from_failed_result(result: &ldap_client_proto::LdapResult) -> Self {
69        if result.code.is_referral() {
70            Self::Referral {
71                urls: result.referral.clone(),
72                result: result.clone(),
73            }
74        } else {
75            Self::ldap(result)
76        }
77    }
78
79    pub fn result_code(&self) -> Option<ResultCode> {
80        match self {
81            Self::Ldap { code, .. } => Some(*code),
82            Self::Referral { result, .. } => Some(result.code),
83            _ => None,
84        }
85    }
86}