2 * Copyright (c) 2005 Massachusetts Institute of Technology
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include<krb5common.h>
36 /**************************************/
37 /* khm_krb5_error(): */
38 /**************************************/
40 khm_krb5_error(krb5_error_code rc, LPCSTR FailedFunctionName,
41 int FreeContextFlag, krb5_context * ctx,
48 #ifdef SHOW_MESSAGE_IN_AN_ANNOYING_WAY
51 int krb5Error = ((int)(rc & 255));
53 if (pkrb5_get_error_message)
54 errText = pkrb5_get_error_message(rc);
56 errText = perror_message(rc);
57 _snprintf(message, sizeof(message),
58 "%s\n(Kerberos error %ld)\n\n%s failed",
62 if (pkrb5_free_error_message)
63 pkrb5_free_error_message(errText);
65 MessageBoxA(NULL, message, "Kerberos Five", MB_OK | MB_ICONERROR |
70 if (FreeContextFlag == 1)
75 pkrb5_cc_close(*ctx, *cache);
79 pkrb5_free_context(*ctx);
90 khm_krb5_initialize(khm_handle ident,
100 krb5_error_code rc = 0;
101 krb5_flags flags = KRB5_TC_OPENCLOSE;
103 if (pkrb5_init_context == NULL)
106 if (*ctx == 0 && (rc = (*pkrb5_init_context)(ctx))) {
107 functionName = "krb5_init_context()";
113 wchar_t wccname[MAX_PATH];
117 cbwccname = sizeof(wccname);
121 if(KHM_FAILED(kcdb_identity_get_attrib(ident, L"Krb5CCName",
124 cbwccname = sizeof(wccname);
126 (khm_krb5_find_ccache_for_identity(ident,
130 #ifdef DEBUG_LIKE_A_MADMAN
137 if(UnicodeStrToAnsi(ccname, sizeof(ccname), wccname) == 0)
140 if((*pkrb5_cc_resolve)(*ctx, ccname, cache)) {
141 functionName = "krb5_cc_resolve()";
148 #ifndef FAILOVER_TO_DEFAULT_CCACHE
152 #ifdef FAILOVER_TO_DEFAULT_CCACHE
153 && (rc = (*pkrb5_cc_default)(*ctx, cache))
156 functionName = "krb5_cc_default()";
162 #ifdef KRB5_TC_NOTICKET
163 flags = KRB5_TC_NOTICKET;
166 if ((rc = (*pkrb5_cc_set_flags)(*ctx, *cache, flags)))
168 if (rc != KRB5_FCC_NOFILE && rc != KRB5_CC_NOTFOUND)
169 khm_krb5_error(rc, "krb5_cc_set_flags()", 0, ctx,
171 else if ((rc == KRB5_FCC_NOFILE || rc == KRB5_CC_NOTFOUND) && *ctx != NULL) {
173 (*pkrb5_cc_close)(*ctx, *cache);
180 return khm_krb5_error(rc, functionName, freeContextFlag, ctx, cache);
184 #define TIMET_TOLERANCE (60*5)
187 khm_get_identity_expiration_time(krb5_context ctx, krb5_ccache cc,
189 krb5_timestamp * pexpiration)
191 krb5_principal principal = 0;
192 char * princ_name = NULL;
194 krb5_error_code code;
195 krb5_error_code cc_code;
197 krb5_timestamp now, expiration = 0;
199 wchar_t w_ident_name[KCDB_IDENT_MAXCCH_NAME];
200 char ident_name[KCDB_IDENT_MAXCCH_NAME];
203 khm_int32 rv = KHM_ERROR_NOT_FOUND;
205 if (!ctx || !cc || !ident || !pexpiration)
206 return KHM_ERROR_GENERAL;
208 code = pkrb5_cc_get_principal(ctx, cc, &principal);
211 return KHM_ERROR_INVALID_PARAM;
213 cb = sizeof(w_ident_name);
214 kcdb_identity_get_name(ident, w_ident_name, &cb);
215 UnicodeStrToAnsi(ident_name, sizeof(ident_name), w_ident_name);
217 code = pkrb5_unparse_name(ctx, principal, &princ_name);
219 /* compare principal to ident. */
221 if ( code || !princ_name ||
222 strcmp(princ_name, ident_name) ) {
224 pkrb5_free_unparsed_name(ctx, princ_name);
225 pkrb5_free_principal(ctx, principal);
226 return KHM_ERROR_UNKNOWN;
229 pkrb5_free_unparsed_name(ctx, princ_name);
230 pkrb5_free_principal(ctx, principal);
232 code = pkrb5_timeofday(ctx, &now);
235 return KHM_ERROR_UNKNOWN;
237 cc_code = pkrb5_cc_start_seq_get(ctx, cc, &cur);
239 while (!(cc_code = pkrb5_cc_next_cred(ctx, cc, &cur, &creds))) {
240 krb5_data * c0 = krb5_princ_name(ctx, creds.server);
241 krb5_data * c1 = krb5_princ_component(ctx, creds.server, 1);
242 krb5_data * r = krb5_princ_realm(ctx, creds.server);
244 if ( c0 && c1 && r && c1->length == r->length &&
245 !strncmp(c1->data,r->data,r->length) &&
246 !strncmp("krbtgt",c0->data,c0->length) ) {
248 /* we have a TGT, check for the expiration time.
249 * if it is valid and renewable, use the renew time
252 if (!(creds.ticket_flags & TKT_FLG_INVALID) &&
253 creds.times.starttime < (now + TIMET_TOLERANCE) &&
254 (creds.times.endtime + TIMET_TOLERANCE) > now) {
255 expiration = creds.times.endtime;
257 if ((creds.ticket_flags & TKT_FLG_RENEWABLE) &&
258 (creds.times.renew_till > creds.times.endtime)) {
259 expiration = creds.times.renew_till;
265 if (cc_code == KRB5_CC_END) {
266 cc_code = pkrb5_cc_end_seq_get(ctx, cc, &cur);
267 rv = KHM_ERROR_SUCCESS;
268 *pexpiration = expiration;
275 khm_krb5_find_ccache_for_identity(khm_handle ident, krb5_context *pctx,
276 void * buffer, khm_size * pcbbuf)
278 krb5_context ctx = 0;
279 krb5_ccache cache = 0;
280 krb5_error_code code;
282 struct _infoNC ** pNCi = NULL;
287 krb5_timestamp expiration = 0;
288 krb5_timestamp best_match_expiration = 0;
289 char best_match_ccname[256] = "";
290 khm_handle csp_params = NULL;
291 khm_handle csp_plugins = NULL;
293 if (!buffer || !pcbbuf)
294 return KHM_ERROR_GENERAL;
298 if (!pcc_initialize ||
304 code = pcc_initialize(&cc_ctx, CC_API_VER_2, NULL, NULL);
308 code = pcc_get_NC_info(cc_ctx, &pNCi);
313 for(i=0; pNCi[i]; i++) {
314 if (pNCi[i]->vers != CC_CRED_V5)
317 code = (*pkrb5_cc_resolve)(ctx, pNCi[i]->name, &cache);
321 /* need a function to check the cache for the identity
322 * and determine if it has valid tickets. If it has
323 * the right identity and valid tickets, store the
324 * expiration time and the cache name. If it has the
325 * right identity but no valid tickets, store the ccache
326 * name and an expiration time of zero. if it does not
327 * have the right identity don't save the name.
329 * Keep searching to find the best cache available.
332 if (KHM_SUCCEEDED(khm_get_identity_expiration_time(ctx, cache,
335 if ( expiration > best_match_expiration ) {
336 best_match_expiration = expiration;
337 StringCbCopyA(best_match_ccname,
338 sizeof(best_match_ccname),
340 StringCbCatA(best_match_ccname,
341 sizeof(best_match_ccname),
347 if(ctx != NULL && cache != NULL)
348 (*pkrb5_cc_close)(ctx, cache);
354 if (KHM_SUCCEEDED(kmm_get_plugins_config(0, &csp_plugins))) {
355 khc_open_space(csp_plugins, L"Krb5Cred\\Parameters", 0, &csp_params);
356 khc_close_space(csp_plugins);
361 if (csp_params == NULL) {
367 KHM_SUCCEEDED(khc_read_int32(csp_params, L"MsLsaList", &t)) && t) {
368 code = (*pkrb5_cc_resolve)(ctx, "MSLSA:", &cache);
369 if (code == 0 && cache) {
370 if (KHM_SUCCEEDED(khm_get_identity_expiration_time(ctx, cache,
373 if ( expiration > best_match_expiration ) {
374 best_match_expiration = expiration;
375 StringCbCopyA(best_match_ccname, sizeof(best_match_ccname),
382 if (ctx != NULL && cache != NULL)
383 (*pkrb5_cc_close)(ctx, cache);
389 khc_read_multi_string(csp_params, L"FileCCList", NULL, &cb)
390 == KHM_ERROR_TOO_LONG &&
391 cb > sizeof(wchar_t) * 2) {
394 char ccname[MAX_PATH + 6];
402 khc_read_multi_string(csp_params, L"FileCCList", ms, &cb);
403 for(t = ms; t && *t; t = multi_string_next(t)) {
404 StringCchPrintfA(ccname, ARRAYLENGTH(ccname),
407 code = (*pkrb5_cc_resolve)(ctx, ccname, &cache);
411 if (KHM_SUCCEEDED(khm_get_identity_expiration_time(ctx, cache,
414 if ( expiration > best_match_expiration ) {
415 best_match_expiration = expiration;
416 StringCbCopyA(best_match_ccname,
417 sizeof(best_match_ccname),
423 if (ctx != NULL && cache != NULL)
424 (*pkrb5_cc_close)(ctx, cache);
432 khc_close_space(csp_params);
435 (*pcc_free_NC_info)(cc_ctx, &pNCi);
438 (*pcc_shutdown)(&cc_ctx);
440 if (best_match_ccname[0]) {
442 if (*pcbbuf = AnsiStrToUnicode((wchar_t *)buffer,
444 best_match_ccname)) {
446 *pcbbuf = (*pcbbuf + 1) * sizeof(wchar_t);
448 return KHM_ERROR_SUCCESS;
453 return KHM_ERROR_GENERAL;