Imported Upstream version 1.10.2
[platform/upstream/krb5.git] / src / lib / krb5 / ccache / ccdefault.c
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 #if defined(USE_KIM)
30 #include <kim/kim.h>
31 #include "kim_library_private.h"
32 #elif defined(USE_LEASH)
33 static void (*pLeash_AcquireInitialTicketsIfNeeded)(krb5_context,krb5_principal,char*,int) = NULL;
34 static HANDLE hLeashDLL = INVALID_HANDLE_VALUE;
35 #ifdef _WIN64
36 #define LEASH_DLL "leashw64.dll"
37 #else
38 #define LEASH_DLL "leashw32.dll"
39 #endif
40 #endif
41
42
43 krb5_error_code KRB5_CALLCONV
44 krb5_cc_default(krb5_context context, krb5_ccache *ccache)
45 {
46     const char *default_name;
47
48     if (!context || context->magic != KV5M_CONTEXT)
49         return KV5M_CONTEXT;
50
51     default_name = krb5_cc_default_name(context);
52     if (default_name == NULL) {
53         /* Could be a bogus context, or an allocation failure, or
54            other things.  Unfortunately the API doesn't allow us
55            to find out any specifics.  */
56         return KRB5_FCC_INTERNAL;
57     }
58
59     return krb5_cc_resolve(context, default_name, ccache);
60 }
61
62 /* This is the internal function which opens the default ccache.  On
63    platforms supporting the login library's automatic popup dialog to
64    get tickets, this function also updated the library's internal view
65    of the current principal associated with this cache.
66
67    All krb5 and GSS functions which need to open a cache to get a tgt
68    to obtain service tickets should call this function, not
69    krb5_cc_default().  */
70
71 krb5_error_code KRB5_CALLCONV
72 krb5int_cc_default(krb5_context context, krb5_ccache *ccache)
73 {
74     if (!context || context->magic != KV5M_CONTEXT) {
75         return KV5M_CONTEXT;
76     }
77
78 #ifdef USE_KIM
79     if (kim_library_allow_automatic_prompting ()) {
80         kim_error err = KIM_NO_ERROR;
81         kim_ccache kimccache = NULL;
82         kim_identity identity = KIM_IDENTITY_ANY;
83         kim_credential_state state;
84         kim_string name = NULL;
85
86         err = kim_ccache_create_from_display_name (&kimccache,
87                                                    krb5_cc_default_name (context));
88
89         if (!err) {
90             err = kim_ccache_get_client_identity (kimccache, &identity);
91         }
92
93         if (!err) {
94             err = kim_ccache_get_state (kimccache, &state);
95         }
96
97         if (err || state != kim_credentials_state_valid) {
98             /* Either the ccache is does not exist or is invalid.  Get new
99              * tickets.  Use the identity in the ccache if there was one. */
100             kim_ccache_free (&kimccache);
101             err = kim_ccache_create_new (&kimccache,
102                                          identity, KIM_OPTIONS_DEFAULT);
103         }
104
105         if (!err) {
106             err = kim_ccache_get_display_name (kimccache, &name);
107         }
108
109         if (!err) {
110             krb5_cc_set_default_name (context, name);
111         }
112
113         kim_identity_free (&identity);
114         kim_string_free (&name);
115         kim_ccache_free (&kimccache);
116     }
117 #else
118 #ifdef USE_LEASH
119     if ( hLeashDLL == INVALID_HANDLE_VALUE ) {
120         hLeashDLL = LoadLibrary(LEASH_DLL);
121         if ( hLeashDLL != INVALID_HANDLE_VALUE ) {
122             (FARPROC) pLeash_AcquireInitialTicketsIfNeeded =
123                 GetProcAddress(hLeashDLL, "not_an_API_Leash_AcquireInitialTicketsIfNeeded");
124         }
125     }
126
127     if ( pLeash_AcquireInitialTicketsIfNeeded ) {
128         char ccname[256]="";
129         pLeash_AcquireInitialTicketsIfNeeded(context, NULL, ccname, sizeof(ccname));
130         if (ccname[0]) {
131             char * ccdefname = krb5_cc_default_name (context);
132             if (!ccdefname || strcmp (ccdefname, ccname) != 0) {
133                 krb5_cc_set_default_name (context, ccname);
134             }
135         }
136     }
137 #endif
138 #endif
139
140     return krb5_cc_default (context, ccache);
141 }