r12865: Upgrade the librpc and libnet code.
[kai/samba.git] / source / libnet / libnet_samdump_keytab.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Extract kerberos keys 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23
24 #include "includes.h"
25 #include "libnet/libnet.h"
26 #include "system/kerberos.h"
27
28 static NTSTATUS samdump_keytab_handle_user(TALLOC_CTX *mem_ctx,
29                                             const char *keytab_name,
30                                             struct creds_CredentialState *creds,
31                                             struct netr_DELTA_ENUM *delta) 
32 {
33         struct netr_DELTA_USER *user = delta->delta_union.user;
34         const char *username = user->account_name.string;
35         struct cli_credentials *credentials;
36         int ret;
37
38         if (!user->nt_password_present) {
39                 /* We can't do anything here */
40                 return NT_STATUS_OK;
41         }
42
43         credentials = cli_credentials_init(mem_ctx);
44         if (!credentials) {
45                 return NT_STATUS_NO_MEMORY;
46         }
47         cli_credentials_set_conf(credentials);
48         cli_credentials_set_username(credentials, username, CRED_SPECIFIED);
49
50         /* We really should consult ldap in the main SamSync code, and
51          * pass a value in here */
52         cli_credentials_set_kvno(credentials, 0);
53         cli_credentials_set_nt_hash(credentials, &user->ntpassword, CRED_SPECIFIED);
54         ret = cli_credentials_set_keytab_name(credentials, keytab_name, CRED_SPECIFIED);
55         if (ret) {
56                 return NT_STATUS_UNSUCCESSFUL;
57         }
58
59         ret = cli_credentials_update_keytab(credentials);
60         if (ret) {
61                 return NT_STATUS_UNSUCCESSFUL;
62         }
63         
64         return NT_STATUS_OK;
65 }
66
67 static NTSTATUS libnet_samdump_keytab_fn(TALLOC_CTX *mem_ctx,           
68                                          void *private,                         
69                                          struct creds_CredentialState *creds,
70                                          enum netr_SamDatabaseID database,
71                                          struct netr_DELTA_ENUM *delta,
72                                          char **error_string)
73 {
74         NTSTATUS nt_status = NT_STATUS_OK;
75         const char *keytab_name = private;
76
77         *error_string = NULL;
78         switch (delta->delta_type) {
79         case NETR_DELTA_USER:
80         {
81                 /* not interested in builtin users */
82                 if (database == SAM_DATABASE_DOMAIN) {
83                         nt_status = samdump_keytab_handle_user(mem_ctx, 
84                                                                keytab_name,
85                                                                creds,
86                                                                delta);
87                         break;
88                 }
89         }
90         default:
91                 /* Can't dump them all right now */
92                 break;
93         }
94         return nt_status;
95 }
96
97 NTSTATUS libnet_SamDump_keytab(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_SamDump_keytab *r)
98 {
99         NTSTATUS nt_status;
100         struct libnet_SamSync r2;
101
102         r2.out.error_string            = NULL;
103         r2.in.binding_string           = r->in.binding_string;
104         r2.in.delta_fn                 = libnet_samdump_keytab_fn;
105         r2.in.fn_ctx                   = discard_const(r->in.keytab_name);
106         r2.in.machine_account          = r->in.machine_account;
107         nt_status                      = libnet_SamSync_netlogon(ctx, mem_ctx, &r2);
108         r->out.error_string            = r2.out.error_string;
109
110         if (!NT_STATUS_IS_OK(nt_status)) {
111                 return nt_status;
112         }
113
114         return nt_status;
115 }