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.

467 lines
14 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) && !ferror(floppyfile))
  181. {
  182. if (fread((void *)(pByte+nTotalRead), 1, 1, floppyfile) == 1)
  183. nTotalRead++;
  184. }
  185. if (ferror(floppyfile)) {
  186. _com_issue_error(HRESULT_FROM_WIN32(ERROR_READ_FAULT));
  187. }
  188. hr = SafeArrayUnaccessData(pSa);
  189. if (FAILED(hr))
  190. _com_issue_error(hr);
  191. //close the file
  192. if (floppyfile)
  193. {
  194. fclose(floppyfile);
  195. floppyfile = NULL;
  196. }
  197. if (nTotalRead != fileLen)
  198. _com_issue_error(HRESULT_FROM_WIN32(ERROR_READ_FAULT));
  199. varData.vt = VT_UI1 | VT_ARRAY;
  200. if (FAILED(SafeArrayCopy(pSa, &varData.parray)))
  201. _com_issue_error(hr);
  202. if (FAILED(SafeArrayDestroy(pSa)))
  203. _com_issue_error(hr);
  204. }
  205. catch (...)
  206. {
  207. if (floppyfile)
  208. fclose(floppyfile);
  209. throw;
  210. }
  211. return varData;
  212. }
  213. //END GetDataFromFloppy
  214. /*********************************************************************
  215. * *
  216. * Written by: Paul Thompson *
  217. * Date: 15 DEC 2000 *
  218. * *
  219. * This function is convert a _variant_t parameter of the type *
  220. * VT_ARRAY | VT_UI1 and returns it in a char array. The caller must*
  221. * free the array with the delete [] call. This function return NULL*
  222. * if the data was not placed in the array. *
  223. * *
  224. *********************************************************************/
  225. //BEGIN GetBinaryArrayFromVariant
  226. char* GetBinaryArrayFromVariant(_variant_t varData)
  227. {
  228. /* local variables */
  229. LPBYTE pByte = NULL;
  230. HRESULT hr;
  231. char * cArray;
  232. int i;
  233. /* function body */
  234. //check incoming parameters
  235. if ((varData.vt != (VT_ARRAY | VT_UI1)) || (!varData.parray))
  236. return NULL;
  237. //get the array size
  238. long uLBound, uUBound, uSLength;
  239. hr = SafeArrayGetLBound(varData.parray, 1, &uLBound);
  240. if (FAILED(hr))
  241. return NULL;
  242. hr = SafeArrayGetUBound(varData.parray, 1, &uUBound);
  243. if (FAILED(hr))
  244. return NULL;
  245. uSLength = uUBound - uLBound + 1;
  246. //create an array to hold all this data
  247. cArray = new char[uSLength+1];
  248. if (!cArray)
  249. return NULL;
  250. //write the data to the file
  251. hr = SafeArrayAccessData(varData.parray,(void**)&pByte);
  252. if (FAILED(hr))
  253. {
  254. delete [] cArray;
  255. return NULL;
  256. }
  257. for (i=0; i<uSLength; i++)
  258. {
  259. cArray[i] = pByte[i];
  260. }
  261. cArray[i] = L'\0';
  262. hr = SafeArrayUnaccessData(varData.parray);
  263. if (FAILED(hr))
  264. {
  265. delete [] cArray;
  266. return NULL;
  267. }
  268. return cArray;
  269. }
  270. //END GetBinaryArrayFromVariant
  271. /*********************************************************************
  272. * *
  273. * Written by: Paul Thompson *
  274. * Date: 15 DEC 2000 *
  275. * *
  276. * This function is convert a char array of binary data to a *
  277. * _variant_t of the type VT_ARRAY | VT_UI1 and returns it. *
  278. * *
  279. *********************************************************************/
  280. //BEGIN SetVariantWithBinaryArray
  281. _variant_t SetVariantWithBinaryArray(char * aData, DWORD dwArray)
  282. {
  283. /* local variables */
  284. LPBYTE pByte = NULL;
  285. HRESULT hr;
  286. _variant_t varData;
  287. SAFEARRAY * pSa = NULL;
  288. SAFEARRAYBOUND bd;
  289. DWORD i;
  290. /* function body */
  291. //check incoming parameters
  292. if (!aData)
  293. return varData;
  294. bd.cElements = dwArray;
  295. bd.lLbound = 0;
  296. //read the data from the file one byte at a time
  297. pSa = SafeArrayCreate(VT_UI1, 1, &bd);
  298. if (!pSa)
  299. return varData;
  300. hr = SafeArrayAccessData(pSa,(void**)&pByte);
  301. if (FAILED(hr))
  302. return varData;
  303. for (i=0; i<dwArray; i++)
  304. {
  305. pByte[i] = aData[i];
  306. }
  307. hr = SafeArrayUnaccessData(pSa);
  308. if (FAILED(hr))
  309. return varData;
  310. varData.vt = VT_UI1 | VT_ARRAY;
  311. if (FAILED(SafeArrayCopy(pSa, &varData.parray)))
  312. {
  313. varData.Clear();
  314. return varData;
  315. }
  316. SafeArrayDestroy(pSa);
  317. return varData;
  318. }
  319. //END SetVariantWithBinaryArray
  320. /*********************************************************************
  321. * *
  322. * Written by: Paul Thompson *
  323. * Date: 15 DEC 2000 *
  324. * *
  325. * This function is returns the size, in bytes, of the given *
  326. * variant array. *
  327. * *
  328. *********************************************************************/
  329. //BEGIN GetVariantArraySize
  330. DWORD GetVariantArraySize(_variant_t & varData)
  331. {
  332. /* local variables */
  333. HRESULT hr;
  334. DWORD uSLength = 0;
  335. long uLBound, uUBound;
  336. /* function body */
  337. //check incoming parameters
  338. if ((varData.vt != (VT_ARRAY | VT_UI1)) || (!varData.parray))
  339. return uSLength;
  340. //get the array size
  341. hr = SafeArrayGetLBound(varData.parray, 1, &uLBound);
  342. if (FAILED(hr))
  343. return uSLength;
  344. hr = SafeArrayGetUBound(varData.parray, 1, &uUBound);
  345. if (FAILED(hr))
  346. return uSLength;
  347. uSLength = DWORD(uUBound - uLBound + 1);
  348. return uSLength;
  349. }
  350. //END GetVariantArraySize
  351. /*********************************************************************
  352. * *
  353. * Written by: Paul Thompson *
  354. * Date: 15 DEC 2000 *
  355. * *
  356. * This function is returns the size, in bytes, of the given *
  357. * variant array. *
  358. * *
  359. *********************************************************************/
  360. //BEGIN PrintVariant
  361. void PrintVariant(const _variant_t & varData)
  362. {
  363. /* local variables */
  364. HRESULT hr;
  365. LPBYTE pByte = NULL;
  366. long i;
  367. WCHAR sData[MAX_PATH] = L"";
  368. /* function body */
  369. //check incoming parameters
  370. if ((varData.vt != (VT_ARRAY | VT_UI1)) || (!varData.parray))
  371. return;
  372. //get the array size
  373. long uLBound, uUBound, uSLength;
  374. hr = SafeArrayGetLBound(varData.parray, 1, &uLBound);
  375. if (FAILED(hr))
  376. return;
  377. hr = SafeArrayGetUBound(varData.parray, 1, &uUBound);
  378. if (FAILED(hr))
  379. return;
  380. uSLength = uUBound - uLBound + 1;
  381. //write the data to the file
  382. hr = SafeArrayAccessData(varData.parray,(void**)&pByte);
  383. if (FAILED(hr))
  384. return;
  385. FILE * myfile;
  386. myfile = _wfopen(L"c:\\CryptCheck.txt", L"a+");
  387. if (myfile == NULL) {
  388. SafeArrayUnaccessData(varData.parray);
  389. return;
  390. }
  391. for (i=0; i<uSLength; i++)
  392. {
  393. fwprintf(myfile, L"%x ", pByte[i]);
  394. }
  395. hr = SafeArrayUnaccessData(varData.parray);
  396. if (FAILED(hr))
  397. return;
  398. fwprintf(myfile, L"\n");
  399. fclose(myfile);
  400. return;
  401. }
  402. //END PrintVariant