6e53793c4a4c4a7484a954a013ec0ad0ccef40d3
[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 DECL_FUNC_PTR(krb5_get_error_message);
147 DECL_FUNC_PTR(krb5_free_error_message);
148
149 // Krb524 functions
150 DECL_FUNC_PTR(krb524_init_ets);
151 DECL_FUNC_PTR(krb524_convert_creds_kdc);
152
153 // ComErr functions
154 DECL_FUNC_PTR(com_err);
155 DECL_FUNC_PTR(error_message);
156
157 // Profile functions
158 DECL_FUNC_PTR(profile_init);    
159 DECL_FUNC_PTR(profile_flush);
160 DECL_FUNC_PTR(profile_release); 
161 DECL_FUNC_PTR(profile_get_subsection_names);
162 DECL_FUNC_PTR(profile_free_list);
163 DECL_FUNC_PTR(profile_get_string);
164 DECL_FUNC_PTR(profile_get_values);
165 DECL_FUNC_PTR(profile_get_relation_names);
166 DECL_FUNC_PTR(profile_clear_relation);
167 DECL_FUNC_PTR(profile_add_relation);
168 DECL_FUNC_PTR(profile_update_relation);
169 DECL_FUNC_PTR(profile_release_string);
170
171 // Service functions
172 DECL_FUNC_PTR(OpenSCManagerA);
173 DECL_FUNC_PTR(OpenServiceA);
174 DECL_FUNC_PTR(QueryServiceStatus);
175 DECL_FUNC_PTR(CloseServiceHandle);
176 DECL_FUNC_PTR(LsaNtStatusToWinError);
177
178 // LSA Functions
179 DECL_FUNC_PTR(LsaConnectUntrusted);
180 DECL_FUNC_PTR(LsaLookupAuthenticationPackage);
181 DECL_FUNC_PTR(LsaCallAuthenticationPackage);
182 DECL_FUNC_PTR(LsaFreeReturnBuffer);
183 DECL_FUNC_PTR(LsaGetLogonSessionData);
184
185 // CCAPI
186 FUNC_INFO ccapi_fi[] = {
187     MAKE_FUNC_INFO(cc_initialize),
188     MAKE_FUNC_INFO(cc_shutdown),
189     MAKE_FUNC_INFO(cc_get_NC_info),
190     MAKE_FUNC_INFO(cc_free_NC_info),
191     END_FUNC_INFO
192 };
193
194 FUNC_INFO k4_fi[] = {
195     MAKE_FUNC_INFO(get_krb_err_txt_entry),
196     MAKE_FUNC_INFO(k_isinst),
197     MAKE_FUNC_INFO(k_isname),
198     MAKE_FUNC_INFO(k_isrealm),
199     MAKE_FUNC_INFO(kadm_change_your_password),
200     MAKE_FUNC_INFO(kname_parse),
201     MAKE_FUNC_INFO(krb_get_cred),
202     MAKE_FUNC_INFO(krb_get_krbhst),
203     MAKE_FUNC_INFO(krb_get_lrealm),
204     MAKE_FUNC_INFO(krb_get_pw_in_tkt),
205     MAKE_FUNC_INFO(krb_get_tf_realm),
206     MAKE_FUNC_INFO(krb_mk_req),
207     MAKE_FUNC_INFO(krb_realmofhost),
208     MAKE_FUNC_INFO(tf_init),
209     MAKE_FUNC_INFO(tf_close),
210     MAKE_FUNC_INFO(tf_get_cred),
211     MAKE_FUNC_INFO(tf_get_pname),
212     MAKE_FUNC_INFO(tf_get_pinst),
213     MAKE_FUNC_INFO(LocalHostAddr),
214     MAKE_FUNC_INFO(tkt_string),
215     MAKE_FUNC_INFO(krb_set_tkt_string),
216     MAKE_FUNC_INFO(initialize_krb_error_func),
217     MAKE_FUNC_INFO(initialize_kadm_error_table),
218     MAKE_FUNC_INFO(dest_tkt),
219     /*        MAKE_FUNC_INFO(lsh_LoadKrb4LeashErrorTables), */// XXX
220     MAKE_FUNC_INFO(krb_in_tkt),
221     MAKE_FUNC_INFO(krb_save_credentials),
222     MAKE_FUNC_INFO(krb_get_krbconf2),
223     MAKE_FUNC_INFO(krb_get_krbrealm2),
224     MAKE_FUNC_INFO(krb_life_to_time),
225     END_FUNC_INFO
226 };
227
228 FUNC_INFO k5_fi[] = {
229     MAKE_FUNC_INFO(krb5_change_password),
230     MAKE_FUNC_INFO(krb5_get_init_creds_opt_init),
231     MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_tkt_life),
232     MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_renew_life),
233     MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_forwardable),
234     MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_proxiable),
235     MAKE_FUNC_INFO(krb5_get_init_creds_opt_set_address_list),
236     MAKE_FUNC_INFO(krb5_get_init_creds_password),
237     MAKE_FUNC_INFO(krb5_get_prompt_types),
238     MAKE_FUNC_INFO(krb5_build_principal_ext),
239     MAKE_FUNC_INFO(krb5_cc_get_name),
240     MAKE_FUNC_INFO(krb5_cc_get_type),
241     MAKE_FUNC_INFO(krb5_cc_resolve),
242     MAKE_FUNC_INFO(krb5_cc_default),
243     MAKE_FUNC_INFO(krb5_cc_default_name),
244     MAKE_FUNC_INFO(krb5_cc_set_default_name),
245     MAKE_FUNC_INFO(krb5_cc_initialize),
246     MAKE_FUNC_INFO(krb5_cc_destroy),
247     MAKE_FUNC_INFO(krb5_cc_close),
248     MAKE_FUNC_INFO(krb5_cc_copy_creds),
249     MAKE_FUNC_INFO(krb5_cc_store_cred),
250     MAKE_FUNC_INFO(krb5_cc_retrieve_cred),
251     MAKE_FUNC_INFO(krb5_cc_get_principal),
252     MAKE_FUNC_INFO(krb5_cc_start_seq_get),
253     MAKE_FUNC_INFO(krb5_cc_next_cred),
254     MAKE_FUNC_INFO(krb5_cc_end_seq_get),
255     MAKE_FUNC_INFO(krb5_cc_remove_cred),
256     MAKE_FUNC_INFO(krb5_cc_set_flags),
257     // MAKE_FUNC_INFO(krb5_cc_get_type),
258     MAKE_FUNC_INFO(krb5_free_context),
259     MAKE_FUNC_INFO(krb5_free_cred_contents),
260     MAKE_FUNC_INFO(krb5_free_principal),
261     MAKE_FUNC_INFO(krb5_get_in_tkt_with_password),
262     MAKE_FUNC_INFO(krb5_init_context),
263     MAKE_FUNC_INFO(krb5_parse_name),
264     MAKE_FUNC_INFO(krb5_timeofday),
265     MAKE_FUNC_INFO(krb5_timestamp_to_sfstring),
266     MAKE_FUNC_INFO(krb5_unparse_name),
267     MAKE_FUNC_INFO(krb5_get_credentials),
268     MAKE_FUNC_INFO(krb5_mk_req),
269     MAKE_FUNC_INFO(krb5_sname_to_principal),
270     MAKE_FUNC_INFO(krb5_get_credentials_renew),
271     MAKE_FUNC_INFO(krb5_free_data),
272     MAKE_FUNC_INFO(krb5_free_data_contents),
273     //  MAKE_FUNC_INFO(krb5_get_realm_domain),
274     MAKE_FUNC_INFO(krb5_free_unparsed_name),
275     MAKE_FUNC_INFO(krb5_os_localaddr),
276     MAKE_FUNC_INFO(krb5_copy_keyblock_contents),
277     MAKE_FUNC_INFO(krb5_copy_data),
278     MAKE_FUNC_INFO(krb5_free_creds),
279     MAKE_FUNC_INFO(krb5_build_principal),
280     MAKE_FUNC_INFO(krb5_get_renewed_creds),
281     MAKE_FUNC_INFO(krb5_free_addresses),
282     MAKE_FUNC_INFO(krb5_get_default_config_files),
283     MAKE_FUNC_INFO(krb5_free_config_files),
284     MAKE_FUNC_INFO(krb5_get_default_realm),
285     MAKE_FUNC_INFO(krb5_set_default_realm),
286     MAKE_FUNC_INFO(krb5_free_ticket),
287     MAKE_FUNC_INFO(krb5_decode_ticket),
288     MAKE_FUNC_INFO(krb5_get_host_realm),
289     MAKE_FUNC_INFO(krb5_free_host_realm),
290     MAKE_FUNC_INFO(krb5_c_random_make_octets),
291     MAKE_FUNC_INFO(krb5_free_default_realm),
292     MAKE_FUNC_INFO(krb5_get_error_message),
293     MAKE_FUNC_INFO(krb5_free_error_message),
294     END_FUNC_INFO
295 };
296
297 FUNC_INFO k524_fi[] = {
298     MAKE_FUNC_INFO(krb524_init_ets),
299     MAKE_FUNC_INFO(krb524_convert_creds_kdc),
300     END_FUNC_INFO
301 };
302
303 FUNC_INFO profile_fi[] = {
304     MAKE_FUNC_INFO(profile_init),
305     MAKE_FUNC_INFO(profile_flush),
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_get_values),
311     MAKE_FUNC_INFO(profile_get_relation_names),
312     MAKE_FUNC_INFO(profile_clear_relation),
313     MAKE_FUNC_INFO(profile_add_relation),
314     MAKE_FUNC_INFO(profile_update_relation),
315     MAKE_FUNC_INFO(profile_release_string),
316     END_FUNC_INFO
317 };
318
319 FUNC_INFO ce_fi[] = {
320     MAKE_FUNC_INFO(com_err),
321     MAKE_FUNC_INFO(error_message),
322     END_FUNC_INFO
323 };
324
325 FUNC_INFO service_fi[] = {
326     MAKE_FUNC_INFO(OpenSCManagerA),
327     MAKE_FUNC_INFO(OpenServiceA),
328     MAKE_FUNC_INFO(QueryServiceStatus),
329     MAKE_FUNC_INFO(CloseServiceHandle),
330     MAKE_FUNC_INFO(LsaNtStatusToWinError),
331     END_FUNC_INFO
332 };
333
334 FUNC_INFO lsa_fi[] = {
335     MAKE_FUNC_INFO(LsaConnectUntrusted),
336     MAKE_FUNC_INFO(LsaLookupAuthenticationPackage),
337     MAKE_FUNC_INFO(LsaCallAuthenticationPackage),
338     MAKE_FUNC_INFO(LsaFreeReturnBuffer),
339     MAKE_FUNC_INFO(LsaGetLogonSessionData),
340     END_FUNC_INFO
341 };
342
343 // psapi functions
344 DECL_FUNC_PTR(GetModuleFileNameExA);
345 DECL_FUNC_PTR(EnumProcessModules);
346
347 FUNC_INFO psapi_fi[] = {
348     MAKE_FUNC_INFO(GetModuleFileNameExA),
349         MAKE_FUNC_INFO(EnumProcessModules),
350         END_FUNC_INFO
351 };
352
353 // toolhelp functions
354 DECL_FUNC_PTR(CreateToolhelp32Snapshot);
355 DECL_FUNC_PTR(Module32First);
356 DECL_FUNC_PTR(Module32Next);
357
358 FUNC_INFO toolhelp_fi[] = {
359     MAKE_FUNC_INFO(CreateToolhelp32Snapshot),
360         MAKE_FUNC_INFO(Module32First),
361         MAKE_FUNC_INFO(Module32Next),
362         END_FUNC_INFO
363 };
364
365 khm_int32 init_imports(void) {
366     OSVERSIONINFO osvi;
367     int imp_rv = 1;
368
369 #define CKRV if(!imp_rv) goto _err_ret
370
371 #ifndef _WIN64
372     imp_rv = LoadFuncs(KRB4_DLL, k4_fi, &hKrb4, 0, 1, 0, 0);
373     CKRV;
374 #endif
375
376     imp_rv = LoadFuncs(KRB5_DLL, k5_fi, &hKrb5, 0, 1, 0, 0);
377     CKRV;
378
379     imp_rv = LoadFuncs(COMERR_DLL, ce_fi, &hComErr, 0, 0, 1, 0);
380     CKRV;
381
382     imp_rv = LoadFuncs(SERVICE_DLL, service_fi, &hService, 0, 1, 0, 0);
383     CKRV;
384
385     imp_rv = LoadFuncs(SECUR32_DLL, lsa_fi, &hSecur32, 0, 1, 1, 1);
386     CKRV;
387 #ifndef _WIN64
388     imp_rv = LoadFuncs(KRB524_DLL, k524_fi, &hKrb524, 0, 1, 1, 1);
389     CKRV;
390 #endif
391     imp_rv = LoadFuncs(PROFILE_DLL, profile_fi, &hProfile, 0, 1, 0, 0);
392     CKRV;
393
394     imp_rv = LoadFuncs(CCAPI_DLL, ccapi_fi, &hCCAPI, 0, 1, 0, 0);
395     /* CCAPI_DLL is optional.  No error check. */
396
397     memset(&osvi, 0, sizeof(OSVERSIONINFO));
398     osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
399     GetVersionEx(&osvi);
400
401     // XXX: We should really use feature testing, first
402     // checking for CreateToolhelp32Snapshot.  If that's
403     // not around, we try the psapi stuff.
404     //
405     // Only load LSA functions if on NT/2000/XP
406     if(osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
407     {
408         // Windows 9x
409         imp_rv = LoadFuncs(TOOLHELPDLL, toolhelp_fi, &hToolHelp32, 0, 1, 0, 0);
410         CKRV;
411
412         hPsapi = 0;
413     }             
414     else if(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
415     {
416         // Windows NT
417         imp_rv = LoadFuncs(PSAPIDLL, psapi_fi, &hPsapi, 0, 1, 0, 0);
418         CKRV;
419
420         hToolHelp32 = 0;
421     }
422
423     AfsAvailable = TRUE; //afscompat_init();
424
425     return KHM_ERROR_SUCCESS;
426
427  _err_ret:
428     return KHM_ERROR_NOT_FOUND;
429 }
430
431 khm_int32 exit_imports(void) {
432     //afscompat_close();
433
434     if (hKrb4)
435         FreeLibrary(hKrb4);
436     if (hKrb5)
437         FreeLibrary(hKrb5);
438     if (hProfile)
439         FreeLibrary(hProfile);
440     if (hComErr)
441         FreeLibrary(hComErr);
442     if (hService)
443         FreeLibrary(hService);
444     if (hSecur32)
445         FreeLibrary(hSecur32);
446     if (hKrb524)
447         FreeLibrary(hKrb524);
448     if (hPsapi)
449         FreeLibrary(hPsapi);
450     if (hToolHelp32)
451         FreeLibrary(hToolHelp32);
452
453     return KHM_ERROR_SUCCESS;
454 }
455
456 int (*Lcom_err)(LPSTR,long,LPSTR,...);
457 LPSTR (*Lerror_message)(long);
458 LPSTR (*Lerror_table_name)(long);
459
460 #pragma warning (disable: 4213)
461 void Leash_load_com_err_callback(FARPROC ce,
462                                  FARPROC em,
463                                  FARPROC etn)
464 {
465     (FARPROC)Lcom_err=ce;
466     (FARPROC)Lerror_message=em;
467     (FARPROC)Lerror_table_name=etn;
468 }