1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//! # DBus interface proxy for: `net.openvpn.v3.sessions`
//!
//! This code was generated by `zbus-xmlgen` `3.1.0` from DBus introspection data.
//! Source: `net.openvpn.v3.sessions.xml`.

use self::constants::*;
use super::sessions_node::{SessionsNodeProxy, SessionsNodeProxyBlocking};
use crate::log::constants::{LogGroup, LogLevel};
use zbus::dbus_proxy;

#[dbus_proxy(
    interface = "net.openvpn.v3.sessions",
    default_service = "net.openvpn.v3.sessions",
    default_path = "/net/openvpn/v3/sessions"
)]
trait Sessions {
    /// FetchAvailableSessions method
    ///
    /// This method will return an array of object paths to session objects the caller is granted access to.
    fn fetch_available_sessions(&self) -> zbus::Result<Vec<zbus::zvariant::OwnedObjectPath>>;

    /// FetchManagedInterfaces method
    ///
    /// This method will return an array of strings containing the virtual network interfaces the session manager is handling. Only interfaces the calling user is granted access to manage will be returned.
    fn fetch_managed_interfaces(&self) -> zbus::Result<Vec<String>>;

    /// LookupConfigName method
    ///
    /// This method will return an array of paths to session objects the caller is granted access to, which where started with the configuration profile name provided to the method. The profile name in the session object will not change if it is changed in the configuration manager after the session has started.
    ///
    /// # Arguments
    ///
    /// * `config_name` - String containing the configuration profile name to lookup.
    #[dbus_proxy(object = "SessionsNode")]
    fn lookup_config_name(&self, config_name: &str);

    /// LookupInterface method
    ///
    /// This method will return the D-Bus path to session object related to the virtual network interface name being looked up. This method will also return paths to interfaces not managed by the user.
    ///
    /// # Arguments
    ///
    /// * `device_name` - String containing the interface name to lookup.
    #[dbus_proxy(object = "SessionsNode")]
    fn lookup_interface(&self, device_name: &str);

    /// NewTunnel method
    ///
    /// This starts a new VPN backend client process for a specific VPN configuration profile. This does not start the connection, it just starts a privileged client process and awaits further instructions. When this method call returns with a session path, it means the backend process have started.
    ///
    /// # Arguments
    ///
    /// * `config_path` -  A string containing the D-Bus object path of the VPN profile.
    ///
    /// # Returns
    /// A string containing a unique D-Bus object path to the created VPN session.
    #[dbus_proxy(object = "SessionsNode")]
    fn new_tunnel(&self, config_path: &zbus::zvariant::ObjectPath<'_>);

    /// TransferOwnership method
    ///
    /// This method transfers the ownership of a session to the given UID value. This feature is by design restricted to the `root` account only and is only expected to be used by `openvpn3-autoload` and similar tools.
    ///
    /// # Arguments
    ///
    /// * `path` - Session object path where to modify the owner property.
    /// * `new_owner_id` - UID value of the new session owner.
    fn transfer_ownership(
        &self,
        path: &zbus::zvariant::ObjectPath<'_>,
        new_owner_uid: u32,
    ) -> zbus::Result<()>;

    /// Log signal
    ///
    /// Whenever the configuration manager want to log something, it issues a Log signal which carries a log group, log verbosity level and a string with the log message itself.
    /// See the separate [logging documentation](https://github.com/OpenVPN/openvpn3-linux/blob/master/docs/dbus/dbus-logging.md) for details on this signal.
    #[dbus_proxy(signal)]
    fn log(&self, group: LogGroup, level: LogLevel, message: &str) -> zbus::Result<()>;

    /// SessionManagerEvent signal
    ///
    /// This signals is sent each time there is a change in regards to active VPN sessions on the system. This is a broadcast signal which is sent to all users on the system containing a bare minimum of details of the related VPN session object.
    #[dbus_proxy(signal)]
    fn session_manager_event(
        &self,
        path: zbus::zvariant::ObjectPath<'_>,
        event_type: EventType,
        owner: u32,
    ) -> zbus::Result<()>;

    /// version property
    #[dbus_proxy(property, name = "version")]
    fn version(&self) -> zbus::Result<String>;
}

pub mod constants {
    use std::fmt;

    use serde_repr::{Deserialize_repr, Serialize_repr};
    use static_assertions::assert_impl_all;
    use zbus::zvariant::Type;

    /// Session Manager Event Type
    ///
    /// Source: openvpn3-linux/src/sessionmgr/sessionmgr-events.hpp
    #[repr(u16)]
    #[derive(Deserialize_repr, Serialize_repr, Copy, Clone, Type, Debug, PartialEq, Eq)]
    pub enum EventType {
        /// Should not be used, identifies an uninitialized object or an error.
        Unset = 0,
        /// A new VPN session was created. It might not yet be started.
        SessCreated = 1,
        /// An existing session object was destroyed, the session was disconnected.
        SessDestroyed = 2,
    }

    assert_impl_all!(EventType: Send, Sync, Unpin);

    impl fmt::Display for EventType {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                Self::Unset => write!(f, "(unset)"),
                Self::SessCreated => write!(f, "Session Created"),
                Self::SessDestroyed => write!(f, "Session Destroyed"),
            }
        }
    }
}