From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 1A25A6C65A for ; Thu, 23 Sep 2021 12:10:38 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 10E9C23D27 for ; Thu, 23 Sep 2021 12:10:08 +0200 (CEST) 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) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id E80EB23D0C for ; Thu, 23 Sep 2021 12:10:06 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 6D4D844A77; Thu, 23 Sep 2021 12:10:00 +0200 (CEST) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Thu, 23 Sep 2021 12:09:56 +0200 Message-Id: <20210923100958.948762-1-dietmar@proxmox.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.588 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 SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox 1/3] add systemd escape_unit and unescape_unit X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 23 Sep 2021 10:10:38 -0000 --- proxmox/src/tools/mod.rs | 1 + proxmox/src/tools/systemd.rs | 83 ++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 proxmox/src/tools/systemd.rs diff --git a/proxmox/src/tools/mod.rs b/proxmox/src/tools/mod.rs index b172b09..b3a3cb0 100644 --- a/proxmox/src/tools/mod.rs +++ b/proxmox/src/tools/mod.rs @@ -20,6 +20,7 @@ pub mod serde; pub mod time; pub mod uuid; pub mod vec; +pub mod systemd; #[cfg(feature = "tfa")] pub mod tfa; diff --git a/proxmox/src/tools/systemd.rs b/proxmox/src/tools/systemd.rs new file mode 100644 index 0000000..68f8631 --- /dev/null +++ b/proxmox/src/tools/systemd.rs @@ -0,0 +1,83 @@ +use anyhow::{bail, Error}; + +fn parse_hex_digit(d: u8) -> Result { + if d >= b'0' && d <= b'9' { + return Ok(d - b'0'); + } + if d >= b'A' && d <= b'F' { + return Ok(d - b'A' + 10); + } + if d >= b'a' && d <= b'f' { + return Ok(d - b'a' + 10); + } + bail!("got invalid hex digit"); +} + +/// Escape strings for usage in systemd unit names +pub fn escape_unit(mut unit: &str, is_path: bool) -> String { + if is_path { + unit = unit.trim_matches('/'); + if unit.is_empty() { + return String::from("-"); + } + } + + let unit = unit.as_bytes(); + + let mut escaped = String::new(); + + for (i, c) in unit.iter().enumerate() { + if *c == b'/' { + escaped.push('-'); + continue; + } + if (i == 0 && *c == b'.') + || !(*c == b'_' + || *c == b'.' + || (*c >= b'0' && *c <= b'9') + || (*c >= b'A' && *c <= b'Z') + || (*c >= b'a' && *c <= b'z')) + { + escaped.push_str(&format!("\\x{:0x}", c)); + } else { + escaped.push(*c as char); + } + } + escaped +} + +/// Unescape strings used in systemd unit names +pub fn unescape_unit(text: &str) -> Result { + let mut i = text.as_bytes(); + + let mut data: Vec = Vec::new(); + + loop { + if i.is_empty() { + break; + } + let next = i[0]; + if next == b'\\' { + if i.len() < 4 { + bail!("short input"); + } + if i[1] != b'x' { + bail!("unkwnown escape sequence"); + } + let h1 = parse_hex_digit(i[2])?; + let h0 = parse_hex_digit(i[3])?; + data.push(h1 << 4 | h0); + i = &i[4..] + } else if next == b'-' { + data.push(b'/'); + i = &i[1..] + } else { + data.push(next); + i = &i[1..] + } + } + + let text = String::from_utf8(data)?; + + Ok(text) +} -- 2.30.2