KWallet

kwalletd.h
1/*
2 SPDX-FileCopyrightText: 2024 Marco Martin <notmart@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include <QDBusContext>
10#include <QObject>
11
12#include <KConfigWatcher>
13
14#include "kwalletd_debug.h"
15
16struct Folder {
17 QString name;
18 QStringList passwords;
19 QStringList maps;
20};
21
22struct Wallet {
23 QString name;
24 QList<Folder> folders;
25};
26
27class SecretServiceClient;
28
29class KWalletD : public QObject, protected QDBusContext
30{
32public:
33 // Enum names and values from KWallet
34 enum EntryType {
35 Unknown = 0,
36 Password,
37 Stream,
38 Map,
39 Unused = 0xffff,
40 };
41 Q_ENUM(EntryType)
42 KWalletD(QObject *parent = nullptr);
43
44 ~KWalletD() override;
45
46public Q_SLOTS:
47 // Is the wallet enabled? If not, all open() calls fail.
48 bool isEnabled() const;
49 // Open and unlock the wallet
50 int open(const QString &wallet, qlonglong wId, const QString &appId);
51 // STUB: Open and unlock the wallet with this path
52 int openPath(const QString &path, qlonglong wId, const QString &appId);
53 // Open the wallet asynchronously
54 int openAsync(const QString &wallet, qlonglong wId, const QString &appId, bool handleSession);
55 // STUB: Open and unlock the wallet with this path asynchronously
56 int openPathAsync(const QString &path, qlonglong wId, const QString &appId, bool handleSession);
57
58 // Close and lock the wallet
59 // If force = true, will close it for all users. Behave. This
60 // can break applications, and is generally intended for use by
61 // the wallet manager app only.
62 int close(const QString &wallet, bool force);
63 int close(int handle, bool force, const QString &appId);
64
65 // STUB: Save to disk but leave open
66 Q_NOREPLY void sync(int handle, const QString &appId);
67
68 // Physically deletes the wallet from disk.
69 int deleteWallet(const QString &wallet);
70
71 // Returns true if the wallet is open
72 bool isOpen(const QString &wallet);
73 bool isOpen(int handle);
74
75 // List the users of this wallet
76 QStringList users(const QString &wallet) const;
77
78 // STUB: Change the password of this wallet
79 void changePassword(const QString &wallet, qlonglong wId, const QString &appId);
80
81 // A list of all wallets
82 QStringList wallets() const;
83
84 // A list of all folders in this wallet
85 QStringList folderList(int handle, const QString &appId);
86 // Does this wallet have this folder?
87 bool hasFolder(int handle, const QString &folder, const QString &appId);
88
89 // Create this folder
90 bool createFolder(int handle, const QString &folder, const QString &appId);
91
92 // Remove this folder
93 bool removeFolder(int handle, const QString &folder, const QString &appId);
94
95 // List of entries in this folder
96 QStringList entryList(int handle, const QString &folder, const QString &appId);
97
98 // Read an entry. If the entry does not exist, it just
99 // returns an empty result. It is your responsibility to check
100 // hasEntry() first.
101 QByteArray readEntry(int handle, const QString &folder, const QString &key, const QString &appId);
102 QByteArray readMap(int handle, const QString &folder, const QString &key, const QString &appId);
103 QString readPassword(int handle, const QString &folder, const QString &key, const QString &appId);
104
105 // Deprecated, use entriesList()
106 QVariantMap readEntryList(int handle, const QString &folder, const QString &key, const QString &appId);
107 // returns in a variantmap every key/value of a folder
108 QVariantMap entriesList(int handle, const QString &folder, const QString &appId);
109 // Deprecated, use mapList()
110 QVariantMap readMapList(int handle, const QString &folder, const QString &key, const QString &appId);
111 // Keys of the QVariantMap are the keys in the folder, values are the binary serialized maps of a key
112 QVariantMap mapList(int handle, const QString &folder, const QString &appId);
113 // use passwordList()
114 QVariantMap readPasswordList(int handle, const QString &folder, const QString &key, const QString &appId);
115 // Key = entry name in the folder Value = password cleartext
116 QVariantMap passwordList(int handle, const QString &folder, const QString &appId);
117
118 // Rename an entry. rc=0 on success.
119 int renameEntry(int handle, const QString &folder, const QString &oldName, const QString &newName, const QString &appId);
120
121 // Write an entry. rc=0 on success.
122 int writeEntry(int handle, const QString &folder, const QString &key, const QByteArray &value, int entryType, const QString &appId);
123 int writeEntry(int handle, const QString &folder, const QString &key, const QByteArray &value, const QString &appId);
124 int writeMap(int handle, const QString &folder, const QString &key, const QByteArray &value, const QString &appId);
125 int writePassword(int handle, const QString &folder, const QString &key, const QString &value, const QString &appId);
126
127 // Does the entry exist?
128 bool hasEntry(int handle, const QString &folder, const QString &key, const QString &appId);
129 // What type is the entry?
130 int entryType(int handle, const QString &folder, const QString &key, const QString &appId);
131 // Remove an entry. rc=0 on success.
132 int removeEntry(int handle, const QString &folder, const QString &key, const QString &appId);
133
134 // Disconnect an app from a wallet
135 bool disconnectApplication(const QString &wallet, const QString &application);
136
137 void reconfigure();
138
139 // Determine
140 bool folderDoesNotExist(const QString &wallet, const QString &folder);
141 bool keyDoesNotExist(const QString &wallet, const QString &folder, const QString &key);
142
143 // Everybody will need to call open() again
144 void closeAllWallets();
145
146 QString networkWallet();
147 QString localWallet();
148
149 // STUB: Open a wallet using a pre-hashed password. This is only useful in cooperation
150 // with the kwallet PAM module. It's also less secure than manually entering the
151 // password as the password hash is transmitted using D-Bus.
152 int pamOpen(const QString &wallet, const QByteArray &passwordHash, int sessionTimeout);
153
154protected:
155 int openInternal(const QString &wallet, qlonglong wId, const QString &appId);
156 // Migrate a single wallet, returns true on success
157 // sourceWallet is the wallet name on kwallet backend
158 // destWallet is the collection name on secretservice
159 bool migrateWallet(const QString &sourceWallet, const QString &destWallet);
160 // Migrates to external (3rd party) Secret Service. The internal migration of legacy items
161 // occurs separately in KWalletFreedesktopCollection::onWalletChangeState()
162 void migrateData();
163 // from an handle int representing a session to the open wallet name of that session
164 QString walletForHandle(int handle, const QString &appId);
165 // A folder as user readable name in the keychain in the format wallet/folder
166 QString folderPath(const QString &folder, const QString &key) const;
167 // Type of a key in a wallet and folder
168 EntryType keyType(const QString &wallet, const QString &folder, const QString &key);
169
170 QString readString(const QString &key, const QString &folder, const QString &wallet, bool *ok);
171 QByteArray readRawJson(const QString &key, const QString &folder, const QString &wallet, bool *ok);
172 QByteArray readBinary(const QString &key, const QString &folder, const QString &wallet, bool *ok);
173 void writeString(const QString &key, const QString &value, const QString &folder, const QString &wallet, bool *ok);
174 void writeBinary(const QString &key, const QByteArray &value, const QString &folder, const QString &wallet, bool *ok);
175 void writeRawJson(const QString &key, const QByteArray &value, const QString &folder, const QString &wallet, bool *ok);
176 void removeItem(const QString &key, const QString &folder, const QString &wallet, bool *ok);
177
178 void timerEvent(QTimerEvent *) override;
179
181 void walletAsyncOpened(int id, int handle); // used to notify KWallet::Wallet
182 void walletListDirty();
183 void walletCreated(const QString &wallet);
184 void walletOpened(const QString &wallet);
185 void walletDeleted(const QString &wallet);
186 void walletClosed(const QString &wallet); // clazy:exclude=overloaded-signal
187
188 void walletClosed(int handle); // clazy:exclude=overloaded-signal
189 // since 5.81
190 void walletClosedId(int handle);
191
192 void allWalletsClosed();
193 void folderListUpdated(const QString &wallet);
194 void folderUpdated(const QString &wallet, const QString &folder);
195 void entryUpdated(const QString &wallet, const QString &folder, const QString &key);
196 void entryRenamed(const QString &wallet, const QString &folder, const QString &oldName, const QString &newName);
197 void entryDeleted(const QString &wallet, const QString &folder, const QString &key);
198 void applicationDisconnected(const QString &wallet, const QString &application);
199
200private:
201 SecretServiceClient *m_backend;
202 // We need to store a structure here as well, because the api has createFolder that would make a folder without any keys
203 QMultiHash<QString, QString> m_structure;
204 QHash<QPair<int, QString>, QString> m_openWallets;
205 QHash<QPair<int, QString>, int> m_idleTimers;
206
207 bool m_enabled = true;
208 bool m_launchManager = false;
209 bool m_closeIdle = false;
210 // in minutes
211 int m_idleTime = 10 * 60 * 1000;
212 KConfigWatcher::Ptr m_configWatcher;
213
214 static unsigned int s_lastTransaction;
215};
QObject(QObject *parent)
Q_ENUM(...)
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 25 2025 11:53:00 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.