1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 |
|
---|
4 | # Unix SMB/CIFS implementation.
|
---|
5 | # Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
|
---|
6 | #
|
---|
7 | # Based on samr.js © Andrew Tridgell <tridge@samba.org>
|
---|
8 | #
|
---|
9 | # This program is free software; you can redistribute it and/or modify
|
---|
10 | # it under the terms of the GNU General Public License as published by
|
---|
11 | # the Free Software Foundation; either version 3 of the License, or
|
---|
12 | # (at your option) any later version.
|
---|
13 | #
|
---|
14 | # This program is distributed in the hope that it will be useful,
|
---|
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | # GNU General Public License for more details.
|
---|
18 | #
|
---|
19 | # You should have received a copy of the GNU General Public License
|
---|
20 | # along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
21 | #
|
---|
22 |
|
---|
23 | import sys
|
---|
24 |
|
---|
25 | sys.path.insert(0, "bin/python")
|
---|
26 |
|
---|
27 | from samba.dcerpc import samr, security
|
---|
28 |
|
---|
29 | def display_lsa_string(str):
|
---|
30 | return str.string
|
---|
31 |
|
---|
32 | def FillUserInfo(samr, dom_handle, users, level):
|
---|
33 | """fill a user array with user information from samrQueryUserInfo"""
|
---|
34 | for i in range(len(users)):
|
---|
35 | user_handle = samr.OpenUser(handle, security.SEC_FLAG_MAXIMUM_ALLOWED, users[i].idx)
|
---|
36 | info = samr.QueryUserInfo(user_handle, level)
|
---|
37 | info.name = users[i].name
|
---|
38 | info.idx = users[i].idx
|
---|
39 | users[i] = info
|
---|
40 | samr.Close(user_handle)
|
---|
41 |
|
---|
42 | def toArray((handle, array, num_entries)):
|
---|
43 | ret = []
|
---|
44 | for x in range(num_entries):
|
---|
45 | ret.append((array.entries[x].idx, array.entries[x].name))
|
---|
46 | return ret
|
---|
47 |
|
---|
48 |
|
---|
49 | def test_Connect(samr):
|
---|
50 | """test the samr_Connect interface"""
|
---|
51 | print "Testing samr_Connect"
|
---|
52 | return samr.Connect2(None, security.SEC_FLAG_MAXIMUM_ALLOWED)
|
---|
53 |
|
---|
54 | def test_LookupDomain(samr, handle, domain):
|
---|
55 | """test the samr_LookupDomain interface"""
|
---|
56 | print "Testing samr_LookupDomain"
|
---|
57 | return samr.LookupDomain(handle, domain)
|
---|
58 |
|
---|
59 | def test_OpenDomain(samr, handle, sid):
|
---|
60 | """test the samr_OpenDomain interface"""
|
---|
61 | print "Testing samr_OpenDomain"
|
---|
62 | return samr.OpenDomain(handle, security.SEC_FLAG_MAXIMUM_ALLOWED, sid)
|
---|
63 |
|
---|
64 | def test_EnumDomainUsers(samr, dom_handle):
|
---|
65 | """test the samr_EnumDomainUsers interface"""
|
---|
66 | print "Testing samr_EnumDomainUsers"
|
---|
67 | users = toArray(samr.EnumDomainUsers(dom_handle, 0, 0, -1))
|
---|
68 | print "Found %d users" % len(users)
|
---|
69 | for idx, user in users:
|
---|
70 | print "\t%s\t(%d)" % (user.string, idx)
|
---|
71 |
|
---|
72 | def test_EnumDomainGroups(samr, dom_handle):
|
---|
73 | """test the samr_EnumDomainGroups interface"""
|
---|
74 | print "Testing samr_EnumDomainGroups"
|
---|
75 | groups = toArray(samr.EnumDomainGroups(dom_handle, 0, 0))
|
---|
76 | print "Found %d groups" % len(groups)
|
---|
77 | for idx, group in groups:
|
---|
78 | print "\t%s\t(%d)" % (group.string, idx)
|
---|
79 |
|
---|
80 | def test_domain_ops(samr, dom_handle):
|
---|
81 | """test domain specific ops"""
|
---|
82 | test_EnumDomainUsers(samr, dom_handle)
|
---|
83 | test_EnumDomainGroups(samr, dom_handle)
|
---|
84 |
|
---|
85 | def test_EnumDomains(samr, handle):
|
---|
86 | """test the samr_EnumDomains interface"""
|
---|
87 | print "Testing samr_EnumDomains"
|
---|
88 |
|
---|
89 | domains = toArray(samr.EnumDomains(handle, 0, -1))
|
---|
90 | print "Found %d domains" % len(domains)
|
---|
91 | for idx, domain in domains:
|
---|
92 | print "\t%s (%d)" % (display_lsa_string(domain), idx)
|
---|
93 | for idx, domain in domains:
|
---|
94 | print "Testing domain %s" % display_lsa_string(domain)
|
---|
95 | sid = samr.LookupDomain(handle, domain)
|
---|
96 | dom_handle = test_OpenDomain(samr, handle, sid)
|
---|
97 | test_domain_ops(samr, dom_handle)
|
---|
98 | samr.Close(dom_handle)
|
---|
99 |
|
---|
100 | if len(sys.argv) != 2:
|
---|
101 | print "Usage: samr.js <BINDING>"
|
---|
102 | sys.exit(1)
|
---|
103 |
|
---|
104 | binding = sys.argv[1]
|
---|
105 |
|
---|
106 | print "Connecting to %s" % binding
|
---|
107 | try:
|
---|
108 | samr = samr.samr(binding)
|
---|
109 | except Exception, e:
|
---|
110 | print "Failed to connect to %s: %s" % (binding, e.message)
|
---|
111 | sys.exit(1)
|
---|
112 |
|
---|
113 | handle = test_Connect(samr)
|
---|
114 | test_EnumDomains(samr, handle)
|
---|
115 | samr.Close(handle)
|
---|
116 |
|
---|
117 | print "All OK"
|
---|