| 1 | /* |
| 2 | * Copyright (C) 2015 Canonical Ltd. |
| 3 | * |
| 4 | * Contact: Alberto Mardegan <[email protected]> |
| 5 | * |
| 6 | * This file is part of lomiri-online-accounts-ui's click hooks |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 3, as published |
| 10 | * by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranties of |
| 14 | * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <Accounts/Account> |
| 22 | #include <Accounts/Manager> |
| 23 | #include <Accounts/Service> |
| 24 | #include <QCoreApplication> |
| 25 | #include <QDateTime> |
| 26 | #include <QDebug> |
| 27 | #include <QDir> |
| 28 | #include <QDomDocument> |
| 29 | #include <QDomElement> |
| 30 | #include <QFile> |
| 31 | #include <QFileInfo> |
| 32 | #include <QJsonArray> |
| 33 | #include <QJsonDocument> |
| 34 | #include <QJsonObject> |
| 35 | #include <QStandardPaths> |
| 36 | #include <QStringList> |
| 37 | #include <click.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <unistd.h> |
| 41 | #include <utime.h> |
| 42 | #include "acl-updater.h" |
| 43 | |
| 44 | static QString findPackageDir(const QString &appId) |
| 45 | { |
| 46 | /* For testing */ |
| 47 | QByteArray packageDirEnv = qgetenv("OAH_CLICK_DIR"); |
| 48 | if (!packageDirEnv.isEmpty()) { |
| 49 | return QString::fromUtf8(packageDirEnv); |
| 50 | } |
| 51 | |
| 52 | QStringList components = appId.split('_'); |
| 53 | QByteArray package = components.first().toUtf8(); |
| 54 | |
| 55 | GError *error = NULL; |
| 56 | ClickUser *user = click_user_new_for_user(NULL, NULL, &error); |
| 57 | if (Q_UNLIKELY(!user)) { |
| 58 | qWarning() << "Unable to read Click database:" << error->message; |
| 59 | g_error_free(error); |
| 60 | return QString(); |
| 61 | } |
| 62 | |
| 63 | gchar *pkgDir = click_user_get_path(user, package.constData(), &error); |
| 64 | if (Q_UNLIKELY(!pkgDir)) { |
| 65 | qWarning() << "Unable to get the Click package directory for" << |
| 66 | package << ":" << error->message; |
| 67 | g_error_free(error); |
| 68 | g_object_unref(user); |
| 69 | return QString(); |
| 70 | } |
| 71 | |
| 72 | QString ret = QString::fromUtf8(pkgDir); |
| 73 | g_object_unref(user); |
| 74 | g_free(pkgDir); |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | static QString stripVersion(const QString &appId) |
| 79 | { |
| 80 | QStringList components = appId.split('_'); |
| 81 | if (components.count() != 3) return appId; |
| 82 | |
| 83 | /* Click packages have a profile of the form |
| 84 | * $name_$application_$version |
| 85 | * (see https://wiki.ubuntu.com/SecurityTeam/Specifications/ApplicationConfinement/Manifest#Click) |
| 86 | * |
| 87 | * So we just need to strip out the last part. |
| 88 | */ |
| 89 | components.removeLast(); |
| 90 | return components.join('_'); |
| 91 | } |
| 92 | |
| 93 | class ManifestFile { |
| 94 | public: |
| 95 | ManifestFile(const QFileInfo &hookFileInfo, |
| 96 | const QString &appId, const QString &shortAppId); |
| 97 | |
| 98 | bool writeFiles(const QDir &accountsDir); |
| 99 | bool writeServiceFile(const QDir &accountsDir, const QString &id, |
| 100 | const QString &provider, const QJsonObject &json); |
| 101 | bool writePlugins(const QDir &accountsDir); |
| 102 | bool writeProviderFile(const QDir &accountsDir, const QString &id, |
| 103 | const QJsonObject &json); |
| 104 | QDomDocument createDocument() const; |
| 105 | QDomElement createGroup(QDomDocument &doc, const QString &name); |
| 106 | void addTemplate(QDomDocument &doc, const QJsonObject &json); |
| 107 | void addSetting(QDomElement &parent, const QString &name, |
| 108 | const QString &value, const QString &type = QString()); |
| 109 | void addSettings(QDomElement &parent, const QJsonObject &json); |
| 110 | void parseManifest(const QJsonObject &mainObject); |
| 111 | void checkId(const QString &shortAppId); |
| 112 | void addProfile(QDomDocument &doc); |
| 113 | void addPackageDir(QDomDocument &doc); |
| 114 | void addTranslations(QDomDocument &doc); |
| 115 | QString profile() const; |
| 116 | void addDesktopFile(QDomDocument &doc); |
| 117 | bool writeXmlFile(const QDomDocument &doc, const QString &fileName) const; |
| 118 | bool isValid() const { return m_isValid; } |
| 119 | |
| 120 | private: |
| 121 | QFileInfo m_hookFileInfo; |
| 122 | QString m_packageDir; |
| 123 | QJsonArray m_services; |
| 124 | QJsonObject m_plugin; |
| 125 | QString m_appId; |
| 126 | QString m_shortAppId; |
| 127 | QString m_trDomain; |
| 128 | bool m_isScope; |
| 129 | bool m_isValid; |
| 130 | }; |
| 131 | |
| 132 | ManifestFile::ManifestFile(const QFileInfo &hookFileInfo, |
| 133 | const QString &appId, |
| 134 | const QString &shortAppId): |
| 135 | m_hookFileInfo(hookFileInfo), |
| 136 | m_appId(appId), |
| 137 | m_shortAppId(shortAppId), |
| 138 | m_isScope(false), |
| 139 | m_isValid(false) |
| 140 | { |
| 141 | QFile file(hookFileInfo.filePath()); |
| 142 | if (file.open(QIODevice::ReadOnly)) { |
| 143 | QJsonDocument doc = QJsonDocument::fromJson(file.readAll()); |
| 144 | QJsonObject mainObject = doc.object(); |
| 145 | file.close(); |
| 146 | |
| 147 | m_services = mainObject.value("services").toArray(); |
| 148 | m_trDomain = mainObject.value("translations").toString(); |
| 149 | m_plugin = mainObject.value("plugin").toObject(); |
| 150 | m_isScope = mainObject.value("scope").toBool(); |
| 151 | m_isValid = !m_services.isEmpty() || !m_plugin.isEmpty(); |
| 152 | |
| 153 | m_packageDir = findPackageDir(appId); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | bool ManifestFile::writeFiles(const QDir &accountsDir) |
| 158 | { |
| 159 | bool ok = true; |
| 160 | QDomDocument doc = createDocument(); |
| 161 | QDomElement root = doc.createElement(QStringLiteral("application")); |
| 162 | root.setAttribute(QStringLiteral("id"), m_shortAppId); |
| 163 | doc.appendChild(root); |
| 164 | |
| 165 | addProfile(doc); |
| 166 | addPackageDir(doc); |
| 167 | addDesktopFile(doc); |
| 168 | addTranslations(doc); |
| 169 | |
| 170 | QDomElement servicesElem = doc.createElement(QStringLiteral("services")); |
| 171 | Q_FOREACH(const QJsonValue &v, m_services) { |
| 172 | QJsonObject o = v.toObject(); |
| 173 | QString provider = o.value("provider").toString(); |
| 174 | QString id = QString("%1_%2").arg(m_shortAppId).arg(provider); |
| 175 | QString description = o.value("description").toString(); |
| 176 | if (description.isEmpty()) description = "."; |
| 177 | |
| 178 | QDomElement serviceElem = doc.createElement(QStringLiteral("service")); |
| 179 | serviceElem.setAttribute(QStringLiteral("id"), id); |
| 180 | QDomElement elem = doc.createElement(QStringLiteral("description")); |
| 181 | elem.appendChild(doc.createTextNode(description)); |
| 182 | serviceElem.appendChild(elem); |
| 183 | servicesElem.appendChild(serviceElem); |
| 184 | |
| 185 | if (!writeServiceFile(accountsDir, id, provider, o)) { |
| 186 | qWarning() << "Writing service file failed" << id; |
| 187 | ok = false; |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | root.appendChild(servicesElem); |
| 192 | |
| 193 | if (!writePlugins(accountsDir)) { |
| 194 | ok = false; |
| 195 | } |
| 196 | |
| 197 | if (ok && !m_services.isEmpty()) { |
| 198 | QString applicationFile = |
| 199 | QString("applications/%1.application").arg(m_shortAppId); |
| 200 | if (!writeXmlFile(doc, accountsDir.filePath(applicationFile))) { |
| 201 | qWarning() << "Writing application file failed" << applicationFile; |
| 202 | ok = false; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return ok; |
| 207 | } |
| 208 | |
| 209 | bool ManifestFile::writeServiceFile(const QDir &accountsDir, const QString &id, |
| 210 | const QString &provider, |
| 211 | const QJsonObject &json) |
| 212 | { |
| 213 | QDomDocument doc = createDocument(); |
| 214 | QDomElement root = doc.createElement(QStringLiteral("service")); |
| 215 | root.setAttribute(QStringLiteral("id"), id); |
| 216 | doc.appendChild(root); |
| 217 | |
| 218 | // type |
| 219 | QDomElement elem = doc.createElement(QStringLiteral("type")); |
| 220 | elem.appendChild(doc.createTextNode(m_shortAppId)); |
| 221 | root.appendChild(elem); |
| 222 | |
| 223 | // provider |
| 224 | elem = doc.createElement(QStringLiteral("provider")); |
| 225 | elem.appendChild(doc.createTextNode(provider)); |
| 226 | root.appendChild(elem); |
| 227 | |
| 228 | // name |
| 229 | QString name = json.value(QStringLiteral("name")).toString(); |
| 230 | if (name.isEmpty()) { |
| 231 | name = "."; |
| 232 | } |
| 233 | elem = doc.createElement(QStringLiteral("name")); |
| 234 | elem.appendChild(doc.createTextNode(name)); |
| 235 | root.appendChild(elem); |
| 236 | |
| 237 | addProfile(doc); |
| 238 | addTranslations(doc); |
| 239 | addTemplate(doc, json); |
| 240 | |
| 241 | return writeXmlFile(doc, accountsDir.filePath(QString("services/%1.service").arg(id))); |
| 242 | } |
| 243 | |
| 244 | bool ManifestFile::writePlugins(const QDir &accountsDir) |
| 245 | { |
| 246 | if (m_plugin.isEmpty()) return true; |
| 247 | |
| 248 | QString qmlPlugin = m_plugin.value("qml").toString(); |
| 249 | if (Q_UNLIKELY(qmlPlugin.isEmpty())) { |
| 250 | qWarning() << "Plugin is missing 'qml' key"; |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | if (!QDir::isAbsolutePath(qmlPlugin)) { |
| 255 | qmlPlugin = m_packageDir + "/" + qmlPlugin; |
| 256 | } |
| 257 | |
| 258 | if (!QFile::exists(qmlPlugin)) { |
| 259 | qWarning() << "Can't find QML files in" << qmlPlugin; |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | QString qmlDestination = QString("qml-plugins/%1").arg(m_shortAppId); |
| 264 | QFile::remove(accountsDir.filePath(qmlDestination)); |
| 265 | if (!QFile::link(qmlPlugin, accountsDir.filePath(qmlDestination))) { |
| 266 | qWarning() << "Cannot symlink QML files" << qmlPlugin; |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | if (!writeProviderFile(accountsDir, m_shortAppId, m_plugin)) { |
| 271 | qWarning() << "Writing provider file failed" << m_shortAppId; |
| 272 | return false; |
| 273 | } |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | bool ManifestFile::writeProviderFile(const QDir &accountsDir, |
| 278 | const QString &id, |
| 279 | const QJsonObject &json) |
| 280 | { |
| 281 | QDomDocument doc = createDocument(); |
| 282 | QDomElement root = doc.createElement(QStringLiteral("provider")); |
| 283 | root.setAttribute(QStringLiteral("id"), id); |
| 284 | doc.appendChild(root); |
| 285 | |
| 286 | // name |
| 287 | QString name = json.value(QStringLiteral("name")).toString(); |
| 288 | if (Q_UNLIKELY(name.isEmpty())) { |
| 289 | qWarning() << "Provider name is required"; |
| 290 | return false; |
| 291 | } |
| 292 | QDomElement elem = doc.createElement(QStringLiteral("name")); |
| 293 | elem.appendChild(doc.createTextNode(name)); |
| 294 | root.appendChild(elem); |
| 295 | |
| 296 | // icon |
| 297 | QString icon = json.value(QStringLiteral("icon")).toString(); |
| 298 | if (Q_UNLIKELY(icon.isEmpty())) { |
| 299 | qWarning() << "Provider icon is required"; |
| 300 | return false; |
| 301 | } |
| 302 | if (!QDir::isAbsolutePath(icon)) { |
| 303 | QString test = m_packageDir + "/" + icon; |
| 304 | if (QFile::exists(test)) { |
| 305 | icon = test; |
| 306 | } |
| 307 | } |
| 308 | elem = doc.createElement(QStringLiteral("icon")); |
| 309 | elem.appendChild(doc.createTextNode(icon)); |
| 310 | root.appendChild(elem); |
| 311 | |
| 312 | addProfile(doc); |
| 313 | addTranslations(doc); |
| 314 | addTemplate(doc, json); |
| 315 | addPackageDir(doc); |
| 316 | |
| 317 | return writeXmlFile(doc, accountsDir.filePath(QString("providers/%1.provider").arg(id))); |
| 318 | } |
| 319 | |
| 320 | QDomDocument ManifestFile::createDocument() const |
| 321 | { |
| 322 | QDomDocument doc; |
| 323 | doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"")); |
| 324 | QString comment = QString("this file is auto-generated by %1; do not modify"). |
| 325 | arg(QCoreApplication::applicationName()); |
| 326 | doc.appendChild(doc.createComment(comment)); |
| 327 | return doc; |
| 328 | } |
| 329 | |
| 330 | QDomElement ManifestFile::createGroup(QDomDocument &doc, const QString &name) |
| 331 | { |
| 332 | QDomElement group = doc.createElement(QStringLiteral("group")); |
| 333 | group.setAttribute(QStringLiteral("name"), name); |
| 334 | return group; |
| 335 | } |
| 336 | |
| 337 | void ManifestFile::addTemplate(QDomDocument &doc, const QJsonObject &json) |
| 338 | { |
| 339 | QDomElement root = doc.documentElement(); |
| 340 | QDomElement templateElem = doc.createElement(QStringLiteral("template")); |
| 341 | |
| 342 | // auth |
| 343 | QDomElement authElem = createGroup(doc, QStringLiteral("auth")); |
| 344 | QJsonObject auth = json.value(QStringLiteral("auth")).toObject(); |
| 345 | for (QJsonObject::const_iterator i = auth.begin(); i != auth.end(); i++) { |
| 346 | QStringList authParts = i.key().split("/"); |
| 347 | if (authParts.count() != 2) { |
| 348 | qWarning() << "auth key must contain exactly one '/'!"; |
| 349 | continue; |
| 350 | } |
| 351 | QString method = authParts[0]; |
| 352 | QString mechanism = authParts[1]; |
| 353 | |
| 354 | addSetting(authElem, QStringLiteral("method"), method); |
| 355 | addSetting(authElem, QStringLiteral("mechanism"), mechanism); |
| 356 | |
| 357 | QDomElement methodGroup = createGroup(doc, method); |
| 358 | authElem.appendChild(methodGroup); |
| 359 | QDomElement mechanismGroup = createGroup(doc, mechanism); |
| 360 | |
| 361 | addSettings(mechanismGroup, i.value().toObject()); |
| 362 | methodGroup.appendChild(mechanismGroup); |
| 363 | } |
| 364 | |
| 365 | if (!auth.isEmpty()) templateElem.appendChild(authElem); |
| 366 | |
| 367 | // settings |
| 368 | QJsonObject settings = json.value(QStringLiteral("settings")).toObject(); |
| 369 | addSettings(templateElem, settings); |
| 370 | |
| 371 | if (templateElem.hasChildNodes()) { |
| 372 | root.appendChild(templateElem); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | void ManifestFile::addSetting(QDomElement &parent, const QString &name, |
| 377 | const QString &value, const QString &type) |
| 378 | { |
| 379 | QDomDocument doc = parent.ownerDocument(); |
| 380 | QDomElement setting = doc.createElement(QStringLiteral("setting")); |
| 381 | setting.setAttribute(QStringLiteral("name"), name); |
| 382 | if (!type.isEmpty()) { |
| 383 | setting.setAttribute(QStringLiteral("type"), type); |
| 384 | } |
| 385 | setting.appendChild(doc.createTextNode(value)); |
| 386 | parent.appendChild(setting); |
| 387 | } |
| 388 | |
| 389 | void ManifestFile::addSettings(QDomElement &parent, const QJsonObject &json) |
| 390 | { |
| 391 | QDomDocument doc = parent.ownerDocument(); |
| 392 | for (QJsonObject::const_iterator i = json.begin(); i != json.end(); i++) { |
| 393 | QDomElement elem = parent; |
| 394 | QStringList parts = i.key().split('/'); |
| 395 | QString key = parts.takeLast(); |
| 396 | Q_FOREACH(const QString &groupName, parts) { |
| 397 | QDomElement subGroup = createGroup(doc, groupName); |
| 398 | elem.appendChild(subGroup); |
| 399 | elem = subGroup; |
| 400 | } |
| 401 | QString value; |
| 402 | QString type; |
| 403 | switch (i.value().type()) { |
| 404 | case QJsonValue::Bool: |
| 405 | type = "b"; |
| 406 | value = i.value().toBool() ? "true" : "false"; |
| 407 | break; |
| 408 | case QJsonValue::String: |
| 409 | value = i.value().toString(); |
| 410 | break; |
| 411 | case QJsonValue::Array: |
| 412 | { |
| 413 | QJsonArray array = i.value().toArray(); |
| 414 | QStringList values; |
| 415 | Q_FOREACH(const QJsonValue &v, array) { |
| 416 | if (Q_UNLIKELY(v.type() != QJsonValue::String)) { |
| 417 | qWarning() << "Only arrays of strings are supported!"; |
| 418 | continue; |
| 419 | } |
| 420 | values.append(QString("'%1'").arg(v.toString())); |
| 421 | } |
| 422 | type = "as"; |
| 423 | value = QString("[%1]").arg(values.join(',')); |
| 424 | } |
| 425 | break; |
| 426 | case QJsonValue::Object: |
| 427 | { |
| 428 | QDomElement subGroup = createGroup(doc, key); |
| 429 | QJsonObject object = i.value().toObject(); |
| 430 | addSettings(subGroup, object); |
| 431 | elem.appendChild(subGroup); |
| 432 | } |
| 433 | break; |
| 434 | default: |
| 435 | qWarning() << "Unsupported setting type:" << i.value(); |
| 436 | } |
| 437 | if (value.isEmpty()) continue; |
| 438 | addSetting(elem, key, value, type); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | void ManifestFile::addProfile(QDomDocument &doc) |
| 443 | { |
| 444 | QDomElement root = doc.documentElement(); |
| 445 | QDomElement elem = doc.createElement(QStringLiteral("profile")); |
| 446 | elem.appendChild(doc.createTextNode(m_appId)); |
| 447 | root.appendChild(elem); |
| 448 | } |
| 449 | |
| 450 | void ManifestFile::addTranslations(QDomDocument &doc) |
| 451 | { |
| 452 | if (m_trDomain.isEmpty()) return; |
| 453 | QDomElement root = doc.documentElement(); |
| 454 | QDomElement elem = doc.createElement(QStringLiteral("translations")); |
| 455 | elem.appendChild(doc.createTextNode(m_trDomain)); |
| 456 | root.appendChild(elem); |
| 457 | } |
| 458 | |
| 459 | void ManifestFile::addPackageDir(QDomDocument &doc) |
| 460 | { |
| 461 | if (Q_UNLIKELY(m_packageDir.isEmpty())) return; |
| 462 | |
| 463 | QDomElement root = doc.documentElement(); |
| 464 | QDomElement elem = doc.createElement(QStringLiteral("package-dir")); |
| 465 | elem.appendChild(doc.createTextNode(m_packageDir)); |
| 466 | root.appendChild(elem); |
| 467 | } |
| 468 | |
| 469 | void ManifestFile::addDesktopFile(QDomDocument &doc) |
| 470 | { |
| 471 | QDomElement root = doc.documentElement(); |
| 472 | QDomElement elem = doc.createElement(QStringLiteral("desktop-entry")); |
| 473 | elem.appendChild(doc.createTextNode(m_isScope ? m_shortAppId : m_appId)); |
| 474 | root.appendChild(elem); |
| 475 | } |
| 476 | |
| 477 | bool ManifestFile::writeXmlFile(const QDomDocument &doc, const QString &fileName) const |
| 478 | { |
| 479 | QFile file(fileName); |
| 480 | if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return false; |
| 481 | |
| 482 | bool ok = (file.write(doc.toByteArray(2)) > 0); |
| 483 | file.close(); |
| 484 | |
| 485 | if (ok) { |
| 486 | return true; |
| 487 | } else { |
| 488 | QFile::remove(fileName); |
| 489 | return false; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | class LibAccountsFile: public QDomDocument { |
| 494 | public: |
| 495 | explicit LibAccountsFile(const QFileInfo &fileInfo); |
| 496 | |
| 497 | QString profile() const; |
| 498 | bool createdByUs() const; |
| 499 | bool isValid() const { return m_isValid; } |
| 500 | |
| 501 | private: |
| 502 | QFileInfo m_fileInfo; |
| 503 | bool m_isValid; |
| 504 | }; |
| 505 | |
| 506 | LibAccountsFile::LibAccountsFile(const QFileInfo &fileInfo): |
| 507 | QDomDocument(), |
| 508 | m_fileInfo(fileInfo), |
| 509 | m_isValid(false) |
| 510 | { |
| 511 | QFile file(fileInfo.filePath()); |
| 512 | if (file.open(QIODevice::ReadOnly)) { |
| 513 | if (setContent(&file)) { |
| 514 | m_isValid = true; |
| 515 | } |
| 516 | file.close(); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | QString LibAccountsFile::profile() const |
| 521 | { |
| 522 | QDomElement root = documentElement(); |
| 523 | return root.firstChildElement("profile").text(); |
| 524 | } |
| 525 | |
| 526 | bool LibAccountsFile::createdByUs() const |
| 527 | { |
| 528 | QString creatorMark = QCoreApplication::applicationName() + ";"; |
| 529 | for (QDomNode n = firstChild(); !n.isNull(); n = n.nextSibling()) { |
| 530 | if (n.isComment() && n.nodeValue().contains(creatorMark)) { |
| 531 | return true; |
| 532 | } |
| 533 | } |
| 534 | return false; |
| 535 | } |
| 536 | |
| 537 | static void disableService(Accounts::Manager *manager, |
| 538 | const QString &serviceId, |
| 539 | const QString &profile) |
| 540 | { |
| 541 | Accounts::Service service = manager->service(serviceId); |
| 542 | if (Q_UNLIKELY(!service.isValid())) return; |
| 543 | |
| 544 | AclUpdater aclUpdater; |
| 545 | Q_FOREACH(Accounts::AccountId accountId, manager->accountListEnabled()) { |
| 546 | Accounts::Account *account = manager->account(accountId); |
| 547 | if (Q_UNLIKELY(!account)) continue; |
| 548 | |
| 549 | if (account->providerName() != service.provider()) continue; |
| 550 | |
| 551 | uint credentialsId = account->credentialsId(); |
| 552 | account->selectService(service); |
| 553 | if (account->isEnabled()) { |
| 554 | account->setEnabled(false); |
| 555 | account->sync(); |
| 556 | aclUpdater.removeApp(stripVersion(profile), credentialsId); |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | static void removeStaleAccounts(Accounts::Manager *manager, |
| 562 | const QString &providerName) |
| 563 | { |
| 564 | Q_FOREACH(Accounts::AccountId id, manager->accountList()) { |
| 565 | Accounts::Account *account = manager->account(id); |
| 566 | if (account->providerName() == providerName) { |
| 567 | account->remove(); |
| 568 | account->syncAndBlock(); |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | static void removeStaleFiles(Accounts::Manager *manager, |
| 574 | const QStringList &fileTypes, |
| 575 | const QDir &accountsDir, |
| 576 | const QDir &hooksDirIn) |
| 577 | { |
| 578 | /* Walk through all of |
| 579 | * ~/.local/share/accounts/{providers,services,applications}/ |
| 580 | * and remove files which are no longer present in hooksDirIn. |
| 581 | */ |
| 582 | Q_FOREACH(const QString &fileType, fileTypes) { |
| 583 | QDir dir(accountsDir.filePath(fileType + "s")); |
| 584 | dir.setFilter(QDir::Files | QDir::Readable); |
| 585 | QStringList fileTypeFilter; |
| 586 | fileTypeFilter << "*." + fileType; |
| 587 | dir.setNameFilters(fileTypeFilter); |
| 588 | |
| 589 | Q_FOREACH(const QFileInfo &fileInfo, dir.entryInfoList()) { |
| 590 | LibAccountsFile file(fileInfo); |
| 591 | |
| 592 | /* If this file was not created by our hook let's ignore it. */ |
| 593 | if (!file.createdByUs()) continue; |
| 594 | |
| 595 | QString profile = file.profile(); |
| 596 | |
| 597 | /* Check that the hook file is still there; if it isn't, then it |
| 598 | * means that the click package was removed, and we must remove our |
| 599 | * copy as well. */ |
| 600 | QString hookFileName = stripVersion(profile) + "_*.accounts"; |
| 601 | QStringList nameFilters = QStringList() << hookFileName; |
| 602 | if (!hooksDirIn.entryList(nameFilters).isEmpty()) continue; |
| 603 | |
| 604 | if (fileType == "service") { |
| 605 | /* Make sure services get disabled. See also: |
| 606 | * https://bugs.launchpad.net/bugs/1417261 */ |
| 607 | disableService(manager, fileInfo.completeBaseName(), profile); |
| 608 | } else if (fileType == "provider") { |
| 609 | /* If this is a provider, we must also remove any accounts |
| 610 | * associated with it */ |
| 611 | removeStaleAccounts(manager, fileInfo.completeBaseName()); |
| 612 | } |
| 613 | QFile::remove(fileInfo.filePath()); |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | static void removeStaleTimestampFiles(const QDir &hooksDirIn) |
| 619 | { |
| 620 | Q_FOREACH(const QFileInfo &fileInfo, hooksDirIn.entryInfoList()) { |
| 621 | if (fileInfo.suffix() != "processed") continue; |
| 622 | |
| 623 | /* Find the corresponding hook file */ |
| 624 | QFileInfo hookInfo(fileInfo.absolutePath() + "/" + fileInfo.completeBaseName()); |
| 625 | |
| 626 | if (!hookInfo.exists()) { |
| 627 | QFile::remove(fileInfo.filePath()); |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | /* Get the modification time of a file; this differs from |
| 633 | * QFileInfo::lastModified() in that if the file is a symlink here we take the |
| 634 | * info from the symlink itself. */ |
| 635 | static QDateTime lastModified(const QFileInfo &fileInfo) |
| 636 | { |
| 637 | struct stat data; |
| 638 | if (lstat(fileInfo.filePath().toUtf8().constData(), &data) < 0) { |
| 639 | return QDateTime(); |
| 640 | } |
| 641 | return QDateTime::fromSecsSinceEpoch(data.st_mtime); |
| 642 | } |
| 643 | |
| 644 | int main(int argc, char **argv) |
| 645 | { |
| 646 | QCoreApplication app(argc, argv); |
| 647 | |
| 648 | Accounts::Manager::Options managerOptions; |
| 649 | if (qgetenv("DBUS_SESSION_BUS_ADDRESS").isEmpty()) { |
| 650 | managerOptions |= Accounts::Manager::DisableNotifications; |
| 651 | } |
| 652 | Accounts::Manager *manager = new Accounts::Manager(managerOptions); |
| 653 | |
| 654 | /* Go through the hook files in ~/.local/share/online-accounts-hooks2/ and |
| 655 | * check if they have already been processed into a file under |
| 656 | * ~/.local/share/accounts/{services,applications}/; |
| 657 | * if not, open the hook file, write the APP_ID somewhere in it, and |
| 658 | * save the result in the location where libaccounts expects to find it. |
| 659 | */ |
| 660 | |
| 661 | QStringList fileTypes; |
| 662 | fileTypes << |
| 663 | QStringLiteral("service") << |
| 664 | QStringLiteral("provider") << |
| 665 | QStringLiteral("application"); |
| 666 | |
| 667 | // This is ~/.local/share/ |
| 668 | const QString localShare = |
| 669 | QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); |
| 670 | QDir hooksDirIn(localShare + "/" + HOOK_FILES_SUBDIR); |
| 671 | |
| 672 | /* Make sure the directories exist */ |
| 673 | QDir accountsDir(localShare + "/accounts"); |
| 674 | accountsDir.mkpath("applications"); |
| 675 | accountsDir.mkpath("services"); |
| 676 | accountsDir.mkpath("providers"); |
| 677 | accountsDir.mkpath("qml-plugins"); |
| 678 | |
| 679 | removeStaleFiles(manager, fileTypes, accountsDir, hooksDirIn); |
| 680 | removeStaleTimestampFiles(hooksDirIn); |
| 681 | |
| 682 | Q_FOREACH(const QFileInfo &fileInfo, hooksDirIn.entryInfoList()) { |
| 683 | if (fileInfo.suffix() != "accounts") continue; |
| 684 | |
| 685 | // Our click hook sets the base name to the APP_ID |
| 686 | QString appId = fileInfo.completeBaseName(); |
| 687 | |
| 688 | /* When publishing this file for libaccounts, we want to strip |
| 689 | * the version number out. */ |
| 690 | QString shortAppId = stripVersion(appId); |
| 691 | |
| 692 | /* We create an empty file whenever we succesfully process a hook file. |
| 693 | * The name of this file is the same as the hook file, with the |
| 694 | * .processed suffix appended. |
| 695 | */ |
| 696 | QFileInfo processedInfo(fileInfo.filePath() + ".processed"); |
| 697 | if (processedInfo.exists() && |
| 698 | processedInfo.lastModified() == lastModified(fileInfo)) { |
| 699 | continue; |
| 700 | } |
| 701 | |
| 702 | ManifestFile manifest = ManifestFile(fileInfo, appId, shortAppId); |
| 703 | if (!manifest.isValid()) { |
| 704 | qWarning() << "Invalid file" << fileInfo.filePath(); |
| 705 | continue; |
| 706 | } |
| 707 | |
| 708 | if (manifest.writeFiles(accountsDir)) { |
| 709 | QFile file(processedInfo.filePath()); |
| 710 | if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { |
| 711 | file.close(); |
| 712 | struct utimbuf sourceTime; |
| 713 | sourceTime.actime = sourceTime.modtime = |
| 714 | lastModified(fileInfo).toSecsSinceEpoch(); |
| 715 | utime(processedInfo.filePath().toUtf8().constData(), |
| 716 | &sourceTime); |
| 717 | } else { |
| 718 | qWarning() << "Could not create timestamp file" << |
| 719 | processedInfo.filePath(); |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | /* To ensure that all the installed services are parsed into |
| 725 | * libaccounts' DB, we enumerate them now. |
| 726 | */ |
| 727 | manager->serviceList(); |
| 728 | delete manager; |
| 729 | |
| 730 | return EXIT_SUCCESS; |
| 731 | } |
| 732 |