Debian Codegraph @

src/fdosecrets/dbus/DBusClient.cpp

raw
1 /*
2 * Copyright (C) 2020 Aetf <[email protected]>
3 * Copyright (C) 2020 Jan Klötzke <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 or (at your option)
8 * version 3 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "DBusClient.h"
20
21 #include "fdosecrets/FdoSecretsSettings.h"
22 #include "fdosecrets/dbus/DBusMgr.h"
23 #include "fdosecrets/objects/SessionCipher.h"
24
25 #include <utility>
26
27 namespace FdoSecrets
28 {
29 bool ProcInfo::operator==(const ProcInfo& other) const
30 {
31 return this->pid == other.pid && this->ppid == other.ppid && this->exePath == other.exePath
32 && this->name == other.name && this->command == other.command;
33 }
34
35 bool ProcInfo::operator!=(const ProcInfo& other) const
36 {
37 return !(*this == other);
38 }
39
40 bool PeerInfo::operator==(const PeerInfo& other) const
41 {
42 return this->address == other.address && this->pid == other.pid && this->valid == other.valid
43 && this->hierarchy == other.hierarchy;
44 }
45
46 bool PeerInfo::operator!=(const PeerInfo& other) const
47 {
48 return !(*this == other);
49 }
50
51 DBusClient::DBusClient(DBusMgr* dbus, PeerInfo process)
52 : m_dbus(dbus)
53 , m_process(std::move(process))
54 {
55 }
56
57 DBusMgr* DBusClient::dbus() const
58 {
59 return m_dbus;
60 }
61
62 QString DBusClient::name() const
63 {
64 auto exePath = m_process.exePath();
65 if (exePath.isEmpty()) {
66 return QObject::tr("unknown executable (DBus address %1)").arg(m_process.address);
67 }
68 if (!m_process.valid) {
69 return QObject::tr("%1 (invalid executable path)").arg(exePath);
70 }
71 return exePath;
72 }
73
74 bool DBusClient::itemKnown(const QUuid& uuid) const
75 {
76 return m_authorizedAll != AuthDecision::Undecided || m_allowed.contains(uuid) || m_allowedOnce.contains(uuid)
77 || m_denied.contains(uuid) || m_deniedOnce.contains(uuid);
78 }
79
80 bool DBusClient::itemAuthorized(const QUuid& uuid) const
81 {
82 if (!FdoSecrets::settings()->confirmAccessItem()) {
83 // everyone is authorized if this is not enabled
84 return true;
85 }
86
87 // check if we have catch-all decision
88 if (m_authorizedAll == AuthDecision::Allowed) {
89 return true;
90 }
91 if (m_authorizedAll == AuthDecision::Denied) {
92 return false;
93 }
94
95 // individual decisions
96 if (m_deniedOnce.contains(uuid) || m_denied.contains(uuid)) {
97 // explicitly denied
98 return false;
99 }
100 if (m_allowedOnce.contains(uuid) || m_allowed.contains(uuid)) {
101 // explicitly allowed
102 return true;
103 }
104 // haven't asked, not authorized by default
105 return false;
106 }
107
108 bool DBusClient::itemAuthorizedResetOnce(const QUuid& uuid)
109 {
110 auto auth = itemAuthorized(uuid);
111 m_deniedOnce.remove(uuid);
112 m_allowedOnce.remove(uuid);
113 return auth;
114 }
115
116 void DBusClient::setItemAuthorized(const QUuid& uuid, AuthDecision auth)
117 {
118 // uuid should only be in exactly one set at any time
119 m_allowed.remove(uuid);
120 m_allowedOnce.remove(uuid);
121 m_denied.remove(uuid);
122 m_deniedOnce.remove(uuid);
123 switch (auth) {
124 case AuthDecision::Allowed:
125 m_allowed.insert(uuid);
126 break;
127 case AuthDecision::AllowedOnce:
128 m_allowedOnce.insert(uuid);
129 break;
130 case AuthDecision::Denied:
131 m_denied.insert(uuid);
132 break;
133 case AuthDecision::DeniedOnce:
134 m_deniedOnce.insert(uuid);
135 break;
136 default:
137 break;
138 }
139 }
140
141 void DBusClient::setAllAuthorized(AuthDecision authorized)
142 {
143 // once variants doesn't make sense here
144 if (authorized == AuthDecision::AllowedOnce) {
145 authorized = AuthDecision::Allowed;
146 }
147 if (authorized == AuthDecision::DeniedOnce) {
148 authorized = AuthDecision::Denied;
149 }
150 m_authorizedAll = authorized;
151 }
152
153 void DBusClient::clearAuthorization()
154 {
155 m_authorizedAll = AuthDecision::Undecided;
156 m_allowed.clear();
157 m_allowedOnce.clear();
158 m_denied.clear();
159 m_deniedOnce.clear();
160 }
161
162 void DBusClient::disconnectDBus()
163 {
164 clearAuthorization();
165 // notify DBusMgr about the removal
166 m_dbus->removeClient(this);
167 }
168
169 QSharedPointer<CipherPair>
170 DBusClient::negotiateCipher(const QString& algorithm, const QVariant& input, QVariant& output, bool& incomplete)
171 {
172 incomplete = false;
173
174 QSharedPointer<CipherPair> cipher{};
175 if (algorithm == PlainCipher::Algorithm) {
176 cipher.reset(new PlainCipher);
177 } else if (algorithm == DhIetf1024Sha256Aes128CbcPkcs7::Algorithm) {
178 QByteArray clientPublicKey = input.toByteArray();
179 cipher.reset(new DhIetf1024Sha256Aes128CbcPkcs7(clientPublicKey));
180 } else {
181 // error notSupported
182 }
183
184 if (!cipher) {
185 return {};
186 }
187
188 if (!cipher->isValid()) {
189 qWarning() << "FdoSecrets: Error creating cipher";
190 return {};
191 }
192
193 output = cipher->negotiationOutput();
194 return cipher;
195 }
196 } // namespace FdoSecrets