1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/ccache/ccdefault.c - Find default credential cache */
3 /*
4  * Copyright 1990, 2007, 2008 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 #include "k5-int.h"
28 
29 #ifdef USE_LEASH
30 static void (*pLeash_AcquireInitialTicketsIfNeeded)(krb5_context,krb5_principal,char*,int) = NULL;
31 static HANDLE hLeashDLL = INVALID_HANDLE_VALUE;
32 #ifdef _WIN64
33 #define LEASH_DLL "leashw64.dll"
34 #else
35 #define LEASH_DLL "leashw32.dll"
36 #endif
37 #endif
38 
39 
40 krb5_error_code KRB5_CALLCONV
krb5_cc_default(krb5_context context,krb5_ccache * ccache)41 krb5_cc_default(krb5_context context, krb5_ccache *ccache)
42 {
43     const char *default_name;
44 
45     if (!context || context->magic != KV5M_CONTEXT)
46         return KV5M_CONTEXT;
47 
48     default_name = krb5_cc_default_name(context);
49     if (default_name == NULL) {
50         /* Could be a bogus context, or an allocation failure, or
51            other things.  Unfortunately the API doesn't allow us
52            to find out any specifics.  */
53         return KRB5_FCC_INTERNAL;
54     }
55 
56     return krb5_cc_resolve(context, default_name, ccache);
57 }
58 
59 /* This is the internal function which opens the default ccache.  On
60    platforms supporting the login library's automatic popup dialog to
61    get tickets, this function also updated the library's internal view
62    of the current principal associated with this cache.
63 
64    All krb5 and GSS functions which need to open a cache to get a tgt
65    to obtain service tickets should call this function, not
66    krb5_cc_default().  */
67 
68 krb5_error_code KRB5_CALLCONV
krb5int_cc_default(krb5_context context,krb5_ccache * ccache)69 krb5int_cc_default(krb5_context context, krb5_ccache *ccache)
70 {
71     if (!context || context->magic != KV5M_CONTEXT) {
72         return KV5M_CONTEXT;
73     }
74 
75 #ifdef USE_LEASH
76     if ( hLeashDLL == INVALID_HANDLE_VALUE ) {
77         hLeashDLL = LoadLibrary(LEASH_DLL);
78         if ( hLeashDLL != INVALID_HANDLE_VALUE ) {
79             (FARPROC) pLeash_AcquireInitialTicketsIfNeeded =
80                 GetProcAddress(hLeashDLL, "not_an_API_Leash_AcquireInitialTicketsIfNeeded");
81         }
82     }
83 
84     if ( pLeash_AcquireInitialTicketsIfNeeded ) {
85         char ccname[256]="";
86         pLeash_AcquireInitialTicketsIfNeeded(context, NULL, ccname, sizeof(ccname));
87         if (ccname[0]) {
88             char * ccdefname = krb5_cc_default_name (context);
89             if (!ccdefname || strcmp (ccdefname, ccname) != 0) {
90                 krb5_cc_set_default_name (context, ccname);
91             }
92         }
93     }
94 #endif
95 
96     return krb5_cc_default (context, ccache);
97 }
98