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