1 /* @(#) $Id: ./src/analysisd/decoders/hostinfo.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
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
14 /* Hostinfo decoder */
18 #include "os_regex/os_regex.h"
19 #include "eventinfo.h"
20 #include "alerts/alerts.h"
23 #define HOSTINFO_FILE "/queue/fts/hostinfo"
24 #define HOST_HOST "Host: "
25 #define HOST_PORT " open ports: "
27 #define HOST_CHANGED "Host information changed."
28 #define HOST_NEW "New host information added."
29 #define PREV_OPEN "Previously"
32 /** Global variables **/
36 char _hi_buf[OS_MAXSTR +1];
40 /* Hostinfo decoder */
41 OSDecoderInfo *hostinfo_dec = NULL;
45 /* Check if the string matches.
47 static char *__go_after(char *x, char *y)
52 /* X and Y must be not null */
64 /* String does not match */
65 if(strncmp(x,y,y_s) != 0)
78 * Initialize the necessary information to process the host information
86 os_calloc(1, sizeof(OSDecoderInfo), hostinfo_dec);
87 hostinfo_dec->id = getDecoderfromlist(HOSTINFO_MOD);
88 hostinfo_dec->type = OSSEC_RL;
89 hostinfo_dec->name = HOSTINFO_MOD;
90 hostinfo_dec->fts = 0;
91 id_new = getDecoderfromlist(HOSTINFO_NEW);
92 id_mod = getDecoderfromlist(HOSTINFO_MOD);
96 /* Opening HOSTINFO_FILE */
97 snprintf(_hi_buf,OS_SIZE_1024, "%s", HOSTINFO_FILE);
100 /* r+ to read and write. Do not truncate */
101 _hi_fp = fopen(_hi_buf,"r+");
104 /* try opening with a w flag, file probably does not exist */
105 _hi_fp = fopen(_hi_buf, "w");
109 _hi_fp = fopen(_hi_buf, "r+");
114 merror(FOPEN_ERROR, ARGV0, _hi_buf);
119 /* clearing the buffer */
120 memset(_hi_buf, '\0', OS_MAXSTR +1);
128 * Return the file pointer to be used
134 fseek(_hi_fp, 0, SEEK_SET);
143 /* Special decoder for Hostinformation
144 * Not using the default rendering tools for simplicity
145 * and to be less resource intensive.
147 int DecodeHostinfo(Eventinfo *lf)
156 char buffer[OS_MAXSTR + 1];
157 char opened[OS_MAXSTR + 1];
161 /* Checking maximum number of errors */
164 merror("%s: Too many errors handling host information db. "
165 "Ignoring it.", ARGV0);
170 /* Zeroing buffers */
171 buffer[OS_MAXSTR] = '\0';
172 opened[OS_MAXSTR] = '\0';
176 merror("%s: Error handling host information database.",ARGV0);
177 hi_err++; /* Increment hi error */
183 /* Copying log to buffer */
184 strncpy(buffer,lf->log, OS_MAXSTR);
188 tmpstr = __go_after(buffer, HOST_HOST);
191 merror("%s: Error handling host information database.",ARGV0);
200 tmpstr = strchr(tmpstr, ',');
203 merror("%s: Error handling host information database.",ARGV0);
213 /* Getting ip only information -- to store */
214 tmpstr = strchr(ip, ' ');
219 bf_size = strlen(ip);
222 /* Reads the file and search for a possible
225 while(fgets(_hi_buf, OS_MAXSTR -1, fp) != NULL)
227 /* Ignore blank lines and lines with a comment */
228 if(_hi_buf[0] == '\n' || _hi_buf[0] == '#')
233 /* Removing new line */
234 tmpstr = strchr(_hi_buf, '\n');
239 /* Checking for ip */
240 if(strncmp(ip, _hi_buf, bf_size) == 0)
242 /* Cannot use strncmp to avoid errors with crafted files */
243 if(strcmp(portss, _hi_buf + bf_size) == 0)
251 tmp_ports = _hi_buf + (bf_size +1);
252 snprintf(opened, OS_MAXSTR, "%s %s", PREV_OPEN, tmp_ports);
259 /* Adding the new entry at the end of the file */
260 fseek(fp, 0, SEEK_END);
261 fprintf(fp,"%s%s\n", ip, portss);
264 /* Setting decoder */
265 lf->decoder_info = hostinfo_dec;
268 /* Setting comment */
271 hostinfo_dec->id = id_mod;
272 //lf->generated_rule->last_events[0] = opened;
276 hostinfo_dec->id = id_new;