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