File indexing completed on 2024-12-08 03:40:40

0001 /*
0002     This file is part of the KDE Password Server
0003     SPDX-FileCopyrightText: 2002 Waldo Bastian (bastian@kde.org)
0004     SPDX-FileCopyrightText: 2012 Dawit Alemayehu (adawit@kde.org)
0005 
0006     SPDX-License-Identifier: GPL-2.0-only
0007 */
0008 
0009 // KDE Password Server
0010 
0011 #ifndef KPASSWDSERVER_H
0012 #define KPASSWDSERVER_H
0013 
0014 #include <QDBusContext>
0015 #include <QDBusMessage>
0016 #include <QHash>
0017 #include <QList>
0018 #include <QWidget>
0019 
0020 #include <KDEDModule>
0021 #include <kio/authinfo.h>
0022 
0023 class KMessageDialog;
0024 class KPasswordDialog;
0025 
0026 namespace KWallet
0027 {
0028 class Wallet;
0029 }
0030 
0031 class KPasswdServer : public KDEDModule, protected QDBusContext
0032 {
0033     Q_OBJECT
0034 
0035 public:
0036     explicit KPasswdServer(QObject *parent, const QList<QVariant> & = QList<QVariant>());
0037     ~KPasswdServer() override;
0038 
0039     // Called by the unit test
0040     void setWalletDisabled(bool d)
0041     {
0042         m_walletDisabled = d;
0043     }
0044 
0045 public Q_SLOTS:
0046     qlonglong checkAuthInfoAsync(KIO::AuthInfo, qlonglong, qlonglong);
0047     qlonglong queryAuthInfoAsync(const KIO::AuthInfo &, const QString &, qlonglong, qlonglong, qlonglong);
0048     void addAuthInfo(const KIO::AuthInfo &, qlonglong);
0049     void removeAuthInfo(const QString &host, const QString &protocol, const QString &user);
0050 
0051     // legacy methods provided for compatibility with old clients
0052     QByteArray checkAuthInfo(const QByteArray &, qlonglong, qlonglong);
0053     QByteArray queryAuthInfo(const QByteArray &, const QString &, qlonglong, qlonglong, qlonglong);
0054     void addAuthInfo(const QByteArray &, qlonglong);
0055 
0056     void processRequest();
0057     // Remove all authentication info associated with windowId
0058     void removeAuthForWindowId(qlonglong windowId);
0059 
0060 Q_SIGNALS:
0061     void checkAuthInfoAsyncResult(qlonglong requestId, qlonglong seqNr, const KIO::AuthInfo &);
0062     void queryAuthInfoAsyncResult(qlonglong requestId, qlonglong seqNr, const KIO::AuthInfo &);
0063 
0064 private Q_SLOTS:
0065     void passwordDialogDone(int result, KPasswordDialog *sender);
0066     void retryDialogDone(int result, KMessageDialog *sender);
0067     void windowRemoved(WId);
0068 
0069 private:
0070     struct AuthInfoContainer {
0071         AuthInfoContainer()
0072         {
0073         }
0074 
0075         KIO::AuthInfo info;
0076         QString directory;
0077 
0078         enum { expNever, expWindowClose, expTime } expire;
0079         QList<qlonglong> windowList;
0080         qulonglong expireTime = expNever;
0081         qlonglong seqNr = 0;
0082 
0083         bool isCanceled = false;
0084 
0085         struct Sorter {
0086             bool operator()(const AuthInfoContainer &n1, const AuthInfoContainer &n2) const;
0087         };
0088     };
0089 
0090     struct Request {
0091         bool isAsync; // true for async requests
0092         qlonglong requestId; // set for async requests only
0093         QDBusMessage transaction; // set for sync requests only
0094         QString key;
0095         KIO::AuthInfo info;
0096         QString errorMsg;
0097         qlonglong windowId;
0098         qlonglong seqNr;
0099         bool prompt;
0100     };
0101 
0102     QString createCacheKey(const KIO::AuthInfo &info);
0103     const AuthInfoContainer *findAuthInfoItem(const QString &key, const KIO::AuthInfo &info);
0104     void removeAuthInfoItem(const QString &key, const KIO::AuthInfo &info);
0105     void addAuthInfoItem(const QString &key, const KIO::AuthInfo &info, qlonglong windowId, qlonglong seqNr, bool canceled);
0106     void copyAuthInfo(const AuthInfoContainer *, KIO::AuthInfo &);
0107     void updateAuthExpire(const QString &key, const AuthInfoContainer *, qlonglong windowId, bool keep);
0108 
0109 #ifdef HAVE_KF6WALLET
0110     bool openWallet(qlonglong windowId);
0111 #endif
0112 
0113     bool hasPendingQuery(const QString &key, const KIO::AuthInfo &info);
0114     void sendResponse(Request *request);
0115     void showPasswordDialog(Request *request);
0116     void updateCachedRequestKey(QList<Request *> &, const QString &oldKey, const QString &newKey);
0117 
0118     using AuthInfoContainerList = QList<AuthInfoContainer>;
0119     QHash<QString, AuthInfoContainerList *> m_authDict;
0120 
0121     QList<Request *> m_authPending;
0122     QList<Request *> m_authWait;
0123     QHash<int, QStringList> mWindowIdList;
0124     QHash<QObject *, Request *> m_authInProgress;
0125     QHash<QObject *, Request *> m_authRetryInProgress;
0126     QStringList m_authPrompted;
0127     KWallet::Wallet *m_wallet;
0128     bool m_walletDisabled;
0129     qlonglong m_seqNr;
0130 };
0131 
0132 #endif