summaryrefslogtreecommitdiffstats
path: root/ipapython
diff options
context:
space:
mode:
authorMartin Basti <mbasti@redhat.com>2014-04-09 12:37:39 +0200
committerMartin Kosek <mkosek@redhat.com>2014-06-03 15:55:32 +0200
commit9c7b0ad156cacbc3afc7d319245090d67ac37e88 (patch)
treee4028bc7f71bb68444da6efa5ad7b93e3a912a3c /ipapython
parent47f473d0cac2e28980c7ecffdbfd480441eab277 (diff)
downloadfreeipa-9c7b0ad156cacbc3afc7d319245090d67ac37e88.tar.gz
freeipa-9c7b0ad156cacbc3afc7d319245090d67ac37e88.tar.xz
freeipa-9c7b0ad156cacbc3afc7d319245090d67ac37e88.zip
DNSName type
Type used to store domain names based on dnspython Part of ticket: IPA should allow internationalized domain names https://fedorahosted.org/freeipa/ticket/3169 Reviewed-By: Jan Cholasta <jcholast@redhat.com>
Diffstat (limited to 'ipapython')
-rw-r--r--ipapython/dnsutil.py100
1 files changed, 100 insertions, 0 deletions
diff --git a/ipapython/dnsutil.py b/ipapython/dnsutil.py
new file mode 100644
index 000000000..9c91578a8
--- /dev/null
+++ b/ipapython/dnsutil.py
@@ -0,0 +1,100 @@
+# Authors: Martin Basti <mbasti@redhat.com>
+#
+# Copyright (C) 2007-2014 Red Hat
+# see file 'COPYING' for use and warranty information
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+import dns.name
+import copy
+
+
+class DNSName(dns.name.Name):
+ labels = None # make pylint happy
+
+ def __init__(self, labels, origin=None):
+ if isinstance(labels, str):
+ #pylint: disable=E1101
+ labels = dns.name.from_text(labels, origin).labels
+ elif isinstance(labels, unicode):
+ #pylint: disable=E1101
+ labels = dns.name.from_unicode(labels, origin).labels
+ elif isinstance(labels, dns.name.Name):
+ labels = labels.labels
+ try:
+ super(DNSName, self).__init__(labels)
+ except UnicodeError:
+ #dnspython bug, punycoded label longer than 63 returns UnicodeError
+ #instead of LabelTooLong
+ raise dns.name.LabelTooLong()
+
+ def __nonzero__(self):
+ #dns.name.from_text('@') is represented like empty tuple
+ #we need to acting '@' as nonzero value
+ return True
+
+ def __copy__(self):
+ return DNSName(self.labels)
+
+ def __deepcopy__(self, memo):
+ return DNSName(copy.deepcopy(self.labels, memo))
+
+ def __str__(self):
+ return self.to_text()
+
+ def __unicode__(self):
+ return self.to_unicode()
+
+ def ToASCII(self):
+ #method named by RFC 3490 and python standard library
+ return str(self).decode('ascii') # must be unicode string
+
+ def concatenate(self, other):
+ return DNSName(super(DNSName, self).concatenate(other))
+
+ def relativize(self, origin):
+ return DNSName(super(DNSName, self).relativize(origin))
+
+ def derelativize(self, origin):
+ return DNSName(super(DNSName, self).derelativize(origin))
+
+ def choose_relativity(self, origin=None, relativize=True):
+ return DNSName(super(DNSName, self).choose_relativity(origin=origin,
+ relativize=relativize))
+
+ def make_absolute(self):
+ return self.derelativize(self.root)
+
+ def is_idn(self):
+ return any(label.startswith('xn--') for label in self.labels)
+
+ def is_ip4_reverse(self):
+ return self.is_subdomain(self.ip4_rev_zone)
+
+ def is_ip6_reverse(self):
+ return self.is_subdomain(self.ip6_rev_zone)
+
+ def is_reverse(self):
+ return self.is_ip4_reverse() or self.is_ip6_reverse()
+
+ def is_empty(self):
+ return len(self.labels) == 0
+
+
+#DNS public constants
+DNSName.root = DNSName(dns.name.root) # '.'
+DNSName.empty = DNSName(dns.name.empty) # '@'
+DNSName.ip4_rev_zone = DNSName(('in-addr', 'arpa', ''))
+DNSName.ip6_rev_zone = DNSName(('ip6', 'arpa', ''))