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