Skip to content

Latest commit

 

History

History
752 lines (633 loc) · 23.4 KB

ssu.cpp

File metadata and controls

752 lines (633 loc) · 23.4 KB
 
Oct 8, 2012
Oct 8, 2012
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* @file ssu.cpp
* @copyright 2012 Jolla Ltd.
* @author Bernd Wachter <bernd.wachter@jollamobile.com>
* @date 2012
*/
#include <QSystemDeviceInfo>
#include <QtXml/QDomDocument>
#include "ssu.h"
#include "../constants.h"
QTM_USE_NAMESPACE
Ssu::Ssu(): QObject(){
errorFlag = false;
Oct 21, 2012
Oct 21, 2012
19
#ifdef SSUCONFHACK
Oct 8, 2012
Oct 8, 2012
20
21
22
23
24
25
26
27
28
29
// dirty hack to make sure we can write to the configuration
// this is currently required since there's no global gconf,
// and we migth not yet have users on bootstrap
QFileInfo settingsInfo(SSU_CONFIGURATION);
if (settingsInfo.groupId() != SSU_GROUP_ID ||
!settingsInfo.permission(QFile::WriteGroup)){
QProcess proc;
proc.start("/usr/bin/ssuconfperm");
proc.waitForFinished();
}
Oct 21, 2012
Oct 21, 2012
30
#endif
Oct 8, 2012
Oct 8, 2012
31
32
33
settings = new QSettings(SSU_CONFIGURATION, QSettings::IniFormat);
repoSettings = new QSettings(SSU_REPO_CONFIGURATION, QSettings::IniFormat);
Nov 4, 2012
Nov 4, 2012
34
boardMappings = new QSettings(SSU_BOARD_MAPPING_CONFIGURATION, QSettings::IniFormat);
Oct 21, 2012
Oct 21, 2012
35
QSettings defaultSettings(SSU_DEFAULT_CONFIGURATION, QSettings::IniFormat);
Oct 8, 2012
Oct 8, 2012
36
Oct 21, 2012
Oct 21, 2012
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
int configVersion=0;
int defaultConfigVersion=0;
if (settings->contains("configVersion"))
configVersion = settings->value("configVersion").toInt();
if (defaultSettings.contains("configVersion"))
defaultConfigVersion = defaultSettings.value("configVersion").toInt();
if (configVersion < defaultConfigVersion){
qDebug() << "Configuration is outdated, updating from " << configVersion
<< " to " << defaultConfigVersion;
for (int i=configVersion+1;i<=defaultConfigVersion;i++){
QStringList defaultKeys;
QString currentSection = QString("%1/").arg(i);
qDebug() << "Processing configuration version " << i;
defaultSettings.beginGroup(currentSection);
defaultKeys = defaultSettings.allKeys();
defaultSettings.endGroup();
foreach (const QString &key, defaultKeys){
Jan 16, 2013
Jan 16, 2013
57
58
59
60
61
62
63
64
65
66
67
// Default keys support both commands and new keys
if (key.compare("cmd-remove", Qt::CaseSensitive) == 0){
// Remove keys listed in value as string list
QStringList oldKeys = defaultSettings.value(currentSection + key).toStringList();
foreach (const QString &oldKey, oldKeys){
if (settings->contains(oldKey)){
settings->remove(oldKey);
qDebug() << "Removing old key:" << oldKey;
}
}
} else if (!settings->contains(key)){
Oct 21, 2012
Oct 21, 2012
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
// Add new keys..
settings->setValue(key, defaultSettings.value(currentSection + key));
qDebug() << "Adding new key: " << key;
} else {
// ... or update the ones where default values has changed.
QVariant oldValue;
// check if an old value exists in an older configuration version
for (int j=i-1;j>0;j--){
if (defaultSettings.contains(QString("%1/").arg(j)+key)){
oldValue = defaultSettings.value(QString("%1/").arg(j)+key);
break;
}
}
// skip updating if there is no old value, since we can't check if the
// default value has changed
if (oldValue.isNull())
continue;
QVariant newValue = defaultSettings.value(currentSection + key);
if (oldValue == newValue){
// old and new value match, no need to do anything, apart from beating the
// person who added a useless key
continue;
} else {
// default value has changed, so check if the configuration is still
// using the old default value...
QVariant currentValue = settings->value(key);
// testcase: handles properly default update of thing with changed value in ssu.ini?
if (currentValue == oldValue){
// ...and update the key if it does
settings->setValue(key, newValue);
qDebug() << "Updating " << key << " from " << currentValue << " to " << newValue;
}
}
}
}
settings->setValue("configVersion", i);
}
Oct 8, 2012
Oct 8, 2012
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
}
#ifdef TARGET_ARCH
if (!settings->contains("arch"))
settings->setValue("arch", TARGET_ARCH);
#else
// FIXME, try to guess a matching architecture
#warning "TARGET_ARCH not defined"
#endif
settings->sync();
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply *)),
SLOT(requestFinished(QNetworkReply *)));
}
QPair<QString, QString> Ssu::credentials(QString scope){
QPair<QString, QString> ret;
settings->beginGroup("credentials-" + scope);
ret.first = settings->value("username").toString();
ret.second = settings->value("password").toString();
settings->endGroup();
return ret;
}
QString Ssu::credentialsScope(QString repoName, bool rndRepo){
if (settings->contains("credentials-scope"))
return settings->value("credentials-scope").toString();
else
return "your-configuration-is-broken-and-does-not-contain-credentials-scope";
}
Oct 24, 2012
Oct 24, 2012
140
141
142
143
144
145
146
QString Ssu::credentialsUrl(QString scope){
if (settings->contains("credentials-url-" + scope))
return settings->value("credentials-url-" + scope).toString();
else
return "your-configuration-is-broken-and-does-not-contain-credentials-url-for-" + scope;
}
Oct 8, 2012
Oct 8, 2012
147
148
149
QString Ssu::deviceFamily(){
QString model = deviceModel();
Nov 4, 2012
Nov 4, 2012
150
151
if (!cachedFamily.isEmpty())
return cachedFamily;
Oct 8, 2012
Oct 8, 2012
152
Nov 4, 2012
Nov 4, 2012
153
154
155
156
157
158
159
160
161
cachedFamily = "UNKNOWN";
if (boardMappings->contains("variants/" + model))
model = boardMappings->value("variants/" + model).toString();
if (boardMappings->contains(model + "/family"))
cachedFamily = boardMappings->value(model + "/family").toString();
return cachedFamily;
Oct 8, 2012
Oct 8, 2012
162
163
164
165
166
}
QString Ssu::deviceModel(){
QDir dir;
QFile procCpuinfo;
Nov 4, 2012
Nov 4, 2012
167
168
169
170
QStringList keys;
if (!cachedModel.isEmpty())
return cachedModel;
Oct 8, 2012
Oct 8, 2012
171
Nov 4, 2012
Nov 4, 2012
172
173
boardMappings->beginGroup("file.exists");
keys = boardMappings->allKeys();
Oct 8, 2012
Oct 8, 2012
174
Nov 4, 2012
Nov 4, 2012
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
// check if the device can be identified by testing for a file
foreach (const QString &key, keys){
QString value = boardMappings->value(key).toString();
if (dir.exists(value)){
cachedModel = key;
break;
}
}
boardMappings->endGroup();
if (!cachedModel.isEmpty()) return cachedModel;
// check if the QSystemInfo model is useful
QSystemDeviceInfo devInfo;
QString model = devInfo.model();
boardMappings->beginGroup("systeminfo.equals");
keys = boardMappings->allKeys();
foreach (const QString &key, keys){
QString value = boardMappings->value(key).toString();
if (model == value){
cachedModel = key;
break;
}
}
boardMappings->endGroup();
if (!cachedModel.isEmpty()) return cachedModel;
// check if the device can be identified by a string in /proc/cpuinfo
Oct 8, 2012
Oct 8, 2012
202
203
204
205
206
procCpuinfo.setFileName("/proc/cpuinfo");
procCpuinfo.open(QIODevice::ReadOnly | QIODevice::Text);
if (procCpuinfo.isOpen()){
QTextStream in(&procCpuinfo);
QString cpuinfo = in.readAll();
Nov 4, 2012
Nov 4, 2012
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
boardMappings->beginGroup("cpuinfo.contains");
keys = boardMappings->allKeys();
foreach (const QString &key, keys){
QString value = boardMappings->value(key).toString();
if (cpuinfo.contains(value)){
cachedModel = key;
break;
}
}
boardMappings->endGroup();
}
if (!cachedModel.isEmpty()) return cachedModel;
// check if there's a match on arch ofr generic fallback. This probably
// only makes sense for x86
boardMappings->beginGroup("arch.equals");
keys = boardMappings->allKeys();
foreach (const QString &key, keys){
QString value = boardMappings->value(key).toString();
if (settings->value("arch").toString() == value){
cachedModel = key;
break;
}
Oct 8, 2012
Oct 8, 2012
232
}
Nov 4, 2012
Nov 4, 2012
233
234
boardMappings->endGroup();
if (cachedModel.isEmpty()) cachedModel = "UNKNOWN";
Oct 8, 2012
Oct 8, 2012
235
Nov 4, 2012
Nov 4, 2012
236
return cachedModel;
Oct 8, 2012
Oct 8, 2012
237
238
239
240
241
}
QString Ssu::deviceUid(){
QString IMEI;
QSystemDeviceInfo devInfo;
Oct 21, 2012
Oct 21, 2012
242
Oct 8, 2012
Oct 8, 2012
243
244
IMEI = devInfo.imei();
// this might not be completely unique (or might change on reflash), but works for now
Oct 25, 2012
Oct 25, 2012
245
246
247
248
249
250
251
252
253
254
if (IMEI == ""){
if (deviceFamily() == "n950-n9" || deviceFamily() == "n900"){
bool ok;
QString IMEIenv = getenv("imei");
IMEIenv.toLongLong(&ok, 10);
if (ok && (IMEIenv.length() == 16 || IMEIenv.length() == 15))
IMEI = IMEIenv;
} else
IMEI = devInfo.uniqueDeviceID();
}
Oct 8, 2012
Oct 8, 2012
255
256
257
258
259
260
261
262
263
264
265
266
267
268
return IMEI;
}
bool Ssu::error(){
return errorFlag;
}
QString Ssu::flavour(){
if (settings->contains("flavour"))
return settings->value("flavour").toString();
else
return "release";
}
Jan 15, 2013
Jan 15, 2013
269
270
271
272
273
274
275
QString Ssu::domain(){
if (settings->contains("domain"))
return settings->value("domain").toString();
else
return "";
}
Oct 8, 2012
Oct 8, 2012
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
bool Ssu::isRegistered(){
if (!settings->contains("privateKey"))
return false;
if (!settings->contains("certificate"))
return false;
return settings->value("registered").toBool();
}
QDateTime Ssu::lastCredentialsUpdate(){
return settings->value("lastCredentialsUpdate").toDateTime();
}
QString Ssu::lastError(){
return errorString;
}
bool Ssu::registerDevice(QDomDocument *response){
QString certificateString = response->elementsByTagName("certificate").at(0).toElement().text();
QSslCertificate certificate(certificateString.toAscii());
if (certificate.isNull()){
// make sure device is in unregistered state on failed registration
settings->setValue("registered", false);
setError("Certificate is invalid");
return false;
} else
settings->setValue("certificate", certificate.toPem());
QString privateKeyString = response->elementsByTagName("privateKey").at(0).toElement().text();
QSslKey privateKey(privateKeyString.toAscii(), QSsl::Rsa);
if (privateKey.isNull()){
settings->setValue("registered", false);
setError("Private key is invalid");
return false;
} else
settings->setValue("privateKey", privateKey.toPem());
// oldUser is just for reference purposes, in case we want to notify
// about owner changes for the device
QString oldUser = response->elementsByTagName("user").at(0).toElement().text();
qDebug() << "Old user:" << oldUser;
// if we came that far everything required for device registration is done
settings->setValue("registered", true);
settings->sync();
emit registrationStatusChanged();
return true;
}
QString Ssu::release(bool rnd){
if (rnd)
return settings->value("rndRelease").toString();
else
return settings->value("release").toString();
}
// RND repos have flavour (devel, testing, release), and release (latest, next)
// Release repos only have release (latest, next, version number)
QString Ssu::repoUrl(QString repoName, bool rndRepo, QHash<QString, QString> repoParameters){
QString r;
QStringList configSections;
Oct 21, 2012
Oct 21, 2012
338
QStringList repoVariables;
Oct 8, 2012
Oct 8, 2012
339
340
341
errorFlag = false;
Oct 21, 2012
Oct 21, 2012
342
343
344
345
346
347
348
349
350
// fill in all arbitrary variables from ssu.ini
settings->beginGroup("repository-url-variables");
repoVariables = settings->allKeys();
foreach (const QString &key, repoVariables){
repoParameters.insert(key, settings->value(key).toString());
}
settings->endGroup();
// add/overwrite some of the variables with sane ones
Oct 8, 2012
Oct 8, 2012
351
352
if (rndRepo){
repoParameters.insert("flavour", repoSettings->value(flavour()+"-flavour/flavour-pattern").toString());
Dec 20, 2012
Dec 20, 2012
353
354
repoParameters.insert("flavourPattern", repoSettings->value(flavour()+"-flavour/flavour-pattern").toString());
repoParameters.insert("flavourName", flavour());
Oct 8, 2012
Oct 8, 2012
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
repoParameters.insert("release", settings->value("rndRelease").toString());
configSections << flavour()+"-flavour" << "rnd" << "all";
} else {
repoParameters.insert("release", settings->value("release").toString());
configSections << "release" << "all";
}
if (!repoParameters.contains("debugSplit"))
repoParameters.insert("debugSplit", "packages");
if (!repoParameters.contains("arch"))
repoParameters.insert("arch", settings->value("arch").toString());
repoParameters.insert("adaptation", settings->value("adaptation").toString());
repoParameters.insert("deviceFamily", deviceFamily());
Nov 4, 2012
Nov 4, 2012
370
repoParameters.insert("deviceModel", deviceModel());
Oct 8, 2012
Oct 8, 2012
371
Jan 15, 2013
Jan 15, 2013
372
// Domain variables
Jan 29, 2013
Jan 29, 2013
373
374
375
376
377
378
379
380
// first read all variables from default-domain
repoSettings->beginGroup("default-domain");
QStringList defKeys = repoSettings->allKeys();
foreach (const QString &key, defKeys){
repoParameters.insert(key, repoSettings->value(key).toString());
}
repoSettings->endGroup();
// then overwrite with domain specific things if that block is available
Jan 15, 2013
Jan 15, 2013
381
382
QString domainSection = domain() + "-domain";
QStringList sections = repoSettings->childGroups();
Jan 29, 2013
Jan 29, 2013
383
if (sections.contains(domainSection)){
Jan 15, 2013
Jan 15, 2013
384
repoSettings->beginGroup(domainSection);
Jan 29, 2013
Jan 29, 2013
385
386
QStringList domainKeys = repoSettings->allKeys();
foreach (const QString &key, domainKeys){
Jan 15, 2013
Jan 15, 2013
387
repoParameters.insert(key, repoSettings->value(key).toString());
Jan 29, 2013
Jan 29, 2013
388
389
}
repoSettings->endGroup();
Jan 15, 2013
Jan 15, 2013
390
391
}
Oct 21, 2012
Oct 21, 2012
392
393
394
395
396
397
398
399
400
401
if (settings->contains("repository-urls/" + repoName))
r = settings->value("repository-urls/" + repoName).toString();
else {
foreach (const QString &section, configSections){
repoSettings->beginGroup(section);
if (repoSettings->contains(repoName)){
r = repoSettings->value(repoName).toString();
repoSettings->endGroup();
break;
}
Oct 8, 2012
Oct 8, 2012
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
repoSettings->endGroup();
}
}
QHashIterator<QString, QString> i(repoParameters);
while (i.hasNext()){
i.next();
r.replace(
QString("%(%1)").arg(i.key()),
i.value());
}
return r;
}
void Ssu::requestFinished(QNetworkReply *reply){
QSslConfiguration sslConfiguration = reply->sslConfiguration();
qDebug() << sslConfiguration.peerCertificate().issuerInfo(QSslCertificate::CommonName);
qDebug() << sslConfiguration.peerCertificate().subjectInfo(QSslCertificate::CommonName);
foreach (const QSslCertificate cert, sslConfiguration.peerCertificateChain()){
qDebug() << "Cert from chain" << cert.subjectInfo(QSslCertificate::CommonName);
}
Nov 4, 2012
Nov 4, 2012
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// what sucks more, this or goto?
do {
if (settings->contains("home-url")){
QString homeUrl = settings->value("home-url").toString().arg("");
homeUrl.remove(QRegExp("//+$"));
QNetworkRequest request = reply->request();
if (request.url().toString().startsWith(homeUrl, Qt::CaseInsensitive)){
// we don't care about errors on download request
if (reply->error() > 0) break;
QByteArray data = reply->readAll();
storeAuthorizedKeys(data);
break;
}
}
Oct 8, 2012
Oct 8, 2012
442
Nov 4, 2012
Nov 4, 2012
443
444
445
if (reply->error() > 0){
pendingRequests--;
setError(reply->errorString());
Oct 8, 2012
Oct 8, 2012
446
return;
Nov 4, 2012
Nov 4, 2012
447
448
449
450
451
452
453
454
455
456
457
} else {
QByteArray data = reply->readAll();
qDebug() << "RequestOutput" << data;
QDomDocument doc;
QString xmlError;
if (!doc.setContent(data, &xmlError)){
pendingRequests--;
setError(tr("Unable to parse server response (%1)").arg(xmlError));
return;
}
Oct 8, 2012
Oct 8, 2012
458
Nov 4, 2012
Nov 4, 2012
459
QString action = doc.elementsByTagName("action").at(0).toElement().text();
Oct 8, 2012
Oct 8, 2012
460
Nov 4, 2012
Nov 4, 2012
461
if (!verifyResponse(&doc)) break;
Oct 8, 2012
Oct 8, 2012
462
Nov 4, 2012
Nov 4, 2012
463
464
465
466
467
468
469
470
471
if (action == "register"){
if (!registerDevice(&doc)) break;
} else if (action == "credentials"){
if (!setCredentials(&doc)) break;
} else {
pendingRequests--;
setError(tr("Response to unknown action encountered: %1").arg(action));
return;
}
Oct 8, 2012
Oct 8, 2012
472
}
Nov 4, 2012
Nov 4, 2012
473
} while (false);
Oct 8, 2012
Oct 8, 2012
474
Nov 4, 2012
Nov 4, 2012
475
476
pendingRequests--;
if (pendingRequests == 0)
Oct 8, 2012
Oct 8, 2012
477
478
479
emit done();
}
Jan 16, 2013
Jan 16, 2013
480
void Ssu::sendRegistration(QString usernameDomain, QString password){
Oct 8, 2012
Oct 8, 2012
481
482
483
errorFlag = false;
QString ssuCaCertificate, ssuRegisterUrl;
Jan 16, 2013
Jan 16, 2013
484
QString username, domainName;
Jan 16, 2013
Jan 16, 2013
485
486
487
488
489
// Username can include also domain, (user@domain), separate those
if (usernameDomain.contains('@')) {
// separate domain/username and set domain
username = usernameDomain.section('@', 0, 0);
Jan 16, 2013
Jan 16, 2013
490
491
domainName = usernameDomain.section('@', 1, 1);
setDomain(domainName);
Jan 16, 2013
Jan 16, 2013
492
493
494
495
496
} else {
// No domain defined
username = usernameDomain;
}
Oct 8, 2012
Oct 8, 2012
497
498
499
500
501
502
503
if (!settings->contains("ca-certificate")){
setError("CA certificate for SSU not set (config key 'ca-certificate')");
return;
} else
ssuCaCertificate = settings->value("ca-certificate").toString();
if (!settings->contains("register-url")){
Jan 15, 2013
Jan 15, 2013
504
505
506
507
508
ssuRegisterUrl = repoUrl("register-url");
if (ssuRegisterUrl.isEmpty()){
setError("URL for SSU registration not set (config key 'register-url')");
return;
}
Oct 8, 2012
Oct 8, 2012
509
510
511
512
} else
ssuRegisterUrl = settings->value("register-url").toString();
QString IMEI = deviceUid();
Oct 21, 2012
Oct 21, 2012
513
514
515
516
if (IMEI == ""){
setError("No valid UID available for your device. For phones: is your modem online?");
return;
}
Oct 8, 2012
Oct 8, 2012
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
QSslConfiguration sslConfiguration;
if (!useSslVerify())
sslConfiguration.setPeerVerifyMode(QSslSocket::VerifyNone);
sslConfiguration.setCaCertificates(QSslCertificate::fromPath(ssuCaCertificate));
QNetworkRequest request;
request.setUrl(QUrl(QString(ssuRegisterUrl)
.arg(IMEI)
));
request.setSslConfiguration(sslConfiguration);
request.setRawHeader("Authorization", "Basic " +
QByteArray(QString("%1:%2")
.arg(username).arg(password)
.toAscii()).toBase64());
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QUrl form;
form.addQueryItem("protocolVersion", SSU_PROTOCOL_VERSION);
form.addQueryItem("deviceModel", deviceModel());
Jan 16, 2013
Jan 16, 2013
538
539
540
if (!domain().isEmpty()){
form.addQueryItem("domain", domain());
}
Oct 8, 2012
Oct 8, 2012
541
542
qDebug() << "Sending request to " << request.url();
Jan 16, 2013
Jan 16, 2013
543
544
qDebug() << form.encodedQueryItems();
Oct 8, 2012
Oct 8, 2012
545
546
QNetworkReply *reply;
Nov 4, 2012
Nov 4, 2012
547
pendingRequests++;
Oct 8, 2012
Oct 8, 2012
548
549
reply = manager->post(request, form.encodedQuery());
// we could expose downloadProgress() from reply in case we want progress info
Nov 4, 2012
Nov 4, 2012
550
551
552
553
554
555
QString homeUrl = settings->value("home-url").toString().arg(username);
if (!homeUrl.isEmpty()){
// clear header, the other request bits are reusable
request.setHeader(QNetworkRequest::ContentTypeHeader, 0);
request.setUrl(homeUrl + "/authorized_keys");
Jan 16, 2013
Jan 16, 2013
556
qDebug() << "sending request to " << request.url();
Nov 4, 2012
Nov 4, 2012
557
558
559
pendingRequests++;
manager->get(request);
}
Oct 8, 2012
Oct 8, 2012
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
}
bool Ssu::setCredentials(QDomDocument *response){
// generate list with all scopes for generic section, add sections
QDomNodeList credentialsList = response->elementsByTagName("credentials");
QStringList credentialScopes;
for (int i=0;i<credentialsList.size();i++){
QDomNode node = credentialsList.at(i);
QString scope;
QDomNamedNodeMap attributes = node.attributes();
if (attributes.contains("scope")){
scope = attributes.namedItem("scope").toAttr().value();
} else {
setError(tr("Credentials element does not have scope"));
return false;
}
if (node.hasChildNodes()){
QDomElement username = node.firstChildElement("username");
QDomElement password = node.firstChildElement("password");
if (username.isNull() || password.isNull()){
setError(tr("Username and/or password not set"));
return false;
} else {
settings->beginGroup("credentials-" + scope);
settings->setValue("username", username.text());
settings->setValue("password", password.text());
settings->endGroup();
settings->sync();
credentialScopes.append(scope);
}
} else {
setError("");
return false;
}
}
settings->setValue("credentialScopes", credentialScopes);
settings->setValue("lastCredentialsUpdate", QDateTime::currentDateTime());
settings->sync();
emit credentialsChanged();
return true;
}
void Ssu::setError(QString errorMessage){
errorFlag = true;
errorString = errorMessage;
Nov 4, 2012
Nov 4, 2012
608
609
610
// assume that we don't even need to wait for other pending requests,
// and just die. This is only relevant for CLI, which well exit after done()
Oct 8, 2012
Oct 8, 2012
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
emit done();
}
void Ssu::setFlavour(QString flavour){
settings->setValue("flavour", flavour);
emit flavourChanged();
}
void Ssu::setRelease(QString release, bool rnd){
if (rnd)
settings->setValue("rndRelease", release);
else
settings->setValue("release", release);
}
Jan 15, 2013
Jan 15, 2013
626
627
628
629
630
void Ssu::setDomain(QString domain){
settings->setValue("domain", domain);
settings->sync();
}
Nov 4, 2012
Nov 4, 2012
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
void Ssu::storeAuthorizedKeys(QByteArray data){
QDir dir;
// only set the key for unprivileged users
if (getuid() < 1000) return;
if (dir.exists(dir.homePath() + "/.ssh/authorized_keys"))
return;
if (!dir.exists(dir.homePath() + "/.ssh"))
if (!dir.mkdir(dir.homePath() + "/.ssh")) return;
QFile::setPermissions(dir.homePath() + "/.ssh",
QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner);
QFile authorizedKeys(dir.homePath() + "/.ssh/authorized_keys");
authorizedKeys.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate);
authorizedKeys.setPermissions(QFile::ReadOwner | QFile::WriteOwner);
QTextStream out(&authorizedKeys);
out << data;
out.flush();
authorizedKeys.close();
}
Oct 8, 2012
Oct 8, 2012
655
656
657
void Ssu::updateCredentials(bool force){
errorFlag = false;
Oct 21, 2012
Oct 21, 2012
658
659
660
661
662
if (deviceUid() == ""){
setError("No valid UID available for your device. For phones: is your modem online?");
return;
}
Oct 8, 2012
Oct 8, 2012
663
664
665
666
667
668
669
670
QString ssuCaCertificate, ssuCredentialsUrl;
if (!settings->contains("ca-certificate")){
setError("CA certificate for SSU not set (config key 'ca-certificate')");
return;
} else
ssuCaCertificate = settings->value("ca-certificate").toString();
if (!settings->contains("credentials-url")){
Jan 15, 2013
Jan 15, 2013
671
672
673
674
675
ssuCredentialsUrl = repoUrl("credentials-url");
if (ssuCredentialsUrl.isEmpty()){
setError("URL for credentials update not set (config key 'credentials-url')");
return;
}
Oct 8, 2012
Oct 8, 2012
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
} else
ssuCredentialsUrl = settings->value("credentials-url").toString();
if (!isRegistered()){
setError("Device is not registered.");
return;
}
if (!force){
// skip updating if the last update was less than a day ago
QDateTime now = QDateTime::currentDateTime();
if (settings->contains("lastCredentialsUpdate")){
QDateTime last = settings->value("lastCredentialsUpdate").toDateTime();
if (last >= now.addDays(-1)){
emit done();
return;
}
}
}
// check when the last update was, decide if an update is required
QSslConfiguration sslConfiguration;
if (!useSslVerify())
sslConfiguration.setPeerVerifyMode(QSslSocket::VerifyNone);
QSslKey privateKey(settings->value("privateKey").toByteArray(), QSsl::Rsa);
QSslCertificate certificate(settings->value("certificate").toByteArray());
QList<QSslCertificate> caCertificates;
caCertificates << QSslCertificate::fromPath(ssuCaCertificate);
sslConfiguration.setCaCertificates(caCertificates);
sslConfiguration.setPrivateKey(privateKey);
sslConfiguration.setLocalCertificate(certificate);
QNetworkRequest request;
request.setUrl(QUrl(ssuCredentialsUrl.arg(deviceUid())));
qDebug() << request.url();
request.setSslConfiguration(sslConfiguration);
Nov 4, 2012
Nov 4, 2012
718
719
pendingRequests++;
manager->get(request);
Oct 8, 2012
Oct 8, 2012
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
}
bool Ssu::useSslVerify(){
if (settings->contains("ssl-verify"))
return settings->value("ssl-verify").toBool();
else
return true;
}
void Ssu::unregister(){
settings->setValue("privateKey", "");
settings->setValue("certificate", "");
settings->setValue("registered", false);
emit registrationStatusChanged();
}
bool Ssu::verifyResponse(QDomDocument *response){
QString action = response->elementsByTagName("action").at(0).toElement().text();
QString deviceId = response->elementsByTagName("deviceId").at(0).toElement().text();
QString protocolVersion = response->elementsByTagName("protocolVersion").at(0).toElement().text();
// compare device ids
if (protocolVersion != SSU_PROTOCOL_VERSION){
setError(
tr("Response has unsupported protocol version %1, client requires version %2")
.arg(protocolVersion)
.arg(SSU_PROTOCOL_VERSION)
);
return false;
}
return true;
}