PEP8: fix E125: continuation line with same indent as next logical line
[samba.git] / auth / credentials / tests / bind.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # This is unit with tests for LDAP access checks
4
5 from __future__ import print_function
6 import optparse
7 import sys
8 import base64
9 import copy
10 import time
11
12 sys.path.insert(0, "bin/python")
13 import samba
14 from samba.tests.subunitrun import SubunitOptions, TestProgram
15
16 import samba.getopt as options
17
18 from ldb import SCOPE_BASE, SCOPE_SUBTREE
19
20 from samba import gensec
21 import samba.tests
22 from samba.tests import delete_force
23
24 parser = optparse.OptionParser("ldap [options] <host>")
25 sambaopts = options.SambaOptions(parser)
26 parser.add_option_group(sambaopts)
27
28 # use command line creds if available
29 credopts = options.CredentialsOptions(parser)
30 parser.add_option_group(credopts)
31 subunitopts = SubunitOptions(parser)
32 parser.add_option_group(subunitopts)
33 opts, args = parser.parse_args()
34
35 if len(args) < 1:
36     parser.print_usage()
37     sys.exit(1)
38
39 host = args[0]
40 lp = sambaopts.get_loadparm()
41 creds = credopts.get_credentials(lp)
42 creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
43 creds_machine = copy.deepcopy(creds)
44 creds_user1 = copy.deepcopy(creds)
45 creds_user2 = copy.deepcopy(creds)
46 creds_user3 = copy.deepcopy(creds)
47 creds_user4 = copy.deepcopy(creds)
48
49 class BindTests(samba.tests.TestCase):
50
51     info_dc = None
52
53     def setUp(self):
54         super(BindTests, self).setUp()
55         # fetch rootDSEs
56
57         self.ldb = samba.tests.connect_samdb(host, credentials=creds, lp=lp, ldap_only=True)
58
59         if self.info_dc is None:
60             res = self.ldb.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
61             self.assertEquals(len(res), 1)
62             BindTests.info_dc = res[0]
63         # cache some of RootDSE props
64         self.schema_dn = self.info_dc["schemaNamingContext"][0]
65         self.domain_dn = self.info_dc["defaultNamingContext"][0]
66         self.config_dn = self.info_dc["configurationNamingContext"][0]
67         self.computer_dn = "CN=centos53,CN=Computers,%s" % self.domain_dn
68         self.password = "P@ssw0rd"
69         self.username = "BindTestUser"
70
71     def tearDown(self):
72         super(BindTests, self).tearDown()
73
74     def test_computer_account_bind(self):
75         # create a computer acocount for the test
76         delete_force(self.ldb, self.computer_dn)
77         self.ldb.add_ldif("""
78 dn: """ + self.computer_dn + """
79 cn: CENTOS53
80 displayName: CENTOS53$
81 name: CENTOS53
82 sAMAccountName: CENTOS53$
83 countryCode: 0
84 objectClass: computer
85 objectClass: organizationalPerson
86 objectClass: person
87 objectClass: top
88 objectClass: user
89 codePage: 0
90 userAccountControl: 4096
91 dNSHostName: centos53.alabala.test
92 operatingSystemVersion: 5.2 (3790)
93 operatingSystem: Windows Server 2003
94 """)
95         self.ldb.modify_ldif("""
96 dn: """ + self.computer_dn + """
97 changetype: modify
98 replace: unicodePwd
99 unicodePwd:: """ + base64.b64encode(u"\"P@ssw0rd\"".encode('utf-16-le')).decode('utf8') + """
100 """)
101
102         # do a simple bind and search with the machine account
103         creds_machine.set_bind_dn(self.computer_dn)
104         creds_machine.set_password(self.password)
105         print("BindTest with: " + creds_machine.get_bind_dn())
106         ldb_machine = samba.tests.connect_samdb(host, credentials=creds_machine,
107                                                 lp=lp, ldap_only=True)
108         res = ldb_machine.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
109
110     def test_user_account_bind(self):
111         # create user
112         self.ldb.newuser(username=self.username, password=self.password)
113         ldb_res = self.ldb.search(base=self.domain_dn,
114                                       scope=SCOPE_SUBTREE,
115                                       expression="(samAccountName=%s)" % self.username)
116         self.assertEquals(len(ldb_res), 1)
117         user_dn = ldb_res[0]["dn"]
118         self.addCleanup(delete_force, self.ldb, user_dn)
119
120         # do a simple bind and search with the user account in format user@realm
121         creds_user1.set_bind_dn(self.username + "@" + creds.get_realm())
122         creds_user1.set_password(self.password)
123         print("BindTest with: " + creds_user1.get_bind_dn())
124         ldb_user1 = samba.tests.connect_samdb(host, credentials=creds_user1,
125                                               lp=lp, ldap_only=True)
126         res = ldb_user1.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
127
128         # do a simple bind and search with the user account in format domain\user
129         creds_user2.set_bind_dn(creds.get_domain() + "\\" + self.username)
130         creds_user2.set_password(self.password)
131         print("BindTest with: " + creds_user2.get_bind_dn())
132         ldb_user2 = samba.tests.connect_samdb(host, credentials=creds_user2,
133                                               lp=lp, ldap_only=True)
134         res = ldb_user2.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
135
136         # do a simple bind and search with the user account DN
137         creds_user3.set_bind_dn(str(user_dn))
138         creds_user3.set_password(self.password)
139         print("BindTest with: " + creds_user3.get_bind_dn())
140         ldb_user3 = samba.tests.connect_samdb(host, credentials=creds_user3,
141                                               lp=lp, ldap_only=True)
142         res = ldb_user3.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
143
144     def test_user_account_bind_no_domain(self):
145         # create user
146         self.ldb.newuser(username=self.username, password=self.password)
147         ldb_res = self.ldb.search(base=self.domain_dn,
148                                       scope=SCOPE_SUBTREE,
149                                       expression="(samAccountName=%s)" % self.username)
150         self.assertEquals(len(ldb_res), 1)
151         user_dn = ldb_res[0]["dn"]
152         self.addCleanup(delete_force, self.ldb, user_dn)
153
154         creds_user4.set_username(self.username)
155         creds_user4.set_password(self.password)
156         creds_user4.set_domain('')
157         creds_user4.set_workstation('')
158         print("BindTest (no domain) with: " + self.username)
159         try:
160             ldb_user4 = samba.tests.connect_samdb(host, credentials=creds_user4,
161                                               lp=lp, ldap_only=True)
162         except:
163             self.fail("Failed to connect without the domain set")
164
165         res = ldb_user4.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
166
167 TestProgram(module=__name__, opts=subunitopts)