I wanted to integrate SQL support direct into dhcpd for various reasons, so I wrote a patch. This integrates ISC dhcpd 4.2.0 with libdbi, and thus into MySQL, postgres, and any other SQL database supported by libdbi. This allows dhcpd to dynamically reference an SQL database based on hardware address.

Note that in general doing SQL lookups from dhcpd is a bad idea. The sort of environment where this is useful is the sort of environment where the LDAP patch is useful, where there is no LDAP around but there is an SQL database. An arbitrary SQL query can be specified, which can return anything that can be put in a ‘host {...}‘ block.

The patch, against dhcpd 4.2.0 release, can be found here (that link was updated on 2 Dec 2010 – the old version is here).

The patch was heavily inspired by the LDAP patch – thank you ISC, Ntelos Inc, Brian Masney & David Cantrell.

So far the patch has had very light testing indeed. Do not use it in a production environment. It should (unlike, I think, the LDAP patch) work fine with IPv6, though I’ve only tested it on IPv4.

To compile:

  • Apply the patch here to the unpacked ISC dhcp source tree.
  • Regenerate the configure script (requires GNU autoconf and automake):
  • Run ‘./configure‘ with the ‘--with-dbi‘ argument to enable DBI.
  • Run ‘make‘ to build ISC dhcp.

Documentation is in the patch, but I have provided an outline below.

My main purpose in posting this here (and similarly to dhcp-users) is to see whether it is useful to anyone other than me. I don’t know much about the innards of dhcpd so I have probably mucked up the memory management (though this was in general taken from ldap.c).

This file provides a generalised interface to libdbi. The following configuration elements can be set (all are strings):

The file allows dynamic configuration of dhcp parameters looked up by hardware address. The user can specify a statement (likely to be SELECT in an SQL environment) which returns data providing the dhcp paramaters
associated with that particular hardware address.

The query is the SELECT statement passed to the SQL backend, into which the following are substituted:

The query does not need a trailing semicolon. Be careful that quotes in the query do not interfere with quotes in the config file.

Columns returned are processed in order, with column names corresponding to entries in the file. The values in the first row are appended after a ” ” and terminated with a “;”.

For example, if the table ‘dhcp’ contained columns haddr, ipv4, droute corresponding to hardware address, IPv4 address and default routes for IPv4, the following query might be used:

If ipv4 were returned as 192.200.0.15, and droute as 192.200.0.1, this
would generate a host block equivalent to:

The following column names have special significance:

An example of the use of entry on MySQL is as follows, and has the same effect as the above example:

This allows for more flexible (if less legible) queries, and avoids problems with databases which are unhappy returning column names containing non-alphanumeric characters.

7 responses to “Adding SQL support to ISC dhcpd”

  1. Richard Pijnenburg Avatar
    Richard Pijnenburg

    Hi,

    at this moment i’m using the ldap patch for dhcp and because of this i’m looking to your patch for sql support.
    Is it still needed to configure shared networks in it ? ( i would assume so but not sure )
    Main thing is that i need to support multiple networks preferably fully dynamic.
    Certain settings will be general like DNS servers, others will be depending on the network ( like the gateway IP ) and others depending on the host it self ( the ip-address and next-server + filename )
    Can this be achieved with your patch?

    Thanks for your help.

    Cheers.

    1. Hi Richard,

      Unlike the LDAP code, the SQL/DBI code is only triggered when looking up hosts, so you are effectively generating a host block. So what you can put in is limited to what you would put in a host block (indeed using the “entry” format, you can put anything in there that you could put in a host block). Given you can write your own query, you can return anything you like to go in that host block, and that could (for instance) be calculated. Currently I’m not passing more than the hardware address and client identifier in through % variables, so you if you wanted to support the same host on different networks, you would have to do that using the config file (as SQL would have no way of determining which network the MAC address is on). My understanding is that this is possible (though I haven’t tried it).

      Things that don’t live in a host block need to come from somewhere else, and that would normally be the config file. I see no reason why one couldn’t retrieve some things from LDAP and some from SQL/DBI though that would be a bit perverse.

      Alex

  2. Hi Alex. Thanks for your work. Just want to know the functionality of your patch before using it. Is your intention to write the dhcp lease information (log info) into database or read (access) some information from database? If the latter one, what kind of information do you access? Thank you very much and look forward to your reply.

  3. Hi Jane,

    The patch does no more than the LDAP patch which is now in DHCP mainline, i.e. it allows the addresses to be defined in the database. It does not write to the databases (not the leases file, and not anything else). Essentially, when an unknown MAC address is encountered, it will query the SQL database, and what is returned will be put into a host block. There are some examples in the body of the article on how this works.

    Alex

  4. Hi Alex. Thank you very much and I got your idea. Great work!

  5. Hi Alex,
    Thanks for your work wich save my life !
    You should ask to the isc dhcp team to integrate it…

  6. Hi Alex,
    Thanks to your work ,I could find an effective way to connect the PostgreSQL and DHCP. I’ve worked in a university of China and I’m always thinking about how to virtual the students’ lessons through some ways .And after I’ve read your article I think I’ve got a method to deal with it .
    I’m appreciate if you can give us more help.Thanks a lot.

Leave a Reply to Richard PijnenburgCancel reply

Discover more from Alex Bligh's blog

Subscribe now to keep reading and get access to the full archive.

Continue reading