660cc528b8049a7feb9e8155b40bb4017190741e
[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     char *filename;
37     int do_free_filename = 0;
38
39     ip = user = filename = "-";
40
41     /* Cleaning the IP */
42     if(lf->srcip && (ar->ar_cmd->expect & SRCIP))
43     {
44         if(strncmp(lf->srcip, "::ffff:", 7) == 0)
45         {
46             ip = lf->srcip + 7;
47         }
48         else
49         {
50             ip = lf->srcip;
51         }
52
53         /* Checking if IP is to ignored */
54         if(Config.white_list)
55         {
56             if(OS_IPFoundList(ip, Config.white_list))
57             {
58                 return;
59             }
60         }
61
62         /* Checking if it is a hostname */
63         if(Config.hostname_white_list)
64         {
65             int srcip_size;
66             OSMatch **wl;
67
68             srcip_size = strlen(ip);
69
70             wl = Config.hostname_white_list;
71             while(*wl)
72             {
73                 if(OSMatch_Execute(ip, srcip_size, *wl))
74                     return;
75                 wl++;
76             }
77         }
78     }
79
80     /* Getting username */
81     if(lf->dstuser && (ar->ar_cmd->expect & USERNAME))
82     {
83         user = lf->dstuser;
84     }
85
86     /* Get the filename */
87     if(lf->filename && (ar->ar_cmd->expect & FILENAME))
88     {
89             filename = os_shell_escape(lf->filename);
90             do_free_filename = 1;
91     }
92
93
94     /* active response on the server.
95      * The response must be here if the ar->location is set to AS
96      * or the ar->location is set to local (REMOTE_AGENT) and the
97      * event location is from here.
98      */
99     if((ar->location & AS_ONLY) ||
100       ((ar->location & REMOTE_AGENT) && (lf->location[0] != '(')) )
101     {
102         if(!(Config.ar & LOCAL_AR))
103             return;
104
105         snprintf(exec_msg, OS_SIZE_1024,
106                 "%s %s %s %d.%ld %d %s %s",
107                 ar->name,
108                 user,
109                 ip,
110                 lf->time,
111                 __crt_ftell,
112                 lf->generated_rule->sigid,
113                 lf->location,
114                 filename);
115
116         if(OS_SendUnix(*execq, exec_msg, 0) < 0)
117         {
118             merror("%s: Error communicating with execd.", ARGV0);
119         }
120     }
121
122
123     /* Active response to the forwarder */
124     else if((Config.ar & REMOTE_AR))
125     {
126         int rc;
127         /*If lf->location start with a (  was generated by remote agent and its ID is included in lf->location
128                 if missing then it must of been generated by the local analysisd so prepend a false id tag */
129         if(lf->location[0] == '(') {
130                 snprintf(exec_msg, OS_SIZE_1024,
131                         "%s %c%c%c %s %s %s %s %d.%ld %d %s %s",
132                         lf->location,
133                         (ar->location & ALL_AGENTS)?ALL_AGENTS_C:NONE_C,
134                         (ar->location & REMOTE_AGENT)?REMOTE_AGENT_C:NONE_C,
135                         (ar->location & SPECIFIC_AGENT)?SPECIFIC_AGENT_C:NONE_C,
136                         ar->agent_id != NULL? ar->agent_id: "(null)",
137                         ar->name,
138                         user,
139                         ip,
140                         lf->time,
141                         __crt_ftell,
142                         lf->generated_rule->sigid,
143                     lf->location,
144                     filename);
145         } else {
146                 snprintf(exec_msg, OS_SIZE_1024,
147                         "(local_source) %s %c%c%c %s %s %s %s %d.%ld %d %s %s",
148                         lf->location,
149                         (ar->location & ALL_AGENTS)?ALL_AGENTS_C:NONE_C,
150                         (ar->location & REMOTE_AGENT)?REMOTE_AGENT_C:NONE_C,
151                         (ar->location & SPECIFIC_AGENT)?SPECIFIC_AGENT_C:NONE_C,
152                         ar->agent_id != NULL? ar->agent_id: "(null)",
153                         ar->name,
154                         user,
155                         ip,
156                         lf->time,
157                         __crt_ftell,
158                         lf->generated_rule->sigid,
159                     lf->location,
160                     filename);
161         }
162
163         if((rc = OS_SendUnix(*arq, exec_msg, 0)) < 0)
164         {
165             if(rc == OS_SOCKBUSY)
166             {
167                 merror("%s: AR socket busy.", ARGV0);
168             }
169             else
170             {
171                 merror("%s: AR socket error (shutdown?).", ARGV0);
172             }
173             merror("%s: Error communicating with ar queue (%d).", ARGV0, rc);
174         }
175     }
176
177     // Clean up Memory
178     if ( filename != NULL && do_free_filename == 1 )
179         free(filename);
180
181     return;
182 }
183
184 /* EOF */