Source code of Windows XP (NT5)
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.

458 lines
13 KiB

  1. #ifdef USE_STDAFX
  2. #include "stdafx.h"
  3. #else
  4. #include <windows.h>
  5. //#include <stdio.h>
  6. #endif
  7. #include <stdio.h>
  8. #include <NtSecApi.h>
  9. #include <comdef.h>
  10. #include <io.h>
  11. #include <winioctl.h>
  12. #include <lm.h>
  13. #include <Dsgetdc.h>
  14. #include "mcsdmmsg.h"
  15. #include "pwdfuncs.h"
  16. #include "PWGen.hpp"
  17. #include "UString.hpp"
  18. using namespace _com_util;
  19. /*********************************************************************
  20. * *
  21. * Written by: Paul Thompson *
  22. * Date: 9 DEC 2000 *
  23. * *
  24. * This function is responsible for enumerating all floppy drives*
  25. * on this server. *
  26. * *
  27. *********************************************************************/
  28. //BEGIN EnumLocalDrives
  29. _bstr_t EnumLocalDrives()
  30. {
  31. /* local constants */
  32. const int ENTRY_SIZE = 4; // Drive letter, colon, backslash, NULL
  33. /* local variables */
  34. _bstr_t strDrives = L"";
  35. WCHAR sDriveList[MAX_PATH];
  36. DWORD dwRes;
  37. /* function body */
  38. try
  39. {
  40. dwRes = GetLogicalDriveStrings(MAX_PATH, sDriveList);
  41. if (dwRes != 0)
  42. {
  43. LPWSTR pTmpBuf = sDriveList;
  44. //check each one to see if it is a floppy drive
  45. while (*pTmpBuf != NULL)
  46. {
  47. //check the type of this drive
  48. UINT uiType = GetDriveType(pTmpBuf);
  49. if ((uiType == DRIVE_REMOVABLE) || (uiType == DRIVE_FIXED) ||
  50. (uiType == DRIVE_CDROM) || (uiType == DRIVE_RAMDISK))
  51. {
  52. strDrives += pTmpBuf;
  53. strDrives += L",";
  54. }
  55. pTmpBuf += ENTRY_SIZE;
  56. }
  57. //remove the trailing ','
  58. WCHAR* pEnd = (WCHAR*)strDrives;
  59. pEnd[strDrives.length() - 1] = L'\0';
  60. }
  61. else
  62. {
  63. _com_issue_error(HRESULT_FROM_WIN32(GetLastError()));
  64. }
  65. }
  66. catch (...)
  67. {
  68. throw;
  69. }
  70. return strDrives;
  71. }
  72. //END EnumLocalDrives
  73. /*********************************************************************
  74. * *
  75. * Written by: Paul Thompson *
  76. * Date: 9 DEC 2000 *
  77. * *
  78. * This function is responsible for saving binary data to a given*
  79. * file path on a floppy drive. *
  80. * *
  81. *********************************************************************/
  82. //BEGIN StoreDataToFloppy
  83. void StoreDataToFloppy(LPCWSTR sPath, _variant_t & varData)
  84. {
  85. /* local variables */
  86. FILE * floppyfile = NULL;
  87. LPBYTE pByte = NULL;
  88. HRESULT hr;
  89. /* function body */
  90. try
  91. {
  92. //check incoming parameters
  93. if ((!sPath) || (varData.vt != (VT_ARRAY | VT_UI1)) ||
  94. (!varData.parray))
  95. {
  96. _com_issue_error(HRESULT_FROM_WIN32(E_INVALIDARG));
  97. }
  98. //open the file
  99. floppyfile = _wfopen(sPath, L"wb");
  100. if (!floppyfile)
  101. _com_issue_error(HRESULT_FROM_WIN32(CO_E_FAILEDTOCREATEFILE));
  102. //get the array size
  103. long uLBound, uUBound;
  104. size_t uSLength;
  105. hr = SafeArrayGetLBound(varData.parray, 1, &uLBound);
  106. if (FAILED(hr))
  107. _com_issue_error(hr);
  108. hr = SafeArrayGetUBound(varData.parray, 1, &uUBound);
  109. if (FAILED(hr))
  110. _com_issue_error(hr);
  111. uSLength = size_t(uUBound - uLBound + 1);
  112. //write the data to the file
  113. hr = SafeArrayAccessData(varData.parray,(void**)&pByte);
  114. if (FAILED(hr))
  115. _com_issue_error(hr);
  116. if (fwrite((void *)pByte, 1, uSLength, floppyfile) != uSLength)
  117. _com_issue_error(HRESULT_FROM_WIN32(ERROR_WRITE_FAULT));
  118. hr = SafeArrayUnaccessData(varData.parray);
  119. if (FAILED(hr))
  120. _com_issue_error(hr);
  121. //close the file
  122. if (floppyfile)
  123. fclose(floppyfile);
  124. }
  125. catch (...)
  126. {
  127. if (floppyfile)
  128. fclose(floppyfile);
  129. throw;
  130. }
  131. }
  132. //END StoreDataToFloppy
  133. /*********************************************************************
  134. * *
  135. * Written by: Paul Thompson *
  136. * Date: 9 DEC 2000 *
  137. * *
  138. * This function is responsible for retrieving binary data from a*
  139. * given file path on a floppy drive. The _variant_t variable *
  140. * returned is of the type VT_UI1 | VT_ARRAY upoin success or *
  141. * VT_EMPTY upon a failure. *
  142. * *
  143. *********************************************************************/
  144. //BEGIN GetDataFromFloppy
  145. _variant_t GetDataFromFloppy(LPCWSTR sPath)
  146. {
  147. /* local variables */
  148. FILE * floppyfile = NULL;
  149. LPBYTE pByte = NULL;
  150. HRESULT hr;
  151. _variant_t varData;
  152. SAFEARRAY * pSa = NULL;
  153. SAFEARRAYBOUND bd;
  154. /* function body */
  155. try
  156. {
  157. //check incoming parameters
  158. if (!sPath)
  159. _com_issue_error(HRESULT_FROM_WIN32(E_INVALIDARG));
  160. //path must have the '\' escaped
  161. // _bstr_t sFile = EscapeThePath(sPath);
  162. //open the file
  163. floppyfile = _wfopen(sPath, L"rb");
  164. if (!floppyfile)
  165. _com_issue_error(HRESULT_FROM_WIN32(ERROR_TOO_MANY_OPEN_FILES));
  166. //get the number of bytes in the file
  167. long fileLen = _filelength(_fileno(floppyfile));
  168. if (fileLen == -1)
  169. _com_issue_error(HRESULT_FROM_WIN32(ERROR_READ_FAULT));
  170. bd.cElements = fileLen;
  171. bd.lLbound = 0;
  172. //read the data from the file one byte at a time
  173. pSa = SafeArrayCreate(VT_UI1, 1, &bd);
  174. if (!pSa)
  175. _com_issue_error(E_FAIL);
  176. hr = SafeArrayAccessData(pSa,(void**)&pByte);
  177. if (FAILED(hr))
  178. _com_issue_error(hr);
  179. long nTotalRead = 0;
  180. while(!feof(floppyfile))
  181. {
  182. if (fread((void *)(pByte+nTotalRead), 1, 1, floppyfile) == 1)
  183. nTotalRead++;
  184. }
  185. hr = SafeArrayUnaccessData(pSa);
  186. if (FAILED(hr))
  187. _com_issue_error(hr);
  188. //close the file
  189. if (floppyfile)
  190. {
  191. fclose(floppyfile);
  192. floppyfile = NULL;
  193. }
  194. if (nTotalRead != fileLen)
  195. _com_issue_error(HRESULT_FROM_WIN32(ERROR_READ_FAULT));
  196. varData.vt = VT_UI1 | VT_ARRAY;
  197. if (FAILED(SafeArrayCopy(pSa, &varData.parray)))
  198. _com_issue_error(hr);
  199. if (FAILED(SafeArrayDestroy(pSa)))
  200. _com_issue_error(hr);
  201. }
  202. catch (...)
  203. {
  204. if (floppyfile)
  205. fclose(floppyfile);
  206. throw;
  207. }
  208. return varData;
  209. }
  210. //END GetDataFromFloppy
  211. /*********************************************************************
  212. * *
  213. * Written by: Paul Thompson *
  214. * Date: 15 DEC 2000 *
  215. * *
  216. * This function is convert a _variant_t parameter of the type *
  217. * VT_ARRAY | VT_UI1 and returns it in a char array. The caller must*
  218. * free the array with the delete [] call. This function return NULL*
  219. * if the data was not placed in the array. *
  220. * *
  221. *********************************************************************/
  222. //BEGIN GetBinaryArrayFromVariant
  223. char* GetBinaryArrayFromVariant(_variant_t varData)
  224. {
  225. /* local variables */
  226. LPBYTE pByte = NULL;
  227. HRESULT hr;
  228. char * cArray;
  229. int i;
  230. /* function body */
  231. //check incoming parameters
  232. if ((varData.vt != (VT_ARRAY | VT_UI1)) || (!varData.parray))
  233. return NULL;
  234. //get the array size
  235. long uLBound, uUBound, uSLength;
  236. hr = SafeArrayGetLBound(varData.parray, 1, &uLBound);
  237. if (FAILED(hr))
  238. return NULL;
  239. hr = SafeArrayGetUBound(varData.parray, 1, &uUBound);
  240. if (FAILED(hr))
  241. return NULL;
  242. uSLength = uUBound - uLBound + 1;
  243. //create an array to hold all this data
  244. cArray = new char[uSLength+1];
  245. if (!cArray)
  246. return NULL;
  247. //write the data to the file
  248. hr = SafeArrayAccessData(varData.parray,(void**)&pByte);
  249. if (FAILED(hr))
  250. {
  251. delete [] cArray;
  252. return NULL;
  253. }
  254. for (i=0; i<uSLength; i++)
  255. {
  256. cArray[i] = pByte[i];
  257. }
  258. cArray[i] = L'\0';
  259. hr = SafeArrayUnaccessData(varData.parray);
  260. if (FAILED(hr))
  261. {
  262. delete [] cArray;
  263. return NULL;
  264. }
  265. return cArray;
  266. }
  267. //END GetBinaryArrayFromVariant
  268. /*********************************************************************
  269. * *
  270. * Written by: Paul Thompson *
  271. * Date: 15 DEC 2000 *
  272. * *
  273. * This function is convert a char array of binary data to a *
  274. * _variant_t of the type VT_ARRAY | VT_UI1 and returns it. *
  275. * *
  276. *********************************************************************/
  277. //BEGIN SetVariantWithBinaryArray
  278. _variant_t SetVariantWithBinaryArray(char * aData, DWORD dwArray)
  279. {
  280. /* local variables */
  281. LPBYTE pByte = NULL;
  282. HRESULT hr;
  283. _variant_t varData;
  284. SAFEARRAY * pSa = NULL;
  285. SAFEARRAYBOUND bd;
  286. DWORD i;
  287. /* function body */
  288. //check incoming parameters
  289. if (!aData)
  290. return varData;
  291. bd.cElements = dwArray;
  292. bd.lLbound = 0;
  293. //read the data from the file one byte at a time
  294. pSa = SafeArrayCreate(VT_UI1, 1, &bd);
  295. if (!pSa)
  296. return varData;
  297. hr = SafeArrayAccessData(pSa,(void**)&pByte);
  298. if (FAILED(hr))
  299. return varData;
  300. for (i=0; i<dwArray; i++)
  301. {
  302. pByte[i] = aData[i];
  303. }
  304. hr = SafeArrayUnaccessData(pSa);
  305. if (FAILED(hr))
  306. return varData;
  307. varData.vt = VT_UI1 | VT_ARRAY;
  308. if (FAILED(SafeArrayCopy(pSa, &varData.parray)))
  309. {
  310. varData.Clear();
  311. return varData;
  312. }
  313. SafeArrayDestroy(pSa);
  314. return varData;
  315. }
  316. //END SetVariantWithBinaryArray
  317. /*********************************************************************
  318. * *
  319. * Written by: Paul Thompson *
  320. * Date: 15 DEC 2000 *
  321. * *
  322. * This function is returns the size, in bytes, of the given *
  323. * variant array. *
  324. * *
  325. *********************************************************************/
  326. //BEGIN GetVariantArraySize
  327. DWORD GetVariantArraySize(_variant_t & varData)
  328. {
  329. /* local variables */
  330. HRESULT hr;
  331. DWORD uSLength = 0;
  332. long uLBound, uUBound;
  333. /* function body */
  334. //check incoming parameters
  335. if ((varData.vt != (VT_ARRAY | VT_UI1)) || (!varData.parray))
  336. return uSLength;
  337. //get the array size
  338. hr = SafeArrayGetLBound(varData.parray, 1, &uLBound);
  339. if (FAILED(hr))
  340. return uSLength;
  341. hr = SafeArrayGetUBound(varData.parray, 1, &uUBound);
  342. if (FAILED(hr))
  343. return uSLength;
  344. uSLength = DWORD(uUBound - uLBound + 1);
  345. return uSLength;
  346. }
  347. //END GetVariantArraySize
  348. /*********************************************************************
  349. * *
  350. * Written by: Paul Thompson *
  351. * Date: 15 DEC 2000 *
  352. * *
  353. * This function is returns the size, in bytes, of the given *
  354. * variant array. *
  355. * *
  356. *********************************************************************/
  357. //BEGIN PrintVariant
  358. void PrintVariant(const _variant_t & varData)
  359. {
  360. /* local variables */
  361. HRESULT hr;
  362. LPBYTE pByte = NULL;
  363. long i;
  364. WCHAR sData[MAX_PATH] = L"";
  365. /* function body */
  366. //check incoming parameters
  367. if ((varData.vt != (VT_ARRAY | VT_UI1)) || (!varData.parray))
  368. return;
  369. //get the array size
  370. long uLBound, uUBound, uSLength;
  371. hr = SafeArrayGetLBound(varData.parray, 1, &uLBound);
  372. if (FAILED(hr))
  373. return;
  374. hr = SafeArrayGetUBound(varData.parray, 1, &uUBound);
  375. if (FAILED(hr))
  376. return;
  377. uSLength = uUBound - uLBound + 1;
  378. //write the data to the file
  379. hr = SafeArrayAccessData(varData.parray,(void**)&pByte);
  380. if (FAILED(hr))
  381. return;
  382. FILE * myfile;
  383. myfile = _wfopen(L"c:\\CryptCheck.txt", L"a+");
  384. for (i=0; i<uSLength; i++)
  385. {
  386. fwprintf(myfile, L"%x ", pByte[i]);
  387. }
  388. hr = SafeArrayUnaccessData(varData.parray);
  389. if (FAILED(hr))
  390. return;
  391. fwprintf(myfile, L"\n");
  392. fclose(myfile);
  393. return;
  394. }
  395. //END PrintVariant