-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.php
More file actions
351 lines (334 loc) · 9.98 KB
/
install.php
File metadata and controls
351 lines (334 loc) · 9.98 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?PHP
/** KontrolVM By KuJoe (https://github.com/KuJoe/kontrolvm) **/
use phpseclib3\Crypt\EC;
require __DIR__ . '/vendor/autoload.php';
$filename = 'LOCKED';
if(file_exists($filename)) {
die("The directory is locked. Please delete the LOCKED file if you are sure you need to run the install.php file (this might overwrite existing data in the database if it exists).");
}
$exampleFile = __DIR__ . '/config.php.example';
$configFile = __DIR__ . '/config.php';
if (file_exists($configFile)) {
require_once('config.php');
} else {
if (file_exists($exampleFile)) {
if (rename($exampleFile, $configFile)) {
require_once('config.php');
} else {
die("Failed to rename config.php.example to config.php.");
}
} else {
die("config.php does not exist and config.php.example does not exist.");
}
}
function generateRandomString($length = 16) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function addSetting($name,$value) {
$stmt = $conn->prepare("INSERT INTO settings (setting_name, setting_value) VALUES (:setting_name, :setting_value)");
$stmt->bindValue(':setting_name', "$name", SQLITE3_TEXT);
$stmt->bindValue(':setting_value', "$value", SQLITE3_TEXT);
$stmt->execute();
return true;
}
$newEncrypt = generateRandomString('32');
$fileContent = file_get_contents($configFile);
$updatedContent = str_replace($cryptkey, $newEncrypt, $fileContent);
if (file_put_contents($configFile, $updatedContent) === false) {
die("Error: Could not write to file: $configFile");
}
//Generate the super user for later.
$username = "admin";
$password = substr(md5(time()), 0, 16);
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
try {
//Create the database tables.
$conn = new PDO("sqlite:$db_file_path");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$tables = [
'nodes' => "CREATE TABLE nodes (
node_id INTEGER PRIMARY KEY AUTOINCREMENT,
hostname TEXT NOT NULL UNIQUE,
ipaddr TEXT NOT NULL,
cluster INTEGER,
cpu_cores INTEGER,
total_memory INTEGER,
disk_space INTEGER,
sshport INTEGER,
sshuser TEXT,
sshkey TEXT,
uptime TEXT,
memused TEXT,
disksys TEXT,
diskclnt TEXT,
load TEXT,
make TEXT,
model TEXT,
cpu TEXT,
vms INTEGER,
lastvnc INTEGER,
lastws INTEGER,
lastvm INTEGER,
os_version TEXT,
kernel_version TEXT,
libvirt_version TEXT,
status INTEGER,
last_updated DATETIME
)",
'last_run' => "CREATE TABLE IF NOT EXISTS last_run (
run_id INTEGER PRIMARY KEY AUTOINCREMENT,
script_name TEXT NOT NULL,
last_run_time INTEGER
)",
'ostemplates' => "CREATE TABLE IF NOT EXISTS ostemplates (
template_id INTEGER PRIMARY KEY AUTOINCREMENT,
filename TEXT NOT NULL,
friendlyname TEXT NOT NULL,
status TEXT NOT NULL,
added DATETIME
)",
'ipv4' => "CREATE TABLE IF NOT EXISTS ipv4 (
ip_id INTEGER PRIMARY KEY AUTOINCREMENT,
ipaddress TEXT NOT NULL,
gwip TEXT NOT NULL,
bw INTEGER,
vmid INTEGER,
node INTEGER,
cluster INTEGER,
notes TEXT,
status INTEGER,
last_updated DATETIME
)",
'ipv6' => "CREATE TABLE IF NOT EXISTS ipv6 (
ip_id INTEGER PRIMARY KEY AUTOINCREMENT,
ipaddress TEXT NOT NULL,
gwip TEXT NOT NULL,
bw INTEGER,
vmid INTEGER,
node INTEGER,
cluster INTEGER,
notes TEXT,
status INTEGER,
last_updated DATETIME
)",
'staff' => "CREATE TABLE staff (
staff_id INTEGER PRIMARY KEY AUTOINCREMENT,
staff_username TEXT NOT NULL DEFAULT '',
staff_email TEXT,
staff_password TEXT NOT NULL DEFAULT 0,
staff_pwreset TEXT,
staff_active INTEGER NOT NULL DEFAULT 0,
staff_rememberme_token TEXT,
staff_mfa TEXT,
staff_role INTEGER,
staff_ip TEXT,
staff_lastlogin TEXT,
staff_failed_logins INTEGER NOT NULL DEFAULT 0,
staff_locked DATETIME NOT NULL DEFAULT '1970-01-01 00:00:01'
)",
'vms' => "CREATE TABLE vms (
vm_id INTEGER PRIMARY KEY AUTOINCREMENT,
node_id INTEGER NOT NULL,
name TEXT NOT NULL,
hostname TEXT NOT NULL,
status INTEGER,
protected INTEGER,
cluster INTEGER,
cpu_cores INTEGER,
memory INTEGER,
ipv4 INTEGER,
ipv6 INTEGER,
os_template TEXT,
os_type TEXT,
mac_address TEXT,
nic INTEGER,
iow INTEGER,
notes TEXT,
vncpw TEXT,
vncport INTEGER,
vncexpire INTEGER,
websockify INTEGER,
netdriver TEXT,
network TEXT,
diskdriver TEXT,
bootorder TEXT,
created_at DATETIME,
last_updated DATETIME
)",
'disks' => "CREATE TABLE IF NOT EXISTS disks (
disk_id INTEGER PRIMARY KEY AUTOINCREMENT,
disk_name TEXT NOT NULL,
disk_size INTEGER,
vm_id INTEGER,
node_id INTEGER,
last_updated DATETIME
)",
'clusters' => "CREATE TABLE IF NOT EXISTS clusters (
cluster_id INTEGER PRIMARY KEY AUTOINCREMENT,
friendlyname TEXT NOT NULL,
notes TEXT,
deployment INTEGER,
status INTEGER,
last_updated DATETIME
)",
'settings' => "CREATE TABLE IF NOT EXISTS settings (
setting_id INTEGER PRIMARY KEY AUTOINCREMENT,
setting_name TEXT NOT NULL,
setting_value TEXT NOT NULL
)",
'logs' => "CREATE TABLE IF NOT EXISTS logs (
log_id INTEGER PRIMARY KEY AUTOINCREMENT,
log_message TEXT NOT NULL,
staff_id INTEGER,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)",
'backups' => "CREATE TABLE IF NOT EXISTS backups (
backup_id INTEGER PRIMARY KEY AUTOINCREMENT,
backup_name TEXT NOT NULL,
backup_size INTEGER,
vm_id INTEGER,
node_id INTEGER,
status INTEGER,
created_at DATETIME
)",
'nics' => "CREATE TABLE IF NOT EXISTS nics (
nic_id INTEGER PRIMARY KEY AUTOINCREMENT,
nic_name TEXT NOT NULL,
mac_address TEXT,
vm_id INTEGER NOT NULL,
node_id INTEGER NOT NULL,
last_updated DATETIME
)",
'networks' => "CREATE TABLE IF NOT EXISTS networks (
net_id INTEGER PRIMARY KEY AUTOINCREMENT,
net_name TEXT NOT NULL,
node_id INTEGER NOT NULL,
last_updated DATETIME
)"
];
foreach ($tables as $name => $sql) {
$stmt = $conn->prepare($sql);
$stmt->execute();
}
// Create Super Admin account
$stmt = $conn->prepare("INSERT INTO staff (staff_username, staff_password, staff_active, staff_role) VALUES (:username, :password, :active, :role)");
$stmt->bindValue(':username', "$username", SQLITE3_TEXT);
$stmt->bindValue(':password', "$hashedPassword", SQLITE3_TEXT);
$stmt->bindValue(':active', '1', SQLITE3_INTEGER);
$stmt->bindValue(':role', '9', SQLITE3_INTEGER);
$stmt->execute();
// Create SystemRescueCD ISO
$stmt = $conn->prepare('INSERT INTO ostemplates (filename, friendlyname, status, added) VALUES (:filename, :friendlyname, :status, :added)');
$stmt->bindValue(':filename', "systemrescue-amd64.iso", SQLITE3_TEXT);
$stmt->bindValue(':friendlyname', "SystemRescueCD", SQLITE3_TEXT);
$stmt->bindValue(':status', '1', SQLITE3_INTEGER);
$stmt->bindValue(':added', time(), SQLITE3_TEXT);
$result = $stmt->execute();
//// Populate default settings
//$setting = [
//'bgupdate' => "true",
//'' => "",
//'' => "",
//];
//foreach ($setting as $name => $value) {
// addSetting($name,$value);
//}
//Create the SSH key
$privateKey = EC::createKey('Ed25519');
$publicKey = $privateKey->getPublicKey();
$privateKeyString = $privateKey->toString('OpenSSH');
$publicKeyString = $publicKey->toString('OpenSSH');
file_put_contents($sshkeypriv, $privateKeyString);
file_put_contents($sshkeypub, $publicKeyString);
//Create wget_isos.sh file
$localFile = "wget_isos.sh";
$wgetCommand = '/usr/bin/wget -O /home/kontrolvm/isos/systemrescue-amd64.iso https://sourceforge.net/projects/systemrescuecd/files/sysresccd-x86/11.03/systemrescue-11.03-amd64.iso/download';
file_put_contents($localFile, $wgetCommand . PHP_EOL, FILE_APPEND | LOCK_EX) !== false;
$success = "Database has been deployed and the tables have been successfully created.<br />An SSH key has been generated for internal use.";
} catch(PDOException $e) {
$error = $e->getMessage();
}
$file = fopen('LOCKED', 'w');
if($file == false) {
$lock = "Unable to lock the directory to prevent the install.php script from being run again. Either manually create a file named <strong>LOCKED</strong> in this directory or delete the install.php to be safe.";
} else {
$lock = "Lock file created to prevent the install.php file from being run again. You can delete the install.php file just to safe.";
fclose($file);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>KontrolVM</title>
<style>
body {
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f4f4f7;
}
.container {
background-color: #fff;
padding: 40px;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
max-width: 400px;
width: 100%;
}
h2 {
text-align: center;
margin-bottom: 20px;
}
.error-message {
color: #dc3545;
background-color: #f8d7da;
border: 1px solid #f5c6cb;
padding: 10px;
margin-bottom: 15px;
border-radius: 4px;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<img src="assets/logo.png" alt="KontrolVM Logo" style="display:block;margin:0 auto;" />
<br />
<br />
<?php if(isset($error)) { ?>
<h2 style="color:red;">Install Fail</h2>
<br />
<div class="error-message"><?php echo $error; ?></div>
<?php } ?>
<?php if(isset($success)) { ?>
<h2 style="color:green;">Install Success</h2>
<br />
<div><?php echo $success; ?></div>
<p>
<br />
The following user was created:<br />
Username: <b><?php echo $username; ?></b><br />
Password: <b><?php echo $password; ?></b><br />
<i>Write these down, they will not be displayed again and cannot be recovered at this time.</i>
<br />
<?php echo $lock; ?>
<br />
<br />
<a href="index.php">LOGIN</a>
</p>
<?php } ?>
<br /><br />
<p style="text-align:center;font-size:0.9em;">Powered by <a href="https://github.com/KuJoe/kontrolvm" target="_blank">KontrolVM</a></p>
</div>
</body>
</html>