Added leashdll/wshelper related files from KFW
[krb5.git] / src / windows / leashdll / leashdll.c
1 #include <windows.h>
2 #include "leashdll.h"
3 #include <krb.h>
4 #include <leashwin.h>
5 #include "leash-int.h"
6
7 HINSTANCE hLeashInst;
8
9 #ifndef NO_KRB4
10 HINSTANCE hKrb4 = 0;
11 #endif
12 HINSTANCE hKrb5 = 0;
13 HINSTANCE hKrb524 = 0;
14 HINSTANCE hSecur32 = 0;
15 HINSTANCE hComErr = 0;
16 HINSTANCE hService = 0;
17 HINSTANCE hProfile = 0;
18 HINSTANCE hPsapi = 0;
19 HINSTANCE hToolHelp32 = 0;
20 HINSTANCE hCcapi = 0;
21
22 DWORD     AfsAvailable = 0;
23
24 #ifndef NO_KRB4
25 // krb4 functions
26 DECL_FUNC_PTR(get_krb_err_txt_entry);
27 DECL_FUNC_PTR(k_isinst);
28 DECL_FUNC_PTR(k_isname);
29 DECL_FUNC_PTR(k_isrealm);
30 DECL_FUNC_PTR(kadm_change_your_password);
31 DECL_FUNC_PTR(kname_parse);
32 DECL_FUNC_PTR(krb_get_cred);
33 DECL_FUNC_PTR(krb_get_krbhst);
34 DECL_FUNC_PTR(krb_get_lrealm);
35 DECL_FUNC_PTR(krb_get_pw_in_tkt);
36 DECL_FUNC_PTR(krb_get_tf_realm);
37 DECL_FUNC_PTR(krb_mk_req);
38 DECL_FUNC_PTR(krb_realmofhost);
39 DECL_FUNC_PTR(tf_init);
40 DECL_FUNC_PTR(tf_close);
41 DECL_FUNC_PTR(tf_get_cred);
42 DECL_FUNC_PTR(tf_get_pname);
43 DECL_FUNC_PTR(tf_get_pinst);
44 DECL_FUNC_PTR(LocalHostAddr);
45 DECL_FUNC_PTR(tkt_string);
46 DECL_FUNC_PTR(krb_set_tkt_string);
47 DECL_FUNC_PTR(initialize_krb_error_func);
48 DECL_FUNC_PTR(initialize_kadm_error_table);
49 DECL_FUNC_PTR(dest_tkt);
50 DECL_FUNC_PTR(lsh_LoadKrb4LeashErrorTables); // XXX
51 DECL_FUNC_PTR(krb_in_tkt);
52 DECL_FUNC_PTR(krb_save_credentials);
53 DECL_FUNC_PTR(krb_get_krbconf2);
54 DECL_FUNC_PTR(krb_get_krbrealm2);
55 DECL_FUNC_PTR(krb_life_to_time);
56 #endif
57
58 // krb5 functions
59 DECL_FUNC_PTR(krb5_change_password);
60 DECL_FUNC_PTR(krb5_get_init_creds_opt_init);
61 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_tkt_life);
62 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_renew_life);
63 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_forwardable);
64 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_proxiable);
65 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_address_list);
66 DECL_FUNC_PTR(krb5_get_init_creds_password);
67 DECL_FUNC_PTR(krb5_build_principal_ext);
68 DECL_FUNC_PTR(krb5_cc_resolve);
69 DECL_FUNC_PTR(krb5_cc_default);
70 DECL_FUNC_PTR(krb5_cc_default_name);
71 DECL_FUNC_PTR(krb5_cc_set_default_name);
72 DECL_FUNC_PTR(krb5_cc_initialize);
73 DECL_FUNC_PTR(krb5_cc_destroy);
74 DECL_FUNC_PTR(krb5_cc_close);
75 DECL_FUNC_PTR(krb5_cc_store_cred);
76 DECL_FUNC_PTR(krb5_cc_copy_creds);
77 // DECL_FUNC_PTR(krb5_cc_retrieve_cred);
78 DECL_FUNC_PTR(krb5_cc_get_principal);
79 DECL_FUNC_PTR(krb5_cc_start_seq_get);
80 DECL_FUNC_PTR(krb5_cc_next_cred);
81 DECL_FUNC_PTR(krb5_cc_end_seq_get);
82 // DECL_FUNC_PTR(krb5_cc_remove_cred);
83 DECL_FUNC_PTR(krb5_cc_set_flags);
84 // DECL_FUNC_PTR(krb5_cc_get_type);
85 DECL_FUNC_PTR(krb5_free_context);
86 DECL_FUNC_PTR(krb5_free_cred_contents);
87 DECL_FUNC_PTR(krb5_free_principal);
88 DECL_FUNC_PTR(krb5_get_in_tkt_with_password);
89 DECL_FUNC_PTR(krb5_init_context);
90 DECL_FUNC_PTR(krb5_parse_name);
91 DECL_FUNC_PTR(krb5_timeofday);
92 DECL_FUNC_PTR(krb5_timestamp_to_sfstring);
93 DECL_FUNC_PTR(krb5_unparse_name);
94 DECL_FUNC_PTR(krb5_get_credentials);
95 DECL_FUNC_PTR(krb5_mk_req);
96 DECL_FUNC_PTR(krb5_sname_to_principal);
97 DECL_FUNC_PTR(krb5_get_credentials_renew);
98 DECL_FUNC_PTR(krb5_free_data);
99 DECL_FUNC_PTR(krb5_free_data_contents);
100 // DECL_FUNC_PTR(krb5_get_realm_domain);
101 DECL_FUNC_PTR(krb5_free_unparsed_name);
102 DECL_FUNC_PTR(krb5_os_localaddr);
103 DECL_FUNC_PTR(krb5_copy_keyblock_contents);
104 DECL_FUNC_PTR(krb5_copy_data);
105 DECL_FUNC_PTR(krb5_free_creds);
106 DECL_FUNC_PTR(krb5_build_principal);
107 DECL_FUNC_PTR(krb5_get_renewed_creds);
108 DECL_FUNC_PTR(krb5_get_default_config_files);
109 DECL_FUNC_PTR(krb5_free_config_files);
110 DECL_FUNC_PTR(krb5_get_default_realm);
111 DECL_FUNC_PTR(krb5_free_ticket);
112 DECL_FUNC_PTR(krb5_decode_ticket);
113 DECL_FUNC_PTR(krb5_get_host_realm);
114 DECL_FUNC_PTR(krb5_free_host_realm);
115 DECL_FUNC_PTR(krb5_c_random_make_octets);
116 DECL_FUNC_PTR(krb5_free_addresses);
117 DECL_FUNC_PTR(krb5_free_default_realm);
118 DECL_FUNC_PTR(krb5_principal_compare);
119 DECL_FUNC_PTR(krb5_string_to_deltat);
120
121 #ifndef NO_KRB4
122 // Krb524 functions
123 DECL_FUNC_PTR(krb524_init_ets);
124 DECL_FUNC_PTR(krb524_convert_creds_kdc);
125 #endif
126
127 // ComErr functions
128 DECL_FUNC_PTR(com_err);
129 DECL_FUNC_PTR(error_message);
130
131 // Profile functions
132 DECL_FUNC_PTR(profile_init);
133 DECL_FUNC_PTR(profile_release);
134 DECL_FUNC_PTR(profile_get_subsection_names);
135 DECL_FUNC_PTR(profile_free_list);
136 DECL_FUNC_PTR(profile_get_string);
137 DECL_FUNC_PTR(profile_release_string);
138 DECL_FUNC_PTR(profile_get_integer);
139
140 // Service functions
141 DECL_FUNC_PTR(OpenSCManagerA);
142 DECL_FUNC_PTR(OpenServiceA);
143 DECL_FUNC_PTR(QueryServiceStatus);
144 DECL_FUNC_PTR(CloseServiceHandle);
145 DECL_FUNC_PTR(LsaNtStatusToWinError);
146
147 // LSA Functions
148 DECL_FUNC_PTR(LsaConnectUntrusted);
149 DECL_FUNC_PTR(LsaLookupAuthenticationPackage);
150 DECL_FUNC_PTR(LsaCallAuthenticationPackage);
151 DECL_FUNC_PTR(LsaFreeReturnBuffer);
152 DECL_FUNC_PTR(LsaGetLogonSessionData);
153
154 // CCAPI Functions
155 DECL_FUNC_PTR(cc_initialize);
156 DECL_FUNC_PTR(cc_shutdown);
157 DECL_FUNC_PTR(cc_get_NC_info);
158 DECL_FUNC_PTR(cc_free_NC_info);
159
160 #ifndef NO_KRB4
161 FUNC_INFO k4_fi[] = {
162     MAKE_FUNC_INFO(get_krb_err_txt_entry),
163     MAKE_FUNC_INFO(k_isinst),
164     MAKE_FUNC_INFO(k_isname),
165     MAKE_FUNC_INFO(k_isrealm),
166     MAKE_FUNC_INFO(kadm_change_your_password),
167     MAKE_FUNC_INFO(kname_parse),
168     MAKE_FUNC_INFO(krb_get_cred),
169     MAKE_FUNC_INFO(krb_get_krbhst),
170     MAKE_FUNC_INFO(krb_get_lrealm),
171     MAKE_FUNC_INFO(krb_get_pw_in_tkt),
172     MAKE_FUNC_INFO(krb_get_tf_realm),
173     MAKE_FUNC_INFO(krb_mk_req),
174     MAKE_FUNC_INFO(krb_realmofhost),
175     MAKE_FUNC_INFO(tf_init),
176     MAKE_FUNC_INFO(tf_close),
177     MAKE_FUNC_INFO(tf_get_cred),
178     MAKE_FUNC_INFO(tf_get_pname),
179     MAKE_FUNC_INFO(tf_get_pinst),
180     MAKE_FUNC_INFO(LocalHostAddr),
181     MAKE_FUNC_INFO(tkt_string),
182     MAKE_FUNC_INFO(krb_set_tkt_string),
183     MAKE_FUNC_INFO(initialize_krb_error_func),
184     MAKE_FUNC_INFO(initialize_kadm_error_table),
185     MAKE_FUNC_INFO(dest_tkt),
186     MAKE_FUNC_INFO(lsh_LoadKrb4LeashErrorTables), // XXX
187     MAKE_FUNC_INFO(krb_in_tkt),
188     MAKE_FUNC_INFO(krb_save_credentials),
189     MAKE_FUNC_INFO(krb_get_krbconf2),
190     MAKE_FUNC_INFO(krb_get_krbrealm2),
191     MAKE_FUNC_INFO(krb_life_to_time),
192     END_FUNC_INFO
193 };
194 #endif
195
196 FUNC_INFO k5_fi[] = {
197     MAKE_FUNC_INFO(krb5_change_password),
198     MAKE_FUNC_INFO(krb5_get_init_creds_opt_init),
199     MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_tkt_life),
200     MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_renew_life),
201     MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_forwardable),
202     MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_proxiable),
203     MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_address_list),
204     MAKE_FUNC_INFO(krb5_get_init_creds_password),
205     MAKE_FUNC_INFO(krb5_build_principal_ext),
206     MAKE_FUNC_INFO(krb5_cc_resolve),
207     MAKE_FUNC_INFO(krb5_cc_default),
208     MAKE_FUNC_INFO(krb5_cc_default_name),
209     MAKE_FUNC_INFO(krb5_cc_set_default_name),
210     MAKE_FUNC_INFO(krb5_cc_initialize),
211     MAKE_FUNC_INFO(krb5_cc_destroy),
212     MAKE_FUNC_INFO(krb5_cc_close),
213     MAKE_FUNC_INFO(krb5_cc_copy_creds),
214     MAKE_FUNC_INFO(krb5_cc_store_cred),
215 // MAKE_FUNC_INFO(krb5_cc_retrieve_cred),
216     MAKE_FUNC_INFO(krb5_cc_get_principal),
217     MAKE_FUNC_INFO(krb5_cc_start_seq_get),
218     MAKE_FUNC_INFO(krb5_cc_next_cred),
219     MAKE_FUNC_INFO(krb5_cc_end_seq_get),
220 // MAKE_FUNC_INFO(krb5_cc_remove_cred),
221     MAKE_FUNC_INFO(krb5_cc_set_flags),
222 // MAKE_FUNC_INFO(krb5_cc_get_type),
223     MAKE_FUNC_INFO(krb5_free_context),
224     MAKE_FUNC_INFO(krb5_free_cred_contents),
225     MAKE_FUNC_INFO(krb5_free_principal),
226     MAKE_FUNC_INFO(krb5_get_in_tkt_with_password),
227     MAKE_FUNC_INFO(krb5_init_context),
228     MAKE_FUNC_INFO(krb5_parse_name),
229     MAKE_FUNC_INFO(krb5_timeofday),
230     MAKE_FUNC_INFO(krb5_timestamp_to_sfstring),
231     MAKE_FUNC_INFO(krb5_unparse_name),
232     MAKE_FUNC_INFO(krb5_get_credentials),
233     MAKE_FUNC_INFO(krb5_mk_req),
234     MAKE_FUNC_INFO(krb5_sname_to_principal),
235     MAKE_FUNC_INFO(krb5_get_credentials_renew),
236     MAKE_FUNC_INFO(krb5_free_data),
237     MAKE_FUNC_INFO(krb5_free_data_contents),
238 //  MAKE_FUNC_INFO(krb5_get_realm_domain),
239     MAKE_FUNC_INFO(krb5_free_unparsed_name),
240     MAKE_FUNC_INFO(krb5_os_localaddr),
241     MAKE_FUNC_INFO(krb5_copy_keyblock_contents),
242     MAKE_FUNC_INFO(krb5_copy_data),
243     MAKE_FUNC_INFO(krb5_free_creds),
244     MAKE_FUNC_INFO(krb5_build_principal),
245     MAKE_FUNC_INFO(krb5_get_renewed_creds),
246     MAKE_FUNC_INFO(krb5_free_addresses),
247     MAKE_FUNC_INFO(krb5_get_default_config_files),
248     MAKE_FUNC_INFO(krb5_free_config_files),
249     MAKE_FUNC_INFO(krb5_get_default_realm),
250     MAKE_FUNC_INFO(krb5_free_ticket),
251     MAKE_FUNC_INFO(krb5_decode_ticket),
252     MAKE_FUNC_INFO(krb5_get_host_realm),
253     MAKE_FUNC_INFO(krb5_free_host_realm),
254     MAKE_FUNC_INFO(krb5_c_random_make_octets),
255     MAKE_FUNC_INFO(krb5_free_default_realm),
256     MAKE_FUNC_INFO(krb5_principal_compare),
257     MAKE_FUNC_INFO(krb5_string_to_deltat),
258     END_FUNC_INFO
259 };
260
261 #ifndef NO_KRB4
262 FUNC_INFO k524_fi[] = {
263     MAKE_FUNC_INFO(krb524_init_ets),
264     MAKE_FUNC_INFO(krb524_convert_creds_kdc),
265     END_FUNC_INFO
266 };
267 #endif
268
269 FUNC_INFO profile_fi[] = {
270     MAKE_FUNC_INFO(profile_init),
271     MAKE_FUNC_INFO(profile_release),
272     MAKE_FUNC_INFO(profile_get_subsection_names),
273     MAKE_FUNC_INFO(profile_free_list),
274     MAKE_FUNC_INFO(profile_get_string),
275     MAKE_FUNC_INFO(profile_release_string),
276     MAKE_FUNC_INFO(profile_get_integer),
277     END_FUNC_INFO
278 };
279
280 FUNC_INFO ce_fi[] = {
281     MAKE_FUNC_INFO(com_err),
282     MAKE_FUNC_INFO(error_message),
283     END_FUNC_INFO
284 };
285
286 FUNC_INFO service_fi[] = {
287     MAKE_FUNC_INFO(OpenSCManagerA),
288     MAKE_FUNC_INFO(OpenServiceA),
289     MAKE_FUNC_INFO(QueryServiceStatus),
290     MAKE_FUNC_INFO(CloseServiceHandle),
291     MAKE_FUNC_INFO(LsaNtStatusToWinError),
292     END_FUNC_INFO
293 };
294
295 FUNC_INFO lsa_fi[] = {
296     MAKE_FUNC_INFO(LsaConnectUntrusted),
297     MAKE_FUNC_INFO(LsaLookupAuthenticationPackage),
298     MAKE_FUNC_INFO(LsaCallAuthenticationPackage),
299     MAKE_FUNC_INFO(LsaFreeReturnBuffer),
300     MAKE_FUNC_INFO(LsaGetLogonSessionData),
301     END_FUNC_INFO
302 };
303
304 // CCAPI v2
305 FUNC_INFO ccapi_fi[] = {
306     MAKE_FUNC_INFO(cc_initialize),
307     MAKE_FUNC_INFO(cc_shutdown),
308     MAKE_FUNC_INFO(cc_get_NC_info),
309     MAKE_FUNC_INFO(cc_free_NC_info),
310     END_FUNC_INFO
311 };
312
313 // psapi functions
314 DECL_FUNC_PTR(GetModuleFileNameExA);
315 DECL_FUNC_PTR(EnumProcessModules);
316
317 FUNC_INFO psapi_fi[] = {
318     MAKE_FUNC_INFO(GetModuleFileNameExA),
319     MAKE_FUNC_INFO(EnumProcessModules),
320     END_FUNC_INFO
321 };
322
323 // toolhelp functions
324 DECL_FUNC_PTR(CreateToolhelp32Snapshot);
325 DECL_FUNC_PTR(Module32First);
326 DECL_FUNC_PTR(Module32Next);
327
328 FUNC_INFO toolhelp_fi[] = {
329     MAKE_FUNC_INFO(CreateToolhelp32Snapshot),
330     MAKE_FUNC_INFO(Module32First),
331     MAKE_FUNC_INFO(Module32Next),
332     END_FUNC_INFO
333 };
334
335 BOOL WINAPI
336 DllMain(
337     HANDLE hinstDLL,
338     DWORD fdwReason,
339     LPVOID lpReserved
340     )
341 {
342     hLeashInst = hinstDLL;
343
344     switch (fdwReason)
345     {
346     case DLL_PROCESS_ATTACH:
347     {
348         OSVERSIONINFO osvi;
349 #ifndef NO_KRB4
350         LoadFuncs(KRB4_DLL, k4_fi, &hKrb4, 0, 1, 0, 0);
351 #endif
352         LoadFuncs(KRB5_DLL, k5_fi, &hKrb5, 0, 1, 0, 0);
353         LoadFuncs(COMERR_DLL, ce_fi, &hComErr, 0, 0, 1, 0);
354         LoadFuncs(SERVICE_DLL, service_fi, &hService, 0, 1, 0, 0);
355         LoadFuncs(SECUR32_DLL, lsa_fi, &hSecur32, 0, 1, 1, 1);
356 #ifndef NO_KRB4
357         LoadFuncs(KRB524_DLL, k524_fi, &hKrb524, 0, 1, 1, 1);
358 #endif
359         LoadFuncs(PROFILE_DLL, profile_fi, &hProfile, 0, 1, 0, 0);
360         LoadFuncs(CCAPI_DLL, ccapi_fi, &hCcapi, 0, 1, 0, 0);
361
362         memset(&osvi, 0, sizeof(OSVERSIONINFO));
363         osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
364         GetVersionEx(&osvi);
365
366         // XXX: We should really use feature testing, first
367         // checking for CreateToolhelp32Snapshot.  If that's
368         // not around, we try the psapi stuff.
369         //
370         // Only load LSA functions if on NT/2000/XP
371         if(osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
372         {
373             // Windows 9x
374             LoadFuncs(TOOLHELPDLL, toolhelp_fi, &hToolHelp32, 0, 1, 0, 0);
375             hPsapi = 0;
376         }
377         else if(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
378         {
379             // Windows NT
380             LoadFuncs(PSAPIDLL, psapi_fi, &hPsapi, 0, 1, 0, 0);
381             hToolHelp32 = 0;
382         }
383
384
385         /*
386          * Register window class for the MITPasswordControl that
387          * replaces normal edit controls for password input.
388          * zero any fields we don't explicitly set
389          */
390         hLeashInst = hinstDLL;
391 #ifndef NO_KRB4
392         if (plsh_LoadKrb4LeashErrorTables)
393             plsh_LoadKrb4LeashErrorTables(hLeashInst, 0);
394 #endif
395
396         Register_MITPasswordEditControl(hLeashInst);
397
398 #ifndef NO_AFS
399         {
400             DWORD AfsStatus = 0;
401             GetAfsStatus(&AfsStatus);
402
403             AfsAvailable = afscompat_init();
404
405             if ( AfsStatus && !AfsAvailable )
406                 SetAfsStatus(0);
407         }
408 #endif
409         return TRUE;
410     }
411     case DLL_PROCESS_DETACH:
412 #ifndef NO_AFS
413         afscompat_close();
414 #endif
415 #ifndef NO_KRB4
416         if (hKrb4)
417             FreeLibrary(hKrb4);
418 #endif
419         if (hKrb5)
420             FreeLibrary(hKrb5);
421         if (hCcapi)
422             FreeLibrary(hCcapi);
423         if (hProfile)
424             FreeLibrary(hProfile);
425         if (hComErr)
426             FreeLibrary(hComErr);
427         if (hService)
428             FreeLibrary(hService);
429         if (hSecur32)
430             FreeLibrary(hSecur32);
431 #ifndef NO_KRB4
432         if (hKrb524)
433             FreeLibrary(hKrb524);
434 #endif
435         if (hPsapi)
436             FreeLibrary(hPsapi);
437         if (hToolHelp32)
438             FreeLibrary(hToolHelp32);
439
440         return TRUE;
441     default:
442         return TRUE;
443     }
444 }