Skip to content

Commit 04ee7ff

Browse files
committed
add exclude_comm filter, and add filename to all of the filter function parameters
1 parent f9cf5f7 commit 04ee7ff

27 files changed

+207
-35
lines changed

config.h.in

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@
125125
/* Define to 1 if `vfork' works. */
126126
#undef HAVE_WORKING_VFORK
127127

128-
/* Define to the sub-directory where libtool stores uninstalled libraries. */
128+
/* Define to the sub-directory in which libtool stores uninstalled libraries.
129+
*/
129130
#undef LT_OBJDIR
130131

131132
/* Name of package */
@@ -260,6 +261,9 @@
260261
/* Filter chain to use */
261262
#undef SNOOPY_CONF_FILTER_CHAIN
262263

264+
/* Is filter "exclude_comm" available? */
265+
#undef SNOOPY_CONF_FILTER_ENABLED_exclude_comm
266+
263267
/* Is filter "exclude_spawns_of" available? */
264268
#undef SNOOPY_CONF_FILTER_ENABLED_exclude_spawns_of
265269

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ AC_ARG_ENABLE(all-filters,
500500
# ==============================================================================
501501
SNOOPY_CONFIGURE_FILTER_ENABLE( [exclude_spawns_of], [This filter drops messages that originate for specified process trees.])
502502
SNOOPY_CONFIGURE_FILTER_ENABLE( [exclude_uid], [This filter drops messages that match given UIDs.])
503+
SNOOPY_CONFIGURE_FILTER_ENABLE( [exclude_comm], [This filter drops messages that match given command.])
503504
SNOOPY_CONFIGURE_FILTER_ENABLE( [only_root], [This filter passes only messages generated by root.])
504505
SNOOPY_CONFIGURE_FILTER_ENABLE( [only_tty], [This filter passes only messages that have a TTY != none.])
505506
SNOOPY_CONFIGURE_FILTER_ENABLE( [only_uid], [This filter passes only messages that match given UIDs.])

etc/snoopy.ini.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
; List of available filters:
7474
; - exclude_spawns_of ; (available=@enable_filter_exclude_spawns_of@) Exclude log entries that occur in specified process trees
7575
; - exclude_uid ; (available=@enable_filter_exclude_uid@) Exclude these UIDs from logging
76+
; - exclude_comm ; (available=@enable_filter_exclude_comm@) Exclude these in command from logging
7677
; - only_root ; (available=@enable_filter_only_root@) Only log root commands
7778
; - only_tty ; (available=@enable_filter_only_tty@) Only log commands associated with a TTY
7879
; - only_uid ; (available=@enable_filter_only_uid@) Only log commands executed by these UIDs
@@ -86,7 +87,8 @@
8687
; - filter_chain = "exclude_uid:0" # Log all commands, except the ones executed by root
8788
; - filter_chain = "exclude_uid:1,2,3" # Log all commands, except those executed by users with UIDs 1, 2 and 3
8889
; - filter_chain = "only_uid:0" # Log only root commands
89-
; - filter_chain = "exclude_spawns_of:cron,my_daemon" # Do not log commands spawned by cron or my_daemon
90+
; - filter_chain = "exclude_spawns_of:cron,my_daemon" # Do not log commands spawned by cron or my_daemon
91+
; - filter_chain = "exclude_comm:mysql,mongo,redis-cli" # Do not log mysql, mongo and redis-cli commands
9092
; - filter_chain = "filter1:arg11;filter2:arg21,arg22;filter3:arg31,32,33"
9193
;
9294
; Default value:
@@ -97,6 +99,7 @@
9799
;filter_chain = "only_uid:0"
98100
;filter_chain = "only_uid:10000"
99101
;filter_chain = "exclude_uid:0"
102+
;filter_chain = "exclude_spawns_of:crond;exclude_comm:mysql,mongo"
100103

101104

102105

src/filter/Makefile.am

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ libsnoopy_filters_all_la_SOURCES += \
4242
exclude_uid.h
4343
endif
4444

45+
### Filter: exclude_comm
46+
#
47+
if FILTER_ENABLED_exclude_comm
48+
libsnoopy_filters_all_la_SOURCES += \
49+
exclude_comm.c \
50+
exclude_comm.h
51+
endif
52+
4553
### Filter: only_root
4654
#
4755
if FILTER_ENABLED_only_root

src/filter/exclude_comm.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* SNOOPY LOGGER
3+
*
4+
* File: snoopy/filter/exclude_comm.c
5+
*
6+
* Copyright (c) 2015 Datto, Inc. All rights reserved.
7+
* Author: Fred Mora - [email protected]
8+
*
9+
* This program is free software; you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation; either version 2, or (at your option)
12+
* any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program; if not, write to the Free Software Foundation,
21+
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22+
*
23+
*
24+
* changelog:
25+
* add exclude_comm filter;
26+
* arstercz
27+
* 2020-10-26
28+
*
29+
*/
30+
31+
32+
/*
33+
* Includes order: from local to global
34+
*/
35+
#include "exclude_comm.h"
36+
#include "exclude_spawns_of.h"
37+
#include "snoopy.h"
38+
39+
#include <stdio.h>
40+
#include <stdlib.h>
41+
#include <string.h>
42+
#include <sys/types.h>
43+
#include <unistd.h>
44+
45+
char* extract_comm(const char *str, const char *needle);
46+
47+
/*
48+
* SNOOPY FILTER: exclude_comm
49+
*
50+
* Description:
51+
* Excludes all log messages for executables that have the specified program name in their ancestors.
52+
* Strategy: We parse arg to create the "list of specified programs" (LoSP).
53+
* Then, we filter by filename from LoSP
54+
*
55+
* Params:
56+
* filename: command full path filename.
57+
* logMessage: Pointer to string that contains formatted log message (may be manipulated)
58+
* arg: Comma-separated list of program comm names for the spawns of which log messages are dropped.
59+
*
60+
* Return:
61+
* SNOOPY_FILTER_PASS or SNOOPY_FILTER_DROP
62+
*/
63+
int snoopy_filter_exclude_comm(const char *filename, char *msg, char const * const arg)
64+
{
65+
char *argDup; // Must not alter arg
66+
char **losp; // List of specified programs derived from arg
67+
int is_comm_in_list = 0;
68+
char *comm;
69+
70+
// Turn comma-separated arg into array of program name strings
71+
argDup = strdup(arg);
72+
losp = string_to_token_array(argDup);
73+
if (losp == NULL) {
74+
// If failure, we cannot filter anything, just pass the message
75+
return SNOOPY_FILTER_PASS;
76+
}
77+
78+
const char* delimiter = "/"; // path delimiter in Unix/Linux
79+
comm = extract_comm(filename, delimiter); // get command name
80+
81+
// Check if one of the program names in losp is an ancestor
82+
is_comm_in_list = find_string_in_array(comm, losp);
83+
free(losp);
84+
free(argDup);
85+
return (is_comm_in_list == 1) ? SNOOPY_FILTER_DROP : SNOOPY_FILTER_PASS; // Error means pass
86+
}
87+
88+
// get comm name from filename
89+
char* extract_comm(const char *str, const char *needle)
90+
{
91+
if (*needle == '\0')
92+
return (char *)str;
93+
94+
char *result = NULL;
95+
for (;;)
96+
{
97+
char *p = strstr(str, needle);
98+
if (p == NULL)
99+
break;
100+
101+
result = p + 1;
102+
str = p + 1;
103+
}
104+
return result;
105+
}

src/filter/exclude_comm.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* SNOOPY LOGGER
3+
*
4+
* File: snoopy/filter/exclude_comm.h
5+
*
6+
* Copyright (c) 2015 Datto, Inc. All rights reserved.
7+
* Author: Fred Mora - [email protected]
8+
*
9+
* This program is free software; you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation; either version 2, or (at your option)
12+
* any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program; if not, write to the Free Software Foundation,
21+
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22+
*
23+
* changelog:
24+
* add exclude_comm filter
25+
* arstercz
26+
* 2020-10-26
27+
*/
28+
29+
30+
31+
/*
32+
* SNOOPY FILTER: exclude_comm
33+
*/
34+
int snoopy_filter_exclude_comm (const char *filename, char *msg, char const * const arg);

src/filter/exclude_spawns_of.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
* Non-public function prototypes
5353
*/
5454
int find_ancestor_in_list(char **name_list);
55-
int find_string_in_array(char *str, char **str_array);
56-
char **string_to_token_array(char *str);
5755

5856

5957

@@ -67,13 +65,14 @@ char **string_to_token_array(char *str);
6765
* of the LoSP.
6866
*
6967
* Params:
68+
* filename: command full path filename.
7069
* logMessage: Pointer to string that contains formatted log message (may be manipulated)
7170
* arg: Comma-separated list of program names for the spawns of which log messages are dropped.
7271
*
7372
* Return:
7473
* SNOOPY_FILTER_PASS or SNOOPY_FILTER_DROP
7574
*/
76-
int snoopy_filter_exclude_spawns_of(char *msg, char const * const arg)
75+
int snoopy_filter_exclude_spawns_of(const char *filename, char *msg, char const * const arg)
7776
{
7877
char *argDup; // Must not alter arg
7978
char **losp; // List of specified programs derived from arg

src/filter/exclude_spawns_of.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@
2626
/*
2727
* SNOOPY FILTER: exclude_spawns_of
2828
*/
29-
int snoopy_filter_exclude_spawns_of (char *msg, char const * const arg);
29+
int snoopy_filter_exclude_spawns_of (const char *filename, char *msg, char const * const arg);
30+
int find_string_in_array(char *str, char **str_array);
31+
char **string_to_token_array(char *str);

src/filter/exclude_uid.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@
4545
* Excludes all log messages comming from specified UIDs
4646
*
4747
* Params:
48+
* filename: command full path filename.
4849
* logMessage: pointer to string that contains formatted log message (may be manipulated)
4950
* arg: Comma-separated list of UIDs for which log messages are dropped, passed for others
5051
*
5152
* Return:
5253
* SNOOPY_FILTER_PASS or SNOOPY_FILTER_DROP
5354
*/
54-
int snoopy_filter_exclude_uid (char *msg, char const * const arg)
55+
int snoopy_filter_exclude_uid (const char *filename, char *msg, char const * const arg)
5556
{
5657
uid_t curUid; // Actual UID of running process
5758
char *argDup = NULL;

src/filter/exclude_uid.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
/*
2626
* SNOOPY FILTER: exclude_uid
2727
*/
28-
int snoopy_filter_exclude_uid (char *msg, char const * const arg);
28+
int snoopy_filter_exclude_uid (const char *filename, char *msg, char const * const arg);

src/filter/noop.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@
3838
* Does nothing (just passes).
3939
*
4040
* Params:
41-
* result: pointer to string, to write result into
42-
* arg: (ignored)
41+
* filename: command full path filename.
42+
* result: pointer to string, to write result into
43+
* arg: (ignored)
4344
*
4445
* Return:
4546
* SNOOPY_FILTER_PASS
4647
*/
47-
int snoopy_filter_noop(char *msg, char const * const arg)
48+
int snoopy_filter_noop(const char *filename, char *msg, char const * const arg)
4849
{
4950
return SNOOPY_FILTER_PASS;
5051
}

src/filter/noop.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
/*
2626
* SNOOPY FILTER: noop
2727
*/
28-
int snoopy_filter_noop(char *msg, char const * const arg);
28+
int snoopy_filter_noop(const char *filename, char *msg, char const * const arg);

src/filter/only_root.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@
4242
* Only logs messages from root (uid=0 actually)
4343
*
4444
* Params:
45-
* msg: pointer to string that contains formatted log message (may be manipulated)
46-
* arg: arguments passed to this filter
45+
* filename: command full path filename.
46+
* msg: pointer to string that contains formatted log message (may be manipulated)
47+
* arg: arguments passed to this filter
4748
*
4849
* Return:
4950
* SNOOPY_FILTER_PASS or SNOOPY_FILTER_DROP
5051
*/
51-
int snoopy_filter_only_root (char *msg, char const * const arg)
52+
int snoopy_filter_only_root (const char *filename, char *msg, char const * const arg)
5253
{
5354
if (0 == getuid()) {
5455
return SNOOPY_FILTER_PASS;

src/filter/only_root.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
/*
2626
* SNOOPY FILTER: only_root
2727
*/
28-
int snoopy_filter_only_root (char *msg, char const * const arg);
28+
int snoopy_filter_only_root (const char *filename, char *msg, char const * const arg);

src/filter/only_tty.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@
4242
* Returns TTY of current process.
4343
*
4444
* Params:
45-
* result: pointer to string, to write result into
46-
* arg: (ignored)
45+
* filename: command full path filename.
46+
* msg: pointer to string, to write result into
47+
* arg: (ignored)
4748
*
4849
* Return:
4950
* number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE
5051
*/
51-
int snoopy_filter_only_tty(char *msg, char const * const arg)
52+
int snoopy_filter_only_tty(const char *filename, char *msg, char const * const arg)
5253
{
5354
char ttyPath[SNOOPY_DATASOURCE_TTY_sizeMaxWithNull];
5455
size_t ttyPathLen = SNOOPY_DATASOURCE_TTY_sizeMaxWithoutNull;

src/filter/only_tty.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@
3434
/*
3535
* SNOOPY FILTER: only_tty
3636
*/
37-
int snoopy_filter_only_tty(char *msg, char const * const arg);
37+
int snoopy_filter_only_tty(const char *filename, char *msg, char const * const arg);

src/filter/only_uid.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@
4545
* Excludes all log messages not comming from specific UID
4646
*
4747
* Params:
48+
* filename: command full path filename.
4849
* logMessage: pointer to string that contains formatted log message (may be manipulated)
4950
* arg: Comma-separated list of UIDs for which log message is passed on, dropped for all others
5051
*
5152
* Return:
5253
* SNOOPY_FILTER_PASS or SNOOPY_FILTER_DROP
5354
*/
54-
int snoopy_filter_only_uid (char *msg, char const * const arg)
55+
int snoopy_filter_only_uid (const char *filename, char *msg, char const * const arg)
5556
{
5657
uid_t curUid; // Actual UID of running process
5758
char *argDup = NULL;

src/filter/only_uid.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
/*
2626
* SNOOPY FILTER: only_uid
2727
*/
28-
int snoopy_filter_only_uid (char *msg, char const * const arg);
28+
int snoopy_filter_only_uid (const char *filename, char *msg, char const * const arg);

src/filtering.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
* SNOOPY_FILTER_PASS or SNOOPY_FILTER_DROP
5656
*/
5757
int snoopy_filtering_check_chain (
58+
const char *filename,
5859
char *logMessage,
5960
char *filterChain
6061
) {
@@ -112,7 +113,7 @@ int snoopy_filtering_check_chain (
112113
}
113114

114115
// Consult the filter, and return immediately if message should be dropped
115-
if (SNOOPY_FILTER_DROP == snoopy_filterregistry_callByName(filterNamePtr, logMessage, filterArgPtr)) {
116+
if (SNOOPY_FILTER_DROP == snoopy_filterregistry_callByName(filterNamePtr, filename, logMessage, filterArgPtr)) {
116117
return SNOOPY_FILTER_DROP;
117118
}
118119
}

src/filtering.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424

2525
int snoopy_filtering_check_chain (
26+
const char *filename,
2627
char *logMessage,
2728
char *chain
2829
);

0 commit comments

Comments
 (0)