source: vendor/current/auth/credentials/tests/bind.py

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

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