1 /*
2  * lib/krb5/ccache/ccdefault.c
3  *
4  * Copyright 1990 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  *
26  *
27  * Find default credential cache
28  */
29 
30 #include "k5-int.h"
31 
32 #if defined(USE_LOGIN_LIBRARY)
33 #include "KerberosLoginPrivate.h"
34 #elif defined(USE_LEASH)
35 static void (*pLeash_AcquireInitialTicketsIfNeeded)(krb5_context,krb5_principal,char*,int) = NULL;
36 static HANDLE hLeashDLL = INVALID_HANDLE_VALUE;
37 #ifdef _WIN64
38 #define LEASH_DLL "leashw64.dll"
39 #else
40 #define LEASH_DLL "leashw32.dll"
41 #endif
42 #endif
43 
44 
45 krb5_error_code KRB5_CALLCONV
46 krb5_cc_default(krb5_context context, krb5_ccache *ccache)
47 {
48 	krb5_os_context	os_ctx;
49 
50 	if (!context || context->magic != KV5M_CONTEXT)
51 		return KV5M_CONTEXT;
52 
53 	os_ctx = context->os_context;
54 
55 	return krb5_cc_resolve(context, krb5_cc_default_name(context), ccache);
56 }
57 
58 /* This is the internal function which opens the default ccache.  On platforms supporting
59    the login library's automatic popup dialog to get tickets, this function also updated the
60    library's internal view of the current principal associated with this cache.
61 
62    All krb5 and GSS functions which need to open a cache to get a tgt to obtain service tickets
63    should call this function, not krb5_cc_default() */
64 
65 krb5_error_code KRB5_CALLCONV
66 krb5int_cc_default(krb5_context context, krb5_ccache *ccache)
67 {
68     if (!context || context->magic != KV5M_CONTEXT) {
69         return KV5M_CONTEXT;
70     }
71 
72 #ifdef USE_LOGIN_LIBRARY
73     {
74         /* make sure the default cache has tix before you open it */
75         KLStatus err = klNoErr;
76         char *outCacheName = NULL;
77 
78         /* Try to make sure a krb5 tgt is in the cache */
79         err = __KLInternalAcquireInitialTicketsForCache (krb5_cc_default_name (context), kerberosVersion_V5,
80                                                          NULL, NULL, &outCacheName);
81         if (err == klNoErr) {
82             /* This function tries to get tickets and put them in the specified
83             cache, however, if the cache does not exist, it may choose to put
84             them elsewhere (ie: the system default) so we set that here */
85             if (strcmp (krb5_cc_default_name (context), outCacheName) != 0) {
86                 krb5_cc_set_default_name (context, outCacheName);
87             }
88             KLDisposeString (outCacheName);
89         }
90     }
91 #else
92 #ifdef USE_LEASH
93     if ( hLeashDLL == INVALID_HANDLE_VALUE ) {
94         hLeashDLL = LoadLibrary(LEASH_DLL);
95         if ( hLeashDLL != INVALID_HANDLE_VALUE ) {
96             (FARPROC) pLeash_AcquireInitialTicketsIfNeeded =
97             GetProcAddress(hLeashDLL, "not_an_API_Leash_AcquireInitialTicketsIfNeeded");
98         }
99     }
100 
101     if ( pLeash_AcquireInitialTicketsIfNeeded ) {
102 	char ccname[256]="";
103         pLeash_AcquireInitialTicketsIfNeeded(context, NULL, ccname, sizeof(ccname));
104 	if (ccname[0]) {
105             if (strcmp (krb5_cc_default_name (context),ccname) != 0) {
106                 krb5_cc_set_default_name (context, ccname);
107             }
108 	}
109     }
110 #endif
111 #endif
112 
113     return krb5_cc_default (context, ccache);
114 }
115