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.

1958 lines
45 KiB

  1. // CerClient.cpp : Implementation of CCerClient
  2. #include "stdafx.h"
  3. #include "CERUpload.h"
  4. #include "CerClient.h"
  5. #include "ErrorCodes.h"
  6. #include "Utilities.h"
  7. #include <Wininet.h>
  8. #include <shlobj.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11. char * CCerClient::_approvedDomains[] = { "ocatest",
  12. "oca.microsoft.com",
  13. "oca.microsoft.de",
  14. "oca.microsoft.fr",
  15. "ocadeviis",
  16. "ocajapan.rte.microsoft.com"};
  17. // Utility Functions
  18. DWORD CCerClient::GetMachineName(wchar_t*Path, wchar_t*FileName, wchar_t*MachineName)
  19. {
  20. FILE *hMappingFile = NULL;
  21. wchar_t *Buffer = NULL;
  22. int ErrorCode = 0;
  23. wchar_t TempMachineName[512];
  24. wchar_t *pSource;
  25. wchar_t *pDest;
  26. wchar_t FilePath[512];
  27. int TabCount;
  28. wchar_t* Currpos = NULL;
  29. ZeroMemory(TempMachineName, sizeof(TempMachineName));
  30. if (Path)
  31. {
  32. if (sizeof Path < sizeof FilePath)
  33. {
  34. wcscpy (FilePath, Path);
  35. wcscat(FilePath,L"\\hits.log");
  36. }
  37. else
  38. {
  39. ErrorCode = FILE_DOES_NOT_EXIST;
  40. goto Done;
  41. }
  42. }
  43. else
  44. {
  45. ErrorCode = FILE_DOES_NOT_EXIST;
  46. goto Done;
  47. }
  48. if (!PathFileExistsW(FilePath))
  49. {
  50. wcscpy(MachineName,L"\0");
  51. ErrorCode = FILE_DOES_NOT_EXIST;
  52. }
  53. else
  54. {
  55. hMappingFile = _wfopen(FilePath, L"r");
  56. Buffer = (wchar_t *) malloc(1024 *sizeof(wchar_t));
  57. if (!Buffer)
  58. {
  59. ErrorCode = 2;
  60. }
  61. else
  62. {
  63. if (hMappingFile == NULL)
  64. {
  65. ErrorCode = 1;
  66. }
  67. else
  68. {
  69. // Search the file for the last occurence of FileName and
  70. // Retrieve the Computer Name
  71. // We want the last occurence of the filename since there may
  72. // be duplicates.
  73. ZeroMemory(Buffer,1024 *sizeof(wchar_t));
  74. while (fgetws(Buffer,1024,hMappingFile) != NULL)
  75. {
  76. if (wcscmp(Buffer,L"\0"))
  77. {
  78. // locate the file name.
  79. TabCount = 0;
  80. Currpos = Buffer;
  81. while (TabCount < 3)
  82. {
  83. ++Currpos;
  84. if (*Currpos == L'\t')
  85. ++TabCount;
  86. }
  87. // Skip the tab
  88. ++Currpos;
  89. Buffer[ wcslen(Buffer) - 1] = L'\0';
  90. if (! wcscmp(FileName,Currpos))
  91. {
  92. // copy the machine name into a temp variable
  93. // The file is tab formatted and the machine name is in the second position
  94. pSource = Buffer;
  95. pDest = TempMachineName;
  96. while (*pSource != L'\t')
  97. ++pSource;
  98. ++pSource; // Skip the tab
  99. while ( (*pSource != L'\t') && (*pSource != L'\0') && (*pSource != L' ') )
  100. {
  101. *pDest = *pSource;
  102. ++pSource;
  103. ++pDest;
  104. }
  105. // Null Terminate the Machine Name
  106. *pDest = L'\0';
  107. }
  108. }
  109. // Clear the buffer
  110. ZeroMemory(Buffer, sizeof (Buffer));
  111. }
  112. }
  113. if (Buffer)
  114. {
  115. free (Buffer);
  116. Buffer = NULL;
  117. }
  118. // If we found a machine name convert it to unicode.
  119. // and return it.
  120. if (TempMachineName[0] == L'\0')
  121. {
  122. wcscpy(MachineName,L"\0");
  123. ErrorCode = 3; // No Machine Name Found
  124. }
  125. else
  126. {
  127. ErrorCode = 0;
  128. wcscpy (MachineName,TempMachineName);
  129. }
  130. }
  131. }
  132. // Close the File
  133. if (Buffer != NULL)
  134. free(Buffer);
  135. if (hMappingFile != NULL)
  136. fclose(hMappingFile);
  137. Done:
  138. return ErrorCode;
  139. }
  140. /////////////////////////////////////////////////////////////////////////////
  141. // CCerClient
  142. STDMETHODIMP
  143. CCerClient::GetFileCount(
  144. BSTR *bstrSharePath,
  145. BSTR *bstrTransactID,
  146. VARIANT *iMaxCount,
  147. VARIANT *RetVal)
  148. {
  149. // TODO: Add your implementation code here
  150. wchar_t *TranslogName = NULL; // Name of temperary transaction file
  151. DWORD TranslogNameLength = 0;
  152. int ErrorCode = 3000;
  153. HANDLE hTransactLog = INVALID_HANDLE_VALUE;
  154. WIN32_FIND_DATAW FindFile;
  155. int FileCount = 0;
  156. HANDLE hFindFile;
  157. CComBSTR FileList = L"";
  158. DWORD dwWritten;
  159. wchar_t Msg[255];
  160. WORD ByteOrderMark = 0xFEFF;
  161. RetVal->vt = VT_INT;
  162. int MaxCount = 0;
  163. // ZeroMemory(FileList,sizeof (FileList));
  164. switch(iMaxCount->vt)
  165. {
  166. case VT_INT:
  167. MaxCount = iMaxCount->intVal;
  168. break;
  169. case VT_I2:
  170. MaxCount = iMaxCount->iVal;
  171. break;
  172. default:
  173. MaxCount = iMaxCount->iVal;
  174. }
  175. wsprintfW(Msg, L"MaxCount = %d",MaxCount);
  176. if ( (MaxCount <= 0) || (MaxCount > 60000) )
  177. {
  178. ErrorCode = BAD_COUNT_VALUE;
  179. }
  180. else
  181. {
  182. //::MessageBoxW(NULL,Msg,L"MaxCount",MB_OK);
  183. //::MessageBoxW(NULL,*bstrSharePath, L"Share Path",MB_OK);
  184. if ( ( *bstrSharePath == NULL) || (!wcscmp ( *bstrSharePath, L"\0")) )
  185. {
  186. ErrorCode = NO_SHARE_PATH;
  187. }
  188. else
  189. {
  190. if (!PathIsDirectoryW(*bstrSharePath))
  191. {
  192. ErrorCode = NO_SHARE_PATH;
  193. }
  194. else
  195. {
  196. // Did we get a transaction id
  197. if ( ( *bstrTransactID == NULL) || (!wcscmp ( *bstrTransactID, L"\0")) )
  198. {
  199. ErrorCode = NO_TRANS_ID;
  200. }
  201. else
  202. {
  203. DWORD transid = 0;
  204. wchar_t *terminator = NULL;
  205. TranslogNameLength = (wcslen (*bstrSharePath) +
  206. wcslen(*bstrTransactID) +
  207. 17)
  208. * sizeof (wchar_t);
  209. TranslogName = (wchar_t *) malloc(TranslogNameLength);
  210. if (TranslogName == NULL)
  211. {
  212. ErrorCode = OUT_OF_MEMORY;
  213. }
  214. else
  215. {
  216. // Create our transaction log file if it does not exist
  217. wsprintfW(TranslogName, L"%s\\%s.txt",
  218. *bstrSharePath,
  219. *bstrTransactID);
  220. // ::MessageBoxW(NULL, L"Createing Transaction log", L"TranslogName",MB_OK);
  221. hTransactLog = CreateFileW(TranslogName,
  222. GENERIC_WRITE,
  223. NULL,
  224. NULL,
  225. CREATE_ALWAYS,
  226. FILE_ATTRIBUTE_NORMAL,
  227. NULL);
  228. if (hTransactLog == INVALID_HANDLE_VALUE)
  229. {
  230. ErrorCode = FILE_CREATE_FAILED;
  231. }
  232. else
  233. {
  234. // Now we have the file open see if any entries already exist
  235. // If yes how many more can we add
  236. // Now add up to MaxCount cab files to the transaction log.
  237. wchar_t SearchPath[MAX_PATH];
  238. wsprintfW(SearchPath,L"%s\\*.cab",*bstrSharePath);
  239. hFindFile = FindFirstFileW(SearchPath,&FindFile);
  240. if (hFindFile != INVALID_HANDLE_VALUE)
  241. {
  242. do
  243. {
  244. if (! (FindFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  245. {
  246. // Check to see if the file has an extension like
  247. // Cabbage etc..
  248. if (FindFile.cFileName[wcslen(FindFile.cFileName) - 4] == L'.')
  249. {
  250. // we are ok keep going
  251. ++ FileCount;
  252. FileList += FindFile.cFileName;
  253. //wcscat(FileList,FindFile.cFileName);
  254. FileList += L"\r\n";
  255. }
  256. }
  257. }
  258. while ( (FindNextFileW(hFindFile, &FindFile)) && (FileCount < (MaxCount) ));
  259. // Write the file list to the transaction log
  260. WriteFile(hTransactLog, &ByteOrderMark,2,&dwWritten,NULL);
  261. WriteFile(hTransactLog,
  262. FileList,
  263. wcslen(FileList) * sizeof(wchar_t),
  264. &dwWritten,
  265. NULL);
  266. CloseHandle(hTransactLog);
  267. hTransactLog = INVALID_HANDLE_VALUE;
  268. FindClose(hFindFile);
  269. // return the count of added files.
  270. ErrorCode = FileCount;
  271. }
  272. else
  273. {
  274. ErrorCode = 0;
  275. }
  276. }
  277. }
  278. }
  279. }
  280. }
  281. }
  282. RetVal->intVal = ErrorCode;
  283. if (hTransactLog != INVALID_HANDLE_VALUE)
  284. CloseHandle(hTransactLog);
  285. if (TranslogName != NULL)
  286. free (TranslogName);
  287. return S_OK;
  288. }
  289. /*
  290. Purpose: Upload a file that is part of a transaction via
  291. The microsoft redirector through HTTP
  292. */
  293. STDMETHODIMP
  294. CCerClient::Upload(
  295. BSTR *Path,
  296. BSTR *TransID,
  297. BSTR *FileName,
  298. BSTR *IncidentID,
  299. BSTR *RedirParam,
  300. VARIANT *RetCode
  301. )
  302. {
  303. return E_NOINTERFACE ;
  304. }
  305. STDMETHODIMP
  306. CCerClient::RetryTransaction(
  307. BSTR *Path,
  308. BSTR *TransID,
  309. BSTR *FileName,
  310. VARIANT *RetVal
  311. )
  312. {
  313. // TODO: Add your implementation code here
  314. return E_NOINTERFACE ;
  315. }
  316. STDMETHODIMP
  317. CCerClient::RetryFile(
  318. BSTR *Path,
  319. BSTR *TransID,
  320. BSTR FileName,
  321. VARIANT *RetCode
  322. )
  323. {
  324. return E_NOINTERFACE ;
  325. }
  326. // Get Upload Server Name
  327. int
  328. CCerClient::
  329. GetUploadServerName (
  330. wchar_t* RedirectorParam,
  331. wchar_t* Language,
  332. wchar_t* ServerName
  333. )
  334. {
  335. DWORD ErrorCode = 0;
  336. HINTERNET hRedirUrl = NULL;
  337. wchar_t* pUploadUrl = NULL;
  338. HINTERNET hSession;
  339. BOOL bRet = TRUE;
  340. DWORD dwLastError = FALSE;
  341. URL_COMPONENTSW urlComponents;
  342. DWORD dwUrlLength = 0;
  343. wchar_t ConnectString [255];
  344. if (Language )
  345. {
  346. wsprintfW(ConnectString,L"http://go.microsoft.com/fwlink/?linkid=%s",RedirectorParam);
  347. }
  348. else
  349. {
  350. wsprintfW(ConnectString,L"http://go.microsoft.com/fwlink/?linkid=%s",RedirectorParam);
  351. }
  352. hSession = InternetOpenW(L"CerClient Control",
  353. INTERNET_OPEN_TYPE_PRECONFIG,
  354. NULL,
  355. NULL,
  356. 0);
  357. if (!hSession)
  358. {
  359. ErrorCode = GetLastError();
  360. return ErrorCode;
  361. }
  362. hRedirUrl = InternetOpenUrlW(hSession, ConnectString, NULL, 0, 0, 0);
  363. if(!hRedirUrl)
  364. {
  365. ErrorCode = GetLastError();
  366. InternetCloseHandle(hSession);
  367. return ErrorCode;
  368. }
  369. // Get the URL returned from the MS Corporate IIS redir.dll isapi URL redirector
  370. dwUrlLength = 512;
  371. pUploadUrl = (wchar_t*)malloc(dwUrlLength);
  372. if(!pUploadUrl)
  373. {
  374. //ReturnCode->intVal = GetLastError();
  375. ErrorCode = GetLastError();
  376. InternetCloseHandle(hSession);
  377. InternetCloseHandle(hRedirUrl);
  378. return ErrorCode;
  379. }
  380. do
  381. {
  382. ZeroMemory(pUploadUrl, dwUrlLength);
  383. bRet = InternetQueryOptionW(hRedirUrl, INTERNET_OPTION_URL, pUploadUrl, &dwUrlLength);
  384. if(!bRet)
  385. {
  386. dwLastError = GetLastError();
  387. // If last error was due to insufficient buffer size, create a new one the correct size.
  388. if(dwLastError == ERROR_INSUFFICIENT_BUFFER)
  389. {
  390. free(pUploadUrl);
  391. pUploadUrl = (wchar_t*)malloc(dwUrlLength);
  392. if(!pUploadUrl)
  393. {
  394. ErrorCode = GetLastError();
  395. InternetCloseHandle(hSession);
  396. InternetCloseHandle(hRedirUrl);
  397. if (pUploadUrl)
  398. free (pUploadUrl);
  399. return ErrorCode;
  400. }
  401. }
  402. else
  403. {
  404. ErrorCode = GetLastError();
  405. InternetCloseHandle(hSession);
  406. InternetCloseHandle(hRedirUrl);
  407. if (pUploadUrl)
  408. free (pUploadUrl);
  409. return ErrorCode;
  410. }
  411. }
  412. }while(!bRet);
  413. // Strip out the host name from the URL
  414. ZeroMemory(&urlComponents, sizeof(URL_COMPONENTSW));
  415. urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
  416. urlComponents.lpszHostName = NULL;
  417. urlComponents.dwHostNameLength = 512;
  418. urlComponents.lpszHostName = (wchar_t*)malloc(urlComponents.dwHostNameLength );
  419. if(!urlComponents.lpszHostName)
  420. {
  421. ErrorCode = GetLastError();
  422. InternetCloseHandle(hSession);
  423. InternetCloseHandle(hRedirUrl);
  424. if (pUploadUrl)
  425. free (pUploadUrl);
  426. return ErrorCode;
  427. }
  428. do
  429. {
  430. ZeroMemory(urlComponents.lpszHostName, urlComponents.dwHostNameLength);
  431. bRet = InternetCrackUrlW(pUploadUrl, dwUrlLength, 0, &urlComponents);
  432. if(!bRet)
  433. {
  434. dwLastError = GetLastError();
  435. // If last error was due to insufficient buffer size, create a new one the correct size.
  436. if(dwLastError == ERROR_INSUFFICIENT_BUFFER)
  437. {
  438. if (urlComponents.lpszHostName != NULL)
  439. free(urlComponents.lpszHostName);
  440. if ( (urlComponents.lpszHostName = (wchar_t*)malloc(urlComponents.dwHostNameLength) )!= NULL)
  441. {
  442. ZeroMemory(urlComponents.lpszHostName,urlComponents.dwHostNameLength);
  443. }
  444. else
  445. {
  446. ErrorCode = GetLastError();
  447. InternetCloseHandle(hSession);
  448. InternetCloseHandle(hRedirUrl);
  449. if (pUploadUrl)
  450. free (pUploadUrl);
  451. return ErrorCode;
  452. }
  453. }
  454. else
  455. {
  456. ErrorCode = GetLastError();
  457. InternetCloseHandle(hSession);
  458. InternetCloseHandle(hRedirUrl);
  459. if (pUploadUrl)
  460. free (pUploadUrl);
  461. return ErrorCode;
  462. }
  463. }
  464. }while(!bRet);
  465. if (hRedirUrl)
  466. InternetCloseHandle(hRedirUrl);
  467. if (hSession)
  468. InternetCloseHandle(hSession);
  469. if (pUploadUrl)
  470. free (pUploadUrl);
  471. wcscpy (ServerName, (wchar_t *)urlComponents.lpszHostName);
  472. if (urlComponents.lpszHostName)
  473. free (urlComponents.lpszHostName);
  474. return 0;
  475. }
  476. STDMETHODIMP
  477. CCerClient::GetFileNames(
  478. BSTR *Path,
  479. BSTR *TransID,
  480. VARIANT *Count,
  481. VARIANT *FileList
  482. )
  483. {
  484. wchar_t *OldFileList= NULL; // Files contained in Transid.Queue.log
  485. wchar_t *NewFileList= NULL; // Files to be re-written to Transid.Queue.log
  486. CComBSTR RetFileList = L""; // List of files to return.
  487. HANDLE hTransIDLog = INVALID_HANDLE_VALUE; // Handle to original Transid.Queue.Log file
  488. wchar_t LogFilePath[MAX_PATH]; // Path to Transid.queue.log file
  489. wchar_t *Temp;
  490. DWORD dwFileSize;
  491. DWORD dwBytesRead;
  492. DWORD dwBytesWritten;
  493. int ErrorCode = 0;
  494. WORD ByteOrderMark = 0xFEFF;
  495. int MaxCount;
  496. switch (Count->vt)
  497. {
  498. case VT_INT:
  499. MaxCount = Count->intVal;
  500. break;
  501. case VT_I2:
  502. MaxCount = Count->iVal;
  503. break;
  504. default:
  505. MaxCount = Count->iVal;
  506. }
  507. // WCHAR Msg[255];
  508. // wsprintfW(Msg,L"Path: %s, TransID: %s, Count: %d, MaxCount: %d",*Path, *TransID, Count->iVal, MaxCount);
  509. // ::MessageBoxW(NULL,Msg,L"Getting File Names",MB_OK);
  510. if ( ( *Path == NULL) || (!wcscmp ( *Path, L"\0")) )
  511. {
  512. ErrorCode = NO_SHARE_PATH;
  513. }
  514. else
  515. {
  516. // Build path to original TransID.queue.log file.
  517. wsprintfW (LogFilePath,L"%s\\%s.txt",*Path, *TransID);
  518. // Open the List of queued files.
  519. hTransIDLog = CreateFileW(LogFilePath,
  520. GENERIC_READ,
  521. NULL,
  522. NULL,
  523. OPEN_EXISTING,
  524. FILE_ATTRIBUTE_NORMAL,
  525. NULL);
  526. // Find out where we left off.
  527. if (hTransIDLog == (INVALID_HANDLE_VALUE))
  528. {
  529. ErrorCode = FILE_OPEN_FAILED;
  530. }
  531. else
  532. {
  533. dwFileSize = GetFileSize(hTransIDLog,NULL);
  534. OldFileList = (wchar_t*) malloc (dwFileSize * 3);
  535. NewFileList = (wchar_t*) malloc (dwFileSize * 3);
  536. if ( (!OldFileList) || (!NewFileList))
  537. {
  538. ErrorCode = OUT_OF_MEMORY;
  539. if (NewFileList) free (NewFileList);
  540. if (OldFileList) free (OldFileList);
  541. }
  542. else
  543. {
  544. ZeroMemory (OldFileList, dwFileSize * 2);
  545. ZeroMemory (NewFileList, dwFileSize * 2);
  546. if (!
  547. ReadFile(hTransIDLog, OldFileList,
  548. dwFileSize,&dwBytesRead,NULL))
  549. {
  550. CloseHandle(hTransIDLog);
  551. goto Done;
  552. }
  553. CloseHandle (hTransIDLog);
  554. hTransIDLog = INVALID_HANDLE_VALUE;
  555. if (dwBytesRead == 0)
  556. {
  557. ErrorCode = FILE_READ_FAILED;
  558. }
  559. else
  560. {
  561. // read through the file buffer until we find a filename that
  562. // does not start with a -
  563. Temp = OldFileList;
  564. wchar_t* pNewFileList=NULL;
  565. wchar_t FileName[MAX_PATH];
  566. DWORD CharCount = dwFileSize;
  567. ++Temp;
  568. --CharCount;
  569. pNewFileList = NewFileList;
  570. if (*Temp == L'-')
  571. {
  572. BOOL Done = FALSE;
  573. // Lets find our starting position
  574. do
  575. {
  576. while( (*Temp != L'\n') && ((CharCount > 0) && (CharCount <= dwFileSize) ))
  577. {
  578. *pNewFileList = *Temp;
  579. ++Temp;
  580. --CharCount;
  581. ++pNewFileList;
  582. }
  583. *pNewFileList = *Temp;
  584. ++pNewFileList;
  585. ++Temp; // Skip over the newline
  586. --CharCount;
  587. if (*Temp != L'-')
  588. Done = TRUE;
  589. *pNewFileList = L'\0';
  590. } while ( (!Done) && ( (CharCount > 0) && (CharCount <= dwFileSize) ));
  591. }
  592. if ( (CharCount > 0) && (CharCount <= dwFileSize) )
  593. {
  594. // Now build the lists....
  595. int dwFileCount = 0;
  596. wchar_t *NewFL;
  597. // wsprintfW(Msg, L"MaxCount = %d", MaxCount);
  598. // ::MessageBoxW(NULL,Msg,L"MAX_COUNT",MB_OK);
  599. while ( (dwFileCount < MaxCount) &&
  600. (CharCount > 0) && (CharCount < dwFileSize) )
  601. {
  602. ZeroMemory (FileName, sizeof(FileName));
  603. NewFL = FileName;
  604. *NewFL = *Temp;
  605. ++NewFL;
  606. ++Temp;
  607. -- CharCount;
  608. // Copy characters until we hit a carriage return
  609. while ( (*Temp != L'\r') && ( (CharCount > 0) && (CharCount <= dwFileSize) ))
  610. {
  611. *NewFL = *Temp;
  612. ++ NewFL;
  613. ++ Temp;
  614. -- CharCount;
  615. *NewFL = L'\0';
  616. }
  617. // Add the new file name to the Return File List string
  618. if (wcslen (FileName) > 0)
  619. {
  620. RetFileList += FileName;
  621. RetFileList += L";";
  622. dwFileCount++;
  623. // only add the cr and lf codes to the NewFileList string.
  624. *NewFL = *Temp;
  625. ++NewFL;
  626. ++Temp;
  627. --CharCount;
  628. *NewFL = *Temp;
  629. ++ Temp;
  630. --CharCount;
  631. wcscat(NewFileList,L"-");
  632. wcscat(NewFileList,FileName);
  633. }
  634. }
  635. // Delete the Current transaction queue file
  636. if (!DeleteFileW(LogFilePath) )
  637. {
  638. //::MessageBoxW(NULL,L"Failed to delete .txt file",NULL,MB_OK);
  639. ;
  640. }
  641. hTransIDLog = CreateFileW( LogFilePath,
  642. GENERIC_WRITE,
  643. NULL,
  644. NULL,
  645. CREATE_ALWAYS,
  646. FILE_ATTRIBUTE_NORMAL,
  647. NULL);
  648. if (hTransIDLog != INVALID_HANDLE_VALUE)
  649. {
  650. WriteFile(hTransIDLog, &ByteOrderMark,2,&dwBytesWritten,NULL);
  651. WriteFile(hTransIDLog,
  652. NewFileList,
  653. wcslen(NewFileList) * sizeof(wchar_t),
  654. &dwBytesWritten,
  655. NULL);
  656. WriteFile(hTransIDLog,
  657. Temp,
  658. wcslen(Temp) * sizeof(wchar_t),
  659. &dwBytesWritten,
  660. NULL);
  661. CloseHandle(hTransIDLog);
  662. hTransIDLog = INVALID_HANDLE_VALUE;
  663. }
  664. }
  665. if (NewFileList)
  666. free (NewFileList);
  667. if (OldFileList)
  668. free (OldFileList);
  669. }
  670. }
  671. }
  672. }
  673. if (hTransIDLog != INVALID_HANDLE_VALUE)
  674. CloseHandle(hTransIDLog);
  675. // ::MessageBoxW(NULL,RetFileList,L"Returning these files after Write",MB_OK);
  676. // wsprintfW(Msg, L"Error code = %d", ErrorCode);
  677. // ::MessageBoxW(NULL,Msg,L"Current Error Status",MB_OK);
  678. Done:
  679. FileList->vt = VT_BSTR;
  680. FileList->bstrVal = RetFileList.Detach();
  681. if(NewFileList)
  682. free(NewFileList);
  683. if(OldFileList)
  684. free(OldFileList);
  685. return S_OK;
  686. }
  687. STDMETHODIMP CCerClient::Browse(BSTR *WindowTitle, VARIANT *Path)
  688. {
  689. BROWSEINFOW BrowseInfo;
  690. CComBSTR SharePath = L"";
  691. LPMALLOC lpMalloc;
  692. HWND hParent;
  693. LPITEMIDLIST pidlSelected = NULL;
  694. wchar_t TempPath[MAX_PATH];
  695. CComBSTR WindowText = *WindowTitle;
  696. WindowText += L" - Microsoft Internet Explorer";
  697. hParent = FindWindowExW(NULL,NULL,L"IEFrame",WindowText);
  698. ZeroMemory (&BrowseInfo,sizeof(BROWSEINFO));
  699. BrowseInfo.hwndOwner = hParent;
  700. BrowseInfo.ulFlags = BIF_USENEWUI | BIF_EDITBOX;
  701. if(::SHGetMalloc(&lpMalloc) == NOERROR)
  702. {
  703. pidlSelected = SHBrowseForFolderW(&BrowseInfo);
  704. if (::SHGetPathFromIDListW(pidlSelected, TempPath))
  705. lpMalloc->Release();
  706. SharePath+=TempPath;
  707. }
  708. Path->vt = VT_BSTR;
  709. Path->bstrVal = SharePath.Detach();
  710. return S_OK;
  711. }
  712. DWORD
  713. CCerClient::GetComputerNameFromCSV(
  714. wchar_t* CsvFileName,
  715. wchar_t* FileName,
  716. wchar_t* ComputerName
  717. )
  718. {
  719. wchar_t *Buffer = NULL;
  720. BOOL Done = FALSE;
  721. HANDLE hCsv = INVALID_HANDLE_VALUE;
  722. wchar_t TempFileName[MAX_PATH];
  723. wchar_t *Source = NULL;
  724. wchar_t *Dest = NULL;
  725. DWORD FileSize = 0;
  726. DWORD dwBytesRead = 0;
  727. // Move to the beginning of the file.
  728. //::MessageBoxW(NULL,L"Getting the ComputerName from the CSV file",NULL,MB_OK);
  729. hCsv = CreateFileW(CsvFileName,
  730. GENERIC_READ,
  731. NULL,
  732. NULL,
  733. OPEN_EXISTING,
  734. FILE_ATTRIBUTE_NORMAL,
  735. NULL);
  736. if (hCsv != INVALID_HANDLE_VALUE)
  737. {
  738. FileSize = GetFileSize(hCsv,NULL);
  739. if (FileSize > 0)
  740. {
  741. if ((Buffer = ( (wchar_t *) malloc (FileSize * 2))) == NULL)
  742. {
  743. CloseHandle(hCsv);
  744. return 0;
  745. }
  746. }
  747. else
  748. {
  749. CloseHandle(hCsv);
  750. return 0;
  751. }
  752. // Now look for the Filename
  753. ZeroMemory(TempFileName,MAX_PATH * sizeof(wchar_t));
  754. ZeroMemory(Buffer,FileSize * 2);
  755. if (!ReadFile(hCsv,
  756. Buffer,
  757. FileSize * 2,
  758. &dwBytesRead,
  759. NULL))
  760. {
  761. CloseHandle (hCsv);
  762. goto Done;
  763. }
  764. Source = Buffer;
  765. int Testing = IS_TEXT_UNICODE_SIGNATURE;
  766. // If the unicode header bytes appear remove skip past them
  767. if (IsTextUnicode(Buffer,FileSize * 2,&Testing))
  768. {
  769. ++Source;
  770. }
  771. while (! Done)
  772. {
  773. Dest = TempFileName;
  774. while ( (*Source != L'\r') &&
  775. (*Source != L'\0') &&
  776. (*Source != L',') )
  777. {
  778. *Dest = *Source;
  779. ++Source;
  780. ++Dest;
  781. }
  782. // Null Terminate the destination string.
  783. *Dest = L'\0';
  784. if (!wcscmp(TempFileName, FileName))
  785. {
  786. ++Source; // Skip the Comma
  787. // Now copy the computer name into ComputerName
  788. Dest = ComputerName;
  789. while ( *Source != L',' )
  790. {
  791. *Dest = *Source;
  792. ++Dest;
  793. ++Source;
  794. }
  795. // Null Terminate the ComputerName
  796. *Dest = L'\0';
  797. Done = TRUE;
  798. }
  799. else
  800. {
  801. ZeroMemory(TempFileName,MAX_PATH * sizeof(wchar_t));
  802. while ((*Source != L'\n') && (*Source != L'\0'))
  803. ++ Source;
  804. if (*Source != L'\0')
  805. ++Source; // skip the newline
  806. else
  807. Done = TRUE;
  808. }
  809. }
  810. CloseHandle(hCsv);
  811. }
  812. Done:
  813. if (Buffer)
  814. free (Buffer);
  815. return 1;
  816. }
  817. // Purpose: Given a list of comma seperated file names
  818. // Return the list as filename,computername; etc...
  819. STDMETHODIMP CCerClient::GetCompuerNames(BSTR *Path, BSTR *TransID, BSTR *FileList, VARIANT *RetFileList)
  820. {
  821. return E_NOINTERFACE ;
  822. }
  823. STDMETHODIMP CCerClient::GetAllComputerNames(BSTR *Path, BSTR *TransID, BSTR *FileList, VARIANT *ReturnList)
  824. {
  825. wchar_t CsvFileName[MAX_PATH];
  826. // BOOL Done = FALSE;
  827. CComBSTR FinalList = L"";
  828. // FILE *hCsv = NULL;
  829. wchar_t FileName[MAX_PATH];
  830. wchar_t *Source = NULL;
  831. wchar_t *Dest = NULL;
  832. wchar_t ComputerName[MAX_PATH];
  833. BOOL Done2 = FALSE;
  834. //::MessageBoxW(NULL,*Path,L"Path to files",MB_OK);
  835. //::MessageBoxW(NULL,*TransID,L"TransID",MB_OK);
  836. //::MessageBoxW(NULL,*FileList,L"File List",MB_OK);
  837. ZeroMemory (ComputerName, MAX_PATH *sizeof(wchar_t));
  838. if (PathIsDirectoryW(*Path))
  839. {
  840. // Build csv file name
  841. wsprintfW(CsvFileName, L"%s\\%s.csv",*Path,*TransID);
  842. //::MessageBoxW(NULL,CsvFileName,L"Looking for CSV File: ",MB_OK);
  843. if (PathFileExistsW(CsvFileName))
  844. {
  845. // now go through the file list and get the machine names
  846. Source = *FileList;
  847. while(!Done2)
  848. {
  849. //::MessageBoxW(NULL,L"Inside the while loop",NULL,MB_OK);
  850. ZeroMemory(FileName, MAX_PATH );
  851. Dest = FileName;
  852. while ((*Source != L'\r') &&
  853. (*Source != L'\0') && (*Source != L',') )
  854. {
  855. *Dest = *Source;
  856. ++Source;
  857. ++Dest;
  858. }
  859. // Null Terminate the destination string.
  860. *Dest = L'\0';
  861. Dest = FileName;
  862. if (!wcscmp(Dest, L"\0"))
  863. Done2 = TRUE;
  864. else
  865. {
  866. // Get the ComputerName;
  867. //::MessageBoxW(NULL,FileName, L"Getting computer name for file: ",MB_OK);
  868. if (GetComputerNameFromCSV(CsvFileName, FileName, ComputerName))
  869. {
  870. //::MessageBoxW(NULL,ComputerName,L"Computer Name Found: ",MB_OK);
  871. // add the file Name and computer name to the return list
  872. FinalList+= FileName;
  873. FinalList += L",";
  874. FinalList += ComputerName;
  875. FinalList += L";";
  876. }
  877. }
  878. if (*Source == L'\0')
  879. Done2 = TRUE;
  880. else
  881. ++Source;
  882. }
  883. }
  884. else
  885. {
  886. //MessageBoxW(NULL,L"Failed to locate CSV File",CsvFileName,MB_OK)
  887. ReturnList->vt = VT_INT;
  888. ReturnList->intVal = FILE_DOES_NOT_EXIST;
  889. return S_OK;
  890. }
  891. }
  892. ReturnList->vt = VT_BSTR;
  893. ReturnList->bstrVal = FinalList.Detach();
  894. return S_OK;
  895. }
  896. int
  897. CCerClient::GetNewFileNameFromCSV(wchar_t *Path, wchar_t *transid, wchar_t *FileName,wchar_t *NewFileName)
  898. {
  899. wchar_t CsvFileName[MAX_PATH];
  900. wchar_t *Buffer = NULL;
  901. BOOL Done = FALSE;
  902. HANDLE hCsv = INVALID_HANDLE_VALUE;
  903. wchar_t TempFileName[MAX_PATH];
  904. wchar_t *Source = NULL;
  905. wchar_t *Dest = NULL;
  906. DWORD FileSize = 0;
  907. DWORD dwBytesRead = 0;
  908. // Move to the beginning of the file.
  909. wsprintfW(CsvFileName,L"%s\\%s.csv",Path,transid);
  910. wcscpy (NewFileName,L"\0");
  911. hCsv = CreateFileW(CsvFileName,
  912. GENERIC_READ,
  913. NULL,
  914. NULL,
  915. OPEN_EXISTING,
  916. FILE_ATTRIBUTE_NORMAL,
  917. NULL);
  918. if (hCsv != INVALID_HANDLE_VALUE)
  919. {
  920. FileSize = GetFileSize(hCsv,NULL);
  921. if (FileSize > 0)
  922. {
  923. if ( (Buffer = (wchar_t *) malloc (FileSize * 2)) ==NULL)
  924. {
  925. CloseHandle(hCsv);
  926. return 0;
  927. }
  928. }
  929. else
  930. {
  931. CloseHandle(hCsv);
  932. return 0;
  933. }
  934. // Now look for the Filename
  935. ZeroMemory(TempFileName,MAX_PATH * sizeof(wchar_t));
  936. ZeroMemory(Buffer,FileSize * 2);
  937. if (!ReadFile(hCsv,
  938. Buffer,
  939. FileSize * 2,
  940. &dwBytesRead,
  941. NULL))
  942. {
  943. CloseHandle(hCsv);
  944. goto Done;
  945. }
  946. Source = Buffer;
  947. int Testing = IS_TEXT_UNICODE_SIGNATURE;
  948. // If the unicode header bytes appear remove skip past them
  949. if (IsTextUnicode(Buffer,FileSize * 2,&Testing))
  950. {
  951. ++Source;
  952. }
  953. while (! Done)
  954. {
  955. Dest = TempFileName;
  956. while ( (*Source != L'\r') &&
  957. (*Source != L'\0') &&
  958. (*Source != L',') )
  959. {
  960. *Dest = *Source;
  961. ++Source;
  962. ++Dest;
  963. }
  964. // Null Terminate the destination string.
  965. *Dest = L'\0';
  966. if (!wcscmp(TempFileName, FileName))
  967. {
  968. // We found the original file name now retrieve the new file name
  969. ++Source; // Skip the Comma
  970. while (*Source != L',') // The new file name is in field 3
  971. ++Source;
  972. ++Source; // Skip the comma
  973. // Now copy the computer name into ComputerName
  974. Dest = NewFileName;
  975. while ( (*Source != L'\r') &&
  976. (*Source != L'\0') )
  977. {
  978. *Dest = *Source;
  979. ++Dest;
  980. ++Source;
  981. }
  982. // Null Terminate the ComputerName
  983. *Dest = L'\0';
  984. Done = TRUE;
  985. }
  986. else
  987. {
  988. ZeroMemory(TempFileName,MAX_PATH * sizeof(wchar_t));
  989. while (*Source != L'\n')
  990. ++ Source;
  991. ++Source; // skip the newline
  992. }
  993. }
  994. CloseHandle(hCsv);
  995. }
  996. Done:
  997. if (Buffer)
  998. free (Buffer);
  999. if (wcscmp(NewFileName,L"\0"))
  1000. return 1;
  1001. else
  1002. return 0;
  1003. }
  1004. STDMETHODIMP CCerClient::RetryFile1(BSTR *Path, BSTR *TransID, BSTR *FileName, BSTR *IncidentID, BSTR *RedirParam, VARIANT *RetCode)
  1005. {
  1006. // Get the Name we renamed the file to
  1007. // Build the source path to the renamed file
  1008. wchar_t DestFileName[MAX_PATH];
  1009. wchar_t SourceFileName[MAX_PATH];
  1010. wchar_t ServerName[MAX_PATH];
  1011. int ErrorCode = 0;
  1012. HINTERNET hSession = NULL;
  1013. HINTERNET hRequest = NULL;
  1014. HINTERNET hConnect = NULL;
  1015. INTERNET_BUFFERS BufferIn = {0};
  1016. DWORD ResponseCode = 0;
  1017. BOOL UploadSuccess = FALSE;
  1018. DWORD NumRetries = 0;
  1019. HANDLE hSourceFile = INVALID_HANDLE_VALUE;
  1020. BYTE *pSourceBuffer = NULL;
  1021. DWORD dwBytesRead = 0;
  1022. DWORD dwBytesWritten = 0;
  1023. BOOL bRet = FALSE;
  1024. DWORD ResLength = 255;
  1025. DWORD index = 0;
  1026. DWORD MaxRetries = 5;
  1027. // wchar_t MachineName[512];
  1028. // wchar_t CSVBuffer[512];
  1029. // HANDLE hCsvFile = INVALID_HANDLE_VALUE;
  1030. wchar_t CSVFileName[255];
  1031. // WORD ByteOrderMark = 0xFEFF;
  1032. wchar_t NewFileName[MAX_PATH];
  1033. static const wchar_t *pszAccept[] = {L"*.*", 0};
  1034. // Build the destination FileName
  1035. if (!GetNewFileNameFromCSV(*Path,*TransID,*FileName,NewFileName))
  1036. {
  1037. RetCode->vt = VT_INT;
  1038. RetCode->intVal = -10;
  1039. }
  1040. // Build Source File Name
  1041. wsprintfW(SourceFileName, L"%s\\%s", *Path, NewFileName);
  1042. // Get the ServerName from the redirector
  1043. ErrorCode = GetUploadServerName(*RedirParam,NULL,ServerName);
  1044. wsprintfW(CSVFileName,L"%s\\%s.csv",*Path,*TransID);
  1045. if (!PathFileExistsW(SourceFileName))
  1046. ErrorCode = FILE_DOES_NOT_EXIST;
  1047. if ( (!wcscmp(*TransID,L"")) || ((*TransID)[0] == L' ') )
  1048. ErrorCode = NO_TRANS_ID;
  1049. wsprintfW(DestFileName, L"CerInqueue\\U_%s.%s.%s",*TransID,*IncidentID,*FileName);
  1050. if ( (pSourceBuffer = (BYTE *) malloc (10000)) == NULL)
  1051. {
  1052. ErrorCode = GetLastError();
  1053. goto Done;
  1054. }
  1055. if (!ErrorCode)
  1056. {
  1057. // Open the internet session
  1058. while ((NumRetries < MaxRetries) && (!UploadSuccess))
  1059. {
  1060. // ::MessageBoxW(NULL,L"Opening the session",NULL,MB_OK);
  1061. hSession = InternetOpenW(L"CerClientControl",
  1062. INTERNET_OPEN_TYPE_PRECONFIG,
  1063. NULL,
  1064. NULL,
  1065. 0);
  1066. if (!hSession)
  1067. {
  1068. free (pSourceBuffer);
  1069. ErrorCode = GetLastError();
  1070. return ErrorCode;
  1071. }
  1072. // ::MessageBoxW(NULL,L"We have a session",NULL,MB_OK);
  1073. hConnect = InternetConnectW(hSession,
  1074. ServerName,
  1075. INTERNET_DEFAULT_HTTP_PORT,
  1076. NULL,
  1077. NULL,
  1078. INTERNET_SERVICE_HTTP,
  1079. 0,
  1080. NULL);
  1081. if (hConnect)
  1082. {
  1083. // ::MessageBoxW(NULL,L"We have a connection",NULL,MB_OK);
  1084. hRequest = HttpOpenRequestW (hConnect,
  1085. L"PUT",
  1086. DestFileName,
  1087. NULL,
  1088. NULL,
  1089. pszAccept,
  1090. INTERNET_FLAG_NEED_FILE|INTERNET_FLAG_NO_CACHE_WRITE,
  1091. 0);
  1092. if (hRequest)
  1093. {
  1094. hSourceFile = CreateFileW( SourceFileName,
  1095. GENERIC_READ,
  1096. FILE_SHARE_READ,
  1097. NULL,
  1098. OPEN_EXISTING,
  1099. FILE_ATTRIBUTE_NORMAL,
  1100. NULL);
  1101. //::MessageBoxW(NULL,L"Request has been opened",NULL,MB_OK);
  1102. if (hSourceFile != INVALID_HANDLE_VALUE)
  1103. {
  1104. // Clear the buffer
  1105. BufferIn.dwStructSize = sizeof( INTERNET_BUFFERSW );
  1106. BufferIn.Next = NULL;
  1107. BufferIn.lpcszHeader = NULL;
  1108. BufferIn.dwHeadersLength = 0;
  1109. BufferIn.dwHeadersTotal = 0;
  1110. BufferIn.lpvBuffer = NULL;
  1111. BufferIn.dwBufferLength = 0;
  1112. BufferIn.dwOffsetLow = 0;
  1113. BufferIn.dwOffsetHigh = 0;
  1114. BufferIn.dwBufferTotal = GetFileSize (hSourceFile, NULL);
  1115. ZeroMemory(pSourceBuffer, 10000); // Fill buffer with data
  1116. if(HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
  1117. {
  1118. // ::MessageBoxW(NULL,L"Sending Request",NULL,MB_OK);
  1119. do
  1120. {
  1121. dwBytesRead = 0;
  1122. if(! ReadFile(hSourceFile, pSourceBuffer, 10000, &dwBytesRead, NULL) )
  1123. {
  1124. ErrorCode = GetLastError();
  1125. }
  1126. else
  1127. {
  1128. bRet = InternetWriteFile(hRequest, pSourceBuffer, dwBytesRead, &dwBytesWritten);
  1129. if ( (!bRet) || (dwBytesWritten==0) )
  1130. {
  1131. ErrorCode = GetLastError();
  1132. }
  1133. }
  1134. } while ((dwBytesRead == 10000) && (!ErrorCode) );
  1135. if (!ErrorCode)
  1136. {
  1137. bRet = HttpEndRequest(hRequest, NULL, 0, 0);
  1138. if (!bRet)
  1139. {
  1140. ErrorCode = GetLastError();
  1141. }
  1142. else
  1143. {
  1144. // ::MessageBoxW(NULL,L"Ending Request",NULL,MB_OK);
  1145. ResponseCode = 0;
  1146. HttpQueryInfo(hRequest,
  1147. HTTP_QUERY_STATUS_CODE |HTTP_QUERY_FLAG_NUMBER ,
  1148. &ResponseCode,
  1149. &ResLength,
  1150. &index);
  1151. if ( (ResponseCode == 200) || (ResponseCode == 201))
  1152. {
  1153. ErrorCode = 0;
  1154. UploadSuccess = TRUE;
  1155. // ::MessageBoxW(NULL,L"Upload was successfull",NULL,MB_OK);
  1156. }
  1157. else
  1158. {
  1159. ErrorCode= ResponseCode;
  1160. ++NumRetries;
  1161. }
  1162. }
  1163. }
  1164. }
  1165. }
  1166. }
  1167. }
  1168. // ::MessageBoxW(NULL,L"Cleaning Up",NULL,MB_OK);
  1169. if (!UploadSuccess)
  1170. {
  1171. ++NumRetries;
  1172. }
  1173. if (hSourceFile != INVALID_HANDLE_VALUE)
  1174. CloseHandle (hSourceFile);
  1175. if (hRequest)
  1176. InternetCloseHandle(hRequest);
  1177. if (hConnect)
  1178. InternetCloseHandle(hConnect);
  1179. if (hSession)
  1180. InternetCloseHandle(hSession);
  1181. }
  1182. // ::MessageBoxW(NULL,L"freeing source buffer",NULL,MB_OK);
  1183. if (pSourceBuffer)
  1184. {
  1185. free (pSourceBuffer);
  1186. pSourceBuffer = NULL;
  1187. }
  1188. }
  1189. Done:
  1190. if (pSourceBuffer)
  1191. free(pSourceBuffer);
  1192. RetCode->vt = VT_INT;
  1193. RetCode->intVal = ErrorCode;
  1194. return S_OK;
  1195. }
  1196. STDMETHODIMP CCerClient::EndTransaction(BSTR *SharePath, BSTR *TransID, VARIANT *RetCode)
  1197. {
  1198. wchar_t TransfileName[MAX_PATH];
  1199. RetCode->vt = VT_INT;
  1200. RetCode->intVal = 0;
  1201. wsprintfW(TransfileName,L"%s\\%s.txt",*SharePath,*TransID);
  1202. if (PathFileExistsW(TransfileName))
  1203. DeleteFileW(TransfileName);
  1204. else
  1205. {
  1206. RetCode->intVal = 1;
  1207. }
  1208. return S_OK;
  1209. }
  1210. STDMETHODIMP CCerClient::Upload1(BSTR *Path, BSTR *TransID, BSTR *FileName, BSTR *IncidentID, BSTR *RedirParam, BSTR *Type, VARIANT *RetCode)
  1211. {
  1212. // TODO: Add your implementation code here
  1213. wchar_t DestFileName[MAX_PATH];
  1214. wchar_t SourceFileName[MAX_PATH];
  1215. wchar_t ServerName[MAX_PATH];
  1216. int ErrorCode = 0;
  1217. HINTERNET hSession = NULL;
  1218. HINTERNET hRequest = NULL;
  1219. HINTERNET hConnect = NULL;
  1220. INTERNET_BUFFERS BufferIn = {0};
  1221. DWORD ResponseCode = 0;
  1222. BOOL UploadSuccess = FALSE;
  1223. DWORD NumRetries = 0;
  1224. HANDLE hSourceFile = INVALID_HANDLE_VALUE;
  1225. BYTE *pSourceBuffer = NULL;
  1226. DWORD dwBytesRead = 0;
  1227. DWORD dwBytesWritten = 0;
  1228. BOOL bRet = FALSE;
  1229. DWORD ResLength = 255;
  1230. DWORD index = 0;
  1231. DWORD MaxRetries = 5;
  1232. wchar_t MachineName[512];
  1233. wchar_t CSVBuffer[512];
  1234. HANDLE hCsvFile = INVALID_HANDLE_VALUE;
  1235. wchar_t CSVFileName[255];
  1236. WORD ByteOrderMark = 0xFEFF;
  1237. static const wchar_t *pszAccept[] = {L"*.*", 0};
  1238. if ((!Path) || (!TransID) ||(!FileName) || (!IncidentID) || (!RedirParam) || (!Type))
  1239. {
  1240. RetCode->vt = VT_INT;
  1241. RetCode->intVal = -1;
  1242. return S_OK;
  1243. }
  1244. // Build Source File Name
  1245. if (!wcscmp(*FileName,L"\0"))
  1246. {
  1247. RetCode->vt = VT_INT;
  1248. RetCode->intVal = -1;
  1249. return S_OK;
  1250. }
  1251. wsprintfW(SourceFileName, L"%s\\%s", *Path, *FileName);
  1252. // Get the ServerName from the redirector
  1253. ErrorCode = GetUploadServerName(*RedirParam,NULL,ServerName);
  1254. if (!ErrorCode)
  1255. {
  1256. wsprintfW(CSVFileName,L"%s\\%s.csv",*Path,*TransID);
  1257. if ( (!PathFileExistsW(SourceFileName) ) || (wcslen(*FileName) < 4 ))
  1258. ErrorCode = FILE_DOES_NOT_EXIST;
  1259. if ( (!wcscmp(*TransID,L"")) || ((*TransID)[0] == L' ') )
  1260. ErrorCode = NO_TRANS_ID;
  1261. // Build the destination FileName
  1262. // First see which Virtual directory to use.
  1263. if ( !_wcsicmp(*Type,L"bluescreen"))
  1264. {
  1265. wsprintfW(DestFileName, L"CerBluescreen\\U_%s.%s.%s",*IncidentID,*TransID,*FileName);
  1266. }
  1267. else
  1268. {
  1269. if (!_wcsicmp(*Type,L"appcompat"))
  1270. {
  1271. wsprintfW(DestFileName, L"CerAppCompat\\U_%s.%s.%s",*IncidentID,*TransID,*FileName);
  1272. }
  1273. else
  1274. {
  1275. if (!_wcsicmp(*Type,L"shutdown"))
  1276. {
  1277. wsprintfW(DestFileName, L"CerShutdown\\U_%s.%s.%s",*IncidentID,*TransID,*FileName);
  1278. }
  1279. else
  1280. {
  1281. ErrorCode = UNKNOWN_UPLOAD_TYPE;
  1282. }
  1283. }
  1284. }
  1285. }
  1286. if (!ErrorCode)
  1287. {
  1288. pSourceBuffer = (BYTE *) malloc (10000);
  1289. if (!pSourceBuffer)
  1290. {
  1291. if (! pSourceBuffer)
  1292. {
  1293. ErrorCode = GetLastError();
  1294. }
  1295. }
  1296. }
  1297. if (!ErrorCode)
  1298. {
  1299. // Open the internet session
  1300. while ((NumRetries < MaxRetries) && (!UploadSuccess))
  1301. {
  1302. // ::MessageBoxW(NULL,L"Opening the session",NULL,MB_OK);
  1303. hSession = InternetOpenW(L"CerClientControl",
  1304. INTERNET_OPEN_TYPE_PRECONFIG,
  1305. NULL,
  1306. NULL,
  1307. 0);
  1308. if (!hSession)
  1309. {
  1310. free (pSourceBuffer);
  1311. ErrorCode = GetLastError();
  1312. return ErrorCode;
  1313. }
  1314. // ::MessageBoxW(NULL,L"We have a session",NULL,MB_OK);
  1315. hConnect = InternetConnectW(hSession,
  1316. ServerName,
  1317. INTERNET_DEFAULT_HTTP_PORT,
  1318. NULL,
  1319. NULL,
  1320. INTERNET_SERVICE_HTTP,
  1321. 0,
  1322. NULL);
  1323. if (hConnect)
  1324. {
  1325. // ::MessageBoxW(NULL,L"We have a connection",NULL,MB_OK);
  1326. hRequest = HttpOpenRequestW (hConnect,
  1327. L"PUT",
  1328. DestFileName,
  1329. NULL,
  1330. NULL,
  1331. pszAccept,
  1332. INTERNET_FLAG_NEED_FILE|INTERNET_FLAG_NO_CACHE_WRITE,
  1333. 0);
  1334. if (hRequest)
  1335. {
  1336. hSourceFile = CreateFileW( SourceFileName,
  1337. GENERIC_READ,
  1338. FILE_SHARE_READ,
  1339. NULL,
  1340. OPEN_EXISTING,
  1341. FILE_ATTRIBUTE_NORMAL,
  1342. NULL);
  1343. //::MessageBoxW(NULL,L"Request has been opened",NULL,MB_OK);
  1344. if (hSourceFile != INVALID_HANDLE_VALUE)
  1345. {
  1346. // Clear the buffer
  1347. BufferIn.dwStructSize = sizeof( INTERNET_BUFFERSW );
  1348. BufferIn.Next = NULL;
  1349. BufferIn.lpcszHeader = NULL;
  1350. BufferIn.dwHeadersLength = 0;
  1351. BufferIn.dwHeadersTotal = 0;
  1352. BufferIn.lpvBuffer = NULL;
  1353. BufferIn.dwBufferLength = 0;
  1354. BufferIn.dwOffsetLow = 0;
  1355. BufferIn.dwOffsetHigh = 0;
  1356. BufferIn.dwBufferTotal = GetFileSize (hSourceFile, NULL);
  1357. ZeroMemory(pSourceBuffer, 10000); // Fill buffer with data
  1358. if(HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
  1359. {
  1360. // ::MessageBoxW(NULL,L"Sending Request",NULL,MB_OK);
  1361. do
  1362. {
  1363. dwBytesRead = 0;
  1364. if(! ReadFile(hSourceFile, pSourceBuffer, 10000, &dwBytesRead, NULL) )
  1365. {
  1366. ErrorCode = GetLastError();
  1367. }
  1368. else
  1369. {
  1370. bRet = InternetWriteFile(hRequest, pSourceBuffer, dwBytesRead, &dwBytesWritten);
  1371. if ( (!bRet) || (dwBytesWritten==0) )
  1372. {
  1373. ErrorCode = GetLastError();
  1374. }
  1375. }
  1376. } while ((dwBytesRead == 10000) && (!ErrorCode) );
  1377. if (!ErrorCode)
  1378. {
  1379. bRet = HttpEndRequest(hRequest, NULL, 0, 0);
  1380. if (!bRet)
  1381. {
  1382. ErrorCode = GetLastError();
  1383. }
  1384. else
  1385. {
  1386. // ::MessageBoxW(NULL,L"Ending Request",NULL,MB_OK);
  1387. ResponseCode = 0;
  1388. HttpQueryInfo(hRequest,
  1389. HTTP_QUERY_STATUS_CODE |HTTP_QUERY_FLAG_NUMBER ,
  1390. &ResponseCode,
  1391. &ResLength,
  1392. &index);
  1393. if ( (ResponseCode == 200) || (ResponseCode == 201))
  1394. {
  1395. ErrorCode = 0;
  1396. UploadSuccess = TRUE;
  1397. // ::MessageBoxW(NULL,L"Upload was successfull",NULL,MB_OK);
  1398. }
  1399. else
  1400. {
  1401. ErrorCode= ResponseCode;
  1402. ++NumRetries;
  1403. }
  1404. }
  1405. }
  1406. }
  1407. }
  1408. }
  1409. }
  1410. // ::MessageBoxW(NULL,L"Cleaning Up",NULL,MB_OK);
  1411. if (!UploadSuccess)
  1412. {
  1413. ++NumRetries;
  1414. }
  1415. if (hSourceFile != INVALID_HANDLE_VALUE)
  1416. CloseHandle (hSourceFile);
  1417. if (hRequest)
  1418. InternetCloseHandle(hRequest);
  1419. if (hConnect)
  1420. InternetCloseHandle(hConnect);
  1421. if (hSession)
  1422. InternetCloseHandle(hSession);
  1423. }
  1424. if (pSourceBuffer)
  1425. {
  1426. free (pSourceBuffer);
  1427. pSourceBuffer = NULL;
  1428. }
  1429. }
  1430. if ( !ErrorCode)
  1431. {
  1432. // Get the Computer Name
  1433. // if there are no errors rename the file just uploaded.
  1434. wchar_t NewFileName[MAX_PATH];
  1435. wchar_t FullPath[MAX_PATH];
  1436. wcscpy (NewFileName, *FileName);
  1437. int x = 0;
  1438. BOOL DONE = FALSE;
  1439. NewFileName[wcslen(NewFileName)] = L'\0';
  1440. // First try just .old
  1441. DWORD NameLength = 0;
  1442. NameLength = wcslen(FullPath)+ wcslen(L"\\.old");
  1443. if (NameLength > MAX_PATH) // nope it won't fit. Reduce the file name length by the difference.
  1444. {
  1445. NewFileName[wcslen(NewFileName) - (MAX_PATH - NameLength)] = L'\0';
  1446. }
  1447. wsprintfW(FullPath,L"%s\\%s.old",*Path,NewFileName);
  1448. if (!PathFileExistsW(FullPath))
  1449. {
  1450. MoveFileW(SourceFileName, FullPath);
  1451. DONE = TRUE;
  1452. }
  1453. else
  1454. { // if that fails then we have to try another method.
  1455. while (!DONE)
  1456. {
  1457. wcscpy (NewFileName, *FileName);
  1458. NewFileName[wcslen(NewFileName)] = L'\0';
  1459. wsprintfW(NewFileName, L"%s.old%d",*FileName,x);
  1460. if ( (wcslen(*Path) + wcslen(L"\\") + wcslen(NewFileName)) > MAX_PATH)
  1461. {
  1462. // Reduce file name by Diff of MAX_PATH and Total name Length
  1463. NameLength = wcslen(*Path) + wcslen(L"\\") + wcslen(NewFileName);
  1464. wcscpy(NewFileName,*FileName);
  1465. NewFileName[wcslen(NewFileName - NameLength)]=L'\0';
  1466. wsprintfW(FullPath, L"%s\\%s.old%d", *Path, NewFileName, x);
  1467. }
  1468. else
  1469. {
  1470. wsprintfW(FullPath,L"%s\\%s.old%d",*Path,NewFileName,x);
  1471. }
  1472. if (!PathFileExistsW(FullPath))
  1473. {
  1474. MoveFileW(SourceFileName, FullPath);
  1475. DONE = TRUE;
  1476. }
  1477. else
  1478. ++x;
  1479. }
  1480. }
  1481. // Update the Upload CSV File
  1482. if (!ErrorCode)
  1483. {
  1484. // ::MessageBoxW(NULL,L"Updateing the csv",NULL,MB_OK);
  1485. wcscpy(MachineName,L"\0");
  1486. GetMachineName(*Path, *FileName, MachineName);
  1487. ZeroMemory(CSVBuffer, 512);
  1488. if (!wcscmp(MachineName,L"\0"))
  1489. {
  1490. wsprintfW(CSVBuffer, L"%s,,%s\r\n",*FileName,PathFindFileNameW(FullPath));
  1491. }
  1492. else
  1493. {
  1494. wsprintfW(CSVBuffer, L"%s,%s,%s\r\n", *FileName,MachineName,PathFindFileNameW(FullPath));
  1495. }
  1496. hCsvFile = CreateFileW(CSVFileName,
  1497. GENERIC_WRITE | GENERIC_READ,
  1498. FILE_SHARE_READ,
  1499. NULL,
  1500. OPEN_EXISTING,
  1501. FILE_ATTRIBUTE_NORMAL,
  1502. NULL);
  1503. if (hCsvFile == INVALID_HANDLE_VALUE)
  1504. {
  1505. // Ok We Need to create a new one. Don't forget the Unicode Signature.
  1506. hCsvFile = CreateFileW(CSVFileName,
  1507. GENERIC_WRITE | GENERIC_READ,
  1508. FILE_SHARE_READ,
  1509. NULL,
  1510. CREATE_ALWAYS,
  1511. FILE_ATTRIBUTE_NORMAL,
  1512. NULL);
  1513. // Write the Unicode Signature
  1514. if (hCsvFile != INVALID_HANDLE_VALUE)
  1515. {
  1516. WriteFile(hCsvFile, &ByteOrderMark,2,&dwBytesWritten,NULL);
  1517. }
  1518. }
  1519. if (hCsvFile != INVALID_HANDLE_VALUE)
  1520. {
  1521. // continue as if the file was created before
  1522. wchar_t* TempBuffer = (wchar_t*) malloc (10000);
  1523. if (TempBuffer != NULL)
  1524. {
  1525. do
  1526. {
  1527. if (!ReadFile(hCsvFile,TempBuffer,10000,&dwBytesRead,NULL))
  1528. {
  1529. ; // we catch this below.
  1530. }
  1531. }
  1532. while(dwBytesRead == 10000);
  1533. free (TempBuffer);
  1534. WriteFile( hCsvFile, CSVBuffer,wcslen(CSVBuffer) *sizeof(wchar_t), &dwBytesWritten,NULL);
  1535. CloseHandle(hCsvFile);
  1536. }
  1537. }
  1538. else
  1539. ErrorCode = FAILED_TO_UPDATE_CSV;
  1540. }
  1541. }
  1542. // Return Upload Status
  1543. // ::MessageBoxW(NULL,L"Returning from the upload function",NULL,MB_OK);
  1544. RetCode->vt = VT_INT;
  1545. RetCode->intVal = ErrorCode;
  1546. return S_OK;
  1547. }
  1548. STDMETHODIMP CCerClient::GetSuccessCount(BSTR *Path, BSTR *TransID, VARIANT *RetVal)
  1549. {
  1550. wchar_t CsvFileName[MAX_PATH];
  1551. DWORD FileCount = 0;
  1552. FILE *hFile;
  1553. wchar_t *Buffer = NULL;
  1554. BOOL Done = FALSE;
  1555. HANDLE hCsv = INVALID_HANDLE_VALUE;
  1556. wchar_t TempFileName[MAX_PATH];
  1557. wchar_t *Source = NULL;
  1558. wchar_t *Dest = NULL;
  1559. DWORD FileSize = 0;
  1560. DWORD dwBytesRead = 0;
  1561. RetVal->vt = VT_INT;
  1562. // Move to the beginning of the file.
  1563. //::MessageBoxW(NULL,L"Getting the ComputerName from the CSV file",NULL,MB_OK);
  1564. wsprintfW(CsvFileName,L"%s\\%s.csv",*Path,*TransID);
  1565. hCsv = CreateFileW(CsvFileName,
  1566. GENERIC_READ,
  1567. NULL,
  1568. NULL,
  1569. OPEN_EXISTING,
  1570. FILE_ATTRIBUTE_NORMAL,
  1571. NULL);
  1572. if (hCsv != INVALID_HANDLE_VALUE)
  1573. {
  1574. FileSize = GetFileSize(hCsv,NULL);
  1575. if (FileSize > 0)
  1576. {
  1577. if ( (Buffer = (wchar_t *) malloc (FileSize * 2)) == NULL)
  1578. {
  1579. RetVal->intVal = -4;
  1580. CloseHandle(hCsv);
  1581. return S_OK;
  1582. }
  1583. }
  1584. else
  1585. {
  1586. RetVal->intVal = -4;
  1587. CloseHandle(hCsv);
  1588. return S_OK;
  1589. }
  1590. // Now look for the Filename
  1591. ZeroMemory(TempFileName,MAX_PATH * sizeof(wchar_t));
  1592. ZeroMemory(Buffer,FileSize * 2);
  1593. if (!ReadFile(hCsv,
  1594. Buffer,
  1595. FileSize * 2,
  1596. &dwBytesRead,
  1597. NULL))
  1598. {
  1599. RetVal->intVal = -4;
  1600. CloseHandle(hCsv);
  1601. if (Buffer)
  1602. free(Buffer);
  1603. return S_OK;
  1604. }
  1605. Source = Buffer;
  1606. int Testing = IS_TEXT_UNICODE_SIGNATURE;
  1607. // If the unicode header bytes appear remove skip past them
  1608. if (IsTextUnicode(Buffer,FileSize * 2,&Testing))
  1609. {
  1610. ++Source;
  1611. }
  1612. while (! Done)
  1613. {
  1614. Dest = TempFileName;
  1615. while ( (*Source != L'\r') &&
  1616. (*Source != L'\0') &&
  1617. (*Source != L',') )
  1618. {
  1619. *Dest = *Source;
  1620. ++Source;
  1621. ++Dest;
  1622. }
  1623. // Null Terminate the destination string.
  1624. *Dest = L'\0';
  1625. if (wcscmp(TempFileName, L"\0"))
  1626. {
  1627. // ::MessageBoxW(NULL,TempFileName,L"Read file name from CSV",MB_OK);
  1628. ++FileCount;
  1629. }
  1630. ZeroMemory(TempFileName,sizeof(TempFileName));
  1631. // Move to the next line
  1632. while ( (*Source != L'\r') && (*Source != L'\0'))
  1633. {
  1634. ++Source;
  1635. }
  1636. if (*Source == L'\r')
  1637. {
  1638. ++Source;
  1639. ++Source;
  1640. }
  1641. if (*Source == L'\0')
  1642. Done = TRUE;
  1643. }
  1644. if (FileCount > 0)
  1645. {
  1646. RetVal->intVal = FileCount;
  1647. }
  1648. else
  1649. {
  1650. RetVal->intVal = 0;
  1651. }
  1652. CloseHandle(hCsv);
  1653. }
  1654. else
  1655. {
  1656. RetVal->intVal = -4;
  1657. }
  1658. if (Buffer)
  1659. free (Buffer);
  1660. return S_OK;
  1661. }