2 # -*- coding: utf-8 -*-
3 # This is unit with tests for LDAP access checks
13 sys.path.append("bin/python")
15 samba.ensure_external_module("testtools", "testtools")
16 samba.ensure_external_module("subunit", "subunit/python")
18 import samba.getopt as options
21 SCOPE_BASE, SCOPE_SUBTREE, LdbError, ERR_NO_SUCH_OBJECT)
22 from samba.dcerpc import security
24 from samba.auth import system_session
25 from samba import gensec
26 from samba.samdb import SamDB
27 from samba.credentials import Credentials
29 from samba.tests import delete_force
30 from subunit.run import SubunitTestRunner
33 parser = optparse.OptionParser("ldap [options] <host>")
34 sambaopts = options.SambaOptions(parser)
35 parser.add_option_group(sambaopts)
37 # use command line creds if available
38 credopts = options.CredentialsOptions(parser)
39 parser.add_option_group(credopts)
40 opts, args = parser.parse_args()
47 lp = sambaopts.get_loadparm()
48 creds = credopts.get_credentials(lp)
49 creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
50 creds_machine = copy.deepcopy(creds)
51 creds_user1 = copy.deepcopy(creds)
52 creds_user2 = copy.deepcopy(creds)
53 creds_user3 = copy.deepcopy(creds)
55 class BindTests(samba.tests.TestCase):
60 super(BindTests, self).setUp()
62 if self.info_dc is None:
63 res = ldb.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
64 self.assertEquals(len(res), 1)
65 BindTests.info_dc = res[0]
66 # cache some of RootDSE props
67 self.schema_dn = self.info_dc["schemaNamingContext"][0]
68 self.domain_dn = self.info_dc["defaultNamingContext"][0]
69 self.config_dn = self.info_dc["configurationNamingContext"][0]
70 self.computer_dn = "CN=centos53,CN=Computers,%s" % self.domain_dn
71 self.password = "P@ssw0rd"
72 self.username = "BindTestUser_" + time.strftime("%s", time.gmtime())
75 super(BindTests, self).tearDown()
77 def test_computer_account_bind(self):
78 # create a computer acocount for the test
79 delete_force(ldb, self.computer_dn)
81 dn: """ + self.computer_dn + """
83 displayName: CENTOS53$
85 sAMAccountName: CENTOS53$
88 objectClass: organizationalPerson
93 userAccountControl: 4096
94 dNSHostName: centos53.alabala.test
95 operatingSystemVersion: 5.2 (3790)
96 operatingSystem: Windows Server 2003
99 dn: """ + self.computer_dn + """
102 unicodePwd:: """ + base64.b64encode("\"P@ssw0rd\"".encode('utf-16-le')) + """
105 # do a simple bind and search with the machine account
106 creds_machine.set_bind_dn(self.computer_dn)
107 creds_machine.set_password(self.password)
108 print "BindTest with: " + creds_machine.get_bind_dn()
109 ldb_machine = SamDB(host, credentials=creds_machine, session_info=system_session(), lp=lp)
110 res = ldb_machine.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
112 def test_user_account_bind(self):
114 ldb.newuser(username=self.username, password=self.password)
115 ldb_res = ldb.search(base=self.domain_dn,
117 expression="(samAccountName=%s)" % self.username)
118 self.assertEquals(len(ldb_res), 1)
119 user_dn = ldb_res[0]["dn"]
121 # do a simple bind and search with the user account in format user@realm
122 creds_user1.set_bind_dn(self.username + "@" + creds.get_realm())
123 creds_user1.set_password(self.password)
124 print "BindTest with: " + creds_user1.get_bind_dn()
125 ldb_user1 = SamDB(host, credentials=creds_user1, session_info=system_session(), lp=lp)
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 = SamDB(host, credentials=creds_user2, lp=lp)
133 res = ldb_user2.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
135 # do a simple bind and search with the user account DN
136 creds_user3.set_bind_dn(str(user_dn))
137 creds_user3.set_password(self.password)
138 print "BindTest with: " + creds_user3.get_bind_dn()
139 ldb_user3 = SamDB(host, credentials=creds_user3, session_info=system_session(), lp=lp)
140 res = ldb_user3.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
142 if not "://" in host:
143 host = "ldap://%s" % host
144 ldb = SamDB(host, credentials=creds, session_info=system_session(), lp=lp)
146 runner = SubunitTestRunner()
148 if not runner.run(unittest.makeSuite(BindTests)).wasSuccessful():