forked from nowsecure/fsmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
214 lines (200 loc) · 4.76 KB
/
util.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
/* fsmon -- MIT - Copyright NowSecure 2015-2016 - [email protected] */
#include <ctype.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#if __APPLE__
#include <sys/sysctl.h>
#endif
#if __linux__
#include <linux/limits.h>
#endif
#include <errno.h>
#include "fsmon.h"
void hexdump(const uint8_t *buf, unsigned int len, int w) {
unsigned int i, j;
if (w < 1) {
w = 16;
}
for (i = 0; i < len; i += w) {
printf ("0x%08x: ", i);
for (j = i; j < i + w; j++) {
if (j<len) printf (j%2?"%02x ":"%02x", buf[j]);
else printf (j%2?" ":" ");
}
printf (" ");
for (j = i; j < i + w; j++)
printf ("%c", isprint (buf[j])? buf[j]: '.');
printf ("\n");
}
}
const char *fm_typestr(int type) {
#define __(x) [x]=#x
const char *types[FSE_MAX_EVENTS] = {
__ (FSE_CREATE_FILE),
__ (FSE_DELETE),
__ (FSE_STAT_CHANGED),
__ (FSE_RENAME),
__ (FSE_CONTENT_MODIFIED),
__ (FSE_CREATE_DIR),
__ (FSE_CHOWN),
__ (FSE_EXCHANGE),
__ (FSE_FINDER_INFO_CHANGED),
__ (FSE_XATTR_MODIFIED),
__ (FSE_XATTR_REMOVED),
};
switch (type) {
case FSE_ARG_DONE: return "FSE_ARG_DONE";
case FSE_OPEN: return "FSE_OPEN";
case FSE_UNKNOWN: return "FSE_UNKNOWN";
}
return (type >= 0 && type < FSE_MAX_EVENTS && types[type])? types[type]: "";
}
const char *fm_argstr(int type) {
#define __(x) [x]=#x
const char *args[13] = {
__ (FSE_ARG_NONE),
__ (FSE_ARG_VNODE),
__ (FSE_ARG_STRING),
__ (FSE_ARG_PATH),
__ (FSE_ARG_INT32),
__ (FSE_ARG_INT64),
__ (FSE_ARG_RAW),
__ (FSE_ARG_INO),
__ (FSE_ARG_UID),
__ (FSE_ARG_DEV),
__ (FSE_ARG_MODE),
__ (FSE_ARG_GID),
__ (FSE_ARG_FINFO),
};
switch (type) {
case FSE_ARG_DONE: return "FSE_ARG_DONE";
case 0: return "FSE_UNKNOWN";
}
return (type >= 0 && type < FSE_MAX_EVENTS && args[type])? args[type]: "";
}
const char *fm_colorstr(int type) {
const char *colors[FSE_MAX_EVENTS] = {
Color_MAGENTA,// FSE_CREATE_FILE
Color_RED, // FSE_DELETE
Color_YELLOW, // FSE_STAT_CHANGED
Color_GREEN, // FSE_RENAME
Color_YELLOW, // FSE_CONTENT_MODIFIED
Color_BLUE, // FSE_CREATE_DIR
Color_YELLOW, // FSE_CHOWN
Color_GREEN, // FSE_EXCHANGE
Color_YELLOW, // FSE_FINDER_INFO_CHANGED
Color_YELLOW, // FSE_XATTR_MODIFIED,
Color_RED, // FSE_XATTR_REMOVED,
};
switch (type) {
case FSE_ARG_DONE: return Color_GREEN;
case FSE_OPEN: return Color_GREEN;
case FSE_UNKNOWN: return Color_RED;
}
return (type >= 0 && type < FSE_MAX_EVENTS)? colors[type]: "";
}
const char *get_proc_name(int pid, int *ppid) {
static char path[PATH_MAX] = {0};
#if __APPLE__
struct kinfo_proc * kinfo = (struct kinfo_proc*)&path;
size_t len = 1000;
int rc, mib[4];
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = pid;
memset (path, 0, sizeof (path));
if ((rc = sysctl (mib, 4, path, &len, NULL, 0)) != 0) {
perror("trace facility failure, KERN_PROC_PID\n");
exit (1);
}
if (ppid) *ppid = kinfo->kp_eproc.e_ppid;
return kinfo->kp_proc.p_comm;
#elif __linux__
char *p, *q;
int fd;
snprintf (path, sizeof (path), "/proc/%d/stat", pid);
fd = open (path, O_RDONLY);
if (fd == -1) {
// eprintf ("Cannot open '%s'\n", path);
return NULL;
}
path[0] = 0;
(void) read (fd, path, sizeof (path));
path[sizeof (path) - 1] = 0;
close (fd);
p = strchr (path, '(');
q = strchr (path, ')');
if (p && q && p < q && q[1] && q[2]) {
*q = 0;
if (ppid) {
char *r = strchr (q + 2, ' ');
if (r) *ppid = atoi (r + 1);
}
return p + 1;
}
return NULL;
#else
#warning getProcName not supported for this platform
return NULL;
#endif
}
bool is_directory (const char *str) {
struct stat buf = {0};
if (!str || !*str) return false;
if (stat (str, &buf) == -1) return false;
if ((S_IFBLK & buf.st_mode) == S_IFBLK) return false;
return S_IFDIR == (S_IFDIR & buf.st_mode);
}
bool copy_file(const char *src, const char *dst) {
char buf[4096];
struct stat stat_src;
int count, mode = 0640;
int fd_src, fd_dst;
fd_src = open (src, O_RDONLY);
if (fd_src == -1) {
perror ("open");
return false;
}
if (!fstat (fd_src, &stat_src)) {
mode = stat_src.st_mode;
}
fd_dst = open (dst, O_RDWR | O_CREAT | O_TRUNC, mode);
if (fd_dst == -1) {
close (fd_src);
return false;
}
for (;;) {
count = read (fd_src, buf, sizeof (buf));
if (count < 1) {
break;
}
(void) write (fd_dst, buf, count);
}
(void) close (fd_src);
(void) close (fd_dst);
return true;
}
static bool isPrintable(const char ch) {
if (ch == '"' || ch == '\\') {
return false;
}
return IS_PRINTABLE (ch);
}
char *fmu_jsonfilter(const char *s) {
char *r, *R = strdup (s);
for (r = R; *r; ) {
if (isPrintable (*r)) {
r++;
} else {
memmove (r, r + 1, strlen (r) + 1);
}
}
return R;
}