The kerberos_verify compoenent of the SessionSetup sync 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    Copyright (C) Luke Howard 2003   
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                            DATA_BLOB *ap_rep,
34                            uint8 session_key[16])
35 {
36         krb5_context context;
37         krb5_auth_context auth_context = NULL;
38         krb5_keytab keytab = NULL;
39         krb5_data packet;
40         krb5_ticket *tkt = NULL;
41         int ret, i;
42         krb5_keyblock * key;
43         krb5_principal host_princ;
44         char *host_princ_s;
45         fstring myname;
46         char *password_s;
47         krb5_data password;
48         krb5_enctype *enctypes = NULL;
49         BOOL auth_ok = False;
50
51         if (!secrets_init()) {
52                 DEBUG(1,("secrets_init failed\n"));
53                 return NT_STATUS_LOGON_FAILURE;
54         }
55
56         password_s = secrets_fetch_machine_password();
57         if (!password_s) {
58                 DEBUG(1,("failed to fetch machine password\n"));
59                 return NT_STATUS_LOGON_FAILURE;
60         }
61
62         password.data = password_s;
63         password.length = strlen(password_s);
64
65         ret = krb5_init_context(&context);
66         if (ret) {
67                 DEBUG(1,("krb5_init_context failed (%s)\n", error_message(ret)));
68                 return NT_STATUS_LOGON_FAILURE;
69         }
70
71         ret = krb5_set_default_realm(context, ads->auth.realm);
72         if (ret) {
73                 DEBUG(1,("krb5_set_default_realm failed (%s)\n", error_message(ret)));
74                 return NT_STATUS_LOGON_FAILURE;
75         }
76
77         /* this whole process is far more complex than I would
78            like. We have to go through all this to allow us to store
79            the secret internally, instead of using /etc/krb5.keytab */
80         ret = krb5_auth_con_init(context, &auth_context);
81         if (ret) {
82                 DEBUG(1,("krb5_auth_con_init failed (%s)\n", error_message(ret)));
83                 return NT_STATUS_LOGON_FAILURE;
84         }
85
86         fstrcpy(myname, global_myname());
87         strlower(myname);
88         asprintf(&host_princ_s, "HOST/%s@%s", myname, lp_realm());
89         ret = krb5_parse_name(context, host_princ_s, &host_princ);
90         if (ret) {
91                 DEBUG(1,("krb5_parse_name(%s) failed (%s)\n", host_princ_s, error_message(ret)));
92                 return NT_STATUS_LOGON_FAILURE;
93         }
94
95         if (!(key = (krb5_keyblock *)malloc(sizeof(*key)))) {
96                 return NT_STATUS_NO_MEMORY;
97         }
98         
99         if ((ret = get_kerberos_allowed_etypes(context, &enctypes))) {
100                 DEBUG(1,("krb5_get_permitted_enctypes failed (%s)\n", 
101                          error_message(ret)));
102                 return NT_STATUS_LOGON_FAILURE;
103         }
104
105         /* we need to setup a auth context with each possible encoding type in turn */
106         for (i=0;enctypes[i];i++) {
107                 if (create_kerberos_key_from_string(context, host_princ, &password, key, enctypes[i])) {
108                         continue;
109                 }
110
111                 krb5_auth_con_setuseruserkey(context, auth_context, key);
112
113                 packet.length = ticket->length;
114                 packet.data = (krb5_pointer)ticket->data;
115
116                 if (!(ret = krb5_rd_req(context, &auth_context, &packet, 
117                                        NULL, keytab, NULL, &tkt))) {
118                         free_kerberos_etypes(context, enctypes);
119                         auth_ok = True;
120                         break;
121                 }
122         }
123
124         if (!auth_ok) {
125                 DEBUG(3,("krb5_rd_req with auth failed (%s)\n", 
126                          error_message(ret)));
127                 return NT_STATUS_LOGON_FAILURE;
128         }
129
130         ret = krb5_mk_rep(context, auth_context, &packet);
131         if (ret) {
132                 DEBUG(3,("Failed to generate mutual authentication reply (%s)\n",
133                         error_message(ret)));
134                 krb5_auth_con_free(context, auth_context);
135                 return NT_STATUS_LOGON_FAILURE;
136         }
137
138         *ap_rep = data_blob(packet.data, packet.length);
139         free(packet.data);
140
141         krb5_get_smb_session_key(context, auth_context, session_key);
142         DEBUG(0,("SMB session key (from ticket) follows:\n"));
143         dump_data(0, session_key, 16);
144
145 #if 0
146         file_save("/tmp/ticket.dat", ticket->data, ticket->length);
147 #endif
148
149         get_auth_data_from_tkt(auth_data, tkt);
150
151 #if 0
152         if (tkt->enc_part2) {
153                 file_save("/tmp/authdata.dat",
154                           tkt->enc_part2->authorization_data[0]->contents,
155                           tkt->enc_part2->authorization_data[0]->length);
156 #endif
157
158         if ((ret = krb5_unparse_name(context, get_principal_from_tkt(tkt),
159                                      principal))) {
160                 DEBUG(3,("krb5_unparse_name failed (%s)\n", 
161                          error_message(ret)));
162                 data_blob_free(auth_data);
163                 data_blob_free(ap_rep);
164                 krb5_auth_con_free(context, auth_context);
165                 return NT_STATUS_LOGON_FAILURE;
166         }
167
168         krb5_auth_con_free(context, auth_context);
169
170         return NT_STATUS_OK;
171 }
172
173 #endif /* HAVE_KRB5 */