3d7084aa0de78a1a3389fa79955129d49577ac53
[samba.git] / source4 / auth / kerberos / kerberos_util.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Kerberos utility functions for GENSEC
5    
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
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    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "system/kerberos.h"
26 #include "system/time.h"
27 #include "system/network.h"
28 #include "auth/kerberos/kerberos.h"
29 #include "auth/auth.h"
30
31 struct principal_container {
32         struct smb_krb5_context *smb_krb5_context;
33         krb5_principal principal;
34 };
35
36 static int free_principal(void *ptr) {
37         struct principal_container *pc = ptr;
38         /* current heimdal - 0.6.3, which we need anyway, fixes segfaults here */
39         krb5_free_principal(pc->smb_krb5_context->krb5_context, pc->principal);
40
41         return 0;
42 }
43
44 krb5_error_code salt_principal_from_credentials(TALLOC_CTX *parent_ctx, 
45                                                 struct cli_credentials *machine_account, 
46                                                 struct smb_krb5_context *smb_krb5_context,
47                                                 krb5_principal *salt_princ)
48 {
49         krb5_error_code ret;
50         char *machine_username;
51         char *salt_body;
52         char *lower_realm;
53         struct principal_container *mem_ctx = talloc(parent_ctx, struct principal_container);
54         if (!mem_ctx) {
55                 return ENOMEM;
56         }
57         
58         machine_username = talloc_strdup(mem_ctx, cli_credentials_get_username(machine_account));
59
60         if (!machine_username) {
61                 talloc_free(mem_ctx);
62                 return ENOMEM;
63         }
64
65         if (machine_username[strlen(machine_username)-1] == '$') {
66                 machine_username[strlen(machine_username)-1] = '\0';
67         }
68         lower_realm = strlower_talloc(mem_ctx, cli_credentials_get_realm(machine_account));
69         if (!lower_realm) {
70                 talloc_free(mem_ctx);
71                 return ENOMEM;
72         }
73
74         salt_body = talloc_asprintf(mem_ctx, "%s.%s", machine_username, 
75                                     lower_realm);
76         if (!salt_body) {
77                 talloc_free(mem_ctx);
78                 return ENOMEM;
79         }
80         
81         ret = krb5_make_principal(smb_krb5_context->krb5_context, salt_princ, 
82                                   cli_credentials_get_realm(machine_account), 
83                                   "host", salt_body, NULL);
84
85         if (ret == 0) {
86                 mem_ctx->smb_krb5_context = talloc_reference(mem_ctx, smb_krb5_context);
87                 mem_ctx->principal = *salt_princ;
88                 talloc_set_destructor(mem_ctx, free_principal);
89         }
90         return ret;
91 }
92
93 krb5_error_code principal_from_credentials(TALLOC_CTX *parent_ctx, 
94                                            struct cli_credentials *credentials, 
95                                            struct smb_krb5_context *smb_krb5_context,
96                                            krb5_principal *princ)
97 {
98         krb5_error_code ret;
99         const char *princ_string;
100         struct principal_container *mem_ctx = talloc(parent_ctx, struct principal_container);
101         if (!mem_ctx) {
102                 return ENOMEM;
103         }
104         
105         princ_string = cli_credentials_get_principal(credentials, mem_ctx);
106
107         if (!princ_string) {
108                 talloc_free(mem_ctx);
109                 princ = NULL;
110                 return 0;
111         }
112
113         ret = krb5_parse_name(smb_krb5_context->krb5_context,
114                               princ_string, princ);
115
116         if (ret == 0) {
117                 mem_ctx->smb_krb5_context = talloc_reference(mem_ctx, smb_krb5_context);
118                 mem_ctx->principal = *princ;
119                 talloc_set_destructor(mem_ctx, free_principal);
120         }
121         return ret;
122 }
123
124 /**
125  * Return a freshly allocated ccache (destroyed by destructor on child
126  * of parent_ctx), for a given set of client credentials 
127  */
128
129  krb5_error_code kinit_to_ccache(TALLOC_CTX *parent_ctx,
130                                  struct cli_credentials *credentials,
131                                  struct smb_krb5_context *smb_krb5_context,
132                                  krb5_ccache ccache) 
133 {
134         krb5_error_code ret;
135         const char *password;
136         time_t kdc_time = 0;
137         krb5_principal princ;
138
139         TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
140
141         if (!mem_ctx) {
142                 return ENOMEM;
143         }
144
145         ret = principal_from_credentials(mem_ctx, credentials, smb_krb5_context, &princ);
146         if (ret) {
147                 talloc_free(mem_ctx);
148                 return ret;
149         }
150
151         password = cli_credentials_get_password(credentials);
152         
153         if (password) {
154                 ret = kerberos_kinit_password_cc(smb_krb5_context->krb5_context, ccache, 
155                                                  princ, 
156                                                  password, NULL, &kdc_time);
157         } else {
158                 /* No password available, try to use a keyblock instead */
159
160                 krb5_keyblock keyblock;
161                 const struct samr_Password *mach_pwd;
162                 mach_pwd = cli_credentials_get_nt_hash(credentials, mem_ctx);
163                 if (!mach_pwd) {
164                         talloc_free(mem_ctx);
165                         DEBUG(1, ("kinit_to_ccache: No password available for kinit\n"));
166                         return EINVAL;
167                 }
168                 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
169                                          ENCTYPE_ARCFOUR_HMAC,
170                                          mach_pwd->hash, sizeof(mach_pwd->hash), 
171                                          &keyblock);
172                 
173                 if (ret == 0) {
174                         ret = kerberos_kinit_keyblock_cc(smb_krb5_context->krb5_context, ccache, 
175                                                          princ,
176                                                          &keyblock, NULL, &kdc_time);
177                         krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &keyblock);
178                 }
179         }
180
181         /* cope with ticket being in the future due to clock skew */
182         if ((unsigned)kdc_time > time(NULL)) {
183                 time_t t = time(NULL);
184                 int time_offset =(unsigned)kdc_time-t;
185                 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset));
186                 krb5_set_real_time(smb_krb5_context->krb5_context, t + time_offset + 1, 0);
187         }
188         
189         if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
190                 DEBUG(1,("kinit for %s failed (%s)\n", 
191                          cli_credentials_get_principal(credentials, mem_ctx), 
192                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
193                                                     ret, mem_ctx)));
194                 talloc_free(mem_ctx);
195                 return ret;
196         }
197         if (ret) {
198                 DEBUG(1,("kinit for %s failed (%s)\n", 
199                          cli_credentials_get_principal(credentials, mem_ctx), 
200                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
201                                                     ret, mem_ctx)));
202                 talloc_free(mem_ctx);
203                 return ret;
204         } 
205         return 0;
206 }
207
208 static int free_keytab(void *ptr) {
209         struct keytab_container *ktc = ptr;
210         krb5_kt_close(ktc->smb_krb5_context->krb5_context, ktc->keytab);
211
212         return 0;
213 }
214
215  int create_memory_keytab(TALLOC_CTX *parent_ctx,
216                           struct cli_credentials *machine_account,
217                           struct smb_krb5_context *smb_krb5_context,
218                           struct keytab_container **keytab_container) 
219 {
220         krb5_error_code ret;
221         const char *password_s;
222         char *old_secret;
223         krb5_data password;
224         int i, kvno;
225         krb5_enctype *enctypes;
226         krb5_principal salt_princ;
227         krb5_principal princ;
228         krb5_keytab keytab;
229
230         TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
231         if (!mem_ctx) {
232                 return ENOMEM;
233         }
234         
235         *keytab_container = talloc(mem_ctx, struct keytab_container);
236
237         ret = krb5_kt_resolve(smb_krb5_context->krb5_context, "MEMORY:", &keytab);
238         if (ret) {
239                 DEBUG(1,("failed to generate a new krb5 keytab: %s\n", 
240                          error_message(ret)));
241                 talloc_free(mem_ctx);
242                 return ret;
243         }
244
245         (*keytab_container)->smb_krb5_context = talloc_reference(*keytab_container, smb_krb5_context);
246         (*keytab_container)->keytab = keytab;
247
248         talloc_set_destructor(*keytab_container, free_keytab);
249
250         ret = salt_principal_from_credentials(mem_ctx, machine_account, 
251                                               smb_krb5_context, 
252                                               &salt_princ);
253         if (ret) {
254                 DEBUG(1,("create_memory_keytab: maksing salt principal failed (%s)\n",
255                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
256                                                     ret, mem_ctx)));
257                 talloc_free(mem_ctx);
258                 return ret;
259         }
260
261         ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
262         if (ret) {
263                 DEBUG(1,("create_memory_keytab: maksing krb5 principal failed (%s)\n",
264                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
265                                                     ret, mem_ctx)));
266                 talloc_free(mem_ctx);
267                 return ret;
268         }
269
270         password_s = cli_credentials_get_password(machine_account);
271         if (!password_s) {
272                 /* If we don't have the plaintext password, try for
273                  * the MD4 password hash */
274
275                 krb5_keytab_entry entry;
276                 const struct samr_Password *mach_pwd;
277                 mach_pwd = cli_credentials_get_nt_hash(machine_account, mem_ctx);
278                 if (!mach_pwd) {
279                         talloc_free(mem_ctx);
280                         DEBUG(1, ("create_memory_keytab: Domain trust informaton for account %s not available\n",
281                                   cli_credentials_get_principal(machine_account, mem_ctx)));
282                         return EINVAL;
283                 }
284                 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
285                                          ENCTYPE_ARCFOUR_HMAC,
286                                          mach_pwd->hash, sizeof(mach_pwd->hash), 
287                                          &entry.keyblock);
288                 if (ret) {
289                         DEBUG(1, ("create_memory_keytab: krb5_keyblock_init failed: %s\n",
290                                   smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
291                                                              ret, mem_ctx)));
292                         return ret;
293                 }
294
295                 entry.principal = princ;
296                 entry.vno       = cli_credentials_get_kvno(machine_account);
297                 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
298                 if (ret) {
299                         DEBUG(1, ("Failed to add ARCFOUR_HMAC (only) entry for %s to keytab: %s",
300                                   cli_credentials_get_principal(machine_account, mem_ctx), 
301                                   smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
302                                                              ret, mem_ctx)));
303                         talloc_free(mem_ctx);
304                         krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
305                         return ret;
306                 }
307                 
308                 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
309
310                 talloc_steal(parent_ctx, *keytab_container);
311                 talloc_free(mem_ctx);
312                 return 0;
313         }
314                 
315         /* good, we actually have the real plaintext */
316
317         ret = get_kerberos_allowed_etypes(smb_krb5_context->krb5_context, 
318                                           &enctypes);
319         if (ret) {
320                 DEBUG(1,("create_memory_keytab: getting encrption types failed (%s)\n",
321                          error_message(ret)));
322                 talloc_free(mem_ctx);
323                 return ret;
324         }
325
326         password.data = discard_const_p(char *, password_s);
327         password.length = strlen(password_s);
328         kvno = cli_credentials_get_kvno(machine_account);
329
330         for (i=0; enctypes[i]; i++) {
331                 krb5_keytab_entry entry;
332                 ret = create_kerberos_key_from_string(smb_krb5_context->krb5_context, 
333                                                       salt_princ, &password, &entry.keyblock, enctypes[i]);
334                 if (ret) {
335                         talloc_free(mem_ctx);
336                         return ret;
337                 }
338
339                 entry.principal = princ;
340                 entry.vno       = kvno;
341                 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
342                 if (ret) {
343                         DEBUG(1, ("Failed to add entry for %s to keytab: %s",
344                                   cli_credentials_get_principal(machine_account, mem_ctx), 
345                                   smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
346                                                              ret, mem_ctx)));
347                         talloc_free(mem_ctx);
348                         krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
349                         return ret;
350                 }
351                 
352                 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
353         }
354
355         old_secret = cli_credentials_get_old_password(machine_account);
356         if (kvno != 0 && old_secret) {
357                 password.data = discard_const_p(char *, old_secret);
358                 password.length = strlen(old_secret);
359                 
360                 for (i=0; enctypes[i]; i++) {
361                         krb5_keytab_entry entry;
362                         ret = create_kerberos_key_from_string(smb_krb5_context->krb5_context, 
363                                                               salt_princ, &password, &entry.keyblock, enctypes[i]);
364                         if (ret) {
365                                 talloc_free(mem_ctx);
366                                 return ret;
367                         }
368                         
369                         entry.principal = princ;
370                         entry.vno       = kvno - 1;
371                         ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
372                         if (ret) {
373                                 DEBUG(1, ("Failed to add 'old password' entry for %s to keytab: %s",
374                                           cli_credentials_get_principal(machine_account, mem_ctx), 
375                                           smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
376                                                                      ret, mem_ctx)));
377                                 talloc_free(mem_ctx);
378                                 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
379                                 return ret;
380                         }
381                         
382                         krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
383                 }
384         }
385
386         free_kerberos_etypes(smb_krb5_context->krb5_context, enctypes);
387
388         talloc_steal(parent_ctx, *keytab_container);
389         talloc_free(mem_ctx);
390         return 0;
391 }
392
393