Removed global_myworkgroup, global_myname, global_myscope. Added liberal
[sfrench/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    
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         krb5_data salt;
40         krb5_encrypt_block eblock;
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
50         if (!secrets_init()) {
51                 DEBUG(1,("secrets_init failed\n"));
52                 return NT_STATUS_LOGON_FAILURE;
53         }
54
55         password_s = secrets_fetch_machine_password();
56         if (!password_s) {
57                 DEBUG(1,("failed to fetch machine password\n"));
58                 return NT_STATUS_LOGON_FAILURE;
59         }
60
61         password.data = password_s;
62         password.length = strlen(password_s);
63
64         ret = krb5_init_context(&context);
65         if (ret) {
66                 DEBUG(1,("krb5_init_context failed (%s)\n", error_message(ret)));
67                 return NT_STATUS_LOGON_FAILURE;
68         }
69
70         ret = krb5_set_default_realm(context, ads->auth.realm);
71         if (ret) {
72                 DEBUG(1,("krb5_set_default_realm failed (%s)\n", error_message(ret)));
73                 return NT_STATUS_LOGON_FAILURE;
74         }
75
76         /* this whole process is far more complex than I would
77            like. We have to go through all this to allow us to store
78            the secret internally, instead of using /etc/krb5.keytab */
79         ret = krb5_auth_con_init(context, &auth_context);
80         if (ret) {
81                 DEBUG(1,("krb5_auth_con_init failed (%s)\n", error_message(ret)));
82                 return NT_STATUS_LOGON_FAILURE;
83         }
84
85         fstrcpy(myname, global_myname());
86         strlower(myname);
87         asprintf(&host_princ_s, "HOST/%s@%s", myname, lp_realm());
88         ret = krb5_parse_name(context, host_princ_s, &host_princ);
89         if (ret) {
90                 DEBUG(1,("krb5_parse_name(%s) failed (%s)\n", host_princ_s, error_message(ret)));
91                 return NT_STATUS_LOGON_FAILURE;
92         }
93
94         ret = krb5_principal2salt(context, host_princ, &salt);
95         if (ret) {
96                 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
97                 return NT_STATUS_LOGON_FAILURE;
98         }
99     
100         if (!(key = (krb5_keyblock *)malloc(sizeof(*key)))) {
101                 return NT_STATUS_NO_MEMORY;
102         }
103         
104         if ((ret = krb5_get_permitted_enctypes(context, &enctypes))) {
105                 DEBUG(1,("krb5_get_permitted_enctypes failed (%s)\n", 
106                          error_message(ret)));
107                 return NT_STATUS_LOGON_FAILURE;
108         }
109
110         /* we need to setup a auth context with each possible encoding type in turn */
111         for (i=0;enctypes[i];i++) {
112                 krb5_use_enctype(context, &eblock, enctypes[i]);
113
114                 ret = krb5_string_to_key(context, &eblock, key, &password, &salt);
115                 if (ret) {
116                         continue;
117                 }
118
119                 krb5_auth_con_setuseruserkey(context, auth_context, key);
120
121                 packet.length = ticket->length;
122                 packet.data = (krb5_pointer)ticket->data;
123
124                 if (!(ret = krb5_rd_req(context, &auth_context, &packet, 
125                                        NULL, keytab, NULL, &tkt))) {
126                         krb5_free_ktypes(context, enctypes);
127                         break;
128                 }
129         }
130
131         if (!enctypes[i]) {
132                 DEBUG(3,("krb5_rd_req with auth failed (%s)\n", 
133                          error_message(ret)));
134                 return NT_STATUS_LOGON_FAILURE;
135         }
136
137 #if 0
138         file_save("/tmp/ticket.dat", ticket->data, ticket->length);
139 #endif
140
141
142         if (tkt->enc_part2) {
143                 *auth_data = data_blob(tkt->enc_part2->authorization_data[0]->contents,
144                                        tkt->enc_part2->authorization_data[0]->length);
145         }
146
147 #if 0
148         if (tkt->enc_part2) {
149                 file_save("/tmp/authdata.dat", 
150                           tkt->enc_part2->authorization_data[0]->contents,
151                           tkt->enc_part2->authorization_data[0]->length);
152         }
153 #endif
154
155         if ((ret = krb5_unparse_name(context, tkt->enc_part2->client, principal))) {
156                 DEBUG(3,("krb5_unparse_name failed (%s)\n", 
157                          error_message(ret)));
158                 return NT_STATUS_LOGON_FAILURE;
159         }
160
161         return NT_STATUS_OK;
162 }
163
164 #endif