libreplace(samba4): let LIBREPLACE depend on LIBREPLACE_NETWORK for now
[samba.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 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 "system/kerberos.h"
26 #include "auth/credentials/credentials.h"
27 #include "auth/credentials/credentials_krb5.h"
28 #include "param/param.h"
29
30 static NTSTATUS samdump_keytab_handle_user(TALLOC_CTX *mem_ctx,
31                                            struct loadparm_context *lp_ctx,
32                                             const char *keytab_name,
33                                             struct netr_DELTA_ENUM *delta) 
34 {
35         struct netr_DELTA_USER *user = delta->delta_union.user;
36         const char *username = user->account_name.string;
37         struct cli_credentials *credentials;
38         int ret;
39
40         if (!user->nt_password_present) {
41                 /* We can't do anything here */
42                 return NT_STATUS_OK;
43         }
44
45         credentials = cli_credentials_init(mem_ctx);
46         if (!credentials) {
47                 return NT_STATUS_NO_MEMORY;
48         }
49         cli_credentials_set_conf(credentials, lp_ctx);
50         cli_credentials_set_username(credentials, username, CRED_SPECIFIED);
51
52         /* We really should consult ldap in the main SamSync code, and
53          * pass a value in here */
54         cli_credentials_set_kvno(credentials, 0);
55         cli_credentials_set_nt_hash(credentials, &user->ntpassword, CRED_SPECIFIED);
56         ret = cli_credentials_set_keytab_name(credentials, lp_ctx, keytab_name, CRED_SPECIFIED);
57         if (ret) {
58                 return NT_STATUS_UNSUCCESSFUL;
59         }
60
61         ret = cli_credentials_update_keytab(credentials, lp_ctx);
62         if (ret) {
63                 return NT_STATUS_UNSUCCESSFUL;
64         }
65         
66         return NT_STATUS_OK;
67 }
68
69 static NTSTATUS libnet_samdump_keytab_fn(TALLOC_CTX *mem_ctx,           
70                                          void *private,                         
71                                          enum netr_SamDatabaseID database,
72                                          struct netr_DELTA_ENUM *delta,
73                                          char **error_string)
74 {
75         NTSTATUS nt_status = NT_STATUS_OK;
76         const char *keytab_name = private;
77
78         *error_string = NULL;
79         switch (delta->delta_type) {
80         case NETR_DELTA_USER:
81         {
82                 /* not interested in builtin users */
83                 if (database == SAM_DATABASE_DOMAIN) {
84                         nt_status = samdump_keytab_handle_user(mem_ctx, 
85                                                                global_loadparm,
86                                                                keytab_name,
87                                                                delta);
88                         break;
89                 }
90         }
91         default:
92                 /* Can't dump them all right now */
93                 break;
94         }
95         return nt_status;
96 }
97
98 NTSTATUS libnet_SamDump_keytab(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_SamDump_keytab *r)
99 {
100         NTSTATUS nt_status;
101         struct libnet_SamSync r2;
102
103         r2.out.error_string            = NULL;
104         r2.in.binding_string           = r->in.binding_string;
105         r2.in.rid_crypt                = true;
106         r2.in.init_fn                  = NULL;
107         r2.in.delta_fn                 = libnet_samdump_keytab_fn;
108         r2.in.fn_ctx                   = discard_const(r->in.keytab_name);
109         r2.in.machine_account          = r->in.machine_account;
110         nt_status                      = libnet_SamSync_netlogon(ctx, mem_ctx, &r2);
111         r->out.error_string            = r2.out.error_string;
112         talloc_steal(mem_ctx, r->out.error_string);
113
114         if (!NT_STATUS_IS_OK(nt_status)) {
115                 return nt_status;
116         }
117
118         return nt_status;
119 }