1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
11 #include "headers/debug_op.h"
12 #include "headers/defs.h"
13 #include "rootcheck.h"
18 static int connect_to_port(int proto, int port);
19 static void try_to_access_ports(void);
21 /* Global variables */
22 static int _ports_open;
23 static int open_ports_size;
24 static char open_ports_str[OS_SIZE_1024 + 1];
27 static int connect_to_port(int proto, int port)
31 struct sockaddr_in server;
32 struct sockaddr_in6 server6;
34 int salen = sizeof(struct sockaddr_in6);
38 if (proto == IPPROTO_UDP) {
39 if ((ossock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
42 } else if (proto == IPPROTO_TCP) {
43 if ((ossock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
50 memset(&server, 0, sizeof(server));
51 server.sin_family = AF_INET;
52 server.sin_port = htons(port);
53 server.sin_addr.s_addr = inet_addr("127.0.0.1");
55 if (connect(ossock, (struct sockaddr *)&server, sizeof(server)) == 0) {
62 if (proto == IPPROTO_UDP) {
63 if ((ossock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
66 } else if (proto == IPPROTO_TCP) {
67 if ((ossock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP)) < 0) {
72 memset(&server6, 0, sizeof(server6));
74 WSAStringToAddress("::1", AF_INET6, NULL, (LPSOCKADDR) &server6,
77 server6.sin6_family = AF_INET6;
78 inet_pton(AF_INET6, "::1", &server6.sin6_addr.s6_addr);
80 server6.sin6_port = htons( port );
82 if(connect(ossock, (struct sockaddr *)&server6, sizeof(server6)) == 0) {
91 static void try_to_access_ports()
95 for (i = 0; i <= 65535; i++) {
96 if (total_ports_tcp[i] && connect_to_port(IPPROTO_TCP, i)) {
99 if (_ports_open == 0) {
100 snprintf(port_proto, 64, "\n %d (tcp),", i);
102 snprintf(port_proto, 64, "%d (tcp),", i);
104 strncat(open_ports_str, port_proto, open_ports_size);
105 open_ports_size -= strlen(port_proto) + 1;
110 if (total_ports_udp[i] && connect_to_port(IPPROTO_UDP, i)) {
113 if (_ports_open == 0) {
114 snprintf(port_proto, 64, "\n %d (udp),", i);
116 snprintf(port_proto, 64, "%d (udp),", i);
119 strncat(open_ports_str, port_proto, open_ports_size);
120 open_ports_size -= strlen(port_proto) + 1;
125 if (_ports_open >= 4) {
133 void check_open_ports()
136 memset(open_ports_str, '\0', OS_SIZE_1024 + 1);
137 open_ports_size = OS_SIZE_1024 - 1;
140 snprintf(open_ports_str, OS_SIZE_1024, "The following ports are open:");
141 open_ports_size -= strlen(open_ports_str) + 1;
143 /* Testing All ports */
144 try_to_access_ports();
146 open_ports_str[strlen(open_ports_str) - 1] = '\0';
148 notify_rk(ALERT_OK, open_ports_str);