r9712: Bunch of small fixes
[sfrench/samba-autobuild/.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 #include "lib/ldb/include/ldb.h"
26
27 static void print_header(const char *txt)
28 {
29         int i;
30         printf("\n%s\n", txt);
31         for (i = 0; txt[i]; i++) putchar('=');
32         putchar('\n');
33 }
34
35 static NTSTATUS print_samba3_policy(struct samba3_policy *ret)
36 {
37         print_header("Account Policies");
38         printf("Min password length: %d\n", ret->min_password_length);
39         printf("Password history length: %d\n", ret->password_history);
40         printf("User must logon to change password: %d\n", ret->user_must_logon_to_change_password);
41         printf("Maximum password age: %d\n", ret->maximum_password_age);
42         printf("Minimum password age: %d\n", ret->minimum_password_age);
43         printf("Lockout duration: %d\n", ret->lockout_duration);
44         printf("Reset Count Minutes: %d\n", ret->reset_count_minutes);
45         printf("Bad Lockout Minutes: %d\n", ret->bad_lockout_minutes);
46         printf("Disconnect Time: %d\n", ret->disconnect_time);
47         printf("Refuse Machine Password Change: %d\n", ret->refuse_machine_password_change);
48
49         return NT_STATUS_OK;
50 }
51
52 static NTSTATUS print_samba3_sam(struct samba3 *samba3)
53 {
54         struct samba3_samaccount *accounts = samba3->samaccounts;
55         uint32_t i;
56         print_header("SAM Database");
57         
58         for (i = 0; i < samba3->samaccount_count; i++) {
59                 printf("%d: %s\n", accounts[i].user_rid, accounts[i].username);
60         }
61
62         return NT_STATUS_OK;
63 }
64
65 static NTSTATUS print_samba3_shares(struct samba3 *samba3)
66 {
67         int i, j;
68         print_header("Configured shares");
69         for (i = 0; i < samba3->share_count; i++) {
70                 struct samba3_share_info *share = &samba3->shares[i];
71                 printf("--- %s ---\n", share->name);
72
73                 for (j = 0; j < share->parameter_count; j++) {
74                         printf("\t%s = %s\n", share->parameters[j].name, share->parameters[j].value);
75                 }
76
77                 printf("\n");
78         }
79
80         return NT_STATUS_OK;
81 }
82
83 static NTSTATUS print_samba3_secrets(struct samba3_secrets *secrets)
84 {
85         int i;
86         print_header("Secrets");
87
88         printf("IPC Credentials:\n");
89         if (secrets->ipc_cred->username_obtained) 
90                 printf("        User: %s\n", cli_credentials_get_username(secrets->ipc_cred));
91         if (secrets->ipc_cred->password_obtained)
92                 printf("        Password: %s\n", cli_credentials_get_password(secrets->ipc_cred));
93
94         if (secrets->ipc_cred->domain_obtained)
95                 printf("        Domain: %s\n\n", cli_credentials_get_domain(secrets->ipc_cred));
96
97         printf("LDAP passwords:\n");
98         for (i = 0; i < secrets->ldappw_count; i++) {
99                 printf("\t%s -> %s\n", secrets->ldappws[i].dn, secrets->ldappws[i].password);
100         }
101         printf("\n");
102
103         printf("Domains:\n");
104         for (i = 0; i < secrets->domain_count; i++) {
105                 printf("\t--- %s ---\n", secrets->domains[i].name);
106                 printf("\tSID: %s\n", dom_sid_string(NULL, &secrets->domains[i].sid));
107                 printf("\tGUID: %s\n", GUID_string(NULL, &secrets->domains[i].guid));
108                 printf("\tPlaintext pwd: %s\n", secrets->domains[i].plaintext_pw);
109                 printf("\tLast Changed: %lu\n", secrets->domains[i].last_change_time);
110                 printf("\tSecure Channel Type: %d\n\n", secrets->domains[i].sec_channel_type);
111         }
112
113         printf("Trusted domains:\n");
114         for (i = 0; i < secrets->trusted_domain_count; i++) {
115                 int j;
116                 for (j = 0; j < secrets->trusted_domains[i].uni_name_len; j++) {
117                         printf("\t--- %s ---\n", secrets->trusted_domains[i].uni_name[j]);
118                 }
119                 printf("\tPassword: %s\n", secrets->trusted_domains[i].pass);
120                 printf("\tModified: %lu\n", secrets->trusted_domains[i].mod_time);
121                 printf("\tSID: %s\n", dom_sid_string(NULL, &secrets->trusted_domains[i].domain_sid));
122         }
123
124         return NT_STATUS_OK;
125 }
126
127 static NTSTATUS print_samba3_regdb(struct samba3_regdb *regdb)
128 {
129         int i;
130         print_header("Registry");
131
132         for (i = 0; i < regdb->key_count; i++) {
133                 int j;
134                 printf("%s\n", regdb->keys[i].name);
135                 for (j = 0; j < regdb->keys[i].value_count; j++) {
136                         printf("\t%s: type %d, length %d\n", 
137                                    regdb->keys[i].values[j].name,
138                                    regdb->keys[i].values[j].type,
139                                    regdb->keys[i].values[j].data.length);
140                 }
141         }
142
143         return NT_STATUS_OK;
144 }
145
146 static NTSTATUS print_samba3_winsdb(struct samba3 *samba3)
147 {
148         int i;
149         print_header("WINS Database");
150
151         for (i = 0; i < samba3->winsdb_count; i++) {
152                 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);
153         }
154
155         return NT_STATUS_OK;
156 }
157
158 static NTSTATUS print_samba3_groupdb(struct samba3_groupdb *db)
159 {
160         int i;
161         print_header("Group Mappings");
162         
163         for (i = 0; i < db->groupmap_count; i++) 
164         {
165                 printf("\t--- Group: %s ---\n", db->groupmappings[i].nt_name);
166                 printf("\tComment: %s\n", db->groupmappings[i].comment);
167                 printf("\tGID: %d\n", db->groupmappings[i].gid);
168                 printf("\tSID Name Use: %d\n", db->groupmappings[i].sid_name_use);
169                 printf("\tSID: %s\n\n", dom_sid_string(NULL, db->groupmappings[i].sid));
170         }
171
172         for (i = 0; i < db->alias_count; i++)
173         {
174                 int j;
175                 printf("\t--- Alias: %s ---\n", dom_sid_string(NULL, db->aliases[i].sid));
176                 for (j = 0; j < db->aliases[i].member_count; j++) {
177                         printf("\t%s\n", dom_sid_string(NULL,db->aliases[i].members[j]));
178                 }
179         }
180
181         return NT_STATUS_OK;
182 }
183
184 static NTSTATUS print_samba3_idmapdb(struct samba3_idmapdb *db)
185 {
186         int i;
187         print_header("Winbindd SID<->GID/UID mappings");
188
189         printf("User High Water Mark: %d\n", db->user_hwm);
190         printf("Group High Water Mark: %d\n\n", db->group_hwm);
191
192         for (i = 0; i < db->mapping_count; i++) {
193                 printf("%s -> %cID %d", 
194                           dom_sid_string(NULL, db->mappings[i].sid),
195                           (db->mappings[i].type == IDMAP_GROUP)?'G':'U',
196                           db->mappings[i].unix_id);
197         }
198
199         return NT_STATUS_OK;
200 }
201
202 static NTSTATUS print_samba3(struct samba3 *samba3)
203 {
204         print_samba3_sam(samba3);
205         print_samba3_policy(&samba3->policy);
206         print_samba3_shares(samba3);
207         print_samba3_winsdb(samba3);
208         print_samba3_regdb(&samba3->registry);
209         print_samba3_secrets(&samba3->secrets);
210         print_samba3_groupdb(&samba3->group);
211         print_samba3_idmapdb(&samba3->idmap);
212
213         return NT_STATUS_OK;
214 }
215
216 static BOOL write_ldif(const char *fn, struct ldb_message **messages, int count)
217 {
218         FILE *f = fopen(fn, "w+");
219         struct ldb_ldif ldif;
220         int i;
221         struct ldb_context *ldb = ldb_init(NULL);
222
223         if (!f) {
224                 DEBUG(0, ("Unable to open LDIF file '%s'\n", fn));
225                 talloc_free(ldb);
226                 return False;
227         }
228
229         for (i = 0; i < count; i++) {
230                 ldif.changetype = LDB_CHANGETYPE_ADD;
231                 ldif.msg = messages[i];
232
233                 ldb_ldif_write_file(ldb, f, &ldif);
234         }
235
236         talloc_free(ldb);
237
238         fclose(f);
239
240         return True;
241 }
242  
243 int main(int argc, char **argv)
244 {
245         int opt;
246         const char *format = "summary";
247         char *libdir = NULL;
248         char *smbconf = NULL;
249         struct samba3 *samba3;
250         poptContext pc;
251         TALLOC_CTX *mem_ctx;
252         struct poptOption long_options[] = {
253                 POPT_AUTOHELP
254                 { "format", 0, POPT_ARG_STRING, &format, 'f', "Format to use (one of: summary, text, ldif)" },
255                 POPT_COMMON_SAMBA
256                 POPT_TABLEEND
257         };
258
259         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
260
261         poptSetOtherOptionHelp(pc, "<libdir> <smb.conf>");
262
263         while((opt = poptGetNextOpt(pc)) != -1) {
264         }
265
266         samba3dump_init_subsystems;
267
268         mem_ctx = talloc_init("samba3dump_context");
269
270         libdir = talloc_strdup(mem_ctx, poptGetArg(pc));
271         smbconf = talloc_strdup(mem_ctx, poptGetArg(pc));
272
273         printf("Reading from libdir '%s', smb.conf file '%s'\n", libdir, smbconf);
274
275         samba3_read(smbconf, libdir, mem_ctx, &samba3);
276
277         if (!strcmp(format, "summary")) {
278                 printf("WINS db entries: %d\n", samba3->winsdb_count);
279                 printf("SAM Accounts: %d\n", samba3->samaccount_count);
280                 printf("Registry key count: %d\n", samba3->registry.key_count);
281                 printf("Shares (including [global]): %d\n", samba3->share_count);
282                 printf("Groupmap count: %d\n", samba3->group.groupmap_count);
283                 printf("Alias count: %d\n", samba3->group.alias_count);
284                 printf("Idmap count: %d\n", samba3->idmap.mapping_count);
285         } else if (!strcmp(format, "text")) {
286                 print_samba3(samba3);
287         } else if (!strcmp(format, "ldif")) {
288                 struct ldb_message **msgs;
289                 struct ldb_context *ldb = ldb_init(mem_ctx);
290                 int i, ret;
291                 const char *hives[] = { "hklm", "hkcr", "hku", "hkpd", "hkpt", NULL };
292
293                 for (i = 0; hives[i]; i++) {
294                         char *fn;
295
296                         ret = samba3_upgrade_registry(&samba3->registry, hives[i], ldb, &msgs); 
297
298                         printf("Writing %s.ldif\n", hives[i]);
299                         asprintf(&fn, "%s.ldif", hives[i]);
300                         write_ldif(fn, msgs, ret); 
301                         SAFE_FREE(fn);
302                 }
303
304                 ret = samba3_upgrade_sam(samba3, ldb, &msgs);
305                 printf("Writing sam.ldif\n");
306                 write_ldif("sam.ldif", msgs, ret);
307
308                 ret = samba3_upgrade_winsdb(samba3, ldb, &msgs);
309                 printf("Writing wins.ldif\n");
310                 write_ldif("wins.ldif", msgs, ret);
311
312                 ret = samba3_upgrade_winbind(samba3, ldb, &msgs);
313                 printf("Writing winbind.ldif\n");
314                 write_ldif("winbind.ldif", msgs, ret);
315         }
316         poptFreeContext(pc);
317
318         return 0;
319 }