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.

374 lines
9.8 KiB

  1. /*-------------------------------------------------------*/
  2. //Copyright (c) 1997 Microsoft Corporation
  3. //
  4. // Util.cpp
  5. //
  6. //Author:
  7. //
  8. //
  9. //Environment:
  10. //
  11. // User Mode - Win32
  12. //
  13. //Revision History:
  14. /*-------------------------------------------------------*/
  15. #include "urltrk.h"
  16. #include <inetreg.h>
  17. const CHAR c_szLogFormat[] = "hh':'mm':'ss";
  18. const CHAR c_szMode[] = "U"; // unknown
  19. const CHAR c_szLogContainerA[] = "Log";
  20. #define MY_CACHE_ENTRY_INFO_SIZE 512
  21. #define MY_MAX_STRING_LEN 512
  22. BOOL ConvertToPrefixedURL(LPCSTR lpszUrl, LPSTR *lplpPrefixedUrl);
  23. LPINTERNET_CACHE_ENTRY_INFOA QueryCacheEntry(LPCSTR lpUrl);
  24. HANDLE GetLogFile(LPCSTR lpUrl, LPINTERNET_CACHE_ENTRY_INFOA pce, LPSTR lpFile);
  25. LPSTR GetLogString(LPMY_LOGGING_INFO lpLogInfo);
  26. ULONG _IsLoggingEnabled(LPCSTR pszUrl)
  27. {
  28. LPINTERNET_CACHE_ENTRY_INFOA pce;
  29. LPSTR lpPfxUrl = NULL;
  30. ULONG dwTrack;
  31. //
  32. ConvertToPrefixedURL(pszUrl, &lpPfxUrl);
  33. if (!lpPfxUrl)
  34. {
  35. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  36. return 0;
  37. }
  38. pce = QueryCacheEntry(lpPfxUrl);
  39. if (!pce)
  40. {
  41. GlobalFree(lpPfxUrl);
  42. SetLastError(ERROR_FILE_NOT_FOUND);
  43. return 0;
  44. }
  45. dwTrack = pce->CacheEntryType;
  46. GlobalFree(pce);
  47. GlobalFree(lpPfxUrl);
  48. return dwTrack;
  49. }
  50. BOOL _WriteHitLogging(LPMY_LOGGING_INFO pmLi)
  51. {
  52. LPSTR lpLogString = NULL;
  53. LPSTR lpPfxUrl = NULL;
  54. LPINTERNET_CACHE_ENTRY_INFOA pce = NULL;
  55. HANDLE hFile;
  56. CHAR lpFile[MAX_PATH];
  57. DWORD dwWritten = 0;
  58. BOOL fuseCache;
  59. BOOL bRet = FALSE;
  60. //
  61. ConvertToPrefixedURL(pmLi->pLogInfo->lpszLoggedUrlName, &lpPfxUrl);
  62. if (!lpPfxUrl)
  63. {
  64. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  65. return bRet;
  66. }
  67. pce = QueryCacheEntry(lpPfxUrl);
  68. if (!pce)
  69. {
  70. GlobalFree(lpPfxUrl);
  71. SetLastError(ERROR_FILE_NOT_FOUND);
  72. return bRet;
  73. }
  74. hFile = GetLogFile(lpPfxUrl, pce, &lpFile[0]);
  75. if (hFile == NULL)
  76. {
  77. GlobalFree(lpPfxUrl);
  78. GlobalFree(pce);
  79. SetLastError(ERROR_FILE_NOT_FOUND);
  80. return bRet;
  81. }
  82. pmLi->fuseCache = GetUrlCacheEntryInfoExA(pmLi->pLogInfo->lpszLoggedUrlName, NULL, NULL, NULL, NULL, NULL, 0);
  83. lpLogString = GetLogString(pmLi);
  84. if (!lpLogString)
  85. {
  86. GlobalFree(lpPfxUrl);
  87. GlobalFree(pce);
  88. CloseHandle(hFile);
  89. return bRet;
  90. }
  91. bRet = WriteFile(hFile, lpLogString, lstrlenA(lpLogString), &dwWritten, NULL);
  92. CloseHandle(hFile);
  93. GlobalFree(lpLogString);
  94. // commit change to cache
  95. if(bRet)
  96. {
  97. bRet = CommitUrlCacheEntry(lpPfxUrl,
  98. lpFile, //
  99. pce->ExpireTime, //ExpireTime
  100. pce->LastModifiedTime, //LastModifiedTime
  101. pce->CacheEntryType,
  102. NULL, //lpHeaderInfo
  103. 0, //dwHeaderSize
  104. NULL, //lpszFileExtension
  105. 0); //reserved
  106. }
  107. // free pce
  108. GlobalFree(pce);
  109. GlobalFree(lpPfxUrl);
  110. return bRet;
  111. }
  112. BOOL IsGlobalOffline(void)
  113. {
  114. DWORD dwState = 0, dwSize = sizeof(DWORD);
  115. BOOL fRet = FALSE;
  116. #ifdef _SBS_
  117. HMODULE hModuleHandle = GetModuleHandleA("sbswinet.dll");
  118. #else
  119. HMODULE hModuleHandle = GetModuleHandleA("wininet.dll");
  120. #endif // _SBS_
  121. if(!hModuleHandle)
  122. return FALSE;
  123. if(InternetQueryOptionA(NULL, INTERNET_OPTION_CONNECTED_STATE, &dwState,
  124. &dwSize))
  125. {
  126. if(dwState & INTERNET_STATE_DISCONNECTED_BY_USER)
  127. fRet = TRUE;
  128. }
  129. return fRet;
  130. }
  131. //
  132. // Helper Functions
  133. //
  134. LPSTR GetLogString(LPMY_LOGGING_INFO pmLi)
  135. {
  136. FILETIME ftIn, ftOut;
  137. ULARGE_INTEGER ulIn, ulOut, ulTotal;
  138. SYSTEMTIME stIn, stOut;
  139. LPSTR lpData = NULL;
  140. CHAR pTimeIn[10], pTimeOut[10];
  141. lpData = (LPSTR)GlobalAlloc(LPTR, lstrlenA(pmLi->pLogInfo->lpszExtendedInfo)+MY_MAX_STRING_LEN);
  142. if (!lpData)
  143. {
  144. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  145. return NULL;
  146. }
  147. // calculate delta of time
  148. SystemTimeToFileTime(&(pmLi->pLogInfo->StartTime), &ftIn);
  149. SystemTimeToFileTime(&(pmLi->pLogInfo->EndTime), &ftOut);
  150. ulIn.LowPart = ftIn.dwLowDateTime;
  151. ulIn.HighPart = ftIn.dwHighDateTime;
  152. ulOut.LowPart = ftOut.dwLowDateTime;
  153. ulOut.HighPart = ftOut.dwHighDateTime;
  154. #ifndef unix
  155. ulTotal.QuadPart = ulOut.QuadPart - ulIn.QuadPart;
  156. #else
  157. U_QUAD_PART(ulTotal) = U_QUAD_PART(ulOut) - U_QUAD_PART(ulIn);
  158. #endif /* unix */
  159. ftOut.dwLowDateTime = ulTotal.LowPart;
  160. ftOut.dwHighDateTime = ulTotal.HighPart;
  161. FileTimeToSystemTime(&ftOut, &stOut);
  162. stIn = pmLi->pLogInfo->StartTime;
  163. // log string: timeEnter+Duration
  164. GetTimeFormat(LOCALE_SYSTEM_DEFAULT, TIME_FORCE24HOURFORMAT, &stIn, c_szLogFormat, pTimeIn, 10);
  165. GetTimeFormat(LOCALE_SYSTEM_DEFAULT, TIME_FORCE24HOURFORMAT, &stOut, c_szLogFormat, pTimeOut, 10);
  166. if (!pmLi->pLogInfo->lpszExtendedInfo)
  167. {
  168. wsprintf(lpData, "%s %d %.2d-%.2d-%d %s %s\r\n", c_szMode,
  169. pmLi->fuseCache,
  170. stIn.wMonth, stIn.wDay, stIn.wYear,
  171. pTimeIn, pTimeOut);
  172. }
  173. else
  174. {
  175. wsprintf(lpData, "%s %d %.2d-%.2d-%d %s %s %s\r\n", c_szMode,
  176. pmLi->fuseCache,
  177. stIn.wMonth, stIn.wDay, stIn.wYear,
  178. pTimeIn, pTimeOut,
  179. pmLi->pLogInfo->lpszExtendedInfo);
  180. }
  181. return lpData;
  182. }
  183. LPSTR ReadTrackingPrefix(void)
  184. {
  185. LPSTR lpPfx = NULL;
  186. DWORD cbPfx = 0;
  187. struct {
  188. INTERNET_CACHE_CONTAINER_INFOA cInfo;
  189. CHAR szBuffer[MAX_PATH+INTERNET_MAX_URL_LENGTH+1];
  190. } ContainerInfo;
  191. DWORD dwModified, dwContainer;
  192. HANDLE hEnum;
  193. dwContainer = sizeof(ContainerInfo);
  194. hEnum = FindFirstUrlCacheContainerA(&dwModified,
  195. &ContainerInfo.cInfo,
  196. &dwContainer,
  197. 0);
  198. if (hEnum)
  199. {
  200. for (;;)
  201. {
  202. if (!lstrcmpiA(ContainerInfo.cInfo.lpszName, c_szLogContainerA))
  203. {
  204. if (ContainerInfo.cInfo.lpszCachePrefix[0])
  205. {
  206. DWORD cb = lstrlenA(ContainerInfo.cInfo.lpszCachePrefix)+sizeof(CHAR);
  207. lpPfx = (LPSTR)GlobalAlloc(LPTR, cb);
  208. if (!lpPfx)
  209. {
  210. SetLastError(ERROR_OUTOFMEMORY);
  211. break;
  212. }
  213. lstrcpynA(lpPfx, ContainerInfo.cInfo.lpszCachePrefix, cb);
  214. }
  215. break;
  216. }
  217. dwContainer = sizeof(ContainerInfo);
  218. if (!FindNextUrlCacheContainerA(hEnum, &ContainerInfo.cInfo, &dwContainer))
  219. {
  220. if (GetLastError() == ERROR_NO_MORE_ITEMS)
  221. break;
  222. }
  223. }
  224. FindCloseUrlCache(hEnum);
  225. }
  226. return lpPfx;
  227. }
  228. // caller must free lplpPrefixedUrl
  229. BOOL
  230. ConvertToPrefixedURL(LPCSTR lpszUrl, LPSTR *lplpPrefixedUrl)
  231. {
  232. BOOL bret = FALSE;
  233. LPTSTR lpPfx = NULL;
  234. if (!lpszUrl)
  235. return bret;
  236. lpPfx = ReadTrackingPrefix();
  237. if (lpPfx)
  238. {
  239. *lplpPrefixedUrl = (LPSTR)GlobalAlloc(LPTR, lstrlenA(lpszUrl)+lstrlenA(lpPfx)+1);
  240. if (*lplpPrefixedUrl)
  241. {
  242. wsprintf(*lplpPrefixedUrl, "%s%s", lpPfx, lpszUrl);
  243. bret = TRUE;
  244. }
  245. GlobalFree(lpPfx);
  246. }
  247. return bret;
  248. }
  249. LPINTERNET_CACHE_ENTRY_INFOA
  250. QueryCacheEntry(LPCSTR lpUrl)
  251. {
  252. // get cache entry info
  253. LPINTERNET_CACHE_ENTRY_INFOA lpCE = NULL;
  254. DWORD dwEntrySize;
  255. BOOL bret = FALSE;
  256. lpCE = (LPINTERNET_CACHE_ENTRY_INFOA)GlobalAlloc(LPTR, MY_CACHE_ENTRY_INFO_SIZE);
  257. if (lpCE)
  258. {
  259. dwEntrySize = MY_CACHE_ENTRY_INFO_SIZE;
  260. while (!(bret = GetUrlCacheEntryInfoA(lpUrl, lpCE, &dwEntrySize)))
  261. {
  262. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  263. {
  264. GlobalFree(lpCE);
  265. lpCE = (LPINTERNET_CACHE_ENTRY_INFOA)GlobalAlloc(LPTR, dwEntrySize);
  266. if (!lpCE)
  267. break;
  268. }
  269. else
  270. break;
  271. }
  272. }
  273. if (!bret && lpCE)
  274. {
  275. GlobalFree(lpCE);
  276. lpCE = NULL;
  277. SetLastError(ERROR_FILE_NOT_FOUND);
  278. }
  279. return lpCE;
  280. }
  281. HANDLE GetLogFile(LPCSTR lpUrl, LPINTERNET_CACHE_ENTRY_INFOA pce, LPSTR lpFile)
  282. {
  283. HANDLE hFile = NULL;
  284. // work around -- begin
  285. if (!CreateUrlCacheEntry(lpUrl, 512, "log", lpFile, 0))
  286. return NULL;
  287. if (pce->lpszLocalFileName)
  288. {
  289. if (!CopyFile(pce->lpszLocalFileName, lpFile, FALSE))
  290. return NULL;
  291. DeleteFile(pce->lpszLocalFileName);
  292. }
  293. // work around -- end
  294. hFile = CreateFile(lpFile,
  295. GENERIC_WRITE,
  296. FILE_SHARE_READ,
  297. NULL,
  298. OPEN_ALWAYS,
  299. FILE_ATTRIBUTE_NORMAL, // | FILE_FLAG_SEQUENTIAL_SCAN,
  300. NULL);
  301. if (hFile == INVALID_HANDLE_VALUE)
  302. return NULL;
  303. // move file pointer to end
  304. if (0xFFFFFFFF == SetFilePointer(hFile, 0, 0, FILE_END))
  305. {
  306. CloseHandle(hFile);
  307. hFile = NULL;
  308. }
  309. return hFile;
  310. }