d81a0a1629b2dc163c38787adfb13d1648559d88
[jra/samba/.git] / source3 / libads / kerberos_verify.c
1 /* 
2    Unix SMB/CIFS implementation.
3    kerberos utility library
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) Remus Koos 2001
6    Copyright (C) Luke Howard 2003   
7    Copyright (C) Guenther Deschner 2003
8    Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26
27 #ifdef HAVE_KRB5
28
29 /**********************************************************************************
30  Try to verify a ticket using the system keytab... the system keytab has kvno -1 entries, so
31  it's more like what microsoft does... see comment in utils/net_ads.c in the
32  ads_keytab_add_entry function for details.
33 ***********************************************************************************/
34
35 static BOOL ads_keytab_verify_ticket(krb5_context context, krb5_auth_context auth_context,
36                         const DATA_BLOB *ticket, krb5_data *p_packet, krb5_ticket **pp_tkt)
37 {
38         krb5_error_code ret = 0;
39         BOOL auth_ok = False;
40
41         krb5_keytab keytab = NULL;
42         krb5_kt_cursor cursor;
43         krb5_keytab_entry kt_entry;
44         char *princ_name = NULL;
45
46         ZERO_STRUCT(kt_entry);
47         ZERO_STRUCT(cursor);
48
49         ret = krb5_kt_default(context, &keytab);
50         if (ret) {
51                 DEBUG(1, ("ads_keytab_verify_ticket: krb5_kt_default failed (%s)\n", error_message(ret)));
52                 goto out;
53         }
54
55         ret = krb5_kt_start_seq_get(context, keytab, &cursor);
56         if (ret) {
57                 DEBUG(1, ("ads_keytab_verify_ticket: krb5_kt_start_seq_get failed (%s)\n", error_message(ret)));
58                 goto out;
59         }
60
61         while (!krb5_kt_next_entry(context, keytab, &kt_entry, &cursor)) {
62                 ret = krb5_unparse_name(context, kt_entry.principal, &princ_name);
63                 if (ret) {
64                         DEBUG(1, ("ads_keytab_verify_ticket: krb5_unparse_name failed (%s)\n", error_message(ret)));
65                         goto out;
66                 }
67                 /* Look for a CIFS ticket */
68                 if (!StrnCaseCmp(princ_name, "cifs/", 5)) {
69 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK
70                         krb5_auth_con_setuseruserkey(context, auth_context, &kt_entry.keyblock);
71 #else
72                         krb5_auth_con_setuseruserkey(context, auth_context, &kt_entry.key);
73 #endif
74
75                         p_packet->length = ticket->length;
76                         p_packet->data = (krb5_pointer)ticket->data;
77
78                         if (!(ret = krb5_rd_req(context, &auth_context, p_packet, NULL, NULL, NULL, pp_tkt))) {
79                                 krb5_free_unparsed_name(context, princ_name);
80                                 princ_name = NULL;
81                                 DEBUG(10,("ads_keytab_verify_ticket: enc type [%u] decrypted message !\n",
82 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK
83                                         (unsigned int) kt_entry.keyblock.keytype));
84 #else
85                                         (unsigned int) kt_entry.key.enctype));
86 #endif
87                                 auth_ok = True;
88                                 break;
89                         }
90                 }
91                 krb5_free_unparsed_name(context, princ_name);
92                 princ_name = NULL;
93         }
94         if (ret && ret != KRB5_KT_END) {
95                 /* This failed because something went wrong, not because the keytab file was empty. */
96                 DEBUG(1, ("ads_keytab_verify_ticket: krb5_kt_next_entry failed (%s)\n", error_message(ret)));
97                 goto out;
98         }
99
100   out:
101
102         if (princ_name) {
103                 krb5_free_unparsed_name(context, princ_name);
104         }
105         {
106                 krb5_kt_cursor zero_csr;
107                 ZERO_STRUCT(zero_csr);
108                 if ((memcmp(&cursor, &zero_csr, sizeof(krb5_kt_cursor)) != 0) && keytab) {
109                         krb5_kt_end_seq_get(context, keytab, &cursor);
110                 }
111         }
112         if (keytab) {
113                 krb5_kt_close(context, keytab);
114         }
115
116         return auth_ok;
117 }
118
119 /**********************************************************************************
120  Try to verify a ticket using the secrets.tdb.
121 ***********************************************************************************/
122
123 static BOOL ads_secrets_verify_ticket(krb5_context context, krb5_auth_context auth_context,
124                         krb5_principal host_princ,
125                         const DATA_BLOB *ticket, krb5_data *p_packet, krb5_ticket **pp_tkt)
126 {
127         krb5_error_code ret = 0;
128         BOOL auth_ok = False;
129         char *password_s = NULL;
130         krb5_data password;
131         krb5_enctype *enctypes = NULL;
132         int i;
133
134         if (!secrets_init()) {
135                 DEBUG(1,("ads_secrets_verify_ticket: secrets_init failed\n"));
136                 return False;
137         }
138
139         password_s = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
140         if (!password_s) {
141                 DEBUG(1,("ads_secrets_verify_ticket: failed to fetch machine password\n"));
142                 return False;
143         }
144
145         password.data = password_s;
146         password.length = strlen(password_s);
147
148         /* CIFS doesn't use addresses in tickets. This would break NAT. JRA */
149
150         if ((ret = get_kerberos_allowed_etypes(context, &enctypes))) {
151                 DEBUG(1,("ads_secrets_verify_ticket: krb5_get_permitted_enctypes failed (%s)\n", 
152                          error_message(ret)));
153                 goto out;
154         }
155
156         p_packet->length = ticket->length;
157         p_packet->data = (krb5_pointer)ticket->data;
158
159         /* We need to setup a auth context with each possible encoding type in turn. */
160         for (i=0;enctypes[i];i++) {
161                 krb5_keyblock *key = NULL;
162
163                 if (!(key = (krb5_keyblock *)malloc(sizeof(*key)))) {
164                         goto out;
165                 }
166         
167                 if (create_kerberos_key_from_string(context, host_princ, &password, key, enctypes[i])) {
168                         SAFE_FREE(key);
169                         continue;
170                 }
171
172                 krb5_auth_con_setuseruserkey(context, auth_context, key);
173
174                 krb5_free_keyblock(context, key);
175
176                 if (!(ret = krb5_rd_req(context, &auth_context, p_packet, 
177                                         NULL,
178                                         NULL, NULL, pp_tkt))) {
179                         DEBUG(10,("ads_secrets_verify_ticket: enc type [%u] decrypted message !\n",
180                                 (unsigned int)enctypes[i] ));
181                         auth_ok = True;
182                         break;
183                 }
184         
185                 DEBUG((ret != KRB5_BAD_ENCTYPE) ? 3 : 10,
186                                 ("ads_secrets_verify_ticket: enc type [%u] failed to decrypt with error %s\n",
187                                 (unsigned int)enctypes[i], error_message(ret)));
188         }
189
190  out:
191
192         free_kerberos_etypes(context, enctypes);
193         SAFE_FREE(password_s);
194
195         return auth_ok;
196 }
197
198 /**********************************************************************************
199  Verify an incoming ticket and parse out the principal name and 
200  authorization_data if available.
201 ***********************************************************************************/
202
203 NTSTATUS ads_verify_ticket(const char *realm, const DATA_BLOB *ticket, 
204                            char **principal, DATA_BLOB *auth_data,
205                            DATA_BLOB *ap_rep,
206                            DATA_BLOB *session_key)
207 {
208         NTSTATUS sret = NT_STATUS_LOGON_FAILURE;
209         krb5_context context = NULL;
210         krb5_auth_context auth_context = NULL;
211         krb5_data packet;
212         krb5_ticket *tkt = NULL;
213         krb5_rcache rcache = NULL;
214         int ret;
215
216         krb5_principal host_princ = NULL;
217         char *host_princ_s = NULL;
218         BOOL got_replay_mutex = False;
219
220         fstring myname;
221         BOOL auth_ok = False;
222
223         ZERO_STRUCT(packet);
224         ZERO_STRUCTP(auth_data);
225         ZERO_STRUCTP(ap_rep);
226         ZERO_STRUCTP(session_key);
227
228         initialize_krb5_error_table();
229         ret = krb5_init_context(&context);
230         if (ret) {
231                 DEBUG(1,("ads_verify_ticket: krb5_init_context failed (%s)\n", error_message(ret)));
232                 return NT_STATUS_LOGON_FAILURE;
233         }
234
235         ret = krb5_set_default_realm(context, realm);
236         if (ret) {
237                 DEBUG(1,("ads_verify_ticket: krb5_set_default_realm failed (%s)\n", error_message(ret)));
238                 goto out;
239         }
240
241         /* This whole process is far more complex than I would
242            like. We have to go through all this to allow us to store
243            the secret internally, instead of using /etc/krb5.keytab */
244
245         ret = krb5_auth_con_init(context, &auth_context);
246         if (ret) {
247                 DEBUG(1,("ads_verify_ticket: krb5_auth_con_init failed (%s)\n", error_message(ret)));
248                 goto out;
249         }
250
251         name_to_fqdn(myname, global_myname());
252         strlower_m(myname);
253         asprintf(&host_princ_s, "host/%s@%s", myname, lp_realm());
254         ret = krb5_parse_name(context, host_princ_s, &host_princ);
255         if (ret) {
256                 DEBUG(1,("ads_verify_ticket: krb5_parse_name(%s) failed (%s)\n",
257                                         host_princ_s, error_message(ret)));
258                 goto out;
259         }
260
261
262         /* Lock a mutex surrounding the replay as there is no locking in the MIT krb5
263          * code surrounding the replay cache... */
264
265         if (!grab_server_mutex("replay cache mutex")) {
266                 DEBUG(1,("ads_verify_ticket: unable to protect replay cache with mutex.\n"));
267                 goto out;
268         }
269
270         got_replay_mutex = True;
271
272         /*
273          * JRA. We must set the rcache here. This will prevent replay attacks.
274          */
275
276         ret = krb5_get_server_rcache(context, krb5_princ_component(context, host_princ, 0), &rcache);
277         if (ret) {
278                 DEBUG(1,("ads_verify_ticket: krb5_get_server_rcache failed (%s)\n", error_message(ret)));
279                 goto out;
280         }
281
282         ret = krb5_auth_con_setrcache(context, auth_context, rcache);
283         if (ret) {
284                 DEBUG(1,("ads_verify_ticket: krb5_auth_con_setrcache failed (%s)\n", error_message(ret)));
285                 goto out;
286         }
287
288         if (lp_use_kerberos_keytab()) {
289                 auth_ok = ads_keytab_verify_ticket(context, auth_context, ticket, &packet, &tkt);
290         }
291         if (!auth_ok) {
292                 auth_ok = ads_secrets_verify_ticket(context, auth_context, host_princ,
293                                                         ticket, &packet, &tkt);
294         }
295
296         release_server_mutex();
297         got_replay_mutex = False;
298
299         if (!auth_ok) {
300                 DEBUG(3,("ads_verify_ticket: krb5_rd_req with auth failed (%s)\n", 
301                          error_message(ret)));
302                 goto out;
303         }
304
305         ret = krb5_mk_rep(context, auth_context, &packet);
306         if (ret) {
307                 DEBUG(3,("ads_verify_ticket: Failed to generate mutual authentication reply (%s)\n",
308                         error_message(ret)));
309                 goto out;
310         }
311
312         *ap_rep = data_blob(packet.data, packet.length);
313         SAFE_FREE(packet.data);
314         packet.length = 0;
315
316         get_krb5_smb_session_key(context, auth_context, session_key, True);
317         dump_data_pw("SMB session key (from ticket)\n", session_key->data, session_key->length);
318
319 #if 0
320         file_save("/tmp/ticket.dat", ticket->data, ticket->length);
321 #endif
322
323         get_auth_data_from_tkt(auth_data, tkt);
324
325         {
326                 TALLOC_CTX *ctx = talloc_init("pac data");
327                 decode_pac_data(auth_data, ctx);
328                 talloc_destroy(ctx);
329         }
330
331 #if 0
332         if (tkt->enc_part2) {
333                 file_save("/tmp/authdata.dat",
334                           tkt->enc_part2->authorization_data[0]->contents,
335                           tkt->enc_part2->authorization_data[0]->length);
336         }
337 #endif
338
339         if ((ret = krb5_unparse_name(context, get_principal_from_tkt(tkt),
340                                      principal))) {
341                 DEBUG(3,("ads_verify_ticket: krb5_unparse_name failed (%s)\n", 
342                          error_message(ret)));
343                 sret = NT_STATUS_LOGON_FAILURE;
344                 goto out;
345         }
346
347         sret = NT_STATUS_OK;
348
349  out:
350
351         if (got_replay_mutex) {
352                 release_server_mutex();
353         }
354
355         if (!NT_STATUS_IS_OK(sret)) {
356                 data_blob_free(auth_data);
357         }
358
359         if (!NT_STATUS_IS_OK(sret)) {
360                 data_blob_free(ap_rep);
361         }
362
363         if (host_princ) {
364                 krb5_free_principal(context, host_princ);
365         }
366
367         if (tkt != NULL) {
368                 krb5_free_ticket(context, tkt);
369         }
370
371         SAFE_FREE(host_princ_s);
372
373         if (auth_context) {
374                 krb5_auth_con_free(context, auth_context);
375         }
376
377         if (context) {
378                 krb5_free_context(context);
379         }
380
381         return sret;
382 }
383
384 #endif /* HAVE_KRB5 */