155 lines
6 KiB
PHP
155 lines
6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require dirname(__DIR__) . '/src/lib.php';
|
|
require dirname(__DIR__) . '/src/actions.php';
|
|
|
|
start_session();
|
|
|
|
if (empty($_SESSION['user'])) {
|
|
page_start('tinyauth-web');
|
|
echo '<h1>tinyauth-web</h1><p><a class="button" href="login.php">Log in with tinyauth</a></p>';
|
|
page_end();
|
|
exit;
|
|
}
|
|
|
|
$groups = $_SESSION['groups'] ?? [];
|
|
$allowedGroup = load_settings()['allowed_group'] ?? '';
|
|
$authorized = is_authorized($groups);
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
require_csrf();
|
|
$action = (string) ($_POST['action'] ?? '');
|
|
|
|
if ($allowedGroup === '' && $action === 'setup') {
|
|
handle_setup($groups);
|
|
} elseif (!$authorized) {
|
|
http_response_code(403);
|
|
exit('Forbidden');
|
|
} elseif ($action === 'reset_group') {
|
|
handle_reset_group();
|
|
} elseif (in_array($action, SERVICE_ACTIONS, true)) {
|
|
handle_service($action);
|
|
} elseif ($action === 'save_config') {
|
|
handle_save_config();
|
|
}
|
|
redirect('index.php');
|
|
}
|
|
|
|
$cfg = config();
|
|
$flashes = take_flashes();
|
|
$lastOutput = $_SESSION['last_output'] ?? '';
|
|
unset($_SESSION['last_output']);
|
|
$token = csrf_token();
|
|
|
|
page_start('tinyauth-web');
|
|
echo '<header><h1>tinyauth-web</h1><span>' . h($_SESSION['user']) . ' · <a href="logout.php">log out</a></span></header>';
|
|
foreach ($flashes as $flash) {
|
|
echo '<p class="flash ' . h($flash['type']) . '">' . h($flash['message']) . '</p>';
|
|
}
|
|
|
|
if ($allowedGroup === '') {
|
|
echo '<section><h2>First-time setup</h2>';
|
|
echo '<p>Choose the tinyauth group whose members may use this tool.</p>';
|
|
if ($groups === []) {
|
|
echo '<p class="flash error">Your account has no groups in tinyauth. Add yourself to a group first, then reload.</p>';
|
|
} else {
|
|
echo '<form method="post"><input type="hidden" name="csrf" value="' . h($token) . '">';
|
|
echo '<input type="hidden" name="action" value="setup">';
|
|
foreach ($groups as $group) {
|
|
echo '<label><input type="radio" name="group" value="' . h($group) . '" required> ' . h($group) . '</label><br>';
|
|
}
|
|
echo '<p><button type="submit">Save access group</button></p></form>';
|
|
}
|
|
echo '</section>';
|
|
page_end();
|
|
exit;
|
|
}
|
|
|
|
if (!$authorized) {
|
|
echo '<p class="flash error">You are not in the required tinyauth group <b>' . h($allowedGroup) . '</b>.</p>';
|
|
page_end();
|
|
exit;
|
|
}
|
|
|
|
$perms = [];
|
|
foreach (SERVICE_ACTIONS as $action) {
|
|
$perms[$action] = service_allowed($action);
|
|
}
|
|
$status = $perms['status'] ? service_run('status') : null;
|
|
$state = 'unknown';
|
|
if ($status !== null && preg_match('/Active:\s+(\S+)/', $status['output'], $m)) {
|
|
$state = $m[1];
|
|
}
|
|
$service = $cfg['service'];
|
|
|
|
echo '<section><h2>Service: <code>' . h($service) . '</code></h2>';
|
|
echo '<p>State: <span class="badge">' . h($state) . '</span></p>';
|
|
echo '<form method="post" style="display:inline-flex;gap:.5rem">';
|
|
echo '<input type="hidden" name="csrf" value="' . h($token) . '">';
|
|
foreach (SERVICE_ACTIONS as $action) {
|
|
$disabled = $perms[$action] ? '' : ' disabled';
|
|
echo '<button type="submit" name="action" value="' . h($action) . '"' . $disabled . '>' . h(ucfirst($action)) . '</button>';
|
|
}
|
|
echo '</form>';
|
|
if ($status !== null) {
|
|
echo '<pre>' . h($status['output']) . '</pre>';
|
|
}
|
|
if ($lastOutput !== '') {
|
|
echo '<pre>' . h($lastOutput) . '</pre>';
|
|
}
|
|
echo '</section>';
|
|
|
|
$envPath = $cfg['env_file'];
|
|
$envReadable = is_file($envPath) && is_readable($envPath);
|
|
$envWritable = $envReadable && is_writable($envPath);
|
|
$dataWritable = is_writable(data_dir());
|
|
|
|
echo '<section><h2>Permission checks</h2><table>';
|
|
foreach (SERVICE_ACTIONS as $action) {
|
|
echo '<tr><td>sudo: systemctl ' . h($action) . ' ' . h($service) . '</td><td>' . permission_mark($perms[$action]) . '</td></tr>';
|
|
}
|
|
echo '<tr><td>Env file readable (' . h($envPath) . ')</td><td>' . permission_mark($envReadable) . '</td></tr>';
|
|
echo '<tr><td>Env file writable</td><td>' . permission_mark($envWritable) . '</td></tr>';
|
|
echo '<tr><td>Data directory writable</td><td>' . permission_mark($dataWritable) . '</td></tr>';
|
|
echo '</table>';
|
|
if (in_array(false, $perms, true)) {
|
|
echo '<p>Missing sudo rules: install <code>sudoers/tinyauth-web.sudoers</code> from this repo (check with <code>visudo -cf</code>).</p>';
|
|
}
|
|
echo '<form method="post" style="margin-top:.8rem"><input type="hidden" name="csrf" value="' . h($token) . '">';
|
|
echo '<button type="submit" name="action" value="reset_group">Change access group</button></form>';
|
|
echo '</section>';
|
|
|
|
echo '<section><h2>Config: <code>' . h($envPath) . '</code></h2>';
|
|
if (!$envReadable) {
|
|
echo '<p class="flash error">The env file cannot be read by this user.</p>';
|
|
} else {
|
|
$vars = env_parse((string) file_get_contents($envPath));
|
|
echo '<form method="post"><input type="hidden" name="csrf" value="' . h($token) . '">';
|
|
echo '<input type="hidden" name="action" value="save_config">';
|
|
echo '<table><tr><th>Variable</th><th>Value</th><th>Delete</th></tr>';
|
|
foreach ($vars as $key => $value) {
|
|
$name = h($key);
|
|
echo '<tr><td><code>' . $name . '</code></td><td>';
|
|
if (preg_match(SECRET_PATTERN, $key)) {
|
|
$hint = $value === '' ? '(not set)' : '(set, leave blank to keep)';
|
|
echo '<input type="password" name="value[' . $name . ']" placeholder="' . h($hint) . '" autocomplete="off">';
|
|
} else {
|
|
echo '<input type="text" name="value[' . $name . ']" value="' . h($value) . '">';
|
|
}
|
|
echo '</td><td><input type="checkbox" name="delete[]" value="' . $name . '"></td></tr>';
|
|
}
|
|
echo '<tr><td><input type="text" name="new_key" placeholder="NEW_VARIABLE"></td>';
|
|
echo '<td><input type="text" name="new_value" placeholder="value"></td><td>add</td></tr>';
|
|
echo '</table>';
|
|
echo '<p><button type="submit"' . ($envWritable ? '' : ' disabled') . '>Save config</button> ';
|
|
echo 'Saving writes a backup first. Restart tinyauth to apply changes.</p>';
|
|
echo '</form>';
|
|
}
|
|
echo '</section>';
|
|
page_end();
|
|
|
|
function permission_mark(bool $ok): string
|
|
{
|
|
return $ok ? '<span class="yes">✔ ok</span>' : '<span class="no">✘ missing</span>';
|
|
}
|