Try to get heimdal working with HEAD.
[ira/wip.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    
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 #include "includes.h"
24
25 #ifdef HAVE_KRB5
26
27 /*
28   verify an incoming ticket and parse out the principal name and 
29   authorization_data if available 
30 */
31 NTSTATUS ads_verify_ticket(ADS_STRUCT *ads, const DATA_BLOB *ticket, 
32                            char **principal, DATA_BLOB *auth_data)
33 {
34         krb5_context context;
35         krb5_auth_context auth_context = NULL;
36         krb5_keytab keytab = NULL;
37         krb5_data packet;
38         krb5_ticket *tkt = NULL;
39         int ret, i;
40         krb5_keyblock * key;
41         krb5_principal host_princ;
42         char *host_princ_s;
43         fstring myname;
44         char *password_s;
45         krb5_data password;
46         krb5_enctype *enctypes = NULL;
47         BOOL auth_ok = False;
48
49         if (!secrets_init()) {
50                 DEBUG(1,("secrets_init failed\n"));
51                 return NT_STATUS_LOGON_FAILURE;
52         }
53
54         password_s = secrets_fetch_machine_password();
55         if (!password_s) {
56                 DEBUG(1,("failed to fetch machine password\n"));
57                 return NT_STATUS_LOGON_FAILURE;
58         }
59
60         password.data = password_s;
61         password.length = strlen(password_s);
62
63         ret = krb5_init_context(&context);
64         if (ret) {
65                 DEBUG(1,("krb5_init_context failed (%s)\n", error_message(ret)));
66                 return NT_STATUS_LOGON_FAILURE;
67         }
68
69         ret = krb5_set_default_realm(context, ads->auth.realm);
70         if (ret) {
71                 DEBUG(1,("krb5_set_default_realm failed (%s)\n", error_message(ret)));
72                 return NT_STATUS_LOGON_FAILURE;
73         }
74
75         /* this whole process is far more complex than I would
76            like. We have to go through all this to allow us to store
77            the secret internally, instead of using /etc/krb5.keytab */
78         ret = krb5_auth_con_init(context, &auth_context);
79         if (ret) {
80                 DEBUG(1,("krb5_auth_con_init failed (%s)\n", error_message(ret)));
81                 return NT_STATUS_LOGON_FAILURE;
82         }
83
84         fstrcpy(myname, global_myname());
85         strlower(myname);
86         asprintf(&host_princ_s, "HOST/%s@%s", myname, lp_realm());
87         ret = krb5_parse_name(context, host_princ_s, &host_princ);
88         if (ret) {
89                 DEBUG(1,("krb5_parse_name(%s) failed (%s)\n", host_princ_s, error_message(ret)));
90                 return NT_STATUS_LOGON_FAILURE;
91         }
92
93         if (!(key = (krb5_keyblock *)malloc(sizeof(*key)))) {
94                 return NT_STATUS_NO_MEMORY;
95         }
96         
97         if ((ret = get_kerberos_allowed_etypes(context, &enctypes))) {
98                 DEBUG(1,("krb5_get_permitted_enctypes failed (%s)\n", 
99                          error_message(ret)));
100                 return NT_STATUS_LOGON_FAILURE;
101         }
102
103         /* we need to setup a auth context with each possible encoding type in turn */
104         for (i=0;enctypes[i];i++) {
105                 if (create_kerberos_key_from_string(context, host_princ, &password, key, enctypes[i])) {
106                         continue;
107                 }
108
109                 krb5_auth_con_setuseruserkey(context, auth_context, key);
110
111                 packet.length = ticket->length;
112                 packet.data = (krb5_pointer)ticket->data;
113
114                 if (!(ret = krb5_rd_req(context, &auth_context, &packet, 
115                                        NULL, keytab, NULL, &tkt))) {
116                         free_kerberos_etypes(context, enctypes);
117                         auth_ok = True;
118                         break;
119                 }
120         }
121
122         if (!auth_ok) {
123                 DEBUG(3,("krb5_rd_req with auth failed (%s)\n", 
124                          error_message(ret)));
125                 SAFE_FREE(key);
126                 return NT_STATUS_LOGON_FAILURE;
127         }
128
129 #if 0
130         file_save("/tmp/ticket.dat", ticket->data, ticket->length);
131 #endif
132
133         get_auth_data_from_tkt(auth_data, tkt);
134
135 #if 0
136         if (tkt->enc_part2) {
137                 file_save("/tmp/authdata.dat", 
138                           tkt->enc_part2->authorization_data[0]->contents,
139                           tkt->enc_part2->authorization_data[0]->length);
140         }
141 #endif
142
143         if ((ret = krb5_unparse_name(context, get_principal_from_tkt(tkt),
144                                      principal))) {
145                 DEBUG(3,("krb5_unparse_name failed (%s)\n", 
146                          error_message(ret)));
147                 return NT_STATUS_LOGON_FAILURE;
148         }
149
150         return NT_STATUS_OK;
151 }
152
153 #endif