doc-xml/smbdotconf: fix server [min|max] protocol documentation
[Samba/gebeck_regimport.git] / python / examples / samr.py
blob9b8e31e434b80f5c094506593a97a937aa9536da
1 #!/usr/bin/env python
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>
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.
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/>.
23 import sys
25 sys.path.insert(0, "bin/python")
27 from samba.dcerpc import samr, security
29 def display_lsa_string(str):
30 return str.string
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)
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
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)
54 def test_LookupDomain(samr, handle, domain):
55 """test the samr_LookupDomain interface"""
56 print "Testing samr_LookupDomain"
57 return samr.LookupDomain(handle, domain)
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)
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)
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)
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)
85 def test_EnumDomains(samr, handle):
86 """test the samr_EnumDomains interface"""
87 print "Testing samr_EnumDomains"
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)
100 if len(sys.argv) != 2:
101 print "Usage: samr.js <BINDING>"
102 sys.exit(1)
104 binding = sys.argv[1]
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)
113 handle = test_Connect(samr)
114 test_EnumDomains(samr, handle)
115 samr.Close(handle)
117 print "All OK"