2 # -*- coding: utf-8 -*-
3 # This is unit with tests for LDAP access checks
5 from __future__ import print_function
12 sys.path.insert(0, "bin/python")
14 from samba.tests.subunitrun import SubunitOptions, TestProgram
16 import samba.getopt as options
18 from ldb import SCOPE_BASE, SCOPE_SUBTREE
20 from samba import gensec
22 from samba.tests import delete_force
24 parser = optparse.OptionParser("ldap [options] <host>")
25 sambaopts = options.SambaOptions(parser)
26 parser.add_option_group(sambaopts)
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()
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)
49 class BindTests(samba.tests.TestCase):
54 super(BindTests, self).setUp()
57 self.ldb = samba.tests.connect_samdb(host, credentials=creds, lp=lp, ldap_only=True)
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"
72 super(BindTests, self).tearDown()
74 def test_computer_account_bind(self):
75 # create a computer acocount for the test
76 delete_force(self.ldb, self.computer_dn)
78 dn: """ + self.computer_dn + """
80 displayName: CENTOS53$
82 sAMAccountName: CENTOS53$
85 objectClass: organizationalPerson
90 userAccountControl: 4096
91 dNSHostName: centos53.alabala.test
92 operatingSystemVersion: 5.2 (3790)
93 operatingSystem: Windows Server 2003
95 self.ldb.modify_ldif("""
96 dn: """ + self.computer_dn + """
99 unicodePwd:: """ + base64.b64encode(u"\"P@ssw0rd\"".encode('utf-16-le')).decode('utf8') + """
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=["*"])
110 def test_user_account_bind(self):
112 self.ldb.newuser(username=self.username, password=self.password)
113 ldb_res = self.ldb.search(base=self.domain_dn,
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)
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=["*"])
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=["*"])
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=["*"])
144 def test_user_account_bind_no_domain(self):
146 self.ldb.newuser(username=self.username, password=self.password)
147 ldb_res = self.ldb.search(base=self.domain_dn,
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)
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)
160 ldb_user4 = samba.tests.connect_samdb(host, credentials=creds_user4,
161 lp=lp, ldap_only=True)
163 self.fail("Failed to connect without the domain set")
165 res = ldb_user4.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
167 TestProgram(module=__name__, opts=subunitopts)