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