r26591: Get the first bits of samba3dump to work again.
[ira/wip.git] / source / scripting / bin / samba3dump
1 #!/usr/bin/python
2 #
3 #       Dump Samba3 data
4 #       Copyright Jelmer Vernooij 2005-2007
5 #       Released under the GNU GPL v3 or later
6 #
7
8 import optparse
9 import os, sys
10 sys.path.append(os.path.join(os.path.dirname(__file__), "../python"))
11 import samba
12 import samba.samba3
13
14 parser = optparse.OptionParser("provision <libdir> [<smb.conf>]")
15 parser.add_option("--format", type="choice", metavar="FORMAT",
16                           choices=["full", "summary"])
17
18 opts, args = parser.parse_args()
19
20 if opts.format is None:
21         opts.format = "summary"
22
23 def print_header(txt):
24         print "\n%s" % txt
25         print "=========================================="
26
27 def print_samba3_policy(pol):
28         print_header("Account Policies")
29         print "Min password length: %d" % pol.min_password_length
30         print "Password history length: %d" % pol.password_history
31         if pol.user_must_logon_to_change_password:
32                 print "User must logon to change password: %d" % pol.user_must_logon_to_change_password
33         if pol.maximum_password_age:
34                 print "Maximum password age: %d" % pol.maximum_password_age
35         if pol.minimum_password_age:
36                 print "Minimum password age: %d" % pol.minimum_password_age
37         if pol.lockout_duration:
38                 print "Lockout duration: %d" % pol.lockout_duration
39         if pol.reset_count_minutes:
40                 print "Reset Count Minutes: %d" % pol.reset_count_minutes
41         if pol.bad_lockout_minutes:
42                 print "Bad Lockout Minutes: %d" % pol.bad_lockout_minutes
43         if pol.disconnect_time:
44                 print "Disconnect Time: %d" % pol.disconnect_time
45         if pol.refuse_machine_password_change:
46                 print "Refuse Machine Password Change: %d" % pol.refuse_machine_password_change
47
48 def print_samba3_sam(samba3):
49         print_header("SAM Database")
50         
51         for a in samba3.samaccounts:
52                 print "%d: %s" % a.user_rid, a.username
53
54 def print_samba3_shares(samba3):
55         print_header("Configured shares")
56         for s in samba3.shares:
57                 print "--- %s ---" % s.name
58
59                 for p in s.parameters:
60                         print "\t%s = %s" % (p.name, p.value)
61
62                 print ""
63
64 def print_samba3_secrets(secrets):
65         print_header("Secrets")
66
67         if secrets.get_auth_user():
68                 print "IPC Credentials:"
69                 if secrets.get_auth_user():
70                         print " User: %s\n" % secrets.get_auth_user()
71                 if secrets.get_auth_password():
72                         print " Password: %s\n" % secrets.get_auth_password()
73                 if secrets.get_auth_domain():
74                         print " Domain: %s\n" % secrets.get_auth_domain()
75
76         if len(list(secrets.ldap_dns())) > 0:
77                 print "LDAP passwords:"
78                 for dn in secrets.ldap_dns():
79                         print "\t%s -> %s" % (dn, secrets.get_ldap_bind_pw(dn))
80                 print ""
81
82         print "Domains:"
83         for domain in secrets.domains():
84                 print "\t--- %s ---" % domain
85                 print "\tSID: %s" % secrets.get_sid(domain)
86                 print "\tGUID: %s" % secrets.get_dom_guid(domain)
87                 print "\tPlaintext pwd: %s" % secrets.get_machine_password(domain)
88                 if secrets.get_machine_last_change_time(domain):
89                         print "\tLast Changed: %lu" % secrets.get_machine_last_change_time(domain)
90                 if secrets.get_machine_sec_channel_type(domain):
91                         print "\tSecure Channel Type: %d\n" % secrets.get_machine_sec_channel_type(domain)
92
93         print "Trusted domains:"
94         for td in secrets.trusted_domains():
95                 print td
96
97 def print_samba3_regdb(regdb):
98         print_header("Registry")
99
100         for k in regdb.keys():
101                 print "%s" % k
102                 for v in regdb.values(k):
103                         print "\t%s: type %d, length %d" % (v.name, v.type, v.data.length)
104
105 def print_samba3_winsdb(winsdb):
106         print_header("WINS Database")
107
108         for name in winsdb:
109                 (ttl, ips, nb_flags) = winsdb[name]
110                 print "%s, nb_flags: %s, ttl: %lu, %d ips, fst: %s" % (name, nb_flags, ttl, len(ips), ips[0])
111
112 def print_samba3_groupmappings(groupdb):
113         print_header("Group Mappings")
114         
115         for sid in groupdb.groupsids():
116                 print "\t--- Group: %s ---" % g.nt_name
117                 print "\tComment: %s" % g.comment
118                 print "\tGID: %d" % g.gid
119                 print "\tSID Name Use: %d" % g.sid_name_use
120                 print "\tSID: %s\n" % g.sid
121
122 def print_samba3_aliases(groupdb):
123         for a in groupdb.aliases:
124                 print "\t--- Alias: %s ---" % a.sid
125                 for m in a.members:
126                         print "\t%s" % m
127
128 def print_samba3_idmapdb(idmapdb):
129         print_header("Winbindd SID<->GID/UID mappings")
130
131         print "User High Water Mark: %d" % idmapdb.user_hwm
132         print "Group High Water Mark: %d\n" % idmapdb.group_hwm
133
134         for e in idmapdb.mappings:
135                 if e.type == e.IDMAP_GROUP:
136                         print "%s -> GID %d" % (e.sid, e.unix_id)
137                 else:
138                         print "%s -> UID %d" % (e.sid, e.unix_id)
139
140 def print_samba3(samba3):
141         print_samba3_policy(samba3.get_policy_db())
142         print_samba3_winsdb(samba3.get_wins_db())
143         print_samba3_regdb(samba3.get_registry())
144         print_samba3_secrets(samba3.get_secrets_db())
145         groupdb = samba3.get_groupmapping_db()
146         print_samba3_groupmappings(groupdb)
147         print_samba3_aliases(groupdb)
148         print_samba3_idmapdb(samba3.get_idmap_db())
149         print_samba3_shares(samba3)
150         print_samba3_sam(samba3)
151
152 def print_samba3_summary(samba3):
153         print "WINS db entries: %d" % len(samba3.get_wins_db())
154         print "Registry key count: %d" % len(samba3.get_registry())
155         groupdb = samba3.get_groupmapping_db()
156         print "Groupmap count: %d" % len(list(groupdb.groupsids()))
157         print "Alias count: %d" % len(list(groupdb.aliases()))
158         idmapdb = samba3.get_idmap_db()
159         print "Idmap count: %d" % (len(list(idmapdb.uids())) + len(list(idmapdb.gids())))
160
161 libdir = args[0]
162 if len(args) > 1:
163         smbconf = args[2]
164 else:
165         smbconf = os.path.join(libdir, "smb.conf")
166
167 samba3 = samba.samba3.Samba3(libdir, smbconf)
168
169 if opts.format == "summary":
170         print_samba3_summary(samba3)
171 elif opts.format == "full":
172         print_samba3(samba3)