r12607: fix the build
[abartlet/samba.git/.git] / source4 / 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 "librpc/gen_ndr/ndr_netlogon.h"
27 #include "librpc/gen_ndr/ndr_samr.h"
28 #include "system/kerberos.h"
29 #include "auth/kerberos/kerberos.h"
30
31 static NTSTATUS samdump_keytab_handle_user(TALLOC_CTX *mem_ctx,
32                                             const char *keytab_name,
33                                             struct creds_CredentialState *creds,
34                                             struct netr_DELTA_ENUM *delta) 
35 {
36         struct netr_DELTA_USER *user = delta->delta_union.user;
37         const char *username = user->account_name.string;
38         struct cli_credentials *credentials;
39         int ret;
40
41         if (!user->nt_password_present) {
42                 /* We can't do anything here */
43                 return NT_STATUS_OK;
44         }
45
46         credentials = cli_credentials_init(mem_ctx);
47         if (!credentials) {
48                 return NT_STATUS_NO_MEMORY;
49         }
50         cli_credentials_set_conf(credentials);
51         cli_credentials_set_username(credentials, username, CRED_SPECIFIED);
52
53         /* We really should consult ldap in the main SamSync code, and
54          * pass a value in here */
55         cli_credentials_set_kvno(credentials, 0);
56         cli_credentials_set_nt_hash(credentials, &user->ntpassword, CRED_SPECIFIED);
57         ret = cli_credentials_set_keytab_name(credentials, keytab_name, CRED_SPECIFIED);
58         if (ret) {
59                 return NT_STATUS_UNSUCCESSFUL;
60         }
61
62         ret = cli_credentials_update_keytab(credentials);
63         if (ret) {
64                 return NT_STATUS_UNSUCCESSFUL;
65         }
66         
67         return NT_STATUS_OK;
68 }
69
70 static NTSTATUS libnet_samdump_keytab_fn(TALLOC_CTX *mem_ctx,           
71                                          void *private,                         
72                                          struct creds_CredentialState *creds,
73                                          enum netr_SamDatabaseID database,
74                                          struct netr_DELTA_ENUM *delta,
75                                          char **error_string)
76 {
77         NTSTATUS nt_status = NT_STATUS_OK;
78         const char *keytab_name = private;
79
80         *error_string = NULL;
81         switch (delta->delta_type) {
82         case NETR_DELTA_USER:
83         {
84                 /* not interested in builtin users */
85                 if (database == SAM_DATABASE_DOMAIN) {
86                         nt_status = samdump_keytab_handle_user(mem_ctx, 
87                                                                keytab_name,
88                                                                creds,
89                                                                delta);
90                         break;
91                 }
92         }
93         default:
94                 /* Can't dump them all right now */
95                 break;
96         }
97         return nt_status;
98 }
99
100 static NTSTATUS libnet_SamDump_keytab_netlogon(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_SamDump_keytab *r)
101 {
102         NTSTATUS nt_status;
103         struct libnet_SamSync r2;
104
105         r2.error_string = NULL;
106         r2.delta_fn = libnet_samdump_keytab_fn;
107         r2.fn_ctx = r->keytab_name;
108         r2.machine_account = NULL; /* TODO:  Create a machine account, fill this in, and the delete it */
109         nt_status = libnet_SamSync_netlogon(ctx, mem_ctx, &r2);
110         r->error_string = r2.error_string;
111
112         if (!NT_STATUS_IS_OK(nt_status)) {
113                 return nt_status;
114         }
115
116         return nt_status;
117 }
118
119
120
121 static NTSTATUS libnet_SamDump_keytab_generic(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_SamDump_keytab *r)
122 {
123         NTSTATUS nt_status;
124         struct libnet_SamDump_keytab r2;
125         r2.level = LIBNET_SAMDUMP_NETLOGON;
126         r2.error_string = NULL;
127         r2.keytab_name = r->keytab_name;
128         nt_status = libnet_SamDump_keytab(ctx, mem_ctx, &r2);
129         r->error_string = r2.error_string;
130         
131         return nt_status;
132 }
133
134 NTSTATUS libnet_SamDump_keytab(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_SamDump_keytab *r)
135 {
136         switch (r->level) {
137         case LIBNET_SAMDUMP_GENERIC:
138                 return libnet_SamDump_keytab_generic(ctx, mem_ctx, r);
139         case LIBNET_SAMDUMP_NETLOGON:
140                 return libnet_SamDump_keytab_netlogon(ctx, mem_ctx, r);
141         }
142
143         return NT_STATUS_INVALID_LEVEL;
144 }