tinyauth-web

Log in with tinyauth

'; 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 '

tinyauth-web

' . h($_SESSION['user']) . ' · log out
'; foreach ($flashes as $flash) { echo '

' . h($flash['message']) . '

'; } if ($allowedGroup === '') { echo '

First-time setup

'; echo '

Choose the tinyauth group whose members may use this tool.

'; if ($groups === []) { echo '

Your account has no groups in tinyauth. Add yourself to a group first, then reload.

'; } else { echo '
'; echo ''; foreach ($groups as $group) { echo '
'; } echo '

'; } echo '
'; page_end(); exit; } if (!$authorized) { echo '

You are not in the required tinyauth group ' . h($allowedGroup) . '.

'; 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 '

Service: ' . h($service) . '

'; echo '

State: ' . h($state) . '

'; echo '
'; echo ''; foreach (SERVICE_ACTIONS as $action) { $disabled = $perms[$action] ? '' : ' disabled'; echo ''; } echo '
'; if ($status !== null) { echo '
' . h($status['output']) . '
'; } if ($lastOutput !== '') { echo '
' . h($lastOutput) . '
'; } echo '
'; $envPath = $cfg['env_file']; $envReadable = is_file($envPath) && is_readable($envPath); $envWritable = $envReadable && is_writable($envPath); $dataWritable = is_writable(data_dir()); echo '

Permission checks

'; foreach (SERVICE_ACTIONS as $action) { echo ''; } echo ''; echo ''; echo ''; echo '
sudo: systemctl ' . h($action) . ' ' . h($service) . '' . permission_mark($perms[$action]) . '
Env file readable (' . h($envPath) . ')' . permission_mark($envReadable) . '
Env file writable' . permission_mark($envWritable) . '
Data directory writable' . permission_mark($dataWritable) . '
'; if (in_array(false, $perms, true)) { echo '

Missing sudo rules: install sudoers/tinyauth-web.sudoers from this repo (check with visudo -cf).

'; } echo '
'; echo '
'; echo '
'; echo '

Config: ' . h($envPath) . '

'; if (!$envReadable) { echo '

The env file cannot be read by this user.

'; } else { $vars = env_parse((string) file_get_contents($envPath)); echo '
'; echo ''; echo ''; foreach ($vars as $key => $value) { $name = h($key); echo ''; } echo ''; echo ''; echo '
VariableValueDelete
' . $name . ''; if (preg_match(SECRET_PATTERN, $key)) { $hint = $value === '' ? '(not set)' : '(set, leave blank to keep)'; echo ''; } else { echo ''; } echo '
add
'; echo '

'; echo 'Saving writes a backup first. Restart tinyauth to apply changes.

'; echo '
'; } echo '
'; page_end(); function permission_mark(bool $ok): string { return $ok ? '✔ ok' : '✘ missing'; }