From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <l.wagner@proxmox.com> Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 1B082963A9 for <pbs-devel@lists.proxmox.com>; Tue, 24 Jan 2023 11:04:12 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id E861F1C6C for <pbs-devel@lists.proxmox.com>; Tue, 24 Jan 2023 11:03:41 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for <pbs-devel@lists.proxmox.com>; Tue, 24 Jan 2023 11:03:40 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 5526345D91 for <pbs-devel@lists.proxmox.com>; Tue, 24 Jan 2023 11:03:40 +0100 (CET) From: Lukas Wagner <l.wagner@proxmox.com> To: pbs-devel@lists.proxmox.com Date: Tue, 24 Jan 2023 11:03:34 +0100 Message-Id: <20230124100337.152394-4-l.wagner@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230124100337.152394-1-l.wagner@proxmox.com> References: <20230124100337.152394-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 2.520 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_HI -5 Sender listed at https://www.dnswl.org/, high trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [lib.rs] Subject: [pbs-devel] [PATCH v3 proxmox 3/6] ldap: add helpers for constructing LDAP filters X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion <pbs-devel.lists.proxmox.com> List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe> List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/> List-Post: <mailto:pbs-devel@lists.proxmox.com> List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help> List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe> X-List-Received-Date: Tue, 24 Jan 2023 10:04:12 -0000 Signed-off-by: Lukas Wagner <l.wagner@proxmox.com> --- proxmox-ldap/src/lib.rs | 79 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 3 deletions(-) diff --git a/proxmox-ldap/src/lib.rs b/proxmox-ldap/src/lib.rs index 48eb863..ac164db 100644 --- a/proxmox-ldap/src/lib.rs +++ b/proxmox-ldap/src/lib.rs @@ -1,13 +1,12 @@ use std::{ + fmt::{Display, Formatter}, fs, path::{Path, PathBuf}, time::Duration, }; use anyhow::{bail, Error}; -use ldap3::{ - Ldap, LdapConnAsync, LdapConnSettings, LdapResult, Scope, SearchEntry, -}; +use ldap3::{Ldap, LdapConnAsync, LdapConnSettings, LdapResult, Scope, SearchEntry}; use native_tls::{Certificate, TlsConnector, TlsConnectorBuilder}; use serde::{Deserialize, Serialize}; @@ -226,3 +225,77 @@ impl LdapConnection { bail!("user not found") } } + +#[allow(dead_code)] +enum FilterElement<'a> { + And(Vec<FilterElement<'a>>), + Or(Vec<FilterElement<'a>>), + Condition(&'a str, &'a str), + Not(Box<FilterElement<'a>>), + Verbatim(&'a str), +} + +impl<'a> Display for FilterElement<'a> { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + fn write_children(f: &mut Formatter<'_>, children: &[FilterElement]) -> std::fmt::Result { + for child in children { + write!(f, "{child}")?; + } + + Ok(()) + } + + match self { + FilterElement::And(children) => { + write!(f, "(&")?; + write_children(f, children)?; + write!(f, ")")?; + } + FilterElement::Or(children) => { + write!(f, "(|")?; + write_children(f, children)?; + write!(f, ")")?; + } + FilterElement::Not(element) => { + write!(f, "(!{})", element)?; + } + FilterElement::Condition(attr, value) => { + write!(f, "({attr}={value})")?; + } + FilterElement::Verbatim(verbatim) => write!(f, "{verbatim}")?, + } + + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::FilterElement::*; + + #[test] + fn test_filter_elements_to_string() { + assert_eq!("(uid=john)", Condition("uid", "john").to_string()); + assert_eq!( + "(!(uid=john))", + Not(Box::new(Condition("uid", "john"))).to_string() + ); + + assert_eq!("(foo=bar)", &Verbatim("(foo=bar)").to_string()); + + let filter_string = And(vec![ + Condition("givenname", "john"), + Condition("sn", "doe"), + Or(vec![ + Condition("email", "john@foo"), + Condition("email", "john@bar"), + ]), + ]) + .to_string(); + + assert_eq!( + "(&(givenname=john)(sn=doe)(|(email=john@foo)(email=john@bar)))", + &filter_string + ); + } +} -- 2.30.2