forked from pandax381/tcpeek
/
init.c
323 lines (301 loc) · 8.87 KB
/
init.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
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
313
314
315
316
317
318
319
320
321
322
323
#include "tcpeek.h"
#define DEFAULT_PCAP_SNAPLEN 138
static void
tcpeek_init_global(void);
static void
tcpeek_init_option(int argc, char *argv[]);
static void
tcpeek_init_signal(void);
static void
tcpeek_init_log(void);
static void
tcpeek_init_session(void);
static void
tcpeek_init_filter_and_stat(void);
static void
tcpeek_init_pcap(void);
static void
tcpeek_init_addr(void);
static void
tcpeek_init_setuid(void);
static void
tcpeek_init_socket(void);
static void
usage(void);
static void
version(void);
void
tcpeek_init(int argc, char *argv[]) {
tcpeek_init_global();
tcpeek_init_option(argc, argv);
tcpeek_init_signal();
tcpeek_init_log();
tcpeek_init_session();
tcpeek_init_filter_and_stat();
tcpeek_init_pcap();
tcpeek_init_addr();
tcpeek_init_setuid();
tcpeek_init_socket();
}
static void
tcpeek_init_global(void) {
memset(&g, 0x00, sizeof(g));
g.option.timeout = 30;
g.option.buffer = 2;
g.option.checksum = TCPEEK_CKSUM_IP;
strncpy(g.option.socket, TCPEEK_SOCKET_FILE, sizeof(g.option.socket) - 1);
g.option.expression = lnklist_create();
lnklist_add_tail(g.option.expression, strdup("RX:RX@%:%"));
lnklist_add_tail(g.option.expression, strdup("TX:TX@%:%"));
g.filter = lnklist_create();
g.soc = -1;
}
static void
tcpeek_init_option(int argc, char *argv[]) {
int opt;
static struct option long_options[] = {
{"user", 1, NULL, 'u'},
{"interface", 1, NULL, 'i'},
{"checksum", 1, NULL, 'c'},
{"socket", 1, NULL, 'U'},
{"timeout", 1, NULL, 't'},
{"buffer", 1, NULL, 'B'},
{"loglevel", 1, NULL, 'l'},
{"quiet", 0, NULL, 'q'},
{"promisc", 0, NULL, 500},
{"icmp", 0, NULL, 501},
{"help", 0, NULL, 'h'},
{"version", 0, NULL, 'v'},
{ NULL, 0, NULL, 0 }
};
while((opt = getopt_long_only(argc, argv, "u:i:c:U:t:B:l:qhv", long_options, NULL)) != -1) {
switch(opt) {
case 'u':
strncpy(g.option.user, optarg, sizeof(g.option.user) - 1);
break;
case 'i':
strncpy(g.option.ifname, optarg, sizeof(g.option.ifname) - 1);
break;
case 'c':
if(!strisequal(optarg, "0") && !strisequal(optarg, "1") && !strisequal(optarg, "2")) {
usage();
tcpeek_terminate(0);
}
g.option.checksum = strtol(optarg, NULL, 10);
break;
case 'U':
strncpy(g.option.socket, optarg, sizeof(g.option.socket) - 1);
break;
case 'B':
if(!strisdigit(optarg)) {
usage();
tcpeek_terminate(0);
}
g.option.buffer = strtol(optarg, NULL, 10);
break;
case 't':
if(!strisdigit(optarg)) {
usage();
tcpeek_terminate(0);
}
g.option.timeout = strtol(optarg, NULL, 10);
break;
case 'q':
g.option.quiet = 1;
break;
case 500:
g.option.promisc = 1;
break;
case 501:
g.option.icmp = 1;
break;
case 'h':
usage();
tcpeek_terminate(0);
case 'v':
version();
tcpeek_terminate(0);
default:
usage();
tcpeek_terminate(1);
}
}
while(optind < argc) {
lnklist_add_tail(g.option.expression, strdup(argv[optind++]));
}
}
static void
tcpeek_init_signal(void) {
static int signals[] = {SIGINT, SIGTERM, SIGPIPE, SIGUSR1, SIGUSR2, SIGALRM, 0};
struct sigaction sig;
int offset;
memset(&sig, 0, sizeof(sig));
sig.sa_handler = tcpeek_signal_handler;
for(offset = 0; signals[offset]; offset++) {
if(sigaction(signals[offset], &sig, NULL) == -1){
error_abort("sigaction: '%d' %s", signals[offset], strerror(errno));
}
}
}
static void
tcpeek_init_log(void) {
//openlog(PACKAGE_NAME, LOG_NDELAY | LOG_PERROR, LOG_DAEMON);
}
static void
tcpeek_init_session(void) {
g.session.table = hashtable_create(TCPEEK_SESSION_TABLE_SIZE);
if(!g.session.table) {
error_abort("hashtable can't create.");
}
pthread_mutex_init(&g.session.mutex, NULL);
}
static void
tcpeek_init_filter_and_stat(void) {
char *expression;
struct tcpeek_filter *filter;
lnklist_iter_init(g.option.expression);
while(lnklist_iter_hasnext(g.option.expression)) {
expression = lnklist_iter_next(g.option.expression);
filter = tcpeek_filter_create();
if(tcpeek_filter_parse(filter, expression) == -1) {
tcpeek_filter_destroy(filter);
error_abort("filter '%s' parse error.", expression);
}
if(!lnklist_add(g.filter, filter, filter->stat ? lnklist_size(g.filter) : 0)) {
tcpeek_filter_destroy(filter);
error_abort("can't allocate.");
}
}
}
static void
tcpeek_init_pcap(void) {
char *ifname, errmsg[PCAP_ERRBUF_SIZE], expression[] = "tcp or icmp";
struct bpf_program bpf;
if(strisempty(g.option.ifname)) {
ifname = pcap_lookupdev(errmsg);
if(!ifname) {
error_abort("%s", errmsg);
}
strncpy(g.option.ifname, ifname, sizeof(g.option.ifname) - 1);
}
g.pcap.pcap = pcap_create(g.option.ifname, errmsg);
if(!g.pcap.pcap) {
error_abort("%s", errmsg);
}
if(pcap_set_buffer_size(g.pcap.pcap, g.option.buffer * 1024 * 1024) != 0) {
error_abort("%s", "can not set buffer size");
}
if(pcap_set_snaplen(g.pcap.pcap, DEFAULT_PCAP_SNAPLEN) != 0) {
error_abort("%s", "can not set snaplen");
}
if(pcap_set_promisc(g.pcap.pcap, g.option.promisc) != 0) {
error_abort("%s", "can not set promiscuous mode");
}
if(pcap_set_timeout(g.pcap.pcap, 1) != 0) {
error_abort("%s", "can not set timeout");
}
if(pcap_activate(g.pcap.pcap) != 0) {
error_abort("%s", pcap_geterr(g.pcap.pcap));
}
if(pcap_compile(g.pcap.pcap, &bpf, expression, 0, 0) == -1) {
error_abort("%s '%s'", pcap_geterr(g.pcap.pcap), expression);
}
if(pcap_setfilter(g.pcap.pcap, &bpf) == -1){
error_abort("%s", pcap_geterr(g.pcap.pcap));
}
pcap_freecode(&bpf);
g.pcap.snapshot = pcap_snapshot(g.pcap.pcap);
g.pcap.datalink = pcap_datalink(g.pcap.pcap);
if(g.pcap.datalink != DLT_EN10MB && g.pcap.datalink != DLT_LINUX_SLL) {
error_abort("not support datalink %s (%s)",
pcap_datalink_val_to_name(g.pcap.datalink),
pcap_datalink_val_to_description(g.pcap.datalink)
);
}
}
static void
tcpeek_init_addr(void) {
struct ifaddrs *ifap, *ifa = NULL;
if(getifaddrs(&ifap) != -1) {
for(ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
if(!ifa->ifa_addr) continue;
if(strisequal(ifa->ifa_name, g.option.ifname) && ifa->ifa_addr->sa_family == AF_INET) {
g.addr.unicast.s_addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
break;
}
}
}
freeifaddrs(ifap);
if(!ifa) {
error_abort("'%s' not found", g.option.ifname);
}
}
static void
tcpeek_init_setuid(void) {
struct passwd *passwd;
gid_t groups[128];
int ngroups;
if(strisempty(g.option.user)) {
return;
}
passwd = strisdigit(g.option.user) ? getpwuid((uid_t)strtol(g.option.user, NULL, 10)) : getpwnam(g.option.user);
if(!passwd) {
error_abort("%s", strerror(errno));
}
ngroups = sizeof(groups);
if(getgrouplist(g.option.user, passwd->pw_gid, groups, &ngroups) == -1) {
error_abort("getgrouplist: %s", strerror(errno));
}
if(setgroups(ngroups, groups) == -1) {
error_abort("setgroups: %s", strerror(errno));
}
if(setgid(passwd->pw_gid) == -1) {
error_abort("setgid: %s", strerror(errno));
}
if(setuid(passwd->pw_uid) == -1) {
error_abort("setuid: %s", strerror(errno));
}
}
static void
tcpeek_init_socket(void) {
struct sockaddr_un sockaddr;
g.soc = socket(PF_UNIX, SOCK_STREAM, 0);
if(g.soc == -1) {
error_abort("socket: %s", strerror(errno));
}
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sun_family = PF_UNIX;
strcpy(sockaddr.sun_path, g.option.socket);
if(bind(g.soc, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1) {
close(g.soc);
g.soc = -1;
error_abort("bind: %s", strerror(errno));
}
}
static void
usage(void) {
printf("usage: %s [option]... [expression]...\n", PACKAGE_NAME);
printf(" option:\n");
printf(" -u --user=uid # set uid\n");
printf(" -i --interface=dev # network device (ex: eth0)\n");
printf(" -U --socket=path # unix domain socket (default: /var/run/tcpeek/tcpeek.sock)\n");
printf(" -c --checksum=[0|1|2] # ckecksum lookup mode 0=none 1=ip 2=tcp (default: 0)\n");
printf(" -t --timeout=sec # session timeout (default: 60)\n");
printf(" -B --buffer=MB # libpcap buffer size (default: 2)\n");
printf(" -l --loglevel=LEVEL # see man syslog (default: LOG_NOTICE)\n");
printf(" -q --quiet # quiet mode\n");
printf(" --promisc # enable promiscuous capture\n");
printf(" --icmp # enable icmp port unreachable lookup\n");
printf(" -h --help # help\n");
printf(" -v --version # version\n");
printf(" expression:\n");
printf(" filter:dir@addr:port[,port...]\n");
printf(" example) '%%' is the same as wildcard '*'\n");
printf(" tcpeek -i eth0 filter:IN@%%:80:443\n");
printf(" tcpeek -i eth0 filter:OUT@192.168.0.0/24:%%\n");
printf(" tcpeek -i eth0 inbound-filter:IN@%%:%% outbound-filter:OUT@192.168.0.100:%%,192.168.0.200:%%\n");
}
static void
version(void) {
printf("%s %s (with %s)\n",PACKAGE_NAME, PACKAGE_VERSION, pcap_lib_version());
}