<?xml version="1.0"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
     "dtd/xml/4.1.2/docbookx.dtd"[
<!ENTITY % afnic_custom SYSTEM "../../lib/afnic-docbook.inc">
%afnic_custom;
]>
<!-- $Id: xml-registry.db,v 1.7 2004-03-14 20:59:18 bortzmeyer Exp $ -->
<article lang="en" id="index" xmlns:xi="http://www.w3.org/2001/XInclude">
  <articleinfo>
    <author>
      <firstname>Stephane</firstname>
      <surname>Bortzmeyer</surname>
      <affiliation>
        <orgname>AFNIC</orgname>
        <address>
          <street>Immeuble International</street>
          <postcode>78181</postcode>
          <city>Saint-Quentin-en-Yvelines</city>
          <country>France</country>
          <email>bortzmeyer@nic.fr</email>
        </address>
      </affiliation>
    </author>
    <pubdate>$Date: 2004-03-14 20:59:18 $</pubdate>
    <copyright>
      <year>2003</year>
      <holder>AFNIC</holder>
    </copyright>
    <abstract><para>This document describes how to set up a
    <emphasis>small</emphasis> DNS registry using a XML file as the
    database (both social and technical) and XSLT to produce the zone file.</para>
    </abstract>
    <title>Setting up a DNS registry with XML and XSL</title>
  </articleinfo>
  <section id="general">
    <title>General view</title>
<para>Every DNS registry will require a database. For large
      registries, a real DBMS (DataBase Management System) is
      mandatory. But for small registries, it can be overkill. These
      small registries (for instance ccTLDs in developing countries)
      typically use adhoc techniques that have serious drawbacks:
      <itemizedlist>
	<listitem><para>Some use only the zone file as a
	database. Social information is made of comments in that zone
	file. It prevents any whois-like service to be created. And it
	does not allow automatic processing of the data (for instance,
	finding all the domains hold by a given entity).</para>
	</listitem>
	<listitem>
	  <para>Some use office software like MS-Excel. Since these
	programs have very poor groupware capabilities, it makes
	difficult for several people to work on the database.</para>
	</listitem>
      </itemizedlist></para>
<para>The solution we suggest is to use a XML file for the database:
      <orderedlist>
	<listitem><para>Since it is a text file, groupware tools like
	CVS will be usable to allow group work.</para>
	</listitem>
	<listitem><para>XML is an open format so you do not depend on
	Microsoft for your data.</para>
	</listitem>
	<listitem>
	  <para>XML is suitable for automatic processing: we will mostly use
	    XSLT but many other tools are possible.</para>
	</listitem>
      </orderedlist></para>
    <warning><para>This document has been throughly checked but the
    ideas presented here have never been actually deployed with a real registry.</para>
    </warning>
</section>
  <section id="xml">
    <title>Brief reminder about XML</title>
<para>In French, you can see the <ulink url="http://www.generic-nic.net/formation/xml/">AFNIC training</ulink>.</para>
    <section id="xml-schemas">
      <title>Writing schemas</title>
<para>One of the funny things about XML is that you can use several
	different programming languages to write the schema. You have:
	<orderedlist>
	  <listitem>
<para>DTD (Document Type Definition), a legacy from SGML, widely
	      implemented and recognized. The simplest solution.</para>	  </listitem>
	  <listitem>
	    <para>XML Schemas, a very verbose language. <footnote><para>"Schemas" has two
	      meanings, the generic one, which include DTD, for
	      instance and the specific one used in this paragraph.</para>
	      </footnote></para></listitem>
	  <listitem>
	    <para>Relax, the least common</para>
	  </listitem>
	</orderedlist></para>
</section>
  </section>
  <section>
    <title>The schema</title>
<para>When you set up a registry, you have to decide on a
      schema. Wether it is in SQL or written for XML, this is a policy
      choice. Here, we will skip the political part and I propose the
      following schema, written using the <link
	linkend="xml-schemas">DTD language</link>.</para>
    <programlisting>
<xi:include href="schema.dtd" parse="text"/>
</programlisting>
  </section>
<section><title>The data</title>
    <para>With such a schema, you can write a zone file.</para>
    <programlisting>
<xi:include href="example-zone.xml" parse="text"/>
</programlisting>
<para>The zone is a simple text file. You can use any editor you want
      (although I would recommand Emacs with its psgml package, which
      simplifies XML edition a lot).</para>
  </section>
<section><title>The processing</title>
<para>To create a zone file suitable for your nameserver (we will use
      the syntax of BIND and nsd), a XSL stylesheet can be as simple
      as:</para>
   <programlisting>
<xi:include href="zone2bind.xslt" parse="text"/>
</programlisting>
<para>You can apply this stylesheet and automatically get the zone
      file in the BIND format. Here is an example with
      <application>libxslt</application>:
<programlisting>
<prompt>% </prompt><command>xsltproc -o <replaceable>db.tld</replaceable> zone2bind.xslt <replaceable>example-zone.xml</replaceable></command>
      </programlisting>
</para>
<para>But XML allows you much more. Suppose we want to get the information about a contact. we will use the 
      <application>xpath</application> tool, part of Perl XML::XPath module<footnote><para>Such a tool is trivial to write with any Xpath library: I have a similar program in Python</para>
      </footnote>. <application>xpath</application> allows us to use Xpath queries against our data:</para>
<programlisting>
<prompt>% </prompt><command>xpath -e <replaceable>'//contact[@handle="SB1"]'</replaceable> ./example-zone.xml  </command>     
Found 1 nodes in ./example-zone.xml:
-- NODE --
<![CDATA[
<contact handle="SB1">
    <name>Bortzmeyer</name>
    <address>Abbey Road</address>
    <city>London</city>
    <country>uk</country>
    <phone>1234</phone>
    <email>bortzmeyer@hotmail.com</email>
  </contact>
]]>
<prompt>% </prompt><command>xpath -e <replaceable>'//contact[city="Saint-Quentin"]'</replaceable> ./example-zone.xml </command>
Found 1 nodes in ./example-zone.xml:
-- NODE --
<![CDATA[
<contact handle="PL1">
    <name>Lubrano</name>
    <address>Immeuble International</address>
    <city>Saint-Quentin</city>
    <country>fr</country>
    <phone>1234</phone>
    <email>lulu@nic.fr</email>
  </contact>
]]>
    </programlisting>
  </section>
  <section>
    <title>The whois server</title>
    <para>The whois protocol is defined in <rfc num="954"/>. You use
    it to find out social information about domain registrants, or
    contacts (technical or administrative). An example of use, with
    the <application>whois</application> program:
<programlisting>
<prompt>% </prompt><command>whois -h whois.example.tld linux.tld</command>
All rights reserved.
Copyright TLD-NIC

Domain: linux.tld

Registrant name: Torvalds
...
      </programlisting></para>
    <para>We present here a very simple whois server, but which is sufficient for our purposes. It opens the XML file, parse it into a <ulink url="http://www.w3c.org/DOM/">DOM object</ulink>, then waits for requests. When one arrives, it uses <ulink url="http://www.w3.org/TR/xpath">Xpath</ulink> to query the data.</para>
    <para>
      <programlisting>
<xi:include href="whoisd.py" parse="text"/>
      </programlisting>
    </para>
  </section>
</article>

