new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / remoted / ar-forward.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All right reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #include <pthread.h>
11
12 #include "shared.h"
13 #include "remoted.h"
14 #include "os_net/os_net.h"
15
16
17 /* Start of a new thread. Only returns on unrecoverable errors. */
18 void *AR_Forward(__attribute__((unused)) void *arg)
19 {
20     int arq = 0;
21     int agent_id = 0;
22     int ar_location = 0;
23
24     char msg_to_send[OS_SIZE_1024 + 1];
25
26     char msg[OS_SIZE_1024 + 1];
27     char *location = NULL;
28     char *ar_location_str = NULL;
29     char *ar_agent_id = NULL;
30     char *tmp_str = NULL;
31
32     /* Create the unix queue */
33     if ((arq = StartMQ(ARQUEUE, READ)) < 0) {
34         ErrorExit(QUEUE_ERROR, ARGV0, ARQUEUE, strerror(errno));
35     }
36
37     memset(msg, '\0', OS_SIZE_1024 + 1);
38
39     /* Daemon loop */
40     while (1) {
41         if (OS_RecvUnix(arq, OS_SIZE_1024, msg)) {
42             /* Always zero the location */
43             ar_location = 0;
44
45             /* Get the location */
46             location = msg;
47
48             /* Location is going to be the agent name */
49             tmp_str = strchr(msg, ')');
50             if (!tmp_str) {
51                 merror(EXECD_INV_MSG, ARGV0, msg);
52                 continue;
53             }
54             *tmp_str = '\0';
55
56             /* Going after the ')' and space */
57             tmp_str += 2;
58
59             /* Extract the source IP */
60             tmp_str = strchr(tmp_str, ' ');
61             if (!tmp_str) {
62                 merror(EXECD_INV_MSG, ARGV0, msg);
63                 continue;
64             }
65             tmp_str++;
66             location++;
67
68             /* Set ar_location */
69             ar_location_str = tmp_str;
70             if (*tmp_str == ALL_AGENTS_C) {
71                 ar_location |= ALL_AGENTS;
72             }
73             tmp_str++;
74             if (*tmp_str == REMOTE_AGENT_C) {
75                 ar_location |= REMOTE_AGENT;
76             } else if (*tmp_str == NO_AR_C) {
77                 ar_location |= NO_AR_MSG;
78             }
79             tmp_str++;
80             if (*tmp_str == SPECIFIC_AGENT_C) {
81                 ar_location |= SPECIFIC_AGENT;
82             }
83
84             /* Extract the active response location */
85             tmp_str = strchr(ar_location_str, ' ');
86             if (!tmp_str) {
87                 merror(EXECD_INV_MSG, ARGV0, msg);
88                 continue;
89             }
90             *tmp_str = '\0';
91             tmp_str++;
92
93             /* Extract the agent id */
94             ar_agent_id = tmp_str;
95             tmp_str = strchr(tmp_str, ' ');
96             if (!tmp_str) {
97                 merror(EXECD_INV_MSG, ARGV0, msg);
98                 continue;
99             }
100             *tmp_str = '\0';
101             tmp_str++;
102
103             /* Create the new message */
104             if (ar_location & NO_AR_MSG) {
105                 snprintf(msg_to_send, OS_SIZE_1024, "%s%s",
106                          CONTROL_HEADER,
107                          tmp_str);
108             } else {
109                 snprintf(msg_to_send, OS_SIZE_1024, "%s%s%s",
110                          CONTROL_HEADER,
111                          EXECD_HEADER,
112                          tmp_str);
113             }
114
115             /* Lock use of keys */
116             key_lock();
117
118             /* Send to ALL agents */
119             if (ar_location & ALL_AGENTS) {
120                 unsigned int i;
121                 for (i = 0; i < keys.keysize; i++) {
122                     send_msg(i, msg_to_send);
123                 }
124             }
125
126             /* Send to the remote agent that generated the event */
127             else if ((ar_location & REMOTE_AGENT) && (location != NULL)) {
128                 agent_id = OS_IsAllowedName(&keys, location);
129                 if (agent_id < 0) {
130                     key_unlock();
131                     merror(AR_NOAGENT_ERROR, ARGV0, location);
132                     continue;
133                 }
134
135                 send_msg((unsigned)agent_id, msg_to_send);
136             }
137
138             /* Send to a pre-defined agent */
139             else if (ar_location & SPECIFIC_AGENT) {
140                 ar_location++;
141
142                 agent_id = OS_IsAllowedID(&keys, ar_agent_id);
143
144                 if (agent_id < 0) {
145                     key_unlock();
146                     merror(AR_NOAGENT_ERROR, ARGV0, ar_agent_id);
147                     continue;
148                 }
149
150                 send_msg((unsigned)agent_id, msg_to_send);
151             }
152
153             /* Lock use of keys */
154             key_unlock();
155         }
156     }
157 }
158