W00t! Client smb signing is now working correctly with krb5 and w2k server.
[bbaumbach/samba-autobuild/.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         NTSTATUS sret = NT_STATUS_LOGON_FAILURE;
37         krb5_context context = NULL;
38         krb5_auth_context auth_context = NULL;
39         krb5_keytab keytab = NULL;
40         krb5_data packet;
41         krb5_ticket *tkt = NULL;
42         krb5_rcache rcache = NULL;
43         int ret, i;
44         krb5_keyblock *key = NULL;
45         krb5_principal host_princ;
46         char *host_princ_s = NULL;
47         fstring myname;
48         char *password_s = NULL;
49         krb5_data password;
50         krb5_enctype *enctypes = NULL;
51 #if 0
52         krb5_address local_addr;
53         krb5_address remote_addr;
54 #endif
55         BOOL auth_ok = False;
56
57         ZERO_STRUCT(packet);
58         ZERO_STRUCT(password);
59         ZERO_STRUCTP(auth_data);
60         ZERO_STRUCTP(ap_rep);
61
62         if (!secrets_init()) {
63                 DEBUG(1,("secrets_init failed\n"));
64                 return NT_STATUS_LOGON_FAILURE;
65         }
66
67         password_s = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
68         if (!password_s) {
69                 DEBUG(1,("failed to fetch machine password\n"));
70                 return NT_STATUS_LOGON_FAILURE;
71         }
72
73         password.data = password_s;
74         password.length = strlen(password_s);
75
76         ret = krb5_init_context(&context);
77         if (ret) {
78                 DEBUG(1,("krb5_init_context failed (%s)\n", error_message(ret)));
79                 return NT_STATUS_LOGON_FAILURE;
80         }
81
82         ret = krb5_set_default_realm(context, ads->auth.realm);
83         if (ret) {
84                 DEBUG(1,("krb5_set_default_realm failed (%s)\n", error_message(ret)));
85                 sret = NT_STATUS_LOGON_FAILURE;
86                 goto out;
87         }
88
89         /* This whole process is far more complex than I would
90            like. We have to go through all this to allow us to store
91            the secret internally, instead of using /etc/krb5.keytab */
92
93         ret = krb5_auth_con_init(context, &auth_context);
94         if (ret) {
95                 DEBUG(1,("krb5_auth_con_init failed (%s)\n", error_message(ret)));
96                 sret = NT_STATUS_LOGON_FAILURE;
97                 goto out;
98         }
99
100         fstrcpy(myname, global_myname());
101         strlower_m(myname);
102         asprintf(&host_princ_s, "HOST/%s@%s", myname, lp_realm());
103         ret = krb5_parse_name(context, host_princ_s, &host_princ);
104         if (ret) {
105                 DEBUG(1,("krb5_parse_name(%s) failed (%s)\n", host_princ_s, error_message(ret)));
106                 sret = NT_STATUS_LOGON_FAILURE;
107                 goto out;
108         }
109
110         /*
111          * JRA. We must set the rcache and the allowed addresses in the auth_context
112          * here. This will prevent replay attacks and ensure the client has got a key from
113          * the correct IP address.
114          */
115
116         ret = krb5_get_server_rcache(context, krb5_princ_component(context, host_princ, 0), &rcache);
117         if (ret) {
118                 DEBUG(1,("krb5_get_server_rcache failed (%s)\n", error_message(ret)));
119                 sret = NT_STATUS_LOGON_FAILURE;
120                 goto out;
121         }
122
123         ret = krb5_auth_con_setrcache(context, auth_context, rcache);
124         if (ret) {
125                 DEBUG(1,("krb5_auth_con_setrcache failed (%s)\n", error_message(ret)));
126                 sret = NT_STATUS_LOGON_FAILURE;
127                 goto out;
128         }
129
130         /* Now we need to add the addresses.... JRA. */
131
132         if (!(key = (krb5_keyblock *)malloc(sizeof(*key)))) {
133                 sret = NT_STATUS_NO_MEMORY;
134                 goto out;
135         }
136         
137         if ((ret = get_kerberos_allowed_etypes(context, &enctypes))) {
138                 DEBUG(1,("krb5_get_permitted_enctypes failed (%s)\n", 
139                          error_message(ret)));
140                 sret = NT_STATUS_LOGON_FAILURE;
141                 goto out;
142         }
143
144         /* we need to setup a auth context with each possible encoding type in turn */
145         for (i=0;enctypes[i];i++) {
146                 if (create_kerberos_key_from_string(context, host_princ, &password, key, enctypes[i])) {
147                         continue;
148                 }
149
150                 krb5_auth_con_setuseruserkey(context, auth_context, key);
151
152                 packet.length = ticket->length;
153                 packet.data = (krb5_pointer)ticket->data;
154
155                 if (!(ret = krb5_rd_req(context, &auth_context, &packet, 
156                                        NULL, keytab, NULL, &tkt))) {
157                         free_kerberos_etypes(context, enctypes);
158                         auth_ok = True;
159                         break;
160                 }
161         }
162
163         if (!auth_ok) {
164                 DEBUG(3,("krb5_rd_req with auth failed (%s)\n", 
165                          error_message(ret)));
166                 sret = NT_STATUS_LOGON_FAILURE;
167                 goto out;
168         }
169
170         ret = krb5_mk_rep(context, auth_context, &packet);
171         if (ret) {
172                 DEBUG(3,("Failed to generate mutual authentication reply (%s)\n",
173                         error_message(ret)));
174                 sret = NT_STATUS_LOGON_FAILURE;
175                 goto out;
176         }
177
178         *ap_rep = data_blob(packet.data, packet.length);
179         free(packet.data);
180
181         get_krb5_smb_session_key(context, auth_context, session_key, True);
182 #ifdef DEBUG_PASSWORD
183         DEBUG(10,("SMB session key (from ticket) follows:\n"));
184         dump_data(10, session_key, 16);
185 #endif
186
187 #if 0
188         file_save("/tmp/ticket.dat", ticket->data, ticket->length);
189 #endif
190
191         get_auth_data_from_tkt(auth_data, tkt);
192
193         {
194                 TALLOC_CTX *ctx = talloc_init("pac data");
195                 decode_pac_data(auth_data, ctx);
196                 talloc_destroy(ctx);
197         }
198
199 #if 0
200         if (tkt->enc_part2) {
201                 file_save("/tmp/authdata.dat",
202                           tkt->enc_part2->authorization_data[0]->contents,
203                           tkt->enc_part2->authorization_data[0]->length);
204 #endif
205
206         if ((ret = krb5_unparse_name(context, get_principal_from_tkt(tkt),
207                                      principal))) {
208                 DEBUG(3,("krb5_unparse_name failed (%s)\n", 
209                          error_message(ret)));
210                 sret = NT_STATUS_LOGON_FAILURE;
211                 goto out;
212         }
213
214         sret = NT_STATUS_OK;
215
216  out:
217
218         if (!NT_STATUS_IS_OK(sret))
219                 data_blob_free(auth_data);
220
221         if (!NT_STATUS_IS_OK(sret))
222                 data_blob_free(ap_rep);
223
224         SAFE_FREE(host_princ_s);
225         SAFE_FREE(password_s);
226
227         if (auth_context)
228                 krb5_auth_con_free(context, auth_context);
229
230         if (context)
231                 krb5_free_context(context);
232
233         return sret;
234 }
235
236 #endif /* HAVE_KRB5 */