2 * Copyright (c) 2003 SkyRope, LLC
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * - Neither the name of Skyrope, LLC nor the names of its contributors may be
14 * used to endorse or promote products derived from this software without
15 * specific prior written permission from Skyrope, LLC.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Portions of this code are derived from portions of the MIT
30 * Leash Ticket Manager and LoadFuncs utilities. For these portions the
31 * following copyright applies.
33 * Copyright (c) 2003,2004 by the Massachusetts Institute of Technology.
34 * All rights reserved.
36 * Export of this software from the United States of America may
37 * require a specific license from the United States Government.
38 * It is the responsibility of any person or organization contemplating
39 * export to obtain such a license before exporting.
41 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
42 * distribute this software and its documentation for any purpose and
43 * without fee is hereby granted, provided that the above copyright
44 * notice appear in all copies and that both that copyright notice and
45 * this permission notice appear in supporting documentation, and that
46 * the name of M.I.T. not be used in advertising or publicity pertaining
47 * to distribution of the software without specific, written prior
48 * permission. Furthermore if you modify this software you must label
49 * your software as modified software and not distribute it in such a
50 * fashion that it might be confused with the original M.I.T. software.
51 * M.I.T. makes no representations about the suitability of
52 * this software for any purpose. It is provided "as is" without express
53 * or implied warranty.
60 #include "afskfw-int.h"
64 #include <rxkad_prototypes.h> /* for life_to_time */
67 * TIMING _____________________________________________________________________
71 #define cminREMIND_TEST 1 // test every minute for expired creds
72 #define cminREMIND_WARN 15 // warn if creds expire in 15 minutes
73 #define cminRENEW 20 // renew creds when there are 20 minutes remaining
74 #define cminMINLIFE 30 // minimum life of Kerberos creds
76 #define c100ns1SECOND (LONGLONG)10000000
77 #define cmsec1SECOND 1000
78 #define cmsec1MINUTE 60000
79 #define csec1MINUTE 60
81 /* Function Pointer Declarations for Delayed Loading */
83 DECL_FUNC_PTR(cc_initialize);
84 DECL_FUNC_PTR(cc_shutdown);
85 DECL_FUNC_PTR(cc_get_NC_info);
86 DECL_FUNC_PTR(cc_free_NC_info);
89 DECL_FUNC_PTR(Leash_get_default_lifetime);
90 DECL_FUNC_PTR(Leash_get_default_forwardable);
91 DECL_FUNC_PTR(Leash_get_default_renew_till);
92 DECL_FUNC_PTR(Leash_get_default_noaddresses);
93 DECL_FUNC_PTR(Leash_get_default_proxiable);
94 DECL_FUNC_PTR(Leash_get_default_publicip);
95 DECL_FUNC_PTR(Leash_get_default_use_krb4);
96 DECL_FUNC_PTR(Leash_get_default_life_min);
97 DECL_FUNC_PTR(Leash_get_default_life_max);
98 DECL_FUNC_PTR(Leash_get_default_renew_min);
99 DECL_FUNC_PTR(Leash_get_default_renew_max);
100 DECL_FUNC_PTR(Leash_get_default_renewable);
103 DECL_FUNC_PTR(krb5_change_password);
104 DECL_FUNC_PTR(krb5_get_init_creds_opt_init);
105 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_tkt_life);
106 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_renew_life);
107 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_forwardable);
108 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_proxiable);
109 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_address_list);
110 DECL_FUNC_PTR(krb5_get_init_creds_password);
111 DECL_FUNC_PTR(krb5_build_principal_ext);
112 DECL_FUNC_PTR(krb5_cc_get_name);
113 DECL_FUNC_PTR(krb5_cc_resolve);
114 DECL_FUNC_PTR(krb5_cc_default);
115 DECL_FUNC_PTR(krb5_cc_default_name);
116 DECL_FUNC_PTR(krb5_cc_set_default_name);
117 DECL_FUNC_PTR(krb5_cc_initialize);
118 DECL_FUNC_PTR(krb5_cc_destroy);
119 DECL_FUNC_PTR(krb5_cc_close);
120 DECL_FUNC_PTR(krb5_cc_store_cred);
121 DECL_FUNC_PTR(krb5_cc_copy_creds);
122 DECL_FUNC_PTR(krb5_cc_retrieve_cred);
123 DECL_FUNC_PTR(krb5_cc_get_principal);
124 DECL_FUNC_PTR(krb5_cc_start_seq_get);
125 DECL_FUNC_PTR(krb5_cc_next_cred);
126 DECL_FUNC_PTR(krb5_cc_end_seq_get);
127 DECL_FUNC_PTR(krb5_cc_remove_cred);
128 DECL_FUNC_PTR(krb5_cc_set_flags);
129 DECL_FUNC_PTR(krb5_cc_get_type);
130 DECL_FUNC_PTR(krb5_free_context);
131 DECL_FUNC_PTR(krb5_free_cred_contents);
132 DECL_FUNC_PTR(krb5_free_principal);
133 DECL_FUNC_PTR(krb5_get_in_tkt_with_password);
134 DECL_FUNC_PTR(krb5_init_context);
135 DECL_FUNC_PTR(krb5_parse_name);
136 DECL_FUNC_PTR(krb5_timeofday);
137 DECL_FUNC_PTR(krb5_timestamp_to_sfstring);
138 DECL_FUNC_PTR(krb5_unparse_name);
139 DECL_FUNC_PTR(krb5_get_credentials);
140 DECL_FUNC_PTR(krb5_mk_req);
141 DECL_FUNC_PTR(krb5_sname_to_principal);
142 DECL_FUNC_PTR(krb5_get_credentials_renew);
143 DECL_FUNC_PTR(krb5_free_data);
144 DECL_FUNC_PTR(krb5_free_data_contents);
145 DECL_FUNC_PTR(krb5_free_unparsed_name);
146 DECL_FUNC_PTR(krb5_os_localaddr);
147 DECL_FUNC_PTR(krb5_copy_keyblock_contents);
148 DECL_FUNC_PTR(krb5_copy_data);
149 DECL_FUNC_PTR(krb5_free_creds);
150 DECL_FUNC_PTR(krb5_build_principal);
151 DECL_FUNC_PTR(krb5_get_renewed_creds);
152 DECL_FUNC_PTR(krb5_get_default_config_files);
153 DECL_FUNC_PTR(krb5_free_config_files);
154 DECL_FUNC_PTR(krb5_get_default_realm);
155 DECL_FUNC_PTR(krb5_free_ticket);
156 DECL_FUNC_PTR(krb5_decode_ticket);
157 DECL_FUNC_PTR(krb5_get_host_realm);
158 DECL_FUNC_PTR(krb5_free_host_realm);
159 DECL_FUNC_PTR(krb5_free_addresses);
160 DECL_FUNC_PTR(krb5_c_random_make_octets);
163 DECL_FUNC_PTR(krb524_init_ets);
164 DECL_FUNC_PTR(krb524_convert_creds_kdc);
167 DECL_FUNC_PTR(krb_get_cred);
168 DECL_FUNC_PTR(tkt_string);
169 DECL_FUNC_PTR(krb_get_tf_realm);
170 DECL_FUNC_PTR(krb_mk_req);
173 DECL_FUNC_PTR(com_err);
174 DECL_FUNC_PTR(error_message);
177 DECL_FUNC_PTR(profile_init);
178 DECL_FUNC_PTR(profile_release);
179 DECL_FUNC_PTR(profile_get_subsection_names);
180 DECL_FUNC_PTR(profile_free_list);
181 DECL_FUNC_PTR(profile_get_string);
182 DECL_FUNC_PTR(profile_release_string);
185 DECL_FUNC_PTR(OpenSCManagerA);
186 DECL_FUNC_PTR(OpenServiceA);
187 DECL_FUNC_PTR(QueryServiceStatus);
188 DECL_FUNC_PTR(CloseServiceHandle);
190 DECL_FUNC_PTR(LsaNtStatusToWinError);
191 #endif /* USE_MS2MIT */
195 DECL_FUNC_PTR(LsaConnectUntrusted);
196 DECL_FUNC_PTR(LsaLookupAuthenticationPackage);
197 DECL_FUNC_PTR(LsaCallAuthenticationPackage);
198 DECL_FUNC_PTR(LsaFreeReturnBuffer);
199 DECL_FUNC_PTR(LsaGetLogonSessionData);
200 #endif /* USE_MS2MIT */
203 FUNC_INFO ccapi_fi[] = {
204 MAKE_FUNC_INFO(cc_initialize),
205 MAKE_FUNC_INFO(cc_shutdown),
206 MAKE_FUNC_INFO(cc_get_NC_info),
207 MAKE_FUNC_INFO(cc_free_NC_info),
211 FUNC_INFO leash_fi[] = {
212 MAKE_FUNC_INFO(Leash_get_default_lifetime),
213 MAKE_FUNC_INFO(Leash_get_default_renew_till),
214 MAKE_FUNC_INFO(Leash_get_default_forwardable),
215 MAKE_FUNC_INFO(Leash_get_default_noaddresses),
216 MAKE_FUNC_INFO(Leash_get_default_proxiable),
217 MAKE_FUNC_INFO(Leash_get_default_publicip),
218 MAKE_FUNC_INFO(Leash_get_default_use_krb4),
219 MAKE_FUNC_INFO(Leash_get_default_life_min),
220 MAKE_FUNC_INFO(Leash_get_default_life_max),
221 MAKE_FUNC_INFO(Leash_get_default_renew_min),
222 MAKE_FUNC_INFO(Leash_get_default_renew_max),
223 MAKE_FUNC_INFO(Leash_get_default_renewable),
227 FUNC_INFO k5_fi[] = {
228 MAKE_FUNC_INFO(krb5_change_password),
229 MAKE_FUNC_INFO(krb5_get_init_creds_opt_init),
230 MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_tkt_life),
231 MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_renew_life),
232 MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_forwardable),
233 MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_proxiable),
234 MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_address_list),
235 MAKE_FUNC_INFO(krb5_get_init_creds_password),
236 MAKE_FUNC_INFO(krb5_build_principal_ext),
237 MAKE_FUNC_INFO(krb5_cc_get_name),
238 MAKE_FUNC_INFO(krb5_cc_resolve),
239 MAKE_FUNC_INFO(krb5_cc_default),
240 MAKE_FUNC_INFO(krb5_cc_default_name),
241 MAKE_FUNC_INFO(krb5_cc_set_default_name),
242 MAKE_FUNC_INFO(krb5_cc_initialize),
243 MAKE_FUNC_INFO(krb5_cc_destroy),
244 MAKE_FUNC_INFO(krb5_cc_close),
245 MAKE_FUNC_INFO(krb5_cc_copy_creds),
246 MAKE_FUNC_INFO(krb5_cc_store_cred),
247 MAKE_FUNC_INFO(krb5_cc_retrieve_cred),
248 MAKE_FUNC_INFO(krb5_cc_get_principal),
249 MAKE_FUNC_INFO(krb5_cc_start_seq_get),
250 MAKE_FUNC_INFO(krb5_cc_next_cred),
251 MAKE_FUNC_INFO(krb5_cc_end_seq_get),
252 MAKE_FUNC_INFO(krb5_cc_remove_cred),
253 MAKE_FUNC_INFO(krb5_cc_set_flags),
254 MAKE_FUNC_INFO(krb5_cc_get_type),
255 MAKE_FUNC_INFO(krb5_free_context),
256 MAKE_FUNC_INFO(krb5_free_cred_contents),
257 MAKE_FUNC_INFO(krb5_free_principal),
258 MAKE_FUNC_INFO(krb5_get_in_tkt_with_password),
259 MAKE_FUNC_INFO(krb5_init_context),
260 MAKE_FUNC_INFO(krb5_parse_name),
261 MAKE_FUNC_INFO(krb5_timeofday),
262 MAKE_FUNC_INFO(krb5_timestamp_to_sfstring),
263 MAKE_FUNC_INFO(krb5_unparse_name),
264 MAKE_FUNC_INFO(krb5_get_credentials),
265 MAKE_FUNC_INFO(krb5_mk_req),
266 MAKE_FUNC_INFO(krb5_sname_to_principal),
267 MAKE_FUNC_INFO(krb5_get_credentials_renew),
268 MAKE_FUNC_INFO(krb5_free_data),
269 MAKE_FUNC_INFO(krb5_free_data_contents),
270 MAKE_FUNC_INFO(krb5_free_unparsed_name),
271 MAKE_FUNC_INFO(krb5_os_localaddr),
272 MAKE_FUNC_INFO(krb5_copy_keyblock_contents),
273 MAKE_FUNC_INFO(krb5_copy_data),
274 MAKE_FUNC_INFO(krb5_free_creds),
275 MAKE_FUNC_INFO(krb5_build_principal),
276 MAKE_FUNC_INFO(krb5_get_renewed_creds),
277 MAKE_FUNC_INFO(krb5_free_addresses),
278 MAKE_FUNC_INFO(krb5_get_default_config_files),
279 MAKE_FUNC_INFO(krb5_free_config_files),
280 MAKE_FUNC_INFO(krb5_get_default_realm),
281 MAKE_FUNC_INFO(krb5_free_ticket),
282 MAKE_FUNC_INFO(krb5_decode_ticket),
283 MAKE_FUNC_INFO(krb5_get_host_realm),
284 MAKE_FUNC_INFO(krb5_free_host_realm),
285 MAKE_FUNC_INFO(krb5_free_addresses),
286 MAKE_FUNC_INFO(krb5_c_random_make_octets),
290 FUNC_INFO k4_fi[] = {
291 MAKE_FUNC_INFO(krb_get_cred),
292 MAKE_FUNC_INFO(krb_get_tf_realm),
293 MAKE_FUNC_INFO(krb_mk_req),
294 MAKE_FUNC_INFO(tkt_string),
298 FUNC_INFO k524_fi[] = {
299 MAKE_FUNC_INFO(krb524_init_ets),
300 MAKE_FUNC_INFO(krb524_convert_creds_kdc),
304 FUNC_INFO profile_fi[] = {
305 MAKE_FUNC_INFO(profile_init),
306 MAKE_FUNC_INFO(profile_release),
307 MAKE_FUNC_INFO(profile_get_subsection_names),
308 MAKE_FUNC_INFO(profile_free_list),
309 MAKE_FUNC_INFO(profile_get_string),
310 MAKE_FUNC_INFO(profile_release_string),
314 FUNC_INFO ce_fi[] = {
315 MAKE_FUNC_INFO(com_err),
316 MAKE_FUNC_INFO(error_message),
320 FUNC_INFO service_fi[] = {
321 MAKE_FUNC_INFO(OpenSCManagerA),
322 MAKE_FUNC_INFO(OpenServiceA),
323 MAKE_FUNC_INFO(QueryServiceStatus),
324 MAKE_FUNC_INFO(CloseServiceHandle),
326 MAKE_FUNC_INFO(LsaNtStatusToWinError),
327 #endif /* USE_MS2MIT */
332 FUNC_INFO lsa_fi[] = {
333 MAKE_FUNC_INFO(LsaConnectUntrusted),
334 MAKE_FUNC_INFO(LsaLookupAuthenticationPackage),
335 MAKE_FUNC_INFO(LsaCallAuthenticationPackage),
336 MAKE_FUNC_INFO(LsaFreeReturnBuffer),
337 MAKE_FUNC_INFO(LsaGetLogonSessionData),
340 #endif /* USE_MS2MIT */
342 /* Static Prototypes */
343 char *afs_realm_of_cell(krb5_context, struct afsconf_cell *);
344 static long get_cellconfig_callback(void *, struct sockaddr_in *, char *);
345 int KFW_AFS_get_cellconfig(char *, struct afsconf_cell *, char *);
346 static krb5_error_code KRB5_CALLCONV KRB5_prompter( krb5_context context,
347 void *data, const char *name, const char *banner, int num_prompts,
348 krb5_prompt prompts[]);
351 /* Static Declarations */
352 static int inited = 0;
353 static int mid_cnt = 0;
354 static struct textField * mid_tb = NULL;
355 static HINSTANCE hKrb5 = 0;
356 static HINSTANCE hKrb4 = 0;
357 static HINSTANCE hKrb524 = 0;
359 static HINSTANCE hSecur32 = 0;
360 #endif /* USE_MS2MIT */
361 static HINSTANCE hAdvApi32 = 0;
362 static HINSTANCE hComErr = 0;
363 static HINSTANCE hService = 0;
364 static HINSTANCE hProfile = 0;
365 static HINSTANCE hLeash = 0;
366 static HINSTANCE hCCAPI = 0;
367 static struct principal_ccache_data * princ_cc_data = NULL;
368 static struct cell_principal_map * cell_princ_map = NULL;
373 static int inited = 0;
376 char mutexName[MAX_PATH];
377 HANDLE hMutex = NULL;
379 sprintf(mutexName, "AFS KFW Init pid=%d", getpid());
381 hMutex = CreateMutex( NULL, TRUE, mutexName );
382 if ( GetLastError() == ERROR_ALREADY_EXISTS ) {
383 if ( WaitForSingleObject( hMutex, INFINITE ) != WAIT_OBJECT_0 ) {
389 LoadFuncs(KRB5_DLL, k5_fi, &hKrb5, 0, 1, 0, 0);
390 LoadFuncs(KRB4_DLL, k4_fi, &hKrb4, 0, 1, 0, 0);
391 LoadFuncs(COMERR_DLL, ce_fi, &hComErr, 0, 0, 1, 0);
392 LoadFuncs(SERVICE_DLL, service_fi, &hService, 0, 1, 0, 0);
394 LoadFuncs(SECUR32_DLL, lsa_fi, &hSecur32, 0, 1, 1, 1);
395 #endif /* USE_MS2MIT */
396 LoadFuncs(KRB524_DLL, k524_fi, &hKrb524, 0, 1, 1, 1);
397 LoadFuncs(PROFILE_DLL, profile_fi, &hProfile, 0, 1, 0, 0);
398 LoadFuncs(LEASH_DLL, leash_fi, &hLeash, 0, 1, 0, 0);
399 LoadFuncs(CCAPI_DLL, ccapi_fi, &hCCAPI, 0, 1, 0, 0);
401 if ( KFW_is_available() ) {
402 char rootcell[MAXCELLCHARS+1];
404 KFW_import_windows_lsa();
405 #endif /* USE_MS2MIT */
406 KFW_import_ccache_data();
407 KFW_AFS_renew_expiring_tokens();
409 /* WIN32 NOTE: no way to get max chars */
410 if (!cm_GetRootCellName(rootcell))
411 KFW_AFS_renew_token_for_cell(rootcell);
414 ReleaseMutex(hMutex);
427 FreeLibrary(hProfile);
429 FreeLibrary(hComErr);
431 FreeLibrary(hService);
434 FreeLibrary(hSecur32);
435 #endif /* USE_MS2MIT */
437 FreeLibrary(hKrb524);
444 static char OpenAFSConfigKeyName[] = "SOFTWARE\\OpenAFS\\Client";
447 KFW_is_available(void)
453 code = RegOpenKeyEx(HKEY_CURRENT_USER, OpenAFSConfigKeyName,
454 0, KEY_QUERY_VALUE, &parmKey);
455 if (code != ERROR_SUCCESS)
456 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, OpenAFSConfigKeyName,
457 0, KEY_QUERY_VALUE, &parmKey);
458 if (code == ERROR_SUCCESS) {
459 len = sizeof(enableKFW);
460 code = RegQueryValueEx(parmKey, "EnableKFW", NULL, NULL,
461 (BYTE *) &enableKFW, &len);
462 if (code != ERROR_SUCCESS) {
465 RegCloseKey (parmKey);
472 if ( hKrb5 && hComErr && hService &&
475 #endif /* USE_MS2MIT */
477 hProfile && hLeash && hCCAPI )
483 KRB5_error(krb5_error_code rc, LPCSTR FailedFunctionName,
484 int FreeContextFlag, krb5_context * ctx,
489 int krb5Error = ((int)(rc & 255));
501 errText = perror_message(rc);
502 _snprintf(message, sizeof(message),
503 "%s\n(Kerberos error %ld)\n\n%s failed",
508 if ( IsDebuggerPresent() )
509 OutputDebugString(message);
511 MessageBox(NULL, message, "Kerberos Five", MB_OK | MB_ICONERROR |
514 if (FreeContextFlag == 1)
516 if (ctx && *ctx != NULL)
518 if (cache && *cache != NULL) {
519 pkrb5_cc_close(*ctx, *cache);
523 pkrb5_free_context(*ctx);
532 KFW_AFS_update_princ_ccache_data(krb5_context ctx, krb5_ccache cc, int lsa)
534 struct principal_ccache_data * next = princ_cc_data;
535 krb5_principal principal = 0;
537 const char * ccname = NULL;
538 krb5_error_code code = 0;
539 krb5_error_code cc_code = 0;
545 if (ctx == 0 || cc == 0)
548 code = pkrb5_cc_get_principal(ctx, cc, &principal);
551 code = pkrb5_unparse_name(ctx, principal, &pname);
552 if ( code ) goto cleanup;
554 ccname = pkrb5_cc_get_name(ctx, cc);
555 if (!ccname) goto cleanup;
557 // Search the existing list to see if we have a match
559 for ( ; next ; next = next->next ) {
560 if ( !strcmp(next->principal,pname) && !strcmp(next->ccache_name, ccname) )
565 // If not, match add a new node to the beginning of the list and assign init it
567 next = (struct principal_ccache_data *) malloc(sizeof(struct principal_ccache_data));
568 next->next = princ_cc_data;
569 princ_cc_data = next;
570 next->principal = _strdup(pname);
571 next->ccache_name = _strdup(ccname);
572 next->from_lsa = lsa;
574 next->expiration_time = 0;
578 flags = 0; // turn off OPENCLOSE mode
579 code = pkrb5_cc_set_flags(ctx, cc, flags);
580 if ( code ) goto cleanup;
582 code = pkrb5_timeofday(ctx, &now);
584 cc_code = pkrb5_cc_start_seq_get(ctx, cc, &cur);
586 while (!(cc_code = pkrb5_cc_next_cred(ctx, cc, &cur, &creds))) {
587 if ( creds.ticket_flags & TKT_FLG_INITIAL ) {
589 // we found the ticket we are looking for
590 // check validity of timestamp
591 // We add a 5 minutes fudge factor to compensate for potential
592 // clock skew errors between the KDC and client OS
594 valid = ((creds.times.starttime > 0) &&
595 now >= (creds.times.starttime - 300) &&
596 now < (creds.times.endtime + 300) &&
597 !(creds.ticket_flags & TKT_FLG_INVALID));
599 if ( next->from_lsa) {
601 next->expiration_time = creds.times.endtime;
603 } else if ( valid ) {
605 next->expiration_time = creds.times.endtime;
606 next->renew = (creds.times.renew_till > creds.times.endtime) &&
607 (creds.ticket_flags & TKT_FLG_RENEWABLE);
610 next->expiration_time = 0;
614 pkrb5_free_cred_contents(ctx, &creds);
615 cc_code = KRB5_CC_END;
618 pkrb5_free_cred_contents(ctx, &creds);
621 if (cc_code == KRB5_CC_END) {
622 code = pkrb5_cc_end_seq_get(ctx, cc, &cur);
623 if (code) goto cleanup;
627 flags = KRB5_TC_OPENCLOSE; //turn on OPENCLOSE
628 code = pkrb5_cc_set_flags(ctx, cc, flags);
631 pkrb5_free_unparsed_name(ctx,pname);
633 pkrb5_free_principal(ctx,principal);
637 KFW_AFS_find_ccache_for_principal(krb5_context ctx, char * principal, char **ccache, int valid_only)
639 struct principal_ccache_data * next = princ_cc_data;
640 char * response = NULL;
642 if ( !principal || !ccache )
646 if ( (!valid_only || !next->expired) && !strcmp(next->principal,principal) ) {
648 // we always want to prefer the MS Kerberos LSA cache or
649 // the cache afscreds created specifically for the principal
650 // if the current entry is either one, drop the previous find
651 if ( next->from_lsa || !strcmp(next->ccache_name,principal) )
654 response = _strdup(next->ccache_name);
655 // MS Kerberos LSA is our best option so use it and quit
656 if ( next->from_lsa )
670 KFW_AFS_delete_princ_ccache_data(krb5_context ctx, char * pname, char * ccname)
672 struct principal_ccache_data ** next = &princ_cc_data;
674 if ( !pname && !ccname )
678 if ( !strcmp((*next)->principal,pname) ||
679 !strcmp((*next)->ccache_name,ccname) ) {
681 free((*next)->principal);
682 free((*next)->ccache_name);
684 (*next) = (*next)->next;
691 KFW_AFS_update_cell_princ_map(krb5_context ctx, char * cell, char *pname, int active)
693 struct cell_principal_map * next = cell_princ_map;
695 // Search the existing list to see if we have a match
697 for ( ; next ; next = next->next ) {
698 if ( !strcmp(next->cell, cell) ) {
699 if ( !strcmp(next->principal,pname) ) {
700 next->active = active;
703 // OpenAFS currently has a restriction of one active token per cell
704 // Therefore, whenever we update the table with a new active cell we
705 // must mark all of the other principal to cell entries as inactive.
713 // If not, match add a new node to the beginning of the list and assign init it
715 next = (struct cell_principal_map *) malloc(sizeof(struct cell_principal_map));
716 next->next = cell_princ_map;
717 cell_princ_map = next;
718 next->principal = _strdup(pname);
719 next->cell = _strdup(cell);
720 next->active = active;
725 KFW_AFS_delete_cell_princ_maps(krb5_context ctx, char * pname, char * cell)
727 struct cell_principal_map ** next = &cell_princ_map;
729 if ( !pname && !cell )
733 if ( !strcmp((*next)->principal,pname) ||
734 !strcmp((*next)->cell,cell) ) {
736 free((*next)->principal);
739 (*next) = (*next)->next;
745 // Returns (if possible) a principal which has been known in
746 // the past to have been used to obtain tokens for the specified
748 // TODO: Attempt to return one which has not yet expired by checking
749 // the principal/ccache data
751 KFW_AFS_find_principals_for_cell(krb5_context ctx, char * cell, char **principals[], int active_only)
753 struct cell_principal_map * next_map = cell_princ_map;
754 const char * princ = NULL;
761 if ( (!active_only || next_map->active) && !strcmp(next_map->cell,cell) ) {
764 next_map = next_map->next;
767 if ( !principals || !count )
770 *principals = (char **) malloc(sizeof(char *) * count);
771 for ( next_map = cell_princ_map, i=0 ; next_map && i<count; next_map = next_map->next )
773 if ( (!active_only || next_map->active) && !strcmp(next_map->cell,cell) ) {
774 (*principals)[i++] = _strdup(next_map->principal);
781 KFW_AFS_find_cells_for_princ(krb5_context ctx, char * pname, char **cells[], int active_only)
784 struct cell_principal_map * next_map = cell_princ_map;
785 const char * princ = NULL;
791 if ( (!active_only || next_map->active) && !strcmp(next_map->principal,pname) ) {
794 next_map = next_map->next;
800 *cells = (char **) malloc(sizeof(char *) * count);
801 for ( next_map = cell_princ_map, i=0 ; next_map && i<count; next_map = next_map->next )
803 if ( (!active_only || next_map->active) && !strcmp(next_map->principal,pname) ) {
804 (*cells)[i++] = _strdup(next_map->cell);
810 /* Given a principal return an existing ccache or create one and return */
812 KFW_get_ccache(krb5_context alt_ctx, krb5_principal principal, krb5_ccache * cc)
817 krb5_error_code code;
819 if (!pkrb5_init_context)
825 code = pkrb5_init_context(&ctx);
826 if (code) goto cleanup;
830 code = pkrb5_unparse_name(ctx, principal, &pname);
831 if (code) goto cleanup;
833 if ( !KFW_AFS_find_ccache_for_principal(ctx,pname,&ccname,TRUE) &&
834 !KFW_AFS_find_ccache_for_principal(ctx,pname,&ccname,FALSE)) {
835 ccname = (char *)malloc(strlen(pname) + 5);
836 sprintf(ccname,"API:%s",pname);
838 code = pkrb5_cc_resolve(ctx, ccname, cc);
840 code = pkrb5_cc_default(ctx, cc);
841 if (code) goto cleanup;
848 pkrb5_free_unparsed_name(ctx,pname);
849 if (ctx && (ctx != alt_ctx))
850 pkrb5_free_context(ctx);
855 // Import Microsoft Credentials into a new MIT ccache
857 KFW_import_windows_lsa(void)
859 krb5_context ctx = 0;
861 krb5_principal princ = 0;
863 krb5_data * princ_realm;
864 krb5_error_code code;
865 char cell[128]="", realm[128]="";
868 if (!pkrb5_init_context)
872 if ( !MSLSA_IsKerberosLogon() )
876 code = pkrb5_init_context(&ctx);
877 if (code) goto cleanup;
879 code = pkrb5_cc_resolve(ctx, LSA_CCNAME, &cc);
880 if (code) goto cleanup;
882 KFW_AFS_update_princ_ccache_data(ctx, cc, TRUE);
884 code = pkrb5_cc_get_principal(ctx, cc, &princ);
885 if ( code ) goto cleanup;
887 code = pkrb5_unparse_name(ctx,princ,&pname);
888 if ( code ) goto cleanup;
890 princ_realm = krb5_princ_realm(ctx, princ);
891 for ( i=0; i<princ_realm->length; i++ ) {
892 realm[i] = princ_realm->data[i];
893 cell[i] = tolower(princ_realm->data[i]);
898 code = KFW_AFS_klog(ctx, cc, "afs", cell, realm, pLeash_get_default_lifetime(),NULL);
899 if ( IsDebuggerPresent() ) {
901 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
902 OutputDebugString(message);
904 if ( code ) goto cleanup;
906 KFW_AFS_update_cell_princ_map(ctx, cell, pname, TRUE);
910 pkrb5_free_unparsed_name(ctx,pname);
912 pkrb5_free_principal(ctx,princ);
914 pkrb5_cc_close(ctx,cc);
916 pkrb5_free_context(ctx);
918 #endif /* USE_MS2MIT */
920 // If there are existing MIT credentials, copy them to a new
921 // ccache named after the principal
923 // Enumerate all existing MIT ccaches and construct entries
924 // in the principal_ccache table
926 // Enumerate all existing AFS Tokens and construct entries
927 // in the cell_principal table
929 KFW_import_ccache_data(void)
931 krb5_context ctx = 0;
933 krb5_principal principal = 0;
935 krb5_error_code code;
936 krb5_error_code cc_code;
939 struct _infoNC ** pNCi = NULL;
942 if ( !pcc_initialize )
945 if ( IsDebuggerPresent() )
946 OutputDebugString("KFW_import_ccache_data()\n");
948 code = pcc_initialize(&cc_ctx, CC_API_VER_2, NULL, NULL);
949 if (code) goto cleanup;
951 code = pcc_get_NC_info(cc_ctx, &pNCi);
952 if (code) goto cleanup;
954 code = pkrb5_init_context(&ctx);
955 if (code) goto cleanup;
957 for ( i=0; pNCi[i]; i++ ) {
958 if ( pNCi[i]->vers != CC_CRED_V5 )
960 if ( IsDebuggerPresent() ) {
961 OutputDebugString("Principal: ");
962 OutputDebugString(pNCi[i]->principal);
963 OutputDebugString(" in ccache ");
964 OutputDebugString(pNCi[i]->name);
965 OutputDebugString("\n");
967 if ( strcmp(pNCi[i]->name,pNCi[i]->principal)
968 && strcmp(pNCi[i]->name,LSA_CCNAME)
971 for ( j=0; pNCi[j]; j++ ) {
972 if (!strcmp(pNCi[j]->name,pNCi[i]->principal)) {
978 code = pkrb5_cc_resolve(ctx, pNCi[i]->principal, &cc);
979 if (code) goto loop_cleanup;
982 krb5_ccache oldcc = 0;
984 if ( IsDebuggerPresent() )
985 OutputDebugString("copying ccache data to new ccache\n");
987 code = pkrb5_parse_name(ctx, pNCi[i]->principal, &principal);
988 if (code) goto loop_cleanup;
989 code = pkrb5_cc_initialize(ctx, cc, principal);
990 if (code) goto loop_cleanup;
992 code = pkrb5_cc_resolve(ctx, pNCi[i]->name, &oldcc);
993 if (code) goto loop_cleanup;
994 code = pkrb5_cc_copy_creds(ctx,oldcc,cc);
996 code = pkrb5_cc_close(ctx,cc);
998 code = pkrb5_cc_close(ctx,oldcc);
1000 KRB5_error(code, "krb5_cc_copy_creds", 0, NULL, NULL);
1003 code = pkrb5_cc_close(ctx,oldcc);
1006 code = pkrb5_cc_resolve(ctx, pNCi[i]->name, &cc);
1007 if (code) goto loop_cleanup;
1010 flags = 0; // turn off OPENCLOSE mode
1011 code = pkrb5_cc_set_flags(ctx, cc, flags);
1012 if ( code ) goto cleanup;
1014 KFW_AFS_update_princ_ccache_data(ctx, cc, !strcmp(pNCi[i]->name,LSA_CCNAME));
1016 cc_code = pkrb5_cc_start_seq_get(ctx, cc, &cur);
1018 while (!(cc_code = pkrb5_cc_next_cred(ctx, cc, &cur, &creds))) {
1019 krb5_data * sname = krb5_princ_name(ctx, creds.server);
1020 krb5_data * cell = krb5_princ_component(ctx, creds.server, 1);
1021 krb5_data * realm = krb5_princ_realm(ctx, creds.server);
1022 if ( sname && cell && !strcmp("afs",sname->data) ) {
1023 struct ktc_principal aserver;
1024 struct ktc_principal aclient;
1025 struct ktc_token atoken;
1028 if ( IsDebuggerPresent() ) {
1029 OutputDebugString("Found AFS ticket: ");
1030 OutputDebugString(sname->data);
1032 OutputDebugString("/");
1033 OutputDebugString(cell->data);
1035 OutputDebugString("@");
1036 OutputDebugString(realm->data);
1037 OutputDebugString("\n");
1040 memset(&aserver, '\0', sizeof(aserver));
1041 strcpy(aserver.name, sname->data);
1042 strcpy(aserver.cell, cell->data);
1044 code = ktc_GetToken(&aserver, &atoken, sizeof(atoken), &aclient);
1046 // Found a token in AFS Client Server which matches
1047 char pname[128], *p, *q;
1048 for ( p=pname, q=aclient.name; *q; p++, q++)
1050 for ( *p++ = '@', q=aclient.cell; *q; p++, q++)
1054 if ( IsDebuggerPresent() ) {
1055 OutputDebugString("Found AFS token: ");
1056 OutputDebugString(pname);
1057 OutputDebugString("\n");
1060 if ( strcmp(pname,pNCi[i]->principal) )
1062 KFW_AFS_update_cell_princ_map(ctx, cell->data, pNCi[i]->principal, active);
1064 // Attempt to import it
1065 KFW_AFS_update_cell_princ_map(ctx, cell->data, pNCi[i]->principal, active);
1067 if ( IsDebuggerPresent() ) {
1068 OutputDebugString("Calling KFW_AFS_klog() to obtain token\n");
1071 code = KFW_AFS_klog(ctx, cc, "afs", cell->data, realm->data, pLeash_get_default_lifetime(),NULL);
1072 if ( IsDebuggerPresent() ) {
1074 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
1075 OutputDebugString(message);
1078 } else if ( IsDebuggerPresent() ) {
1079 OutputDebugString("Found ticket: ");
1080 OutputDebugString(sname->data);
1081 if ( cell && cell->data ) {
1082 OutputDebugString("/");
1083 OutputDebugString(cell->data);
1085 OutputDebugString("@");
1086 OutputDebugString(realm->data);
1087 OutputDebugString("\n");
1089 pkrb5_free_cred_contents(ctx, &creds);
1092 if (cc_code == KRB5_CC_END) {
1093 cc_code = pkrb5_cc_end_seq_get(ctx, cc, &cur);
1094 if (cc_code) goto loop_cleanup;
1098 flags = KRB5_TC_OPENCLOSE; //turn on OPENCLOSE
1099 code = pkrb5_cc_set_flags(ctx, cc, flags);
1101 pkrb5_cc_close(ctx,cc);
1105 pkrb5_free_principal(ctx,principal);
1112 pkrb5_free_context(ctx);
1114 pcc_free_NC_info(cc_ctx, &pNCi);
1116 pcc_shutdown(&cc_ctx);
1121 KFW_AFS_get_cred(char * username,
1128 krb5_context ctx = 0;
1131 char ** realmlist = 0;
1132 krb5_principal principal = 0;
1134 krb5_error_code code;
1135 char local_cell[MAXCELLCHARS+1];
1136 char **cells = NULL;
1138 struct afsconf_cell cellconfig;
1140 if (!pkrb5_init_context)
1143 if ( IsDebuggerPresent() ) {
1144 OutputDebugString("KFW_AFS_get_cred for token ");
1145 OutputDebugString(username);
1146 OutputDebugString(" in cell ");
1147 OutputDebugString(cell);
1148 OutputDebugString("\n");
1151 code = pkrb5_init_context(&ctx);
1152 if ( code ) goto cleanup;
1154 code = KFW_AFS_get_cellconfig( cell, (void*)&cellconfig, local_cell);
1155 if ( code ) goto cleanup;
1157 realm = strchr(username,'@');
1162 if ( !realm || !realm[0] )
1163 realm = afs_realm_of_cell(ctx, &cellconfig); // do not free
1165 if ( IsDebuggerPresent() ) {
1166 OutputDebugString("Realm: ");
1167 OutputDebugString(realm);
1168 OutputDebugString("\n");
1171 code = pkrb5_build_principal(ctx, &principal, strlen(realm),
1176 code = KFW_get_ccache(ctx, principal, &cc);
1177 if ( code ) goto cleanup;
1179 code = pkrb5_unparse_name(ctx, principal, &pname);
1180 if ( code ) goto cleanup;
1182 if ( lifetime == 0 )
1183 lifetime = pLeash_get_default_lifetime();
1185 code = KFW_kinit(ctx, cc, HWND_DESKTOP,
1189 pLeash_get_default_forwardable(),
1190 pLeash_get_default_proxiable(),
1191 pLeash_get_default_renewable() ? pLeash_get_default_renew_till() : 0,
1192 pLeash_get_default_noaddresses(),
1193 pLeash_get_default_publicip());
1194 if ( IsDebuggerPresent() ) {
1196 sprintf(message,"KFW_kinit() returns: %d\n",code);
1197 OutputDebugString(message);
1199 if ( code ) goto cleanup;
1201 KFW_AFS_update_princ_ccache_data(ctx, cc, FALSE);
1203 code = KFW_AFS_klog(ctx, cc, "afs", cell, realm, lifetime,smbname);
1204 if ( IsDebuggerPresent() ) {
1206 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
1207 OutputDebugString(message);
1209 if ( code ) goto cleanup;
1211 KFW_AFS_update_cell_princ_map(ctx, cell, pname, TRUE);
1213 // Attempt to obtain new tokens for other cells supported by the same
1215 cell_count = KFW_AFS_find_cells_for_princ(ctx, pname, &cells, TRUE);
1216 if ( cell_count > 1 ) {
1217 while ( cell_count-- ) {
1218 if ( strcmp(cells[cell_count],cell) ) {
1219 if ( IsDebuggerPresent() ) {
1221 sprintf(message,"found another cell for the same principal: %s\n",cell);
1222 OutputDebugString(message);
1224 code = KFW_AFS_get_cellconfig( cells[cell_count], (void*)&cellconfig, local_cell);
1225 if ( code ) continue;
1227 realm = afs_realm_of_cell(ctx, &cellconfig); // do not free
1228 if ( IsDebuggerPresent() ) {
1229 OutputDebugString("Realm: ");
1230 OutputDebugString(realm);
1231 OutputDebugString("\n");
1234 code = KFW_AFS_klog(ctx, cc, "afs", cells[cell_count], realm, lifetime,smbname);
1235 if ( IsDebuggerPresent() ) {
1237 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
1238 OutputDebugString(message);
1241 free(cells[cell_count]);
1244 } else if ( cell_count == 1 ) {
1251 pkrb5_free_unparsed_name(ctx,pname);
1253 pkrb5_cc_close(ctx, cc);
1255 if ( code && reasonP ) {
1256 *reasonP = (char *)perror_message(code);
1262 KFW_AFS_destroy_tickets_for_cell(char * cell)
1264 krb5_context ctx = 0;
1265 krb5_error_code code;
1267 char ** principals = NULL;
1269 if (!pkrb5_init_context)
1272 if ( IsDebuggerPresent() ) {
1273 OutputDebugString("KFW_AFS_destroy_ticets_for_cell: ");
1274 OutputDebugString(cell);
1275 OutputDebugString("\n");
1278 code = pkrb5_init_context(&ctx);
1281 count = KFW_AFS_find_principals_for_cell(ctx, cell, &principals, FALSE);
1283 krb5_principal princ = 0;
1287 int cell_count = KFW_AFS_find_cells_for_princ(ctx, principals[count], NULL, TRUE);
1288 if ( cell_count > 1 ) {
1289 // TODO - What we really should do here is verify whether or not any of the
1290 // other cells which use this principal to obtain its credentials actually
1291 // have valid tokens or not. If they are currently using these credentials
1292 // we will skip them. For the time being we assume that if there is an active
1293 // map in the table that they are actively being used.
1297 code = pkrb5_parse_name(ctx, principals[count], &princ);
1298 if (code) goto loop_cleanup;
1300 code = KFW_get_ccache(ctx, princ, &cc);
1301 if (code) goto loop_cleanup;
1303 code = pkrb5_cc_destroy(ctx, cc);
1308 pkrb5_cc_close(ctx, cc);
1312 pkrb5_free_principal(ctx, princ);
1316 KFW_AFS_update_cell_princ_map(ctx, cell, principals[count], FALSE);
1317 free(principals[count]);
1321 pkrb5_free_context(ctx);
1326 KFW_AFS_renew_expiring_tokens(void)
1328 krb5_error_code code = 0;
1329 krb5_context ctx = 0;
1332 struct principal_ccache_data * pcc_next = princ_cc_data;
1335 const char * realm = NULL;
1336 char local_cell[MAXCELLCHARS+1]="";
1337 struct afsconf_cell cellconfig;
1339 if (!pkrb5_init_context)
1342 if ( pcc_next == NULL ) // nothing to do
1345 if ( IsDebuggerPresent() ) {
1346 OutputDebugString("KFW_AFS_renew_expiring_tokens\n");
1349 code = pkrb5_init_context(&ctx);
1350 if (code) goto cleanup;
1352 code = pkrb5_timeofday(ctx, &now);
1353 if (code) goto cleanup;
1355 for ( ; pcc_next ; pcc_next = pcc_next->next ) {
1356 if ( pcc_next->expired )
1359 if ( now >= (pcc_next->expiration_time) ) {
1360 if ( !pcc_next->from_lsa ) {
1361 pcc_next->expired = 1;
1366 if ( pcc_next->renew && now >= (pcc_next->expiration_time - cminRENEW * csec1MINUTE) ) {
1367 code = pkrb5_cc_resolve(ctx, pcc_next->ccache_name, &cc);
1370 code = KFW_renew(ctx,cc);
1372 if ( code && pcc_next->from_lsa)
1374 #endif /* USE_MS2MIT */
1377 KFW_AFS_update_princ_ccache_data(ctx, cc, pcc_next->from_lsa);
1378 if (code) goto loop_cleanup;
1380 // Attempt to obtain new tokens for other cells supported by the same
1382 cell_count = KFW_AFS_find_cells_for_princ(ctx, pcc_next->principal, &cells, TRUE);
1383 if ( cell_count > 0 ) {
1384 while ( cell_count-- ) {
1385 if ( IsDebuggerPresent() ) {
1386 OutputDebugString("Cell: ");
1387 OutputDebugString(cells[cell_count]);
1388 OutputDebugString("\n");
1390 code = KFW_AFS_get_cellconfig( cells[cell_count], (void*)&cellconfig, local_cell);
1391 if ( code ) continue;
1392 realm = afs_realm_of_cell(ctx, &cellconfig); // do not free
1393 if ( IsDebuggerPresent() ) {
1394 OutputDebugString("Realm: ");
1395 OutputDebugString(realm);
1396 OutputDebugString("\n");
1398 code = KFW_AFS_klog(ctx, cc, "afs", cells[cell_count], (char *)realm, pLeash_get_default_lifetime(),NULL);
1399 if ( IsDebuggerPresent() ) {
1401 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
1402 OutputDebugString(message);
1404 free(cells[cell_count]);
1412 pkrb5_cc_close(ctx,cc);
1419 pkrb5_cc_close(ctx,cc);
1421 pkrb5_free_context(ctx);
1428 KFW_AFS_renew_token_for_cell(char * cell)
1430 krb5_error_code code = 0;
1431 krb5_context ctx = 0;
1433 char ** principals = NULL;
1435 if (!pkrb5_init_context)
1438 if ( IsDebuggerPresent() ) {
1439 OutputDebugString("KFW_AFS_renew_token_for_cell:");
1440 OutputDebugString(cell);
1441 OutputDebugString("\n");
1444 code = pkrb5_init_context(&ctx);
1445 if (code) goto cleanup;
1447 count = KFW_AFS_find_principals_for_cell(ctx, cell, &principals, TRUE);
1449 // We know we must have a credential somewhere since we are
1450 // trying to renew a token
1452 KFW_import_ccache_data();
1453 count = KFW_AFS_find_principals_for_cell(ctx, cell, &principals, TRUE);
1456 krb5_principal princ = 0;
1457 krb5_principal service = 0;
1459 krb5_creds mcreds, creds;
1460 #endif /* COMMENT */
1462 const char * realm = NULL;
1463 struct afsconf_cell cellconfig;
1464 char local_cell[MAXCELLCHARS+1];
1467 code = pkrb5_parse_name(ctx, principals[count], &princ);
1468 if (code) goto loop_cleanup;
1470 code = KFW_get_ccache(ctx, princ, &cc);
1471 if (code) goto loop_cleanup;
1473 code = KFW_AFS_get_cellconfig( cell, (void*)&cellconfig, local_cell);
1474 if ( code ) goto loop_cleanup;
1476 realm = afs_realm_of_cell(ctx, &cellconfig); // do not free
1477 if ( IsDebuggerPresent() ) {
1478 OutputDebugString("Realm: ");
1479 OutputDebugString(realm);
1480 OutputDebugString("\n");
1484 /* krb5_cc_remove_cred() is not implemented
1487 code = pkrb5_build_principal(ctx, &service, strlen(realm),
1488 realm, "afs", cell, NULL);
1490 memset(&mcreds, 0, sizeof(krb5_creds));
1491 mcreds.client = princ;
1492 mcreds.server = service;
1494 code = pkrb5_cc_retrieve_cred(ctx, cc, 0, &mcreds, &creds);
1496 if ( IsDebuggerPresent() ) {
1497 char * cname, *sname;
1498 pkrb5_unparse_name(ctx, creds.client, &cname);
1499 pkrb5_unparse_name(ctx, creds.server, &sname);
1500 OutputDebugString("Removing credential for client \"");
1501 OutputDebugString(cname);
1502 OutputDebugString("\" and service \"");
1503 OutputDebugString(sname);
1504 OutputDebugString("\"\n");
1505 pkrb5_free_unparsed_name(ctx,cname);
1506 pkrb5_free_unparsed_name(ctx,sname);
1509 code = pkrb5_cc_remove_cred(ctx, cc, 0, &creds);
1510 pkrb5_free_principal(ctx, creds.client);
1511 pkrb5_free_principal(ctx, creds.server);
1514 #endif /* COMMENT */
1516 code = KFW_AFS_klog(ctx, cc, "afs", cell, (char *)realm, pLeash_get_default_lifetime(),NULL);
1517 if ( IsDebuggerPresent() ) {
1519 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
1520 OutputDebugString(message);
1525 pkrb5_cc_close(ctx, cc);
1529 pkrb5_free_principal(ctx, princ);
1533 pkrb5_free_principal(ctx, service);
1537 KFW_AFS_update_cell_princ_map(ctx, cell, principals[count], code ? FALSE : TRUE);
1538 free(principals[count]);
1542 code = -1; // we did not renew the tokens
1545 pkrb5_free_context(ctx);
1546 return (code ? FALSE : TRUE);
1551 KFW_AFS_renew_tokens_for_all_cells(void)
1553 struct cell_principal_map * next = cell_princ_map;
1555 if ( IsDebuggerPresent() )
1556 OutputDebugString("KFW_AFS_renew_tokens_for_all()\n");
1561 for ( ; next ; next = next->next ) {
1563 KFW_AFS_renew_token_for_cell(next->cell);
1569 KFW_renew(krb5_context alt_ctx, krb5_ccache alt_cc)
1571 krb5_error_code code = 0;
1572 krb5_context ctx = 0;
1574 krb5_principal me = 0;
1575 krb5_principal server = 0;
1576 krb5_creds my_creds;
1577 krb5_data *realm = 0;
1579 if (!pkrb5_init_context)
1582 memset(&my_creds, 0, sizeof(krb5_creds));
1587 code = pkrb5_init_context(&ctx);
1588 if (code) goto cleanup;
1594 code = pkrb5_cc_default(ctx, &cc);
1595 if (code) goto cleanup;
1598 code = pkrb5_cc_get_principal(ctx, cc, &me);
1599 if (code) goto cleanup;
1601 realm = krb5_princ_realm(ctx, me);
1603 code = pkrb5_build_principal_ext(ctx, &server,
1604 realm->length,realm->data,
1605 KRB5_TGS_NAME_SIZE, KRB5_TGS_NAME,
1606 realm->length,realm->data,
1611 if ( IsDebuggerPresent() ) {
1612 char * cname, *sname;
1613 pkrb5_unparse_name(ctx, me, &cname);
1614 pkrb5_unparse_name(ctx, server, &sname);
1615 OutputDebugString("Renewing credential for client \"");
1616 OutputDebugString(cname);
1617 OutputDebugString("\" and service \"");
1618 OutputDebugString(sname);
1619 OutputDebugString("\"\n");
1620 pkrb5_free_unparsed_name(ctx,cname);
1621 pkrb5_free_unparsed_name(ctx,sname);
1624 my_creds.client = me;
1625 my_creds.server = server;
1627 code = pkrb5_get_renewed_creds(ctx, &my_creds, me, cc, NULL);
1629 if ( IsDebuggerPresent() ) {
1631 sprintf(message,"krb5_get_renewed_creds() failed: %d\n",code);
1632 OutputDebugString(message);
1637 code = pkrb5_cc_initialize(ctx, cc, me);
1639 if ( IsDebuggerPresent() ) {
1641 sprintf(message,"krb5_cc_initialize() failed: %d\n",code);
1642 OutputDebugString(message);
1647 code = pkrb5_cc_store_cred(ctx, cc, &my_creds);
1649 if ( IsDebuggerPresent() ) {
1651 sprintf(message,"krb5_cc_store_cred() failed: %d\n",code);
1652 OutputDebugString(message);
1658 if (my_creds.client == me)
1659 my_creds.client = 0;
1660 if (my_creds.server == server)
1661 my_creds.server = 0;
1662 pkrb5_free_cred_contents(ctx, &my_creds);
1664 pkrb5_free_principal(ctx, me);
1666 pkrb5_free_principal(ctx, server);
1667 if (cc && (cc != alt_cc))
1668 pkrb5_cc_close(ctx, cc);
1669 if (ctx && (ctx != alt_ctx))
1670 pkrb5_free_context(ctx);
1675 KFW_kinit( krb5_context alt_ctx,
1678 char *principal_name,
1680 krb5_deltat lifetime,
1683 krb5_deltat renew_life,
1688 krb5_error_code code = 0;
1689 krb5_context ctx = 0;
1691 krb5_principal me = 0;
1693 krb5_creds my_creds;
1694 krb5_get_init_creds_opt options;
1695 krb5_address ** addrs = NULL;
1696 int i = 0, addr_count = 0;
1698 if (!pkrb5_init_context)
1701 pkrb5_get_init_creds_opt_init(&options);
1702 memset(&my_creds, 0, sizeof(my_creds));
1710 code = pkrb5_init_context(&ctx);
1711 if (code) goto cleanup;
1717 code = pkrb5_cc_default(ctx, &cc);
1718 if (code) goto cleanup;
1721 code = pkrb5_parse_name(ctx, principal_name, &me);
1725 code = pkrb5_unparse_name(ctx, me, &name);
1730 lifetime = pLeash_get_default_lifetime();
1738 pkrb5_get_init_creds_opt_set_tkt_life(&options, lifetime);
1739 pkrb5_get_init_creds_opt_set_forwardable(&options,
1740 forwardable ? 1 : 0);
1741 pkrb5_get_init_creds_opt_set_proxiable(&options,
1743 pkrb5_get_init_creds_opt_set_renew_life(&options,
1746 pkrb5_get_init_creds_opt_set_address_list(&options,NULL);
1750 // we are going to add the public IP address specified by the user
1751 // to the list provided by the operating system
1752 krb5_address ** local_addrs=NULL;
1755 pkrb5_os_localaddr(ctx, &local_addrs);
1756 while ( local_addrs[i++] );
1759 addrs = (krb5_address **) malloc((addr_count+1) * sizeof(krb5_address *));
1761 pkrb5_free_addresses(ctx, local_addrs);
1764 memset(addrs, 0, sizeof(krb5_address *) * (addr_count+1));
1766 while ( local_addrs[i] ) {
1767 addrs[i] = (krb5_address *)malloc(sizeof(krb5_address));
1768 if (addrs[i] == NULL) {
1769 pkrb5_free_addresses(ctx, local_addrs);
1773 addrs[i]->magic = local_addrs[i]->magic;
1774 addrs[i]->addrtype = local_addrs[i]->addrtype;
1775 addrs[i]->length = local_addrs[i]->length;
1776 addrs[i]->contents = (unsigned char *)malloc(addrs[i]->length);
1777 if (!addrs[i]->contents) {
1778 pkrb5_free_addresses(ctx, local_addrs);
1782 memcpy(addrs[i]->contents,local_addrs[i]->contents,
1783 local_addrs[i]->length); /* safe */
1786 pkrb5_free_addresses(ctx, local_addrs);
1788 addrs[i] = (krb5_address *)malloc(sizeof(krb5_address));
1789 if (addrs[i] == NULL)
1792 addrs[i]->magic = KV5M_ADDRESS;
1793 addrs[i]->addrtype = AF_INET;
1794 addrs[i]->length = 4;
1795 addrs[i]->contents = (unsigned char *)malloc(addrs[i]->length);
1796 if (!addrs[i]->contents)
1799 netIPAddr = htonl(publicIP);
1800 memcpy(addrs[i]->contents,&netIPAddr,4);
1802 pkrb5_get_init_creds_opt_set_address_list(&options,addrs);
1807 code = pkrb5_get_init_creds_password(ctx,
1810 password, // password
1811 KRB5_prompter, // prompter
1812 hParent, // prompter data
1819 code = pkrb5_cc_initialize(ctx, cc, me);
1823 code = pkrb5_cc_store_cred(ctx, cc, &my_creds);
1829 for ( i=0;i<addr_count;i++ ) {
1831 if ( addrs[i]->contents )
1832 free(addrs[i]->contents);
1837 if (my_creds.client == me)
1838 my_creds.client = 0;
1839 pkrb5_free_cred_contents(ctx, &my_creds);
1841 pkrb5_free_unparsed_name(ctx, name);
1843 pkrb5_free_principal(ctx, me);
1844 if (cc && (cc != alt_cc))
1845 pkrb5_cc_close(ctx, cc);
1846 if (ctx && (ctx != alt_ctx))
1847 pkrb5_free_context(ctx);
1853 KFW_kdestroy(krb5_context alt_ctx, krb5_ccache alt_cc)
1857 krb5_error_code code;
1859 if (!pkrb5_init_context)
1868 code = pkrb5_init_context(&ctx);
1869 if (code) goto cleanup;
1875 code = pkrb5_cc_default(ctx, &cc);
1876 if (code) goto cleanup;
1879 code = pkrb5_cc_destroy(ctx, cc);
1880 if ( !code ) cc = 0;
1883 if (cc && (cc != alt_cc))
1884 pkrb5_cc_close(ctx, cc);
1885 if (ctx && (ctx != alt_ctx))
1886 pkrb5_free_context(ctx);
1894 GetSecurityLogonSessionData(PSECURITY_LOGON_SESSION_DATA * ppSessionData)
1896 NTSTATUS Status = 0;
1898 TOKEN_STATISTICS Stats;
1904 *ppSessionData = NULL;
1906 Success = OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &TokenHandle );
1910 Success = GetTokenInformation( TokenHandle, TokenStatistics, &Stats, sizeof(TOKEN_STATISTICS), &ReqLen );
1911 CloseHandle( TokenHandle );
1915 Status = pLsaGetLogonSessionData( &Stats.AuthenticationId, ppSessionData );
1916 if ( FAILED(Status) || !ppSessionData )
1923 // MSLSA_IsKerberosLogon() does not validate whether or not there are valid tickets in the
1924 // cache. It validates whether or not it is reasonable to assume that if we
1925 // attempted to retrieve valid tickets we could do so. Microsoft does not
1926 // automatically renew expired tickets. Therefore, the cache could contain
1927 // expired or invalid tickets. Microsoft also caches the user's password
1928 // and will use it to retrieve new TGTs if the cache is empty and tickets
1932 MSLSA_IsKerberosLogon(VOID)
1934 PSECURITY_LOGON_SESSION_DATA pSessionData = NULL;
1935 BOOL Success = FALSE;
1937 if ( GetSecurityLogonSessionData(&pSessionData) ) {
1938 if ( pSessionData->AuthenticationPackage.Buffer ) {
1944 usBuffer = (pSessionData->AuthenticationPackage).Buffer;
1945 usLength = (pSessionData->AuthenticationPackage).Length;
1948 lstrcpynW (buffer, usBuffer, usLength);
1949 lstrcatW (buffer,L"");
1950 if ( !lstrcmpW(L"Kerberos",buffer) )
1954 pLsaFreeReturnBuffer(pSessionData);
1958 #endif /* USE_MS2MIT */
1960 static BOOL CALLBACK
1961 MultiInputDialogProc( HWND hDialog, UINT message, WPARAM wParam, LPARAM lParam)
1965 switch ( message ) {
1967 if ( GetDlgCtrlID((HWND) wParam) != ID_MID_TEXT )
1969 SetFocus(GetDlgItem( hDialog, ID_MID_TEXT));
1972 for ( i=0; i < mid_cnt ; i++ ) {
1973 if (mid_tb[i].echo == 0)
1974 SendDlgItemMessage(hDialog, ID_MID_TEXT+i, EM_SETPASSWORDCHAR, 32, 0);
1975 else if (mid_tb[i].echo == 2)
1976 SendDlgItemMessage(hDialog, ID_MID_TEXT+i, EM_SETPASSWORDCHAR, '*', 0);
1981 switch ( LOWORD(wParam) ) {
1983 for ( i=0; i < mid_cnt ; i++ ) {
1984 if ( !GetDlgItemText(hDialog, ID_MID_TEXT+i, mid_tb[i].buf, mid_tb[i].len) )
1985 *mid_tb[i].buf = '\0';
1989 EndDialog(hDialog, LOWORD(wParam));
1997 lpwAlign( LPWORD lpIn )
2005 return (LPWORD) ul;;
2009 * dialog widths are measured in 1/4 character widths
2010 * dialog height are measured in 1/8 character heights
2014 MultiInputDialog( HINSTANCE hinst, HWND hwndOwner,
2015 char * ptext[], int numlines, int width,
2016 int tb_cnt, struct textField * tb)
2020 LPDLGITEMTEMPLATE lpdit;
2026 hgbl = GlobalAlloc(GMEM_ZEROINIT, 4096);
2033 lpdt = (LPDLGTEMPLATE)GlobalLock(hgbl);
2035 // Define a dialog box.
2037 lpdt->style = WS_POPUP | WS_BORDER | WS_SYSMENU
2038 | DS_MODALFRAME | WS_CAPTION | DS_CENTER
2039 | DS_SETFOREGROUND | DS_3DLOOK
2040 | DS_SETFONT | DS_FIXEDSYS | DS_NOFAILCREATE;
2041 lpdt->cdit = numlines + (2 * tb_cnt) + 2; // number of controls
2044 lpdt->cx = 20 + width * 4;
2045 lpdt->cy = 20 + (numlines + tb_cnt + 4) * 14;
2047 lpw = (LPWORD) (lpdt + 1);
2048 *lpw++ = 0; // no menu
2049 *lpw++ = 0; // predefined dialog box class (by default)
2051 lpwsz = (LPWSTR) lpw;
2052 nchar = MultiByteToWideChar (CP_ACP, 0, "", -1, lpwsz, 128);
2054 *lpw++ = 8; // font size (points)
2055 lpwsz = (LPWSTR) lpw;
2056 nchar = MultiByteToWideChar (CP_ACP, 0, "MS Shell Dlg",
2060 //-----------------------
2061 // Define an OK button.
2062 //-----------------------
2063 lpw = lpwAlign (lpw); // align DLGITEMTEMPLATE on DWORD boundary
2064 lpdit = (LPDLGITEMTEMPLATE) lpw;
2065 lpdit->style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP | WS_BORDER;
2066 lpdit->dwExtendedStyle = 0;
2067 lpdit->x = (lpdt->cx - 14)/4 - 20;
2068 lpdit->y = 10 + (numlines + tb_cnt + 2) * 14;
2071 lpdit->id = IDOK; // OK button identifier
2073 lpw = (LPWORD) (lpdit + 1);
2075 *lpw++ = 0x0080; // button class
2077 lpwsz = (LPWSTR) lpw;
2078 nchar = MultiByteToWideChar (CP_ACP, 0, "OK", -1, lpwsz, 50);
2080 *lpw++ = 0; // no creation data
2082 //-----------------------
2083 // Define an Cancel button.
2084 //-----------------------
2085 lpw = lpwAlign (lpw); // align DLGITEMTEMPLATE on DWORD boundary
2086 lpdit = (LPDLGITEMTEMPLATE) lpw;
2087 lpdit->style = WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP | WS_BORDER;
2088 lpdit->dwExtendedStyle = 0;
2089 lpdit->x = (lpdt->cx - 14)*3/4 - 20;
2090 lpdit->y = 10 + (numlines + tb_cnt + 2) * 14;
2093 lpdit->id = IDCANCEL; // CANCEL button identifier
2095 lpw = (LPWORD) (lpdit + 1);
2097 *lpw++ = 0x0080; // button class
2099 lpwsz = (LPWSTR) lpw;
2100 nchar = MultiByteToWideChar (CP_ACP, 0, "Cancel", -1, lpwsz, 50);
2102 *lpw++ = 0; // no creation data
2104 /* Add controls for preface data */
2105 for ( i=0; i<numlines; i++) {
2106 /*-----------------------
2107 * Define a static text control.
2108 *-----------------------*/
2109 lpw = lpwAlign (lpw); /* align DLGITEMTEMPLATE on DWORD boundary */
2110 lpdit = (LPDLGITEMTEMPLATE) lpw;
2111 lpdit->style = WS_CHILD | WS_VISIBLE | SS_LEFT;
2112 lpdit->dwExtendedStyle = 0;
2114 lpdit->y = 10 + i * 14;
2115 lpdit->cx = strlen(ptext[i]) * 4 + 10;
2117 lpdit->id = ID_TEXT + i; // text identifier
2119 lpw = (LPWORD) (lpdit + 1);
2121 *lpw++ = 0x0082; // static class
2123 lpwsz = (LPWSTR) lpw;
2124 nchar = MultiByteToWideChar (CP_ACP, 0, ptext[i],
2125 -1, lpwsz, 2*width);
2127 *lpw++ = 0; // no creation data
2130 for ( i=0, pwid = 0; i<tb_cnt; i++) {
2131 if ( pwid < strlen(tb[i].label) )
2132 pwid = strlen(tb[i].label);
2135 for ( i=0; i<tb_cnt; i++) {
2137 /*-----------------------
2138 * Define a static text control.
2139 *-----------------------*/
2140 lpw = lpwAlign (lpw); /* align DLGITEMTEMPLATE on DWORD boundary */
2141 lpdit = (LPDLGITEMTEMPLATE) lpw;
2142 lpdit->style = WS_CHILD | WS_VISIBLE | SS_LEFT;
2143 lpdit->dwExtendedStyle = 0;
2145 lpdit->y = 10 + (numlines + i + 1) * 14;
2146 lpdit->cx = pwid * 4;
2148 lpdit->id = ID_TEXT + numlines + i; // text identifier
2150 lpw = (LPWORD) (lpdit + 1);
2152 *lpw++ = 0x0082; // static class
2154 lpwsz = (LPWSTR) lpw;
2155 nchar = MultiByteToWideChar (CP_ACP, 0, tb[i].label ? tb[i].label : "",
2158 *lpw++ = 0; // no creation data
2160 /*-----------------------
2161 * Define an edit control.
2162 *-----------------------*/
2163 lpw = lpwAlign (lpw); /* align DLGITEMTEMPLATE on DWORD boundary */
2164 lpdit = (LPDLGITEMTEMPLATE) lpw;
2165 lpdit->style = WS_CHILD | WS_VISIBLE | ES_LEFT | WS_TABSTOP | WS_BORDER | (tb[i].echo == 1 ? 0L : ES_PASSWORD);
2166 lpdit->dwExtendedStyle = 0;
2167 lpdit->x = 10 + (pwid + 1) * 4;
2168 lpdit->y = 10 + (numlines + i + 1) * 14;
2169 lpdit->cx = (width - (pwid + 1)) * 4;
2171 lpdit->id = ID_MID_TEXT + i; // identifier
2173 lpw = (LPWORD) (lpdit + 1);
2175 *lpw++ = 0x0081; // edit class
2177 lpwsz = (LPWSTR) lpw;
2178 nchar = MultiByteToWideChar (CP_ACP, 0, tb[i].def ? tb[i].def : "",
2181 *lpw++ = 0; // no creation data
2185 ret = DialogBoxIndirect(hinst, (LPDLGTEMPLATE) hgbl,
2186 hwndOwner, (DLGPROC) MultiInputDialogProc);
2190 case 0: /* Timeout */
2198 sprintf(buf,"DialogBoxIndirect() failed: %d",GetLastError());
2199 MessageBox(hwndOwner,
2202 MB_OK | MB_ICONINFORMATION | MB_TASKMODAL);
2209 multi_field_dialog(HWND hParent, char * preface, int n, struct textField tb[])
2211 HINSTANCE hInst = 0;
2215 char * plines[16], *p = preface ? preface : "";
2218 for ( i=0; i<16; i++ )
2221 while (*p && numlines < 16) {
2222 plines[numlines++] = p;
2223 for ( ;*p && *p != '\r' && *p != '\n'; p++ );
2224 if ( *p == '\r' && *(p+1) == '\n' ) {
2227 } else if ( *p == '\n' ) {
2230 if ( strlen(plines[numlines-1]) > maxwidth )
2231 maxwidth = strlen(plines[numlines-1]);
2234 for ( i=0;i<n;i++ ) {
2235 len = strlen(tb[i].label) + 1 + (tb[i].len > 40 ? 40 : tb[i].len);
2236 if ( maxwidth < len )
2240 return(MultiInputDialog(hInst, hParent, plines, numlines, maxwidth, n, tb));
2243 static krb5_error_code KRB5_CALLCONV
2244 KRB5_prompter( krb5_context context,
2249 krb5_prompt prompts[])
2251 krb5_error_code errcode = 0;
2253 struct textField * tb = NULL;
2254 int len = 0, blen=0, nlen=0;
2255 HWND hParent = (HWND)data;
2258 nlen = strlen(name)+2;
2261 blen = strlen(banner)+2;
2263 tb = (struct textField *) malloc(sizeof(struct textField) * num_prompts);
2266 memset(tb,0,sizeof(struct textField) * num_prompts);
2267 for ( i=0; i < num_prompts; i++ ) {
2268 tb[i].buf = prompts[i].reply->data;
2269 tb[i].len = prompts[i].reply->length;
2270 tb[i].label = prompts[i].prompt;
2272 tb[i].echo = (prompts[i].hidden ? 2 : 1);
2275 ok = multi_field_dialog(hParent,(char *)banner,num_prompts,tb);
2277 for ( i=0; i < num_prompts; i++ )
2278 prompts[i].reply->length = strlen(prompts[i].reply->data);
2286 for (i = 0; i < num_prompts; i++) {
2287 memset(prompts[i].reply->data, 0, prompts[i].reply->length);
2294 KFW_AFS_wait_for_service_start(void)
2299 CurrentState = SERVICE_START_PENDING;
2300 memset(HostName, '\0', sizeof(HostName));
2301 gethostname(HostName, sizeof(HostName));
2303 while (CurrentState != SERVICE_RUNNING || CurrentState != SERVICE_STOPPED)
2305 if (GetServiceStatus(HostName, TRANSARCAFSDAEMON, &CurrentState) != NOERROR)
2307 if ( IsDebuggerPresent() ) {
2308 switch ( CurrentState ) {
2309 case SERVICE_STOPPED:
2310 OutputDebugString("SERVICE_STOPPED\n");
2312 case SERVICE_START_PENDING:
2313 OutputDebugString("SERVICE_START_PENDING\n");
2315 case SERVICE_STOP_PENDING:
2316 OutputDebugString("SERVICE_STOP_PENDING\n");
2318 case SERVICE_RUNNING:
2319 OutputDebugString("SERVICE_RUNNING\n");
2321 case SERVICE_CONTINUE_PENDING:
2322 OutputDebugString("SERVICE_CONTINUE_PENDING\n");
2324 case SERVICE_PAUSE_PENDING:
2325 OutputDebugString("SERVICE_PAUSE_PENDING\n");
2327 case SERVICE_PAUSED:
2328 OutputDebugString("SERVICE_PAUSED\n");
2331 OutputDebugString("UNKNOWN Service State\n");
2334 if (CurrentState == SERVICE_STOPPED)
2336 if (CurrentState == SERVICE_RUNNING)
2352 memset(HostName, '\0', sizeof(HostName));
2353 gethostname(HostName, sizeof(HostName));
2354 if (GetServiceStatus(HostName, TRANSARCAFSDAEMON, &CurrentState) != NOERROR)
2356 if (CurrentState != SERVICE_RUNNING)
2359 rc = ktc_ForgetAllTokens();
2366 krb5_context alt_ctx,
2378 struct ktc_principal aserver;
2379 struct ktc_principal aclient;
2380 char realm_of_user[REALM_SZ]; /* Kerberos realm of user */
2381 char realm_of_cell[REALM_SZ]; /* Kerberos realm of cell */
2382 char local_cell[MAXCELLCHARS+1];
2383 char Dmycell[MAXCELLCHARS+1];
2384 struct ktc_token atoken;
2385 struct ktc_token btoken;
2386 struct afsconf_cell ak_cellconfig; /* General information about the cell */
2387 char RealmName[128];
2389 char ServiceName[128];
2393 krb5_context ctx = 0;
2396 krb5_creds * k5creds = 0;
2397 krb5_error_code code;
2398 krb5_principal client_principal = 0;
2402 memset(HostName, '\0', sizeof(HostName));
2403 gethostname(HostName, sizeof(HostName));
2404 if (GetServiceStatus(HostName, TRANSARCAFSDAEMON, &CurrentState) != NOERROR) {
2405 if ( IsDebuggerPresent() )
2406 OutputDebugString("Unable to retrieve AFSD Service Status\n");
2409 if (CurrentState != SERVICE_RUNNING) {
2410 if ( IsDebuggerPresent() )
2411 OutputDebugString("AFSD Service NOT RUNNING\n");
2415 if (!pkrb5_init_context)
2418 memset(RealmName, '\0', sizeof(RealmName));
2419 memset(CellName, '\0', sizeof(CellName));
2420 memset(ServiceName, '\0', sizeof(ServiceName));
2421 memset(realm_of_user, '\0', sizeof(realm_of_user));
2422 memset(realm_of_cell, '\0', sizeof(realm_of_cell));
2423 if (cell && cell[0])
2424 strcpy(Dmycell, cell);
2426 memset(Dmycell, '\0', sizeof(Dmycell));
2428 // NULL or empty cell returns information on local cell
2429 if (rc = KFW_AFS_get_cellconfig(Dmycell, &ak_cellconfig, local_cell))
2431 // KFW_AFS_error(rc, "get_cellconfig()");
2438 code = pkrb5_init_context(&ctx);
2439 if (code) goto cleanup;
2445 code = pkrb5_cc_default(ctx, &cc);
2446 if (code) goto skip_krb5_init;
2449 memset((char *)&increds, 0, sizeof(increds));
2451 code = pkrb5_cc_get_principal(ctx, cc, &client_principal);
2453 if ( code == KRB5_CC_NOTFOUND && IsDebuggerPresent() )
2455 OutputDebugString("Principal Not Found for ccache\n");
2457 goto skip_krb5_init;
2459 i = krb5_princ_realm(ctx, client_principal)->length;
2462 strncpy(realm_of_user,krb5_princ_realm(ctx, client_principal)->data,i);
2463 realm_of_user[i] = 0;
2468 if ( !try_krb5 || !realm_of_user[0] ) {
2469 if ((rc = (*pkrb_get_tf_realm)((*ptkt_string)(), realm_of_user)) != KSUCCESS)
2477 strcpy(realm_of_cell, afs_realm_of_cell(ctx, &ak_cellconfig));
2479 if (strlen(service) == 0)
2480 strcpy(ServiceName, "afs");
2482 strcpy(ServiceName, service);
2484 if (strlen(cell) == 0)
2485 strcpy(CellName, local_cell);
2487 strcpy(CellName, cell);
2489 if (strlen(realm) == 0)
2490 strcpy(RealmName, realm_of_cell);
2492 strcpy(RealmName, realm);
2494 memset(&creds, '\0', sizeof(creds));
2499 /* First try service/cell@REALM */
2500 if (code = pkrb5_build_principal(ctx, &increds.server,
2510 increds.client = client_principal;
2511 increds.times.endtime = 0;
2512 /* Ask for DES since that is what V4 understands */
2513 increds.keyblock.enctype = ENCTYPE_DES_CBC_CRC;
2516 if ( IsDebuggerPresent() ) {
2517 char * cname, *sname;
2518 pkrb5_unparse_name(ctx, increds.client, &cname);
2519 pkrb5_unparse_name(ctx, increds.server, &sname);
2520 OutputDebugString("Getting tickets for \"");
2521 OutputDebugString(cname);
2522 OutputDebugString("\" and service \"");
2523 OutputDebugString(sname);
2524 OutputDebugString("\"\n");
2525 pkrb5_free_unparsed_name(ctx,cname);
2526 pkrb5_free_unparsed_name(ctx,sname);
2529 code = pkrb5_get_credentials(ctx, 0, cc, &increds, &k5creds);
2530 if (code == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN ||
2531 code == KRB5KRB_ERR_GENERIC /* heimdal */ ||
2532 code == KRB5KRB_AP_ERR_MSG_TYPE) {
2533 /* Or service@REALM */
2534 pkrb5_free_principal(ctx,increds.server);
2536 code = pkrb5_build_principal(ctx, &increds.server,
2542 if ( IsDebuggerPresent() ) {
2543 char * cname, *sname;
2544 pkrb5_unparse_name(ctx, increds.client, &cname);
2545 pkrb5_unparse_name(ctx, increds.server, &sname);
2546 OutputDebugString("krb5_get_credentials() returned Service Principal Unknown\n");
2547 OutputDebugString("Trying again: getting tickets for \"");
2548 OutputDebugString(cname);
2549 OutputDebugString("\" and service \"");
2550 OutputDebugString(sname);
2551 OutputDebugString("\"\n");
2552 pkrb5_free_unparsed_name(ctx,cname);
2553 pkrb5_free_unparsed_name(ctx,sname);
2557 code = pkrb5_get_credentials(ctx, 0, cc, &increds, &k5creds);
2560 if ((code == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN ||
2561 code == KRB5KRB_ERR_GENERIC /* heimdal */ ||
2562 code == KRB5KRB_AP_ERR_MSG_TYPE) &&
2563 strcmp(RealmName, realm_of_cell)) {
2564 /* Or service/cell@REALM_OF_CELL */
2565 strcpy(RealmName, realm_of_cell);
2566 pkrb5_free_principal(ctx,increds.server);
2568 code = pkrb5_build_principal(ctx, &increds.server,
2575 if ( IsDebuggerPresent() ) {
2576 char * cname, *sname;
2577 pkrb5_unparse_name(ctx, increds.client, &cname);
2578 pkrb5_unparse_name(ctx, increds.server, &sname);
2579 OutputDebugString("krb5_get_credentials() returned Service Principal Unknown\n");
2580 OutputDebugString("Trying again: getting tickets for \"");
2581 OutputDebugString(cname);
2582 OutputDebugString("\" and service \"");
2583 OutputDebugString(sname);
2584 OutputDebugString("\"\n");
2585 pkrb5_free_unparsed_name(ctx,cname);
2586 pkrb5_free_unparsed_name(ctx,sname);
2590 code = pkrb5_get_credentials(ctx, 0, cc, &increds, &k5creds);
2593 if (code == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN ||
2594 code == KRB5KRB_ERR_GENERIC /* heimdal */ ||
2595 code == KRB5KRB_AP_ERR_MSG_TYPE) {
2596 /* Or service@REALM_OF_CELL */
2597 pkrb5_free_principal(ctx,increds.server);
2599 code = pkrb5_build_principal(ctx, &increds.server,
2605 if ( IsDebuggerPresent() ) {
2606 char * cname, *sname;
2607 pkrb5_unparse_name(ctx, increds.client, &cname);
2608 pkrb5_unparse_name(ctx, increds.server, &sname);
2609 OutputDebugString("krb5_get_credentials() returned Service Principal Unknown\n");
2610 OutputDebugString("Trying again: getting tickets for \"");
2611 OutputDebugString(cname);
2612 OutputDebugString("\" and service \"");
2613 OutputDebugString(sname);
2614 OutputDebugString("\"\n");
2615 pkrb5_free_unparsed_name(ctx,cname);
2616 pkrb5_free_unparsed_name(ctx,sname);
2620 code = pkrb5_get_credentials(ctx, 0, cc, &increds, &k5creds);
2625 if ( IsDebuggerPresent() ) {
2627 sprintf(message,"krb5_get_credentials returns: %d\n",code);
2628 OutputDebugString(message);
2634 /* This code inserts the entire K5 ticket into the token
2635 * No need to perform a krb524 translation which is
2636 * commented out in the code below
2638 if (k5creds->ticket.length > MAXKTCTICKETLEN)
2641 memset(&aserver, '\0', sizeof(aserver));
2642 strncpy(aserver.name, ServiceName, MAXKTCNAMELEN - 1);
2643 strncpy(aserver.cell, CellName, MAXKTCREALMLEN - 1);
2645 memset(&atoken, '\0', sizeof(atoken));
2646 atoken.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
2647 atoken.startTime = k5creds->times.starttime;
2648 atoken.endTime = k5creds->times.endtime;
2649 memcpy(&atoken.sessionKey, k5creds->keyblock.contents, k5creds->keyblock.length);
2650 atoken.ticketLen = k5creds->ticket.length;
2651 memcpy(atoken.ticket, k5creds->ticket.data, atoken.ticketLen);
2654 rc = ktc_GetToken(&aserver, &btoken, sizeof(btoken), &aclient);
2655 if (rc != 0 && rc != KTC_NOENT && rc != KTC_NOCELL) {
2656 if ( rc == KTC_NOCM && retry < 20 ) {
2659 goto retry_gettoken5;
2664 if (atoken.kvno == btoken.kvno &&
2665 atoken.ticketLen == btoken.ticketLen &&
2666 !memcmp(&atoken.sessionKey, &btoken.sessionKey, sizeof(atoken.sessionKey)) &&
2667 !memcmp(atoken.ticket, btoken.ticket, atoken.ticketLen))
2669 /* Success - Nothing to do */
2673 // * Reset the "aclient" structure before we call ktc_SetToken.
2674 // * This structure was first set by the ktc_GetToken call when
2675 // * we were comparing whether identical tokens already existed.
2677 len = min(k5creds->client->data[0].length,MAXKTCNAMELEN - 1);
2678 strncpy(aclient.name, k5creds->client->data[0].data, len);
2679 aclient.name[len] = '\0';
2681 if ( k5creds->client->length > 1 ) {
2683 strcat(aclient.name, ".");
2684 p = aclient.name + strlen(aclient.name);
2685 len = min(k5creds->client->data[1].length,MAXKTCNAMELEN - strlen(aclient.name) - 1);
2686 strncpy(p, k5creds->client->data[1].data, len);
2689 aclient.instance[0] = '\0';
2691 strcpy(aclient.cell, realm_of_cell);
2693 len = min(k5creds->client->realm.length,strlen(realm_of_cell));
2694 if ( strncmp(realm_of_cell, k5creds->client->realm.data, len) ) {
2696 strcat(aclient.name, "@");
2697 p = aclient.name + strlen(aclient.name);
2698 len = min(k5creds->client->realm.length,MAXKTCNAMELEN - strlen(aclient.name) - 1);
2699 strncpy(p, k5creds->client->realm.data, len);
2704 strncpy(aclient.smbname, smbname, MAXRANDOMNAMELEN);
2705 aclient.smbname[MAXRANDOMNAMELEN-1] = '\0';
2707 aclient.smbname[0] = '\0';
2710 rc = ktc_SetToken(&aserver, &atoken, &aclient, 0);
2712 goto cleanup; /* We have successfully inserted the token */
2715 /* Otherwise, the ticket could have been too large so try to
2716 * convert using the krb524d running with the KDC
2718 code = pkrb524_convert_creds_kdc(ctx, k5creds, &creds);
2719 pkrb5_free_creds(ctx, k5creds);
2721 if ( IsDebuggerPresent() ) {
2723 sprintf(message,"krb524_convert_creds_kdc returns: %d\n",code);
2724 OutputDebugString(message);
2732 code = (*pkrb_get_cred)(ServiceName, CellName, RealmName, &creds);
2733 if (code == NO_TKT_FIL) {
2734 // if the problem is that we have no krb4 tickets
2735 // do not attempt to continue
2738 if (code != KSUCCESS)
2739 code = (*pkrb_get_cred)(ServiceName, "", RealmName, &creds);
2741 if (code != KSUCCESS)
2743 if ((code = (*pkrb_mk_req)(&ticket, ServiceName, CellName, RealmName, 0)) == KSUCCESS)
2745 if ((code = (*pkrb_get_cred)(ServiceName, CellName, RealmName, &creds)) != KSUCCESS)
2750 else if ((code = (*pkrb_mk_req)(&ticket, ServiceName, "", RealmName, 0)) == KSUCCESS)
2752 if ((code = (*pkrb_get_cred)(ServiceName, "", RealmName, &creds)) != KSUCCESS)
2767 memset(&aserver, '\0', sizeof(aserver));
2768 strncpy(aserver.name, ServiceName, MAXKTCNAMELEN - 1);
2769 strncpy(aserver.cell, CellName, MAXKTCREALMLEN - 1);
2771 memset(&atoken, '\0', sizeof(atoken));
2772 atoken.kvno = creds.kvno;
2773 atoken.startTime = creds.issue_date;
2774 atoken.endTime = creds.issue_date + life_to_time(0,creds.lifetime);
2775 memcpy(&atoken.sessionKey, creds.session, 8);
2776 atoken.ticketLen = creds.ticket_st.length;
2777 memcpy(atoken.ticket, creds.ticket_st.dat, atoken.ticketLen);
2780 rc = ktc_GetToken(&aserver, &btoken, sizeof(btoken), &aclient);
2781 if (rc != 0 && rc != KTC_NOENT && rc != KTC_NOCELL) {
2782 if ( rc == KTC_NOCM && retry < 20 ) {
2785 goto retry_gettoken;
2787 KFW_AFS_error(rc, "ktc_GetToken()");
2792 if (atoken.kvno == btoken.kvno &&
2793 atoken.ticketLen == btoken.ticketLen &&
2794 !memcmp(&atoken.sessionKey, &btoken.sessionKey, sizeof(atoken.sessionKey)) &&
2795 !memcmp(atoken.ticket, btoken.ticket, atoken.ticketLen))
2800 // * Reset the "aclient" structure before we call ktc_SetToken.
2801 // * This structure was first set by the ktc_GetToken call when
2802 // * we were comparing whether identical tokens already existed.
2804 strncpy(aclient.name, creds.pname, MAXKTCNAMELEN - 1);
2807 strncat(aclient.name, ".", MAXKTCNAMELEN - 1);
2808 strncat(aclient.name, creds.pinst, MAXKTCNAMELEN - 1);
2810 strcpy(aclient.instance, "");
2812 if ( strcmp(realm_of_cell, creds.realm) )
2814 strncat(aclient.name, "@", MAXKTCNAMELEN - 1);
2815 strncpy(aclient.name, creds.realm, MAXKTCREALMLEN - 1);
2817 aclient.name[MAXKTCREALMLEN-1] = '\0';
2819 strcpy(aclient.cell, CellName);
2822 strncpy(aclient.smbname, smbname, MAXRANDOMNAMELEN);
2823 aclient.smbname[MAXRANDOMNAMELEN-1] = '\0';
2825 aclient.smbname[0] = '\0';
2828 if (rc = ktc_SetToken(&aserver, &atoken, &aclient, 0))
2830 KFW_AFS_error(rc, "ktc_SetToken()");
2836 if (client_principal)
2837 pkrb5_free_principal(ctx,client_principal);
2838 /* increds.client == client_principal */
2840 pkrb5_free_principal(ctx,increds.server);
2841 if (cc && (cc != alt_cc))
2842 pkrb5_cc_close(ctx, cc);
2843 if (ctx && (ctx != alt_ctx))
2844 pkrb5_free_context(ctx);
2846 return(rc? rc : code);
2849 /**************************************/
2850 /* afs_realm_of_cell(): */
2851 /**************************************/
2853 afs_realm_of_cell(krb5_context ctx, struct afsconf_cell *cellconfig)
2855 static char krbrlm[REALM_SZ+1]="";
2856 char ** realmlist=NULL;
2862 r = pkrb5_get_host_realm(ctx, cellconfig->hostName[0], &realmlist);
2863 if ( !r && realmlist && realmlist[0] ) {
2864 strcpy(krbrlm, realmlist[0]);
2865 pkrb5_free_host_realm(ctx, realmlist);
2871 char *t = cellconfig->name;
2876 if (islower(c)) c=toupper(c);
2884 /**************************************/
2885 /* KFW_AFS_get_cellconfig(): */
2886 /**************************************/
2888 KFW_AFS_get_cellconfig(char *cell, struct afsconf_cell *cellconfig, char *local_cell)
2891 char newcell[MAXCELLCHARS+1];
2893 local_cell[0] = (char)0;
2894 memset(cellconfig, 0, sizeof(*cellconfig));
2896 /* WIN32: cm_GetRootCellName(local_cell) - NOTE: no way to get max chars */
2897 if (rc = cm_GetRootCellName(local_cell))
2902 if (strlen(cell) == 0)
2903 strcpy(cell, local_cell);
2905 /* WIN32: cm_SearchCellFile(cell, pcallback, pdata) */
2906 strcpy(cellconfig->name, cell);
2908 rc = cm_SearchCellFile(cell, newcell, get_cellconfig_callback, (void*)cellconfig);
2909 #ifdef AFS_AFSDB_ENV
2912 rc = cm_SearchCellByDNS(cell, newcell, &ttl, get_cellconfig_callback, (void*)cellconfig);
2918 /**************************************/
2919 /* get_cellconfig_callback(): */
2920 /**************************************/
2922 get_cellconfig_callback(void *cellconfig, struct sockaddr_in *addrp, char *namep)
2924 struct afsconf_cell *cc = (struct afsconf_cell *)cellconfig;
2926 cc->hostAddr[cc->numServers] = *addrp;
2927 strcpy(cc->hostName[cc->numServers], namep);
2933 /**************************************/
2934 /* KFW_AFS_error(): */
2935 /**************************************/
2937 KFW_AFS_error(LONG rc, LPCSTR FailedFunctionName)
2940 const char *errText;
2942 // Using AFS defines as error messages for now, until Transarc
2943 // gets back to me with "string" translations of each of these
2945 if (rc == KTC_ERROR)
2946 errText = "KTC_ERROR";
2947 else if (rc == KTC_TOOBIG)
2948 errText = "KTC_TOOBIG";
2949 else if (rc == KTC_INVAL)
2950 errText = "KTC_INVAL";
2951 else if (rc == KTC_NOENT)
2952 errText = "KTC_NOENT";
2953 else if (rc == KTC_PIOCTLFAIL)
2954 errText = "KTC_PIOCTLFAIL";
2955 else if (rc == KTC_NOPIOCTL)
2956 errText = "KTC_NOPIOCTL";
2957 else if (rc == KTC_NOCELL)
2958 errText = "KTC_NOCELL";
2959 else if (rc == KTC_NOCM)
2960 errText = "KTC_NOCM: The service, Transarc AFS Daemon, most likely is not started!";
2962 errText = "Unknown error!";
2964 sprintf(message, "%s (0x%x)\n(%s failed)", errText, rc, FailedFunctionName);
2966 if ( IsDebuggerPresent() ) {
2967 OutputDebugString(message);
2968 OutputDebugString("\n");
2970 MessageBox(NULL, message, "AFS", MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND);
2976 LPSTR lpszMachineName,
2977 LPSTR lpszServiceName,
2978 DWORD *lpdwCurrentState)
2981 SC_HANDLE schSCManager = NULL;
2982 SC_HANDLE schService = NULL;
2983 DWORD fdwDesiredAccess = 0;
2984 SERVICE_STATUS ssServiceStatus = {0};
2987 *lpdwCurrentState = 0;
2989 fdwDesiredAccess = GENERIC_READ;
2991 schSCManager = OpenSCManager(lpszMachineName,
2995 if(schSCManager == NULL)
2997 hr = GetLastError();
3001 schService = OpenService(schSCManager,
3005 if(schService == NULL)
3007 hr = GetLastError();
3011 fRet = QueryServiceStatus(schService,
3016 hr = GetLastError();
3020 *lpdwCurrentState = ssServiceStatus.dwCurrentState;
3024 CloseServiceHandle(schService);
3025 CloseServiceHandle(schSCManager);
3038 for (n = 0; fi[n].func_ptr_var; n++)
3039 *(fi[n].func_ptr_var) = 0;
3040 if (h) FreeLibrary(h);
3045 const char* dll_name,
3047 HINSTANCE* ph, // [out, optional] - DLL handle
3048 int* pindex, // [out, optional] - index of last func loaded (-1 if none)
3049 int cleanup, // cleanup function pointers and unload on error
3050 int go_on, // continue loading even if some functions cannot be loaded
3051 int silent // do not pop-up a system dialog if DLL cannot be loaded
3060 if (pindex) *pindex = -1;
3062 for (n = 0; fi[n].func_ptr_var; n++)
3063 *(fi[n].func_ptr_var) = 0;
3066 em = SetErrorMode(SEM_FAILCRITICALERRORS);
3067 h = LoadLibrary(dll_name);
3075 for (i = 0; (go_on || !error) && (i < n); i++)
3077 void* p = (void*)GetProcAddress(h, fi[i].func_name);
3083 *(fi[i].func_ptr_var) = p;
3086 if (pindex) *pindex = last_i;
3087 if (error && cleanup && !go_on) {
3088 for (i = 0; i < n; i++) {
3089 *(fi[i].func_ptr_var) = 0;
3095 if (error) return 0;
3099 BOOL KFW_probe_kdc(struct afsconf_cell * cellconfig)
3101 krb5_context ctx = 0;
3103 krb5_error_code code;
3105 const char * realm = 0;
3106 krb5_principal principal = 0;
3108 char password[PROBE_PASSWORD_LEN+1];
3109 BOOL serverReachable = 0;
3111 if (!pkrb5_init_context)
3114 code = pkrb5_init_context(&ctx);
3115 if (code) goto cleanup;
3118 realm = afs_realm_of_cell(ctx, cellconfig); // do not free
3120 code = pkrb5_build_principal(ctx, &principal, strlen(realm),
3121 realm, PROBE_USERNAME, NULL, NULL);
3122 if ( code ) goto cleanup;
3124 code = KFW_get_ccache(ctx, principal, &cc);
3125 if ( code ) goto cleanup;
3127 code = pkrb5_unparse_name(ctx, principal, &pname);
3128 if ( code ) goto cleanup;
3130 pwdata.data = password;
3131 pwdata.length = PROBE_PASSWORD_LEN;
3132 code = pkrb5_c_random_make_octets(ctx, &pwdata);
3135 for ( i=0 ; i<PROBE_PASSWORD_LEN ; i++ )
3138 password[PROBE_PASSWORD_LEN] = '\0';
3140 code = KFW_kinit(NULL, NULL, HWND_DESKTOP,
3150 case KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN:
3151 case KRB5KDC_ERR_CLIENT_REVOKED:
3152 case KRB5KDC_ERR_CLIENT_NOTYET:
3153 case KRB5KDC_ERR_PREAUTH_FAILED:
3154 case KRB5KDC_ERR_PREAUTH_REQUIRED:
3155 case KRB5KDC_ERR_PADATA_TYPE_NOSUPP:
3156 serverReachable = TRUE;
3159 serverReachable = FALSE;
3164 pkrb5_free_unparsed_name(ctx,pname);
3166 pkrb5_free_principal(ctx,principal);
3168 pkrb5_cc_close(ctx,cc);
3170 pkrb5_free_context(ctx);
3172 return serverReachable;