1 /* @(#) $Id: check_open_ports.c,v 1.8 2009/06/24 18:53:07 dcid Exp $ */
3 /* Copyright (C) 2009 Trend Micro Inc.
6 * This program is a free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License (version 3) as published by the FSF - Free Software
14 #include "headers/defs.h"
15 #include "headers/debug_op.h"
17 #include "rootcheck.h"
22 char open_ports_str[OS_SIZE_1024 + 1];
25 int connect_to_port(int proto, int port)
30 struct sockaddr_in server;
32 if(proto == IPPROTO_UDP)
34 if((ossock = socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP)) < 0)
37 else if(proto == IPPROTO_TCP)
39 if((ossock = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0)
43 memset(&server, 0, sizeof(server));
44 server.sin_family = AF_INET;
45 server.sin_port = htons( port );
46 server.sin_addr.s_addr = inet_addr("127.0.0.1");
48 if(connect(ossock, (struct sockaddr *)&server, sizeof(server)) == 0)
58 /* try_to_access_ports */
59 void try_to_access_ports()
63 for(i = 0; i<= 65535; i++)
65 if(total_ports_tcp[i] && connect_to_port(IPPROTO_TCP, i))
71 snprintf(port_proto, 64, "\n %d (tcp),", i);
75 snprintf(port_proto, 64, "%d (tcp),", i);
77 strncat(open_ports_str, port_proto, open_ports_size);
78 open_ports_size -= strlen(port_proto) +1;
82 if(total_ports_udp[i] && connect_to_port(IPPROTO_UDP, i))
88 snprintf(port_proto, 64, "\n %d (udp),", i);
92 snprintf(port_proto, 64, "%d (udp),", i);
95 strncat(open_ports_str, port_proto, open_ports_size);
96 open_ports_size -= strlen(port_proto) +1;
110 /* check_open_ports: v0.1
111 * Check all open ports
113 void check_open_ports()
115 memset(open_ports_str, '\0', OS_SIZE_1024 +1);
116 open_ports_size = OS_SIZE_1024 - 1;
120 snprintf(open_ports_str, OS_SIZE_1024, "The following ports are open:");
121 open_ports_size-=strlen(open_ports_str) +1;
123 /* Testing All ports */
124 try_to_access_ports();
126 open_ports_str[strlen(open_ports_str) -1] = '\0';
128 notify_rk(ALERT_OK, open_ports_str);