Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[registration] Restore original domain on registration failure
  • Loading branch information
Thomas Perl committed Dec 2, 2013
1 parent 0b5ba81 commit b4ad503
Showing 1 changed file with 67 additions and 46 deletions.
113 changes: 67 additions & 46 deletions libssu/ssu.cpp
Expand Up @@ -28,6 +28,8 @@

#include "../constants.h"

#define SSU_NETWORK_REQUEST_DOMAIN_DATA (static_cast<QNetworkRequest::Attribute>(QNetworkRequest::User + 1))

static void restoreUid(){
if (getuid() == 0){
seteuid(0);
Expand Down Expand Up @@ -234,6 +236,8 @@ void Ssu::requestFinished(QNetworkReply *reply){
QSslConfiguration sslConfiguration = reply->sslConfiguration();
SsuLog *ssuLog = SsuLog::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)
ssuLog->print(LOG_DEBUG, QString("Certificate used was issued for '%1' by '%2'. Complete chain:")
Expand All @@ -253,63 +257,77 @@ void Ssu::requestFinished(QNetworkReply *reply){
}
#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
// 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;
if (settings->contains("home-url")){
QString homeUrl = settings->value("home-url").toString().arg("");
homeUrl.remove(QRegExp("//+$"));

if (request.url().toString().startsWith(homeUrl, Qt::CaseInsensitive)){
// we don't care about errors on download request
if (reply->error() == 0) {
QByteArray data = reply->readAll();
storeAuthorizedKeys(data);
}

goto success;
}
}

if (reply->error() > 0){
pendingRequests--;
setError(reply->errorString());
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;
}
if (reply->error() > 0){
setError(reply->errorString());
goto failure;
}

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")
.arg(action));
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;
}
action = doc.elementsByTagName("action").at(0).toElement().text();

if (!verifyResponse(&doc)) {
goto failure;
}

ssuLog->print(LOG_DEBUG, QString("Handling request of type %1")
.arg(action));
if (action == "register") {
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));
if (pendingRequests == 0)
if (pendingRequests == 0) {
emit done();
}
}

void Ssu::sendRegistration(QString usernameDomain, QString password){
Expand All @@ -322,6 +340,10 @@ void Ssu::sendRegistration(QString usernameDomain, QString password){
SsuCoreConfig *settings = SsuCoreConfig::instance();
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
if (usernameDomain.contains('@')) {
// separate domain/username and set domain
Expand Down Expand Up @@ -362,7 +384,6 @@ void Ssu::sendRegistration(QString usernameDomain, QString password){

sslConfiguration.setCaCertificates(QSslCertificate::fromPath(ssuCaCertificate));

QNetworkRequest request;
request.setUrl(QUrl(QString(ssuRegisterUrl)
.arg(IMEI)
));
Expand Down

0 comments on commit b4ad503

Please sign in to comment.