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