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
// Souk - socket_permission.rs
// Copyright (C) 2022  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 gtk::glib;

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

#[glib::flags(name = "SkSocketPermission")]
pub enum SkSocketPermission {
    #[flags_value(name = "none")]
    NONE = 1 << 0,
    #[flags_value(name = "unknown")]
    UNKNOWN = 1 << 1,
    #[flags_value(name = "x11")]
    X11 = 1 << 2,
    #[flags_value(name = "wayland")]
    WAYLAND = 1 << 3,
    #[flags_value(name = "fallback-x11")]
    FALLBACK_X11 = 1 << 4,
    #[flags_value(name = "pulseaudio")]
    PULSEAUDIO = 1 << 5,
    #[flags_value(name = "system-bus")]
    SYSTEM_BUS = 1 << 6,
    #[flags_value(name = "session-bus")]
    SESSION_BUS = 1 << 7,
    #[flags_value(name = "ssh-auth")]
    SSH_AUTH = 1 << 8,
    #[flags_value(name = "pcsc")]
    PCSC = 1 << 9,
    #[flags_value(name = "cups")]
    CUPS = 1 << 10,
}

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

        if self.contains(Self::X11) && !self.contains(Self::FALLBACK_X11) {
            summary |= SkPermissionSummary::READ_DATA;
        }

        if self.contains(Self::SESSION_BUS) {
            summary |= SkPermissionSummary::FULL_SESSION_BUS_ACCESS;
        }

        if self.contains(Self::SYSTEM_BUS) {
            summary |= SkPermissionSummary::FULL_SYSTEM_BUS_ACCESS;
        }

        if self.contains(Self::SSH_AUTH) {
            summary |= SkPermissionSummary::READ_DATA;
        }

        if self.contains(Self::PCSC) {
            summary |= SkPermissionSummary::READ_DATA;
        }

        if self.contains(Self::UNKNOWN) {
            summary |= SkPermissionSummary::UNKNOWN;
        }

        summary
    }

    fn context_details(&self) -> Vec<SkContextDetail> {
        let mut details = Vec::new();
        let kind = SkContextDetailKind::Icon;
        let level = SkContextDetailLevel::Bad;

        if self.contains(Self::X11) && !self.contains(Self::FALLBACK_X11) {
            let icon_name = "computer-symbolic";
            let title = i18n("Legacy Windowing System");
            let description = i18n("Uses a legacy windowing system, can access the contents of other windows and intercept keyboard inputs");

            details.push(SkContextDetail::new(
                kind,
                icon_name,
                level,
                &title,
                &description,
            ));
        }

        if self.contains(Self::SESSION_BUS) {
            let icon_name = "system-run-symbolic";
            let title = i18n("Access to All Session Services");
            let description = i18n("Has access to all session services");

            details.push(SkContextDetail::new(
                kind,
                icon_name,
                level,
                &title,
                &description,
            ));
        }

        if self.contains(Self::SYSTEM_BUS) {
            let icon_name = "system-run-symbolic";
            let title = i18n("Access to All System Services");
            let description = i18n("Has access to all system services");

            details.push(SkContextDetail::new(
                kind,
                icon_name,
                level,
                &title,
                &description,
            ));
        }

        if self.contains(Self::SSH_AUTH) {
            let icon_name = "dialog-password-symbolic";
            let title = i18n("Access to SSH Authentication");
            let description = i18n("Can access SSH keys / can perform authentications");

            details.push(SkContextDetail::new(
                kind,
                icon_name,
                level,
                &title,
                &description,
            ));
        }

        if self.contains(Self::PCSC) {
            let icon_name = "dialog-password-symbolic";
            let title = i18n("Access to Smartcards / Security Keys");
            let description = i18n("Can access smartcards and security devices");

            details.push(SkContextDetail::new(
                kind,
                icon_name,
                level,
                &title,
                &description,
            ));
        }

        if self.contains(Self::CUPS) {
            let icon_name = "printer-symbolic";
            let level = SkContextDetailLevel::Neutral;
            let title = i18n("Access to Printer Management");
            let description = i18n("Unrestricted access to printer management");

            details.push(SkContextDetail::new(
                kind,
                icon_name,
                level,
                &title,
                &description,
            ));
        }

        if self.contains(Self::UNKNOWN) {
            let icon_name = "dialog-question-symbolic";
            let level = SkContextDetailLevel::Bad;
            let title = i18n("Access to Unknown Socket");
            let description = i18n("Can access an unknown socket");

            details.push(SkContextDetail::new(
                kind,
                icon_name,
                level,
                &title,
                &description,
            ));
        }

        details
    }
}

impl From<&str> for SkSocketPermission {
    fn from(value: &str) -> Self {
        match value {
            "x11" => Self::X11,
            "wayland" => Self::WAYLAND,
            "fallback-x11" => Self::FALLBACK_X11,
            "pulseaudio" => Self::PULSEAUDIO,
            "system-bus" => Self::SYSTEM_BUS,
            "session-bus" => Self::SESSION_BUS,
            "ssh-auth" => Self::SSH_AUTH,
            "pcsc" => Self::PCSC,
            "cups" => Self::CUPS,
            _ => Self::UNKNOWN,
        }
    }
}