From 151c39e6aa8e9ec9d9cbe31bcca81ada71d9c72b Mon Sep 17 00:00:00 2001 From: Max Christian Date: Wed, 14 Sep 2016 11:33:40 +0200 Subject: quick and dirty dyndns service This script updates powerdns entries. It does not permission checks yet and is best described as a proof of concept . --- update.php | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 update.php diff --git a/update.php b/update.php new file mode 100644 index 0000000..a033d38 --- /dev/null +++ b/update.php @@ -0,0 +1,93 @@ +&ipv6=&domain=&user=&pass= + +function print_error() +{ + echo 'pg error'; +} + +// Connecting, selecting database +echo "content-type: text/html\n\n"; +# $conn = pg_connect('host=/var/run/postgresql user=username dbname=databasename'); +$dbconn = pg_connect('host=10.0.0.101 user=powerdns dbname=powerdns connect_timeout=5') + or die('Could not connect: ' . pg_last_error()); + +// if(!$dbconn) +// { +// echo "fehler\n"; +// die('Could not connect: ' . pg_last_error()); +// } +echo "gut\n"; + +// Performing SQL query + +$ip = "127.0.0.1"; +if(isset($_GET['ip'])) +{ $ip = $_GET['ip']; } + +$ipv6 = "::0"; +if(isset($_GET['ipv6'])) +{ $ipv6 = $_GET['ipv6']; } + + +if(! preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $ip) ) +{ die("illegal ip"); } + + +$user = ''; +if(isset($_GET['user'])) +{ $user = $_GET['user']; } + +if(! preg_match('/[a-z]+/', $user) ) +{ die("illegal user"); } + + + +$stmt = pg_prepare($dbconn, "updatesoa", "update records set content='ns1.first-ns.de postmaster.robot.first-ns.de ".date("U")." 600 300 1800 300' where id=6;") + or die('Could not connect: ' . pg_last_error()); +$result = pg_execute($dbconn, "updatesoa", array()); + +if(pg_affected_rows($result) != 1) +{ + die("unable to update soa, result: ".pg_affected_rows($result)); +} + + +#$query = "UPDATE records SET content='$ip' WHERE type='A' AND name='wire-me.de'; "; +#echo 'gut'; +# $result = pg_query($query) or die('Query failed: ' . pg_last_error()); +$stmt = pg_prepare($dbconn, "updatedns", "UPDATE records SET content=$1 WHERE type=$2 AND name=$3;") + or die('Could not connect: ' . pg_last_error()); +$result = pg_execute($dbconn, "updatedns", array($ip, "A", $user.".wire-me.de")); +$result = pg_execute($dbconn, "updatedns", array($ipv6, "AAAA", $user.".wire-me.de")); + +if(pg_affected_rows($result) != 1) +{ + $fh = fopen("error.log", "a+"); + fwrite($fh, "IP: $ip; user: $user;\n"); + fclose($fh); + die("unable to update user"); +} + + + +echo '
rows: "'.pg_affected_rows($result).'"'; +echo '
result: "'.$result.'"'; +echo '
error: "'.pg_result_error($result).'"'; +// Printing results in HTML +// echo "\n"; +// while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) { +// echo "\t\n"; +// foreach ($line as $col_value) { +// echo "\t\t\n"; +// } +// echo "\t\n"; +// } +// echo "
$col_value
\n"; + +// Free resultset +pg_free_result($result); + +// Closing connection +pg_close($dbconn); +?> -- cgit v1.2.3