From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <f.ebner@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 E5A5F717BE;
 Fri, 11 Jun 2021 13:44:47 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id B940A10DD7;
 Fri, 11 Jun 2021 13:44:42 +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))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id E6EDF10909;
 Fri, 11 Jun 2021 13:44:27 +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 C1D1D42EDF;
 Fri, 11 Jun 2021 13:44:27 +0200 (CEST)
From: Fabian Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com,
	pbs-devel@lists.proxmox.com
Date: Fri, 11 Jun 2021 13:44:10 +0200
Message-Id: <20210611114418.28772-22-f.ebner@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20210611114418.28772-1-f.ebner@proxmox.com>
References: <20210611114418.28772-1-f.ebner@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.821 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
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [mod.rs, proxmox.com, repositories.rs, lib.rs]
Subject: [pve-devel] [PATCH v6 pve-rs 1/4] initial commit
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Fri, 11 Jun 2021 11:44:47 -0000

with a module for wrapping apt repository functions.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
---

Changes from v5:
    * Start with a fresh repository.
    * Add comments to exported functions.
    * Make exported functions pub.
    * Use Option<&str> for the digest parameter.

 .cargo/config           |  5 +++
 .gitignore              |  3 ++
 Cargo.toml              | 22 +++++++++++++
 rustfmt.toml            |  1 +
 src/apt/mod.rs          |  1 +
 src/apt/repositories.rs | 69 +++++++++++++++++++++++++++++++++++++++++
 src/lib.rs              |  1 +
 7 files changed, 102 insertions(+)
 create mode 100644 .cargo/config
 create mode 100644 .gitignore
 create mode 100644 Cargo.toml
 create mode 100644 rustfmt.toml
 create mode 100644 src/apt/mod.rs
 create mode 100644 src/apt/repositories.rs
 create mode 100644 src/lib.rs

diff --git a/.cargo/config b/.cargo/config
new file mode 100644
index 0000000..3b5b6e4
--- /dev/null
+++ b/.cargo/config
@@ -0,0 +1,5 @@
+[source]
+[source.debian-packages]
+directory = "/usr/share/cargo/registry"
+[source.crates-io]
+replace-with = "debian-packages"
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..52e46cb
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+/target
+Cargo.lock
+/PVE
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..d3b9955
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,22 @@
+[package]
+name = "pve-rs"
+version = "0.1.0"
+authors = ["Proxmox Support Team <support@proxmox.com>"]
+edition = "2018"
+license = "AGPL-3"
+description = "PVE parts which have been ported to Rust"
+homepage = "https://www.proxmox.com"
+exclude = [
+    "build",
+    "debian",
+    "PVE",
+]
+
+[lib]
+crate-type = [ "cdylib" ]
+
+[dependencies]
+anyhow = "1.0"
+proxmox = { version = "0.11.5" }
+proxmox-apt = "0.1.1"
+perlmod = { version = "0.4.3", features = [ "exporter" ] }
diff --git a/rustfmt.toml b/rustfmt.toml
new file mode 100644
index 0000000..32a9786
--- /dev/null
+++ b/rustfmt.toml
@@ -0,0 +1 @@
+edition = "2018"
diff --git a/src/apt/mod.rs b/src/apt/mod.rs
new file mode 100644
index 0000000..574c1a7
--- /dev/null
+++ b/src/apt/mod.rs
@@ -0,0 +1 @@
+mod repositories;
diff --git a/src/apt/repositories.rs b/src/apt/repositories.rs
new file mode 100644
index 0000000..1643a23
--- /dev/null
+++ b/src/apt/repositories.rs
@@ -0,0 +1,69 @@
+#[perlmod::package(name = "PVE::RS::APT::Repositories", lib = "pve_rs")]
+mod export {
+    use anyhow::{bail, Error};
+
+    use perlmod::{to_value, Value};
+
+    /// Returns all APT repositories configured in `/etc/apt/sources.list` and
+    /// in `/etc/apt/sources.list.d` including disabled repositories.
+    ///
+    /// The first return value is the list of successfully parsed files of type `APTRepositoryFile`.
+    /// The second return value is the list of problems of type `APTRepositoryFileError`.
+    /// The third return value is a common digest of all successfully parsed files as a hexadecimal
+    /// string.
+    #[export(raw_return)]
+    pub fn repositories() -> Result<(Value, Value, Value), Error> {
+        let (files, errors) = proxmox_apt::repositories::repositories()?;
+
+        if files.is_empty() {
+            bail!("no APT repository files could be parsed!");
+        }
+
+        let common_digest = proxmox_apt::repositories::common_digest(&files);
+
+        let hex_digest = proxmox::tools::digest_to_hex(&common_digest);
+
+        Ok((
+            to_value(&files)?,
+            to_value(&errors)?,
+            to_value(&hex_digest)?,
+        ))
+    }
+
+    /// Provides additional information about the repositories.
+    ///
+    /// If `digest` is specified and doesn't match the current one, an error is returned.
+    ///
+    /// The first return value is the list of informations of type `APTRepositoryInfo`.
+    /// The second (resp. third) return values is a boolean indicating whether the enterprise
+    /// (resp. no-subscription) repository is configured.
+    #[export(raw_return)]
+    pub fn check_repositories(digest: Option<&str>) -> Result<(Value, Value, Value), Error> {
+        let (files, _) = proxmox_apt::repositories::repositories()?;
+
+        if files.is_empty() {
+            bail!("no APT repository files could be parsed!");
+        }
+
+        if let Some(digest) = digest {
+            let expected_digest = proxmox::tools::hex_to_digest(digest)?;
+            let current_digest = proxmox_apt::repositories::common_digest(&files);
+            if current_digest != expected_digest {
+                bail!("detected modified configuration - file changed by other user? Try again.");
+            }
+        }
+
+        let infos = proxmox_apt::repositories::check_repositories(&files);
+
+        let enterprise_enabled =
+            proxmox_apt::repositories::enterprise_repository_enabled(&files, "pve");
+        let no_subscription_enabled =
+            proxmox_apt::repositories::no_subscription_repository_enabled(&files, "pve");
+
+        Ok((
+            to_value(&infos)?,
+            to_value(&enterprise_enabled)?,
+            to_value(&no_subscription_enabled)?,
+        ))
+    }
+}
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..10b3376
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1 @@
+pub mod apt;
-- 
2.20.1