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