cef1454007036b577a9115a776e286b6cf28ef49
[ossec-hids.git] / src / os_csyslogd / alert.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 "csyslogd.h"
17 #include "config/config.h"
18 #include "os_net/os_net.h"
19
20
21
22
23
24 /** int OS_Alert_SendSyslog
25  * Sends an alert via syslog.
26  * Returns 1 on success or 0 on error.
27  */
28 int OS_Alert_SendSyslog(alert_data *al_data, SyslogConfig *syslog_config)
29 {
30     char *tstamp;
31     char user_msg[256];
32     char srcip_msg[256];
33     
34     char syslog_msg[OS_SIZE_2048 +1];
35
36
37     /* Invalid socket. */
38     if(syslog_config->socket < 0)
39     {
40         return(0);
41     }
42     
43
44     /* Clearing the memory before insert */
45     memset(syslog_msg, '\0', OS_SIZE_2048 +1);
46
47
48     /* Looking if location is set */
49     if(syslog_config->location)
50     {
51         if(!OSMatch_Execute(al_data->location,
52                             strlen(al_data->location),
53                             syslog_config->location))
54         {
55             return(0);
56         }
57     }
58
59
60     /* Looking for the level */
61     if(syslog_config->level)
62     {
63         if(al_data->level < syslog_config->level)
64         {
65             return(0);
66         }
67     }
68
69
70     /* Looking for rule id */
71     if(syslog_config->rule_id)
72     {
73         int id_i = 0;
74         while(syslog_config->rule_id[id_i] != 0)
75         {
76             if(syslog_config->rule_id[id_i] == al_data->rule)
77             {
78                 break;
79             }
80             id_i++;
81         }
82
83
84         /* If we found, id is going to be a valid rule */
85         if(!syslog_config->rule_id[id_i])
86         {
87             return(0);
88         }
89     }
90
91
92     /* Looking for the group */
93     if(syslog_config->group)
94     {
95         if(!OSMatch_Execute(al_data->group,
96                             strlen(al_data->group),
97                             syslog_config->group))
98         {
99             return(0);
100         }
101     }
102
103
104     /* Fixing the timestamp to be syslog compatible. 
105      * We have 2008 Jul 10 10:11:23
106      * Should be: Jul 10 10:11:23
107      */
108     tstamp = al_data->date;
109     if(strlen(al_data->date) > 14)
110     {
111         tstamp+=5;
112
113         /* Fixing first digit if the day is < 10 */ 
114         if(tstamp[4] == '0')
115             tstamp[4] = ' ';
116     }
117     
118
119     /* Adding source ip. */
120     if(!al_data->srcip || 
121        ((al_data->srcip[0] == '(') &&
122         (al_data->srcip[1] == 'n') &&
123         (al_data->srcip[2] == 'o')))
124     {
125         srcip_msg[0] = '\0';
126     }
127     else
128     {
129         snprintf(srcip_msg, 255, " srcip: %s;", al_data->srcip);
130     }
131
132
133     /* Adding username. */
134     if(!al_data->user || 
135        ((al_data->user[0] == '(') &&
136         (al_data->user[1] == 'n') &&
137         (al_data->user[2] == 'o')))
138     {
139         user_msg[0] = '\0';
140     }
141     else
142     {
143         snprintf(user_msg, 255, " user: %s;", al_data->user);
144     }
145
146
147     /* Inserting data */
148     if(syslog_config->format == DEFAULT_CSYSLOG)
149     {
150         /* Building syslog message. */
151         snprintf(syslog_msg, OS_SIZE_2048,
152                 "<%d>%s %s ossec: Alert Level: %d; Rule: %d - %s; "
153                 "Location: %s;%s%s  %s",
154                 syslog_config->priority, tstamp, __shost,
155                 al_data->level, al_data->rule, al_data->comment,
156                 al_data->location, 
157
158                 /* Source ip. */
159                 srcip_msg,
160                 user_msg,
161                 al_data->log[0]);
162     }
163
164
165     OS_SendUDPbySize(syslog_config->socket, strlen(syslog_msg), syslog_msg);
166     
167     return(1);
168 }
169
170
171 /* EOF */