Imported Upstream version 2.7
[ossec-hids.git] / src / analysisd / lists_make.c
1 /* @(#) $Id: ./src/analysisd/lists_make.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
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 3) as published by the FSF - Free Software
10  * Foundation
11  */
12
13 #include "shared.h"
14 #include "rules.h"
15 #include "cdb/cdb.h"
16 #include "cdb/cdb_make.h"
17 #include <fcntl.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <errno.h>
21 #include "lists_make.h"
22
23 void Lists_OP_MakeAll(int force)
24 {
25     ListNode *lnode = OS_GetFirstList();
26     while(lnode)
27     {
28         Lists_OP_MakeCDB(lnode->txt_filename,
29                          lnode->cdb_filename,
30                          force);
31         lnode = lnode->next;
32     }
33 }
34
35 void Lists_OP_MakeCDB(char *txt_filename, char *cdb_filename, int force)
36 {
37     /*
38     struct stat cdb_stat;
39     struct stat txt_stat;
40     */
41     struct cdb_make cdbm;
42     FILE *tmp_fd;
43     FILE *txt_fd;
44     char *tmp_str;
45     char *key, *val;
46     char str[OS_MAXSTR+1];
47
48     str[OS_MAXSTR]= '\0';
49     char tmp_filename[OS_MAXSTR];
50     tmp_filename[OS_MAXSTR - 2] = '\0';
51     snprintf(tmp_filename, OS_MAXSTR - 2, "%s.tmp", txt_filename);
52
53     /*
54     if((stat(txt_filename, &txt_stat)) == -1)
55         debug1("%s: stat of file %s failed", ARGV0, txt_filename);
56     if((stat(cdb_filename, &cdb_stat)) == -1)
57         debug1("%s: stat of file %s failed", ARGV0, cdb_filename);
58         */
59     if(File_DateofChange(txt_filename) > File_DateofChange(cdb_filename) ||
60        force)
61     {
62         printf(" * File %s need to be updated\n", cdb_filename);
63         tmp_fd = fopen(tmp_filename, "w+");
64         cdb_make_start(&cdbm, tmp_fd);
65         if(!(txt_fd = fopen(txt_filename, "r")))
66         {
67             merror(FOPEN_ERROR, ARGV0, txt_filename);
68             return;
69         }
70         while((fgets(str, OS_MAXSTR-1,txt_fd)) != NULL)
71         {
72             /* Removing new lines or carriage returns. */
73             tmp_str = strchr(str, '\r');
74             if(tmp_str)
75                 *tmp_str = '\0';
76             tmp_str = strchr(str, '\n');
77             if(tmp_str)
78                 *tmp_str = '\0';
79             if((val = strchr(str, ':')))
80             {
81                 *val = '\0';
82                 val++;
83             }
84             else
85             {
86                 continue;
87             }
88             key = str;
89             cdb_make_add(&cdbm, key, strlen(key), val, strlen(val));
90             if(force) print_out("  * adding - key: %s value: %s",key,val);
91         }
92         cdb_make_finish(&cdbm);
93         rename(tmp_filename,cdb_filename);
94     }
95     else
96     {
97         printf(" * File %s does not need to be compiled\n", cdb_filename);
98     }
99 }