|
1 | 1 | #include "identity.hpp" |
| 2 | +#include <type_traits> |
2 | 3 | #include <utility> |
| 4 | +#include <vector> |
3 | 5 |
|
4 | 6 | #include <qobject.h> |
5 | 7 | #include <qtmetamacros.h> |
| 8 | +#include <qtypes.h> |
6 | 9 | #include <sys/types.h> |
7 | 10 |
|
8 | 11 | #define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE |
|
12 | 15 | #define signals Q_SIGNALS |
13 | 16 | #include <grp.h> |
14 | 17 | #include <pwd.h> |
| 18 | +#include <unistd.h> |
| 19 | + |
| 20 | +#include "gobjectref.hpp" |
15 | 21 |
|
16 | 22 | namespace qs::service::polkit { |
17 | 23 | Identity::Identity( |
18 | 24 | id_t id, |
19 | 25 | QString name, |
20 | 26 | QString displayName, |
21 | 27 | bool isGroup, |
22 | | - PolkitIdentity* polkitIdentity, |
| 28 | + GObjectRef<PolkitIdentity> polkitIdentity, |
23 | 29 | QObject* parent |
24 | 30 | ) |
25 | 31 | : QObject(parent) |
26 | | - , polkitIdentity(polkitIdentity) |
| 32 | + , polkitIdentity(std::move(polkitIdentity)) |
27 | 33 | , mId(id) |
28 | 34 | , mName(std::move(name)) |
29 | 35 | , mDisplayName(std::move(displayName)) |
30 | 36 | , mIsGroup(isGroup) {} |
31 | 37 |
|
32 | 38 | Identity::~Identity() = default; |
33 | 39 |
|
34 | | -Identity* Identity::fromPolkitIdentity(PolkitIdentity* identity) { |
35 | | - if (POLKIT_IS_UNIX_USER(identity)) { |
36 | | - auto uid = polkit_unix_user_get_uid(POLKIT_UNIX_USER(identity)); |
37 | | - auto* pw = getpwuid(uid); |
| 40 | +Identity* Identity::fromPolkitIdentity(GObjectRef<PolkitIdentity> identity) { |
| 41 | + if (POLKIT_IS_UNIX_USER(identity.get())) { |
| 42 | + auto uid = polkit_unix_user_get_uid(POLKIT_UNIX_USER(identity.get())); |
| 43 | + |
| 44 | + auto bufSize = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 45 | + // The call can fail with -1, in this case choose a default that is |
| 46 | + // big enough. |
| 47 | + if (bufSize == -1) bufSize = 16384; |
| 48 | + auto buffer = std::vector<char>(bufSize); |
| 49 | + |
| 50 | + std::aligned_storage_t<sizeof(passwd), alignof(passwd)> pwBuf; |
| 51 | + passwd* pw = nullptr; |
| 52 | + getpwuid_r(uid, reinterpret_cast<passwd*>(&pwBuf), buffer.data(), bufSize, &pw); |
| 53 | + |
38 | 54 | auto name = |
39 | 55 | (pw && pw->pw_name && *pw->pw_name) ? QString::fromUtf8(pw->pw_name) : QString::number(uid); |
| 56 | + |
40 | 57 | return new Identity( |
41 | 58 | uid, |
42 | 59 | name, |
43 | 60 | (pw && pw->pw_gecos && *pw->pw_gecos) ? QString::fromUtf8(pw->pw_gecos) : name, |
44 | 61 | false, |
45 | | - identity |
| 62 | + std::move(identity) |
46 | 63 | ); |
47 | 64 | } |
48 | 65 |
|
49 | | - if (POLKIT_IS_UNIX_GROUP(identity)) { |
50 | | - auto gid = polkit_unix_group_get_gid(POLKIT_UNIX_GROUP(identity)); |
51 | | - auto* gr = getgrgid(gid); |
| 66 | + if (POLKIT_IS_UNIX_GROUP(identity.get())) { |
| 67 | + auto gid = polkit_unix_group_get_gid(POLKIT_UNIX_GROUP(identity.get())); |
| 68 | + |
| 69 | + auto bufSize = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 70 | + // The call can fail with -1, in this case choose a default that is |
| 71 | + // big enough. |
| 72 | + if (bufSize == -1) bufSize = 16384; |
| 73 | + auto buffer = std::vector<char>(bufSize); |
| 74 | + |
| 75 | + std::aligned_storage_t<sizeof(group), alignof(group)> grBuf; |
| 76 | + group* gr = nullptr; |
| 77 | + getgrgid_r(gid, reinterpret_cast<group*>(&grBuf), buffer.data(), bufSize, &gr); |
| 78 | + |
52 | 79 | auto name = |
53 | 80 | (gr && gr->gr_name && *gr->gr_name) ? QString::fromUtf8(gr->gr_name) : QString::number(gid); |
54 | | - return new Identity(gid, name, name, true, identity); |
| 81 | + return new Identity(gid, name, name, true, std::move(identity)); |
55 | 82 | } |
56 | 83 |
|
57 | 84 | // A different type of identity is netgroup. |
58 | 85 | return nullptr; |
59 | 86 | } |
60 | 87 |
|
61 | | -id_t Identity::id() const { return this->mId; } |
| 88 | +quint32 Identity::id() const { return this->mId; } |
62 | 89 | const QString& Identity::name() const { return this->mName; } |
63 | 90 | const QString& Identity::displayName() const { return this->mDisplayName; } |
64 | 91 | bool Identity::isGroup() const { return this->mIsGroup; } |
|
0 commit comments