Home > DNS, Linux, Networking > Creating a dynamic DNS service using djbdns

Creating a dynamic DNS service using djbdns

December 7th, 2011 Leave a comment Print Print Go to comments

Before some days I decided to create a dynamic DNS service for a domain I own. There are many dynamic DNS services, e.g. dyndns and FreeDNS, but none gives you the power I wanted. For example I needed custom records such as SSHFP but most services don’t allow you this type of entries. Also, many of them aren’t free, especially when you transfer your own domain.

So I went on to make my own service. I knew from the beginning that BIND wasn’t what I wanted. I wanted something small, fast and secure. So I chose djbdns. Unfortunately, the IPv6 support isn’t that good so I used dbndns, which is a djbdns with some patches, especially for IPv6. (Yes, the title says djbdns, but it’s the same, if you don’t like the debian patches I can tell you how to transform a line to make it djbdns compatible!)

The first part was to design the interface. I could make a fancy web page with buttons and nice images. However, since most of the users would like a simple command that they could insert at their crontab to update their IP, I decided to just get a number of parameters and return a status string. Much simpler, more functional and I get to  spend more time coding the core of the server!

And then it’s the functionalities. Which would be the supported entries? IPv4 and IPv6 are ok for the beginning. But would the user be able to choose whatever IP he wanted? What about the hostname? Most users want a dynamic DNS so they can connect to their home box using ssh. As I said at the first paragraph, SSHFP records should be supported. But other records such as TXT and SRV? That’s why I used privileges. Every user has a privilege, and the higher the privilege the more powers you have. For the moment the following are implemented:

  • Level 1: Simple user. You are allowed one hostname and the IP is the IP you had when you made your request.
  • Level 2: Simple user with IP selection. You are allowed one hostname but you have the option to select your IP. If you don’t supply one, the IP that made the request is used.
  • Level 3: Simple user with IP and hostname selection. You are allowed to also change your hostname. If you don’t supply one the default will be used. Also, this allows you to have multiple hostnames at the same time.
  • Level 10: Administrator. Higher level, you can add, change and remove users.

All levels have IPv4, IPv6 and SSHFP support. Other privilege levels (4 … 9) are reserved for future improvements.

What about privacy? It was very important for me that all entries would ‘disappear’ after some time. The user should leave no traces. The problem is that the entry should be updated many times and not just the first time you change your IP but that’s the price for privacy! And this is something that you don’t find at many dynamic DNS services out there.

So, we’ve got many users, with different usernames, passwords and privileges, different hostnames and IPs, A, AAAA and SSHFP records, expiration times and many many more. What’s missing? SQLite! I was already running a mysql server, but I just wanted something simple. And at the end I found out that it helped me a lot with the DNS data file update.

I ended up writing a simple PHP program that would take a number of input variables:

  • username: User’s username
  • password: User’s password
  • action: The action the user wants to commit

Valid actions with their arguments are:

  • update4: Update IPv4 address
    • ip: The new IP address (if you’re allowed to set it)
    • hostname: The hostname (if you’re allowed to set it)
  • update6: Update IPv6 address
    • ip: The new IP address (if you’re allowed to set it)
    • hostname: The hostname (if you’re allowed to set it)
  • list: List all IPv4, IPv6 and SSHFP entries.
  • delete4: Delete an IPv4 dns entry immediately (before it expires by itself)
    • hostname: The hostname (if you’re allowed to set it)
  • delete6: Delete an IPv6 dns entry immediately (before it expires by itself)
    • hostname: The hostname (if you’re allowed to set it)
  • setsshfp: Add an SSHFP record
    • hostname: The hostname (if you’re allowed to set it)
    • key: The key as you can find it at /etc/ssh/ssh_host_rsa_key.pub and /etc/ssh/ssh_host_dsa_key.pub. Warning! If there are any ‘+’ in the key (very probable since it’s base64 encoded) you must escape them!
  • delsshfp: Delete an SSHFP record
    • hostname: The hostname (if you’re allowed to set it)
  • changepw: Change password
    • newpw: The new password
  • adduser: Add a new user (if you’re allowed to add one)
    • newuser: The new user’s username
    • newpw: The new user’s password
    • newpriv: The new user’s privilege level
    • newhostname: The new user’s default hostname
  • deluser: Delete a user (if you’re allowed to delete one)
    • deluser: The user to delete
  • edituser: Edit a user (if you’re allowed to edit one)
    • Any of the following options can be used
    • newpw: User’s new password
    • newpriv: User’s new privilege level
    • newhostname: User’s new default hostname
  • cleanup: Cleanup the database.
TIP: Use curl with the -F option, at least for the setsshfp (curl -F ‘</etc/ssh/ssh_host_rsa_key.pub’ …).

The server is almost finished! What’s missing is inserting all these into the data file for the dns server. I decided that I didn’t want to code a c/perl program and I would write a simple shellscript. Since the djbdns data file consists of values seperated by the ‘:’ character, with  a few sql commands to sqlite3 and the -separator ‘:’ argument the updated datafile is ready. But when should the file be regenerated? I thought of a cronjob but I really didn’t like the idea of waiting. And this is where the SQLite database idea came handy! Using inotifywait from the inotify-tools I wait for close events on the database and then I update the djbdns data file. I use the daemontools to run this shellscript and till now everything seems to work great!

You can download everything here:

  • The PHP file that contains the daemon. At the top you can see two configuration options, the location where the database file can be found and the duration of each entry. The file has a txt extension, of course you’ll have to rename it. Also remember that if you store your database under the document root you should deny access from the web!!!
  • An SQL dump file that your can use to generate a sample database. A user with username and password ‘admin’ has been created, remember to change the password or create a new admin and delete the old one!
  • The shellscript that regenerates the data file.
  • The run file for daemontools. It can be merged with the previous, but you may want to generate the datafile using another way, so I created a different file.
Remember that this is just a simple dynamic DNS service designed for a small number of users!
Please leave feedback and suggestions!
Categories: DNS, Linux, Networking Tags:
  1. nflix
    August 21st, 2013 at 00:08 | #1

    hi, nice tutorial, how can i set the sql dump? have i to import it to phpmyadmin? thanks

    • Fotis
      September 2nd, 2013 at 13:41 | #2

      An SQLite database is used, so you must use the sqlite3 tool. sqlite3 updns.db < createdb.sql and you’re done.

  2. October 2nd, 2014 at 15:35 | #3

    Hi colleagues, its great piece of writing concerning tutoringand fully explained, keep it up all the time.

  3. October 4th, 2014 at 00:56 | #4

    Thanks for finally writing about >Creating a dynamic DNS service usiing djbdns | Fotis’ Blog <Liked it!

  4. October 8th, 2014 at 10:38 | #5

    Awesome things here. I am very glad to look your article.
    Thank you so much and I am taking a look ahead to contact you.
    Will you please drop me a mail?

  5. October 9th, 2014 at 11:34 | #6

    So marriage or wedding is a very good and attractive occasion of ones life and so want to design and plan every moment of it.
    When styling your hair, remember salon, returning to for
    from it also classy, rollers based with bobby pinastre.
    They realized that it is not just about standing in one corner of the hall and
    taking random shots.

  1. No trackbacks yet.

This site is using OpenAvatar based on

SEO Powered by Platinum SEO from Techblissonline
%d bloggers like this: