r23792: convert Samba4 to GPLv3
[garming/samba-autobuild/.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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "system/kerberos.h"
25 #include "auth/kerberos/kerberos.h"
26 #include "auth/credentials/credentials.h"
27 #include "auth/credentials/credentials_krb5.h"
28
29 struct principal_container {
30         struct smb_krb5_context *smb_krb5_context;
31         krb5_principal principal;
32 };
33
34 static int free_principal(struct principal_container *pc)
35 {
36         /* current heimdal - 0.6.3, which we need anyway, fixes segfaults here */
37         krb5_free_principal(pc->smb_krb5_context->krb5_context, pc->principal);
38
39         return 0;
40 }
41
42 static krb5_error_code salt_principal_from_credentials(TALLOC_CTX *parent_ctx, 
43                                                        struct cli_credentials *machine_account, 
44                                                        struct smb_krb5_context *smb_krb5_context,
45                                                        krb5_principal *salt_princ)
46 {
47         krb5_error_code ret;
48         char *machine_username;
49         char *salt_body;
50         char *lower_realm;
51         const char *salt_principal;
52         struct principal_container *mem_ctx = talloc(parent_ctx, struct principal_container);
53         if (!mem_ctx) {
54                 return ENOMEM;
55         }
56
57         salt_principal = cli_credentials_get_salt_principal(machine_account);
58         if (salt_principal) {
59                 ret = krb5_parse_name(smb_krb5_context->krb5_context, salt_principal, salt_princ); 
60         } else {
61                 machine_username = talloc_strdup(mem_ctx, cli_credentials_get_username(machine_account));
62                 
63                 if (!machine_username) {
64                         talloc_free(mem_ctx);
65                         return ENOMEM;
66                 }
67                 
68                 if (machine_username[strlen(machine_username)-1] == '$') {
69                         machine_username[strlen(machine_username)-1] = '\0';
70                 }
71                 lower_realm = strlower_talloc(mem_ctx, cli_credentials_get_realm(machine_account));
72                 if (!lower_realm) {
73                         talloc_free(mem_ctx);
74                         return ENOMEM;
75                 }
76                 
77                 salt_body = talloc_asprintf(mem_ctx, "%s.%s", machine_username, 
78                                             lower_realm);
79                 if (!salt_body) {
80                         talloc_free(mem_ctx);
81                 return ENOMEM;
82                 }
83                 
84                 ret = krb5_make_principal(smb_krb5_context->krb5_context, salt_princ, 
85                                           cli_credentials_get_realm(machine_account), 
86                                           "host", salt_body, NULL);
87         } 
88
89         if (ret == 0) {
90                 /* This song-and-dance effectivly puts the principal
91                  * into talloc, so we can't loose it. */
92                 mem_ctx->smb_krb5_context = talloc_reference(mem_ctx, smb_krb5_context);
93                 mem_ctx->principal = *salt_princ;
94                 talloc_set_destructor(mem_ctx, free_principal);
95         }
96         return ret;
97 }
98
99 /* Obtain the principal set on this context.  Requires a
100  * smb_krb5_context because we are doing krb5 principal parsing with
101  * the library routines.  The returned princ is placed in the talloc
102  * system by means of a destructor (do *not* free). */
103
104 krb5_error_code principal_from_credentials(TALLOC_CTX *parent_ctx, 
105                                            struct cli_credentials *credentials, 
106                                            struct smb_krb5_context *smb_krb5_context,
107                                            krb5_principal *princ)
108 {
109         krb5_error_code ret;
110         const char *princ_string;
111         struct principal_container *mem_ctx = talloc(parent_ctx, struct principal_container);
112         if (!mem_ctx) {
113                 return ENOMEM;
114         }
115         
116         princ_string = cli_credentials_get_principal(credentials, mem_ctx);
117
118         /* A NULL here has meaning, as the gssapi server case will
119          * then use the principal from the client */
120         if (!princ_string) {
121                 talloc_free(mem_ctx);
122                 princ = NULL;
123                 return 0;
124         }
125
126         ret = krb5_parse_name(smb_krb5_context->krb5_context,
127                               princ_string, princ);
128
129         if (ret == 0) {
130                 /* This song-and-dance effectivly puts the principal
131                  * into talloc, so we can't loose it. */
132                 mem_ctx->smb_krb5_context = talloc_reference(mem_ctx, smb_krb5_context);
133                 mem_ctx->principal = *princ;
134                 talloc_set_destructor(mem_ctx, free_principal);
135         }
136         return ret;
137 }
138
139 /**
140  * Return a freshly allocated ccache (destroyed by destructor on child
141  * of parent_ctx), for a given set of client credentials 
142  */
143
144  krb5_error_code kinit_to_ccache(TALLOC_CTX *parent_ctx,
145                                  struct cli_credentials *credentials,
146                                  struct smb_krb5_context *smb_krb5_context,
147                                  krb5_ccache ccache) 
148 {
149         krb5_error_code ret;
150         const char *password;
151         time_t kdc_time = 0;
152         krb5_principal princ;
153         int tries;
154         TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
155
156         if (!mem_ctx) {
157                 return ENOMEM;
158         }
159
160         ret = principal_from_credentials(mem_ctx, credentials, smb_krb5_context, &princ);
161         if (ret) {
162                 talloc_free(mem_ctx);
163                 return ret;
164         }
165
166         password = cli_credentials_get_password(credentials);
167
168         tries = 2;
169         while (tries--) {
170                 if (password) {
171                         ret = kerberos_kinit_password_cc(smb_krb5_context->krb5_context, ccache, 
172                                                          princ, 
173                                                          password, NULL, &kdc_time);
174                 } else {
175                         /* No password available, try to use a keyblock instead */
176                         
177                         krb5_keyblock keyblock;
178                         const struct samr_Password *mach_pwd;
179                         mach_pwd = cli_credentials_get_nt_hash(credentials, mem_ctx);
180                         if (!mach_pwd) {
181                                 talloc_free(mem_ctx);
182                                 DEBUG(1, ("kinit_to_ccache: No password available for kinit\n"));
183                                 return EINVAL;
184                         }
185                         ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
186                                                  ETYPE_ARCFOUR_HMAC_MD5,
187                                                  mach_pwd->hash, sizeof(mach_pwd->hash), 
188                                                  &keyblock);
189                         
190                         if (ret == 0) {
191                                 ret = kerberos_kinit_keyblock_cc(smb_krb5_context->krb5_context, ccache, 
192                                                                  princ,
193                                                                  &keyblock, NULL, &kdc_time);
194                                 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &keyblock);
195                         }
196                 }
197
198                 if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
199                         /* Perhaps we have been given an invalid skew, so try again without it */
200                         time_t t = time(NULL);
201                         krb5_set_real_time(smb_krb5_context->krb5_context, t, 0);
202                 } else {
203                         /* not a skew problem */
204                         break;
205                 }
206         }
207
208         if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
209                 DEBUG(1,("kinit for %s failed (%s)\n", 
210                          cli_credentials_get_principal(credentials, mem_ctx), 
211                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
212                                                     ret, mem_ctx)));
213                 talloc_free(mem_ctx);
214                 return ret;
215         }
216
217         /* cope with ticket being in the future due to clock skew */
218         if ((unsigned)kdc_time > time(NULL)) {
219                 time_t t = time(NULL);
220                 int time_offset =(unsigned)kdc_time-t;
221                 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset));
222                 krb5_set_real_time(smb_krb5_context->krb5_context, t + time_offset + 1, 0);
223         }
224         
225         if (ret == KRB5KDC_ERR_PREAUTH_FAILED && cli_credentials_wrong_password(credentials)) {
226                 ret = kinit_to_ccache(parent_ctx,
227                                       credentials,
228                                       smb_krb5_context,
229                                       ccache); 
230         }
231         if (ret) {
232                 DEBUG(1,("kinit for %s failed (%s)\n", 
233                          cli_credentials_get_principal(credentials, mem_ctx), 
234                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
235                                                     ret, mem_ctx)));
236                 talloc_free(mem_ctx);
237                 return ret;
238         } 
239         talloc_free(mem_ctx);
240         return 0;
241 }
242
243 static int free_keytab(struct keytab_container *ktc)
244 {
245         krb5_kt_close(ktc->smb_krb5_context->krb5_context, ktc->keytab);
246
247         return 0;
248 }
249
250 int smb_krb5_open_keytab(TALLOC_CTX *mem_ctx,
251                          struct smb_krb5_context *smb_krb5_context, 
252                          const char *keytab_name, struct keytab_container **ktc) 
253 {
254         krb5_keytab keytab;
255         int ret;
256         ret = krb5_kt_resolve(smb_krb5_context->krb5_context, keytab_name, &keytab);
257         if (ret) {
258                 DEBUG(1,("failed to open krb5 keytab: %s\n", 
259                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
260                                                     ret, mem_ctx)));
261                 return ret;
262         }
263
264         *ktc = talloc(mem_ctx, struct keytab_container);
265         if (!*ktc) {
266                 return ENOMEM;
267         }
268
269         (*ktc)->smb_krb5_context = talloc_reference(*ktc, smb_krb5_context);
270         (*ktc)->keytab = keytab;
271         talloc_set_destructor(*ktc, free_keytab);
272
273         return 0;
274 }
275
276 static krb5_error_code keytab_add_keys(TALLOC_CTX *parent_ctx,
277                                        const char *princ_string,
278                                        krb5_principal princ,
279                                        krb5_principal salt_princ,
280                                        int kvno,
281                                        const char *password_s,
282                                        struct smb_krb5_context *smb_krb5_context,
283                                        const char **enctype_strings,
284                                        krb5_keytab keytab)
285 {
286         int i;
287         krb5_error_code ret;
288         krb5_data password;
289         TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
290         if (!mem_ctx) {
291                 return ENOMEM;
292         }
293
294         password.data = discard_const_p(char *, password_s);
295         password.length = strlen(password_s);
296
297         for (i=0; enctype_strings[i]; i++) {
298                 krb5_keytab_entry entry;
299                 krb5_enctype enctype;
300                 ret = krb5_string_to_enctype(smb_krb5_context->krb5_context, enctype_strings[i], &enctype);
301                 if (ret != 0) {
302                         DEBUG(1, ("Failed to interpret %s as a krb5 encryption type: %s\n",                               
303                                   enctype_strings[i],
304                                   smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
305                                                              ret, mem_ctx)));
306                         talloc_free(mem_ctx);
307                         return ret;
308                 }
309                 ret = create_kerberos_key_from_string(smb_krb5_context->krb5_context, 
310                                                       salt_princ, &password, &entry.keyblock, enctype);
311                 if (ret != 0) {
312                         talloc_free(mem_ctx);
313                         return ret;
314                 }
315
316                 entry.principal = princ;
317                 entry.vno       = kvno;
318                 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
319                 if (ret != 0) {
320                         DEBUG(1, ("Failed to add %s entry for %s(kvno %d) to keytab: %s\n",
321                                   enctype_strings[i],
322                                   princ_string,
323                                   kvno,
324                                   smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
325                                                              ret, mem_ctx)));
326                         talloc_free(mem_ctx);
327                         krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
328                         return ret;
329                 }
330
331                 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n", 
332                           princ_string, kvno,
333                           enctype_strings[i]));
334                 
335                 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
336         }
337         talloc_free(mem_ctx);
338         return 0;
339 }
340
341 static int create_keytab(TALLOC_CTX *parent_ctx,
342                          struct cli_credentials *machine_account,
343                          struct smb_krb5_context *smb_krb5_context,
344                          const char **enctype_strings,
345                          krb5_keytab keytab,
346                          BOOL add_old) 
347 {
348         krb5_error_code ret;
349         const char *password_s;
350         const char *old_secret;
351         int kvno;
352         krb5_principal salt_princ;
353         krb5_principal princ;
354         const char *princ_string;
355
356         TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
357         if (!mem_ctx) {
358                 return ENOMEM;
359         }
360
361         princ_string = cli_credentials_get_principal(machine_account, mem_ctx);
362         /* Get the principal we will store the new keytab entries under */
363         ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
364         if (ret) {
365                 DEBUG(1,("create_keytab: makeing krb5 principal failed (%s)\n",
366                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
367                                                     ret, mem_ctx)));
368                 talloc_free(mem_ctx);
369                 return ret;
370         }
371
372         /* The salt used to generate these entries may be different however, fetch that */
373         ret = salt_principal_from_credentials(mem_ctx, machine_account, 
374                                               smb_krb5_context, 
375                                               &salt_princ);
376         if (ret) {
377                 DEBUG(1,("create_keytab: makeing salt principal failed (%s)\n",
378                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
379                                                     ret, mem_ctx)));
380                 talloc_free(mem_ctx);
381                 return ret;
382         }
383
384         /* Finally, do the dance to get the password to put in the entry */
385         password_s = cli_credentials_get_password(machine_account);
386         if (!password_s) {
387                 krb5_keytab_entry entry;
388                 const struct samr_Password *mach_pwd;
389
390                 if (!str_list_check(enctype_strings, "arcfour-hmac-md5")) {
391                         DEBUG(1, ("Asked to create keytab, but with only an NT hash supplied, "
392                                   "but not listing arcfour-hmac-md5 as an enc type to include in the keytab!\n"));
393                         talloc_free(mem_ctx);
394                         return EINVAL;
395                 }
396
397                 /* If we don't have the plaintext password, try for
398                  * the MD4 password hash */
399                 mach_pwd = cli_credentials_get_nt_hash(machine_account, mem_ctx);
400                 if (!mach_pwd) {
401                         /* OK, nothing to do here */
402                         talloc_free(mem_ctx);
403                         return 0;
404                 }
405                 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
406                                          ETYPE_ARCFOUR_HMAC_MD5,
407                                          mach_pwd->hash, sizeof(mach_pwd->hash), 
408                                          &entry.keyblock);
409                 if (ret) {
410                         DEBUG(1, ("create_keytab: krb5_keyblock_init failed: %s\n",
411                                   smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
412                                                              ret, mem_ctx)));
413                         talloc_free(mem_ctx);
414                         return ret;
415                 }
416
417                 entry.principal = princ;
418                 entry.vno       = cli_credentials_get_kvno(machine_account);
419                 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
420                 if (ret) {
421                         DEBUG(1, ("Failed to add ARCFOUR_HMAC (only) entry for %s to keytab: %s",
422                                   cli_credentials_get_principal(machine_account, mem_ctx), 
423                                   smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
424                                                              ret, mem_ctx)));
425                         talloc_free(mem_ctx);
426                         krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
427                         return ret;
428                 }
429                 
430                 DEBUG(5, ("Added %s(kvno %d) to keytab (arcfour-hmac-md5)\n", 
431                           cli_credentials_get_principal(machine_account, mem_ctx),
432                           cli_credentials_get_kvno(machine_account)));
433
434                 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
435
436                 /* Can't go any further, we only have this one key */
437                 talloc_free(mem_ctx);
438                 return 0;
439         }
440         
441         kvno = cli_credentials_get_kvno(machine_account);
442         /* good, we actually have the real plaintext */
443         ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ, 
444                               kvno, password_s, smb_krb5_context, 
445                               enctype_strings, keytab);
446         if (!ret) {
447                 talloc_free(mem_ctx);
448                 return ret;
449         }
450
451         if (!add_old || kvno == 0) {
452                 talloc_free(mem_ctx);
453                 return 0;
454         }
455
456         old_secret = cli_credentials_get_old_password(machine_account);
457         if (!old_secret) {
458                 talloc_free(mem_ctx);
459                 return 0;
460         }
461         
462         ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ, 
463                               kvno - 1, old_secret, smb_krb5_context, 
464                               enctype_strings, keytab);
465         if (!ret) {
466                 talloc_free(mem_ctx);
467                 return ret;
468         }
469
470         talloc_free(mem_ctx);
471         return 0;
472 }
473
474
475 /*
476  * Walk the keytab, looking for entries of this principal name, with KVNO other than current kvno -1.
477  *
478  * These entries are now stale, we only keep the current, and previous entries around.
479  *
480  * Inspired by the code in Samba3 for 'use kerberos keytab'.
481  *
482  */
483
484 static krb5_error_code remove_old_entries(TALLOC_CTX *parent_ctx,
485                                           struct cli_credentials *machine_account,
486                                           struct smb_krb5_context *smb_krb5_context,
487                                           krb5_keytab keytab, BOOL *found_previous)
488 {
489         krb5_error_code ret, ret2;
490         krb5_kt_cursor cursor;
491         krb5_principal princ;
492         int kvno;
493         TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
494         const char *princ_string;
495         if (!mem_ctx) {
496                 return ENOMEM;
497         }
498
499         *found_previous = False;
500         princ_string = cli_credentials_get_principal(machine_account, mem_ctx);
501
502         /* Get the principal we will store the new keytab entries under */
503         ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
504         if (ret) {
505                 DEBUG(1,("update_keytab: makeing krb5 principal failed (%s)\n",
506                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
507                                                     ret, mem_ctx)));
508                 talloc_free(mem_ctx);
509                 return ret;
510         }
511
512         kvno = cli_credentials_get_kvno(machine_account);
513
514         /* for each entry in the keytab */
515         ret = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
516         switch (ret) {
517         case 0:
518                 break;
519         case HEIM_ERR_OPNOTSUPP:
520         case ENOENT:
521         case KRB5_KT_END:
522                 /* no point enumerating if there isn't anything here */
523                 talloc_free(mem_ctx);
524                 return 0;
525         default:
526                 DEBUG(1,("failed to open keytab for read of old entries: %s\n",
527                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
528                                                     ret, mem_ctx)));
529                 talloc_free(mem_ctx);
530                 return ret;
531         }
532
533         while (!ret) {
534                 krb5_keytab_entry entry;
535                 ret = krb5_kt_next_entry(smb_krb5_context->krb5_context, keytab, &entry, &cursor);
536                 if (ret) {
537                         break;
538                 }
539                 /* if it matches our principal */
540                 if (!krb5_kt_compare(smb_krb5_context->krb5_context, &entry, princ, 0, 0)) {
541                         /* Free the entry, it wasn't the one we were looking for anyway */
542                         krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
543                         continue;
544                 }
545
546                 /* delete it, if it is not kvno -1 */
547                 if (entry.vno != (kvno - 1 )) {
548                         /* Release the enumeration.  We are going to
549                          * have to start this from the top again,
550                          * because deletes during enumeration may not
551                          * always be consistant.
552                          *
553                          * Also, the enumeration locks a FILE: keytab
554                          */
555                 
556                         krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
557
558                         ret = krb5_kt_remove_entry(smb_krb5_context->krb5_context, keytab, &entry);
559                         krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
560
561                         /* Deleted: Restart from the top */
562                         ret2 = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
563                         if (ret2) {
564                                 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
565                                 DEBUG(1,("failed to restart enumeration of keytab: %s\n",
566                                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
567                                                                     ret, mem_ctx)));
568                                 
569                                 talloc_free(mem_ctx);
570                                 return ret2;
571                         }
572
573                         if (ret) {
574                                 break;
575                         }
576                         
577                 } else {
578                         *found_previous = True;
579                 }
580                 
581                 /* Free the entry, we don't need it any more */
582                 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
583                 
584                 
585         }
586         krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
587
588         switch (ret) {
589         case 0:
590                 break;
591         case ENOENT:
592         case KRB5_KT_END:
593                 ret = 0;
594                 break;
595         default:
596                 DEBUG(1,("failed in deleting old entries for principal: %s: %s\n",
597                          princ_string, 
598                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
599                                                     ret, mem_ctx)));
600         }
601         talloc_free(mem_ctx);
602         return ret;
603 }
604
605 int smb_krb5_update_keytab(TALLOC_CTX *parent_ctx,
606                            struct cli_credentials *machine_account,
607                            struct smb_krb5_context *smb_krb5_context,
608                            const char **enctype_strings,
609                            struct keytab_container *keytab_container) 
610 {
611         krb5_error_code ret;
612         BOOL found_previous;
613         TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
614         if (!mem_ctx) {
615                 return ENOMEM;
616         }
617
618         ret = remove_old_entries(mem_ctx, machine_account, 
619                                  smb_krb5_context, keytab_container->keytab, &found_previous);
620         if (ret != 0) {
621                 talloc_free(mem_ctx);
622                 return ret;
623         }
624         
625         /* Create a new keytab.  If during the cleanout we found
626          * entires for kvno -1, then don't try and duplicate them.
627          * Otherwise, add kvno, and kvno -1 */
628         
629         ret = create_keytab(mem_ctx, machine_account, smb_krb5_context, 
630                             enctype_strings, 
631                             keytab_container->keytab, 
632                             found_previous ? False : True);
633         talloc_free(mem_ctx);
634         return ret;
635 }
636
637 _PUBLIC_ int smb_krb5_create_memory_keytab(TALLOC_CTX *parent_ctx,
638                                            struct cli_credentials *machine_account,
639                                            struct smb_krb5_context *smb_krb5_context,
640                                            const char **enctype_strings,
641                                            struct keytab_container **keytab_container) 
642 {
643         krb5_error_code ret;
644         TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
645         const char *rand_string;
646         const char *keytab_name;
647         if (!mem_ctx) {
648                 return ENOMEM;
649         }
650         
651         *keytab_container = talloc(mem_ctx, struct keytab_container);
652
653         rand_string = generate_random_str(mem_ctx, 16);
654         if (!rand_string) {
655                 talloc_free(mem_ctx);
656                 return ENOMEM;
657         }
658
659         keytab_name = talloc_asprintf(mem_ctx, "MEMORY:%s", 
660                                       rand_string);
661         if (!keytab_name) {
662                 talloc_free(mem_ctx);
663                 return ENOMEM;
664         }
665
666         ret = smb_krb5_open_keytab(mem_ctx, smb_krb5_context, keytab_name, keytab_container);
667         if (ret) {
668                 return ret;
669         }
670
671         ret = smb_krb5_update_keytab(mem_ctx, machine_account, smb_krb5_context, enctype_strings, *keytab_container);
672         if (ret == 0) {
673                 talloc_steal(parent_ctx, *keytab_container);
674         } else {
675                 *keytab_container = NULL;
676         }
677         talloc_free(mem_ctx);
678         return ret;
679 }
680