Skip to content

Latest commit

 

History

History
588 lines (482 loc) · 18.1 KB

ssu.cpp

File metadata and controls

588 lines (482 loc) · 18.1 KB
 
Oct 8, 2012
Oct 8, 2012
1
2
3
4
5
6
7
/**
* @file ssu.cpp
* @copyright 2012 Jolla Ltd.
* @author Bernd Wachter <bernd.wachter@jollamobile.com>
* @date 2012
*/
Apr 4, 2013
Apr 4, 2013
8
#include <QtNetwork>
Oct 8, 2012
Oct 8, 2012
9
#include <QtXml/QDomDocument>
Mar 11, 2013
Mar 11, 2013
10
May 23, 2013
May 23, 2013
11
12
13
14
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <QUrlQuery>
#endif
Sep 9, 2013
Sep 9, 2013
15
16
17
#include <getdef.h>
#include <pwd.h>
Oct 8, 2012
Oct 8, 2012
18
#include "ssu.h"
Mar 19, 2013
Mar 19, 2013
19
#include "ssulog.h"
Mar 19, 2013
Mar 19, 2013
20
#include "ssuvariables.h"
Mar 30, 2013
Mar 30, 2013
21
22
#include "ssucoreconfig.h"
#include "ssurepomanager.h"
Oct 8, 2012
Oct 8, 2012
23
Mar 19, 2013
Mar 19, 2013
24
25
#include "../constants.h"
Mar 30, 2013
Mar 30, 2013
26
Ssu::Ssu(): QObject(){
Oct 8, 2012
Oct 8, 2012
27
errorFlag = false;
Mar 11, 2013
Mar 11, 2013
28
pendingRequests = 0;
Oct 8, 2012
Oct 8, 2012
29
Oct 21, 2012
Oct 21, 2012
30
#ifdef SSUCONFHACK
Oct 8, 2012
Oct 8, 2012
31
32
33
34
35
36
37
38
39
40
// 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
41
#endif
Oct 8, 2012
Oct 8, 2012
42
Mar 30, 2013
Mar 30, 2013
43
SsuCoreConfig *settings = SsuCoreConfig::instance();
Oct 8, 2012
Oct 8, 2012
44
45
46
47
48
49
50
51
52
53
#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();
Mar 30, 2013
Mar 30, 2013
54
55
Oct 8, 2012
Oct 8, 2012
56
57
58
59
60
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply *)),
SLOT(requestFinished(QNetworkReply *)));
}
Mar 30, 2013
Mar 30, 2013
61
62
// FIXME, the whole credentials stuff needs reworking
// should probably be part of repo handling instead of core configuration
Oct 8, 2012
Oct 8, 2012
63
QPair<QString, QString> Ssu::credentials(QString scope){
Mar 30, 2013
Mar 30, 2013
64
65
SsuCoreConfig *settings = SsuCoreConfig::instance();
return settings->credentials(scope);
Oct 8, 2012
Oct 8, 2012
66
67
68
}
QString Ssu::credentialsScope(QString repoName, bool rndRepo){
Mar 30, 2013
Mar 30, 2013
69
70
SsuCoreConfig *settings = SsuCoreConfig::instance();
return settings->credentialsScope(repoName, rndRepo);
Oct 8, 2012
Oct 8, 2012
71
72
}
Oct 24, 2012
Oct 24, 2012
73
QString Ssu::credentialsUrl(QString scope){
Mar 30, 2013
Mar 30, 2013
74
75
SsuCoreConfig *settings = SsuCoreConfig::instance();
return settings->credentialsUrl(scope);
Oct 24, 2012
Oct 24, 2012
76
77
}
Oct 8, 2012
Oct 8, 2012
78
79
80
81
bool Ssu::error(){
return errorFlag;
}
Mar 30, 2013
Mar 30, 2013
82
// Wrappers around SsuCoreConfig
Oct 8, 2012
Oct 8, 2012
83
QString Ssu::flavour(){
Mar 30, 2013
Mar 30, 2013
84
85
SsuCoreConfig *settings = SsuCoreConfig::instance();
return settings->flavour();
Oct 8, 2012
Oct 8, 2012
86
87
}
Mar 28, 2013
Mar 28, 2013
88
int Ssu::deviceMode(){
Mar 30, 2013
Mar 30, 2013
89
90
SsuCoreConfig *settings = SsuCoreConfig::instance();
return settings->deviceMode();
Mar 28, 2013
Mar 28, 2013
91
92
}
Jan 15, 2013
Jan 15, 2013
93
QString Ssu::domain(){
Mar 30, 2013
Mar 30, 2013
94
SsuCoreConfig *settings = SsuCoreConfig::instance();
Jul 5, 2013
Jul 5, 2013
95
return settings->domain(true);
Jan 15, 2013
Jan 15, 2013
96
97
}
Oct 8, 2012
Oct 8, 2012
98
bool Ssu::isRegistered(){
Mar 30, 2013
Mar 30, 2013
99
100
SsuCoreConfig *settings = SsuCoreConfig::instance();
return settings->isRegistered();
Oct 8, 2012
Oct 8, 2012
101
102
103
}
QDateTime Ssu::lastCredentialsUpdate(){
Mar 30, 2013
Mar 30, 2013
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
SsuCoreConfig *settings = SsuCoreConfig::instance();
return settings->lastCredentialsUpdate();
}
QString Ssu::release(bool rnd){
SsuCoreConfig *settings = SsuCoreConfig::instance();
return settings->release(rnd);
}
void Ssu::setDeviceMode(int mode, int editMode){
SsuCoreConfig *settings = SsuCoreConfig::instance();
settings->setDeviceMode(mode, editMode);
}
void Ssu::setFlavour(QString flavour){
SsuCoreConfig *settings = SsuCoreConfig::instance();
settings->setFlavour(flavour);
}
void Ssu::setRelease(QString release, bool rnd){
SsuCoreConfig *settings = SsuCoreConfig::instance();
settings->setRelease(release, rnd);
}
void Ssu::setDomain(QString domain){
SsuCoreConfig *settings = SsuCoreConfig::instance();
Apr 2, 2013
Apr 2, 2013
130
settings->setDomain(domain);
Mar 30, 2013
Mar 30, 2013
131
132
133
134
135
}
bool Ssu::useSslVerify(){
SsuCoreConfig *settings = SsuCoreConfig::instance();
return settings->useSslVerify();
Oct 8, 2012
Oct 8, 2012
136
137
}
Mar 30, 2013
Mar 30, 2013
138
139
140
141
//...
Oct 8, 2012
Oct 8, 2012
142
143
144
145
146
147
QString Ssu::lastError(){
return errorString;
}
bool Ssu::registerDevice(QDomDocument *response){
QString certificateString = response->elementsByTagName("certificate").at(0).toElement().text();
May 23, 2013
May 23, 2013
148
QSslCertificate certificate(certificateString.toLatin1());
Mar 19, 2013
Mar 19, 2013
149
SsuLog *ssuLog = SsuLog::instance();
Mar 30, 2013
Mar 30, 2013
150
SsuCoreConfig *settings = SsuCoreConfig::instance();
Oct 8, 2012
Oct 8, 2012
151
152
153
154
155
156
157
158
159
160
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();
May 23, 2013
May 23, 2013
161
QSslKey privateKey(privateKeyString.toLatin1(), QSsl::Rsa);
Oct 8, 2012
Oct 8, 2012
162
163
164
165
166
167
168
169
170
171
172
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();
Mar 19, 2013
Mar 19, 2013
173
ssuLog->print(LOG_DEBUG, QString("Old user for your device was: %1").arg(oldUser));
Oct 8, 2012
Oct 8, 2012
174
175
176
177
178
179
180
181
182
183
// if we came that far everything required for device registration is done
settings->setValue("registered", true);
settings->sync();
emit registrationStatusChanged();
return true;
}
// RND repos have flavour (devel, testing, release), and release (latest, next)
// Release repos only have release (latest, next, version number)
Mar 28, 2013
Mar 28, 2013
184
185
186
QString Ssu::repoUrl(QString repoName, bool rndRepo,
QHash<QString, QString> repoParameters,
QHash<QString, QString> parametersOverride){
Mar 30, 2013
Mar 30, 2013
187
188
SsuRepoManager manager;
return manager.url(repoName, rndRepo, repoParameters, parametersOverride);
Oct 8, 2012
Oct 8, 2012
189
190
191
192
}
void Ssu::requestFinished(QNetworkReply *reply){
QSslConfiguration sslConfiguration = reply->sslConfiguration();
Mar 19, 2013
Mar 19, 2013
193
SsuLog *ssuLog = SsuLog::instance();
Mar 30, 2013
Mar 30, 2013
194
SsuCoreConfig *settings = SsuCoreConfig::instance();
Oct 8, 2012
Oct 8, 2012
195
May 23, 2013
May 23, 2013
196
197
198
199
200
201
202
203
204
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
ssuLog->print(LOG_DEBUG, QString("Certificate used was issued for '%1' by '%2'. Complete chain:")
.arg(sslConfiguration.peerCertificate().subjectInfo(QSslCertificate::CommonName).join(""))
.arg(sslConfiguration.peerCertificate().issuerInfo(QSslCertificate::CommonName).join("")));
foreach (const QSslCertificate cert, sslConfiguration.peerCertificateChain()){
ssuLog->print(LOG_DEBUG, QString("-> %1").arg(cert.subjectInfo(QSslCertificate::CommonName).join("")));
}
#else
Mar 19, 2013
Mar 19, 2013
205
ssuLog->print(LOG_DEBUG, QString("Certificate used was issued for '%1' by '%2'. Complete chain:")
Mar 11, 2013
Mar 11, 2013
206
207
.arg(sslConfiguration.peerCertificate().subjectInfo(QSslCertificate::CommonName))
.arg(sslConfiguration.peerCertificate().issuerInfo(QSslCertificate::CommonName)));
Oct 8, 2012
Oct 8, 2012
208
209
foreach (const QSslCertificate cert, sslConfiguration.peerCertificateChain()){
Mar 19, 2013
Mar 19, 2013
210
ssuLog->print(LOG_DEBUG, QString("-> %1").arg(cert.subjectInfo(QSslCertificate::CommonName)));
Oct 8, 2012
Oct 8, 2012
211
}
May 23, 2013
May 23, 2013
212
#endif
Oct 8, 2012
Oct 8, 2012
213
May 23, 2013
May 23, 2013
214
/// @TODO: indicate that the device is not registered if there's a 404 on credentials update url
Nov 4, 2012
Nov 4, 2012
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// 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
230
Nov 4, 2012
Nov 4, 2012
231
232
233
if (reply->error() > 0){
pendingRequests--;
setError(reply->errorString());
Oct 8, 2012
Oct 8, 2012
234
return;
Nov 4, 2012
Nov 4, 2012
235
236
237
238
239
240
241
242
243
244
245
} 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
246
Nov 4, 2012
Nov 4, 2012
247
QString action = doc.elementsByTagName("action").at(0).toElement().text();
Oct 8, 2012
Oct 8, 2012
248
Nov 4, 2012
Nov 4, 2012
249
if (!verifyResponse(&doc)) break;
Oct 8, 2012
Oct 8, 2012
250
Nov 4, 2012
Nov 4, 2012
251
252
253
254
255
256
257
258
259
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
260
}
Nov 4, 2012
Nov 4, 2012
261
} while (false);
Oct 8, 2012
Oct 8, 2012
262
Nov 4, 2012
Nov 4, 2012
263
pendingRequests--;
Mar 11, 2013
Mar 11, 2013
264
Mar 19, 2013
Mar 19, 2013
265
ssuLog->print(LOG_DEBUG, QString("Request finished, pending requests: %1").arg(pendingRequests));
Nov 4, 2012
Nov 4, 2012
266
if (pendingRequests == 0)
Oct 8, 2012
Oct 8, 2012
267
268
269
emit done();
}
Jan 16, 2013
Jan 16, 2013
270
void Ssu::sendRegistration(QString usernameDomain, QString password){
Oct 8, 2012
Oct 8, 2012
271
272
273
errorFlag = false;
QString ssuCaCertificate, ssuRegisterUrl;
Jan 16, 2013
Jan 16, 2013
274
QString username, domainName;
Jan 16, 2013
Jan 16, 2013
275
Mar 19, 2013
Mar 19, 2013
276
SsuLog *ssuLog = SsuLog::instance();
Mar 30, 2013
Mar 30, 2013
277
SsuCoreConfig *settings = SsuCoreConfig::instance();
Mar 19, 2013
Mar 19, 2013
278
Jan 16, 2013
Jan 16, 2013
279
280
281
282
// 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
283
284
domainName = usernameDomain.section('@', 1, 1);
setDomain(domainName);
Jan 16, 2013
Jan 16, 2013
285
286
287
288
289
} else {
// No domain defined
username = usernameDomain;
}
Jul 6, 2013
Jul 6, 2013
290
291
292
ssuCaCertificate = SsuRepoManager::caCertificatePath();
if (ssuCaCertificate.isEmpty()){
setError("CA certificate for SSU not set ('_ca-certificate in domain')");
Oct 8, 2012
Oct 8, 2012
293
return;
Jul 6, 2013
Jul 6, 2013
294
}
Oct 8, 2012
Oct 8, 2012
295
296
if (!settings->contains("register-url")){
Jan 15, 2013
Jan 15, 2013
297
298
299
300
301
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
302
303
304
} else
ssuRegisterUrl = settings->value("register-url").toString();
Mar 19, 2013
Mar 19, 2013
305
QString IMEI = deviceInfo.deviceUid();
Oct 21, 2012
Oct 21, 2012
306
307
308
309
if (IMEI == ""){
setError("No valid UID available for your device. For phones: is your modem online?");
return;
}
Oct 8, 2012
Oct 8, 2012
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
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)
May 23, 2013
May 23, 2013
325
.toLatin1()).toBase64());
Oct 8, 2012
Oct 8, 2012
326
327
328
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QUrl form;
May 23, 2013
May 23, 2013
329
330
331
332
333
334
335
336
337
338
339
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
QUrlQuery q;
q.addQueryItem("protocolVersion", SSU_PROTOCOL_VERSION);
q.addQueryItem("deviceModel", deviceInfo.deviceModel());
if (!domain().isEmpty()){
q.addQueryItem("domain", domain());
}
form.setQuery(q);
#else
Oct 8, 2012
Oct 8, 2012
340
form.addQueryItem("protocolVersion", SSU_PROTOCOL_VERSION);
Mar 19, 2013
Mar 19, 2013
341
form.addQueryItem("deviceModel", deviceInfo.deviceModel());
Jan 16, 2013
Jan 16, 2013
342
343
344
if (!domain().isEmpty()){
form.addQueryItem("domain", domain());
}
May 23, 2013
May 23, 2013
345
#endif
Oct 8, 2012
Oct 8, 2012
346
347
qDebug() << "Sending request to " << request.url();
May 23, 2013
May 23, 2013
348
//qDebug() << form.encodedQueryItems();
Jan 16, 2013
Jan 16, 2013
349
Oct 8, 2012
Oct 8, 2012
350
351
QNetworkReply *reply;
Nov 4, 2012
Nov 4, 2012
352
pendingRequests++;
May 23, 2013
May 23, 2013
353
354
355
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
reply = manager->post(request, form.query(QUrl::FullyEncoded).toStdString().c_str());
#else
Oct 8, 2012
Oct 8, 2012
356
reply = manager->post(request, form.encodedQuery());
May 23, 2013
May 23, 2013
357
#endif
Oct 8, 2012
Oct 8, 2012
358
// we could expose downloadProgress() from reply in case we want progress info
Nov 4, 2012
Nov 4, 2012
359
360
361
362
363
364
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");
Mar 19, 2013
Mar 19, 2013
365
ssuLog->print(LOG_DEBUG, QString("Trying to get SSH keys from %1").arg(request.url().toString()));
Nov 4, 2012
Nov 4, 2012
366
367
368
pendingRequests++;
manager->get(request);
}
Oct 8, 2012
Oct 8, 2012
369
370
371
}
bool Ssu::setCredentials(QDomDocument *response){
Mar 30, 2013
Mar 30, 2013
372
SsuCoreConfig *settings = SsuCoreConfig::instance();
Oct 8, 2012
Oct 8, 2012
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// 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
418
Mar 19, 2013
Mar 19, 2013
419
420
SsuLog *ssuLog = SsuLog::instance();
Mar 11, 2013
Mar 11, 2013
421
// dump error message to systemd journal for easier debugging
Mar 19, 2013
Mar 19, 2013
422
ssuLog->print(LOG_WARNING, errorMessage);
Mar 11, 2013
Mar 11, 2013
423
Nov 4, 2012
Nov 4, 2012
424
// assume that we don't even need to wait for other pending requests,
Mar 11, 2013
Mar 11, 2013
425
// and just die. This is only relevant for CLI, which will exit after done()
Oct 8, 2012
Oct 8, 2012
426
427
428
emit done();
}
Nov 4, 2012
Nov 4, 2012
429
430
void Ssu::storeAuthorizedKeys(QByteArray data){
QDir dir;
Sep 9, 2013
Sep 9, 2013
431
SsuLog *ssuLog = SsuLog::instance();
Nov 4, 2012
Nov 4, 2012
432
Sep 9, 2013
Sep 9, 2013
433
434
435
436
437
438
439
440
441
442
443
444
445
int uid_min = getdef_num("UID_MIN", -1);
QString homePath;
if (getuid() >= uid_min){
homePath = dir.homePath();
} else if (getuid() == 0){
// place authorized_keys in the default users home when run with uid0
struct passwd *pw = getpwuid(uid_min);
if (pw == NULL){
ssuLog->print(LOG_DEBUG, QString("Unable to find password entry for uid %1")
.arg(uid_min));
return;
}
Nov 4, 2012
Nov 4, 2012
446
Sep 9, 2013
Sep 9, 2013
447
448
449
450
451
452
453
454
455
456
//homePath = QString(pw->pw_dir);
homePath = pw->pw_dir;
// use users uid/gid for creating the directories and files
setegid(pw->pw_gid);
seteuid(uid_min);
ssuLog->print(LOG_DEBUG, QString("Dropping to %1/%2 for writing authorized keys")
.arg(uid_min)
.arg(pw->pw_gid));
} else
Nov 4, 2012
Nov 4, 2012
457
458
return;
Sep 9, 2013
Sep 9, 2013
459
460
461
462
463
464
465
466
467
468
469
470
if (dir.exists(homePath + "/.ssh/authorized_keys")){
ssuLog->print(LOG_DEBUG, QString(".ssh/authorized_keys already exists in %1")
.arg(homePath));
return;
}
if (!dir.exists(homePath + "/.ssh"))
if (!dir.mkdir(homePath + "/.ssh")){
ssuLog->print(LOG_DEBUG, QString("Unable to create .ssh in %1")
.arg(homePath));
return;
}
Nov 4, 2012
Nov 4, 2012
471
Sep 9, 2013
Sep 9, 2013
472
QFile::setPermissions(homePath + "/.ssh",
Nov 4, 2012
Nov 4, 2012
473
474
QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner);
Sep 9, 2013
Sep 9, 2013
475
QFile authorizedKeys(homePath + "/.ssh/authorized_keys");
Nov 4, 2012
Nov 4, 2012
476
477
478
479
480
481
authorizedKeys.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate);
authorizedKeys.setPermissions(QFile::ReadOwner | QFile::WriteOwner);
QTextStream out(&authorizedKeys);
out << data;
out.flush();
authorizedKeys.close();
Sep 9, 2013
Sep 9, 2013
482
483
484
485
486
if (getuid() == 0){
seteuid(0);
setegid(0);
}
Nov 4, 2012
Nov 4, 2012
487
488
}
Oct 8, 2012
Oct 8, 2012
489
void Ssu::updateCredentials(bool force){
Mar 30, 2013
Mar 30, 2013
490
SsuCoreConfig *settings = SsuCoreConfig::instance();
Oct 8, 2012
Oct 8, 2012
491
492
errorFlag = false;
Mar 19, 2013
Mar 19, 2013
493
494
SsuLog *ssuLog = SsuLog::instance();
Mar 19, 2013
Mar 19, 2013
495
if (deviceInfo.deviceUid() == ""){
Oct 21, 2012
Oct 21, 2012
496
497
498
499
setError("No valid UID available for your device. For phones: is your modem online?");
return;
}
Oct 8, 2012
Oct 8, 2012
500
QString ssuCaCertificate, ssuCredentialsUrl;
Jul 6, 2013
Jul 6, 2013
501
502
503
ssuCaCertificate = SsuRepoManager::caCertificatePath();
if (ssuCaCertificate.isEmpty()){
setError("CA certificate for SSU not set ('_ca-certificate in domain')");
Oct 8, 2012
Oct 8, 2012
504
return;
Jul 6, 2013
Jul 6, 2013
505
}
Oct 8, 2012
Oct 8, 2012
506
507
if (!settings->contains("credentials-url")){
Jan 15, 2013
Jan 15, 2013
508
509
510
511
512
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
513
514
515
516
517
518
519
520
521
} else
ssuCredentialsUrl = settings->value("credentials-url").toString();
if (!isRegistered()){
setError("Device is not registered.");
return;
}
if (!force){
Mar 11, 2013
Mar 11, 2013
522
// skip updating if the last update was less than 30 minutes ago
Oct 8, 2012
Oct 8, 2012
523
524
525
526
QDateTime now = QDateTime::currentDateTime();
if (settings->contains("lastCredentialsUpdate")){
QDateTime last = settings->value("lastCredentialsUpdate").toDateTime();
Mar 11, 2013
Mar 11, 2013
527
if (last >= now.addSecs(-1800)){
Mar 19, 2013
Mar 19, 2013
528
ssuLog->print(LOG_DEBUG, QString("Skipping credentials update, last update was at %1")
Mar 11, 2013
Mar 11, 2013
529
.arg(last.toString()));
Oct 8, 2012
Oct 8, 2012
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
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;
Mar 19, 2013
Mar 19, 2013
552
request.setUrl(QUrl(ssuCredentialsUrl.arg(deviceInfo.deviceUid())));
Oct 8, 2012
Oct 8, 2012
553
Mar 19, 2013
Mar 19, 2013
554
ssuLog->print(LOG_DEBUG, QString("Sending credential update request to %1")
Mar 11, 2013
Mar 11, 2013
555
.arg(request.url().toString()));
Oct 8, 2012
Oct 8, 2012
556
557
request.setSslConfiguration(sslConfiguration);
Nov 4, 2012
Nov 4, 2012
558
559
pendingRequests++;
manager->get(request);
Oct 8, 2012
Oct 8, 2012
560
561
562
563
}
void Ssu::unregister(){
Mar 30, 2013
Mar 30, 2013
564
SsuCoreConfig *settings = SsuCoreConfig::instance();
Oct 8, 2012
Oct 8, 2012
565
566
567
settings->setValue("privateKey", "");
settings->setValue("certificate", "");
settings->setValue("registered", false);
Mar 22, 2013
Mar 22, 2013
568
settings->sync();
Oct 8, 2012
Oct 8, 2012
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
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;
}