Imported Upstream version 2.5.1
[ossec-hids.git] / src / analysisd / decoders / plugins / ossecalert_decoder.c
1 /* @(#) $Id$ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All rights 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  * License details at the LICENSE file included with OSSEC or 
12  * online at: http://www.ossec.net/en/licensing.html
13  */
14
15
16 #include "shared.h"
17 #include "eventinfo.h"
18 #include "config.h"
19
20
21
22
23 /* OSSECAlert decoder init */
24 void *OSSECAlert_Decoder_Init()
25 {
26     debug1("%s: Initializing OSSECAlert decoder.", ARGV0);
27
28
29     /* There is nothing else to do over here */
30     return(NULL);
31 }
32
33
34
35 #define oa_strchr(x,y,z) z = strchr(x,y); if(!z){ return(NULL); }
36
37 /* OSSECAlert decoder 
38  * Will extract the rule_id and point back to the original rule.
39  * Will also extract srcip and username if available.
40  * Examples:
41  * 
42  */                             
43 void *OSSECAlert_Decoder_Exec(Eventinfo *lf)
44 {
45     char *oa_id = 0;
46     char *oa_location;
47     char *oa_val;
48     char oa_newlocation[256];
49     char *tmp_str = NULL;
50     void *rule_pointer;
51
52
53     lf->decoder_info->type = OSSEC_ALERT;
54
55
56     /* Checking the alert level. */
57     if(strncmp("Alert Level: ", lf->log, 12) != 0)
58     {
59         return(NULL);
60     }
61
62     
63     /* Going past the level. */
64     oa_strchr(lf->log, ';', tmp_str);
65     tmp_str++;
66
67
68     /* Getting rule id. */
69     oa_strchr(tmp_str, ':', tmp_str);
70     tmp_str++;
71     if(*tmp_str != ' ')
72     {
73         return(NULL);
74     }    
75     tmp_str++;
76
77     
78     /* Getting id. */
79     oa_id = tmp_str;
80     oa_strchr(tmp_str, ' ', tmp_str);
81     *tmp_str = '\0';
82
83
84     /* Getting rule structure. */
85     rule_pointer = OSHash_Get(Config.g_rules_hash, oa_id);
86     if(!rule_pointer)
87     {
88         merror("%s: WARN: Rule id '%s' not found internally.", ARGV0, oa_id);
89         *tmp_str = ' ';
90         return(NULL);
91     }
92     *tmp_str = ' ';
93     oa_strchr(tmp_str, ';', tmp_str);
94     tmp_str++;
95
96
97     /* Checking location. */
98     if(strncmp(" Location: ", tmp_str, 11) != 0)
99     {
100         return(NULL);
101     }
102     tmp_str+=11;
103
104
105     /* Setting location; */
106     oa_location = tmp_str;
107     
108
109     oa_strchr(tmp_str, ';', tmp_str);
110     *tmp_str = '\0';
111
112
113     /* Setting new location. */
114     oa_newlocation[255] = '\0';
115
116     if(lf->hostname == lf->location)
117     {
118         snprintf(oa_newlocation, 255, "%s|%s", lf->location, oa_location);
119         free(lf->location);
120         os_strdup(oa_newlocation, lf->location);
121         lf->hostname = lf->location;
122     }
123     else
124     {
125         snprintf(oa_newlocation, 255, "%s->%s|%s", lf->hostname, 
126                  lf->location, oa_location);
127         free(lf->location);
128         os_strdup(oa_newlocation, lf->location);
129         lf->hostname = lf->location;
130     }
131
132     *tmp_str = ';';
133     tmp_str++;
134
135     
136     /* Getting additional fields. */
137     while((*tmp_str == ' ') && (tmp_str[1] != ' '))
138     {
139         tmp_str++;
140         oa_val = tmp_str;
141
142         tmp_str = strchr(tmp_str, ';');
143         if(!tmp_str)
144         {
145             return(NULL);
146         }
147         *tmp_str = '\0';
148
149         if(strncmp(oa_val, "srcip: ", 7) == 0)
150         {
151             os_strdup(oa_val + 7, lf->srcip);
152         }
153         if(strncmp(oa_val, "user: ", 6) == 0)
154         {
155             os_strdup(oa_val + 6, lf->dstuser);
156         }
157
158         *tmp_str = ';';
159         tmp_str++;
160     }
161     
162
163     /* Removing space. */
164     while(*tmp_str == ' ')
165         tmp_str++;
166     
167     
168     /* Creating new full log. */
169     free(lf->full_log);
170     os_strdup(tmp_str, lf->full_log);
171     lf->log = lf->full_log;
172     
173
174     /* Rule that generated. */
175     lf->generated_rule = rule_pointer;
176
177
178     return(NULL);
179 }
180
181 /* END Decoder */