r13316: Let the carnage begin....
[gd/samba/.git] / source3 / libads / kerberos.c
1 /* 
2    Unix SMB/CIFS implementation.
3    kerberos utility library
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) Remus Koos 2001
6    Copyright (C) Nalin Dahyabhai <nalin@redhat.com> 2004.
7    Copyright (C) Jeremy Allison 2004.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
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
26 #ifdef HAVE_KRB5
27
28 #define LIBADS_CCACHE_NAME "MEMORY:libads"
29
30 /*
31   we use a prompter to avoid a crash bug in the kerberos libs when 
32   dealing with empty passwords
33   this prompter is just a string copy ...
34 */
35 static krb5_error_code 
36 kerb_prompter(krb5_context ctx, void *data,
37                const char *name,
38                const char *banner,
39                int num_prompts,
40                krb5_prompt prompts[])
41 {
42         if (num_prompts == 0) return 0;
43
44         memset(prompts[0].reply->data, '\0', prompts[0].reply->length);
45         if (prompts[0].reply->length > 0) {
46                 if (data) {
47                         strncpy(prompts[0].reply->data, data, prompts[0].reply->length-1);
48                         prompts[0].reply->length = strlen(prompts[0].reply->data);
49                 } else {
50                         prompts[0].reply->length = 0;
51                 }
52         }
53         return 0;
54 }
55
56 /*
57   simulate a kinit, putting the tgt in the given cache location. If cache_name == NULL
58   place in default cache location.
59   remus@snapserver.com
60 */
61 int kerberos_kinit_password(const char *principal,
62                                 const char *password,
63                                 int time_offset,
64                                 time_t *expire_time,
65                                 time_t *renew_till_time,
66                                 const char *cache_name,
67                                 BOOL request_pac,
68                                 time_t renewable_time)
69 {
70         krb5_context ctx = NULL;
71         krb5_error_code code = 0;
72         krb5_ccache cc = NULL;
73         krb5_principal me;
74         krb5_creds my_creds;
75         krb5_get_init_creds_opt opt;
76
77         initialize_krb5_error_table();
78         if ((code = krb5_init_context(&ctx)))
79                 return code;
80
81         if (time_offset != 0) {
82                 krb5_set_real_time(ctx, time(NULL) + time_offset, 0);
83         }
84
85         DEBUG(10,("kerberos_kinit_password: using %s as ccache\n",
86                         cache_name ? cache_name: krb5_cc_default_name(ctx)));
87
88         if ((code = krb5_cc_resolve(ctx, cache_name ? cache_name : krb5_cc_default_name(ctx), &cc))) {
89                 krb5_free_context(ctx);
90                 return code;
91         }
92         
93         if ((code = krb5_parse_name(ctx, principal, &me))) {
94                 krb5_free_context(ctx); 
95                 return code;
96         }
97
98         krb5_get_init_creds_opt_init(&opt);
99         krb5_get_init_creds_opt_set_renew_life(&opt, renewable_time);
100         krb5_get_init_creds_opt_set_forwardable(&opt, 1);
101         
102         if (request_pac) {
103 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_SET_PAC_REQUEST
104                 krb5_get_init_creds_opt_set_pac_request(ctx, &opt, True);
105 #endif
106         }
107
108         if ((code = krb5_get_init_creds_password(ctx, &my_creds, me, CONST_DISCARD(char *,password), 
109                                                  kerb_prompter, 
110                                                  NULL, 0, NULL, &opt))) {
111                 krb5_free_principal(ctx, me);
112                 krb5_free_context(ctx);         
113                 return code;
114         }
115         
116         if ((code = krb5_cc_initialize(ctx, cc, me))) {
117                 krb5_free_cred_contents(ctx, &my_creds);
118                 krb5_free_principal(ctx, me);
119                 krb5_free_context(ctx);         
120                 return code;
121         }
122         
123         if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
124                 krb5_cc_close(ctx, cc);
125                 krb5_free_cred_contents(ctx, &my_creds);
126                 krb5_free_principal(ctx, me);
127                 krb5_free_context(ctx);         
128                 return code;
129         }
130
131         if (expire_time) {
132                 *expire_time = (time_t) my_creds.times.endtime;
133         }
134
135         if (renew_till_time) {
136                 *renew_till_time = (time_t) my_creds.times.renew_till;
137         }
138
139         krb5_cc_close(ctx, cc);
140         krb5_free_cred_contents(ctx, &my_creds);
141         krb5_free_principal(ctx, me);
142         krb5_free_context(ctx);         
143         
144         return 0;
145 }
146
147
148
149 /* run kinit to setup our ccache */
150 int ads_kinit_password(ADS_STRUCT *ads)
151 {
152         char *s;
153         int ret;
154         const char *account_name;
155         fstring acct_name;
156
157         if ( IS_DC ) {
158                 /* this will end up getting a ticket for DOMAIN@RUSTED.REA.LM */
159                 account_name = lp_workgroup();
160         } else {
161                 /* always use the sAMAccountName for security = domain */
162                 /* global_myname()$@REA.LM */
163                 if ( lp_security() == SEC_DOMAIN ) {
164                         fstr_sprintf( acct_name, "%s$", global_myname() );
165                         account_name = acct_name;
166                 }
167                 else 
168                         /* This looks like host/global_myname()@REA.LM */
169                         account_name = ads->auth.user_name;
170         }
171
172         if (asprintf(&s, "%s@%s", account_name, ads->auth.realm) == -1) {
173                 return KRB5_CC_NOMEM;
174         }
175
176         if (!ads->auth.password) {
177                 return KRB5_LIBOS_CANTREADPWD;
178         }
179         
180         ret = kerberos_kinit_password(s, ads->auth.password, ads->auth.time_offset,
181                         &ads->auth.expire, NULL, NULL, False, ads->auth.renewable);
182
183         if (ret) {
184                 DEBUG(0,("kerberos_kinit_password %s failed: %s\n", 
185                          s, error_message(ret)));
186         }
187         free(s);
188         return ret;
189 }
190
191 int ads_kdestroy(const char *cc_name)
192 {
193         krb5_error_code code;
194         krb5_context ctx = NULL;
195         krb5_ccache cc = NULL;
196
197         initialize_krb5_error_table();
198         if ((code = krb5_init_context (&ctx))) {
199                 DEBUG(3, ("ads_kdestroy: kdb5_init_context failed: %s\n", 
200                         error_message(code)));
201                 return code;
202         }
203   
204         if (!cc_name) {
205                 if ((code = krb5_cc_default(ctx, &cc))) {
206                         krb5_free_context(ctx);
207                         return code;
208                 }
209         } else {
210                 if ((code = krb5_cc_resolve(ctx, cc_name, &cc))) {
211                         DEBUG(3, ("ads_kdestroy: krb5_cc_resolve failed: %s\n",
212                                   error_message(code)));
213                         krb5_free_context(ctx);
214                         return code;
215                 }
216         }
217
218         if ((code = krb5_cc_destroy (ctx, cc))) {
219                 DEBUG(3, ("ads_kdestroy: krb5_cc_destroy failed: %s\n", 
220                         error_message(code)));
221         }
222
223         krb5_free_context (ctx);
224         return code;
225 }
226
227 /************************************************************************
228  Routine to fetch the salting principal for a service.  Active
229  Directory may use a non-obvious principal name to generate the salt
230  when it determines the key to use for encrypting tickets for a service,
231  and hopefully we detected that when we joined the domain.
232  ************************************************************************/
233
234 static char *kerberos_secrets_fetch_salting_principal(const char *service, int enctype)
235 {
236         char *key = NULL;
237         char *ret = NULL;
238
239         asprintf(&key, "%s/%s/enctype=%d", SECRETS_SALTING_PRINCIPAL, service, enctype);
240         if (!key) {
241                 return NULL;
242         }
243         ret = (char *)secrets_fetch(key, NULL);
244         SAFE_FREE(key);
245         return ret;
246 }
247
248 /************************************************************************
249  Routine to get the salting principal for this service.  Active
250  Directory may use a non-obvious principal name to generate the salt
251  when it determines the key to use for encrypting tickets for a service,
252  and hopefully we detected that when we joined the domain.
253  Caller must free if return is not null.
254  ************************************************************************/
255
256 krb5_principal kerberos_fetch_salt_princ_for_host_princ(krb5_context context,
257                                                         krb5_principal host_princ,
258                                                         int enctype)
259 {
260         char *unparsed_name = NULL, *salt_princ_s = NULL;
261         krb5_principal ret_princ = NULL;
262
263         if (krb5_unparse_name(context, host_princ, &unparsed_name) != 0) {
264                 return (krb5_principal)NULL;
265         }
266
267         if ((salt_princ_s = kerberos_secrets_fetch_salting_principal(unparsed_name, enctype)) == NULL) {
268                 krb5_free_unparsed_name(context, unparsed_name);
269                 return (krb5_principal)NULL;
270         }
271
272         if (krb5_parse_name(context, salt_princ_s, &ret_princ) != 0) {
273                 krb5_free_unparsed_name(context, unparsed_name);
274                 SAFE_FREE(salt_princ_s);
275                 return (krb5_principal)NULL;
276         }
277         krb5_free_unparsed_name(context, unparsed_name);
278         SAFE_FREE(salt_princ_s);
279         return ret_princ;
280 }
281
282 /************************************************************************
283  Routine to set the salting principal for this service.  Active
284  Directory may use a non-obvious principal name to generate the salt
285  when it determines the key to use for encrypting tickets for a service,
286  and hopefully we detected that when we joined the domain.
287  Setting principal to NULL deletes this entry.
288  ************************************************************************/
289
290 BOOL kerberos_secrets_store_salting_principal(const char *service,
291                                               int enctype,
292                                               const char *principal)
293 {
294         char *key = NULL;
295         BOOL ret = False;
296         krb5_context context = NULL;
297         krb5_principal princ = NULL;
298         char *princ_s = NULL;
299         char *unparsed_name = NULL;
300
301         krb5_init_context(&context);
302         if (!context) {
303                 return False;
304         }
305         if (strchr_m(service, '@')) {
306                 asprintf(&princ_s, "%s", service);
307         } else {
308                 asprintf(&princ_s, "%s@%s", service, lp_realm());
309         }
310
311         if (krb5_parse_name(context, princ_s, &princ) != 0) {
312                 goto out;
313                 
314         }
315         if (krb5_unparse_name(context, princ, &unparsed_name) != 0) {
316                 goto out;
317         }
318
319         asprintf(&key, "%s/%s/enctype=%d", SECRETS_SALTING_PRINCIPAL, unparsed_name, enctype);
320         if (!key)  {
321                 goto out;
322         }
323
324         if ((principal != NULL) && (strlen(principal) > 0)) {
325                 ret = secrets_store(key, principal, strlen(principal) + 1);
326         } else {
327                 ret = secrets_delete(key);
328         }
329
330  out:
331
332         SAFE_FREE(key);
333         SAFE_FREE(princ_s);
334
335         if (unparsed_name) {
336                 krb5_free_unparsed_name(context, unparsed_name);
337         }
338         if (context) {
339                 krb5_free_context(context);
340         }
341
342         return ret;
343 }
344
345 /************************************************************************
346  Routine to get initial credentials as a service ticket for the local machine.
347  Returns a buffer initialized with krb5_mk_req_extended.
348  ************************************************************************/
349
350 static krb5_error_code get_service_ticket(krb5_context ctx,
351                                         krb5_ccache ccache,
352                                         const char *service_principal,
353                                         int enctype,
354                                         krb5_data *p_outbuf)
355 {
356         krb5_creds creds, *new_creds = NULL;
357         char *service_s = NULL;
358         char *machine_account = NULL, *password = NULL;
359         krb5_data in_data;
360         krb5_auth_context auth_context = NULL;
361         krb5_error_code err = 0;
362
363         ZERO_STRUCT(creds);
364
365         asprintf(&machine_account, "%s$@%s", global_myname(), lp_realm());
366         if (machine_account == NULL) {
367                 goto out;
368         }
369         password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
370         if (password == NULL) {
371                 goto out;
372         }
373         if ((err = kerberos_kinit_password(machine_account, password, 0, NULL, NULL, 
374                                            LIBADS_CCACHE_NAME, False, 0)) != 0) {
375                 DEBUG(0,("get_service_ticket: kerberos_kinit_password %s@%s failed: %s\n", 
376                         machine_account,
377                         lp_realm(),
378                         error_message(err)));
379                 goto out;
380         }
381
382         /* Ok - the above call has gotten a TGT. Now we need to get a service
383            ticket to ourselves. */
384
385         /* Set up the enctype and client and server principal fields for krb5_get_credentials. */
386         kerberos_set_creds_enctype(&creds, enctype);
387
388         if ((err = krb5_cc_get_principal(ctx, ccache, &creds.client))) {
389                 DEBUG(3, ("get_service_ticket: krb5_cc_get_principal failed: %s\n", 
390                         error_message(err)));
391                 goto out;
392         }
393
394         if (strchr_m(service_principal, '@')) {
395                 asprintf(&service_s, "%s", service_principal);
396         } else {
397                 asprintf(&service_s, "%s@%s", service_principal, lp_realm());
398         }
399
400         if ((err = krb5_parse_name(ctx, service_s, &creds.server))) {
401                 DEBUG(0,("get_service_ticket: krb5_parse_name %s failed: %s\n", 
402                         service_s, error_message(err)));
403                 goto out;
404         }
405
406         if ((err = krb5_get_credentials(ctx, 0, ccache, &creds, &new_creds))) {
407                 DEBUG(5,("get_service_ticket: krb5_get_credentials for %s enctype %d failed: %s\n", 
408                         service_s, enctype, error_message(err)));
409                 goto out;
410         }
411
412         memset(&in_data, '\0', sizeof(in_data));
413         if ((err = krb5_mk_req_extended(ctx, &auth_context, 0, &in_data,
414                         new_creds, p_outbuf)) != 0) {
415                 DEBUG(0,("get_service_ticket: krb5_mk_req_extended failed: %s\n", 
416                         error_message(err)));
417                 goto out;
418         }
419
420  out:
421
422         if (auth_context) {
423                 krb5_auth_con_free(ctx, auth_context);
424         }
425         if (new_creds) {
426                 krb5_free_creds(ctx, new_creds);
427         }
428         if (creds.server) {
429                 krb5_free_principal(ctx, creds.server);
430         }
431         if (creds.client) {
432                 krb5_free_principal(ctx, creds.client);
433         }
434
435         SAFE_FREE(service_s);
436         SAFE_FREE(password);
437         SAFE_FREE(machine_account);
438         return err;
439 }
440
441 /************************************************************************
442  Check if the machine password can be used in conjunction with the salting_principal
443  to generate a key which will successfully decrypt the AP_REQ already
444  gotten as a message to the local machine.
445  ************************************************************************/
446
447 static BOOL verify_service_password(krb5_context ctx,
448                                     int enctype,
449                                     const char *salting_principal,
450                                     krb5_data *in_data)
451 {
452         BOOL ret = False;
453         krb5_principal salting_kprinc = NULL;
454         krb5_ticket *ticket = NULL;
455         krb5_keyblock key;
456         krb5_data passdata;
457         char *salting_s = NULL;
458         char *machine_account = NULL, *password = NULL;
459         krb5_auth_context auth_context = NULL;
460         krb5_error_code err;
461
462         memset(&passdata, '\0', sizeof(passdata));
463         memset(&key, '\0', sizeof(key));
464
465         asprintf(&machine_account, "%s$@%s", global_myname(), lp_realm());
466         if (machine_account == NULL) {
467                 goto out;
468         }
469         password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
470         if (password == NULL) {
471                 goto out;
472         }
473
474         if (strchr_m(salting_principal, '@')) {
475                 asprintf(&salting_s, "%s", salting_principal);
476         } else {
477                 asprintf(&salting_s, "%s@%s", salting_principal, lp_realm());
478         }
479
480         if ((err = krb5_parse_name(ctx, salting_s, &salting_kprinc))) {
481                 DEBUG(0,("verify_service_password: krb5_parse_name %s failed: %s\n", 
482                         salting_s, error_message(err)));
483                 goto out;
484         }
485
486         passdata.length = strlen(password);
487         passdata.data = (char*)password;
488         if ((err = create_kerberos_key_from_string_direct(ctx, salting_kprinc, &passdata, &key, enctype))) {
489                 DEBUG(0,("verify_service_password: create_kerberos_key_from_string %d failed: %s\n",
490                         enctype, error_message(err)));
491                 goto out;
492         }
493
494         if ((err = krb5_auth_con_init(ctx, &auth_context)) != 0) {
495                 DEBUG(0,("verify_service_password: krb5_auth_con_init failed %s\n", error_message(err)));
496                 goto out;
497         }
498
499         if ((err = krb5_auth_con_setuseruserkey(ctx, auth_context, &key)) != 0) {
500                 DEBUG(0,("verify_service_password: krb5_auth_con_setuseruserkey failed %s\n", error_message(err)));
501                 goto out;
502         }
503
504         if (!(err = krb5_rd_req(ctx, &auth_context, in_data, NULL, NULL, NULL, &ticket))) {
505                 DEBUG(10,("verify_service_password: decrypted message with enctype %u salt %s!\n",
506                                 (unsigned int)enctype, salting_s));
507                 ret = True;
508         }
509
510  out:
511
512         memset(&passdata, 0, sizeof(passdata));
513         krb5_free_keyblock_contents(ctx, &key);
514         if (ticket != NULL) {
515                 krb5_free_ticket(ctx, ticket);
516         }
517         if (salting_kprinc) {
518                 krb5_free_principal(ctx, salting_kprinc);
519         }
520         SAFE_FREE(salting_s);
521         SAFE_FREE(password);
522         SAFE_FREE(machine_account);
523         return ret;
524 }
525
526 /************************************************************************
527  *
528  * From the current draft of kerberos-clarifications:
529  *
530  *     It is not possible to reliably generate a user's key given a pass
531  *     phrase without contacting the KDC, since it will not be known
532  *     whether alternate salt or parameter values are required.
533  *
534  * And because our server has a password, we have this exact problem.  We
535  * make multiple guesses as to which principal name provides the salt which
536  * the KDC is using.
537  *
538  ************************************************************************/
539
540 static void kerberos_derive_salting_principal_for_enctype(const char *service_principal,
541                                                           krb5_context ctx,
542                                                           krb5_ccache ccache,
543                                                           krb5_enctype enctype,
544                                                           krb5_enctype *enctypes)
545 {
546         char *salting_principals[3] = {NULL, NULL, NULL}, *second_principal = NULL;
547         krb5_error_code err = 0;
548         krb5_data outbuf;
549         int i, j;
550
551         memset(&outbuf, '\0', sizeof(outbuf));
552
553         /* Check that the service_principal is useful. */
554         if ((service_principal == NULL) || (strlen(service_principal) == 0)) {
555                 return;
556         }
557
558         /* Generate our first guess -- the principal as-given. */
559         asprintf(&salting_principals[0], "%s", service_principal);
560         if ((salting_principals[0] == NULL) || (strlen(salting_principals[0]) == 0)) {
561                 return;
562         }
563
564         /* Generate our second guess -- the computer's principal, as Win2k3. */
565         asprintf(&second_principal, "host/%s.%s", global_myname(), lp_realm());
566         if (second_principal != NULL) {
567                 strlower_m(second_principal);
568                 asprintf(&salting_principals[1], "%s@%s", second_principal, lp_realm());
569                 SAFE_FREE(second_principal);
570         }
571         if ((salting_principals[1] == NULL) || (strlen(salting_principals[1]) == 0)) {
572                 goto out;
573         }
574
575         /* Generate our third guess -- the computer's principal, as Win2k. */
576         asprintf(&second_principal, "HOST/%s", global_myname());
577         if (second_principal != NULL) {
578                 strlower_m(second_principal + 5);
579                 asprintf(&salting_principals[2], "%s@%s",
580                         second_principal, lp_realm());
581                 SAFE_FREE(second_principal);
582         }
583         if ((salting_principals[2] == NULL) || (strlen(salting_principals[2]) == 0)) {
584                 goto out;
585         }
586
587         /* Get a service ticket for ourselves into our memory ccache. */
588         /* This will commonly fail if there is no principal by that name (and we're trying
589            many names). So don't print a debug 0 error. */
590
591         if ((err = get_service_ticket(ctx, ccache, service_principal, enctype, &outbuf)) != 0) {
592                 DEBUG(3, ("verify_service_password: get_service_ticket failed: %s\n", 
593                         error_message(err)));
594                 goto out;
595         }
596
597         /* At this point we have a message to ourselves, salted only the KDC knows how. We
598            have to work out what that salting is. */
599
600         /* Try and find the correct salting principal. */
601         for (i = 0; i < sizeof(salting_principals) / sizeof(salting_principals[i]); i++) {
602                 if (verify_service_password(ctx, enctype, salting_principals[i], &outbuf)) {
603                         break;
604                 }
605         }
606
607         /* If we failed to get a match, return. */
608         if (i >= sizeof(salting_principals) / sizeof(salting_principals[i])) {
609                 goto out;
610         }
611
612         /* If we succeeded, store the principal for use for all enctypes which
613          * share the same cipher and string-to-key function.  Doing this here
614          * allows servers which just pass a keytab to krb5_rd_req() to work
615          * correctly. */
616         for (j = 0; enctypes[j] != 0; j++) {
617                 if (enctype != enctypes[j]) {
618                         /* If this enctype isn't compatible with the one which
619                          * we used, skip it. */
620
621                         if (!kerberos_compatible_enctypes(ctx, enctypes[j], enctype))
622                                 continue;
623                 }
624                 /* If the principal which gives us the proper salt is the one
625                  * which we would normally guess, don't bother noting anything
626                  * in the secrets tdb. */
627                 if (strcmp(service_principal, salting_principals[i]) != 0) {
628                         kerberos_secrets_store_salting_principal(service_principal,
629                                                                 enctypes[j],
630                                                                 salting_principals[i]);
631                 }
632         }
633
634  out :
635
636         kerberos_free_data_contents(ctx, &outbuf);
637         SAFE_FREE(salting_principals[0]);
638         SAFE_FREE(salting_principals[1]);
639         SAFE_FREE(salting_principals[2]);
640         SAFE_FREE(second_principal);
641 }
642
643 /************************************************************************
644  Go through all the possible enctypes for this principal.
645  ************************************************************************/
646
647 static void kerberos_derive_salting_principal_direct(krb5_context context,
648                                         krb5_ccache ccache,
649                                         krb5_enctype *enctypes,
650                                         char *service_principal)
651 {
652         int i;
653
654         /* Try for each enctype separately, because the rules are
655          * different for different enctypes. */
656         for (i = 0; enctypes[i] != 0; i++) {
657                 /* Delete secrets entry first. */
658                 kerberos_secrets_store_salting_principal(service_principal, 0, NULL);
659 #ifdef ENCTYPE_ARCFOUR_HMAC
660                 if (enctypes[i] == ENCTYPE_ARCFOUR_HMAC) {
661                         /* Of course this'll always work, so just save
662                          * ourselves the effort. */
663                         continue;
664                 }
665 #endif
666                 /* Try to figure out what's going on with this
667                  * principal. */
668                 kerberos_derive_salting_principal_for_enctype(service_principal,
669                                                                 context,
670                                                                 ccache,
671                                                                 enctypes[i],
672                                                                 enctypes);
673         }
674 }
675
676 /************************************************************************
677  Wrapper function for the above.
678  ************************************************************************/
679
680 BOOL kerberos_derive_salting_principal(char *service_principal)
681 {
682         krb5_context context = NULL;
683         krb5_enctype *enctypes = NULL;
684         krb5_ccache ccache = NULL;
685         krb5_error_code ret = 0;
686
687         initialize_krb5_error_table();
688         if ((ret = krb5_init_context(&context)) != 0) {
689                 DEBUG(1,("kerberos_derive_cifs_salting_principals: krb5_init_context failed. %s\n",
690                         error_message(ret)));
691                 return False;
692         }
693         if ((ret = get_kerberos_allowed_etypes(context, &enctypes)) != 0) {
694                 DEBUG(1,("kerberos_derive_cifs_salting_principals: get_kerberos_allowed_etypes failed. %s\n",
695                         error_message(ret)));
696                 goto out;
697         }
698
699         if ((ret = krb5_cc_resolve(context, LIBADS_CCACHE_NAME, &ccache)) != 0) {
700                 DEBUG(3, ("get_service_ticket: krb5_cc_resolve for %s failed: %s\n", 
701                         LIBADS_CCACHE_NAME, error_message(ret)));
702                 goto out;
703         }
704
705         kerberos_derive_salting_principal_direct(context, ccache, enctypes, service_principal);
706
707   out: 
708         if (enctypes) {
709                 free_kerberos_etypes(context, enctypes);
710         }
711         if (ccache) {
712                 krb5_cc_destroy(context, ccache);
713         }
714         if (context) {
715                 krb5_free_context(context);
716         }
717
718         return ret ? False : True;
719 }
720
721 /************************************************************************
722  Core function to try and determine what salt is being used for any keytab
723  keys.
724  ************************************************************************/
725
726 BOOL kerberos_derive_cifs_salting_principals(void)
727 {
728         fstring my_fqdn;
729         char *service = NULL;
730         krb5_context context = NULL;
731         krb5_enctype *enctypes = NULL;
732         krb5_ccache ccache = NULL;
733         krb5_error_code ret = 0;
734         BOOL retval = False;
735
736         initialize_krb5_error_table();
737         if ((ret = krb5_init_context(&context)) != 0) {
738                 DEBUG(1,("kerberos_derive_cifs_salting_principals: krb5_init_context failed. %s\n",
739                         error_message(ret)));
740                 return False;
741         }
742         if ((ret = get_kerberos_allowed_etypes(context, &enctypes)) != 0) {
743                 DEBUG(1,("kerberos_derive_cifs_salting_principals: get_kerberos_allowed_etypes failed. %s\n",
744                         error_message(ret)));
745                 goto out;
746         }
747
748         if ((ret = krb5_cc_resolve(context, LIBADS_CCACHE_NAME, &ccache)) != 0) {
749                 DEBUG(3, ("get_service_ticket: krb5_cc_resolve for %s failed: %s\n", 
750                         LIBADS_CCACHE_NAME, error_message(ret)));
751                 goto out;
752         }
753
754         if (asprintf(&service, "%s$", global_myname()) != -1) {
755                 strlower_m(service);
756                 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
757                 SAFE_FREE(service);
758         }
759         if (asprintf(&service, "cifs/%s", global_myname()) != -1) {
760                 strlower_m(service);
761                 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
762                 SAFE_FREE(service);
763         }
764         if (asprintf(&service, "host/%s", global_myname()) != -1) {
765                 strlower_m(service);
766                 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
767                 SAFE_FREE(service);
768         }
769         if (asprintf(&service, "cifs/%s.%s", global_myname(), lp_realm()) != -1) {
770                 strlower_m(service);
771                 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
772                 SAFE_FREE(service);
773         }
774         if (asprintf(&service, "host/%s.%s", global_myname(), lp_realm()) != -1) {
775                 strlower_m(service);
776                 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
777                 SAFE_FREE(service);
778         }
779         name_to_fqdn(my_fqdn, global_myname());
780         if (asprintf(&service, "cifs/%s", my_fqdn) != -1) {
781                 strlower_m(service);
782                 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
783                 SAFE_FREE(service);
784         }
785         if (asprintf(&service, "host/%s", my_fqdn) != -1) {
786                 strlower_m(service);
787                 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
788                 SAFE_FREE(service);
789         }
790
791         retval = True;
792
793   out: 
794         if (enctypes) {
795                 free_kerberos_etypes(context, enctypes);
796         }
797         if (ccache) {
798                 krb5_cc_destroy(context, ccache);
799         }
800         if (context) {
801                 krb5_free_context(context);
802         }
803         return retval;
804 }
805 #endif