r9455: Support for reading the policy database
[sfrench/samba-autobuild/.git] / source / 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/policy.h"
24 #include "lib/samba3/sam.h"
25 #include "lib/cmdline/popt_common.h"
26
27 static const char *libdir = "/var/lib/samba";
28
29 static NTSTATUS print_policy(void)
30 {
31         struct samba3_policy *ret;
32         char *policy_file;
33         TALLOC_CTX *mem_ctx = talloc_init(NULL);
34
35         policy_file = talloc_asprintf(mem_ctx, "%s/account_policy.tdb", libdir);
36
37         printf("Opening policy file %s\n", policy_file);
38
39         ret = samba3_read_account_policy(mem_ctx, policy_file);
40
41         if (ret == NULL) 
42                 return NT_STATUS_UNSUCCESSFUL;
43         
44         printf("Min password length: %d\n", ret->min_password_length);
45
46         talloc_free(mem_ctx);
47
48         return NT_STATUS_OK;
49 }
50
51 static NTSTATUS print_sam(void)
52 {
53         struct samba3_samaccount *accounts;
54         uint32_t count, i;
55         char *tdbsam_file;
56         NTSTATUS status;
57         
58         asprintf(&tdbsam_file, "%s/passdb.tdb", libdir);
59
60         printf("Opening TDB sam %s\n", tdbsam_file);
61
62         status = samba3_read_tdbsam(NULL, tdbsam_file, &accounts, &count);
63         if (NT_STATUS_IS_ERR(status)) {
64                 fprintf(stderr, "Error reading tdbsam database %s\n", tdbsam_file);
65                 SAFE_FREE(tdbsam_file);
66                 return status;
67         }
68         SAFE_FREE(tdbsam_file);
69
70         for (i = 0; i < count; i++) {
71                 printf("%d: %s\n", accounts[i].user_rid, accounts[i].username);
72         }
73
74         return NT_STATUS_OK;
75 }
76  
77 int main(int argc, char **argv)
78 {
79         int opt;
80         poptContext pc;
81         struct poptOption long_options[] = {
82                 POPT_AUTOHELP
83                 { "libdir", 0, POPT_ARG_STRING, &libdir, 'l', "Set libdir [/var/lib/samba]", "LIBDIR" },
84                 POPT_COMMON_SAMBA
85                 POPT_TABLEEND
86         };
87
88         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
89
90         poptSetOtherOptionHelp(pc, "<smb.conf>");
91
92         while((opt = poptGetNextOpt(pc)) != -1) {
93         }
94
95         print_sam();
96         print_policy();
97
98         poptFreeContext(pc);
99
100         return 0;
101 }