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.

249 lines
8.0 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. Outlook.cpp
  5. Abstract:
  6. Replace the path in Outlook private data.
  7. Author:
  8. Geoffrey Guo (geoffguo) 1-Jul-2002 Created
  9. Revision History:
  10. <alias> <date> <comments>
  11. --*/
  12. #define NOT_USE_SAFE_STRING
  13. #include "clmt.h"
  14. #define STRSAFE_LIB
  15. #include <strsafe.h>
  16. #define OUTLOOK_VALUENAME TEXT("01020fff")
  17. //-----------------------------------------------------------------------//
  18. //
  19. // ReplaceOutlookPSTPath()
  20. //
  21. // DESCRIPTION:
  22. // Replace Outlook PST data file path
  23. //
  24. // lpDataIn: Input data buffer
  25. // dwcbInSize: Input data size
  26. // lpDataOut: Output data buffer
  27. // lpcbOutSize: Output data size
  28. // lpRegStr: Input parameter structure
  29. //-----------------------------------------------------------------------//
  30. LONG ReplaceOutlookPSTPath (
  31. LPBYTE lpDataIn,
  32. DWORD dwcbInSize,
  33. LPBYTE *lpDataOut,
  34. LPDWORD lpcbOutSize,
  35. PREG_STRING_REPLACE lpRegStr)
  36. {
  37. LONG lRet = ERROR_INVALID_PARAMETER;
  38. DWORD dwSize, dwAttrib, dwHeadSize;
  39. LPWSTR lpWideInputBuf = NULL;
  40. LPWSTR lpWideOutputBuf = NULL;
  41. int j;
  42. // Check if path contains ".pst"
  43. if (MyStrCmpIA((LPSTR)(lpDataIn + dwcbInSize - 5), ".pst") != 0)
  44. goto Cleanup;
  45. // Since EntryID's format is 0x.......00 00 (this is last part of GUID) 00 (followed by path)
  46. // Search from back for the beginning of path
  47. for (j = dwcbInSize - 1; j--; (j> 0))
  48. {
  49. if ( (lpDataIn[j] == (BYTE)0) && (lpDataIn[j-1] == (BYTE)0))
  50. {
  51. dwHeadSize = j + 1;
  52. dwSize = MultiByteToWideChar (CP_ACP, 0, (LPSTR)(&lpDataIn[j+1]), -1, NULL, 0);
  53. lpWideInputBuf = (LPWSTR)calloc (dwSize+1, sizeof(TCHAR));
  54. if (!lpWideInputBuf)
  55. {
  56. lRet = ERROR_NOT_ENOUGH_MEMORY;
  57. break;
  58. }
  59. MultiByteToWideChar (CP_ACP, 0, (LPSTR)(&lpDataIn[j+1]), -1, lpWideInputBuf, dwSize+1);
  60. lpRegStr->cchMaxStrLen = GetMaxStrLen (lpRegStr);
  61. lpWideOutputBuf = ReplaceSingleString (
  62. lpWideInputBuf,
  63. REG_SZ,
  64. lpRegStr,
  65. NULL,
  66. &dwAttrib,
  67. TRUE);
  68. if (lpWideOutputBuf)
  69. {
  70. dwSize = WideCharToMultiByte(CP_ACP, 0, lpWideOutputBuf, -1, 0, 0, NULL, NULL);
  71. *lpcbOutSize = dwHeadSize + dwSize;
  72. *lpDataOut = (LPBYTE)calloc (*lpcbOutSize, 1);
  73. if (*lpDataOut)
  74. {
  75. memcpy (*lpDataOut, lpDataIn, dwHeadSize);
  76. WideCharToMultiByte(CP_ACP, 0, lpWideOutputBuf, -1, (LPSTR)&((*lpDataOut)[dwHeadSize]), dwSize+1, NULL, NULL);
  77. lRet = ERROR_SUCCESS;
  78. }
  79. else
  80. lRet = ERROR_NOT_ENOUGH_MEMORY;
  81. free (lpWideOutputBuf);
  82. }
  83. free (lpWideInputBuf);
  84. break;
  85. }
  86. }
  87. Cleanup:
  88. return lRet;
  89. }
  90. HRESULT UpdatePSTpath(
  91. HKEY hRootKey,
  92. LPTSTR lpUserName,
  93. LPTSTR lpSubKeyName,
  94. LPTSTR lpValueName,
  95. PREG_STRING_REPLACE lpRegStr)
  96. {
  97. HRESULT hr = S_OK;
  98. HKEY hKey = NULL;
  99. HKEY hSubKey = NULL;
  100. DWORD i, j, dwType, dwSizePath, cbOutSize;
  101. DWORD dwCchsizeforRenameValueData;
  102. LPBYTE lpOutputBuf = NULL;
  103. LONG lRet = ERROR_SUCCESS;
  104. LPTSTR lpszSectionName = NULL;
  105. LPTSTR szRenameValueDataLine = NULL;
  106. TCHAR szKeyName[MAX_PATH];
  107. TCHAR szBuf[MAX_PATH+1];
  108. TCHAR szIndex[MAX_PATH];
  109. DPF(APPmsg, L"Enter UpdatePSTpath: ");
  110. lRet = RegOpenKeyEx(
  111. hRootKey,
  112. lpSubKeyName,
  113. 0,
  114. KEY_READ,
  115. &hKey);
  116. if (lRet != ERROR_SUCCESS)
  117. {
  118. if ( ( ERROR_FILE_NOT_FOUND == lRet)
  119. || (ERROR_PATH_NOT_FOUND == lRet) )
  120. {
  121. hr = S_FALSE;
  122. DPF (APPwar, L"UpdatePSTpath: RegOpenKeyEx Failed! error code: %d", lRet);
  123. }
  124. else
  125. {
  126. DPF (APPerr, L"UpdatePSTpath: RegOpenKeyEx Failed! Error: %d", lRet);
  127. hr = HRESULT_FROM_WIN32(lRet);
  128. }
  129. goto Exit;
  130. }
  131. i = 0;
  132. while (TRUE)
  133. {
  134. if (lRet = RegEnumKey(hKey, i++, szKeyName, MAX_PATH) != ERROR_SUCCESS)
  135. break;
  136. if (lRet = RegOpenKeyEx(hKey, szKeyName, 0, KEY_ALL_ACCESS, &hSubKey) == ERROR_SUCCESS)
  137. {
  138. dwSizePath = MAX_PATH;
  139. if (lRet = RegQueryValueEx(
  140. hSubKey,
  141. OUTLOOK_VALUENAME,
  142. 0,
  143. &dwType,
  144. (LPBYTE)szBuf,
  145. &dwSizePath) == ERROR_SUCCESS)
  146. {
  147. lRet = ReplaceOutlookPSTPath (
  148. (LPBYTE)szBuf,
  149. dwSizePath,
  150. &lpOutputBuf,
  151. &cbOutSize,
  152. lpRegStr);
  153. if (lRet == ERROR_SUCCESS)
  154. {
  155. hr = SetSectionName (lpUserName, &lpszSectionName);
  156. dwCchsizeforRenameValueData = lstrlen(lpSubKeyName) +
  157. lstrlen(OUTLOOK_VALUENAME) +
  158. cbOutSize * 4 + MAX_PATH;
  159. szRenameValueDataLine = (TCHAR*)calloc(dwCchsizeforRenameValueData, sizeof(TCHAR));
  160. if (!szRenameValueDataLine)
  161. {
  162. hr = E_OUTOFMEMORY;
  163. break;
  164. }
  165. hr = StringCchPrintf(szRenameValueDataLine,dwCchsizeforRenameValueData,
  166. TEXT("%d,%u,\"%s\\%s\",\"%s\", "), CONSTANT_REG_VALUE_DATA_RENAME,
  167. dwType,lpSubKeyName,szKeyName,OUTLOOK_VALUENAME);
  168. if (FAILED(hr))
  169. break;
  170. dwSizePath = lstrlen(szRenameValueDataLine);
  171. for (j = 0; j < cbOutSize-1; j++)
  172. hr = StringCchPrintf(&szRenameValueDataLine[dwSizePath+j*4],dwCchsizeforRenameValueData-dwSizePath-j*4,
  173. TEXT("%02x, "), lpOutputBuf[j]);
  174. hr = StringCchPrintf(&szRenameValueDataLine[dwSizePath+j*4],dwCchsizeforRenameValueData-dwSizePath-j*4,
  175. TEXT("%02x"), lpOutputBuf[j]);
  176. g_dwKeyIndex++;
  177. _itot(g_dwKeyIndex,szIndex,16);
  178. if (!WritePrivateProfileString(lpszSectionName,szIndex,szRenameValueDataLine,g_szToDoINFFileName))
  179. {
  180. hr = HRESULT_FROM_WIN32(GetLastError());
  181. break;
  182. }
  183. if (szRenameValueDataLine)
  184. {
  185. free (szRenameValueDataLine);
  186. szRenameValueDataLine = NULL;
  187. }
  188. if (lpszSectionName)
  189. {
  190. free (lpszSectionName);
  191. lpszSectionName = NULL;
  192. }
  193. }
  194. }
  195. RegCloseKey(hSubKey);
  196. }
  197. else
  198. {
  199. DPF (APPerr, L"UpdatePSTpath: RegQueryValue Failed! Error: %d", lRet);
  200. hr = HRESULT_FROM_WIN32(lRet);
  201. break;
  202. }
  203. }
  204. RegCloseKey(hKey);
  205. if ( (lRet == ERROR_NO_MORE_ITEMS) ||(lRet == ERROR_INVALID_FUNCTION) )
  206. {
  207. lRet = ERROR_SUCCESS;
  208. }
  209. if (szRenameValueDataLine)
  210. free (szRenameValueDataLine);
  211. if (lpszSectionName)
  212. free (lpszSectionName);
  213. hr = HRESULT_FROM_WIN32(lRet);
  214. Exit:
  215. DPF(APPmsg, L"Exit UpdatePSTpath: %d", lRet);
  216. return hr;
  217. }