Debian Codegraph @

online-accounts-service/src/libaccounts-service.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2014 Canonical Ltd.
3 *
4 * Contact: Alberto Mardegan <[email protected]>
5 *
6 * This file is part of lomiri-online-accounts-service
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 <online-accounts-common/debug.h>
22 #include "libaccounts-service.h"
23 #include "utils.h"
24
25 #include <Accounts/Account>
26 #include <Accounts/Manager>
27 #include <Accounts/Service>
28 #include <QDBusArgument>
29 #include <QDBusConnection>
30 #include <QVariantMap>
31
32 using namespace OnlineAccountsUi;
33
34 static QString stripVersion(const QString &appId)
35 {
36 QStringList components = appId.split('_');
37 if (components.count() != 3) return appId;
38
39 /* Click packages have a profile of the form
40 * $name_$application_$version
41 * (see https://wiki.ubuntu.com/SecurityTeam/Specifications/ApplicationConfinement/Manifest#Click)
42 *
43 * We assume that this is a click package, and strip out the last part.
44 */
45 components.removeLast();
46 return components.join('_');
47 }
48
49 namespace OnlineAccountsUi {
50
51 struct ServiceChanges {
52 QString service;
53 QString serviceType;
54 quint32 serviceId;
55 QVariantMap settings;
56 QStringList removedKeys;
57 };
58
59 struct AccountChanges {
60 quint32 accountId;
61 bool created;
62 bool deleted;
63 QString provider;
64 QList<ServiceChanges> serviceChanges;
65 };
66
67 struct PendingWrite {
68 PendingWrite(const QDBusConnection &c, const QDBusMessage &m):
69 message(m), connection(c) {}
70 QDBusMessage message;
71 QDBusConnection connection;
72 };
73
74 class LibaccountsServicePrivate: public QObject
75 {
76 Q_OBJECT
77 Q_DECLARE_PUBLIC(LibaccountsService)
78
79 public:
80 LibaccountsServicePrivate(LibaccountsService *q);
81 ~LibaccountsServicePrivate() {};
82
83 void writeChanges(const AccountChanges &changes);
84
85 private Q_SLOTS:
86 void onAccountSynced();
87 void onAccountError(Accounts::Error error);
88
89 private:
90 Accounts::Manager m_manager;
91 QHash<Accounts::Account *,PendingWrite> m_pendingWrites;
92 mutable LibaccountsService *q_ptr;
93 };
94
95 } // namespace
96
97 LibaccountsServicePrivate::LibaccountsServicePrivate(LibaccountsService *q):
98 QObject(q),
99 m_manager(new Accounts::Manager(this)),
100 q_ptr(q)
101 {
102 }
103
104 void LibaccountsServicePrivate::writeChanges(const AccountChanges &changes)
105 {
106 Q_Q(LibaccountsService);
107
108 Accounts::Account *account;
109
110 if (changes.created) {
111 account = m_manager.createAccount(changes.provider);
112 } else {
113 account = m_manager.account(changes.accountId);
114 if (Q_UNLIKELY(!account)) {
115 qWarning() << "Couldn't load account" << changes.accountId;
116 return;
117 }
118 }
119
120 Q_ASSERT(account);
121
122 if (changes.deleted) {
123 account->remove();
124 } else {
125 Q_FOREACH(const ServiceChanges &sc, changes.serviceChanges) {
126 if (sc.service == "global") {
127 account->selectService();
128 } else {
129 Accounts::Service service = m_manager.service(sc.service);
130 if (Q_UNLIKELY(!service.isValid())) {
131 qWarning() << "Invalid service" << sc.service;
132 continue;
133 }
134
135 account->selectService(service);
136 }
137
138 QMapIterator<QString, QVariant> it(sc.settings);
139 while (it.hasNext()) {
140 it.next();
141 account->setValue(it.key(), it.value());
142 }
143
144 Q_FOREACH(const QString &key, sc.removedKeys) {
145 account->remove(key);
146 }
147 }
148 }
149
150 m_pendingWrites.insert(account,
151 PendingWrite(q->connection(), q->message()));
152 QObject::connect(account, SIGNAL(synced()),
153 this, SLOT(onAccountSynced()));
154 QObject::connect(account, SIGNAL(error(Accounts::Error)),
155 this, SLOT(onAccountError(Accounts::Error)));
156 account->sync();
157 }
158
159 void LibaccountsServicePrivate::onAccountSynced()
160 {
161 Accounts::Account *account = qobject_cast<Accounts::Account*>(sender());
162 uint accountId = account->id();
163 account->deleteLater();
164
165 QHash<Accounts::Account*,PendingWrite>::iterator i =
166 m_pendingWrites.find(account);
167 if (Q_LIKELY(i != m_pendingWrites.end())) {
168 PendingWrite &w = i.value();
169 w.connection.send(w.message.createReply(accountId));
170 m_pendingWrites.erase(i);
171 }
172 }
173
174 void LibaccountsServicePrivate::onAccountError(Accounts::Error error)
175 {
176 Accounts::Account *account = qobject_cast<Accounts::Account*>(sender());
177 account->deleteLater();
178
179 QHash<Accounts::Account*,PendingWrite>::iterator i =
180 m_pendingWrites.find(account);
181 if (Q_LIKELY(i != m_pendingWrites.end())) {
182 PendingWrite &w = i.value();
183 QDBusMessage reply =
184 w.message.createErrorReply(QDBusError::InternalError,
185 error.message());
186 w.connection.send(reply);
187 m_pendingWrites.erase(i);
188 }
189 }
190
191 LibaccountsService::LibaccountsService(QObject *parent):
192 QObject(parent),
193 d_ptr(new LibaccountsServicePrivate(this))
194 {
195 }
196
197 LibaccountsService::~LibaccountsService()
198 {
199 delete d_ptr;
200 }
201
202 void LibaccountsService::store(const QDBusMessage &msg)
203 {
204 Q_D(LibaccountsService);
205
206 DEBUG() << "Got request:" << msg;
207
208 /* The following line tells QtDBus not to generate a reply now */
209 setDelayedReply(true);
210
211 AccountChanges changes;
212
213 // signature: "ubbsa(ssua{sv}as)"
214 QList<QVariant> args = msg.arguments();
215 int n = 0;
216 changes.accountId = args.value(n++).toUInt();
217 changes.created = args.value(n++).toBool();
218 changes.deleted = args.value(n++).toBool();
219 changes.provider = args.value(n++).toString();
220
221 /* before continuing demarshalling the arguments, check if the provider ID
222 * matches the apparmor label of the peer; if it doesn't, we shouldn't
223 * honour this request. */
224 QString profile = apparmorProfileOfPeer(msg);
225 if (stripVersion(profile) != changes.provider) {
226 DEBUG() << "Declining AccountManager store request to" << profile <<
227 "for provider" << changes.provider;
228 QDBusMessage reply = msg.createErrorReply(QDBusError::AccessDenied,
229 "Profile/provider mismatch");
230 connection().send(reply);
231 return;
232 }
233
234 const QDBusArgument dbusChanges = args.value(n++).value<QDBusArgument>();
235 dbusChanges.beginArray();
236 while (!dbusChanges.atEnd()) {
237 ServiceChanges sc;
238 dbusChanges.beginStructure();
239 dbusChanges >> sc.service;
240 dbusChanges >> sc.serviceType;
241 dbusChanges >> sc.serviceId;
242 dbusChanges >> sc.settings;
243 dbusChanges >> sc.removedKeys;
244 dbusChanges.endStructure();
245
246 changes.serviceChanges.append(sc);
247 }
248 dbusChanges.endArray();
249
250 d->writeChanges(changes);
251 }
252
253 #include "libaccounts-service.moc"