Skip to content

Commit ca6b797

Browse files
committed
ausearch: add option to exclude by message type
Similar to the existing option `-m`, which searches by the given message type(s), add the option `-M` to exclude by the given message type(s).
1 parent 17af844 commit ca6b797

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

Diff for: docs/ausearch.8

+3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ Flush output on every line. Most useful when stdout is connected to a pipe and t
112112
.BR \-m ,\ \-\-message \ \fImessage-type\fP\ |\ \fIcomma-sep-message-type-list\fP
113113
Search for an event matching the given \fImessage type\fP. (Message types are also known as record types.) You may also enter a \fIcomma separated list of message types\fP or multiple individual message types each with its own \fI-m\fP option. There is an \fBALL\fP message type that doesn't exist in the actual logs. It allows you to get all messages in the system. The list of valid messages types is long. The program will display the list whenever no message type is passed with this parameter. The message type can be either text or numeric. If you enter a list, there can be only commas and no spaces separating the list.
114114
.TP
115+
.BR \-M ,\ \-\-message-exclude \ \fImessage-type\fP\ |\ \fIcomma-sep-message-type-list\fP
116+
Filter out events matching the given \fImessage type\fP. (Message types are also known as record types.) You may also enter a \fIcomma separated list of message types\fP or multiple individual message types each with its own \fI-m\fP option. There is an \fBALL\fP message type that doesn't exist in the actual logs. It allows you to exclude all messages in the system. The list of valid messages types is long. The program will display the list whenever no message type is passed with this parameter. The message type can be either text or numeric. If you enter a list, there can be only commas and no spaces separating the list. This option is mutual exclusive with the option \fB\-m\fP.
117+
.TP
115118
.BR \-n ,\ \-\-node
116119
Search for events originating from a specific machine. Multiple nodes are allowed, and if any nodes match, the event is matched. This search uses the node field in audit events. Also see the \-\-host command which search for events related to host information in the audit trail.
117120
.TP

Diff for: src/ausearch-match.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ int match(llist *l)
144144
if (found)
145145
break;
146146
} while ((n = list_next(l)));
147-
if (!found)
147+
if (!(found ^ event_type_inverted))
148148
return 0;
149149
}
150150

Diff for: src/ausearch-options.c

+17-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ report_t report_format = RPT_DEFAULT;
5050
unsigned int event_id = -1;
5151
gid_t event_gid = -1, event_egid = -1;
5252
ilist *event_type = NULL;
53+
int event_type_inverted = -1;
5354
pid_t event_pid = -1, event_ppid = -1;
5455
success_t event_success = S_UNSET;
5556
auparse_esc_t escape_mode = AUPARSE_ESC_TTY;
@@ -93,7 +94,8 @@ S_TIME_END, S_TIME_START, S_TERMINAL, S_ALL_UID, S_EFF_UID, S_UID, S_LOGINID,
9394
S_VERSION, S_EXACT_MATCH, S_EXECUTABLE, S_CONTEXT, S_SUBJECT, S_OBJECT,
9495
S_PPID, S_KEY, S_RAW, S_NODE, S_IN_LOGS, S_JUST_ONE, S_SESSION, S_EXIT,
9596
S_LINEBUFFERED, S_UUID, S_VMNAME, S_DEBUG, S_CHECKPOINT, S_ARCH, S_FORMAT,
96-
S_EXTRA_TIME, S_EXTRA_LABELS, S_EXTRA_KEYS, S_EXTRA_OBJ2, S_ESCAPE, S_EOE_TMO };
97+
S_EXTRA_TIME, S_EXTRA_LABELS, S_EXTRA_KEYS, S_EXTRA_OBJ2, S_ESCAPE, S_EOE_TMO,
98+
S_MESSAGE_TYPE_EXCLUDE };
9799

98100
static const struct nv_pair optiontab[] = {
99101
{ S_EVENT, "-a" },
@@ -136,6 +138,8 @@ static const struct nv_pair optiontab[] = {
136138
{ S_LINEBUFFERED, "--line-buffered" },
137139
{ S_MESSAGE_TYPE, "-m" },
138140
{ S_MESSAGE_TYPE, "--message" },
141+
{ S_MESSAGE_TYPE_EXCLUDE, "-M" },
142+
{ S_MESSAGE_TYPE_EXCLUDE, "--message-exclude" },
139143
{ S_NODE, "-n" },
140144
{ S_NODE, "--node" },
141145
{ S_OBJECT, "-o" },
@@ -222,6 +226,7 @@ static void usage(void)
222226
"\t-k,--key <key string>\t\tsearch based on key field\n"
223227
"\t-l, --line-buffered\t\tFlush output on every line\n"
224228
"\t-m,--message <Message type>\tsearch based on message type\n"
229+
"\t-M,--message-exclude <Message type>\texclude based on message type\n"
225230
"\t-n,--node <Node name>\t\tsearch based on machine's name\n"
226231
"\t-o,--object <SE Linux Object context> search based on context of object\n"
227232
"\t-p,--pid <Process id>\t\tsearch based on process id\n"
@@ -317,6 +322,8 @@ int check_params(int count, char *vars[])
317322
return -1;
318323
}
319324
while (c < count && retval == 0) {
325+
int option;
326+
320327
// Go ahead and point to the next argument
321328
if (c+1 < count) {
322329
if (vars[c+1][0] != '-')
@@ -326,7 +333,8 @@ int check_params(int count, char *vars[])
326333
} else
327334
optarg = NULL;
328335

329-
switch (audit_lookup_option(vars[c])) {
336+
option = audit_lookup_option(vars[c]);
337+
switch (option) {
330338
case S_EVENT:
331339
if (!optarg) {
332340
fprintf(stderr,
@@ -607,12 +615,19 @@ int check_params(int count, char *vars[])
607615
}
608616
break;
609617
case S_MESSAGE_TYPE:
618+
case S_MESSAGE_TYPE_EXCLUDE:
610619
if (!optarg) {
611620
fprintf(stderr,
612621
"Argument is required for %s\n",
613622
vars[c]);
614623
retval = -1;
624+
} else if ((option == S_MESSAGE_TYPE && event_type_inverted == 1) ||
625+
option == S_MESSAGE_TYPE_EXCLUDE && event_type_inverted == 0) {
626+
fprintf(stderr,
627+
"Option -m is mutual exclusive with option -M\n");
628+
retval = -1;
615629
} else {
630+
event_type_inverted = (option == S_MESSAGE_TYPE_EXCLUDE);
616631
if (strcasecmp(optarg, "ALL") != 0) {
617632
retval = parse_msg(optarg);
618633
}

Diff for: src/ausearch-options.h

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern int event_debug;
4141
extern pid_t event_ppid;
4242
extern uint32_t event_session_id;
4343
extern ilist *event_type;
44+
extern int event_type_inverted;
4445

4546
/* Data type to govern output format */
4647
extern report_t report_format;

0 commit comments

Comments
 (0)