2 # -*- coding: utf-8 -*-
3 # This is unit with tests for LDAP access checks
13 sys.path.insert(0, "bin/python")
15 samba.ensure_external_module("testtools", "testtools")
16 samba.ensure_external_module("subunit", "subunit/python")
18 import samba.getopt as options
20 from ldb import SCOPE_BASE, SCOPE_SUBTREE
22 from samba import gensec
23 import samba.tests, unittest
24 from samba.tests import delete_force
25 from subunit.run import SubunitTestRunner
27 parser = optparse.OptionParser("ldap [options] <host>")
28 sambaopts = options.SambaOptions(parser)
29 parser.add_option_group(sambaopts)
31 # use command line creds if available
32 credopts = options.CredentialsOptions(parser)
33 parser.add_option_group(credopts)
34 opts, args = parser.parse_args()
41 lp = sambaopts.get_loadparm()
42 creds = credopts.get_credentials(lp)
43 creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
44 creds_machine = copy.deepcopy(creds)
45 creds_user1 = copy.deepcopy(creds)
46 creds_user2 = copy.deepcopy(creds)
47 creds_user3 = copy.deepcopy(creds)
49 class BindTests(samba.tests.TestCase):
54 super(BindTests, self).setUp()
56 if self.info_dc is None:
57 res = ldb.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
58 self.assertEquals(len(res), 1)
59 BindTests.info_dc = res[0]
60 # cache some of RootDSE props
61 self.schema_dn = self.info_dc["schemaNamingContext"][0]
62 self.domain_dn = self.info_dc["defaultNamingContext"][0]
63 self.config_dn = self.info_dc["configurationNamingContext"][0]
64 self.computer_dn = "CN=centos53,CN=Computers,%s" % self.domain_dn
65 self.password = "P@ssw0rd"
66 self.username = "BindTestUser_" + time.strftime("%s", time.gmtime())
69 super(BindTests, self).tearDown()
71 def test_computer_account_bind(self):
72 # create a computer acocount for the test
73 delete_force(ldb, self.computer_dn)
75 dn: """ + self.computer_dn + """
77 displayName: CENTOS53$
79 sAMAccountName: CENTOS53$
82 objectClass: organizationalPerson
87 userAccountControl: 4096
88 dNSHostName: centos53.alabala.test
89 operatingSystemVersion: 5.2 (3790)
90 operatingSystem: Windows Server 2003
93 dn: """ + self.computer_dn + """
96 unicodePwd:: """ + base64.b64encode("\"P@ssw0rd\"".encode('utf-16-le')) + """
99 # do a simple bind and search with the machine account
100 creds_machine.set_bind_dn(self.computer_dn)
101 creds_machine.set_password(self.password)
102 print "BindTest with: " + creds_machine.get_bind_dn()
103 ldb_machine = samba.tests.connect_samdb(host, credentials=creds_machine,
104 lp=lp, ldap_only=True)
105 res = ldb_machine.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
107 def test_user_account_bind(self):
109 ldb.newuser(username=self.username, password=self.password)
110 ldb_res = ldb.search(base=self.domain_dn,
112 expression="(samAccountName=%s)" % self.username)
113 self.assertEquals(len(ldb_res), 1)
114 user_dn = ldb_res[0]["dn"]
116 # do a simple bind and search with the user account in format user@realm
117 creds_user1.set_bind_dn(self.username + "@" + creds.get_realm())
118 creds_user1.set_password(self.password)
119 print "BindTest with: " + creds_user1.get_bind_dn()
120 ldb_user1 = samba.tests.connect_samdb(host, credentials=creds_user1,
121 lp=lp, ldap_only=True)
122 res = ldb_user1.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
124 # do a simple bind and search with the user account in format domain\user
125 creds_user2.set_bind_dn(creds.get_domain() + "\\" + self.username)
126 creds_user2.set_password(self.password)
127 print "BindTest with: " + creds_user2.get_bind_dn()
128 ldb_user2 = samba.tests.connect_samdb(host, credentials=creds_user2,
129 lp=lp, ldap_only=True)
130 res = ldb_user2.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
132 # do a simple bind and search with the user account DN
133 creds_user3.set_bind_dn(str(user_dn))
134 creds_user3.set_password(self.password)
135 print "BindTest with: " + creds_user3.get_bind_dn()
136 ldb_user3 = samba.tests.connect_samdb(host, credentials=creds_user3,
137 lp=lp, ldap_only=True)
138 res = ldb_user3.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
141 ldb = samba.tests.connect_samdb(host, credentials=creds, lp=lp, ldap_only=True)
143 runner = SubunitTestRunner()
145 if not runner.run(unittest.makeSuite(BindTests)).wasSuccessful():