Imported Upstream version 2.5.1
[ossec-hids.git] / src / remoted / secure.c
1 /* @(#) $Id$ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All right reserved.
5  *
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 2) as published by the FSF - Free Software
9  * Foundation
10  */
11
12
13
14 #include "shared.h"
15 #include "os_net/os_net.h"
16
17
18 #include "remoted.h"
19
20
21 /** void HandleSecure() v0.3
22  * Handle the secure connections
23  */
24 void HandleSecure()
25 {
26     int agentid;
27
28     char buffer[OS_MAXSTR +1];
29     char cleartext_msg[OS_MAXSTR +1]; 
30     char srcip[IPSIZE +1];
31     char *tmp_msg;
32     char srcmsg[OS_FLSIZE +1];
33
34
35     int recv_b;
36
37     struct sockaddr_in peer_info;
38     socklen_t peer_size;
39
40
41     /* Send msg init */
42     send_msg_init();
43
44
45     /* Initializing key mutex. */
46     keyupdate_init();
47
48
49     /* Initializing manager */
50     manager_init(0);
51
52
53     /* Creating Ar forwarder thread */
54     if(CreateThread(AR_Forward, (void *)NULL) != 0)
55     {
56         ErrorExit(THREAD_ERROR, ARGV0);
57     }
58     
59     /* Creating wait_for_msgs thread */
60     if(CreateThread(wait_for_msgs, (void *)NULL) != 0)
61     {
62         ErrorExit(THREAD_ERROR, ARGV0);
63     }
64
65
66     /* Connecting to the message queue
67      * Exit if it fails.
68      */
69     if((logr.m_queue = StartMQ(DEFAULTQUEUE,WRITE)) < 0)
70     {
71         ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQUEUE);
72     }
73         
74     
75     verbose(AG_AX_AGENTS, ARGV0, MAX_AGENTS);    
76
77     
78     /* Reading authentication keys */
79     verbose(ENC_READ, ARGV0);
80         
81     OS_ReadKeys(&keys);
82     
83     debug1("%s: DEBUG: OS_StartCounter.", ARGV0);
84     OS_StartCounter(&keys);
85     debug1("%s: DEBUG: OS_StartCounter completed.", ARGV0);
86
87
88     /* setting up peer size */
89     peer_size = sizeof(peer_info);
90     logr.peer_size = sizeof(peer_info);
91
92
93     /* Initializing some variables */
94     memset(buffer, '\0', OS_MAXSTR +1);
95     memset(cleartext_msg, '\0', OS_MAXSTR +1);
96     memset(srcmsg, '\0', OS_FLSIZE +1);
97     tmp_msg = NULL;
98     
99     
100     
101     /* loop in here */
102     while(1)
103     {
104         /* Receiving message  */
105         recv_b = recvfrom(logr.sock, buffer, OS_MAXSTR, 0, 
106                 (struct sockaddr *)&peer_info, &peer_size);
107
108
109         /* Nothing received */
110         if(recv_b <= 0)
111         {
112             continue;
113         }
114
115
116         /* Setting the source ip */
117         strncpy(srcip, inet_ntoa(peer_info.sin_addr), IPSIZE);
118         srcip[IPSIZE] = '\0';
119
120
121
122         /* Getting a valid agentid */ 
123         if(buffer[0] == '!')
124         {
125             tmp_msg = buffer;
126             tmp_msg++;
127             
128             
129             /* We need to make sure that we have a valid id
130              * and that we reduce the recv buffer size.
131              */
132             while(isdigit((int)*tmp_msg))
133             {
134                 tmp_msg++;
135                 recv_b--;
136             }
137
138             if(*tmp_msg != '!')
139             {
140                 merror(ENCFORMAT_ERROR, __local_name, srcip);
141                 continue;
142             }
143
144             *tmp_msg = '\0';
145             tmp_msg++;
146             recv_b-=2;
147
148             agentid = OS_IsAllowedDynamicID(&keys, buffer +1, srcip);
149             if(agentid == -1)
150             {
151                 if(check_keyupdate())
152                 {
153                     agentid = OS_IsAllowedDynamicID(&keys, buffer +1, srcip);
154                     if(agentid == -1)
155                     {
156                         merror(ENC_IP_ERROR, ARGV0, srcip);
157                         continue;
158                     }
159                 }
160                 else
161                 {
162                     merror(ENC_IP_ERROR, ARGV0, srcip);
163                     continue;
164                 }
165             }
166         }
167         else
168         {
169             agentid = OS_IsAllowedIP(&keys, srcip); 
170             if(agentid < 0)
171             {
172                 if(check_keyupdate())
173                 {
174                     agentid = OS_IsAllowedIP(&keys, srcip);
175                     if(agentid == -1)
176                     {
177                         merror(DENYIP_WARN,ARGV0,srcip);
178                         continue;
179                     }
180                 }
181                 else
182                 {
183                     merror(DENYIP_WARN,ARGV0,srcip);
184                     continue;
185                 }
186             }
187             tmp_msg = buffer;
188         }
189         
190
191         /* Decrypting the message */    
192         tmp_msg = ReadSecMSG(&keys, tmp_msg, cleartext_msg,
193                              agentid, recv_b -1);
194         if(tmp_msg == NULL)
195         {
196             /* If duplicated, a warning was already generated */
197             continue;
198         }
199
200
201         /* Check if it is a control message */ 
202         if(IsValidHeader(tmp_msg))
203         {
204             /* We need to save the peerinfo if it is a control msg */
205             memcpy(&keys.keyentries[agentid]->peer_info, &peer_info, peer_size);
206             keys.keyentries[agentid]->rcvd = time(0);
207
208             save_controlmsg(agentid, tmp_msg);
209
210             continue;
211         }
212
213
214         /* Generating srcmsg */
215         snprintf(srcmsg, OS_FLSIZE,"(%s) %s",keys.keyentries[agentid]->name, 
216                                              keys.keyentries[agentid]->ip->ip);
217         
218
219         /* If we can't send the message, try to connect to the
220          * socket again. If it not exit.
221          */
222         if(SendMSG(logr.m_queue, tmp_msg, srcmsg, 
223                    SECURE_MQ) < 0)
224         {
225             merror(QUEUE_ERROR, ARGV0, DEFAULTQUEUE, strerror(errno));
226
227             if((logr.m_queue = StartMQ(DEFAULTQUEUE, WRITE)) < 0)
228             {
229                 ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQUEUE);
230             }
231         }
232     }
233 }
234
235
236
237 /* EOF */