r26687: python: Update status after feedback from abartlett.
[samba.git] / source4 / 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     for user in samdb:
51         print "%s" % user
52
53 def print_samba3_shares(shares):
54     print_header("Configured shares")
55     for s in shares:
56         print "--- %s ---" % s.name
57         for p in s:
58             print "\t%s = %s" % (p.key, p.value)
59         print ""
60
61 def print_samba3_secrets(secrets):
62     print_header("Secrets")
63
64     if secrets.get_auth_user():
65         print "IPC Credentials:"
66         if secrets.get_auth_user():
67             print "    User: %s\n" % secrets.get_auth_user()
68         if secrets.get_auth_password():
69             print "    Password: %s\n" % secrets.get_auth_password()
70         if secrets.get_auth_domain():
71             print "    Domain: %s\n" % secrets.get_auth_domain()
72
73     if len(list(secrets.ldap_dns())) > 0:
74         print "LDAP passwords:"
75         for dn in secrets.ldap_dns():
76             print "\t%s -> %s" % (dn, secrets.get_ldap_bind_pw(dn))
77         print ""
78
79     print "Domains:"
80     for domain in secrets.domains():
81         print "\t--- %s ---" % domain
82         print "\tSID: %s" % secrets.get_sid(domain)
83         print "\tGUID: %s" % secrets.get_domain_guid(domain)
84         print "\tPlaintext pwd: %s" % secrets.get_machine_password(domain)
85         if secrets.get_machine_last_change_time(domain):
86             print "\tLast Changed: %lu" % secrets.get_machine_last_change_time(domain)
87         if secrets.get_machine_sec_channel_type(domain):
88             print "\tSecure Channel Type: %d\n" % secrets.get_machine_sec_channel_type(domain)
89
90     print "Trusted domains:"
91     for td in secrets.trusted_domains():
92         print td
93
94 def print_samba3_regdb(regdb):
95     print_header("Registry")
96     from registry import str_regtype
97
98     for k in regdb.keys():
99         print "[%s]" % k
100         for (value_name, (type, value))  in regdb.values(k).items():
101             print "\"%s\"=%s:%s" % (value_name, str_regtype(type), value)
102
103 def print_samba3_winsdb(winsdb):
104     print_header("WINS Database")
105
106     for name in winsdb:
107         (ttl, ips, nb_flags) = winsdb[name]
108         print "%s, nb_flags: %s, ttl: %lu, %d ips, fst: %s" % (name, nb_flags, ttl, len(ips), ips[0])
109
110 def print_samba3_groupmappings(groupdb):
111     print_header("Group Mappings")
112     
113     for sid in groupdb.groupsids():
114         print "\t--- Group: %s ---" % sid
115
116 def print_samba3_aliases(groupdb):
117     for sid in groupdb.aliases():
118         print "\t--- Alias: %s ---" % sid
119
120 def print_samba3_idmapdb(idmapdb):
121     print_header("Winbindd SID<->GID/UID mappings")
122
123     print "User High Water Mark: %d" % idmapdb.get_user_hwm()
124     print "Group High Water Mark: %d\n" % idmapdb.get_group_hwm()
125
126     for uid in idmapdb.uids():
127         print "%s -> UID %d" % (idmapdb.get_user_sid(uid), uid)
128
129     for gid in idmapdb.gids():
130         print "%s -> GID %d" % (idmapdb.get_group_sid(gid), gid)
131
132 def print_samba3(samba3):
133     print_samba3_policy(samba3.get_policy_db())
134     print_samba3_winsdb(samba3.get_wins_db())
135     print_samba3_regdb(samba3.get_registry())
136     print_samba3_secrets(samba3.get_secrets_db())
137     print_samba3_idmapdb(samba3.get_idmap_db())
138     print_samba3_sam(samba3.get_sam_db())
139     groupdb = samba3.get_groupmapping_db()
140     print_samba3_groupmappings(groupdb)
141     print_samba3_aliases(groupdb)
142     print_samba3_shares(samba3.get_shares())
143
144 def print_samba3_summary(samba3):
145     print "WINS db entries: %d" % len(samba3.get_wins_db())
146     print "Registry key count: %d" % len(samba3.get_registry())
147     groupdb = samba3.get_groupmapping_db()
148     print "Groupmap count: %d" % len(list(groupdb.groupsids()))
149     print "Alias count: %d" % len(list(groupdb.aliases()))
150     idmapdb = samba3.get_idmap_db()
151     print "Idmap count: %d" % (len(list(idmapdb.uids())) + len(list(idmapdb.gids())))
152
153 libdir = args[0]
154 if len(args) > 1:
155     smbconf = args[1]
156 else:
157     smbconf = os.path.join(libdir, "smb.conf")
158
159 samba3 = samba.samba3.Samba3(libdir, smbconf)
160
161 if opts.format == "summary":
162     print_samba3_summary(samba3)
163 elif opts.format == "full":
164     print_samba3(samba3)