Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

464 lines
10 KiB

  1. /*
  2. * dload.cpp
  3. */
  4. #include "pch.h"
  5. #include <delayimp.h>
  6. #define FreeLib(hDll) \
  7. {if (hDll && hDll != INVALID_HANDLE_VALUE) FreeLibrary(hDll);}
  8. typedef struct
  9. {
  10. PCHAR Name;
  11. FARPROC Function;
  12. } FUNCPTRS;
  13. #if DBG
  14. void
  15. OutputDBString(
  16. CHAR *text
  17. );
  18. #endif
  19. extern int __dprint(LPSTR sz);
  20. extern HINSTANCE ghSymSrv;
  21. // from lz32.dll
  22. LONG fail_LZCopy(
  23. INT hfSource,
  24. INT hfDest
  25. )
  26. {
  27. SetLastError(ERROR_MOD_NOT_FOUND);
  28. __dprint("LZCopy() not found in module LZ32.DLL\n");
  29. return false;
  30. }
  31. VOID fail_LZClose(
  32. INT hFile
  33. )
  34. {
  35. SetLastError(ERROR_MOD_NOT_FOUND);
  36. __dprint("LZClose() not found in module LZ32.DLL\n");
  37. }
  38. INT fail_LZOpenFileA(
  39. LPTSTR lpFileName,
  40. LPOFSTRUCT lpReOpenBuf,
  41. WORD wStyle
  42. )
  43. {
  44. SetLastError(ERROR_MOD_NOT_FOUND);
  45. __dprint("LZOpenFileA() not found in module LZ32.DLL\n");
  46. return false;
  47. }
  48. // from wininet.dll
  49. BOOL fail_HttpSendRequestA(
  50. HINTERNET hRequest,
  51. LPCTSTR lpszHeaders,
  52. DWORD dwHeadersLength,
  53. LPVOID lpOptional,
  54. DWORD dwOptionalLength
  55. )
  56. {
  57. SetLastError(ERROR_MOD_NOT_FOUND);
  58. __dprint("HttpSEndRequestA() not found in module WININET.DLL\n");
  59. return 0;
  60. }
  61. BOOL fail_HttpQueryInfoA(
  62. HINTERNET hRequest,
  63. DWORD dwInfoLevel,
  64. LPVOID lpBuffer,
  65. LPDWORD lpdwBufferLength,
  66. LPDWORD lpdwIndex
  67. )
  68. {
  69. SetLastError(ERROR_MOD_NOT_FOUND);
  70. __dprint("HttpQueryInfoA() not found in module WININET.DLL\n");
  71. return false;
  72. }
  73. BOOL fail_InternetReadFile(
  74. IN HINTERNET hFile,
  75. IN LPVOID lpBuffer,
  76. IN DWORD dwNumberOfBytesToRead,
  77. OUT LPDWORD lpdwNumberOfBytesRead
  78. )
  79. {
  80. SetLastError(ERROR_MOD_NOT_FOUND);
  81. __dprint("InternetReadFile() not found in module WININET.DLL\n");
  82. return ERROR_MOD_NOT_FOUND;
  83. }
  84. BOOL fail_InternetQueryDataAvailable(
  85. HINTERNET hFile,
  86. LPDWORD lpdwNumberOfBytesAvailable,
  87. DWORD dwFlags,
  88. DWORD dwContext
  89. )
  90. {
  91. SetLastError(ERROR_MOD_NOT_FOUND);
  92. __dprint("InternetQueryDataAvailable() not found in module WININET.DLL\n");
  93. return ERROR_MOD_NOT_FOUND;
  94. }
  95. HINTERNET fail_HttpOpenRequestA(
  96. HINTERNET hConnect,
  97. LPCTSTR lpszVerb,
  98. LPCTSTR lpszObjectName,
  99. LPCTSTR lpszVersion,
  100. LPCTSTR lpszReferrer,
  101. LPCTSTR *lplpszAcceptTypes,
  102. DWORD dwFlags,
  103. DWORD dwContext
  104. )
  105. {
  106. SetLastError(ERROR_MOD_NOT_FOUND);
  107. __dprint("HttpOpenRequestA() not found in module WININET.DLL\n");
  108. return false;
  109. }
  110. BOOL fail_FtpGetFileA(
  111. HINTERNET hConnect,
  112. LPCTSTR lpszRemoteFile,
  113. LPCTSTR lpszNewFile,
  114. BOOL fFailIfExists,
  115. DWORD dwFlagsAndAttributes,
  116. DWORD dwFlags,
  117. DWORD dwContext
  118. )
  119. {
  120. SetLastError(ERROR_MOD_NOT_FOUND);
  121. __dprint("FtpGEtFileA() not found in module WININET.DLL\n");
  122. return false;
  123. }
  124. BOOL fail_FtpSetCurrentDirectoryA(
  125. HINTERNET hConnect,
  126. LPCTSTR lpszDirectory
  127. )
  128. {
  129. SetLastError(ERROR_MOD_NOT_FOUND);
  130. __dprint("FtpSetCurrentDirectoryA() not found in module WININET.DLL\n");
  131. return false;
  132. }
  133. BOOL fail_FtpFindFirstFileA(
  134. HINTERNET hConnect,
  135. LPCTSTR lpszSearchFile,
  136. LPWIN32_FIND_DATA lpFindFileData ,
  137. DWORD dwFlags,
  138. DWORD dwContext
  139. )
  140. {
  141. SetLastError(ERROR_MOD_NOT_FOUND);
  142. __dprint("FtpFindFirstFileA() not found in module WININET.DLL\n");
  143. return ERROR_MOD_NOT_FOUND;
  144. }
  145. BOOL fail_InternetCloseHandle(
  146. IN HINTERNET hInternet
  147. )
  148. {
  149. SetLastError(ERROR_MOD_NOT_FOUND);
  150. __dprint("InternetCloseHandle() not found in module WININET.DLL\n");
  151. return false;
  152. }
  153. HINTERNET fail_InternetOpenA(
  154. IN LPCTSTR lpszAgent,
  155. IN DWORD dwAccessType,
  156. IN LPCTSTR lpszProxyName,
  157. IN LPCTSTR lpszProxyBypass,
  158. IN DWORD dwFlags
  159. )
  160. {
  161. SetLastError(ERROR_MOD_NOT_FOUND);
  162. __dprint("InternetOpenA() not found in module WININET.DLL\n");
  163. return 0;
  164. }
  165. DWORD fail_InternetErrorDlg(
  166. IN HWND hWnd,
  167. IN OUT HINTERNET hRequest,
  168. IN DWORD dwError,
  169. IN DWORD dwFlags,
  170. IN OUT LPVOID *lppvData
  171. )
  172. {
  173. SetLastError(ERROR_MOD_NOT_FOUND);
  174. __dprint("InternetErrorDlg() not found in module WININET.DLL\n");
  175. return ERROR_MOD_NOT_FOUND;
  176. }
  177. HINTERNET fail_InternetConnectA(
  178. IN HINTERNET hInternet,
  179. IN LPCTSTR lpszServerName,
  180. IN INTERNET_PORT nServerPort,
  181. IN LPCTSTR lpszUserName,
  182. IN LPCTSTR lpszPassword,
  183. IN DWORD dwService,
  184. IN DWORD dwFlags,
  185. IN DWORD_PTR dwContext
  186. )
  187. {
  188. SetLastError(ERROR_MOD_NOT_FOUND);
  189. __dprint("InternetConnectA() not found in module WININET.DLL\n");
  190. return 0;
  191. }
  192. // from user32.dll
  193. int fail_wvsprintfA(
  194. LPTSTR lpOutput,
  195. LPCTSTR lpFormat,
  196. va_list arglist
  197. )
  198. {
  199. SetLastError(ERROR_MOD_NOT_FOUND);
  200. __dprint("wvsprintfA() not found in module USER32.DLL\n");
  201. return 0;
  202. }
  203. int fail_wsprintfA(
  204. LPTSTR lpOut,
  205. LPCTSTR lpFmt,
  206. ...
  207. )
  208. {
  209. SetLastError(ERROR_MOD_NOT_FOUND);
  210. __dprint("wsprintfA() not found in module USER32.DLL\n");
  211. return 0;
  212. }
  213. LPTSTR fail_CharLowerA(
  214. LPTSTR lpsz
  215. )
  216. {
  217. SetLastError(ERROR_MOD_NOT_FOUND);
  218. __dprint("CharLowerA() not found in module USER32.DLL\n");
  219. return 0;
  220. }
  221. HWND fail_GetDesktopWindow(VOID)
  222. {
  223. SetLastError(ERROR_MOD_NOT_FOUND);
  224. __dprint("GetDeskTopWindow() not found in module USER32.DLL\n");
  225. return 0;
  226. }
  227. #if 0
  228. // from ntdll.dll
  229. char *fail_strstr(
  230. const char *sz,
  231. const char *token
  232. )
  233. {
  234. SetLastError(ERROR_MOD_NOT_FOUND);
  235. __dprint("strstr() not found in module NTDLL.DLL\n");
  236. return 0;
  237. }
  238. char *fail_strchr(
  239. const char *sz,
  240. int c
  241. )
  242. {
  243. SetLastError(ERROR_MOD_NOT_FOUND);
  244. __dprint("strchr() not found in module NTDLL.DLL\n");
  245. return 0;
  246. }
  247. int fail__strnicmp(
  248. const char *one,
  249. const char *two,
  250. size_t len
  251. )
  252. {
  253. SetLastError(ERROR_MOD_NOT_FOUND);
  254. __dprint("_strnicmp() not found in module NTDLL.DLL\n");
  255. return 1;
  256. }
  257. int fail_isspace(int c)
  258. {
  259. SetLastError(ERROR_MOD_NOT_FOUND);
  260. __dprint("isspace() not found in module NTDLL.DLL\n");
  261. return 0;
  262. }
  263. #endif
  264. FUNCPTRS FailPtrs[] = {
  265. // lz32.dll
  266. {"LZCopy", (FARPROC)fail_LZCopy},
  267. {"LZClose", (FARPROC)fail_LZClose},
  268. {"LZOpenFileA", (FARPROC)fail_LZOpenFileA},
  269. // wininet.dll
  270. {"HttpSendRequestA", (FARPROC)fail_HttpSendRequestA},
  271. {"HttpQueryInfoA", (FARPROC)fail_HttpQueryInfoA},
  272. {"InternetReadFile", (FARPROC)fail_InternetReadFile},
  273. {"InternetQueryDataAvailable", (FARPROC)fail_InternetQueryDataAvailable},
  274. {"HttpOpenRequestA", (FARPROC)fail_HttpOpenRequestA},
  275. {"FtpGetFileA", (FARPROC)fail_FtpGetFileA},
  276. {"FtpSetCurrentDirectoryA", (FARPROC)fail_FtpSetCurrentDirectoryA},
  277. {"FtpFindFirstFileA", (FARPROC)fail_FtpFindFirstFileA},
  278. {"InternetCloseHandle", (FARPROC)fail_InternetCloseHandle},
  279. {"InternetOpenA", (FARPROC)fail_InternetOpenA},
  280. {"InternetErrorDlg", (FARPROC)fail_InternetErrorDlg},
  281. {"InternetConnectA", (FARPROC)fail_InternetConnectA},
  282. // user32.dll
  283. {"wvsprintfA", (FARPROC)fail_wvsprintfA},
  284. {"wsprintfA", (FARPROC)fail_wsprintfA},
  285. {"CharLowerA", (FARPROC)fail_CharLowerA},
  286. {"GetDesktopWindow", (FARPROC)fail_GetDesktopWindow},
  287. #if 0
  288. // ntdll.dll
  289. {"strstr", (FARPROC)fail_strstr},
  290. {"strchr", (FARPROC)fail_strchr},
  291. {"_strnicmp", (FARPROC)fail__strnicmp},
  292. {"isspace", (FARPROC)fail_isspace},
  293. #endif
  294. // cabinet.dll is called by ordinal. We will AV here
  295. {NULL, NULL}
  296. };
  297. #define cDLLs 4
  298. enum {
  299. modLZ32 = 0,
  300. modWININET,
  301. modUSER32,
  302. modCABINET,
  303. modMax
  304. };
  305. HINSTANCE hDelayLoadDll[modMax];
  306. const char *szModList[modMax] =
  307. {
  308. "lz32.dll", "wininet.dll", "user32.dll", "cabinet.dll"
  309. };
  310. int
  311. FindSupportedDelayLoadModule(
  312. const char *name
  313. )
  314. {
  315. int i;
  316. for (i = 0; i < modMax; i++) {
  317. if (!_stricmp(name, szModList[i]))
  318. return i;
  319. }
  320. return -1;
  321. }
  322. FARPROC
  323. FindFailureProc(
  324. const char *szProcName)
  325. {
  326. FUNCPTRS *fp;
  327. for (fp = FailPtrs; fp->Name; fp++) {
  328. if (!_stricmp(fp->Name, szProcName))
  329. return fp->Function;
  330. }
  331. return NULL;
  332. }
  333. FARPROC
  334. WINAPI
  335. SymSrvDelayLoadHook (
  336. UINT unReason,
  337. PDelayLoadInfo pDelayInfo
  338. )
  339. {
  340. FARPROC rc = NULL;
  341. if (dliStartProcessing == unReason)
  342. {
  343. DWORD iDll = FindSupportedDelayLoadModule(pDelayInfo->szDll);
  344. if (iDll == -1)
  345. return 0;
  346. if (!hDelayLoadDll[iDll] || hDelayLoadDll[iDll] == INVALID_HANDLE_VALUE) {
  347. hDelayLoadDll[iDll] = LoadLibrary(pDelayInfo->szDll);
  348. if (!hDelayLoadDll[iDll]) {
  349. hDelayLoadDll[iDll] = (HINSTANCE)INVALID_HANDLE_VALUE;
  350. }
  351. }
  352. if (INVALID_HANDLE_VALUE != hDelayLoadDll[iDll] && ghSymSrv)
  353. rc = GetProcAddress(hDelayLoadDll[iDll], pDelayInfo->dlp.szProcName);
  354. if (!rc)
  355. rc = FindFailureProc(pDelayInfo->dlp.szProcName);
  356. #if DBG
  357. if (!rc) {
  358. OutputDBString("BogusDelayLoad function encountered...\n");
  359. }
  360. #endif
  361. }
  362. if (rc && ghSymSrv)
  363. *pDelayInfo->ppfn = rc;
  364. return rc;
  365. }
  366. #if 0
  367. typedef struct tagVERHEAD {
  368. WORD wTotLen;
  369. WORD wValLen;
  370. WORD wType; /* always 0 */
  371. WCHAR szKey[(sizeof("VS_VERSION_INFO")+3)&~03];
  372. VS_FIXEDFILEINFO vsf;
  373. } VERHEAD ;
  374. #endif
  375. PfnDliHook __pfnDliNotifyHook = SymSrvDelayLoadHook;
  376. PfnDliHook __pfnDliFailureHook = NULL;
  377. #if DBG
  378. void
  379. OutputDBString(
  380. char *text
  381. )
  382. {
  383. char sz[256];
  384. CopyStrArray(sz, "SYMSRV: ");
  385. CatStrArray(sz, text);
  386. OutputDebugString(sz);
  387. }
  388. #endif