source: trunk/src/sh_dbCreate.c@ 538

Last change on this file since 538 was 481, checked in by katerina, 9 years ago

Enhancements and fixes for tickets #374, #375, #376, #377, #378, and #379.

File size: 5.3 KB
Line 
1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 2015 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config_xor.h"
21
22#include <stdio.h>
23#include <string.h>
24#include <ctype.h>
25
26#include "samhain.h"
27#include "sh_utils.h"
28#include "sh_hash.h"
29#include "sh_files.h"
30
31#include "sh_dbIO.h"
32#include "sh_dbIO_int.h"
33#include "sh_pthread.h"
34#include "sh_guid.h"
35
36#undef FIL__
37#define FIL__ _("sh_dbCreate.c")
38
39#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
40
41static int dbCreate_writeout()
42{
43 char uuid[SH_UUID_BUF];
44 char * path;
45 int retval;
46
47 if (sh.outpath == NULL || sh.outpath[0] == '\0')
48 {
49 sh_uuid_generate_random(uuid, sizeof(uuid));
50 path = sh_util_strconcat(_("file."), sh.host.name, ".", uuid, NULL);
51 }
52 else
53 path = sh_util_strdup(sh.outpath);
54
55 retval = sh_dbIO_writeout_to_path(path);
56 SH_FREE(path);
57 return retval;
58}
59
60static void dbCreate_run_filecheck(unsigned long add_mask, char * str)
61{
62 int status;
63
64 int reported = 0;
65 unsigned long check_flags = (MASK_READONLY_ | MODI_INIT | add_mask);
66 char * dir_name = sh_util_dirname(str);
67 char * file_name = sh_util_basename(str);
68
69 status = sh_files_filecheck (SH_LEVEL_READONLY, check_flags,
70 dir_name, file_name, &reported, 0);
71
72 if (status == SH_FILE_UNKNOWN)
73 {
74 sh_hash_insert_null(str);
75 }
76
77 return;
78}
79
80static int dbCreate_filecheck(char * str)
81{
82 unsigned long add_mask = 0;
83
84 if (*str == '+')
85 {
86 add_mask = MODI_TXT;
87 ++str; while (isspace((int)*str)) ++str;
88 }
89 if (*str != '/')
90 {
91 char * tmp = sh_util_safe_name (str);
92 sh_error_handle((-1), FIL__, __LINE__, EINVAL, MSG_E_SUBGPATH,
93 _("Not an absolute path"),
94 _("dbCreate_filecheck"), tmp);
95 SH_FREE(tmp);
96 return -1;
97 }
98 dbCreate_run_filecheck(add_mask, str);
99 return 0;
100}
101
102char * rtrim(char * str)
103{
104 size_t len;
105
106 if (!str)
107 return str;
108
109 len = strlen(str);
110 while (len > 0)
111 {
112 --len;
113 if (str[len] == '\n' || str[len] == '\r')
114 str[len] = '\0';
115 else
116 break;
117 }
118
119 return str;
120}
121
122static int dbCreate_loop(FILE * fd)
123{
124 int status, retval = 0;
125 size_t linesize = MAX_PATH_STORE+2;
126 char * line = SH_ALLOC(linesize);
127
128 do {
129 status = sh_dbIO_getline(fd, line, linesize);
130
131 if (status > 0)
132 {
133 char * str = rtrim(line);
134 while (isspace((int)*str)) ++str;
135 if (*str != '#')
136 {
137 int fstatus = -1;
138 size_t len = 0;
139 char * p = sh_files_parse_input(str, &len);
140
141 if (p)
142 {
143 fstatus = dbCreate_filecheck(p);
144 SH_FREE(p);
145 }
146 if (fstatus != 0)
147 retval = -1;
148 }
149 }
150 } while (status != -1);
151
152 SH_FREE(line);
153 return retval;
154}
155
156static FILE * dbCreate_open (const char * path)
157{
158 FILE * fd = fopen(path, "r");
159 if (!fd)
160 {
161 int error = errno;
162 char * tmp = sh_util_safe_name (path);
163 sh_error_handle((-1), FIL__, __LINE__, error, MSG_E_SUBGPATH,
164 _("Cannot open file for read"),
165 _("dbCreate_open"), tmp);
166 SH_FREE(tmp);
167 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
168 }
169 return fd;
170}
171
172static void dbCreate_setup()
173{
174 sh_hash_set_initialized();
175 sh.flag.isdaemon = S_FALSE;
176 sh.flag.loop = S_FALSE;
177 sh.flag.update = S_FALSE;
178 sh.flag.checkSum = SH_CHECK_CHECK;
179
180 sh.statistics.files_report = 0;
181 ShDFLevel[SH_ERR_T_FILE] = SH_ERR_SEVERE;
182 ShDFLevel[SH_ERR_T_RO] = SH_ERR_SEVERE;
183 ShDFLevel[SH_ERR_T_NAME] = SH_ERR_SEVERE;
184
185 sh_error_only_stderr (S_TRUE);
186 sh_error_setprint(_("none"));
187
188 return;
189}
190
191
192int sh_dbCreate (const char * path)
193{
194 FILE * fd;
195
196 /* Initialize application status
197 */
198 dbCreate_setup();
199
200 /* Open file list
201 */
202 fd = dbCreate_open(path);
203
204 /* Load the database
205 */
206 sh_hash_init_and_checksum();
207
208 /* Loop over file list to check files.
209 */
210 dbCreate_loop(fd);
211
212 /* Close file list
213 */
214 fclose(fd);
215
216 /* Write out database
217 */
218 if (0 != dbCreate_writeout())
219 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
220
221 /* Exit on success.
222 */
223 aud_exit(FIL__, __LINE__, EXIT_SUCCESS);
224 return 0;
225}
226
227#endif
Note: See TracBrowser for help on using the repository browser.