xref: /freebsd/crypto/krb5/src/lib/krb5/ccache/ccapi/winccld.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 #if defined(_WIN32)
3 /*
4  * winccld.c --- routine for dynamically loading the ccache DLL if
5  * it's present.
6  */
7 
8 #include <windows.h>
9 #include <stdio.h>
10 #include "k5-int.h"
11 #include "stdcc.h"
12 
13 /* from fcc-proto.h */
14 extern const krb5_cc_ops krb5_fcc_ops;
15 
16 #define KRB5_WINCCLD_C_
17 #include "winccld.h"
18 
19 static int krb5_win_ccdll_loaded = 0;
20 
21 extern void krb5_win_ccdll_load(krb5_context context);
22 extern int krb5_is_ccdll_loaded(void);
23 
24 /*
25  * return codes
26  */
27 #define LF_OK           0
28 #define LF_NODLL        1
29 #define LF_NOFUNC       2
30 
31 #ifdef _WIN64
32 #define KRBCC_DLL      "krbcc64.dll"
33 #else
34 #define KRBCC_DLL      "krbcc32.dll"
35 #endif
36 
37 static int LoadFuncs(const char* dll_name, FUNC_INFO fi[],
38                      HINSTANCE* ph, int debug);
39 
LoadFuncs(const char * dll_name,FUNC_INFO fi[],HINSTANCE * ph,int debug)40 static int LoadFuncs(const char* dll_name, FUNC_INFO fi[],
41                      HINSTANCE* ph, int debug)
42 {
43     HINSTANCE h;
44     int i, n;
45     int error = 0;
46 
47     if (ph) *ph = 0;
48 
49     for (n = 0; fi[n].func_ptr_var; n++) {
50         *(fi[n].func_ptr_var) = 0;
51     }
52 
53     if (!(h = LoadLibrary(dll_name))) {
54         /* Get error for source debugging purposes. */
55         error = (int)GetLastError();
56         return LF_NODLL;
57     }
58 
59     if (debug)
60         printf("Loaded %s\n", dll_name);
61     for (i = 0; !error && (i < n); i++) {
62         void* p = (void*)GetProcAddress(h, fi[i].func_name);
63         if (!p) {
64             if (debug)
65                 printf("Could not get function: %s\n", fi[i].func_name);
66             error = 1;
67         } else {
68             *(fi[i].func_ptr_var) = p;
69             if (debug)
70                 printf("Loaded function %s at 0x%08X\n", fi[i].func_name, p);
71         }
72     }
73     if (error) {
74         for (i = 0; i < n; i++) {
75             *(fi[i].func_ptr_var) = 0;
76         }
77         FreeLibrary(h);
78         return LF_NOFUNC;
79     }
80     if (ph) *ph = h;
81     return LF_OK;
82 }
83 
krb5_win_ccdll_load(krb5_context context)84 void krb5_win_ccdll_load(krb5_context context)
85 {
86     krb5_cc_register(context, &krb5_fcc_ops, 0);
87     if (krb5_win_ccdll_loaded)
88         return;
89     if (LoadFuncs(KRBCC_DLL, krbcc_fi, 0, 0))
90         return;         /* Error, give up */
91     krb5_win_ccdll_loaded = 1;
92     krb5_cc_dfl_ops = &krb5_cc_stdcc_ops; /* Use stdcc! */
93 }
94 
krb5_is_ccdll_loaded(void)95 int krb5_is_ccdll_loaded(void)
96 {
97     return krb5_win_ccdll_loaded;
98 }
99 
100 #endif  /* Windows */
101