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