1 /*
2     This file is part of the KDE libraries
3     SPDX-FileCopyrightText: 2002-2004 George Staikos <staikos@kde.org>
4     SPDX-FileCopyrightText: 2008 Michael Leupold <lemma@confuego.org>
5 
6     SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #ifndef _KWALLETD_H_
10 #define _KWALLETD_H_
11 
12 #include "kwalletbackend.h"
13 #include <QDBusServiceWatcher>
14 #include <QHash>
15 #include <QPointer>
16 #include <QString>
17 #include <QtDBus>
18 #include <stdlib.h>
19 #include <time.h>
20 
21 #include "ktimeout.h"
22 #include "kwalletsessionstore.h"
23 
24 class KDirWatch;
25 class KTimeout;
26 
27 // @Private
28 class KWalletTransaction;
29 class KWalletSessionStore;
30 
31 class KWalletD : public QObject, protected QDBusContext
32 {
33     Q_OBJECT
34 
35 public:
36     KWalletD();
37     ~KWalletD() override;
38 
39 public Q_SLOTS:
40     // Is the wallet enabled?  If not, all open() calls fail.
41     bool isEnabled() const;
42 
43     // Open and unlock the wallet
44     int open(const QString &wallet, qlonglong wId, const QString &appid);
45 
46     // Open and unlock the wallet with this path
47     int openPath(const QString &path, qlonglong wId, const QString &appid);
48 
49     // Open the wallet asynchronously
50     int openAsync(const QString &wallet, qlonglong wId, const QString &appid, bool handleSession);
51 
52     // Open and unlock the wallet with this path asynchronously
53     int openPathAsync(const QString &path, qlonglong wId, const QString &appid, bool handleSession);
54 
55     // Close and lock the wallet
56     // If force = true, will close it for all users.  Behave.  This
57     // can break applications, and is generally intended for use by
58     // the wallet manager app only.
59     int close(const QString &wallet, bool force);
60     int close(int handle, bool force, const QString &appid);
61 
62     // Save to disk but leave open
63     Q_NOREPLY void sync(int handle, const QString &appid);
64 
65     // Physically deletes the wallet from disk.
66     int deleteWallet(const QString &wallet);
67 
68     // Returns true if the wallet is open
69     bool isOpen(const QString &wallet);
70     bool isOpen(int handle);
71 
72     // List the users of this wallet
73     QStringList users(const QString &wallet) const;
74 
75     // Change the password of this wallet
76     void changePassword(const QString &wallet, qlonglong wId, const QString &appid);
77 
78     // A list of all wallets
79     QStringList wallets() const;
80 
81     // A list of all folders in this wallet
82     QStringList folderList(int handle, const QString &appid);
83 
84     // Does this wallet have this folder?
85     bool hasFolder(int handle, const QString &folder, const QString &appid);
86 
87     // Create this folder
88     bool createFolder(int handle, const QString &folder, const QString &appid);
89 
90     // Remove this folder
91     bool removeFolder(int handle, const QString &folder, const QString &appid);
92 
93     // List of entries in this folder
94     QStringList entryList(int handle, const QString &folder, const QString &appid);
95 
96     // Read an entry.  If the entry does not exist, it just
97     // returns an empty result.  It is your responsibility to check
98     // hasEntry() first.
99     QByteArray readEntry(int handle, const QString &folder, const QString &key, const QString &appid);
100     QByteArray readMap(int handle, const QString &folder, const QString &key, const QString &appid);
101     QString readPassword(int handle, const QString &folder, const QString &key, const QString &appid);
102 
103 #if KWALLET_BUILD_DEPRECATED_SINCE(5, 72)
104     // use entriesList()
105     QVariantMap readEntryList(int handle, const QString &folder, const QString &key, const QString &appid);
106 #endif
107 
108 #if KWALLET_BUILD_DEPRECATED_SINCE(5, 72)
109     // use mapList()
110     QVariantMap readMapList(int handle, const QString &folder, const QString &key, const QString &appid);
111 #endif
112 
113 #if KWALLET_BUILD_DEPRECATED_SINCE(5, 72)
114     // use passwordList()
115     QVariantMap readPasswordList(int handle, const QString &folder, const QString &key, const QString &appid);
116 #endif
117 
118     QVariantMap entriesList(int handle, const QString &folder, const QString &appid);
119     QVariantMap mapList(int handle, const QString &folder, const QString &appid);
120     QVariantMap passwordList(int handle, const QString &folder, const QString &appid);
121 
122     // Rename an entry.  rc=0 on success.
123     int renameEntry(int handle, const QString &folder, const QString &oldName, const QString &newName, const QString &appid);
124 
125     // Write an entry.  rc=0 on success.
126     int writeEntry(int handle, const QString &folder, const QString &key, const QByteArray &value, int entryType, const QString &appid);
127     int writeEntry(int handle, const QString &folder, const QString &key, const QByteArray &value, const QString &appid);
128     int writeMap(int handle, const QString &folder, const QString &key, const QByteArray &value, const QString &appid);
129     int writePassword(int handle, const QString &folder, const QString &key, const QString &value, const QString &appid);
130 
131     // Does the entry exist?
132     bool hasEntry(int handle, const QString &folder, const QString &key, const QString &appid);
133 
134     // What type is the entry?
135     int entryType(int handle, const QString &folder, const QString &key, const QString &appid);
136 
137     // Remove an entry.  rc=0 on success.
138     int removeEntry(int handle, const QString &folder, const QString &key, const QString &appid);
139 
140     // Disconnect an app from a wallet
141     bool disconnectApplication(const QString &wallet, const QString &application);
142 
143     void reconfigure();
144 
145     // Determine
146     bool folderDoesNotExist(const QString &wallet, const QString &folder);
147     bool keyDoesNotExist(const QString &wallet, const QString &folder, const QString &key);
148 
149     void closeAllWallets();
150 
151     QString networkWallet();
152 
153     QString localWallet();
154 
155     void screenSaverChanged(bool);
156 
157     // Open a wallet using a pre-hashed password. This is only useful in cooperation
158     // with the kwallet PAM module. It's also less secure than manually entering the
159     // password as the password hash is transmitted using D-Bus.
160     int pamOpen(const QString &wallet, const QByteArray &passwordHash, int sessionTimeout);
161 
162 Q_SIGNALS:
163     void walletAsyncOpened(int id, int handle); // used to notify KWallet::Wallet
164     void walletListDirty();
165     void walletCreated(const QString &wallet);
166     void walletOpened(const QString &wallet);
167     void walletDeleted(const QString &wallet);
168     void walletClosed(const QString &wallet); // clazy:exclude=overloaded-signal
169 
170     // TODO KF6 remove this signal, replaced by walletClosedId(int)
171     void walletClosed(int handle); // clazy:exclude=overloaded-signal
172 
173     // since 5.81
174     void walletClosedId(int handle);
175 
176     void allWalletsClosed();
177     void folderListUpdated(const QString &wallet);
178     void folderUpdated(const QString &, const QString &);
179     void applicationDisconnected(const QString &wallet, const QString &application);
180 
181 private Q_SLOTS:
182     void slotServiceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner);
183     void emitWalletListDirty();
184     void timedOutClose(int handle);
185     void timedOutSync(int handle);
186     void notifyFailures();
187     void processTransactions();
188     void activatePasswordDialog();
189     void registerKWalletd4Service();
190 #ifdef Q_WS_X11
191     void connectToScreenSaver();
192 #endif
193 
194 private:
195     // Internal - open a wallet
196     int internalOpen(const QString &appid, const QString &wallet, bool isPath, WId w, bool modal, const QString &service);
197     // Internal - close this wallet.
198     int internalClose(KWallet::Backend *const w, const int handle, const bool force, const bool saveBeforeClose = true);
199 
200     bool isAuthorizedApp(const QString &appid, const QString &wallet, WId w);
201     // This also validates the handle.  May return NULL.
202     KWallet::Backend *getWallet(const QString &appid, int handle);
203     // Generate a new unique handle.
204     int generateHandle();
205     // Emit signals about closing wallets
206     void doCloseSignals(int, const QString &);
207     void emitFolderUpdated(const QString &, const QString &);
208     // Implicitly allow access for this application
209     bool implicitAllow(const QString &wallet, const QString &app);
210     bool implicitDeny(const QString &wallet, const QString &app);
211 
212     void doTransactionChangePassword(const QString &appid, const QString &wallet, qlonglong wId);
213     void doTransactionOpenCancelled(const QString &appid, const QString &wallet, const QString &service);
214     int doTransactionOpen(const QString &appid, const QString &wallet, bool isPath, qlonglong wId, bool modal, const QString &service);
215     void initiateSync(int handle);
216 
217     void setupDialog(QWidget *dialog, WId wId, const QString &appid, bool modal);
218     void checkActiveDialog();
219 
220     QPair<int, KWallet::Backend *> findWallet(const QString &walletName) const;
221 
222     typedef QHash<int, KWallet::Backend *> Wallets;
223     Wallets _wallets;
224     KDirWatch *_dw;
225     int _failed;
226 
227     // configuration values
228     bool _leaveOpen, _closeIdle, _launchManager, _enabled;
229     bool _openPrompt, _firstUse, _showingFailureNotify;
230     int _idleTime;
231     QMap<QString, QStringList> _implicitAllowMap, _implicitDenyMap;
232     KTimeout _closeTimers;
233     KTimeout _syncTimers;
234     const int _syncTime;
235     static bool _processing;
236 
237     KWalletTransaction *_curtrans; // current transaction
238     QList<KWalletTransaction *> _transactions;
239     QPointer<QWidget> activeDialog;
240 
241 #ifdef Q_WS_X11
242     QDBusInterface *screensaver;
243 #endif
244 
245     // sessions
246     KWalletSessionStore _sessions;
247     QDBusServiceWatcher _serviceWatcher;
248 
249     bool _useGpg;
250 };
251 
252 #endif
253