1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
/*
* Copyright (C) 2014 Canonical Ltd.
*
* Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
*
* This file is part of online-accounts-ui
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Accounts/Manager>
#include <QCoreApplication>
#include <QDateTime>
#include <QDebug>
#include <QDir>
#include <QDomDocument>
#include <QDomElement>
#include <QFile>
#include <QFileInfo>
#include <QStandardPaths>
#include <QStringList>
#include <click.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <utime.h>
static QString findPackageDir(const QString &appId)
{
/* For testing */
QByteArray packageDirEnv = qgetenv("OAH_CLICK_DIR");
if (!packageDirEnv.isEmpty()) {
return QString::fromUtf8(packageDirEnv);
}
QStringList components = appId.split('_');
QByteArray package = components.first().toUtf8();
GError *error = NULL;
ClickUser *user = click_user_new_for_user(NULL, NULL, &error);
if (Q_UNLIKELY(!user)) {
qWarning() << "Unable to read Click database:" << error->message;
g_error_free(error);
return QString();
}
gchar *pkgDir = click_user_get_path(user, package.constData(), &error);
if (Q_UNLIKELY(!pkgDir)) {
qWarning() << "Unable to get the Click package directory for" <<
package << ":" << error->message;
g_error_free(error);
g_object_unref(user);
return QString();
}
QString ret = QString::fromUtf8(pkgDir);
g_object_unref(user);
g_free(pkgDir);
return ret;
}
static QString stripVersion(const QString &appId)
{
QStringList components = appId.split('_');
if (components.count() != 3) return appId;
/* Click packages have a profile of the form
* $name_$application_$version
* (see https://wiki.ubuntu.com/SecurityTeam/Specifications/ApplicationConfinement/Manifest#Click)
*
* So we just need to strip out the last part.
*/
components.removeLast();
return components.join('_');
}
/* Get the modification time of a file; this differs from
* QFileInfo::lastModified() in that if the file is a symlink here we take the
* info from the symlink itself. */
static QDateTime lastModified(const QFileInfo &fileInfo)
{
struct stat data;
if (lstat(fileInfo.filePath().toUtf8().constData(), &data) < 0) {
return QDateTime();
}
return QDateTime::fromTime_t(data.st_mtime);
}
class LibAccountsFile: public QDomDocument {
public:
LibAccountsFile(const QFileInfo &hookFileInfo);
void checkId(const QString &shortAppId);
void addProfile(const QString &appId);
void addPackageDir(const QString &appId);
QString profile() const;
void addDesktopFile(const QString &appId);
void addServiceType(const QString &shortAppId);
void checkIconPath(const QString &appId);
bool writeTo(const QString &fileName) const;
void addCreatorMark();
bool createdByUs() const;
bool isValid() const { return m_isValid; }
private:
QFileInfo m_hookFileInfo;
bool m_isValid;
};
LibAccountsFile::LibAccountsFile(const QFileInfo &hookFileInfo):
QDomDocument(),
m_hookFileInfo(hookFileInfo),
m_isValid(false)
{
QFile file(hookFileInfo.filePath());
if (file.open(QIODevice::ReadOnly)) {
if (setContent(&file)) {
m_isValid = true;
}
file.close();
}
}
void LibAccountsFile::checkId(const QString &shortAppId)
{
/* checks that the root element's "id" attributes is consistent with the
* file name */
QDomElement root = documentElement();
root.setAttribute(QStringLiteral("id"), shortAppId);
}
void LibAccountsFile::addProfile(const QString &appId)
{
QDomElement root = documentElement();
QDomElement elem = createElement(QStringLiteral("profile"));
elem.appendChild(createTextNode(appId));
root.appendChild(elem);
}
void LibAccountsFile::addPackageDir(const QString &appId)
{
QString packageDir = findPackageDir(appId);
if (Q_UNLIKELY(packageDir.isEmpty())) return;
QDomElement root = documentElement();
QDomElement elem = createElement(QStringLiteral("package-dir"));
elem.appendChild(createTextNode(packageDir));
root.appendChild(elem);
}
QString LibAccountsFile::profile() const
{
QDomElement root = documentElement();
return root.firstChildElement("profile").text();
}
void LibAccountsFile::addDesktopFile(const QString &appId)
{
QString desktopEntryTag = QStringLiteral("desktop-entry");
QDomElement root = documentElement();
/* if a <desktop-entry> element already exists, don't touch it */
QDomElement elem = root.firstChildElement(desktopEntryTag);
if (!elem.isNull()) return;
elem = createElement(desktopEntryTag);
elem.appendChild(createTextNode(appId));
root.appendChild(elem);
}
void LibAccountsFile::addServiceType(const QString &shortAppId)
{
QString serviceTypeTag = QStringLiteral("type");
QDomElement root = documentElement();
/* if a <service-type> element already exists, don't touch it */
QDomElement elem = root.firstChildElement(serviceTypeTag);
if (!elem.isNull()) return;
elem = createElement(serviceTypeTag);
elem.appendChild(createTextNode(shortAppId));
root.appendChild(elem);
}
void LibAccountsFile::checkIconPath(const QString &appId)
{
QString iconTag = QStringLiteral("icon");
QDomElement root = documentElement();
/* if the <icon> element does not exist, do nothing*/
QDomElement elem = root.firstChildElement(iconTag);
if (elem.isNull()) return;
/* If the icon path is absolute, do nothing */
QString icon = elem.text();
if (QDir::isAbsolutePath(icon)) return;
/* Otherwise, try appending it to the click package install dir */
QString packageDir = findPackageDir(appId);
if (Q_UNLIKELY(packageDir.isEmpty())) return;
QFileInfo iconFile(packageDir + "/" + icon);
if (iconFile.exists()) {
while (elem.hasChildNodes()) {
elem.removeChild(elem.firstChild());
}
elem.appendChild(createTextNode(iconFile.canonicalFilePath()));
}
}
bool LibAccountsFile::writeTo(const QString &fileName) const
{
/* Make sure that the target directory exists */
QDir fileAsDirectory(fileName);
fileAsDirectory.mkpath("..");
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return false;
bool ok = (file.write(toByteArray(2)) > 0);
file.close();
if (ok) {
struct utimbuf sourceTime;
sourceTime.actime = sourceTime.modtime =
lastModified(m_hookFileInfo).toTime_t();
utime(fileName.toUtf8().constData(), &sourceTime);
return true;
} else {
QFile::remove(fileName);
return false;
}
}
void LibAccountsFile::addCreatorMark()
{
QString comment = QString("this file is auto-generated by %1; do not modify").
arg(QCoreApplication::applicationName());
appendChild(createComment(comment));
}
bool LibAccountsFile::createdByUs() const
{
QString creatorMark = QCoreApplication::applicationName() + ";";
for (QDomNode n = firstChild(); !n.isNull(); n = n.nextSibling()) {
if (n.isComment() && n.nodeValue().contains(creatorMark)) {
return true;
}
}
return false;
}
static void removeStaleAccounts(Accounts::Manager *manager,
const QString &providerName)
{
Q_FOREACH(Accounts::AccountId id, manager->accountList()) {
Accounts::Account *account = manager->account(id);
if (account->providerName() == providerName) {
account->remove();
account->syncAndBlock();
}
}
}
static void removeStaleFiles(Accounts::Manager *manager,
const QStringList &fileTypes,
const QString &localShare,
const QDir &hooksDirIn)
{
/* Walk through all of
* ~/.local/share/accounts/{providers,services,service-types,applications}/
* and remove files which are no longer present in hooksDirIn.
*/
Q_FOREACH(const QString &fileType, fileTypes) {
QDir dir(QString("%1/accounts/%2s").arg(localShare).arg(fileType));
dir.setFilter(QDir::Files | QDir::Readable);
QStringList fileTypeFilter;
fileTypeFilter << "*." + fileType;
dir.setNameFilters(fileTypeFilter);
Q_FOREACH(const QFileInfo &fileInfo, dir.entryInfoList()) {
LibAccountsFile file(fileInfo.filePath());
/* If this file was not created by our hook let's ignore it. */
if (!file.createdByUs()) continue;
QString profile = file.profile();
/* Check that the hook file is still there; if it isn't, then it
* means that the click package was removed, and we must remove our
* copy as well. */
QString hookFileName = stripVersion(profile) + "_*." + fileType;
QStringList nameFilters = QStringList() << hookFileName;
if (!hooksDirIn.entryList(nameFilters).isEmpty()) continue;
QFile::remove(fileInfo.filePath());
/* If this is a provider, we must also remove any accounts
* associated with it */
if (fileType == QStringLiteral("provider")) {
removeStaleAccounts(manager, fileInfo.completeBaseName());
}
}
}
}
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
Accounts::Manager::Options managerOptions;
if (qgetenv("DBUS_SESSION_BUS_ADDRESS").isEmpty()) {
managerOptions |= Accounts::Manager::DisableNotifications;
}
Accounts::Manager *manager = new Accounts::Manager(managerOptions);
/* Go through the hook files in ~/.local/share/online-accounts-hooks/ and
* check if they have already been processed into a file under
* ~/.local/share/accounts/{providers,services,service-types,applications}/;
* if not, open the hook file, write the APP_ID somewhere in it, and
* save the result in the location where libaccounts expects to find it.
*/
QStringList fileTypes;
fileTypes << QStringLiteral("provider") <<
QStringLiteral("service") <<
QStringLiteral("service-type") <<
QStringLiteral("application");
// This is ~/.local/share/
const QString localShare =
QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
QDir hooksDirIn(localShare + "/" HOOK_FILES_SUBDIR);
removeStaleFiles(manager, fileTypes, localShare, hooksDirIn);
Q_FOREACH(const QFileInfo &fileInfo, hooksDirIn.entryInfoList()) {
const QString fileType = fileInfo.suffix();
// Filter out the files which we don't support
if (!fileTypes.contains(fileType)) continue;
// Our click hook sets the base name to the APP_ID
QString appId = fileInfo.completeBaseName();
/* When publishing this file for libaccounts, we want to strip
* the version number out. */
QString shortAppId = stripVersion(appId);
/* When building the destination file name, use the file suffix with an
* "s" appended: remember that libaccounts uses
* ~/.local/share/accounts/{providers,services,service-types,applications}.
* */
QString destination = QString("%1/accounts/%2s/%3.%2").
arg(localShare).arg(fileInfo.suffix()).arg(shortAppId);
QFileInfo destinationInfo(destination);
/* If the destination is there and up to date, we have nothing to do */
if (destinationInfo.exists() &&
destinationInfo.lastModified() == lastModified(fileInfo)) {
continue;
}
LibAccountsFile xml = LibAccountsFile(fileInfo);
if (!xml.isValid()) continue;
xml.addCreatorMark();
xml.checkId(shortAppId);
xml.addProfile(appId);
xml.addPackageDir(appId);
xml.checkIconPath(appId);
if (fileType == "application") {
xml.addDesktopFile(appId);
} else if (fileType == "service") {
xml.addServiceType(shortAppId);
}
xml.writeTo(destination);
}
/* To ensure that all the installed services are parsed into
* libaccounts' DB, we enumerate them now.
*/
manager->serviceList();
delete manager;
return EXIT_SUCCESS;
}
|