File indexing completed on 2024-11-24 03:41:06

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2002-2004 George Staikos <staikos@kde.org>
0004     SPDX-FileCopyrightText: 2008 Michael Leupold <lemma@confuego.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef _KWALLETD_H_
0010 #define _KWALLETD_H_
0011 
0012 #include "kwalletbackend.h"
0013 #include <QDBusServiceWatcher>
0014 #include <QHash>
0015 #include <QPointer>
0016 #include <QString>
0017 #include <QtDBus>
0018 #include <stdlib.h>
0019 #include <time.h>
0020 
0021 #include "ktimeout.h"
0022 #include "kwalletsessionstore.h"
0023 
0024 class KDirWatch;
0025 class KTimeout;
0026 
0027 // @Private
0028 class KWalletTransaction;
0029 class KWalletSessionStore;
0030 class KWalletFreedesktopService;
0031 
0032 class KWalletD : public QObject, protected QDBusContext
0033 {
0034     Q_OBJECT
0035 
0036 public:
0037     KWalletD();
0038     ~KWalletD() override;
0039 
0040     static QString encodeWalletName(const QString &name);
0041     static QString decodeWalletName(const QString &mangledName);
0042 
0043     int nextTransactionId() const;
0044     int
0045     openAsync(const QString &wallet, qlonglong wId, const QString &appid, bool handleSession, const QDBusConnection &connection, const QDBusMessage &message);
0046     // Close and lock the wallet
0047     // Accepts "message" for working from other QDBusContexts
0048     int close(int handle, bool force, const QString &appid, const QDBusMessage &message);
0049 public Q_SLOTS:
0050 
0051     // Is the wallet enabled?  If not, all open() calls fail.
0052     bool isEnabled() const;
0053 
0054     // Open and unlock the wallet
0055     int open(const QString &wallet, qlonglong wId, const QString &appid);
0056 
0057     // Open and unlock the wallet with this path
0058     int openPath(const QString &path, qlonglong wId, const QString &appid);
0059 
0060     // Open the wallet asynchronously
0061     int openAsync(const QString &wallet, qlonglong wId, const QString &appid, bool handleSession);
0062 
0063     // Open and unlock the wallet with this path asynchronously
0064     int openPathAsync(const QString &path, qlonglong wId, const QString &appid, bool handleSession);
0065 
0066     // Close and lock the wallet
0067     // If force = true, will close it for all users.  Behave.  This
0068     // can break applications, and is generally intended for use by
0069     // the wallet manager app only.
0070     int close(const QString &wallet, bool force);
0071     int close(int handle, bool force, const QString &appid);
0072 
0073     // Save to disk but leave open
0074     Q_NOREPLY void sync(int handle, const QString &appid);
0075 
0076     // Physically deletes the wallet from disk.
0077     int deleteWallet(const QString &wallet);
0078 
0079     // Returns true if the wallet is open
0080     bool isOpen(const QString &wallet);
0081     bool isOpen(int handle);
0082 
0083     // List the users of this wallet
0084     QStringList users(const QString &wallet) const;
0085 
0086     // Change the password of this wallet
0087     void changePassword(const QString &wallet, qlonglong wId, const QString &appid);
0088 
0089     // A list of all wallets
0090     QStringList wallets() const;
0091 
0092     // A list of all folders in this wallet
0093     QStringList folderList(int handle, const QString &appid);
0094 
0095     // Does this wallet have this folder?
0096     bool hasFolder(int handle, const QString &folder, const QString &appid);
0097 
0098     // Create this folder
0099     bool createFolder(int handle, const QString &folder, const QString &appid);
0100 
0101     // Remove this folder
0102     bool removeFolder(int handle, const QString &folder, const QString &appid);
0103 
0104     // List of entries in this folder
0105     QStringList entryList(int handle, const QString &folder, const QString &appid);
0106 
0107     // Read an entry.  If the entry does not exist, it just
0108     // returns an empty result.  It is your responsibility to check
0109     // hasEntry() first.
0110     QByteArray readEntry(int handle, const QString &folder, const QString &key, const QString &appid);
0111     QByteArray readMap(int handle, const QString &folder, const QString &key, const QString &appid);
0112     QString readPassword(int handle, const QString &folder, const QString &key, const QString &appid);
0113 
0114 #if KWALLET_BUILD_DEPRECATED_SINCE(5, 72)
0115     // use entriesList()
0116     QVariantMap readEntryList(int handle, const QString &folder, const QString &key, const QString &appid);
0117 #endif
0118 
0119 #if KWALLET_BUILD_DEPRECATED_SINCE(5, 72)
0120     // use mapList()
0121     QVariantMap readMapList(int handle, const QString &folder, const QString &key, const QString &appid);
0122 #endif
0123 
0124 #if KWALLET_BUILD_DEPRECATED_SINCE(5, 72)
0125     // use passwordList()
0126     QVariantMap readPasswordList(int handle, const QString &folder, const QString &key, const QString &appid);
0127 #endif
0128 
0129     QVariantMap entriesList(int handle, const QString &folder, const QString &appid);
0130     QVariantMap mapList(int handle, const QString &folder, const QString &appid);
0131     QVariantMap passwordList(int handle, const QString &folder, const QString &appid);
0132 
0133     // Rename an entry.  rc=0 on success.
0134     int renameEntry(int handle, const QString &folder, const QString &oldName, const QString &newName, const QString &appid);
0135     // Rename the wallet
0136     int renameWallet(const QString &oldName, const QString &newName);
0137 
0138     // Write an entry.  rc=0 on success.
0139     int writeEntry(int handle, const QString &folder, const QString &key, const QByteArray &value, int entryType, const QString &appid);
0140     int writeEntry(int handle, const QString &folder, const QString &key, const QByteArray &value, const QString &appid);
0141     int writeMap(int handle, const QString &folder, const QString &key, const QByteArray &value, const QString &appid);
0142     int writePassword(int handle, const QString &folder, const QString &key, const QString &value, const QString &appid);
0143 
0144     // Does the entry exist?
0145     bool hasEntry(int handle, const QString &folder, const QString &key, const QString &appid);
0146 
0147     // What type is the entry?
0148     int entryType(int handle, const QString &folder, const QString &key, const QString &appid);
0149 
0150     // Remove an entry.  rc=0 on success.
0151     int removeEntry(int handle, const QString &folder, const QString &key, const QString &appid);
0152 
0153     // Disconnect an app from a wallet
0154     bool disconnectApplication(const QString &wallet, const QString &application);
0155 
0156     void reconfigure();
0157 
0158     // Determine
0159     bool folderDoesNotExist(const QString &wallet, const QString &folder);
0160     bool keyDoesNotExist(const QString &wallet, const QString &folder, const QString &key);
0161 
0162     void closeAllWallets();
0163 
0164     QString networkWallet();
0165 
0166     QString localWallet();
0167 
0168     void screenSaverChanged(bool);
0169 
0170     // Open a wallet using a pre-hashed password. This is only useful in cooperation
0171     // with the kwallet PAM module. It's also less secure than manually entering the
0172     // password as the password hash is transmitted using D-Bus.
0173     int pamOpen(const QString &wallet, const QByteArray &passwordHash, int sessionTimeout);
0174 
0175 Q_SIGNALS:
0176     void walletAsyncOpened(int id, int handle); // used to notify KWallet::Wallet
0177     void walletListDirty();
0178     void walletCreated(const QString &wallet);
0179     void walletOpened(const QString &wallet);
0180     void walletDeleted(const QString &wallet);
0181     void walletClosed(const QString &wallet); // clazy:exclude=overloaded-signal
0182 
0183     // TODO KF6 remove this signal, replaced by walletClosedId(int)
0184     void walletClosed(int handle); // clazy:exclude=overloaded-signal
0185 
0186     // since 5.81
0187     void walletClosedId(int handle);
0188 
0189     void allWalletsClosed();
0190     void folderListUpdated(const QString &wallet);
0191     void folderUpdated(const QString &, const QString &);
0192     void entryUpdated(const QString &, const QString &, const QString &);
0193     void entryRenamed(const QString &, const QString &, const QString &, const QString &);
0194     void entryDeleted(const QString &, const QString &, const QString &);
0195     void applicationDisconnected(const QString &wallet, const QString &application);
0196 
0197 private Q_SLOTS:
0198     void slotServiceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner);
0199     void emitWalletListDirty();
0200     void timedOutClose(int handle);
0201     void timedOutSync(int handle);
0202     void notifyFailures();
0203     void processTransactions();
0204     void activatePasswordDialog();
0205 #ifdef Q_WS_X11
0206     void connectToScreenSaver();
0207 #endif
0208 
0209 private:
0210     // Internal - open a wallet
0211     int internalOpen(const QString &appid, const QString &wallet, bool isPath, WId w, bool modal, const QString &service);
0212     // Internal - close this wallet.
0213     int internalClose(KWallet::Backend *const w, const int handle, const bool force, const bool saveBeforeClose = true);
0214 
0215     bool isAuthorizedApp(const QString &appid, const QString &wallet, WId w);
0216     // This also validates the handle.  May return NULL.
0217     KWallet::Backend *getWallet(const QString &appid, int handle);
0218     // Generate a new unique handle.
0219     int generateHandle();
0220     // Emit signals about closing wallets
0221     void doCloseSignals(int, const QString &);
0222     void emitFolderUpdated(const QString &, const QString &);
0223     void emitEntryUpdated(const QString &, const QString &, const QString &);
0224     void emitEntryRenamed(const QString &, const QString &, const QString &, const QString &);
0225     void emitEntryDeleted(const QString &, const QString &, const QString &);
0226     // Implicitly allow access for this application
0227     bool implicitAllow(const QString &wallet, const QString &app);
0228     bool implicitDeny(const QString &wallet, const QString &app);
0229 
0230     void doTransactionChangePassword(const QString &appid, const QString &wallet, qlonglong wId);
0231     void doTransactionOpenCancelled(const QString &appid, const QString &wallet, const QString &service);
0232     int doTransactionOpen(const QString &appid, const QString &wallet, bool isPath, qlonglong wId, bool modal, const QString &service);
0233     void initiateSync(int handle);
0234 
0235     void setupDialog(QWidget *dialog, WId wId, const QString &appid, bool modal);
0236     void checkActiveDialog();
0237 
0238     QPair<int, KWallet::Backend *> findWallet(const QString &walletName) const;
0239 
0240     typedef QHash<int, KWallet::Backend *> Wallets;
0241     Wallets _wallets;
0242     KDirWatch *_dw;
0243     int _failed;
0244 
0245     // configuration values
0246     bool _leaveOpen, _closeIdle, _launchManager, _enabled;
0247     bool _openPrompt, _firstUse, _showingFailureNotify;
0248     int _idleTime;
0249     QMap<QString, QStringList> _implicitAllowMap, _implicitDenyMap;
0250     KTimeout _closeTimers;
0251     KTimeout _syncTimers;
0252     const int _syncTime;
0253     static bool _processing;
0254 
0255     KWalletTransaction *_curtrans; // current transaction
0256     QList<KWalletTransaction *> _transactions;
0257     QPointer<QWidget> activeDialog;
0258 
0259 #ifdef Q_WS_X11
0260     QDBusInterface *screensaver;
0261 #endif
0262 
0263     // sessions
0264     KWalletSessionStore _sessions;
0265     QDBusServiceWatcher _serviceWatcher;
0266 
0267     std::unique_ptr<KWalletFreedesktopService> _fdoService;
0268 
0269     bool _useGpg;
0270 };
0271 
0272 #endif