[pbs-devel] [PATCH proxmox-backup 13/17] auth ldap: add `certificate-path` option
Lukas Wagner
l.wagner at proxmox.com
Tue Jan 3 15:23:04 CET 2023
This allows adding a custom root CA for TLS-encrypted
LDAP connections.
Note: this commit adds a direct depedencency to
the `native-tls` crate, on which we already dependeded
transitively.
Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
Cargo.toml | 2 ++
pbs-api-types/src/ldap.rs | 5 +++++
src/auth.rs | 7 +++++++
src/server/ldap.rs | 15 +++++++++++++--
4 files changed, 27 insertions(+), 2 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
index c9f1f185..7837bf39 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -122,6 +122,7 @@ ldap3 = { version = "0.11.0-beta.1", default_features=false, features=["tls"]}
libc = "0.2"
log = "0.4.17"
nix = "0.24"
+native-tls = "0.2.8"
nom = "7"
num-traits = "0.2"
once_cell = "1.3.1"
@@ -174,6 +175,7 @@ ldap3.workspace = true
libc.workspace = true
log.workspace = true
nix.workspace = true
+native-tls.workspace = true
nom.workspace = true
num-traits.workspace = true
once_cell.workspace = true
diff --git a/pbs-api-types/src/ldap.rs b/pbs-api-types/src/ldap.rs
index 672c81cd..99196c04 100644
--- a/pbs-api-types/src/ldap.rs
+++ b/pbs-api-types/src/ldap.rs
@@ -74,6 +74,11 @@ pub struct LdapRealmConfig {
/// Verify server certificate
#[serde(skip_serializing_if = "Option::is_none")]
pub verify: Option<bool>,
+ /// CA certificate to use for the server. If set,
+ /// the certificate stored at the given path will
+ /// be added to the set of trusted root CAs.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub certificate_path: Option<String>,
/// Bind domain to use for looking up users
#[serde(skip_serializing_if = "Option::is_none")]
pub bind_dn: Option<String>,
diff --git a/src/auth.rs b/src/auth.rs
index 101bec0e..46d6c56c 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -185,6 +185,12 @@ pub fn ldap_api_type_to_ldap_config(config: &LdapRealmConfig) -> Result<LdapConf
LdapMode::Ldaps => LdapConnectionMode::Ldaps,
};
+ let root_certificate = if let Some(path) = config.certificate_path.as_ref() {
+ Some(proxmox_sys::fs::file_read_string(path)?)
+ } else {
+ None
+ };
+
Ok(LdapConfig {
servers,
port: config.port,
@@ -194,5 +200,6 @@ pub fn ldap_api_type_to_ldap_config(config: &LdapRealmConfig) -> Result<LdapConf
bind_password: auth_helpers::get_ldap_bind_password(&config.realm)?,
tls_mode,
verify_certificate: config.verify.unwrap_or_default(),
+ root_certificate_pem: root_certificate,
})
}
diff --git a/src/server/ldap.rs b/src/server/ldap.rs
index 2e218cf6..8cc35181 100644
--- a/src/server/ldap.rs
+++ b/src/server/ldap.rs
@@ -5,6 +5,7 @@ use ldap3::{
adapters::{Adapter, EntriesOnly, PagedResults},
Ldap, LdapConnAsync, LdapConnSettings, LdapResult, Scope, SearchEntry,
};
+use native_tls::{Certificate, TlsConnector};
#[derive(PartialEq, Eq, Clone, Copy)]
/// LDAP connection security
@@ -36,6 +37,8 @@ pub struct LdapConfig {
pub tls_mode: LdapConnectionMode,
/// Verify the server's TLS certificate
pub verify_certificate: bool,
+ /// Custom TLS root certificiate
+ pub root_certificate_pem: Option<String>,
}
pub struct LdapConnection {
@@ -163,11 +166,19 @@ impl LdapConnection {
async fn try_connect(&self, url: &str) -> Result<(LdapConnAsync, Ldap), Error> {
let starttls = self.config.tls_mode == LdapConnectionMode::StartTls;
+ let mut connector_builder = TlsConnector::builder();
+ connector_builder.danger_accept_invalid_certs(!self.config.verify_certificate);
+
+ if let Some(certificate) = self.config.root_certificate_pem.as_deref() {
+ let cert = Certificate::from_pem(certificate.as_bytes())?;
+ connector_builder.add_root_certificate(cert);
+ }
+
LdapConnAsync::with_settings(
LdapConnSettings::new()
- .set_no_tls_verify(!self.config.verify_certificate)
.set_starttls(starttls)
- .set_conn_timeout(Self::LDAP_CONNECTION_TIMEOUT),
+ .set_conn_timeout(Self::LDAP_CONNECTION_TIMEOUT)
+ .set_connector(connector_builder.build()?),
url,
)
.await
--
2.30.2
More information about the pbs-devel
mailing list