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