izmjene licence
[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     else
44     {
45         return (0);
46     }
47
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");
52
53     if(connect(ossock, (struct sockaddr *)&server, sizeof(server)) == 0)
54     {
55         rc = 1;
56     }
57
58     close(ossock);
59
60     return(rc);
61 }
62
63 /* try_to_access_ports */
64 void try_to_access_ports()
65 {
66     int i;
67
68     for(i = 0; i<= 65535; i++)
69     {
70         if(total_ports_tcp[i] && connect_to_port(IPPROTO_TCP, i))
71         {
72             char port_proto[64];
73
74             if(_ports_open == 0)
75             {
76                 snprintf(port_proto, 64, "\n      %d (tcp),", i);
77             }
78             else
79             {
80                 snprintf(port_proto, 64, "%d (tcp),", i);
81             }
82             strncat(open_ports_str, port_proto, open_ports_size);
83             open_ports_size -= strlen(port_proto) +1;
84
85             _ports_open++;
86         }
87         if(total_ports_udp[i] && connect_to_port(IPPROTO_UDP, i))
88         {
89             char port_proto[64];
90
91             if(_ports_open == 0)
92             {
93                 snprintf(port_proto, 64, "\n      %d (udp),", i);
94             }
95             else
96             {
97                 snprintf(port_proto, 64, "%d (udp),", i);
98             }
99
100             strncat(open_ports_str, port_proto, open_ports_size);
101             open_ports_size -= strlen(port_proto) +1;
102
103             _ports_open++;
104         }
105
106         if(_ports_open >= 4)
107         {
108             _ports_open = 0;
109         }
110     }
111
112 }
113
114
115 /*  check_open_ports: v0.1
116  *  Check all open ports
117  */
118 void check_open_ports()
119 {
120     memset(open_ports_str, '\0', OS_SIZE_1024 +1);
121     open_ports_size = OS_SIZE_1024 - 1;
122     _ports_open = 0;
123
124     #ifndef OSSECHIDS
125     snprintf(open_ports_str, OS_SIZE_1024, "The following ports are open:");
126     open_ports_size-=strlen(open_ports_str) +1;
127
128     /* Testing All ports */
129     try_to_access_ports();
130
131     open_ports_str[strlen(open_ports_str) -1] = '\0';
132
133     notify_rk(ALERT_OK, open_ports_str);
134
135     #endif
136     return;
137 }
138
139 /* EOF */