forked from a2o/snoopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.c
187 lines (160 loc) · 4.35 KB
/
log.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
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
/*
* SNOOPY LOGGER
*
* File: log.c
*
* Copyright (c) 2014-2015 Bostjan Skufca <[email protected]>
*
* 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 "log.h"
#include "snoopy.h"
#include "configuration.h"
#if defined(SNOOPY_FILTERING_ENABLED)
#include "filtering.h"
#endif
#include "inputdatastorage.h"
#include "message.h"
#include "misc.h"
#include "outputregistry.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* snoopy_log_syscall_execv
*
* Description:
* Log the call to syscall execv()
*
* Params:
* filename: filename of program being executed
* argv: arguments being passed to execv()
*
* Return:
* void
*/
void snoopy_log_syscall_execv (
const char *filename,
char *const argv[]
) {
// Syscall was done without new environmental variables, so let's create
// a fake empty array to simulate it.
char *envp[] = { NULL };
snoopy_log_syscall_exec("execv", filename, argv, envp);
}
/*
* snoopy_log_syscall_execve
*
* Description:
* Log the call to syscall execve()
*
* Params:
* filename: filename of program being executed
* argv: arguments being passed to execve()
* envp: environment being passed to execve()
*
* Return:
* void
*/
void snoopy_log_syscall_execve (
const char *filename,
char *const argv[],
char *const envp[]
) {
snoopy_log_syscall_exec("execve", filename, argv, envp);
}
/*
* snoopy_log_syscall_exec
*
* Description:
* Common routine that does execv(e)() logging
*
* Params:
* syscallName: system call name to log
* filename: filename of program being executed
* argv: arguments being passed to execv(e)()
* envp: environment being passed to execve()
*
* Return:
* void
*/
void snoopy_log_syscall_exec (
const char *syscallName,
const char *filename,
char *const argv[],
char *const envp[]
) {
char *logMessage = NULL;
snoopy_configuration_t *CFG;
/* Initialize Snoopy */
snoopy_init();
/* Get config pointer */
CFG = snoopy_configuration_get();
// Store arguments passed to execv(e)()
snoopy_inputdatastorage_store_filename(filename);
snoopy_inputdatastorage_store_argv((char**)argv);
snoopy_inputdatastorage_store_envp((char**)envp);
/* Initialize empty log message */
logMessage = malloc(SNOOPY_LOG_MESSAGE_MAX_SIZE);
logMessage[0] = '\0';
/* Generate log message in specified format */
snoopy_message_generateFromFormat(logMessage, CFG->message_format);
#if defined(SNOOPY_FILTERING_ENABLED)
/* Should message be passed to syslog or not? */
if (
(SNOOPY_FALSE == CFG->filtering_enabled)
||
(
(SNOOPY_TRUE == CFG->filtering_enabled)
&&
(SNOOPY_FILTER_PASS == snoopy_filtering_check_chain(logMessage, CFG->filter_chain))
)
) {
#endif
snoopy_log_dispatch(logMessage, SNOOPY_LOG_MESSAGE);
#if defined(SNOOPY_FILTERING_ENABLED)
}
#endif
/* Housekeeping */
free(logMessage);
snoopy_cleanup();
}
/*
* snoopy_log_dispatch
*
* Description:
* Dispatch given message to configured output
*
* Params:
* logMessage: message to dispatch
* errorOrMessage: is this a message or an error?
*
* Return:
* int: See snoopy.h (SNOOPY_OUTPUT_*) for details.
*/
int snoopy_log_dispatch (
char *logMessage,
int errorOrMessage
) {
/* Dispatch only if non-zero size */
if (0 == strlen(logMessage)) {
return SNOOPY_OUTPUT_GRACEFUL_DISCARD;
}
// Dispatch to configured output
return snoopy_outputregistry_dispatch(logMessage, errorOrMessage);
}