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