b00074be56ec9bf8fb86f0b47f62cfc7431ae28b
[jra/samba/.git] / source4 / lib / samba3 / samba3dump.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba3 database dump utility
4
5     Copyright (C) Jelmer Vernooij       2005
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "lib/samba3/samba3.h"
24 #include "lib/cmdline/popt_common.h"
25
26 static void print_header(const char *txt)
27 {
28         int i;
29         printf("\n%s\n", txt);
30         for (i = 0; txt[i]; i++) putchar('=');
31         putchar('\n');
32 }
33
34 static NTSTATUS print_samba3_policy(struct samba3_policy *ret)
35 {
36         print_header("Account Policies");
37         printf("Min password length: %d\n", ret->min_password_length);
38         printf("Password history length: %d\n", ret->password_history);
39         printf("User must logon to change password: %d\n", ret->user_must_logon_to_change_password);
40         printf("Maximum password age: %d\n", ret->maximum_password_age);
41         printf("Minimum password age: %d\n", ret->minimum_password_age);
42         printf("Lockout duration: %d\n", ret->lockout_duration);
43         printf("Reset Count Minutes: %d\n", ret->reset_count_minutes);
44         printf("Bad Lockout Minutes: %d\n", ret->bad_lockout_minutes);
45         printf("Disconnect Time: %d\n", ret->disconnect_time);
46         printf("Refuse Machine Password Change: %d\n", ret->refuse_machine_password_change);
47
48         return NT_STATUS_OK;
49 }
50
51 static NTSTATUS print_samba3_sam(struct samba3 *samba3)
52 {
53         struct samba3_samaccount *accounts = samba3->samaccounts;
54         uint32_t count = samba3->samaccount_count, i;
55         print_header("SAM Database");
56         
57         for (i = 0; i < count; i++) {
58                 printf("%d: %s\n", accounts[i].user_rid, accounts[i].username);
59         }
60
61         return NT_STATUS_OK;
62 }
63
64 static NTSTATUS print_samba3_shares(struct samba3 *samba3)
65 {
66         int i, j;
67         print_header("Configured shares");
68         for (i = 0; i < samba3->share_count; i++) {
69                 struct samba3_share_info *share = &samba3->shares[i];
70                 printf("--- %s ---\n", share->name);
71
72                 for (j = 0; j < share->parameter_count; j++) {
73                         printf("\t%s = %s\n", share->parameters[j].name, share->parameters[j].value);
74                 }
75
76                 printf("\n");
77         }
78
79         return NT_STATUS_OK;
80 }
81
82 static NTSTATUS print_samba3_secrets(struct samba3_secrets *secrets)
83 {
84         int i;
85         print_header("Secrets");
86
87         printf("IPC Credentials:\n");
88         printf("        User: %s\n", cli_credentials_get_username(secrets->ipc_cred));
89         printf("        Password: %s\n", cli_credentials_get_password(secrets->ipc_cred));
90         printf("        Domain: %s\n\n", cli_credentials_get_domain(secrets->ipc_cred));
91
92         printf("LDAP passwords:\n");
93         for (i = 0; i < secrets->ldappw_count; i++) {
94                 printf("\t%s -> %s\n", secrets->ldappws[i].dn, secrets->ldappws[i].password);
95         }
96         printf("\n");
97
98         printf("Domains:\n");
99         for (i = 0; i < secrets->domain_count; i++) {
100                 printf("\t--- %s ---\n", secrets->domains[i].name);
101                 printf("\tSID: %s\n", dom_sid_string(NULL, &secrets->domains[i].sid));
102                 printf("\tGUID: %s\n", GUID_string(NULL, &secrets->domains[i].guid));
103                 printf("\tPlaintext pwd: %s\n", secrets->domains[i].plaintext_pw);
104                 printf("\tLast Changed: %lu\n", secrets->domains[i].last_change_time);
105                 printf("\tSecure Channel Type: %d\n\n", secrets->domains[i].sec_channel_type);
106         }
107
108         printf("Trusted domains:\n");
109         for (i = 0; i < secrets->trusted_domain_count; i++) {
110                 int j;
111                 for (j = 0; j < secrets->trusted_domains[i].uni_name_len; j++) {
112                         printf("\t--- %s ---\n", secrets->trusted_domains[i].uni_name[j]);
113                 }
114                 printf("\tPassword: %s\n", secrets->trusted_domains[i].pass);
115                 printf("\tModified: %lu\n", secrets->trusted_domains[i].mod_time);
116                 printf("\tSID: %s\n", dom_sid_string(NULL, &secrets->trusted_domains[i].domain_sid));
117         }
118
119         return NT_STATUS_OK;
120 }
121
122 static NTSTATUS print_samba3_regdb(struct samba3_regdb *regdb)
123 {
124         int i;
125         print_header("Registry");
126
127         for (i = 0; i < regdb->key_count; i++) {
128                 int j;
129                 printf("%s\n", regdb->keys[i].name);
130                 for (j = 0; j < regdb->keys[i].value_count; j++) {
131                         printf("\t%s: type %d, length %d\n", 
132                                    regdb->keys[i].values[j].name,
133                                    regdb->keys[i].values[j].type,
134                                    regdb->keys[i].values[j].data.length);
135                 }
136         }
137
138         return NT_STATUS_OK;
139 }
140
141 static NTSTATUS print_samba3_winsdb(struct samba3 *samba3)
142 {
143         int i;
144         print_header("WINS Database");
145
146         for (i = 0; i < samba3->winsdb_count; i++) {
147                 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);
148         }
149
150         return NT_STATUS_OK;
151 }
152
153 static NTSTATUS print_samba3(struct samba3 *samba3)
154 {
155         print_samba3_sam(samba3);
156         print_samba3_policy(&samba3->policy);
157         print_samba3_shares(samba3);
158         print_samba3_winsdb(samba3);
159         print_samba3_regdb(&samba3->registry);
160         print_samba3_secrets(&samba3->secrets);
161
162         return NT_STATUS_OK;
163 }
164  
165 int main(int argc, char **argv)
166 {
167         int opt;
168         const char *format = "summary";
169         const char *libdir = "/var/lib/samba";
170         struct samba3 *samba3;
171         poptContext pc;
172         struct poptOption long_options[] = {
173                 POPT_AUTOHELP
174                 { "format", 0, POPT_ARG_STRING, &format, 'f', "Format to use (one of: summary, text, ldif)" },
175                 { "libdir", 0, POPT_ARG_STRING, &libdir, 'l', "Set libdir [/var/lib/samba]", "LIBDIR" },
176                 POPT_COMMON_SAMBA
177                 POPT_TABLEEND
178         };
179
180         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
181
182         poptSetOtherOptionHelp(pc, "<smb.conf>");
183
184         while((opt = poptGetNextOpt(pc)) != -1) {
185         }
186
187         samba3_read(poptGetArg(pc), libdir, NULL, &samba3);
188
189         if (!strcmp(format, "summary")) {
190                 printf("WINS db entries: %d\n", samba3->winsdb_count);
191                 printf("SAM Accounts: %d\n", samba3->samaccount_count);
192                 printf("Registry key count: %d\n", samba3->registry.key_count);
193                 printf("Shares (including [global]): %d\n", samba3->share_count);
194                 printf("Groupmap count: %d\n", samba3->group.groupmap_count);
195                 printf("Alias count: %d\n", samba3->group.alias_count);
196                 printf("Idmap count: %d\n", samba3->idmap.mapping_count);
197         } else if (!strcmp(format, "text")) {
198                 print_samba3(samba3);
199         } else if (!strcmp(format, "ldif")) {
200                 printf("FIXME\n");
201         }
202         poptFreeContext(pc);
203
204         return 0;
205 }