Simple dynamic dns with tinydns + php

Tagged:  

I've been using djbdns for quite a while now to host my domains. One of the nice things about it is the simple configuration file.

I've written a small php script to handle dynamic dns entries for my connection at home. It just reads a template data file determined by the name you give the php script in GET vars, replaces a token with the requested or current ip and calls make to update the data.

I had to modify the Makefile for tinydns's root direcotry to concatenate all the finished data files before calling tinydns-data. Of course your webserver needs to be able to write to the tindyns data directory.

put this somewhere on your webserver

<?php
//this should really be in an external file and hashed
$password_for['yourname'] = 'secretpassword';

$tinydns_data_path = '/etc/tinydns/root';

$name = $_GET['name'];
$address = ip2long($_GET['address']);

if ( ! $address ) {
        $address = ip2long($_SERVER['REMOTE_ADDR']);

}

$dotted_quad = long2ip($address);

//check that name/password match
if ( !($name && $password_for[$name] === $_GET['password']) ) {

        print 'incorrect password';
        exit;

}

$template = file("$tinydns_data_path/$name.tpl");

foreach ($template as $line) {

        $data[] = str_replace('%IP%',$dotted_quad,$line);

}

chdir($tinydns_data_path);

file_put_contents("$name.data", implode(,$data) );

system('make');

change Makefile in your tinydns data directory to this:

data.cdb: *.data

        cat *.data > data
        /usr/local/bin/tinydns-data

create a template file for each username you want to use. call it yourname.tpl. %IP% gets replaced with your real IP address.

=home.somedomain.net:%IP%:600

+ gameserver.somedomain.net:%IP%:600

mv your original static data file to a file named something like static.data

now just call

wget -O /dev/null http://whatever/whatever.php?name=yourname\&password=secretpassword

from your home connection whenever you get a new ip. You can also force a specific address with the address GET variable.

easy.

other solutions

Here are some other ways that people are doing dynamic dns with djbdns. I haven't looked at these extensively.

Hey, great post on the subject, thx!