/
userinfo.cpp
545 lines (490 loc) · 16.3 KB
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
/*
* Copyright (C) 2020 Open Mobile Platform LLC.
*
* You may use this file under the terms of the BSD license as follows:
*
* "Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Nemo Mobile nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/
#include "userinfo.h"
#include "userinfo_p.h"
34
#include "logging_p.h"
36
37
#include <QFile>
#include <QFileSystemWatcher>
38
#include <QSocketNotifier>
39
#include <grp.h>
40
#include <poll.h>
41
42
43
44
45
46
#include <pwd.h>
#include <sys/types.h>
#include <systemd/sd-login.h>
namespace {
47
const auto UserDatabaseFile = QStringLiteral("/etc/passwd");
48
const auto GroupDatabaseFile = QStringLiteral("/etc/group");
50
51
enum SpecialIds : uid_t {
DeviceOwnerId = 100000,
52
UnknownCurrentUserId = (uid_t)(-2),
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
InvalidId = (uid_t)(-1),
};
QString nameFromGecos(const char *gecos)
{
// typically GECOS has (sub)fields separated by ","
// and the first one of them is full name of the user.
// Sometimes it contains just the full name or it might be empty,
// thus do this on best effort basis.
auto name = QString::fromUtf8(gecos);
int i = name.indexOf(QStringLiteral(","));
if (i != -1)
name.truncate(i);
return name;
}
}
UserInfoPrivate::UserInfoPrivate()
: m_uid(InvalidId)
, m_loggedIn(false)
74
, m_watcher(nullptr)
75
, m_alone(Unknown)
76
77
78
79
80
81
82
{
}
UserInfoPrivate::UserInfoPrivate(struct passwd *pwd)
: m_uid(pwd->pw_uid)
, m_username(QString::fromUtf8(pwd->pw_name))
, m_name(nameFromGecos(pwd->pw_gecos))
83
// require_active == true -> only active user is logged in.
84
85
// Specifying seat should make sure that remote users are not
// counted as they don't have seats.
86
, m_loggedIn(sd_uid_is_on_seat(m_uid, 1, "seat0") > 0)
87
, m_watcher(nullptr)
88
, m_alone(Unknown)
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
{
}
UserInfoPrivate::~UserInfoPrivate()
{
}
QWeakPointer<UserInfoPrivate> UserInfoPrivate::s_current;
void UserInfoPrivate::set(struct passwd *pwd)
{
QString username;
QString name;
if (pwd) {
Q_ASSERT(pwd->pw_uid == m_uid);
username = QString::fromUtf8(pwd->pw_name);
name = nameFromGecos(pwd->pw_gecos);
} else if (m_uid != InvalidId) {
m_uid = InvalidId;
emit uidChanged();
}
if (m_username != username) {
m_username = username;
emit usernameChanged();
115
// Username is used as displayName only if name is empty, avoid emitting changed twice
116
117
if (m_name.isEmpty() && name.isEmpty())
emit displayNameChanged();
118
119
120
121
122
}
if (m_name != name) {
m_name = name;
emit nameChanged();
123
emit displayNameChanged();
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
bool UserInfoPrivate::alone()
{
if (m_alone == Unknown)
updateAlone(true);
return m_alone == Yes;
}
void UserInfoPrivate::updateAlone(bool force)
{
if (!force && m_alone == Unknown) {
// Skip if the value is not needed and the check is not forced
return;
}
Tristated alone = Yes;
if (m_uid != InvalidId && m_uid != UnknownCurrentUserId && m_uid != DeviceOwnerId) {
// There must be at least one other user besides device owner
// if the uid is valid and known and it's not device owner
alone = No;
} else {
// Can not determine from uid, check users group
errno = 0;
struct group *grp = getgrnam("users");
if (!grp) {
qCWarning(lcUsersLog) << "Could not read users group:" << strerror(errno);
// Guessing that user is probably alone
} else {
for (int i = 0; grp->gr_mem[i] != nullptr; ++i) {
struct passwd *pwd = getpwnam(grp->gr_mem[i]);
if (pwd && pwd->pw_uid != DeviceOwnerId) {
// Found someone that's not device owner
alone = No;
break;
}
// pwd must not be free'd
}
// grp must not be free'd
}
}
if (m_alone != alone) {
m_alone = alone;
if (!force) {
// Emit only if something needed the value already, i.e. it was known
emit aloneChanged();
}
}
}
177
178
179
180
/**
* Construct UserInfo for the current user
*
* If it has been constructed before, this reuses the old data.
181
182
183
184
* If it can not determine the current user, then it constructs
* an object that doesn't point to any user until a user becomes
* active on seat0. That should happen very soon after user
* session has been started.
185
186
187
*/
UserInfo::UserInfo()
{
188
d_ptr = UserInfoPrivate::s_current.toStrongRef();
189
190
191
if (d_ptr.isNull()) {
uid_t uid = InvalidId;
struct passwd *pwd;
192
193
194
195
196
197
198
if (sd_seat_get_active("seat0", NULL, &uid) >= 0 && uid != InvalidId) {
if ((pwd = getpwuid(uid))) {
d_ptr = QSharedPointer<UserInfoPrivate>(new UserInfoPrivate(pwd));
} else {
// User did not exist, should not happen
d_ptr = QSharedPointer<UserInfoPrivate>(new UserInfoPrivate);
}
200
201
202
203
// User is not active yet
d_ptr = QSharedPointer<UserInfoPrivate>(new UserInfoPrivate);
d_ptr->m_uid = UnknownCurrentUserId;
waitForActivation();
204
205
}
// pwd must not be free'd
206
207
if (current())
UserInfoPrivate::s_current = d_ptr;
208
209
}
connectSignals();
210
watchForChanges();
211
212
213
214
215
216
217
218
219
220
221
222
223
224
}
UserInfo::UserInfo(const UserInfo &other)
: QObject(other.parent())
, d_ptr(other.d_ptr)
{
connectSignals();
}
/**
* Construct UserInfo by uid
*/
UserInfo::UserInfo(int uid)
{
225
226
227
auto current_d = UserInfoPrivate::s_current.toStrongRef();
if (!current_d.isNull() && current_d->m_uid == (uid_t)uid) {
d_ptr = current_d;
229
230
231
232
233
234
235
236
237
struct passwd *pwd = (uid_t)uid != InvalidId ? getpwuid((uid_t)uid) : nullptr;
if (pwd) {
d_ptr = QSharedPointer<UserInfoPrivate>(new UserInfoPrivate(pwd));
} else {
d_ptr = QSharedPointer<UserInfoPrivate>(new UserInfoPrivate);
}
// pwd must not be free'd
if (current())
UserInfoPrivate::s_current = d_ptr;
238
239
240
241
242
243
244
245
246
}
connectSignals();
}
/**
* Construct UserInfo by username
*/
UserInfo::UserInfo(QString username)
{
247
248
249
auto current_d = UserInfoPrivate::s_current.toStrongRef();
if (!current_d.isNull() && current_d->m_username == username) {
d_ptr = current_d;
251
252
253
254
255
256
257
258
259
struct passwd *pwd = getpwnam(username.toUtf8().constData());
if (pwd) {
d_ptr = QSharedPointer<UserInfoPrivate>(new UserInfoPrivate(pwd));
} else {
d_ptr = QSharedPointer<UserInfoPrivate>(new UserInfoPrivate);
}
// pwd must not be free'd
if (current())
UserInfoPrivate::s_current = d_ptr;
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
}
connectSignals();
}
/**
* Construct a placeholder user that doesn't exist
*
* Placeholder users are always invalid.
*/
UserInfo UserInfo::placeholder()
{
return UserInfo(InvalidId);
}
UserInfo::~UserInfo()
{
}
/**
279
* Returns true if the user exists
280
281
282
283
*/
bool UserInfo::isValid() const
{
Q_D(const UserInfo);
284
return d->m_uid != InvalidId && d->m_uid != UnknownCurrentUserId;
287
288
289
290
291
292
293
294
295
296
297
298
299
300
QString UserInfo::displayName() const
{
Q_D(const UserInfo);
if (d->m_name.isEmpty()) {
if (type() == DeviceOwner) {
//: Default value for device owner's name when it is not set
//% "Device owner"
return qtTrId("systemsettings-li-device_owner");
}
return d->m_username;
}
return d->m_name;
}
301
302
303
304
305
306
307
308
309
310
311
312
QString UserInfo::username() const
{
Q_D(const UserInfo);
return d->m_username;
}
void UserInfo::setUsername(QString username)
{
Q_D(UserInfo);
if (d->m_username != username) {
d->m_username = username;
emit d_ptr->usernameChanged();
313
314
if (d->m_name.isEmpty())
emit d_ptr->displayNameChanged();
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
}
}
QString UserInfo::name() const
{
Q_D(const UserInfo);
return d->m_name;
}
void UserInfo::setName(QString name)
{
Q_D(UserInfo);
if (d->m_name != name) {
d->m_name = name;
emit d_ptr->nameChanged();
330
emit d_ptr->displayNameChanged();
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
}
}
UserInfo::UserType UserInfo::type() const
{
Q_D(const UserInfo);
// Device lock considers user with id 100000 as device owner.
// Some other places consider the user belonging to sailfish-system
// as device owner. We have to pick one here.
return (d->m_uid == DeviceOwnerId) ? DeviceOwner : User;
}
int UserInfo::uid() const
{
Q_D(const UserInfo);
return (int)d->m_uid;
}
349
350
351
352
353
354
355
void UserInfo::setUid(int uid)
{
Q_D(const UserInfo);
if ((uid_t)uid != d->m_uid)
replace(UserInfo(uid).d_ptr);
}
356
357
358
359
360
361
362
363
364
365
366
/**
* Returs true if user is logged in on seat0 and is the active user, i.e. the current user
*/
bool UserInfo::current() const
{
Q_D(const UserInfo);
// Any logged in user (on seat0) must be the current one
// since we don't have multisession.
return d->m_loggedIn;
}
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
bool UserInfo::updateCurrent()
{
Q_D(UserInfo);
bool previous = d->m_loggedIn;
d->m_loggedIn = sd_uid_is_on_seat(d->m_uid, 1, "seat0") > 0;
if (d->m_loggedIn != previous) {
if (d->m_loggedIn)
UserInfoPrivate::s_current = d_ptr;
else if (UserInfoPrivate::s_current == d_ptr)
UserInfoPrivate::s_current.clear();
emit d_ptr->currentChanged();
return true;
}
return false;
}
383
384
385
386
387
388
389
390
391
392
393
394
/**
* Returns true if there is only one user on the device
*/
bool UserInfo::alone()
{
Q_D(UserInfo);
return d->alone();
}
/**
* Resets object reloading all information
*/
395
396
397
398
void UserInfo::reset()
{
Q_D(UserInfo);
d->set((isValid()) ? getpwuid(d->m_uid) : nullptr);
399
updateCurrent();
400
d->updateAlone();
403
404
405
void UserInfo::replace(QSharedPointer<UserInfoPrivate> other)
{
auto old = d_ptr;
406
disconnect(old.data(), 0, this, 0);
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
d_ptr = other;
if (old->m_username != d_ptr->m_username) {
emit usernameChanged();
// Username is used as displayName only if name is empty, avoid emitting changed twice
if (old->m_name.isEmpty() && d_ptr->m_name.isEmpty())
emit displayNameChanged();
}
if (old->m_name != d_ptr->m_name) {
emit nameChanged();
emit displayNameChanged();
}
if (old->m_uid != d_ptr->m_uid)
emit uidChanged();
if (old->m_loggedIn != d_ptr->m_loggedIn)
emit currentChanged();
426
427
428
429
if (old->m_watcher)
watchForChanges();
430
431
432
433
// If alone value was known, ensure that new d_ptr also knows it
if (old->m_alone != UserInfoPrivate::Unknown && old->alone() != d_ptr->alone())
emit aloneChanged();
434
connectSignals();
437
438
439
440
441
UserInfo &UserInfo::operator=(const UserInfo &other)
{
if (this == &other)
return *this;
442
replace(other.d_ptr);
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
return *this;
}
bool UserInfo::operator==(const UserInfo &other) const
{
if (!isValid())
return false;
return d_ptr == other.d_ptr;
}
bool UserInfo::operator!=(const UserInfo &other) const
{
if (!isValid())
return true;
return d_ptr != other.d_ptr;
}
void UserInfo::connectSignals()
{
463
connect(d_ptr.data(), &UserInfoPrivate::displayNameChanged, this, &UserInfo::displayNameChanged);
464
465
466
connect(d_ptr.data(), &UserInfoPrivate::usernameChanged, this, &UserInfo::usernameChanged);
connect(d_ptr.data(), &UserInfoPrivate::nameChanged, this, &UserInfo::nameChanged);
connect(d_ptr.data(), &UserInfoPrivate::uidChanged, this, &UserInfo::uidChanged);
467
connect(d_ptr.data(), &UserInfoPrivate::currentChanged, this, &UserInfo::currentChanged);
468
connect(d_ptr.data(), &UserInfoPrivate::aloneChanged, this, &UserInfo::aloneChanged);
471
472
473
474
void UserInfo::watchForChanges()
{
Q_D(UserInfo);
d->m_watcher = new QFileSystemWatcher(this);
475
476
477
QStringList missing = d->m_watcher->addPaths(QStringList() << UserDatabaseFile << GroupDatabaseFile);
if (missing.count() == 2) {
qCWarning(lcUsersLog) << "Could not watch for changes in user or group database";
478
479
delete d->m_watcher;
d->m_watcher = nullptr;
480
481
} else if (missing.count() > 0) {
qCWarning(lcUsersLog) << "Could not watch for changes in" << missing;
483
connect(d->m_watcher, &QFileSystemWatcher::fileChanged, this, [this] (const QString &path) {
484
Q_D(UserInfo);
485
486
487
488
489
490
491
492
493
494
if (QFile::exists(path)) {
if (path == UserDatabaseFile) {
// User database updated, reset model
qCDebug(lcUsersLog) << UserDatabaseFile << "changed, reseting model";
reset();
} else if (d->m_alone != UserInfoPrivate::Unknown) { // && path == GroupDatabaseFile
// Group database updated, update alone status
qCDebug(lcUsersLog) << GroupDatabaseFile << "changed, checking alone status again";
d->updateAlone();
}
496
497
498
if (!d->m_watcher->files().contains(path)) {
if (QFile::exists(path) && d->m_watcher->addPath(path)) {
qCDebug(lcUsersLog) << "Re-watching" << path << "for changes";
500
qCWarning(lcUsersLog) << "Stopped watching" << path << "for changes";
501
502
503
504
505
506
}
}
});
}
}
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
void UserInfo::waitForActivation()
{
// Monitor systemd-logind for changes on seats
sd_login_monitor *monitor;
if (sd_login_monitor_new("seat", &monitor) < 0) {
qCWarning(lcUsersLog) << "Could not start monitoring seat changes";
} else {
int fd = sd_login_monitor_get_fd(monitor);
if (fd < 0) {
qCWarning(lcUsersLog) << "Could not get file descriptor, not monitoring seat changes";
sd_login_monitor_unref(monitor);
} else if (!(sd_login_monitor_get_events(monitor) & POLLIN)) {
// Should not happen
qCWarning(lcUsersLog) << "Wrong events bits, not monitoring seat changes";
sd_login_monitor_unref(monitor);
} else {
auto *notifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
connect(notifier, &QSocketNotifier::activated, this, [this, notifier, monitor](int socket) {
Q_UNUSED(socket)
// Check if seat0 has got active user
uid_t uid = InvalidId;
if (sd_seat_get_active("seat0", NULL, &uid) >= 0 && uid != InvalidId) {
qCDebug(lcUsersLog) << "User activated on seat0";
replace(UserInfo().d_ptr);
notifier->deleteLater();
// Otherwise it was not the event we are waiting for, just flush
} else if (sd_login_monitor_flush(monitor) < 0) {
qCWarning(lcUsersLog) << "Monitor flush failed";
notifier->deleteLater();
}
});
connect(notifier, &QObject::destroyed, [monitor] {
qCDebug(lcUsersLog) << "Stopped monitoring seat changes";
sd_login_monitor_unref(monitor);
});
qCDebug(lcUsersLog) << "Started monitoring seat changes";
}
}
}