d962f47aebc8c51c1af0377cf73d14b2d9b414a8
[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     *rc = 0;
30
31     char syslog_msg[OS_SIZE_2048 +1];
32
33     al_data = GetAlertData(0, logff[pos].fp);
34     if(!al_data)
35     {
36         return(NULL);
37     }
38
39
40     memset(syslog_msg, '\0', OS_SIZE_2048 +1);
41
42
43
44     /* Adding source ip. */
45     if(!al_data->srcip ||
46        ((al_data->srcip[0] == '(') &&
47         (al_data->srcip[1] == 'n') &&
48         (al_data->srcip[2] == 'o')))
49     {
50         srcip_msg[0] = '\0';
51     }
52     else
53     {
54         snprintf(srcip_msg, 255, " srcip: %s;", al_data->srcip);
55     }
56
57
58     /* Adding username. */
59     if(!al_data->user ||
60        ((al_data->user[0] == '(') &&
61         (al_data->user[1] == 'n') &&
62         (al_data->user[2] == 'o')))
63     {
64         user_msg[0] = '\0';
65     }
66     else
67     {
68         snprintf(user_msg, 255, " user: %s;", al_data->user);
69     }
70
71
72     if(al_data->log[1] == NULL)
73     {
74         /* Building syslog message. */
75         snprintf(syslog_msg, OS_SIZE_2048,
76                 "ossec: Alert Level: %d; Rule: %d - %s; "
77                 "Location: %s;%s%s  %s",
78                 al_data->level, al_data->rule, al_data->comment,
79                 al_data->location,
80                 srcip_msg,
81                 user_msg,
82                 al_data->log[0]);
83     }
84     else
85     {
86         char *tmp_msg = NULL;
87         short int j = 0;
88
89         while(al_data->log[j] != NULL)
90         {
91             tmp_msg = os_LoadString(tmp_msg, al_data->log[j]);
92             tmp_msg = os_LoadString(tmp_msg, "\n");
93             if(tmp_msg == NULL)
94             {
95                 FreeAlertData(al_data);
96                 return(NULL);
97             }
98             j++;
99         }
100         if(strlen(tmp_msg) > 1596)
101         {
102             tmp_msg[1594] = '.';
103             tmp_msg[1595] = '.';
104             tmp_msg[1596] = '.';
105             tmp_msg[1597] = '\0';
106         }
107         snprintf(syslog_msg, OS_SIZE_2048,
108                 "ossec: Alert Level: %d; Rule: %d - %s; "
109                 "Location: %s;%s%s  %s",
110                 al_data->level, al_data->rule, al_data->comment,
111                 al_data->location,
112                 srcip_msg,
113                 user_msg,
114                 tmp_msg);
115     }
116
117
118     /* Clearing the memory */
119     FreeAlertData(al_data);
120
121
122
123     /* Sending message to queue */
124     if(drop_it == 0)
125     {
126         if(SendMSG(logr_queue,syslog_msg,logff[pos].file, LOCALFILE_MQ) < 0)
127         {
128             merror(QUEUE_SEND, ARGV0);
129             if((logr_queue = StartMQ(DEFAULTQPATH,WRITE)) < 0)
130             {
131                 ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
132             }
133         }
134     }
135
136     return(NULL);
137 }
138
139