Skip to content

Commit 5293b58

Browse files
author
Felipe Zimmerle
committed
Workable pipeline
- Processing logs with batch mode - Sending data to end server
1 parent c276f6b commit 5293b58

15 files changed

+925
-12
lines changed

Makefile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
CC=gcc
2-
CFLAGS=-I. -Ipipe_elements/. -Iinput_elements/. -Wno-pointer-sign -Wno-variadic-macros
3-
LIBS=-lyajl
2+
CFLAGS=-I. -Ipipe_elements/. -Iinput_elements/. -Wno-pointer-sign -Wno-variadic-macros -O0 -g
3+
LIBS=-lyajl -lcurl -lssl -lcrypto
44

55
%.o: %.c
66
$(CC) -c -o $@ $< $(CFLAGS)
77

88
OBJS = \
9+
configuration.o \
910
input_elements/read_from_filesystem.o \
1011
input_elements/input_batch.o \
1112
input_elements/filesystem-walker.o \
13+
mlogc-ng.o \
14+
pipeline.o \
1215
pipe_elements/dump.o \
1316
pipe_elements/persistence.o \
14-
configuration.o \
15-
mlogc-ng.o \
16-
pipeline.o
17+
pipe_elements/send_to_server.o
1718

1819
mlogc-ng: $(OBJS)
1920
gcc -o $@ $^ $(CFLAGS) $(LIBS)

input_elements/filesystem-walker.c

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
2+
#include <stdlib.h>
3+
#include <stdio.h>
4+
#include <dirent.h>
5+
#include <string.h>
6+
#include <sys/stat.h>
7+
8+
#include "mlogc-ng.h"
9+
#include "input_batch.h"
10+
#include "read_from_filesystem.h"
11+
12+
int inspect_file(unsigned char *full_path, void *audit_log_entry_cb,
13+
struct read_from_filesystem_config_t *conf)
14+
{
15+
int res = 0;
16+
FILE *fp;
17+
struct stat s;
18+
char *file_content_buf = NULL;
19+
struct audit_log_entry_t audit_log;
20+
void (*ptrEntry)(struct audit_log_entry_t *,
21+
struct read_from_filesystem_config_t *);
22+
ptrEntry = audit_log_entry_cb;
23+
24+
memset(&audit_log, '\0', sizeof(audit_log));
25+
26+
d("inspecting: %s\n", full_path);
27+
28+
res = stat(full_path, &s);
29+
if (res < 0)
30+
{
31+
e("Not able to get file size: %s\n", full_path);
32+
goto failed;
33+
}
34+
35+
if (s.st_size > MAX_FILE_SIZE)
36+
{
37+
e("This file is too big for us. Limit is: %d bytes\n", MAX_FILE_SIZE);
38+
res = -1;
39+
goto failed;
40+
}
41+
42+
file_content_buf = malloc(s.st_size + 1);
43+
if (file_content_buf == NULL)
44+
{
45+
e("Failed to allocate memory to read: %s\n", full_path);
46+
res = -1;
47+
goto failed;
48+
}
49+
50+
/* Everything is ok, lets have the data inside a buffer. */
51+
fp = fopen(full_path, "r");
52+
if (fp == NULL)
53+
{
54+
e("Failed to open the file: %s\n", full_path);
55+
res = -1;
56+
goto failed_fp;
57+
}
58+
59+
fread(file_content_buf, sizeof(char), s.st_size, fp);
60+
fclose(fp);
61+
62+
audit_log.hostname = "-";
63+
audit_log.remote_addr = "-";
64+
audit_log.remote_user = "-";
65+
audit_log.local_user = "-";
66+
audit_log.logtime = "-";
67+
audit_log.request = "-";
68+
audit_log.response_status = "-";
69+
audit_log.bytes_sent = "-";
70+
audit_log.refer = "-";
71+
audit_log.user_agent = "-";
72+
audit_log.uniqueid = "-";
73+
audit_log.sessionid = "-";
74+
audit_log.audit_file = full_path;
75+
audit_log.extra = "0";
76+
audit_log.md5 = "-";
77+
audit_log.audit_size = "0";
78+
79+
if (load_buffer(file_content_buf, &audit_log) == 0)
80+
{
81+
(*ptrEntry)(&audit_log, conf);
82+
}
83+
84+
failed_fp:
85+
free(file_content_buf);
86+
failed:
87+
return res;
88+
}
89+
90+
int open_directory_recursive(unsigned char *path, void *audit_log_entry_cb,
91+
struct read_from_filesystem_config_t *conf)
92+
{
93+
DIR *d;
94+
struct dirent *entry;
95+
int res = 0;
96+
97+
d = opendir(path);
98+
if (d == NULL)
99+
{
100+
e("Failed to open directory: %s\n", path);
101+
res = -1;
102+
goto failed;
103+
}
104+
105+
entry = readdir(d);
106+
while (entry != NULL)
107+
{
108+
const char *d_name = NULL;
109+
int full_path_len = 0;
110+
char *full_path = NULL;
111+
112+
d_name = entry->d_name;
113+
114+
full_path_len = snprintf(NULL, 0, "%s/%s", path, d_name);
115+
full_path = malloc(sizeof(char) * (full_path_len + 1));
116+
if (full_path == NULL)
117+
{
118+
e("Failed: missing memory to continue.\n");
119+
return -1;
120+
}
121+
122+
full_path_len = snprintf(full_path, full_path_len + 1, "%s/%s", path,
123+
d_name);
124+
125+
/* if it is a dir (different from "." or ".." we want to jump in. */
126+
if (entry->d_type & DT_DIR)
127+
{
128+
if (strcmp (d_name, "..") != 0 && strcmp (d_name, ".") != 0)
129+
{
130+
/* FIXME: we should not ignore this return value. */
131+
open_directory_recursive(full_path, audit_log_entry_cb, conf);
132+
}
133+
}
134+
135+
/* if it is a file, let see inside. */
136+
if (!(entry->d_type & DT_DIR))
137+
{
138+
inspect_file(full_path, audit_log_entry_cb, conf);
139+
}
140+
141+
free(full_path);
142+
143+
entry = readdir(d);
144+
}
145+
146+
failed_read_dir:
147+
closedir(d);
148+
failed:
149+
return res;
150+
}
151+

input_elements/filesystem-walker.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
#ifndef __FILESYSTEM_WALKER_H__
3+
#define __FILESYSTEM_WALKER_H__
4+
5+
int inspect_file(unsigned char *, void *);
6+
int open_directory_recursive(unsigned char *, void *);
7+
8+
#endif

0 commit comments

Comments
 (0)