public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Lukas Wagner <l.wagner@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup 13/17] auth ldap: add `certificate-path` option
Date: Tue,  3 Jan 2023 15:23:04 +0100	[thread overview]
Message-ID: <20230103142308.656240-14-l.wagner@proxmox.com> (raw)
In-Reply-To: <20230103142308.656240-1-l.wagner@proxmox.com>

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@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





  parent reply	other threads:[~2023-01-03 14:23 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-03 14:22 [pbs-devel] [PATCH-SERIES proxmox-{backup, widget-toolkit} 00/17] add LDAP realm support Lukas Wagner
2023-01-03 14:22 ` [pbs-devel] [PATCH proxmox-backup 01/17] pbs-config: add delete_authid to ACL-tree Lukas Wagner
2023-01-04 10:23   ` Wolfgang Bumiller
2023-01-03 14:22 ` [pbs-devel] [PATCH proxmox-backup 02/17] ui: add 'realm' field in user edit Lukas Wagner
2023-01-03 14:22 ` [pbs-devel] [PATCH proxmox-backup 03/17] api-types: add LDAP configuration type Lukas Wagner
2023-01-03 14:22 ` [pbs-devel] [PATCH proxmox-backup 04/17] api: add routes for managing LDAP realms Lukas Wagner
2023-01-04 11:16   ` Wolfgang Bumiller
2023-01-03 14:22 ` [pbs-devel] [PATCH proxmox-backup 05/17] auth: add LDAP module Lukas Wagner
2023-01-04 13:23   ` Wolfgang Bumiller
2023-01-09 10:52     ` Lukas Wagner
2023-01-03 14:22 ` [pbs-devel] [PATCH proxmox-backup 06/17] auth: add LDAP realm authenticator Lukas Wagner
2023-01-04 13:32   ` Wolfgang Bumiller
2023-01-04 14:48     ` Thomas Lamprecht
2023-01-09 11:00     ` Lukas Wagner
2023-01-03 14:22 ` [pbs-devel] [PATCH proxmox-backup 07/17] api-types: add config options for LDAP user sync Lukas Wagner
2023-01-04 13:40   ` Wolfgang Bumiller
2023-01-09 13:58     ` Lukas Wagner
2023-01-03 14:22 ` [pbs-devel] [PATCH proxmox-backup 08/17] server: add LDAP realm sync job Lukas Wagner
2023-01-03 14:23 ` [pbs-devel] [PATCH proxmox-backup 09/17] manager: add LDAP commands Lukas Wagner
2023-01-03 14:23 ` [pbs-devel] [PATCH proxmox-backup 10/17] manager: add sync command for LDAP realms Lukas Wagner
2023-01-04 13:56   ` Wolfgang Bumiller
2023-01-03 14:23 ` [pbs-devel] [PATCH proxmox-backup 11/17] docs: add configuration file reference for domains.cfg Lukas Wagner
2023-01-03 14:23 ` [pbs-devel] [PATCH proxmox-backup 12/17] docs: add documentation for LDAP realms Lukas Wagner
2023-01-03 14:23 ` Lukas Wagner [this message]
2023-01-03 14:23 ` [pbs-devel] [PATCH proxmox-widget-toolkit 14/17] auth ui: add LDAP realm edit panel Lukas Wagner
2023-01-03 14:23 ` [pbs-devel] [PATCH proxmox-widget-toolkit 15/17] auth ui: add LDAP sync UI Lukas Wagner
2023-01-03 14:23 ` [pbs-devel] [PATCH proxmox-widget-toolkit 16/17] auth ui: add `onlineHelp` for AuthEditLDAP Lukas Wagner
2023-01-03 14:23 ` [pbs-devel] [PATCH proxmox-widget-toolkit 17/17] auth ui: add `firstname` and `lastname` sync-attribute fields Lukas Wagner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230103142308.656240-14-l.wagner@proxmox.com \
    --to=l.wagner@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal