use enumflags2::{bitflags, BitFlags};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use static_assertions::assert_impl_all;
use std::collections::HashMap;
use zbus_names::{
BusName, OwnedBusName, OwnedInterfaceName, OwnedUniqueName, UniqueName, WellKnownName,
};
use zvariant::{DeserializeDict, Optional, SerializeDict, Type};
use super::Result;
use crate::{proxy, OwnedGuid};
#[bitflags]
#[repr(u32)]
#[derive(Type, Debug, PartialEq, Eq, Copy, Clone, Serialize, Deserialize)]
pub enum RequestNameFlags {
AllowReplacement = 0x01,
ReplaceExisting = 0x02,
DoNotQueue = 0x04,
}
assert_impl_all!(RequestNameFlags: Send, Sync, Unpin);
#[repr(u32)]
#[derive(Deserialize_repr, Serialize_repr, Type, Debug, PartialEq, Eq)]
pub enum RequestNameReply {
PrimaryOwner = 0x01,
InQueue = 0x02,
Exists = 0x03,
AlreadyOwner = 0x04,
}
assert_impl_all!(RequestNameReply: Send, Sync, Unpin);
#[repr(u32)]
#[derive(Deserialize_repr, Serialize_repr, Type, Debug, PartialEq, Eq)]
pub enum ReleaseNameReply {
Released = 0x01,
NonExistent = 0x02,
NotOwner = 0x03,
}
assert_impl_all!(ReleaseNameReply: Send, Sync, Unpin);
#[derive(Debug, Default, DeserializeDict, PartialEq, Eq, SerializeDict, Type)]
#[zvariant(signature = "a{sv}")]
pub struct ConnectionCredentials {
#[zvariant(rename = "UnixUserID")]
pub(crate) unix_user_id: Option<u32>,
#[zvariant(rename = "UnixGroupIDs")]
pub(crate) unix_group_ids: Option<Vec<u32>>,
#[zvariant(rename = "ProcessID")]
pub(crate) process_id: Option<u32>,
#[zvariant(rename = "WindowsSID")]
pub(crate) windows_sid: Option<String>,
#[zvariant(rename = "LinuxSecurityLabel")]
pub(crate) linux_security_label: Option<Vec<u8>>,
}
impl ConnectionCredentials {
pub fn unix_user_id(&self) -> Option<u32> {
self.unix_user_id
}
pub fn unix_group_ids(&self) -> Option<&Vec<u32>> {
self.unix_group_ids.as_ref()
}
pub fn into_unix_group_ids(self) -> Option<Vec<u32>> {
self.unix_group_ids
}
pub fn process_id(&self) -> Option<u32> {
self.process_id
}
pub fn windows_sid(&self) -> Option<&String> {
self.windows_sid.as_ref()
}
pub fn into_windows_sid(self) -> Option<String> {
self.windows_sid
}
pub fn linux_security_label(&self) -> Option<&Vec<u8>> {
self.linux_security_label.as_ref()
}
pub fn into_linux_security_label(self) -> Option<Vec<u8>> {
self.linux_security_label
}
pub fn set_unix_user_id(mut self, unix_user_id: u32) -> Self {
self.unix_user_id = Some(unix_user_id);
self
}
pub fn add_unix_group_id(mut self, unix_group_id: u32) -> Self {
self.unix_group_ids
.get_or_insert_with(Vec::new)
.push(unix_group_id);
self
}
pub fn set_process_id(mut self, process_id: u32) -> Self {
self.process_id = Some(process_id);
self
}
pub fn set_windows_sid(mut self, windows_sid: String) -> Self {
self.windows_sid = Some(windows_sid);
self
}
pub fn set_linux_security_label(mut self, linux_security_label: Vec<u8>) -> Self {
self.linux_security_label = Some(linux_security_label);
self
}
}
#[proxy(
default_service = "org.freedesktop.DBus",
default_path = "/org/freedesktop/DBus",
interface = "org.freedesktop.DBus"
)]
pub trait DBus {
#[zbus(name = "AddMatch")]
fn add_match_rule(&self, rule: crate::MatchRule<'_>) -> Result<()>;
fn get_adt_audit_session_data(&self, bus_name: BusName<'_>) -> Result<Vec<u8>>;
fn get_connection_credentials(&self, bus_name: BusName<'_>) -> Result<ConnectionCredentials>;
#[zbus(name = "GetConnectionSELinuxSecurityContext")]
fn get_connection_selinux_security_context(&self, bus_name: BusName<'_>) -> Result<Vec<u8>>;
#[zbus(name = "GetConnectionUnixProcessID")]
fn get_connection_unix_process_id(&self, bus_name: BusName<'_>) -> Result<u32>;
fn get_connection_unix_user(&self, bus_name: BusName<'_>) -> Result<u32>;
fn get_id(&self) -> Result<OwnedGuid>;
fn get_name_owner(&self, name: BusName<'_>) -> Result<OwnedUniqueName>;
fn hello(&self) -> Result<OwnedUniqueName>;
fn list_activatable_names(&self) -> Result<Vec<OwnedBusName>>;
fn list_names(&self) -> Result<Vec<OwnedBusName>>;
fn list_queued_owners(&self, name: WellKnownName<'_>) -> Result<Vec<OwnedUniqueName>>;
fn name_has_owner(&self, name: BusName<'_>) -> Result<bool>;
fn release_name(&self, name: WellKnownName<'_>) -> Result<ReleaseNameReply>;
fn reload_config(&self) -> Result<()>;
#[zbus(name = "RemoveMatch")]
fn remove_match_rule(&self, rule: crate::MatchRule<'_>) -> Result<()>;
fn request_name(
&self,
name: WellKnownName<'_>,
flags: BitFlags<RequestNameFlags>,
) -> Result<RequestNameReply>;
fn start_service_by_name(&self, name: WellKnownName<'_>, flags: u32) -> Result<u32>;
fn update_activation_environment(&self, environment: HashMap<&str, &str>) -> Result<()>;
#[zbus(signal)]
fn name_owner_changed(
&self,
name: BusName<'_>,
old_owner: Optional<UniqueName<'_>>,
new_owner: Optional<UniqueName<'_>>,
);
#[zbus(signal)]
fn name_lost(&self, name: BusName<'_>);
#[zbus(signal)]
fn name_acquired(&self, name: BusName<'_>);
#[zbus(property)]
fn features(&self) -> Result<Vec<String>>;
#[zbus(property)]
fn interfaces(&self) -> Result<Vec<OwnedInterfaceName>>;
}
assert_impl_all!(DBusProxy<'_>: Send, Sync, Unpin);
#[cfg(feature = "blocking-api")]
assert_impl_all!(DBusProxyBlocking<'_>: Send, Sync, Unpin);