d00394fd790459d4ef43aa68671f1138ade64080
[samba.git] / source4 / libcli / auth / 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 #include "system/kerberos.h"
27 #include "libcli/auth/kerberos.h"
28 #include "asn_1.h"
29
30 #ifdef HAVE_KRB5
31
32 static DATA_BLOB unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data)
33 {
34         DATA_BLOB out;
35         DATA_BLOB pac_contents = data_blob(NULL, 0);
36         struct asn1_data data;
37         int data_type;
38         if (!auth_data->length) {
39                 return data_blob(NULL, 0);
40         }
41
42         asn1_load(&data, *auth_data);
43         asn1_start_tag(&data, ASN1_SEQUENCE(0));
44         asn1_start_tag(&data, ASN1_SEQUENCE(0));
45         asn1_start_tag(&data, ASN1_CONTEXT(0));
46         asn1_read_Integer(&data, &data_type);
47         asn1_end_tag(&data);
48         asn1_start_tag(&data, ASN1_CONTEXT(1));
49         asn1_read_OctetString(&data, &pac_contents);
50         asn1_end_tag(&data);
51         asn1_end_tag(&data);
52         asn1_end_tag(&data);
53         asn1_free(&data);
54
55         out = data_blob_talloc(mem_ctx, pac_contents.data, pac_contents.length);
56
57         data_blob_free(&pac_contents);
58
59         return out;
60 }
61
62 /**********************************************************************************
63  Try to verify a ticket using the system keytab... the system keytab has kvno -1 entries, so
64  it's more like what microsoft does... see comment in utils/net_ads.c in the
65  ads_keytab_add_entry function for details.
66 ***********************************************************************************/
67
68 static krb5_error_code ads_keytab_verify_ticket(krb5_context context, krb5_auth_context auth_context,
69                         const DATA_BLOB *ticket, krb5_data *p_packet, krb5_ticket **pp_tkt,
70                         krb5_keyblock *keyblock)
71 {
72         krb5_error_code ret = 0;
73
74         krb5_keytab keytab = NULL;
75         krb5_kt_cursor cursor;
76         krb5_keytab_entry kt_entry;
77         char *princ_name = NULL;
78
79         ZERO_STRUCT(kt_entry);
80         ZERO_STRUCT(cursor);
81
82         ZERO_STRUCTP(keyblock);
83
84         ret = krb5_kt_default(context, &keytab);
85         if (ret) {
86                 DEBUG(1, ("ads_keytab_verify_ticket: krb5_kt_default failed (%s)\n", error_message(ret)));
87                 goto out;
88         }
89
90         ret = krb5_kt_start_seq_get(context, keytab, &cursor);
91         if (ret) {
92                 DEBUG(1, ("ads_keytab_verify_ticket: krb5_kt_start_seq_get failed (%s)\n", error_message(ret)));
93                 goto out;
94         }
95
96         while (!(ret = krb5_kt_next_entry(context, keytab, &kt_entry, &cursor))) {
97                 ret = krb5_unparse_name(context, kt_entry.principal, &princ_name);
98                 if (ret) {
99                         DEBUG(1, ("ads_keytab_verify_ticket: krb5_unparse_name failed (%s)\n", error_message(ret)));
100                         goto out;
101                 }
102                 DEBUG(10, ("Checking principal: %s\n", princ_name));
103                 /* Look for a CIFS ticket */
104                 if (!strncasecmp(princ_name, "cifs/", 5) || 
105                     !strncasecmp(princ_name, "host/", 5) ||
106                     !strncasecmp(princ_name, "ldap/", 5)) {
107 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK
108                         krb5_auth_con_setuseruserkey(context, auth_context, &kt_entry.keyblock);
109 #else
110                         krb5_auth_con_setuseruserkey(context, auth_context, &kt_entry.key);
111 #endif
112
113                         p_packet->length = ticket->length;
114                         p_packet->data = (krb5_pointer)ticket->data;
115
116                         ret = krb5_rd_req(context, &auth_context, p_packet, NULL, NULL, NULL, pp_tkt);
117                         if (!ret) {
118                                 unsigned int keytype;
119                                 krb5_free_unparsed_name(context, princ_name);
120                                 princ_name = NULL;
121 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK
122                                 keytype = (unsigned int) kt_entry.keyblock.keytype;
123                                 copy_EncryptionKey(&kt_entry.keyblock, keyblock);
124 #else
125                                 keytype = (unsigned int) kt_entry.key.enctype;
126                                 /* TODO: copy the keyblock on MIT krb5*/
127 #endif
128                                 DEBUG(10,("ads_keytab_verify_ticket: enc type [%u] decrypted message !\n",
129                                           keytype));
130                                 
131                                 break;
132                         }
133                 }
134                 krb5_free_unparsed_name(context, princ_name);
135                 princ_name = NULL;
136         }
137         if (ret && ret != KRB5_KT_END) {
138                 /* This failed because something went wrong, not because the keytab file was empty. */
139                 DEBUG(1, ("ads_keytab_verify_ticket: krb5_kt_next_entry failed (%s)\n", error_message(ret)));
140         } else if (ret == KRB5_KT_END) {
141                 DEBUG(10, ("ads_keytab_verify_ticket: no keytab entry found: %s\n", error_message(ret)));
142         } else {
143                 DEBUG(10, ("ads_keytab_verify_ticket: keytab entry found: %s\n", princ_name));
144         }
145   out:
146
147         if (princ_name) {
148                 krb5_free_unparsed_name(context, princ_name);
149         }
150         {
151                 krb5_kt_cursor zero_csr;
152                 ZERO_STRUCT(zero_csr);
153                 if ((memcmp(&cursor, &zero_csr, sizeof(krb5_kt_cursor)) != 0) && keytab) {
154                         krb5_kt_end_seq_get(context, keytab, &cursor);
155                 }
156         }
157         if (keytab) {
158                 krb5_kt_close(context, keytab);
159         }
160
161         return ret;
162 }
163
164 /**********************************************************************************
165  Try to verify a ticket using the secrets.tdb.
166 ***********************************************************************************/
167
168 static krb5_error_code ads_secrets_verify_ticket(krb5_context context, krb5_auth_context auth_context,
169                         krb5_principal host_princ,
170                         const DATA_BLOB *ticket, krb5_data *p_packet, krb5_ticket **pp_tkt,
171                         krb5_keyblock *keyblock)
172 {
173         krb5_error_code ret = 0;
174         char *password_s = NULL;
175         krb5_data password;
176         krb5_enctype *enctypes = NULL;
177         int i;
178
179         ZERO_STRUCTP(keyblock);
180
181         if (!secrets_init()) {
182                 DEBUG(1,("ads_secrets_verify_ticket: secrets_init failed\n"));
183                 return KRB5_KT_END;
184         }
185
186         password_s = secrets_fetch_machine_password(lp_workgroup());
187         if (!password_s) {
188                 DEBUG(1,("ads_secrets_verify_ticket: failed to fetch machine password\n"));
189                 return KRB5_KT_END;
190         }
191
192         password.data = password_s;
193         password.length = strlen(password_s);
194
195         /* CIFS doesn't use addresses in tickets. This would break NAT. JRA */
196
197         if ((ret = get_kerberos_allowed_etypes(context, &enctypes))) {
198                 DEBUG(1,("ads_secrets_verify_ticket: krb5_get_permitted_enctypes failed (%s)\n", 
199                          error_message(ret)));
200                 goto out;
201         }
202
203         p_packet->length = ticket->length;
204         p_packet->data = (krb5_pointer)ticket->data;
205
206         ret = KRB5_BAD_ENCTYPE;
207         /* We need to setup a auth context with each possible encoding type in turn. */
208         for (i=0;enctypes[i];i++) {
209                 krb5_error_code our_ret;
210                 our_ret = create_kerberos_key_from_string(context, host_princ, &password, keyblock, enctypes[i]);
211                 if (our_ret) {
212                         ret = our_ret;
213                         continue;
214                 }
215
216                 krb5_auth_con_setuseruserkey(context, auth_context, keyblock);
217
218                 our_ret = krb5_rd_req(context, &auth_context, p_packet, 
219                                         NULL,
220                                         NULL, NULL, pp_tkt);
221                 if (!our_ret) {
222                         DEBUG(10,("ads_secrets_verify_ticket: enc type [%u] decrypted message !\n",
223                                 (unsigned int)enctypes[i] ));
224                         ret = our_ret;
225                         break;
226                 }
227
228                 krb5_free_keyblock_contents(context, keyblock);
229
230                 DEBUG((our_ret != KRB5_BAD_ENCTYPE) ? 3 : 10,
231                                 ("ads_secrets_verify_ticket: enc type [%u] failed to decrypt with error %s\n",
232                                 (unsigned int)enctypes[i], error_message(our_ret)));
233
234                 if (our_ret !=  KRB5_BAD_ENCTYPE) {
235                         ret = our_ret;
236                 }
237         }
238
239  out:
240
241         free_kerberos_etypes(context, enctypes);
242         SAFE_FREE(password_s);
243
244         return ret;
245 }
246
247 /**********************************************************************************
248  Verify an incoming ticket and parse out the principal name and 
249  authorization_data if available.
250 ***********************************************************************************/
251
252  NTSTATUS ads_verify_ticket(TALLOC_CTX *mem_ctx, 
253                            krb5_context context,
254                            krb5_auth_context auth_context,
255                            const char *realm, const DATA_BLOB *ticket, 
256                            char **principal, DATA_BLOB *auth_data,
257                            DATA_BLOB *ap_rep,
258                            krb5_keyblock *keyblock)
259 {
260         NTSTATUS sret = NT_STATUS_LOGON_FAILURE;
261         krb5_data packet;
262         krb5_ticket *tkt = NULL;
263         krb5_rcache rcache = NULL;
264         int ret;
265
266         krb5_principal host_princ = NULL;
267         char *host_princ_s = NULL;
268         BOOL got_replay_mutex = False;
269
270         char *myname;
271
272         char *malloc_principal;
273
274         ZERO_STRUCT(packet);
275         ZERO_STRUCTP(auth_data);
276         ZERO_STRUCTP(ap_rep);
277
278         /* This whole process is far more complex than I would
279            like. We have to go through all this to allow us to store
280            the secret internally, instead of using /etc/krb5.keytab */
281
282         myname = name_to_fqdn(mem_ctx, lp_netbios_name());
283         strlower_m(myname);
284         asprintf(&host_princ_s, "host/%s@%s", myname, lp_realm());
285         ret = krb5_parse_name(context, host_princ_s, &host_princ);
286         if (ret) {
287                 DEBUG(1,("ads_verify_ticket: krb5_parse_name(%s) failed (%s)\n",
288                                         host_princ_s, error_message(ret)));
289                 goto out;
290         }
291
292
293         /* Lock a mutex surrounding the replay as there is no locking in the MIT krb5
294          * code surrounding the replay cache... */
295
296         if (!grab_server_mutex("replay cache mutex")) {
297                 DEBUG(1,("ads_verify_ticket: unable to protect replay cache with mutex.\n"));
298                 goto out;
299         }
300
301         got_replay_mutex = True;
302
303         /*
304          * JRA. We must set the rcache here. This will prevent replay attacks.
305          */
306
307         ret = krb5_get_server_rcache(context, krb5_princ_component(context, host_princ, 0), &rcache);
308         if (ret) {
309                 DEBUG(1,("ads_verify_ticket: krb5_get_server_rcache failed (%s)\n", error_message(ret)));
310                 goto out;
311         }
312
313         ret = krb5_auth_con_setrcache(context, auth_context, rcache);
314         if (ret) {
315                 DEBUG(1,("ads_verify_ticket: krb5_auth_con_setrcache failed (%s)\n", error_message(ret)));
316                 goto out;
317         }
318
319         ret = ads_keytab_verify_ticket(context, auth_context, ticket, &packet, &tkt, keyblock);
320         if (ret) {
321                 DEBUG(10, ("ads_secrets_verify_ticket: using host principal: [%s]\n", host_princ_s));
322                 ret = ads_secrets_verify_ticket(context, auth_context, host_princ,
323                                                         ticket, &packet, &tkt, keyblock);
324         }
325
326         release_server_mutex();
327         got_replay_mutex = False;
328
329         if (ret) {
330                 DEBUG(3,("ads_verify_ticket: krb5_rd_req with auth failed (%s)\n", 
331                          error_message(ret)));
332                 goto out;
333         }
334
335         ret = krb5_mk_rep(context, auth_context, &packet);
336         if (ret) {
337                 DEBUG(3,("ads_verify_ticket: Failed to generate mutual authentication reply (%s)\n",
338                         error_message(ret)));
339                 goto out;
340         }
341
342         *ap_rep = data_blob_talloc(mem_ctx, packet.data, packet.length);
343         SAFE_FREE(packet.data);
344         packet.length = 0;
345
346 #if 0
347         file_save("/tmp/ticket.dat", ticket->data, ticket->length);
348 #endif
349
350         *auth_data = get_auth_data_from_tkt(mem_ctx, tkt);
351
352         *auth_data = unwrap_pac(mem_ctx, auth_data);
353
354 #if 0
355         if (tkt->enc_part2) {
356                 file_save("/tmp/authdata.dat",
357                           tkt->enc_part2->authorization_data[0]->contents,
358                           tkt->enc_part2->authorization_data[0]->length);
359         }
360 #endif
361
362         if ((ret = krb5_unparse_name(context, get_principal_from_tkt(tkt),
363                                      &malloc_principal))) {
364                 DEBUG(3,("ads_verify_ticket: krb5_unparse_name failed (%s)\n", 
365                          error_message(ret)));
366                 sret = NT_STATUS_LOGON_FAILURE;
367                 goto out;
368         }
369
370         *principal = talloc_strdup(mem_ctx, malloc_principal);
371         SAFE_FREE(malloc_principal);
372         if (!principal) {
373                 DEBUG(3,("ads_verify_ticket: talloc_strdup() failed\n"));
374                 sret = NT_STATUS_NO_MEMORY;
375                 goto out;
376         }
377
378         sret = NT_STATUS_OK;
379
380  out:
381
382         if (got_replay_mutex) {
383                 release_server_mutex();
384         }
385
386         if (!NT_STATUS_IS_OK(sret)) {
387                 data_blob_free(auth_data);
388         }
389
390         if (!NT_STATUS_IS_OK(sret)) {
391                 data_blob_free(ap_rep);
392         }
393
394         if (host_princ) {
395                 krb5_free_principal(context, host_princ);
396         }
397
398         if (tkt != NULL) {
399                 krb5_free_ticket(context, tkt);
400         }
401
402         SAFE_FREE(host_princ_s);
403
404         return sret;
405 }
406
407 #endif /* HAVE_KRB5 */