1f2afbe688e85b2f1a9579a064557c7868d89cef
[metze/samba/wip.git] / source4 / scripting / python / examples / samr.py
1 #!/usr/bin/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, lsa
28
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
36         users[i] = info
37         samr.Close(user_handle)
38
39 def toArray((handle, array, num_entries)):
40     ret = []
41     for x in range(num_entries):
42         ret.append((array.entries[x].idx, array.entries[x].name))
43     return ret
44     
45
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)
50
51 def test_LookupDomain(samr, handle, domain):
52     """test the samr_LookupDomain interface"""
53     print "Testing samr_LookupDomain"
54     return samr.LookupDomain(handle, domain)
55
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)
60
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)
68
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)
76
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)
81
82 def test_EnumDomains(samr, handle):
83     """test the samr_EnumDomains interface"""
84     print "Testing samr_EnumDomains"
85
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)
96
97 if len(sys.argv) != 2:
98    print "Usage: samr.js <BINDING>"
99    sys.exit(1)
100
101 binding = sys.argv[1]
102
103 print "Connecting to %s" % binding
104 try:
105     samr = samr.samr(binding)
106 except Exception, e:
107     print "Failed to connect to %s: %s" % (binding, e.message)
108     sys.exit(1)
109
110 handle = test_Connect(samr)
111 test_EnumDomains(samr, handle)
112 samr.Close(handle)
113
114 print "All OK"