summaryrefslogtreecommitdiffstats
path: root/modules-available/sysconfig/inc/configmodule/sshconfig.inc.php
blob: a62d1035fd796abbdf12fa258b518798c92b19d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php

ConfigModule::registerModule(
	ConfigModule_SshConfig::MODID, // ID
	Dictionary::translateFileModule('sysconfig', 'config-module', 'sshconfig_title'), // Title
	Dictionary::translateFileModule('sysconfig', 'config-module', 'sshconfig_description'), // Description
	Dictionary::translateFileModule('sysconfig', 'config-module', 'group_sshconfig'), // Group
	true, // Only one per config?
	500
);

class ConfigModule_SshConfig extends ConfigModule
{
	const MODID = 'SshConfig';
	const VERSION = 1;

	protected function generateInternal(string $tgz, ?string $parent)
	{
		if (!$this->validateConfig())
			return false;
		$config = $this->moduleData + array(
			'filename' => $tgz,
			'failOnParentFail' => false,
			'parent' => $parent
		);
		return Taskmanager::submit('SshdConfigGenerator', $config);
	}

	protected function moduleVersion(): int
	{
		return self::VERSION;
	}

	protected function validateConfig(): bool
	{
		// UPGRADE
		if (isset($this->moduleData['allowPasswordLogin']) && !isset($this->moduleData['allowedUsersLogin'])) {
			$this->moduleData['allowPasswordLogin'] = strtoupper($this->moduleData['allowPasswordLogin']);
			if (!in_array($this->moduleData['allowPasswordLogin'], ['NO', 'USER_ONLY', 'YES'])) {
				$this->moduleData['allowPasswordLogin'] = 'NO';
			}
			$this->moduleData['allowedUsersLogin'] = 'ALL';
		}
		return isset($this->moduleData['allowPasswordLogin']) && isset($this->moduleData['allowedUsersLogin'])
			&& isset($this->moduleData['listenPort']);
	}

	public function setData(string $key, $value): bool
	{
		switch ($key) {
		case 'publicKey':
			if ($value === false) {
				error_log('Unsetting publicKey');
				unset($this->moduleData[$key]);
				return true;
			}
			return false;
		case 'allowPasswordLogin':
			if (!in_array($value, ['NO', 'USER_ONLY', 'YES']))
				return false;
			break;
		case 'allowedUsersLogin';
			if (!in_array($value, ['ROOT_ONLY', 'USER_ONLY', 'ALL']))
				return false;
			break;
		case 'listenPort':
			if (!is_numeric($value) || $value < 1 || $value > 65535)
				return false;
			$value = (int)$value;
			break;
		default:
			return false;
		}
		$this->moduleData[$key] = $value;
		return true;
	}

}