Tony Murray fad0bffc43 Escape SNMP & IPMI shell commands (#9537)
hopefully doesn't break anything
Mostly issues with snmp oids and options containing spaces.  Try to remove all of those.

DO NOT DELETE THIS TEXT

#### Please note

> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.

- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)

#### Testers

If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
After you are done testing, you can remove the changes with `./scripts/github-remove`.  If there are schema changes, you can ask on discord how to revert.
2018-12-16 13:42:50 +00:00

76 lines
2.3 KiB
PHP

<?php
/**
* output.php
*
* runs the requested command and outputs as a file or json
*
* 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 3 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2016 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
use LibreNMS\Authentication\LegacyAuth;
if (!LegacyAuth::user()->hasGlobalAdmin()) {
echo("Insufficient Privileges");
exit();
}
$hostname = escapeshellcmd($_REQUEST['hostname']);
$type = $_REQUEST['type'];
switch ($type) {
case 'poller':
$cmd = ['php', $config['install_dir'] . '/poller.php', '-h', $hostname, '-r', '-f', '-d'];
$filename = "poller-$hostname.txt";
break;
case 'snmpwalk':
$device = device_by_name($hostname);
$cmd = gen_snmpwalk_cmd($device, '.', '-OUneb');
$filename = $device['os'] . '-' . $device['hostname'] . '.snmpwalk';
break;
case 'discovery':
$cmd = ['php', $config['install_dir'] . '/discovery.php', '-h', $hostname, '-d'];
$filename = "discovery-$hostname.txt";
break;
default:
echo 'You must specify a valid type';
exit;
}
// ---- Output ----
$proc = new \Symfony\Component\Process\Process($cmd);
if ($_GET['format'] == 'text') {
header("Content-type: text/plain");
header('X-Accel-Buffering: no');
$proc->run(function ($type, $buffer) {
echo preg_replace('/\033\[[\d;]+m/', '', $buffer) . PHP_EOL;
ob_flush();
flush(); // you have to flush buffer
});
} elseif ($_GET['format'] == 'download') {
$proc->run();
$output = $proc->getOutput();
$output = preg_replace('/\033\[[\d;]+m/', '', $output);
file_download($filename, $output);
}