Files
cacti/cli/audit_database.php
T
TheWitnessandTheWitness 3ab4c69fb7 PHPStan Level 8 - Phase 1 Conversion (#8135)
* PHPStan Level 8 - Phase 1 Conversion

Reduces the real (no-baseline) PHPStan level 8 error count from ~2,724 to 425
and regenerates phpstan-baseline.neon accordingly (8,941 -> 1,921 lines).

Core type-safety changes
- lib/database.php: narrow db_fetch_row()/db_fetch_row_prepared() to return
  array (was bool|array) and db_fetch_assoc()/db_fetch_assoc_prepared() to
  return array (was mixed); the _prepared workers coerce the connection-failure
  path to an empty array so the declared type is honest. empty()/cacti_sizeof()
  checks are unaffected.
- lib/database.php: narrow $db_conn guards from is_object() to instanceof PDO in
  the transaction/quote/insert-id helpers so the PDO calls resolve.
- lib/mib_cache.php: the fluent MibCache methods mib()/object()/table()/row()
  now return self instead of a bare object so chained calls type-check.

Type-safety casts and guards (mechanical, behaviour-preserving)
- Cast string|false/|null builtin results feeding string parameters
  (json_decode, base64_encode, trim, htmle, preg_replace, explode, etc.).
- Cast int|string / int|false values feeding int id/pid parameters.
- Guard resource|false fopen()/popen() handles before fwrite/fclose/fread, and
  wrap foreach over array|false/list|false results with `?: []`.

Genuine bugs found and fixed
- cli/add_tree.php / lib/api_tree.php: site tree nodes could not be created via
  the CLI - the --site-id option was never parsed, $nodeTypes had no 'site'
  entry (passing null to api_tree_item_save(int $type) -> TypeError), and
  api_tree_item_save() never persisted site_id. All three fixed.
- tree.php: leaves_exist() is typed : int but returned db_fetch_assoc_prepared()
  (an array of rows) for a SELECT COUNT(*); now uses db_fetch_cell_prepared()
  and returns the scalar count.
- lib/mib_cache.php: $entries_per_object was a float (division / ceil) used as
  an array key (PHP 8.1 implicit float-to-int key deprecation); now cast to int.

Config
- .phpstan.neon: add notIdentical.alwaysTrue and booleanAnd.alwaysFalse to the
  existing always-true/false ignore family (same "PHPStan gets confused"
  dynamic-constant cause already covered by identical.alwaysTrue,
  notEqual.alwaysTrue, booleanAnd.leftAlwaysTrue, etc.).
- phpstan-baseline.neon: regenerated; the remaining 425 entries are the
  untouched long tail for Phase 2.

* Remove redundant @phpstan-ignore comments

Follow-up cleanup for the level 8 Phase 1 work. Removes 14 inline
@phpstan-ignore comments that no longer suppress any reported error:

- cli/repair_database.php: `@phpstan-ignore booleanAnd.alwaysFalse` is now
  covered by the global booleanAnd.alwaysFalse ignore added to .phpstan.neon.
- 13 others (host.php, include/global.php, include/session.php,
  lib/api_automation.php x2, lib/functions.php x2, lib/html_utility.php x2,
  lib/installer.php, lib/package.php, lib/reports.php, lib/rrd.php) no longer
  match any reported error after the type-safety fixes (e.g. the
  db_fetch_row_prepared() return-type narrowing removed the offset-assign
  errors the api_automation ignores were suppressing).

Verified with reportUnmatchedIgnoredErrors enabled: 0 unmatched ignores
remain and the full level 8 run still reports 0 errors.

* ci: satisfy validator ratchet and refresh security sink baselines

- lib/api_tree.php: give the new tree-item site_id a real numeric
  constraint ('^[-0-9]+$', matching html_reports.php) instead of an
  empty regex, so the validator constraint ratchet passes without
  bumping its baseline.
- tests/security/baselines: refresh the three sink/hotspot signatures
  whose code text changed from the (string) casts added in this branch
  (lib/poller.php setsid, cli/rrdresize.php json_encode, lib/import.php
  simplexml_load_string).

* ci(js): allow DOMPurify 3.4.16 and update sync_js pin test

The dependabot bump (#8134) moved package.json/package-lock.json to
DOMPurify 3.4.16 on develop but left tests/js/sync_js.test.mjs pinned to
3.4.15, so the PR merge commit fails the 'DOMPurify is pinned' contract.
Bring package.json/package-lock.json to 3.4.16 (matching develop) and
update the pin test to expect 3.4.16.

* test: update sql.php null-handling assertion for the (string) cast

This branch changed scripts/sql.php from trim(\ ?? '') to
trim((string) \) (the phpstan-safe form that coerces both null and
false from shell_exec()). Update SqlScriptsTest's null-return assertion
to match the new cast instead of the removed '?? ''.

* fix: address Copilot review - ownership IDs, fopen guards, mib_cache bound

- chown()/chgrp(): pass integer UID/GID from fileowner()/filegroup()
  (guarding the false lookup) instead of string-casting them, which made
  them resolve account/group names like '1001' and fail. Covers
  lib/api_device.php, lib/boost.php and poller_maintenance.php (x2).
- fopen() failure now short-circuits instead of reporting success:
  lib/package.php returns false, lib/clog_webapi.php and utilities.php
  render an error, and snmpagent_mibcachechild.php skips the cache write
  and sentinel unlink so a concurrent writer's lock is preserved.
- lib/mib_cache.php: keep the float quotient as the loop bound and cast
  only the array offset, so a non-even entries/columns split no longer
  truncates the loop and drops a cached row.

* revert: keep db_fetch_* returning false on error; baseline Level 8 instead

Per review, restoring the intentional fail-safe: db_fetch_row[_prepared]()
return bool|array and db_fetch_assoc[_prepared]() return mixed again, so a
genuine SQL error (not just a missing connection) is signalled as false and
the === false checks in cdef_is_in_use(), boost_process_* and aggregate.php
work as before. Dropped the is_array() ? : [] coercion.

Level 8 stays green by regenerating phpstan-baseline.neon to capture the
re-introduced bool|array/mixed call-site errors (the ~1,850 the narrowing
had eliminated). Tightening those callers to drop them from the baseline is
Phase 2 work.

---------

Co-authored-by: TheWitness <copilot@users.noreply.github.com>
2026-10-02 10:08:56 -04:00

1287 lines
38 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| 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; either version 2 |
| of the License, or (at your option) any later version. |
| |
| 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. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
require(__DIR__ . '/../include/cli_check.php');
chdir('..');
if (POLLER_ID > 1) {
print 'FATAL: This utility is designed for the main Data Collector only' . PHP_EOL;
exit(1);
}
// process calling arguments
$parms = $_SERVER['argv'];
array_shift($parms);
$upgrade = false;
$create = false;
$loadopt = false;
$report = false;
$repair = false;
$altersopt = false;
$debug = false;
if (cacti_sizeof($parms)) {
$shortopts = 'VvHhDd';
$longopts = [
'create',
'load',
'report',
'upgrade',
'repair',
'alters',
'version',
'debug',
'help'
];
$options = getopt($shortopts, $longopts);
foreach ($options as $arg => $value) {
switch($arg) {
case 'create':
$create = true;
break;
case 'load':
$loadopt = true;
break;
case 'report':
$report = true;
break;
case 'repair':
$repair = true;
break;
case 'alters':
$altersopt = true;
break;
case 'upgrade':
$upgrade = true;
break;
case 'debug':
case 'D':
case 'd':
$debug = true;
break;
case 'version':
case 'V':
case 'v':
display_version();
exit(0);
case 'help':
case 'H':
case 'h':
display_help();
exit(0);
default:
print "ERROR: Invalid Argument: ($arg)" . PHP_EOL . PHP_EOL;
display_help();
exit(1);
}
}
$db_version = substring_index(db_fetch_cell('SELECT cacti FROM version'), '.', 3);
if ($db_version != CACTI_VERSION && !isset($options['upgrade'])) {
$upgrade_required = true;
} else {
$upgrade_required = false;
}
if ($upgrade_required) {
print 'WARNING: Cacti must be upgraded first. Use the --upgrade option to perform that upgrade' . PHP_EOL;
exit(1);
}
if ($db_version != CACTI_VERSION && $upgrade) {
upgrade_database();
}
if ($repair) {
repair_database();
} elseif ($create) {
create_tables();
} elseif ($report) {
$alters = report_audit_results();
if (cacti_sizeof($alters)) {
exit(1);
}
} elseif ($altersopt) {
repair_database(false);
} elseif ($loadopt) {
load_audit_database();
} else {
display_help();
}
exit(0);
} else {
display_help();
exit(1);
}
/** Quote a value for a MySQL option file.
*
* The option file format is not byte transparent: an unquoted '#' starts a
* comment, a backslash escapes, trailing whitespace is stripped, and a newline
* would inject a further option. Values therefore have to be double quoted with
* the backslash and quote characters escaped.
*
* @param string $value Raw value.
*
* @return string Quoted value safe to write into the file.
*/
function audit_database_option_quote($value) {
return '"' . addcslashes((string) $value, '\\"') . '"';
}
/**
* audit_database_defaults_file - writes the database credentials to a private
* file for --defaults-file.
*
* The password is deliberately kept off the command line. Anything passed as
* an argument is readable from the process list by every local user for the
* lifetime of the command.
*
* @param string $username The database user.
* @param string $password The database password.
* @param string $hostname The database host.
* @param string $port The database port.
*
* @return string|false The path to the file, or false when it cannot be created.
*/
function audit_database_defaults_file($username, $password, $hostname, $port) {
$path = tempnam(sys_get_temp_dir(), 'cacti_audit_');
if ($path === false) {
return false;
}
// narrow the permissions before the secret is written
if (!chmod($path, 0600)) {
unlink($path);
return false;
}
// A newline cannot be represented in the option file format; refuse rather
// than silently authenticate with a mangled credential.
foreach ([$username, $password, $hostname] as $value) {
if (strpbrk((string) $value, "\r\n") !== false) {
unlink($path);
return false;
}
}
$contents = '[client]' . PHP_EOL;
$contents .= 'user=' . audit_database_option_quote($username) . PHP_EOL;
$contents .= 'password=' . audit_database_option_quote($password) . PHP_EOL;
$contents .= 'host=' . audit_database_option_quote($hostname) . PHP_EOL;
if ($hostname != 'localhost') {
$contents .= 'port=' . intval($port) . PHP_EOL;
}
if (file_put_contents($path, $contents) === false) {
unlink($path);
return false;
}
return $path;
}
function upgrade_database() : void {
$start = microtime(true);
cacti_log('NOTE: Upgrading Cacti, this will take a few minutes.', true, 'UPGRADE');
$return_var = 0;
$output = [];
exec('php ' . CACTI_PATH_CLI . '/upgrade_database.php --debug', $output, $return_var);
$end = microtime(true);
if ($return_var == 0) {
cacti_log(sprintf('NOTE: Cacti Upgrade succeeded in %.2f seconds', $end - $start), true, 'UPGRADE');
} else {
cacti_log('WARNING: Cacti Upgrade Encountered Errors. Messages below. Details are below, but also in Cacti upgrade log.', true, 'UPGRADE');
print '------------------------------------------------------------------------------------------------' . PHP_EOL;
print implode(PHP_EOL, $output) . PHP_EOL;
print '------------------------------------------------------------------------------------------------' . PHP_EOL;
}
$pistart = microtime(true);
// Upgrade plugins now
$plugins = glob(CACTI_PATH_PLUGINS . '/*', GLOB_ONLYDIR);
// Do syslog and thold first if found
$preorder[] = CACTI_PATH_PLUGINS . '/thold';
$preorder[] = CACTI_PATH_PLUGINS . '/syslog';
foreach (($plugins ?: []) as $p) {
if (str_contains($p, 'thold')) {
// Skip, upgrading this first
} elseif (str_contains($p, 'syslog')) {
// Skip, upgrading this second
} else {
$preorder[] = $p;
}
}
$plugins = $preorder;
if (cacti_sizeof($plugins)) {
if (!defined('IN_PLUGIN_INSTALL')) {
define('IN_PLUGIN_INSTALL', 1);
}
foreach ($plugins as $plugin) {
$parts = explode('/', $plugin);
$pname = end($parts);
$ufunc1 = 'plugin_' . $pname . '_upgrade';
$ufunc2 = $pname . '_upgrade_database';
$ufunc3 = $pname . '_setup_table_new';
if (!plugin_installed($pname)) {
cacti_log("NOTE: Plugin $pname is not installed, skipping.", true, 'UPGRADE');
continue;
}
if (file_exists($plugin . '/INFO')) {
// See if the plugin requires upgrading
$info = parse_ini_file($plugin . '/INFO', true);
$version = $info['info']['version'];
$old = db_fetch_cell_prepared('SELECT version
FROM plugin_config
WHERE directory = ?',
[$pname]);
if ($version != $old) {
if (file_exists($plugin . '/setup.php')) {
require_once($plugin . '/setup.php');
if (file_exists($plugin . '/includes/database.php')) {
require_once($plugin . '/includes/database.php');
}
// Always run the new function if it's there
// Some plugins don't upgrade in the proper way
if (function_exists($ufunc3)) {
cacti_log("NOTE: Running Plugin $pname install function due to some plugins not upgrading properly.", true, 'UPGRADE');
$ufunc3(true);
}
if (function_exists($ufunc2)) {
cacti_log("NOTE: Upgrading Plugin $pname from $old to $version using alternate upgrade path.", true, 'UPGRADE');
$ufunc2(true);
} elseif (function_exists($ufunc1)) {
cacti_log("NOTE: Upgrading Plugin $pname from $old to $version using standard upgrade path.", true, 'UPGRADE');
} else {
cacti_log("WARNING: Plugin $pname lacks an upgrade function.", true, 'UPGRADE');
}
if (file_exists($plugin . '/database_upgrade.php')) {
cacti_log("NOTE: Upgrading Plugin $pname from $old to $version using upgrade script.", true, 'UPGRADE');
$return_var = 0;
$output = [];
exec('php ' . CACTI_PATH_PLUGINS . '/' . $pname . '/database_upgrade.php --type=large --force-ver=' . $old, $output, $return_var);
if ($return_var == 0) {
print implode(PHP_EOL, $output) . PHP_EOL;
cacti_log("NOTE: Cacti Plugin $pname Upgrade Succeeded.", true, 'UPGRADE');
print '------------------------------------------------------------------------------------------------' . PHP_EOL;
print implode(PHP_EOL, $output) . PHP_EOL;
print '------------------------------------------------------------------------------------------------' . PHP_EOL;
} else {
cacti_log("WARNING: Cacti Plugin $pname Upgrade Encountered Errors.", true, 'UPGRADE');
print '------------------------------------------------------------------------------------------------' . PHP_EOL;
print implode(PHP_EOL, $output) . PHP_EOL;
print '------------------------------------------------------------------------------------------------' . PHP_EOL;
}
}
} else {
cacti_log("WARNING: Plugin $pname lacks a setup file.", true, 'UPGRADE');
}
} else {
cacti_log("NOTE: Plugin $pname Does not Require Upgrade", true, 'UPGRADE');
}
} else {
cacti_log("WARNING: Plugin $pname lacks an INFO file. Can not upgrade!", true, 'UPGRADE');
}
}
}
// Unregister plugins that no longer exist
// We keep legacy tables due to potential
// issues.
print '------------------------------------------------------------------------------------------------' . PHP_EOL;
cacti_log('NOTE: Pruning invalid and deprecated plugins while preserving tables', true, 'UPGRADE');
$plugins = db_fetch_assoc('SELECT directory FROM plugin_config');
if (cacti_sizeof($plugins)) {
foreach ($plugins as $p) {
$pname = $p['directory'];
if (!file_exists(CACTI_PATH_PLUGINS . '/' . $pname . '/INFO')) {
if (file_exists(CACTI_PATH_PLUGINS . '/' . $pname . '/setup.php')) {
cacti_log("NOTE: Uninstalling Plugin $pname which is not supported. Preserving tables.", true, 'UPGRADE');
api_plugin_uninstall($pname, false);
} else {
cacti_log("NOTE: Uninstalling Plugin $pname which is not supported and setup.php not found. Preserving tables.", true, 'UPGRADE');
db_execute_prepared('DELETE FROM plugin_config WHERE directory = ?', [$pname]);
db_execute_prepared('DELETE FROM plugin_db_changes WHERE plugin = ?', [$pname]);
db_execute_prepared('DELETE FROM plugin_hooks WHERE name = ?', [$pname]);
db_execute_prepared('DELETE FROM plugin_realms WHERE plugin = ?', [$pname]);
}
}
}
}
print '------------------------------------------------------------------------------------------------' . PHP_EOL;
$end = microtime(true);
cacti_log(sprintf('NOTE: Cacti Plugin Upgrades completed in %.2f seconds', $end - $pistart), true, 'UPGRADE');
cacti_log(sprintf('NOTE: Audit Upgrade completed in %.2f seconds.', $end - $start), true, 'UPGRADE');
}
function plugin_installed(string $plugin) : bool {
$installed = db_fetch_cell_prepared('SELECT COUNT(*)
FROM plugin_config
WHERE directory = ?
AND status = 1',
[$plugin]);
return $installed ? true : false;
}
function repair_database(bool $run = true) : void {
global $altersopt, $database_default;
$alters = report_audit_results(false);
if (!db_has_permissions(['ALTER', 'DROP', 'INSERT', 'LOCK TABLES'])) {
print 'ERROR: Required a required permission is missing for DB repair' . PHP_EOL;
exit(1);
}
$good = 0;
$bad = 0;
if (cacti_sizeof($alters)) {
foreach ($alters as $table => $changes) {
$tblinfo = db_fetch_row_prepared('SELECT ENGINE, SUBSTRING_INDEX(TABLE_COLLATION, "_", 1) AS COLLATION
FROM information_schema.tables
WHERE TABLE_SCHEMA = ?
AND TABLE_NAME = ?',
[$database_default, $table]);
if (isset($tblinfo['COLLATION'])) {
$collation = $tblinfo['COLLATION'];
} else {
$collation = 'utf8mb4';
}
if ($tblinfo['ENGINE'] == 'MyISAM') {
$suffix = ",\n ENGINE=InnoDB ROW_FORMAT=Dynamic CHARSET=" . $collation;
} else {
$suffix = ",\n ROW_FORMAT=Dynamic CHARSET=" . $collation;
}
$sql = 'ALTER TABLE `' . $table . "`\n " . implode(",\n ", $changes) . $suffix . ';';
if ($run) {
print '------------------------------------------------------------------------------------------------' . PHP_EOL;
print 'Executing Alter for Table : ' . $table;
$result = db_execute($sql);
if ($result) {
$good++;
print ' - Success' . PHP_EOL;
} else {
$bad++;
print ' - Failed' . PHP_EOL;
print $sql . PHP_EOL;
}
} else {
print '------------------------------------------------------------------------------------------------' . PHP_EOL;
print '-- Proposed Alter for Table : ' . $table . PHP_EOL . PHP_EOL;
print $sql . PHP_EOL . PHP_EOL;
}
}
}
print '------------------------------------------------------------------------------------------------' . PHP_EOL;
if ($bad == 0 && $good == 0) {
print ($altersopt ? '-- ' : '') . 'Repair Completed! No changes performed.' . PHP_EOL;
} elseif ($bad) {
print 'Repair Completed! ' . $good . ' Alters succeeded and ' . $bad . ' failed!' . PHP_EOL;
} else {
print 'Repair Completed! All ' . $good . ' Alters succeeded!' . PHP_EOL;
}
}
function report_audit_results(bool $output = true) : array {
global $database_default, $altersopt;
$db_name = 'Tables_in_' . $database_default;
create_tables();
$tables = db_fetch_assoc('SHOW TABLES');
$alters = [];
$cols = [
'table_type' => 'Type',
'table_null' => 'Null',
'table_key' => 'Key',
'table_default' => 'Default',
'table_extra' => 'Extra'
];
$idxs = [
'idx_non_unique' => 'Non_unique',
'idx_key_name' => 'Key_name',
'idx_seq_in_index' => 'Seq_in_index',
'idx_column_name' => 'Column_name',
'idx_packed' => 'Packed',
'idx_comment' => 'Comment'
];
if (cacti_sizeof($tables)) {
foreach ($tables as $table) {
$alter_cmds = [];
$table_name = $table[$db_name];
$status = db_fetch_row('SHOW TABLE STATUS LIKE "' . $table_name . '"');
if ($status['Collation'] == 'utf8mb4_unicode_ci' || $status['Collation'] == 'utf8_general_ci') {
$collation = 'utf8';
} else {
$collation = 'latin';
}
if ($output) {
print '------------------------------------------------------------------------------------------------' . PHP_EOL;
printf('Checking Table: %-45s', '\'' . $table_name . '\'');
} else {
printf(($altersopt ? '-- ' : '') . 'Scanning Table: %-45s', '\'' . $table_name . '\'');
}
$table_exists = db_fetch_cell_prepared('SELECT COUNT(*)
FROM table_columns
WHERE table_name = ?',
[$table_name]);
if (!$table_exists) {
$plugin_table = db_fetch_row_prepared('SELECT *
FROM plugin_db_changes
WHERE `table` = ?
AND method = ?',
[$table_name, 'create']);
if (!cacti_sizeof($plugin_table)) {
if ($output) {
print ' - Does not Exist. Possible Plugin' . PHP_EOL;
continue;
}
}
if ($output) {
print ' - Plugin Detected' . PHP_EOL;
continue;
} else {
print ' - Completed' . PHP_EOL;
}
} elseif (!$output) {
print ' - Completed' . PHP_EOL;
}
/* Column scanning comes in two parts. In the first part, we
* scan the columns in the current database to the saved schema
* In the second pass, we look for the columns from the saved
* schema to look for missing ones.
*/
$i = 1;
$errors = 0;
$warnings = 0;
$col_added = [];
$col_alter = [];
$columns = db_fetch_assoc('SHOW COLUMNS IN ' . $table_name);
$exists = db_fetch_cell_prepared('SELECT COUNT(*) FROM table_columns
WHERE table_name = ?',
[$table_name]);
if ($exists) {
if (cacti_sizeof($columns)) {
foreach ($columns as $c) {
$sequence_off = false;
$dbc = db_fetch_row_prepared('SELECT *
FROM table_columns
WHERE table_name = ?
AND table_field = ?',
[$table_name, $c['Field']]);
if (!cacti_sizeof($dbc)) {
$plugin_column = db_fetch_row_prepared('SELECT *
FROM plugin_db_changes
WHERE `table` = ?
AND `column` = ?
AND method = ?',
[$table_name, $c['Field'], 'addcolumn']);
if (!cacti_sizeof($plugin_column)) {
if ($output) {
print PHP_EOL . 'WARNING Col: \'' . $c['Field'] . '\', does not exist in default Cacti. Plugin possible';
}
$warnings++;
}
} else {
foreach ($cols as $dbcol => $col) {
if ($col == 'Type' && $dbc[$dbcol] == 'text') {
if ($collation == 'latin') {
$dbc[$dbcol] = 'mediumtext';
}
}
// work around MariaDB compatibility issue
$c[$col] = ! $c[$col] ?: str_replace('current_timestamp()', 'CURRENT_TIMESTAMP', $c[$col]);
$c[$col] = ! $c[$col] ?: str_replace('on update', 'ON UPDATE', $c[$col]);
$dbc[$dbcol] = ! $dbc[$dbcol] ?: str_replace('current_timestamp()', 'CURRENT_TIMESTAMP', $dbc[$dbcol]);
$dbc[$dbcol] = ! $dbc[$dbcol] ?: str_replace('on update', 'ON UPDATE', $dbc[$dbcol]);
// work around MySQL 8.x simplified int columns
if (str_contains($dbc[$dbcol], 'int(')) {
// Get the integer first
$parts = explode('(', $dbc[$dbcol]);
$adbccol = $parts[0];
// Get attributes next
$parts = explode(' ', $parts[1], 2);
if (isset($parts[1])) {
$adbccol .= ' ' . $parts[1];
}
$adbccol = trim($adbccol);
} else {
$adbccol = $dbc[$dbcol];
}
// Work Around for MySQL 8
$c[$col] = trim(str_replace('DEFAULT_GENERATED', '', $c[$col]));
if (($c[$col] != $dbc[$dbcol] && $c[$col] != $adbccol) && $c[$col] != 'mediumtext') {
if ($output) {
if ($col != 'Key') {
if ($col == 'Extra' && $dbc[$dbcol] == '1' && $c[$col] == '') {
// Handle Timestamp on DEFAULT/ON UPDATE irregularities
continue;
} else {
print PHP_EOL . 'ERROR Col: \'' . $c['Field'] . '\', Attribute \'' . $col . '\' invalid. Should be: \'' . $dbc[$dbcol] . '\', Is: \'' . $c[$col] . '\'';
}
}
}
if (array_search($dbc['table_field'], $col_alter, true) === false) {
$alter_cmds[] = make_column_alter($table_name, $dbc);
$col_alter[] = $dbc['table_field'];
$errors++;
}
}
}
}
$i++;
}
}
/* In this pass, we will gather the default schema and look for
* missing information.
*/
$db_columns = db_fetch_assoc_prepared('SELECT *
FROM table_columns
WHERE table_name = ?',
[$table_name]);
if (cacti_sizeof($db_columns)) {
foreach ($db_columns as $dbc) {
if (!db_column_exists($table_name, $dbc['table_field'])) {
if (array_search($dbc['table_field'], $col_added, true) === false) {
if ($output) {
print PHP_EOL . 'WARNING Col: \'' . $dbc['table_field'] . '\' is missing from \'' . $table_name . '\'';
}
$alter_cmds[] = make_column_add($table_name, $dbc);
$col_added[] = $dbc['table_field'];
$errors++;
}
}
}
}
/* Index scanning comes in two parts. In the first part, we
* scan the indexes in the current database to the saved schema
* In the second pass, we look for the indexes from the saved
* schema to look for missing ones.
*/
$indexes = db_fetch_assoc('SHOW INDEXES IN ' . $table_name);
$idx_added = [];
$idx_dropped = [];
if (cacti_sizeof($indexes)) {
foreach ($indexes as $i) {
$key_exists = db_fetch_cell_prepared('SELECT COUNT(*)
FROM table_indexes
WHERE idx_table_name = ?
AND idx_key_name = ?',
[$i['Table'], $i['Key_name']]);
$dbc = db_fetch_row_prepared('SELECT *
FROM table_indexes
WHERE idx_table_name = ?
AND idx_key_name = ?
AND idx_seq_in_index = ?
AND idx_column_name = ?
ORDER BY idx_seq_in_index',
[$i['Table'], $i['Key_name'], $i['Seq_in_index'], $i['Column_name']]);
if (!cacti_sizeof($dbc)) {
if ($key_exists) {
// Ignore till Phase II
} elseif (array_search($i['Key_name'], $idx_dropped, true) === false) {
// Primary keys come in Phase II
if ($i['Key_name'] != 'PRIMARY') {
if ($output) {
print PHP_EOL . 'WARNING Index: \'' . $i['Key_name'] . '\', does not exist in default Cacti. Dropping.';
}
$alter_cmds[] = 'DROP INDEX ' . $i['Key_name'];
$idx_dropped[] = $i['Key_name'];
$errors++;
}
}
} else {
foreach ($idxs as $dbidx => $idx) {
if ($i[$idx] != $dbc[$dbidx]) {
// Primary keys come in Phase II
if ($i['Key_name'] != 'PRIMARY') {
if (array_search($i['Key_name'], $idx_added, true) === false) {
if ($output) {
print PHP_EOL . 'ERROR Index: \'' . $i['Key_name'] . '\', Attribute \'' . $idx . '\' invalid. Should be: \'' . $dbc[$dbidx] . '\', Is: \'' . $i[$idx] . '\'';
}
$alter_cmds = array_merge($alter_cmds, make_index_alter($table_name, $i['Key_name']));
$idx_added[] = $i['Key_name'];
$errors++;
}
}
}
}
}
}
}
// check for missing indexes and add them
$db_indexes = db_fetch_assoc_prepared('SELECT *
FROM table_indexes
WHERE idx_table_name = ?',
[$table_name]);
if (cacti_sizeof($db_indexes)) {
foreach ($db_indexes as $i) {
if (!db_index_exists($table_name, $i['idx_key_name'])) {
if (array_search($i['idx_key_name'], $idx_added, true) === false) {
if ($output) {
print PHP_EOL . 'ERROR Index: \'' . $i['idx_key_name'] . '\', is missing from \'' . $table_name . '\'';
}
$alter_cmds = array_merge($alter_cmds, make_index_alter($table_name, $i['idx_key_name']));
$idx_added[] = $i['idx_key_name'];
$errors++;
}
} else {
$prop_seq = db_fetch_cell_prepared('SELECT COUNT(*)
FROM table_indexes
WHERE idx_table_name = ?
AND idx_key_name = ?',
[$table_name, $i['idx_key_name']]);
$curr_seq = get_sequence_count($table_name, $i['idx_key_name']);
$curr_column_seq = get_column_sequence_number($table_name, $i['idx_key_name'], $i['idx_column_name']);
// print PHP_EOL . "Prop Seq:" . $prop_seq . ", Curr Seq:" . $curr_seq . PHP_EOL;
if ($curr_seq != $prop_seq || $curr_column_seq != $i['idx_seq_in_index']) {
if (array_search($i['idx_key_name'], $idx_dropped, true) === false) {
if ($output) {
if ($curr_seq != $prop_seq) {
print PHP_EOL . 'WARNING Index: \'' . $i['idx_key_name'] . '\', has differing number of columns. Dropping.';
}
if ($curr_column_seq != $i['idx_seq_in_index']) {
print PHP_EOL . 'WARNING Index: \'' . $i['idx_key_name'] . '\', has resequenced columns. Dropping.';
}
}
$alter_cmds = array_merge($alter_cmds, make_index_alter($table_name, $i['idx_key_name']));
$idx_added[] = $i['idx_key_name'];
$idx_dropped[] = $i['idx_key_name'];
$errors++;
}
}
}
}
}
if ($output) {
if ($errors || $warnings) {
print PHP_EOL . PHP_EOL . 'ERRORS: ' . $errors . ', WARNINGS: ' . $warnings . PHP_EOL;
} else {
print ' - Clean' . PHP_EOL;
}
}
if (cacti_sizeof($alter_cmds)) {
$alters[$table_name] = $alter_cmds;
}
}
}
}
if ($output) {
print '------------------------------------------------------------------------------------------------' . PHP_EOL;
if (cacti_sizeof($alters)) {
print 'ERRORS are fixable using the --repair option. WARNINGS will not be repaired' . PHP_EOL;
print 'due to ambiguous use of the column.' . PHP_EOL;
} else {
print 'Audit was clean, no errors or warnings' . PHP_EOL;
}
print '------------------------------------------------------------------------------------------------' . PHP_EOL;
}
return $alters;
}
function make_column_props(array &$dbc) : string {
$alter_cmd = '';
if (isset($dbc['table_default'])) {
$dbc['table_default'] = str_replace('current_timestamp()', 'CURRENT_TIMESTAMP', $dbc['table_default']);
}
if (isset($dbc['table_extra'])) {
$dbc['table_extra'] = str_replace('current_timestamp()', 'CURRENT_TIMESTAMP', $dbc['table_extra']);
$dbc['table_extra'] = str_replace('on update', 'ON UPDATE', $dbc['table_extra']);
$dbc['table_extra'] = trim(str_replace('DEFAULT_GENERATED', '', $dbc['table_extra']));
}
if ($dbc['table_null'] == 'YES') {
if ($dbc['table_default'] == 'NULL') {
// Ignore
} elseif ($dbc['table_default'] === null) {
// Ignore
} elseif ($dbc['table_default'] === '') {
// Ignore
} elseif ($dbc['table_default'] != 'CURRENT_TIMESTAMP') {
$alter_cmd .= ' DEFAULT "' . $dbc['table_default'] . '"';
} else {
$alter_cmd .= ' DEFAULT CURRENT_TIMESTAMP';
}
} elseif ($dbc['table_default'] !== 'NULL' && $dbc['table_default'] !== null) {
if ($dbc['table_default'] == 'CURRENT_TIMESTAMP') {
$alter_cmd .= ' DEFAULT CURRENT_TIMESTAMP';
} elseif ($dbc['table_extra'] != 'auto_increment') {
if (str_contains($dbc['table_type'], 'int(') && $dbc['table_default'] == '') {
$alter_cmd .= ' DEFAULT "0"';
} else {
$alter_cmd .= " DEFAULT '" . $dbc['table_default'] . "'";
}
}
}
if ($dbc['table_extra'] != '') {
$alter_cmd .= ' ' . $dbc['table_extra'];
}
return $alter_cmd;
}
function make_column_alter(string $table, array $dbc) : string {
$alter_cmd = 'MODIFY COLUMN `' . $dbc['table_field'] . '` ' .
$dbc['table_type'] . ($dbc['table_null'] == 'NO' ? ' NOT NULL' : '');
$alter_cmd .= make_column_props($dbc);
return $alter_cmd;
}
function make_column_add(string $table, array $dbc) : string {
$after = get_previous_column($table, $dbc['table_field']);
if ($after != 'first') {
$after = 'AFTER `' . $after . '`';
}
$alter_cmd = 'ADD COLUMN `' . $dbc['table_field'] . '` ' .
$dbc['table_type'] . ($dbc['table_null'] == 'NO' ? ' NOT NULL' : '');
$alter_cmd .= make_column_props($dbc);
$alter_cmd .= ' ' . $after;
return $alter_cmd;
}
function get_previous_column(string $table, string $column) : string {
$sequence = db_fetch_cell_prepared('SELECT table_sequence
FROM table_columns
WHERE table_name = ?
AND table_field = ?',
[$table, $column]);
if (!empty($sequence)) {
if ($sequence == 1) {
return 'first';
} else {
$previous = db_fetch_cell_prepared('SELECT table_field
FROM table_columns
WHERE table_name = ?
AND table_sequence = ?',
[$table, $sequence - 1]);
return $previous;
}
} else {
return 'unknown';
}
}
function make_index_alter(string $table, string $key) : array {
$alter_cmds = [];
$alter_cmd = '';
$primary_dropped = false;
$parts = db_fetch_assoc_prepared('SELECT *
FROM table_indexes
WHERE idx_table_name = ?
AND idx_key_name = ?
ORDER BY idx_seq_in_index',
[$table, $key]);
$sequence_cnt = get_sequence_count($table, $key);
// print PHP_EOL . "NOTE: INDEX KEY is $key, Baseline Sequence: " . cacti_sizeof($parts) . ", Actual Sequence: $sequence_cnt" . PHP_EOL;
if ($sequence_cnt != cacti_sizeof($parts) && $sequence_cnt > 0) {
if ($key == 'PRIMARY') {
$primary_dropped = true;
$alter_cmd .= "DROP PRIMARY KEY,\n ";
} else {
$alter_cmd .= 'DROP INDEX `' . $key . "`,\n ";
}
} elseif (db_index_exists($table, $key)) {
if ($key == 'PRIMARY') {
$primary_dropped = true;
$alter_cmd .= "DROP PRIMARY KEY,\n ";
} else {
$alter_cmd .= 'DROP INDEX `' . $key . "`,\n ";
}
}
$using = '';
if (cacti_sizeof($parts)) {
$i = 0;
$index_type = '';
foreach ($parts as $p) {
if ($i == 0 && $p['idx_key_name'] == 'PRIMARY') {
if ($primary_dropped == false) {
$alter_cmd .= "DROP PRIMARY KEY,\n ";
}
$alter_cmd .= 'ADD PRIMARY KEY (';
} elseif ($i == 0) {
if ($p['idx_non_unique'] == 1) {
$alter_cmd .= 'ADD INDEX `' . $key . '` (';
} else {
$alter_cmd .= 'ADD UNIQUE INDEX `' . $key . '` (';
}
}
if ($using == '' && isset($p['idx_index_type']) && $p['idx_index_type'] != '') {
$using = $p['idx_index_type'];
}
$alter_cmd .= ($i > 0 ? ',' : '') . '`' . $p['idx_column_name'] . '`';
$i++;
}
if ($using != '') {
$alter_cmd .= ') USING ' . $using;
}
$alter_cmds[] = $alter_cmd;
}
return $alter_cmds;
}
function get_sequence_count(string $table, string $index) : int {
$indexes = db_fetch_assoc("SHOW INDEXES IN $table");
$sequence_cnt = 0;
if (cacti_sizeof($indexes)) {
foreach ($indexes as $i) {
if ($index == $i['Key_name']) {
$sequence_cnt++;
}
}
}
return $sequence_cnt;
}
function get_column_sequence_number(string $table, string $index, string $column) : int {
$indexes = db_fetch_assoc("SHOW INDEXES IN $table");
if (cacti_sizeof($indexes)) {
foreach ($indexes as $i) {
$sequence = $i['Seq_in_index'];
if ($i['Key_name'] == $index) {
if ($i['Column_name'] == $column) {
return $sequence;
}
}
}
}
return -1;
}
function create_tables(bool $load = true) : void {
global $database_default, $database_username, $database_password, $database_port, $database_hostname;
global $altersopt, $debug;
if (!db_has_permissions('CREATE')) {
print 'ERROR: Unable to create audit tables, permission required' . PHP_EOL;
exit(1);
}
db_execute("CREATE TABLE IF NOT EXISTS table_columns (
table_name varchar(50) NOT NULL,
table_sequence int(10) unsigned NOT NULL,
table_field varchar(50) NOT NULL,
table_type varchar(50) default NULL,
table_null varchar(10) default NULL,
table_key varchar(4) default NULL,
table_default varchar(50) default NULL,
table_extra varchar(128) default NULL,
PRIMARY KEY (table_name, table_sequence, table_field))
ENGINE=InnoDB
COMMENT='Holds Default Cacti Table Definitions'");
$exists_columns = db_table_exists('table_columns');
if (!$exists_columns) {
print "Failed to create 'table_columns'";
exit;
}
db_execute("CREATE TABLE IF NOT EXISTS table_indexes (
idx_table_name varchar(50) NOT NULL,
idx_non_unique int(10) unsigned default NULL,
idx_key_name varchar(128) NOT NULL,
idx_seq_in_index int(10) unsigned NOT NULL,
idx_column_name varchar(50) NOT NULL,
idx_collation varchar(10) default NULL,
idx_cardinality int(10) unsigned default NULL,
idx_sub_part varchar(50) default NULL,
idx_packed varchar(128) default NULL,
idx_null varchar(10) default NULL,
idx_index_type varchar(20) default NULL,
idx_comment varchar(128) default NULL,
PRIMARY KEY (idx_table_name, idx_key_name, idx_seq_in_index, idx_column_name))
ENGINE=InnoDB
COMMENT='Holds Default Cacti Index Definitions'");
$exists_indexes = db_table_exists('table_indexes');
if (!$exists_indexes) {
print "ERROR: Failed to create 'table_indexes'" . PHP_EOL;
exit;
}
if ($load) {
if (!db_has_permissions(['DROP', 'INSERT', 'LOCK TABLES'])) {
print 'ERROR: Required a required permission is missing for DB load' . PHP_EOL;
exit(1);
}
// Handle case to address Mariadb dropping the mysql command
if (file_exists('/usr/bin/mariadb')) {
$db_shell = '/usr/bin/mariadb';
} elseif (file_exists('/usr/bin/mysql')) {
$db_shell = '/usr/bin/mysql';
} elseif (file_exists('/usr/local/bin/mariadb')) {
$db_shell = '/usr/local/bin/mariadb';
} elseif (file_exists('/usr/local/bin/mysql')) {
$db_shell = '/usr/local/bin/mysql';
} else {
// which appends a newline, which would split the command in two and
// strand the credential argument on the second line.
$db_shell = trim((string) shell_exec('which mysql'));
if ($db_shell == '') {
print 'FATAL: mysql or mariadb command not found!' . PHP_EOL;
exit(1);
}
}
if ($database_hostname != 'localhost') {
$port = ' --port=' . cacti_escapeshellarg($database_port);
} else {
$port = '';
}
db_execute('TRUNCATE table_columns');
db_execute('TRUNCATE table_indexes');
$output = [];
$error = 0;
if (file_exists(CACTI_PATH_DOCS . '/audit_schema.sql')) {
/* the credentials go in a private defaults file rather than on the
* command line, where any local user could read them out of the
* process list for as long as the import runs */
$defaults_file = audit_database_defaults_file($database_username, $database_password, $database_hostname, $database_port);
if ($defaults_file === false) {
print 'FATAL: Unable to create a private credentials file' . PHP_EOL;
exit(1);
}
$cmd = $db_shell . ' --defaults-file=' . cacti_escapeshellarg($defaults_file) .
' ' . cacti_escapeshellarg($database_default) .
' < ' . CACTI_PATH_DOCS . '/audit_schema.sql';
exec($cmd, $output, $error);
unlink($defaults_file);
if ($debug) {
print 'Called: ' . $cmd . PHP_EOL;
print 'Result: ' . $error . PHP_EOL;
print 'Output: ' . PHP_EOL . print_r($output, true) . PHP_EOL;
}
if ($error == 0) {
print ($altersopt ? '-- ' : '') . 'SUCCESS: Loaded the Audit Schema' . PHP_EOL;
} else {
print '@ ' . CACTI_PATH_DOCS . '/audit_schema.sql' . PHP_EOL;
print 'FATAL: Failed Load the Audit Schema' . PHP_EOL;
print 'ERROR: ' . implode(",\n ", $output) . PHP_EOL;
exit(1);
}
} else {
print 'FATAL: Failed to find Audit Schema' . PHP_EOL;
}
}
}
function load_audit_database() : void {
global $database_default, $database_username, $database_password;
$db_name = 'Tables_in_' . $database_default;
if (!db_has_permissions(['DROP', 'INSERT', 'LOCK TABLES'])) {
print 'ERROR: Required a required permission is missing for DB load' . PHP_EOL;
exit(1);
}
create_tables(false);
db_execute('TRUNCATE table_columns');
db_execute('TRUNCATE table_indexes');
$tables = db_fetch_assoc('SHOW TABLES');
if (cacti_sizeof($tables)) {
foreach ($tables as $table) {
$table_name = $table[$db_name];
$columns = db_fetch_assoc('SHOW COLUMNS IN ' . $table_name);
$indexes = db_fetch_assoc('SHOW INDEXES IN ' . $table_name);
print 'Importing Table: ' . $table_name;
$i = 1;
if (cacti_sizeof($columns)) {
foreach ($columns as $c) {
db_execute_prepared('INSERT INTO table_columns
(table_name, table_sequence, table_field, table_type, table_null, table_key, table_default, table_extra)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
[
$table_name,
$i,
$c['Field'],
$c['Type'],
$c['Null'],
$c['Key'],
$c['Default'],
$c['Extra']
]
);
$i++;
}
}
print ' - Done' . PHP_EOL;
if (cacti_sizeof($indexes)) {
foreach ($indexes as $i) {
db_execute_prepared('INSERT INTO table_indexes
(idx_table_name, idx_non_unique, idx_key_name, idx_seq_in_index, idx_column_name,
idx_collation, idx_cardinality, idx_sub_part, idx_packed, idx_null, idx_index_type, idx_comment)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
[
$i['Table'],
$i['Non_unique'],
$i['Key_name'],
$i['Seq_in_index'],
$i['Column_name'],
$i['Collation'],
$i['Cardinality'],
$i['Sub_part'],
$i['Packed'],
$i['Null'],
$i['Index_type'],
$i['Comment']
]
);
}
}
}
}
if (is_dir(CACTI_PATH_DOCS . '')) {
print PHP_EOL . 'Exporting Table Audit Table Creation Logic to ' . CACTI_PATH_DOCS . '/audit_schema.sql' . PHP_EOL;
$retval = db_dump_data($database_default, 'table_columns table_indexes', [], CACTI_PATH_DOCS . '/audit_schema.sql');
if ($retval) {
print 'Finished Creating Audit Schema with ERROR' . PHP_EOL . PHP_EOL;
} else {
print 'Finished Creating Audit Schema' . PHP_EOL . PHP_EOL;
}
} else {
print PHP_EOL . 'FATAL: Docs directory does not exist!' . PHP_EOL . PHP_EOL;
}
}
/**
* display_version - displays version information
*
* @return void
*/
function display_version() : void {
$version = get_cacti_cli_version();
print "Cacti Database Audit Utility, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
}
/**
* display_help - displays help information
*
* @return void
*/
function display_help() : void {
display_version();
print PHP_EOL . 'usage: audit_database.php --report | --repair [ --upgrade ]' . PHP_EOL . PHP_EOL;
print 'Cacti utility for auditing and correcting your Cacti database. This utility can' . PHP_EOL;
print 'will scan your Cacti database and report any problems in the schema that it finds.' . PHP_EOL . PHP_EOL;
print 'Options:' . PHP_EOL;
print ' --report - Report on any issues found in the audit of the database' . PHP_EOL;
print ' --repair - Repair any issues found during the audit of the database' . PHP_EOL;
print ' --upgrade - Upgrade the Cacti database before running' . PHP_EOL . PHP_EOL;
print 'Developer Options:' . PHP_EOL;
print ' --create - Initialize or Re-initialize the Audit Schema tables.' . PHP_EOL;
print ' --load - Take a pristine Cacti install and create Audit Schema and file.' . PHP_EOL;
print ' --alters - Print out all the alter commands vs. executing for debugging.' . PHP_EOL . PHP_EOL;
}