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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Souk - service_permission.rs
// Copyright (C) 2022-2023  Felix Häcker <haeckerfelix@gnome.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use glib::{ParamSpec, Properties};
use gtk::glib;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use lazy_static::lazy_static;
use once_cell::unsync::OnceCell;

use crate::main::context::{SkContextDetail, SkContextDetailKind, SkContextDetailLevel};
use crate::main::flatpak::permissions::{PermissionDetails, SkPermissionSummary};
use crate::main::i18n::{i18n, i18n_f};

lazy_static! {
    static ref SENSITIVE_SERVICES: Vec<&'static str> = vec![
        "org.gnome.SessionManager",
        "org.freedesktop.PackageKit",
        "org.freedesktop.NetworkManager",
        "org.freedesktop.UDisks2",
        "ca.desrt.dconf",
        "org.gnome.SettingsDaemon",
        "org.freedesktop.secrets",
        "org.freedesktop.Flatpak"
    ];
}

mod imp {
    use super::*;

    #[derive(Debug, Default, Properties)]
    #[properties(wrapper_type = super::SkServicePermission)]
    pub struct SkServicePermission {
        #[property(get, set, construct_only)]
        name: OnceCell<String>,
        #[property(get, set, construct_only)]
        is_system: OnceCell<bool>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for SkServicePermission {
        const NAME: &'static str = "SkServicePermission";
        type Type = super::SkServicePermission;
    }

    impl ObjectImpl for SkServicePermission {
        fn properties() -> &'static [ParamSpec] {
            Self::derived_properties()
        }

        fn property(&self, id: usize, pspec: &ParamSpec) -> glib::Value {
            Self::derived_property(self, id, pspec)
        }

        fn set_property(&self, id: usize, value: &glib::Value, pspec: &ParamSpec) {
            Self::derived_set_property(self, id, value, pspec)
        }
    }
}

glib::wrapper! {
    pub struct SkServicePermission(ObjectSubclass<imp::SkServicePermission>);
}

impl SkServicePermission {
    pub fn new(name: &str, is_system: bool) -> Self {
        glib::Object::builder()
            .property("name", name)
            .property("is-system", is_system)
            .build()
    }

    pub fn no_access_context() -> SkContextDetail {
        let kind = SkContextDetailKind::Icon;
        let icon_name = "system-run-symbolic".to_string();
        let level = SkContextDetailLevel::Good;

        let title = i18n("No Access to Services");
        let description = i18n("Does not communicate with any service");

        SkContextDetail::new(kind, &icon_name, level, &title, &description)
    }
}

impl PermissionDetails for SkServicePermission {
    fn summary(&self) -> SkPermissionSummary {
        let mut summary = SkPermissionSummary::empty();

        if SENSITIVE_SERVICES
            .iter()
            .any(|i| self.name().starts_with(i))
        {
            summary |= SkPermissionSummary::READWRITE_DATA;
        }

        if self.name().starts_with("org.freedesktop.Flatpak") {
            summary |= SkPermissionSummary::SANDBOX_ESCAPE;
        }

        summary
    }

    fn context_details(&self) -> Vec<SkContextDetail> {
        let kind = SkContextDetailKind::Icon;
        let icon_name = "system-run-symbolic".to_string();
        let mut level = if !self.is_system() {
            SkContextDetailLevel::Neutral
        } else {
            SkContextDetailLevel::Moderate
        };
        let mut title = if !self.is_system() {
            i18n_f("Access to “{}” Service", &[&self.name()])
        } else {
            i18n_f("Access to System Service “{}”", &[&self.name()])
        };
        let mut description = i18n("Allows sending commands to the service and sharing data");

        // Well known dbus services

        if self.name().starts_with("org.gnome.SettingsDaemon") {
            title = i18n("Access to System Settings Service");
            description = i18n("Can read and modify system settings");
        }

        if self.name().starts_with("org.gnome.SessionManager") {
            title = i18n("Access to Session Manager Service");
            description = i18n("Has access to the current user session and open applications");
        }

        if self.name().starts_with("org.freedesktop.PackageKit") {
            title = i18n("Access to System Package Management");
            description = i18n("Can install, uninstall or update system packages");
        }

        if self.name().starts_with("org.freedesktop.Flatpak") {
            title = i18n("Access to Flatpak Service");
            description = i18n("Can execute arbitrary commands on the host system");
        }

        if self.name().starts_with("org.freedesktop.secrets") {
            title = i18n("Access to Password/Key Management Service");
            description = i18n("Can read, edit or delete passwords and keys");
        }

        if self.name().starts_with("org.freedesktop.FileManager") {
            title = i18n("Access to File Manager Service");
            description = i18n("Can open folders or files");
        }

        if self.name().starts_with("org.freedesktop.NetworkManager") {
            title = i18n("Access to System Network Service");
            description = i18n("Can read and modify network settings");
        }

        if self.name().starts_with("org.freedesktop.ScreenSaver") {
            title = i18n("Access to Screen Saver Service");
            description = i18n("Can read and modify screen saver settings");
        }

        if self.name().starts_with("org.freedesktop.Avahi") {
            title = i18n("Access to Avahi Network Service");
            description = i18n("Can publish or find information on the local network");
        }

        if self.name().starts_with("org.freedesktop.UPower") {
            title = i18n("Access to Power Management Service");
            description = i18n("Can read information about the current power consumption / status");
        }

        if self.name().starts_with("org.freedesktop.UDisks2") {
            title = i18n("Access to Disks Management Service");
            description = i18n("Can access, mount, unmount, or edit disk volumes");
        }

        if self.name().starts_with("ca.desrt.dconf") {
            title = i18n("Access to System Settings Database Service");
            description = i18n("Can read and modify system / application settings");
        }

        if SENSITIVE_SERVICES
            .iter()
            .any(|i| self.name().starts_with(i))
        {
            level = SkContextDetailLevel::Bad;
        }

        vec![SkContextDetail::new(
            kind,
            &icon_name,
            level,
            &title,
            &description,
        )]
    }
}