#!/usr/local/bin/php
<?php

/*
 * Copyright (C) 2019 Deciso B.V.
 * Copyright (C) 2019-2026 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';

/* normalize the config path */
function strip_config_path($path)
{
   $path = preg_replace('/\.\.*/', '.', $path);
   $path = preg_replace('/^\./', '', $path);
   return preg_replace('/\.*$/', '', $path);
}

/* return simple type or json for config property */
function read_config_prop($content)
{
    global $jflags;

    if (is_array($content)) {
        return json_encode($content, $jflags) . PHP_EOL;
    } elseif ($content != '') {
        return $content . PHP_EOL;
    }

    return null;
}

/* retrieve config property by path (e.g. system.firmware) */
function get_config_prop($path, $cnf)
{
    $path = !is_array($path) ? explode('.', $path) : $path;
    $node_name = array_shift($path);

    if ($node_name != '') {
        $cnf = isset($cnf[$node_name]) ? $cnf[$node_name] : '';
    }

    if (!empty($path)) {
        return get_config_prop($path, $cnf);
    }

    return read_config_prop($cnf);
}

/* flush config property by path (but prevent dropping the root node) */
function flush_config_prop($path)
{
    global $config;

    if ($path == '') {
        echo "Cannot flush root node." . PHP_EOL;
        return;
    }

    $nodes = explode('.', $path);
    $final = array_pop($nodes);
    $cnf = &$config;

    foreach ($nodes as $node_name) {
        if (!isset($cnf[$node_name])) {
            echo "Cannot find $path" . PHP_EOL;
            return;
        }
        $cnf = &$cnf[$node_name];
    }

    if (!isset($cnf[$final])) {
        echo "Cannot find $path" . PHP_EOL;
        return;
    }

    echo "======= $path:" . PHP_EOL;
    echo read_config_prop($cnf[$final]);
    echo "=======" . PHP_EOL;

    echo "Do you want to flush this config property? [y/N]: ";

    $fp = fopen('php://stdin', 'r');
    $key = chop(fgets($fp));
    fclose($fp);

    if (!in_array($key, ['y', 'Y'])) {
        return;
    }

    unset($cnf[$final]);
    write_config("Flushed {$path} via pluginctl");

    echo "Done. A backup was created and can be restored if needed." . PHP_EOL;
}

function get_all_services($name = '', $id = '', $showall = true)
{
    $services = [];

    foreach (plugins_services() as $service) {
        /* fail if name filter does not match */
        if ($name != '' && $service['name'] != $name) {
            continue;
        }

        /* fail if ID lookup is not possible or does not match */
        if ($id !== '' && (!array_key_exists('id', $service) || $service['id'] != $id)) {
            continue;
        }

        /* hide extra services for low level features */
        if (!$showall && !empty($service['noshow'])) {
            continue;
        }

        /* fetch status so the caller does not have to */
        $service['status'] = service_message($service);

        /* collect all matches */
        $services[] = $service;
    }

    return $services;
}

function print_help()
{
    echo "Usage: pluginctl [-46cDdfghIiMmrQqSsvXx] ...\n\n";
    echo "\t-4 IPv4 address mode, return primary address of interface\n";
    echo "\t-6 IPv4 address mode, return primary address of interface\n";
    echo "\t-c configure mode (default), executes plugin [_configure] hook\n";
    echo "\t-D ifconfig mode, lists available devices\n";
    echo "\t-d device mode, lists registered devices\n";
    echo "\t-f flush config property (raw, e.g. system.firmware.plugins)\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 information mode, lists registered device statistics\n";
    echo "\t-i invoke dynamic interface registration\n";
    echo "\t-M invoke model migrations in quiet mode\n";
    echo "\t-m invoke model migrations in verbose mode\n";
    echo "\t-m queue mode returns interfaces and device dependencies\n";
    echo "\t-q quiet switch for configure mode\n";
    echo "\t-r run mode (e.g. command)\n";
    echo "\t-S service metadata dump\n";
    echo "\t-s service mode (e.g. myservice restart)\n";
    echo "\t-v invoke model validations\n";
    echo "\t-X XMLRPC sync metadata dump\n";
    echo "\t-x XMLRPC sync key/value list\n";
}

$jflags = JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_INVALID_UTF8_IGNORE;
$opts = getopt('46cDdfghIiMmrQqSsvXx', [], $optind);
$args = array_slice($argv, $optind);
$nopts = array_flip(str_split('q'));
$ret = 0;

/* mode is enforced by first seen mode argument, or 'c' if none given */
$mode = array_key_first(array_diff_key($opts, $nopts)) ?? 'c';

switch ($mode) {
    case '4':
    case '6':
        $ints = !empty($args[0]) ? [$args[0]] : array_keys($config['interfaces'] ?? []);
        $ifconfig_details = legacy_interfaces_details();
        $result = [];

        foreach ($ints as $int) {
            if (empty($config['interfaces'][$int])) {
                continue;
            }

            $result[$int] = [];

            foreach (['inet' => '4', 'inet6' => '6'] as $family => $opt) {
                if (!isset($opts[$opt])) {
                    continue;
                }

                $raw = $opt === '4' ? interfaces_primary_address($int, $ifconfig_details) :
                    interfaces_primary_address6($int, $ifconfig_details);

                $result[$int][] = [
                    'address' => $raw[0],
                    'network' => $raw[1],
                    'bits' => $raw[2],
                    'device' => $raw[3],
                    'interface' => $int,
                    'family' => $family,
                ];
            }
        }
        echo json_encode(!empty($result) ? $result : new ArrayObject(), $jflags) . PHP_EOL;
        break;
    case 'c':
        if (empty($args[0])) {
            /* no argument list all configure targets  */
            $results = [];
            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) {
                        if (empty($results[$when])) {
                            $results[$when] = [];
                        }
                        $results[$when][] = $name;
                    }
                }
            }

            ksort($results);

            foreach ($results as $key => $value) {
                echo "$key => " . implode(', ', $value) . "\n";
            }
        } else {
            /* second argument is hook */
            $hook = array_shift($args);
            /* other arguments are passed as is */
            $ret_int = plugins_configure($hook, !isset($opts['q']), $args);
            /* cannot taint $ret in this case */
            if ($ret_int && !isset($opts['q'])) {
                echo "The configuration hook `$hook' is not known to the system." . PHP_EOL;
            }
        }
        break;
    case 'D':
        echo json_encode(legacy_interfaces_details($args[0] ?? null), $jflags) . PHP_EOL;
        break;
    case '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);
                }
            }
        }
        break;
    case 'f':
        flush_config_prop(strip_config_path($args[0] ?? ''));
        break;
    case 'g':
        echo get_config_prop(strip_config_path($args[0] ?? ''), $config);
        break;
    case 'h':
        print_help();
        break;
    case 'I':
        echo json_encode(legacy_interface_stats($args[0] ?? null), $jflags) . PHP_EOL;
    case 'i':
        if (plugins_interfaces()) {
            write_config('Updated plugin interface configuration');
        }
        break;
    case 'M':
        pass_safe('/usr/local/opnsense/mvc/script/run_migrations.php', [], $ret);
        break;
    case 'm':
        pass_safe('/usr/local/opnsense/mvc/script/run_migrations.php -v', [], $ret);
        break;
    case 'Q':
        echo json_encode(interfaces_dependencies(), $jflags) . PHP_EOL;
        break;
    case 'r':
        $cmd = array_shift($args);
        $result = plugins_run($cmd, $args);
        if (!empty($result)) {
            echo json_encode($result, $jflags) . PHP_EOL;
        }
        break;
    case 'S':
        $services = get_all_services(isset($args[0]) ? $args[0] : '', isset($args[1]) ? $args[1] : '', false);
        echo json_encode($services, $jflags) . PHP_EOL;
        break;
    case 's':
        $results = [];
        $services = get_all_services(isset($args[0]) ? $args[0] : '', isset($args[2]) ? $args[2] : '');
        foreach ($services as $service) {
            if (empty($args[0])) {
                $results[$service['name']] = 1;
                continue;
            }

            $filter = isset($service['id']) ? ['id' => $service['id']] : [];
            $act = isset($args[1]) ? $args[1] : '';
            $name = $service['name'];

            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;
                case 'status':
                    echo htmlspecialchars($service['status']) . PHP_EOL;
                    break;
                default:
                    echo "Unknown command `$act'\n";
                    break;
            }
        }

        if (empty($args[0])) {
            ksort($results);

            foreach ($results as $key => $value) {
                echo "$key\n";
            }
        }

        break;
    case 'v':
        pass_safe('/usr/local/opnsense/mvc/script/run_validations.php', [], $ret);
        break;
    case 'X':
    case 'x':
        $payload = [];
        foreach (plugins_xmlrpc_sync() as $key => $data) {
            if (!empty($args[0]) && strcasecmp($key, $args[0])) {
                continue;
            }
            $payload[$key] = isset($opts['x']) ? ($data['description'] ?? '') : $data;
        }
        if (!empty($payload)) {
            natcasesort($payload);
        } else {
            $payload = new ArrayObject();
        }
        echo json_encode($payload, $jflags) . PHP_EOL;
        break;
    default:
        /* do nothing */
        break;
}

exit($ret);
