2 # -*- coding: utf-8 -*-
4 # Unix SMB/CIFS implementation.
5 # Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
7 # Based on samr.js © Andrew Tridgell <tridge@samba.org>
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.
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.
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/>.
25 sys.path.insert(0, "bin/python")
27 from samba.dcerpc import samr, security, lsa
29 def FillUserInfo(samr, dom_handle, users, level):
30 """fill a user array with user information from samrQueryUserInfo"""
31 for i in range(len(users)):
32 user_handle = samr.OpenUser(handle, security.SEC_FLAG_MAXIMUM_ALLOWED, users[i].idx)
33 info = samr.QueryUserInfo(user_handle, level)
34 info.name = users[i].name
35 info.idx = users[i].idx
37 samr.Close(user_handle)
39 def toArray((handle, array, num_entries)):
41 for x in range(num_entries):
42 ret.append((array.entries[x].idx, array.entries[x].name))
46 def test_Connect(samr):
47 """test the samr_Connect interface"""
48 print "Testing samr_Connect"
49 return samr.Connect2(None, security.SEC_FLAG_MAXIMUM_ALLOWED)
51 def test_LookupDomain(samr, handle, domain):
52 """test the samr_LookupDomain interface"""
53 print "Testing samr_LookupDomain"
54 return samr.LookupDomain(handle, domain)
56 def test_OpenDomain(samr, handle, sid):
57 """test the samr_OpenDomain interface"""
58 print "Testing samr_OpenDomain"
59 return samr.OpenDomain(handle, security.SEC_FLAG_MAXIMUM_ALLOWED, sid)
61 def test_EnumDomainUsers(samr, dom_handle):
62 """test the samr_EnumDomainUsers interface"""
63 print "Testing samr_EnumDomainUsers"
64 users = toArray(samr.EnumDomainUsers(dom_handle, 0, 0, -1))
65 print "Found %d users" % len(users)
66 for idx, user in users:
67 print "\t%s\t(%d)" % (user, idx)
69 def test_EnumDomainGroups(samr, dom_handle):
70 """test the samr_EnumDomainGroups interface"""
71 print "Testing samr_EnumDomainGroups"
72 groups = toArray(samr.EnumDomainGroups(dom_handle, 0, 0))
73 print "Found %d groups" % len(groups)
74 for idx, group in groups:
75 print "\t%s\t(%d)" % (group, idx)
77 def test_domain_ops(samr, dom_handle):
78 """test domain specific ops"""
79 test_EnumDomainUsers(samr, dom_handle)
80 test_EnumDomainGroups(samr, dom_handle)
82 def test_EnumDomains(samr, handle):
83 """test the samr_EnumDomains interface"""
84 print "Testing samr_EnumDomains"
86 domains = toArray(samr.EnumDomains(handle, 0, -1))
87 print "Found %d domains" % len(domains)
88 for idx, domain in domains:
89 print "\t%s (%d)" % (domain, idx)
90 for idx, domain in domains:
91 print "Testing domain %s" % domain
92 sid = samr.LookupDomain(handle, domain)
93 dom_handle = test_OpenDomain(samr, handle, sid)
94 test_domain_ops(samr, dom_handle)
95 samr.Close(dom_handle)
97 if len(sys.argv) != 2:
98 print "Usage: samr.js <BINDING>"
101 binding = sys.argv[1]
103 print "Connecting to %s" % binding
105 samr = samr.samr(binding)
107 print "Failed to connect to %s: %s" % (binding, e.message)
110 handle = test_Connect(samr)
111 test_EnumDomains(samr, handle)