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
|
/*
Copyright 2024 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 <dbm_migration.h>
#include <lastseen.h>
#include <logging.h>
#include <string_lib.h>
typedef struct
{
double q;
double expect;
double var;
} QPoint0;
typedef struct
{
time_t lastseen;
QPoint Q; // Average time between connections (rolling weighted average)
} KeyHostSeen1;
#define QPOINT0_OFFSET 128
/*
* Structure of version 0 lastseen entry:
*
* flag | hostkey -> address | QPoint
* | | | \- 3*double
* | | \- 128 chars
* | \- N*chars
* \- 1 byte, '+' or '-'
*/
static bool LastseenMigrationVersion0(DBHandle *db)
{
/* For some reason DB migration for LMDB was disabled in 2014 (in commit
* 8611970bfa33be7b3cf0724eb684833e08582850). Unfortunately there is no
* mention as to why this was done. Maybe it was not working?
*
* However, we're re-enabling it now (10 years later). Since this
* migration function has not been active for the last 10 years, the
* safest thing is to remove the migration logic, and only update the
* version number.
*
* If you have not upgraded CFEngine in the last 10 years, this will be
* the least of your problems.
*/
return WriteDB(db, "version", "1", sizeof("1"));
}
static bool LastseenMigrationVersion1(DBHandle *db)
{
DBCursor *cursor;
if (!NewDBCursor(db, &cursor))
{
Log(LOG_LEVEL_ERR,
"Unable to create database cursor during lastseen migration");
return false;
}
char *key;
void *value;
int key_size, value_size;
// Iterate through all key-value pairs
while (NextDB(cursor, &key, &key_size, &value, &value_size))
{
if (key_size == 0)
{
Log(LOG_LEVEL_WARNING,
"Found zero-length key during lastseen migration");
continue;
}
// Only look for old KeyHostSeen entries
if (key[0] != 'q')
{
// Warn about completely unexpected keys
if ((key[0] != 'k') && (key[0] != 'a') && !StringEqualN(key, "version", key_size))
{
Log(LOG_LEVEL_WARNING,
"Found unexpected key '%s' during lastseen migration. "
"Only expecting 'version' or 'k', 'a' and 'q[i|o]' prefixed keys.",
key);
}
continue;
}
KeyHostSeen1 *old_value = value;
KeyHostSeen new_value = {
.acknowledged = true, // We don't know, assume yes
.lastseen = old_value->lastseen,
.Q = old_value->Q,
};
// This will overwrite the entry
if (!DBCursorWriteEntry(cursor, &new_value, sizeof(new_value)))
{
Log(LOG_LEVEL_ERR,
"Unable to write version 2 of entry for key '%s' during lastseen migration.",
key);
return false;
}
}
if (!DeleteDBCursor(cursor))
{
Log(LOG_LEVEL_ERR, "Unable to close cursor during lastseen migration");
return false;
}
if (!WriteDB(db, "version", "2", sizeof("2")))
{
Log(LOG_LEVEL_ERR, "Failed to update version number during lastseen migration");
return false;
}
Log(LOG_LEVEL_INFO, "Migrated lastseen database from version 1 to 2");
return true;
}
const DBMigrationFunction dbm_migration_plan_lastseen[] =
{
LastseenMigrationVersion0,
LastseenMigrationVersion1,
NULL
};
|