The Identity, Policy and Audit system
CentOS Sources
2016-11-03 403b09ab980c02ef36095973349a13e0181c794a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
From 89544d521e6aa52e8b9e5e7dc4676046b5792abd Mon Sep 17 00:00:00 2001
From: Jan Cholasta <jcholast@redhat.com>
Date: Tue, 23 Aug 2016 12:53:39 +0200
Subject: [PATCH] dns: fix crash in interactive mode against old servers
 
Add a client-side fallback of the dnsrecord_split_parts command for old
servers to avoid CommandError in dnsrecord_add and dnsrecord_mod CLI
interactive mode.
 
https://fedorahosted.org/freeipa/ticket/6203
 
Reviewed-By: Martin Basti <mbasti@redhat.com>
---
 ipaclient/plugins/dns.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 61 insertions(+), 1 deletion(-)
 
diff --git a/ipaclient/plugins/dns.py b/ipaclient/plugins/dns.py
index 5e29b8c6666f94f52add7f2e1de8679127886fc2..b9ab709bf0b1d72cfb9d32aeefd77a0e8bc74d7a 100644
--- a/ipaclient/plugins/dns.py
+++ b/ipaclient/plugins/dns.py
@@ -22,6 +22,7 @@ from __future__ import print_function
 
 import six
 import copy
+import re
 
 from ipaclient.frontend import MethodOverride
 from ipalib import errors
@@ -30,7 +31,8 @@ from ipalib.dns import (get_record_rrtype,
                         iterate_rrparams_by_parts,
                         part_name_format,
                         record_name_format)
-from ipalib.parameters import Bool
+from ipalib.frontend import Command
+from ipalib.parameters import Bool, Str
 from ipalib.plugable import Registry
 from ipalib import _, ngettext
 from ipapython.dnsutil import DNSName
@@ -121,6 +123,64 @@ class dnszone_mod(DNSZoneMethodOverride):
     pass
 
 
+# Support old servers without dnsrecord_split_parts
+# Do not add anything new here!
+@register(no_fail=True)
+class dnsrecord_split_parts(Command):
+    NO_CLI = True
+
+    takes_args = (
+        Str('name'),
+        Str('value'),
+    )
+
+    def execute(self, name, value, *args, **options):
+        def split_exactly(count):
+            values = value.split()
+            if len(values) != count:
+                return None
+            return tuple(values)
+
+        result = ()
+
+        rrtype = get_record_rrtype(name)
+        if rrtype in ('A', 'AAAA', 'CNAME', 'DNAME', 'NS', 'PTR'):
+            result = split_exactly(1)
+        elif rrtype in ('AFSDB', 'KX', 'MX'):
+            result = split_exactly(2)
+        elif rrtype in ('CERT', 'DLV', 'DS', 'SRV', 'TLSA'):
+            result = split_exactly(4)
+        elif rrtype in ('NAPTR'):
+            result = split_exactly(6)
+        elif rrtype in ('A6', 'TXT'):
+            result = (value,)
+        elif rrtype == 'LOC':
+            regex = re.compile(
+                r'(?P<d1>\d{1,2}\s+)'
+                r'(?:(?P<m1>\d{1,2}\s+)'
+                r'(?P<s1>\d{1,2}(?:\.\d{1,3})?\s+)?)?'
+                r'(?P<dir1>[NS])\s+'
+                r'(?P<d2>\d{1,3}\s+)'
+                r'(?:(?P<m2>\d{1,2}\s+)'
+                r'(?P<s2>\d{1,2}(?:\.\d{1,3})?\s+)?)?'
+                r'(?P<dir2>[WE])\s+'
+                r'(?P<alt>-?\d{1,8}(?:\.\d{1,2})?)m?'
+                r'(?:\s+(?P<siz>\d{1,8}(?:\.\d{1,2})?)m?'
+                r'(?:\s+(?P<hp>\d{1,8}(?:\.\d{1,2})?)m?'
+                r'(?:\s+(?P<vp>\d{1,8}(?:\.\d{1,2})?)m?\s*)?)?)?$')
+
+            m = regex.match(value)
+            if m is not None:
+                result = tuple(
+                    x.strip() if x is not None else x for x in m.groups())
+        elif rrtype == 'SSHFP':
+            values = value.split(None, 2)
+            if len(values) == 3:
+                result = tuple(values)
+
+        return dict(result=result)
+
+
 @register(override=True, no_fail=True)
 class dnsrecord_add(MethodOverride):
     no_option_msg = 'No options to add a specific record provided.\n' \
-- 
2.7.4