r9722: Initial attempt at converting samba3dump to EJS..
[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 (ARGV.length != 3) {
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         for (i = 0; txt[i]; i++) putchar('=');
41         putchar('\n');
42 }
43
44 function print_samba3_policy(pol)
45 {
46         print_header("Account Policies");
47         printf("Min password length: %d\n", pol.min_password_length);
48         printf("Password history length: %d\n", pol.password_history);
49         printf("User must logon to change password: %d\n", pol.user_must_logon_to_change_password);
50         printf("Maximum password age: %d\n", pol.maximum_password_age);
51         printf("Minimum password age: %d\n", pol.minimum_password_age);
52         printf("Lockout duration: %d\n", pol.lockout_duration);
53         printf("Reset Count Minutes: %d\n", pol.reset_count_minutes);
54         printf("Bad Lockout Minutes: %d\n", pol.bad_lockout_minutes);
55         printf("Disconnect Time: %d\n", pol.disconnect_time);
56         printf("Refuse Machine Password Change: %d\n", pol.refuse_machine_password_change);
57 }
58
59 function print_samba3_sam(samba3)
60 {
61         print_header("SAM Database");
62         
63         for (i = 0; i < samba3.samaccount_count; i++) {
64                 printf("%d: %s\n", samba3.samaccounts[i].user_rid, samba3.samaccounts[i].username);
65         }
66 }
67
68 function print_samba3_shares(samba3)
69 {
70         print_header("Configured shares");
71         for (i = 0; i < samba3.share_count; i++) {
72                 printf("--- %s ---\n", samba3.shares[i].name);
73
74                 for (j = 0; j < samba3.shares[i].parameter_count; j++) {
75                         printf("\t%s = %s\n", samba3.shares[i].parameters[j].name, samba3.shares[i].parameters[j].value);
76                 }
77
78                 println("");
79         }
80 }
81
82 function print_samba3_secrets(secrets)
83 {
84         print_header("Secrets");
85
86         println("IPC Credentials:");
87         if (secrets.ipc_cred.username_obtained) 
88                 printf("        User: %s\n", secrets.ipc_cred.username);
89         if (secrets.ipc_cred.password_obtained)
90                 printf("        Password: %s\n", secrets.ipc_cred.password);
91
92         if (secrets.ipc_cred.domain_obtained)
93                 printf("        Domain: %s\n\n", secrets.ipc_cred.domain);
94
95         println("LDAP passwords:");
96         for (i = 0; i < secrets.ldappw_count; i++) {
97                 printf("\t%s -> %s\n", secrets.ldappws[i].dn, secrets.ldappws[i].password);
98         }
99         println("");
100
101         println("Domains:");
102         for (i = 0; i < secrets.domain_count; i++) {
103                 printf("\t--- %s ---\n", secrets.domains[i].name);
104                 printf("\tSID: %s\n", secrets.domains[i].sid);
105                 printf("\tGUID: %s\n", secrets.domains[i].guid);
106                 printf("\tPlaintext pwd: %s\n", secrets.domains[i].plaintext_pw);
107                 printf("\tLast Changed: %lu\n", secrets.domains[i].last_change_time);
108                 printf("\tSecure Channel Type: %d\n\n", secrets.domains[i].sec_channel_type);
109         }
110
111         println("Trusted domains:");
112         for (i = 0; i < secrets.trusted_domain_count; i++) {
113                 for (j = 0; j < secrets.trusted_domains[i].uni_name_len; j++) {
114                         printf("\t--- %s ---\n", secrets.trusted_domains[i].uni_name[j]);
115                 }
116                 printf("\tPassword: %s\n", secrets.trusted_domains[i].pass);
117                 printf("\tModified: %lu\n", secrets.trusted_domains[i].mod_time);
118                 printf("\tSID: %s\n", secrets.trusted_domains[i].domain_sid);
119         }
120 }
121
122 function print_samba3_regdb(regdb)
123 {
124         print_header("Registry");
125
126         for (i = 0; i < regdb.key_count; i++) {
127                 printf("%s\n", regdb.keys[i].name);
128                 for (j = 0; j < regdb.keys[i].value_count; j++) {
129                         printf("\t%s: type %d, length %d\n", 
130                                    regdb.keys[i].values[j].name,
131                                    regdb.keys[i].values[j].type,
132                                    regdb.keys[i].values[j].data.length);
133                 }
134         }
135 }
136
137 function print_samba3_winsdb(samba3)
138 {
139         print_header("WINS Database");
140
141         for (i = 0; i < samba3.winsdb_count; i++) {
142                 printf("%s, nb_flags: %x, type: %d, ttl: %lu, %d ips\n", samba3.winsdb_entries[i].name, samba3.winsdb_entries[i].nb_flags, samba3.winsdb_entries[i].type, samba3.winsdb_entries[i].ttl, samba3.winsdb_entries[i].ip_count);
143         }
144 }
145
146 function print_samba3_groupdb(groupdb)
147 {
148         int i;
149         print_header("Group Mappings");
150         
151         for (i = 0; i < groupdb.groupmap_count; i++) 
152         {
153                 printf("\t--- Group: %s ---\n", groupdb.groupmappings[i].nt_name);
154                 printf("\tComment: %s\n", groupdb.groupmappings[i].comment);
155                 printf("\tGID: %d\n", groupdb.groupmappings[i].gid);
156                 printf("\tSID Name Use: %d\n", groupdb.groupmappings[i].sid_name_use);
157                 printf("\tSID: %s\n\n", groupdb.groupmappings[i].sid);
158         }
159
160         for (i = 0; i < groupdb.alias_count; i++)
161         {
162                 int j;
163                 printf("\t--- Alias: %s ---\n", groupdb.aliases[i].sid);
164                 for (j = 0; j < groupdb.aliases[i].member_count; j++) {
165                         printf("\t%s\n", groupdb.aliases[i].members[j]);
166                 }
167         }
168 }
169
170 function print_samba3_idmapdb(idmapdb)
171 {
172         print_header("Winbindd SID<->GID/UID mappings");
173
174         printf("User High Water Mark: %d\n", idmapdb.user_hwm);
175         printf("Group High Water Mark: %d\n\n", idmapdb.group_hwm);
176
177         for (i = 0; i < idmapdb.mapping_count; i++) {
178                 printf("%s -> ", 
179                           idmapdb.mappings[i].sid);
180
181                 if (idmapdb.mappings[i].type == IDMAP_GROUP) { 
182                         printf("GID %d", idmapdb.mappings[i].unix_id);
183                 } else {
184                         printf("UID %d", idmapdb.mappings[i].unix_id);
185                 }
186         }
187 }
188
189 function print_samba3(samba3)
190 {
191         print_samba3_sam(samba3);
192         print_samba3_policy(samba3.policy);
193         print_samba3_shares(samba3);
194         print_samba3_winsdb(samba3);
195         print_samba3_regdb(samba3.registry);
196         print_samba3_secrets(samba3.secrets);
197         print_samba3_groupdb(samba3.group);
198         print_samba3_idmapdb(samba3.idmap);
199 }
200
201 function print_samba3_summary(samba3)
202 {
203         printf("WINS db entries: %d\n", samba3.winsdb_count);
204         printf("SAM Accounts: %d\n", samba3.samaccount_count);
205         printf("Registry key count: %d\n", samba3.registry.key_count);
206         printf("Shares (including [global]): %d\n", samba3.share_count);
207         printf("Groupmap count: %d\n", samba3.group.groupmap_count);
208         printf("Alias count: %d\n", samba3.group.alias_count);
209         printf("Idmap count: %d\n", samba3.idmap.mapping_count);
210 }
211
212 samba3 = samba3_read(ARGV[1], ARGV[2]);
213
214 if (options.format == "summary") {
215         print_samba3_summary(samba3);
216 } else if (options.format == "full") {
217         print_samba3(samba3);
218 }
219
220 return 0;