46bf4ac5b5cb122461db1ca28e37840361b14f95
[ossec-hids.git] / src / analysisd / alerts / exec.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 /* Basic e-mailing operations */
13
14
15 #include "shared.h"
16 #include "rules.h"
17 #include "alerts.h"
18 #include "config.h"
19 #include "active-response.h"
20
21 #include "os_net/os_net.h"
22 #include "os_regex/os_regex.h"
23 #include "os_execd/execd.h"
24
25 #include "eventinfo.h"
26
27
28 /* OS_Exec v0.1 
29  */
30 void OS_Exec(int *execq, int *arq, Eventinfo *lf, active_response *ar)
31 {
32     char exec_msg[OS_SIZE_1024 +1];
33     char *ip;
34     char *user;
35
36
37     /* Cleaning the IP */
38     if(lf->srcip && (ar->ar_cmd->expect & SRCIP))
39     {
40         ip = strrchr(lf->srcip, ':');
41         if(ip)
42         {
43             ip++;
44         }
45         else
46         {
47             ip = lf->srcip;
48         }
49
50
51         /* Checking if IP is to ignored */
52         if(Config.white_list)
53         {
54             if(OS_IPFoundList(ip, Config.white_list))
55             {
56                 return;
57             }
58         }
59
60         /* Checking if it is a hostname */
61         if(Config.hostname_white_list)
62         {
63             int srcip_size;
64             OSMatch **wl;
65
66             srcip_size = strlen(ip);
67         
68             wl = Config.hostname_white_list;
69             while(*wl)
70             {
71                 if(OSMatch_Execute(ip, srcip_size, *wl))
72                     return;
73                 wl++;
74             }
75         }
76     }
77     else
78     {
79         ip = "-";
80     }
81    
82    
83     /* Getting username */
84     if(lf->dstuser && (ar->ar_cmd->expect & USERNAME))
85     {
86         user = lf->dstuser;
87     }
88     else
89     {
90         user = "-";
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",
107                 ar->name,
108                 user,
109                 ip,
110                 lf->time,
111                 __crt_ftell,
112                 lf->generated_rule->sigid,
113                 lf->location);
114
115         if(OS_SendUnix(*execq, exec_msg, 0) < 0)
116         {
117             merror("%s: Error communicating with execd.", ARGV0);
118         }
119     }
120    
121
122     /* Active response to the forwarder */ 
123     else if((Config.ar & REMOTE_AR) && (lf->location[0] == '('))
124     {
125         int rc;
126         snprintf(exec_msg, OS_SIZE_1024,
127                 "%s %c%c%c %s %s %s %s %d.%ld %d %s",
128                 lf->location,
129                 (ar->location & ALL_AGENTS)?ALL_AGENTS_C:NONE_C,
130                 (ar->location & REMOTE_AGENT)?REMOTE_AGENT_C:NONE_C,
131                 (ar->location & SPECIFIC_AGENT)?SPECIFIC_AGENT_C:NONE_C,
132                 ar->agent_id != NULL? ar->agent_id: "(null)",
133                 ar->name,
134                 user,
135                 ip,
136                 lf->time,
137                 __crt_ftell,
138                 lf->generated_rule->sigid,
139                 lf->location);
140        
141         if((rc = OS_SendUnix(*arq, exec_msg, 0)) < 0)
142         {
143             if(rc == OS_SOCKBUSY)
144             {
145                 merror("%s: AR socket busy.", ARGV0);
146             }
147             else
148             {
149                 merror("%s: AR socket error (shutdown?).", ARGV0);   
150             }
151             merror("%s: Error communicating with ar queue (%d).", ARGV0, rc);
152         }
153     }
154     
155     return;
156 }
157
158 /* EOF */