5 #include "servicefp/servicefp.h"
8 #include "output-plugins/log.h"
10 #include <libgen.h> // dirname()
12 void free_queue(); // util-cxt.c
13 extern globalconfig config
;
15 const char *u_ntop(const struct in6_addr ip_addr
, int af
, char *dest
)
21 dest
, INET_ADDRSTRLEN
+ 1)) {
22 perror("Something died in inet_ntop");
25 } else if (af
== AF_INET6
) {
26 if (!inet_ntop(AF_INET6
, &ip_addr
, dest
, INET6_ADDRSTRLEN
+ 1)) {
27 perror("Something died in inet_ntop");
33 const char *u_ntop_dst(packetinfo
*pi
, char *dest
)
35 if (pi
->af
== AF_INET
) {
39 dest
, INET_ADDRSTRLEN
+ 1)) {
40 perror("Something died in inet_ntop");
43 } else if (pi
->af
== AF_INET6
) {
44 if (!inet_ntop(AF_INET6
, &pi
->ip6
->ip_dst
, dest
, INET6_ADDRSTRLEN
+ 1)) {
45 perror("Something died in inet_ntop");
52 const char *u_ntop_src(packetinfo
*pi
, char *dest
)
54 if (pi
->af
== AF_INET
) {
58 dest
, INET_ADDRSTRLEN
+ 1)) {
59 perror("Something died in inet_ntop");
62 } else if (pi
->af
== AF_INET6
) {
63 if (!inet_ntop(AF_INET6
, &pi
->ip6
->ip_src
, dest
, INET6_ADDRSTRLEN
+ 1)) {
64 perror("Something died in inet_ntop");
71 uint8_t normalize_ttl (uint8_t ttl
)
73 if (ttl
> 128) return 255;
74 if (ttl
> 64) return 128;
75 if (ttl
> 32) return 64;
80 void unload_tcp_sigs()
82 if(config
.ctf
& CO_SYN
&& config
.sig_syn
){
83 unload_sigs(config
.sig_syn
, config
.sig_hashsize
);
85 if(config
.ctf
& CO_SYNACK
&& config
.sig_synack
){
86 unload_sigs(config
.sig_synack
, config
.sig_hashsize
);
88 if(config
.ctf
& CO_ACK
&& config
.sig_ack
){
89 unload_sigs(config
.sig_ack
, config
.sig_hashsize
);
91 if(config
.ctf
& CO_RST
&& config
.sig_rst
){
92 unload_sigs(config
.sig_rst
, config
.sig_hashsize
);
94 if(config
.ctf
& CO_FIN
&& config
.sig_fin
){
95 unload_sigs(config
.sig_fin
, config
.sig_hashsize
);
99 void print_pcap_stats()
101 if (config
.handle
== NULL
) return;
102 if (pcap_stats(config
.handle
, &config
.ps
) == -1) {
103 pcap_perror(config
.handle
, "pcap_stats");
106 olog("-- libpcap:\n");
107 olog("-- Total packets received :%12u\n",config
.ps
.ps_recv
);
108 olog("-- Total packets dropped :%12u\n",config
.ps
.ps_drop
);
109 olog("-- Total packets dropped by Interface :%12u\n",config
.ps
.ps_ifdrop
);
118 * logdir = get_abs_path(logpath);
122 * change to the directory
124 if (chdir(config
.chroot_dir
) != 0) {
125 elog("set_chroot: Can not chdir to \"%s\": %s\n", config
.chroot_dir
,
130 * always returns an absolute pathname
132 absdir
= getcwd(NULL
, 0);
135 * make the chroot call
137 if (chroot(absdir
) < 0) {
138 elog("Can not chroot to \"%s\": absolute: %s: %s\n", config
.chroot_dir
,
139 absdir
, strerror(errno
));
143 if (chdir("/") < 0) {
144 elog("Can not chdir to \"/\" after chroot: %s\n",
152 int drop_privs(long userid
, long groupid
)
156 if ((i
= setgid(groupid
)) < 0) {
157 elog("[!] Unable to set group ID: %s\n", strerror(i
));
165 if ((i
= setuid(userid
)) < 0) {
166 elog("[!] Unable to set user ID: %s\n", strerror(i
));
173 int is_valid_path(const char *path
)
181 if (stat(path
, &st
) == 0) {
182 // path already exists. is it regular and writable?
183 if (!S_ISREG(st
.st_mode
) || access(path
, W_OK
) != -1) {
191 if (stat(dir
, &st
) != 0) {
194 if (!S_ISDIR(st
.st_mode
) || access(dir
, W_OK
) == -1) {
200 int touch_pid_file(const char *path
, long uid
, long gid
)
203 fd
= open(path
, O_CREAT
, 0664);
205 rc
= fchown(fd
, uid
, gid
);
208 elog("Failed to create pid file '%s', %d\n", path
,rc
);
214 long get_gid(const char *group_name
)
219 if(!group_name
) return 0;
220 if (!isdigit(group_name
[0])) {
221 gr
= getgrnam(group_name
);
223 elog("ERROR: couldn't get ID for group %s, group does not exist.\n", group_name
);
228 return strtoul(group_name
, &endptr
, 10);
231 long get_uid(const char *user_name
, int *out_gid
)
235 if(!user_name
) return 0;
236 if (isdigit(user_name
[0]) == 0) {
237 pw
= getpwnam(user_name
);
241 *out_gid
= pw
->pw_gid
;
246 return strtoul(config
.user_name
, &endptr
, 10);
249 int create_pid_file(const char *path
)
257 path
= config
.pidfile
;
259 if (!is_valid_path(path
)) {
260 printf("PID path \"%s\" aint writable", path
);
263 if ((fd
= open(path
, O_CREAT
| O_WRONLY
,
264 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
)) == -1) {
271 lock
.l_type
= F_WRLCK
;
273 lock
.l_whence
= SEEK_SET
;
276 if (fcntl(fd
, F_SETLK
, &lock
) == -1) {
277 if (errno
== EACCES
|| errno
== EAGAIN
) {
285 snprintf(pid_buffer
, sizeof(pid_buffer
), "%d\n", (int)getpid());
286 if (ftruncate(fd
, 0) != 0) {
289 if (write(fd
, pid_buffer
, strlen(pid_buffer
)) == -1) {
303 exit(0); /* parent */
318 if ((fd
= open("/dev/null", O_RDWR
)) >= 0) {
330 char *hex2mac(const uint8_t *mac
)
335 snprintf(buf
, sizeof(buf
), "%02X:%02X:%02X:%02X:%02X:%02X",
336 (mac
[0] & 0xFF), (mac
[1] & 0xFF), (mac
[2] & 0xFF),
337 (mac
[3] & 0xFF), (mac
[4] & 0xFF), (mac
[5] & 0xFF));