Imported Upstream version 2.7
[ossec-hids.git] / src / analysisd / alerts / exec.c
1 /* @(#) $Id: ./src/analysisd/alerts/exec.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 /* Basic e-mailing operations */
14
15
16 #include "shared.h"
17 #include "rules.h"
18 #include "alerts.h"
19 #include "config.h"
20 #include "active-response.h"
21
22 #include "os_net/os_net.h"
23 #include "os_regex/os_regex.h"
24 #include "os_execd/execd.h"
25
26 #include "eventinfo.h"
27
28
29 /* OS_Exec v0.1
30  */
31 void OS_Exec(int *execq, int *arq, Eventinfo *lf, active_response *ar)
32 {
33     char exec_msg[OS_SIZE_1024 +1];
34     char *ip;
35     char *user;
36
37
38     /* Cleaning the IP */
39     if(lf->srcip && (ar->ar_cmd->expect & SRCIP))
40     {
41         if(strncmp(lf->srcip, "::ffff:", 7) == 0)
42         {
43             ip = lf->srcip + 7;
44         }
45         else
46         {
47             ip = lf->srcip;
48         }
49
50         /* Checking if IP is to ignored */
51         if(Config.white_list)
52         {
53             if(OS_IPFoundList(ip, Config.white_list))
54             {
55                 return;
56             }
57         }
58
59         /* Checking if it is a hostname */
60         if(Config.hostname_white_list)
61         {
62             int srcip_size;
63             OSMatch **wl;
64
65             srcip_size = strlen(ip);
66
67             wl = Config.hostname_white_list;
68             while(*wl)
69             {
70                 if(OSMatch_Execute(ip, srcip_size, *wl))
71                     return;
72                 wl++;
73             }
74         }
75     }
76     else
77     {
78         ip = "-";
79     }
80
81
82     /* Getting username */
83     if(lf->dstuser && (ar->ar_cmd->expect & USERNAME))
84     {
85         user = lf->dstuser;
86     }
87     else
88     {
89         user = "-";
90     }
91
92
93     /* active response on the server.
94      * The response must be here if the ar->location is set to AS
95      * or the ar->location is set to local (REMOTE_AGENT) and the
96      * event location is from here.
97      */
98     if((ar->location & AS_ONLY) ||
99       ((ar->location & REMOTE_AGENT) && (lf->location[0] != '(')) )
100     {
101         if(!(Config.ar & LOCAL_AR))
102             return;
103
104         snprintf(exec_msg, OS_SIZE_1024,
105                 "%s %s %s %d.%ld %d %s",
106                 ar->name,
107                 user,
108                 ip,
109                 lf->time,
110                 __crt_ftell,
111                 lf->generated_rule->sigid,
112                 lf->location);
113
114         if(OS_SendUnix(*execq, exec_msg, 0) < 0)
115         {
116             merror("%s: Error communicating with execd.", ARGV0);
117         }
118     }
119
120
121     /* Active response to the forwarder */
122     else if((Config.ar & REMOTE_AR))
123     {
124         int rc;
125         /*If lf->location start with a (  was generated by remote agent and its ID is included in lf->location
126                 if missing then it must of been generated by the local analysisd so prepend a false id tag */
127         if(lf->location[0] == '(') {
128                 snprintf(exec_msg, OS_SIZE_1024,
129                         "%s %c%c%c %s %s %s %s %d.%ld %d",
130                         lf->location,
131                         (ar->location & ALL_AGENTS)?ALL_AGENTS_C:NONE_C,
132                         (ar->location & REMOTE_AGENT)?REMOTE_AGENT_C:NONE_C,
133                         (ar->location & SPECIFIC_AGENT)?SPECIFIC_AGENT_C:NONE_C,
134                         ar->agent_id != NULL? ar->agent_id: "(null)",
135                         ar->name,
136                         user,
137                         ip,
138                         lf->time,
139                         __crt_ftell,
140                         lf->generated_rule->sigid);
141         } else {
142                 snprintf(exec_msg, OS_SIZE_1024,
143                         "(local_source) %s %c%c%c %s %s %s %s %d.%ld %d",
144                         lf->location,
145                         (ar->location & ALL_AGENTS)?ALL_AGENTS_C:NONE_C,
146                         (ar->location & REMOTE_AGENT)?REMOTE_AGENT_C:NONE_C,
147                         (ar->location & SPECIFIC_AGENT)?SPECIFIC_AGENT_C:NONE_C,
148                         ar->agent_id != NULL? ar->agent_id: "(null)",
149                         ar->name,
150                         user,
151                         ip,
152                         lf->time,
153                         __crt_ftell,
154                         lf->generated_rule->sigid);
155         }
156
157         if((rc = OS_SendUnix(*arq, exec_msg, 0)) < 0)
158         {
159             if(rc == OS_SOCKBUSY)
160             {
161                 merror("%s: AR socket busy.", ARGV0);
162             }
163             else
164             {
165                 merror("%s: AR socket error (shutdown?).", ARGV0);
166             }
167             merror("%s: Error communicating with ar queue (%d).", ARGV0, rc);
168         }
169     }
170
171     return;
172 }
173
174 /* EOF */