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