core/src/sbin/pluginctl
Franco Fichtner c2a9ce8c26 plugins: $verbose argument in plugins_run() is spurious
plugins_run() was copied from plugins_configure() but since plugins_run()
already provides output and the only caller ignores the $verbose it is
better remvoed.
2022-10-20 12:54:04 +02:00

173 lines
5.7 KiB
PHP
Executable File

#!/usr/local/bin/php
<?php
/*
* Copyright (C) 2019 Deciso B.V.
* Copyright (C) 2019-2022 Franco Fichtner <franco@opnsense.org>
* Copyright (C) 2017 Alexander Shursha <kekek2@ya.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
require_once 'config.inc';
require_once 'util.inc';
require_once 'system.inc';
require_once 'interfaces.inc';
require_once 'filter.inc';
require_once 'auth.inc';
/**
* retrieve config property by path (e.g. system.firmware), return simple type or json
*/
function get_config_prop($path, $cnf = null)
{
global $config;
$cnf = $cnf === null ? $config : $cnf;
$path = !is_array($path) ? explode('.', $path) : $path;
$node_name = array_shift($path);
if (isset($cnf[$node_name])) {
if (!empty($path)) {
return get_config_prop($path, $cnf[$node_name]);
} else {
$content = $cnf[$node_name];
if (is_array($content)) {
return json_encode($content, true);
} else {
return $content;
}
}
} else {
return null;
}
}
$opts = getopt('cdghirs', [], $optind);
$args = array_slice($argv, $optind);
if (isset($opts['h'])) {
echo "Usage: pluginctl [-c | -d | -g | -h | -i | -r | -s] ...\n\n";
echo "\t-c configure mode (default), executes plugin [_configure] hook\n";
echo "\t-d device mode, lists registered devices\n";
echo "\t-g get config property (raw, e.g. system.firmware.plugins)\n";
echo "\t-h show this help text and exit\n";
echo "\t-i invoke dynamic interface registration\n";
echo "\t-r run mode (e.g. command)\n";
echo "\t-s service mode (e.g. myservice restart)\n\n";
echo "Without arguments, a list of plugins of the requested type is shown.\n";
} elseif (isset($opts['d'])) {
foreach (plugins_devices() as $device) {
if (empty($device['type'])) {
continue;
}
if (empty($args[0])) {
echo $device['type'] . PHP_EOL;
} elseif ($args[0] == $device['type']) {
foreach (array_keys($device['names']) as $name) {
echo $name . PHP_EOL;
}
} elseif (in_array($args[0], array_keys($device['names']))) {
if (!empty($device['function'])) {
echo "Reconfiguring {$args[0]}...";
flush();
call_user_func_array($device['function'], [$args[0]]);
echo "done.\n";
} else {
echo "Device {$args[0]} cannot be reconfigured\n";
exit (1);
}
}
}
} elseif (isset($opts['s'])) {
$name = !empty($args[0]) ? $args[0] : '';
$act = !empty($args[1]) ? $args[1] : '';
$filter = [];
if (!empty($args[2])) {
$filter['id'] = $args[2];
}
switch ($act) {
case 'start':
echo service_control_start($name, $filter) . PHP_EOL;
break;
case 'stop':
echo service_control_stop($name, $filter) . PHP_EOL;
break;
case 'restart':
echo service_control_restart($name, $filter) . PHP_EOL;
break;
default:
echo "Unknown command `$act'\n";
break;
}
} elseif (isset($opts['r'])) {
$cmd = array_shift($args);
$result = plugins_run($cmd, $args);
if (!empty($result)) {
$response = json_encode($result, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
echo $response;
}
} elseif (isset($opts['g'])) {
echo get_config_prop($args[0]) . PHP_EOL;
} elseif (isset($opts['i'])) {
if (plugins_interfaces()) {
write_config('Updated plugin interface configuration');
}
} elseif (empty($args[0])) {
// no arguments, list plugins of selected type
$results = [];
if (isset($opts['s'])) {
foreach (plugins_services() as $service) {
$results[$service['name']] = 1;
}
} else {
foreach (plugins_scan() as $name => $path) {
try {
include_once $path;
} catch (\Error $e) {
error_log($e);
}
$func = sprintf('%s_configure', $name);
if (function_exists($func)) {
foreach ($func() as $when => $worker) {
$results[$when] = 1;
}
}
}
}
$results = array_keys($results);
sort($results);
foreach ($results as $result) {
echo "$result\n";
}
} else {
/* second argument is hook */
$hook = array_shift($args);
/* other arguments are passed as is */
plugins_configure($hook, true, $args);
}
exit (0);