forked from a2o/snoopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexclude_uid.c
94 lines (77 loc) · 2.51 KB
/
exclude_uid.c
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
/*
* SNOOPY LOGGER
*
* File: snoopy/filter/exclude_uid.c
*
* Copyright (c) 2014-2015 Bostjan Skufca <bostjan@a2o.si>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* Includes order: from local to global
*/
#include "exclude_uid.h"
#include "snoopy.h"
#include "parser.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
/*
* SNOOPY FILTER: exclude_uid
*
* Description:
* Excludes all log messages comming from specified UIDs
*
* Params:
* logMessage: pointer to string that contains formatted log message (may be manipulated)
* arg: Comma-separated list of UIDs for which log messages are dropped, passed for others
*
* Return:
* SNOOPY_FILTER_PASS or SNOOPY_FILTER_DROP
*/
int snoopy_filter_exclude_uid (char *msg, char const * const arg)
{
uid_t curUid; // Actual UID of running process
char *argDup = NULL;
char **argParsed = NULL;
int argCount = 0;
int retVal = -1;
/* Get uid of current process */
curUid = getuid();
/* Parse arguments - values are malloc()-ed */
argDup = strdup(arg);
argCount = snoopy_parser_argList_csv(argDup, &argParsed);
/* Loop through all UIDs passed to the filter as argument */
for (int i=0 ; i<argCount ; i++) {
uid_t argCurUid; // Actual UID to be used for comparison
// Convert literal UID to numeric type
argCurUid = (uid_t) atol(argParsed[i]);
// If UID matches, drop the message
if (argCurUid == curUid) {
retVal = SNOOPY_FILTER_DROP;
goto RETURN_AND_FREE_ALL;
}
}
retVal = SNOOPY_FILTER_PASS;
goto RETURN_AND_FREE_ALL;
RETURN_AND_FREE_ALL:
/* Cleanup */
free(argDup);
free(argParsed);
// None of the UIDs matched so far, therefore we are passing this message on
return retVal;
}