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
// Souk - app_permissions.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 gio::ListStore;
use glib::{KeyFile, ParamSpec, Properties};
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{gio, glib};
use lazy_static::lazy_static;
use once_cell::unsync::OnceCell;

use super::types::*;

lazy_static! {
    static ref SERVICE_WHITELIST: Vec<&'static str> = vec![
        "org.kde.StatusNotifier",
        "org.mpris.MediaPlayer",
        "org.freedesktop.Notifications",
        "com.canonical.AppMenu.Registrar",
        "com.canonical.indicator.application",
        "com.canonical.Unity.LauncherEntry",
        "org.a11y.Bus"
    ];
}

mod imp {
    use super::*;

    #[derive(Debug, Default, Properties)]
    #[properties(wrapper_type = super::SkAppPermissions)]
    pub struct SkAppPermissions {
        #[property(get, set, construct_only)]
        filesystems: OnceCell<ListStore>,
        #[property(get, set, construct_only)]
        services: OnceCell<ListStore>,
        #[property(get, set, construct_only)]
        devices: OnceCell<SkDevicePermission>,
        #[property(get, set, construct_only)]
        sockets: OnceCell<SkSocketPermission>,
        #[property(get, set, construct_only)]
        subsystems: OnceCell<SkSubsystemPermission>,
    }

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

    impl ObjectImpl for SkAppPermissions {
        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)
        }
    }

    impl SkAppPermissions {
        pub fn is_whitelisted(list: Vec<&str>, value: &str) -> bool {
            let res = list.iter().any(|i| value.starts_with(i));
            if res {
                debug!("Ignoring whitelisted permission entry: {}", value);
            }
            res
        }
    }
}

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

impl SkAppPermissions {
    pub fn new(
        filesystems: &ListStore,
        services: &ListStore,
        devices: &SkDevicePermission,
        sockets: &SkSocketPermission,
        subsystems: &SkSubsystemPermission,
    ) -> Self {
        glib::Object::builder()
            .property("filesystems", filesystems)
            .property("services", services)
            .property("devices", devices)
            .property("sockets", sockets)
            .property("subsystems", subsystems)
            .build()
    }

    pub fn from_metadata(keyfile: &KeyFile) -> Self {
        let filesystems = ListStore::new(SkFilesystemPermission::static_type());
        if let Ok(filesystem_list) = keyfile.string_list("Context", "filesystems") {
            for filesystem in filesystem_list {
                let value = SkFilesystemPermission::from_flatpak(filesystem.to_str());
                filesystems.append(&value);
            }
        }

        let services = ListStore::new(SkServicePermission::static_type());
        if let Ok(session_list) = keyfile.keys("Session Bus Policy") {
            for service in session_list {
                if imp::SkAppPermissions::is_whitelisted(
                    SERVICE_WHITELIST.to_vec(),
                    service.to_str(),
                ) {
                    continue;
                }
                let value = SkServicePermission::new(service.to_str(), false);
                services.append(&value);
            }
        }
        if let Ok(system_list) = keyfile.keys("System Bus Policy") {
            for service in system_list {
                if imp::SkAppPermissions::is_whitelisted(
                    SERVICE_WHITELIST.to_vec(),
                    service.to_str(),
                ) {
                    continue;
                }
                let value = SkServicePermission::new(service.to_str(), true);
                services.append(&value);
            }
        }

        let mut devices = SkDevicePermission::NONE;
        if let Ok(device_list) = keyfile.string_list("Context", "devices") {
            for device in device_list {
                devices |= device.to_str().into();
                devices.remove(SkDevicePermission::NONE);
            }
        }

        let mut sockets = SkSocketPermission::NONE;
        if let Ok(socket_list) = keyfile.string_list("Context", "sockets") {
            for socket in socket_list {
                sockets |= socket.to_str().into();
                sockets.remove(SkSocketPermission::NONE);
            }
        }

        let mut subsystems = SkSubsystemPermission::NONE;
        if let Ok(subsystem_list) = keyfile.string_list("Context", "shared") {
            for subsystem in subsystem_list {
                subsystems |= subsystem.to_str().into();
                subsystems.remove(SkSubsystemPermission::NONE);
            }
        }

        Self::new(&filesystems, &services, &devices, &sockets, &subsystems)
    }

    /// Compares with a different `SkAppPermissions` object, and returns the
    /// additional permissions which aren't in `self`
    pub fn additional_permissions(&self, other: &Self) -> Self {
        let devices = other.devices().difference(self.devices());
        let sockets = other.sockets().difference(self.sockets());
        let subsystems = other.subsystems().difference(self.subsystems());

        let filesystems = ListStore::new(SkFilesystemPermission::static_type());
        for filesystem in other.filesystems().snapshot() {
            let filesystem: SkFilesystemPermission = filesystem.downcast().unwrap();
            if !self.filesystems().snapshot().iter().any(|a| {
                let a: &SkFilesystemPermission = a.downcast_ref().unwrap();
                a.path() == filesystem.path() && a.kind() == filesystem.kind()
            }) {
                filesystems.append(&filesystem);
            }
        }

        let services = ListStore::new(SkServicePermission::static_type());
        for service in other.services().snapshot() {
            let service: SkServicePermission = service.downcast().unwrap();
            if !self.services().snapshot().iter().any(|a| {
                let a: &SkServicePermission = a.downcast_ref().unwrap();
                a.name() == service.name() && a.is_system() == service.is_system()
            }) {
                services.append(&service);
            }
        }

        Self::new(&filesystems, &services, &devices, &sockets, &subsystems)
    }
}