r26592: Finish fixing the samba3dump script.
[jelmer/samba4-debian.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 "=" * len(txt)
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(samdb):
49     print_header("SAM Database")
50     
51     for user in samdb:
52         print "%s" % user
53
54 def print_samba3_shares(shares):
55     print_header("Configured shares")
56     for s in shares:
57         print "--- %s ---" % s.name
58
59         for p in s:
60             print "\t%s = %s" % (p.key, 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 ---" % sid
117
118 def print_samba3_aliases(groupdb):
119     for sid in groupdb.aliases():
120         print "\t--- Alias: %s ---" % sid
121
122 def print_samba3_idmapdb(idmapdb):
123     print_header("Winbindd SID<->GID/UID mappings")
124
125     print "User High Water Mark: %d" % idmapdb.get_user_hwm()
126     print "Group High Water Mark: %d\n" % idmapdb.get_group_hwm()
127
128     for uid in idmapdb.uids():
129         print "%s -> UID %d" % (idmapdb.get_user_sid(uid), uid)
130
131     for gid in idmapdb.gids():
132         print "%s -> GID %d" % (idmapdb.get_group_sid(gid), gid)
133
134 def print_samba3(samba3):
135     print_samba3_policy(samba3.get_policy_db())
136     print_samba3_winsdb(samba3.get_wins_db())
137     print_samba3_regdb(samba3.get_registry())
138     print_samba3_secrets(samba3.get_secrets_db())
139     print_samba3_idmapdb(samba3.get_idmap_db())
140     print_samba3_sam(samba3.get_sam_db())
141     groupdb = samba3.get_groupmapping_db()
142     print_samba3_groupmappings(groupdb)
143     print_samba3_aliases(groupdb)
144     print_samba3_shares(samba3.get_shares())
145
146 def print_samba3_summary(samba3):
147     print "WINS db entries: %d" % len(samba3.get_wins_db())
148     print "Registry key count: %d" % len(samba3.get_registry())
149     groupdb = samba3.get_groupmapping_db()
150     print "Groupmap count: %d" % len(list(groupdb.groupsids()))
151     print "Alias count: %d" % len(list(groupdb.aliases()))
152     idmapdb = samba3.get_idmap_db()
153     print "Idmap count: %d" % (len(list(idmapdb.uids())) + len(list(idmapdb.gids())))
154
155 libdir = args[0]
156 if len(args) > 1:
157     smbconf = args[2]
158 else:
159     smbconf = os.path.join(libdir, "smb.conf")
160
161 samba3 = samba.samba3.Samba3(libdir, smbconf)
162
163 if opts.format == "summary":
164     print_samba3_summary(samba3)
165 elif opts.format == "full":
166     print_samba3(samba3)