| 1 | /* |
| 2 | * This file is part of lomiri-online-accounts-ui |
| 3 | * |
| 4 | * Copyright (C) 2011-2014 Canonical Ltd. |
| 5 | * |
| 6 | * Contact: Alberto Mardegan <[email protected]> |
| 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 "signonui-request.h" |
| 22 | |
| 23 | #include "browser-request.h" |
| 24 | #include <online-accounts-common/debug.h> |
| 25 | #include "dialog-request.h" |
| 26 | #include "external-browser-request.h" |
| 27 | #include <online-accounts-common/globals.h> |
| 28 | |
| 29 | #include <Accounts/Account> |
| 30 | #include <Accounts/Provider> |
| 31 | #include <LomiriOnlineAccountsPlugin/account-manager.h> |
| 32 | #include <LomiriOnlineAccountsPlugin/request-handler.h> |
| 33 | #include <QDBusArgument> |
| 34 | #include <QPointer> |
| 35 | #include <QUrl> |
| 36 | #include <SignOn/uisessiondata.h> |
| 37 | #include <SignOn/uisessiondata_priv.h> |
| 38 | #include <sys/apparmor.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <unistd.h> |
| 41 | |
| 42 | using namespace SignOnUi; |
| 43 | |
| 44 | namespace SignOnUi { |
| 45 | |
| 46 | class RequestPrivate: public QObject |
| 47 | { |
| 48 | Q_OBJECT |
| 49 | Q_DECLARE_PUBLIC(Request) |
| 50 | |
| 51 | public: |
| 52 | RequestPrivate(Request *request); |
| 53 | ~RequestPrivate(); |
| 54 | |
| 55 | private: |
| 56 | Accounts::Account *findAccount(); |
| 57 | |
| 58 | private: |
| 59 | mutable Request *q_ptr; |
| 60 | QVariantMap m_clientData; |
| 61 | QPointer<RequestHandler> m_handler; |
| 62 | Accounts::Account *m_account; |
| 63 | }; |
| 64 | |
| 65 | } // namespace |
| 66 | |
| 67 | RequestPrivate::RequestPrivate(Request *request): |
| 68 | QObject(request), |
| 69 | q_ptr(request), |
| 70 | m_handler(0) |
| 71 | { |
| 72 | const QVariantMap ¶meters = request->parameters(); |
| 73 | if (parameters.contains(SSOUI_KEY_CLIENT_DATA)) { |
| 74 | QVariant variant = parameters[SSOUI_KEY_CLIENT_DATA]; |
| 75 | m_clientData = (variant.type() == QVariant::Map) ? |
| 76 | variant.toMap() : |
| 77 | qdbus_cast<QVariantMap>(variant.value<QDBusArgument>()); |
| 78 | } |
| 79 | |
| 80 | m_account = findAccount(); |
| 81 | } |
| 82 | |
| 83 | RequestPrivate::~RequestPrivate() |
| 84 | { |
| 85 | } |
| 86 | |
| 87 | Accounts::Account *RequestPrivate::findAccount() |
| 88 | { |
| 89 | Q_Q(Request); |
| 90 | |
| 91 | uint identity = q->identity(); |
| 92 | if (identity == 0) |
| 93 | return 0; |
| 94 | |
| 95 | /* Find the account using this identity. |
| 96 | * FIXME: there might be more than one! |
| 97 | */ |
| 98 | OnlineAccountsUi::AccountManager *manager = |
| 99 | OnlineAccountsUi::AccountManager::instance(); |
| 100 | Q_FOREACH(Accounts::AccountId accountId, manager->accountList()) { |
| 101 | Accounts::Account *account = manager->account(accountId); |
| 102 | if (account == 0) continue; |
| 103 | |
| 104 | QVariant value(QVariant::UInt); |
| 105 | if (account->value("CredentialsId", value) != Accounts::NONE && |
| 106 | value.toUInt() == identity) { |
| 107 | return account; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Not found |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | #ifndef NO_REQUEST_FACTORY |
| 116 | Request *Request::newRequest(int id, |
| 117 | const QString &clientProfile, |
| 118 | const QVariantMap ¶meters, |
| 119 | QObject *parent) |
| 120 | { |
| 121 | if (parameters.contains(SSOUI_KEY_OPENURL)) { |
| 122 | QUrl finalUrl = |
| 123 | QUrl::fromUserInput(parameters.value(SSOUI_KEY_FINALURL). |
| 124 | toString()); |
| 125 | if (finalUrl.host() == "localhost") { |
| 126 | /* Let's assume that we only use a loopback callback URL when we |
| 127 | * are forced to use the system browser; when we don't have this |
| 128 | * constraint, we can generally use any other URL. */ |
| 129 | return new SignOnUi::ExternalBrowserRequest(id, clientProfile, |
| 130 | parameters, parent); |
| 131 | } else { |
| 132 | return new SignOnUi::BrowserRequest(id, clientProfile, |
| 133 | parameters, parent); |
| 134 | } |
| 135 | } else { |
| 136 | return new SignOnUi::DialogRequest(id, clientProfile, |
| 137 | parameters, parent); |
| 138 | } |
| 139 | } |
| 140 | #endif |
| 141 | |
| 142 | static QString findClientProfile(const QString &clientProfile, |
| 143 | const QVariantMap ¶meters) |
| 144 | { |
| 145 | QString profile = clientProfile; |
| 146 | /* If the request is coming on the SignOnUi interface from an |
| 147 | * unconfined process and it carries the SSOUI_KEY_PID key, it means that |
| 148 | * it's coming from signond. In that case, we want to know what was the |
| 149 | * client which originated the call. |
| 150 | */ |
| 151 | if (profile == "unconfined" && |
| 152 | parameters.contains(SSOUI_KEY_PID)) { |
| 153 | pid_t pid = parameters.value(SSOUI_KEY_PID).toUInt(); |
| 154 | if (pid == getpid()) { |
| 155 | /* If the request was initiated by our own process, we might not |
| 156 | * have the rights to call the aa_* functions (we might be |
| 157 | * confined). */ |
| 158 | return QString(); |
| 159 | } |
| 160 | |
| 161 | char *con = NULL, *mode = NULL; |
| 162 | int ret = aa_gettaskcon(pid, &con, &mode); |
| 163 | if (Q_LIKELY(ret >= 0)) { |
| 164 | profile = QString::fromUtf8(con); |
| 165 | /* libapparmor allocates "con" and "mode" in a single allocation, |
| 166 | * so freeing "con" is actually freeing both. */ |
| 167 | free(con); |
| 168 | } else { |
| 169 | qWarning() << "Couldn't get apparmor profile of PID" << pid; |
| 170 | } |
| 171 | } |
| 172 | return profile; |
| 173 | } |
| 174 | |
| 175 | Request::Request(int id, |
| 176 | const QString &clientProfile, |
| 177 | const QVariantMap ¶meters, |
| 178 | QObject *parent): |
| 179 | OnlineAccountsUi::Request(SIGNONUI_INTERFACE, id, |
| 180 | findClientProfile(clientProfile, parameters), |
| 181 | parameters, parent), |
| 182 | d_ptr(new RequestPrivate(this)) |
| 183 | { |
| 184 | } |
| 185 | |
| 186 | Request::~Request() |
| 187 | { |
| 188 | } |
| 189 | |
| 190 | QString Request::ssoId(const QVariantMap ¶meters) |
| 191 | { |
| 192 | return parameters[SSOUI_KEY_REQUESTID].toString(); |
| 193 | } |
| 194 | |
| 195 | QString Request::ssoId() const |
| 196 | { |
| 197 | return Request::ssoId(parameters()); |
| 198 | } |
| 199 | |
| 200 | void Request::setWindow(QWindow *window) |
| 201 | { |
| 202 | /* Show the window only if we are in a prompt session */ |
| 203 | if (qgetenv("MIR_SOCKET").isEmpty()) { |
| 204 | QVariantMap result; |
| 205 | result[SSOUI_KEY_ERROR] = SignOn::QUERY_ERROR_FORBIDDEN; |
| 206 | setResult(result); |
| 207 | } else { |
| 208 | OnlineAccountsUi::Request::setWindow(window); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | uint Request::identity() const |
| 213 | { |
| 214 | return parameters().value(SSOUI_KEY_IDENTITY).toUInt(); |
| 215 | } |
| 216 | |
| 217 | QString Request::method() const |
| 218 | { |
| 219 | return parameters().value(SSOUI_KEY_METHOD).toString(); |
| 220 | } |
| 221 | |
| 222 | QString Request::mechanism() const |
| 223 | { |
| 224 | return parameters().value(SSOUI_KEY_MECHANISM).toString(); |
| 225 | } |
| 226 | |
| 227 | QString Request::providerId() const |
| 228 | { |
| 229 | Q_D(const Request); |
| 230 | return d->m_account ? d->m_account->providerName() : |
| 231 | d->m_clientData.value("providerId").toString(); |
| 232 | } |
| 233 | |
| 234 | QString Request::windowTitle() const |
| 235 | { |
| 236 | if (parameters().contains(SSOUI_KEY_TITLE)) { |
| 237 | return parameters()[SSOUI_KEY_TITLE].toString(); |
| 238 | } |
| 239 | |
| 240 | OnlineAccountsUi::AccountManager *manager = |
| 241 | OnlineAccountsUi::AccountManager::instance(); |
| 242 | Accounts::Provider provider = manager->provider(providerId()); |
| 243 | return provider.displayName(); |
| 244 | } |
| 245 | |
| 246 | const QVariantMap &Request::clientData() const |
| 247 | { |
| 248 | Q_D(const Request); |
| 249 | return d->m_clientData; |
| 250 | } |
| 251 | |
| 252 | void Request::setHandler(RequestHandler *handler) |
| 253 | { |
| 254 | Q_D(Request); |
| 255 | if (handler && d->m_handler) { |
| 256 | qWarning() << "Request handler already set!"; |
| 257 | return; |
| 258 | } |
| 259 | d->m_handler = handler; |
| 260 | } |
| 261 | |
| 262 | RequestHandler *Request::handler() const |
| 263 | { |
| 264 | Q_D(const Request); |
| 265 | return d->m_handler; |
| 266 | } |
| 267 | |
| 268 | void Request::setCanceled() |
| 269 | { |
| 270 | QVariantMap result; |
| 271 | result[SSOUI_KEY_ERROR] = SignOn::QUERY_ERROR_CANCELED; |
| 272 | |
| 273 | setResult(result); |
| 274 | } |
| 275 | |
| 276 | #include "signonui-request.moc" |