ctdb-scripts: Move connection tracking to 10.interface
[samba4-gss.git] / source4 / scripting / bin / samba3dump
blob1a5d74fbf96d977afbb8a92259f9d13ec2e87388
1 #!/usr/bin/env python3
3 #    Dump Samba3 data
4 #    Copyright Jelmer Vernooij 2005-2007
5 #    Released under the GNU GPL v3 or later
8 import optparse
9 import os, sys
11 # Find right directory when running from source tree
12 sys.path.insert(0, "bin/python")
14 import samba
15 import samba.samba3
16 from samba.samba3 import param as s3param
17 from samba.dcerpc import lsa
19 parser = optparse.OptionParser("samba3dump <libdir> [<smb.conf>]")
20 parser.add_option("--format", type="choice", metavar="FORMAT",
21                   choices=["full", "summary"])
23 opts, args = parser.parse_args()
25 if opts.format is None:
26     opts.format = "summary"
28 def print_header(txt):
29     print("\n%s" % txt)
30     print("=" * len(txt))
32 def print_samba3_policy(pol):
33     print_header("Account Policies")
34     print("Min password length: %d" % pol['min password length'])
35     print("Password history length: %d" % pol['password history'])
36     if pol['user must logon to change password']:
37         print("User must logon to change password: %d" % pol['user must logon to change password'])
38     if pol['maximum password age']:
39         print("Maximum password age: %d" % pol['maximum password age'])
40     if pol['minimum password age']:
41         print("Minimum password age: %d" % pol['minimum password age'])
42     if pol['lockout duration']:
43         print("Lockout duration: %d" % pol['lockout duration'])
44     if pol['reset count minutes']:
45         print("Reset Count Minutes: %d" % pol['reset count minutes'])
46     if pol['bad lockout attempt']:
47         print("Bad Lockout Minutes: %d" % pol['bad lockout attempt'])
48     if pol['disconnect time']:
49         print("Disconnect Time: %d" % pol['disconnect time'])
50     if pol['refuse machine password change']:
51         print("Refuse Machine Password Change: %d" % pol['refuse machine password change'])
53 def print_samba3_sam(samdb):
54     print_header("SAM Database")
55     for user in samdb.search_users(0):
56         print("%s (%d): %s" % (user['account_name'], user['rid'], user['fullname']))
58 def print_samba3_shares(lp):
59     print_header("Configured shares")
60     for s in lp.services():
61         print("--- %s ---" % s)
62         for p in ['path']:
63             print("\t%s = %s" % (p, lp.get(p, s)))
64         print("")
66 def print_samba3_secrets(secrets):
67     print_header("Secrets")
69     if secrets.get_auth_user():
70         print("IPC Credentials:")
71         if secrets.get_auth_user():
72             print("    User: %s\n" % secrets.get_auth_user())
73         if secrets.get_auth_password():
74             print("    Password: %s\n" % secrets.get_auth_password())
75         if secrets.get_auth_domain():
76             print("    Domain: %s\n" % secrets.get_auth_domain())
78     if len(list(secrets.ldap_dns())) > 0:
79         print("LDAP passwords:")
80         for dn in secrets.ldap_dns():
81             print("\t%s -> %s" % (dn, secrets.get_ldap_bind_pw(dn)))
82         print("")
84     print("Domains:")
85     for domain in secrets.domains():
86         print("\t--- %s ---" % domain)
87         print("\tSID: %s" % secrets.get_sid(domain))
88         print("\tGUID: %s" % secrets.get_domain_guid(domain))
89         print("\tPlaintext pwd: %s" % secrets.get_machine_password(domain))
90         if secrets.get_machine_last_change_time(domain):
91             print("\tLast Changed: %lu" % secrets.get_machine_last_change_time(domain))
92         if secrets.get_machine_sec_channel_type(domain):
93             print("\tSecure Channel Type: %d\n" % secrets.get_machine_sec_channel_type(domain))
95     print("Trusted domains:")
96     for td in secrets.trusted_domains():
97         print(td)
99 def print_samba3_regdb(regdb):
100     print_header("Registry")
101     from samba.registry import str_regtype
103     for k in regdb.keys():
104         print("[%s]" % k)
105         for (value_name, (type, value))  in regdb.values(k).items():
106             print("\"%s\"=%s:%s" % (value_name, str_regtype(type), value))
108 def print_samba3_winsdb(winsdb):
109     print_header("WINS Database")
111     for name in winsdb:
112         (ttl, ips, nb_flags) = winsdb[name]
113         print("%s, nb_flags: %s, ttl: %lu, %d ips, fst: %s" % (name, nb_flags, ttl, len(ips), ips[0]))
115 def print_samba3_groupmappings(groupdb):
116     print_header("Group Mappings")
117     
118     for g in groupdb.enum_group_mapping(samba.samba3.passdb.get_global_sam_sid(),
119                                         lsa.SID_NAME_DOM_GRP):
120         print("\t--- Group: %s ---" % g.sid)
122 def print_samba3_aliases(groupdb):
123     for g in groupdb.enum_group_mapping(samba.samba3.passdb.get_global_sam_sid(),
124                                         lsa.SID_NAME_ALIAS):
125         print("\t--- Alias: %s ---" % g.sid)
127 def print_samba3_idmapdb(idmapdb):
128     print_header("Winbindd SID<->GID/UID mappings")
130     print("User High Water Mark: %d" % idmapdb.get_user_hwm())
131     print("Group High Water Mark: %d\n" % idmapdb.get_group_hwm())
133     for uid in idmapdb.uids():
134         print("%s -> UID %d" % (idmapdb.get_user_sid(uid), uid))
136     for gid in idmapdb.gids():
137         print("%s -> GID %d" % (idmapdb.get_group_sid(gid), gid))
139 def print_samba3(samba3):
140     passdb = samba3.get_sam_db()
141     print_samba3_policy(passdb.get_account_policy())
142     print_samba3_winsdb(samba3.get_wins_db())
143     print_samba3_regdb(samba3.get_registry())
144     print_samba3_secrets(samba3.get_secrets_db())
145     print_samba3_idmapdb(samba3.get_idmap_db())
146     print_samba3_sam(passdb)
147     print_samba3_groupmappings(passdb)
148     print_samba3_aliases(passdb)
149     print_samba3_shares(samba3.lp)
151 def print_samba3_summary(samba3):
152     print("WINS db entries: %d" % len(samba3.get_wins_db()))
153     print("Registry key count: %d" % len(samba3.get_registry()))
154     passdb = samba3.get_sam_db()
155     print("Groupmap count: %d" % len(passdb.enum_group_mapping()))
156     print("Alias count: %d" % len(passdb.search_aliases()))
157     idmapdb = samba3.get_idmap_db()
158     print("Idmap count: %d" % (len(list(idmapdb.uids())) + len(list(idmapdb.gids()))))
160 if len(args) < 1:
161     parser.print_help()
162     sys.exit(1)
164 libdir = args[0]
165 if len(args) < 1:
166     smbconf = args[1]
167 else:
168     smbconf = os.path.join(libdir, "smb.conf")
170 s3_lp = s3param.get_context()
171 s3_lp.set("private dir", libdir)
172 s3_lp.set("state directory", libdir)
173 s3_lp.set("lock directory", libdir)
174 s3_lp.load(smbconf)
175 samba3 = samba.samba3.Samba3(smbconf, s3_lp)
177 if opts.format == "summary":
178     print_samba3_summary(samba3)
179 elif opts.format == "full":
180     print_samba3(samba3)