| 1 | /* |
| 2 | * AIDE (Advanced Intrusion Detection Environment) |
| 3 | * |
| 4 | * Copyright (C) 1999-2006, 2010-2011, 2013, 2019-2025 Rami Lehti, |
| 5 | * Pablo Virolainen, Richard van den Berg, Hannes von Haugwitz |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of the |
| 10 | * License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include "aide.h" |
| 23 | #include <stdbool.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <errno.h> |
| 27 | #include <time.h> |
| 28 | #include "attributes.h" |
| 29 | #include "config.h" |
| 30 | #include "hashsum.h" |
| 31 | #include "url.h" |
| 32 | #include <stdlib.h> |
| 33 | #include "db.h" |
| 34 | #include "db_line.h" |
| 35 | #include "db_file.h" |
| 36 | #include "md.h" |
| 37 | |
| 38 | #ifdef WITH_CURL |
| 39 | #include "fopen.h" |
| 40 | #endif |
| 41 | |
| 42 | #include "db_config.h" |
| 43 | #include "log.h" |
| 44 | #include "be.h" |
| 45 | |
| 46 | #include "base64.h" |
| 47 | #include "util.h" |
| 48 | |
| 49 | static long readoct(char* s, database* db, char* field_name){ |
| 50 | long i; |
| 51 | char* e; |
| 52 | i=strtol(s,&e,8); |
| 53 | if (e[0]!='\0') { |
| 54 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "could not read '%s' from database: strtol (base: 8) failed for '%s'", field_name, s) |
| 55 | } |
| 56 | return i; |
| 57 | } |
| 58 | |
| 59 | static long readlong(char* s, database* db, char* field_name){ |
| 60 | long i; |
| 61 | char* e; |
| 62 | i=strtol(s,&e,10); |
| 63 | if (e[0]!='\0') { |
| 64 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "could not read '%s' from database: strtol failed for '%s'", field_name, s) |
| 65 | } |
| 66 | return i; |
| 67 | } |
| 68 | |
| 69 | #ifdef HAVE_FSTYPE |
| 70 | static unsigned long readunsignedlong(char* s, database* db, char* field_name){ |
| 71 | unsigned long i; |
| 72 | char* e; |
| 73 | i=strtoul(s,&e,10); |
| 74 | if (e[0]!='\0') { |
| 75 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "could not read '%s' from database: strtoul failed for '%s'", field_name, s) |
| 76 | return 0; |
| 77 | } |
| 78 | return i; |
| 79 | } |
| 80 | #endif |
| 81 | |
| 82 | static long long readlonglong(char* s, database* db, char* field_name){ |
| 83 | long long int i; |
| 84 | char* e; |
| 85 | i=strtoll(s,&e,10); |
| 86 | if (e[0]!='\0') { |
| 87 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "could not read '%s' from database: strtoll failed for '%s'", field_name, s) |
| 88 | } |
| 89 | return i; |
| 90 | } |
| 91 | |
| 92 | static time_t read_time_t(char *str, database *db, const char *field_name) { |
| 93 | char *decoded = NULL; |
| 94 | char *time_t_str = str; |
| 95 | if (strcmp(str, "0") == 0) { |
| 96 | return 0; |
| 97 | } |
| 98 | if (*str >= 'M' && *str <= 'O') { |
| 99 | /* handle legacy base64 representation */ |
| 100 | decoded = (char *) decode_base64(str, strlen(str), NULL); |
| 101 | if (decoded == NULL) { |
| 102 | /* warning is logged in decode_base64 */ |
| 103 | return 0; |
| 104 | } |
| 105 | time_t_str = decoded; |
| 106 | log_msg(LOG_LEVEL_TRACE, "read_time_t: base64 decoded '%s' to '%s'", str, decoded); |
| 107 | } |
| 108 | char *endp = NULL; |
| 109 | time_t t = strtol(time_t_str, &endp, 10); |
| 110 | if (endp[0] != '\0' || (errno = ERANGE && (t == LONG_MAX || t == LONG_MIN))) { |
| 111 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "could not read '%s' from database: strtol failed for '%s' (setting '%s' to 0)", field_name, time_t_str, field_name) |
| 112 | t = 0; |
| 113 | } else { |
| 114 | log_msg(LOG_LEVEL_TRACE, "read_time_t: converted '%s' '%s' to %lld ", field_name, time_t_str, (long long)t); |
| 115 | } |
| 116 | |
| 117 | free(decoded); |
| 118 | return t; |
| 119 | } |
| 120 | |
| 121 | static struct md_container *init_db_attrs(url_t *u) { |
| 122 | struct md_container *mdc = NULL; |
| 123 | if (conf->db_attrs) { |
| 124 | switch (u->type) { |
| 125 | case url_stdin: |
| 126 | case url_stdout: |
| 127 | case url_stderr: |
| 128 | case url_fd: |
| 129 | case url_file: |
| 130 | case url_http: |
| 131 | case url_https: |
| 132 | case url_ftp: { |
| 133 | mdc = checked_malloc(sizeof(struct md_container)); /* freed in close_db_attrs */ |
| 134 | mdc->todo_attr = conf->db_attrs; |
| 135 | init_md(mdc, u->value, NULL); |
| 136 | |
| 137 | break; |
| 138 | } |
| 139 | /* unsupported database types */ |
| 140 | case url_syslog: { |
| 141 | /* do nothing */ |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | return mdc; |
| 147 | } |
| 148 | |
| 149 | static db_line *close_db_attrs (database *db) { |
| 150 | db_line *line = NULL; |
| 151 | if (db->mdc != NULL) { |
| 152 | md_hashsums hs; |
| 153 | line = checked_malloc(sizeof(struct db_line)); |
| 154 | line->filename = (db->url)->value; |
| 155 | line->perm = 0; |
| 156 | line->attr = conf->db_attrs; |
| 157 | close_md(db->mdc, &hs, line->filename, NULL); |
| 158 | hashsums2line(&hs, line, NULL); |
| 159 | free(db->mdc); |
| 160 | } |
| 161 | return line; |
| 162 | } |
| 163 | |
| 164 | int db_init(database* db, bool readonly, bool gzip) { |
| 165 | void* fp = NULL; |
| 166 | |
| 167 | log_msg(LOG_LEVEL_TRACE,"db_init(): arguments: db=%p, gzip=%s", (void*) db, btoa(gzip)); |
| 168 | |
| 169 | db->mdc = init_db_attrs(db->url); |
| 170 | bool created = false; |
| 171 | fp=be_init(db->url, readonly, gzip, false, db->linenumber, db->filename, db->linebuf, &created); |
| 172 | if (created) { db->flags |= DB_FLAG_CREATED; } |
| 173 | if(fp==NULL) { |
| 174 | return RETFAIL; |
| 175 | } else { |
| 176 | #ifdef WITH_ZLIB |
| 177 | if (gzip) { |
| 178 | db->gzp = fp; |
| 179 | } else { |
| 180 | #endif |
| 181 | db->fp = fp; |
| 182 | #ifdef WITH_ZLIB |
| 183 | } |
| 184 | #endif |
| 185 | return RETOK; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | db_entry_t db_readline(database* db, bool include_limited_entries){ |
| 190 | return db_readline_file(db, include_limited_entries); |
| 191 | } |
| 192 | |
| 193 | byte* base64tobyte(char* src,int len,size_t *ret_len) |
| 194 | { |
| 195 | if(strcmp(src,"0")!=0){ |
| 196 | return decode_base64(src,len,ret_len); |
| 197 | } |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | static char *read_linkname(char *s) |
| 202 | { |
| 203 | if (s == NULL) |
| 204 | return (NULL); |
| 205 | |
| 206 | if (s[0] == '0') |
| 207 | { |
| 208 | if (s[1] == '\0') |
| 209 | return (NULL); |
| 210 | |
| 211 | if (s[1] == '-') |
| 212 | return (checked_strdup("")); |
| 213 | |
| 214 | if (s[1] == '0') { |
| 215 | s++; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | decode_string(s); |
| 220 | |
| 221 | return checked_strdup(s); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | #define CHAR2HASH(hash) \ |
| 226 | case attr_ ##hash : { \ |
| 227 | line->hashsums[hash_ ##hash]=base64tobyte(ss[db->fields[i]], \ |
| 228 | strlen(ss[db->fields[i]]), NULL); \ |
| 229 | log_msg(LOG_LEVEL_TRACE, "%s: copy " #hash " hashsum (%s) to %p", line->filename, ss[db->fields[i]], (void*) line->hashsums[hash_ ##hash]); \ |
| 230 | break; \ |
| 231 | } |
| 232 | |
| 233 | db_line* db_char2line(char** ss, database* db){ |
| 234 | |
| 235 | db_line* line=(db_line*)checked_malloc(sizeof(db_line)*1); |
| 236 | |
| 237 | line->perm=0; |
| 238 | line->uid=0; |
| 239 | line->gid=0; |
| 240 | line->atime=0; |
| 241 | line->ctime=0; |
| 242 | line->mtime=0; |
| 243 | line->inode=0; |
| 244 | line->nlink=0; |
| 245 | line->bcount=0; |
| 246 | line->size=0; |
| 247 | line->filename=NULL; |
| 248 | line->fullpath=NULL; |
| 249 | line->linkname=NULL; |
| 250 | #ifdef WITH_POSIX_ACL |
| 251 | line->acl=NULL; |
| 252 | #endif |
| 253 | #ifdef WITH_XATTR |
| 254 | line->xattrs=NULL; |
| 255 | #endif |
| 256 | line->e2fsattrs=0; |
| 257 | line->cntx=NULL; |
| 258 | line->capabilities=NULL; |
| 259 | |
| 260 | for (int i = 0 ; i < num_hashes ; ++i) { |
| 261 | line->hashsums[i]=NULL; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | line->attr=conf->attr; /* attributes from @@dbspec */ |
| 266 | |
| 267 | for(int i=0;i<db->num_fields;i++){ |
| 268 | |
| 269 | log_msg(LOG_LEVEL_TRACE, "db_char2line(): %ld[%d]: '%s' (%p)", db->lineno, i, ss[i], (void*) ss[i]); |
| 270 | |
| 271 | switch (db->fields[i]) { |
| 272 | case attr_filename : { |
| 273 | if(ss[db->fields[i]]!=NULL){ |
| 274 | line->fullpath=checked_strdup(ss[db->fields[i]]); |
| 275 | line->filename=line->fullpath; |
| 276 | } else { |
| 277 | log_msg(LOG_LEVEL_ERROR, "db_char2line(): error while reading database"); |
| 278 | exit(EXIT_FAILURE); |
| 279 | } |
| 280 | break; |
| 281 | } |
| 282 | case attr_linkname : { |
| 283 | line->linkname = read_linkname(ss[db->fields[i]]); |
| 284 | break; |
| 285 | } |
| 286 | case attr_mtime : { |
| 287 | line->mtime=read_time_t(ss[db->fields[i]], db, "mtime"); |
| 288 | break; |
| 289 | } |
| 290 | case attr_bcount : { |
| 291 | line->bcount=readlonglong(ss[db->fields[i]], db, "bcount"); |
| 292 | break; |
| 293 | } |
| 294 | case attr_atime : { |
| 295 | line->atime=read_time_t(ss[db->fields[i]], db, "atime"); |
| 296 | break; |
| 297 | } |
| 298 | case attr_ctime : { |
| 299 | line->ctime=read_time_t(ss[db->fields[i]], db, "ctime"); |
| 300 | break; |
| 301 | } |
| 302 | case attr_inode : { |
| 303 | line->inode=readlong(ss[db->fields[i]], db, "inode"); |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | case attr_uid : { |
| 308 | line->uid=readlong(ss[db->fields[i]], db, "uid"); |
| 309 | break; |
| 310 | } |
| 311 | case attr_gid : { |
| 312 | line->gid=readlong(ss[db->fields[i]], db, "gid"); |
| 313 | break; |
| 314 | } |
| 315 | case attr_size : { |
| 316 | line->size=readlonglong(ss[db->fields[i]], db, "size"); |
| 317 | break; |
| 318 | } |
| 319 | CHAR2HASH(md5) |
| 320 | CHAR2HASH(sha256) |
| 321 | CHAR2HASH(sha512) |
| 322 | CHAR2HASH(sha1) |
| 323 | CHAR2HASH(rmd160) |
| 324 | CHAR2HASH(tiger) |
| 325 | CHAR2HASH(crc32) |
| 326 | CHAR2HASH(crc32b) |
| 327 | CHAR2HASH(haval) |
| 328 | CHAR2HASH(whirlpool) |
| 329 | CHAR2HASH(gostr3411_94) |
| 330 | CHAR2HASH(stribog256) |
| 331 | CHAR2HASH(stribog512) |
| 332 | CHAR2HASH(sha512_256) |
| 333 | CHAR2HASH(sha3_256) |
| 334 | CHAR2HASH(sha3_512) |
| 335 | case attr_acl : { |
| 336 | #ifdef WITH_POSIX_ACL |
| 337 | char *tval = NULL; |
| 338 | |
| 339 | tval = strtok(ss[db->fields[i]], ","); |
| 340 | |
| 341 | line->acl = NULL; |
| 342 | |
| 343 | if (tval[0] == '0') |
| 344 | line->acl = NULL; |
| 345 | else if (!strcmp(tval, "POSIX")) |
| 346 | { |
| 347 | line->acl = checked_malloc(sizeof(acl_type)); |
| 348 | line->acl->acl_a = NULL; |
| 349 | line->acl->acl_d = NULL; |
| 350 | |
| 351 | tval = strtok(NULL, ","); |
| 352 | line->acl->acl_a = (char *)base64tobyte(tval, strlen(tval), NULL); |
| 353 | tval = strtok(NULL, ","); |
| 354 | line->acl->acl_d = (char *)base64tobyte(tval, strlen(tval), NULL); |
| 355 | } |
| 356 | /* else, it's broken... */ |
| 357 | #endif |
| 358 | break; |
| 359 | } |
| 360 | case attr_xattrs : { |
| 361 | #ifdef WITH_XATTR |
| 362 | size_t num = 0; |
| 363 | char *tval = NULL; |
| 364 | |
| 365 | tval = strtok(ss[db->fields[i]], ","); |
| 366 | num = readlong(tval, db, "xattrs"); |
| 367 | if (num) |
| 368 | { |
| 369 | line->xattrs = checked_malloc(sizeof(xattrs_type)); |
| 370 | line->xattrs->ents = checked_calloc(sizeof(xattr_node), num); |
| 371 | line->xattrs->sz = num; |
| 372 | line->xattrs->num = num; |
| 373 | num = 0; |
| 374 | while (num < line->xattrs->num) |
| 375 | { |
| 376 | tval = strtok(NULL, ","); |
| 377 | decode_string(tval); |
| 378 | line->xattrs->ents[num].key = checked_strdup(tval); |
| 379 | tval = strtok(NULL, ","); |
| 380 | if (strcmp(tval,"0") != 0) { |
| 381 | line->xattrs->ents[num].val = decode_base64(tval, strlen(tval), &line->xattrs->ents[num].vsz); |
| 382 | } else { |
| 383 | line->xattrs->ents[num].val = checked_strdup(""); |
| 384 | line->xattrs->ents[num].vsz = 0; |
| 385 | } |
| 386 | if (line->xattrs->ents[num].val == NULL) { |
| 387 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "error while reading xattrs for '%s' from database (discarding extended attributes)", line->filename) |
| 388 | for (int j = num; j >= 0 ; --j) { |
| 389 | free(line->xattrs->ents[j].key); |
| 390 | line->xattrs->ents[j].key = NULL; |
| 391 | free(line->xattrs->ents[j].val); |
| 392 | line->xattrs->ents[j].val = NULL; |
| 393 | } |
| 394 | line->xattrs->num = 0; |
| 395 | } else { |
| 396 | ++num; |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | #endif |
| 401 | break; |
| 402 | } |
| 403 | |
| 404 | case attr_selinux : { |
| 405 | byte *val = NULL; |
| 406 | |
| 407 | val = base64tobyte(ss[db->fields[i]], strlen(ss[db->fields[i]]),NULL); |
| 408 | line->cntx = (char *)val; |
| 409 | break; |
| 410 | } |
| 411 | |
| 412 | case attr_perm : { |
| 413 | line->perm=readoct(ss[db->fields[i]], db, "permissions"); |
| 414 | break; |
| 415 | } |
| 416 | |
| 417 | case attr_linkcount : { |
| 418 | line->nlink=readlong(ss[db->fields[i]], db, "nlink"); |
| 419 | break; |
| 420 | } |
| 421 | |
| 422 | case attr_attr : { |
| 423 | line->attr=readlonglong(ss[db->fields[i]], db, "attr"); |
| 424 | break; |
| 425 | } |
| 426 | |
| 427 | case attr_e2fsattrs : { |
| 428 | line->e2fsattrs=readlong(ss[db->fields[i]], db, "e2fsattrs"); |
| 429 | break; |
| 430 | } |
| 431 | |
| 432 | case attr_capabilities : { |
| 433 | byte *val = NULL; |
| 434 | |
| 435 | val = base64tobyte(ss[db->fields[i]], strlen(ss[db->fields[i]]),NULL); |
| 436 | line->capabilities = (char *)val; |
| 437 | break; |
| 438 | } |
| 439 | case attr_fs_type : { |
| 440 | #ifdef HAVE_FSTYPE |
| 441 | line->fs_type = readunsignedlong(ss[db->fields[i]], db, attributes[attr_fs_type].db_name); |
| 442 | #endif |
| 443 | break; |
| 444 | } |
| 445 | case attr_bsize : |
| 446 | case attr_sizeg : |
| 447 | case attr_rdev : |
| 448 | case attr_dev : |
| 449 | case attr_ftype : |
| 450 | case attr_checkinode : |
| 451 | case attr_allhashsums : |
| 452 | case attr_growing : |
| 453 | case attr_compressed : |
| 454 | case attr_allownewfile : |
| 455 | case attr_allowrmfile : { |
| 456 | /* no db field */ |
| 457 | break; |
| 458 | } |
| 459 | case attr_unknown : { |
| 460 | /* Unknown fields are ignored. */ |
| 461 | break; |
| 462 | } |
| 463 | |
| 464 | } |
| 465 | |
| 466 | } |
| 467 | |
| 468 | return line; |
| 469 | } |
| 470 | |
| 471 | int db_writespec(db_config* dbconf) |
| 472 | { |
| 473 | if( |
| 474 | #ifdef WITH_ZLIB |
| 475 | (dbconf->gzip_dbout && dbconf->database_out.gzp) || |
| 476 | #endif |
| 477 | (dbconf->database_out.fp!=NULL)){ |
| 478 | if(db_writespec_file(dbconf)==RETOK){ |
| 479 | return RETOK; |
| 480 | } |
| 481 | } |
| 482 | return RETFAIL; |
| 483 | } |
| 484 | |
| 485 | int db_writeline(db_line* line,db_config* dbconf){ |
| 486 | |
| 487 | if (line==NULL||dbconf==NULL) return RETOK; |
| 488 | |
| 489 | if ( |
| 490 | #ifdef WITH_ZLIB |
| 491 | (dbconf->gzip_dbout && dbconf->database_out.gzp) || |
| 492 | #endif |
| 493 | (dbconf->database_out.fp!=NULL)) { |
| 494 | if (db_writeline_file(line)==RETOK) { |
| 495 | return RETOK; |
| 496 | } |
| 497 | } |
| 498 | return RETFAIL; |
| 499 | } |
| 500 | |
| 501 | void db_close(void) { |
| 502 | if (conf->database_out.url) { |
| 503 | switch (conf->database_out.url->type) { |
| 504 | case url_stdin: |
| 505 | case url_stdout: |
| 506 | case url_stderr: |
| 507 | case url_fd: |
| 508 | case url_file: { |
| 509 | if ( |
| 510 | #ifdef WITH_ZLIB |
| 511 | (conf->gzip_dbout && conf->database_out.gzp) || |
| 512 | #endif |
| 513 | (conf->database_out.fp!=NULL)) { |
| 514 | db_close_file(conf); |
| 515 | } |
| 516 | break; |
| 517 | } |
| 518 | case url_http: |
| 519 | case url_https: |
| 520 | case url_ftp: |
| 521 | { |
| 522 | #ifdef WITH_CURL |
| 523 | if (conf->database_out.fp!=NULL) { |
| 524 | url_fclose(conf->database_out.fp); |
| 525 | conf->database_out.fp = NULL; |
| 526 | } |
| 527 | #endif /* WITH CURL */ |
| 528 | break; |
| 529 | } |
| 530 | /* unsupported database types */ |
| 531 | case url_syslog: { |
| 532 | /* do nothing */ |
| 533 | break; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | conf->database_in.db_line = close_db_attrs(&conf->database_in); |
| 538 | conf->database_out.db_line = close_db_attrs(&conf->database_out); |
| 539 | conf->database_new.db_line = close_db_attrs(&conf->database_new); |
| 540 | } |
| 541 | |
| 542 | void free_db_line(db_line* dl) |
| 543 | { |
| 544 | if (dl==NULL) { |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | #define checked_free(x) do { free(x); x=NULL; } while (0) |
| 549 | |
| 550 | for (int i = 0 ; i < num_hashes ; ++i) { |
| 551 | checked_free(dl->hashsums[i]); |
| 552 | } |
| 553 | |
| 554 | dl->filename=NULL; |
| 555 | checked_free(dl->fullpath); |
| 556 | checked_free(dl->linkname); |
| 557 | |
| 558 | #ifdef WITH_ACL |
| 559 | if (dl->acl) |
| 560 | { |
| 561 | free(dl->acl->acl_a); |
| 562 | free(dl->acl->acl_d); |
| 563 | } |
| 564 | checked_free(dl->acl); |
| 565 | #endif |
| 566 | |
| 567 | #ifdef WITH_XATTR |
| 568 | if (dl->xattrs) |
| 569 | free(dl->xattrs->ents); |
| 570 | checked_free(dl->xattrs); |
| 571 | checked_free(dl->cntx); |
| 572 | #endif |
| 573 | } |