2 header("Content-type: text/plain");
4 define("ZONENAME", "your-zonename");
5 define("KEYNAME", "your/dns/update.key");
6 $appIdentifier = "someca";
8 if(!isset($_GET['token']) || !isset($_GET['t1']) || !isset($_GET['t2']) || !isset($_GET['action'])){
11 $link = mysqli_connect("localhost", "db-user", "db-pw", "db");
12 if($_GET['token'] != "your-token-here"){
17 if(!preg_match("/^[a-zA-Z0-9]+$/", $t1) || !preg_match("/^[a-zA-Z0-9]+$/", $t2)){
23 $stmt = $link->prepare("INSERT INTO tokens SET type=?, name=?");
24 $stmt->bind_param("ss", $type, $name);
25 $type=$_GET['action'];
27 if($_GET['action'] == "http"){
30 file_put_contents(".well-known/$appIdentifier-challenge/$t1.txt", $t2);
31 } else if($_GET['action'] == "dns") {
34 $dnscalls .= "update delete {$t1}._$appIdentifier._auth." . ZONENAME . " TXT\n"
35 ."update add {$t1}._$appIdentifier._auth." . ZONENAME . " 60 TXT {$t2}\n";
38 $stmt = $link->prepare("SELECT type, name FROM tokens WHERE created + 60000 < CURRENT_TIMESTAMP;");
41 /* bind result variables */
42 $stmt->bind_result($type, $name);
46 while($stmt->fetch()){
48 unlink(".well-known/$appIdentifier-challenge/{$name}.txt");
49 } else if($type == "dns") {
50 $dnscalls .= "update delete {$name}._$appIdentifier._auth." . ZONENAME . " TXT\n";
52 $todelete[] = array("type"=>$type, "name"=>$name);
55 $stmtd = $link->prepare("DELETE FROM tokens WHERE type=? AND name=?");
56 $stmtd->bind_param("ss", $type, $name);
58 foreach($todelete as $val){
68 function dnsAction($command) {
69 $call = "server localhost\n$command\nsend\nquit\n";
71 $nsupdate = popen("/usr/bin/nsupdate -k " . KEYNAME, 'w');
72 fwrite($nsupdate, $call);
73 $retval = pclose($nsupdate); // nsupdate doesn't return anything useful when called this way