summaryrefslogtreecommitdiff
path: root/html/maintenance.php
blob: a328ef437b2437714612e251a248a011fa97643b (plain)
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
<?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);');
  $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)) {
  $stm = $db -> prepare('SELECT COUNT(1) FROM machines WHERE machines.name=:name');
  $stm -> bindValue('name', $_GET['machine']);
  $result = $stm -> execute();
  $result = $result -> fetchArray();
  $stm -> close();

  if ($result[0] == 0) {
    $key = bin2hex(random_bytes(64));
    $stm = $db -> prepare('INSERT INTO machines (name) VALUES (:machine)');
    $stm -> bindValue('machine', $_GET['machine']);
    $stm -> execute();
    $machine_id = $db -> lastInsertRowID();
    $stm -> close();

    $key_id = $db -> add_key();

    $stm = $db -> prepare(
      'INSERT INTO "values" (machine_id, state_id) VALUES (:machine, :state)'
    );
    $stm -> bindValue('machine', $machine_id);
    $result = $db -> query(
      'SELECT states.name, states.id FROM states'
    );
    while ($row = $result -> fetchArray()) {
      if ($row['name'] == 'running')
        $running_id = $row['id'];
      $stm -> bindValue('state', $row['id']);
      $stm -> execute();
    }
    $stm -> close();

    $stm = $db -> prepare(
      'INSERT INTO permissions (key_id, value_id) VALUES (:key,:value)');
    $stm -> bindValue('value', $running_id);
    $stm -> bindValue('key', $key_id);
    $stm -> execute();
    $stm -> close();
  }

  $stm = $db -> prepare(
    'SELECT states.name, keys.key 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'] . '"' . "\n";
  }
  die();
}

if (array_key_exists('ddns', $_GET)) {
  $result = $db -> query(
    'SELECT keys.key, COUNT(1) as cnt FROM keys' .
    ' JOIN permissions ON permissions.key_id = keys.id' .
    ' JOIN "values" ON permissions.value_id = "values".id' .
    ' JOIN states ON "values".state_id = states.id' .
    ' WHERE states.name = ' . "'" . 'running' . "'" .
    ' GROUP BY keys.key' .
    ' ORDER BY cnt DESC'
  );
  while ($row = $result -> fetchArray()) {
    if ($row['cnt'] != 1) {
      echo $row['key'] . "\n";
      die();
    }
  }

  $key_id = $db -> add_key();

  $stm = $db -> prepare('INSERT INTO permissions (key_id, value_id) VALUES (:key_id, :value_id)');
  $stm -> bindValue('key_id', $key_id);
  $result = $db -> query(
    'SELECT "values".id as value_id FROM "values"' .
    ' JOIN states ON states.id = "values".state_id' .
    ' WHERE states.name = ' . "'" . 'running' . "'"
  );
  while ($row = $result -> fetchArray()) {
    $stm -> bindValue('value_id', $row['value_id']);
    $stm -> execute();
  }
  $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>
<h1>Danger zone</h1>
<a href="?drop=DROP">drop</a><br>
</body></html>