-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathloadlib.c
524 lines (496 loc) · 12.8 KB
/
loadlib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*------
* Module: loadlib.c
*
* Description: This module contains routines related to
* delay load import libraries.
*
* Comments: See "readme.txt" for copyright and license information.
*-------
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#ifndef WIN32
#include <errno.h>
#endif /* WIN32 */
#include "loadlib.h"
#ifdef USE_LIBPQ
#ifdef RESET_CRYPTO_CALLBACKS
#include <openssl/ssl.h>
#endif /* RESET_CRYPTO_CALLBACKS */
#include <libpq-fe.h>
#endif /* USE_LIBPQ */
#include "pgenlist.h"
#ifdef WIN32
#ifdef _MSC_VER
#pragma comment(lib, "Delayimp")
#ifdef USE_LIBPQ
#pragma comment(lib, "libpq")
#pragma comment(lib, "ssleay32")
#ifdef RESET_CRYPTO_CALLBACKS
#pragma comment(lib, "libeay32")
#endif /* RESET_CRYPTO_CALLBACKS */
#endif /* USE_LIBPQ */
#ifdef _HANDLE_ENLIST_IN_DTC_
#ifdef UNICODE_SUPPORT
#pragma comment(lib, "pgenlist")
#else
#pragma comment(lib, "pgenlista")
#endif /* UNICODE_SUPPORT */
#endif /* _HANDLE_ENLIST_IN_DTC_ */
// The followings works under VC++6.0 but doesn't work under VC++7.0.
// Please add the equivalent linker options using command line etc.
#if (_MSC_VER == 1200) && defined(DYNAMIC_LOAD) // VC6.0
#ifdef USE_LIBPQ
#pragma comment(linker, "/Delayload:libpq.dll")
#pragma comment(linker, "/Delayload:ssleay32.dll")
#ifdef RESET_CRYPTO_CALLBACKS
#pragma comment(linker, "/Delayload:libeay32.dll")
#endif /* RESET_CRYPTO_CALLBACKS */
#endif /* USE_LIBPQ */
#ifdef UNICODE_SUPPORT
#pragma comment(linker, "/Delayload:pgenlist.dll")
#else
#pragma comment(linker, "/Delayload:pgenlista.dll")
#endif /* UNICODE_SUPPORT */
#pragma comment(linker, "/Delay:UNLOAD")
#endif /* _MSC_VER */
#endif /* _MSC_VER */
#if defined(DYNAMIC_LOAD)
#define WIN_DYN_LOAD
CSTR libpqdll = "LIBPQ.dll";
#ifdef _WIN64
CSTR gssapidll = "GSSAPI64.dll";
#else
CSTR gssapidll = "GSSAPI32.dll";
#endif /* _WIN64 */
#ifdef UNICODE_SUPPORT
CSTR pgenlist = "pgenlist";
CSTR pgenlistdll = "PGENLIST.dll";
#else
CSTR pgenlist = "pgenlista";
CSTR pgenlistdll = "PGENLISTA.dll";
#endif /* UNICODE_SUPPORT */
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
#define _MSC_DELAY_LOAD_IMPORT
#endif /* MSC_VER */
#endif /* DYNAMIC_LOAD */
#endif /* WIN32 */
#if defined(_MSC_DELAY_LOAD_IMPORT)
#if (_MSC_VER < 1300)
#define TRY_DLI_HOOK \
__try { \
__pfnDliFailureHook = DliErrorHook; \
__pfnDliNotifyHook = DliErrorHook;
#define RELEASE_NOTIFY_HOOK \
__pfnDliNotifyHook = NULL;
#else
#define TRY_DLI_HOOK \
__try { \
__pfnDliFailureHook2 = DliErrorHook; \
__pfnDliNotifyHook2 = DliErrorHook;
#define RELEASE_NOTIFY_HOOK \
__pfnDliNotifyHook2 = NULL;
#endif /* _MSC_VER */
#else
#define TRY_DLI_HOOK
#define RELEASE_NOTIFY_HOOK
#endif /* _MSC_DELAY_LOAD_IMPORT */
CSTR libpqlib = "libpq";
#ifdef _WIN64
CSTR gssapilib = "gssapi64";
#else
CSTR gssapilib = "gssapi32";
#endif /* _WIN64 */
#ifdef USE_LIBPQ
CSTR checkproc1 = "PQconnectdbParams";
static int connect_withparam_available = -1;
CSTR checkproc2 = "PQconninfoParse";
static int sslverify_available = -1;
#endif /* USE_LIBPQ */
#if defined(_MSC_DELAY_LOAD_IMPORT)
static BOOL loaded_libpq = FALSE, loaded_ssllib = FALSE;
static BOOL loaded_pgenlist = FALSE, loaded_gssapi = FALSE;
/*
* Load psqlodbc path based libpq dll.
*/
static HMODULE MODULE_load_from_psqlodbc_path(const char *module_name)
{
extern HINSTANCE s_hModule;
HMODULE hmodule = NULL;
char szFileName[MAX_PATH];
if (GetModuleFileName(s_hModule, szFileName, sizeof(szFileName)) > 0)
{
char drive[_MAX_DRIVE], dir[_MAX_DIR], sysdir[MAX_PATH];
_splitpath(szFileName, drive, dir, NULL, NULL);
GetSystemDirectory(sysdir, MAX_PATH);
snprintf(szFileName, sizeof(szFileName), "%s%s%s.dll", drive, dir, module_name);
if (_strnicmp(szFileName, sysdir, strlen(sysdir)) != 0)
{
hmodule = LoadLibraryEx(szFileName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
mylog("psqlodbc path based %s loaded module=%p\n", module_name, hmodule);
}
}
return hmodule;
}
/*
* Error hook function for delay load import.
* Try to load psqlodbc path based libpq.
* Load alternative ssl library SSLEAY32 or LIBSSL32.
*/
#if (_MSC_VER < 1300)
extern PfnDliHook __pfnDliFailureHook;
extern PfnDliHook __pfnDliNotifyHook;
#else
extern PfnDliHook __pfnDliFailureHook2;
extern PfnDliHook __pfnDliNotifyHook2;
#endif /* _MSC_VER */
static FARPROC WINAPI
DliErrorHook(unsigned dliNotify,
PDelayLoadInfo pdli)
{
HMODULE hmodule = NULL;
int i;
static const char * const libarray[] = {"libssl32", "ssleay32"};
mylog("Dli%sHook %s Notify=%d\n", (dliFailLoadLib == dliNotify || dliFailGetProc == dliNotify) ? "Error" : "Notify", NULL != pdli->szDll ? pdli->szDll : pdli->dlp.szProcName, dliNotify);
switch (dliNotify)
{
case dliNotePreLoadLibrary:
case dliFailLoadLib:
RELEASE_NOTIFY_HOOK
if (_strnicmp(pdli->szDll, libpqlib, strlen(libpqlib)) == 0)
{
if (hmodule = MODULE_load_from_psqlodbc_path(libpqlib), NULL == hmodule)
hmodule = LoadLibrary(libpqlib);
#ifdef USE_LIBPQ
if (NULL == hmodule)
connect_withparam_available = sslverify_available = FALSE;
if (connect_withparam_available < 0)
{
if (NULL == GetProcAddress(hmodule, checkproc1))
connect_withparam_available = FALSE;
else
connect_withparam_available = TRUE;
inolog("connect_withparam_available=%d\n", connect_withparam_available);
}
if (sslverify_available < 0)
{
if (NULL == GetProcAddress(hmodule, checkproc2))
sslverify_available = FALSE;
else
sslverify_available = TRUE;
}
#endif /* USE_LIBPQ */
}
else if (_strnicmp(pdli->szDll, pgenlist, strlen(pgenlist)) == 0)
{
if (hmodule = MODULE_load_from_psqlodbc_path(pgenlist), NULL == hmodule)
hmodule = LoadLibrary(pgenlist);
}
#ifdef USE_GSS
else if (_strnicmp(pdli->szDll, gssapilib, strlen(gssapilib)) == 0)
{
#ifdef USE_LIBPQ
if (hmodule = GetModuleHandle(gssapilib), NULL == hmodule)
#endif
{
if (hmodule = MODULE_load_from_psqlodbc_path(gssapilib), NULL == hmodule)
{
if (hmodule = LoadLibrary(gssapilib), NULL != hmodule)
loaded_gssapi = TRUE;
}
else
loaded_gssapi = TRUE;
}
}
#endif /* USE_GSS */
else if (0 == _stricmp(pdli->szDll, libarray[0]) ||
0 == _stricmp(pdli->szDll, libarray[1]))
{
mylog("getting alternative ssl library instead of %s\n", pdli->szDll);
for (i = 0; i < sizeof(libarray) / sizeof(const char * const); i++)
{
if (hmodule = GetModuleHandle(libarray[i]), NULL != hmodule)
break;
}
}
break;
}
return (FARPROC) hmodule;
}
/*
* unload delay loaded libraries.
*
* Openssl Library nmake defined
* ssleay32.dll is vc make, libssl32.dll is mingw make.
*/
#ifndef SSL_DLL
#define SSL_DLL "SSLEAY32.dll"
#endif /* SSL_DLL */
typedef BOOL (WINAPI *UnloadFunc)(LPCSTR);
void CleanupDelayLoadedDLLs(void)
{
BOOL success;
#if (_MSC_VER < 1300) /* VC6 DELAYLOAD IMPORT */
UnloadFunc func = __FUnloadDelayLoadedDLL;
#else
UnloadFunc func = __FUnloadDelayLoadedDLL2;
#endif
/* The dll names are case sensitive for the unload helper */
if (loaded_libpq)
{
#ifdef RESET_CRYPTO_CALLBACKS
/*
* May be needed to avoid crash on exit
* when libpq doesn't reset the callbacks.
*/
CRYPTO_set_locking_callback(NULL);
CRYPTO_set_id_callback(NULL);
mylog("passed RESET_CRYPTO_CALLBACKS\n");
#else
mylog("not passed RESET_CRYPTO_CALLBACKS\n");
#endif /* RESET_CRYPTO_CALLBACKS */
success = (*func)(libpqdll);
mylog("%s unload success=%d\n", libpqdll, success);
}
if (loaded_ssllib)
{
success = (*func)(SSL_DLL);
mylog("ssldll unload success=%d\n", success);
}
if (loaded_pgenlist)
{
success = (*func)(pgenlistdll);
mylog("%s unload success=%d\n", pgenlistdll, success);
}
if (loaded_gssapi)
{
success = (*func)(gssapidll);
mylog("%s unload success=%d\n", gssapidll, success);
}
return;
}
#else
void CleanupDelayLoadedDLLs(void)
{
return;
}
#endif /* _MSC_DELAY_LOAD_IMPORT */
#ifdef USE_LIBPQ
#if defined(_MSC_DELAY_LOAD_IMPORT)
static int filter_env2encoding(int level)
{
switch (level & 0xffff)
{
case ERROR_MOD_NOT_FOUND:
case ERROR_PROC_NOT_FOUND:
return EXCEPTION_EXECUTE_HANDLER;
}
return EXCEPTION_CONTINUE_SEARCH;
}
#endif /* _MSC_DELAY_LOAD_IMPORT */
BOOL ssl_verify_available(void)
{
if (sslverify_available < 0)
{
#if defined(_MSC_DELAY_LOAD_IMPORT)
TRY_DLI_HOOK
PQenv2encoding();
}
__except (filter_env2encoding(GetExceptionCode())) {
}
if (sslverify_available < 0)
sslverify_available = 0;
#else
sslverify_available = 1;
#endif /* _MSC_DELAY_LOAD_IMPORT */
}
return (0 != sslverify_available);
}
BOOL connect_with_param_available(void)
{
if (connect_withparam_available < 0)
{
#if defined(_MSC_DELAY_LOAD_IMPORT)
TRY_DLI_HOOK
PQescapeLiteral(NULL, NULL, 0);
}
__except (filter_env2encoding(GetExceptionCode())) {
}
if (connect_withparam_available < 0)
{
inolog("connect_withparam_available is set to false\n");
connect_withparam_available = 0;
}
#else
#ifdef NOT_USED /* currently not yet used */
#ifdef HAVE_LIBLTDL
lt_dlhandle dlhandle = lt_dlopenext(libpqlib);
connect_withparam_available = 1;
if (NULL != dlhandle)
{
if (NULL == lt_dlsym(dlhandle, checkproc1))
connect_withparam_available = 0;
lt_dlclose(dlhandle);
}
#endif /* HAVE_LIBLTDL */
#endif /* NOT_USED */
#endif /* _MSC_DELAY_LOAD_IMPORT */
}
return (0 != connect_withparam_available);
}
void *CALL_PQconnectdb(const char *conninfo, BOOL *libpqLoaded)
{
void *pqconn = NULL;
*libpqLoaded = TRUE;
#if defined(_MSC_DELAY_LOAD_IMPORT)
TRY_DLI_HOOK
inolog("calling PQconnectdb\n");
pqconn = PQconnectdb(conninfo);
}
__except ((GetExceptionCode() & 0xffff) == ERROR_MOD_NOT_FOUND ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
*libpqLoaded = FALSE;
}
RELEASE_NOTIFY_HOOK
if (*libpqLoaded)
{
loaded_libpq = TRUE;
/* ssllibs are already loaded by libpq
if (PQgetssl(pqconn))
loaded_ssllib = TRUE;
*/
}
#else
pqconn = PQconnectdb(conninfo);
#endif /* _MSC_DELAY_LOAD_IMPORT */
return pqconn;
}
void *CALL_PQconnectdbParams(const char *opts[], const char *vals[], BOOL *libpqLoaded)
{
void *pqconn = NULL;
*libpqLoaded = TRUE;
#if defined(_MSC_DELAY_LOAD_IMPORT)
TRY_DLI_HOOK
inolog("calling PQconnectdbParams\n");
pqconn = PQconnectdbParams(opts, vals, 0);
}
__except ((GetExceptionCode() & 0xffff) == ERROR_MOD_NOT_FOUND ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
*libpqLoaded = FALSE;
}
RELEASE_NOTIFY_HOOK
if (*libpqLoaded)
{
loaded_libpq = TRUE;
/* ssllibs are already loaded by libpq
if (PQgetssl(pqconn))
loaded_ssllib = TRUE;
*/
}
#else
pqconn = PQconnectdbParams(opts, vals, 0);
#endif /* _MSC_DELAY_LOAD_IMPORT */
return pqconn;
}
#else
BOOL ssl_verify_available(void)
{
return FALSE;
}
BOOL connect_with_param_available(void)
{
return FALSE;
}
#endif /* USE_LIBPQ */
#ifdef _HANDLE_ENLIST_IN_DTC_
RETCODE CALL_EnlistInDtc(ConnectionClass *conn, void *pTra, int method)
{
RETCODE ret;
BOOL loaded = TRUE;
#if defined(_MSC_DELAY_LOAD_IMPORT)
if (!loaded_pgenlist)
{
TRY_DLI_HOOK
ret = EnlistInDtc(conn, pTra, method);
}
__except ((GetExceptionCode() & 0xffff) == ERROR_MOD_NOT_FOUND ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
loaded = FALSE;
}
if (loaded)
loaded_pgenlist = TRUE;
RELEASE_NOTIFY_HOOK
}
else
ret = EnlistInDtc(conn, pTra, method);
#else
ret = EnlistInDtc(conn, pTra, method);
loaded_pgenlist = TRUE;
#endif /* _MSC_DELAY_LOAD_IMPORT */
return ret;
}
RETCODE CALL_DtcOnDisconnect(ConnectionClass *conn)
{
if (loaded_pgenlist)
return DtcOnDisconnect(conn);
return FALSE;
}
RETCODE CALL_IsolateDtcConn(ConnectionClass *conn, BOOL continueConnection)
{
if (loaded_pgenlist)
return IsolateDtcConn(conn, continueConnection);
return FALSE;
}
void *CALL_GetTransactionObject(HRESULT *hres)
{
void *ret;
BOOL loaded = TRUE;
#if defined(_MSC_DELAY_LOAD_IMPORT)
if (!loaded_pgenlist)
{
TRY_DLI_HOOK
ret = GetTransactionObject(hres);
}
__except ((GetExceptionCode() & 0xffff) == ERROR_MOD_NOT_FOUND ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
loaded = FALSE;
}
if (loaded)
loaded_pgenlist = TRUE;
RELEASE_NOTIFY_HOOK
}
else
ret = GetTransactionObject(hres);
#else
ret = GetTransactionObject(hres);
loaded_pgenlist = TRUE;
#endif /* _MSC_DELAY_LOAD_IMPORT */
return ret;
}
void CALL_ReleaseTransactionObject(void *pObj)
{
if (loaded_pgenlist)
ReleaseTransactionObject(pObj);
return;
}
#endif /* _HANDLE_ENLIST_IN_DTC_ */
#if defined(WIN_DYN_LOAD)
BOOL SSLLIB_check(void)
{
extern HINSTANCE s_hModule;
HMODULE hmodule = NULL;
mylog("checking libpq library\n");
/* First search the driver's folder */
#ifdef USE_LIBPQ
if (NULL == (hmodule = MODULE_load_from_psqlodbc_path(libpqlib)))
/* Second try the PATH ordinarily */
hmodule = LoadLibrary(libpqlib);
mylog("libpq hmodule=%p\n", hmodule);
#endif /* USE_LIBPQ */
if (hmodule)
FreeLibrary(hmodule);
return (NULL != hmodule);
}
#else
BOOL SSLLIB_check(void)
{
return TRUE;
}
#endif /* WIN_DYN_LOAD */