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.

403 lines
10 KiB

  1. /*++
  2. Copyright (c) 1994-1998, Microsoft Corporation All rights reserved.
  3. Module Name:
  4. worldmap.c
  5. Abstract:
  6. This module implements the world map for the Date/Time applet.
  7. Revision History:
  8. --*/
  9. // Include Files.
  10. #include <nt.h>
  11. #include <ntrtl.h>
  12. #include <nturtl.h>
  13. #include "timedate.h"
  14. #include "rc.h"
  15. #include <cpl.h>
  16. //
  17. // Global Variables.
  18. //
  19. HINSTANCE g_hInst = NULL;
  20. CRITICAL_SECTION g_csDll; /* The shared critical section */
  21. //
  22. //for TS time zone redirection
  23. //
  24. BOOL g_bShowOnlyTimeZone = FALSE;
  25. //
  26. //from kernel32p.lib
  27. //
  28. extern BOOL IsTimeZoneRedirectionEnabled();
  29. //
  30. // Function Prototypes.
  31. //
  32. extern BOOL OpenDateTimePropertySheet(HWND hwnd, LPCTSTR cmdline);
  33. BOOL
  34. EnableTimePrivilege(
  35. PTOKEN_PRIVILEGES *pPreviousState,
  36. ULONG *pPreviousStateLength);
  37. BOOL
  38. ResetTimePrivilege(
  39. PTOKEN_PRIVILEGES PreviousState,
  40. ULONG PreviousStateLength);
  41. int DoMessageBox(
  42. HWND hWnd,
  43. DWORD wText,
  44. DWORD wCaption,
  45. DWORD wType);
  46. ////////////////////////////////////////////////////////////////////////////
  47. //
  48. // LibMain
  49. //
  50. ////////////////////////////////////////////////////////////////////////////
  51. BOOL WINAPI LibMain(
  52. HANDLE hDll,
  53. DWORD dwReason,
  54. LPVOID lpReserved)
  55. {
  56. switch (dwReason)
  57. {
  58. case ( DLL_PROCESS_ATTACH ) :
  59. {
  60. g_hInst = hDll;
  61. DisableThreadLibraryCalls(hDll);
  62. InitializeCriticalSection(&g_csDll);
  63. break;
  64. }
  65. case ( DLL_PROCESS_DETACH ) :
  66. {
  67. DeleteCriticalSection(&g_csDll);
  68. break;
  69. }
  70. }
  71. return (TRUE);
  72. }
  73. ////////////////////////////////////////////////////////////////////////////
  74. //
  75. // CplApplet
  76. //
  77. // The main applet information manager.
  78. //
  79. ////////////////////////////////////////////////////////////////////////////
  80. LONG WINAPI CPlApplet(
  81. HWND hwnd,
  82. UINT uMsg,
  83. LPARAM lParam1,
  84. LPARAM lParam2)
  85. {
  86. static BOOL fReEntered = FALSE;
  87. switch (uMsg)
  88. {
  89. case ( CPL_INIT ) :
  90. {
  91. return (TRUE);
  92. }
  93. case ( CPL_GETCOUNT ) :
  94. {
  95. //
  96. // How many applets are in this DLL?
  97. //
  98. return (1);
  99. }
  100. case ( CPL_INQUIRE ) :
  101. {
  102. //
  103. // Fill the CPLINFO with the pertinent information.
  104. //
  105. #define lpOldCPlInfo ((LPCPLINFO)lParam2)
  106. switch (lParam1)
  107. {
  108. case ( 0 ) :
  109. {
  110. lpOldCPlInfo->idIcon = IDI_TIMEDATE;
  111. lpOldCPlInfo->idName = IDS_TIMEDATE;
  112. lpOldCPlInfo->idInfo = IDS_TIMEDATEINFO;
  113. break;
  114. }
  115. }
  116. lpOldCPlInfo->lData = 0L;
  117. return (TRUE);
  118. }
  119. case ( CPL_NEWINQUIRE ) :
  120. {
  121. #define lpCPlInfo ((LPNEWCPLINFO)lParam2)
  122. switch (lParam1)
  123. {
  124. case ( 0 ) :
  125. {
  126. lpCPlInfo->hIcon = LoadIcon( g_hInst,
  127. MAKEINTRESOURCE(IDI_TIMEDATE) );
  128. LoadString( g_hInst,
  129. IDS_TIMEDATE,
  130. lpCPlInfo->szName,
  131. ARRAYSIZE(lpCPlInfo->szName) );
  132. LoadString( g_hInst,
  133. IDS_TIMEDATEINFO,
  134. lpCPlInfo->szInfo,
  135. ARRAYSIZE(lpCPlInfo->szInfo) );
  136. lpCPlInfo->dwHelpContext = 0;
  137. break;
  138. }
  139. }
  140. lpCPlInfo->dwSize = sizeof(NEWCPLINFO);
  141. lpCPlInfo->lData = 0L;
  142. lpCPlInfo->szHelpFile[0] = 0;
  143. return (TRUE);
  144. }
  145. case ( CPL_DBLCLK ) :
  146. {
  147. lParam2 = (LPARAM)0;
  148. // fall thru...
  149. }
  150. case ( CPL_STARTWPARMS ) :
  151. {
  152. //
  153. // Do the applet thing.
  154. //
  155. switch (lParam1)
  156. {
  157. case ( 0 ) :
  158. {
  159. PTOKEN_PRIVILEGES PreviousState;
  160. ULONG PreviousStateLength;
  161. if (EnableTimePrivilege(&PreviousState, &PreviousStateLength))
  162. {
  163. OpenDateTimePropertySheet(hwnd, (LPCTSTR)lParam2);
  164. ResetTimePrivilege(PreviousState, PreviousStateLength);
  165. }
  166. else
  167. {
  168. //
  169. //If TZ redirection is enabled, regular users can change time zone
  170. //it will only be changed for current TS session.
  171. //
  172. if(IsTimeZoneRedirectionEnabled())
  173. {
  174. g_bShowOnlyTimeZone = TRUE;
  175. OpenDateTimePropertySheet(hwnd, (LPCTSTR)lParam2);
  176. g_bShowOnlyTimeZone = FALSE;
  177. }
  178. else
  179. {
  180. DoMessageBox( hwnd,
  181. IDS_NOTIMEERROR,
  182. IDS_CAPTION,
  183. MB_OK | MB_ICONINFORMATION );
  184. }
  185. }
  186. break;
  187. }
  188. }
  189. break;
  190. }
  191. case ( CPL_EXIT ) :
  192. {
  193. fReEntered = FALSE;
  194. //
  195. // Free up any allocations of resources made.
  196. //
  197. break;
  198. }
  199. default :
  200. {
  201. return (0L);
  202. }
  203. }
  204. return (1L);
  205. }
  206. ////////////////////////////////////////////////////////////////////////////
  207. //
  208. // EnableTimePrivilege
  209. //
  210. ////////////////////////////////////////////////////////////////////////////
  211. BOOL EnableTimePrivilege(
  212. PTOKEN_PRIVILEGES *pPreviousState,
  213. ULONG *pPreviousStateLength)
  214. {
  215. NTSTATUS NtStatus;
  216. HANDLE Token;
  217. LUID SystemTimePrivilege;
  218. PTOKEN_PRIVILEGES NewState;
  219. //
  220. // Open our own token.
  221. //
  222. NtStatus = NtOpenProcessToken( NtCurrentProcess(),
  223. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  224. &Token );
  225. if (!NT_SUCCESS(NtStatus))
  226. {
  227. return (FALSE);
  228. }
  229. //
  230. // Initialize the adjustment structure.
  231. //
  232. SystemTimePrivilege = RtlConvertLongToLuid(SE_SYSTEMTIME_PRIVILEGE);
  233. NewState = (PTOKEN_PRIVILEGES)LocalAlloc(LPTR, 100);
  234. if (NewState == NULL)
  235. {
  236. return (FALSE);
  237. }
  238. NewState->PrivilegeCount = 1;
  239. NewState->Privileges[0].Luid = SystemTimePrivilege;
  240. NewState->Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  241. *pPreviousState = (PTOKEN_PRIVILEGES)LocalAlloc(LPTR, 100);
  242. if (*pPreviousState == NULL)
  243. {
  244. LocalFree(NewState);
  245. return (FALSE);
  246. }
  247. // Set the state of the privilege to ENABLED.
  248. NtStatus = NtAdjustPrivilegesToken( Token, // TokenHandle
  249. FALSE, // DisableAllPrivileges
  250. NewState, // NewState
  251. 100, // BufferLength
  252. *pPreviousState, // PreviousState (OPTIONAL)
  253. pPreviousStateLength ); // ReturnLength
  254. //
  255. // Clean up some stuff before returning.
  256. //
  257. LocalFree(NewState);
  258. if (NtStatus == STATUS_SUCCESS)
  259. {
  260. NtClose(Token);
  261. return (TRUE);
  262. }
  263. else
  264. {
  265. LocalFree(*pPreviousState);
  266. NtClose(Token);
  267. return (FALSE);
  268. }
  269. }
  270. ////////////////////////////////////////////////////////////////////////////
  271. //
  272. // ResetTimePrivilege
  273. //
  274. // Restore previous privilege state for setting system time.
  275. //
  276. ////////////////////////////////////////////////////////////////////////////
  277. BOOL ResetTimePrivilege(
  278. PTOKEN_PRIVILEGES PreviousState,
  279. ULONG PreviousStateLength)
  280. {
  281. NTSTATUS NtStatus;
  282. HANDLE Token;
  283. LUID SystemTimePrivilege;
  284. ULONG ReturnLength;
  285. if (PreviousState == NULL)
  286. {
  287. return (FALSE);
  288. }
  289. // Open our own token.
  290. NtStatus = NtOpenProcessToken( NtCurrentProcess(),
  291. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  292. &Token);
  293. if (!NT_SUCCESS(NtStatus))
  294. {
  295. return (FALSE);
  296. }
  297. // Initialize the adjustment structure.
  298. SystemTimePrivilege = RtlConvertLongToLuid(SE_SYSTEMTIME_PRIVILEGE);
  299. // Restore previous state of the privilege.
  300. NtStatus = NtAdjustPrivilegesToken( Token, // TokenHandle
  301. FALSE, // DisableAllPrivileges
  302. PreviousState, // NewState
  303. PreviousStateLength, // BufferLength
  304. NULL, // PreviousState (OPTIONAL)
  305. &ReturnLength ); // ReturnLength
  306. // Clean up some stuff before returning.
  307. LocalFree(PreviousState);
  308. NtClose(Token);
  309. return (NT_SUCCESS(NtStatus));
  310. }
  311. ////////////////////////////////////////////////////////////////////////////
  312. //
  313. // DoMessageBox
  314. //
  315. ////////////////////////////////////////////////////////////////////////////
  316. int DoMessageBox(
  317. HWND hWnd,
  318. DWORD wText,
  319. DWORD wCaption,
  320. DWORD wType)
  321. {
  322. TCHAR szText[2 * MAX_PATH];
  323. TCHAR szCaption[MAX_PATH];
  324. if (!LoadString(g_hInst, wText, szText, CharSizeOf(szText)))
  325. {
  326. return (0);
  327. }
  328. if (!LoadString(g_hInst, wCaption, szCaption, CharSizeOf(szCaption)))
  329. {
  330. return (0);
  331. }
  332. return ( MessageBox(hWnd, szText, szCaption, wType) );
  333. }