1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/** -*- c++ -*-
* networkaccount.cpp
*
* Copyright (c) 2000-2002 Michael Haeckel <haeckel@kde.org>
* Copyright (c) 2002 Marc Mutz <mutz@kde.org>
*
* This file is based on work on pop3 and imap account implementations
* by Don Sanders <sanders@kde.org> and Michael Haeckel <haeckel@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "networkaccount.h"
#include "accountmanager.h"
#include "kmkernel.h"
#include "globalsettings.h"
#include <tdeconfig.h>
#include <tdeio/global.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kdebug.h>
#include <tdewallet.h>
using TDEIO::MetaData;
using KWallet::Wallet;
#include <climits>
namespace KMail {
// for restricting number of concurrent connections to the same server
static TQMap<TQString, int> s_serverConnections;
NetworkAccount::NetworkAccount( AccountManager * parent, const TQString & name, uint id )
: KMAccount( parent, name, id ),
mSlave( 0 ),
mAuth( "*" ),
mPort( 0 ),
mStorePasswd( false ),
mUseSSL( false ),
mUseTLS( false ),
mAskAgain( false ),
mPasswdDirty( false ),
mStorePasswdInConfig( false )
{
}
NetworkAccount::~NetworkAccount() {
}
void NetworkAccount::init() {
KMAccount::init();
mSieveConfig = SieveConfig();
mLogin = TQString();
mPasswd = TQString();
mAuth = "*";
mHost = TQString();
mPort = defaultPort();
mStorePasswd = false;
mUseSSL = false;
mUseTLS = false;
mAskAgain = false;
}
//
//
// Getters and Setters
//
//
void NetworkAccount::setLogin( const TQString & login ) {
mLogin = login;
}
TQString NetworkAccount::passwd() const {
if ( storePasswd() && mPasswd.isEmpty() )
mOwner->readPasswords();
return decryptStr( mPasswd );
}
void NetworkAccount::setPasswd( const TQString & passwd, bool storeInConfig ) {
if ( mPasswd != encryptStr( passwd ) ) {
mPasswd = encryptStr( passwd );
mPasswdDirty = true;
}
setStorePasswd( storeInConfig );
}
void NetworkAccount::clearPasswd() {
setPasswd( "", false );
}
void NetworkAccount::setAuth( const TQString & auth ) {
mAuth = auth;
}
void NetworkAccount::setStorePasswd( bool store ) {
if( mStorePasswd != store && store )
mPasswdDirty = true;
mStorePasswd = store;
}
void NetworkAccount::setHost( const TQString & host ) {
mHost = host;
}
void NetworkAccount::setPort( unsigned short int port ) {
mPort = port;
}
void NetworkAccount::setUseSSL( bool use ) {
mUseSSL = use;
}
void NetworkAccount::setUseTLS( bool use ) {
mUseTLS = use;
}
void NetworkAccount::setSieveConfig( const SieveConfig & config ) {
mSieveConfig = config;
}
//
//
// read/write config
//
//
void NetworkAccount::readConfig( /*const*/ TDEConfig/*Base*/ & config ) {
KMAccount::readConfig( config );
setLogin( config.readEntry( "login" ) );
if ( config.readNumEntry( "store-passwd", false ) ) { // ### s/Num/Bool/
mStorePasswd = true;
TQString encpasswd = config.readEntry( "pass" );
if ( encpasswd.isEmpty() ) {
encpasswd = config.readEntry( "passwd" );
if ( !encpasswd.isEmpty() ) encpasswd = importPassword( encpasswd );
}
if ( !encpasswd.isEmpty() ) {
setPasswd( decryptStr( encpasswd ), true );
// migrate to KWallet if available
if ( Wallet::isEnabled() ) {
config.deleteEntry( "pass" );
config.deleteEntry( "passwd" );
mPasswdDirty = true;
mStorePasswdInConfig = false;
} else {
mPasswdDirty = false; // set by setPasswd() on first read
mStorePasswdInConfig = true;
}
} else {
// read password if wallet is already open, otherwise defer to on-demand loading
if ( Wallet::isOpen( Wallet::NetworkWallet() ) )
readPassword();
}
} else {
setPasswd( "", false );
}
setHost( config.readEntry( "host" ) );
unsigned int port = config.readUnsignedNumEntry( "port", defaultPort() );
if ( port > USHRT_MAX ) port = defaultPort();
setPort( port );
setAuth( config.readEntry( "auth", "*" ) );
setUseSSL( config.readBoolEntry( "use-ssl", false ) );
setUseTLS( config.readBoolEntry( "use-tls", false ) );
mSieveConfig.readConfig( config );
}
void NetworkAccount::writeConfig( TDEConfig/*Base*/ & config ) /*const*/ {
KMAccount::writeConfig( config );
config.writeEntry( "login", login() );
config.writeEntry( "store-passwd", storePasswd() );
if ( storePasswd() ) {
// write password to the wallet if possbile and necessary
bool passwdStored = false;
if ( mPasswdDirty ) {
Wallet *wallet = kmkernel->wallet();
if ( wallet && wallet->writePassword( "account-" + TQString::number(mId), passwd() ) == 0 ) {
passwdStored = true;
mPasswdDirty = false;
mStorePasswdInConfig = false;
}
} else {
passwdStored = !mStorePasswdInConfig; // already in the wallet
}
// if wallet is not available, write to config file, since the account
// manager deletes this group, we need to write it always
if ( !passwdStored && ( mStorePasswdInConfig || KMessageBox::warningYesNo( 0,
i18n("KWallet is not available. It is strongly recommended to use "
"KWallet for managing your passwords.\n"
"However, KMail can store the password in its configuration "
"file instead. The password is stored in an obfuscated format, "
"but should not be considered secure from decryption efforts "
"if access to the configuration file is obtained.\n"
"Do you want to store the password for account '%1' in the "
"configuration file?").arg( name() ),
i18n("KWallet Not Available"),
KGuiItem( i18n("Store Password") ),
KGuiItem( i18n("Do Not Store Password") ) )
== KMessageBox::Yes ) ) {
config.writeEntry( "pass", encryptStr( passwd() ) );
mStorePasswdInConfig = true;
}
}
// delete password from the wallet if password storage is disabled
if (!storePasswd() && !Wallet::keyDoesNotExist(
Wallet::NetworkWallet(), "kmail", "account-" + TQString::number(mId))) {
Wallet *wallet = kmkernel->wallet();
if (wallet)
wallet->removeEntry( "account-" + TQString::number(mId) );
}
config.writeEntry( "host", host() );
config.writeEntry( "port", static_cast<unsigned int>( port() ) );
config.writeEntry( "auth", auth() );
config.writeEntry( "use-ssl", useSSL() );
config.writeEntry( "use-tls", useTLS() );
mSieveConfig.writeConfig( config );
}
//
//
// Network processing
//
//
KURL NetworkAccount::getUrl() const {
KURL url;
url.setProtocol( protocol() );
url.setUser( login() );
url.setPass( passwd() );
url.setHost( host() );
url.setPort( port() );
return url;
}
MetaData NetworkAccount::slaveConfig() const {
MetaData m;
m.insert( "tls", useTLS() ? "on" : "off" );
return m;
}
void NetworkAccount::pseudoAssign( const KMAccount * a ) {
KMAccount::pseudoAssign( a );
const NetworkAccount * n = dynamic_cast<const NetworkAccount*>( a );
if ( !n ) return;
setLogin( n->login() );
setPasswd( n->passwd(), n->storePasswd() );
setHost( n->host() );
setPort( n->port() );
setAuth( n->auth() );
setUseSSL( n->useSSL() );
setUseTLS( n->useTLS() );
setSieveConfig( n->sieveConfig() );
}
void NetworkAccount::readPassword() {
if ( !storePasswd() )
return;
// ### workaround for broken Wallet::keyDoesNotExist() which returns wrong
// results for new entries without closing and reopening the wallet
if ( Wallet::isOpen( Wallet::NetworkWallet() ) )
{
Wallet *wallet = kmkernel->wallet();
if (!wallet || !wallet->hasEntry( "account-" + TQString::number(mId) ) )
return;
}
else
{
if (Wallet::keyDoesNotExist( Wallet::NetworkWallet(), "kmail", "account-" + TQString::number(mId) ) )
return;
}
if ( kmkernel->wallet() ) {
TQString passwd;
kmkernel->wallet()->readPassword( "account-" + TQString::number(mId), passwd );
setPasswd( passwd, true );
mPasswdDirty = false;
}
}
void NetworkAccount::setCheckingMail( bool checking )
{
mCheckingMail = checking;
if ( host().isEmpty() )
return;
if ( checking ) {
if ( s_serverConnections.find( host() ) != s_serverConnections.end() )
s_serverConnections[host()] += 1;
else
s_serverConnections[host()] = 1;
kdDebug(5006) << "check mail started - connections for host "
<< host() << " now is "
<< s_serverConnections[host()] << endl;
} else {
if ( s_serverConnections.find( host() ) != s_serverConnections.end() &&
s_serverConnections[host()] > 0 ) {
s_serverConnections[host()] -= 1;
kdDebug(5006) << "connections to server " << host()
<< " now " << s_serverConnections[host()] << endl;
}
}
}
bool NetworkAccount::mailCheckCanProceed() const
{
bool offlineMode = KMKernel::isOffline();
kdDebug(5006) << "for host " << host()
<< " current connections="
<< (s_serverConnections.find(host())==s_serverConnections.end() ? 0 : s_serverConnections[host()])
<< " and limit is " << GlobalSettings::self()->maxConnectionsPerHost()
<< endl;
bool connectionLimitForHostReached = !host().isEmpty()
&& GlobalSettings::self()->maxConnectionsPerHost() > 0
&& s_serverConnections.find( host() ) != s_serverConnections.end()
&& s_serverConnections[host()] >= GlobalSettings::self()->maxConnectionsPerHost();
kdDebug(5006) << "connection limit reached: "
<< connectionLimitForHostReached << endl;
return ( !connectionLimitForHostReached && !offlineMode );
}
void NetworkAccount::resetConnectionList( NetworkAccount* acct )
{
s_serverConnections[ acct->host() ] = 0;
}
} // namespace KMail
#include "networkaccount.moc"
|