summaryrefslogtreecommitdiff
path: root/ddns-status.php
blob: 6ab6c4bf36f06732b95bb4c8bb841b60038308eb (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
<?php

function die_http($error, $message, $extra_message) {
  header('Status: ' . $error . ' ' . $message);
  print 'Error ' . $error . ': ' . $message . "\n";
  if ($extra_message != '')
    print "<br>\n" . $extra_message . "\n";
  die();
}

$pin = popen('dig @127.0.0.1 ddns.eckner.net AXFR | sort -k4,4', 'r');

if (!$pin)
  die_http(500, 'Internal Server Error', 'Failed to get Zone.');

$found = false;
$axfr = array();
while (($line = fgets($pin)) !== false) {
  $line = trim($line);
  if (substr($line, 0, 1) == ';' || $line == '')
    continue;

  $line = preg_split("/[\s]+/", $line);

  if ($line[0] == 'pool32.ddns.eckner.net.')
    continue;

  if ($line[2] != 'IN')
    continue;

  if ($line[3] == 'A')
    $axfr[$line[0]][$line[3]] = '4';
  if ($line[3] == 'AAAA')
    $axfr[$line[0]][$line[3]] = '6';
  if ($_SERVER['REMOTE_ADDR'] == $line[4])
    $found = true;
}
pclose($pin);

if (! $found)
  die_http(403, 'Forbidden', 'You are not known to ddns.');

$hosts = explode(
  "\n",
  trim(
    shell_exec(
      'cut -d" " -f2 < /srv/http/vhosts/eckner.net/ddns/tokens'
    )
  )
);

$filters = explode( 
  "\n",
  shell_exec(
    'cut -d" " -f1 < /srv/http/vhosts/eckner.net/ddns/ip-filters'
  ) . "v4\nv6"
);

sort($filters);
sort($hosts);

function print_existence($prefix, $host) {
  global $axfr;
  if (!array_key_exists($host . '.ddns.eckner.net.', $axfr)) {
    print $prefix . "<font color=\"ff0000\">\n";
    print $prefix . "  N&ouml;\n";
    print $prefix . "</font>\n";
    return;
  }
  print $prefix . "<font color=\"00ff00\">\n";
  print $prefix . "  IPv" . implode('&amp;', $axfr[$host . '.ddns.eckner.net.']) . "\n";
  print $prefix . "</font>\n";
}

print "<html>\n";
print "  <head>\n";
print "    <title>\n";
print "      ddns status\n";
print "    </title>\n";
print "  </head>\n";
print "  <body>\n";
print "    <table>\n";

print "      <tr>\n";
print "        <th>\n";
print "          Host\n";
print "        </th>\n";
print "        <th>\n";
print "          &nbsp;\n";
print "        </th>\n";
foreach ($filters as $filter) {
  print "        <th>\n";
  print "          " . $filter . "\n";
  print "        </th>\n";
}
print "      </tr>\n";

foreach ($hosts as $host) {
  print "      <tr>\n";
  print "        <td>\n";
  print "          " . $host . "\n";
  print "        </td>\n";
  print "        <td>\n";
  print_existence('          ', $host);
  print "        </td>\n";
  foreach ($filters as $filter) {
    print "        <td>\n";
    print_existence('          ', $host . '.' . $filter);
    print "        </td>\n";
  }
  print "      </tr>\n";
}

print "    </table>\n";
print "  </body>\n";
print "</html>";