| 1 | /* |
| 2 | * AIDE (Advanced Intrusion Detection Environment) |
| 3 | * |
| 4 | * Copyright (C) 1999-2007, 2010-2013, 2016, 2018-2025 Rami Lehti, |
| 5 | * Pablo Virolainen, Mike Markley, Richard van den Berg, |
| 6 | * Hannes von Haugwitz |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of the |
| 11 | * License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #include "config.h" |
| 24 | #include "aide.h" |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <stdio.h> |
| 29 | #include <time.h> |
| 30 | #include <stdarg.h> |
| 31 | #include <stdbool.h> |
| 32 | #include <unistd.h> |
| 33 | #include "db_config.h" |
| 34 | #include "gen_list.h" |
| 35 | #include "hashsum.h" |
| 36 | #include "log.h" |
| 37 | #include "progress.h" |
| 38 | #include "url.h" |
| 39 | #include "db.h" |
| 40 | #ifdef WITH_CURL |
| 41 | #include "fopen.h" |
| 42 | #endif |
| 43 | |
| 44 | #include "attributes.h" |
| 45 | |
| 46 | #include <errno.h> |
| 47 | |
| 48 | #include "base64.h" |
| 49 | #include "db_line.h" |
| 50 | #include "db_file.h" |
| 51 | #include "util.h" |
| 52 | #include "errorcodes.h" |
| 53 | |
| 54 | #ifdef WITH_ZLIB |
| 55 | #include <zlib.h> |
| 56 | #endif |
| 57 | |
| 58 | #define BUFSIZE 16384 |
| 59 | |
| 60 | #include "md.h" |
| 61 | |
| 62 | |
| 63 | |
| 64 | static bool db_parse_spec(database* db, char **saveptr){ |
| 65 | char *token = NULL; |
| 66 | |
| 67 | DB_ATTR_TYPE seen_attrs = 0LLU; |
| 68 | |
| 69 | db->fields = checked_malloc(1*sizeof(ATTRIBUTE)); |
| 70 | |
| 71 | while ((token = strtok_r(NULL, " ", saveptr)) != NULL) { |
| 72 | if (strncmp("@@", token, 2) == 0) { |
| 73 | LOG_DB_FORMAT_LINE(LOG_LEVEL_ERROR, "unexpected token while reading db_spec: '%s'", token); |
| 74 | return false; |
| 75 | } else { |
| 76 | ATTRIBUTE l; |
| 77 | db->fields = checked_realloc(db->fields, (db->num_fields+1)*sizeof(ATTRIBUTE)); |
| 78 | db->fields[db->num_fields]=attr_unknown; |
| 79 | for (l=0;l<num_attrs;l++){ |
| 80 | if (attributes[l].db_name && strcmp(attributes[l].db_name,token)==0) { |
| 81 | if (ATTR(l)&seen_attrs) { |
| 82 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "@@db_spec: skip redefined field '%s' at position %i", token, db->num_fields) |
| 83 | db->fields[db->num_fields]=attr_unknown; |
| 84 | } else { |
| 85 | db->fields[db->num_fields]=l; |
| 86 | seen_attrs |= ATTR(l); |
| 87 | LOG_DB_FORMAT_LINE(LOG_LEVEL_DEBUG, "@@db_spec: define field '%s' at position %i", token, db->num_fields) |
| 88 | } |
| 89 | db->num_fields++; |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | if(l==attr_unknown){ |
| 94 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "@@db_spec: skip unknown field '%s' at position %i", token, db->num_fields); |
| 95 | db->fields[db->num_fields]=attr_unknown; |
| 96 | db->num_fields++; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /* Lets generate attr from db_order if database does not have attr */ |
| 102 | conf->attr=DB_ATTR_UNDEF; |
| 103 | |
| 104 | for (int i=0;i<db->num_fields;i++) { |
| 105 | if (db->fields[i] == attr_attr) { |
| 106 | conf->attr=1; |
| 107 | } |
| 108 | } |
| 109 | if (conf->attr==DB_ATTR_UNDEF) { |
| 110 | conf->attr=0; |
| 111 | for(int i=0;i<db->num_fields;i++) { |
| 112 | conf->attr|=1LL<<db->fields[i]; |
| 113 | } |
| 114 | char *str; |
| 115 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "missing attr field, generated attr field from dbspec: %s (comparison may be incorrect)", str = diff_database_attributes(0, conf->attr)) |
| 116 | free(str); |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | static char *fgets_wrapper(char *ptr, size_t size, database *db) { |
| 122 | char * buf = NULL; |
| 123 | |
| 124 | #ifdef WITH_CURL |
| 125 | switch ((db->url)->type) { |
| 126 | case url_http: |
| 127 | case url_https: |
| 128 | case url_ftp: { |
| 129 | buf = url_fgets(ptr, size, (URL_FILE *)db->fp); |
| 130 | if (!buf) { |
| 131 | log_msg(LOG_LEVEL_ERROR, "url_fgets failed for %s", (db->url)->raw); |
| 132 | exit(IO_ERROR); |
| 133 | } |
| 134 | break; |
| 135 | } |
| 136 | default: |
| 137 | #endif |
| 138 | |
| 139 | #ifdef WITH_ZLIB |
| 140 | if (db->gzp == NULL) { |
| 141 | db->gzp=gzdopen(fileno((FILE *)db->fp),"rb"); |
| 142 | if (db->gzp == NULL) { |
| 143 | log_msg(LOG_LEVEL_ERROR, "gzdopen failed for %s", (db->url)->raw); |
| 144 | exit(IO_ERROR); |
| 145 | } |
| 146 | } |
| 147 | buf = gzgets(db->gzp, ptr, size); |
| 148 | if (!buf && !gzeof(db->gzp)) { |
| 149 | int gzerrnum; |
| 150 | log_msg(LOG_LEVEL_ERROR, "gzgets failed for %s: %s", (db->url)->raw, gzerror(db->gzp, &gzerrnum)); |
| 151 | exit(IO_ERROR); |
| 152 | } |
| 153 | #else |
| 154 | buf = fgets(ptr, size, db->fp); |
| 155 | if (ferror(db->fp)) { |
| 156 | log_msg(LOG_LEVEL_ERROR, "fgets failed for %s: %s", (db->url)->raw, strerror(errno)); |
| 157 | exit(IO_ERROR); |
| 158 | } |
| 159 | #endif |
| 160 | #ifdef WITH_CURL |
| 161 | } |
| 162 | #endif |
| 163 | if (buf && db->mdc) { |
| 164 | update_md(db->mdc, buf, strlen(buf)); |
| 165 | } |
| 166 | return buf; |
| 167 | } |
| 168 | |
| 169 | static char *get_next_dbline(database *db) { |
| 170 | char buffer[2048]; |
| 171 | char *line = NULL; |
| 172 | size_t len = 0; |
| 173 | |
| 174 | while (fgets_wrapper(buffer, sizeof(buffer), db) != NULL) { |
| 175 | size_t buffer_len = strlen(buffer); |
| 176 | line = checked_realloc(line, buffer_len + len + 1); |
| 177 | strncpy(line + len, buffer, buffer_len+1); |
| 178 | len += buffer_len; |
| 179 | /* remove newline character */ |
| 180 | if (line[len-1] == '\n') { |
| 181 | line[len-1] = '\0'; |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | return line; |
| 186 | } |
| 187 | |
| 188 | db_entry_t db_readline_file(database* db, bool include_limited_entries) { |
| 189 | char **s = NULL; |
| 190 | char *saveptr, *token; |
| 191 | char *line = NULL; |
| 192 | |
| 193 | db_entry_t entry = { .line = NULL, .limit = false }; |
| 194 | |
| 195 | while ((line = get_next_dbline(db)) != NULL) { |
| 196 | db->lineno++; |
| 197 | LOG_LEVEL db_parse_log_level = LOG_LEVEL_DEBUG; |
| 198 | switch (line[0]) { |
| 199 | case '#': |
| 200 | LOG_DB_FORMAT_LINE(db_parse_log_level, "db_read_file: skip comment line: '%s'", line) |
| 201 | break; |
| 202 | case '\0': |
| 203 | LOG_DB_FORMAT_LINE(db_parse_log_level, "%s", "db_read_file: skip empty line") |
| 204 | break; |
| 205 | default: |
| 206 | saveptr = NULL; |
| 207 | token = strtok_r(line, " ", &saveptr); |
| 208 | if (token == NULL) { |
| 209 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "skip line (no token found): '%s'", line) |
| 210 | break; |
| 211 | } else if (strcmp("@@db_spec", token) == 0) { |
| 212 | if (db->fields) { |
| 213 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "skip additional '%s' line", token) |
| 214 | } else { |
| 215 | LOG_DB_FORMAT_LINE(db_parse_log_level, "db_read_file: parse '%s'", token) |
| 216 | db_parse_spec(db, &saveptr); |
| 217 | } |
| 218 | } else if (strcmp("@@begin_db", token) == 0) { |
| 219 | if (db->flags&DB_FLAG_PARSE) { |
| 220 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "skip additional '%s' line", token) |
| 221 | } else { |
| 222 | LOG_DB_FORMAT_LINE(db_parse_log_level, "%s", "db_read_file: start reading database (found '@@begin_db')") |
| 223 | db->flags |= DB_FLAG_PARSE; |
| 224 | } |
| 225 | if ((token = strtok_r(NULL, "\n", &saveptr)) != NULL) { |
| 226 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "skip unexpected string after '@@begin_db': '%s'", token) |
| 227 | } |
| 228 | } else if (strcmp("@@end_db", token) == 0) { |
| 229 | if (db->flags&DB_FLAG_PARSE) { |
| 230 | db->flags &= ~(DB_FLAG_PARSE); |
| 231 | LOG_DB_FORMAT_LINE(db_parse_log_level, "%s", "db_read_file: stop reading database (found '@@end_db')") |
| 232 | if ((token = strtok_r(NULL, "\n", &saveptr)) != NULL) { |
| 233 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "skip unexpected string after '@@end_db': '%s'", token) |
| 234 | } |
| 235 | return entry; |
| 236 | } else { |
| 237 | LOG_DB_FORMAT_LINE(LOG_LEVEL_ERROR, "%s", "unexpected '@@end_db', expected '@@begin_db'") |
| 238 | exit(DATABASE_ERROR); |
| 239 | } |
| 240 | } else if (db->flags&DB_FLAG_PARSE) { |
| 241 | if (*token != '/') { |
| 242 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "skip line with invalid path: '%s'", token) |
| 243 | break; |
| 244 | } else { |
| 245 | decode_string(token); |
| 246 | if (check_limit(token, true, NULL)) { |
| 247 | if (include_limited_entries) { |
| 248 | entry.limit = true; |
| 249 | db_parse_log_level = LOG_LEVEL_LIMIT; |
| 250 | } else { |
| 251 | progress_status(PROGRESS_SKIPPED, NULL); |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | LOG_DB_FORMAT_LINE(db_parse_log_level, "db_read_file: parse '%s'", token) |
| 256 | s = checked_malloc(sizeof(char*)*num_attrs); |
| 257 | for(ATTRIBUTE j=0; j<num_attrs; j++){ |
| 258 | s[j]=NULL; |
| 259 | } |
| 260 | int i = 0; |
| 261 | while(token != NULL) { |
| 262 | if (token[0] == '#') { |
| 263 | LOG_DB_FORMAT_LINE(db_parse_log_level, "%s", "db_read_file: skip inline comment") |
| 264 | break; |
| 265 | } else if (i >= db->num_fields) { |
| 266 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "skip unexpected string '%s')", token); |
| 267 | } else if (db->fields[i] != attr_unknown) { |
| 268 | s[db->fields[i]] = checked_strdup(token); |
| 269 | LOG_DB_FORMAT_LINE(db_parse_log_level, "db_read_file: '%s' set field '%s' (position %d): '%s'", s[0], attributes[db->fields[i]].db_name, i, token) |
| 270 | } else { |
| 271 | LOG_DB_FORMAT_LINE(db_parse_log_level, "skip unknown/redefined field at position: %d: '%s'", i, token); |
| 272 | } |
| 273 | token = strtok_r(NULL, " ", &saveptr); |
| 274 | i++; |
| 275 | } |
| 276 | if (i<db->num_fields) { |
| 277 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "skip cutoff database line '%s' found (field '%s' (position: %d) is missing)", s[0], attributes[db->fields[i]].db_name, i) |
| 278 | for(int a=0;a<i;a++){ |
| 279 | free(s[db->fields[a]]); |
| 280 | s[db->fields[a]] = NULL; |
| 281 | } |
| 282 | free(s); |
| 283 | s = NULL; |
| 284 | } else { |
| 285 | entry.line = db_char2line(s, db); |
| 286 | for(int j=0;j<db->num_fields;j++){ |
| 287 | if(db->fields[j]!=attr_unknown && |
| 288 | s[db->fields[j]]!=NULL){ |
| 289 | free(s[db->fields[j]]); |
| 290 | s[db->fields[j]]=NULL; |
| 291 | } |
| 292 | } |
| 293 | free(s); |
| 294 | return entry; |
| 295 | } |
| 296 | } |
| 297 | } else { |
| 298 | LOG_DB_FORMAT_LINE(db_parse_log_level, "skip line '%s' ('@@begin_db' not (yet) found)", line) |
| 299 | } |
| 300 | } |
| 301 | free(line); |
| 302 | line = NULL; |
| 303 | } |
| 304 | if (db->flags&DB_FLAG_PARSE) { |
| 305 | LOG_DB_FORMAT_LINE(LOG_LEVEL_ERROR, "%s", "missing '@@end_db' (incomplete database file)") |
| 306 | exit(DATABASE_ERROR); |
| 307 | } else { |
| 308 | /* empty database */ |
| 309 | return entry; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | static int str_format(char *, int n, const char*, ...) |
| 314 | #ifdef __GNUC__ |
| 315 | __attribute__ ((format (printf, 3, 4))) |
| 316 | #endif |
| 317 | ; |
| 318 | static int str_format(char *str, int n, const char* format, ...) { |
| 319 | va_list ap; |
| 320 | va_start(ap, format); |
| 321 | int len = vsnprintf(NULL, 0, format, ap); |
| 322 | va_end(ap); |
| 323 | if (str) { |
| 324 | va_start(ap, format); |
| 325 | vsnprintf(&str[n], len+1, format, ap); |
| 326 | va_end(ap); |
| 327 | } |
| 328 | return len; |
| 329 | } |
| 330 | |
| 331 | static int str_filename(char *str, int n, char *path) { |
| 332 | char *safe_path = NULL; |
| 333 | int len = 0; |
| 334 | if (contains_unsafe(path)) { |
| 335 | safe_path = encode_string(path); |
| 336 | } |
| 337 | len = str_format(str, n+len, "%s", safe_path?safe_path:path); |
| 338 | free(safe_path); |
| 339 | return len; |
| 340 | } |
| 341 | |
| 342 | static int str_linkname(char *str, int n, char *path) { |
| 343 | char *safe_path = NULL; |
| 344 | int len = 0; |
| 345 | if (path == NULL) { |
| 346 | len = str_format(str, n, " %s", "0"); |
| 347 | return len; |
| 348 | } |
| 349 | if (*path == '\0') { |
| 350 | len = str_format(str, n, " %s", "0-"); |
| 351 | return len; |
| 352 | } |
| 353 | if (contains_unsafe(path)) { |
| 354 | safe_path = encode_string(path); |
| 355 | } |
| 356 | len += str_format(str, n, " %s%s", *path == '0'?"0":"", safe_path?safe_path:path); |
| 357 | free(safe_path); |
| 358 | return len; |
| 359 | } |
| 360 | |
| 361 | static int byte_base64(char *str, int n, byte *src, int src_len) { |
| 362 | char *enc = src ? encode_base64(src, src_len) : NULL; |
| 363 | int len = str_format(str, n, " %s", enc ? enc : "0"); |
| 364 | free(enc); |
| 365 | return len; |
| 366 | } |
| 367 | |
| 368 | #define CASE_BYTE_BASE64(attr, src, src_len) case attr : { n += byte_base64(str, n, src, src_len); break; } |
| 369 | |
| 370 | #define CASE_HASHSUM(x) CASE_BYTE_BASE64(attr_ ##x, line->hashsums[hash_ ##x], hashsums[hash_ ##x].length) |
| 371 | |
| 372 | #ifdef WITH_XATTR |
| 373 | static int str_xattr(char *str, int n, xattrs_type *xattrs) { |
| 374 | if (xattrs) { |
| 375 | size_t m = 0; |
| 376 | m = str_format(str, n, " %lu", xattrs->num); |
| 377 | xattr_node *xattr = xattrs->ents; |
| 378 | for (size_t i = xattrs->num; i > 0; --i) { |
| 379 | char *enc_key = NULL; |
| 380 | if (contains_unsafe(xattr->key)) { |
| 381 | enc_key = encode_string(xattr->key); |
| 382 | } |
| 383 | char *enc_value = encode_base64(xattr->val, xattr->vsz); |
| 384 | m += str_format(str, n + m, ",%s,%s", enc_key?enc_key:xattr->key, enc_value?enc_value:"0"); |
| 385 | free(enc_key); |
| 386 | free(enc_value); |
| 387 | ++xattr; |
| 388 | } |
| 389 | return m; |
| 390 | } else { |
| 391 | return str_format(str, n, " %lu", 0LU); |
| 392 | } |
| 393 | } |
| 394 | #endif |
| 395 | |
| 396 | #ifdef WITH_ACL |
| 397 | static int str_acl(char *str, int n, acl_type *acl) { |
| 398 | #ifdef WITH_POSIX_ACL |
| 399 | if (acl) { |
| 400 | char *enc_acl_a = acl->acl_a ? encode_base64((byte *)acl->acl_a, strlen(acl->acl_a)) : NULL; |
| 401 | char *enc_acl_d = acl->acl_d ? encode_base64((byte *)acl->acl_d, strlen(acl->acl_d)) : NULL; |
| 402 | int len = str_format(str, n, " %s,%s,%s", "POSIX", enc_acl_a ? enc_acl_a : "0", enc_acl_d ? enc_acl_d : "0"); |
| 403 | free(enc_acl_d); |
| 404 | free(enc_acl_a); |
| 405 | return len; |
| 406 | } else { |
| 407 | return str_format(str, n, " %lu", 0LU); |
| 408 | } |
| 409 | #endif |
| 410 | } |
| 411 | #endif |
| 412 | |
| 413 | static int construct_database_line(db_line *line, char *str) { |
| 414 | int n = 0; |
| 415 | |
| 416 | for (ATTRIBUTE i = 0; i < num_attrs; ++i) { |
| 417 | if (attributes[i].db_name && ATTR(i) & conf->db_out_attrs) { |
| 418 | switch (i) { |
| 419 | case attr_filename: { |
| 420 | n += str_filename(str, n, line->filename); |
| 421 | break; |
| 422 | } |
| 423 | case attr_attr: { |
| 424 | n += str_format(str, n, " %llu", line->attr); |
| 425 | break; |
| 426 | } |
| 427 | case attr_inode: { |
| 428 | n += str_format(str, n, " %li", line->inode); |
| 429 | break; |
| 430 | } |
| 431 | case attr_size: { |
| 432 | n += str_format(str, n, " %lli", line->size); |
| 433 | break; |
| 434 | } |
| 435 | case attr_bcount: { |
| 436 | n += str_format(str, n, " %lli", line->bcount); |
| 437 | break; |
| 438 | } |
| 439 | case attr_perm: { |
| 440 | n += str_format(str, n, " %lo", (long)line->perm); |
| 441 | break; |
| 442 | } |
| 443 | case attr_uid: { |
| 444 | n += str_format(str, n, " %li", line->uid); |
| 445 | break; |
| 446 | } |
| 447 | case attr_gid: { |
| 448 | n += str_format(str, n, " %li", line->gid); |
| 449 | break; |
| 450 | } |
| 451 | case attr_atime: { |
| 452 | n += str_format(str, n, " %ld", (long)line->atime); |
| 453 | break; |
| 454 | } |
| 455 | case attr_ctime: { |
| 456 | n += str_format(str, n, " %ld", (long)line->ctime); |
| 457 | break; |
| 458 | } |
| 459 | case attr_mtime: { |
| 460 | n += str_format(str, n, " %ld", (long)line->mtime); |
| 461 | break; |
| 462 | } |
| 463 | case attr_linkname: { |
| 464 | n += str_linkname(str, n, line->linkname); |
| 465 | break; |
| 466 | ; |
| 467 | } |
| 468 | case attr_linkcount: { |
| 469 | n += str_format(str, n, " %li", line->nlink); |
| 470 | break; |
| 471 | } |
| 472 | CASE_HASHSUM(md5) |
| 473 | CASE_HASHSUM(sha1) |
| 474 | CASE_HASHSUM(rmd160) |
| 475 | CASE_HASHSUM(tiger) |
| 476 | CASE_HASHSUM(crc32) |
| 477 | CASE_HASHSUM(crc32b) |
| 478 | CASE_HASHSUM(haval) |
| 479 | CASE_HASHSUM(gostr3411_94) |
| 480 | CASE_HASHSUM(stribog256) |
| 481 | CASE_HASHSUM(stribog512) |
| 482 | CASE_HASHSUM(sha256) |
| 483 | CASE_HASHSUM(sha512) |
| 484 | CASE_HASHSUM(whirlpool) |
| 485 | CASE_HASHSUM(sha512_256) |
| 486 | CASE_HASHSUM(sha3_256) |
| 487 | CASE_HASHSUM(sha3_512) |
| 488 | case attr_acl: { |
| 489 | #ifdef WITH_ACL |
| 490 | n += str_acl(str, n, line->acl); |
| 491 | #endif |
| 492 | break; |
| 493 | } |
| 494 | case attr_xattrs: { |
| 495 | #ifdef WITH_XATTR |
| 496 | n += str_xattr(str, n, line->xattrs); |
| 497 | #endif |
| 498 | break; |
| 499 | } |
| 500 | case attr_selinux: { |
| 501 | #ifdef WITH_SELINUX |
| 502 | n += byte_base64(str, n, (byte *)line->cntx, line->cntx ? strlen(line->cntx) : 0); |
| 503 | #endif |
| 504 | break; |
| 505 | } |
| 506 | case attr_e2fsattrs: { |
| 507 | #ifdef WITH_E2FSATTRS |
| 508 | n += str_format(str, n, " %lu", line->e2fsattrs); |
| 509 | #endif |
| 510 | break; |
| 511 | } |
| 512 | case attr_capabilities: { |
| 513 | #ifdef WITH_CAPABILITIES |
| 514 | n += byte_base64(str, n, (byte *)line->capabilities, |
| 515 | line->capabilities ? strlen(line->capabilities) : 0); |
| 516 | #endif |
| 517 | break; |
| 518 | } |
| 519 | case attr_fs_type : { |
| 520 | #ifdef HAVE_FSTYPE |
| 521 | n += str_format(str, n, " %llu", (unsigned long long) line->fs_type); |
| 522 | #endif |
| 523 | break; |
| 524 | } |
| 525 | case attr_ftype: |
| 526 | case attr_bsize: |
| 527 | case attr_rdev: |
| 528 | case attr_dev: |
| 529 | case attr_allhashsums: |
| 530 | case attr_sizeg: |
| 531 | case attr_checkinode: |
| 532 | case attr_allownewfile: |
| 533 | case attr_allowrmfile: |
| 534 | case attr_growing: |
| 535 | case attr_compressed: |
| 536 | case attr_unknown: { |
| 537 | /* nothing to write to database */ |
| 538 | break; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | if (str) { |
| 544 | snprintf(&str[n], 2, "\n"); |
| 545 | } |
| 546 | n++; |
| 547 | |
| 548 | return n; |
| 549 | } |
| 550 | |
| 551 | static void handle_io_error_on_write(char *function_str) { |
| 552 | if (conf->database_out.url->type == url_file && conf->database_out.flags&DB_FLAG_CREATED) { |
| 553 | log_msg(LOG_LEVEL_ERROR, "%s failed for %s (remove incompletely written database)", function_str, ((conf->database_out).url)->raw); |
| 554 | unlink(conf->database_out.url->value); |
| 555 | } else { |
| 556 | log_msg(LOG_LEVEL_ERROR, "%s failed for %s", function_str, ((conf->database_out).url)->raw); |
| 557 | } |
| 558 | exit(IO_ERROR); |
| 559 | } |
| 560 | |
| 561 | static void db_out_write(char * str, size_t len) { |
| 562 | if ((conf->database_out).mdc) { |
| 563 | update_md((conf->database_out).mdc, str, len); |
| 564 | } |
| 565 | |
| 566 | #ifdef WITH_ZLIB |
| 567 | if(conf->gzip_dbout) { |
| 568 | if (gzwrite((conf->database_out).gzp, str, len) < (int) len) { |
| 569 | handle_io_error_on_write("gzwrite"); |
| 570 | } |
| 571 | } else { |
| 572 | #endif |
| 573 | if (fwrite(str, sizeof(char), len, conf->database_out.fp) < len) { |
| 574 | handle_io_error_on_write("fwrite"); |
| 575 | } |
| 576 | if (fflush(conf->database_out.fp) != 0) { |
| 577 | handle_io_error_on_write("fflush"); |
| 578 | } |
| 579 | #ifdef WITH_ZLIB |
| 580 | } |
| 581 | #endif |
| 582 | } |
| 583 | |
| 584 | int db_writeline_file(db_line* line) { |
| 585 | |
| 586 | char *str = NULL; |
| 587 | int n = construct_database_line(line, str); |
| 588 | |
| 589 | str = checked_malloc(n+1); |
| 590 | construct_database_line(line, str); |
| 591 | |
| 592 | db_out_write(str, n); |
| 593 | |
| 594 | free(str); |
| 595 | |
| 596 | return RETOK; |
| 597 | } |
| 598 | |
| 599 | static int construct_database_header(db_config *dbconf, char *str) { |
| 600 | int n = 0; |
| 601 | |
| 602 | n += str_format(str, n, "%s", "@@begin_db\n"); |
| 603 | if (dbconf->database_add_metadata) { |
| 604 | time_t db_gen_time = time(NULL); |
| 605 | char *time_str = get_time_string(&db_gen_time); |
| 606 | n += str_format(str, n, |
| 607 | "# This file was generated by Aide, version %s\n" |
| 608 | "# Time of generation was %s\n", |
| 609 | conf->aide_version, time_str); |
| 610 | free(time_str); |
| 611 | } |
| 612 | if (dbconf->config_version) { |
| 613 | n += str_format(str, n, |
| 614 | "# The config version used to generate this file was:\n" |
| 615 | "# %s\n", |
| 616 | dbconf->config_version); |
| 617 | } |
| 618 | n += str_format(str, n, "%s", "@@db_spec"); |
| 619 | for (ATTRIBUTE i = 0; i < num_attrs; ++i) { |
| 620 | if (attributes[i].db_name && attributes[i].attr & conf->db_out_attrs) { |
| 621 | n += str_format(str, n, " %s", attributes[i].db_name); |
| 622 | } |
| 623 | } |
| 624 | n += str_format(str, n, "%s", "\n"); |
| 625 | return n; |
| 626 | } |
| 627 | |
| 628 | int db_writespec_file(db_config* dbconf) { |
| 629 | char *str = NULL; |
| 630 | int n = construct_database_header(dbconf, str); |
| 631 | |
| 632 | str = checked_malloc(n+1); |
| 633 | construct_database_header(dbconf, str); |
| 634 | |
| 635 | db_out_write(str, n); |
| 636 | |
| 637 | free(str); |
| 638 | |
| 639 | return RETOK; |
| 640 | } |
| 641 | |
| 642 | int db_close_file(db_config* dbconf){ |
| 643 | |
| 644 | if(dbconf->database_out.fp |
| 645 | #ifdef WITH_ZLIB |
| 646 | || dbconf->database_out.gzp |
| 647 | #endif |
| 648 | ){ |
| 649 | char *end_db_str = "@@end_db\n"; |
| 650 | db_out_write(end_db_str, strlen(end_db_str)); |
| 651 | } |
| 652 | |
| 653 | #ifdef WITH_ZLIB |
| 654 | if(dbconf->gzip_dbout){ |
| 655 | if(gzclose(dbconf->database_out.gzp)){ |
| 656 | log_msg(LOG_LEVEL_ERROR,"unable to gzclose database '%s': %s", (dbconf->database_out.url)->raw, strerror(errno)); |
| 657 | return RETFAIL; |
| 658 | } |
| 659 | dbconf->database_out.gzp = NULL; |
| 660 | }else { |
| 661 | #endif |
| 662 | if(fclose(dbconf->database_out.fp)){ |
| 663 | log_msg(LOG_LEVEL_ERROR,"unable to close database '%s': %s", (dbconf->database_out.url)->raw, strerror(errno)); |
| 664 | return RETFAIL; |
| 665 | } |
| 666 | dbconf->database_out.fp = NULL; |
| 667 | #ifdef WITH_ZLIB |
| 668 | } |
| 669 | #endif |
| 670 | |
| 671 | return RETOK; |
| 672 | } |
| 673 | // vi: ts=8 sw=8 |