Commit 8c911452 authored by Bernd Wachter's avatar Bernd Wachter

Merge pull request #27 from thp/restore-domain

[registration] Restore original domain on registration failure
parents 0b5ba81a b4ad503d
...@@ -28,6 +28,8 @@ ...@@ -28,6 +28,8 @@
#include "../constants.h" #include "../constants.h"
#define SSU_NETWORK_REQUEST_DOMAIN_DATA (static_cast<QNetworkRequest::Attribute>(QNetworkRequest::User + 1))
static void restoreUid(){ static void restoreUid(){
if (getuid() == 0){ if (getuid() == 0){
seteuid(0); seteuid(0);
...@@ -234,6 +236,8 @@ void Ssu::requestFinished(QNetworkReply *reply){ ...@@ -234,6 +236,8 @@ void Ssu::requestFinished(QNetworkReply *reply){
QSslConfiguration sslConfiguration = reply->sslConfiguration(); QSslConfiguration sslConfiguration = reply->sslConfiguration();
SsuLog *ssuLog = SsuLog::instance(); SsuLog *ssuLog = SsuLog::instance();
SsuCoreConfig *settings = SsuCoreConfig::instance(); SsuCoreConfig *settings = SsuCoreConfig::instance();
QNetworkRequest request = reply->request();
QVariant originalDomainVariant = request.attribute(SSU_NETWORK_REQUEST_DOMAIN_DATA);
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
ssuLog->print(LOG_DEBUG, QString("Certificate used was issued for '%1' by '%2'. Complete chain:") ssuLog->print(LOG_DEBUG, QString("Certificate used was issued for '%1' by '%2'. Complete chain:")
...@@ -253,63 +257,77 @@ void Ssu::requestFinished(QNetworkReply *reply){ ...@@ -253,63 +257,77 @@ void Ssu::requestFinished(QNetworkReply *reply){
} }
#endif #endif
pendingRequests--;
QString action;
QByteArray data;
QDomDocument doc;
QString xmlError;
/// @TODO: indicate that the device is not registered if there's a 404 on credentials update url /// @TODO: indicate that the device is not registered if there's a 404 on credentials update url
// what sucks more, this or goto? if (settings->contains("home-url")){
do { QString homeUrl = settings->value("home-url").toString().arg("");
if (settings->contains("home-url")){ homeUrl.remove(QRegExp("//+$"));
QString homeUrl = settings->value("home-url").toString().arg("");
homeUrl.remove(QRegExp("//+$")); if (request.url().toString().startsWith(homeUrl, Qt::CaseInsensitive)){
QNetworkRequest request = reply->request(); // we don't care about errors on download request
if (reply->error() == 0) {
if (request.url().toString().startsWith(homeUrl, Qt::CaseInsensitive)){ QByteArray data = reply->readAll();
// we don't care about errors on download request storeAuthorizedKeys(data);
if (reply->error() > 0) break;
QByteArray data = reply->readAll();
storeAuthorizedKeys(data);
break;
} }
goto success;
} }
}
if (reply->error() > 0){ if (reply->error() > 0){
pendingRequests--; setError(reply->errorString());
setError(reply->errorString()); goto failure;
return; }
} else {
QByteArray data = reply->readAll();
ssuLog->print(LOG_DEBUG, QString("RequestOutput %1")
.arg(data.data()));
QDomDocument doc;
QString xmlError;
if (!doc.setContent(data, &xmlError)){
pendingRequests--;
setError(tr("Unable to parse server response (%1)").arg(xmlError));
return;
}
QString action = doc.elementsByTagName("action").at(0).toElement().text(); data = reply->readAll();
ssuLog->print(LOG_DEBUG, QString("RequestOutput %1")
.arg(data.data()));
if (!verifyResponse(&doc)) break; if (!doc.setContent(data, &xmlError)){
setError(tr("Unable to parse server response (%1)").arg(xmlError));
goto failure;
}
ssuLog->print(LOG_DEBUG, QString("Handling request of type %1") action = doc.elementsByTagName("action").at(0).toElement().text();
.arg(action));
if (action == "register"){ if (!verifyResponse(&doc)) {
if (!registerDevice(&doc)) break; goto failure;
} else if (action == "credentials"){ }
if (!setCredentials(&doc)) break;
} else { ssuLog->print(LOG_DEBUG, QString("Handling request of type %1")
pendingRequests--; .arg(action));
setError(tr("Response to unknown action encountered: %1").arg(action)); if (action == "register") {
return; if (registerDevice(&doc)) {
} goto success;
} }
} while (false); } else if (action == "credentials") {
if (setCredentials(&doc)) {
goto success;
}
} else {
setError(tr("Response to unknown action encountered: %1").arg(action));
}
pendingRequests--; failure:
// Restore the original domain in case of failures with the registration
if (!originalDomainVariant.isNull()) {
QString originalDomain = originalDomainVariant.toString();
ssuLog->print(LOG_DEBUG, QString("Restoring domain on error: '%1'").arg(originalDomain));
setDomain(originalDomain);
}
// Fall through to cleanup handling in success from failure label
success:
ssuLog->print(LOG_DEBUG, QString("Request finished, pending requests: %1").arg(pendingRequests)); ssuLog->print(LOG_DEBUG, QString("Request finished, pending requests: %1").arg(pendingRequests));
if (pendingRequests == 0) if (pendingRequests == 0) {
emit done(); emit done();
}
} }
void Ssu::sendRegistration(QString usernameDomain, QString password){ void Ssu::sendRegistration(QString usernameDomain, QString password){
...@@ -322,6 +340,10 @@ void Ssu::sendRegistration(QString usernameDomain, QString password){ ...@@ -322,6 +340,10 @@ void Ssu::sendRegistration(QString usernameDomain, QString password){
SsuCoreConfig *settings = SsuCoreConfig::instance(); SsuCoreConfig *settings = SsuCoreConfig::instance();
SsuDeviceInfo deviceInfo; SsuDeviceInfo deviceInfo;
QNetworkRequest request;
request.setAttribute(SSU_NETWORK_REQUEST_DOMAIN_DATA, domain());
ssuLog->print(LOG_DEBUG, QString("Saving current domain before request: '%1'").arg(domain()));
// Username can include also domain, (user@domain), separate those // Username can include also domain, (user@domain), separate those
if (usernameDomain.contains('@')) { if (usernameDomain.contains('@')) {
// separate domain/username and set domain // separate domain/username and set domain
...@@ -362,7 +384,6 @@ void Ssu::sendRegistration(QString usernameDomain, QString password){ ...@@ -362,7 +384,6 @@ void Ssu::sendRegistration(QString usernameDomain, QString password){
sslConfiguration.setCaCertificates(QSslCertificate::fromPath(ssuCaCertificate)); sslConfiguration.setCaCertificates(QSslCertificate::fromPath(ssuCaCertificate));
QNetworkRequest request;
request.setUrl(QUrl(QString(ssuRegisterUrl) request.setUrl(QUrl(QString(ssuRegisterUrl)
.arg(IMEI) .arg(IMEI)
)); ));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment