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.

494 lines
11 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows NT Security
  4. // Copyright (C) Microsoft Corporation, 1997 - 1999
  5. //
  6. // File: toolutl.cpp
  7. //
  8. // Contents: Utilities for the tools
  9. //
  10. // History: 17-Jun-97 xiaohs Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include <windows.h>
  14. #include <assert.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <memory.h>
  19. #include <time.h>
  20. #include <wchar.h>
  21. #include <stdarg.h>
  22. #include "unicode.h"
  23. #include "toolutl.h"
  24. #define MAX_STRING_RSC_SIZE 512
  25. WCHAR wszBuffer[MAX_STRING_RSC_SIZE];
  26. DWORD dwBufferSize=sizeof(wszBuffer)/sizeof(wszBuffer[0]);
  27. WCHAR wszBuffer2[MAX_STRING_RSC_SIZE];
  28. WCHAR wszBuffer3[MAX_STRING_RSC_SIZE];
  29. //+-------------------------------------------------------------------------
  30. // Allocation and free routines
  31. //--------------------------------------------------------------------------
  32. void *ToolUtlAlloc(IN size_t cbBytes, HMODULE hModule, int idsString)
  33. {
  34. void *pv=NULL;
  35. pv=malloc(cbBytes);
  36. //out put error message
  37. if((pv==NULL) && (hModule!=NULL) && (idsString!=0))
  38. {
  39. IDSwprintf(hModule, idsString);
  40. }
  41. return pv;
  42. }
  43. void ToolUtlFree(IN void *pv)
  44. {
  45. if(pv)
  46. free(pv);
  47. }
  48. //--------------------------------------------------------------------------
  49. //
  50. // Output routines
  51. //--------------------------------------------------------------------------
  52. //---------------------------------------------------------------------------
  53. // The private version of _wcsnicmp
  54. //----------------------------------------------------------------------------
  55. int IDSwcsnicmp(HMODULE hModule, WCHAR *pwsz, int idsString, DWORD dwCount)
  56. {
  57. assert(pwsz);
  58. //load the string
  59. if(!LoadStringU(hModule, idsString, wszBuffer, dwBufferSize))
  60. return -1;
  61. return _wcsnicmp(pwsz, wszBuffer,dwCount);
  62. }
  63. //---------------------------------------------------------------------------
  64. // The private version of _wcsicmp
  65. //----------------------------------------------------------------------------
  66. int IDSwcsicmp(HMODULE hModule, WCHAR *pwsz, int idsString)
  67. {
  68. assert(pwsz);
  69. //load the string
  70. if(!LoadStringU(hModule, idsString, wszBuffer, dwBufferSize))
  71. return -1;
  72. return _wcsicmp(pwsz, wszBuffer);
  73. }
  74. //-------------------------------------------------------------------------
  75. //
  76. // The private version of wprintf. Input is an ID for a stirng resource
  77. // and the output is the standard output of wprintf.
  78. //
  79. //-------------------------------------------------------------------------
  80. void IDSwprintf(HMODULE hModule, int idsString, ...)
  81. {
  82. va_list vaPointer;
  83. va_start(vaPointer, idsString);
  84. //load the string
  85. LoadStringU(hModule, idsString, wszBuffer, dwBufferSize);
  86. vwprintf(wszBuffer,vaPointer);
  87. return;
  88. }
  89. void IDS_IDSwprintf(HMODULE hModule, int idString, int idStringTwo)
  90. {
  91. //load the string
  92. LoadStringU(hModule, idString, wszBuffer, dwBufferSize);
  93. //load the string two
  94. LoadStringU(hModule, idStringTwo, wszBuffer2, dwBufferSize);
  95. //print buffer2 on top of buffer1
  96. wprintf(wszBuffer,wszBuffer2);
  97. return;
  98. }
  99. void IDS_IDS_DW_DWwprintf(HMODULE hModule, int idString, int idStringTwo, DWORD dwOne, DWORD dwTwo)
  100. {
  101. //load the string
  102. LoadStringU(hModule, idString, wszBuffer, dwBufferSize);
  103. //load the string two
  104. LoadStringU(hModule, idStringTwo, wszBuffer2, dwBufferSize);
  105. //print buffer2 on top of buffer1
  106. wprintf(wszBuffer,wszBuffer2,dwOne, dwTwo);
  107. return;
  108. }
  109. void IDS_IDS_IDSwprintf(HMODULE hModule, int ids1,int ids2,int ids3)
  110. {
  111. //load the string
  112. LoadStringU(hModule, ids1, wszBuffer, dwBufferSize);
  113. //load the string two
  114. LoadStringU(hModule, ids2, wszBuffer2, dwBufferSize);
  115. //load the string three
  116. LoadStringU(hModule, ids3, wszBuffer3, dwBufferSize);
  117. wprintf(wszBuffer,wszBuffer2,wszBuffer3);
  118. return;
  119. }
  120. void IDS_DW_IDS_IDSwprintf(HMODULE hModule, int ids1,DWORD dw,int ids2,int ids3)
  121. {
  122. //load the string
  123. LoadStringU(hModule, ids1, wszBuffer, dwBufferSize);
  124. //load the string two
  125. LoadStringU(hModule, ids2, wszBuffer2, dwBufferSize);
  126. //load the string three
  127. LoadStringU(hModule, ids3, wszBuffer3, dwBufferSize);
  128. wprintf(wszBuffer,dw,wszBuffer2,wszBuffer3,dw);
  129. return;
  130. }
  131. void IDS_IDS_IDS_IDSwprintf(HMODULE hModule, int ids1,int ids2,int ids3, int ids4)
  132. {
  133. WCHAR wszBuffer4[MAX_STRING_RSC_SIZE];
  134. //load the string
  135. LoadStringU(hModule, ids1, wszBuffer, dwBufferSize);
  136. //load the string two
  137. LoadStringU(hModule, ids2, wszBuffer2, dwBufferSize);
  138. //load the string three
  139. LoadStringU(hModule, ids3, wszBuffer3, dwBufferSize);
  140. //load the string four
  141. LoadStringU(hModule, ids4, wszBuffer4, dwBufferSize);
  142. wprintf(wszBuffer,wszBuffer2,wszBuffer3,wszBuffer4);
  143. return;
  144. }
  145. ///////////////////////////////////////////////////////////////
  146. //
  147. // Convert WSZ to SZ
  148. //
  149. //
  150. HRESULT WSZtoSZ(LPWSTR wsz, LPSTR *psz)
  151. {
  152. DWORD cbSize=0;
  153. assert(psz);
  154. *psz=NULL;
  155. if(!wsz)
  156. return S_OK;
  157. cbSize=WideCharToMultiByte(0,0,wsz,-1,
  158. NULL,0,0,0);
  159. if(cbSize==0)
  160. return HRESULT_FROM_WIN32(GetLastError());
  161. *psz=(LPSTR)ToolUtlAlloc(cbSize);
  162. if(*psz==NULL)
  163. return E_OUTOFMEMORY;
  164. if(WideCharToMultiByte(0,0,wsz,-1,
  165. *psz,cbSize,0,0))
  166. {
  167. return S_OK;
  168. }
  169. else
  170. {
  171. ToolUtlFree(*psz);
  172. return HRESULT_FROM_WIN32(GetLastError());
  173. }
  174. }
  175. //--------------------------------------------------------------------------------
  176. //
  177. //get the bytes from the file name
  178. //
  179. //---------------------------------------------------------------------------------
  180. HRESULT RetrieveBLOBFromFile(LPWSTR pwszFileName,DWORD *pcb,BYTE **ppb)
  181. {
  182. HRESULT hr=E_FAIL;
  183. HANDLE hFile=NULL;
  184. HANDLE hFileMapping=NULL;
  185. DWORD cbData=0;
  186. BYTE *pbData=0;
  187. DWORD cbHighSize=0;
  188. if(!pcb || !ppb || !pwszFileName)
  189. return E_INVALIDARG;
  190. *ppb=NULL;
  191. *pcb=0;
  192. if ((hFile = CreateFileU(pwszFileName,
  193. GENERIC_READ,
  194. FILE_SHARE_READ,
  195. NULL, // lpsa
  196. OPEN_EXISTING,
  197. FILE_ATTRIBUTE_NORMAL,
  198. NULL)) == INVALID_HANDLE_VALUE)
  199. {
  200. hr=HRESULT_FROM_WIN32(GetLastError());
  201. goto CLEANUP;
  202. }
  203. if((cbData = GetFileSize(hFile, &cbHighSize)) == 0xffffffff)
  204. {
  205. hr=HRESULT_FROM_WIN32(GetLastError());
  206. goto CLEANUP;
  207. }
  208. //we do not handle file more than 4G bytes
  209. if(cbHighSize != 0)
  210. {
  211. hr=E_FAIL;
  212. goto CLEANUP;
  213. }
  214. //create a file mapping object
  215. if(NULL == (hFileMapping=CreateFileMapping(
  216. hFile,
  217. NULL,
  218. PAGE_READONLY,
  219. 0,
  220. 0,
  221. NULL)))
  222. {
  223. hr=HRESULT_FROM_WIN32(GetLastError());
  224. goto CLEANUP;
  225. }
  226. //create a view of the file
  227. if(NULL == (pbData=(BYTE *)MapViewOfFile(
  228. hFileMapping,
  229. FILE_MAP_READ,
  230. 0,
  231. 0,
  232. cbData)))
  233. {
  234. hr=HRESULT_FROM_WIN32(GetLastError());
  235. goto CLEANUP;
  236. }
  237. hr=S_OK;
  238. *pcb=cbData;
  239. *ppb=pbData;
  240. CLEANUP:
  241. if(hFile)
  242. CloseHandle(hFile);
  243. if(hFileMapping)
  244. CloseHandle(hFileMapping);
  245. return hr;
  246. }
  247. //+-------------------------------------------------------------------------
  248. // Write a blob to a file
  249. //--------------------------------------------------------------------------
  250. HRESULT OpenAndWriteToFile(
  251. LPCWSTR pwszFileName,
  252. PBYTE pb,
  253. DWORD cb
  254. )
  255. {
  256. HRESULT hr=E_FAIL;
  257. HANDLE hFile=NULL;
  258. DWORD dwBytesWritten=0;
  259. if(!pwszFileName || !pb || (cb==0))
  260. return E_INVALIDARG;
  261. hFile = CreateFileU(pwszFileName,
  262. GENERIC_WRITE,
  263. 0, // fdwShareMode
  264. NULL, // lpsa
  265. CREATE_ALWAYS,
  266. 0, // fdwAttrsAndFlags
  267. 0); // TemplateFile
  268. if (INVALID_HANDLE_VALUE == hFile)
  269. {
  270. hr=HRESULT_FROM_WIN32(GetLastError());
  271. }
  272. else
  273. {
  274. if (!WriteFile(
  275. hFile,
  276. pb,
  277. cb,
  278. &dwBytesWritten,
  279. NULL // lpOverlapped
  280. ))
  281. {
  282. hr=HRESULT_FROM_WIN32(GetLastError());
  283. }
  284. else
  285. {
  286. if(dwBytesWritten != cb)
  287. hr=E_FAIL;
  288. else
  289. hr=S_OK;
  290. }
  291. CloseHandle(hFile);
  292. }
  293. return hr;
  294. }
  295. //----------------------------------------------------------------------------
  296. //
  297. // Get an absolutely name from the path, such as "c:\public\mydoc\doc.doc."
  298. // This function will return doc.doc
  299. //
  300. //----------------------------------------------------------------------------
  301. void GetFileName(LPWSTR pwszPath, LPWSTR *ppwszName)
  302. {
  303. DWORD dwLength=0;
  304. assert(pwszPath);
  305. assert(ppwszName);
  306. (*ppwszName)=pwszPath;
  307. if(0==(dwLength=wcslen(pwszPath)))
  308. return;
  309. (*ppwszName)=pwszPath+dwLength-1;
  310. for(; dwLength>0; dwLength--)
  311. {
  312. if((**ppwszName)=='\\')
  313. break;
  314. (*ppwszName)--;
  315. }
  316. (*ppwszName)++;
  317. }
  318. //----------------------------------------------------------------------------
  319. //
  320. // Compose the private key file structure:
  321. // "pvkFileName"\0"keysepc"\0"provtype"\0"provname"\0\0
  322. //
  323. //----------------------------------------------------------------------------
  324. HRESULT ComposePvkString( CRYPT_KEY_PROV_INFO *pKeyProvInfo,
  325. LPWSTR *ppwszPvkString,
  326. DWORD *pcwchar)
  327. {
  328. HRESULT hr=S_OK;
  329. DWORD cwchar=0;
  330. LPWSTR pwszAddr=0;
  331. WCHAR wszKeySpec[12];
  332. WCHAR wszProvType[12];
  333. assert(pKeyProvInfo);
  334. assert(ppwszPvkString);
  335. assert(pcwchar);
  336. //convert dwKeySpec and dwProvType to wchar
  337. swprintf(wszKeySpec, L"%lu", pKeyProvInfo->dwKeySpec);
  338. swprintf(wszProvType, L"%lu", pKeyProvInfo->dwProvType);
  339. //count of the number of characters we need
  340. cwchar=(pKeyProvInfo->pwszProvName) ?
  341. (wcslen(pKeyProvInfo->pwszProvName)+1) : 1;
  342. //add the ContainerName + two DWORDs
  343. cwchar += wcslen(pKeyProvInfo->pwszContainerName)+1+
  344. wcslen(wszKeySpec)+1+wcslen(wszProvType)+1+1;
  345. *ppwszPvkString=(LPWSTR)ToolUtlAlloc(cwchar * sizeof(WCHAR));
  346. if(!(*ppwszPvkString))
  347. return E_OUTOFMEMORY;
  348. //copy the private key file name .
  349. wcscpy((*ppwszPvkString), pKeyProvInfo->pwszContainerName);
  350. pwszAddr=(*ppwszPvkString)+wcslen(*ppwszPvkString)+1;
  351. //copy the key spec
  352. wcscpy(pwszAddr, wszKeySpec);
  353. pwszAddr=pwszAddr+wcslen(wszKeySpec)+1;
  354. //copy the provider type
  355. wcscpy(pwszAddr, wszProvType);
  356. pwszAddr=pwszAddr+wcslen(wszProvType)+1;
  357. //copy the provider name
  358. if(pKeyProvInfo->pwszProvName)
  359. {
  360. wcscpy(pwszAddr, pKeyProvInfo->pwszProvName);
  361. pwszAddr=pwszAddr+wcslen(pKeyProvInfo->pwszProvName)+1;
  362. }
  363. else
  364. {
  365. *pwszAddr=L'\0';
  366. pwszAddr++;
  367. }
  368. //NULL terminate the string
  369. *pwszAddr=L'\0';
  370. *pcwchar=cwchar;
  371. return S_OK;
  372. }