r9746: Add "staff" as possible alternative to wheel
[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                 TALLOC_CTX *mem_ctx = talloc_new(NULL);
91                 printf("        User: %s\n", cli_credentials_get_username(secrets->ipc_cred, mem_ctx));
92                 talloc_free(mem_ctx);
93         }
94         if (secrets->ipc_cred->password_obtained)
95                 printf("        Password: %s\n", cli_credentials_get_password(secrets->ipc_cred));
96
97         if (secrets->ipc_cred->domain_obtained)
98                 printf("        Domain: %s\n\n", cli_credentials_get_domain(secrets->ipc_cred));
99
100         printf("LDAP passwords:\n");
101         for (i = 0; i < secrets->ldappw_count; i++) {
102                 printf("\t%s -> %s\n", secrets->ldappws[i].dn, secrets->ldappws[i].password);
103         }
104         printf("\n");
105
106         printf("Domains:\n");
107         for (i = 0; i < secrets->domain_count; i++) {
108                 printf("\t--- %s ---\n", secrets->domains[i].name);
109                 printf("\tSID: %s\n", dom_sid_string(NULL, &secrets->domains[i].sid));
110                 printf("\tGUID: %s\n", GUID_string(NULL, &secrets->domains[i].guid));
111                 printf("\tPlaintext pwd: %s\n", secrets->domains[i].plaintext_pw);
112                 printf("\tLast Changed: %lu\n", secrets->domains[i].last_change_time);
113                 printf("\tSecure Channel Type: %d\n\n", secrets->domains[i].sec_channel_type);
114         }
115
116         printf("Trusted domains:\n");
117         for (i = 0; i < secrets->trusted_domain_count; i++) {
118                 int j;
119                 for (j = 0; j < secrets->trusted_domains[i].uni_name_len; j++) {
120                         printf("\t--- %s ---\n", secrets->trusted_domains[i].uni_name[j]);
121                 }
122                 printf("\tPassword: %s\n", secrets->trusted_domains[i].pass);
123                 printf("\tModified: %lu\n", secrets->trusted_domains[i].mod_time);
124                 printf("\tSID: %s\n", dom_sid_string(NULL, &secrets->trusted_domains[i].domain_sid));
125         }
126
127         return NT_STATUS_OK;
128 }
129
130 static NTSTATUS print_samba3_regdb(struct samba3_regdb *regdb)
131 {
132         int i;
133         print_header("Registry");
134
135         for (i = 0; i < regdb->key_count; i++) {
136                 int j;
137                 printf("%s\n", regdb->keys[i].name);
138                 for (j = 0; j < regdb->keys[i].value_count; j++) {
139                         printf("\t%s: type %d, length %d\n", 
140                                    regdb->keys[i].values[j].name,
141                                    regdb->keys[i].values[j].type,
142                                    regdb->keys[i].values[j].data.length);
143                 }
144         }
145
146         return NT_STATUS_OK;
147 }
148
149 static NTSTATUS print_samba3_winsdb(struct samba3 *samba3)
150 {
151         int i;
152         print_header("WINS Database");
153
154         for (i = 0; i < samba3->winsdb_count; i++) {
155                 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);
156         }
157
158         return NT_STATUS_OK;
159 }
160
161 static NTSTATUS print_samba3_groupdb(struct samba3_groupdb *db)
162 {
163         int i;
164         print_header("Group Mappings");
165         
166         for (i = 0; i < db->groupmap_count; i++) 
167         {
168                 printf("\t--- Group: %s ---\n", db->groupmappings[i].nt_name);
169                 printf("\tComment: %s\n", db->groupmappings[i].comment);
170                 printf("\tGID: %d\n", db->groupmappings[i].gid);
171                 printf("\tSID Name Use: %d\n", db->groupmappings[i].sid_name_use);
172                 printf("\tSID: %s\n\n", dom_sid_string(NULL, db->groupmappings[i].sid));
173         }
174
175         for (i = 0; i < db->alias_count; i++)
176         {
177                 int j;
178                 printf("\t--- Alias: %s ---\n", dom_sid_string(NULL, db->aliases[i].sid));
179                 for (j = 0; j < db->aliases[i].member_count; j++) {
180                         printf("\t%s\n", dom_sid_string(NULL,db->aliases[i].members[j]));
181                 }
182         }
183
184         return NT_STATUS_OK;
185 }
186
187 static NTSTATUS print_samba3_idmapdb(struct samba3_idmapdb *db)
188 {
189         int i;
190         print_header("Winbindd SID<->GID/UID mappings");
191
192         printf("User High Water Mark: %d\n", db->user_hwm);
193         printf("Group High Water Mark: %d\n\n", db->group_hwm);
194
195         for (i = 0; i < db->mapping_count; i++) {
196                 printf("%s -> %cID %d", 
197                           dom_sid_string(NULL, db->mappings[i].sid),
198                           (db->mappings[i].type == IDMAP_GROUP)?'G':'U',
199                           db->mappings[i].unix_id);
200         }
201
202         return NT_STATUS_OK;
203 }
204
205 static NTSTATUS print_samba3(struct samba3 *samba3)
206 {
207         print_samba3_sam(samba3);
208         print_samba3_policy(&samba3->policy);
209         print_samba3_shares(samba3);
210         print_samba3_winsdb(samba3);
211         print_samba3_regdb(&samba3->registry);
212         print_samba3_secrets(&samba3->secrets);
213         print_samba3_groupdb(&samba3->group);
214         print_samba3_idmapdb(&samba3->idmap);
215
216         return NT_STATUS_OK;
217 }
218
219 static BOOL write_ldif(const char *fn, struct ldb_message **messages, int count)
220 {
221         FILE *f = fopen(fn, "w+");
222         struct ldb_ldif ldif;
223         int i;
224         struct ldb_context *ldb = ldb_init(NULL);
225
226         if (!f) {
227                 DEBUG(0, ("Unable to open LDIF file '%s'\n", fn));
228                 talloc_free(ldb);
229                 return False;
230         }
231
232         for (i = 0; i < count; i++) {
233                 ldif.changetype = LDB_CHANGETYPE_ADD;
234                 ldif.msg = messages[i];
235
236                 ldb_ldif_write_file(ldb, f, &ldif);
237         }
238
239         talloc_free(ldb);
240
241         fclose(f);
242
243         return True;
244 }
245  
246 int main(int argc, char **argv)
247 {
248         int opt;
249         const char *format = "summary";
250         char *libdir = NULL;
251         char *smbconf = NULL;
252         struct samba3 *samba3;
253         poptContext pc;
254         TALLOC_CTX *mem_ctx;
255         struct poptOption long_options[] = {
256                 POPT_AUTOHELP
257                 { "format", 0, POPT_ARG_STRING, &format, 'f', "Format to use (one of: summary, text, ldif)" },
258                 POPT_COMMON_SAMBA
259                 POPT_TABLEEND
260         };
261
262         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
263
264         poptSetOtherOptionHelp(pc, "<libdir> <smb.conf>");
265
266         while((opt = poptGetNextOpt(pc)) != -1) {
267         }
268
269         samba3dump_init_subsystems;
270
271         mem_ctx = talloc_init("samba3dump_context");
272
273         libdir = talloc_strdup(mem_ctx, poptGetArg(pc));
274         smbconf = talloc_strdup(mem_ctx, poptGetArg(pc));
275
276         printf("Reading from libdir '%s', smb.conf file '%s'\n", libdir, smbconf);
277
278         samba3_read(smbconf, libdir, mem_ctx, &samba3);
279
280         if (!strcmp(format, "summary")) {
281                 printf("WINS db entries: %d\n", samba3->winsdb_count);
282                 printf("SAM Accounts: %d\n", samba3->samaccount_count);
283                 printf("Registry key count: %d\n", samba3->registry.key_count);
284                 printf("Shares (including [global]): %d\n", samba3->share_count);
285                 printf("Groupmap count: %d\n", samba3->group.groupmap_count);
286                 printf("Alias count: %d\n", samba3->group.alias_count);
287                 printf("Idmap count: %d\n", samba3->idmap.mapping_count);
288         } else if (!strcmp(format, "text")) {
289                 print_samba3(samba3);
290         } else if (!strcmp(format, "ldif")) {
291                 struct ldb_message **msgs;
292                 struct ldb_context *ldb = ldb_init(mem_ctx);
293                 int i, ret;
294                 const char *hives[] = { "hklm", "hkcr", "hku", "hkpd", "hkpt", NULL };
295
296                 for (i = 0; hives[i]; i++) {
297                         char *fn;
298
299                         ret = samba3_upgrade_registry(&samba3->registry, hives[i], ldb, &msgs); 
300
301                         printf("Writing %s.ldif\n", hives[i]);
302                         asprintf(&fn, "%s.ldif", hives[i]);
303                         write_ldif(fn, msgs, ret); 
304                         SAFE_FREE(fn);
305                 }
306
307                 ret = samba3_upgrade_sam(samba3, ldb, &msgs);
308                 printf("Writing sam.ldif\n");
309                 write_ldif("sam.ldif", msgs, ret);
310
311                 ret = samba3_upgrade_winsdb(samba3, ldb, &msgs);
312                 printf("Writing wins.ldif\n");
313                 write_ldif("wins.ldif", msgs, ret);
314
315                 ret = samba3_upgrade_winbind(samba3, ldb, &msgs);
316                 printf("Writing winbind.ldif\n");
317                 write_ldif("winbind.ldif", msgs, ret);
318         }
319         poptFreeContext(pc);
320
321         return 0;
322 }