summaryrefslogtreecommitdiff
path: root/html/maintenance.php
blob: 9dd58063f499fb7bddacca9f8dfe07bdfe558ae0 (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
<?php

if ($_SERVER['REMOTE_ADDR'] !== $_SERVER['SERVER_ADDR']) {
  die();
}

$db = new SQLite3('/srv/http/vhosts/eckner.net/colocation.eckner.net/backend/sqlite.db');

$states = array('running', 'ping', 'power');

if (array_key_exists('drop',$_GET) && ($_GET['drop']=='DROP')) {
  $db -> exec('DROP TABLE "machines"');
  $db -> exec('DROP TABLE "keys"');
  $db -> exec('DROP TABLE "permissions"');
  $db -> exec('DROP TABLE "states"');
  $db -> exec('DROP TABLE "values"');
  $db -> exec('CREATE TABLE "machines" (id integer primary key AUTOINCREMENT, name varchar(32) not null unique, ' . implode(' int not null default 0, ', $states) . ' int not null default 0, 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, machine_id integer not null, state_id integer not null)');
  $db -> exec('CREATE TABLE "values" (machine_id integer not null, state_id integer not null, value integer)');
  $stm = $db -> prepare('INSERT INTO states (name) VALUES (:state)');
  foreach ($states as $state) {
    $stm -> bindValue('state', $state);
    $stm -> execute();
  }
  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();

    $stm = $db -> prepare('INSERT INTO keys (key) VALUES (:key)');
    $stm -> bindValue('key', bin2hex(random_bytes(64)));
    $stm -> execute();
    $key_id = $db -> lastInsertRowID();
    $stm -> close();

    $stm = $db -> prepare(
      'INSERT INTO permissions (key_id, machine_id, state_id) VALUES (:key,:machine,1)');
    $stm -> bindValue('machine', $machine_id);
    $stm -> bindValue('key', $key_id);
    $stm -> execute();
    $stm -> close();

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

  $stm = $db -> prepare(
    'SELECT states.name, keys.key FROM machines' .
    ' JOIN permissions ON permissions.machine_id = machines.id' .
    ' JOIN states ON permissions.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();
}