smb-vc-reset-on-request-20090218
[openafs.git] / src / WINNT / netidmgr_plugin / dynimport.c
1 /*
2 * Copyright (c) 2005 Massachusetts Institute of Technology
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 /* $Id$ */
26
27 #include<windows.h>
28 #include<khdefs.h>
29 #include<kherror.h>
30 #include<dynimport.h>
31
32 HINSTANCE hKrb4 = 0;
33 HINSTANCE hKrb5 = 0;
34 HINSTANCE hKrb524 = 0;
35 HINSTANCE hSecur32 = 0;
36 HINSTANCE hComErr = 0;
37 HINSTANCE hService = 0;
38 HINSTANCE hProfile = 0;
39 HINSTANCE hPsapi = 0; 
40 HINSTANCE hToolHelp32 = 0; 
41 HINSTANCE hCCAPI = 0;
42
43 DWORD     AfsAvailable = 0;
44
45 // CCAPI
46 DECL_FUNC_PTR(cc_initialize);
47 DECL_FUNC_PTR(cc_shutdown);
48 DECL_FUNC_PTR(cc_get_NC_info);
49 DECL_FUNC_PTR(cc_free_NC_info);
50
51 // krb4 functions
52 DECL_FUNC_PTR(get_krb_err_txt_entry);
53 DECL_FUNC_PTR(k_isinst);
54 DECL_FUNC_PTR(k_isname);
55 DECL_FUNC_PTR(k_isrealm);
56 DECL_FUNC_PTR(kadm_change_your_password);
57 DECL_FUNC_PTR(kname_parse);
58 DECL_FUNC_PTR(krb_get_cred);
59 DECL_FUNC_PTR(krb_get_krbhst);
60 DECL_FUNC_PTR(krb_get_lrealm);
61 DECL_FUNC_PTR(krb_get_pw_in_tkt);
62 DECL_FUNC_PTR(krb_get_tf_realm);
63 DECL_FUNC_PTR(krb_mk_req);
64 DECL_FUNC_PTR(krb_realmofhost);
65 DECL_FUNC_PTR(tf_init);
66 DECL_FUNC_PTR(tf_close);
67 DECL_FUNC_PTR(tf_get_cred);
68 DECL_FUNC_PTR(tf_get_pname);
69 DECL_FUNC_PTR(tf_get_pinst);
70 DECL_FUNC_PTR(LocalHostAddr);
71 DECL_FUNC_PTR(tkt_string);
72 DECL_FUNC_PTR(krb_set_tkt_string);
73 DECL_FUNC_PTR(initialize_krb_error_func);
74 DECL_FUNC_PTR(initialize_kadm_error_table);
75 DECL_FUNC_PTR(dest_tkt);
76 DECL_FUNC_PTR(krb_in_tkt);
77 DECL_FUNC_PTR(krb_save_credentials);
78 DECL_FUNC_PTR(krb_get_krbconf2);
79 DECL_FUNC_PTR(krb_get_krbrealm2);
80 DECL_FUNC_PTR(krb_life_to_time);
81
82 // krb5 functions
83 DECL_FUNC_PTR(krb5_change_password);
84 DECL_FUNC_PTR(krb5_get_init_creds_opt_init);
85 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_tkt_life);
86 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_renew_life);
87 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_forwardable);
88 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_proxiable);
89 DECL_FUNC_PTR(krb5_get_init_creds_opt_set_address_list);
90 DECL_FUNC_PTR(krb5_get_init_creds_password);
91 DECL_FUNC_PTR(krb5_get_prompt_types);
92 DECL_FUNC_PTR(krb5_build_principal_ext);
93 DECL_FUNC_PTR(krb5_cc_get_name);
94 DECL_FUNC_PTR(krb5_cc_get_type);
95 DECL_FUNC_PTR(krb5_cc_resolve);
96 DECL_FUNC_PTR(krb5_cc_default);
97 DECL_FUNC_PTR(krb5_cc_default_name);
98 DECL_FUNC_PTR(krb5_cc_set_default_name);
99 DECL_FUNC_PTR(krb5_cc_initialize);
100 DECL_FUNC_PTR(krb5_cc_destroy);
101 DECL_FUNC_PTR(krb5_cc_close);
102 DECL_FUNC_PTR(krb5_cc_store_cred);
103 DECL_FUNC_PTR(krb5_cc_copy_creds);
104 DECL_FUNC_PTR(krb5_cc_retrieve_cred);
105 DECL_FUNC_PTR(krb5_cc_get_principal);
106 DECL_FUNC_PTR(krb5_cc_start_seq_get);
107 DECL_FUNC_PTR(krb5_cc_next_cred);
108 DECL_FUNC_PTR(krb5_cc_end_seq_get);
109 DECL_FUNC_PTR(krb5_cc_remove_cred);
110 DECL_FUNC_PTR(krb5_cc_set_flags);
111 // DECL_FUNC_PTR(krb5_cc_get_type);
112 DECL_FUNC_PTR(krb5_free_context);
113 DECL_FUNC_PTR(krb5_free_cred_contents);
114 DECL_FUNC_PTR(krb5_free_principal);
115 DECL_FUNC_PTR(krb5_get_in_tkt_with_password);
116 DECL_FUNC_PTR(krb5_init_context);
117 DECL_FUNC_PTR(krb5_parse_name);
118 DECL_FUNC_PTR(krb5_timeofday);
119 DECL_FUNC_PTR(krb5_timestamp_to_sfstring);
120 DECL_FUNC_PTR(krb5_unparse_name);
121 DECL_FUNC_PTR(krb5_get_credentials);
122 DECL_FUNC_PTR(krb5_mk_req);
123 DECL_FUNC_PTR(krb5_sname_to_principal);
124 DECL_FUNC_PTR(krb5_get_credentials_renew);
125 DECL_FUNC_PTR(krb5_free_data);
126 DECL_FUNC_PTR(krb5_free_data_contents);
127 // DECL_FUNC_PTR(krb5_get_realm_domain);
128 DECL_FUNC_PTR(krb5_free_unparsed_name);
129 DECL_FUNC_PTR(krb5_os_localaddr);
130 DECL_FUNC_PTR(krb5_copy_keyblock_contents);
131 DECL_FUNC_PTR(krb5_copy_data);
132 DECL_FUNC_PTR(krb5_free_creds);
133 DECL_FUNC_PTR(krb5_build_principal);
134 DECL_FUNC_PTR(krb5_get_renewed_creds);
135 DECL_FUNC_PTR(krb5_get_default_config_files);
136 DECL_FUNC_PTR(krb5_free_config_files);
137 DECL_FUNC_PTR(krb5_get_default_realm);
138 DECL_FUNC_PTR(krb5_set_default_realm);
139 DECL_FUNC_PTR(krb5_free_ticket);
140 DECL_FUNC_PTR(krb5_decode_ticket);
141 DECL_FUNC_PTR(krb5_get_host_realm);
142 DECL_FUNC_PTR(krb5_free_host_realm);
143 DECL_FUNC_PTR(krb5_c_random_make_octets);
144 DECL_FUNC_PTR(krb5_free_addresses);
145 DECL_FUNC_PTR(krb5_free_default_realm);
146
147 // Krb524 functions
148 DECL_FUNC_PTR(krb524_init_ets);
149 DECL_FUNC_PTR(krb524_convert_creds_kdc);
150
151 // ComErr functions
152 DECL_FUNC_PTR(com_err);
153 DECL_FUNC_PTR(error_message);
154
155 // Profile functions
156 DECL_FUNC_PTR(profile_init);    
157 DECL_FUNC_PTR(profile_flush);
158 DECL_FUNC_PTR(profile_release); 
159 DECL_FUNC_PTR(profile_get_subsection_names);
160 DECL_FUNC_PTR(profile_free_list);
161 DECL_FUNC_PTR(profile_get_string);
162 DECL_FUNC_PTR(profile_get_values);
163 DECL_FUNC_PTR(profile_get_relation_names);
164 DECL_FUNC_PTR(profile_clear_relation);
165 DECL_FUNC_PTR(profile_add_relation);
166 DECL_FUNC_PTR(profile_update_relation);
167 DECL_FUNC_PTR(profile_release_string);
168
169 // Service functions
170 DECL_FUNC_PTR(OpenSCManagerA);
171 DECL_FUNC_PTR(OpenServiceA);
172 DECL_FUNC_PTR(QueryServiceStatus);
173 DECL_FUNC_PTR(CloseServiceHandle);
174 DECL_FUNC_PTR(LsaNtStatusToWinError);
175
176 // LSA Functions
177 DECL_FUNC_PTR(LsaConnectUntrusted);
178 DECL_FUNC_PTR(LsaLookupAuthenticationPackage);
179 DECL_FUNC_PTR(LsaCallAuthenticationPackage);
180 DECL_FUNC_PTR(LsaFreeReturnBuffer);
181 DECL_FUNC_PTR(LsaGetLogonSessionData);
182
183 // CCAPI
184 FUNC_INFO ccapi_fi[] = {
185     MAKE_FUNC_INFO(cc_initialize),
186     MAKE_FUNC_INFO(cc_shutdown),
187     MAKE_FUNC_INFO(cc_get_NC_info),
188     MAKE_FUNC_INFO(cc_free_NC_info),
189     END_FUNC_INFO
190 };
191
192 FUNC_INFO k4_fi[] = {
193     MAKE_FUNC_INFO(get_krb_err_txt_entry),
194     MAKE_FUNC_INFO(k_isinst),
195     MAKE_FUNC_INFO(k_isname),
196     MAKE_FUNC_INFO(k_isrealm),
197     MAKE_FUNC_INFO(kadm_change_your_password),
198     MAKE_FUNC_INFO(kname_parse),
199     MAKE_FUNC_INFO(krb_get_cred),
200     MAKE_FUNC_INFO(krb_get_krbhst),
201     MAKE_FUNC_INFO(krb_get_lrealm),
202     MAKE_FUNC_INFO(krb_get_pw_in_tkt),
203     MAKE_FUNC_INFO(krb_get_tf_realm),
204     MAKE_FUNC_INFO(krb_mk_req),
205     MAKE_FUNC_INFO(krb_realmofhost),
206     MAKE_FUNC_INFO(tf_init),
207     MAKE_FUNC_INFO(tf_close),
208     MAKE_FUNC_INFO(tf_get_cred),
209     MAKE_FUNC_INFO(tf_get_pname),
210     MAKE_FUNC_INFO(tf_get_pinst),
211     MAKE_FUNC_INFO(LocalHostAddr),
212     MAKE_FUNC_INFO(tkt_string),
213     MAKE_FUNC_INFO(krb_set_tkt_string),
214     MAKE_FUNC_INFO(initialize_krb_error_func),
215     MAKE_FUNC_INFO(initialize_kadm_error_table),
216     MAKE_FUNC_INFO(dest_tkt),
217     /*        MAKE_FUNC_INFO(lsh_LoadKrb4LeashErrorTables), */// XXX
218     MAKE_FUNC_INFO(krb_in_tkt),
219     MAKE_FUNC_INFO(krb_save_credentials),
220     MAKE_FUNC_INFO(krb_get_krbconf2),
221     MAKE_FUNC_INFO(krb_get_krbrealm2),
222     MAKE_FUNC_INFO(krb_life_to_time),
223     END_FUNC_INFO
224 };
225
226 FUNC_INFO k5_fi[] = {
227     MAKE_FUNC_INFO(krb5_change_password),
228     MAKE_FUNC_INFO(krb5_get_init_creds_opt_init),
229     MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_tkt_life),
230     MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_renew_life),
231     MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_forwardable),
232     MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_proxiable),
233     MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_address_list),
234     MAKE_FUNC_INFO(krb5_get_init_creds_password),
235     MAKE_FUNC_INFO(krb5_get_prompt_types),
236     MAKE_FUNC_INFO(krb5_build_principal_ext),
237     MAKE_FUNC_INFO(krb5_cc_get_name),
238     MAKE_FUNC_INFO(krb5_cc_get_type),
239     MAKE_FUNC_INFO(krb5_cc_resolve),
240     MAKE_FUNC_INFO(krb5_cc_default),
241     MAKE_FUNC_INFO(krb5_cc_default_name),
242     MAKE_FUNC_INFO(krb5_cc_set_default_name),
243     MAKE_FUNC_INFO(krb5_cc_initialize),
244     MAKE_FUNC_INFO(krb5_cc_destroy),
245     MAKE_FUNC_INFO(krb5_cc_close),
246     MAKE_FUNC_INFO(krb5_cc_copy_creds),
247     MAKE_FUNC_INFO(krb5_cc_store_cred),
248     MAKE_FUNC_INFO(krb5_cc_retrieve_cred),
249     MAKE_FUNC_INFO(krb5_cc_get_principal),
250     MAKE_FUNC_INFO(krb5_cc_start_seq_get),
251     MAKE_FUNC_INFO(krb5_cc_next_cred),
252     MAKE_FUNC_INFO(krb5_cc_end_seq_get),
253     MAKE_FUNC_INFO(krb5_cc_remove_cred),
254     MAKE_FUNC_INFO(krb5_cc_set_flags),
255     // MAKE_FUNC_INFO(krb5_cc_get_type),
256     MAKE_FUNC_INFO(krb5_free_context),
257     MAKE_FUNC_INFO(krb5_free_cred_contents),
258     MAKE_FUNC_INFO(krb5_free_principal),
259     MAKE_FUNC_INFO(krb5_get_in_tkt_with_password),
260     MAKE_FUNC_INFO(krb5_init_context),
261     MAKE_FUNC_INFO(krb5_parse_name),
262     MAKE_FUNC_INFO(krb5_timeofday),
263     MAKE_FUNC_INFO(krb5_timestamp_to_sfstring),
264     MAKE_FUNC_INFO(krb5_unparse_name),
265     MAKE_FUNC_INFO(krb5_get_credentials),
266     MAKE_FUNC_INFO(krb5_mk_req),
267     MAKE_FUNC_INFO(krb5_sname_to_principal),
268     MAKE_FUNC_INFO(krb5_get_credentials_renew),
269     MAKE_FUNC_INFO(krb5_free_data),
270     MAKE_FUNC_INFO(krb5_free_data_contents),
271     //  MAKE_FUNC_INFO(krb5_get_realm_domain),
272     MAKE_FUNC_INFO(krb5_free_unparsed_name),
273     MAKE_FUNC_INFO(krb5_os_localaddr),
274     MAKE_FUNC_INFO(krb5_copy_keyblock_contents),
275     MAKE_FUNC_INFO(krb5_copy_data),
276     MAKE_FUNC_INFO(krb5_free_creds),
277     MAKE_FUNC_INFO(krb5_build_principal),
278     MAKE_FUNC_INFO(krb5_get_renewed_creds),
279     MAKE_FUNC_INFO(krb5_free_addresses),
280     MAKE_FUNC_INFO(krb5_get_default_config_files),
281     MAKE_FUNC_INFO(krb5_free_config_files),
282     MAKE_FUNC_INFO(krb5_get_default_realm),
283     MAKE_FUNC_INFO(krb5_set_default_realm),
284     MAKE_FUNC_INFO(krb5_free_ticket),
285     MAKE_FUNC_INFO(krb5_decode_ticket),
286     MAKE_FUNC_INFO(krb5_get_host_realm),
287     MAKE_FUNC_INFO(krb5_free_host_realm),
288     MAKE_FUNC_INFO(krb5_c_random_make_octets),
289     MAKE_FUNC_INFO(krb5_free_default_realm),
290     END_FUNC_INFO
291 };
292
293 FUNC_INFO k524_fi[] = {
294     MAKE_FUNC_INFO(krb524_init_ets),
295     MAKE_FUNC_INFO(krb524_convert_creds_kdc),
296     END_FUNC_INFO
297 };
298
299 FUNC_INFO profile_fi[] = {
300     MAKE_FUNC_INFO(profile_init),
301     MAKE_FUNC_INFO(profile_flush),
302     MAKE_FUNC_INFO(profile_release), 
303     MAKE_FUNC_INFO(profile_get_subsection_names),
304     MAKE_FUNC_INFO(profile_free_list),
305     MAKE_FUNC_INFO(profile_get_string),
306     MAKE_FUNC_INFO(profile_get_values),
307     MAKE_FUNC_INFO(profile_get_relation_names),
308     MAKE_FUNC_INFO(profile_clear_relation),
309     MAKE_FUNC_INFO(profile_add_relation),
310     MAKE_FUNC_INFO(profile_update_relation),
311     MAKE_FUNC_INFO(profile_release_string),
312     END_FUNC_INFO
313 };
314
315 FUNC_INFO ce_fi[] = {
316     MAKE_FUNC_INFO(com_err),
317     MAKE_FUNC_INFO(error_message),
318     END_FUNC_INFO
319 };
320
321 FUNC_INFO service_fi[] = {
322     MAKE_FUNC_INFO(OpenSCManagerA),
323     MAKE_FUNC_INFO(OpenServiceA),
324     MAKE_FUNC_INFO(QueryServiceStatus),
325     MAKE_FUNC_INFO(CloseServiceHandle),
326     MAKE_FUNC_INFO(LsaNtStatusToWinError),
327     END_FUNC_INFO
328 };
329
330 FUNC_INFO lsa_fi[] = {
331     MAKE_FUNC_INFO(LsaConnectUntrusted),
332     MAKE_FUNC_INFO(LsaLookupAuthenticationPackage),
333     MAKE_FUNC_INFO(LsaCallAuthenticationPackage),
334     MAKE_FUNC_INFO(LsaFreeReturnBuffer),
335     MAKE_FUNC_INFO(LsaGetLogonSessionData),
336     END_FUNC_INFO
337 };
338
339 // psapi functions
340 DECL_FUNC_PTR(GetModuleFileNameExA);
341 DECL_FUNC_PTR(EnumProcessModules);
342
343 FUNC_INFO psapi_fi[] = {
344     MAKE_FUNC_INFO(GetModuleFileNameExA),
345         MAKE_FUNC_INFO(EnumProcessModules),
346         END_FUNC_INFO
347 };
348
349 // toolhelp functions
350 DECL_FUNC_PTR(CreateToolhelp32Snapshot);
351 DECL_FUNC_PTR(Module32First);
352 DECL_FUNC_PTR(Module32Next);
353
354 FUNC_INFO toolhelp_fi[] = {
355     MAKE_FUNC_INFO(CreateToolhelp32Snapshot),
356         MAKE_FUNC_INFO(Module32First),
357         MAKE_FUNC_INFO(Module32Next),
358         END_FUNC_INFO
359 };
360
361 khm_int32 init_imports(void) {
362     OSVERSIONINFO osvi;
363     int imp_rv = 1;
364
365 #define CKRV if(!imp_rv) goto _err_ret
366
367 #ifndef _WIN64
368     imp_rv = LoadFuncs(KRB4_DLL, k4_fi, &hKrb4, 0, 1, 0, 0);
369     CKRV;
370 #endif
371
372     imp_rv = LoadFuncs(KRB5_DLL, k5_fi, &hKrb5, 0, 1, 0, 0);
373     CKRV;
374
375     imp_rv = LoadFuncs(COMERR_DLL, ce_fi, &hComErr, 0, 0, 1, 0);
376     CKRV;
377
378     imp_rv = LoadFuncs(SERVICE_DLL, service_fi, &hService, 0, 1, 0, 0);
379     CKRV;
380
381     imp_rv = LoadFuncs(SECUR32_DLL, lsa_fi, &hSecur32, 0, 1, 1, 1);
382     CKRV;
383 #ifndef _WIN64
384     imp_rv = LoadFuncs(KRB524_DLL, k524_fi, &hKrb524, 0, 1, 1, 1);
385     CKRV;
386 #endif
387     imp_rv = LoadFuncs(PROFILE_DLL, profile_fi, &hProfile, 0, 1, 0, 0);
388     CKRV;
389
390     imp_rv = LoadFuncs(CCAPI_DLL, ccapi_fi, &hCCAPI, 0, 1, 0, 0);
391     /* CCAPI_DLL is optional.  No error check. */
392
393     memset(&osvi, 0, sizeof(OSVERSIONINFO));
394     osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
395     GetVersionEx(&osvi);
396
397     // XXX: We should really use feature testing, first
398     // checking for CreateToolhelp32Snapshot.  If that's
399     // not around, we try the psapi stuff.
400     //
401     // Only load LSA functions if on NT/2000/XP
402     if(osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
403     {
404         // Windows 9x
405         imp_rv = LoadFuncs(TOOLHELPDLL, toolhelp_fi, &hToolHelp32, 0, 1, 0, 0);
406         CKRV;
407
408         hPsapi = 0;
409     }             
410     else if(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
411     {
412         // Windows NT
413         imp_rv = LoadFuncs(PSAPIDLL, psapi_fi, &hPsapi, 0, 1, 0, 0);
414         CKRV;
415
416         hToolHelp32 = 0;
417     }
418
419     AfsAvailable = TRUE; //afscompat_init();
420
421     return KHM_ERROR_SUCCESS;
422
423  _err_ret:
424     return KHM_ERROR_NOT_FOUND;
425 }
426
427 khm_int32 exit_imports(void) {
428     //afscompat_close();
429
430     if (hKrb4)
431         FreeLibrary(hKrb4);
432     if (hKrb5)
433         FreeLibrary(hKrb5);
434     if (hProfile)
435         FreeLibrary(hProfile);
436     if (hComErr)
437         FreeLibrary(hComErr);
438     if (hService)
439         FreeLibrary(hService);
440     if (hSecur32)
441         FreeLibrary(hSecur32);
442     if (hKrb524)
443         FreeLibrary(hKrb524);
444     if (hPsapi)
445         FreeLibrary(hPsapi);
446     if (hToolHelp32)
447         FreeLibrary(hToolHelp32);
448
449     return KHM_ERROR_SUCCESS;
450 }
451
452 int (*Lcom_err)(LPSTR,long,LPSTR,...);
453 LPSTR (*Lerror_message)(long);
454 LPSTR (*Lerror_table_name)(long);
455
456 #pragma warning (disable: 4213)
457 void Leash_load_com_err_callback(FARPROC ce,
458                                  FARPROC em,
459                                  FARPROC etn)
460 {
461     (FARPROC)Lcom_err=ce;
462     (FARPROC)Lerror_message=em;
463     (FARPROC)Lerror_table_name=etn;
464 }