r13107: Follow the lead of Heimdal's kpasswdd and use the HDB (hdb-ldb in our
[mat/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 "auth/kerberos/kerberos.h"
27 #include "auth/auth.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(void *ptr) {
35         struct principal_container *pc = ptr;
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 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
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         if (password) {
169                 ret = kerberos_kinit_password_cc(smb_krb5_context->krb5_context, ccache, 
170                                                  princ, 
171                                                  password, NULL, &kdc_time);
172         } else {
173                 /* No password available, try to use a keyblock instead */
174
175                 krb5_keyblock keyblock;
176                 const struct samr_Password *mach_pwd;
177                 mach_pwd = cli_credentials_get_nt_hash(credentials, mem_ctx);
178                 if (!mach_pwd) {
179                         talloc_free(mem_ctx);
180                         DEBUG(1, ("kinit_to_ccache: No password available for kinit\n"));
181                         return EINVAL;
182                 }
183                 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
184                                          ENCTYPE_ARCFOUR_HMAC,
185                                          mach_pwd->hash, sizeof(mach_pwd->hash), 
186                                          &keyblock);
187                 
188                 if (ret == 0) {
189                         ret = kerberos_kinit_keyblock_cc(smb_krb5_context->krb5_context, ccache, 
190                                                          princ,
191                                                          &keyblock, NULL, &kdc_time);
192                         krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &keyblock);
193                 }
194         }
195
196         /* cope with ticket being in the future due to clock skew */
197         if ((unsigned)kdc_time > time(NULL)) {
198                 time_t t = time(NULL);
199                 int time_offset =(unsigned)kdc_time-t;
200                 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset));
201                 krb5_set_real_time(smb_krb5_context->krb5_context, t + time_offset + 1, 0);
202         }
203         
204         if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
205                 DEBUG(1,("kinit for %s failed (%s)\n", 
206                          cli_credentials_get_principal(credentials, mem_ctx), 
207                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
208                                                     ret, mem_ctx)));
209                 talloc_free(mem_ctx);
210                 return ret;
211         }
212         if (ret) {
213                 DEBUG(1,("kinit for %s failed (%s)\n", 
214                          cli_credentials_get_principal(credentials, mem_ctx), 
215                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
216                                                     ret, mem_ctx)));
217                 talloc_free(mem_ctx);
218                 return ret;
219         } 
220         return 0;
221 }
222
223 static int free_keytab(void *ptr) {
224         struct keytab_container *ktc = ptr;
225         krb5_kt_close(ktc->smb_krb5_context->krb5_context, ktc->keytab);
226
227         return 0;
228 }
229
230 int smb_krb5_open_keytab(TALLOC_CTX *mem_ctx,
231                          struct smb_krb5_context *smb_krb5_context, 
232                          const char *keytab_name, struct keytab_container **ktc) 
233 {
234         krb5_keytab keytab;
235         int ret;
236         ret = krb5_kt_resolve(smb_krb5_context->krb5_context, keytab_name, &keytab);
237         if (ret) {
238                 DEBUG(1,("failed to open krb5 keytab: %s\n", 
239                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
240                                                     ret, mem_ctx)));
241                 return ret;
242         }
243
244         *ktc = talloc(mem_ctx, struct keytab_container);
245         if (!*ktc) {
246                 return ENOMEM;
247         }
248
249         (*ktc)->smb_krb5_context = talloc_reference(*ktc, smb_krb5_context);
250         (*ktc)->keytab = keytab;
251         talloc_set_destructor(*ktc, free_keytab);
252
253         return 0;
254 }
255
256 struct enctypes_container {
257         struct smb_krb5_context *smb_krb5_context;
258         krb5_enctype *enctypes;
259 };
260
261 static int free_enctypes(void *ptr) {
262         struct enctypes_container *etc = ptr;
263         free_kerberos_etypes(etc->smb_krb5_context->krb5_context, etc->enctypes);
264         return 0;
265 }
266
267 static krb5_error_code keytab_add_keys(TALLOC_CTX *parent_ctx,
268                                        const char *princ_string,
269                                        krb5_principal princ,
270                                        krb5_principal salt_princ,
271                                        int kvno,
272                                        const char *password_s,
273                                        struct smb_krb5_context *smb_krb5_context,
274                                        krb5_keytab keytab)
275 {
276         int i;
277         krb5_error_code ret;
278         krb5_enctype *enctypes;
279         char *enctype_string;
280         struct enctypes_container *etc;
281         krb5_data password;
282         TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
283         if (!mem_ctx) {
284                 return ENOMEM;
285         }
286
287         etc = talloc(mem_ctx, struct enctypes_container);
288         if (!etc) {
289                 talloc_free(mem_ctx);
290                 return ENOMEM;
291         }
292         ret = get_kerberos_allowed_etypes(smb_krb5_context->krb5_context, 
293                                           &enctypes);
294         if (ret != 0) {
295                 DEBUG(1,("keytab_add_keys: getting encrption types failed (%s)\n",
296                          error_message(ret)));
297                 talloc_free(mem_ctx);
298                 return ret;
299         }
300
301         etc->smb_krb5_context = talloc_reference(etc, smb_krb5_context);
302         etc->enctypes = enctypes;
303
304         talloc_set_destructor(etc, free_enctypes);
305
306         password.data = discard_const_p(char *, password_s);
307         password.length = strlen(password_s);
308
309         for (i=0; enctypes[i]; i++) {
310                 krb5_keytab_entry entry;
311                 ret = create_kerberos_key_from_string(smb_krb5_context->krb5_context, 
312                                                       salt_princ, &password, &entry.keyblock, enctypes[i]);
313                 if (ret != 0) {
314                         talloc_free(mem_ctx);
315                         return ret;
316                 }
317
318                 entry.principal = princ;
319                 entry.vno       = kvno;
320                 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
321                 enctype_string = NULL;
322                 krb5_enctype_to_string(smb_krb5_context->krb5_context, enctypes[i], &enctype_string);
323                 if (ret != 0) {
324                         DEBUG(1, ("Failed to add %s entry for %s(kvno %d) to keytab: %s\n",
325                                   enctype_string,
326                                   princ_string,
327                                   kvno,
328                                   smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
329                                                              ret, mem_ctx)));
330                         talloc_free(mem_ctx);
331                         free(enctype_string);           
332                         krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
333                         return ret;
334                 }
335
336                 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n", 
337                           princ_string, kvno,
338                           enctype_string));
339                 free(enctype_string);           
340                 
341                 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
342         }
343         talloc_free(mem_ctx);
344         return 0;
345 }
346
347 static int create_keytab(TALLOC_CTX *parent_ctx,
348                          struct cli_credentials *machine_account,
349                          struct smb_krb5_context *smb_krb5_context,
350                          krb5_keytab keytab,
351                          BOOL add_old) 
352 {
353         krb5_error_code ret;
354         const char *password_s;
355         char *enctype_string;
356         const char *old_secret;
357         int kvno;
358         krb5_principal salt_princ;
359         krb5_principal princ;
360         const char *princ_string;
361
362         TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
363         if (!mem_ctx) {
364                 return ENOMEM;
365         }
366
367         princ_string = cli_credentials_get_principal(machine_account, mem_ctx);
368         /* Get the principal we will store the new keytab entries under */
369         ret = principal_from_credentials(mem_ctx, machine_account, smb_krb5_context, &princ);
370         if (ret) {
371                 DEBUG(1,("create_keytab: makeing krb5 principal failed (%s)\n",
372                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
373                                                     ret, mem_ctx)));
374                 talloc_free(mem_ctx);
375                 return ret;
376         }
377
378         /* The salt used to generate these entries may be different however, fetch that */
379         ret = salt_principal_from_credentials(mem_ctx, machine_account, 
380                                               smb_krb5_context, 
381                                               &salt_princ);
382         if (ret) {
383                 DEBUG(1,("create_keytab: makeing salt principal failed (%s)\n",
384                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
385                                                     ret, mem_ctx)));
386                 talloc_free(mem_ctx);
387                 return ret;
388         }
389
390         /* Finally, do the dance to get the password to put in the entry */
391         password_s = cli_credentials_get_password(machine_account);
392         if (!password_s) {
393                 /* If we don't have the plaintext password, try for
394                  * the MD4 password hash */
395
396                 krb5_keytab_entry entry;
397                 const struct samr_Password *mach_pwd;
398                 mach_pwd = cli_credentials_get_nt_hash(machine_account, mem_ctx);
399                 if (!mach_pwd) {
400                         DEBUG(1, ("create_keytab: Domain trust informaton for account %s not available\n",
401                                   cli_credentials_get_principal(machine_account, mem_ctx)));
402                         talloc_free(mem_ctx);
403                         return EINVAL;
404                 }
405                 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
406                                          ENCTYPE_ARCFOUR_HMAC,
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                 krb5_enctype_to_string(smb_krb5_context->krb5_context, ENCTYPE_ARCFOUR_HMAC, &enctype_string);
431                 DEBUG(5, ("Added %s(kvno %d) to keytab (%s)\n", 
432                           cli_credentials_get_principal(machine_account, mem_ctx),
433                           cli_credentials_get_kvno(machine_account),
434                           enctype_string));
435                 free(enctype_string);           
436
437                 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
438
439                 /* Can't go any further, we only have this one key */
440                 talloc_free(mem_ctx);
441                 return 0;
442         }
443         
444         kvno = cli_credentials_get_kvno(machine_account);
445         /* good, we actually have the real plaintext */
446         ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ, 
447                               kvno, password_s, smb_krb5_context, keytab);
448         if (!ret) {
449                 talloc_free(mem_ctx);
450                 return ret;
451         }
452
453         if (!add_old || kvno == 0) {
454                 talloc_free(mem_ctx);
455                 return 0;
456         }
457
458         old_secret = cli_credentials_get_old_password(machine_account);
459         if (!old_secret) {
460                 talloc_free(mem_ctx);
461                 return 0;
462         }
463         
464         ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ, 
465                               kvno - 1, old_secret, smb_krb5_context, 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                            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                             keytab_container->keytab, 
631                             found_previous ? False : True);
632         talloc_free(mem_ctx);
633         return ret;
634 }
635
636 int smb_krb5_create_memory_keytab(TALLOC_CTX *parent_ctx,
637                                   struct cli_credentials *machine_account,
638                                   struct smb_krb5_context *smb_krb5_context,
639                                   struct keytab_container **keytab_container) 
640 {
641         krb5_error_code ret;
642         TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
643         const char *rand_string;
644         const char *keytab_name;
645         if (!mem_ctx) {
646                 return ENOMEM;
647         }
648         
649         *keytab_container = talloc(mem_ctx, struct keytab_container);
650
651         rand_string = generate_random_str(mem_ctx, 16);
652         if (!rand_string) {
653                 talloc_free(mem_ctx);
654                 return ENOMEM;
655         }
656
657         keytab_name = talloc_asprintf(mem_ctx, "MEMORY:%s", 
658                                       rand_string);
659         if (!keytab_name) {
660                 talloc_free(mem_ctx);
661                 return ENOMEM;
662         }
663
664         ret = smb_krb5_open_keytab(mem_ctx, smb_krb5_context, keytab_name, keytab_container);
665         if (ret) {
666                 return ret;
667         }
668
669         ret = smb_krb5_update_keytab(mem_ctx, machine_account, smb_krb5_context, *keytab_container);
670         if (ret == 0) {
671                 talloc_steal(parent_ctx, *keytab_container);
672         } else {
673                 *keytab_container = NULL;
674         }
675         talloc_free(mem_ctx);
676         return ret;
677 }
678