Debian Codegraph @

src/fdosecrets/dbus/DBusClient.h

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 #ifndef KEEPASSXC_FDOSECRETS_DBUSCLIENT_H
20 #define KEEPASSXC_FDOSECRETS_DBUSCLIENT_H
21
22 #include <QPointer>
23 #include <QSet>
24 #include <QUuid>
25
26 #include "core/Global.h"
27
28 namespace FdoSecrets
29 {
30 class DBusMgr;
31 class CipherPair;
32
33 struct ProcInfo
34 {
35 uint pid;
36 uint ppid;
37 QString exePath;
38 QString name;
39 QString command;
40
41 bool operator==(const ProcInfo& other) const;
42 bool operator!=(const ProcInfo& other) const;
43 };
44
45 /**
46 * Contains info representing a process.
47 * This can be obtained by DBusMgr::serviceInfo given a dbus address.
48 */
49 struct PeerInfo
50 {
51 /**
52 * @brief DBus address
53 */
54 QString address;
55
56 uint pid;
57 /**
58 * @brief Whether current process' exePath points to a valid executable file.
59 *
60 * Note that an empty exePath is not valid.
61 */
62 bool valid;
63
64 /**
65 * @brief List of parents of the process.
66 *
67 * The first element is the current process. The last element is usually PID 1.
68 *
69 * This is for showing to the user only and is intentionally simple.
70 * Getting detailed process info is beyond the scope of KPXC.
71 */
72 QList<ProcInfo> hierarchy;
73
74 QString exePath() const
75 {
76 return hierarchy.front().exePath;
77 }
78
79 bool operator==(const PeerInfo& other) const;
80 bool operator!=(const PeerInfo& other) const;
81 };
82
83 /**
84 * Represent a client that has made requests to our service. A client is identified by its
85 * DBus address, which is guaranteed to be unique by the DBus protocol.
86 *
87 * An object of this class is created on the first request and destroyed
88 * when the client address vanishes from the bus. DBus guarantees that the
89 * client address is not reused.
90 *
91 * One client may have multiple `Session`s with our service, and this class
92 * manages the negotiation state (if any) of ciphers and per-client authorization
93 * status.
94 */
95 class DBusClient
96 {
97 public:
98 /**
99 * @brief Given peer's service address, construct a client object
100 * @param address obtained from `QDBusMessage::service()`
101 * @param process the process info
102 */
103 explicit DBusClient(DBusMgr* dbus, PeerInfo process);
104
105 DBusMgr* dbus() const;
106
107 /**
108 * @return The human readable client name, usually the process name
109 */
110 QString name() const;
111
112 /**
113 * @return The unique DBus address of the client
114 */
115 QString address() const
116 {
117 return m_process.address;
118 }
119
120 /**
121 * @return The process id of the client
122 */
123 uint pid() const
124 {
125 return m_process.pid;
126 }
127
128 /**
129 * @return The process info
130 */
131 const PeerInfo& processInfo() const
132 {
133 return m_process;
134 }
135
136 QSharedPointer<CipherPair>
137 negotiateCipher(const QString& algorithm, const QVariant& input, QVariant& output, bool& incomplete);
138
139 /**
140 * Check if the item is known in this client's auth list
141 */
142 bool itemKnown(const QUuid& uuid) const;
143
144 /**
145 * Check if client may access item identified by @a uuid.
146 */
147 bool itemAuthorized(const QUuid& uuid) const;
148
149 /**
150 * Check if client may access item identified by @a uuid, and also reset any once auth.
151 */
152 bool itemAuthorizedResetOnce(const QUuid& uuid);
153
154 /**
155 * Authorize client to access item identified by @a uuid.
156 */
157 void setItemAuthorized(const QUuid& uuid, AuthDecision auth);
158
159 /**
160 * Authorize client to access all items.
161 */
162 void setAllAuthorized(AuthDecision authorized);
163
164 /**
165 * Forget all previous authorization.
166 */
167 void clearAuthorization();
168
169 /**
170 * Forcefully disconnect the client.
171 * Force close any remaining session, and cleanup negotiation states
172 */
173 void disconnectDBus();
174
175 private:
176 QPointer<DBusMgr> m_dbus;
177 PeerInfo m_process;
178
179 AuthDecision m_authorizedAll{AuthDecision::Undecided};
180
181 QSet<QUuid> m_allowed{};
182 QSet<QUuid> m_denied{};
183
184 QSet<QUuid> m_allowedOnce{};
185 QSet<QUuid> m_deniedOnce{};
186 };
187
188 using DBusClientPtr = QSharedPointer<DBusClient>;
189 } // namespace FdoSecrets
190 Q_DECLARE_METATYPE(FdoSecrets::DBusClientPtr);
191
192 #endif // KEEPASSXC_FDOSECRETS_DBUSCLIENT_H