1/*
2 This file is part of the KDE Password Server
3
4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
5 Copyright (C) 2012 Dawit Alemayehu (adawit@kde.org)
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 version 2 as published by the Free Software Foundation.
10
11 This software is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this library; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21//----------------------------------------------------------------------------
22//
23// KDE Password Server
24
25#ifndef KPASSWDSERVER_H
26#define KPASSWDSERVER_H
27
28#include <QtCore/QHash>
29#include <QtCore/QList>
30#include <QtDBus/QtDBus>
31#include <qwindowdefs.h>
32
33#include <kio/authinfo.h>
34#include <kdedmodule.h>
35
36namespace KWallet {
37 class Wallet;
38}
39
40using namespace KIO;
41
42class KPasswdServer : public KDEDModule, protected QDBusContext
43{
44 Q_OBJECT
45
46public:
47 KPasswdServer(QObject* parent, const QList<QVariant>& = QList<QVariant>());
48 ~KPasswdServer();
49
50 // Called by the unit test
51 void setWalletDisabled(bool d) { m_walletDisabled = d; }
52
53public Q_SLOTS:
54 qlonglong checkAuthInfoAsync(KIO::AuthInfo, qlonglong, qlonglong);
55 qlonglong queryAuthInfoAsync(const KIO::AuthInfo &, const QString &, qlonglong, qlonglong, qlonglong);
56 void addAuthInfo(const KIO::AuthInfo &, qlonglong);
57 void removeAuthInfo(const QString& host, const QString& protocol, const QString& user);
58
59 // legacy methods provided for compatibility with old clients
60 QByteArray checkAuthInfo(const QByteArray &, qlonglong, qlonglong);
61 QByteArray queryAuthInfo(const QByteArray &, const QString &, qlonglong, qlonglong, qlonglong);
62 void addAuthInfo(const QByteArray &, qlonglong);
63
64 void processRequest();
65 // Remove all authentication info associated with windowId
66 void removeAuthForWindowId(qlonglong windowId);
67
68Q_SIGNALS:
69 void checkAuthInfoAsyncResult(qlonglong requestId, qlonglong seqNr, const KIO::AuthInfo &);
70 void queryAuthInfoAsyncResult(qlonglong requestId, qlonglong seqNr, const KIO::AuthInfo &);
71
72private Q_SLOTS:
73 void passwordDialogDone(int);
74 void retryDialogDone(int);
75 void windowRemoved(WId);
76
77private:
78 struct AuthInfoContainer {
79 AuthInfoContainer() : expire( expNever ), seqNr( 0 ), isCanceled( false ) {}
80
81 KIO::AuthInfo info;
82 QString directory;
83
84 enum { expNever, expWindowClose, expTime } expire;
85 QList<qlonglong> windowList;
86 qulonglong expireTime;
87 qlonglong seqNr;
88
89 bool isCanceled;
90
91 struct Sorter {
92 bool operator() (AuthInfoContainer* n1, AuthInfoContainer* n2) const;
93 };
94 };
95
96 struct Request {
97 bool isAsync; // true for async requests
98 qlonglong requestId; // set for async requests only
99 QDBusMessage transaction; // set for sync requests only
100 QString key;
101 KIO::AuthInfo info;
102 QString errorMsg;
103 qlonglong windowId;
104 qlonglong seqNr;
105 bool prompt;
106 };
107
108 QString createCacheKey( const KIO::AuthInfo &info );
109 const AuthInfoContainer *findAuthInfoItem(const QString &key, const KIO::AuthInfo &info);
110 void removeAuthInfoItem(const QString &key, const KIO::AuthInfo &info);
111 void addAuthInfoItem(const QString &key, const KIO::AuthInfo &info, qlonglong windowId, qlonglong seqNr, bool canceled);
112 void copyAuthInfo(const AuthInfoContainer*, KIO::AuthInfo&);
113 void updateAuthExpire(const QString &key, const AuthInfoContainer *, qlonglong windowId, bool keep);
114 int findWalletEntry( const QMap<QString,QString>& map, const QString& username );
115 bool openWallet( qlonglong windowId );
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 typedef QList<AuthInfoContainer*> AuthInfoContainerList;
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