python:samba:tests: Fix code spelling
[samba.git] / python / samba / tests / auth_log_netlogon.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2017
3 # Copyright (C) Catalyst IT Ltd. 2017
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
19 """
20     Tests that exercise the auth logging for a successful netlogon attempt
21
22     NOTE: As the netlogon authentication is performed once per session,
23           there is only one test in this routine.  If another test is added
24           only the test executed first will generate the netlogon auth message
25 """
26
27 import samba.tests
28 import os
29 from samba.samdb import SamDB
30 import samba.tests.auth_log_base
31 from samba.credentials import Credentials
32 from samba.dcerpc import netlogon
33 from samba.dcerpc.dcerpc import AS_SYSTEM_MAGIC_PATH_TOKEN
34 from samba.auth import system_session
35 from samba.tests import delete_force
36 from samba.dsdb import UF_WORKSTATION_TRUST_ACCOUNT, UF_PASSWD_NOTREQD
37 from samba.dcerpc.misc import SEC_CHAN_WKSTA
38 from samba.dcerpc.windows_event_ids import (
39     EVT_ID_SUCCESSFUL_LOGON,
40     EVT_LOGON_NETWORK
41 )
42
43
44 class AuthLogTestsNetLogon(samba.tests.auth_log_base.AuthLogTestBase):
45
46     def setUp(self):
47         super(AuthLogTestsNetLogon, self).setUp()
48         self.lp = samba.tests.env_loadparm()
49         self.session = system_session()
50         self.ldb = SamDB(
51             session_info=self.session,
52             lp=self.lp)
53
54         self.domain = os.environ["DOMAIN"]
55         self.netbios_name = "NetLogonGood"
56         self.machinepass = "abcdefghij"
57         self.remoteAddress = AS_SYSTEM_MAGIC_PATH_TOKEN
58         self.base_dn = self.ldb.domain_dn()
59         self.dn = ("cn=%s,cn=users,%s" % (self.netbios_name, self.base_dn))
60
61         utf16pw = ('"' + self.machinepass + '"').encode('utf-16-le')
62         self.ldb.add({
63             "dn": self.dn,
64             "objectclass": "computer",
65             "sAMAccountName": "%s$" % self.netbios_name,
66             "userAccountControl":
67                 str(UF_WORKSTATION_TRUST_ACCOUNT | UF_PASSWD_NOTREQD),
68             "unicodePwd": utf16pw})
69
70     def tearDown(self):
71         super(AuthLogTestsNetLogon, self).tearDown()
72         delete_force(self.ldb, self.dn)
73
74     def _test_netlogon(self, binding, checkFunction):
75
76         def isLastExpectedMessage(msg):
77             return (
78                 msg["type"] == "Authorization" and
79                 msg["Authorization"]["serviceDescription"] == "DCE/RPC" and
80                 msg["Authorization"]["authType"] == "schannel" and
81                 msg["Authorization"]["transportProtection"] == "SEAL")
82
83         if binding:
84             binding = "[schannel,%s]" % binding
85         else:
86             binding = "[schannel]"
87
88         machine_creds = Credentials()
89         machine_creds.guess(self.get_loadparm())
90         machine_creds.set_secure_channel_type(SEC_CHAN_WKSTA)
91         machine_creds.set_password(self.machinepass)
92         machine_creds.set_username(self.netbios_name + "$")
93
94         netlogon_conn = netlogon.netlogon("ncalrpc:%s" % binding,
95                                           self.get_loadparm(),
96                                           machine_creds)
97
98         messages = self.waitForMessages(isLastExpectedMessage, netlogon_conn)
99         checkFunction(messages)
100
101     def netlogon_check(self, messages):
102
103         expected_messages = 5
104         self.assertEqual(expected_messages,
105                           len(messages),
106                           "Did not receive the expected number of messages")
107
108         # Check the first message it should be an Authorization
109         msg = messages[0]
110         self.assertEqual("Authorization", msg["type"])
111         self.assertEqual("DCE/RPC",
112                           msg["Authorization"]["serviceDescription"])
113         self.assertEqual("ncalrpc", msg["Authorization"]["authType"])
114         self.assertEqual("NONE", msg["Authorization"]["transportProtection"])
115         self.assertTrue(self.is_guid(msg["Authorization"]["sessionId"]))
116
117         # Check the fourth message it should be a NETLOGON Authentication
118         msg = messages[3]
119         self.assertEqual("Authentication", msg["type"])
120         self.assertEqual("NETLOGON",
121                           msg["Authentication"]["serviceDescription"])
122         self.assertEqual("ServerAuthenticate",
123                           msg["Authentication"]["authDescription"])
124         self.assertEqual("NT_STATUS_OK",
125                           msg["Authentication"]["status"])
126         self.assertEqual("HMAC-SHA256",
127                           msg["Authentication"]["passwordType"])
128         self.assertEqual(EVT_ID_SUCCESSFUL_LOGON,
129                           msg["Authentication"]["eventId"])
130         self.assertEqual(EVT_LOGON_NETWORK,
131                           msg["Authentication"]["logonType"])
132
133     def test_netlogon(self):
134         self._test_netlogon("SEAL", self.netlogon_check)