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
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
28 import samba
.tests
, unittest
29 from samba
.tests
import delete_force
30 from subunit
.run
import SubunitTestRunner
31 from samba
.tests
import TestCase
, TestSkipped
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
= samba
.tests
.connect_samdb(host
, credentials
=creds_machine
,
110 lp
=lp
, ldap_only
=True)
111 res
= ldb_machine
.search(base
="", expression
="", scope
=SCOPE_BASE
, attrs
=["*"])
113 def test_user_account_bind(self
):
115 ldb
.newuser(username
=self
.username
, password
=self
.password
)
116 ldb_res
= ldb
.search(base
=self
.domain_dn
,
118 expression
="(samAccountName=%s)" % self
.username
)
119 self
.assertEquals(len(ldb_res
), 1)
120 user_dn
= ldb_res
[0]["dn"]
122 # do a simple bind and search with the user account in format user@realm
123 creds_user1
.set_bind_dn(self
.username
+ "@" + creds
.get_realm())
124 creds_user1
.set_password(self
.password
)
125 print "BindTest with: " + creds_user1
.get_bind_dn()
126 ldb_user1
= samba
.tests
.connect_samdb(host
, credentials
=creds_user1
,
127 lp
=lp
, ldap_only
=True)
128 res
= ldb_user1
.search(base
="", expression
="", scope
=SCOPE_BASE
, attrs
=["*"])
130 # do a simple bind and search with the user account in format domain\user
131 creds_user2
.set_bind_dn(creds
.get_domain() + "\\" + self
.username
)
132 creds_user2
.set_password(self
.password
)
133 print "BindTest with: " + creds_user2
.get_bind_dn()
134 ldb_user2
= samba
.tests
.connect_samdb(host
, credentials
=creds_user2
,
135 lp
=lp
, ldap_only
=True)
136 res
= ldb_user2
.search(base
="", expression
="", scope
=SCOPE_BASE
, attrs
=["*"])
138 # do a simple bind and search with the user account DN
139 creds_user3
.set_bind_dn(str(user_dn
))
140 creds_user3
.set_password(self
.password
)
141 print "BindTest with: " + creds_user3
.get_bind_dn()
142 ldb_user3
= samba
.tests
.connect_samdb(host
, credentials
=creds_user3
,
143 lp
=lp
, ldap_only
=True)
144 res
= ldb_user3
.search(base
="", expression
="", scope
=SCOPE_BASE
, attrs
=["*"])
147 ldb
= samba
.tests
.connect_samdb(host
, credentials
=creds
, lp
=lp
, ldap_only
=True)
149 runner
= SubunitTestRunner()
151 if not runner
.run(unittest
.makeSuite(BindTests
)).wasSuccessful():