new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / logcollector / read_ossecalert.c
1 /* Copyright (C) 2012 Daniel B. Cid (http://dcid.me)
2  * All right reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #include "shared.h"
11 #include "headers/read-alert.h"
12 #include "logcollector.h"
13
14
15 void *read_ossecalert(int pos, __attribute__((unused)) int *rc, int drop_it)
16 {
17     alert_data *al_data;
18     char user_msg[256];
19     char srcip_msg[256];
20     char syslog_msg[OS_SIZE_2048 + 1];
21
22     *rc = 0;
23
24     al_data = GetAlertData(0, logff[pos].fp);
25     if (!al_data) {
26         return (NULL);
27     }
28
29     memset(syslog_msg, '\0', OS_SIZE_2048 + 1);
30
31     /* Add source ip */
32     if (!al_data->srcip ||
33             ((al_data->srcip[0] == '(') &&
34              (al_data->srcip[1] == 'n') &&
35              (al_data->srcip[2] == 'o'))) {
36         srcip_msg[0] = '\0';
37     } else {
38         snprintf(srcip_msg, 255, " srcip: %s;", al_data->srcip);
39     }
40
41     /* Add username */
42     if (!al_data->user ||
43             ((al_data->user[0] == '(') &&
44              (al_data->user[1] == 'n') &&
45              (al_data->user[2] == 'o'))) {
46         user_msg[0] = '\0';
47     } else {
48         snprintf(user_msg, 255, " user: %s;", al_data->user);
49     }
50
51     if (al_data->log[1] == NULL) {
52         /* Build syslog message */
53         snprintf(syslog_msg, OS_SIZE_2048,
54                  "ossec: Alert Level: %d; Rule: %d - %s; "
55                  "Location: %s;%s%s  %s",
56                  al_data->level, al_data->rule, al_data->comment,
57                  al_data->location,
58                  srcip_msg,
59                  user_msg,
60                  al_data->log[0]);
61     } else {
62         char *tmp_msg = NULL;
63         short int j = 0;
64
65         while (al_data->log[j] != NULL) {
66             tmp_msg = os_LoadString(tmp_msg, al_data->log[j]);
67             tmp_msg = os_LoadString(tmp_msg, "\n");
68             if (tmp_msg == NULL) {
69                 FreeAlertData(al_data);
70                 return (NULL);
71             }
72             j++;
73         }
74
75         if (tmp_msg == NULL) {
76             FreeAlertData(al_data);
77             return (NULL);
78         }
79
80         if (strlen(tmp_msg) > 1596) {
81             tmp_msg[1594] = '.';
82             tmp_msg[1595] = '.';
83             tmp_msg[1596] = '.';
84             tmp_msg[1597] = '\0';
85         }
86         snprintf(syslog_msg, OS_SIZE_2048,
87                  "ossec: Alert Level: %d; Rule: %d - %s; "
88                  "Location: %s;%s%s  %s",
89                  al_data->level, al_data->rule, al_data->comment,
90                  al_data->location,
91                  srcip_msg,
92                  user_msg,
93                  tmp_msg);
94
95         free(tmp_msg);
96     }
97
98     /* Clear the memory */
99     FreeAlertData(al_data);
100
101     /* Send message to queue */
102     if (drop_it == 0) {
103         if (SendMSG(logr_queue, syslog_msg, logff[pos].file, LOCALFILE_MQ) < 0) {
104             merror(QUEUE_SEND, ARGV0);
105             if ((logr_queue = StartMQ(DEFAULTQPATH, WRITE)) < 0) {
106                 ErrorExit(QUEUE_FATAL, ARGV0, DEFAULTQPATH);
107             }
108         }
109     }
110
111     return (NULL);
112 }
113