<?php
header("Content-type: text/plain");
-define("ZONENAME", "cacert.dyn.dogcraft.de");
-define("KEYNAME", "keys/Kcacert.dyn.dogcraft.de.+165+54687.key");
+define("ZONENAME", "your-zonename");
+define("KEYNAME", "your/dns/update.key");
+$appIdentifier = "someca";
if(!isset($_GET['token']) || !isset($_GET['t1']) || !isset($_GET['t2']) || !isset($_GET['action'])){
die("Error");
}
-if($_GET['token'] != "rD1m3A9ew6Hs4DIv7lnTxNbR6dr"){
+$link = mysqli_connect("localhost", "db-user", "db-pw", "db");
+if($_GET['token'] != "your-token-here"){
die ();
}
$t1 = $_GET['t1'];
if(!preg_match("/^[a-zA-Z0-9]+$/", $t1) || !preg_match("/^[a-zA-Z0-9]+$/", $t2)){
die("Error");
}
-$todelete = array();
-if(file_exists("data.php")){
- include ("data.php");
-}
+$dnscalls = "";
+if($t1!="purge"){
+ $stmt = $link->prepare("INSERT INTO tokens SET type=?, name=?");
+ $stmt->bind_param("ss", $type, $name);
+ $type=$_GET['action'];
+ $name = $t1;
+ if($_GET['action'] == "http"){
+ $stmt->execute();
-$time = time()/60;
-if(!isset($todelete[$time])){
- $todelete[$time] = array();
+ file_put_contents(".well-known/$appIdentifier-challenge/$t1.txt", $t2);
+ } else if($_GET['action'] == "dns") {
+ $stmt->execute();
+
+ $dnscalls .= "update delete {$t1}._$appIdentifier._auth." . ZONENAME . " TXT\n"
+ ."update add {$t1}._$appIdentifier._auth." . ZONENAME . " 60 TXT {$t2}\n";
+ }
}
+$stmt = $link->prepare("SELECT type, name FROM tokens WHERE created + 60000 < CURRENT_TIMESTAMP;");
+$stmt->execute();
-$dnscalls = "";
+/* bind result variables */
+$stmt->bind_result($type, $name);
+$todelete = array();
-if($_GET['action'] == "http"){
- $todelete[$time][] = array("http", $t1);
- file_put_contents("cacert-$t1.txt", $t2);
-} else if($_GET['action'] == "dns") {
- $todelete[$time][] = array("dns", $t1);
- $dnscalls .= "update delete cacert-{$t1}." . ZONENAME . " TXT\n"
- ."update add cacert-{$t1}." . ZONENAME . " 60 TXT {$t2}\n";
-}
-$copy = $todelete;
-foreach($copy as $nt => $ar){
- if($nt < $time - 2){
- unset($todelete[$nt]);
- foreach($ar as $act){
- if($act[0] == "http"){
- unlink("cacert-{$act[1]}.txt");
- } else if($act[0] == "dns") {
- $dnscalls .= "update delete cacert-{$act[1]}." . ZONENAME . " TXT\n";
- }
- }
+/* fetch value */
+while($stmt->fetch()){
+ if($type == "http"){
+ unlink(".well-known/$appIdentifier-challenge/{$name}.txt");
+ } else if($type == "dns") {
+ $dnscalls .= "update delete {$name}._$appIdentifier._auth." . ZONENAME . " TXT\n";
}
+ $todelete[] = array("type"=>$type, "name"=>$name);
+}
+
+$stmtd = $link->prepare("DELETE FROM tokens WHERE type=? AND name=?");
+$stmtd->bind_param("ss", $type, $name);
+
+foreach($todelete as $val){
+ $type = $val["type"];
+ $name = $val["name"];
+ $stmtd->execute();
}
-file_put_contents("data.php", "<?php \$todelete = ".var_export($todelete,true).";\n?>");
if($dnscalls != ""){
dnsAction($dnscalls);
fwrite($nsupdate, $call);
$retval = pclose($nsupdate); // nsupdate doesn't return anything useful when called this way
}
-