r9724: Rewrite samba3dump in JS. The summary works now, but the full output
[sfrench/samba-autobuild/.git] / source4 / scripting / bin / samba3dump
1 #!/bin/sh
2 exec smbscript "$0" ${1+"$@"}
3 /*
4         Dump Samba3 data
5         Copyright Jelmer Vernooij 2005
6         Released under the GNU GPL v2 or later
7 */
8
9 options = GetOptions(ARGV,
10                 "POPT_AUTOHELP",
11                 "POPT_COMMON_SAMBA",
12                 "POPT_COMMON_VERSION",
13                 'format=s',
14                 'quiet', 'blank');
15
16 if (options == undefined) {
17    println("Failed to parse options");
18    return -1;
19 }
20
21 if (options.format == undefined) {
22         options.format = "summary";
23 }
24
25 if (options.format != "summary" && options.format != "full") {
26         printf("Unknown format %s\n", options.format);
27         return -1;
28 }
29
30 libinclude("base.js");
31
32 if (options.ARGV.length != 2) {
33         println("Usage: samba3dump <libdir> <smb.conf>");
34         return -1;
35 }
36
37 function print_header(txt)
38 {
39         printf("\n%s\n", txt);
40         println("==========================================");
41 }
42
43 function print_samba3_policy(pol)
44 {
45         print_header("Account Policies");
46         printf("Min password length: %d\n", pol.min_password_length);
47         printf("Password history length: %d\n", pol.password_history);
48         printf("User must logon to change password: %d\n", pol.user_must_logon_to_change_password);
49         printf("Maximum password age: %d\n", pol.maximum_password_age);
50         printf("Minimum password age: %d\n", pol.minimum_password_age);
51         printf("Lockout duration: %d\n", pol.lockout_duration);
52         printf("Reset Count Minutes: %d\n", pol.reset_count_minutes);
53         printf("Bad Lockout Minutes: %d\n", pol.bad_lockout_minutes);
54         printf("Disconnect Time: %d\n", pol.disconnect_time);
55         printf("Refuse Machine Password Change: %d\n", pol.refuse_machine_password_change);
56 }
57
58 function print_samba3_sam(samba3)
59 {
60         print_header("SAM Database");
61         
62         for (a in samba3.samaccounts) {
63                 printf("%d: %s\n", a.user_rid, a.username);
64         }
65 }
66
67 function print_samba3_shares(samba3)
68 {
69         print_header("Configured shares");
70         for (s in samba3.shares) {
71                 printf("--- %s ---\n", s.name);
72
73                 for (p in s.parameters) {
74                         printf("\t%s = %s\n", p.name, p.value);
75                 }
76
77                 println("");
78         }
79 }
80
81 function print_samba3_secrets(secrets)
82 {
83         print_header("Secrets");
84
85         println("IPC Credentials:");
86         if (secrets.ipc_cred.username_obtained) 
87                 printf("        User: %s\n", secrets.ipc_cred.get_username);
88         if (secrets.ipc_cred.password_obtained)
89                 printf("        Password: %s\n", secrets.ipc_cred.get_password);
90
91         if (secrets.ipc_cred.domain_obtained)
92                 printf("        Domain: %s\n\n", secrets.ipc_cred.get_domain);
93
94         println("LDAP passwords:");
95         for (pw in secrets.ldappws) {
96                 printf("\t%s -> %s\n", pw.dn, pw.password);
97         }
98         println("");
99
100         println("Domains:");
101         for (d in secrets.domains) {
102                 printf("\t--- %s ---\n", d.name);
103                 printf("\tSID: %s\n", d.sid);
104                 printf("\tGUID: %s\n", d.guid);
105                 printf("\tPlaintext pwd: %s\n", d.plaintext_pw);
106                 printf("\tLast Changed: %lu\n", d.last_change_time);
107                 printf("\tSecure Channel Type: %d\n\n", d.sec_channel_type);
108         }
109
110         println("Trusted domains:");
111         for (td in secrets.trusted_domains) {
112                 for (j = 0; j < td.uni_name_len; j++) {
113                         printf("\t--- %s ---\n", td.uni_name[j]);
114                 }
115                 printf("\tPassword: %s\n", td.pass);
116                 printf("\tModified: %lu\n", td.mod_time);
117                 printf("\tSID: %s\n", td.domain_sid);
118         }
119 }
120
121 function print_samba3_regdb(regdb)
122 {
123         print_header("Registry");
124
125         for (k in regdb.keys) {
126                 printf("%s\n", k.name);
127                 for (v in k.values) {
128                         printf("\t%s: type %d, length %d\n", v.name, v.type, v.data.length);
129                 }
130         }
131 }
132
133 function print_samba3_winsdb(samba3)
134 {
135         print_header("WINS Database");
136
137         for (e in samba3.winsentries) {
138                 printf("%s, nb_flags: %x, type: %d, ttl: %lu, %d ips\n", e.name, e.nb_flags, e.type, e.ttl, e.ip_count);
139         }
140 }
141
142 function print_samba3_groupmappings(groupdb)
143 {
144         int i;
145         print_header("Group Mappings");
146         
147         for (g in groupdb.groupmappings) {
148                 printf("\t--- Group: %s ---\n", g.nt_name);
149                 printf("\tComment: %s\n", g.comment);
150                 printf("\tGID: %d\n", g.gid);
151                 printf("\tSID Name Use: %d\n", g.sid_name_use);
152                 printf("\tSID: %s\n\n", g.sid);
153         }
154 }
155
156 function print_samba3_aliases(groupdb)
157 {
158         for (a in groupdb.aliases) {
159                 int j;
160                 printf("\t--- Alias: %s ---\n", a.sid);
161                 for (j = 0; j < a.member_count; j++) {
162                         printf("\t%s\n", a.members[j]);
163                 }
164         }
165 }
166
167 function print_samba3_idmapdb(idmapdb)
168 {
169         print_header("Winbindd SID<->GID/UID mappings");
170
171         printf("User High Water Mark: %d\n", idmapdb.user_hwm);
172         printf("Group High Water Mark: %d\n\n", idmapdb.group_hwm);
173
174         for (e in idmapdb.mappings) {
175                 printf("%s -> ", e.sid);
176
177                 if (e.type == IDMAP_GROUP) { 
178                         printf("GID %d", e.unix_id);
179                 } else {
180                         printf("UID %d", e.unix_id);
181                 }
182         }
183 }
184
185 function print_samba3(samba3)
186 {
187         print_samba3_sam(samba3);
188         print_samba3_policy(samba3.policy);
189         print_samba3_shares(samba3);
190         print_samba3_winsdb(samba3);
191         print_samba3_regdb(samba3.registry);
192         print_samba3_secrets(samba3.secrets);
193         print_samba3_groupmappings(samba3);
194         print_samba3_aliases(samba3);
195         print_samba3_idmapdb(samba3.idmap);
196 }
197
198 function print_samba3_summary(samba3)
199 {
200         printf("WINS db entries: %d\n", samba3.winsentries.length);
201         printf("SAM Accounts: %d\n", samba3.samaccounts.length);
202         printf("Registry key count: %d\n", samba3.registry.keys.length);
203         printf("Shares (including [global]): %d\n", samba3.shares.length);
204         printf("Groupmap count: %d\n", samba3.groupmappings.length);
205         printf("Alias count: %d\n", samba3.aliases.length);
206         printf("Idmap count: %d\n", samba3.idmapdb.mappings.length);
207 }
208
209 samba3 = samba3_read(options.ARGV[0], options.ARGV[1]);
210
211 if (options.format == "summary") {
212         print_samba3_summary(samba3);
213 } else if (options.format == "full") {
214         print_samba3(samba3);
215 }
216
217 return 0;