summaryrefslogtreecommitdiff
path: root/html/maintenance.php
blob: 6d36eb0e802c9659ed7a1a59a2a63ac144b2076e (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
<?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();

    $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 "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 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();
}