summaryrefslogtreecommitdiff
path: root/src/bin/rules.c
blob: ee5f66da414a2f9ee51dada6a91ce029ef9fd7ad (plain) (blame)
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
/*
 * Copyright © 2011 ASP64 <guillaume.friloux@asp64.com>
 *
 * 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * @file rules.c
 * @brief Functions the manipulates rules
 * @author Guillaume Friloux <guillaume.friloux@asp64.com>
 * @version 1.0
 *
 * @bug : Better parsing of tags (we only escape double quotes right now)
 *
 * Functions the manipulates rules
 *
 */
#include "rules.h"

/**
 * @fn int rules_load(void)
 * @brief List the rules directory and loads every rule in it
 *
 * @return 0 if no error, -1 if can't open rules dir
 */
int rules_load(void)
{
	DIR *fd;
	struct dirent * entree;

	list_rules = NULL;

	// For every rule in rules directory, we will load it
	fd = opendir(global_rulesdir);
	if( !fd )
	{
		EINA_LOG_DOM_ERR(einadom_rules, "Can't open rules directory %s : %s", global_rulesdir, strerror(errno));
		return(-1);
	}
	while((entree = readdir(fd)) != NULL)
	{
		if( !strcmp(entree->d_name, ".") ) continue;
		if( !strcmp(entree->d_name, "..") ) continue;

		EINA_LOG_DOM_DBG(einadom_rules, "Found rule file %s", entree->d_name);

		rules_load_rule(entree->d_name);
	}

	closedir(fd);
	return(0);
}

/**
 * @fn int rules_load_rule(char *rule_name)
 * @brief This function receives the name of a rule file, and ask for libconf
 *  to analyze it
 *
 * @param rule_name (char *) name of the rule (filename)
 *
 * @return 0
 */
int rules_load_rule(char *rule_name)
{
	char full_path[512];
	struct libconfig myconf;

	sprintf(full_path, "%s%s", global_rulesdir, rule_name);

	libconfig_init(full_path, &myconf);
	libconfig_load(&myconf);

	// We have to make a new rule struct
	rules_temp = malloc(sizeof(struct rule));

	rules_temp->name = malloc(sizeof(char) * ( strlen(rule_name) + 1 ) );
	strcpy(rules_temp->name, rule_name);

	// We init default values for our struct
	rules_temp->filename = NULL;
	rules_temp->type = NULL;
	rules_temp->source_host = NULL;
	rules_temp->source_path = NULL;
	rules_temp->tags = NULL;
	rules_temp->todel = 0;

	libconfig_list(&myconf, rules_load_rule_loadspec);

	libconfig_free(&myconf);

	// We insert our new rule in our eina rule list
	list_rules = eina_list_append(list_rules, rules_temp);
	return(0);
}

/**
 * @fn int rules_load_rule_loadspec(char *variable, char *value)
 * @brief This function is a callback used by libconf to send us
 *  every variable+value that libconf found in a rulefile
 *
 * @param variable (char *) variable's name
 * @param value (char *) variable's value
 *
 * @return 0
 */
int rules_load_rule_loadspec(char *variable, char *value)
{
	if( !strcmp(variable, "filename") )
	{
		rules_temp->filename = malloc(sizeof(char) * ( strlen(value) + 1 ));
		strcpy(rules_temp->filename, value);
	}
	else if( ( !strcmp(variable, "message") )
	    || ( !strcmp(variable, "message_match") )
	    || ( !strcmp(variable, "message_unmatch") )
	       )
	{
		struct regex *tmp_regex;
		int ret;

		tmp_regex = malloc(sizeof(struct regex));

		ret = regcomp(&(tmp_regex->preg), value, REG_EXTENDED);
		if( ret )
		{
			EINA_LOG_DOM_ERR(einadom_rules, "Regcomp failed to compile regexp %s", value);
			free(tmp_regex);
			return(0);
		}

		tmp_regex->message = malloc(sizeof(char) * ( strlen(value) + 1 ));
		strcpy(tmp_regex->message, value);

		if( !strcmp(variable, "message_unmatch") )
			tmp_regex->must_match = 0;
		else
			tmp_regex->must_match = 1;

		rules_temp->list_regex = eina_list_append(rules_temp->list_regex, tmp_regex);
	}
	else if( !strcmp(variable, "type") )
	{
		rules_temp->type = malloc(sizeof(char) * ( strlen(value) + 1 ));
		strcpy(rules_temp->type, value);
	}
	else if( !strcmp(variable, "source_host") )
	{
		rules_temp->source_host = malloc(sizeof(char) * ( strlen(value) + 1 ));
		strcpy(rules_temp->source_host, value);
	}
	else if( !strcmp(variable, "source_path") )
	{
		rules_temp->source_path = malloc(sizeof(char) * ( strlen(value) + 1 ));
		strcpy(rules_temp->source_path, value);
	}
	else if( !strcmp(variable, "tags") )
	{
		rules_temp->tags = malloc(sizeof(char) * ( strlen(value) + 1 ));
		strcpy(rules_temp->tags, value);
	}
	else if( !strcmp(variable, "delete") )
	{
		rules_temp->todel = atoi(value);
	}
	else
	{
		EINA_LOG_DOM_ERR(einadom_rules, "Unknown variable name %s", variable);
	}
	return(0);
}


/**
 * @fn int rules_print(void)
 * @brief This function will print to stdout every rules that is in
 *  the rules list.
 *
 * @return 0
 */
int rules_print(void)
{
	Eina_List *l;
	struct rule *foundrule = NULL;
	
	EINA_LIST_FOREACH(list_rules, l, foundrule)
	{
		EINA_LOG_DOM_INFO(einadom_rules,
		                  "\n[%s]\n"
		                  "\tfilename\t= %s\n"
		                  "\ttype\t\t= %s\n"
		                  "\tsource_host\t= %s\n"
		                  "\tsource_path\t= %s\n"
		                  "\ttags\t\t= %s\n"
		                  "\tdelete\t= %d\n\n",
		                  foundrule->name,
		                  foundrule->filename,
		                  foundrule->type,
		                  foundrule->source_host,
		                  foundrule->source_path,
		                  foundrule->tags,
		                  foundrule->todel);
	}
	return(0);
}

/**
 * @fn int rules_list(int (*callback)(struct rule *foundrule))
 * @brief This function will list every rule in the list, to send each
 *  item to the given callback
 *
 * @param callback (int *) Callback that will receive all the items
 *
 * @return 0
 */
int rules_list(int (*callback)(struct rule *foundrule))
{
	Eina_List *l;
	struct rule *foundrule = NULL;
	
	EINA_LIST_FOREACH(list_rules, l, foundrule)
	{
		callback(foundrule);
	}
	return(0);
}


/**
 * @fn int rules_filtermessage(struct logmessage *new_logmessage)
 * @brief This function will get a log message, and apply all the rules
 *  on it before giving it back and filtered
 *
 * @param new_logmessage (struct logmessage *)
 *
 * @return 0
 */
int rules_filtermessage(struct logmessage *new_logmessage)
{
	Eina_List *l, *l2;
	struct rule *foundrule = NULL;
	struct regex *foundregex = NULL;
	int ret,
	    excluded = 0;

	// We check each rules to see what we have to do
	EINA_LIST_FOREACH(list_rules, l, foundrule)
	{
		// If filename is set, we check that our log is affected
		// By this rule's filename
		if( foundrule->filename )
		{
			ret = fnmatch(foundrule->filename, new_logmessage->source_path, FNM_NOESCAPE);
			if( ret )
			{
				EINA_LOG_DOM_DBG(einadom_rules, "Log \"%s\" from \"%s\" is not affected by rule %s (filename exclude)", new_logmessage->message, new_logmessage->source_path, foundrule->name);
				continue;
			}
		}

		// For every regex affected to the rule, we apply it to the
		// Log message we got to see if we have something to filter
		EINA_LIST_FOREACH(foundrule->list_regex, l2, foundregex)
		{
			size_t nmatch = 2;                                                      
			regmatch_t pmatch[2];

			ret = regexec(&(foundregex->preg), new_logmessage->message,nmatch, pmatch, 0);

			// If regexec returns 0, then the log message matches the regex.
			// If regexec returns 1, then the log message doesnt match the regex.
			// So if the regex must match (message= or message_match= in rule, so must_match=1) or musnt match (message_unmatch=, must_match=0),
			// We can consider that the message must be filtered if regex returns something different than 'must_match'
			if( ret == foundregex->must_match )
			{
				EINA_LOG_DOM_INFO(einadom_rules, "Log \"%s\" from \"%s\" is not affected by rule %s (message exclude : %s / %d / %d)", new_logmessage->message, new_logmessage->source_path, foundrule->name, foundregex->message, foundregex->must_match, ret);
				excluded = 1;
				break;
			}
		}

		if( excluded )
			continue;

		// If we get here, then our log message have to be filtered

		// If type is specified, then we have to override it
		if( foundrule->type )
			logmessages_set_type(new_logmessage, foundrule->type);

		if( foundrule->source_host )
			logmessages_set_sourcehost(new_logmessage, foundrule->source_host);

		if( foundrule->tags )
			logmessages_add_tag(new_logmessage, foundrule->tags);

		if( foundrule->todel )
			logmessages_set_todel(new_logmessage, 1);
	}
	return(0);
}