| 1 | /* |
| 2 | * Copyright (C) 2013 Canonical Ltd. |
| 3 | * |
| 4 | * Contact: Alberto Mardegan <[email protected]> |
| 5 | * |
| 6 | * This file is part of liblomiri-online-accounts-plugin |
| 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 "account-manager.h" |
| 22 | #include "application-manager.h" |
| 23 | |
| 24 | #include <QDebug> |
| 25 | #include <QDomDocument> |
| 26 | #include <QDomElement> |
| 27 | #include <QFile> |
| 28 | #include <QSettings> |
| 29 | #include <QStandardPaths> |
| 30 | |
| 31 | using namespace OnlineAccountsUi; |
| 32 | |
| 33 | ApplicationManager *ApplicationManager::m_instance = 0; |
| 34 | |
| 35 | namespace OnlineAccountsUi { |
| 36 | class ApplicationManagerPrivate |
| 37 | { |
| 38 | public: |
| 39 | ApplicationManagerPrivate(); |
| 40 | |
| 41 | QString applicationProfile(const QString &applicationId) const; |
| 42 | bool applicationMatchesProfile(const Accounts::Application &application, |
| 43 | const QString &profile) const; |
| 44 | static QString stripVersion(const QString &appId); |
| 45 | static QString displayId(const QString &appId); |
| 46 | }; |
| 47 | } // namespace |
| 48 | |
| 49 | ApplicationManagerPrivate::ApplicationManagerPrivate() |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | QString ApplicationManagerPrivate::applicationProfile(const QString &applicationId) const |
| 54 | { |
| 55 | /* We need to load the XML file and look for the "profile" element. The |
| 56 | * file lookup would become unnecessary if a domDocument() method were |
| 57 | * added to the Accounts::Application class. */ |
| 58 | QString localShare = |
| 59 | QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); |
| 60 | QFile file(QString("%1/accounts/applications/%2.application"). |
| 61 | arg(localShare).arg(applicationId)); |
| 62 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 63 | qDebug() << "file not found:" << file.fileName(); |
| 64 | /* libaccounts would fall back to looking into /usr/share/accounts/, |
| 65 | * but we know that .click packages don't install files in there, and |
| 66 | * currently the profile information is only attached to click |
| 67 | * applications. Therefore, if we don't find the file in |
| 68 | * ~/.local/share/accounts/, we can assume we won't find the profile |
| 69 | * info anywhere. |
| 70 | */ |
| 71 | return QString(); |
| 72 | } |
| 73 | QDomDocument doc; |
| 74 | doc.setContent(&file); |
| 75 | const QDomElement root = doc.documentElement(); |
| 76 | return root.firstChildElement(QStringLiteral("profile")).text(); |
| 77 | } |
| 78 | |
| 79 | bool ApplicationManagerPrivate::applicationMatchesProfile(const Accounts::Application &application, |
| 80 | const QString &profile) const |
| 81 | { |
| 82 | /* We don't restrict unconfined apps. */ |
| 83 | if (profile == QStringLiteral("unconfined")) return true; |
| 84 | |
| 85 | /* It's a confined app. We must make sure that the applicationId it |
| 86 | * specified matches the apparmor profile. |
| 87 | */ |
| 88 | QString declaredProfile = applicationProfile(application.name()); |
| 89 | return declaredProfile == profile; |
| 90 | } |
| 91 | |
| 92 | QString ApplicationManagerPrivate::stripVersion(const QString &appId) |
| 93 | { |
| 94 | QStringList components = appId.split('_'); |
| 95 | if (components.count() != 3) return QString(); |
| 96 | |
| 97 | /* Click packages have a profile of the form |
| 98 | * $name_$application_$version |
| 99 | * (see https://wiki.ubuntu.com/SecurityTeam/Specifications/ApplicationConfinement/Manifest#Click) |
| 100 | * |
| 101 | * We assume that this is a click package, and strip out the last part. |
| 102 | */ |
| 103 | components.removeLast(); |
| 104 | return components.join('_'); |
| 105 | } |
| 106 | |
| 107 | QString ApplicationManagerPrivate::displayId(const QString &appId) |
| 108 | { |
| 109 | QStringList components = appId.split('_').mid(0, 2); |
| 110 | if (components.count() != 2) return appId; |
| 111 | |
| 112 | return components.join('/'); |
| 113 | } |
| 114 | |
| 115 | ApplicationManager *ApplicationManager::instance() |
| 116 | { |
| 117 | if (!m_instance) { |
| 118 | m_instance = new ApplicationManager; |
| 119 | } |
| 120 | |
| 121 | return m_instance; |
| 122 | } |
| 123 | |
| 124 | ApplicationManager::ApplicationManager(QObject *parent): |
| 125 | QObject(parent), |
| 126 | d_ptr(new ApplicationManagerPrivate) |
| 127 | { |
| 128 | } |
| 129 | |
| 130 | ApplicationManager::~ApplicationManager() |
| 131 | { |
| 132 | delete d_ptr; |
| 133 | } |
| 134 | |
| 135 | QVariantMap ApplicationManager::applicationInfo(const QString &claimedAppId, |
| 136 | const QString &profile) |
| 137 | { |
| 138 | Q_D(const ApplicationManager); |
| 139 | |
| 140 | if (Q_UNLIKELY(profile.isEmpty())) return QVariantMap(); |
| 141 | |
| 142 | /* Special case: when the applicationId is "lomiri-system-settings", we don't |
| 143 | * require the existance of the .application file, because this request |
| 144 | * will always be about creating a new account. */ |
| 145 | if (claimedAppId == "lomiri-system-settings") { |
| 146 | QVariantMap app; |
| 147 | app.insert(QStringLiteral("id"), claimedAppId); |
| 148 | app.insert(QStringLiteral("profile"), profile); |
| 149 | return app; |
| 150 | } |
| 151 | |
| 152 | QString applicationId = claimedAppId; |
| 153 | Accounts::Application application = |
| 154 | AccountManager::instance()->application(applicationId); |
| 155 | if (!application.isValid()) { |
| 156 | application = applicationFromProfile(profile); |
| 157 | applicationId = application.name(); |
| 158 | } |
| 159 | |
| 160 | /* Make sure that the app is who it claims to be */ |
| 161 | if (!d->applicationMatchesProfile(application, profile)) { |
| 162 | qDebug() << "Given applicationId doesn't match profile"; |
| 163 | return QVariantMap(); |
| 164 | } |
| 165 | |
| 166 | QVariantMap app; |
| 167 | app.insert(QStringLiteral("id"), applicationId); |
| 168 | app.insert(QStringLiteral("displayId"), d->displayId(applicationId)); |
| 169 | app.insert(QStringLiteral("displayName"), application.displayName()); |
| 170 | app.insert(QStringLiteral("icon"), application.iconName()); |
| 171 | /* The applicationMatchesProfile() test above ensures that either the peer |
| 172 | * is unconfined, or the profile in the .application file matches the one |
| 173 | * we see from our peer. |
| 174 | * In the first case, what we really want is the profile from the |
| 175 | * .application file (if that's set), to cover the case where an unconfined |
| 176 | * process is asking authorization on behalf of a confined app. */ |
| 177 | QString targetProfile = d->applicationProfile(application.name()); |
| 178 | if (targetProfile.isEmpty()) { |
| 179 | targetProfile = profile; |
| 180 | } |
| 181 | app.insert(QStringLiteral("profile"), targetProfile); |
| 182 | |
| 183 | /* List all the services supported by this application */ |
| 184 | QVariantList serviceIds; |
| 185 | Accounts::ServiceList allServices = |
| 186 | AccountManager::instance()->serviceList(); |
| 187 | Q_FOREACH(const Accounts::Service &service, allServices) { |
| 188 | if (!application.serviceUsage(service).isEmpty()) { |
| 189 | serviceIds.append(service.name()); |
| 190 | } |
| 191 | } |
| 192 | app.insert(QStringLiteral("services"), serviceIds); |
| 193 | |
| 194 | return app; |
| 195 | } |
| 196 | |
| 197 | QVariantMap ApplicationManager::providerInfo(const QString &providerId) const |
| 198 | { |
| 199 | Accounts::Provider provider = |
| 200 | AccountManager::instance()->provider(providerId); |
| 201 | if (Q_UNLIKELY(!provider.isValid())) { |
| 202 | qWarning() << "Invalid provider" << providerId; |
| 203 | return QVariantMap(); |
| 204 | } |
| 205 | |
| 206 | QVariantMap info; |
| 207 | info.insert(QStringLiteral("id"), providerId); |
| 208 | info.insert(QStringLiteral("displayName"), provider.displayName()); |
| 209 | info.insert(QStringLiteral("icon"), provider.iconName()); |
| 210 | info.insert(QStringLiteral("isSingleAccount"), provider.isSingleAccount()); |
| 211 | |
| 212 | /* Get Ubuntu-specific information directly from the XML file */ |
| 213 | const QDomDocument doc = provider.domDocument(); |
| 214 | QDomElement root = doc.documentElement(); |
| 215 | info.insert(QStringLiteral("profile"), |
| 216 | root.firstChildElement("profile").text()); |
| 217 | info.insert(QStringLiteral("package-dir"), |
| 218 | root.firstChildElement("package-dir").text()); |
| 219 | return info; |
| 220 | } |
| 221 | |
| 222 | QStringList ApplicationManager::usefulProviders() const |
| 223 | { |
| 224 | AccountManager *manager = AccountManager::instance(); |
| 225 | Accounts::ServiceList allServices = manager->serviceList(); |
| 226 | QStringList providers; |
| 227 | Q_FOREACH(const Accounts::Service &service, allServices) { |
| 228 | if (providers.contains(service.provider())) continue; |
| 229 | |
| 230 | if (!manager->applicationList(service).isEmpty() || |
| 231 | service.provider() == "ubuntuone") { |
| 232 | providers.append(service.provider()); |
| 233 | } |
| 234 | } |
| 235 | return providers; |
| 236 | } |
| 237 | |
| 238 | QStringList |
| 239 | ApplicationManager::addApplicationToAcl(const QStringList &acl, |
| 240 | const QString &applicationId) const |
| 241 | { |
| 242 | Q_D(const ApplicationManager); |
| 243 | |
| 244 | QStringList newAcl = acl; |
| 245 | QString profile = d->applicationProfile(applicationId); |
| 246 | qDebug() << "profile of" << applicationId << ":" << profile; |
| 247 | if (!profile.isEmpty()) { |
| 248 | newAcl.append(profile); |
| 249 | } |
| 250 | return newAcl; |
| 251 | } |
| 252 | |
| 253 | QStringList |
| 254 | ApplicationManager::removeApplicationFromAcl(const QStringList &acl, |
| 255 | const QString &applicationId) const |
| 256 | { |
| 257 | Q_D(const ApplicationManager); |
| 258 | |
| 259 | QString profile = d->applicationProfile(applicationId); |
| 260 | if (profile.isEmpty()) { |
| 261 | return acl; |
| 262 | } |
| 263 | |
| 264 | QStringList newAcl; |
| 265 | QString unversionedProfile = |
| 266 | ApplicationManagerPrivate::stripVersion(profile); |
| 267 | Q_FOREACH(const QString &app, acl) { |
| 268 | if (app != profile && |
| 269 | (unversionedProfile.isEmpty() || |
| 270 | !app.startsWith(unversionedProfile))) { |
| 271 | newAcl.append(app); |
| 272 | } |
| 273 | } |
| 274 | return newAcl; |
| 275 | } |
| 276 | |
| 277 | Accounts::Application |
| 278 | ApplicationManager::applicationFromProfile(const QString &profile) |
| 279 | { |
| 280 | /* If the profile is not a click package profile, we have no way of knowing |
| 281 | * what application it is. */ |
| 282 | QStringList components = profile.split('_'); |
| 283 | if (components.count() != 3) return Accounts::Application(); |
| 284 | |
| 285 | /* First try to see if we can use the full profile as app ID; if not, strip |
| 286 | * out the version, and if that fails as well then use only the package |
| 287 | * name. */ |
| 288 | AccountManager *manager = AccountManager::instance(); |
| 289 | Accounts::Application application = manager->application(profile); |
| 290 | if (application.isValid()) return application; |
| 291 | |
| 292 | QString applicationId = components[0] + "_" + components[1]; |
| 293 | application = manager->application(applicationId); |
| 294 | if (application.isValid()) return application; |
| 295 | |
| 296 | return manager->application(components[0]); |
| 297 | } |