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
|
<?php
if ($_SERVER['REMOTE_ADDR'] !== $_SERVER['SERVER_ADDR']) {
die();
}
include "db.php";
if (array_key_exists('drop',$_GET) && ($_GET['drop']=='DROP')) {
$db -> exec('DROP TABLE IF EXISTS "machines"');
$db -> exec('DROP TABLE IF EXISTS "keys"');
$db -> exec('DROP TABLE IF EXISTS "permissions"');
$db -> exec('DROP TABLE IF EXISTS "states"');
$db -> exec('DROP TABLE IF EXISTS "values"');
$db -> exec('CREATE TABLE "machines" (id integer primary key AUTOINCREMENT, name varchar(32) not null unique, last_update TIMESTAMP default CURRENT_TIMESTAMP);');
$db -> exec('CREATE TABLE "keys" (id integer primary key AUTOINCREMENT, key varchar(128) not null unique, comment varchar(128) not null);');
$db -> exec('CREATE TABLE "states" (id integer primary key AUTOINCREMENT, name varchar(32) not null unique)');
$db -> exec('CREATE TABLE "permissions" (key_id integer not null, value_id integer not null)');
$db -> exec('CREATE TABLE "values" (id integer primary key AUTOINCREMENT, machine_id integer not null, state_id integer not null, value integer)');
$stm = $db -> prepare('INSERT INTO states (name) VALUES (:state)');
if (! $stm)
die();
foreach (array('running', 'ping', 'power') as $state) {
$stm -> bindValue('state', $state);
if (! $stm -> execute())
die();
}
echo 'ok' . "\n";
die();
}
if (array_key_exists('machine',$_GET)) {
$db -> add_machine($_GET['machine']);
if (array_key_exists('new_key', $_GET) && array_key_exists('comment', $_GET))
$db -> add_key_for($_GET['machine'], $_GET['new_key'], $_GET['comment']);
$stm = $db -> prepare(
'SELECT states.name, keys.key, keys.comment FROM machines' .
' JOIN "values" ON "values".machine_id = machines.id' .
' JOIN permissions ON permissions.value_id = "values".id' .
' JOIN states ON "values".state_id = states.id' .
' JOIN keys ON permissions.key_id = keys.id' .
' WHERE machines.name=:machine');
$stm -> bindValue('machine', $_GET['machine']);
$result = $stm -> execute();
while ($row = $result -> fetchArray()) {
echo 'permission = "' . $row['name'] . '", key = "' . $row['key'] . '", comment = "' . $row['comment'] . '" <br>' . "\n";
}
echo 'done.' . "\n";
die();
}
if (array_key_exists('ddns_machines', $_GET)) {
$ddns_tokens = file('/srv/http/vhosts/eckner.net/ddns/tokens');
foreach ($ddns_tokens as $ddns_token) {
$db -> add_machine(explode(' ', trim($ddns_token), 2)[1]);
}
echo 'done.' . "\n";
die();
}
if (array_key_exists('ddns', $_GET)) {
$result = $db -> query(
'SELECT keys.id FROM keys' .
' WHERE keys.comment = ' . "'" . 'ddns' . "'"
);
$row = $result -> fetchArray();
if (! $row)
$key_id = $db -> add_key('ddns');
else
$key_id = $row['id'];
$stm = $db -> prepare('INSERT INTO permissions (key_id, value_id) VALUES (:key_id, :value_id)');
$stm -> bindValue('key_id', $key_id);
$getstm = $db -> prepare(
'SELECT "values".id as value_id FROM "values"' .
' JOIN states ON states.id = "values".state_id' .
' WHERE states.name = :state' .
' AND NOT EXISTS (' .
'SELECT 1 FROM permissions' .
' WHERE permissions.value_id = "values".id' .
' AND permissions.key_id = :key_id' .
')'
);
$getstm -> bindValue('state', 'running');
$getstm -> bindValue('key_id', $key_id);
$result = $getstm -> execute();
while ($row = $result -> fetchArray()) {
$stm -> bindValue('value_id', $row['value_id']);
$stm -> execute();
}
$getstm -> close();
$stm -> close();
$stm = $db -> prepare('SELECT keys.key from keys WHERE keys.id = :key_id');
$stm -> bindValue('key_id', $key_id);
$result = $stm -> execute();
$result = $result -> fetchArray();
echo $result['key'] . "\n";
$stm -> close();
die();
}
?><html><body>
<h1>Query machines</h1>
<?php
$result = $db -> query('SELECT machines.name AS machine FROM machines');
while ($row = $result -> fetchArray()) {
echo '<a href="?machine=' . $row['machine'] . '">' . $row['machine'] . '</a><br>' . "\n";
}
?>
<h1>Global Tasks</h1>
<a href="?ddns">ddns</a><br>
<a href="?ddns_machines">initialize machines from ddns tokens</a><br>
<h1>Danger zone</h1>
<a href="?drop=DROP">drop</a><br>
</body></html>
|