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.

450 lines
11 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. precomp.h
  5. Abstract:
  6. Main header for BITS server extensions
  7. --*/
  8. #define INITGUID
  9. #include<nt.h>
  10. #include<ntrtl.h>
  11. #include<nturtl.h>
  12. #include <windows.h>
  13. #include <httpfilt.h>
  14. #include <httpext.h>
  15. #include <objbase.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <malloc.h>
  19. #include <iadmw.h>
  20. #include <iiscnfg.h>
  21. #include <shlwapi.h>
  22. #include <winsock2.h>
  23. #include <iphlpapi.h>
  24. #include <sddl.h>
  25. #include <aclapi.h>
  26. #include "winhttp.h"
  27. // #include "inethttp.h"
  28. #include <activeds.h>
  29. #include <bitsmsg.h>
  30. #include "resource.h"
  31. #include <smartptr.h>
  32. typedef StringHandleA StringHandle;
  33. #include <bitscfg.h>
  34. typedef SmartRefPointer<IMSAdminBase> SmartMetabasePointer;
  35. typedef SmartRefPointer<IBITSExtensionSetupFactory> SmartBITSExtensionSetupFactoryPointer;
  36. typedef SmartRefPointer<IBITSExtensionSetup> SmartBITSExtensionSetupPointer;
  37. #if defined(DBG)
  38. // check build
  39. #define BITS_MUST_SUCCEED( expr ) ASSERT( expr )
  40. #else
  41. // free build
  42. #define BITS_MUST_SUCCEED( expr ) ( expr )
  43. #endif
  44. const UINT32 LOG_INFO = 0x1;
  45. const UINT32 LOG_WARNING = 0x2;
  46. const UINT32 LOG_ERROR = 0x4;
  47. const UINT32 LOG_CALLBEGIN = 0x8;
  48. const UINT32 LOG_CALLEND = 0x10;
  49. #if defined(DBG)
  50. const UINT32 DEFAULT_LOG_FLAGS = LOG_INFO | LOG_WARNING | LOG_ERROR | LOG_CALLBEGIN | LOG_CALLEND;
  51. #else
  52. const UINT32 DEFAULT_LOG_FLAGS = 0;
  53. #endif
  54. const UINT32 DEFAULT_LOG_SIZE = 20;
  55. // LogSetings path under HKEY_LOCAL_MACHINE
  56. const char * const LOG_SETTINGS_PATH = "SOFTWARE\\Microsoft\\BITSServer";
  57. // Values
  58. // (REG_EXPAND_SZ). Contains the full path of the log file name
  59. const char * const LOG_FILENAME_VALUE = "LogFileName";
  60. // (REG_DWORD) Contains the log flags
  61. const char * const LOG_FLAGS_VALUE = "LogFlags";
  62. // (REG_DWORD) Contains the log size in MB
  63. const char * const LOG_SIZE_VALUE = "LogSize";
  64. // (REG_DWORD) Indicates if we will be appending the ProcessId to the log file name
  65. const char * const PER_PROCESS_LOG_VALUE = "PerProcessLog";
  66. extern UINT32 g_LogFlags;
  67. HRESULT LogInit();
  68. void LogClose();
  69. void LogInternal( UINT32 LogFlags, char *Format, va_list arglist );
  70. void inline Log( UINT32 LogFlags, char *Format, ... )
  71. {
  72. if ( !( g_LogFlags & LogFlags ) )
  73. return;
  74. va_list arglist;
  75. va_start( arglist, Format );
  76. LogInternal( LogFlags, Format, arglist );
  77. }
  78. const char *LookupHTTPStatusCodeText( DWORD HttpCode );
  79. class ServerException : public ComError
  80. {
  81. public:
  82. ServerException() :
  83. ComError( 0 ),
  84. m_HttpCode( 0 ),
  85. m_Context( 0 )
  86. {
  87. }
  88. ServerException( HRESULT Code, DWORD HttpCode = 0, DWORD Context = 0x5 ) :
  89. ComError( Code ),
  90. m_HttpCode( HttpCode ? HttpCode : MapStatus( Code ) ),
  91. m_Context( Context )
  92. {
  93. }
  94. ServerException( const ComError & Error ) :
  95. ComError( Error.m_Hr ),
  96. m_HttpCode( MapStatus( Error.m_Hr ) ),
  97. m_Context( 0x5 )
  98. {
  99. }
  100. ServerException( const ServerException & Error ) :
  101. ComError( Error.m_Hr ),
  102. m_HttpCode( Error.m_HttpCode ),
  103. m_Context( Error.m_Context )
  104. {
  105. }
  106. HRESULT GetCode() const
  107. {
  108. return m_Hr;
  109. }
  110. DWORD GetHttpCode() const
  111. {
  112. return m_HttpCode;
  113. }
  114. DWORD GetContext() const
  115. {
  116. return m_Context;
  117. }
  118. void SendErrorResponse( EXTENSION_CONTROL_BLOCK * ExtensionControlBlock ) const;
  119. DWORD MapStatus( HRESULT Hr ) const;
  120. private:
  121. DWORD m_HttpCode;
  122. DWORD m_Context;
  123. };
  124. inline UINT64 FILETIMEToUINT64( const FILETIME & FileTime )
  125. {
  126. ULARGE_INTEGER LargeInteger;
  127. LargeInteger.HighPart = FileTime.dwHighDateTime;
  128. LargeInteger.LowPart = FileTime.dwLowDateTime;
  129. return LargeInteger.QuadPart;
  130. }
  131. inline FILETIME UINT64ToFILETIME( UINT64 Int64Value )
  132. {
  133. ULARGE_INTEGER LargeInteger;
  134. LargeInteger.QuadPart = Int64Value;
  135. FILETIME FileTime;
  136. FileTime.dwHighDateTime = LargeInteger.HighPart;
  137. FileTime.dwLowDateTime = LargeInteger.LowPart;
  138. return FileTime;
  139. }
  140. // API thunks
  141. UINT64 BITSGetFileSize(
  142. HANDLE Handle );
  143. UINT64 BITSSetFilePointer(
  144. HANDLE Handle,
  145. INT64 Distance,
  146. DWORD MoveMethod );
  147. DWORD
  148. BITSWriteFile(
  149. HANDLE Handle,
  150. LPCVOID Buffer,
  151. DWORD NumberOfBytesToWrite);
  152. void
  153. BITSCreateDirectory(
  154. const CHAR *Path );
  155. void
  156. BITSRenameFile(
  157. LPCTSTR ExistingName,
  158. LPCTSTR NewName,
  159. bool AllowOverwrites );
  160. void
  161. BITSDeleteFile(
  162. LPCTSTR FileName );
  163. GUID
  164. BITSCreateGuid();
  165. GUID
  166. BITSGuidFromString( const char *String );
  167. StringHandle
  168. BITSStringFromGuid(
  169. GUID Guid );
  170. StringHandle
  171. BITSUnicodeToStringHandle( const WCHAR *pStr );
  172. StringHandle
  173. BITSUrlCombine(
  174. const char *Base,
  175. const char *Relative,
  176. DWORD dwFlags );
  177. StringHandle
  178. BITSUrlCanonicalize(
  179. const char *URL,
  180. DWORD dwFlags );
  181. void
  182. BITSSetCurrentThreadToken(
  183. HANDLE hToken );
  184. // Metadata wrappers
  185. StringHandle
  186. GetMetaDataString(
  187. IMSAdminBase *IISAdminBase,
  188. METADATA_HANDLE Handle,
  189. LPCWSTR Path,
  190. DWORD dwIdentifier,
  191. LPCSTR DefaultValue );
  192. DWORD
  193. GetMetaDataDWORD(
  194. IMSAdminBase *IISAdminBase,
  195. METADATA_HANDLE Handle,
  196. LPCWSTR Path,
  197. DWORD dwIdentifier,
  198. DWORD DefaultValue );
  199. class WorkStringBufferA
  200. {
  201. char *Data;
  202. public:
  203. WorkStringBufferA( SIZE_T Size )
  204. {
  205. Data = new char[Size];
  206. }
  207. WorkStringBufferA( const char* String )
  208. {
  209. size_t BufferSize = strlen(String) + 1;
  210. Data = new char[ BufferSize ];
  211. memcpy( Data, String, BufferSize );
  212. }
  213. ~WorkStringBufferA()
  214. {
  215. delete[] Data;
  216. }
  217. char *GetBuffer()
  218. {
  219. return Data;
  220. }
  221. };
  222. class WorkStringBufferW
  223. {
  224. WCHAR *Data;
  225. public:
  226. WorkStringBufferW( SIZE_T Size )
  227. {
  228. Data = new WCHAR[Size];
  229. }
  230. WorkStringBufferW( const WCHAR* String )
  231. {
  232. size_t BufferSize = wcslen(String) + 1;
  233. Data = new WCHAR[ BufferSize ];
  234. memcpy( Data, String, BufferSize * sizeof( WCHAR ) );
  235. }
  236. ~WorkStringBufferW()
  237. {
  238. delete[] Data;
  239. }
  240. WCHAR *GetBuffer()
  241. {
  242. return Data;
  243. }
  244. };
  245. typedef WorkStringBufferA WorkStringBuffer;
  246. const char * const BITS_CONNECTIONS_NAME_WITH_SLASH="BITS-Connections\\";
  247. const char * const BITS_CONNECTIONS_NAME="BITS-Connections";
  248. const UINT64 NanoSec100PerSec = 10000000; //no of 100 nanosecs per sec
  249. const DWORD WorkerRunInterval = 1000 * 60 /*secs*/ * 60 /*mins*/ * 12; /* hours */ /* twice a day */
  250. const UINT64 CleanupThreshold = NanoSec100PerSec * 60 /*secs*/ * 60 /*mins*/ * 24 /* hours */ * 3; // 3 days
  251. //
  252. // Configuration manager
  253. //
  254. #include "bitssrvcfg.h"
  255. class ConfigurationManager;
  256. class VDirConfig
  257. {
  258. friend ConfigurationManager;
  259. LONG m_Refs;
  260. FILETIME m_LastLookup;
  261. static StringHandle GetFullPath( StringHandle Path );
  262. public:
  263. StringHandle m_Path;
  264. StringHandle m_PhysicalPath;
  265. StringHandle m_SessionDir;
  266. StringHandle m_ConnectionsDir;
  267. StringHandle m_RequestsDir;
  268. StringHandle m_RepliesDir;
  269. DWORD m_NoProgressTimeout;
  270. UINT64 m_MaxFileSize;
  271. BITS_SERVER_NOTIFICATION_TYPE m_NotificationType;
  272. StringHandle m_NotificationURL;
  273. bool m_UploadEnabled;
  274. StringHandle m_HostId;
  275. DWORD m_HostIdFallbackTimeout;
  276. DWORD m_ExecutePermissions;
  277. bool m_AllowOverwrites;
  278. VDirConfig(
  279. StringHandle Path,
  280. SmartMetabasePointer AdminBase );
  281. void AddRef()
  282. {
  283. InterlockedIncrement( &m_Refs );
  284. }
  285. void Release()
  286. {
  287. if (!InterlockedDecrement( &m_Refs ))
  288. delete this;
  289. }
  290. };
  291. typedef SmartRefPointer<VDirConfig> SmartVDirConfig;
  292. class MapCacheEntry
  293. {
  294. friend ConfigurationManager;
  295. FILETIME m_LastLookup;
  296. public:
  297. StringHandle m_InstanceMetabasePath;
  298. StringHandle m_URL;
  299. SmartVDirConfig m_Config;
  300. DWORD m_URLDepth;
  301. MapCacheEntry(
  302. StringHandle InstanceMetabasePath,
  303. StringHandle URL,
  304. SmartVDirConfig Config,
  305. DWORD URLDepth ) :
  306. m_InstanceMetabasePath( InstanceMetabasePath ),
  307. m_URL( URL ),
  308. m_Config( Config ),
  309. m_URLDepth( URLDepth )
  310. {
  311. GetSystemTimeAsFileTime( &m_LastLookup );
  312. }
  313. };
  314. class ConfigurationManager
  315. {
  316. public:
  317. ConfigurationManager();
  318. ~ConfigurationManager();
  319. SmartVDirConfig GetConfig2( StringHandle InstanceMetabasePath, StringHandle URL,
  320. DWORD *pURLDepth );
  321. SmartVDirConfig GetConfig( StringHandle InstanceMetabasePath, StringHandle URL,
  322. DWORD *pURLDepth );
  323. static const PATH_CACHE_ENTRIES = 10;
  324. static const MAP_CACHE_ENTRIES = 10;
  325. private:
  326. SmartMetabasePointer m_IISAdminBase;
  327. CRITICAL_SECTION m_CacheCS;
  328. DWORD m_ChangeNumber;
  329. SmartVDirConfig m_PathCacheEntries[ PATH_CACHE_ENTRIES ];
  330. MapCacheEntry *m_MapCacheEntries[ MAP_CACHE_ENTRIES ];
  331. void FlushCache();
  332. bool HandleCacheConsistency();
  333. // L2 cache
  334. SmartVDirConfig Lookup( StringHandle Path );
  335. void Insert( SmartVDirConfig NewConfig );
  336. SmartVDirConfig GetVDirConfig( StringHandle Path );
  337. // L1 cache
  338. SmartVDirConfig Lookup( StringHandle InstanceMetabasePath,
  339. StringHandle URL,
  340. DWORD *pURLDepth );
  341. SmartVDirConfig Insert( StringHandle InstanceMetabasePath,
  342. StringHandle URL,
  343. StringHandle Path,
  344. DWORD URLDepth );
  345. StringHandle GetVDirPath( StringHandle InstanceMetabasePath,
  346. StringHandle URL,
  347. DWORD *pURLDepth );
  348. };
  349. extern ConfigurationManager *g_ConfigMan;
  350. extern HMODULE g_hinst;
  351. extern PropertyIDManager *g_PropertyMan;