r15210: Add wrapper functions smb_krb5_parse_name, smb_krb5_unparse_name,
[sfrench/samba-autobuild/.git] / source / 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_ext(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 = smb_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, NULL, 0, NULL, &opt)))
110         {
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_ext(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 (smb_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                 SAFE_FREE(unparsed_name);
269                 return (krb5_principal)NULL;
270         }
271
272         if (smb_krb5_parse_name(context, salt_princ_s, &ret_princ) != 0) {
273                 SAFE_FREE(unparsed_name);
274                 SAFE_FREE(salt_princ_s);
275                 return (krb5_principal)NULL;
276         }
277         SAFE_FREE(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 (smb_krb5_parse_name(context, princ_s, &princ) != 0) {
312                 goto out;
313                 
314         }
315         if (smb_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         SAFE_FREE(unparsed_name);
335
336         if (context) {
337                 krb5_free_context(context);
338         }
339
340         return ret;
341 }
342
343 /************************************************************************
344  Routine to get initial credentials as a service ticket for the local machine.
345  Returns a buffer initialized with krb5_mk_req_extended.
346  ************************************************************************/
347
348 static krb5_error_code get_service_ticket(krb5_context ctx,
349                                         krb5_ccache ccache,
350                                         const char *service_principal,
351                                         int enctype,
352                                         krb5_data *p_outbuf)
353 {
354         krb5_creds creds, *new_creds = NULL;
355         char *service_s = NULL;
356         char *machine_account = NULL, *password = NULL;
357         krb5_data in_data;
358         krb5_auth_context auth_context = NULL;
359         krb5_error_code err = 0;
360
361         ZERO_STRUCT(creds);
362
363         asprintf(&machine_account, "%s$@%s", global_myname(), lp_realm());
364         if (machine_account == NULL) {
365                 goto out;
366         }
367         password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
368         if (password == NULL) {
369                 goto out;
370         }
371         if ((err = kerberos_kinit_password(machine_account, password, 
372                                            0, LIBADS_CCACHE_NAME)) != 0) {
373                 DEBUG(0,("get_service_ticket: kerberos_kinit_password %s failed: %s\n", 
374                         machine_account,
375                         error_message(err)));
376                 goto out;
377         }
378
379         /* Ok - the above call has gotten a TGT. Now we need to get a service
380            ticket to ourselves. */
381
382         /* Set up the enctype and client and server principal fields for krb5_get_credentials. */
383         kerberos_set_creds_enctype(&creds, enctype);
384
385         if ((err = krb5_cc_get_principal(ctx, ccache, &creds.client))) {
386                 DEBUG(3, ("get_service_ticket: krb5_cc_get_principal failed: %s\n", 
387                         error_message(err)));
388                 goto out;
389         }
390
391         if (strchr_m(service_principal, '@')) {
392                 asprintf(&service_s, "%s", service_principal);
393         } else {
394                 asprintf(&service_s, "%s@%s", service_principal, lp_realm());
395         }
396
397         if ((err = smb_krb5_parse_name(ctx, service_s, &creds.server))) {
398                 DEBUG(0,("get_service_ticket: smb_krb5_parse_name %s failed: %s\n", 
399                         service_s, error_message(err)));
400                 goto out;
401         }
402
403         if ((err = krb5_get_credentials(ctx, 0, ccache, &creds, &new_creds))) {
404                 DEBUG(5,("get_service_ticket: krb5_get_credentials for %s enctype %d failed: %s\n", 
405                         service_s, enctype, error_message(err)));
406                 goto out;
407         }
408
409         memset(&in_data, '\0', sizeof(in_data));
410         if ((err = krb5_mk_req_extended(ctx, &auth_context, 0, &in_data,
411                         new_creds, p_outbuf)) != 0) {
412                 DEBUG(0,("get_service_ticket: krb5_mk_req_extended failed: %s\n", 
413                         error_message(err)));
414                 goto out;
415         }
416
417  out:
418
419         if (auth_context) {
420                 krb5_auth_con_free(ctx, auth_context);
421         }
422         if (new_creds) {
423                 krb5_free_creds(ctx, new_creds);
424         }
425         if (creds.server) {
426                 krb5_free_principal(ctx, creds.server);
427         }
428         if (creds.client) {
429                 krb5_free_principal(ctx, creds.client);
430         }
431
432         SAFE_FREE(service_s);
433         SAFE_FREE(password);
434         SAFE_FREE(machine_account);
435         return err;
436 }
437
438 /************************************************************************
439  Check if the machine password can be used in conjunction with the salting_principal
440  to generate a key which will successfully decrypt the AP_REQ already
441  gotten as a message to the local machine.
442  ************************************************************************/
443
444 static BOOL verify_service_password(krb5_context ctx,
445                                     int enctype,
446                                     const char *salting_principal,
447                                     krb5_data *in_data)
448 {
449         BOOL ret = False;
450         krb5_principal salting_kprinc = NULL;
451         krb5_ticket *ticket = NULL;
452         krb5_keyblock key;
453         krb5_data passdata;
454         char *salting_s = NULL;
455         char *machine_account = NULL, *password = NULL;
456         krb5_auth_context auth_context = NULL;
457         krb5_error_code err;
458
459         memset(&passdata, '\0', sizeof(passdata));
460         memset(&key, '\0', sizeof(key));
461
462         asprintf(&machine_account, "%s$@%s", global_myname(), lp_realm());
463         if (machine_account == NULL) {
464                 goto out;
465         }
466         password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
467         if (password == NULL) {
468                 goto out;
469         }
470
471         if (strchr_m(salting_principal, '@')) {
472                 asprintf(&salting_s, "%s", salting_principal);
473         } else {
474                 asprintf(&salting_s, "%s@%s", salting_principal, lp_realm());
475         }
476
477         if ((err = smb_krb5_parse_name(ctx, salting_s, &salting_kprinc))) {
478                 DEBUG(0,("verify_service_password: smb_krb5_parse_name %s failed: %s\n", 
479                         salting_s, error_message(err)));
480                 goto out;
481         }
482
483         passdata.length = strlen(password);
484         passdata.data = (char*)password;
485         if ((err = create_kerberos_key_from_string_direct(ctx, salting_kprinc, &passdata, &key, enctype))) {
486                 DEBUG(0,("verify_service_password: create_kerberos_key_from_string %d failed: %s\n",
487                         enctype, error_message(err)));
488                 goto out;
489         }
490
491         if ((err = krb5_auth_con_init(ctx, &auth_context)) != 0) {
492                 DEBUG(0,("verify_service_password: krb5_auth_con_init failed %s\n", error_message(err)));
493                 goto out;
494         }
495
496         if ((err = krb5_auth_con_setuseruserkey(ctx, auth_context, &key)) != 0) {
497                 DEBUG(0,("verify_service_password: krb5_auth_con_setuseruserkey failed %s\n", error_message(err)));
498                 goto out;
499         }
500
501         if (!(err = krb5_rd_req(ctx, &auth_context, in_data, NULL, NULL, NULL, &ticket))) {
502                 DEBUG(10,("verify_service_password: decrypted message with enctype %u salt %s!\n",
503                                 (unsigned int)enctype, salting_s));
504                 ret = True;
505         }
506
507  out:
508
509         memset(&passdata, 0, sizeof(passdata));
510         krb5_free_keyblock_contents(ctx, &key);
511         if (ticket != NULL) {
512                 krb5_free_ticket(ctx, ticket);
513         }
514         if (salting_kprinc) {
515                 krb5_free_principal(ctx, salting_kprinc);
516         }
517         SAFE_FREE(salting_s);
518         SAFE_FREE(password);
519         SAFE_FREE(machine_account);
520         return ret;
521 }
522
523 /************************************************************************
524  *
525  * From the current draft of kerberos-clarifications:
526  *
527  *     It is not possible to reliably generate a user's key given a pass
528  *     phrase without contacting the KDC, since it will not be known
529  *     whether alternate salt or parameter values are required.
530  *
531  * And because our server has a password, we have this exact problem.  We
532  * make multiple guesses as to which principal name provides the salt which
533  * the KDC is using.
534  *
535  ************************************************************************/
536
537 static void kerberos_derive_salting_principal_for_enctype(const char *service_principal,
538                                                           krb5_context ctx,
539                                                           krb5_ccache ccache,
540                                                           krb5_enctype enctype,
541                                                           krb5_enctype *enctypes)
542 {
543         char *salting_principals[3] = {NULL, NULL, NULL}, *second_principal = NULL;
544         krb5_error_code err = 0;
545         krb5_data outbuf;
546         int i, j;
547
548         memset(&outbuf, '\0', sizeof(outbuf));
549
550         /* Check that the service_principal is useful. */
551         if ((service_principal == NULL) || (strlen(service_principal) == 0)) {
552                 return;
553         }
554
555         /* Generate our first guess -- the principal as-given. */
556         asprintf(&salting_principals[0], "%s", service_principal);
557         if ((salting_principals[0] == NULL) || (strlen(salting_principals[0]) == 0)) {
558                 return;
559         }
560
561         /* Generate our second guess -- the computer's principal, as Win2k3. */
562         asprintf(&second_principal, "host/%s.%s", global_myname(), lp_realm());
563         if (second_principal != NULL) {
564                 strlower_m(second_principal);
565                 asprintf(&salting_principals[1], "%s@%s", second_principal, lp_realm());
566                 SAFE_FREE(second_principal);
567         }
568         if ((salting_principals[1] == NULL) || (strlen(salting_principals[1]) == 0)) {
569                 goto out;
570         }
571
572         /* Generate our third guess -- the computer's principal, as Win2k. */
573         asprintf(&second_principal, "HOST/%s", global_myname());
574         if (second_principal != NULL) {
575                 strlower_m(second_principal + 5);
576                 asprintf(&salting_principals[2], "%s@%s",
577                         second_principal, lp_realm());
578                 SAFE_FREE(second_principal);
579         }
580         if ((salting_principals[2] == NULL) || (strlen(salting_principals[2]) == 0)) {
581                 goto out;
582         }
583
584         /* Get a service ticket for ourselves into our memory ccache. */
585         /* This will commonly fail if there is no principal by that name (and we're trying
586            many names). So don't print a debug 0 error. */
587
588         if ((err = get_service_ticket(ctx, ccache, service_principal, enctype, &outbuf)) != 0) {
589                 DEBUG(3, ("verify_service_password: get_service_ticket failed: %s\n", 
590                         error_message(err)));
591                 goto out;
592         }
593
594         /* At this point we have a message to ourselves, salted only the KDC knows how. We
595            have to work out what that salting is. */
596
597         /* Try and find the correct salting principal. */
598         for (i = 0; i < sizeof(salting_principals) / sizeof(salting_principals[i]); i++) {
599                 if (verify_service_password(ctx, enctype, salting_principals[i], &outbuf)) {
600                         break;
601                 }
602         }
603
604         /* If we failed to get a match, return. */
605         if (i >= sizeof(salting_principals) / sizeof(salting_principals[i])) {
606                 goto out;
607         }
608
609         /* If we succeeded, store the principal for use for all enctypes which
610          * share the same cipher and string-to-key function.  Doing this here
611          * allows servers which just pass a keytab to krb5_rd_req() to work
612          * correctly. */
613         for (j = 0; enctypes[j] != 0; j++) {
614                 if (enctype != enctypes[j]) {
615                         /* If this enctype isn't compatible with the one which
616                          * we used, skip it. */
617
618                         if (!kerberos_compatible_enctypes(ctx, enctypes[j], enctype))
619                                 continue;
620                 }
621                 /* If the principal which gives us the proper salt is the one
622                  * which we would normally guess, don't bother noting anything
623                  * in the secrets tdb. */
624                 if (strcmp(service_principal, salting_principals[i]) != 0) {
625                         kerberos_secrets_store_salting_principal(service_principal,
626                                                                 enctypes[j],
627                                                                 salting_principals[i]);
628                 }
629         }
630
631  out :
632
633         kerberos_free_data_contents(ctx, &outbuf);
634         SAFE_FREE(salting_principals[0]);
635         SAFE_FREE(salting_principals[1]);
636         SAFE_FREE(salting_principals[2]);
637         SAFE_FREE(second_principal);
638 }
639
640 /************************************************************************
641  Go through all the possible enctypes for this principal.
642  ************************************************************************/
643
644 static void kerberos_derive_salting_principal_direct(krb5_context context,
645                                         krb5_ccache ccache,
646                                         krb5_enctype *enctypes,
647                                         char *service_principal)
648 {
649         int i;
650
651         /* Try for each enctype separately, because the rules are
652          * different for different enctypes. */
653         for (i = 0; enctypes[i] != 0; i++) {
654                 /* Delete secrets entry first. */
655                 kerberos_secrets_store_salting_principal(service_principal, 0, NULL);
656 #ifdef ENCTYPE_ARCFOUR_HMAC
657                 if (enctypes[i] == ENCTYPE_ARCFOUR_HMAC) {
658                         /* Of course this'll always work, so just save
659                          * ourselves the effort. */
660                         continue;
661                 }
662 #endif
663                 /* Try to figure out what's going on with this
664                  * principal. */
665                 kerberos_derive_salting_principal_for_enctype(service_principal,
666                                                                 context,
667                                                                 ccache,
668                                                                 enctypes[i],
669                                                                 enctypes);
670         }
671 }
672
673 /************************************************************************
674  Wrapper function for the above.
675  ************************************************************************/
676
677 BOOL kerberos_derive_salting_principal(char *service_principal)
678 {
679         krb5_context context = NULL;
680         krb5_enctype *enctypes = NULL;
681         krb5_ccache ccache = NULL;
682         krb5_error_code ret = 0;
683
684         initialize_krb5_error_table();
685         if ((ret = krb5_init_context(&context)) != 0) {
686                 DEBUG(1,("kerberos_derive_cifs_salting_principals: krb5_init_context failed. %s\n",
687                         error_message(ret)));
688                 return False;
689         }
690         if ((ret = get_kerberos_allowed_etypes(context, &enctypes)) != 0) {
691                 DEBUG(1,("kerberos_derive_cifs_salting_principals: get_kerberos_allowed_etypes failed. %s\n",
692                         error_message(ret)));
693                 goto out;
694         }
695
696         if ((ret = krb5_cc_resolve(context, LIBADS_CCACHE_NAME, &ccache)) != 0) {
697                 DEBUG(3, ("get_service_ticket: krb5_cc_resolve for %s failed: %s\n", 
698                         LIBADS_CCACHE_NAME, error_message(ret)));
699                 goto out;
700         }
701
702         kerberos_derive_salting_principal_direct(context, ccache, enctypes, service_principal);
703
704   out: 
705         if (enctypes) {
706                 free_kerberos_etypes(context, enctypes);
707         }
708         if (ccache) {
709                 krb5_cc_destroy(context, ccache);
710         }
711         if (context) {
712                 krb5_free_context(context);
713         }
714
715         return ret ? False : True;
716 }
717
718 /************************************************************************
719  Core function to try and determine what salt is being used for any keytab
720  keys.
721  ************************************************************************/
722
723 BOOL kerberos_derive_cifs_salting_principals(void)
724 {
725         fstring my_fqdn;
726         char *service = NULL;
727         krb5_context context = NULL;
728         krb5_enctype *enctypes = NULL;
729         krb5_ccache ccache = NULL;
730         krb5_error_code ret = 0;
731         BOOL retval = False;
732
733         initialize_krb5_error_table();
734         if ((ret = krb5_init_context(&context)) != 0) {
735                 DEBUG(1,("kerberos_derive_cifs_salting_principals: krb5_init_context failed. %s\n",
736                         error_message(ret)));
737                 return False;
738         }
739         if ((ret = get_kerberos_allowed_etypes(context, &enctypes)) != 0) {
740                 DEBUG(1,("kerberos_derive_cifs_salting_principals: get_kerberos_allowed_etypes failed. %s\n",
741                         error_message(ret)));
742                 goto out;
743         }
744
745         if ((ret = krb5_cc_resolve(context, LIBADS_CCACHE_NAME, &ccache)) != 0) {
746                 DEBUG(3, ("get_service_ticket: krb5_cc_resolve for %s failed: %s\n", 
747                         LIBADS_CCACHE_NAME, error_message(ret)));
748                 goto out;
749         }
750
751         if (asprintf(&service, "%s$", global_myname()) != -1) {
752                 strlower_m(service);
753                 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
754                 SAFE_FREE(service);
755         }
756         if (asprintf(&service, "cifs/%s", global_myname()) != -1) {
757                 strlower_m(service);
758                 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
759                 SAFE_FREE(service);
760         }
761         if (asprintf(&service, "host/%s", global_myname()) != -1) {
762                 strlower_m(service);
763                 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
764                 SAFE_FREE(service);
765         }
766         if (asprintf(&service, "cifs/%s.%s", global_myname(), lp_realm()) != -1) {
767                 strlower_m(service);
768                 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
769                 SAFE_FREE(service);
770         }
771         if (asprintf(&service, "host/%s.%s", global_myname(), lp_realm()) != -1) {
772                 strlower_m(service);
773                 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
774                 SAFE_FREE(service);
775         }
776         name_to_fqdn(my_fqdn, global_myname());
777         if (asprintf(&service, "cifs/%s", my_fqdn) != -1) {
778                 strlower_m(service);
779                 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
780                 SAFE_FREE(service);
781         }
782         if (asprintf(&service, "host/%s", my_fqdn) != -1) {
783                 strlower_m(service);
784                 kerberos_derive_salting_principal_direct(context, ccache, enctypes, service);
785                 SAFE_FREE(service);
786         }
787
788         retval = True;
789
790   out: 
791         if (enctypes) {
792                 free_kerberos_etypes(context, enctypes);
793         }
794         if (ccache) {
795                 krb5_cc_destroy(context, ccache);
796         }
797         if (context) {
798                 krb5_free_context(context);
799         }
800         return retval;
801 }
802
803 int kerberos_kinit_password(const char *principal,
804                             const char *password,
805                             int time_offset,
806                             const char *cache_name)
807 {
808         return kerberos_kinit_password_ext(principal, 
809                                            password, 
810                                            time_offset, 
811                                            0, 
812                                            0,
813                                            cache_name,
814                                            False,
815                                            0);
816 }
817
818 #endif