How do you map an email address onto a SOA RNAME field?

1.8k Views Asked by At

Is there an existing/standard algorithm for mapping an email address onto a RNAME field of a SOA record (and its inverse)? I'm using the dnspython package but I don't see anything in their source tree to handle this. I ran into the edge case of having a period '.' in the username that needs to be escaped and wondering if there are any other edge cases that I am missing. RFC 1035 simply states:

A <domain-name> which specifies the mailbox of the person responsible for this zone.

None of the RFCs that update 1035 expand upon RNAME field aside from a brief mention in RFC 1183.

1

There are 1 best solutions below

0
On

Here is what I came up with using dnspython:

from dns.name import from_text


def email_to_rname(email):
    """Convert standard email address into RNAME field for SOA record.

    >>> email_to_rname('[email protected]')
    <DNS name johndoe.example.com.>
    >>> email_to_rname('[email protected]')
    <DNS name john\.doe.example.com.>
    >>> print email_to_rname('[email protected]')
    johndoe.example.com.
    >>> print email_to_rname('[email protected]')
    john\.doe.example.com.

    """
    username, domain = email.split('@', 1)
    username = username.replace('.', '\\.')  # escape . in username
    return from_text('.'.join((username, domain)))


def rname_to_email(rname):
    """Convert SOA record RNAME field into standard email address.

    >>> rname_to_email(from_text('johndoe.example.com.'))
    '[email protected]'
    >>> rname_to_email(from_text('john\\.doe.example.com.'))
    '[email protected]'
    >>> rname_to_email(email_to_rname('[email protected]'))
    '[email protected]'
    >>> rname_to_email(email_to_rname('[email protected]'))
    '[email protected]'

    """
    labels = list(rname)
    username, domain = labels[0], '.'.join(labels[1:]).rstrip('.')
    username = username.replace('\\.', '.')  # unescape . in username
    return '@'.join((username, domain))