Removed version number from file header.
[samba.git] / source / libads / kerberos.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   simulate a kinit, putting the tgt in the default cache location
29   remus@snapserver.com
30 */
31 int kerberos_kinit_password(const char *principal, const char *password)
32 {
33         krb5_context ctx;
34         krb5_error_code code = 0;
35         krb5_ccache cc;
36         krb5_principal me;
37         krb5_creds my_creds;
38
39         if (! *password) {
40                 /* kerberos dies on an empty password! */
41                 return KRB5_PARSE_MALFORMED;
42         }
43
44         if ((code = krb5_init_context(&ctx)))
45                 return code;
46         
47         if ((code = krb5_cc_default(ctx, &cc))) {
48                 krb5_free_context(ctx);
49                 return code;
50         }
51         
52         if ((code = krb5_parse_name(ctx, principal, &me))) {
53                 krb5_free_context(ctx); 
54                 return code;
55         }
56         
57         if ((code = krb5_get_init_creds_password(ctx, &my_creds, me, (char*)password, NULL, 
58                                                 NULL, 0, NULL, NULL))) {
59                 krb5_free_principal(ctx, me);
60                 krb5_free_context(ctx);         
61                 return code;
62         }
63         
64         if ((code = krb5_cc_initialize(ctx, cc, me))) {
65                 krb5_free_cred_contents(ctx, &my_creds);
66                 krb5_free_principal(ctx, me);
67                 krb5_free_context(ctx);         
68                 return code;
69         }
70         
71         if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
72                 krb5_cc_close(ctx, cc);
73                 krb5_free_cred_contents(ctx, &my_creds);
74                 krb5_free_principal(ctx, me);
75                 krb5_free_context(ctx);         
76                 return code;
77         }
78         
79         krb5_cc_close(ctx, cc);
80         krb5_free_cred_contents(ctx, &my_creds);
81         krb5_free_principal(ctx, me);
82         krb5_free_context(ctx);         
83         
84         return 0;
85 }
86
87
88
89 /* run kinit to setup our ccache */
90 int ads_kinit_password(ADS_STRUCT *ads)
91 {
92         char *s;
93         int ret;
94
95         if (!ads->user_name) {
96                 /* by default use the machine account */
97                 extern pstring global_myname;
98                 fstring myname;
99                 fstrcpy(myname, global_myname);
100                 strlower(myname);
101                 asprintf(&ads->user_name, "HOST/%s", global_myname);
102         }
103         asprintf(&s, "%s@%s", ads->user_name, ads->realm);
104         ret = kerberos_kinit_password(s, ads->password);
105
106         if (ret) {
107                 DEBUG(0,("kerberos_kinit_password %s failed: %s\n", 
108                          s, error_message(ret)));
109         }
110         free(s);
111         return ret;
112 }
113
114 /*
115   verify an incoming ticket and parse out the principal name and 
116   authorization_data if available 
117 */
118 NTSTATUS ads_verify_ticket(ADS_STRUCT *ads, const DATA_BLOB *ticket, 
119                            char **principal, DATA_BLOB *auth_data)
120 {
121         krb5_context context;
122         krb5_auth_context auth_context = NULL;
123         krb5_keytab keytab = NULL;
124         krb5_data packet;
125         krb5_ticket *tkt = NULL;
126         krb5_data salt;
127         krb5_encrypt_block eblock;
128         int ret;
129         krb5_keyblock * key;
130         krb5_principal host_princ;
131         char *host_princ_s;
132         extern pstring global_myname;
133         fstring myname;
134         char *password_s;
135         krb5_data password;
136
137         if (!secrets_init()) {
138                 DEBUG(1,("secrets_init failed\n"));
139                 return NT_STATUS_LOGON_FAILURE;
140         }
141
142         password_s = secrets_fetch_machine_password();
143         if (!password_s) {
144                 DEBUG(1,("failed to fetch machine password\n"));
145                 return NT_STATUS_LOGON_FAILURE;
146         }
147
148         password.data = password_s;
149         password.length = strlen(password_s);
150
151         ret = krb5_init_context(&context);
152         if (ret) {
153                 DEBUG(1,("krb5_init_context failed (%s)\n", error_message(ret)));
154                 return NT_STATUS_LOGON_FAILURE;
155         }
156
157         ret = krb5_set_default_realm(context, ads->realm);
158         if (ret) {
159                 DEBUG(1,("krb5_set_default_realm failed (%s)\n", error_message(ret)));
160                 ads_destroy(&ads);
161                 return NT_STATUS_LOGON_FAILURE;
162         }
163
164         /* this whole process is far more complex than I would
165            like. We have to go through all this to allow us to store
166            the secret internally, instead of using /etc/krb5.keytab */
167         ret = krb5_auth_con_init(context, &auth_context);
168         if (ret) {
169                 DEBUG(1,("krb5_auth_con_init failed (%s)\n", error_message(ret)));
170                 return NT_STATUS_LOGON_FAILURE;
171         }
172
173         fstrcpy(myname, global_myname);
174         strlower(myname);
175         asprintf(&host_princ_s, "HOST/%s@%s", myname, lp_realm());
176         ret = krb5_parse_name(context, host_princ_s, &host_princ);
177         if (ret) {
178                 DEBUG(1,("krb5_parse_name(%s) failed (%s)\n", host_princ_s, error_message(ret)));
179                 return NT_STATUS_LOGON_FAILURE;
180         }
181
182         ret = krb5_principal2salt(context, host_princ, &salt);
183         if (ret) {
184                 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
185                 return NT_STATUS_LOGON_FAILURE;
186         }
187     
188         if (!(key = (krb5_keyblock *)malloc(sizeof(*key)))) {
189                 return NT_STATUS_NO_MEMORY;
190         }
191         
192         krb5_use_enctype(context, &eblock, ENCTYPE_DES_CBC_MD5);
193         
194         ret = krb5_string_to_key(context, &eblock, key, &password, &salt);
195         if (ret) {
196                 DEBUG(1,("krb5_string_to_key failed (%s)\n", error_message(ret)));
197                 return NT_STATUS_LOGON_FAILURE;
198         }
199
200         krb5_auth_con_setuseruserkey(context, auth_context, key);
201
202         packet.length = ticket->length;
203         packet.data = (krb5_pointer)ticket->data;
204
205 #if 0
206         file_save("/tmp/ticket.dat", ticket->data, ticket->length);
207 #endif
208
209         if ((ret = krb5_rd_req(context, &auth_context, &packet, 
210                                NULL, keytab, NULL, &tkt))) {
211                 DEBUG(3,("krb5_rd_req with auth failed (%s)\n", 
212                          error_message(ret)));
213                 return NT_STATUS_LOGON_FAILURE;
214         }
215
216         if (tkt->enc_part2) {
217                 *auth_data = data_blob(tkt->enc_part2->authorization_data[0]->contents,
218                                        tkt->enc_part2->authorization_data[0]->length);
219         }
220
221 #if 0
222         if (tkt->enc_part2) {
223                 file_save("/tmp/authdata.dat", 
224                           tkt->enc_part2->authorization_data[0]->contents,
225                           tkt->enc_part2->authorization_data[0]->length);
226         }
227 #endif
228
229         if ((ret = krb5_unparse_name(context, tkt->enc_part2->client, principal))) {
230                 DEBUG(3,("krb5_unparse_name failed (%s)\n", 
231                          error_message(ret)));
232                 return NT_STATUS_LOGON_FAILURE;
233         }
234
235         return NT_STATUS_OK;
236 }
237
238 #endif