From 7bb570eb8ca0bedde9d970b80c05d050454c6a32 Mon Sep 17 00:00:00 2001 From: Ron Waldon-Howe Date: Sat, 16 Nov 2024 09:02:51 +1100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Extract=20XML-specific=20d?= =?UTF-8?q?ata=20structures?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/dbus2/busd/pull/159#discussion_r1842070138 and https://github.com/dbus2/busd/pull/159#discussion_r1842093715 --- src/config.rs | 141 ++++------------------------------------------ src/config/xml.rs | 139 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 129 deletions(-) create mode 100644 src/config/xml.rs diff --git a/src/config.rs b/src/config.rs index 701938b..16dce24 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,6 +4,12 @@ use anyhow::{Error, Result}; use quick_xml::{events::Event, Reader}; use serde::Deserialize; +mod xml; + +use xml::{ + Document, Element, PolicyContext, PolicyElement, RuleAttributes, RuleElement, TypeElement, +}; + const EXPECTED_DOCTYPE_PARTS: &[&str] = &[ "busconfig", "PUBLIC", @@ -27,7 +33,12 @@ pub enum Access { Deny, } -/// implements [`dbus-daemon`'s Configuration File](https://dbus.freedesktop.org/doc/dbus-daemon.1.html#configuration_file) +/// The bus configuration. +/// +/// This is currently only loaded from the [XML configuration files] defined by the specification. +/// We plan to add support for other formats (e.g JSON) in the future. +/// +/// [XML configuration files]: https://dbus.freedesktop.org/doc/dbus-daemon.1.html#configuration_file #[derive(Clone, Debug, Default, Deserialize, PartialEq)] #[serde(try_from = "Document")] pub struct BusConfig { @@ -255,39 +266,6 @@ pub enum BusType { System, } -#[derive(Clone, Debug, Default, Deserialize, PartialEq)] -struct Document { - #[serde(rename = "$value", default)] - busconfig: Vec, -} - -#[derive(Clone, Debug, Deserialize, PartialEq)] -#[serde(rename_all = "snake_case")] -enum Element { - AllowAnonymous, - Auth(String), - Fork, - KeepUmask, - // TODO: support `` TODO: support `` - Listen(String), - Limit, - Pidfile(PathBuf), - Policy(PolicyElement), - Servicedir(PathBuf), - Servicehelper(PathBuf), - /// Requests a standard set of session service directories. - /// Its effect is similar to specifying a series of elements for each of the data - /// directories, in the order given here. - StandardSessionServicedirs, - /// Specifies the standard system-wide activation directories that should be searched for - /// service files. - StandardSystemServicedirs, - Syslog, - Type(TypeElement), - User(String), -} - #[derive(Clone, Debug, Default, PartialEq)] pub struct Limits { /// total size in bytes of messages incoming from a single connection @@ -474,27 +452,6 @@ impl TryFrom for OptionalPolicy { } } -#[derive(Clone, Debug, Deserialize, PartialEq)] -#[serde(rename_all = "snake_case")] -enum PolicyContext { - Default, - Mandatory, -} - -#[derive(Clone, Debug, Default, Deserialize, PartialEq)] -struct PolicyElement { - #[serde(rename = "@at_console")] - at_console: Option, - #[serde(rename = "@context")] - context: Option, - #[serde(rename = "@group")] - group: Option, - #[serde(rename = "$value", default)] - rules: Vec, - #[serde(rename = "@user")] - user: Option, -} - #[derive(Clone, Debug, PartialEq)] pub struct ReceiveOperation { pub error: String, @@ -637,74 +594,6 @@ fn rules_try_from_rule_elements(value: Vec) -> Result> { pub type Rule = (Access, Operation); -#[derive(Clone, Debug, Default, Deserialize, PartialEq)] -struct RuleAttributes { - #[serde(rename = "@max_fds")] - max_fds: Option, - #[serde(rename = "@min_fds")] - min_fds: Option, - - #[serde(rename = "@receive_error")] - receive_error: Option, - #[serde(rename = "@receive_interface")] - receive_interface: Option, - /// deprecated and ignored - #[serde(rename = "@receive_member")] - receive_member: Option, - #[serde(rename = "@receive_path")] - receive_path: Option, - #[serde(rename = "@receive_sender")] - receive_sender: Option, - #[serde(rename = "@receive_type")] - receive_type: Option, - - #[serde(rename = "@send_broadcast")] - send_broadcast: Option, - #[serde(rename = "@send_destination")] - send_destination: Option, - #[serde(rename = "@send_destination_prefix")] - send_destination_prefix: Option, - #[serde(rename = "@send_error")] - send_error: Option, - #[serde(rename = "@send_interface")] - send_interface: Option, - /// deprecated and ignored: https://github.com/dbus2/busd/issues/79 - #[serde(rename = "@send_member")] - send_member: Option, - #[serde(rename = "@send_path")] - send_path: Option, - #[serde(rename = "@send_type")] - send_type: Option, - - /// deprecated and ignored - #[serde(rename = "@receive_requested_reply")] - receive_requested_reply: Option, - /// deprecated and ignored - #[serde(rename = "@send_requested_reply")] - send_requested_reply: Option, - - /// deprecated and ignored - #[serde(rename = "@eavesdrop")] - eavesdrop: Option, - - #[serde(rename = "@own")] - own: Option, - #[serde(rename = "@own_prefix")] - own_prefix: Option, - - #[serde(rename = "@group")] - group: Option, - #[serde(rename = "@user")] - user: Option, -} - -#[derive(Clone, Debug, Deserialize, PartialEq)] -#[serde(rename_all = "snake_case")] -enum RuleElement { - Allow(RuleAttributes), - Deny(RuleAttributes), -} - #[derive(Clone, Debug, PartialEq)] pub struct SendOperation { pub broadcast: Option, @@ -748,12 +637,6 @@ impl From for SendOperation { } } -#[derive(Clone, Debug, Deserialize, PartialEq)] -struct TypeElement { - #[serde(rename = "$text")] - r#type: BusType, -} - #[cfg(test)] mod tests { use super::*; diff --git a/src/config/xml.rs b/src/config/xml.rs new file mode 100644 index 0000000..83f62e1 --- /dev/null +++ b/src/config/xml.rs @@ -0,0 +1,139 @@ +use std::path::PathBuf; + +use serde::Deserialize; + +use super::{BusType, MessageType}; + +/// The bus configuration. +/// +/// This is currently only loaded from the [XML configuration files] defined by the specification. +/// We plan to add support for other formats (e.g JSON) in the future. +/// +/// [XML configuration files]: https://dbus.freedesktop.org/doc/dbus-daemon.1.html#configuration_file +#[derive(Clone, Debug, Default, Deserialize, PartialEq)] +pub struct Document { + #[serde(rename = "$value", default)] + pub busconfig: Vec, +} + +#[derive(Clone, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum Element { + AllowAnonymous, + Auth(String), + Fork, + KeepUmask, + // TODO: support `` TODO: support `` + Listen(String), + Limit, + Pidfile(PathBuf), + Policy(PolicyElement), + Servicedir(PathBuf), + Servicehelper(PathBuf), + /// Requests a standard set of session service directories. + /// Its effect is similar to specifying a series of elements for each of the data + /// directories, in the order given here. + StandardSessionServicedirs, + /// Specifies the standard system-wide activation directories that should be searched for + /// service files. + StandardSystemServicedirs, + Syslog, + Type(TypeElement), + User(String), +} + +#[derive(Clone, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum PolicyContext { + Default, + Mandatory, +} + +#[derive(Clone, Debug, Default, Deserialize, PartialEq)] +pub struct PolicyElement { + #[serde(rename = "@at_console")] + pub at_console: Option, + #[serde(rename = "@context")] + pub context: Option, + #[serde(rename = "@group")] + pub group: Option, + #[serde(rename = "$value", default)] + pub rules: Vec, + #[serde(rename = "@user")] + pub user: Option, +} + +#[derive(Clone, Debug, Default, Deserialize, PartialEq)] +pub struct RuleAttributes { + #[serde(rename = "@max_fds")] + pub max_fds: Option, + #[serde(rename = "@min_fds")] + pub min_fds: Option, + + #[serde(rename = "@receive_error")] + pub receive_error: Option, + #[serde(rename = "@receive_interface")] + pub receive_interface: Option, + /// deprecated and ignored + #[serde(rename = "@receive_member")] + pub receive_member: Option, + #[serde(rename = "@receive_path")] + pub receive_path: Option, + #[serde(rename = "@receive_sender")] + pub receive_sender: Option, + #[serde(rename = "@receive_type")] + pub receive_type: Option, + + #[serde(rename = "@send_broadcast")] + pub send_broadcast: Option, + #[serde(rename = "@send_destination")] + pub send_destination: Option, + #[serde(rename = "@send_destination_prefix")] + pub send_destination_prefix: Option, + #[serde(rename = "@send_error")] + pub send_error: Option, + #[serde(rename = "@send_interface")] + pub send_interface: Option, + /// deprecated and ignored: https://github.com/dbus2/busd/issues/79 + #[serde(rename = "@send_member")] + pub send_member: Option, + #[serde(rename = "@send_path")] + pub send_path: Option, + #[serde(rename = "@send_type")] + pub send_type: Option, + + /// deprecated and ignored + #[serde(rename = "@receive_requested_reply")] + pub receive_requested_reply: Option, + /// deprecated and ignored + #[serde(rename = "@send_requested_reply")] + pub send_requested_reply: Option, + + /// deprecated and ignored + #[serde(rename = "@eavesdrop")] + pub eavesdrop: Option, + + #[serde(rename = "@own")] + pub own: Option, + #[serde(rename = "@own_prefix")] + pub own_prefix: Option, + + #[serde(rename = "@group")] + pub group: Option, + #[serde(rename = "@user")] + pub user: Option, +} + +#[derive(Clone, Debug, Deserialize, PartialEq)] +#[serde(rename_all = "snake_case")] +pub enum RuleElement { + Allow(RuleAttributes), + Deny(RuleAttributes), +} + +#[derive(Clone, Debug, Deserialize, PartialEq)] +pub struct TypeElement { + #[serde(rename = "$text")] + pub r#type: BusType, +}