6 #include "servicefp/servicefp.h"
9 #include "output-plugins/log.h"
11 #include <libgen.h> // dirname()
13 void free_queue(); // util-cxt.c
14 extern globalconfig config
;
16 const char *u_ntop(const struct in6_addr ip_addr
, int af
, char *dest
)
22 dest
, INET_ADDRSTRLEN
+ 1)) {
23 perror("Something died in inet_ntop");
26 } else if (af
== AF_INET6
) {
27 if (!inet_ntop(AF_INET6
, &ip_addr
, dest
, INET6_ADDRSTRLEN
+ 1)) {
28 perror("Something died in inet_ntop");
34 const char *u_ntop_dst(packetinfo
*pi
, char *dest
)
36 if (pi
->af
== AF_INET
) {
40 dest
, INET_ADDRSTRLEN
+ 1)) {
41 perror("Something died in inet_ntop");
44 } else if (pi
->af
== AF_INET6
) {
45 if (!inet_ntop(AF_INET6
, &pi
->ip6
->ip_dst
, dest
, INET6_ADDRSTRLEN
+ 1)) {
46 perror("Something died in inet_ntop");
53 const char *u_ntop_src(packetinfo
*pi
, char *dest
)
55 if (pi
->af
== AF_INET
) {
59 dest
, INET_ADDRSTRLEN
+ 1)) {
60 perror("Something died in inet_ntop");
63 } else if (pi
->af
== AF_INET6
) {
64 if (!inet_ntop(AF_INET6
, &pi
->ip6
->ip_src
, dest
, INET6_ADDRSTRLEN
+ 1)) {
65 perror("Something died in inet_ntop");
72 uint8_t normalize_ttl (uint8_t ttl
)
74 if (ttl
> 128) return 255;
75 if (ttl
> 64) return 128;
76 if (ttl
> 32) return 64;
80 void bucket_keys_NULL()
83 for (cxkey
= 0; cxkey
< BUCKET_SIZE
; cxkey
++) {
88 void unload_tcp_sigs()
90 if(config
.ctf
& CO_SYN
&& config
.sig_syn
){
91 unload_sigs(config
.sig_syn
, config
.sig_hashsize
);
93 if(config
.ctf
& CO_SYNACK
&& config
.sig_synack
){
94 unload_sigs(config
.sig_synack
, config
.sig_hashsize
);
96 if(config
.ctf
& CO_ACK
&& config
.sig_ack
){
97 unload_sigs(config
.sig_ack
, config
.sig_hashsize
);
99 if(config
.ctf
& CO_RST
&& config
.sig_rst
){
100 unload_sigs(config
.sig_rst
, config
.sig_hashsize
);
102 if(config
.ctf
& CO_FIN
&& config
.sig_fin
){
103 unload_sigs(config
.sig_fin
, config
.sig_hashsize
);
107 void print_pcap_stats()
109 if (config
.handle
== NULL
) return;
110 if (pcap_stats(config
.handle
, &config
.ps
) == -1) {
111 pcap_perror(config
.handle
, "pcap_stats");
114 olog("-- libpcap:\n");
115 olog("-- Total packets received :%12u\n",config
.ps
.ps_recv
);
116 olog("-- Total packets dropped :%12u\n",config
.ps
.ps_drop
);
117 olog("-- Total packets dropped by Interface :%12u\n",config
.ps
.ps_ifdrop
);
127 * logdir = get_abs_path(logpath);
131 * change to the directory
133 if (chdir(config
.chroot_dir
) != 0) {
134 elog("set_chroot: Can not chdir to \"%s\": %s\n", config
.chroot_dir
,
139 * always returns an absolute pathname
141 absdir
= getcwd(NULL
, 0);
142 abslen
= strlen(absdir
);
145 * make the chroot call
147 if (chroot(absdir
) < 0) {
148 elog("Can not chroot to \"%s\": absolute: %s: %s\n", config
.chroot_dir
,
149 absdir
, strerror(errno
));
153 if (chdir("/") < 0) {
154 elog("Can not chdir to \"/\" after chroot: %s\n",
170 unsigned long groupid
= 0;
171 unsigned long userid
= 0;
173 if (config
.group_name
!= NULL
) {
175 if (!isdigit(config
.group_name
[0])) {
176 gr
= getgrnam(config
.group_name
);
178 if(config
.chroot_dir
){
179 elog("ERROR: you have chrootetd and must set numeric group ID.\n");
182 elog("ERROR: couldn't get ID for group %s, group does not exist.", config
.group_name
)
186 groupid
= gr
->gr_gid
;
188 groupid
= strtoul(config
.group_name
, &endptr
, 10);
192 if (config
.user_name
!= NULL
) {
195 if (isdigit(config
.user_name
[0]) == 0) {
196 pw
= getpwnam(config
.user_name
);
200 printf("[E] User %s not found!\n", config
.user_name
);
203 userid
= strtoul(config
.user_name
, &endptr
, 10);
204 pw
= getpwuid(userid
);
207 if (config
.group_name
== NULL
&& pw
!= NULL
) {
208 groupid
= pw
->pw_gid
;
213 if ((i
= setgid(groupid
)) < 0) {
214 printf("Unable to set group ID: %s", strerror(i
));
222 if (getuid() == 0 && initgroups(config
.user_name
, groupid
) < 0) {
223 printf("Unable to init group names (%s/%lu)", config
.user_name
,
226 if ((i
= setuid(userid
)) < 0) {
227 printf("Unable to set user ID: %s\n", strerror(i
));
233 int is_valid_path(const char *path
)
242 memcpy(dir
, path
, strnlen(path
, STDBUF
));
245 if (stat(dir
, &st
) != 0) {
248 if (!S_ISDIR(st
.st_mode
) || access(dir
, W_OK
) == -1) {
254 int create_pid_file(const char *path
)
262 path
= config
.pidfile
;
264 if (!is_valid_path(path
)) {
265 printf("PID path \"%s\" aint writable", path
);
268 if ((fd
= open(path
, O_CREAT
| O_WRONLY
,
269 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
)) == -1) {
276 lock
.l_type
= F_WRLCK
;
278 lock
.l_whence
= SEEK_SET
;
281 if (fcntl(fd
, F_SETLK
, &lock
) == -1) {
282 if (errno
== EACCES
|| errno
== EAGAIN
) {
290 snprintf(pid_buffer
, sizeof(pid_buffer
), "%d\n", (int)getpid());
291 if (ftruncate(fd
, 0) != 0) {
294 if (write(fd
, pid_buffer
, strlen(pid_buffer
)) != 0) {
308 exit(0); /* parent */
311 config
.use_syslog
= 1;
324 if ((fd
= open("/dev/null", O_RDWR
)) >= 0) {
333 if (config
.pidfile
) {
334 return create_pid_file(config
.pidfile
);
340 char *hex2mac(const uint8_t *mac
)
345 snprintf(buf
, sizeof(buf
), "%02X:%02X:%02X:%02X:%02X:%02X",
346 (mac
[0] & 0xFF), (mac
[1] & 0xFF), (mac
[2] & 0xFF),
347 (mac
[3] & 0xFF), (mac
[4] & 0xFF), (mac
[5] & 0xFF));