-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsammodel.cpp
More file actions
359 lines (267 loc) · 8.07 KB
/
Copy pathsammodel.cpp
File metadata and controls
359 lines (267 loc) · 8.07 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
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
#include "global.h"
#include "sammodel.h"
CSAMGroupsModel::CSAMGroupsModel(QObject *parent)
: QAbstractItemModel(parent)
{
}
CSAMGroupsModel::~CSAMGroupsModel() = default;
void CSAMGroupsModel::keyChanged(const QModelIndex &key)
{
// Close old key
if (hive_num >= 0) {
if (groups_count > 0) {
beginRemoveRows(QModelIndex(), 0, groups_count - 1);
endRemoveRows();
}
hive_num = -1;
groups_count = 0;
}
// Exit if no valid key passed
if (!key.isValid()) return;
// Read new SAM
struct nk_key *ck = nullptr;
struct hive *h = nullptr;
if (!cgl->reg->keyPrepare(key.internalPointer(), h, hive_num, ck)) {
hive_num = -1;
groups_count = 0;
return;
}
if (h->type != HTYPE_SAM) {
hive_num = -1;
groups_count = 0;
return;
}
groups_count = cgl->reg->listGroups(h).count();
if (groups_count > 0) {
beginInsertRows(QModelIndex(), 0, groups_count - 1);
endInsertRows();
}
Q_EMIT valuesReloaded();
}
inline quintptr gid2id(quint16 gid, qint16 member_idx)
{
return (member_idx << 16) + gid;
}
inline void id2gid(quintptr id, quint16 &gid, qint16 &member_idx)
{
gid = id & 0x0ffff;
member_idx = (id >> 16) & 0x0ffff;
}
QModelIndex CSAMGroupsModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent) || hive_num < 0)
return QModelIndex();
struct hive *h = cgl->reg->getHivePtr(hive_num);
const QList<CGroup> grps = cgl->reg->listGroups(h);
if (!parent.isValid()) { // top-level - group names
if (row < 0 || row >= grps.count())
return QModelIndex();
return createIndex(row, column, gid2id(grps.at(row).grpid, -1));
}
qint16 mid = 0;
quint16 gid = 0;
id2gid(parent.internalId(), gid, mid);
const int idx = grps.indexOf(CGroup(gid));
if (idx >= 0) {
if (row >= 0 && row < grps.at(idx).members.count())
return createIndex(row, column, gid2id(gid, row));
}
return QModelIndex();
}
QModelIndex CSAMGroupsModel::parent(const QModelIndex &child) const
{
if (!child.isValid() || hive_num < 0)
return QModelIndex();
struct hive *h = cgl->reg->getHivePtr(hive_num);
const QList<CGroup> grps = cgl->reg->listGroups(h);
qint16 mid = 0;
quint16 gid = 0;
id2gid(child.internalId(), gid, mid);
if (mid < 0) // child is top-level item, group
return QModelIndex();
// child is username, return index of group and group's position
const int idx = grps.indexOf(CGroup(gid));
if (idx >= 0)
return createIndex(idx, 0, gid2id(gid, -1));
return QModelIndex();
}
int CSAMGroupsModel::rowCount(const QModelIndex &parent) const
{
if (hive_num < 0 || parent.column() > 0)
return 0;
struct hive *h = cgl->reg->getHivePtr(hive_num);
const QList<CGroup> grps = cgl->reg->listGroups(h);
if (!parent.isValid())
return grps.count();
qint16 mid = 0;
quint16 gid = 0;
id2gid(parent.internalId(), gid, mid);
const int idx = grps.indexOf(CGroup(gid));
if (idx >= 0 && mid < 0)
return grps.at(idx).members.count();
return 0;
}
int CSAMGroupsModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return 1;
}
QVariant CSAMGroupsModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || hive_num < 0)
return QVariant();
struct hive *h = cgl->reg->getHivePtr(hive_num);
const QList<CGroup> grps = cgl->reg->listGroups(h);
qint16 mid = 0;
quint16 gid = 0;
id2gid(index.internalId(), gid, mid);
const int idx = grps.indexOf(CGroup(gid));
if (role == Qt::DisplayRole) {
if (idx >= 0) {
if (mid >= 0) { // username
const CGroupMember m = grps.at(idx).members.at(mid);
return QString("(0x%1) %2 (SID: %3)")
.arg(static_cast<quint16>(m.rid), 3, 16, QChar('0'))
.arg(m.name, m.sid);
}
// group
return QString("(0x%1) %2")
.arg(static_cast<quint16>(grps.at(idx).grpid), 3, 16, QChar('0'))
.arg(grps.at(idx).name);
}
} else if (role == Qt::StatusTipRole) {
if (idx >= 0 && mid < 0)
return grps.at(idx).fullname;
} else if (role == Qt::DecorationRole && idx >= 0) {
if (mid >= 0)
return QIcon(QSL(":/icons/user-properties"));
return QIcon(QSL(":/icons/user-group-properties"));
}
return QVariant();
}
Qt::ItemFlags CSAMGroupsModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return QAbstractItemModel::flags(index);
}
CSAMUsersModel::CSAMUsersModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
CSAMUsersModel::~CSAMUsersModel() = default;
void CSAMUsersModel::keyChanged(const QModelIndex &key)
{
// Close old key
if (hive_num >= 0) {
if (val_count > 0) {
beginRemoveRows(QModelIndex(), 0, val_count - 1);
endRemoveRows();
}
hive_num = -1;
val_count = 0;
}
// Exit if no valid key passed
if (!key.isValid()) return;
// Read new SAM
struct nk_key *ck = nullptr;
struct hive *h = nullptr;
if (!cgl->reg->keyPrepare(key.internalPointer(), h, hive_num, ck)) {
hive_num = -1;
val_count = 0;
return;
}
if (h->type != HTYPE_SAM) {
hive_num = -1;
val_count = 0;
return;
}
val_count = cgl->reg->listUsers(h).count();
if (val_count > 0) {
beginInsertRows(QModelIndex(), 0, val_count - 1);
endInsertRows();
}
Q_EMIT valuesReloaded();
}
int CSAMUsersModel::getUserRID(const QModelIndex &index) const
{
if (!index.isValid() || hive_num < 0) return -1;
struct hive *h = cgl->reg->getHivePtr(hive_num);
const QList<CUser> ul = cgl->reg->listUsers(h);
const int row = index.row();
if (row < 0 || row >= ul.count()) return -1;
return ul.at(row).rid;
}
int CSAMUsersModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return val_count;
}
int CSAMUsersModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return 6;
}
QString bool2string(bool val)
{
if (val)
return QSL("+");
return QString();
}
QVariant CSAMUsersModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || hive_num < 0)
return QVariant();
struct hive *h = cgl->reg->getHivePtr(hive_num);
const QList<CUser> ul = cgl->reg->listUsers(h);
const int row = index.row();
const int col = index.column();
if (row < 0 || row >= ul.count()) return QVariant();
const CUser &v = ul.at(row);
if (role == Qt::DisplayRole) {
switch (col) {
case 0: // rid
return QSL("0x%1").arg(static_cast<quint16>(v.rid), 0, 16, QChar('0'));
case 1: // username
return v.username;
case 2: // admin?
return bool2string(v.is_admin);
case 3: // locked?
return bool2string(v.is_locked);
case 4: // no password?
return bool2string(v.is_blank_pw);
case 5: // full name
return v.fullname;
default:
break;
}
}
return QVariant();
}
Qt::ItemFlags CSAMUsersModel::flags(const QModelIndex &index) const
{
Q_UNUSED(index)
return (Qt::ItemIsSelectable | Qt::ItemIsEnabled);
}
QVariant CSAMUsersModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
switch (section) {
case 0:
return tr("RID");
case 1:
return tr("Username");
case 2:
return tr("Admin?");
case 3:
return tr("Locked?");
case 4:
return tr("No password?");
case 5:
return tr("Full name");
default:
return QVariant();
}
}
return QVariant();
}