1e6bc1c9b1eb1e91948b685bb975cd9655e555b6
[ossec-hids.git] / src / remoted / ar-forward.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 #include "shared.h"
14 #include <pthread.h>
15
16 #include "remoted.h"
17 #include "os_net/os_net.h"
18
19
20
21 /** void *AR_Forward(void *arg) v0.1
22  * Start of a new thread. Only returns
23  * on unrecoverable errors.
24  */
25 void *AR_Forward(void *arg)
26 {
27     int i = 0;
28     int arq = 0;
29     int agent_id = 0;
30     int ar_location = 0;
31     
32     char msg_to_send[OS_SIZE_1024 +1];
33     
34     char msg[OS_SIZE_1024 +1];
35     char *location = NULL;
36     char *ar_location_str = NULL;
37     char *ar_agent_id = NULL;
38     char *tmp_str = NULL;
39
40
41     /* Creating the unix queue */
42     if((arq = StartMQ(ARQUEUE, READ)) < 0)
43     {
44         ErrorExit(QUEUE_ERROR, ARGV0, ARQUEUE, strerror(errno));
45     }
46
47     memset(msg, '\0', OS_SIZE_1024 +1);
48
49     /* Daemon loop */
50     while(1)
51     {
52         if(OS_RecvUnix(arq, OS_SIZE_1024, msg))
53         {
54             /* Always zeroing the location */
55             ar_location = 0;
56             
57             
58             /* Getting the location */
59             location = msg;
60
61
62             /* Location is going to be the agent name */
63             tmp_str = strchr(msg, ')');
64             if(!tmp_str)
65             {
66                 merror(EXECD_INV_MSG, ARGV0, msg);
67                 continue;
68             }
69             *tmp_str = '\0';
70
71
72             /* Going after the ')' and space */
73             tmp_str += 2;
74
75
76             /* Extracting the source ip */
77             tmp_str = strchr(tmp_str, ' ');
78             if(!tmp_str)
79             {
80                 merror(EXECD_INV_MSG, ARGV0, msg);
81                 continue;
82             }
83             tmp_str++;
84             location++;
85
86
87             /* Setting ar_location */
88             ar_location_str = tmp_str;
89             if(*tmp_str == ALL_AGENTS_C)
90             {
91                 ar_location|=ALL_AGENTS;
92             }
93             tmp_str++;
94             if(*tmp_str == REMOTE_AGENT_C)
95             {
96                 ar_location|=REMOTE_AGENT;
97             }
98             else if(*tmp_str == NO_AR_C)
99             {
100                 ar_location|=NO_AR_MSG;
101             }
102             tmp_str++;
103             if(*tmp_str == SPECIFIC_AGENT_C)
104             {
105                 ar_location|=SPECIFIC_AGENT;
106             }
107             
108             
109             /*** Extracting the active response location ***/
110             tmp_str = strchr(ar_location_str, ' ');
111             if(!tmp_str)
112             {
113                 merror(EXECD_INV_MSG, ARGV0, msg);
114                 continue;
115             }
116             *tmp_str = '\0';
117             tmp_str++;
118
119
120             /*** Extracting the agent id */
121             ar_agent_id = tmp_str;
122             tmp_str = strchr(tmp_str, ' ');
123             if(!tmp_str)
124             {
125                 merror(EXECD_INV_MSG, ARGV0, msg);
126                 continue;
127             }
128             *tmp_str = '\0';
129             tmp_str++;
130             
131             
132             /*** Creating the new message ***/
133             if(ar_location & NO_AR_MSG)
134             {
135                 snprintf(msg_to_send, OS_SIZE_1024, "%s%s", 
136                                       CONTROL_HEADER,
137                                       tmp_str);
138             }
139             else
140             {
141                 snprintf(msg_to_send, OS_SIZE_1024, "%s%s%s", 
142                                       CONTROL_HEADER,
143                                       EXECD_HEADER,
144                                       tmp_str);
145             }
146
147             
148             /* Lock use of keys */
149             key_lock();
150             
151             
152             /* Sending to ALL agents */
153             if(ar_location & ALL_AGENTS)
154             {
155                 for(i = 0;i< keys.keysize; i++)
156                 {
157                     send_msg(i, msg_to_send);
158                 }
159             }
160
161             /* Send to the remote agent that generated the event */
162             else if((ar_location & REMOTE_AGENT) && (location != NULL))
163             {
164                 agent_id = OS_IsAllowedName(&keys, location);
165                 if(agent_id < 0)
166                 {
167                     key_unlock();
168                     merror(AR_NOAGENT_ERROR, ARGV0, location);
169                     continue;
170                 }
171                 
172                 send_msg(agent_id, msg_to_send);
173             }
174
175             /* Send to a pre-defined agent */
176             else if(ar_location & SPECIFIC_AGENT)
177             {
178                 ar_location++;
179
180                 agent_id = OS_IsAllowedID(&keys, ar_agent_id);
181                 
182                 if(agent_id < 0)
183                 {
184                     key_unlock();
185                     merror(AR_NOAGENT_ERROR, ARGV0, ar_agent_id);
186                     continue;
187                 }
188
189                 send_msg(agent_id, msg_to_send);
190             }
191
192             /* Lock use of keys */
193             key_unlock();
194         }
195     }
196 }
197
198  
199
200 /* EOF */