Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Cacti wmi Plugin AI Instructions

## Project Overview
This is a Cacti plugin. It integrates with the Cacti monitoring platform via the plugin hook architecture.

## Technology Stack
- PHP 7.4+ (targeting Cacti 1.2.x compatibility)
- MySQL/MariaDB via Cacti's DB abstraction layer
- PSR-12 coding standards

## Key Rules
- Use prepared statements (db_execute_prepared, db_fetch_row_prepared, etc.) for ALL queries with variables
- Use get_request_var() / get_filter_request_var() for ALL user input, never raw $_REQUEST/$_GET/$_POST
- Use html_escape() / htmlspecialchars() for ALL output of DB/user values in HTML context
- Use cacti_escapeshellarg() for ALL shell command arguments
- No PHP 8.0+ features (str_contains, match, union types, named args) - target PHP 7.4
- Use ?? and ??= operators (PHP 7.4) instead of isset() ternary patterns
- All unserialize() calls must use allowed_classes => false

## Testing
- Tests in tests/ directory
- Use Pest PHP or PHPUnit
- php -l lint check required before commit
225 changes: 225 additions & 0 deletions .github/workflows/plugin-ci-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
# +-------------------------------------------------------------------------+
# | 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/ |
# +-------------------------------------------------------------------------+

name: Plugin Integration Tests

on:
push:
branches:
- main
- develop
pull_request:
branches:
- main
- develop

jobs:
integration-test:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
php: ['8.1', '8.2', '8.3', '8.4']
os: [ubuntu-latest]

services:
mariadb:
image: mariadb:10.6
env:
MYSQL_ROOT_PASSWORD: cactiroot
MYSQL_DATABASE: cacti
MYSQL_USER: cactiuser
MYSQL_PASSWORD: cactiuser
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3

name: PHP ${{ matrix.php }} Integration Test on ${{ matrix.os }}

steps:
- name: Checkout Cacti
uses: actions/checkout@v4
with:
repository: Cacti/cacti
path: cacti

- name: Checkout wmi Plugin
uses: actions/checkout@v4
with:
path: cacti/plugins/wmi

- name: Install PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: intl, mysql, gd, ldap, gmp, xml, curl, json, mbstring
ini-values: "post_max_size=256M, max_execution_time=60, date.timezone=America/New_York"

- name: Check PHP version
run: php -v

- name: Run apt-get update
run: sudo apt-get update

- name: Install System Dependencies
run: sudo apt-get install -y apache2 snmp snmpd rrdtool fping libapache2-mod-php${{ matrix.php }}

- name: Start SNMPD Agent and Test
run: |
sudo systemctl start snmpd
sudo snmpwalk -c public -v2c -On localhost .1.3.6.1.2.1.1

- name: Setup Permissions
run: |
sudo chown -R www-data:runner ${{ github.workspace }}/cacti
sudo find ${{ github.workspace }}/cacti -type d -exec chmod 775 {} \;
sudo find ${{ github.workspace }}/cacti -type f -exec chmod 664 {} \;
sudo chmod +x ${{ github.workspace }}/cacti/cmd.php
sudo chmod +x ${{ github.workspace }}/cacti/poller.php

- name: Create MySQL Config
run: |
echo -e "[client]\nuser = root\npassword = cactiroot\nhost = 127.0.0.1\n" > ~/.my.cnf
cat ~/.my.cnf

- name: Initialize Cacti Database
env:
MYSQL_AUTH_USR: '--defaults-file=~/.my.cnf'
run: |
mysql $MYSQL_AUTH_USR -e 'CREATE DATABASE IF NOT EXISTS cacti;'
mysql $MYSQL_AUTH_USR -e "CREATE USER IF NOT EXISTS 'cactiuser'@'localhost' IDENTIFIED BY 'cactiuser';"
mysql $MYSQL_AUTH_USR -e "GRANT ALL PRIVILEGES ON cacti.* TO 'cactiuser'@'localhost';"
mysql $MYSQL_AUTH_USR -e "GRANT SELECT ON mysql.time_zone_name TO 'cactiuser'@'localhost';"
mysql $MYSQL_AUTH_USR -e "FLUSH PRIVILEGES;"
mysql $MYSQL_AUTH_USR cacti < ${{ github.workspace }}/cacti/cacti.sql
mysql $MYSQL_AUTH_USR -e "INSERT INTO settings (name, value) VALUES ('path_php_binary', '/usr/bin/php')" cacti

- name: Validate composer files
run: |
cd ${{ github.workspace }}/cacti
if [ -f composer.json ]; then
composer validate --strict || true
fi

- name: Install Composer Dependencies
run: |
cd ${{ github.workspace }}/cacti
if [ -f composer.json ]; then
sudo composer install --prefer-dist --no-progress
fi

- name: Create Cacti config.php
run: |
cat ${{ github.workspace }}/cacti/include/config.php.dist | \
sed -r "s/localhost/127.0.0.1/g" | \
sed -r "s/'cacti'/'cacti'/g" | \
sed -r "s/'cactiuser'/'cactiuser'/g" | \
sed -r "s/'cactiuser'/'cactiuser'/g" > ${{ github.workspace }}/cacti/include/config.php
sudo chmod 664 ${{ github.workspace }}/cacti/include/config.php

- name: Configure Apache
run: |
cat << 'EOF' | sed 's#GITHUB_WORKSPACE#${{ github.workspace }}#g' > /tmp/cacti.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot GITHUB_WORKSPACE/cacti

<Directory GITHUB_WORKSPACE/cacti>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EOF
sudo cp /tmp/cacti.conf /etc/apache2/sites-available/000-default.conf
sudo systemctl restart apache2

- name: Install Cacti via CLI
run: |
cd ${{ github.workspace }}/cacti
sudo php cli/install_cacti.php --accept-eula --install --force

- name: Install wmi Plugin
run: |
cd ${{ github.workspace }}/cacti
sudo php cli/plugin_manage.php --plugin=wmi --install --enable

# - name: import wmi Plugin Sample Data
# run: |
# cd ${{ github.workspace }}/cacti/plugins/wmi
# sudo php cli_import.php --filename=.github/workflows/wmi_sample_data.xml
# if [ $? -ne 0 ]; then
# echo "Failed to import Thold sample data"
# exit 1
# fi

- name: Check PHP Syntax for Plugin
run: |
cd ${{ github.workspace }}/cacti/plugins/wmi
if find . -name '*.php' -exec php -l {} 2>&1 \; | grep -iv 'no syntax errors detected'; then
echo "Syntax errors found!"
exit 1
fi

- name: Remove the plugins directory exclusion from the .phpstan.neon
run: sed '/plugins/d' -i .phpstan.neon
working-directory: ${{ github.workspace }}/cacti

- name: Mark composer scripts executable
run: sudo chmod +x ${{ github.workspace }}/cacti/include/vendor/bin/*

- name: Run Linter on base code
run: composer run-script lint ${{ github.workspace }}/cacti/plugins/wmi
working-directory: ${{ github.workspace }}/cacti

- name: Checking coding standards on base code
run: composer run-script phpcsfixer ${{ github.workspace }}/cacti/plugins/wmi
working-directory: ${{ github.workspace }}/cacti

# - name: Run PHPStan at Level 6 on base code outside of Composer due to technical issues
# run: ./include/vendor/bin/phpstan analyze --level 6 ${{ github.workspace }}/cacti/plugins/wmi
# working-directory: ${{ github.workspace }}/cacti

- name: Run Cacti Poller
run: |
cd ${{ github.workspace }}/cacti
sudo php poller.php --poller=1 --force --debug
if ! grep -q "SYSTEM STATS" log/cacti.log; then
echo "Cacti poller did not finish successfully"
cat log/cacti.log
exit 1
fi

- name: View Cacti Logs
if: always()
run: |
if [ -f ${{ github.workspace }}/cacti/log/cacti.log ]; then
echo "=== Cacti Log ==="
sudo cat ${{ github.workspace }}/cacti/log/cacti.log
fi
2 changes: 1 addition & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function plugin_wmi_create_dataquery_xml($id) {
}
$data .= "\t\t</items>\n";

$data_input_data = db_fetch_assoc("SELECT * FROM data_input_fields WHERE data_input_fields.data_input_id=$input AND input_output = 'in' ORDER BY id DESC");
$data_input_data = db_fetch_assoc_prepared("SELECT * FROM data_input_fields WHERE data_input_fields.data_input_id=? AND input_output = 'in' ORDER BY id DESC", array($input));
$data .= "\t\t<data>\n";
$i = 0;
if (cacti_sizeof($data_input_data) > 0) {
Expand Down
26 changes: 16 additions & 10 deletions linux_wmi.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ function exec() {
function clean() {
$this->username = cacti_escapeshellarg($this->username);
$this->password = cacti_escapeshellarg($this->password);
$this->hostname = trim($this->hostname);
/* hostname must be quoted: trim() alone does not neutralise shell metacharacters */
$this->hostname = cacti_escapeshellarg(trim($this->hostname));
$this->binary = cacti_escapeshellarg($this->binary);
$this->command = cacti_escapeshellarg($this->command);
}
Expand Down Expand Up @@ -289,19 +290,24 @@ function retrieve_account() {

function decode($info) {
$info = base64_decode($info);
$info = unserialize($info);
$info = $info['password'];

return $info;
/*
* Legacy records were stored with serialize(). Detect and migrate on
* read so existing credentials survive the format change. New writes
* always use JSON (see encode()).
Comment on lines +295 to +297
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The block comment says legacy records are “migrated on read”, but the code only detects/decodes the legacy serialization format and does not write back the new JSON encoding. Either adjust the comment to avoid implying a database migration, or implement a real write-back migration when legacy data is successfully decoded.

Suggested change
* Legacy records were stored with serialize(). Detect and migrate on
* read so existing credentials survive the format change. New writes
* always use JSON (see encode()).
* Legacy records were stored with serialize(). Detect and decode them
* during reads so existing credentials continue to work after the format
* change. New writes always use JSON (see encode()).

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. The migration is read-only by design. A separate batch migration script would be needed to convert all stored records.

*/
if (substr($info, 0, strlen('a:')) === 'a:') {
$decoded = @unserialize($info, ['allowed_classes' => false]);
return is_array($decoded) ? ($decoded['password'] ?? '') : '';
}

$decoded = json_decode($info, true);
return is_array($decoded) ? ($decoded['password'] ?? '') : '';
Comment on lines 291 to +305
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decode() assumes base64_decode($info) returns a valid string. If $info is NULL/empty/invalid, base64_decode() can emit warnings and return non-string values, and then substr()/json_decode() will also warn and may end up treating the password as empty. Consider validating the input type and using strict base64 decoding (second arg true) with an explicit failure path before attempting legacy unserialize() / json_decode().

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid. Will add a null/false check on base64_decode return.

}

function encode($info) {
$a = array(rand(1,time()) => rand(1,time()),'password' => '', rand(1,time()) => rand(1,time()));
$a['password'] = $info;
$a = serialize($a);
$a = base64_encode($a);

return $a;
$a = ['password' => $info];
return base64_encode(json_encode($a));
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

encode() does not handle json_encode() failure. When json_encode() returns false (e.g., invalid UTF-8 in the password), base64_encode() will end up encoding an empty string, which can silently wipe stored credentials. Add error handling (e.g., check json_last_error() / use JSON_THROW_ON_ERROR and fail or fall back) so credentials are never overwritten with an empty/invalid encoding.

Suggested change
return base64_encode(json_encode($a));
$encoded = json_encode($a);
if ($encoded === false) {
/*
* Do not base64-encode a failed json_encode() result, as that can
* silently store an empty/invalid credential blob. Fall back to the
* legacy serialize() format, which decode() already supports.
*/
$this->error = 'ERROR: Failed to JSON encode WMI credentials: ' . json_last_error_msg();
$encoded = serialize($a);
}
return base64_encode($encoded);

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid. Will add json_encode failure check.

}
}

4 changes: 2 additions & 2 deletions poller_wmi.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ function process_all_devices() {

if (sizeof($dead_devices)) {
foreach($dead_devices as $device) {
db_execute('DELETE FROM host_wmi_cache WHERE host_id='. $device['host_id']);
db_execute('DELETE FROM host_wmi_query WHERE host_id='. $device['host_id']);
db_execute_prepared('DELETE FROM host_wmi_cache WHERE host_id=?', array($device['host_id']));
db_execute_prepared('DELETE FROM host_wmi_query WHERE host_id=?', array($device['host_id']));
print "Purged WMI Device with ID '" . $device['host_id'] . "'" . PHP_EOL;
}
}
Expand Down
2 changes: 1 addition & 1 deletion script/wmi-script.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function wmi_script($hostname, $host_id, $wmiquery, $cmd = '', $arg1 = '', $arg2
$wmi->binary = $config['base_path'] . '/plugins/wmi/wmic';

/* Fetch the info for this WMI query from the database, exit if not found */
$wmiinfo = db_fetch_row("SELECT * FROM plugin_wmi_queries WHERE queryname = '$wmiquery'", FALSE);
$wmiinfo = db_fetch_row_prepared('SELECT * FROM plugin_wmi_queries WHERE queryname = ?', [$wmiquery]);
if (!isset($wmiinfo['queryclass'])) {
return '';
}
Expand Down
Loading