Source code of Windows XP (NT5)
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.

471 lines
9.5 KiB

  1. // stdafx.h : include file for standard system include files,
  2. // or project specific include files that are used frequently,
  3. // but are changed infrequently
  4. #if !defined(AFX_STDAFX_H__97A939A5_466D_43B0_9150_CFBCC67F024B__INCLUDED_)
  5. #define AFX_STDAFX_H__97A939A5_466D_43B0_9150_CFBCC67F024B__INCLUDED_
  6. #ifndef __WIN9XBUILD__
  7. #include <nt.h>
  8. #include <ntrtl.h>
  9. #include <nturtl.h>
  10. #include <ntddkbd.h>
  11. #include <ntddmou.h>
  12. #include <windows.h>
  13. #include <winbase.h>
  14. #include <winerror.h>
  15. #include <ntsecapi.h>
  16. #include <lmcons.h>
  17. #include <lm.h>
  18. #include <wtsapi32.h>
  19. #include <winsta.h>
  20. #include "license.h"
  21. #include "tlsapi.h"
  22. #include "lscsp.h"
  23. #endif
  24. #include "safsessionresolver.h"
  25. #include "tsremdsk.h"
  26. #include "tsstl.h"
  27. #include "locks.h"
  28. #include "map.h"
  29. #include "myassert.h"
  30. #include "icshelpapi.h"
  31. #if _MSC_VER > 1000
  32. #pragma once
  33. #endif // _MSC_VER > 1000
  34. #ifndef STRICT
  35. #define STRICT
  36. #endif
  37. #ifndef _WIN32_WINNT
  38. #define _WIN32_WINNT 0x0400
  39. #endif
  40. #define _ATL_APARTMENT_THREADED
  41. #include <atlbase.h>
  42. //You may derive a class from CComModule and use it if you want to override
  43. //something, but do not change the name of _Module
  44. #ifndef __WIN9XBUILD__
  45. #ifndef DBG
  46. #define IDLE_SHUTDOWN_PERIOD 10 * 1000 * 60 // 10 min wait
  47. #define EXPIRE_HELPSESSION_PERIOD 60 * 60 * 1000 // Clean up help session every hrs
  48. #else
  49. #define IDLE_SHUTDOWN_PERIOD 1000 * 60 * 5 // 1 min. time out
  50. #define EXPIRE_HELPSESSION_PERIOD 5 * 60 * 1000 // Clean up help session every 5 min.
  51. #endif
  52. #define SESSMGR_NOCONNECTIONEVENT_NAME L"Global\\RemoteDesktopSessMgr"
  53. #define SERVICE_STARTUP_WAITHINT 1000*30 // 30 second wait
  54. #define REGKEY_SYSTEM_EVENTSOURCE L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\System"
  55. #define MAX_FETCHIPADDRESSRETRY 5
  56. #define DEFAULT_MAXTICKET_EXPIRY 30 * 24 * 60 * 60 // Default max. ticket valid period
  57. class CServiceModule : public CComModule
  58. {
  59. public:
  60. static DWORD WINAPI
  61. HandlerEx(
  62. DWORD dwControl,
  63. DWORD dwEventType,
  64. LPVOID lpEventData,
  65. LPVOID lpContext
  66. );
  67. HRESULT
  68. RegisterServer(
  69. FILE* pSetupLog,
  70. BOOL bRegTypeLib,
  71. BOOL bService
  72. );
  73. HRESULT
  74. UnregisterServer(FILE* pSetupLog);
  75. HRESULT
  76. SetupEventViewerSource(
  77. FILE* pSetupLog
  78. );
  79. void
  80. Init(
  81. _ATL_OBJMAP_ENTRY* p,
  82. HINSTANCE h,
  83. UINT nServiceNameID,
  84. UINT nServiceDispNameID,
  85. UINT nServiceDescID,
  86. const GUID* plibid = NULL
  87. );
  88. void Start();
  89. void
  90. ServiceMain(
  91. DWORD dwArgc,
  92. LPTSTR* lpszArgv
  93. );
  94. void Handler(DWORD dwOpcode);
  95. void Run();
  96. BOOL IsInstalled(FILE* pSetupLog);
  97. BOOL Install(FILE* pSetupLog);
  98. BOOL Uninstall(FILE* pSetupLog);
  99. LONG Unlock();
  100. BOOL UpdateService(FILE* pSetupLog);
  101. void
  102. LogInfoEvent(
  103. DWORD dwCode
  104. );
  105. void
  106. LogWarningEvent(
  107. DWORD dwCode
  108. );
  109. void
  110. LogErrorEvent(
  111. DWORD dwCode
  112. );
  113. void
  114. LogEventWithStatusCode(
  115. DWORD dwEventType,
  116. DWORD dwEventId,
  117. DWORD dwCode
  118. );
  119. void
  120. LogEventString(
  121. DWORD dwEventType,
  122. DWORD dwEventId,
  123. DWORD dwNumString,
  124. LPCTSTR* lpStrings,
  125. DWORD dwBinaryDataSize = 0,
  126. LPVOID lpRawData = NULL
  127. );
  128. void
  129. LogSessmgrEventLog(
  130. DWORD dwEventType,
  131. DWORD dwEventCode,
  132. CComBSTR& bstrNoviceDomain,
  133. CComBSTR& bstrNoviceAccount,
  134. CComBSTR& bstrRaType,
  135. CComBSTR& bstrExpertIPFromClient,
  136. CComBSTR& bstrExpertIPFromTS,
  137. DWORD dwErrCode
  138. );
  139. void
  140. SetServiceStatus(DWORD dwState);
  141. void
  142. SetupAsLocalServer();
  143. ULONG
  144. AddRef();
  145. ULONG
  146. Release();
  147. CServiceModule() : m_RefCount(0), CComModule() {}
  148. DWORD
  149. GetServiceStartupStatus()
  150. {
  151. return m_dwServiceStartupStatus;
  152. }
  153. BOOL
  154. IsSuccessServiceStartup()
  155. {
  156. return ERROR_SUCCESS == m_dwServiceStartupStatus;
  157. }
  158. BOOL
  159. InitializeSessmgr();
  160. //Implementation
  161. private:
  162. static HANDLE gm_hIdle;
  163. static HANDLE gm_hIdleMonitorThread;
  164. static HANDLE gm_hICSAlertEvent;
  165. static void WINAPI _ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv);
  166. static void WINAPI _Handler(DWORD dwOpcode);
  167. static unsigned int WINAPI IdleMonitorThread( void* ptr );
  168. //static unsigned int WINAPI StartupInitThread( void* ptr );
  169. // data members
  170. public:
  171. //
  172. // Refer to CreateService() for 256
  173. //
  174. TCHAR m_szServiceDesc[257];
  175. TCHAR m_szServiceName[257];
  176. TCHAR m_szServiceDispName[257];
  177. SERVICE_STATUS_HANDLE m_hServiceStatus;
  178. SERVICE_STATUS m_status;
  179. DWORD dwThreadID;
  180. BOOL m_bService;
  181. CCriticalSection m_Locks;
  182. long m_RefCount;
  183. BOOL m_Initialized;
  184. DWORD m_dwServiceStartupStatus;
  185. };
  186. extern CServiceModule _Module;
  187. #else
  188. class CExeModule : public CComModule
  189. {
  190. private:
  191. CSafeCounter m_RefCount;
  192. public:
  193. CExeModule() : m_RefCount(0) {}
  194. LONG Unlock();
  195. DWORD dwThreadID;
  196. HANDLE hEventShutdown;
  197. void MonitorShutdown();
  198. bool StartMonitor();
  199. bool bActivity;
  200. ULONG
  201. AddRef() { return ++m_RefCount; }
  202. ULONG
  203. Release() { return --m_RefCount; }
  204. };
  205. extern CExeModule _Module;
  206. #endif
  207. #include <atlcom.h>
  208. #include "sessmgrmc.h"
  209. #include "sessmgr.h"
  210. #include "helper.h"
  211. #include "helptab.h"
  212. #include "helpmgr.h"
  213. #include "helpsess.h"
  214. #include "tsremdsk.h"
  215. #include "rderror.h"
  216. class CComBSTRtoLPTSTR {
  217. private:
  218. LPTSTR m_pszString;
  219. LPTSTR
  220. ConvertString( LPWSTR pszString )
  221. {
  222. #ifdef UNICODE
  223. return pszString;
  224. #else
  225. char* pszAnsiString = NULL;
  226. int length = wcslen(pszString);
  227. int bufSize;
  228. if( length > 0 )
  229. {
  230. bufSize = WideCharToMultiByte(
  231. GetACP(),
  232. WC_DEFAULTCHAR,
  233. (LPWSTR)pszString,
  234. -1,
  235. NULL,
  236. 0,
  237. NULL,
  238. NULL
  239. );
  240. if(bufSize != 0)
  241. {
  242. pszAnsiString = (LPSTR)LocalAlloc(LPTR, (bufSize + 1) * sizeof(char));
  243. if( NULL != pszAnsiString )
  244. {
  245. bufSize = WideCharToMultiByte(
  246. GetACP(),
  247. WC_DEFAULTCHAR,
  248. pszString,
  249. length,
  250. pszAnsiString,
  251. bufSize,
  252. NULL,
  253. NULL
  254. );
  255. if( bufSize == 0 )
  256. {
  257. LocalFree( pszAnsiString );
  258. pszAnsiString = NULL;
  259. MYASSERT(FALSE);
  260. }
  261. }
  262. }
  263. }
  264. return pszAnsiString;
  265. #endif
  266. }
  267. public:
  268. CComBSTRtoLPTSTR( CComBSTR& bstr )
  269. {
  270. m_pszString = ConvertString( bstr );
  271. if( NULL == m_pszString )
  272. {
  273. throw GetLastError();
  274. }
  275. }
  276. ~CComBSTRtoLPTSTR()
  277. {
  278. #ifndef UNICODE
  279. if( NULL != m_pszString )
  280. {
  281. LocalFree( m_pszString );
  282. }
  283. #endif
  284. }
  285. operator LPCTSTR()
  286. {
  287. return m_pszString;
  288. }
  289. operator LPTSTR()
  290. {
  291. return m_pszString;
  292. }
  293. };
  294. #ifndef AllocMemory
  295. #define AllocMemory(size) \
  296. LocalAlloc(LPTR, size)
  297. #endif
  298. #ifndef FreeMemory
  299. #define FreeMemory(ptr) \
  300. if(ptr) \
  301. { \
  302. LocalFree(ptr); \
  303. ptr=NULL; \
  304. }
  305. #endif
  306. #ifndef ReallocMemory
  307. #define ReallocMemory(ptr, size) \
  308. LocalReAlloc(ptr, size, LMEM_ZEROINIT)
  309. #endif
  310. #define REGKEYCONTROL_REMDSK _TEXT("Software\\Microsoft\\Remote Desktop")
  311. #define CLASS_PRIVATE
  312. #define CLASS_PUBLIC
  313. #define CLASS_STATIC
  314. struct __declspec(uuid("A55737AB-5B26-4A21-99B7-6D0C606F515E")) SESSIONRESOLVER;
  315. #define SESSIONRESOLVERCLSID __uuidof(SESSIONRESOLVER)
  316. #define HELPSESSION_UNSOLICATED _TEXT("UnsolicitedHelp")
  317. #define HELPSESSION_NORMAL_RA _TEXT("SolicitedHelp")
  318. //
  319. // HYDRA_CERT_REG_KEY
  320. // HYDRA_X509_CERTIFICATE
  321. // X509_CERT_PUBLIC_KEY_NAME
  322. // defined in lscsp.h
  323. //
  324. #define REGKEY_TSX509_CERT _TEXT(HYDRA_CERT_REG_KEY)
  325. #define REGVALUE_TSX509_CERT _TEXT(HYDRA_X509_CERTIFICATE)
  326. #define LSA_TSX509_CERT_PUBLIC_KEY_NAME X509_CERT_PUBLIC_KEY_NAME
  327. //
  328. // Message for notify session logoff or disconnect,
  329. // WPARAM : Not use
  330. // LPARAM : the logoff or disconnecting session.
  331. //
  332. #define WM_SESSIONLOGOFFDISCONNECT (WM_APP + 1)
  333. //
  334. // Message for expiring help ticket
  335. // WPARAM : Not use
  336. // LPARAM : Not use
  337. //
  338. #define WM_EXPIREHELPSESSION (WM_SESSIONLOGOFFDISCONNECT + 1)
  339. //
  340. // Notification message to reload termsrv public key
  341. // WPARAM : Not use
  342. // LPARAM : Not use
  343. //
  344. #define WM_LOADTSPUBLICKEY (WM_EXPIREHELPSESSION + 1)
  345. //
  346. // Notification for rdsaddin terminates
  347. // WPARAM : Not use
  348. // LPARAM : Pointer to help session (ticket) object.
  349. //
  350. #define WM_HELPERRDSADDINEXIT (WM_LOADTSPUBLICKEY + 1)
  351. //{{AFX_INSERT_LOCATION}}
  352. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  353. #endif // !defined(AFX_STDAFX_H__97A939A5_466D_43B0_9150_CFBCC67F024B__INCLUDED)