-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.c
272 lines (221 loc) · 6.15 KB
/
user.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
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
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Author: Nichole Boufford <[email protected]>
*
* Copyright (C) 2022 University of British Columbia
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*/
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <bpf/bpf.h>
#include <pthread.h>
#include <netinet/in.h>
#include <syslog.h>
#include <dirent.h>
#include <time.h>
#include "common.h"
#include "record.h"
#include "spade.c"
#include "track.skel.h"
#define MAX_LINE_SIZE 100
static struct track *skel = NULL;
static int fd;
static int inode_track_index = 0;
static void init_log()
{
char filename[40];
struct tm *time_str;
time_t current_time = time(NULL);
time_str = localtime(¤t_time);
strftime(filename, sizeof(filename), "/tmp/prov_%Y-%m-%d_%H:%M:%S.json", time_str);
fd = open(filename, O_RDWR | O_CREAT);
}
static void sig_handler(int sig)
{
if (sig == SIGTERM) {
syslog(LOG_INFO, "thoth: Received termination signal...");
track__destroy(skel);
exit(0);
}
}
// Start with adding just one id into the map
int add_inode(struct track *skel, uint32_t index, uint64_t value)
{
int map_fd;
uint64_t id;
if (inode_track_index > INODE_MAX_ENTRY) {
printf("max tracking directories reached, failed to add directory\r\n");
return -1;
}
map_fd = bpf_object__find_map_fd_by_name(skel->obj, "inode_map");
id = value;
bpf_map_update_elem(map_fd, &inode_track_index, &id, BPF_ANY);
inode_track_index++;
return 0;
}
// TODO
int remove_inode(struct track *skel, uint32_t index, uint64_t value)
{
return 0;
}
void write_to_file(struct entry_t *entry, char *buffer)
{
// should lock here
spade_write_node_file(fd, entry, buffer);
spade_write_node_proc(fd, entry);
spade_write_edge(fd, entry);
// should unlock here
}
// this is a temporary fix for resolving the file path
void process_file_path(struct entry_t *entry, char *buffer)
{
int path_len = 0;
// sanity check
if (entry->file_path_depth <= 0 || entry->file_path_depth > PATH_DEPTH_MAX)
return;
for (int i = entry->file_path_depth - 1; i >= 0; i--) {
int len = 0;
if (i == entry->file_path_depth - 1) {
len = sprintf(buffer, "/");
if (len > 0)
path_len += len;
}
if (i == 0)
len = sprintf(buffer + path_len, "%s", entry->file_path[i]);
else
len = sprintf(buffer + path_len, "%s/", entry->file_path[i]);
if (len > 0)
path_len += len;
}
}
int buf_process_entry(void *ctx, void *data, size_t len)
{
struct entry_t *read_entry = (struct entry_t *)data;
char path_buffer[TOTAL_PATH_MAX];
process_file_path(read_entry, (char *)&path_buffer);
write_to_file(read_entry, (char *)&path_buffer);
return 0;
}
static int cli_process_msg(struct op_msg *msg, struct err_msg *err)
{
DIR *dir;
if (msg->op == ADD_DIR) {
dir = opendir(msg->arg[0]);
if (dir == NULL) {
err->err = ERR_OK;
snprintf(err->msg, MSG_LEN, "error! cannot find directory: %s", msg->arg[0]);
return 0;
}
struct dirent *dir_entry = readdir(dir);
if (dir_entry == NULL) {
err->err = ERR_OK;
snprintf(err->msg, MSG_LEN, "error adding directory to tracked directories");
}
// TODO: get response from add
add_inode(skel, 0, dir_entry->d_ino);
}
if (msg->op == RM_DIR) {
dir = opendir(msg->arg[0]);
if (dir == NULL) {
err->err = ERR_OK;
snprintf(err->msg, MSG_LEN, "error! cannot find directory: %s", msg->arg[0]);
return 0;
}
struct dirent *dir_entry = readdir(dir);
if (dir_entry == NULL) {
err->err = ERR_OK;
snprintf(err->msg, MSG_LEN, "error adding directory to tracked directories");
}
// TODO: get response from remove
remove_inode(skel, 0, dir_entry->d_ino);
}
return 0;
}
void * cli_server(void *data)
{
int sockfd;
socklen_t client_len;
struct sockaddr_in server_addr, client_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Error opening socket for cli");
exit(-1);
}
bzero((char *) &server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(CLI_PORT);
if (bind(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
perror("Error binding socket for cli");
close(sockfd);
exit(-1);
}
listen(sockfd, 5);
client_len = sizeof(client_addr);
int newsockfd;
while (1) {
newsockfd = accept(sockfd, (struct sockaddr *) &client_addr, &client_len);
if (newsockfd < 0)
perror("error on socket accept");
struct op_msg r_msg;
int n = read(newsockfd, &r_msg, sizeof(r_msg));
if (n < 0)
perror("error reading from socket");
// TODO: remove and replace with real error msg
struct err_msg e_msg = {
.err = ERR_OK,
};
sprintf(e_msg.msg, "tracking: %s", r_msg.arg[0]);
cli_process_msg(&r_msg, &e_msg);
n = write(newsockfd, &e_msg, sizeof(e_msg));
if (n < 0)
perror("error writing to socket");
close(newsockfd);
}
return 0;
}
int main(int argc, char *argv[])
{
struct ring_buffer *ringbuf = NULL;
int err, map_fd;
pthread_t cli_thread_id;
syslog(LOG_INFO, "thothd: Registering signal handler...");
signal(SIGTERM, sig_handler);
syslog(LOG_INFO, "thothd: Starting CLI server...");
int rc = pthread_create(&cli_thread_id, NULL, cli_server, NULL);
if (rc != 0)
syslog(LOG_ERR, "thothd: error starting cli thread");
printf("starting...\r\n");
skel = track__open_and_load();
if (!skel) {
syslog(LOG_ERR, "Failed to load bpf skeleton");
goto close_prog;
}
err = track__attach(skel);
if (err != 0)
syslog(LOG_ERR, "Error attaching skeleton\r\n");
init_log();
if (fd < 0)
syslog(LOG_ERR, "error creating log file\r\n");
// Locate ring buffer
map_fd = bpf_object__find_map_fd_by_name(skel->obj, "ringbuf");
if (map_fd < 0) {
syslog(LOG_ERR, "Failed to find ring buffer map object");
goto close_prog;
}
ringbuf = ring_buffer__new(map_fd, buf_process_entry, NULL, NULL);
while (ring_buffer__poll(ringbuf, -1) >= 0) {
// collect prov in callback
}
close_prog:
close(fd);
track__destroy(skel);
}