2 * Copyright (c) 2004, 2005, 2006, 2007, 2008 Secure Endpoints Inc.
3 * Copyright (c) 2003 SkyRope, LLC
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Skyrope, LLC nor the names of its contributors may be
15 * used to endorse or promote products derived from this software without
16 * specific prior written permission from Skyrope, LLC.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Portions of this code are derived from portions of the MIT
31 * Leash Ticket Manager and LoadFuncs utilities. For these portions the
32 * following copyright applies.
34 * Copyright (c) 2003,2004 by the Massachusetts Institute of Technology.
35 * All rights reserved.
37 * Export of this software from the United States of America may
38 * require a specific license from the United States Government.
39 * It is the responsibility of any person or organization contemplating
40 * export to obtain such a license before exporting.
42 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
43 * distribute this software and its documentation for any purpose and
44 * without fee is hereby granted, provided that the above copyright
45 * notice appear in all copies and that both that copyright notice and
46 * this permission notice appear in supporting documentation, and that
47 * the name of M.I.T. not be used in advertising or publicity pertaining
48 * to distribution of the software without specific, written prior
49 * permission. Furthermore if you modify this software you must label
50 * your software as modified software and not distribute it in such a
51 * fashion that it might be confused with the original M.I.T. software.
52 * M.I.T. makes no representations about the suitability of
53 * this software for any purpose. It is provided "as is" without express
54 * or implied warranty.
66 #include "afskfw-int.h"
74 #include <afs/ptserver.h>
75 #include <afs/ptuser.h>
78 #include <WINNT\afsreg.h>
81 * TIMING _____________________________________________________________________
85 #define cminREMIND_TEST 1 // test every minute for expired creds
86 #define cminREMIND_WARN 15 // warn if creds expire in 15 minutes
87 #define cminRENEW 20 // renew creds when there are 20 minutes remaining
88 #define cminMINLIFE 30 // minimum life of Kerberos creds
90 #define c100ns1SECOND (LONGLONG)10000000
91 #define cmsec1SECOND 1000
92 #define cmsec1MINUTE 60000
93 #define csec1MINUTE 60
95 /* Function Pointer Declarations for Delayed Loading */
97 DECL_FUNC_PTR(cc_initialize);
98 DECL_FUNC_PTR(cc_shutdown);
99 DECL_FUNC_PTR(cc_get_NC_info);
100 DECL_FUNC_PTR(cc_free_NC_info);
104 DECL_FUNC_PTR(Leash_get_default_lifetime);
105 DECL_FUNC_PTR(Leash_get_default_forwardable);
106 DECL_FUNC_PTR(Leash_get_default_renew_till);
107 DECL_FUNC_PTR(Leash_get_default_noaddresses);
108 DECL_FUNC_PTR(Leash_get_default_proxiable);
109 DECL_FUNC_PTR(Leash_get_default_publicip);
110 DECL_FUNC_PTR(Leash_get_default_use_krb4);
111 DECL_FUNC_PTR(Leash_get_default_life_min);
112 DECL_FUNC_PTR(Leash_get_default_life_max);
113 DECL_FUNC_PTR(Leash_get_default_renew_min);
114 DECL_FUNC_PTR(Leash_get_default_renew_max);
115 DECL_FUNC_PTR(Leash_get_default_renewable);
116 DECL_FUNC_PTR(Leash_get_default_mslsa_import);
120 DECL_FUNC_PTR(krb5_change_password);
121 DECL_FUNC_PTR(krb5_get_init_creds_opt_init);
122 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_tkt_life);
123 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_renew_life);
124 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_forwardable);
125 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_proxiable);
126 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_address_list);
127 DECL_FUNC_PTR(krb5_get_init_creds_password);
128 DECL_FUNC_PTR(krb5_build_principal_ext);
129 DECL_FUNC_PTR(krb5_cc_get_name);
130 DECL_FUNC_PTR(krb5_cc_resolve);
131 DECL_FUNC_PTR(krb5_cc_default);
132 DECL_FUNC_PTR(krb5_cc_default_name);
133 DECL_FUNC_PTR(krb5_cc_set_default_name);
134 DECL_FUNC_PTR(krb5_cc_initialize);
135 DECL_FUNC_PTR(krb5_cc_destroy);
136 DECL_FUNC_PTR(krb5_cc_close);
137 DECL_FUNC_PTR(krb5_cc_store_cred);
138 DECL_FUNC_PTR(krb5_cc_copy_creds);
139 DECL_FUNC_PTR(krb5_cc_retrieve_cred);
140 DECL_FUNC_PTR(krb5_cc_get_principal);
141 DECL_FUNC_PTR(krb5_cc_start_seq_get);
142 DECL_FUNC_PTR(krb5_cc_next_cred);
143 DECL_FUNC_PTR(krb5_cc_end_seq_get);
144 DECL_FUNC_PTR(krb5_cc_remove_cred);
145 DECL_FUNC_PTR(krb5_cc_set_flags);
146 DECL_FUNC_PTR(krb5_cc_get_type);
147 DECL_FUNC_PTR(krb5_free_context);
148 DECL_FUNC_PTR(krb5_free_cred_contents);
149 DECL_FUNC_PTR(krb5_free_principal);
150 DECL_FUNC_PTR(krb5_get_in_tkt_with_password);
151 DECL_FUNC_PTR(krb5_init_context);
152 DECL_FUNC_PTR(krb5_parse_name);
153 DECL_FUNC_PTR(krb5_timeofday);
154 DECL_FUNC_PTR(krb5_timestamp_to_sfstring);
155 DECL_FUNC_PTR(krb5_unparse_name);
156 DECL_FUNC_PTR(krb5_get_credentials);
157 DECL_FUNC_PTR(krb5_mk_req);
158 DECL_FUNC_PTR(krb5_sname_to_principal);
159 DECL_FUNC_PTR(krb5_get_credentials_renew);
160 DECL_FUNC_PTR(krb5_free_data);
161 DECL_FUNC_PTR(krb5_free_data_contents);
162 DECL_FUNC_PTR(krb5_free_unparsed_name);
163 DECL_FUNC_PTR(krb5_os_localaddr);
164 DECL_FUNC_PTR(krb5_copy_keyblock_contents);
165 DECL_FUNC_PTR(krb5_copy_data);
166 DECL_FUNC_PTR(krb5_free_creds);
167 DECL_FUNC_PTR(krb5_build_principal);
168 DECL_FUNC_PTR(krb5_get_renewed_creds);
169 DECL_FUNC_PTR(krb5_get_default_config_files);
170 DECL_FUNC_PTR(krb5_free_config_files);
171 DECL_FUNC_PTR(krb5_get_default_realm);
172 DECL_FUNC_PTR(krb5_free_default_realm);
173 DECL_FUNC_PTR(krb5_free_ticket);
174 DECL_FUNC_PTR(krb5_decode_ticket);
175 DECL_FUNC_PTR(krb5_get_host_realm);
176 DECL_FUNC_PTR(krb5_free_host_realm);
177 DECL_FUNC_PTR(krb5_free_addresses);
178 DECL_FUNC_PTR(krb5_c_random_make_octets);
182 DECL_FUNC_PTR(krb524_init_ets);
183 DECL_FUNC_PTR(krb524_convert_creds_kdc);
188 DECL_FUNC_PTR(krb_get_cred);
189 DECL_FUNC_PTR(tkt_string);
190 DECL_FUNC_PTR(krb_get_tf_realm);
191 DECL_FUNC_PTR(krb_mk_req);
195 DECL_FUNC_PTR(com_err);
196 DECL_FUNC_PTR(error_message);
199 DECL_FUNC_PTR(profile_init);
200 DECL_FUNC_PTR(profile_release);
201 DECL_FUNC_PTR(profile_get_subsection_names);
202 DECL_FUNC_PTR(profile_free_list);
203 DECL_FUNC_PTR(profile_get_string);
204 DECL_FUNC_PTR(profile_release_string);
207 DECL_FUNC_PTR(OpenSCManagerA);
208 DECL_FUNC_PTR(OpenServiceA);
209 DECL_FUNC_PTR(QueryServiceStatus);
210 DECL_FUNC_PTR(CloseServiceHandle);
212 DECL_FUNC_PTR(LsaNtStatusToWinError);
213 #endif /* USE_MS2MIT */
217 DECL_FUNC_PTR(LsaConnectUntrusted);
218 DECL_FUNC_PTR(LsaLookupAuthenticationPackage);
219 DECL_FUNC_PTR(LsaCallAuthenticationPackage);
220 DECL_FUNC_PTR(LsaFreeReturnBuffer);
221 DECL_FUNC_PTR(LsaGetLogonSessionData);
222 #endif /* USE_MS2MIT */
225 FUNC_INFO ccapi_fi[] = {
226 MAKE_FUNC_INFO(cc_initialize),
227 MAKE_FUNC_INFO(cc_shutdown),
228 MAKE_FUNC_INFO(cc_get_NC_info),
229 MAKE_FUNC_INFO(cc_free_NC_info),
234 FUNC_INFO leash_fi[] = {
235 MAKE_FUNC_INFO(Leash_get_default_lifetime),
236 MAKE_FUNC_INFO(Leash_get_default_renew_till),
237 MAKE_FUNC_INFO(Leash_get_default_forwardable),
238 MAKE_FUNC_INFO(Leash_get_default_noaddresses),
239 MAKE_FUNC_INFO(Leash_get_default_proxiable),
240 MAKE_FUNC_INFO(Leash_get_default_publicip),
241 MAKE_FUNC_INFO(Leash_get_default_use_krb4),
242 MAKE_FUNC_INFO(Leash_get_default_life_min),
243 MAKE_FUNC_INFO(Leash_get_default_life_max),
244 MAKE_FUNC_INFO(Leash_get_default_renew_min),
245 MAKE_FUNC_INFO(Leash_get_default_renew_max),
246 MAKE_FUNC_INFO(Leash_get_default_renewable),
250 FUNC_INFO leash_opt_fi[] = {
251 MAKE_FUNC_INFO(Leash_get_default_mslsa_import),
256 FUNC_INFO k5_fi[] = {
257 MAKE_FUNC_INFO(krb5_change_password),
258 MAKE_FUNC_INFO(krb5_get_init_creds_opt_init),
259 MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_tkt_life),
260 MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_renew_life),
261 MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_forwardable),
262 MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_proxiable),
263 MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_address_list),
264 MAKE_FUNC_INFO(krb5_get_init_creds_password),
265 MAKE_FUNC_INFO(krb5_build_principal_ext),
266 MAKE_FUNC_INFO(krb5_cc_get_name),
267 MAKE_FUNC_INFO(krb5_cc_resolve),
268 MAKE_FUNC_INFO(krb5_cc_default),
269 MAKE_FUNC_INFO(krb5_cc_default_name),
270 MAKE_FUNC_INFO(krb5_cc_set_default_name),
271 MAKE_FUNC_INFO(krb5_cc_initialize),
272 MAKE_FUNC_INFO(krb5_cc_destroy),
273 MAKE_FUNC_INFO(krb5_cc_close),
274 MAKE_FUNC_INFO(krb5_cc_copy_creds),
275 MAKE_FUNC_INFO(krb5_cc_store_cred),
276 MAKE_FUNC_INFO(krb5_cc_retrieve_cred),
277 MAKE_FUNC_INFO(krb5_cc_get_principal),
278 MAKE_FUNC_INFO(krb5_cc_start_seq_get),
279 MAKE_FUNC_INFO(krb5_cc_next_cred),
280 MAKE_FUNC_INFO(krb5_cc_end_seq_get),
281 MAKE_FUNC_INFO(krb5_cc_remove_cred),
282 MAKE_FUNC_INFO(krb5_cc_set_flags),
283 MAKE_FUNC_INFO(krb5_cc_get_type),
284 MAKE_FUNC_INFO(krb5_free_context),
285 MAKE_FUNC_INFO(krb5_free_cred_contents),
286 MAKE_FUNC_INFO(krb5_free_principal),
287 MAKE_FUNC_INFO(krb5_get_in_tkt_with_password),
288 MAKE_FUNC_INFO(krb5_init_context),
289 MAKE_FUNC_INFO(krb5_parse_name),
290 MAKE_FUNC_INFO(krb5_timeofday),
291 MAKE_FUNC_INFO(krb5_timestamp_to_sfstring),
292 MAKE_FUNC_INFO(krb5_unparse_name),
293 MAKE_FUNC_INFO(krb5_get_credentials),
294 MAKE_FUNC_INFO(krb5_mk_req),
295 MAKE_FUNC_INFO(krb5_sname_to_principal),
296 MAKE_FUNC_INFO(krb5_get_credentials_renew),
297 MAKE_FUNC_INFO(krb5_free_data),
298 MAKE_FUNC_INFO(krb5_free_data_contents),
299 MAKE_FUNC_INFO(krb5_free_unparsed_name),
300 MAKE_FUNC_INFO(krb5_os_localaddr),
301 MAKE_FUNC_INFO(krb5_copy_keyblock_contents),
302 MAKE_FUNC_INFO(krb5_copy_data),
303 MAKE_FUNC_INFO(krb5_free_creds),
304 MAKE_FUNC_INFO(krb5_build_principal),
305 MAKE_FUNC_INFO(krb5_get_renewed_creds),
306 MAKE_FUNC_INFO(krb5_free_addresses),
307 MAKE_FUNC_INFO(krb5_get_default_config_files),
308 MAKE_FUNC_INFO(krb5_free_config_files),
309 MAKE_FUNC_INFO(krb5_get_default_realm),
310 MAKE_FUNC_INFO(krb5_free_default_realm),
311 MAKE_FUNC_INFO(krb5_free_ticket),
312 MAKE_FUNC_INFO(krb5_decode_ticket),
313 MAKE_FUNC_INFO(krb5_get_host_realm),
314 MAKE_FUNC_INFO(krb5_free_host_realm),
315 MAKE_FUNC_INFO(krb5_free_addresses),
316 MAKE_FUNC_INFO(krb5_c_random_make_octets),
321 FUNC_INFO k4_fi[] = {
322 MAKE_FUNC_INFO(krb_get_cred),
323 MAKE_FUNC_INFO(krb_get_tf_realm),
324 MAKE_FUNC_INFO(krb_mk_req),
325 MAKE_FUNC_INFO(tkt_string),
331 FUNC_INFO k524_fi[] = {
332 MAKE_FUNC_INFO(krb524_init_ets),
333 MAKE_FUNC_INFO(krb524_convert_creds_kdc),
338 FUNC_INFO profile_fi[] = {
339 MAKE_FUNC_INFO(profile_init),
340 MAKE_FUNC_INFO(profile_release),
341 MAKE_FUNC_INFO(profile_get_subsection_names),
342 MAKE_FUNC_INFO(profile_free_list),
343 MAKE_FUNC_INFO(profile_get_string),
344 MAKE_FUNC_INFO(profile_release_string),
348 FUNC_INFO ce_fi[] = {
349 MAKE_FUNC_INFO(com_err),
350 MAKE_FUNC_INFO(error_message),
354 FUNC_INFO service_fi[] = {
355 MAKE_FUNC_INFO(OpenSCManagerA),
356 MAKE_FUNC_INFO(OpenServiceA),
357 MAKE_FUNC_INFO(QueryServiceStatus),
358 MAKE_FUNC_INFO(CloseServiceHandle),
360 MAKE_FUNC_INFO(LsaNtStatusToWinError),
361 #endif /* USE_MS2MIT */
366 FUNC_INFO lsa_fi[] = {
367 MAKE_FUNC_INFO(LsaConnectUntrusted),
368 MAKE_FUNC_INFO(LsaLookupAuthenticationPackage),
369 MAKE_FUNC_INFO(LsaCallAuthenticationPackage),
370 MAKE_FUNC_INFO(LsaFreeReturnBuffer),
371 MAKE_FUNC_INFO(LsaGetLogonSessionData),
374 #endif /* USE_MS2MIT */
376 /* Static Prototypes */
377 char *afs_realm_of_cell(krb5_context, struct afsconf_cell *);
378 static long get_cellconfig_callback(void *, struct sockaddr_in *, char *);
379 int KFW_AFS_get_cellconfig(char *, struct afsconf_cell *, char *);
380 static krb5_error_code KRB5_CALLCONV KRB5_prompter( krb5_context context,
381 void *data, const char *name, const char *banner, int num_prompts,
382 krb5_prompt prompts[]);
385 /* Static Declarations */
386 static int inited = 0;
387 static int mid_cnt = 0;
388 static struct textField * mid_tb = NULL;
389 static HINSTANCE hKrb5 = 0;
391 static HINSTANCE hKrb4 = 0;
392 #endif /* USE_KRB4 */
394 static HINSTANCE hKrb524 = 0;
397 static HINSTANCE hSecur32 = 0;
398 #endif /* USE_MS2MIT */
399 static HINSTANCE hAdvApi32 = 0;
400 static HINSTANCE hComErr = 0;
401 static HINSTANCE hService = 0;
402 static HINSTANCE hProfile = 0;
404 static HINSTANCE hLeash = 0;
405 static HINSTANCE hLeashOpt = 0;
407 static HINSTANCE hCCAPI = 0;
408 static struct principal_ccache_data * princ_cc_data = NULL;
409 static struct cell_principal_map * cell_princ_map = NULL;
414 static int inited = 0;
417 char mutexName[MAX_PATH];
418 HANDLE hMutex = NULL;
420 sprintf(mutexName, "AFS KFW Init pid=%d", getpid());
422 hMutex = CreateMutex( NULL, TRUE, mutexName );
423 if ( GetLastError() == ERROR_ALREADY_EXISTS ) {
424 if ( WaitForSingleObject( hMutex, INFINITE ) != WAIT_OBJECT_0 ) {
430 LoadFuncs(KRB5_DLL, k5_fi, &hKrb5, 0, 1, 0, 0);
431 LoadFuncs(COMERR_DLL, ce_fi, &hComErr, 0, 0, 1, 0);
432 LoadFuncs(PROFILE_DLL, profile_fi, &hProfile, 0, 1, 0, 0);
434 LoadFuncs(KRB4_DLL, k4_fi, &hKrb4, 0, 1, 0, 0);
435 #endif /* USE_KRB4 */
436 LoadFuncs(SERVICE_DLL, service_fi, &hService, 0, 1, 0, 0);
438 LoadFuncs(SECUR32_DLL, lsa_fi, &hSecur32, 0, 1, 1, 1);
439 #endif /* USE_MS2MIT */
441 LoadFuncs(KRB524_DLL, k524_fi, &hKrb524, 0, 1, 1, 1);
444 LoadFuncs(LEASH_DLL, leash_fi, &hLeash, 0, 1, 0, 0);
445 LoadFuncs(LEASH_DLL, leash_opt_fi, &hLeashOpt, 0, 1, 0, 0);
447 LoadFuncs(CCAPI_DLL, ccapi_fi, &hCCAPI, 0, 1, 0, 0);
449 if ( KFW_is_available() ) {
450 char rootcell[CELL_MAXNAMELEN+1];
452 KFW_import_windows_lsa();
453 #endif /* USE_MS2MIT */
454 KFW_import_ccache_data();
455 KFW_AFS_renew_expiring_tokens();
457 /* WIN32 NOTE: no way to get max chars */
458 if (!cm_GetRootCellName(rootcell))
459 KFW_AFS_renew_token_for_cell(rootcell);
462 ReleaseMutex(hMutex);
472 FreeLibrary(hLeashOpt);
478 FreeLibrary(hKrb524);
484 FreeLibrary(hSecur32);
485 #endif /* USE_MS2MIT */
487 FreeLibrary(hService);
489 FreeLibrary(hComErr);
491 FreeLibrary(hProfile);
495 #endif /* USE_KRB4 */
500 typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
503 static int init = TRUE;
504 static int bIsWow64 = FALSE;
508 LPFN_ISWOW64PROCESS fnIsWow64Process = NULL;
510 hModule = GetModuleHandle(TEXT("kernel32"));
512 fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(hModule, "IsWow64Process");
514 if (NULL != fnIsWow64Process)
516 if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
518 // on error, assume FALSE.
519 // in other words, do nothing.
522 FreeLibrary(hModule);
530 KFW_accept_dotted_usernames(void)
536 code = RegOpenKeyEx(HKEY_CURRENT_USER, AFSREG_USER_OPENAFS_SUBKEY,
537 0, (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey);
538 if (code == ERROR_SUCCESS) {
540 code = RegQueryValueEx(parmKey, "AcceptDottedPrincipalNames", NULL, NULL,
541 (BYTE *) &value, &len);
542 RegCloseKey(parmKey);
544 if (code != ERROR_SUCCESS) {
545 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_OPENAFS_SUBKEY,
546 0, (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey);
547 if (code == ERROR_SUCCESS) {
549 code = RegQueryValueEx(parmKey, "AcceptDottedPrincipalNames", NULL, NULL,
550 (BYTE *) &value, &len);
551 RegCloseKey (parmKey);
565 code = RegOpenKeyEx(HKEY_CURRENT_USER, AFSREG_USER_OPENAFS_SUBKEY,
566 0, (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey);
567 if (code == ERROR_SUCCESS) {
568 len = sizeof(use524);
569 code = RegQueryValueEx(parmKey, "Use524", NULL, NULL,
570 (BYTE *) &use524, &len);
571 RegCloseKey(parmKey);
573 if (code != ERROR_SUCCESS) {
574 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_OPENAFS_SUBKEY,
575 0, (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey);
576 if (code == ERROR_SUCCESS) {
577 len = sizeof(use524);
578 code = RegQueryValueEx(parmKey, "Use524", NULL, NULL,
579 (BYTE *) &use524, &len);
580 RegCloseKey (parmKey);
587 KFW_is_available(void)
593 code = RegOpenKeyEx(HKEY_CURRENT_USER, AFSREG_USER_OPENAFS_SUBKEY,
594 0, (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey);
595 if (code == ERROR_SUCCESS) {
596 len = sizeof(enableKFW);
597 code = RegQueryValueEx(parmKey, "EnableKFW", NULL, NULL,
598 (BYTE *) &enableKFW, &len);
599 RegCloseKey (parmKey);
602 if (code != ERROR_SUCCESS) {
603 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_OPENAFS_SUBKEY,
604 0, (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey);
605 if (code == ERROR_SUCCESS) {
606 len = sizeof(enableKFW);
607 code = RegQueryValueEx(parmKey, "EnableKFW", NULL, NULL,
608 (BYTE *) &enableKFW, &len);
609 RegCloseKey (parmKey);
617 if ( hKrb5 && hComErr && hService &&
620 #endif /* USE_MS2MIT */
633 KRB5_error(krb5_error_code rc, LPCSTR FailedFunctionName,
634 int FreeContextFlag, krb5_context * ctx,
639 int krb5Error = ((int)(rc & 255));
651 errText = perror_message(rc);
652 _snprintf(message, sizeof(message),
653 "%s\n(Kerberos error %ld)\n\n%s failed",
658 if ( IsDebuggerPresent() )
659 OutputDebugString(message);
661 MessageBox(NULL, message, "Kerberos Five", MB_OK | MB_ICONERROR |
664 if (FreeContextFlag == 1)
666 if (ctx && *ctx != NULL)
668 if (cache && *cache != NULL) {
669 pkrb5_cc_close(*ctx, *cache);
673 pkrb5_free_context(*ctx);
682 KFW_AFS_update_princ_ccache_data(krb5_context ctx, krb5_ccache cc, int lsa)
684 struct principal_ccache_data * next = princ_cc_data;
685 krb5_principal principal = 0;
687 const char * ccname = NULL;
688 const char * cctype = NULL;
689 char * ccfullname = NULL;
690 krb5_error_code code = 0;
691 krb5_error_code cc_code = 0;
697 if (ctx == 0 || cc == 0)
700 code = pkrb5_cc_get_principal(ctx, cc, &principal);
703 code = pkrb5_unparse_name(ctx, principal, &pname);
704 if ( code ) goto cleanup;
706 ccname = pkrb5_cc_get_name(ctx, cc);
707 if (!ccname) goto cleanup;
709 cctype = pkrb5_cc_get_type(ctx, cc);
710 if (!cctype) goto cleanup;
712 ccfullname = malloc(strlen(ccname) + strlen(cctype) + 2);
713 if (!ccfullname) goto cleanup;
715 sprintf(ccfullname, "%s:%s", cctype, ccname);
717 // Search the existing list to see if we have a match
719 for ( ; next ; next = next->next ) {
720 if ( !strcmp(next->principal,pname) && !strcmp(next->ccache_name, ccfullname) )
725 // If not, match add a new node to the beginning of the list and assign init it
727 next = (struct principal_ccache_data *) malloc(sizeof(struct principal_ccache_data));
728 next->next = princ_cc_data;
729 princ_cc_data = next;
730 next->principal = _strdup(pname);
731 next->ccache_name = ccfullname;
733 next->from_lsa = lsa;
735 next->expiration_time = 0;
739 flags = 0; // turn off OPENCLOSE mode
740 code = pkrb5_cc_set_flags(ctx, cc, flags);
741 if ( code ) goto cleanup;
743 code = pkrb5_timeofday(ctx, &now);
745 cc_code = pkrb5_cc_start_seq_get(ctx, cc, &cur);
747 while (!(cc_code = pkrb5_cc_next_cred(ctx, cc, &cur, &creds))) {
748 if ( creds.ticket_flags & TKT_FLG_INITIAL ) {
750 // we found the ticket we are looking for
751 // check validity of timestamp
752 // We add a 5 minutes fudge factor to compensate for potential
753 // clock skew errors between the KDC and client OS
755 valid = ((creds.times.starttime > 0) &&
756 now >= (creds.times.starttime - 300) &&
757 now < (creds.times.endtime + 300) &&
758 !(creds.ticket_flags & TKT_FLG_INVALID));
760 if ( next->from_lsa) {
762 next->expiration_time = creds.times.endtime;
764 } else if ( valid ) {
766 next->expiration_time = creds.times.endtime;
767 next->renew = (creds.times.renew_till > creds.times.endtime) &&
768 (creds.ticket_flags & TKT_FLG_RENEWABLE);
771 next->expiration_time = 0;
775 pkrb5_free_cred_contents(ctx, &creds);
776 cc_code = KRB5_CC_END;
779 pkrb5_free_cred_contents(ctx, &creds);
782 if (cc_code == KRB5_CC_END) {
783 code = pkrb5_cc_end_seq_get(ctx, cc, &cur);
784 if (code) goto cleanup;
788 flags = KRB5_TC_OPENCLOSE; //turn on OPENCLOSE
789 code = pkrb5_cc_set_flags(ctx, cc, flags);
794 pkrb5_free_unparsed_name(ctx,pname);
796 pkrb5_free_principal(ctx,principal);
800 KFW_AFS_find_ccache_for_principal(krb5_context ctx, char * principal, char **ccache, int valid_only)
802 struct principal_ccache_data * next = princ_cc_data;
803 char * response = NULL;
805 if ( !principal || !ccache )
809 if ( (!valid_only || !next->expired) && !strcmp(next->principal,principal) ) {
811 // we always want to prefer the MS Kerberos LSA cache or
812 // the cache afscreds created specifically for the principal
813 // if the current entry is either one, drop the previous find
814 if ( next->from_lsa || !strcmp(next->ccache_name,principal) )
817 response = _strdup(next->ccache_name);
818 // MS Kerberos LSA is our best option so use it and quit
819 if ( next->from_lsa )
833 KFW_AFS_delete_princ_ccache_data(krb5_context ctx, char * pname, char * ccname)
835 struct principal_ccache_data ** next = &princ_cc_data;
837 if ( !pname && !ccname )
841 if ( !strcmp((*next)->principal,pname) ||
842 !strcmp((*next)->ccache_name,ccname) ) {
844 free((*next)->principal);
845 free((*next)->ccache_name);
847 (*next) = (*next)->next;
854 KFW_AFS_update_cell_princ_map(krb5_context ctx, char * cell, char *pname, int active)
856 struct cell_principal_map * next = cell_princ_map;
858 // Search the existing list to see if we have a match
860 for ( ; next ; next = next->next ) {
861 if ( !strcmp(next->cell, cell) ) {
862 if ( !strcmp(next->principal,pname) ) {
863 next->active = active;
866 // OpenAFS currently has a restriction of one active token per cell
867 // Therefore, whenever we update the table with a new active cell we
868 // must mark all of the other principal to cell entries as inactive.
876 // If not, match add a new node to the beginning of the list and assign init it
878 next = (struct cell_principal_map *) malloc(sizeof(struct cell_principal_map));
879 next->next = cell_princ_map;
880 cell_princ_map = next;
881 next->principal = _strdup(pname);
882 next->cell = _strdup(cell);
883 next->active = active;
888 KFW_AFS_delete_cell_princ_maps(krb5_context ctx, char * pname, char * cell)
890 struct cell_principal_map ** next = &cell_princ_map;
892 if ( !pname && !cell )
896 if ( !strcmp((*next)->principal,pname) ||
897 !strcmp((*next)->cell,cell) ) {
899 free((*next)->principal);
902 (*next) = (*next)->next;
908 // Returns (if possible) a principal which has been known in
909 // the past to have been used to obtain tokens for the specified
911 // TODO: Attempt to return one which has not yet expired by checking
912 // the principal/ccache data
914 KFW_AFS_find_principals_for_cell(krb5_context ctx, char * cell, char **principals[], int active_only)
916 struct cell_principal_map * next_map = cell_princ_map;
917 const char * princ = NULL;
924 if ( (!active_only || next_map->active) && !strcmp(next_map->cell,cell) ) {
927 next_map = next_map->next;
930 if ( !principals || !count )
933 *principals = (char **) malloc(sizeof(char *) * count);
934 for ( next_map = cell_princ_map, i=0 ; next_map && i<count; next_map = next_map->next )
936 if ( (!active_only || next_map->active) && !strcmp(next_map->cell,cell) ) {
937 (*principals)[i++] = _strdup(next_map->principal);
944 KFW_AFS_find_cells_for_princ(krb5_context ctx, char * pname, char **cells[], int active_only)
947 struct cell_principal_map * next_map = cell_princ_map;
948 const char * princ = NULL;
954 if ( (!active_only || next_map->active) && !strcmp(next_map->principal,pname) ) {
957 next_map = next_map->next;
963 *cells = (char **) malloc(sizeof(char *) * count);
964 for ( next_map = cell_princ_map, i=0 ; next_map && i<count; next_map = next_map->next )
966 if ( (!active_only || next_map->active) && !strcmp(next_map->principal,pname) ) {
967 (*cells)[i++] = _strdup(next_map->cell);
973 /* Given a principal return an existing ccache or create one and return */
975 KFW_get_ccache(krb5_context alt_ctx, krb5_principal principal, krb5_ccache * cc)
977 krb5_context ctx = NULL;
979 char * ccname = NULL;
980 krb5_error_code code;
982 if (!pkrb5_init_context)
988 code = pkrb5_init_context(&ctx);
989 if (code) goto cleanup;
993 code = pkrb5_unparse_name(ctx, principal, &pname);
994 if (code) goto cleanup;
996 if ( !KFW_AFS_find_ccache_for_principal(ctx,pname,&ccname,TRUE) &&
997 !KFW_AFS_find_ccache_for_principal(ctx,pname,&ccname,FALSE)) {
998 ccname = (char *)malloc(strlen(pname) + 5);
999 sprintf(ccname,"API:%s",pname);
1001 code = pkrb5_cc_resolve(ctx, ccname, cc);
1003 code = pkrb5_cc_default(ctx, cc);
1004 if (code) goto cleanup;
1011 pkrb5_free_unparsed_name(ctx,pname);
1012 if (ctx && (ctx != alt_ctx))
1013 pkrb5_free_context(ctx);
1018 // Import Microsoft Credentials into a new MIT ccache
1020 KFW_import_windows_lsa(void)
1022 krb5_context ctx = NULL;
1023 krb5_ccache cc = NULL;
1024 krb5_principal princ = NULL;
1025 char * pname = NULL;
1026 krb5_data * princ_realm;
1027 krb5_error_code code;
1028 char cell[128]="", realm[128]="", *def_realm = 0;
1030 DWORD dwMsLsaImport;
1032 if (!pkrb5_init_context)
1035 code = pkrb5_init_context(&ctx);
1036 if (code) goto cleanup;
1038 code = pkrb5_cc_resolve(ctx, LSA_CCNAME, &cc);
1039 if (code) goto cleanup;
1041 KFW_AFS_update_princ_ccache_data(ctx, cc, TRUE);
1043 code = pkrb5_cc_get_principal(ctx, cc, &princ);
1044 if ( code ) goto cleanup;
1046 dwMsLsaImport = pLeash_get_default_mslsa_import ? pLeash_get_default_mslsa_import() : 1;
1047 switch ( dwMsLsaImport ) {
1048 case 0: /* do not import */
1050 case 1: /* always import */
1052 case 2: { /* matching realm */
1053 char ms_realm[128] = "", *r;
1056 for ( r=ms_realm, i=0; i<krb5_princ_realm(ctx, princ)->length; r++, i++ ) {
1057 *r = krb5_princ_realm(ctx, princ)->data[i];
1061 if (code = pkrb5_get_default_realm(ctx, &def_realm))
1064 if (strcmp(def_realm, ms_realm))
1072 code = pkrb5_unparse_name(ctx,princ,&pname);
1073 if ( code ) goto cleanup;
1075 princ_realm = krb5_princ_realm(ctx, princ);
1076 for ( i=0; i<princ_realm->length; i++ ) {
1077 realm[i] = princ_realm->data[i];
1078 cell[i] = tolower(princ_realm->data[i]);
1083 code = KFW_AFS_klog(ctx, cc, "afs", cell, realm, pLeash_get_default_lifetime(),NULL);
1084 if ( IsDebuggerPresent() ) {
1086 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
1087 OutputDebugString(message);
1089 if ( code ) goto cleanup;
1091 KFW_AFS_update_cell_princ_map(ctx, cell, pname, TRUE);
1095 pkrb5_free_unparsed_name(ctx,pname);
1097 pkrb5_free_principal(ctx,princ);
1099 pkrb5_free_default_realm(ctx, def_realm);
1101 pkrb5_cc_close(ctx,cc);
1103 pkrb5_free_context(ctx);
1105 #endif /* USE_MS2MIT */
1107 // If there are existing MIT credentials, copy them to a new
1108 // ccache named after the principal
1110 // Enumerate all existing MIT ccaches and construct entries
1111 // in the principal_ccache table
1113 // Enumerate all existing AFS Tokens and construct entries
1114 // in the cell_principal table
1116 KFW_import_ccache_data(void)
1118 krb5_context ctx = NULL;
1119 krb5_ccache cc = NULL;
1120 krb5_principal principal = NULL;
1122 krb5_error_code code;
1123 krb5_error_code cc_code;
1125 apiCB * cc_ctx = NULL;
1126 struct _infoNC ** pNCi = NULL;
1129 if ( !pcc_initialize )
1132 if ( IsDebuggerPresent() )
1133 OutputDebugString("KFW_import_ccache_data()\n");
1135 code = pcc_initialize(&cc_ctx, CC_API_VER_2, NULL, NULL);
1136 if (code) goto cleanup;
1138 code = pcc_get_NC_info(cc_ctx, &pNCi);
1139 if (code) goto cleanup;
1141 code = pkrb5_init_context(&ctx);
1142 if (code) goto cleanup;
1144 for ( i=0; pNCi[i]; i++ ) {
1145 if ( pNCi[i]->vers != CC_CRED_V5 )
1147 if ( IsDebuggerPresent() ) {
1148 OutputDebugString("Principal: ");
1149 OutputDebugString(pNCi[i]->principal);
1150 OutputDebugString(" in ccache ");
1151 OutputDebugString(pNCi[i]->name);
1152 OutputDebugString("\n");
1154 if ( strcmp(pNCi[i]->name,pNCi[i]->principal)
1155 && strcmp(pNCi[i]->name,LSA_CCNAME)
1158 for ( j=0; pNCi[j]; j++ ) {
1159 if (!strcmp(pNCi[j]->name,pNCi[i]->principal)) {
1165 code = pkrb5_cc_resolve(ctx, pNCi[i]->principal, &cc);
1166 if (code) goto loop_cleanup;
1169 krb5_ccache oldcc = 0;
1171 if ( IsDebuggerPresent() )
1172 OutputDebugString("copying ccache data to new ccache\n");
1174 code = pkrb5_parse_name(ctx, pNCi[i]->principal, &principal);
1175 if (code) goto loop_cleanup;
1176 code = pkrb5_cc_initialize(ctx, cc, principal);
1177 if (code) goto loop_cleanup;
1179 code = pkrb5_cc_resolve(ctx, pNCi[i]->name, &oldcc);
1180 if (code) goto loop_cleanup;
1181 code = pkrb5_cc_copy_creds(ctx,oldcc,cc);
1183 code = pkrb5_cc_close(ctx,cc);
1185 code = pkrb5_cc_close(ctx,oldcc);
1187 KRB5_error(code, "krb5_cc_copy_creds", 0, NULL, NULL);
1190 code = pkrb5_cc_close(ctx,oldcc);
1193 code = pkrb5_cc_resolve(ctx, pNCi[i]->name, &cc);
1194 if (code) goto loop_cleanup;
1197 flags = 0; // turn off OPENCLOSE mode
1198 code = pkrb5_cc_set_flags(ctx, cc, flags);
1199 if ( code ) goto cleanup;
1201 KFW_AFS_update_princ_ccache_data(ctx, cc, !strcmp(pNCi[i]->name,LSA_CCNAME));
1203 cc_code = pkrb5_cc_start_seq_get(ctx, cc, &cur);
1205 while (!(cc_code = pkrb5_cc_next_cred(ctx, cc, &cur, &creds))) {
1206 krb5_data * sname = krb5_princ_name(ctx, creds.server);
1207 krb5_data * cell = krb5_princ_component(ctx, creds.server, 1);
1208 krb5_data * realm = krb5_princ_realm(ctx, creds.server);
1209 if ( sname && cell && !strcmp("afs",sname->data) ) {
1210 struct ktc_principal aserver;
1211 struct ktc_principal aclient;
1212 struct ktc_token atoken;
1215 if ( IsDebuggerPresent() ) {
1216 OutputDebugString("Found AFS ticket: ");
1217 OutputDebugString(sname->data);
1219 OutputDebugString("/");
1220 OutputDebugString(cell->data);
1222 OutputDebugString("@");
1223 OutputDebugString(realm->data);
1224 OutputDebugString("\n");
1227 memset(&aserver, '\0', sizeof(aserver));
1228 strcpy(aserver.name, sname->data);
1229 strcpy(aserver.cell, cell->data);
1231 code = ktc_GetToken(&aserver, &atoken, sizeof(atoken), &aclient);
1233 // Found a token in AFS Client Server which matches
1234 char pname[128], *p, *q;
1235 for ( p=pname, q=aclient.name; *q; p++, q++)
1237 for ( *p++ = '@', q=aclient.cell; *q; p++, q++)
1241 if ( IsDebuggerPresent() ) {
1242 OutputDebugString("Found AFS token: ");
1243 OutputDebugString(pname);
1244 OutputDebugString("\n");
1247 if ( strcmp(pname,pNCi[i]->principal) )
1249 KFW_AFS_update_cell_princ_map(ctx, cell->data, pNCi[i]->principal, active);
1251 // Attempt to import it
1252 KFW_AFS_update_cell_princ_map(ctx, cell->data, pNCi[i]->principal, active);
1254 if ( IsDebuggerPresent() ) {
1255 OutputDebugString("Calling KFW_AFS_klog() to obtain token\n");
1258 code = KFW_AFS_klog(ctx, cc, "afs", cell->data, realm->data,
1262 pLeash_get_default_lifetime(),
1263 #endif /* USE_LEASH */
1265 if ( IsDebuggerPresent() ) {
1267 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
1268 OutputDebugString(message);
1271 } else if ( IsDebuggerPresent() ) {
1272 OutputDebugString("Found ticket: ");
1273 OutputDebugString(sname->data);
1274 if ( cell && cell->data ) {
1275 OutputDebugString("/");
1276 OutputDebugString(cell->data);
1278 OutputDebugString("@");
1279 OutputDebugString(realm->data);
1280 OutputDebugString("\n");
1282 pkrb5_free_cred_contents(ctx, &creds);
1285 if (cc_code == KRB5_CC_END) {
1286 cc_code = pkrb5_cc_end_seq_get(ctx, cc, &cur);
1287 if (cc_code) goto loop_cleanup;
1291 flags = KRB5_TC_OPENCLOSE; //turn on OPENCLOSE
1292 code = pkrb5_cc_set_flags(ctx, cc, flags);
1294 pkrb5_cc_close(ctx,cc);
1298 pkrb5_free_principal(ctx,principal);
1305 pkrb5_free_context(ctx);
1307 pcc_free_NC_info(cc_ctx, &pNCi);
1309 pcc_shutdown(&cc_ctx);
1314 KFW_AFS_get_cred( char * username,
1321 krb5_context ctx = NULL;
1322 krb5_ccache cc = NULL;
1323 char * realm = NULL, * userrealm = NULL;
1324 krb5_principal principal = NULL;
1325 char * pname = NULL;
1326 krb5_error_code code;
1327 char local_cell[CELL_MAXNAMELEN+1];
1328 char **cells = NULL;
1330 struct afsconf_cell cellconfig;
1333 if (!pkrb5_init_context)
1336 if ( IsDebuggerPresent() ) {
1337 OutputDebugString("KFW_AFS_get_cred for token ");
1338 OutputDebugString(username);
1339 OutputDebugString(" in cell ");
1340 OutputDebugString(cell);
1341 OutputDebugString("\n");
1344 memset(&cellconfig, 0, sizeof(cellconfig));
1346 code = pkrb5_init_context(&ctx);
1347 if ( code ) goto cleanup;
1349 code = KFW_AFS_get_cellconfig( cell, (void*)&cellconfig, local_cell);
1350 if ( code ) goto cleanup;
1352 realm = afs_realm_of_cell(ctx, &cellconfig); // do not free
1354 userrealm = strchr(username,'@');
1356 pname = strdup(username);
1357 if (!KFW_accept_dotted_usernames()) {
1358 userrealm = strchr(pname, '@');
1361 /* handle kerberos iv notation */
1362 while ( dot = strchr(pname,'.') ) {
1368 pname = malloc(strlen(username) + strlen(realm) + 2);
1370 strcpy(pname, username);
1372 if (!KFW_accept_dotted_usernames()) {
1373 /* handle kerberos iv notation */
1374 while ( dot = strchr(pname,'.') ) {
1379 strcat(pname,realm);
1381 if ( IsDebuggerPresent() ) {
1382 OutputDebugString("Realm: ");
1383 OutputDebugString(realm);
1384 OutputDebugString("\n");
1387 code = pkrb5_parse_name(ctx, pname, &principal);
1388 if ( code ) goto cleanup;
1390 code = KFW_get_ccache(ctx, principal, &cc);
1391 if ( code ) goto cleanup;
1393 if ( lifetime == 0 )
1397 lifetime = pLeash_get_default_lifetime();
1400 if ( password && password[0] ) {
1401 code = KFW_kinit( ctx, cc, HWND_DESKTOP,
1406 1, /* forwardable */
1407 0, /* not proxiable */
1409 1, /* noaddresses */
1410 0 /* no public ip */
1412 pLeash_get_default_forwardable(),
1413 pLeash_get_default_proxiable(),
1414 pLeash_get_default_renewable() ? pLeash_get_default_renew_till() : 0,
1415 pLeash_get_default_noaddresses(),
1416 pLeash_get_default_publicip()
1417 #endif /* USE_LEASH */
1420 if ( IsDebuggerPresent() ) {
1422 sprintf(message,"KFW_kinit() returns: %d\n",code);
1423 OutputDebugString(message);
1425 if ( code ) goto cleanup;
1427 KFW_AFS_update_princ_ccache_data(ctx, cc, FALSE);
1430 code = KFW_AFS_klog(ctx, cc, "afs", cell, realm, lifetime, smbname);
1431 if ( IsDebuggerPresent() ) {
1433 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
1434 OutputDebugString(message);
1436 if ( code ) goto cleanup;
1438 KFW_AFS_update_cell_princ_map(ctx, cell, pname, TRUE);
1440 // Attempt to obtain new tokens for other cells supported by the same
1442 cell_count = KFW_AFS_find_cells_for_princ(ctx, pname, &cells, TRUE);
1443 if ( cell_count > 1 ) {
1444 while ( cell_count-- ) {
1445 if ( strcmp(cells[cell_count],cell) ) {
1446 if ( IsDebuggerPresent() ) {
1448 sprintf(message,"found another cell for the same principal: %s\n",cell);
1449 OutputDebugString(message);
1452 if (cellconfig.linkedCell) {
1453 free(cellconfig.linkedCell);
1454 cellconfig.linkedCell = NULL;
1456 code = KFW_AFS_get_cellconfig( cells[cell_count], (void*)&cellconfig, local_cell);
1457 if ( code ) continue;
1459 realm = afs_realm_of_cell(ctx, &cellconfig); // do not free
1460 if ( IsDebuggerPresent() ) {
1461 OutputDebugString("Realm: ");
1462 OutputDebugString(realm);
1463 OutputDebugString("\n");
1466 code = KFW_AFS_klog(ctx, cc, "afs", cells[cell_count], realm, lifetime, smbname);
1467 if ( IsDebuggerPresent() ) {
1469 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
1470 OutputDebugString(message);
1473 free(cells[cell_count]);
1476 } else if ( cell_count == 1 ) {
1485 pkrb5_cc_close(ctx, cc);
1486 if ( cellconfig.linkedCell )
1487 free(cellconfig.linkedCell);
1489 if ( code && reasonP ) {
1490 *reasonP = (char *)perror_message(code);
1496 KFW_AFS_destroy_tickets_for_cell(char * cell)
1498 krb5_context ctx = NULL;
1499 krb5_error_code code;
1501 char ** principals = NULL;
1503 if (!pkrb5_init_context)
1506 if ( IsDebuggerPresent() ) {
1507 OutputDebugString("KFW_AFS_destroy_tickets_for_cell: ");
1508 OutputDebugString(cell);
1509 OutputDebugString("\n");
1512 code = pkrb5_init_context(&ctx);
1515 count = KFW_AFS_find_principals_for_cell(ctx, cell, &principals, FALSE);
1517 krb5_principal princ = 0;
1521 int cell_count = KFW_AFS_find_cells_for_princ(ctx, principals[count], NULL, TRUE);
1522 if ( cell_count > 1 ) {
1523 // TODO - What we really should do here is verify whether or not any of the
1524 // other cells which use this principal to obtain its credentials actually
1525 // have valid tokens or not. If they are currently using these credentials
1526 // we will skip them. For the time being we assume that if there is an active
1527 // map in the table that they are actively being used.
1531 code = pkrb5_parse_name(ctx, principals[count], &princ);
1532 if (code) goto loop_cleanup;
1534 code = KFW_get_ccache(ctx, princ, &cc);
1535 if (code) goto loop_cleanup;
1537 code = pkrb5_cc_destroy(ctx, cc);
1542 pkrb5_cc_close(ctx, cc);
1546 pkrb5_free_principal(ctx, princ);
1550 KFW_AFS_update_cell_princ_map(ctx, cell, principals[count], FALSE);
1551 free(principals[count]);
1556 pkrb5_free_context(ctx);
1561 KFW_AFS_destroy_tickets_for_principal(char * user)
1563 krb5_context ctx = NULL;
1564 krb5_error_code code;
1566 char ** cells = NULL;
1567 krb5_principal princ = NULL;
1568 krb5_ccache cc = NULL;
1570 if (!pkrb5_init_context)
1573 if ( IsDebuggerPresent() ) {
1574 OutputDebugString("KFW_AFS_destroy_tickets_for_user: ");
1575 OutputDebugString(user);
1576 OutputDebugString("\n");
1579 code = pkrb5_init_context(&ctx);
1582 code = pkrb5_parse_name(ctx, user, &princ);
1583 if (code) goto loop_cleanup;
1585 code = KFW_get_ccache(ctx, princ, &cc);
1586 if (code) goto loop_cleanup;
1588 code = pkrb5_cc_destroy(ctx, cc);
1593 pkrb5_cc_close(ctx, cc);
1597 pkrb5_free_principal(ctx, princ);
1601 count = KFW_AFS_find_cells_for_princ(ctx, user, &cells, TRUE);
1604 KFW_AFS_update_cell_princ_map(ctx, cells[count], user, FALSE);
1611 pkrb5_free_context(ctx);
1616 KFW_AFS_renew_expiring_tokens(void)
1618 krb5_error_code code = 0;
1619 krb5_context ctx = NULL;
1620 krb5_ccache cc = NULL;
1622 struct principal_ccache_data * pcc_next = princ_cc_data;
1625 const char * realm = NULL;
1626 char local_cell[CELL_MAXNAMELEN+1]="";
1627 struct afsconf_cell cellconfig;
1629 if (!pkrb5_init_context)
1632 if ( pcc_next == NULL ) // nothing to do
1635 if ( IsDebuggerPresent() ) {
1636 OutputDebugString("KFW_AFS_renew_expiring_tokens\n");
1639 memset(&cellconfig, 0, sizeof(cellconfig));
1641 code = pkrb5_init_context(&ctx);
1642 if (code) goto cleanup;
1644 code = pkrb5_timeofday(ctx, &now);
1645 if (code) goto cleanup;
1647 for ( ; pcc_next ; pcc_next = pcc_next->next ) {
1648 if ( pcc_next->expired )
1651 if ( now >= (pcc_next->expiration_time) ) {
1652 if ( !pcc_next->from_lsa ) {
1653 pcc_next->expired = 1;
1658 if ( pcc_next->renew && now >= (pcc_next->expiration_time - cminRENEW * csec1MINUTE) ) {
1659 code = pkrb5_cc_resolve(ctx, pcc_next->ccache_name, &cc);
1662 code = KFW_renew(ctx,cc);
1664 if ( code && pcc_next->from_lsa)
1666 #endif /* USE_MS2MIT */
1669 KFW_AFS_update_princ_ccache_data(ctx, cc, pcc_next->from_lsa);
1670 if (code) goto loop_cleanup;
1672 // Attempt to obtain new tokens for other cells supported by the same
1674 cell_count = KFW_AFS_find_cells_for_princ(ctx, pcc_next->principal, &cells, TRUE);
1675 if ( cell_count > 0 ) {
1676 while ( cell_count-- ) {
1677 if ( IsDebuggerPresent() ) {
1678 OutputDebugString("Cell: ");
1679 OutputDebugString(cells[cell_count]);
1680 OutputDebugString("\n");
1682 if (cellconfig.linkedCell) {
1683 free(cellconfig.linkedCell);
1684 cellconfig.linkedCell = NULL;
1686 code = KFW_AFS_get_cellconfig( cells[cell_count], (void*)&cellconfig, local_cell);
1687 if ( code ) continue;
1688 realm = afs_realm_of_cell(ctx, &cellconfig); // do not free
1689 if ( IsDebuggerPresent() ) {
1690 OutputDebugString("Realm: ");
1691 OutputDebugString(realm);
1692 OutputDebugString("\n");
1694 code = KFW_AFS_klog(ctx, cc, "afs", cells[cell_count], (char *)realm, 0, NULL);
1695 if ( IsDebuggerPresent() ) {
1697 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
1698 OutputDebugString(message);
1700 free(cells[cell_count]);
1708 pkrb5_cc_close(ctx,cc);
1715 pkrb5_cc_close(ctx,cc);
1717 pkrb5_free_context(ctx);
1718 if (cellconfig.linkedCell)
1719 free(cellconfig.linkedCell);
1726 KFW_AFS_renew_token_for_cell(char * cell)
1728 krb5_error_code code = 0;
1729 krb5_context ctx = NULL;
1731 char ** principals = NULL;
1733 if (!pkrb5_init_context)
1736 if ( IsDebuggerPresent() ) {
1737 OutputDebugString("KFW_AFS_renew_token_for_cell:");
1738 OutputDebugString(cell);
1739 OutputDebugString("\n");
1742 code = pkrb5_init_context(&ctx);
1743 if (code) goto cleanup;
1745 count = KFW_AFS_find_principals_for_cell(ctx, cell, &principals, TRUE);
1747 // We know we must have a credential somewhere since we are
1748 // trying to renew a token
1750 KFW_import_ccache_data();
1751 count = KFW_AFS_find_principals_for_cell(ctx, cell, &principals, TRUE);
1754 krb5_principal princ = 0;
1755 krb5_principal service = 0;
1757 krb5_creds mcreds, creds;
1758 #endif /* COMMENT */
1760 const char * realm = NULL;
1761 struct afsconf_cell cellconfig;
1762 char local_cell[CELL_MAXNAMELEN+1];
1764 memset(&cellconfig, 0, sizeof(cellconfig));
1767 code = pkrb5_parse_name(ctx, principals[count], &princ);
1768 if (code) goto loop_cleanup;
1770 code = KFW_get_ccache(ctx, princ, &cc);
1771 if (code) goto loop_cleanup;
1773 if (cellconfig.linkedCell) {
1774 free(cellconfig.linkedCell);
1775 cellconfig.linkedCell = NULL;
1777 code = KFW_AFS_get_cellconfig( cell, (void*)&cellconfig, local_cell);
1778 if ( code ) goto loop_cleanup;
1780 realm = afs_realm_of_cell(ctx, &cellconfig); // do not free
1781 if ( IsDebuggerPresent() ) {
1782 OutputDebugString("Realm: ");
1783 OutputDebugString(realm);
1784 OutputDebugString("\n");
1788 /* krb5_cc_remove_cred() is not implemented
1791 code = pkrb5_build_principal(ctx, &service, strlen(realm),
1792 realm, "afs", cell, NULL);
1794 memset(&mcreds, 0, sizeof(krb5_creds));
1795 mcreds.client = princ;
1796 mcreds.server = service;
1798 code = pkrb5_cc_retrieve_cred(ctx, cc, 0, &mcreds, &creds);
1800 if ( IsDebuggerPresent() ) {
1801 char * cname, *sname;
1802 pkrb5_unparse_name(ctx, creds.client, &cname);
1803 pkrb5_unparse_name(ctx, creds.server, &sname);
1804 OutputDebugString("Removing credential for client \"");
1805 OutputDebugString(cname);
1806 OutputDebugString("\" and service \"");
1807 OutputDebugString(sname);
1808 OutputDebugString("\"\n");
1809 pkrb5_free_unparsed_name(ctx,cname);
1810 pkrb5_free_unparsed_name(ctx,sname);
1813 code = pkrb5_cc_remove_cred(ctx, cc, 0, &creds);
1814 pkrb5_free_principal(ctx, creds.client);
1815 pkrb5_free_principal(ctx, creds.server);
1818 #endif /* COMMENT */
1820 code = KFW_AFS_klog(ctx, cc, "afs", cell, (char *)realm, 0,NULL);
1821 if ( IsDebuggerPresent() ) {
1823 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
1824 OutputDebugString(message);
1829 pkrb5_cc_close(ctx, cc);
1833 pkrb5_free_principal(ctx, princ);
1837 pkrb5_free_principal(ctx, service);
1840 if (cellconfig.linkedCell) {
1841 free(cellconfig.linkedCell);
1842 cellconfig.linkedCell = NULL;
1845 KFW_AFS_update_cell_princ_map(ctx, cell, principals[count], code ? FALSE : TRUE);
1846 free(principals[count]);
1850 code = -1; // we did not renew the tokens
1854 pkrb5_free_context(ctx);
1855 return (code ? FALSE : TRUE);
1860 KFW_AFS_renew_tokens_for_all_cells(void)
1862 struct cell_principal_map * next = cell_princ_map;
1864 if ( IsDebuggerPresent() )
1865 OutputDebugString("KFW_AFS_renew_tokens_for_all()\n");
1870 for ( ; next ; next = next->next ) {
1872 KFW_AFS_renew_token_for_cell(next->cell);
1878 KFW_renew(krb5_context alt_ctx, krb5_ccache alt_cc)
1880 krb5_error_code code = 0;
1881 krb5_context ctx = NULL;
1882 krb5_ccache cc = NULL;
1883 krb5_principal me = NULL;
1884 krb5_principal server = NULL;
1885 krb5_creds my_creds;
1886 krb5_data *realm = NULL;
1888 if (!pkrb5_init_context)
1891 memset(&my_creds, 0, sizeof(krb5_creds));
1896 code = pkrb5_init_context(&ctx);
1897 if (code) goto cleanup;
1903 code = pkrb5_cc_default(ctx, &cc);
1904 if (code) goto cleanup;
1907 code = pkrb5_cc_get_principal(ctx, cc, &me);
1908 if (code) goto cleanup;
1910 realm = krb5_princ_realm(ctx, me);
1912 code = pkrb5_build_principal_ext(ctx, &server,
1913 realm->length,realm->data,
1914 KRB5_TGS_NAME_SIZE, KRB5_TGS_NAME,
1915 realm->length,realm->data,
1920 if ( IsDebuggerPresent() ) {
1921 char * cname, *sname;
1922 pkrb5_unparse_name(ctx, me, &cname);
1923 pkrb5_unparse_name(ctx, server, &sname);
1924 OutputDebugString("Renewing credential for client \"");
1925 OutputDebugString(cname);
1926 OutputDebugString("\" and service \"");
1927 OutputDebugString(sname);
1928 OutputDebugString("\"\n");
1929 pkrb5_free_unparsed_name(ctx,cname);
1930 pkrb5_free_unparsed_name(ctx,sname);
1933 my_creds.client = me;
1934 my_creds.server = server;
1936 code = pkrb5_get_renewed_creds(ctx, &my_creds, me, cc, NULL);
1938 if ( IsDebuggerPresent() ) {
1940 sprintf(message,"krb5_get_renewed_creds() failed: %d\n",code);
1941 OutputDebugString(message);
1946 code = pkrb5_cc_initialize(ctx, cc, me);
1948 if ( IsDebuggerPresent() ) {
1950 sprintf(message,"krb5_cc_initialize() failed: %d\n",code);
1951 OutputDebugString(message);
1956 code = pkrb5_cc_store_cred(ctx, cc, &my_creds);
1958 if ( IsDebuggerPresent() ) {
1960 sprintf(message,"krb5_cc_store_cred() failed: %d\n",code);
1961 OutputDebugString(message);
1967 if (my_creds.client == me)
1968 my_creds.client = 0;
1969 if (my_creds.server == server)
1970 my_creds.server = 0;
1971 pkrb5_free_cred_contents(ctx, &my_creds);
1973 pkrb5_free_principal(ctx, me);
1975 pkrb5_free_principal(ctx, server);
1976 if (cc && (cc != alt_cc))
1977 pkrb5_cc_close(ctx, cc);
1978 if (ctx && (ctx != alt_ctx))
1979 pkrb5_free_context(ctx);
1984 KFW_kinit( krb5_context alt_ctx,
1987 char *principal_name,
1989 krb5_deltat lifetime,
1992 krb5_deltat renew_life,
1997 krb5_error_code code = 0;
1998 krb5_context ctx = NULL;
1999 krb5_ccache cc = NULL;
2000 krb5_principal me = NULL;
2002 krb5_creds my_creds;
2003 krb5_get_init_creds_opt options;
2004 krb5_address ** addrs = NULL;
2005 int i = 0, addr_count = 0;
2007 if (!pkrb5_init_context)
2010 pkrb5_get_init_creds_opt_init(&options);
2011 memset(&my_creds, 0, sizeof(my_creds));
2019 code = pkrb5_init_context(&ctx);
2020 if (code) goto cleanup;
2026 code = pkrb5_cc_default(ctx, &cc);
2027 if (code) goto cleanup;
2030 code = pkrb5_parse_name(ctx, principal_name, &me);
2034 code = pkrb5_unparse_name(ctx, me, &name);
2042 lifetime = pLeash_get_default_lifetime();
2043 #endif /* USE_LEASH */
2050 pkrb5_get_init_creds_opt_set_tkt_life(&options, lifetime);
2051 pkrb5_get_init_creds_opt_set_forwardable(&options,
2052 forwardable ? 1 : 0);
2053 pkrb5_get_init_creds_opt_set_proxiable(&options,
2055 pkrb5_get_init_creds_opt_set_renew_life(&options,
2058 pkrb5_get_init_creds_opt_set_address_list(&options,NULL);
2062 // we are going to add the public IP address specified by the user
2063 // to the list provided by the operating system
2064 krb5_address ** local_addrs=NULL;
2067 pkrb5_os_localaddr(ctx, &local_addrs);
2068 while ( local_addrs[i++] );
2071 addrs = (krb5_address **) malloc((addr_count+1) * sizeof(krb5_address *));
2073 pkrb5_free_addresses(ctx, local_addrs);
2076 memset(addrs, 0, sizeof(krb5_address *) * (addr_count+1));
2078 while ( local_addrs[i] ) {
2079 addrs[i] = (krb5_address *)malloc(sizeof(krb5_address));
2080 if (addrs[i] == NULL) {
2081 pkrb5_free_addresses(ctx, local_addrs);
2085 addrs[i]->magic = local_addrs[i]->magic;
2086 addrs[i]->addrtype = local_addrs[i]->addrtype;
2087 addrs[i]->length = local_addrs[i]->length;
2088 addrs[i]->contents = (unsigned char *)malloc(addrs[i]->length);
2089 if (!addrs[i]->contents) {
2090 pkrb5_free_addresses(ctx, local_addrs);
2094 memcpy(addrs[i]->contents,local_addrs[i]->contents,
2095 local_addrs[i]->length); /* safe */
2098 pkrb5_free_addresses(ctx, local_addrs);
2100 addrs[i] = (krb5_address *)malloc(sizeof(krb5_address));
2101 if (addrs[i] == NULL)
2104 addrs[i]->magic = KV5M_ADDRESS;
2105 addrs[i]->addrtype = AF_INET;
2106 addrs[i]->length = 4;
2107 addrs[i]->contents = (unsigned char *)malloc(addrs[i]->length);
2108 if (!addrs[i]->contents)
2111 netIPAddr = htonl(publicIP);
2112 memcpy(addrs[i]->contents,&netIPAddr,4);
2114 pkrb5_get_init_creds_opt_set_address_list(&options,addrs);
2119 code = pkrb5_get_init_creds_password(ctx,
2122 password, // password
2123 KRB5_prompter, // prompter
2124 hParent, // prompter data
2131 code = pkrb5_cc_initialize(ctx, cc, me);
2135 code = pkrb5_cc_store_cred(ctx, cc, &my_creds);
2141 for ( i=0;i<addr_count;i++ ) {
2143 if ( addrs[i]->contents )
2144 free(addrs[i]->contents);
2149 if (my_creds.client == me)
2150 my_creds.client = 0;
2151 pkrb5_free_cred_contents(ctx, &my_creds);
2153 pkrb5_free_unparsed_name(ctx, name);
2155 pkrb5_free_principal(ctx, me);
2156 if (cc && (cc != alt_cc))
2157 pkrb5_cc_close(ctx, cc);
2158 if (ctx && (ctx != alt_ctx))
2159 pkrb5_free_context(ctx);
2165 KFW_kdestroy(krb5_context alt_ctx, krb5_ccache alt_cc)
2167 krb5_context ctx = NULL;
2168 krb5_ccache cc = NULL;
2169 krb5_error_code code;
2171 if (!pkrb5_init_context)
2180 code = pkrb5_init_context(&ctx);
2181 if (code) goto cleanup;
2187 code = pkrb5_cc_default(ctx, &cc);
2188 if (code) goto cleanup;
2191 code = pkrb5_cc_destroy(ctx, cc);
2192 if ( !code ) cc = 0;
2195 if (cc && (cc != alt_cc))
2196 pkrb5_cc_close(ctx, cc);
2197 if (ctx && (ctx != alt_ctx))
2198 pkrb5_free_context(ctx);
2206 GetSecurityLogonSessionData(PSECURITY_LOGON_SESSION_DATA * ppSessionData)
2208 NTSTATUS Status = 0;
2210 TOKEN_STATISTICS Stats;
2216 *ppSessionData = NULL;
2218 Success = OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &TokenHandle );
2222 Success = GetTokenInformation( TokenHandle, TokenStatistics, &Stats, sizeof(TOKEN_STATISTICS), &ReqLen );
2223 CloseHandle( TokenHandle );
2227 Status = pLsaGetLogonSessionData( &Stats.AuthenticationId, ppSessionData );
2228 if ( FAILED(Status) || !ppSessionData )
2235 // MSLSA_IsKerberosLogon() does not validate whether or not there are valid tickets in the
2236 // cache. It validates whether or not it is reasonable to assume that if we
2237 // attempted to retrieve valid tickets we could do so. Microsoft does not
2238 // automatically renew expired tickets. Therefore, the cache could contain
2239 // expired or invalid tickets. Microsoft also caches the user's password
2240 // and will use it to retrieve new TGTs if the cache is empty and tickets
2244 MSLSA_IsKerberosLogon(VOID)
2246 PSECURITY_LOGON_SESSION_DATA pSessionData = NULL;
2247 BOOL Success = FALSE;
2249 if ( GetSecurityLogonSessionData(&pSessionData) ) {
2250 if ( pSessionData->AuthenticationPackage.Buffer ) {
2256 usBuffer = (pSessionData->AuthenticationPackage).Buffer;
2257 usLength = (pSessionData->AuthenticationPackage).Length;
2260 lstrcpynW (buffer, usBuffer, usLength);
2261 lstrcatW (buffer,L"");
2262 if ( !lstrcmpW(L"Kerberos",buffer) )
2266 pLsaFreeReturnBuffer(pSessionData);
2270 #endif /* USE_MS2MIT */
2272 static BOOL CALLBACK
2273 MultiInputDialogProc( HWND hDialog, UINT message, WPARAM wParam, LPARAM lParam)
2277 switch ( message ) {
2279 if ( GetDlgCtrlID((HWND) wParam) != ID_MID_TEXT )
2281 SetFocus(GetDlgItem( hDialog, ID_MID_TEXT));
2284 for ( i=0; i < mid_cnt ; i++ ) {
2285 if (mid_tb[i].echo == 0)
2286 SendDlgItemMessage(hDialog, ID_MID_TEXT+i, EM_SETPASSWORDCHAR, 32, 0);
2287 else if (mid_tb[i].echo == 2)
2288 SendDlgItemMessage(hDialog, ID_MID_TEXT+i, EM_SETPASSWORDCHAR, '*', 0);
2293 switch ( LOWORD(wParam) ) {
2295 for ( i=0; i < mid_cnt ; i++ ) {
2296 if ( !GetDlgItemText(hDialog, ID_MID_TEXT+i, mid_tb[i].buf, mid_tb[i].len) )
2297 *mid_tb[i].buf = '\0';
2301 EndDialog(hDialog, LOWORD(wParam));
2309 lpwAlign( LPWORD lpIn )
2313 ul = (ULONG_PTR) lpIn;
2317 return (LPWORD) ul;;
2321 * dialog widths are measured in 1/4 character widths
2322 * dialog height are measured in 1/8 character heights
2326 MultiInputDialog( HINSTANCE hinst, HWND hwndOwner,
2327 char * ptext[], int numlines, int width,
2328 int tb_cnt, struct textField * tb)
2332 LPDLGITEMTEMPLATE lpdit;
2338 hgbl = GlobalAlloc(GMEM_ZEROINIT, 4096);
2345 lpdt = (LPDLGTEMPLATE)GlobalLock(hgbl);
2347 // Define a dialog box.
2349 lpdt->style = WS_POPUP | WS_BORDER | WS_SYSMENU
2350 | DS_MODALFRAME | WS_CAPTION | DS_CENTER
2351 | DS_SETFOREGROUND | DS_3DLOOK
2352 | DS_SETFONT | DS_FIXEDSYS | DS_NOFAILCREATE;
2353 lpdt->cdit = numlines + (2 * tb_cnt) + 2; // number of controls
2356 lpdt->cx = 20 + width * 4;
2357 lpdt->cy = 20 + (numlines + tb_cnt + 4) * 14;
2359 lpw = (LPWORD) (lpdt + 1);
2360 *lpw++ = 0; // no menu
2361 *lpw++ = 0; // predefined dialog box class (by default)
2363 lpwsz = (LPWSTR) lpw;
2364 nchar = MultiByteToWideChar (CP_ACP, 0, "", -1, lpwsz, 128);
2366 *lpw++ = 8; // font size (points)
2367 lpwsz = (LPWSTR) lpw;
2368 nchar = MultiByteToWideChar (CP_ACP, 0, "MS Shell Dlg",
2372 //-----------------------
2373 // Define an OK button.
2374 //-----------------------
2375 lpw = lpwAlign (lpw); // align DLGITEMTEMPLATE on DWORD boundary
2376 lpdit = (LPDLGITEMTEMPLATE) lpw;
2377 lpdit->style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP | WS_BORDER;
2378 lpdit->dwExtendedStyle = 0;
2379 lpdit->x = (lpdt->cx - 14)/4 - 20;
2380 lpdit->y = 10 + (numlines + tb_cnt + 2) * 14;
2383 lpdit->id = IDOK; // OK button identifier
2385 lpw = (LPWORD) (lpdit + 1);
2387 *lpw++ = 0x0080; // button class
2389 lpwsz = (LPWSTR) lpw;
2390 nchar = MultiByteToWideChar (CP_ACP, 0, "OK", -1, lpwsz, 50);
2392 *lpw++ = 0; // no creation data
2394 //-----------------------
2395 // Define an Cancel button.
2396 //-----------------------
2397 lpw = lpwAlign (lpw); // align DLGITEMTEMPLATE on DWORD boundary
2398 lpdit = (LPDLGITEMTEMPLATE) lpw;
2399 lpdit->style = WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP | WS_BORDER;
2400 lpdit->dwExtendedStyle = 0;
2401 lpdit->x = (lpdt->cx - 14)*3/4 - 20;
2402 lpdit->y = 10 + (numlines + tb_cnt + 2) * 14;
2405 lpdit->id = IDCANCEL; // CANCEL button identifier
2407 lpw = (LPWORD) (lpdit + 1);
2409 *lpw++ = 0x0080; // button class
2411 lpwsz = (LPWSTR) lpw;
2412 nchar = MultiByteToWideChar (CP_ACP, 0, "Cancel", -1, lpwsz, 50);
2414 *lpw++ = 0; // no creation data
2416 /* Add controls for preface data */
2417 for ( i=0; i<numlines; i++) {
2418 /*-----------------------
2419 * Define a static text control.
2420 *-----------------------*/
2421 lpw = lpwAlign (lpw); /* align DLGITEMTEMPLATE on DWORD boundary */
2422 lpdit = (LPDLGITEMTEMPLATE) lpw;
2423 lpdit->style = WS_CHILD | WS_VISIBLE | SS_LEFT;
2424 lpdit->dwExtendedStyle = 0;
2426 lpdit->y = 10 + i * 14;
2427 lpdit->cx = (short)strlen(ptext[i]) * 4 + 10;
2429 lpdit->id = ID_TEXT + i; // text identifier
2431 lpw = (LPWORD) (lpdit + 1);
2433 *lpw++ = 0x0082; // static class
2435 lpwsz = (LPWSTR) lpw;
2436 nchar = MultiByteToWideChar (CP_ACP, 0, ptext[i],
2437 -1, lpwsz, 2*width);
2439 *lpw++ = 0; // no creation data
2442 for ( i=0, pwid = 0; i<tb_cnt; i++) {
2443 int len = (int)strlen(tb[i].label);
2448 for ( i=0; i<tb_cnt; i++) {
2450 /*-----------------------
2451 * Define a static text control.
2452 *-----------------------*/
2453 lpw = lpwAlign (lpw); /* align DLGITEMTEMPLATE on DWORD boundary */
2454 lpdit = (LPDLGITEMTEMPLATE) lpw;
2455 lpdit->style = WS_CHILD | WS_VISIBLE | SS_LEFT;
2456 lpdit->dwExtendedStyle = 0;
2458 lpdit->y = 10 + (numlines + i + 1) * 14;
2459 lpdit->cx = pwid * 4;
2461 lpdit->id = ID_TEXT + numlines + i; // text identifier
2463 lpw = (LPWORD) (lpdit + 1);
2465 *lpw++ = 0x0082; // static class
2467 lpwsz = (LPWSTR) lpw;
2468 nchar = MultiByteToWideChar (CP_ACP, 0, tb[i].label ? tb[i].label : "",
2471 *lpw++ = 0; // no creation data
2473 /*-----------------------
2474 * Define an edit control.
2475 *-----------------------*/
2476 lpw = lpwAlign (lpw); /* align DLGITEMTEMPLATE on DWORD boundary */
2477 lpdit = (LPDLGITEMTEMPLATE) lpw;
2478 lpdit->style = WS_CHILD | WS_VISIBLE | ES_LEFT | WS_TABSTOP | WS_BORDER | (tb[i].echo == 1 ? 0L : ES_PASSWORD);
2479 lpdit->dwExtendedStyle = 0;
2480 lpdit->x = 10 + (pwid + 1) * 4;
2481 lpdit->y = 10 + (numlines + i + 1) * 14;
2482 lpdit->cx = (width - (pwid + 1)) * 4;
2484 lpdit->id = ID_MID_TEXT + i; // identifier
2486 lpw = (LPWORD) (lpdit + 1);
2488 *lpw++ = 0x0081; // edit class
2490 lpwsz = (LPWSTR) lpw;
2491 nchar = MultiByteToWideChar (CP_ACP, 0, tb[i].def ? tb[i].def : "",
2494 *lpw++ = 0; // no creation data
2498 ret = DialogBoxIndirect(hinst, (LPDLGTEMPLATE) hgbl,
2499 hwndOwner, (DLGPROC) MultiInputDialogProc);
2503 case 0: /* Timeout */
2511 sprintf(buf,"DialogBoxIndirect() failed: %d",GetLastError());
2512 MessageBox(hwndOwner,
2515 MB_OK | MB_ICONINFORMATION | MB_TASKMODAL);
2522 multi_field_dialog(HWND hParent, char * preface, int n, struct textField tb[])
2524 HINSTANCE hInst = 0;
2528 char * plines[16], *p = preface ? preface : "";
2531 for ( i=0; i<16; i++ )
2534 while (*p && numlines < 16) {
2535 plines[numlines++] = p;
2536 for ( ;*p && *p != '\r' && *p != '\n'; p++ );
2537 if ( *p == '\r' && *(p+1) == '\n' ) {
2540 } else if ( *p == '\n' ) {
2543 if ( strlen(plines[numlines-1]) > maxwidth )
2544 maxwidth = (int)strlen(plines[numlines-1]);
2547 for ( i=0;i<n;i++ ) {
2548 len = (int)strlen(tb[i].label) + 1 + (tb[i].len > 40 ? 40 : tb[i].len);
2549 if ( maxwidth < len )
2553 return(MultiInputDialog(hInst, hParent, plines, numlines, maxwidth, n, tb)?1:0);
2556 static krb5_error_code KRB5_CALLCONV
2557 KRB5_prompter( krb5_context context,
2562 krb5_prompt prompts[])
2564 krb5_error_code errcode = 0;
2566 struct textField * tb = NULL;
2567 int len = 0, blen=0, nlen=0;
2568 HWND hParent = (HWND)data;
2571 nlen = (int)strlen(name)+2;
2574 blen = (int)strlen(banner)+2;
2576 tb = (struct textField *) malloc(sizeof(struct textField) * num_prompts);
2579 memset(tb,0,sizeof(struct textField) * num_prompts);
2580 for ( i=0; i < num_prompts; i++ ) {
2581 tb[i].buf = prompts[i].reply->data;
2582 tb[i].len = prompts[i].reply->length;
2583 tb[i].label = prompts[i].prompt;
2585 tb[i].echo = (prompts[i].hidden ? 2 : 1);
2588 ok = multi_field_dialog(hParent,(char *)banner,num_prompts,tb);
2590 for ( i=0; i < num_prompts; i++ )
2591 prompts[i].reply->length = (unsigned int)strlen(prompts[i].reply->data);
2599 for (i = 0; i < num_prompts; i++) {
2600 memset(prompts[i].reply->data, 0, prompts[i].reply->length);
2607 KFW_AFS_wait_for_service_start(void)
2612 CurrentState = SERVICE_START_PENDING;
2613 memset(HostName, '\0', sizeof(HostName));
2614 gethostname(HostName, sizeof(HostName));
2616 while (CurrentState != SERVICE_RUNNING || CurrentState != SERVICE_STOPPED)
2618 if (GetServiceStatus(HostName, TRANSARCAFSDAEMON, &CurrentState) != NOERROR)
2620 if ( IsDebuggerPresent() ) {
2621 switch ( CurrentState ) {
2622 case SERVICE_STOPPED:
2623 OutputDebugString("SERVICE_STOPPED\n");
2625 case SERVICE_START_PENDING:
2626 OutputDebugString("SERVICE_START_PENDING\n");
2628 case SERVICE_STOP_PENDING:
2629 OutputDebugString("SERVICE_STOP_PENDING\n");
2631 case SERVICE_RUNNING:
2632 OutputDebugString("SERVICE_RUNNING\n");
2634 case SERVICE_CONTINUE_PENDING:
2635 OutputDebugString("SERVICE_CONTINUE_PENDING\n");
2637 case SERVICE_PAUSE_PENDING:
2638 OutputDebugString("SERVICE_PAUSE_PENDING\n");
2640 case SERVICE_PAUSED:
2641 OutputDebugString("SERVICE_PAUSED\n");
2644 OutputDebugString("UNKNOWN Service State\n");
2647 if (CurrentState == SERVICE_STOPPED)
2649 if (CurrentState == SERVICE_RUNNING)
2665 memset(HostName, '\0', sizeof(HostName));
2666 gethostname(HostName, sizeof(HostName));
2667 if (GetServiceStatus(HostName, TRANSARCAFSDAEMON, &CurrentState) != NOERROR)
2669 if (CurrentState != SERVICE_RUNNING)
2672 rc = ktc_ForgetAllTokens();
2678 #define ALLOW_REGISTER 1
2680 ViceIDToUsername(char *username,
2681 char *realm_of_user,
2682 char *realm_of_cell,
2684 struct ktc_principal *aclient,
2685 struct ktc_principal *aserver,
2686 struct ktc_token *atoken)
2688 static char lastcell[CELL_MAXNAMELEN+1] = { 0 };
2689 static char confdir[512] = { 0 };
2690 #ifdef AFS_ID_TO_NAME
2691 char username_copy[BUFSIZ];
2692 #endif /* AFS_ID_TO_NAME */
2693 long viceId = ANONYMOUSID; /* AFS uid of user */
2695 #ifdef ALLOW_REGISTER
2697 #endif /* ALLOW_REGISTER */
2699 if (confdir[0] == '\0')
2700 cm_GetConfigDir(confdir, sizeof(confdir));
2702 strcpy(lastcell, aserver->cell);
2704 if (!pr_Initialize (0, confdir, aserver->cell)) {
2705 char sname[PR_MAXNAMELEN];
2706 strncpy(sname, username, PR_MAXNAMELEN);
2707 sname[PR_MAXNAMELEN-1] = '\0';
2708 status = pr_SNameToId (sname, &viceId);
2713 * This is a crock, but it is Transarc's crock, so
2714 * we have to play along in order to get the
2715 * functionality. The way the afs id is stored is
2716 * as a string in the username field of the token.
2717 * Contrary to what you may think by looking at
2718 * the code for tokens, this hack (AFS ID %d) will
2719 * not work if you change %d to something else.
2723 * This code is taken from cklog -- it lets people
2724 * automatically register with the ptserver in foreign cells
2727 #ifdef ALLOW_REGISTER
2729 if (viceId != ANONYMOUSID) {
2730 #else /* ALLOW_REGISTER */
2731 if ((status == 0) && (viceId != ANONYMOUSID))
2732 #endif /* ALLOW_REGISTER */
2734 #ifdef AFS_ID_TO_NAME
2735 strncpy(username_copy, username, BUFSIZ);
2736 snprintf (username, BUFSIZ, "%s (AFS ID %d)", username_copy, (int) viceId);
2737 #endif /* AFS_ID_TO_NAME */
2739 #ifdef ALLOW_REGISTER
2740 } else if (strcmp(realm_of_user, realm_of_cell) != 0) {
2742 strncpy(aclient->name, username, MAXKTCNAMELEN - 1);
2743 strcpy(aclient->instance, "");
2744 strncpy(aclient->cell, realm_of_user, MAXKTCREALMLEN - 1);
2745 if (status = ktc_SetToken(aserver, atoken, aclient, 0))
2747 if (status = pr_Initialize(1L, confdir, aserver->cell))
2749 status = pr_CreateUser(username, &id);
2753 #ifdef AFS_ID_TO_NAME
2754 strncpy(username_copy, username, BUFSIZ);
2755 snprintf (username, BUFSIZ, "%s (AFS ID %d)", username_copy, (int) viceId);
2756 #endif /* AFS_ID_TO_NAME */
2759 #endif /* ALLOW_REGISTER */
2765 copy_realm_of_ticket(krb5_context context, char * dest, size_t destlen, krb5_creds *v5cred) {
2766 krb5_error_code code;
2767 krb5_ticket *ticket;
2770 code = pkrb5_decode_ticket(&v5cred->ticket, &ticket);
2772 len = krb5_princ_realm(context, ticket->server)->length;
2773 if (len > destlen - 1)
2776 strncpy(dest, krb5_princ_realm(context, ticket->server)->data, len);
2779 pkrb5_free_ticket(context, ticket);
2785 krb5_context alt_ctx,
2790 int lifetime, /* unused parameter */
2798 #endif /* USE_KRB4 */
2799 struct ktc_principal aserver;
2800 struct ktc_principal aclient;
2801 char realm_of_user[REALM_SZ]; /* Kerberos realm of user */
2802 char realm_of_cell[REALM_SZ]; /* Kerberos realm of cell */
2803 char local_cell[CELL_MAXNAMELEN+1];
2804 char Dmycell[CELL_MAXNAMELEN+1];
2805 struct ktc_token atoken;
2806 struct ktc_token btoken;
2807 struct afsconf_cell ak_cellconfig; /* General information about the cell */
2808 char RealmName[128];
2810 char ServiceName[128];
2814 krb5_context ctx = NULL;
2815 krb5_ccache cc = NULL;
2817 krb5_creds * k5creds = NULL;
2818 krb5_error_code code;
2819 krb5_principal client_principal = NULL;
2820 krb5_data * k5data = NULL;
2824 memset(HostName, '\0', sizeof(HostName));
2825 gethostname(HostName, sizeof(HostName));
2826 if (GetServiceStatus(HostName, TRANSARCAFSDAEMON, &CurrentState) != NOERROR) {
2827 if ( IsDebuggerPresent() )
2828 OutputDebugString("Unable to retrieve AFSD Service Status\n");
2831 if (CurrentState != SERVICE_RUNNING) {
2832 if ( IsDebuggerPresent() )
2833 OutputDebugString("AFSD Service NOT RUNNING\n");
2837 if (!pkrb5_init_context)
2840 memset(&ak_cellconfig, 0, sizeof(ak_cellconfig));
2841 memset(RealmName, '\0', sizeof(RealmName));
2842 memset(CellName, '\0', sizeof(CellName));
2843 memset(ServiceName, '\0', sizeof(ServiceName));
2844 memset(realm_of_user, '\0', sizeof(realm_of_user));
2845 memset(realm_of_cell, '\0', sizeof(realm_of_cell));
2846 if (cell && cell[0])
2847 strcpy(Dmycell, cell);
2849 memset(Dmycell, '\0', sizeof(Dmycell));
2851 // NULL or empty cell returns information on local cell
2852 if (rc = KFW_AFS_get_cellconfig(Dmycell, &ak_cellconfig, local_cell))
2854 // KFW_AFS_error(rc, "get_cellconfig()");
2861 code = pkrb5_init_context(&ctx);
2862 if (code) goto cleanup;
2868 code = pkrb5_cc_default(ctx, &cc);
2869 if (code) goto skip_krb5_init;
2872 memset((char *)&increds, 0, sizeof(increds));
2874 code = pkrb5_cc_get_principal(ctx, cc, &client_principal);
2876 if ( code == KRB5_CC_NOTFOUND && IsDebuggerPresent() )
2878 OutputDebugString("Principal Not Found for ccache\n");
2880 goto skip_krb5_init;
2883 if (!KFW_accept_dotted_usernames()) {
2884 /* look for client principals which cannot be distinguished
2885 * from Kerberos 4 multi-component principal names
2887 k5data = krb5_princ_component(ctx,client_principal,0);
2888 for ( i=0; i<k5data->length; i++ ) {
2889 if ( k5data->data[i] == '.' )
2892 if (i != k5data->length)
2894 OutputDebugString("Illegal Principal name contains dot in first component\n");
2895 rc = KRB5KRB_ERR_GENERIC;
2900 i = krb5_princ_realm(ctx, client_principal)->length;
2903 strncpy(realm_of_user,krb5_princ_realm(ctx, client_principal)->data,i);
2904 realm_of_user[i] = 0;
2909 if ( !try_krb5 || !realm_of_user[0] ) {
2910 if ((rc = (*pkrb_get_tf_realm)((*ptkt_string)(), realm_of_user)) != KSUCCESS)
2919 strcpy(realm_of_cell, afs_realm_of_cell(ctx, &ak_cellconfig));
2921 if (strlen(service) == 0)
2922 strcpy(ServiceName, "afs");
2924 strcpy(ServiceName, service);
2926 if (strlen(cell) == 0)
2927 strcpy(CellName, local_cell);
2929 strcpy(CellName, cell);
2931 /* This is for Kerberos v4 only */
2932 if (strlen(realm) == 0)
2933 strcpy(RealmName, realm_of_cell);
2935 strcpy(RealmName, realm);
2937 memset(&creds, '\0', sizeof(creds));
2941 code = KRB5KRB_ERR_GENERIC;
2944 increds.client = client_principal;
2945 increds.times.endtime = 0;
2946 /* Ask for DES since that is what V4 understands */
2947 increds.keyblock.enctype = ENCTYPE_DES_CBC_CRC;
2949 /* If there was a specific realm we are supposed to try
2952 if (strlen(realm) != 0) {
2953 /* service/cell@REALM */
2955 code = pkrb5_build_principal(ctx, &increds.server,
2961 if ( IsDebuggerPresent() ) {
2962 char * cname, *sname;
2963 pkrb5_unparse_name(ctx, increds.client, &cname);
2964 pkrb5_unparse_name(ctx, increds.server, &sname);
2965 OutputDebugString("Getting tickets for \"");
2966 OutputDebugString(cname);
2967 OutputDebugString("\" and service \"");
2968 OutputDebugString(sname);
2969 OutputDebugString("\"\n");
2970 pkrb5_free_unparsed_name(ctx,cname);
2971 pkrb5_free_unparsed_name(ctx,sname);
2975 code = pkrb5_get_credentials(ctx, 0, cc, &increds, &k5creds);
2977 if (code == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN ||
2978 code == KRB5_ERR_HOST_REALM_UNKNOWN ||
2979 code == KRB5KRB_ERR_GENERIC /* heimdal */ ||
2980 code == KRB5KRB_AP_ERR_MSG_TYPE) {
2981 /* Or service@REALM */
2982 pkrb5_free_principal(ctx,increds.server);
2984 code = pkrb5_build_principal(ctx, &increds.server,
2990 if ( IsDebuggerPresent() ) {
2991 char * cname, *sname;
2992 pkrb5_unparse_name(ctx, increds.client, &cname);
2993 pkrb5_unparse_name(ctx, increds.server, &sname);
2994 OutputDebugString("krb5_get_credentials() returned Service Principal Unknown\n");
2995 OutputDebugString("Trying again: getting tickets for \"");
2996 OutputDebugString(cname);
2997 OutputDebugString("\" and service \"");
2998 OutputDebugString(sname);
2999 OutputDebugString("\"\n");
3000 pkrb5_free_unparsed_name(ctx,cname);
3001 pkrb5_free_unparsed_name(ctx,sname);
3005 code = pkrb5_get_credentials(ctx, 0, cc, &increds, &k5creds);
3009 /* we have a local realm for the cell */
3010 strcpy(realm_of_cell, realm);
3013 /* Otherwise, first try service/cell@CLIENT_REALM */
3014 if (code = pkrb5_build_principal(ctx, &increds.server,
3015 (int)strlen(realm_of_user),
3024 if ( IsDebuggerPresent() ) {
3025 char * cname, *sname;
3026 pkrb5_unparse_name(ctx, increds.client, &cname);
3027 pkrb5_unparse_name(ctx, increds.server, &sname);
3028 OutputDebugString("Getting tickets for \"");
3029 OutputDebugString(cname);
3030 OutputDebugString("\" and service \"");
3031 OutputDebugString(sname);
3032 OutputDebugString("\"\n");
3033 pkrb5_free_unparsed_name(ctx,cname);
3034 pkrb5_free_unparsed_name(ctx,sname);
3037 code = pkrb5_get_credentials(ctx, 0, cc, &increds, &k5creds);
3039 /* The client's realm is a local realm for the cell.
3040 * Save it so that later the pts registration will not
3043 strcpy(realm_of_cell, realm_of_user);
3046 if ((code == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN ||
3047 code == KRB5_ERR_HOST_REALM_UNKNOWN ||
3048 code == KRB5KRB_ERR_GENERIC /* heimdal */ ||
3049 code == KRB5KRB_AP_ERR_MSG_TYPE) &&
3050 strcmp(realm_of_user, realm_of_cell)) {
3051 /* Then service/cell@CELL_REALM */
3052 pkrb5_free_principal(ctx,increds.server);
3054 code = pkrb5_build_principal(ctx, &increds.server,
3055 (int)strlen(realm_of_cell),
3060 if ( IsDebuggerPresent() ) {
3061 char * cname, *sname;
3062 pkrb5_unparse_name(ctx, increds.client, &cname);
3063 pkrb5_unparse_name(ctx, increds.server, &sname);
3064 OutputDebugString("krb5_get_credentials() returned Service Principal Unknown\n");
3065 OutputDebugString("Trying again: getting tickets for \"");
3066 OutputDebugString(cname);
3067 OutputDebugString("\" and service \"");
3068 OutputDebugString(sname);
3069 OutputDebugString("\"\n");
3070 pkrb5_free_unparsed_name(ctx,cname);
3071 pkrb5_free_unparsed_name(ctx,sname);
3075 code = pkrb5_get_credentials(ctx, 0, cc, &increds, &k5creds);
3077 if (!code && !strlen(realm_of_cell))
3078 copy_realm_of_ticket(ctx, realm_of_cell, sizeof(realm_of_cell), k5creds);
3081 if (code == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN ||
3082 code == KRB5_ERR_HOST_REALM_UNKNOWN ||
3083 code == KRB5KRB_ERR_GENERIC /* heimdal */ ||
3084 code == KRB5KRB_AP_ERR_MSG_TYPE) {
3085 /* Finally service@CELL_REALM */
3086 pkrb5_free_principal(ctx,increds.server);
3088 code = pkrb5_build_principal(ctx, &increds.server,
3089 (int)strlen(realm_of_cell),
3094 if ( IsDebuggerPresent() ) {
3095 char * cname, *sname;
3096 pkrb5_unparse_name(ctx, increds.client, &cname);
3097 pkrb5_unparse_name(ctx, increds.server, &sname);
3098 OutputDebugString("krb5_get_credentials() returned Service Principal Unknown\n");
3099 OutputDebugString("Trying again: getting tickets for \"");
3100 OutputDebugString(cname);
3101 OutputDebugString("\" and service \"");
3102 OutputDebugString(sname);
3103 OutputDebugString("\"\n");
3104 pkrb5_free_unparsed_name(ctx,cname);
3105 pkrb5_free_unparsed_name(ctx,sname);
3109 code = pkrb5_get_credentials(ctx, 0, cc, &increds, &k5creds);
3110 if (!code && !strlen(realm_of_cell))
3111 copy_realm_of_ticket(ctx, realm_of_cell, sizeof(realm_of_cell), k5creds);
3116 if ( IsDebuggerPresent() ) {
3118 sprintf(message,"krb5_get_credentials returns: %d\n",code);
3119 OutputDebugString(message);
3125 /* This code inserts the entire K5 ticket into the token
3126 * No need to perform a krb524 translation which is
3127 * commented out in the code below
3129 if (KFW_use_krb524() ||
3130 k5creds->ticket.length > MAXKTCTICKETLEN)
3133 memset(&aserver, '\0', sizeof(aserver));
3134 strncpy(aserver.name, ServiceName, MAXKTCNAMELEN - 1);
3135 strncpy(aserver.cell, CellName, MAXKTCREALMLEN - 1);
3137 memset(&atoken, '\0', sizeof(atoken));
3138 atoken.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
3139 atoken.startTime = k5creds->times.starttime;
3140 atoken.endTime = k5creds->times.endtime;
3141 memcpy(&atoken.sessionKey, k5creds->keyblock.contents, k5creds->keyblock.length);
3142 atoken.ticketLen = k5creds->ticket.length;
3143 memcpy(atoken.ticket, k5creds->ticket.data, atoken.ticketLen);
3146 rc = ktc_GetToken(&aserver, &btoken, sizeof(btoken), &aclient);
3147 if (rc != 0 && rc != KTC_NOENT && rc != KTC_NOCELL) {
3148 if ( rc == KTC_NOCM && retry < 20 ) {
3151 goto retry_gettoken5;
3156 if (atoken.kvno == btoken.kvno &&
3157 atoken.ticketLen == btoken.ticketLen &&
3158 !memcmp(&atoken.sessionKey, &btoken.sessionKey, sizeof(atoken.sessionKey)) &&
3159 !memcmp(atoken.ticket, btoken.ticket, atoken.ticketLen))
3161 /* Success - Nothing to do */
3165 // * Reset the "aclient" structure before we call ktc_SetToken.
3166 // * This structure was first set by the ktc_GetToken call when
3167 // * we were comparing whether identical tokens already existed.
3169 len = min(k5creds->client->data[0].length,MAXKTCNAMELEN - 1);
3170 strncpy(aclient.name, k5creds->client->data[0].data, len);
3171 aclient.name[len] = '\0';
3173 if ( k5creds->client->length > 1 ) {
3175 strcat(aclient.name, ".");
3176 p = aclient.name + strlen(aclient.name);
3177 len = min(k5creds->client->data[1].length,MAXKTCNAMELEN - (int)strlen(aclient.name) - 1);
3178 strncpy(p, k5creds->client->data[1].data, len);
3181 aclient.instance[0] = '\0';
3183 strcpy(aclient.cell, realm_of_cell);
3185 len = min(k5creds->client->realm.length,(int)strlen(realm_of_cell));
3186 /* For Khimaira, always append the realm name */
3187 if ( 1 /* strncmp(realm_of_cell, k5creds->client->realm.data, len) */ ) {
3189 strcat(aclient.name, "@");
3190 p = aclient.name + strlen(aclient.name);
3191 len = min(k5creds->client->realm.length,MAXKTCNAMELEN - (int)strlen(aclient.name) - 1);
3192 strncpy(p, k5creds->client->realm.data, len);
3196 GetEnvironmentVariable(DO_NOT_REGISTER_VARNAME, NULL, 0);
3197 if (GetLastError() == ERROR_ENVVAR_NOT_FOUND)
3198 ViceIDToUsername(aclient.name, realm_of_user, realm_of_cell, CellName,
3199 &aclient, &aserver, &atoken);
3202 strncpy(aclient.smbname, smbname, sizeof(aclient.smbname));
3203 aclient.smbname[sizeof(aclient.smbname)-1] = '\0';
3205 aclient.smbname[0] = '\0';
3208 rc = ktc_SetToken(&aserver, &atoken, &aclient, (aclient.smbname[0]?AFS_SETTOK_LOGON:0));
3210 goto cleanup; /* We have successfully inserted the token */
3216 /* Otherwise, the ticket could have been too large so try to
3217 * convert using the krb524d running with the KDC
3219 code = pkrb524_convert_creds_kdc(ctx, k5creds, &creds);
3220 pkrb5_free_creds(ctx, k5creds);
3222 if ( IsDebuggerPresent() ) {
3224 sprintf(message,"krb524_convert_creds_kdc returns: %d\n",code);
3225 OutputDebugString(message);
3230 #endif /* USE_KRB524 */
3234 code = (*pkrb_get_cred)(ServiceName, CellName, RealmName, &creds);
3235 if (code == NO_TKT_FIL) {
3236 // if the problem is that we have no krb4 tickets
3237 // do not attempt to continue
3240 if (code != KSUCCESS)
3241 code = (*pkrb_get_cred)(ServiceName, "", RealmName, &creds);
3243 if (code != KSUCCESS)
3245 if ((code = (*pkrb_mk_req)(&ticket, ServiceName, CellName, RealmName, 0)) == KSUCCESS)
3247 if ((code = (*pkrb_get_cred)(ServiceName, CellName, RealmName, &creds)) != KSUCCESS)
3252 else if ((code = (*pkrb_mk_req)(&ticket, ServiceName, "", RealmName, 0)) == KSUCCESS)
3254 if ((code = (*pkrb_get_cred)(ServiceName, "", RealmName, &creds)) != KSUCCESS)
3269 memset(&aserver, '\0', sizeof(aserver));
3270 strncpy(aserver.name, ServiceName, MAXKTCNAMELEN - 1);
3271 strncpy(aserver.cell, CellName, MAXKTCREALMLEN - 1);
3273 memset(&atoken, '\0', sizeof(atoken));
3274 atoken.kvno = creds.kvno;
3275 atoken.startTime = creds.issue_date;
3276 atoken.endTime = creds.issue_date + life_to_time(0,creds.lifetime);
3277 memcpy(&atoken.sessionKey, creds.session, 8);
3278 atoken.ticketLen = creds.ticket_st.length;
3279 memcpy(atoken.ticket, creds.ticket_st.dat, atoken.ticketLen);
3282 rc = ktc_GetToken(&aserver, &btoken, sizeof(btoken), &aclient);
3283 if (rc != 0 && rc != KTC_NOENT && rc != KTC_NOCELL) {
3284 if ( rc == KTC_NOCM && retry < 20 ) {
3287 goto retry_gettoken;
3289 KFW_AFS_error(rc, "ktc_GetToken()");
3294 if (atoken.kvno == btoken.kvno &&
3295 atoken.ticketLen == btoken.ticketLen &&
3296 !memcmp(&atoken.sessionKey, &btoken.sessionKey, sizeof(atoken.sessionKey)) &&
3297 !memcmp(atoken.ticket, btoken.ticket, atoken.ticketLen))
3302 // * Reset the "aclient" structure before we call ktc_SetToken.
3303 // * This structure was first set by the ktc_GetToken call when
3304 // * we were comparing whether identical tokens already existed.
3306 strncpy(aclient.name, creds.pname, MAXKTCNAMELEN - 1);
3309 strncat(aclient.name, ".", MAXKTCNAMELEN - 1);
3310 strncat(aclient.name, creds.pinst, MAXKTCNAMELEN - 1);
3312 strcpy(aclient.instance, "");
3314 strncat(aclient.name, "@", MAXKTCNAMELEN - 1);
3315 strncat(aclient.name, creds.realm, MAXKTCREALMLEN - 1);
3316 aclient.name[MAXKTCREALMLEN-1] = '\0';
3318 strcpy(aclient.cell, CellName);
3320 GetEnvironmentVariable(DO_NOT_REGISTER_VARNAME, NULL, 0);
3321 if (GetLastError() == ERROR_ENVVAR_NOT_FOUND)
3322 ViceIDToUsername(aclient.name, realm_of_user, realm_of_cell, CellName,
3323 &aclient, &aserver, &atoken);
3326 strncpy(aclient.smbname, smbname, sizeof(aclient.smbname));
3327 aclient.smbname[sizeof(aclient.smbname)-1] = '\0';
3329 aclient.smbname[0] = '\0';
3332 if (rc = ktc_SetToken(&aserver, &atoken, &aclient, (aclient.smbname[0]?AFS_SETTOK_LOGON:0)))
3334 KFW_AFS_error(rc, "ktc_SetToken()");
3340 if (client_principal)
3341 pkrb5_free_principal(ctx,client_principal);
3342 /* increds.client == client_principal */
3344 pkrb5_free_principal(ctx,increds.server);
3345 if (cc && (cc != alt_cc))
3346 pkrb5_cc_close(ctx, cc);
3347 if (ctx && (ctx != alt_ctx))
3348 pkrb5_free_context(ctx);
3349 if (ak_cellconfig.linkedCell)
3350 free(ak_cellconfig.linkedCell);
3352 return(rc? rc : code);
3355 /**************************************/
3356 /* afs_realm_of_cell(): */
3357 /**************************************/
3359 afs_realm_of_cell(krb5_context ctx, struct afsconf_cell *cellconfig)
3361 static char krbrlm[REALM_SZ+1]="";
3362 char ** realmlist=NULL;
3368 r = pkrb5_get_host_realm(ctx, cellconfig->hostName[0], &realmlist);
3369 if ( !r && realmlist && realmlist[0] ) {
3370 strcpy(krbrlm, realmlist[0]);
3371 pkrb5_free_host_realm(ctx, realmlist);
3377 char *t = cellconfig->name;
3382 if (islower(c)) c=toupper(c);
3390 /**************************************/
3391 /* KFW_AFS_get_cellconfig(): */
3392 /**************************************/
3394 KFW_AFS_get_cellconfig(char *cell, struct afsconf_cell *cellconfig, char *local_cell)
3397 char newcell[CELL_MAXNAMELEN+1];
3398 char linkedcell[CELL_MAXNAMELEN+1]="";
3400 local_cell[0] = (char)0;
3401 memset(cellconfig, 0, sizeof(*cellconfig));
3403 /* WIN32: cm_GetRootCellName(local_cell) - NOTE: no way to get max chars */
3404 if (rc = cm_GetRootCellName(local_cell))
3409 if (strlen(cell) == 0)
3410 strcpy(cell, local_cell);
3412 /* WIN32: cm_SearchCellFile(cell, pcallback, pdata) */
3413 rc = cm_SearchCellFileEx(cell, newcell, linkedcell, get_cellconfig_callback, (void*)cellconfig);
3414 #ifdef AFS_AFSDB_ENV
3417 rc = cm_SearchCellByDNS(cell, newcell, &ttl, get_cellconfig_callback, (void*)cellconfig);
3422 strcpy(cellconfig->name, newcell);
3424 cellconfig->linkedCell = strdup(linkedcell);
3429 /**************************************/
3430 /* get_cellconfig_callback(): */
3431 /**************************************/
3433 get_cellconfig_callback(void *cellconfig, struct sockaddr_in *addrp, char *namep)
3435 struct afsconf_cell *cc = (struct afsconf_cell *)cellconfig;
3437 cc->hostAddr[cc->numServers] = *addrp;
3438 strcpy(cc->hostName[cc->numServers], namep);
3444 /**************************************/
3445 /* KFW_AFS_error(): */
3446 /**************************************/
3448 KFW_AFS_error(LONG rc, LPCSTR FailedFunctionName)
3451 const char *errText;
3453 // Using AFS defines as error messages for now, until Transarc
3454 // gets back to me with "string" translations of each of these
3456 if (rc == KTC_ERROR)
3457 errText = "KTC_ERROR";
3458 else if (rc == KTC_TOOBIG)
3459 errText = "KTC_TOOBIG";
3460 else if (rc == KTC_INVAL)
3461 errText = "KTC_INVAL";
3462 else if (rc == KTC_NOENT)
3463 errText = "KTC_NOENT";
3464 else if (rc == KTC_PIOCTLFAIL)
3465 errText = "KTC_PIOCTLFAIL";
3466 else if (rc == KTC_NOPIOCTL)
3467 errText = "KTC_NOPIOCTL";
3468 else if (rc == KTC_NOCELL)
3469 errText = "KTC_NOCELL";
3470 else if (rc == KTC_NOCM)
3471 errText = "KTC_NOCM: The service, Transarc AFS Daemon, most likely is not started!";
3473 errText = "Unknown error!";
3475 sprintf(message, "%s (0x%x)\n(%s failed)", errText, rc, FailedFunctionName);
3477 if ( IsDebuggerPresent() ) {
3478 OutputDebugString(message);
3479 OutputDebugString("\n");
3481 MessageBox(NULL, message, "AFS", MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND);
3487 LPSTR lpszMachineName,
3488 LPSTR lpszServiceName,
3489 DWORD *lpdwCurrentState)
3492 SC_HANDLE schSCManager = NULL;
3493 SC_HANDLE schService = NULL;
3494 DWORD fdwDesiredAccess = 0;
3495 SERVICE_STATUS ssServiceStatus = {0};
3498 *lpdwCurrentState = 0;
3500 fdwDesiredAccess = GENERIC_READ;
3502 schSCManager = OpenSCManager(lpszMachineName,
3506 if(schSCManager == NULL)
3508 hr = GetLastError();
3512 schService = OpenService(schSCManager,
3516 if(schService == NULL)
3518 hr = GetLastError();
3522 fRet = QueryServiceStatus(schService,
3527 hr = GetLastError();
3531 *lpdwCurrentState = ssServiceStatus.dwCurrentState;
3535 CloseServiceHandle(schService);
3536 CloseServiceHandle(schSCManager);
3549 for (n = 0; fi[n].func_ptr_var; n++)
3550 *(fi[n].func_ptr_var) = 0;
3551 if (h) FreeLibrary(h);
3556 const char* dll_name,
3558 HINSTANCE* ph, // [out, optional] - DLL handle
3559 int* pindex, // [out, optional] - index of last func loaded (-1 if none)
3560 int cleanup, // cleanup function pointers and unload on error
3561 int go_on, // continue loading even if some functions cannot be loaded
3562 int silent // do not pop-up a system dialog if DLL cannot be loaded
3571 if (pindex) *pindex = -1;
3573 for (n = 0; fi[n].func_ptr_var; n++)
3574 *(fi[n].func_ptr_var) = 0;
3577 em = SetErrorMode(SEM_FAILCRITICALERRORS);
3578 h = LoadLibrary(dll_name);
3586 for (i = 0; (go_on || !error) && (i < n); i++)
3588 void* p = (void*)GetProcAddress(h, fi[i].func_name);
3594 *(fi[i].func_ptr_var) = p;
3597 if (pindex) *pindex = last_i;
3598 if (error && cleanup && !go_on) {
3599 for (i = 0; i < n; i++) {
3600 *(fi[i].func_ptr_var) = 0;
3606 if (error) return 0;
3610 BOOL KFW_probe_kdc(struct afsconf_cell * cellconfig)
3612 krb5_context ctx = NULL;
3613 krb5_ccache cc = NULL;
3614 krb5_error_code code;
3616 const char * realm = NULL;
3617 krb5_principal principal = NULL;