libnet4: Use dom_sid_str_buf
[garming/samba-autobuild/.git] / source4 / libnet / libnet_samdump.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Extract the user/system database from a remote SamSync server
5
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22
23 #include "includes.h"
24 #include "libnet/libnet.h"
25 #include "../lib/util/dlinklist.h"
26 #include "samba3/samba3.h"
27 #include "libcli/security/security.h"
28 #include "param/param.h"
29
30
31 struct samdump_secret {
32         struct samdump_secret *prev, *next;
33         DATA_BLOB secret;
34         char *name;
35         NTTIME mtime;
36 };
37
38 struct samdump_trusted_domain {
39         struct samdump_trusted_domain *prev, *next;
40         struct dom_sid *sid;
41         char *name;
42 };
43
44 struct samdump_state {
45         struct samdump_secret *secrets;
46         struct samdump_trusted_domain *trusted_domains;
47 };
48
49 static NTSTATUS vampire_samdump_handle_user(TALLOC_CTX *mem_ctx,
50                                             struct netr_DELTA_ENUM *delta) 
51 {
52         uint32_t rid = delta->delta_id_union.rid;
53         struct netr_DELTA_USER *user = delta->delta_union.user;
54         const char *username = user->account_name.string;
55         char *hex_lm_password;
56         char *hex_nt_password;
57
58         hex_lm_password = smbpasswd_sethexpwd(mem_ctx, 
59                                               user->lm_password_present ? &user->lmpassword : NULL, 
60                                               user->acct_flags);
61         hex_nt_password = smbpasswd_sethexpwd(mem_ctx, 
62                                               user->nt_password_present ? &user->ntpassword : NULL, 
63                                               user->acct_flags);
64
65         printf("%s:%d:%s:%s:%s:LCT-%08X\n", username,
66                rid, hex_lm_password, hex_nt_password,
67                smbpasswd_encode_acb_info(mem_ctx, user->acct_flags),
68                (unsigned int)nt_time_to_unix(user->last_password_change));
69
70         return NT_STATUS_OK;
71 }
72
73 static NTSTATUS vampire_samdump_handle_secret(TALLOC_CTX *mem_ctx,
74                                               struct samdump_state *samdump_state,
75                                               struct netr_DELTA_ENUM *delta) 
76 {
77         struct netr_DELTA_SECRET *secret = delta->delta_union.secret;
78         const char *name = delta->delta_id_union.name;
79         struct samdump_secret *n = talloc(samdump_state, struct samdump_secret);
80
81         n->name = talloc_strdup(n, name);
82         n->secret = data_blob_talloc(n, secret->current_cipher.cipher_data, secret->current_cipher.maxlen);
83         n->mtime = secret->current_cipher_set_time;
84
85         DLIST_ADD(samdump_state->secrets, n);
86
87         return NT_STATUS_OK;
88 }
89
90 static NTSTATUS vampire_samdump_handle_trusted_domain(TALLOC_CTX *mem_ctx,
91                                               struct samdump_state *samdump_state,
92                                               struct netr_DELTA_ENUM *delta) 
93 {
94         struct netr_DELTA_TRUSTED_DOMAIN *trusted_domain = delta->delta_union.trusted_domain;
95         struct dom_sid *dom_sid = delta->delta_id_union.sid;
96
97         struct samdump_trusted_domain *n = talloc(samdump_state, struct samdump_trusted_domain);
98
99         n->name = talloc_strdup(n, trusted_domain->domain_name.string);
100         n->sid = talloc_steal(n, dom_sid);
101
102         DLIST_ADD(samdump_state->trusted_domains, n);
103
104         return NT_STATUS_OK;
105 }
106
107 static NTSTATUS libnet_samdump_fn(TALLOC_CTX *mem_ctx,
108                                   void *private_data,
109                                   enum netr_SamDatabaseID database,
110                                   struct netr_DELTA_ENUM *delta,
111                                   char **error_string)
112 {
113         NTSTATUS nt_status = NT_STATUS_OK;
114         struct samdump_state *samdump_state = (struct samdump_state *)private_data;
115
116         *error_string = NULL;
117         switch (delta->delta_type) {
118         case NETR_DELTA_USER:
119         {
120                 /* not interested in builtin users */
121                 if (database == SAM_DATABASE_DOMAIN) {
122                         nt_status = vampire_samdump_handle_user(mem_ctx, 
123                                                                 delta);
124                 }
125                 break;
126         }
127         case NETR_DELTA_SECRET:
128         {
129                 nt_status = vampire_samdump_handle_secret(mem_ctx,
130                                                           samdump_state,
131                                                           delta);
132                 break;
133         }
134         case NETR_DELTA_TRUSTED_DOMAIN:
135         {
136                 nt_status = vampire_samdump_handle_trusted_domain(mem_ctx,
137                                                                   samdump_state,
138                                                                   delta);
139                 break;
140         }
141         default:
142                 /* Can't dump them all right now */
143                 break;
144         }
145         return nt_status;
146 }
147
148 NTSTATUS libnet_SamDump(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, 
149                         struct libnet_SamDump *r)
150 {
151         NTSTATUS nt_status;
152         struct libnet_SamSync r2;
153         struct samdump_state *samdump_state = talloc(mem_ctx, struct samdump_state);
154
155         struct samdump_trusted_domain *t;
156         struct samdump_secret *s;
157
158         if (!samdump_state) {
159                 return NT_STATUS_NO_MEMORY;
160         }
161
162         samdump_state->secrets         = NULL;
163         samdump_state->trusted_domains = NULL;
164
165         r2.out.error_string            = NULL;
166         r2.in.binding_string           = r->in.binding_string;
167         r2.in.init_fn                  = NULL;
168         r2.in.delta_fn                 = libnet_samdump_fn;
169         r2.in.fn_ctx                   = samdump_state;
170         r2.in.machine_account          = r->in.machine_account;
171         nt_status                      = libnet_SamSync_netlogon(ctx, samdump_state, &r2);
172         r->out.error_string            = r2.out.error_string;
173         talloc_steal(mem_ctx, r->out.error_string);
174
175         if (!NT_STATUS_IS_OK(nt_status)) {
176                 talloc_free(samdump_state);
177                 return nt_status;
178         }
179
180         printf("Trusted domains, sids and secrets:\n");
181         for (t=samdump_state->trusted_domains; t; t=t->next) {
182                 char *secret_name = talloc_asprintf(mem_ctx, "G$$%s", t->name);
183                 for (s=samdump_state->secrets; s; s=s->next) {
184                         size_t converted_size = 0;
185                         char *secret_string;
186                         struct dom_sid_buf buf;
187                         if (strcasecmp_m(s->name, secret_name) != 0) {
188                                 continue;
189                         }
190                         if (!convert_string_talloc_handle(mem_ctx, lpcfg_iconv_handle(ctx->lp_ctx), CH_UTF16, CH_UNIX,
191                                                   s->secret.data, s->secret.length, 
192                                                   (void **)&secret_string, &converted_size)) {
193                                 r->out.error_string = talloc_asprintf(mem_ctx, 
194                                                                       "Could not convert secret for domain %s to a string",
195                                                                       t->name);
196                                 talloc_free(samdump_state);
197                                 return NT_STATUS_INVALID_PARAMETER;
198                         }
199                         printf("%s\t%s\t%s\n", 
200                                t->name,
201                                dom_sid_str_buf(t->sid, &buf),
202                                secret_string);
203                 }
204         }
205         talloc_free(samdump_state);
206         return nt_status;
207 }
208