1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | # This is unit with tests for LDAP access checks
|
---|
4 |
|
---|
5 | import optparse
|
---|
6 | import sys
|
---|
7 | import base64
|
---|
8 | import copy
|
---|
9 | import time
|
---|
10 |
|
---|
11 | sys.path.insert(0, "bin/python")
|
---|
12 | import samba
|
---|
13 | from samba.tests.subunitrun import SubunitOptions, TestProgram
|
---|
14 |
|
---|
15 | import samba.getopt as options
|
---|
16 |
|
---|
17 | from ldb import SCOPE_BASE, SCOPE_SUBTREE
|
---|
18 |
|
---|
19 | from samba import gensec
|
---|
20 | import samba.tests
|
---|
21 | from samba.tests import delete_force
|
---|
22 |
|
---|
23 | parser = optparse.OptionParser("ldap [options] <host>")
|
---|
24 | sambaopts = options.SambaOptions(parser)
|
---|
25 | parser.add_option_group(sambaopts)
|
---|
26 |
|
---|
27 | # use command line creds if available
|
---|
28 | credopts = options.CredentialsOptions(parser)
|
---|
29 | parser.add_option_group(credopts)
|
---|
30 | subunitopts = SubunitOptions(parser)
|
---|
31 | parser.add_option_group(subunitopts)
|
---|
32 | opts, args = parser.parse_args()
|
---|
33 |
|
---|
34 | if len(args) < 1:
|
---|
35 | parser.print_usage()
|
---|
36 | sys.exit(1)
|
---|
37 |
|
---|
38 | host = args[0]
|
---|
39 | lp = sambaopts.get_loadparm()
|
---|
40 | creds = credopts.get_credentials(lp)
|
---|
41 | creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
|
---|
42 | creds_machine = copy.deepcopy(creds)
|
---|
43 | creds_user1 = copy.deepcopy(creds)
|
---|
44 | creds_user2 = copy.deepcopy(creds)
|
---|
45 | creds_user3 = copy.deepcopy(creds)
|
---|
46 |
|
---|
47 | class 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("""
|
---|
76 | dn: """ + self.computer_dn + """
|
---|
77 | cn: CENTOS53
|
---|
78 | displayName: CENTOS53$
|
---|
79 | name: CENTOS53
|
---|
80 | sAMAccountName: CENTOS53$
|
---|
81 | countryCode: 0
|
---|
82 | objectClass: computer
|
---|
83 | objectClass: organizationalPerson
|
---|
84 | objectClass: person
|
---|
85 | objectClass: top
|
---|
86 | objectClass: user
|
---|
87 | codePage: 0
|
---|
88 | userAccountControl: 4096
|
---|
89 | dNSHostName: centos53.alabala.test
|
---|
90 | operatingSystemVersion: 5.2 (3790)
|
---|
91 | operatingSystem: Windows Server 2003
|
---|
92 | """)
|
---|
93 | self.ldb.modify_ldif("""
|
---|
94 | dn: """ + self.computer_dn + """
|
---|
95 | changetype: modify
|
---|
96 | replace: unicodePwd
|
---|
97 | unicodePwd:: """ + 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 |
|
---|
142 | TestProgram(module=__name__, opts=subunitopts)
|
---|