Imported Upstream version 2.7
[ossec-hids.git] / src / rootcheck / check_open_ports.c
1 /* @(#) $Id: ./src/rootcheck/check_open_ports.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All right reserved.
6  *
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
10  * Foundation
11  */
12
13
14 #include "shared.h"
15 #include "headers/defs.h"
16 #include "headers/debug_op.h"
17
18 #include "rootcheck.h"
19
20
21 int _ports_open;
22 int open_ports_size;
23 char open_ports_str[OS_SIZE_1024 + 1];
24
25 /* connect_to_port */
26 int connect_to_port(int proto, int port)
27 {
28     int rc = 0;
29
30     int ossock;
31     struct sockaddr_in server;
32
33     if(proto == IPPROTO_UDP)
34     {
35         if((ossock = socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP)) < 0)
36             return(0);
37     }
38     else if(proto == IPPROTO_TCP)
39     {
40         if((ossock = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0)
41             return(0);
42     }
43
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");
48
49     if(connect(ossock, (struct sockaddr *)&server, sizeof(server)) == 0)
50     {
51         rc = 1;
52     }
53
54     close(ossock);
55
56     return(rc);
57 }
58
59 /* try_to_access_ports */
60 void try_to_access_ports()
61 {
62     int i;
63
64     for(i = 0; i<= 65535; i++)
65     {
66         if(total_ports_tcp[i] && connect_to_port(IPPROTO_TCP, i))
67         {
68             char port_proto[64];
69
70             if(_ports_open == 0)
71             {
72                 snprintf(port_proto, 64, "\n      %d (tcp),", i);
73             }
74             else
75             {
76                 snprintf(port_proto, 64, "%d (tcp),", i);
77             }
78             strncat(open_ports_str, port_proto, open_ports_size);
79             open_ports_size -= strlen(port_proto) +1;
80
81             _ports_open++;
82         }
83         if(total_ports_udp[i] && connect_to_port(IPPROTO_UDP, i))
84         {
85             char port_proto[64];
86
87             if(_ports_open == 0)
88             {
89                 snprintf(port_proto, 64, "\n      %d (udp),", i);
90             }
91             else
92             {
93                 snprintf(port_proto, 64, "%d (udp),", i);
94             }
95
96             strncat(open_ports_str, port_proto, open_ports_size);
97             open_ports_size -= strlen(port_proto) +1;
98
99             _ports_open++;
100         }
101
102         if(_ports_open >= 4)
103         {
104             _ports_open = 0;
105         }
106     }
107
108 }
109
110
111 /*  check_open_ports: v0.1
112  *  Check all open ports
113  */
114 void check_open_ports()
115 {
116     memset(open_ports_str, '\0', OS_SIZE_1024 +1);
117     open_ports_size = OS_SIZE_1024 - 1;
118     _ports_open = 0;
119
120     #ifndef OSSECHIDS
121     snprintf(open_ports_str, OS_SIZE_1024, "The following ports are open:");
122     open_ports_size-=strlen(open_ports_str) +1;
123
124     /* Testing All ports */
125     try_to_access_ports();
126
127     open_ports_str[strlen(open_ports_str) -1] = '\0';
128
129     notify_rk(ALERT_OK, open_ports_str);
130
131     #endif
132     return;
133 }
134
135 /* EOF */