| 1 | /* |
| 2 | This file is part of the KDE Password Server |
| 3 | SPDX-FileCopyrightText: 2002 Waldo Bastian (bastian@kde.org) |
| 4 | SPDX-FileCopyrightText: 2012 Dawit Alemayehu (adawit@kde.org) |
| 5 | |
| 6 | SPDX-License-Identifier: GPL-2.0-only |
| 7 | */ |
| 8 | |
| 9 | // KDE Password Server |
| 10 | |
| 11 | #ifndef KPASSWDSERVER_H |
| 12 | #define KPASSWDSERVER_H |
| 13 | |
| 14 | #include <QDBusContext> |
| 15 | #include <QDBusMessage> |
| 16 | #include <QHash> |
| 17 | #include <QList> |
| 18 | #include <QWidget> |
| 19 | |
| 20 | #include <KDEDModule> |
| 21 | #include <kio/authinfo.h> |
| 22 | |
| 23 | class KMessageDialog; |
| 24 | class KPasswordDialog; |
| 25 | |
| 26 | namespace KWallet |
| 27 | { |
| 28 | class Wallet; |
| 29 | } |
| 30 | |
| 31 | class KPasswdServer : public KDEDModule, protected QDBusContext |
| 32 | { |
| 33 | Q_OBJECT |
| 34 | |
| 35 | public: |
| 36 | explicit KPasswdServer(QObject *parent, const QList<QVariant> & = QList<QVariant>()); |
| 37 | ~KPasswdServer() override; |
| 38 | |
| 39 | // Called by the unit test |
| 40 | void setWalletDisabled(bool d) |
| 41 | { |
| 42 | m_walletDisabled = d; |
| 43 | } |
| 44 | |
| 45 | public Q_SLOTS: |
| 46 | qlonglong checkAuthInfoAsync(KIO::AuthInfo, qlonglong, qlonglong); |
| 47 | qlonglong queryAuthInfoAsync(const KIO::AuthInfo &, const QString &, qlonglong, qlonglong, qlonglong); |
| 48 | void addAuthInfo(const KIO::AuthInfo &, qlonglong); |
| 49 | void removeAuthInfo(const QString &host, const QString &protocol, const QString &user); |
| 50 | |
| 51 | // legacy methods provided for compatibility with old clients |
| 52 | QByteArray checkAuthInfo(const QByteArray &, qlonglong, qlonglong); |
| 53 | QByteArray queryAuthInfo(const QByteArray &, const QString &, qlonglong, qlonglong, qlonglong); |
| 54 | void addAuthInfo(const QByteArray &, qlonglong); |
| 55 | |
| 56 | void processRequest(); |
| 57 | // Remove all authentication info associated with windowId |
| 58 | void removeAuthForWindowId(qlonglong windowId); |
| 59 | |
| 60 | Q_SIGNALS: |
| 61 | void checkAuthInfoAsyncResult(qlonglong requestId, qlonglong seqNr, const KIO::AuthInfo &); |
| 62 | void queryAuthInfoAsyncResult(qlonglong requestId, qlonglong seqNr, const KIO::AuthInfo &); |
| 63 | |
| 64 | private Q_SLOTS: |
| 65 | void passwordDialogDone(int result, KPasswordDialog *sender); |
| 66 | void retryDialogDone(int result, KMessageDialog *sender); |
| 67 | void windowRemoved(WId); |
| 68 | |
| 69 | private: |
| 70 | struct AuthInfoContainer { |
| 71 | AuthInfoContainer() |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | KIO::AuthInfo info; |
| 76 | QString directory; |
| 77 | |
| 78 | enum { |
| 79 | expNever, |
| 80 | expWindowClose, |
| 81 | expTime |
| 82 | } expire; |
| 83 | QList<qlonglong> windowList; |
| 84 | qulonglong expireTime = expNever; |
| 85 | qlonglong seqNr = 0; |
| 86 | |
| 87 | bool isCanceled = false; |
| 88 | |
| 89 | struct Sorter { |
| 90 | bool operator()(const AuthInfoContainer &n1, const AuthInfoContainer &n2) const; |
| 91 | }; |
| 92 | }; |
| 93 | |
| 94 | struct Request { |
| 95 | bool isAsync; // true for async requests |
| 96 | qlonglong requestId; // set for async requests only |
| 97 | QDBusMessage transaction; // set for sync requests only |
| 98 | QString key; |
| 99 | KIO::AuthInfo info; |
| 100 | QString errorMsg; |
| 101 | qlonglong windowId; |
| 102 | qlonglong seqNr; |
| 103 | bool prompt; |
| 104 | }; |
| 105 | |
| 106 | QString createCacheKey(const KIO::AuthInfo &info); |
| 107 | const AuthInfoContainer *findAuthInfoItem(const QString &key, const KIO::AuthInfo &info); |
| 108 | void removeAuthInfoItem(const QString &key, const KIO::AuthInfo &info); |
| 109 | void addAuthInfoItem(const QString &key, const KIO::AuthInfo &info, qlonglong windowId, qlonglong seqNr, bool canceled); |
| 110 | void copyAuthInfo(const AuthInfoContainer *, KIO::AuthInfo &); |
| 111 | void updateAuthExpire(const QString &key, const AuthInfoContainer *, qlonglong windowId, bool keep); |
| 112 | |
| 113 | #ifdef HAVE_KF6WALLET |
| 114 | bool openWallet(qlonglong windowId); |
| 115 | #endif |
| 116 | |
| 117 | bool hasPendingQuery(const QString &key, const KIO::AuthInfo &info); |
| 118 | void sendResponse(Request *request); |
| 119 | void showPasswordDialog(Request *request); |
| 120 | void updateCachedRequestKey(QList<Request *> &, const QString &oldKey, const QString &newKey); |
| 121 | |
| 122 | using AuthInfoContainerList = QList<AuthInfoContainer>; |
| 123 | QHash<QString, AuthInfoContainerList *> m_authDict; |
| 124 | |
| 125 | QList<Request *> m_authPending; |
| 126 | QList<Request *> m_authWait; |
| 127 | QHash<int, QStringList> mWindowIdList; |
| 128 | QHash<QObject *, Request *> m_authInProgress; |
| 129 | QHash<QObject *, Request *> m_authRetryInProgress; |
| 130 | QStringList m_authPrompted; |
| 131 | KWallet::Wallet *m_wallet; |
| 132 | bool m_walletDisabled; |
| 133 | qlonglong m_seqNr; |
| 134 | }; |
| 135 | |
| 136 | #endif |
| 137 | |