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(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 hAfsTokens = 0;
363 static HINSTANCE hAfsConf = 0;
364 static HINSTANCE hComErr = 0;
365 static HINSTANCE hService = 0;
366 static HINSTANCE hProfile = 0;
367 static HINSTANCE hLeash = 0;
368 static HINSTANCE hCCAPI = 0;
369 static struct principal_ccache_data * princ_cc_data = NULL;
370 static struct cell_principal_map * cell_princ_map = NULL;
375 static int inited = 0;
378 char mutexName[MAX_PATH];
379 HANDLE hMutex = NULL;
381 sprintf(mutexName, "AFS KFW Init pid=%d", getpid());
383 hMutex = CreateMutex( NULL, TRUE, mutexName );
384 if ( GetLastError() == ERROR_ALREADY_EXISTS ) {
385 if ( WaitForSingleObject( hMutex, INFINITE ) != WAIT_OBJECT_0 ) {
391 LoadFuncs(KRB5_DLL, k5_fi, &hKrb5, 0, 1, 0, 0);
392 LoadFuncs(KRB4_DLL, k4_fi, &hKrb4, 0, 1, 0, 0);
393 LoadFuncs(COMERR_DLL, ce_fi, &hComErr, 0, 0, 1, 0);
394 LoadFuncs(SERVICE_DLL, service_fi, &hService, 0, 1, 0, 0);
396 LoadFuncs(SECUR32_DLL, lsa_fi, &hSecur32, 0, 1, 1, 1);
397 #endif /* USE_MS2MIT */
398 LoadFuncs(KRB524_DLL, k524_fi, &hKrb524, 0, 1, 1, 1);
399 LoadFuncs(PROFILE_DLL, profile_fi, &hProfile, 0, 1, 0, 0);
400 LoadFuncs(LEASH_DLL, leash_fi, &hLeash, 0, 1, 0, 0);
401 LoadFuncs(CCAPI_DLL, ccapi_fi, &hCCAPI, 0, 1, 0, 0);
403 if ( KFW_is_available() ) {
404 char rootcell[MAXCELLCHARS+1];
406 KFW_import_windows_lsa();
407 #endif /* USE_MS2MIT */
408 KFW_import_ccache_data();
409 KFW_AFS_renew_expiring_tokens();
411 /* WIN32 NOTE: no way to get max chars */
412 if (!cm_GetRootCellName(rootcell))
413 KFW_AFS_renew_token_for_cell(rootcell);
416 ReleaseMutex(hMutex);
429 FreeLibrary(hProfile);
431 FreeLibrary(hAfsTokens);
433 FreeLibrary(hAfsConf);
435 FreeLibrary(hComErr);
437 FreeLibrary(hService);
440 FreeLibrary(hSecur32);
441 #endif /* USE_MS2MIT */
443 FreeLibrary(hKrb524);
450 static char OpenAFSConfigKeyName[] = "SOFTWARE\\OpenAFS\\Client";
453 KFW_is_available(void)
459 code = RegOpenKeyEx(HKEY_CURRENT_USER, OpenAFSConfigKeyName,
460 0, KEY_QUERY_VALUE, &parmKey);
461 if (code != ERROR_SUCCESS)
462 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, OpenAFSConfigKeyName,
463 0, KEY_QUERY_VALUE, &parmKey);
464 if (code == ERROR_SUCCESS) {
465 len = sizeof(enableKFW);
466 code = RegQueryValueEx(parmKey, "EnableKFW", NULL, NULL,
467 (BYTE *) &enableKFW, &len);
468 if (code != ERROR_SUCCESS) {
471 RegCloseKey (parmKey);
478 if ( hKrb5 && hComErr && hService &&
481 #endif /* USE_MS2MIT */
483 hProfile && hAfsTokens && hAfsConf && hLeash && hCCAPI )
489 KRB5_error(krb5_error_code rc, LPCSTR FailedFunctionName,
490 int FreeContextFlag, krb5_context * ctx,
495 int krb5Error = ((int)(rc & 255));
507 errText = perror_message(rc);
508 _snprintf(message, sizeof(message),
509 "%s\n(Kerberos error %ld)\n\n%s failed",
514 if ( IsDebuggerPresent() )
515 OutputDebugString(message);
517 MessageBox(NULL, message, "Kerberos Five", MB_OK | MB_ICONERROR |
520 if (FreeContextFlag == 1)
522 if (ctx && *ctx != NULL)
524 if (cache && *cache != NULL) {
525 pkrb5_cc_close(*ctx, *cache);
529 pkrb5_free_context(*ctx);
538 KFW_AFS_update_princ_ccache_data(krb5_context ctx, krb5_ccache cc, int lsa)
540 struct principal_ccache_data * next = princ_cc_data;
541 krb5_principal principal = 0;
543 const char * ccname = NULL;
544 krb5_error_code code = 0;
545 krb5_error_code cc_code = 0;
551 if (ctx == 0 || cc == 0)
554 code = pkrb5_cc_get_principal(ctx, cc, &principal);
557 code = pkrb5_unparse_name(ctx, principal, &pname);
558 if ( code ) goto cleanup;
560 ccname = pkrb5_cc_get_name(ctx, cc);
561 if (!ccname) goto cleanup;
563 // Search the existing list to see if we have a match
565 for ( ; next ; next = next->next ) {
566 if ( !strcmp(next->principal,pname) && !strcmp(next->ccache_name, ccname) )
571 // If not, match add a new node to the beginning of the list and assign init it
573 next = (struct principal_ccache_data *) malloc(sizeof(struct principal_ccache_data));
574 next->next = princ_cc_data;
575 princ_cc_data = next;
576 next->principal = _strdup(pname);
577 next->ccache_name = _strdup(ccname);
578 next->from_lsa = lsa;
580 next->expiration_time = 0;
584 flags = 0; // turn off OPENCLOSE mode
585 code = pkrb5_cc_set_flags(ctx, cc, flags);
586 if ( code ) goto cleanup;
588 code = pkrb5_timeofday(ctx, &now);
590 cc_code = pkrb5_cc_start_seq_get(ctx, cc, &cur);
592 while (!(cc_code = pkrb5_cc_next_cred(ctx, cc, &cur, &creds))) {
593 if ( creds.ticket_flags & TKT_FLG_INITIAL ) {
595 // we found the ticket we are looking for
596 // check validity of timestamp
597 // We add a 5 minutes fudge factor to compensate for potential
598 // clock skew errors between the KDC and client OS
600 valid = ((creds.times.starttime > 0) &&
601 now >= (creds.times.starttime - 300) &&
602 now < (creds.times.endtime + 300) &&
603 !(creds.ticket_flags & TKT_FLG_INVALID));
605 if ( next->from_lsa) {
607 next->expiration_time = creds.times.endtime;
609 } else if ( valid ) {
611 next->expiration_time = creds.times.endtime;
612 next->renew = (creds.times.renew_till > creds.times.endtime) &&
613 (creds.ticket_flags & TKT_FLG_RENEWABLE);
616 next->expiration_time = 0;
620 pkrb5_free_cred_contents(ctx, &creds);
621 cc_code = KRB5_CC_END;
624 pkrb5_free_cred_contents(ctx, &creds);
627 if (cc_code == KRB5_CC_END) {
628 code = pkrb5_cc_end_seq_get(ctx, cc, &cur);
629 if (code) goto cleanup;
633 flags = KRB5_TC_OPENCLOSE; //turn on OPENCLOSE
634 code = pkrb5_cc_set_flags(ctx, cc, flags);
637 pkrb5_free_unparsed_name(ctx,pname);
639 pkrb5_free_principal(ctx,principal);
643 KFW_AFS_find_ccache_for_principal(krb5_context ctx, char * principal, char **ccache, int valid_only)
645 struct principal_ccache_data * next = princ_cc_data;
646 char * response = NULL;
648 if ( !principal || !ccache )
652 if ( (!valid_only || !next->expired) && !strcmp(next->principal,principal) ) {
654 // we always want to prefer the MS Kerberos LSA cache or
655 // the cache afscreds created specifically for the principal
656 // if the current entry is either one, drop the previous find
657 if ( next->from_lsa || !strcmp(next->ccache_name,principal) )
660 response = _strdup(next->ccache_name);
661 // MS Kerberos LSA is our best option so use it and quit
662 if ( next->from_lsa )
676 KFW_AFS_delete_princ_ccache_data(krb5_context ctx, char * pname, char * ccname)
678 struct principal_ccache_data ** next = &princ_cc_data;
680 if ( !pname && !ccname )
684 if ( !strcmp((*next)->principal,pname) ||
685 !strcmp((*next)->ccache_name,ccname) ) {
687 free((*next)->principal);
688 free((*next)->ccache_name);
690 (*next) = (*next)->next;
697 KFW_AFS_update_cell_princ_map(krb5_context ctx, char * cell, char *pname, int active)
699 struct cell_principal_map * next = cell_princ_map;
701 // Search the existing list to see if we have a match
703 for ( ; next ; next = next->next ) {
704 if ( !strcmp(next->cell, cell) ) {
705 if ( !strcmp(next->principal,pname) ) {
706 next->active = active;
709 // OpenAFS currently has a restriction of one active token per cell
710 // Therefore, whenever we update the table with a new active cell we
711 // must mark all of the other principal to cell entries as inactive.
719 // If not, match add a new node to the beginning of the list and assign init it
721 next = (struct cell_principal_map *) malloc(sizeof(struct cell_principal_map));
722 next->next = cell_princ_map;
723 cell_princ_map = next;
724 next->principal = _strdup(pname);
725 next->cell = _strdup(cell);
726 next->active = active;
731 KFW_AFS_delete_cell_princ_maps(krb5_context ctx, char * pname, char * cell)
733 struct cell_principal_map ** next = &cell_princ_map;
735 if ( !pname && !cell )
739 if ( !strcmp((*next)->principal,pname) ||
740 !strcmp((*next)->cell,cell) ) {
742 free((*next)->principal);
745 (*next) = (*next)->next;
751 // Returns (if possible) a principal which has been known in
752 // the past to have been used to obtain tokens for the specified
754 // TODO: Attempt to return one which has not yet expired by checking
755 // the principal/ccache data
757 KFW_AFS_find_principals_for_cell(krb5_context ctx, char * cell, char **principals[], int active_only)
759 struct cell_principal_map * next_map = cell_princ_map;
760 const char * princ = NULL;
767 if ( (!active_only || next_map->active) && !strcmp(next_map->cell,cell) ) {
770 next_map = next_map->next;
773 if ( !principals || !count )
776 *principals = (char **) malloc(sizeof(char *) * count);
777 for ( next_map = cell_princ_map, i=0 ; next_map && i<count; next_map = next_map->next )
779 if ( (!active_only || next_map->active) && !strcmp(next_map->cell,cell) ) {
780 (*principals)[i++] = _strdup(next_map->principal);
787 KFW_AFS_find_cells_for_princ(krb5_context ctx, char * pname, char **cells[], int active_only)
790 struct cell_principal_map * next_map = cell_princ_map;
791 const char * princ = NULL;
797 if ( (!active_only || next_map->active) && !strcmp(next_map->principal,pname) ) {
800 next_map = next_map->next;
806 *cells = (char **) malloc(sizeof(char *) * count);
807 for ( next_map = cell_princ_map, i=0 ; next_map && i<count; next_map = next_map->next )
809 if ( (!active_only || next_map->active) && !strcmp(next_map->principal,pname) ) {
810 (*cells)[i++] = _strdup(next_map->cell);
816 /* Given a principal return an existing ccache or create one and return */
818 KFW_get_ccache(krb5_context alt_ctx, krb5_principal principal, krb5_ccache * cc)
823 krb5_error_code code;
825 if (!pkrb5_init_context)
831 code = pkrb5_init_context(&ctx);
832 if (code) goto cleanup;
836 code = pkrb5_unparse_name(ctx, principal, &pname);
837 if (code) goto cleanup;
839 if ( !KFW_AFS_find_ccache_for_principal(ctx,pname,&ccname,TRUE) &&
840 !KFW_AFS_find_ccache_for_principal(ctx,pname,&ccname,FALSE)) {
841 ccname = (char *)malloc(strlen(pname) + 5);
842 sprintf(ccname,"API:%s",pname);
844 code = pkrb5_cc_resolve(ctx, ccname, cc);
846 code = pkrb5_cc_default(ctx, cc);
847 if (code) goto cleanup;
854 pkrb5_free_unparsed_name(ctx,pname);
855 if (ctx && (ctx != alt_ctx))
856 pkrb5_free_context(ctx);
861 // Import Microsoft Credentials into a new MIT ccache
863 KFW_import_windows_lsa(void)
865 krb5_context ctx = 0;
867 krb5_principal princ = 0;
870 krb5_error_code code;
874 if (!pkrb5_init_context)
878 if ( !MSLSA_IsKerberosLogon() )
882 code = pkrb5_init_context(&ctx);
883 if (code) goto cleanup;
885 code = pkrb5_cc_resolve(ctx, LSA_CCNAME, &cc);
886 if (code) goto cleanup;
888 KFW_AFS_update_princ_ccache_data(ctx, cc, TRUE);
890 code = pkrb5_cc_get_principal(ctx, cc, &princ);
891 if ( code ) goto cleanup;
893 code = pkrb5_unparse_name(ctx,princ,&pname);
894 if ( code ) goto cleanup;
896 realm = krb5_princ_realm(ctx, princ);
897 for ( i=0; i<realm->length; i++ ) {
898 cell[i] = tolower(realm->data[i]);
902 code = KFW_AFS_klog(ctx, cc, "afs", cell, realm->data, pLeash_get_default_lifetime(),NULL);
903 if ( IsDebuggerPresent() ) {
905 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
906 OutputDebugString(message);
908 if ( code ) goto cleanup;
910 KFW_AFS_update_cell_princ_map(ctx, cell, pname, TRUE);
914 pkrb5_free_unparsed_name(ctx,pname);
916 pkrb5_free_principal(ctx,princ);
918 pkrb5_cc_close(ctx,cc);
920 pkrb5_free_context(ctx);
922 #endif /* USE_MS2MIT */
924 // If there are existing MIT credentials, copy them to a new
925 // ccache named after the principal
927 // Enumerate all existing MIT ccaches and construct entries
928 // in the principal_ccache table
930 // Enumerate all existing AFS Tokens and construct entries
931 // in the cell_principal table
933 KFW_import_ccache_data(void)
935 krb5_context ctx = 0;
937 krb5_principal principal = 0;
939 krb5_error_code code;
940 krb5_error_code cc_code;
943 struct _infoNC ** pNCi = NULL;
946 if ( !pcc_initialize )
949 if ( IsDebuggerPresent() )
950 OutputDebugString("KFW_import_ccache_data()\n");
952 code = pcc_initialize(&cc_ctx, CC_API_VER_2, NULL, NULL);
953 if (code) goto cleanup;
955 code = pcc_get_NC_info(cc_ctx, &pNCi);
956 if (code) goto cleanup;
958 code = pkrb5_init_context(&ctx);
959 if (code) goto cleanup;
961 for ( i=0; pNCi[i]; i++ ) {
962 if ( pNCi[i]->vers != CC_CRED_V5 )
964 if ( IsDebuggerPresent() ) {
965 OutputDebugString("Principal: ");
966 OutputDebugString(pNCi[i]->principal);
967 OutputDebugString(" in ccache ");
968 OutputDebugString(pNCi[i]->name);
969 OutputDebugString("\n");
971 if ( strcmp(pNCi[i]->name,pNCi[i]->principal)
972 && strcmp(pNCi[i]->name,LSA_CCNAME)
975 for ( j=0; pNCi[j]; j++ ) {
976 if (!strcmp(pNCi[j]->name,pNCi[i]->principal)) {
982 code = pkrb5_cc_resolve(ctx, pNCi[i]->principal, &cc);
983 if (code) goto loop_cleanup;
986 krb5_ccache oldcc = 0;
988 if ( IsDebuggerPresent() )
989 OutputDebugString("copying ccache data to new ccache\n");
991 code = pkrb5_parse_name(ctx, pNCi[i]->principal, &principal);
992 if (code) goto loop_cleanup;
993 code = pkrb5_cc_initialize(ctx, cc, principal);
994 if (code) goto loop_cleanup;
996 code = pkrb5_cc_resolve(ctx, pNCi[i]->name, &oldcc);
997 if (code) goto loop_cleanup;
998 code = pkrb5_cc_copy_creds(ctx,oldcc,cc);
1000 code = pkrb5_cc_close(ctx,cc);
1002 code = pkrb5_cc_close(ctx,oldcc);
1004 KRB5_error(code, "krb5_cc_copy_creds", 0, NULL, NULL);
1007 code = pkrb5_cc_close(ctx,oldcc);
1010 code = pkrb5_cc_resolve(ctx, pNCi[i]->name, &cc);
1011 if (code) goto loop_cleanup;
1014 flags = 0; // turn off OPENCLOSE mode
1015 code = pkrb5_cc_set_flags(ctx, cc, flags);
1016 if ( code ) goto cleanup;
1018 KFW_AFS_update_princ_ccache_data(ctx, cc, !strcmp(pNCi[i]->name,LSA_CCNAME));
1020 cc_code = pkrb5_cc_start_seq_get(ctx, cc, &cur);
1022 while (!(cc_code = pkrb5_cc_next_cred(ctx, cc, &cur, &creds))) {
1023 krb5_data * sname = krb5_princ_name(ctx, creds.server);
1024 krb5_data * cell = krb5_princ_component(ctx, creds.server, 1);
1025 krb5_data * realm = krb5_princ_realm(ctx, creds.server);
1026 if ( sname && cell && !strcmp("afs",sname->data) ) {
1027 struct ktc_principal aserver;
1028 struct ktc_principal aclient;
1029 struct ktc_token atoken;
1032 if ( IsDebuggerPresent() ) {
1033 OutputDebugString("Found AFS ticket: ");
1034 OutputDebugString(sname->data);
1036 OutputDebugString("/");
1037 OutputDebugString(cell->data);
1039 OutputDebugString("@");
1040 OutputDebugString(realm->data);
1041 OutputDebugString("\n");
1044 memset(&aserver, '\0', sizeof(aserver));
1045 strcpy(aserver.name, sname->data);
1046 strcpy(aserver.cell, cell->data);
1048 code = ktc_GetToken(&aserver, &atoken, sizeof(atoken), &aclient);
1050 // Found a token in AFS Client Server which matches
1051 char pname[128], *p, *q;
1052 for ( p=pname, q=aclient.name; *q; p++, q++)
1054 for ( *p++ = '@', q=aclient.cell; *q; p++, q++)
1058 if ( IsDebuggerPresent() ) {
1059 OutputDebugString("Found AFS token: ");
1060 OutputDebugString(pname);
1061 OutputDebugString("\n");
1064 if ( strcmp(pname,pNCi[i]->principal) )
1066 KFW_AFS_update_cell_princ_map(ctx, cell->data, pNCi[i]->principal, active);
1068 // Attempt to import it
1069 KFW_AFS_update_cell_princ_map(ctx, cell->data, pNCi[i]->principal, active);
1071 if ( IsDebuggerPresent() ) {
1072 OutputDebugString("Calling KFW_AFS_klog() to obtain token\n");
1075 code = KFW_AFS_klog(ctx, cc, "afs", cell->data, realm->data, pLeash_get_default_lifetime(),NULL);
1076 if ( IsDebuggerPresent() ) {
1078 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
1079 OutputDebugString(message);
1082 } else if ( IsDebuggerPresent() ) {
1083 OutputDebugString("Found ticket: ");
1084 OutputDebugString(sname->data);
1085 if ( cell && cell->data ) {
1086 OutputDebugString("/");
1087 OutputDebugString(cell->data);
1089 OutputDebugString("@");
1090 OutputDebugString(realm->data);
1091 OutputDebugString("\n");
1093 pkrb5_free_cred_contents(ctx, &creds);
1096 if (cc_code == KRB5_CC_END) {
1097 cc_code = pkrb5_cc_end_seq_get(ctx, cc, &cur);
1098 if (cc_code) goto loop_cleanup;
1102 flags = KRB5_TC_OPENCLOSE; //turn on OPENCLOSE
1103 code = pkrb5_cc_set_flags(ctx, cc, flags);
1105 pkrb5_cc_close(ctx,cc);
1109 pkrb5_free_principal(ctx,principal);
1116 pkrb5_free_context(ctx);
1118 pcc_free_NC_info(cc_ctx, &pNCi);
1120 pcc_shutdown(&cc_ctx);
1125 KFW_AFS_get_cred(char * username,
1132 krb5_context ctx = 0;
1135 char ** realmlist = 0;
1136 krb5_principal principal = 0;
1138 krb5_error_code code;
1139 char local_cell[MAXCELLCHARS+1];
1140 char **cells = NULL;
1142 struct afsconf_cell cellconfig;
1144 if (!pkrb5_init_context)
1147 if ( IsDebuggerPresent() ) {
1148 OutputDebugString("KFW_AFS_get_cred for token ");
1149 OutputDebugString(username);
1150 OutputDebugString(" in cell ");
1151 OutputDebugString(cell);
1152 OutputDebugString("\n");
1155 code = pkrb5_init_context(&ctx);
1156 if ( code ) goto cleanup;
1158 code = KFW_AFS_get_cellconfig( cell, (void*)&cellconfig, local_cell);
1159 if ( code ) goto cleanup;
1161 realm = strchr(username,'@');
1166 if ( !realm || !realm[0] )
1167 realm = afs_realm_of_cell(&cellconfig); // do not free
1169 if ( IsDebuggerPresent() ) {
1170 OutputDebugString("Realm: ");
1171 OutputDebugString(realm);
1172 OutputDebugString("\n");
1175 code = pkrb5_build_principal(ctx, &principal, strlen(realm),
1180 code = KFW_get_ccache(ctx, principal, &cc);
1181 if ( code ) goto cleanup;
1183 code = pkrb5_unparse_name(ctx, principal, &pname);
1184 if ( code ) goto cleanup;
1186 if ( lifetime == 0 )
1187 lifetime = pLeash_get_default_lifetime();
1189 code = KFW_kinit(ctx, cc, HWND_DESKTOP,
1193 pLeash_get_default_forwardable(),
1194 pLeash_get_default_proxiable(),
1195 pLeash_get_default_renewable() ? pLeash_get_default_renew_till() : 0,
1196 pLeash_get_default_noaddresses(),
1197 pLeash_get_default_publicip());
1198 if ( IsDebuggerPresent() ) {
1200 sprintf(message,"KFW_kinit() returns: %d\n",code);
1201 OutputDebugString(message);
1203 if ( code ) goto cleanup;
1205 KFW_AFS_update_princ_ccache_data(ctx, cc, FALSE);
1207 code = KFW_AFS_klog(ctx, cc, "afs", cell, realm, lifetime,smbname);
1208 if ( IsDebuggerPresent() ) {
1210 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
1211 OutputDebugString(message);
1213 if ( code ) goto cleanup;
1215 KFW_AFS_update_cell_princ_map(ctx, cell, pname, TRUE);
1217 // Attempt to obtain new tokens for other cells supported by the same
1219 cell_count = KFW_AFS_find_cells_for_princ(ctx, pname, &cells, TRUE);
1220 if ( cell_count > 1 ) {
1221 while ( cell_count-- ) {
1222 if ( strcmp(cells[cell_count],cell) ) {
1223 if ( IsDebuggerPresent() ) {
1225 sprintf(message,"found another cell for the same principal: %s\n",cell);
1226 OutputDebugString(message);
1228 code = KFW_AFS_get_cellconfig( cells[cell_count], (void*)&cellconfig, local_cell);
1229 if ( code ) continue;
1231 realm = afs_realm_of_cell(&cellconfig); // do not free
1232 if ( IsDebuggerPresent() ) {
1233 OutputDebugString("Realm: ");
1234 OutputDebugString(realm);
1235 OutputDebugString("\n");
1238 code = KFW_AFS_klog(ctx, cc, "afs", cells[cell_count], realm, lifetime,smbname);
1239 if ( IsDebuggerPresent() ) {
1241 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
1242 OutputDebugString(message);
1245 free(cells[cell_count]);
1248 } else if ( cell_count == 1 ) {
1255 pkrb5_free_unparsed_name(ctx,pname);
1257 pkrb5_cc_close(ctx, cc);
1259 if ( code && reasonP ) {
1260 *reasonP = (char *)perror_message(code);
1266 KFW_AFS_destroy_tickets_for_cell(char * cell)
1268 krb5_context ctx = 0;
1269 krb5_error_code code;
1271 char ** principals = NULL;
1273 if (!pkrb5_init_context)
1276 if ( IsDebuggerPresent() ) {
1277 OutputDebugString("KFW_AFS_destroy_ticets_for_cell: ");
1278 OutputDebugString(cell);
1279 OutputDebugString("\n");
1282 code = pkrb5_init_context(&ctx);
1285 count = KFW_AFS_find_principals_for_cell(ctx, cell, &principals, FALSE);
1287 krb5_principal princ = 0;
1291 int cell_count = KFW_AFS_find_cells_for_princ(ctx, principals[count], NULL, TRUE);
1292 if ( cell_count > 1 ) {
1293 // TODO - What we really should do here is verify whether or not any of the
1294 // other cells which use this principal to obtain its credentials actually
1295 // have valid tokens or not. If they are currently using these credentials
1296 // we will skip them. For the time being we assume that if there is an active
1297 // map in the table that they are actively being used.
1301 code = pkrb5_parse_name(ctx, principals[count], &princ);
1302 if (code) goto loop_cleanup;
1304 code = KFW_get_ccache(ctx, princ, &cc);
1305 if (code) goto loop_cleanup;
1307 code = pkrb5_cc_destroy(ctx, cc);
1312 pkrb5_cc_close(ctx, cc);
1316 pkrb5_free_principal(ctx, princ);
1320 KFW_AFS_update_cell_princ_map(ctx, cell, principals[count], FALSE);
1321 free(principals[count]);
1325 pkrb5_free_context(ctx);
1330 KFW_AFS_renew_expiring_tokens(void)
1332 krb5_error_code code = 0;
1333 krb5_context ctx = 0;
1336 struct principal_ccache_data * pcc_next = princ_cc_data;
1339 const char * realm = NULL;
1340 char local_cell[MAXCELLCHARS+1]="";
1341 struct afsconf_cell cellconfig;
1343 if (!pkrb5_init_context)
1346 if ( pcc_next == NULL ) // nothing to do
1349 if ( IsDebuggerPresent() ) {
1350 OutputDebugString("KFW_AFS_renew_expiring_tokens\n");
1353 code = pkrb5_init_context(&ctx);
1354 if (code) goto cleanup;
1356 code = pkrb5_timeofday(ctx, &now);
1357 if (code) goto cleanup;
1359 for ( ; pcc_next ; pcc_next = pcc_next->next ) {
1360 if ( pcc_next->expired )
1363 if ( now >= (pcc_next->expiration_time) ) {
1364 if ( !pcc_next->from_lsa ) {
1365 pcc_next->expired = 1;
1370 if ( pcc_next->renew && now >= (pcc_next->expiration_time - cminRENEW * csec1MINUTE) ) {
1371 code = pkrb5_cc_resolve(ctx, pcc_next->ccache_name, &cc);
1374 code = KFW_renew(ctx,cc);
1376 if ( code && pcc_next->from_lsa)
1378 #endif /* USE_MS2MIT */
1381 KFW_AFS_update_princ_ccache_data(ctx, cc, pcc_next->from_lsa);
1382 if (code) goto loop_cleanup;
1384 // Attempt to obtain new tokens for other cells supported by the same
1386 cell_count = KFW_AFS_find_cells_for_princ(ctx, pcc_next->principal, &cells, TRUE);
1387 if ( cell_count > 0 ) {
1388 while ( cell_count-- ) {
1389 if ( IsDebuggerPresent() ) {
1390 OutputDebugString("Cell: ");
1391 OutputDebugString(cells[cell_count]);
1392 OutputDebugString("\n");
1394 code = KFW_AFS_get_cellconfig( cells[cell_count], (void*)&cellconfig, local_cell);
1395 if ( code ) continue;
1396 realm = afs_realm_of_cell(&cellconfig); // do not free
1397 if ( IsDebuggerPresent() ) {
1398 OutputDebugString("Realm: ");
1399 OutputDebugString(realm);
1400 OutputDebugString("\n");
1402 code = KFW_AFS_klog(ctx, cc, "afs", cells[cell_count], (char *)realm, pLeash_get_default_lifetime(),NULL);
1403 if ( IsDebuggerPresent() ) {
1405 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
1406 OutputDebugString(message);
1408 free(cells[cell_count]);
1416 pkrb5_cc_close(ctx,cc);
1423 pkrb5_cc_close(ctx,cc);
1425 pkrb5_free_context(ctx);
1432 KFW_AFS_renew_token_for_cell(char * cell)
1434 krb5_error_code code = 0;
1435 krb5_context ctx = 0;
1437 char ** principals = NULL;
1439 if (!pkrb5_init_context)
1442 if ( IsDebuggerPresent() ) {
1443 OutputDebugString("KFW_AFS_renew_token_for_cell:");
1444 OutputDebugString(cell);
1445 OutputDebugString("\n");
1448 code = pkrb5_init_context(&ctx);
1449 if (code) goto cleanup;
1451 count = KFW_AFS_find_principals_for_cell(ctx, cell, &principals, TRUE);
1453 // We know we must have a credential somewhere since we are
1454 // trying to renew a token
1456 KFW_import_ccache_data();
1457 count = KFW_AFS_find_principals_for_cell(ctx, cell, &principals, TRUE);
1460 krb5_principal princ = 0;
1461 krb5_principal service = 0;
1463 krb5_creds mcreds, creds;
1464 #endif /* COMMENT */
1466 const char * realm = NULL;
1467 struct afsconf_cell cellconfig;
1468 char local_cell[MAXCELLCHARS+1];
1471 code = pkrb5_parse_name(ctx, principals[count], &princ);
1472 if (code) goto loop_cleanup;
1474 code = KFW_get_ccache(ctx, princ, &cc);
1475 if (code) goto loop_cleanup;
1477 code = KFW_AFS_get_cellconfig( cell, (void*)&cellconfig, local_cell);
1478 if ( code ) goto loop_cleanup;
1480 realm = afs_realm_of_cell(&cellconfig); // do not free
1481 if ( IsDebuggerPresent() ) {
1482 OutputDebugString("Realm: ");
1483 OutputDebugString(realm);
1484 OutputDebugString("\n");
1488 /* krb5_cc_remove_cred() is not implemented
1491 code = pkrb5_build_principal(ctx, &service, strlen(realm),
1492 realm, "afs", cell, NULL);
1494 memset(&mcreds, 0, sizeof(krb5_creds));
1495 mcreds.client = princ;
1496 mcreds.server = service;
1498 code = pkrb5_cc_retrieve_cred(ctx, cc, 0, &mcreds, &creds);
1500 if ( IsDebuggerPresent() ) {
1501 char * cname, *sname;
1502 pkrb5_unparse_name(ctx, creds.client, &cname);
1503 pkrb5_unparse_name(ctx, creds.server, &sname);
1504 OutputDebugString("Removing credential for client \"");
1505 OutputDebugString(cname);
1506 OutputDebugString("\" and service \"");
1507 OutputDebugString(sname);
1508 OutputDebugString("\"\n");
1509 pkrb5_free_unparsed_name(ctx,cname);
1510 pkrb5_free_unparsed_name(ctx,sname);
1513 code = pkrb5_cc_remove_cred(ctx, cc, 0, &creds);
1514 pkrb5_free_principal(ctx, creds.client);
1515 pkrb5_free_principal(ctx, creds.server);
1518 #endif /* COMMENT */
1520 code = KFW_AFS_klog(ctx, cc, "afs", cell, (char *)realm, pLeash_get_default_lifetime(),NULL);
1521 if ( IsDebuggerPresent() ) {
1523 sprintf(message,"KFW_AFS_klog() returns: %d\n",code);
1524 OutputDebugString(message);
1529 pkrb5_cc_close(ctx, cc);
1533 pkrb5_free_principal(ctx, princ);
1537 pkrb5_free_principal(ctx, service);
1541 KFW_AFS_update_cell_princ_map(ctx, cell, principals[count], code ? FALSE : TRUE);
1542 free(principals[count]);
1546 code = -1; // we did not renew the tokens
1549 pkrb5_free_context(ctx);
1550 return (code ? FALSE : TRUE);
1555 KFW_AFS_renew_tokens_for_all_cells(void)
1557 struct cell_principal_map * next = cell_princ_map;
1559 if ( IsDebuggerPresent() )
1560 OutputDebugString("KFW_AFS_renew_tokens_for_all()\n");
1565 for ( ; next ; next = next->next ) {
1567 KFW_AFS_renew_token_for_cell(next->cell);
1573 KFW_renew(krb5_context alt_ctx, krb5_ccache alt_cc)
1575 krb5_error_code code = 0;
1576 krb5_context ctx = 0;
1578 krb5_principal me = 0;
1579 krb5_principal server = 0;
1580 krb5_creds my_creds;
1581 krb5_data *realm = 0;
1583 if (!pkrb5_init_context)
1586 memset(&my_creds, 0, sizeof(krb5_creds));
1591 code = pkrb5_init_context(&ctx);
1592 if (code) goto cleanup;
1598 code = pkrb5_cc_default(ctx, &cc);
1599 if (code) goto cleanup;
1602 code = pkrb5_cc_get_principal(ctx, cc, &me);
1603 if (code) goto cleanup;
1605 realm = krb5_princ_realm(ctx, me);
1607 code = pkrb5_build_principal_ext(ctx, &server,
1608 realm->length,realm->data,
1609 KRB5_TGS_NAME_SIZE, KRB5_TGS_NAME,
1610 realm->length,realm->data,
1615 if ( IsDebuggerPresent() ) {
1616 char * cname, *sname;
1617 pkrb5_unparse_name(ctx, me, &cname);
1618 pkrb5_unparse_name(ctx, server, &sname);
1619 OutputDebugString("Renewing credential for client \"");
1620 OutputDebugString(cname);
1621 OutputDebugString("\" and service \"");
1622 OutputDebugString(sname);
1623 OutputDebugString("\"\n");
1624 pkrb5_free_unparsed_name(ctx,cname);
1625 pkrb5_free_unparsed_name(ctx,sname);
1628 my_creds.client = me;
1629 my_creds.server = server;
1631 code = pkrb5_get_renewed_creds(ctx, &my_creds, me, cc, NULL);
1633 if ( IsDebuggerPresent() ) {
1635 sprintf(message,"krb5_get_renewed_creds() failed: %d\n",code);
1636 OutputDebugString(message);
1641 code = pkrb5_cc_initialize(ctx, cc, me);
1643 if ( IsDebuggerPresent() ) {
1645 sprintf(message,"krb5_cc_initialize() failed: %d\n",code);
1646 OutputDebugString(message);
1651 code = pkrb5_cc_store_cred(ctx, cc, &my_creds);
1653 if ( IsDebuggerPresent() ) {
1655 sprintf(message,"krb5_cc_store_cred() failed: %d\n",code);
1656 OutputDebugString(message);
1662 if (my_creds.client == me)
1663 my_creds.client = 0;
1664 if (my_creds.server == server)
1665 my_creds.server = 0;
1666 pkrb5_free_cred_contents(ctx, &my_creds);
1668 pkrb5_free_principal(ctx, me);
1670 pkrb5_free_principal(ctx, server);
1671 if (cc && (cc != alt_cc))
1672 pkrb5_cc_close(ctx, cc);
1673 if (ctx && (ctx != alt_ctx))
1674 pkrb5_free_context(ctx);
1679 KFW_kinit( krb5_context alt_ctx,
1682 char *principal_name,
1684 krb5_deltat lifetime,
1687 krb5_deltat renew_life,
1692 krb5_error_code code = 0;
1693 krb5_context ctx = 0;
1695 krb5_principal me = 0;
1697 krb5_creds my_creds;
1698 krb5_get_init_creds_opt options;
1699 krb5_address ** addrs = NULL;
1700 int i = 0, addr_count = 0;
1702 if (!pkrb5_init_context)
1705 pkrb5_get_init_creds_opt_init(&options);
1706 memset(&my_creds, 0, sizeof(my_creds));
1714 code = pkrb5_init_context(&ctx);
1715 if (code) goto cleanup;
1721 code = pkrb5_cc_default(ctx, &cc);
1722 if (code) goto cleanup;
1725 code = pkrb5_parse_name(ctx, principal_name, &me);
1729 code = pkrb5_unparse_name(ctx, me, &name);
1734 lifetime = pLeash_get_default_lifetime();
1742 pkrb5_get_init_creds_opt_set_tkt_life(&options, lifetime);
1743 pkrb5_get_init_creds_opt_set_forwardable(&options,
1744 forwardable ? 1 : 0);
1745 pkrb5_get_init_creds_opt_set_proxiable(&options,
1747 pkrb5_get_init_creds_opt_set_renew_life(&options,
1750 pkrb5_get_init_creds_opt_set_address_list(&options,NULL);
1754 // we are going to add the public IP address specified by the user
1755 // to the list provided by the operating system
1756 krb5_address ** local_addrs=NULL;
1759 pkrb5_os_localaddr(ctx, &local_addrs);
1760 while ( local_addrs[i++] );
1763 addrs = (krb5_address **) malloc((addr_count+1) * sizeof(krb5_address *));
1765 pkrb5_free_addresses(ctx, local_addrs);
1768 memset(addrs, 0, sizeof(krb5_address *) * (addr_count+1));
1770 while ( local_addrs[i] ) {
1771 addrs[i] = (krb5_address *)malloc(sizeof(krb5_address));
1772 if (addrs[i] == NULL) {
1773 pkrb5_free_addresses(ctx, local_addrs);
1777 addrs[i]->magic = local_addrs[i]->magic;
1778 addrs[i]->addrtype = local_addrs[i]->addrtype;
1779 addrs[i]->length = local_addrs[i]->length;
1780 addrs[i]->contents = (unsigned char *)malloc(addrs[i]->length);
1781 if (!addrs[i]->contents) {
1782 pkrb5_free_addresses(ctx, local_addrs);
1786 memcpy(addrs[i]->contents,local_addrs[i]->contents,
1787 local_addrs[i]->length); /* safe */
1790 pkrb5_free_addresses(ctx, local_addrs);
1792 addrs[i] = (krb5_address *)malloc(sizeof(krb5_address));
1793 if (addrs[i] == NULL)
1796 addrs[i]->magic = KV5M_ADDRESS;
1797 addrs[i]->addrtype = AF_INET;
1798 addrs[i]->length = 4;
1799 addrs[i]->contents = (unsigned char *)malloc(addrs[i]->length);
1800 if (!addrs[i]->contents)
1803 netIPAddr = htonl(publicIP);
1804 memcpy(addrs[i]->contents,&netIPAddr,4);
1806 pkrb5_get_init_creds_opt_set_address_list(&options,addrs);
1811 code = pkrb5_get_init_creds_password(ctx,
1814 password, // password
1815 KRB5_prompter, // prompter
1816 hParent, // prompter data
1823 code = pkrb5_cc_initialize(ctx, cc, me);
1827 code = pkrb5_cc_store_cred(ctx, cc, &my_creds);
1833 for ( i=0;i<addr_count;i++ ) {
1835 if ( addrs[i]->contents )
1836 free(addrs[i]->contents);
1841 if (my_creds.client == me)
1842 my_creds.client = 0;
1843 pkrb5_free_cred_contents(ctx, &my_creds);
1845 pkrb5_free_unparsed_name(ctx, name);
1847 pkrb5_free_principal(ctx, me);
1848 if (cc && (cc != alt_cc))
1849 pkrb5_cc_close(ctx, cc);
1850 if (ctx && (ctx != alt_ctx))
1851 pkrb5_free_context(ctx);
1857 KFW_kdestroy(krb5_context alt_ctx, krb5_ccache alt_cc)
1861 krb5_error_code code;
1863 if (!pkrb5_init_context)
1872 code = pkrb5_init_context(&ctx);
1873 if (code) goto cleanup;
1879 code = pkrb5_cc_default(ctx, &cc);
1880 if (code) goto cleanup;
1883 code = pkrb5_cc_destroy(ctx, cc);
1884 if ( !code ) cc = 0;
1887 if (cc && (cc != alt_cc))
1888 pkrb5_cc_close(ctx, cc);
1889 if (ctx && (ctx != alt_ctx))
1890 pkrb5_free_context(ctx);
1898 GetSecurityLogonSessionData(PSECURITY_LOGON_SESSION_DATA * ppSessionData)
1900 NTSTATUS Status = 0;
1902 TOKEN_STATISTICS Stats;
1908 *ppSessionData = NULL;
1910 Success = OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &TokenHandle );
1914 Success = GetTokenInformation( TokenHandle, TokenStatistics, &Stats, sizeof(TOKEN_STATISTICS), &ReqLen );
1915 CloseHandle( TokenHandle );
1919 Status = pLsaGetLogonSessionData( &Stats.AuthenticationId, ppSessionData );
1920 if ( FAILED(Status) || !ppSessionData )
1927 // MSLSA_IsKerberosLogon() does not validate whether or not there are valid tickets in the
1928 // cache. It validates whether or not it is reasonable to assume that if we
1929 // attempted to retrieve valid tickets we could do so. Microsoft does not
1930 // automatically renew expired tickets. Therefore, the cache could contain
1931 // expired or invalid tickets. Microsoft also caches the user's password
1932 // and will use it to retrieve new TGTs if the cache is empty and tickets
1936 MSLSA_IsKerberosLogon(VOID)
1938 PSECURITY_LOGON_SESSION_DATA pSessionData = NULL;
1939 BOOL Success = FALSE;
1941 if ( GetSecurityLogonSessionData(&pSessionData) ) {
1942 if ( pSessionData->AuthenticationPackage.Buffer ) {
1948 usBuffer = (pSessionData->AuthenticationPackage).Buffer;
1949 usLength = (pSessionData->AuthenticationPackage).Length;
1952 lstrcpynW (buffer, usBuffer, usLength);
1953 lstrcatW (buffer,L"");
1954 if ( !lstrcmpW(L"Kerberos",buffer) )
1958 pLsaFreeReturnBuffer(pSessionData);
1962 #endif /* USE_MS2MIT */
1964 static BOOL CALLBACK
1965 MultiInputDialogProc( HWND hDialog, UINT message, WPARAM wParam, LPARAM lParam)
1969 switch ( message ) {
1971 if ( GetDlgCtrlID((HWND) wParam) != ID_MID_TEXT )
1973 SetFocus(GetDlgItem( hDialog, ID_MID_TEXT));
1976 for ( i=0; i < mid_cnt ; i++ ) {
1977 if (mid_tb[i].echo == 0)
1978 SendDlgItemMessage(hDialog, ID_MID_TEXT+i, EM_SETPASSWORDCHAR, 32, 0);
1979 else if (mid_tb[i].echo == 2)
1980 SendDlgItemMessage(hDialog, ID_MID_TEXT+i, EM_SETPASSWORDCHAR, '*', 0);
1985 switch ( LOWORD(wParam) ) {
1987 for ( i=0; i < mid_cnt ; i++ ) {
1988 if ( !GetDlgItemText(hDialog, ID_MID_TEXT+i, mid_tb[i].buf, mid_tb[i].len) )
1989 *mid_tb[i].buf = '\0';
1993 EndDialog(hDialog, LOWORD(wParam));
2001 lpwAlign( LPWORD lpIn )
2009 return (LPWORD) ul;;
2013 * dialog widths are measured in 1/4 character widths
2014 * dialog height are measured in 1/8 character heights
2018 MultiInputDialog( HINSTANCE hinst, HWND hwndOwner,
2019 char * ptext[], int numlines, int width,
2020 int tb_cnt, struct textField * tb)
2024 LPDLGITEMTEMPLATE lpdit;
2030 hgbl = GlobalAlloc(GMEM_ZEROINIT, 4096);
2037 lpdt = (LPDLGTEMPLATE)GlobalLock(hgbl);
2039 // Define a dialog box.
2041 lpdt->style = WS_POPUP | WS_BORDER | WS_SYSMENU
2042 | DS_MODALFRAME | WS_CAPTION | DS_CENTER
2043 | DS_SETFOREGROUND | DS_3DLOOK
2044 | DS_SETFONT | DS_FIXEDSYS | DS_NOFAILCREATE;
2045 lpdt->cdit = numlines + (2 * tb_cnt) + 2; // number of controls
2048 lpdt->cx = 20 + width * 4;
2049 lpdt->cy = 20 + (numlines + tb_cnt + 4) * 14;
2051 lpw = (LPWORD) (lpdt + 1);
2052 *lpw++ = 0; // no menu
2053 *lpw++ = 0; // predefined dialog box class (by default)
2055 lpwsz = (LPWSTR) lpw;
2056 nchar = MultiByteToWideChar (CP_ACP, 0, "", -1, lpwsz, 128);
2058 *lpw++ = 8; // font size (points)
2059 lpwsz = (LPWSTR) lpw;
2060 nchar = MultiByteToWideChar (CP_ACP, 0, "MS Shell Dlg",
2064 //-----------------------
2065 // Define an OK button.
2066 //-----------------------
2067 lpw = lpwAlign (lpw); // align DLGITEMTEMPLATE on DWORD boundary
2068 lpdit = (LPDLGITEMTEMPLATE) lpw;
2069 lpdit->style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP | WS_BORDER;
2070 lpdit->dwExtendedStyle = 0;
2071 lpdit->x = (lpdt->cx - 14)/4 - 20;
2072 lpdit->y = 10 + (numlines + tb_cnt + 2) * 14;
2075 lpdit->id = IDOK; // OK button identifier
2077 lpw = (LPWORD) (lpdit + 1);
2079 *lpw++ = 0x0080; // button class
2081 lpwsz = (LPWSTR) lpw;
2082 nchar = MultiByteToWideChar (CP_ACP, 0, "OK", -1, lpwsz, 50);
2084 *lpw++ = 0; // no creation data
2086 //-----------------------
2087 // Define an Cancel button.
2088 //-----------------------
2089 lpw = lpwAlign (lpw); // align DLGITEMTEMPLATE on DWORD boundary
2090 lpdit = (LPDLGITEMTEMPLATE) lpw;
2091 lpdit->style = WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP | WS_BORDER;
2092 lpdit->dwExtendedStyle = 0;
2093 lpdit->x = (lpdt->cx - 14)*3/4 - 20;
2094 lpdit->y = 10 + (numlines + tb_cnt + 2) * 14;
2097 lpdit->id = IDCANCEL; // CANCEL button identifier
2099 lpw = (LPWORD) (lpdit + 1);
2101 *lpw++ = 0x0080; // button class
2103 lpwsz = (LPWSTR) lpw;
2104 nchar = MultiByteToWideChar (CP_ACP, 0, "Cancel", -1, lpwsz, 50);
2106 *lpw++ = 0; // no creation data
2108 /* Add controls for preface data */
2109 for ( i=0; i<numlines; i++) {
2110 /*-----------------------
2111 * Define a static text control.
2112 *-----------------------*/
2113 lpw = lpwAlign (lpw); /* align DLGITEMTEMPLATE on DWORD boundary */
2114 lpdit = (LPDLGITEMTEMPLATE) lpw;
2115 lpdit->style = WS_CHILD | WS_VISIBLE | SS_LEFT;
2116 lpdit->dwExtendedStyle = 0;
2118 lpdit->y = 10 + i * 14;
2119 lpdit->cx = strlen(ptext[i]) * 4 + 10;
2121 lpdit->id = ID_TEXT + i; // text identifier
2123 lpw = (LPWORD) (lpdit + 1);
2125 *lpw++ = 0x0082; // static class
2127 lpwsz = (LPWSTR) lpw;
2128 nchar = MultiByteToWideChar (CP_ACP, 0, ptext[i],
2129 -1, lpwsz, 2*width);
2131 *lpw++ = 0; // no creation data
2134 for ( i=0, pwid = 0; i<tb_cnt; i++) {
2135 if ( pwid < strlen(tb[i].label) )
2136 pwid = strlen(tb[i].label);
2139 for ( i=0; i<tb_cnt; i++) {
2141 /*-----------------------
2142 * Define a static text control.
2143 *-----------------------*/
2144 lpw = lpwAlign (lpw); /* align DLGITEMTEMPLATE on DWORD boundary */
2145 lpdit = (LPDLGITEMTEMPLATE) lpw;
2146 lpdit->style = WS_CHILD | WS_VISIBLE | SS_LEFT;
2147 lpdit->dwExtendedStyle = 0;
2149 lpdit->y = 10 + (numlines + i + 1) * 14;
2150 lpdit->cx = pwid * 4;
2152 lpdit->id = ID_TEXT + numlines + i; // text identifier
2154 lpw = (LPWORD) (lpdit + 1);
2156 *lpw++ = 0x0082; // static class
2158 lpwsz = (LPWSTR) lpw;
2159 nchar = MultiByteToWideChar (CP_ACP, 0, tb[i].label ? tb[i].label : "",
2162 *lpw++ = 0; // no creation data
2164 /*-----------------------
2165 * Define an edit control.
2166 *-----------------------*/
2167 lpw = lpwAlign (lpw); /* align DLGITEMTEMPLATE on DWORD boundary */
2168 lpdit = (LPDLGITEMTEMPLATE) lpw;
2169 lpdit->style = WS_CHILD | WS_VISIBLE | ES_LEFT | WS_TABSTOP | WS_BORDER | (tb[i].echo == 1 ? 0L : ES_PASSWORD);
2170 lpdit->dwExtendedStyle = 0;
2171 lpdit->x = 10 + (pwid + 1) * 4;
2172 lpdit->y = 10 + (numlines + i + 1) * 14;
2173 lpdit->cx = (width - (pwid + 1)) * 4;
2175 lpdit->id = ID_MID_TEXT + i; // identifier
2177 lpw = (LPWORD) (lpdit + 1);
2179 *lpw++ = 0x0081; // edit class
2181 lpwsz = (LPWSTR) lpw;
2182 nchar = MultiByteToWideChar (CP_ACP, 0, tb[i].def ? tb[i].def : "",
2185 *lpw++ = 0; // no creation data
2189 ret = DialogBoxIndirect(hinst, (LPDLGTEMPLATE) hgbl,
2190 hwndOwner, (DLGPROC) MultiInputDialogProc);
2194 case 0: /* Timeout */
2202 sprintf(buf,"DialogBoxIndirect() failed: %d",GetLastError());
2203 MessageBox(hwndOwner,
2206 MB_OK | MB_ICONINFORMATION | MB_TASKMODAL);
2213 multi_field_dialog(HWND hParent, char * preface, int n, struct textField tb[])
2215 HINSTANCE hInst = 0;
2219 char * plines[16], *p = preface ? preface : "";
2222 for ( i=0; i<16; i++ )
2225 while (*p && numlines < 16) {
2226 plines[numlines++] = p;
2227 for ( ;*p && *p != '\r' && *p != '\n'; p++ );
2228 if ( *p == '\r' && *(p+1) == '\n' ) {
2231 } else if ( *p == '\n' ) {
2234 if ( strlen(plines[numlines-1]) > maxwidth )
2235 maxwidth = strlen(plines[numlines-1]);
2238 for ( i=0;i<n;i++ ) {
2239 len = strlen(tb[i].label) + 1 + (tb[i].len > 40 ? 40 : tb[i].len);
2240 if ( maxwidth < len )
2244 return(MultiInputDialog(hInst, hParent, plines, numlines, maxwidth, n, tb));
2247 static krb5_error_code KRB5_CALLCONV
2248 KRB5_prompter( krb5_context context,
2253 krb5_prompt prompts[])
2255 krb5_error_code errcode = 0;
2257 struct textField * tb = NULL;
2258 int len = 0, blen=0, nlen=0;
2259 HWND hParent = (HWND)data;
2262 nlen = strlen(name)+2;
2265 blen = strlen(banner)+2;
2267 tb = (struct textField *) malloc(sizeof(struct textField) * num_prompts);
2270 memset(tb,0,sizeof(struct textField) * num_prompts);
2271 for ( i=0; i < num_prompts; i++ ) {
2272 tb[i].buf = prompts[i].reply->data;
2273 tb[i].len = prompts[i].reply->length;
2274 tb[i].label = prompts[i].prompt;
2276 tb[i].echo = (prompts[i].hidden ? 2 : 1);
2279 ok = multi_field_dialog(hParent,(char *)banner,num_prompts,tb);
2281 for ( i=0; i < num_prompts; i++ )
2282 prompts[i].reply->length = strlen(prompts[i].reply->data);
2290 for (i = 0; i < num_prompts; i++) {
2291 memset(prompts[i].reply->data, 0, prompts[i].reply->length);
2298 KFW_AFS_wait_for_service_start(void)
2303 CurrentState = SERVICE_START_PENDING;
2304 memset(HostName, '\0', sizeof(HostName));
2305 gethostname(HostName, sizeof(HostName));
2307 while (CurrentState != SERVICE_RUNNING || CurrentState != SERVICE_STOPPED)
2309 if (GetServiceStatus(HostName, TRANSARCAFSDAEMON, &CurrentState) != NOERROR)
2311 if ( IsDebuggerPresent() ) {
2312 switch ( CurrentState ) {
2313 case SERVICE_STOPPED:
2314 OutputDebugString("SERVICE_STOPPED\n");
2316 case SERVICE_START_PENDING:
2317 OutputDebugString("SERVICE_START_PENDING\n");
2319 case SERVICE_STOP_PENDING:
2320 OutputDebugString("SERVICE_STOP_PENDING\n");
2322 case SERVICE_RUNNING:
2323 OutputDebugString("SERVICE_RUNNING\n");
2325 case SERVICE_CONTINUE_PENDING:
2326 OutputDebugString("SERVICE_CONTINUE_PENDING\n");
2328 case SERVICE_PAUSE_PENDING:
2329 OutputDebugString("SERVICE_PAUSE_PENDING\n");
2331 case SERVICE_PAUSED:
2332 OutputDebugString("SERVICE_PAUSED\n");
2335 OutputDebugString("UNKNOWN Service State\n");
2338 if (CurrentState == SERVICE_STOPPED)
2340 if (CurrentState == SERVICE_RUNNING)
2356 memset(HostName, '\0', sizeof(HostName));
2357 gethostname(HostName, sizeof(HostName));
2358 if (GetServiceStatus(HostName, TRANSARCAFSDAEMON, &CurrentState) != NOERROR)
2360 if (CurrentState != SERVICE_RUNNING)
2363 rc = ktc_ForgetAllTokens();
2370 krb5_context alt_ctx,
2382 struct ktc_principal aserver;
2383 struct ktc_principal aclient;
2384 char realm_of_user[REALM_SZ]; /* Kerberos realm of user */
2385 char realm_of_cell[REALM_SZ]; /* Kerberos realm of cell */
2386 char local_cell[MAXCELLCHARS+1];
2387 char Dmycell[MAXCELLCHARS+1];
2388 struct ktc_token atoken;
2389 struct ktc_token btoken;
2390 struct afsconf_cell ak_cellconfig; /* General information about the cell */
2391 char RealmName[128];
2393 char ServiceName[128];
2397 krb5_context ctx = 0;
2400 krb5_creds * k5creds = 0;
2401 krb5_error_code code;
2402 krb5_principal client_principal = 0;
2403 char * cname = 0, *sname = 0;
2407 memset(HostName, '\0', sizeof(HostName));
2408 gethostname(HostName, sizeof(HostName));
2409 if (GetServiceStatus(HostName, TRANSARCAFSDAEMON, &CurrentState) != NOERROR) {
2410 if ( IsDebuggerPresent() )
2411 OutputDebugString("Unable to retrieve AFSD Service Status\n");
2414 if (CurrentState != SERVICE_RUNNING) {
2415 if ( IsDebuggerPresent() )
2416 OutputDebugString("AFSD Service NOT RUNNING\n");
2420 if (!pkrb5_init_context)
2423 memset(RealmName, '\0', sizeof(RealmName));
2424 memset(CellName, '\0', sizeof(CellName));
2425 memset(ServiceName, '\0', sizeof(ServiceName));
2426 memset(realm_of_user, '\0', sizeof(realm_of_user));
2427 memset(realm_of_cell, '\0', sizeof(realm_of_cell));
2428 if (cell && cell[0])
2429 strcpy(Dmycell, cell);
2431 memset(Dmycell, '\0', sizeof(Dmycell));
2433 // NULL or empty cell returns information on local cell
2434 if (rc = KFW_AFS_get_cellconfig(Dmycell, &ak_cellconfig, local_cell))
2436 // KFW_AFS_error(rc, "get_cellconfig()");
2443 code = pkrb5_init_context(&ctx);
2444 if (code) goto cleanup;
2450 code = pkrb5_cc_default(ctx, &cc);
2451 if (code) goto skip_krb5_init;
2454 memset((char *)&increds, 0, sizeof(increds));
2456 code = pkrb5_cc_get_principal(ctx, cc, &client_principal);
2458 if ( code == KRB5_CC_NOTFOUND && IsDebuggerPresent() )
2460 OutputDebugString("Principal Not Found for ccache\n");
2462 goto skip_krb5_init;
2464 i = krb5_princ_realm(ctx, client_principal)->length;
2467 strncpy(realm_of_user,krb5_princ_realm(ctx, client_principal)->data,i);
2468 realm_of_user[i] = 0;
2473 if ( !try_krb5 || !realm_of_user[0] ) {
2474 if ((rc = (*pkrb_get_tf_realm)((*ptkt_string)(), realm_of_user)) != KSUCCESS)
2482 strcpy(realm_of_cell, afs_realm_of_cell(&ak_cellconfig));
2484 if (strlen(service) == 0)
2485 strcpy(ServiceName, "afs");
2487 strcpy(ServiceName, service);
2489 if (strlen(cell) == 0)
2490 strcpy(CellName, local_cell);
2492 strcpy(CellName, cell);
2494 if (strlen(realm) == 0)
2495 strcpy(RealmName, realm_of_cell);
2497 strcpy(RealmName, realm);
2499 memset(&creds, '\0', sizeof(creds));
2504 /* First try service/cell@REALM */
2505 if (code = pkrb5_build_principal(ctx, &increds.server,
2515 increds.client = client_principal;
2516 increds.times.endtime = 0;
2517 /* Ask for DES since that is what V4 understands */
2518 increds.keyblock.enctype = ENCTYPE_DES_CBC_CRC;
2521 if ( IsDebuggerPresent() ) {
2522 pkrb5_unparse_name(ctx, increds.client, &cname);
2523 pkrb5_unparse_name(ctx, increds.server, &sname);
2524 OutputDebugString("Getting tickets for \"");
2525 OutputDebugString(cname);
2526 OutputDebugString("\" and service \"");
2527 OutputDebugString(sname);
2528 OutputDebugString("\"\n");
2532 code = pkrb5_get_credentials(ctx, 0, cc, &increds, &k5creds);
2533 if (code == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN ||
2534 code == KRB5KRB_ERR_GENERIC /* heimdal */ ||
2535 code == KRB5KRB_AP_ERR_MSG_TYPE) {
2536 /* Or service@REALM */
2537 pkrb5_free_principal(ctx,increds.server);
2539 code = pkrb5_build_principal(ctx, &increds.server,
2545 if ( IsDebuggerPresent() ) {
2546 char * cname, *sname;
2547 pkrb5_unparse_name(ctx, increds.client, &cname);
2548 pkrb5_unparse_name(ctx, increds.server, &sname);
2549 OutputDebugString("krb5_get_credentials() returned Service Principal Unknown\n");
2550 OutputDebugString("Trying again: getting tickets for \"");
2551 OutputDebugString(cname);
2552 OutputDebugString("\" and service \"");
2553 OutputDebugString(sname);
2554 OutputDebugString("\"\n");
2555 pkrb5_free_unparsed_name(ctx,cname);
2556 pkrb5_free_unparsed_name(ctx,sname);
2561 code = pkrb5_get_credentials(ctx, 0, cc, &increds, &k5creds);
2564 if ((code == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN ||
2565 code == KRB5KRB_ERR_GENERIC /* heimdal */ ||
2566 code == KRB5KRB_AP_ERR_MSG_TYPE) &&
2567 strcmp(RealmName, realm_of_cell)) {
2568 /* Or service/cell@REALM_OF_CELL */
2569 strcpy(RealmName, realm_of_cell);
2570 pkrb5_free_principal(ctx,increds.server);
2572 code = pkrb5_build_principal(ctx, &increds.server,
2579 if ( IsDebuggerPresent() ) {
2580 char * cname, *sname;
2581 pkrb5_unparse_name(ctx, increds.client, &cname);
2582 pkrb5_unparse_name(ctx, increds.server, &sname);
2583 OutputDebugString("krb5_get_credentials() returned Service Principal Unknown\n");
2584 OutputDebugString("Trying again: getting tickets for \"");
2585 OutputDebugString(cname);
2586 OutputDebugString("\" and service \"");
2587 OutputDebugString(sname);
2588 OutputDebugString("\"\n");
2589 pkrb5_free_unparsed_name(ctx,cname);
2590 pkrb5_free_unparsed_name(ctx,sname);
2595 code = pkrb5_get_credentials(ctx, 0, cc, &increds, &k5creds);
2598 if (code == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN ||
2599 code == KRB5KRB_ERR_GENERIC /* heimdal */ ||
2600 code == KRB5KRB_AP_ERR_MSG_TYPE) {
2601 /* Or service@REALM_OF_CELL */
2602 pkrb5_free_principal(ctx,increds.server);
2604 code = pkrb5_build_principal(ctx, &increds.server,
2610 if ( IsDebuggerPresent() ) {
2611 char * cname, *sname;
2612 pkrb5_unparse_name(ctx, increds.client, &cname);
2613 pkrb5_unparse_name(ctx, increds.server, &sname);
2614 OutputDebugString("krb5_get_credentials() returned Service Principal Unknown\n");
2615 OutputDebugString("Trying again: getting tickets for \"");
2616 OutputDebugString(cname);
2617 OutputDebugString("\" and service \"");
2618 OutputDebugString(sname);
2619 OutputDebugString("\"\n");
2620 pkrb5_free_unparsed_name(ctx,cname);
2621 pkrb5_free_unparsed_name(ctx,sname);
2626 code = pkrb5_get_credentials(ctx, 0, cc, &increds, &k5creds);
2631 if ( IsDebuggerPresent() ) {
2633 sprintf(message,"krb5_get_credentials returns: %d\n",code);
2634 OutputDebugString(message);
2640 /* This code inserts the entire K5 ticket into the token
2641 * No need to perform a krb524 translation which is
2642 * commented out in the code below
2644 if (k5creds->ticket.length > MAXKTCTICKETLEN)
2647 memset(&aserver, '\0', sizeof(aserver));
2648 strncpy(aserver.name, ServiceName, MAXKTCNAMELEN - 1);
2649 strncpy(aserver.cell, CellName, MAXKTCREALMLEN - 1);
2651 memset(&atoken, '\0', sizeof(atoken));
2652 atoken.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
2653 atoken.startTime = k5creds->times.starttime;
2654 atoken.endTime = k5creds->times.endtime;
2655 memcpy(&atoken.sessionKey, k5creds->keyblock.contents, k5creds->keyblock.length);
2656 atoken.ticketLen = k5creds->ticket.length;
2657 memcpy(atoken.ticket, k5creds->ticket.data, atoken.ticketLen);
2660 rc = ktc_GetToken(&aserver, &btoken, sizeof(btoken), &aclient);
2661 if (rc != 0 && rc != KTC_NOENT && rc != KTC_NOCELL) {
2662 if ( rc == KTC_NOCM && retry < 20 ) {
2665 goto retry_gettoken5;
2670 if (atoken.kvno == btoken.kvno &&
2671 atoken.ticketLen == btoken.ticketLen &&
2672 !memcmp(&atoken.sessionKey, &btoken.sessionKey, sizeof(atoken.sessionKey)) &&
2673 !memcmp(atoken.ticket, btoken.ticket, atoken.ticketLen))
2675 /* Success - Nothing to do */
2679 // * Reset the "aclient" structure before we call ktc_SetToken.
2680 // * This structure was first set by the ktc_GetToken call when
2681 // * we were comparing whether identical tokens already existed.
2683 len = min(k5creds->client->data[0].length,MAXKTCNAMELEN - 1);
2684 strncpy(aclient.name, k5creds->client->data[0].data, len);
2685 aclient.name[len] = '\0';
2687 if ( k5creds->client->length > 1 ) {
2689 strcat(aclient.name, ".");
2690 p = aclient.name + strlen(aclient.name);
2691 len = min(k5creds->client->data[1].length,MAXKTCNAMELEN - strlen(aclient.name) - 1);
2692 strncpy(p, k5creds->client->data[1].data, len);
2695 aclient.instance[0] = '\0';
2697 strcpy(aclient.cell, realm_of_cell);
2699 len = min(k5creds->client->realm.length,strlen(realm_of_cell));
2700 if ( strncmp(realm_of_cell, k5creds->client->realm.data, len) ) {
2702 strcat(aclient.name, "@");
2703 p = aclient.name + strlen(aclient.name);
2704 len = min(k5creds->client->realm.length,MAXKTCNAMELEN - strlen(aclient.name) - 1);
2705 strncpy(p, k5creds->client->realm.data, len);
2710 strncpy(aclient.smbname, smbname, MAXRANDOMNAMELEN);
2711 aclient.smbname[MAXRANDOMNAMELEN-1] = '\0';
2713 aclient.smbname[0] = '\0';
2716 rc = ktc_SetToken(&aserver, &atoken, &aclient, 0);
2718 goto cleanup; /* We have successfully inserted the token */
2721 /* Otherwise, the ticket could have been too large so try to
2722 * convert using the krb524d running with the KDC
2724 code = pkrb524_convert_creds_kdc(ctx, k5creds, &creds);
2725 pkrb5_free_creds(ctx, k5creds);
2727 if ( IsDebuggerPresent() ) {
2729 sprintf(message,"krb524_convert_creds_kdc returns: %d\n",code);
2730 OutputDebugString(message);
2738 code = (*pkrb_get_cred)(ServiceName, CellName, RealmName, &creds);
2739 if (code == NO_TKT_FIL) {
2740 // if the problem is that we have no krb4 tickets
2741 // do not attempt to continue
2744 if (code != KSUCCESS)
2745 code = (*pkrb_get_cred)(ServiceName, "", RealmName, &creds);
2747 if (code != KSUCCESS)
2749 if ((code = (*pkrb_mk_req)(&ticket, ServiceName, CellName, RealmName, 0)) == KSUCCESS)
2751 if ((code = (*pkrb_get_cred)(ServiceName, CellName, RealmName, &creds)) != KSUCCESS)
2756 else if ((code = (*pkrb_mk_req)(&ticket, ServiceName, "", RealmName, 0)) == KSUCCESS)
2758 if ((code = (*pkrb_get_cred)(ServiceName, "", RealmName, &creds)) != KSUCCESS)
2773 memset(&aserver, '\0', sizeof(aserver));
2774 strncpy(aserver.name, ServiceName, MAXKTCNAMELEN - 1);
2775 strncpy(aserver.cell, CellName, MAXKTCREALMLEN - 1);
2777 memset(&atoken, '\0', sizeof(atoken));
2778 atoken.kvno = creds.kvno;
2779 atoken.startTime = creds.issue_date;
2780 atoken.endTime = creds.issue_date + life_to_time(0,creds.lifetime);
2781 memcpy(&atoken.sessionKey, creds.session, 8);
2782 atoken.ticketLen = creds.ticket_st.length;
2783 memcpy(atoken.ticket, creds.ticket_st.dat, atoken.ticketLen);
2786 rc = ktc_GetToken(&aserver, &btoken, sizeof(btoken), &aclient);
2787 if (rc != 0 && rc != KTC_NOENT && rc != KTC_NOCELL) {
2788 if ( rc == KTC_NOCM && retry < 20 ) {
2791 goto retry_gettoken;
2793 KFW_AFS_error(rc, "ktc_GetToken()");
2798 if (atoken.kvno == btoken.kvno &&
2799 atoken.ticketLen == btoken.ticketLen &&
2800 !memcmp(&atoken.sessionKey, &btoken.sessionKey, sizeof(atoken.sessionKey)) &&
2801 !memcmp(atoken.ticket, btoken.ticket, atoken.ticketLen))
2806 // * Reset the "aclient" structure before we call ktc_SetToken.
2807 // * This structure was first set by the ktc_GetToken call when
2808 // * we were comparing whether identical tokens already existed.
2810 strncpy(aclient.name, creds.pname, MAXKTCNAMELEN - 1);
2813 strncat(aclient.name, ".", MAXKTCNAMELEN - 1);
2814 strncat(aclient.name, creds.pinst, MAXKTCNAMELEN - 1);
2816 strcpy(aclient.instance, "");
2818 if ( strcmp(realm_of_cell, creds.realm) )
2820 strncat(aclient.name, "@", MAXKTCNAMELEN - 1);
2821 strncpy(aclient.name, creds.realm, MAXKTCREALMLEN - 1);
2823 aclient.name[MAXKTCREALMLEN-1] = '\0';
2825 strcpy(aclient.cell, CellName);
2828 strncpy(aclient.smbname, smbname, MAXRANDOMNAMELEN);
2829 aclient.smbname[MAXRANDOMNAMELEN-1] = '\0';
2831 aclient.smbname[0] = '\0';
2834 if (rc = ktc_SetToken(&aserver, &atoken, &aclient, 0))
2836 KFW_AFS_error(rc, "ktc_SetToken()");
2843 pkrb5_free_unparsed_name(ctx,cname);
2845 pkrb5_free_unparsed_name(ctx,sname);
2846 if (client_principal)
2847 pkrb5_free_principal(ctx,client_principal);
2848 /* increds.client == client_principal */
2850 pkrb5_free_principal(ctx,increds.server);
2851 if (cc && (cc != alt_cc))
2852 pkrb5_cc_close(ctx, cc);
2853 if (ctx && (ctx != alt_ctx))
2854 pkrb5_free_context(ctx);
2856 return(rc? rc : code);
2859 /**************************************/
2860 /* afs_realm_of_cell(): */
2861 /**************************************/
2863 afs_realm_of_cell(struct afsconf_cell *cellconfig)
2865 static char krbrlm[REALM_SZ+1]="";
2866 krb5_context ctx = 0;
2867 char ** realmlist=NULL;
2873 if (!pkrb5_init_context)
2876 r = pkrb5_init_context(&ctx);
2878 r = pkrb5_get_host_realm(ctx, cellconfig->hostName[0], &realmlist);
2879 if ( !r && realmlist && realmlist[0] ) {
2880 strcpy(krbrlm, realmlist[0]);
2881 pkrb5_free_host_realm(ctx, realmlist);
2884 pkrb5_free_context(ctx);
2889 char *t = cellconfig->name;
2894 if (islower(c)) c=toupper(c);
2902 /**************************************/
2903 /* KFW_AFS_get_cellconfig(): */
2904 /**************************************/
2906 KFW_AFS_get_cellconfig(char *cell, struct afsconf_cell *cellconfig, char *local_cell)
2909 char newcell[MAXCELLCHARS+1];
2911 local_cell[0] = (char)0;
2912 memset(cellconfig, 0, sizeof(*cellconfig));
2914 /* WIN32: cm_GetRootCellName(local_cell) - NOTE: no way to get max chars */
2915 if (rc = cm_GetRootCellName(local_cell))
2920 if (strlen(cell) == 0)
2921 strcpy(cell, local_cell);
2923 /* WIN32: cm_SearchCellFile(cell, pcallback, pdata) */
2924 strcpy(cellconfig->name, cell);
2926 rc = cm_SearchCellFile(cell, newcell, get_cellconfig_callback, (void*)cellconfig);
2927 #ifdef AFS_AFSDB_ENV
2930 rc = cm_SearchCellByDNS(cell, newcell, &ttl, get_cellconfig_callback, (void*)cellconfig);
2936 /**************************************/
2937 /* get_cellconfig_callback(): */
2938 /**************************************/
2940 get_cellconfig_callback(void *cellconfig, struct sockaddr_in *addrp, char *namep)
2942 struct afsconf_cell *cc = (struct afsconf_cell *)cellconfig;
2944 cc->hostAddr[cc->numServers] = *addrp;
2945 strcpy(cc->hostName[cc->numServers], namep);
2951 /**************************************/
2952 /* KFW_AFS_error(): */
2953 /**************************************/
2955 KFW_AFS_error(LONG rc, LPCSTR FailedFunctionName)
2958 const char *errText;
2960 // Using AFS defines as error messages for now, until Transarc
2961 // gets back to me with "string" translations of each of these
2963 if (rc == KTC_ERROR)
2964 errText = "KTC_ERROR";
2965 else if (rc == KTC_TOOBIG)
2966 errText = "KTC_TOOBIG";
2967 else if (rc == KTC_INVAL)
2968 errText = "KTC_INVAL";
2969 else if (rc == KTC_NOENT)
2970 errText = "KTC_NOENT";
2971 else if (rc == KTC_PIOCTLFAIL)
2972 errText = "KTC_PIOCTLFAIL";
2973 else if (rc == KTC_NOPIOCTL)
2974 errText = "KTC_NOPIOCTL";
2975 else if (rc == KTC_NOCELL)
2976 errText = "KTC_NOCELL";
2977 else if (rc == KTC_NOCM)
2978 errText = "KTC_NOCM: The service, Transarc AFS Daemon, most likely is not started!";
2980 errText = "Unknown error!";
2982 sprintf(message, "%s (0x%x)\n(%s failed)", errText, rc, FailedFunctionName);
2984 if ( IsDebuggerPresent() ) {
2985 OutputDebugString(message);
2986 OutputDebugString("\n");
2988 MessageBox(NULL, message, "AFS", MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND);
2994 LPSTR lpszMachineName,
2995 LPSTR lpszServiceName,
2996 DWORD *lpdwCurrentState)
2999 SC_HANDLE schSCManager = NULL;
3000 SC_HANDLE schService = NULL;
3001 DWORD fdwDesiredAccess = 0;
3002 SERVICE_STATUS ssServiceStatus = {0};
3005 *lpdwCurrentState = 0;
3007 fdwDesiredAccess = GENERIC_READ;
3009 schSCManager = OpenSCManager(lpszMachineName,
3013 if(schSCManager == NULL)
3015 hr = GetLastError();
3019 schService = OpenService(schSCManager,
3023 if(schService == NULL)
3025 hr = GetLastError();
3029 fRet = QueryServiceStatus(schService,
3034 hr = GetLastError();
3038 *lpdwCurrentState = ssServiceStatus.dwCurrentState;
3042 CloseServiceHandle(schService);
3043 CloseServiceHandle(schSCManager);
3056 for (n = 0; fi[n].func_ptr_var; n++)
3057 *(fi[n].func_ptr_var) = 0;
3058 if (h) FreeLibrary(h);
3063 const char* dll_name,
3065 HINSTANCE* ph, // [out, optional] - DLL handle
3066 int* pindex, // [out, optional] - index of last func loaded (-1 if none)
3067 int cleanup, // cleanup function pointers and unload on error
3068 int go_on, // continue loading even if some functions cannot be loaded
3069 int silent // do not pop-up a system dialog if DLL cannot be loaded
3078 if (pindex) *pindex = -1;
3080 for (n = 0; fi[n].func_ptr_var; n++)
3081 *(fi[n].func_ptr_var) = 0;
3084 em = SetErrorMode(SEM_FAILCRITICALERRORS);
3085 h = LoadLibrary(dll_name);
3093 for (i = 0; (go_on || !error) && (i < n); i++)
3095 void* p = (void*)GetProcAddress(h, fi[i].func_name);
3101 *(fi[i].func_ptr_var) = p;
3104 if (pindex) *pindex = last_i;
3105 if (error && cleanup && !go_on) {
3106 for (i = 0; i < n; i++) {
3107 *(fi[i].func_ptr_var) = 0;
3113 if (error) return 0;
3117 BOOL KFW_probe_kdc(struct afsconf_cell * cellconfig)
3119 krb5_context ctx = 0;
3121 krb5_error_code code;
3123 const char * realm = 0;
3124 krb5_principal principal = 0;
3126 char password[PROBE_PASSWORD_LEN+1];
3127 BOOL serverReachable = 0;
3129 realm = afs_realm_of_cell(cellconfig); // do not free
3131 code = pkrb5_build_principal(ctx, &principal, strlen(realm),
3132 realm, PROBE_USERNAME, NULL, NULL);
3133 if ( code ) goto cleanup;
3135 code = KFW_get_ccache(ctx, principal, &cc);
3136 if ( code ) goto cleanup;
3138 code = pkrb5_unparse_name(ctx, principal, &pname);
3139 if ( code ) goto cleanup;
3141 pwdata.data = password;
3142 pwdata.length = PROBE_PASSWORD_LEN;
3143 code = pkrb5_c_random_make_octets(ctx, &pwdata);
3146 for ( i=0 ; i<PROBE_PASSWORD_LEN ; i++ )
3149 password[PROBE_PASSWORD_LEN] = '\0';
3151 code = KFW_kinit(NULL, NULL, HWND_DESKTOP,
3161 case KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN:
3162 case KRB5KDC_ERR_CLIENT_REVOKED:
3163 case KRB5KDC_ERR_CLIENT_NOTYET:
3164 case KRB5KDC_ERR_PREAUTH_FAILED:
3165 case KRB5KDC_ERR_PREAUTH_REQUIRED:
3166 case KRB5KDC_ERR_PADATA_TYPE_NOSUPP:
3167 serverReachable = TRUE;
3170 serverReachable = FALSE;
3175 pkrb5_free_unparsed_name(ctx,pname);
3177 pkrb5_free_principal(ctx,principal);
3179 pkrb5_cc_close(ctx,cc);
3181 pkrb5_free_context(ctx);
3183 return serverReachable;