1# Unix SMB/CIFS implementation.
2# Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
3# Written by Joe Guo <joeg@catalyst.net.nz>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17#
18
19import os
20from samba.tests.samba_tool.base import SambaToolCmdTest
21
22
23class DemoteCmdTestCase(SambaToolCmdTest):
24    """Test for samba-tool domain demote subcommand"""
25
26    def setUp(self):
27        super(DemoteCmdTestCase, self).setUp()
28        self.creds_string = "-U{0}%{1}".format(
29            os.environ["DC_USERNAME"], os.environ["DC_PASSWORD"])
30
31        self.dc_server = os.environ['DC_SERVER']
32        self.dburl = "ldap://%s" % os.environ["DC_SERVER"]
33        self.samdb = self.getSamDB("-H", self.dburl, self.creds_string)
34
35    def test_demote_and_remove_dns(self):
36        """
37        Test domain demote command will also remove dns references
38        """
39
40        server = os.environ['SERVER']  # the server to demote
41        zone = os.environ['REALM'].lower()
42
43        # make sure zone exist
44        result, out, err = self.runsubcmd(
45            "dns", "zoneinfo", server, zone, self.creds_string)
46        self.assertCmdSuccess(result, out, err)
47
48        # add a A record for the server to demote
49        result, out, err = self.runsubcmd(
50            "dns", "add", self.dc_server, zone,
51            server, "A", "192.168.0.193", self.creds_string)
52        self.assertCmdSuccess(result, out, err)
53
54        # make sure above A record exist
55        result, out, err = self.runsubcmd(
56            "dns", "query", self.dc_server, zone,
57            server, 'A', self.creds_string)
58        self.assertCmdSuccess(result, out, err)
59
60        # the above A record points to this host
61        dnshostname = '{0}.{1}'.format(server, zone)
62
63        # add a SRV record points to above host
64        srv_record = "{0} 65530 65530 65530".format(dnshostname)
65        self.runsubcmd(
66            "dns", "add", self.dc_server, zone, 'testrecord', "SRV",
67            srv_record, self.creds_string)
68
69        # make sure above SRV record exist
70        result, out, err = self.runsubcmd(
71            "dns", "query", self.dc_server, zone,
72            "testrecord", 'SRV', self.creds_string)
73        self.assertCmdSuccess(result, out, err)
74
75        for type_ in ['CNAME', 'NS', 'PTR']:
76            # create record
77            self.runsubcmd(
78                "dns", "add", self.dc_server, zone,
79                'testrecord', type_, dnshostname,
80                self.creds_string)
81            self.assertCmdSuccess(result, out, err)
82
83            # check exist
84            result, out, err = self.runsubcmd(
85                "dns", "query", self.dc_server, zone,
86                "testrecord", 'SRV', self.creds_string)
87            self.assertCmdSuccess(result, out, err)
88
89        # now demote
90        result, out, err = self.runsubcmd(
91            "domain", "demote",
92            "--server", self.dc_server,
93            "--configfile", os.environ["CONFIGFILE"],
94            "--workgroup", os.environ["DOMAIN"],
95            self.creds_string)
96        self.assertCmdSuccess(result, out, err)
97
98        result, out, err = self.runsubcmd(
99            "dns", "query", self.dc_server, zone,
100            server, 'ALL', self.creds_string)
101        self.assertCmdFail(result)
102
103        result, out, err = self.runsubcmd(
104            "dns", "query", self.dc_server, zone,
105            "testrecord", 'ALL', self.creds_string)
106        self.assertCmdFail(result)
107