Imported Upstream version 2.7
[ossec-hids.git] / src / logcollector / read_ossecalert.c
1 /* @(#) $Id: ./src/logcollector/read_ossecalert.c, 2012/03/30 dcid Exp $
2  */
3
4 /* Copyright (C) 2012 Daniel B. Cid (http://dcid.me)
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 /* Read the syslog */
14
15
16 #include "shared.h"
17 #include "headers/read-alert.h"
18 #include "logcollector.h"
19
20
21
22 /* Read syslog files/snort fast/apache files */
23 void *read_ossecalert(int pos, int *rc, int drop_it)
24 {
25     alert_data *al_data;
26     char user_msg[256];
27     char srcip_msg[256];
28
29     char syslog_msg[OS_SIZE_2048 +1];
30
31     al_data = GetAlertData(0, logff[pos].fp);
32     if(!al_data)
33     {
34         return(NULL);
35     }
36
37
38     memset(syslog_msg, '\0', OS_SIZE_2048 +1);
39
40
41
42     /* Adding source ip. */
43     if(!al_data->srcip ||
44        ((al_data->srcip[0] == '(') &&
45         (al_data->srcip[1] == 'n') &&
46         (al_data->srcip[2] == 'o')))
47     {
48         srcip_msg[0] = '\0';
49     }
50     else
51     {
52         snprintf(srcip_msg, 255, " srcip: %s;", al_data->srcip);
53     }
54
55
56     /* Adding username. */
57     if(!al_data->user ||
58        ((al_data->user[0] == '(') &&
59         (al_data->user[1] == 'n') &&
60         (al_data->user[2] == 'o')))
61     {
62         user_msg[0] = '\0';
63     }
64     else
65     {
66         snprintf(user_msg, 255, " user: %s;", al_data->user);
67     }
68
69
70     if(al_data->log[1] == NULL)
71     {
72         /* Building syslog message. */
73         snprintf(syslog_msg, OS_SIZE_2048,
74                 "ossec: Alert Level: %d; Rule: %d - %s; "
75                 "Location: %s;%s%s  %s",
76                 al_data->level, al_data->rule, al_data->comment,
77                 al_data->location,
78                 srcip_msg,
79                 user_msg,
80                 al_data->log[0]);
81     }
82     else
83     {
84         char *tmp_msg = NULL;
85         short int j = 0;
86
87         while(al_data->log[j] != NULL)
88         {
89             tmp_msg = os_LoadString(tmp_msg, al_data->log[j]);
90             tmp_msg = os_LoadString(tmp_msg, "\n");
91             if(tmp_msg == NULL)
92             {
93                 FreeAlertData(al_data);
94                 return(NULL);
95             }
96             j++;
97         }
98         if(strlen(tmp_msg) > 1596)
99         {
100             tmp_msg[1594] = '.';
101             tmp_msg[1595] = '.';
102             tmp_msg[1596] = '.';
103             tmp_msg[1597] = '\0';
104         }
105         snprintf(syslog_msg, OS_SIZE_2048,
106                 "ossec: Alert Level: %d; Rule: %d - %s; "
107                 "Location: %s;%s%s  %s",
108                 al_data->level, al_data->rule, al_data->comment,
109                 al_data->location,
110                 srcip_msg,
111                 user_msg,
112                 tmp_msg);
113     }
114
115
116     /* Clearing the memory */
117     FreeAlertData(al_data);
118
119
120
121     /* Sending message to queue */
122     if(drop_it == 0)
123     {
124         if(SendMSG(logr_queue,syslog_msg,logff[pos].file, LOCALFILE_MQ) < 0)
125         {
126             merror(QUEUE_SEND, ARGV0);
127             if((logr_queue = StartMQ(DEFAULTQPATH,WRITE)) < 0)
128             {
129                 ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
130             }
131         }
132     }
133
134     return(NULL);
135 }
136
137