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
|
/*
* Copyright (C) by Olivier Goffart <ogoffart@woboq.com>
*
* 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.
*/
#include "accountmanager.h"
#include "sslerrordialog.h"
#include <theme.h>
#include <creds/credentialsfactory.h>
#include <creds/abstractcredentials.h>
#include <cookiejar.h>
#include <QSettings>
#include <QDir>
#include <QNetworkAccessManager>
namespace {
static const char urlC[] = "url";
static const char authTypeC[] = "authType";
static const char userC[] = "user";
static const char httpUserC[] = "http_user";
static const char caCertsKeyC[] = "CaCertificates";
static const char accountsC[] = "Accounts";
}
namespace OCC {
AccountManager *AccountManager::instance()
{
static AccountManager instance;
return &instance;
}
bool AccountManager::restore()
{
auto settings = Account::settingsWithGroup(QLatin1String(accountsC));
// If there are no accounts, check the old format.
if (settings->childGroups().isEmpty()) {
return restoreFromLegacySettings();
}
foreach (const auto& accountId, settings->childGroups()) {
settings->beginGroup(accountId);
if (auto acc = load(*settings)) {
acc->_id = accountId;
addAccount(acc);
}
settings->endGroup();
}
return true;
}
bool AccountManager::restoreFromLegacySettings()
{
// try to open the correctly themed settings
auto settings = Account::settingsWithGroup(Theme::instance()->appName());
bool migratedCreds = false;
// if the settings file could not be opened, the childKeys list is empty
// then try to load settings from a very old place
if( settings->childKeys().isEmpty() ) {
// Now try to open the original ownCloud settings to see if they exist.
QString oCCfgFile = QDir::fromNativeSeparators( settings->fileName() );
// replace the last two segments with ownCloud/owncloud.cfg
oCCfgFile = oCCfgFile.left( oCCfgFile.lastIndexOf('/'));
oCCfgFile = oCCfgFile.left( oCCfgFile.lastIndexOf('/'));
oCCfgFile += QLatin1String("/ownCloud/owncloud.cfg");
qDebug() << "Migrate: checking old config " << oCCfgFile;
QFileInfo fi( oCCfgFile );
if( fi.isReadable() ) {
QSettings *oCSettings = new QSettings(oCCfgFile, QSettings::IniFormat);
oCSettings->beginGroup(QLatin1String("ownCloud"));
// Check the theme url to see if it is the same url that the oC config was for
QString overrideUrl = Theme::instance()->overrideServerUrl();
if( !overrideUrl.isEmpty() ) {
if (overrideUrl.endsWith('/')) { overrideUrl.chop(1); }
QString oCUrl = oCSettings->value(QLatin1String(urlC)).toString();
if (oCUrl.endsWith('/')) { oCUrl.chop(1); }
// in case the urls are equal reset the settings object to read from
// the ownCloud settings object
qDebug() << "Migrate oC config if " << oCUrl << " == " << overrideUrl << ":"
<< (oCUrl == overrideUrl ? "Yes" : "No");
if( oCUrl == overrideUrl ) {
migratedCreds = true;
settings.reset( oCSettings );
} else {
delete oCSettings;
}
}
}
}
// Try to load the single account.
if (!settings->childKeys().isEmpty()) {
if (auto acc = load(*settings)) {
if (migratedCreds) {
acc->setMigrated(true);
}
addAccount(acc);
return true;
}
}
return false;
}
void AccountManager::save()
{
auto settings = Account::settingsWithGroup(QLatin1String(accountsC));
foreach (const auto &acc, _accounts) {
settings->beginGroup(acc->account()->id());
save(acc->account(), *settings);
settings->endGroup();
}
}
void AccountManager::save(const AccountPtr& acc, QSettings& settings)
{
settings.setValue(QLatin1String(urlC), acc->_url.toString());
if (acc->_credentials) {
acc->_credentials->persist();
Q_FOREACH(QString key, acc->_settingsMap.keys()) {
settings.setValue(key, acc->_settingsMap.value(key));
}
settings.setValue(QLatin1String(authTypeC), acc->_credentials->authType());
// HACK: Save http_user also as user
if (acc->_settingsMap.contains(httpUserC))
settings.setValue(userC, acc->_settingsMap.value(httpUserC));
}
settings.sync();
// Save accepted certificates.
settings.beginGroup(QLatin1String("General"));
qDebug() << "Saving " << acc->approvedCerts().count() << " unknown certs.";
QByteArray certs;
Q_FOREACH( const QSslCertificate& cert, acc->approvedCerts() ) {
certs += cert.toPem() + '\n';
}
if (!certs.isEmpty()) {
settings.setValue( QLatin1String(caCertsKeyC), certs );
}
settings.endGroup();
// Save cookies.
if (acc->_am) {
CookieJar* jar = qobject_cast<CookieJar*>(acc->_am->cookieJar());
if (jar) {
qDebug() << "Saving cookies.";
jar->save();
}
}
}
AccountPtr AccountManager::load(QSettings& settings)
{
auto acc = Account::create();
acc->setUrl(settings.value(QLatin1String(urlC)).toUrl());
// We want to only restore settings for that auth type and the user value
acc->_settingsMap.insert(QLatin1String(userC), settings.value(userC));
QString authTypePrefix = settings.value(authTypeC).toString() + "_";
Q_FOREACH(QString key, settings.childKeys()) {
if (!key.startsWith(authTypePrefix))
continue;
acc->_settingsMap.insert(key, settings.value(key));
}
acc->setCredentials(CredentialsFactory::create(settings.value(QLatin1String(authTypeC)).toString()));
// now the cert, it is in the general group
settings.beginGroup(QLatin1String("General"));
acc->setApprovedCerts(QSslCertificate::fromData(settings.value(caCertsKeyC).toByteArray()));
acc->setSslErrorHandler(new SslDialogErrorHandler);
settings.endGroup();
return acc;
}
AccountState *AccountManager::addAccount(const AccountPtr& newAccount)
{
auto id = newAccount->id();
if (id.isEmpty() || !isAccountIdAvailable(id)) {
id = generateFreeAccountId();
}
newAccount->_id = id;
AccountStatePtr newAccountState(new AccountState(newAccount));
_accounts << newAccountState;
emit accountAdded(newAccountState.data());
return newAccountState.data();
}
void AccountManager::deleteAccount(AccountState* account)
{
auto it = std::find(_accounts.begin(), _accounts.end(), account);
if (it == _accounts.end()) { return; }
auto copy = *it; // keep a reference to the shared pointer so it does not delete it just yet
_accounts.erase(it);
auto settings = Account::settingsWithGroup(QLatin1String(accountsC));
settings->remove(account->account()->id());
emit accountRemoved(account);
}
void AccountManager::shutdown()
{
auto accountsCopy = _accounts;
_accounts.clear();
foreach (const auto &acc, accountsCopy) {
emit accountRemoved(acc.data());
}
}
bool AccountManager::isAccountIdAvailable(const QString& id) const
{
foreach (const auto& acc, _accounts) {
if (acc->account()->id() == id) {
return false;
}
}
return true;
}
QString AccountManager::generateFreeAccountId() const
{
int i = 0;
forever {
QString id = QString::number(i);
if (isAccountIdAvailable(id)) {
return id;
}
++i;
}
}
}
|