1 /* @(#) $Id: ./src/rootcheck/check_open_ports.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
15 #include "headers/defs.h"
16 #include "headers/debug_op.h"
18 #include "rootcheck.h"
23 char open_ports_str[OS_SIZE_1024 + 1];
26 int connect_to_port(int proto, int port)
31 struct sockaddr_in server;
33 if(proto == IPPROTO_UDP)
35 if((ossock = socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP)) < 0)
38 else if(proto == IPPROTO_TCP)
40 if((ossock = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0)
44 memset(&server, 0, sizeof(server));
45 server.sin_family = AF_INET;
46 server.sin_port = htons( port );
47 server.sin_addr.s_addr = inet_addr("127.0.0.1");
49 if(connect(ossock, (struct sockaddr *)&server, sizeof(server)) == 0)
59 /* try_to_access_ports */
60 void try_to_access_ports()
64 for(i = 0; i<= 65535; i++)
66 if(total_ports_tcp[i] && connect_to_port(IPPROTO_TCP, i))
72 snprintf(port_proto, 64, "\n %d (tcp),", i);
76 snprintf(port_proto, 64, "%d (tcp),", i);
78 strncat(open_ports_str, port_proto, open_ports_size);
79 open_ports_size -= strlen(port_proto) +1;
83 if(total_ports_udp[i] && connect_to_port(IPPROTO_UDP, i))
89 snprintf(port_proto, 64, "\n %d (udp),", i);
93 snprintf(port_proto, 64, "%d (udp),", i);
96 strncat(open_ports_str, port_proto, open_ports_size);
97 open_ports_size -= strlen(port_proto) +1;
111 /* check_open_ports: v0.1
112 * Check all open ports
114 void check_open_ports()
116 memset(open_ports_str, '\0', OS_SIZE_1024 +1);
117 open_ports_size = OS_SIZE_1024 - 1;
121 snprintf(open_ports_str, OS_SIZE_1024, "The following ports are open:");
122 open_ports_size-=strlen(open_ports_str) +1;
124 /* Testing All ports */
125 try_to_access_ports();
127 open_ports_str[strlen(open_ports_str) -1] = '\0';
129 notify_rk(ALERT_OK, open_ports_str);