-
Notifications
You must be signed in to change notification settings - Fork 377
/
Copy pathpassivedns.c
1603 lines (1406 loc) · 45.6 KB
/
passivedns.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** This file is a part of PassiveDNS.
**
** Copyright (C) 2010-2013, Edward Fjellskål <edwardfjellskaal@gmail.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 2 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, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
*/
/* I N C L U D E S **********************************************************/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <signal.h>
#include <pcap.h>
#include <getopt.h>
#include <time.h>
#include <sys/types.h>
#include <grp.h>
#include <pwd.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syslog.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <limits.h>
#include "passivedns.h"
#include "dns.h"
#ifdef HAVE_JSON
#include <jansson.h>
#endif /* HAVE_JSON */
#ifdef HAVE_PFRING
#include <pfring.h>
#endif /* HAVE_PFRING */
#ifndef CONFDIR
#define CONFDIR "/etc/passivedns/"
#endif
/* G L O B A L S *** (or candidates for refactoring, as we say)***********/
globalconfig config;
connection *bucket[BUCKET_SIZE];
static volatile sig_atomic_t signal_reopen_log_files = 0;
/* I N T E R N A L P R O T O T Y P E S ***********************************/
static void usage();
static void show_version();
void check_vlan (packetinfo *pi);
void prepare_null (packetinfo *pi);
void prepare_raw (packetinfo *pi);
void prepare_sll (packetinfo *pi);
void prepare_eth (packetinfo *pi);
void prepare_ip4 (packetinfo *pi);
void prepare_ip4ip (packetinfo *pi);
void prepare_ip6 (packetinfo *pi);
void prepare_ip6ip (packetinfo *pi);
void prepare_udp (packetinfo *pi);
void prepare_tcp (packetinfo *pi);
void parse_eth (packetinfo *pi);
void parse_ip4 (packetinfo *pi);
void parse_ip6 (packetinfo *pi);
void parse_udp (packetinfo *pi);
void parse_tcp (packetinfo *pi);
const char *u_ntop_src(packetinfo *pi, char *dest);
void set_pkt_end_ptr (packetinfo *pi);
void check_interrupt();
void end_sessions();
void set_end_sessions();
void set_end_dns_records();
void cxt_init();
int connection_tracking(packetinfo *pi);
connection *cxt_new(packetinfo *pi);
void del_connection(connection *, connection **);
void print_pdns_stats();
void free_config();
void reopen_log_files();
void game_over ();
void got_packet(u_char *useless, const struct pcap_pkthdr *pheader,
const u_char *packet);
#ifdef HAVE_PFRING
void pfring_got_packet(const struct pfring_pkthdr *pfheader,
const u_char *packet, const u_char *useless);
#endif /* HAVE_PFRING */
/* F U N C T I O N S ********************************************************/
#ifdef HAVE_PFRING
void pfring_got_packet(const struct pfring_pkthdr *pfheader,
const u_char *packet, const u_char *useless)
{
/* pcap_pkthdr and pfring_pkthdr are identical to each other*/
struct pcap_pkthdr *pheader = (struct pcap_pkthdr *)pfheader;
/* Set timestamp if it's not set */
if (pheader->ts.tv_sec == 0)
pheader->ts.tv_sec = time(NULL);
/* pfring_loop orders the arguments differently than pcap_loop */
got_packet((u_char *)useless, (const struct pcap_pkthdr *)pheader, packet);
}
#endif /* HAVE_PFRING */
void got_packet(u_char *useless, const struct pcap_pkthdr *pheader,
const u_char *packet)
{
config.p_s.got_packets++;
packetinfo pstruct = {0};
packetinfo *pi = &pstruct;
pi->packet = packet;
pi->pheader = pheader;
set_pkt_end_ptr (pi);
memcpy( &config.tstamp, &pi->pheader->ts, sizeof( struct timeval ) ); /* Global */
if (signal_reopen_log_files)
reopen_log_files();
if (config.intr_flag != 0) {
check_interrupt();
}
config.inpacket = 1;
switch (config.linktype) {
case DLT_NULL:
prepare_null(pi);
break;
case DLT_RAW:
prepare_raw(pi);
break;
#ifdef DLT_LINUX_SLL
case DLT_LINUX_SLL:
prepare_sll(pi);
break;
#endif
default:
prepare_eth(pi);
check_vlan(pi);
break;
}
switch (pi->eth_type) {
case ETHERNET_TYPE_IP:
prepare_ip4(pi);
parse_ip4(pi);
break;
case ETHERNET_TYPE_IPV6:
prepare_ip6(pi);
parse_ip6(pi);
break;
default:
config.p_s.otherl_recv++;
//vlog(0x3, "[*] ETHERNET TYPE : %x\n",pi->eth_hdr->eth_ip_type);
break;
}
config.inpacket = 0;
}
void prepare_null(packetinfo *pi)
{
pi->eth_hlen = LOOPBACK_HDR_LEN;
if ((u_int32_t)*pi->packet == AF_INET) {
pi->eth_type = ETHERNET_TYPE_IP;
} else {
pi->eth_type = ETHERNET_TYPE_IPV6;
}
}
void prepare_raw(packetinfo *pi)
{
pi->eth_hlen = 0;
if (IP_V((ip4_header *)pi->packet) == 4)
pi->eth_type = ETHERNET_TYPE_IP;
else
pi->eth_type = ETHERNET_TYPE_IPV6;
}
void prepare_sll(packetinfo *pi)
{
pi->eth_hlen = SLL_HDR_LEN;
if (IP_V((ip4_header *)(pi->packet + SLL_HDR_LEN)) == 4)
pi->eth_type = ETHERNET_TYPE_IP;
else
pi->eth_type = ETHERNET_TYPE_IPV6;
}
void prepare_eth(packetinfo *pi)
{
if (pi->packet + ETHERNET_HEADER_LEN > pi->end_ptr)
return;
config.p_s.eth_recv++;
pi->eth_hdr = (ether_header *) (pi->packet);
pi->eth_type = ntohs(pi->eth_hdr->eth_ip_type);
pi->eth_hlen = ETHERNET_HEADER_LEN;
}
void check_vlan(packetinfo *pi)
{
if (pi->eth_type == ETHERNET_TYPE_8021Q) {
vlog(0x3, "[*] ETHERNET TYPE 8021Q\n");
config.p_s.vlan_recv++;
pi->vlan = pi->eth_hdr->eth_8_vid;
pi->eth_type = ntohs(pi->eth_hdr->eth_8_ip_type);
pi->eth_hlen += 4;
if ( pi->eth_type == ETHERNET_TYPE_8021Q ) {
vlog(0x3, "[*] ETHERNET TYPE 8021Q in 8021Q\n");
pi->eth_type = ntohs(pi->eth_hdr->eth_82_ip_type);
pi->eth_hlen += 4;
pi->vlan = pi->eth_hdr->eth_82_vid;
}
}
else if (pi->eth_type == (ETHERNET_TYPE_802Q1MT | ETHERNET_TYPE_802Q1MT2 |
ETHERNET_TYPE_802Q1MT3 | ETHERNET_TYPE_8021AD)) {
vlog(0x3, "[*] ETHERNET TYPE 802Q1MT\n");
pi->mvlan = pi->eth_hdr->eth_82_mvid;
pi->eth_type = ntohs(pi->eth_hdr->eth_82_ip_type);
pi->eth_hlen += 8;
}
}
void prepare_ip4(packetinfo *pi)
{
config.p_s.ip4_recv++;
pi->af = AF_INET;
pi->ip4 = (ip4_header *) (pi->packet + pi->eth_hlen);
pi->packet_bytes = (pi->ip4->ip_len - (IP_HL(pi->ip4) * 4));
}
void parse_ip4(packetinfo *pi)
{
/* Paranoia */
if (((pi->packet + pi->eth_hlen) + (IP_HL(pi->ip4) * 4)) > pi->end_ptr) {
dlog("[D] Refusing to parse IPv4 packet: IPv4-hdr passed end_ptr\n");
return;
}
switch (pi->ip4->ip_p) {
case IP_PROTO_TCP:
prepare_tcp(pi);
parse_tcp(pi);
break;
case IP_PROTO_UDP:
prepare_udp(pi);
parse_udp(pi);
break;
case IP_PROTO_IP4:
prepare_ip4ip(pi);
break;
case IP_PROTO_IP6:
prepare_ip4ip(pi);
break;
default:
break;
}
}
void prepare_ip6ip(packetinfo *pi)
{
packetinfo pipi;
memset(&pipi, 0, sizeof(packetinfo));
config.p_s.ip6ip_recv++;
pipi.pheader = pi->pheader;
pipi.packet = (pi->packet + pi->eth_hlen + IP6_HEADER_LEN);
pipi.end_ptr = pi->end_ptr;
if (pi->ip6->next == IP_PROTO_IP4) {
prepare_ip4(&pipi);
parse_ip4(&pipi);
}
else {
prepare_ip6(&pipi);
parse_ip6(&pipi);
}
}
void prepare_ip4ip(packetinfo *pi)
{
packetinfo pipi;
memset(&pipi, 0, sizeof(packetinfo));
config.p_s.ip4ip_recv++;
pipi.pheader = pi->pheader;
pipi.packet = (pi->packet + pi->eth_hlen + (IP_HL(pi->ip4) * 4));
pipi.end_ptr = pi->end_ptr;
if (pi->ip4->ip_p == IP_PROTO_IP4) {
prepare_ip4(&pipi);
parse_ip4(&pipi);
}
else {
prepare_ip6(&pipi);
parse_ip6(&pipi);
}
}
void prepare_ip6(packetinfo *pi)
{
config.p_s.ip6_recv++;
pi->af = AF_INET6;
pi->ip6 = (ip6_header *) (pi->packet + pi->eth_hlen);
pi->packet_bytes = pi->ip6->len;
}
void parse_ip6(packetinfo *pi)
{
switch (pi->ip6->next) {
case IP_PROTO_TCP:
prepare_tcp(pi);
parse_tcp(pi);
break;
case IP_PROTO_UDP:
prepare_udp(pi);
parse_udp(pi);
break;
case IP_PROTO_IP4:
prepare_ip6ip(pi);
break;
case IP_PROTO_IP6:
prepare_ip6ip(pi);
break;
default:
break;
}
}
void set_pkt_end_ptr(packetinfo *pi)
{
/* Paranoia! */
if (pi->pheader->len <= SNAPLENGTH)
pi->end_ptr = (pi->packet + pi->pheader->len);
else
pi->end_ptr = (pi->packet + SNAPLENGTH);
}
void prepare_tcp(packetinfo *pi)
{
config.p_s.tcp_recv++;
if (pi->af == AF_INET) {
vlog(0x3, "[*] IPv4 PROTOCOL TYPE TCP:\n");
pi->tcph = (tcp_header *) (pi->packet + pi->eth_hlen +
(IP_HL(pi->ip4) * 4));
pi->plen = (pi->pheader->caplen - (TCP_OFFSET(pi->tcph)) * 4 -
(IP_HL(pi->ip4) * 4) - pi->eth_hlen);
pi->payload = (pi->packet + pi->eth_hlen + (IP_HL(pi->ip4) * 4) +
(TCP_OFFSET(pi->tcph) * 4));
}
else if (pi->af == AF_INET6) {
vlog(0x3, "[*] IPv6 PROTOCOL TYPE TCP:\n");
pi->tcph = (tcp_header *) (pi->packet + pi->eth_hlen + IP6_HEADER_LEN);
pi->plen = (pi->pheader->caplen - (TCP_OFFSET(pi->tcph)) * 4 -
IP6_HEADER_LEN - pi->eth_hlen);
pi->payload = (pi->packet + pi->eth_hlen + IP6_HEADER_LEN +
(TCP_OFFSET(pi->tcph)*4));
}
pi->proto = IP_PROTO_TCP;
pi->s_port = pi->tcph->src_port;
pi->d_port = pi->tcph->dst_port;
connection_tracking(pi);
}
void prepare_udp(packetinfo *pi)
{
config.p_s.udp_recv++;
if (pi->af == AF_INET) {
vlog(0x3, "[*] IPv4 PROTOCOL TYPE UDP:\n");
pi->udph = (udp_header *) (pi->packet + pi->eth_hlen +
(IP_HL(pi->ip4) * 4));
pi->plen = pi->pheader->caplen - UDP_HEADER_LEN -
(IP_HL(pi->ip4) * 4) - pi->eth_hlen;
pi->payload = (pi->packet + pi->eth_hlen +
(IP_HL(pi->ip4) * 4) + UDP_HEADER_LEN);
}
else if (pi->af == AF_INET6) {
vlog(0x3, "[*] IPv6 PROTOCOL TYPE UDP:\n");
pi->udph = (udp_header *) (pi->packet + pi->eth_hlen +
IP6_HEADER_LEN);
pi->plen = pi->pheader->caplen - UDP_HEADER_LEN -
IP6_HEADER_LEN - pi->eth_hlen;
pi->payload = (pi->packet + pi->eth_hlen +
IP6_HEADER_LEN + UDP_HEADER_LEN);
}
pi->proto = IP_PROTO_UDP;
pi->s_port = pi->udph->src_port;
pi->d_port = pi->udph->dst_port;
connection_tracking(pi);
}
void parse_tcp(packetinfo *pi)
{
if (pi->plen <= 0) return;
/* Reliable traffic comes from the servers (normally on port 53 or 5353)
* and the client has sent at least one packet on that
* connecton (Maybe asking for an aswer :) */
dlog("[D] Parsing TCP packet...\n");
dns_parser(pi);
}
void parse_udp(packetinfo *pi)
{
if (pi->plen <= 0) return;
/* Reliable traffic comes from the servers (normally on port 53 or 5353)
* and the client has sent at least one packet on that
* connecton (Maybe asking for an aswer :) */
dlog("[D] Parsing UDP packet...\n");
dns_parser(pi);
}
int connection_tracking(packetinfo *pi)
{
struct in6_addr *ip_src;
struct in6_addr *ip_dst;
struct in6_addr ips;
struct in6_addr ipd;
uint16_t src_port = pi->s_port;
uint16_t dst_port = pi->d_port;
int af = pi->af;
connection *cxt = NULL;
connection *head = NULL;
uint32_t hash;
if(af == AF_INET6){
ip_src = &PI_IP6SRC(pi);
ip_dst = &PI_IP6DST(pi);
}
else {
#ifdef BSD_DERIVED
ips.__u6_addr.__u6_addr32[0] = pi->ip4->ip_src;
ipd.__u6_addr.__u6_addr32[0] = pi->ip4->ip_dst;
#else
ips.s6_addr32[0] = pi->ip4->ip_src;
ipd.s6_addr32[0] = pi->ip4->ip_dst;
#endif
ip_src = &ips;
ip_dst = &ipd;
}
/* Find the right connection bucket */
if (af == AF_INET)
hash = CXT_HASH4(IP4ADDR(ip_src), IP4ADDR(ip_dst), src_port, dst_port,
pi->proto);
else if (af == AF_INET6)
hash = CXT_HASH6(ip_src, ip_dst, src_port, dst_port, pi->proto);
else {
dlog("[D] Only CTX with AF_INET and AF_INET6 are supported: %d\n", af);
return 0;
}
cxt = bucket[hash];
head = cxt;
/* Search through the bucket */
while (cxt != NULL)
{
/* Two-way compare of given connection against connection table */
if (af == AF_INET) {
if (CMP_CXT4(cxt, IP4ADDR(ip_src), src_port, IP4ADDR(ip_dst), dst_port)) {
/* Client sends first packet (TCP/SYN - UDP?) hence this is a client */
dlog("[D] Found existing v4 client connection.\n");
return cxt_update_client(cxt, pi);
}
else if (CMP_CXT4(cxt, IP4ADDR(ip_dst), dst_port, IP4ADDR(ip_src), src_port)) {
if (pi->sc == SC_SERVER) {
/* This is a server */
dlog("[D] Found existing v4 server connection.\n");
return cxt_update_server(cxt, pi);
}
else {
/* This is a client, where we saw a mid-stream DNS response first */
dlog("[D] Found existing unknown v4 server connection.\n");
return cxt_update_client(cxt, pi);
}
}
}
else if (af == AF_INET6) {
if (CMP_CXT6(cxt, ip_src, src_port, ip_dst, dst_port)) {
dlog("[D] Found existing v6 client connection.\n");
return cxt_update_client(cxt, pi);
}
else if (CMP_CXT6(cxt, ip_dst, dst_port, ip_src, src_port)) {
dlog("[D] Found existing v6 client connection.\n");
return cxt_update_server(cxt, pi);
}
}
cxt = cxt->next;
}
/* Bucket turned upside down didn't yield anything. New connection */
dlog("[D] New connection.\n");
cxt = cxt_new(pi);
/* New connections are pushed on to the head of bucket[s_hash] */
cxt->next = head;
if (head != NULL)
/* Are we double linked? */
head->prev = cxt;
bucket[hash] = cxt;
pi->cxt = cxt;
return cxt_update_unknown(cxt, pi);
}
/* Freshly smelling connection :d */
connection *cxt_new(packetinfo *pi)
{
struct in6_addr ips;
struct in6_addr ipd;
connection *cxt;
config.cxtrackerid++;
cxt = (connection *) calloc(1, sizeof(connection));
cxt->cxid = config.cxtrackerid;
cxt->af = pi->af;
if (pi->tcph)
cxt->s_tcpFlags |= pi->tcph->t_flags;
cxt->start_time = pi->pheader->ts.tv_sec;
cxt->last_pkt_time = pi->pheader->ts.tv_sec;
if (pi->af == AF_INET6){
cxt->s_ip = PI_IP6SRC(pi);
cxt->d_ip = PI_IP6DST(pi);
}
else {
#ifdef BSD_DERIVED
ips.__u6_addr.__u6_addr32[0] = pi->ip4->ip_src;
ipd.__u6_addr.__u6_addr32[0] = pi->ip4->ip_dst;
#else
ips.s6_addr32[0] = pi->ip4->ip_src;
ipd.s6_addr32[0] = pi->ip4->ip_dst;
#endif
cxt->s_ip = ips;
cxt->d_ip = ipd;
}
cxt->s_port = pi->s_port;
cxt->d_port = pi->d_port;
cxt->proto = pi->proto;
cxt->check = 0x00;
cxt->reversed = 0;
config.curcxt++;
return cxt;
}
int cxt_update_client(connection *cxt, packetinfo *pi)
{
cxt->last_pkt_time = pi->pheader->ts.tv_sec;
if (pi->tcph)
cxt->s_tcpFlags |= pi->tcph->t_flags;
cxt->s_total_bytes += pi->packet_bytes;
cxt->s_total_pkts += 1;
pi->cxt = cxt;
pi->sc = SC_CLIENT;
if (cxt->s_total_bytes > MAX_BYTE_CHECK ||
cxt->s_total_pkts > MAX_PKT_CHECK) {
return 0; /* Don't Check! */
}
return SC_CLIENT;
}
int cxt_update_unknown(connection *cxt, packetinfo *pi)
{
cxt->last_pkt_time = pi->pheader->ts.tv_sec;
if (pi->tcph)
cxt->s_tcpFlags |= pi->tcph->t_flags;
cxt->s_total_bytes += pi->packet_bytes;
cxt->s_total_pkts += 1;
pi->cxt = cxt;
pi->sc = SC_UNKNOWN;
if (cxt->s_total_bytes > MAX_BYTE_CHECK ||
cxt->s_total_pkts > MAX_PKT_CHECK) {
return 0; /* Don't Check! */
}
return SC_UNKNOWN;
}
int cxt_update_server(connection *cxt, packetinfo *pi)
{
cxt->last_pkt_time = pi->pheader->ts.tv_sec;
if (pi->tcph)
cxt->d_tcpFlags |= pi->tcph->t_flags;
cxt->d_total_bytes += pi->packet_bytes;
cxt->d_total_pkts += 1;
pi->cxt = cxt;
pi->sc = SC_SERVER;
if (cxt->d_total_bytes > MAX_BYTE_CHECK ||
cxt->d_total_pkts > MAX_PKT_CHECK)
return 0; /* Don't check! */
return SC_SERVER;
}
void end_all_sessions()
{
connection *cxt;
int cxkey;
config.llcxt = 0;
for (cxkey = 0; cxkey < BUCKET_SIZE; cxkey++)
{
cxt = bucket[cxkey];
while (cxt != NULL)
{
config.llcxt++;
if (cxt->prev)
cxt->prev->next = cxt->next;
if (cxt->next)
cxt->next->prev = cxt->prev;
connection *tmp = cxt;
cxt = cxt->next;
del_connection(tmp, &bucket[cxkey]);
if (cxt == NULL)
bucket[cxkey] = NULL;
}
}
dlog("CXT in list before cleaning: %10u\n", config.llcxt);
dlog("CXT in list after cleaning: %10u\n", config.curcxt);
}
void end_sessions()
{
connection *cxt;
time_t check_time;
check_time = config.tstamp.tv_sec;
int ended, expired = 0;
config.llcxt = 0;
int iter;
for (iter = 0; iter < BUCKET_SIZE; iter++)
{
cxt = bucket[iter];
while (cxt != NULL)
{
ended = 0;
config.llcxt++;
/* TCP */
if (cxt->proto == IP_PROTO_TCP) {
/* FIN from both sides */
if (cxt->s_tcpFlags & TF_FIN && cxt->d_tcpFlags & TF_FIN
&& (check_time - cxt->last_pkt_time) > 5)
ended = 1;
/* RST from either side */
else if ((cxt->s_tcpFlags & TF_RST || cxt->d_tcpFlags & TF_RST)
&& (check_time - cxt->last_pkt_time) > 5)
ended = 1;
else if ((check_time - cxt->last_pkt_time) > TCP_TIMEOUT)
expired = 1;
}
/* UDP */
else if (cxt->proto == IP_PROTO_UDP
&& (check_time - cxt->last_pkt_time) > UDP_TIMEOUT)
expired = 1;
/* ICMP */
else if (cxt->proto == IP_PROTO_ICMP ||
cxt->proto == IP6_PROTO_ICMP) {
if ((check_time - cxt->last_pkt_time) > ICMP_TIMEOUT)
expired = 1;
}
/* All other protocols */
else if ((check_time - cxt->last_pkt_time) > OTHER_TIMEOUT)
expired = 1;
if (ended == 1 || expired == 1) {
/* Remove from the hash */
if (cxt->prev)
cxt->prev->next = cxt->next;
if (cxt->next)
cxt->next->prev = cxt->prev;
connection *tmp = cxt;
connection *tmp_pre = cxt->prev;
ended = expired = 0;
cxt = cxt->next;
del_connection(tmp, &bucket[iter]);
if (cxt == NULL && tmp_pre == NULL)
bucket[iter] = NULL;
}
else
cxt = cxt->next;
}
}
dlog("CXT in list before cleaning: %10u\n", config.llcxt);
dlog("CXT in list after cleaning: %10u\n", config.curcxt);
}
void del_connection(connection * cxt, connection ** bucket_ptr)
{
connection *prev = cxt->prev; /* Older connections */
connection *next = cxt->next; /* Newer connections */
if (prev == NULL) {
/* Beginning of list */
*bucket_ptr = next;
/* Not only entry */
if (next)
next->prev = NULL;
}
else if (next == NULL) {
/* At end of list! */
prev->next = NULL;
}
else {
/* A node */
prev->next = next;
next->prev = prev;
}
/* Free and set to NULL */
free(cxt);
cxt = NULL;
config.curcxt--;
}
const char *u_ntop_src(packetinfo *pi, char *dest)
{
if (pi->af == AF_INET) {
if (!inet_ntop(AF_INET, &pi->ip4->ip_src, dest, INET_ADDRSTRLEN + 1)) {
perror("Something died in inet_ntop");
return NULL;
}
}
else if (pi->af == AF_INET6) {
if (!inet_ntop(AF_INET6, &pi->ip6->ip_src, dest,
INET6_ADDRSTRLEN + 1)) {
perror("Something died in inet_ntop");
return NULL;
}
}
return dest;
}
void check_interrupt()
{
dlog("[D] In interrupt. Flag: %d\n",config.intr_flag);
if (ISSET_INTERRUPT_END(config))
game_over();
else if (ISSET_INTERRUPT_SESSION(config))
set_end_sessions();
else if (ISSET_INTERRUPT_DNS(config))
set_end_dns_records();
else
config.intr_flag = 0;
}
void sig_alarm_handler()
{
time_t now_t;
now_t = config.tstamp.tv_sec;
dlog("[D] Got SIG ALRM: %lu\n", now_t);
/* Each time check for timed out sessions */
set_end_sessions();
/* Only check for timed-out dns records each 10 minutes */
if ((now_t - config.dnslastchk) >= 600) {
set_end_dns_records();
}
alarm(TIMEOUT);
}
void sig_hup_handler()
{
signal_reopen_log_files = 1;
}
void reopen_log_files()
{
if (config.output_log) {
if (config.logfile_fd != NULL && config.logfile_fd != stdout)
fclose(config.logfile_fd);
config.logfile_fd = fopen(config.logfile, "a");
}
if (config.output_log_nxd) {
if (config.logfile_all) {
/* Do nothing, since both logs use the same file */
}
else {
if (config.logfile_nxd_fd != NULL && config.logfile_nxd_fd != stdout)
fclose(config.logfile_nxd_fd);
config.logfile_nxd_fd = fopen(config.logfile_nxd, "a");
}
}
signal_reopen_log_files = 0;
}
void set_end_dns_records()
{
config.intr_flag |= INTERRUPT_DNS;
if (config.inpacket == 0) {
expire_dns_records();
config.dnslastchk = config.tstamp.tv_sec;
config.intr_flag &= ~INTERRUPT_DNS;
}
}
void set_end_sessions()
{
config.intr_flag |= INTERRUPT_SESSION;
if (config.inpacket == 0) {
end_sessions();
config.intr_flag &= ~INTERRUPT_SESSION;
}
}
static int set_chroot(void)
{
char *absdir;
/* Change to the directory */
if (chdir(config.chroot_dir) != 0)
printf("set_chroot: Can not chdir to \"%s\": %s\n",
config.chroot_dir,strerror(errno));
/* Always returns an absolute pathname */
absdir = getcwd(NULL, 0);
/* Make the chroot call */
if (chroot(absdir) < 0)
printf("Could not chroot to \"%s\": absolute: %s: %s\n",
config.chroot_dir, absdir, strerror(errno));
if (chdir("/") < 0)
printf("Could not chdir to \"/\" after chroot: %s\n", strerror(errno));
return 0;
}
int drop_privs(void)
{
struct group *gr;
struct passwd *pw;
char *endptr;
int i;
int do_setuid = 0;
int do_setgid = 0;
unsigned long groupid = 0;
unsigned long userid = 0;
if (config.group_name != NULL) {
do_setgid = 1;
if (!isdigit(config.group_name[0])) {
gr = getgrnam(config.group_name);
if (!gr) {
if (config.chroot_dir) {
elog("ERROR: you have chrooted and must set numeric group ID.\n");
exit(1);
}
else {
elog("ERROR: couldn't get ID for group %s, group does not exist.", config.group_name);
exit(1);
}
}
groupid = gr->gr_gid;
}
else {
groupid = strtoul(config.group_name, &endptr, 10);
}
}
if (config.user_name != NULL) {
do_setuid = 1;
do_setgid = 1;
if (isdigit(config.user_name[0]) == 0) {
pw = getpwnam(config.user_name);
if (pw != NULL) {
userid = pw->pw_uid;
}
else {
printf("[E] User %s not found!\n", config.user_name);
}
}
else {
userid = strtoul(config.user_name, &endptr, 10);
pw = getpwuid(userid);
}
if (config.group_name == NULL && pw != NULL)
groupid = pw->pw_gid;
}
if (do_setgid) {
if ((i = setgid(groupid)) < 0)
printf("Unable to set group ID: %s", strerror(i));
}
endgrent();
endpwent();
if (do_setuid) {
if (getuid() == 0 && initgroups(config.user_name, groupid) < 0)
printf("Unable to init group names (%s/%lu)", config.user_name,
groupid);
if ((i = setuid(userid)) < 0)
printf("Unable to set user ID: %s\n", strerror(i));
}
return 0;
}
int is_valid_path(const char *path)
{
char dir[STDBUF];
struct stat st;
if (path == NULL)
return 0;
memcpy(dir, path, strnlen(path, STDBUF));
dirname(dir);
if (stat(dir, &st) != 0)
return 0;
if (!S_ISDIR(st.st_mode) || access(dir, W_OK) == -1)
return 0;
return 1;
}
int create_pid_file(const char *path)
{
char pid_buffer[12];
struct flock lock;
int fd;
if (!path)
path = config.pidfile;
if (!is_valid_path(path))
printf("PID path \"%s\" aint writable", path);
if ((fd = open(path, O_CREAT | O_WRONLY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
return ERROR;
/* PID file locking */
lock.l_type = F_WRLCK;
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
if (fcntl(fd, F_SETLK, &lock) == -1) {
close(fd);
return ERROR;
}
snprintf(pid_buffer, sizeof(pid_buffer), "%d\n", (int)getpid());
if (ftruncate(fd, 0) != 0) {
close(fd);
return ERROR;