-
Notifications
You must be signed in to change notification settings - Fork 2
/
auditon.cpp
330 lines (291 loc) · 8 KB
/
auditon.cpp
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
#include <bsm/libbsm.h>
#include <cstdlib>
#include <unistd.h>
#include <string>
#include <unordered_map>
enum class Command {
GETPOLICY,
SETPOLICY,
UNSETPOLICY,
GETMASK,
SETMASK,
UNSETMASK,
GETCLASS,
SETCLASS,
UNSETCLASS
};
static auto usage() {
fprintf(stderr, "usage: auditon getpolicy\n"
" auditon setpolicy <policy>\n"
" auditon unsetpolicy <policy>\n"
" auditon getmask <pid>\n"
" auditon setmask <pid> <event-classes>\n"
" auditon unsetmask <pid> <event-classes>\n"
" auditon getclass <event-name>\n"
" auditon setclass <event-name> <event-class>\n"
" auditon unsetclass <event-name> <event-class>\n");
return EXIT_FAILURE;
}
int main(int argc, char **argv) {
std::unordered_map<std::string, Command> commands{
{"getpolicy", Command::GETPOLICY}, {"setpolicy", Command::SETPOLICY},
{"unsetpolicy", Command::UNSETPOLICY}, {"getmask", Command::GETMASK},
{"setmask", Command::SETMASK}, {"unsetmask", Command::UNSETMASK},
{"getclass", Command::GETCLASS}, {"setclass", Command::SETCLASS},
{"unsetclass", Command::UNSETCLASS},
};
if (argc <= 1) {
return usage();
}
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
return usage();
}
const auto command = commands.find(argv[1]);
if (command == commands.end()) {
fprintf(stderr, "error: unknown command\n");
return usage();
}
if (geteuid() != 0) {
// Re-exec with sudo.
char *cmd[argc + 2];
cmd[0] = "sudo";
for (int i = 0; i < argc; ++i) {
cmd[i + 1] = argv[i];
}
cmd[argc + 1] = nullptr;
execvp("sudo", cmd);
}
switch (command->second) {
case Command::GETPOLICY: {
int policy;
if (audit_get_policy(&policy)) {
perror("error");
return EXIT_FAILURE;
}
// Based on known policy names, 128 should always be enough.
char buf[128];
auto size = au_poltostr(policy, 128, buf);
if (size < 0) {
perror("error");
return EXIT_FAILURE;
}
printf("%.*s\n", (int)size, buf);
break;
}
case Command::SETPOLICY: {
if (argc != 3) {
fprintf(stderr, "usage: auditon setpolicy <policy>\n");
return EXIT_FAILURE;
}
int current_policy;
if (audit_get_policy(¤t_policy)) {
perror("error");
return EXIT_FAILURE;
}
int new_policy;
if (au_strtopol(argv[2], &new_policy)) {
perror("error");
return EXIT_FAILURE;
}
int policy = current_policy | new_policy;
if (audit_set_policy(&policy)) {
perror("error");
return EXIT_FAILURE;
}
break;
}
case Command::UNSETPOLICY: {
if (argc != 3) {
fprintf(stderr, "usage: auditon unsetpolicy <policy>\n");
return EXIT_FAILURE;
}
int current_policy;
if (audit_get_policy(¤t_policy)) {
perror("error");
return EXIT_FAILURE;
}
int new_policy;
if (au_strtopol(argv[2], &new_policy)) {
perror("error");
return EXIT_FAILURE;
}
int policy = current_policy & ~new_policy;
if (audit_set_policy(&policy)) {
perror("error");
return EXIT_FAILURE;
}
break;
}
case Command::GETMASK: {
if (argc != 3) {
fprintf(stderr, "usage: auditon getmask <pid>\n");
return EXIT_FAILURE;
}
auditpinfo_t api;
api.ap_pid = atoi(argv[2]);
if (audit_get_pinfo(&api, sizeof(api))) {
perror("error");
return EXIT_FAILURE;
}
// For buffer sizing, see "BUGS" section of `man au_mask`.
// TODO: I don't think the docs are right.
const size_t bufsize = 128;
char buf[bufsize];
if (getauditflagschar(buf, &api.ap_mask, 0)) {
perror("error");
return EXIT_FAILURE;
}
printf("%s\n", buf);
break;
}
case Command::SETMASK: {
if (argc != 4) {
fprintf(stderr, "usage: auditon setmask <pid> <event-classes>\n");
return EXIT_FAILURE;
}
auditpinfo_t api;
api.ap_pid = atoi(argv[2]);
if (audit_get_pinfo(&api, sizeof(api))) {
perror("error");
return EXIT_FAILURE;
}
auto new_mask = api.ap_mask;
if (getauditflagsbin(argv[3], &new_mask)) {
perror("error");
return EXIT_FAILURE;
}
api.ap_mask.am_success |= new_mask.am_success;
api.ap_mask.am_failure |= new_mask.am_failure;
if (audit_set_pmask(&api, sizeof(api))) {
perror("error");
return EXIT_FAILURE;
}
break;
}
case Command::UNSETMASK: {
if (argc != 4) {
fprintf(stderr, "usage: auditon unsetmask <pid> <event-classes>\n");
return EXIT_FAILURE;
}
auditpinfo_t api;
api.ap_pid = atoi(argv[2]);
if (audit_get_pinfo(&api, sizeof(api))) {
perror("error");
return EXIT_FAILURE;
}
auto new_mask = api.ap_mask;
if (getauditflagsbin(argv[3], &new_mask)) {
perror("error");
return EXIT_FAILURE;
}
api.ap_mask.am_success &= ~new_mask.am_success;
api.ap_mask.am_failure &= ~new_mask.am_failure;
if (audit_set_pmask(&api, sizeof(api))) {
perror("error");
return EXIT_FAILURE;
}
break;
}
case Command::GETCLASS: {
if (argc != 3) {
fprintf(stderr, "usage: auditon getclass <event-name>\n");
return EXIT_FAILURE;
}
au_evclass_map_t evc_map;
const auto event_name = argv[2];
auto event_num = getauevnonam(event_name);
if (not event_num) {
perror("error");
return EXIT_FAILURE;
}
evc_map.ec_number = *event_num;
if (audit_get_class(&evc_map, sizeof(evc_map))) {
perror("error");
return EXIT_FAILURE;
}
// This function starts an iteration over all class_ent records.
setauclass();
bool match = false;
for (auto class_ent = getauclassent(); class_ent != nullptr;
class_ent = getauclassent()) {
auto mask = class_ent->ac_class;
// Only check singular masks, for example not the "all" mask.
if (__builtin_popcount(mask) != 1) {
continue;
}
if (evc_map.ec_class & mask) {
match = true;
printf("%s,", class_ent->ac_name);
}
}
endauclass();
if (not match) {
fprintf(stderr, "error: %s does not belong to any classes\n", event_name);
return EXIT_FAILURE;
}
// This "\b " overwrites the trailing ','
printf("\b \n");
break;
}
case Command::SETCLASS: {
if (argc != 4) {
fprintf(stderr, "usage: auditon setclass <event-name> <event-classes>\n");
return EXIT_FAILURE;
}
au_evclass_map_t evc_map;
auto event_num = getauevnonam(argv[2]);
if (not event_num) {
perror("error");
return EXIT_FAILURE;
}
evc_map.ec_number = *event_num;
if (audit_get_class(&evc_map, sizeof(evc_map))) {
perror("error");
return EXIT_FAILURE;
}
const auto class_name = argv[3];
auto class_ent = getauclassnam(class_name);
if (not class_ent) {
perror("error");
return EXIT_FAILURE;
}
evc_map.ec_class |= class_ent->ac_class;
if (audit_set_class(&evc_map, sizeof(evc_map))) {
perror("error");
return EXIT_FAILURE;
}
break;
}
case Command::UNSETCLASS: {
if (argc != 4) {
fprintf(stderr,
"usage: auditon unsetclass <event-name> <event-classes>\n");
return EXIT_FAILURE;
}
au_evclass_map_t evc_map;
auto event_num = getauevnonam(argv[2]);
if (not event_num) {
perror("error");
return EXIT_FAILURE;
}
evc_map.ec_number = *event_num;
if (audit_get_class(&evc_map, sizeof(evc_map))) {
perror("error");
return EXIT_FAILURE;
}
const auto class_name = argv[3];
auto class_ent = getauclassnam(class_name);
if (not class_ent) {
perror("error");
return EXIT_FAILURE;
}
evc_map.ec_class &= ~class_ent->ac_class;
if (audit_set_class(&evc_map, sizeof(evc_map))) {
perror("error");
return EXIT_FAILURE;
}
break;
}
}
return EXIT_SUCCESS;
}