17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * lib/krb5/ccache/ccdefault.c
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * Copyright 1990 by the Massachusetts Institute of Technology.
57c478bd9Sstevel@tonic-gate  * All Rights Reserved.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
87c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
97c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
107c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
11*159d09a2SMark Phalan  *
127c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
137c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
147c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
157c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
167c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
177c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
187c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
197c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
207c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
217c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
227c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
237c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
247c478bd9Sstevel@tonic-gate  * or implied warranty.
25*159d09a2SMark Phalan  *
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * Find default credential cache
287c478bd9Sstevel@tonic-gate  */
30*159d09a2SMark Phalan #include "k5-int.h"
32*159d09a2SMark Phalan #if defined(USE_LOGIN_LIBRARY)
33*159d09a2SMark Phalan #include "KerberosLoginPrivate.h"
34*159d09a2SMark Phalan #elif defined(USE_LEASH)
35*159d09a2SMark Phalan static void (*pLeash_AcquireInitialTicketsIfNeeded)(krb5_context,krb5_principal,char*,int) = NULL;
36*159d09a2SMark Phalan static HANDLE hLeashDLL = INVALID_HANDLE_VALUE;
37*159d09a2SMark Phalan #ifdef _WIN64
38*159d09a2SMark Phalan #define LEASH_DLL "leashw64.dll"
39*159d09a2SMark Phalan #else
40*159d09a2SMark Phalan #define LEASH_DLL "leashw32.dll"
417c478bd9Sstevel@tonic-gate #endif
42*159d09a2SMark Phalan #endif
43*159d09a2SMark Phalan 
45505d05c7Sgtb krb5_error_code KRB5_CALLCONV
krb5_cc_default(krb5_context context,krb5_ccache * ccache)46505d05c7Sgtb krb5_cc_default(krb5_context context, krb5_ccache *ccache)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	krb5_os_context	os_ctx;
507c478bd9Sstevel@tonic-gate 	if (!context || context->magic != KV5M_CONTEXT)
517c478bd9Sstevel@tonic-gate 		return KV5M_CONTEXT;
537c478bd9Sstevel@tonic-gate 	os_ctx = context->os_context;
55505d05c7Sgtb 	return krb5_cc_resolve(context, krb5_cc_default_name(context), ccache);
567c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate /* This is the internal function which opens the default ccache.  On platforms supporting
597c478bd9Sstevel@tonic-gate    the login library's automatic popup dialog to get tickets, this function also updated the
60*159d09a2SMark Phalan    library's internal view of the current principal associated with this cache.
61*159d09a2SMark Phalan 
627c478bd9Sstevel@tonic-gate    All krb5 and GSS functions which need to open a cache to get a tgt to obtain service tickets
637c478bd9Sstevel@tonic-gate    should call this function, not krb5_cc_default() */
65505d05c7Sgtb krb5_error_code KRB5_CALLCONV
krb5int_cc_default(krb5_context context,krb5_ccache * ccache)66505d05c7Sgtb krb5int_cc_default(krb5_context context, krb5_ccache *ccache)
677c478bd9Sstevel@tonic-gate {
68*159d09a2SMark Phalan     if (!context || context->magic != KV5M_CONTEXT) {
69*159d09a2SMark Phalan         return KV5M_CONTEXT;
70*159d09a2SMark Phalan     }
72505d05c7Sgtb #ifdef USE_LOGIN_LIBRARY
73*159d09a2SMark Phalan     {
74*159d09a2SMark Phalan         /* make sure the default cache has tix before you open it */
75*159d09a2SMark Phalan         KLStatus err = klNoErr;
76*159d09a2SMark Phalan         char *outCacheName = NULL;
77*159d09a2SMark Phalan 
78*159d09a2SMark Phalan         /* Try to make sure a krb5 tgt is in the cache */
79*159d09a2SMark Phalan         err = __KLInternalAcquireInitialTicketsForCache (krb5_cc_default_name (context), kerberosVersion_V5,
80*159d09a2SMark Phalan                                                          NULL, NULL, &outCacheName);
81*159d09a2SMark Phalan         if (err == klNoErr) {
82*159d09a2SMark Phalan             /* This function tries to get tickets and put them in the specified
83*159d09a2SMark Phalan             cache, however, if the cache does not exist, it may choose to put
84*159d09a2SMark Phalan             them elsewhere (ie: the system default) so we set that here */
85*159d09a2SMark Phalan             if (strcmp (krb5_cc_default_name (context), outCacheName) != 0) {
86*159d09a2SMark Phalan                 krb5_cc_set_default_name (context, outCacheName);
87*159d09a2SMark Phalan             }
88*159d09a2SMark Phalan             KLDisposeString (outCacheName);
89*159d09a2SMark Phalan         }
90*159d09a2SMark Phalan     }
91*159d09a2SMark Phalan #else
92*159d09a2SMark Phalan #ifdef USE_LEASH
93*159d09a2SMark Phalan     if ( hLeashDLL == INVALID_HANDLE_VALUE ) {
94*159d09a2SMark Phalan         hLeashDLL = LoadLibrary(LEASH_DLL);
95*159d09a2SMark Phalan         if ( hLeashDLL != INVALID_HANDLE_VALUE ) {
96*159d09a2SMark Phalan             (FARPROC) pLeash_AcquireInitialTicketsIfNeeded =
97*159d09a2SMark Phalan             GetProcAddress(hLeashDLL, "not_an_API_Leash_AcquireInitialTicketsIfNeeded");
98*159d09a2SMark Phalan         }
99*159d09a2SMark Phalan     }
100*159d09a2SMark Phalan 
101*159d09a2SMark Phalan     if ( pLeash_AcquireInitialTicketsIfNeeded ) {
102*159d09a2SMark Phalan 	char ccname[256]="";
103*159d09a2SMark Phalan         pLeash_AcquireInitialTicketsIfNeeded(context, NULL, ccname, sizeof(ccname));
104*159d09a2SMark Phalan 	if (ccname[0]) {
105*159d09a2SMark Phalan             if (strcmp (krb5_cc_default_name (context),ccname) != 0) {
106*159d09a2SMark Phalan                 krb5_cc_set_default_name (context, ccname);
107*159d09a2SMark Phalan             }
108*159d09a2SMark Phalan 	}
109*159d09a2SMark Phalan     }
110*159d09a2SMark Phalan #endif
1117c478bd9Sstevel@tonic-gate #endif
1137c478bd9Sstevel@tonic-gate     return krb5_cc_default (context, ccache);
1147c478bd9Sstevel@tonic-gate }