smbd: Pass error_context_count through smbd_smb2_request_error_ex()
[Samba.git] / python / examples / samr.py
blobcbfbb1af9aba8e3cf11405be3bf4c7054e90bdbc
1 #!/usr/bin/env python3
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/>.
23 import sys
25 sys.path.insert(0, "bin/python")
27 from samba.dcerpc import samr, security
30 def display_lsa_string(str):
31 return str.string
34 def FillUserInfo(samr, dom_handle, users, level):
35 """fill a user array with user information from samrQueryUserInfo"""
36 for i in range(len(users)):
37 user_handle = samr.OpenUser(handle, security.SEC_FLAG_MAXIMUM_ALLOWED, users[i].idx)
38 info = samr.QueryUserInfo(user_handle, level)
39 info.name = users[i].name
40 info.idx = users[i].idx
41 users[i] = info
42 samr.Close(user_handle)
45 def toArray(handle, array, num_entries):
46 ret = []
47 for x in range(num_entries):
48 ret.append((array.entries[x].idx, array.entries[x].name))
49 return ret
52 def test_Connect(samr):
53 """test the samr_Connect interface"""
54 print("Testing samr_Connect")
55 return samr.Connect2(None, security.SEC_FLAG_MAXIMUM_ALLOWED)
58 def test_LookupDomain(samr, handle, domain):
59 """test the samr_LookupDomain interface"""
60 print("Testing samr_LookupDomain")
61 return samr.LookupDomain(handle, domain)
64 def test_OpenDomain(samr, handle, sid):
65 """test the samr_OpenDomain interface"""
66 print("Testing samr_OpenDomain")
67 return samr.OpenDomain(handle, security.SEC_FLAG_MAXIMUM_ALLOWED, sid)
70 def test_EnumDomainUsers(samr, dom_handle):
71 """test the samr_EnumDomainUsers interface"""
72 print("Testing samr_EnumDomainUsers")
73 users = toArray(*samr.EnumDomainUsers(dom_handle, 0, 0, 0xffffffff))
74 print("Found %d users" % len(users))
75 for idx, user in users:
76 print("\t%s\t(%d)" % (user.string, idx))
79 def test_EnumDomainGroups(samr, dom_handle):
80 """test the samr_EnumDomainGroups interface"""
81 print("Testing samr_EnumDomainGroups")
82 groups = toArray(*samr.EnumDomainGroups(dom_handle, 0, 0))
83 print("Found %d groups" % len(groups))
84 for idx, group in groups:
85 print("\t%s\t(%d)" % (group.string, idx))
88 def test_domain_ops(samr, dom_handle):
89 """test domain specific ops"""
90 test_EnumDomainUsers(samr, dom_handle)
91 test_EnumDomainGroups(samr, dom_handle)
94 def test_EnumDomains(samr, handle):
95 """test the samr_EnumDomains interface"""
96 print("Testing samr_EnumDomains")
98 domains = toArray(*samr.EnumDomains(handle, 0, 0xffffffff))
99 print("Found %d domains" % len(domains))
100 for idx, domain in domains:
101 print("\t%s (%d)" % (display_lsa_string(domain), idx))
102 for idx, domain in domains:
103 print("Testing domain %s" % display_lsa_string(domain))
104 sid = samr.LookupDomain(handle, domain)
105 dom_handle = test_OpenDomain(samr, handle, sid)
106 test_domain_ops(samr, dom_handle)
107 samr.Close(dom_handle)
110 if len(sys.argv) != 2:
111 print("Usage: samr.js <BINDING>")
112 sys.exit(1)
114 binding = sys.argv[1]
116 print("Connecting to %s" % binding)
117 try:
118 samr = samr.samr(binding)
119 except Exception as e:
120 print("Failed to connect to %s: %s" % (binding, e.message))
121 sys.exit(1)
123 handle = test_Connect(samr)
124 test_EnumDomains(samr, handle)
125 samr.Close(handle)
127 print("All OK")