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)
48 memset(&server, 0, sizeof(server));
49 server.sin_family = AF_INET;
50 server.sin_port = htons( port );
51 server.sin_addr.s_addr = inet_addr("127.0.0.1");
53 if(connect(ossock, (struct sockaddr *)&server, sizeof(server)) == 0)
63 /* try_to_access_ports */
64 void try_to_access_ports()
68 for(i = 0; i<= 65535; i++)
70 if(total_ports_tcp[i] && connect_to_port(IPPROTO_TCP, i))
76 snprintf(port_proto, 64, "\n %d (tcp),", i);
80 snprintf(port_proto, 64, "%d (tcp),", i);
82 strncat(open_ports_str, port_proto, open_ports_size);
83 open_ports_size -= strlen(port_proto) +1;
87 if(total_ports_udp[i] && connect_to_port(IPPROTO_UDP, i))
93 snprintf(port_proto, 64, "\n %d (udp),", i);
97 snprintf(port_proto, 64, "%d (udp),", i);
100 strncat(open_ports_str, port_proto, open_ports_size);
101 open_ports_size -= strlen(port_proto) +1;
115 /* check_open_ports: v0.1
116 * Check all open ports
118 void check_open_ports()
120 memset(open_ports_str, '\0', OS_SIZE_1024 +1);
121 open_ports_size = OS_SIZE_1024 - 1;
125 snprintf(open_ports_str, OS_SIZE_1024, "The following ports are open:");
126 open_ports_size-=strlen(open_ports_str) +1;
128 /* Testing All ports */
129 try_to_access_ports();
131 open_ports_str[strlen(open_ports_str) -1] = '\0';
133 notify_rk(ALERT_OK, open_ports_str);