1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
|
/*
Copyright 2019 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <cf3.defs.h>
#include <lastseen.h>
#include <conversion.h>
#include <hash.h>
#include <locks.h>
#include <item_lib.h>
#include <known_dirs.h>
#ifdef LMDB
#include <lmdb.h>
#endif
void UpdateLastSawHost(const char *hostkey, const char *address,
bool incoming, time_t timestamp);
/*
* Lastseen database schema (version 1):
*
* Version entry
*
* key: "version\0"
* value: "1\0"
*
* "Quality of connection" entries
*
* key: q<direction><hostkey> (direction: 'i' for incoming, 'o' for outgoing)
* value: struct KeyHostSeen
*
* "Hostkey" entries
*
* key: k<hostkey> ("MD5-ffffefefeefef..." or "SHA-abacabaca...")
* value: <address> (IPv4 or IPv6)
*
* "Address", or reverse, entries (auxiliary)
*
* key: a<address> (IPv6 or IPv6)
* value: <hostkey>
*
*
*
* Schema version 0 mapped direction + hostkey to address + quality of
* connection. This approach had a number of drawbacks:
* - There were two potentially conflicting addresses for given hostkey.
* - There was no way to quickly lookup hostkey by address.
* - Address update required traversal of the whole database.
*
* In order to overcome these limitations, new schema normalized (in relational
* algebra sense) the data relations.
*/
/* TODO #ifndef NDEBUG check, report loudly, and fix consistency issues in every operation. */
/*****************************************************************************/
/**
* @brief Same as LastSaw() but the digest parameter is the hash as a
* "SHA=..." string, to avoid converting twice.
*/
void LastSaw1(const char *ipaddress, const char *hashstr,
LastSeenRole role)
{
const char *mapip = MapAddress(ipaddress);
UpdateLastSawHost(hashstr, mapip, role == LAST_SEEN_ROLE_ACCEPT, time(NULL));
}
void LastSaw(const char *ipaddress, const char *digest, LastSeenRole role)
{
char databuf[CF_HOSTKEY_STRING_SIZE];
if (strlen(ipaddress) == 0)
{
Log(LOG_LEVEL_INFO, "LastSeen registry for empty IP with role %d", role);
return;
}
HashPrintSafe(databuf, sizeof(databuf), digest, CF_DEFAULT_DIGEST, true);
const char *mapip = MapAddress(ipaddress);
UpdateLastSawHost(databuf, mapip, role == LAST_SEEN_ROLE_ACCEPT, time(NULL));
}
/*****************************************************************************/
void UpdateLastSawHost(const char *hostkey, const char *address,
bool incoming, time_t timestamp)
{
DBHandle *db = NULL;
if (!OpenDB(&db, dbid_lastseen))
{
Log(LOG_LEVEL_ERR, "Unable to open last seen db");
return;
}
/* Update quality-of-connection entry */
char quality_key[CF_BUFSIZE];
snprintf(quality_key, CF_BUFSIZE, "q%c%s", incoming ? 'i' : 'o', hostkey);
KeyHostSeen newq = { .lastseen = timestamp };
KeyHostSeen q;
if (ReadDB(db, quality_key, &q, sizeof(q)))
{
newq.Q = QAverage(q.Q, newq.lastseen - q.lastseen, 0.4);
}
else
{
/* FIXME: more meaningful default value? */
newq.Q = QDefinite(0);
}
WriteDB(db, quality_key, &newq, sizeof(newq));
/* Update forward mapping */
char hostkey_key[CF_BUFSIZE];
snprintf(hostkey_key, CF_BUFSIZE, "k%s", hostkey);
WriteDB(db, hostkey_key, address, strlen(address) + 1);
/* Update reverse mapping */
char address_key[CF_BUFSIZE];
snprintf(address_key, CF_BUFSIZE, "a%s", address);
WriteDB(db, address_key, hostkey, strlen(hostkey) + 1);
CloseDB(db);
}
/*****************************************************************************/
/* Lookup a reverse entry (IP->KeyHash) in lastseen database. */
static bool Address2HostkeyInDB(DBHandle *db, const char *address, char *result, size_t result_size)
{
char address_key[CF_BUFSIZE];
char hostkey[CF_BUFSIZE];
/* Address key: "a" + address */
snprintf(address_key, CF_BUFSIZE, "a%s", address);
if (!ReadDB(db, address_key, &hostkey, sizeof(hostkey)))
{
return false;
}
#ifndef NDEBUG
/* Check for inconsistencies. Return success even if db is found
* inconsistent, since the reverse entry is already found. */
char hostkey_key[CF_BUFSIZE];
char back_address[CF_BUFSIZE];
/* Hostkey key: "k" + hostkey */
snprintf(hostkey_key, CF_BUFSIZE, "k%s", hostkey);
if (!ReadDB(db, hostkey_key, &back_address, sizeof(back_address)))
{
Log(LOG_LEVEL_WARNING, "Lastseen db inconsistency: "
"no key entry '%s' for existing host entry '%s'",
hostkey_key, address_key);
}
#endif
strlcpy(result, hostkey, result_size);
return true;
}
/*****************************************************************************/
/* Given an address it returns a key - its own key if address is 127.0.0.1,
* else it looks the "aADDRESS" entry in lastseen. */
bool Address2Hostkey(char *dst, size_t dst_size, const char *address)
{
bool retval = false;
dst[0] = '\0';
if ((strcmp(address, "127.0.0.1") == 0) ||
(strcmp(address, "::1") == 0) ||
(strcmp(address, VIPADDRESS) == 0))
{
Log(LOG_LEVEL_DEBUG,
"Address2Hostkey: Returning local key for address %s",
address);
if (PUBKEY)
{
unsigned char digest[EVP_MAX_MD_SIZE + 1];
HashPubKey(PUBKEY, digest, CF_DEFAULT_DIGEST);
HashPrintSafe(dst, dst_size, digest,
CF_DEFAULT_DIGEST, true);
retval = true;
}
else
{
Log(LOG_LEVEL_VERBOSE,
"Local key not found, generate one with cf-key?");
retval = false;
}
}
else /* lastseen lookup */
{
DBHandle *db;
if (OpenDB(&db, dbid_lastseen))
{
retval = Address2HostkeyInDB(db, address, dst, dst_size);
CloseDB(db);
if (!retval)
{
Log(LOG_LEVEL_VERBOSE,
"Key digest for address '%s' was not found in lastseen db!",
address);
}
}
}
return retval;
}
/**
* @brief detects whether input is a host/ip name or a key digest
*
* @param[in] key digest (SHA/MD5 format) or free host name string
* (character '=' is optional but recommended)
* @retval true if a key digest, false otherwise
*/
static bool IsDigestOrHost(const char *input)
{
if (strncmp(input, "SHA=", 3) == 0)
{
return true;
}
else if (strncmp(input, "MD5=", 3) == 0)
{
return true;
}
else
{
return false;
}
}
/**
* @brief check whether the lastseen DB is coherent or not.
*
* It is allowed for a aIP1 -> KEY1 to not have a reverse kKEY1 -> IP.
* kKEY1 *must* exist, but may point to another IP.
* Same for IP values, they *must* appear as aIP entries, but we don't
* care where they point to.
* So for every aIP->KEY1 entry there should be a kKEY1->whatever entry.
* And for every kKEY->IP1 entry there should be a aIP1->whatever entry.
*
* If a host changes IP, then we have a new entry aIP2 -> KEY1 together
* with the aIP1 -> KEY1 entry. ALLOWED.
*
* If a host changes key, then its entry will become aIP1 -> KEY2.
* Then still it will exist kKEY1 -> IP1 but also kKEY2 -> IP1. ALLOWED
*
* Can I have a IP value of some kKEY that does not have any aIP entry?
* NO because at some time aIP it was written in the database.
* SO EVERY kIP must be found in aIPS.
* kIPS SUBSET OF aIPS
*
* Can I have a KEY value of some aIP that does not have any kKEY entry?
* NO for the same reason.
* SO EVERY akey must be found in kkeys.
* aKEYS SUBSET OF kKEYS
*
* FIN
*
* @TODO P.S. redesign lastseen. Really, these whole requirements are
* implemented on top of a simple key-value store, no wonder it's such a
* mess. I believe that reverse mapping is not even needed since only
* aIP entries are ever looked up. kKEY entries can be deprecated and
* forget all the false notion of "schema consistency" in this key-value
* store...
*
* @retval true if the lastseen DB is coherent, false otherwise.
*/
bool IsLastSeenCoherent(void)
{
DBHandle *db;
DBCursor *cursor;
if (!OpenDB(&db, dbid_lastseen))
{
char *db_path = DBIdToPath(dbid_lastseen);
Log(LOG_LEVEL_ERR, "Unable to open lastseen database '%s'", db_path);
free(db_path);
return false;
}
if (!NewDBCursor(db, &cursor))
{
Log(LOG_LEVEL_ERR, "Unable to create lastseen database cursor");
CloseDB(db);
return false;
}
char *key;
void *value;
int ksize, vsize;
Item *qKEYS = NULL;
Item *aKEYS = NULL;
Item *kKEYS = NULL;
Item *aIPS = NULL;
Item *kIPS = NULL;
bool result = true;
while (NextDB(cursor, &key, &ksize, &value, &vsize))
{
if (strcmp(key, "version") != 0 &&
strncmp(key, "qi", 2) != 0 &&
strncmp(key, "qo", 2) != 0 &&
key[0] != 'k' &&
key[0] != 'a')
{
Log(LOG_LEVEL_WARNING,
"lastseen db inconsistency, unexpected key: %s",
key);
result = false;
}
if (key[0] == 'q' )
{
if (strncmp(key,"qiSHA=",5)==0 || strncmp(key,"qoSHA=",5)==0 ||
strncmp(key,"qiMD5=",5)==0 || strncmp(key,"qoMD5=",5)==0)
{
if (!IsItemIn(qKEYS, key+2))
{
PrependItem(&qKEYS, key+2, NULL);
}
}
}
if (key[0] == 'k' )
{
if (strncmp(key, "kSHA=", 4)==0 || strncmp(key, "kMD5=", 4)==0)
{
if (!IsItemIn(kKEYS, key+1))
{
PrependItem(&kKEYS, key+1, NULL);
}
if (!IsItemIn(kIPS, value))
{
PrependItem(&kIPS, value, NULL);
}
}
}
if (key[0] == 'a' )
{
if (!IsItemIn(aIPS, key+1))
{
PrependItem(&aIPS, key+1, NULL);
}
if (!IsItemIn(aKEYS, value))
{
PrependItem(&aKEYS, value, NULL);
}
}
}
DeleteDBCursor(cursor);
CloseDB(db);
/* For every kKEY->IP1 entry there should be a aIP1->whatever entry.
* So basically: kIPS SUBSET OF aIPS. */
Item *kip = kIPS;
while (kip != NULL)
{
if (!IsItemIn(aIPS, kip->name))
{
Log(LOG_LEVEL_WARNING,
"lastseen db inconsistency, found kKEY -> '%s' entry, "
"but no 'a%s' -> any key entry exists!",
kip->name, kip->name);
result = false;
}
kip = kip->next;
}
/* For every aIP->KEY1 entry there should be a kKEY1->whatever entry.
* So basically: aKEYS SUBSET OF kKEYS. */
Item *akey = aKEYS;
while (akey != NULL)
{
if (!IsItemIn(kKEYS, akey->name))
{
Log(LOG_LEVEL_WARNING,
"lastseen db inconsistency, found aIP -> '%s' entry, "
"but no 'k%s' -> any ip entry exists!",
akey->name, akey->name);
result = false;
}
akey = akey->next;
}
DeleteItemList(qKEYS);
DeleteItemList(aKEYS);
DeleteItemList(kKEYS);
DeleteItemList(aIPS);
DeleteItemList(kIPS);
return result;
}
/**
* @brief removes all traces of host 'ip' from lastseen DB
*
* @param[in] ip : either in (SHA/MD5 format)
* @param[in,out] digest: return corresponding digest of input host.
* If NULL, return nothing
* @param[in] digest_size: size of digest parameter
* @retval true if entry was deleted, false otherwise
*/
bool DeleteIpFromLastSeen(const char *ip, char *digest, size_t digest_size)
{
DBHandle *db;
bool res = false;
if (!OpenDB(&db, dbid_lastseen))
{
char *db_path = DBIdToPath(dbid_lastseen);
Log(LOG_LEVEL_ERR, "Unable to open lastseen database '%s'", db_path);
free(db_path);
return false;
}
char bufkey[CF_BUFSIZE + 1];
char bufhost[CF_BUFSIZE + 1];
strcpy(bufhost, "a");
strlcat(bufhost, ip, CF_BUFSIZE);
char key[CF_BUFSIZE];
if (ReadDB(db, bufhost, &key, sizeof(key)) == true)
{
strcpy(bufkey, "k");
strlcat(bufkey, key, CF_BUFSIZE);
if (HasKeyDB(db, bufkey, strlen(bufkey) + 1) == false)
{
res = false;
goto clean;
}
else
{
if (digest != NULL)
{
strlcpy(digest, bufkey + 1, digest_size);
}
DeleteDB(db, bufkey);
DeleteDB(db, bufhost);
res = true;
}
}
else
{
res = false;
goto clean;
}
strcpy(bufkey, "qi");
strlcat(bufkey, key, CF_BUFSIZE);
DeleteDB(db, bufkey);
strcpy(bufkey, "qo");
strlcat(bufkey, key, CF_BUFSIZE);
DeleteDB(db, bufkey);
clean:
CloseDB(db);
return res;
}
/**
* @brief removes all traces of key digest 'key' from lastseen DB
*
* @param[in] key : either in (SHA/MD5 format)
* @param[in,out] ip : return the key corresponding host.
* If NULL, return nothing
* @param[in] ip_size : length of ip parameter
* @retval true if entry was deleted, false otherwise
*/
bool DeleteDigestFromLastSeen(const char *key, char *ip, size_t ip_size)
{
DBHandle *db;
bool res = false;
if (!OpenDB(&db, dbid_lastseen))
{
char *db_path = DBIdToPath(dbid_lastseen);
Log(LOG_LEVEL_ERR, "Unable to open lastseen database '%s'", db_path);
free(db_path);
return false;
}
char bufkey[CF_BUFSIZE + 1];
char bufhost[CF_BUFSIZE + 1];
strcpy(bufkey, "k");
strlcat(bufkey, key, CF_BUFSIZE);
char host[CF_BUFSIZE];
if (ReadDB(db, bufkey, &host, sizeof(host)) == true)
{
strcpy(bufhost, "a");
strlcat(bufhost, host, CF_BUFSIZE);
if (HasKeyDB(db, bufhost, strlen(bufhost) + 1) == false)
{
res = false;
goto clean;
}
else
{
if (ip != NULL)
{
strlcpy(ip, host, ip_size);
}
DeleteDB(db, bufhost);
DeleteDB(db, bufkey);
res = true;
}
}
else
{
res = false;
goto clean;
}
strcpy(bufkey, "qi");
strlcat(bufkey, key, CF_BUFSIZE);
DeleteDB(db, bufkey);
strcpy(bufkey, "qo");
strlcat(bufkey, key, CF_BUFSIZE);
DeleteDB(db, bufkey);
clean:
CloseDB(db);
return res;
}
/*****************************************************************************/
bool ScanLastSeenQuality(LastSeenQualityCallback callback, void *ctx)
{
StringMap *lastseen_db = LoadDatabaseToStringMap(dbid_lastseen);
if (!lastseen_db)
{
return false;
}
MapIterator it = MapIteratorInit(lastseen_db->impl);
MapKeyValue *item;
Seq *hostkeys = SeqNew(100, free);
while ((item = MapIteratorNext(&it)) != NULL)
{
char *key = item->key;
/* Only look for "keyhost" entries */
if (key[0] != 'k')
{
continue;
}
SeqAppend(hostkeys, xstrdup(key + 1));
}
for (int i = 0; i < SeqLength(hostkeys); ++i)
{
const char *hostkey = SeqAt(hostkeys, i);
char keyhost_key[CF_BUFSIZE];
snprintf(keyhost_key, CF_BUFSIZE, "k%s", hostkey);
char *address = NULL;
address = (char*)StringMapGet(lastseen_db, keyhost_key);
if (!address)
{
Log(LOG_LEVEL_ERR, "Failed to read address for key '%s'.", hostkey);
continue;
}
char incoming_key[CF_BUFSIZE];
snprintf(incoming_key, CF_BUFSIZE, "qi%s", hostkey);
KeyHostSeen *incoming = NULL;
incoming = (KeyHostSeen*)StringMapGet(lastseen_db, incoming_key);
if (incoming)
{
if (!(*callback)(hostkey, address, true, incoming, ctx))
{
break;
}
}
char outgoing_key[CF_BUFSIZE];
snprintf(outgoing_key, CF_BUFSIZE, "qo%s", hostkey);
KeyHostSeen *outgoing = NULL;
outgoing = (KeyHostSeen*)StringMapGet(lastseen_db, outgoing_key);
if (outgoing)
{
if (!(*callback)(hostkey, address, false, outgoing, ctx))
{
break;
}
}
}
StringMapDestroy(lastseen_db);
SeqDestroy(hostkeys);
return true;
}
/*****************************************************************************/
int LastSeenHostKeyCount(void)
{
CF_DB *dbp;
CF_DBC *dbcp;
QPoint entry;
char *key;
void *value;
int ksize, vsize;
int count = 0;
if (OpenDB(&dbp, dbid_lastseen))
{
memset(&entry, 0, sizeof(entry));
if (NewDBCursor(dbp, &dbcp))
{
while (NextDB(dbcp, &key, &ksize, &value, &vsize))
{
/* Only look for valid "hostkey" entries */
if ((key[0] != 'k') || (value == NULL))
{
continue;
}
count++;
}
DeleteDBCursor(dbcp);
}
CloseDB(dbp);
}
return count;
}
/**
* @brief removes all traces of entry 'input' from lastseen DB
*
* @param[in] key digest (SHA/MD5 format) or free host name string
* @param[in] must_be_coherent. false : delete if lastseen is incoherent,
* true : don't if lastseen is incoherent
* @param[out] equivalent. If input is a host, return its corresponding
* digest. If input is a digest, return its
* corresponding host. CAN BE NULL! If equivalent
* is null, it stays as NULL
* @retval 0 if entry was deleted, <>0 otherwise
*/
int RemoveKeysFromLastSeen(const char *input, bool must_be_coherent,
char *equivalent, size_t equivalent_size)
{
bool is_coherent = false;
if (must_be_coherent == true)
{
is_coherent = IsLastSeenCoherent();
if (is_coherent == false)
{
Log(LOG_LEVEL_ERR, "Lastseen database is incoherent (there is not a 1-to-1 relationship between hosts and keys) and coherence check is enforced. Will not proceed to remove entries from it.");
return 254;
}
}
bool is_digest;
is_digest = IsDigestOrHost(input);
if (is_digest == true)
{
Log(LOG_LEVEL_VERBOSE, "Removing digest '%s' from lastseen database\n", input);
if (DeleteDigestFromLastSeen(input, equivalent, equivalent_size) == false)
{
Log(LOG_LEVEL_ERR, "Unable to remove digest from lastseen database.");
return 252;
}
}
else
{
Log(LOG_LEVEL_VERBOSE, "Removing host '%s' from lastseen database\n", input);
if (DeleteIpFromLastSeen(input, equivalent, equivalent_size) == false)
{
Log(LOG_LEVEL_ERR, "Unable to remove host from lastseen database.");
return 253;
}
}
Log(LOG_LEVEL_INFO, "Removed corresponding entries from lastseen database.");
return 0;
}
|