|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) MIKO LLC - All Rights Reserved |
|
4
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited |
|
5
|
|
|
* Proprietary and confidential |
|
6
|
|
|
* Written by Nikolay Beketov, 7 2020 |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace MikoPBX\Core\System\Configs; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
use MikoPBX\Core\System\MikoPBXConfig; |
|
14
|
|
|
use MikoPBX\Core\System\Util; |
|
15
|
|
|
use Phalcon\Di\Injectable; |
|
16
|
|
|
|
|
17
|
|
|
class SSHConf extends Injectable |
|
18
|
|
|
{ |
|
19
|
|
|
private MikoPBXConfig $mikoPBXConfig; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* SSHConf constructor. |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->mikoPBXConfig = new MikoPBXConfig(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Configure SSH settings |
|
31
|
|
|
**/ |
|
32
|
|
|
public function configure() |
|
33
|
|
|
{ |
|
34
|
|
|
$dropbear_dir = '/etc/dropbear'; |
|
35
|
|
|
Util::mwMkdir($dropbear_dir); |
|
36
|
|
|
|
|
37
|
|
|
$keytypes = [ |
|
38
|
|
|
"rsa" => "SSHRsaKey", |
|
39
|
|
|
"dss" => "SSHDssKey", |
|
40
|
|
|
"ecdsa" => "SSHecdsaKey" // SSHecdsaKey // SSHEcdsaKey |
|
41
|
|
|
]; |
|
42
|
|
|
// Get keys from DB |
|
43
|
|
|
$dropbearkeyPath = Util::which('dropbearkey'); |
|
44
|
|
|
$dropbearPath = Util::which('dropbear'); |
|
45
|
|
|
foreach ($keytypes as $keytype => $db_key) { |
|
46
|
|
|
$res_keyfilepath = "{$dropbear_dir}/dropbear_" . $keytype . "_host_key"; |
|
47
|
|
|
$key = $this->mikoPBXConfig->getGeneralSettings($db_key); |
|
48
|
|
|
$key = (isset($key) && is_string($key)) ? trim($key) : ""; |
|
49
|
|
|
if (strlen($key) > 100) { |
|
50
|
|
|
// Store key to file |
|
51
|
|
|
file_put_contents($res_keyfilepath, base64_decode($key)); |
|
52
|
|
|
} |
|
53
|
|
|
// If key not exists, we will generate and store new one into file and database |
|
54
|
|
|
if ( ! file_exists($res_keyfilepath)) { |
|
55
|
|
|
// Generation |
|
56
|
|
|
Util::mwExec("{$dropbearkeyPath} -t $keytype -f $res_keyfilepath"); |
|
57
|
|
|
// Storing |
|
58
|
|
|
$new_key = base64_encode(file_get_contents($res_keyfilepath)); |
|
59
|
|
|
$this->mikoPBXConfig->setGeneralSettings("$db_key", "$new_key"); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
$ssh_port = escapeshellcmd($this->mikoPBXConfig->getGeneralSettings('SSHPort')); |
|
63
|
|
|
// Restart dropbear |
|
64
|
|
|
Util::killByName('dropbear'); |
|
65
|
|
|
usleep(500000); |
|
66
|
|
|
Util::mwExec("{$dropbearPath} -p '{$ssh_port}' -c /etc/rc/hello > /var/log/dropbear_start.log"); |
|
67
|
|
|
$this->generateAuthorizedKeys(); |
|
68
|
|
|
$this->updateShellPassword(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Stores authorized_keys from DB to files |
|
73
|
|
|
*/ |
|
74
|
|
|
public function generateAuthorizedKeys(): void |
|
75
|
|
|
{ |
|
76
|
|
|
$ssh_dir = '/root/.ssh'; |
|
77
|
|
|
Util::mwMkdir($ssh_dir); |
|
78
|
|
|
$conf_data = $this->mikoPBXConfig->getGeneralSettings('SSHAuthorizedKeys'); |
|
79
|
|
|
file_put_contents("{$ssh_dir}/authorized_keys", $conf_data); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Setups root user password |
|
84
|
|
|
**/ |
|
85
|
|
|
public function updateShellPassword(): void |
|
86
|
|
|
{ |
|
87
|
|
|
$password = $this->mikoPBXConfig->getGeneralSettings('SSHPassword'); |
|
88
|
|
|
$echoPath = Util::which('echo'); |
|
89
|
|
|
$chpasswdPath = Util::which('chpasswd'); |
|
90
|
|
|
Util::mwExec("{$echoPath} \"root:$password\" | {$chpasswdPath}"); |
|
91
|
|
|
} |
|
92
|
|
|
} |