All articles

A DynDNS service of my own [update]

After dyn.com all but discontinued its free DynamicDNS service, I can no longer reach my home network from outside. Two ways to change that.

After dyn.com all but discontinued its free DynamicDNS service, I currently cannot reach my home network from outside.

I see 2 ways to change that:

  1. Move to a different dynamic DNS provider, for example noip.com
  2. Build my own DynDNS

I went with option 2. That way I do not run the risk of having to switch providers again a few months from now. It is also fairly simple, since I already run a server that can be used for it.

Setting up a dynamic DNS breaks down into roughly these 4 steps:

  1. Pick a subdomain for dynamic DNS
  2. Adjust the DNS records of the main domain
  3. Configure your own server as a name server
  4. Write an update script for the DNS updates

1. Picking a subdomain

In principle any subdomain will do. You do need access to the DNS zone of the corresponding main domain, though. For the example in this guide I will work with the following assumptions:

Domain: domain.tld

Subdomain for DynamicDNS: dns.domain.tld

2. Adjusting the DNS records of the main domain

To be able to use the chosen subdomain – or further subdomains following the pattern host1.dns.domain.tld – as DynDNS addresses, we have to turn dns.domain.tld into a new DNS zone. In other words, we arrange for name resolution of that subdomain and everything below it to go through an additional name server. That job is then handled by our own server.

To do so we add the last two lines to the DNS zone of the main domain, which should look similar to this:

domain.tld        1800   IN  NS        dns1.provider.tld
domain.tld        1800   IN  NS        dns2.provider.tld
domain.tld       86400   IN  MX  10    mail.domain.tld
domain.tld       86400   IN  A         <IP>
mail.domain.tld  86400   IN  A         <IP>
*.domain.tld     86400   IN  A         <IP>
www.domain.tld   86400   IN  A         <IP>
dns.domain.tld    1800   IN  NS        ns1.domain.tld
dns.domain.tld    1800   IN  NS        ns2.domain.tld

This tells DNS clients to ask an additional name server for the IP mapping of the zone dns.domain.tld. We can then change those mappings as we please, because we have access to that server.

3. Making our own server the name server for the dns.domain.tld zone

The second-to-last step is to make the server behind domain.tld the name server for the DNS zone dns.domain.tld. My server is managed entirely with ISPConfig, so I could do this step through a wizard. However you produce it, the finished DNS zone entry looks like this:

dns.domain.tld      1800   IN  NS  ns1.domain.tld
dns.domain.tld      1800   IN  NS  ns2.domain.tld
dns.domain.tld        60   IN  A   <dynamic IP>
*.dns.domain.tld      60   IN  A   <dynamic IP>

The last two lines are the important ones. They assign an IP to the subdomains dns.domain.tld and *.dns.domain.tld. That IP is meant to match our dynamic IP and to be refreshed regularly by the script from step 4. For that refresh to take effect, it is important to pick as short a TTL (time to live) as possible – 60 seconds here. The TTL states when a client resolves the name dns.domain.tld again against a possibly changed IP, that is, how long it takes for the subdomain to point at a new IP.

4. Writing an update script for DNS updates

The final step is a script that takes the IP address to use and writes it into our DNS server. The script below works if the DNS server is managed by ISPConfig. With a different control panel or a DNS server addressed directly, it needs to be adapted accordingly.

[Update 11 Jan 2015] – zone update added, IDs are now read from the database.

<?php

require('soap_config.php');

$client = new SoapClient(null,
      array('location'   => $soap_location,
            'uri'        => $soap_uri,
            'trace'      => 1,
            'exceptions' => 1));

$con = mysqli_connect(<HOSTNAME>,<USER>,<PASS>,<DB>);

$url = $_REQUEST['domain'];

// Perform queries
$sql = "SELECT id FROM dns_rr WHERE name = '$url.' AND type ='A'";
$res = mysqli_fetch_row(mysqli_query($con,$sql));
$id = $res[0];

$sql = "SELECT id FROM dns_rr WHERE name = '*.$url.' AND type ='A'";
$res = mysqli_fetch_row(mysqli_query($con,$sql));
$wild_id = $res[0];

$sql = "SELECT id FROM dns_soa WHERE origin = '$url.'";
$res = mysqli_fetch_row(mysqli_query($con,$sql));
$zone_id = $res[0];

mysqli_close($con);

try {

        $session_id = $client->login($_REQUEST['user'],$_REQUEST['pass']);

        //* Get the dns record
        $dns_record1 = $client->dns_a_get($session_id, $id);
        $dns_record2 = $client->dns_a_get($session_id, $wild_id);

        //* Enter IP
        $dns_record1['data'] = $_REQUEST['ip'];
        $dns_record2['data'] = $_REQUEST['ip'];
        $dns_record1['serial'] = $dns_record1['serial']+1;
        $dns_record2['serial'] = $dns_record2['serial']+1;
        $client->dns_a_update($session_id, null, $id, $dns_record1);
        $client->dns_a_update($session_id, null, $wild_id, $dns_record2);

        $zone = $client->dns_zone_get($session_id, $zone_id);
        $zone['serial'] = $zone['serial'] + 1;
        $client->dns_zone_update($session_id, 0, $zone_id, $zone);

        $client->logout($session_id);

} catch (SoapFault $e) {
	echo $client->__getLastResponse();
	die('SOAP Error: '.$e->getMessage());
}

?>

Note: this script is essentially the example script that ISPConfig ships (/remoting_client/examples/dns_a_update.php). That is also where you find the required soap_config.php. The difference here is that the IDs of the A records and of the zone are read from the database.

The script is then called like this:

http://domain.tld/{PathToScript}/update.php?ip={IP}&user={USER}&pass={PASSWORD}&domain={DOMAIN}
  • IP: the IP the record should point to.
  • USER: a user permitted to change DNS records.
  • PASSWORD: that user’s password.
  • DOMAIN: the domain to update. The script reads the IDs of the DNS A record and of the zone from the ISPConfig database itself (tables dns_rr and dns_soa).

This makes the update script easy to configure in, say, a Fritz!Box router, which offers a “custom” DynDNS provider setting for exactly this purpose. The URL is then:

http://domain.tld/{PathToScript}/update.php?ip=<ipaddr>&user=<username>&pass=<password>&domain=<domain>