/** * @file ssuurlresolver.cpp * @copyright 2012 Jolla Ltd. * @author Bernd Wachter * @date 2012 */ #include #include #include "ssuurlresolver.h" SsuUrlResolver::SsuUrlResolver(): QObject(){ QObject::connect(this,SIGNAL(done()), QCoreApplication::instance(),SLOT(quit()), Qt::QueuedConnection); } void SsuUrlResolver::printJournal(int priority, QString message){ QByteArray ba = message.toUtf8(); const char *ca = ba.constData(); if (sd_journal_print(LOG_INFO, "ssu: %s", ca) < 0){ QFile logfile; QTextStream logstream; logfile.setFileName("/tmp/ssu.log"); logfile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append); logstream.setDevice(&logfile); logstream << message << "\n"; logstream.flush(); } } void SsuUrlResolver::run(){ QHash repoParameters; QString resolvedUrl, repo; bool isRnd = false; PluginFrame in(std::cin); if (in.headerEmpty()){ // FIXME, do something; we need at least repo header printJournal(LOG_WARNING, "Received empty header list. Most likely your ssu setup is broken"); } PluginFrame::HeaderListIterator it; QStringList headerList; /* hasKey() for some reason makes zypper run into timeouts, so we have to handle special keys here */ for (it=in.headerBegin();it!=in.headerEnd();it++){ QString first = QString::fromStdString((*it).first); QString second = QString::fromStdString((*it).second); if (first == "repo"){ repo = second; } else if (first == "rnd"){ isRnd = true; } else if (first == "deviceFamily"){ repoParameters.insert(first, second); } else if (first == "arch"){ repoParameters.insert(first, second); } else if (first == "debug"){ repoParameters.insert("debugSplit", "debug"); } else { if ((*it).second.empty()) headerList.append(first); else headerList.append(QString("%1=%2") .arg(first) .arg(second)); } } if (!ssu.useSslVerify()) headerList.append("ssl_verify=no"); if (isRnd){ SignalWait w; connect(&ssu, SIGNAL(done()), &w, SLOT(finished())); ssu.updateCredentials(); w.sleep(); } // TODO: check for credentials scope required for repository; check if the file exists; // compare with configuration, and dump credentials to file if necessary printJournal(LOG_DEBUG, QString("Requesting credentials for '%1' with RND status %2...").arg(repo).arg(isRnd)); QString credentialsScope = ssu.credentialsScope(repo, isRnd); if (!credentialsScope.isEmpty()){ headerList.append(QString("credentials=%1").arg(credentialsScope)); QFileInfo credentialsFileInfo("/etc/zypp/credentials.d/" + credentialsScope); if (!credentialsFileInfo.exists() || credentialsFileInfo.lastModified() <= ssu.lastCredentialsUpdate()){ QFile credentialsFile(credentialsFileInfo.filePath()); credentialsFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); QTextStream out(&credentialsFile); QPair credentials = ssu.credentials(credentialsScope); out << "[" << ssu.credentialsUrl(credentialsScope) << "]\n"; out << "username=" << credentials.first << "\n"; out << "password=" << credentials.second << "\n"; out.flush(); credentialsFile.close(); } } if (headerList.isEmpty()){ resolvedUrl = ssu.repoUrl(repo, isRnd, repoParameters); } else { resolvedUrl = QString("%1?%2") .arg(ssu.repoUrl(repo, isRnd, repoParameters)) .arg(headerList.join("&")); } printJournal(LOG_INFO, QString("%1 resolved to %2").arg(repo).arg(resolvedUrl)); PluginFrame out("RESOLVEDURL"); out.setBody(resolvedUrl.toStdString()); out.writeTo(std::cout); emit done(); }