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.

358 lines
8.3 KiB

  1. /******************************************************************************
  2. *
  3. * FLATTEMP.C
  4. *
  5. * This module contains code for the FLATTEMP utility.
  6. * This utility adds or removes the Flat Temporary directory registry key.
  7. *
  8. * Copyright Citrix Systems Inc. 1996
  9. * Copyright (c) 1998-1999 Microsoft Corporation
  10. *
  11. *******************************************************************************/
  12. #include <nt.h>
  13. #include <ntrtl.h>
  14. #include <nturtl.h>
  15. #include <windows.h>
  16. #include <winstaw.h>
  17. #include <regapi.h>
  18. #include <assert.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <utilsub.h>
  22. #include <string.h>
  23. #include <utildll.h>
  24. #include <locale.h>
  25. #include <winnlsp.h>
  26. #include "flattemp.h"
  27. #include "printfoa.h"
  28. // max length of the locale string
  29. #define MAX_LOCALE_STRING 64
  30. /*
  31. * Global Data
  32. */
  33. USHORT help_flag = FALSE; // User wants help
  34. USHORT fQuery = FALSE; // query
  35. USHORT fDisable = FALSE; // disable
  36. USHORT fEnable = FALSE; // enable
  37. TOKMAP ptm[] = {
  38. {L"/query", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &fQuery},
  39. {L"/enable", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &fEnable},
  40. {L"/disable", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &fDisable},
  41. {L"/?", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &help_flag},
  42. {0, 0, 0, 0, 0}
  43. };
  44. /*
  45. * Local function prototypes.
  46. */
  47. VOID Usage(BOOLEAN bError);
  48. LONG DeleteKey(VOID);
  49. LONG AddKey(VOID);
  50. BOOL QueryKey(VOID);
  51. #define SZENABLE TEXT("1")
  52. /*******************************************************************************
  53. *
  54. * main
  55. *
  56. ******************************************************************************/
  57. int __cdecl
  58. main(INT argc, CHAR **argv)
  59. {
  60. WCHAR *CmdLine;
  61. WCHAR **argvW;
  62. LONG rc = 0;
  63. INT i;
  64. POLICY_TS_MACHINE policy;
  65. WCHAR wszString[MAX_LOCALE_STRING + 1];
  66. setlocale(LC_ALL, ".OCP");
  67. // We don't want LC_CTYPE set the same as the others or else we will see
  68. // garbage output in the localized version, so we need to explicitly
  69. // set it to correct console output code page
  70. _snwprintf(wszString, sizeof(wszString)/sizeof(WCHAR), L".%d", GetConsoleOutputCP());
  71. wszString[sizeof(wszString)/sizeof(WCHAR) - 1] = L'\0';
  72. _wsetlocale(LC_CTYPE, wszString);
  73. SetThreadUILanguage(0);
  74. /*
  75. * Massage the command line.
  76. */
  77. argvW = MassageCommandLine((DWORD)argc);
  78. if (argvW == NULL) {
  79. ErrorPrintf(IDS_ERROR_MALLOC);
  80. return(FAILURE);
  81. }
  82. /*
  83. * parse the cmd line without parsing the program name (argc-1, argv+1)
  84. */
  85. rc = ParseCommandLine(argc-1, argvW+1, ptm, 0);
  86. /*
  87. * Check for error from ParseCommandLine
  88. */
  89. if ( help_flag || rc ) {
  90. if ( !help_flag && !(rc & PARSE_FLAG_NO_PARMS) ) {
  91. Usage(TRUE);
  92. return(FAILURE);
  93. } else {
  94. Usage(FALSE);
  95. return(SUCCESS);
  96. }
  97. }
  98. /*
  99. * If there is a policy to user tmp folders, then prevent
  100. * this tool from running.
  101. */
  102. RegGetMachinePolicy( &policy );
  103. if ( policy.fPolicyTempFoldersPerSession )
  104. {
  105. Message( IDS_ACCESS_DENIED_DUE_TO_GROUP_POLICY );
  106. return ( FAILURE );
  107. }
  108. //Check if we are running under Terminal Server
  109. if(!AreWeRunningTerminalServices())
  110. {
  111. ErrorPrintf(IDS_ERROR_NOT_TS);
  112. return(FAILURE);
  113. }
  114. if (!TestUserForAdmin(FALSE)) {
  115. Message(IDS_ACCESS_DENIED_ADMIN);
  116. return(FAILURE);
  117. }
  118. /*
  119. * Enable or disable
  120. */
  121. rc = 0; // reset in case it changed above and we're querying...
  122. if ( fDisable ) {
  123. rc = DeleteKey();
  124. Message(IDS_FLATTEMP_DISABLED);
  125. } else if ( fEnable ) {
  126. rc = AddKey();
  127. Message(IDS_FLATTEMP_ENABLED);
  128. } else if ( fQuery ) {
  129. if ( QueryKey() ) {
  130. Message(IDS_FLATTEMP_ENABLED);
  131. } else {
  132. Message(IDS_FLATTEMP_DISABLED);
  133. }
  134. }
  135. /*
  136. * Error? (It'll be set if there's a problem...)
  137. */
  138. if ( rc ) {
  139. Message(IDS_ACCESS_DENIED);
  140. }
  141. return(SUCCESS);
  142. }
  143. /*******************************************************************************
  144. *
  145. * Usage
  146. *
  147. * Output the usage message for this utility.
  148. *
  149. * ENTRY:
  150. * bError (input)
  151. * TRUE if the 'invalid parameter(s)' message should preceed the usage
  152. * message and the output go to stderr; FALSE for no such error
  153. * string and output goes to stdout.
  154. *
  155. * EXIT:
  156. *
  157. *
  158. ******************************************************************************/
  159. void
  160. Usage( BOOLEAN bError )
  161. {
  162. if ( bError ) {
  163. Message(IDS_ERROR_INVALID_PARAMETERS);
  164. }
  165. Message(IDS_HELP_USAGE1);
  166. Message(IDS_HELP_USAGE2);
  167. Message(IDS_HELP_USAGE3);
  168. Message(IDS_HELP_USAGE4);
  169. Message(IDS_HELP_USAGE5);
  170. } /* Usage() */
  171. /*******************************************************************************
  172. *
  173. * QueryKey
  174. *
  175. * ENTRY:
  176. *
  177. * EXIT: TRUE - enabled
  178. * FALSE - disabled (key doesn't exist or isn't "1")
  179. *
  180. *
  181. ******************************************************************************/
  182. BOOL
  183. QueryKey()
  184. {
  185. DWORD dwType = REG_SZ;
  186. DWORD dwSize = 3 * sizeof(WCHAR);
  187. WCHAR szValue[3];
  188. HKEY Handle;
  189. LONG rc;
  190. /*
  191. * Open registry
  192. */
  193. if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  194. REG_CONTROL_TSERVER,
  195. 0,
  196. KEY_READ,
  197. &Handle ) != ERROR_SUCCESS )
  198. return FALSE;
  199. /*
  200. * Read registry value
  201. */
  202. rc = RegQueryValueExW( Handle,
  203. REG_CITRIX_FLATTEMPDIR,
  204. NULL,
  205. &dwType,
  206. (PUCHAR)&szValue,
  207. &dwSize );
  208. /*
  209. * Close registry and key handle
  210. */
  211. RegCloseKey( Handle );
  212. if ( rc == ERROR_SUCCESS && lstrcmp(szValue,SZENABLE) == 0 ) {
  213. return TRUE;
  214. } else {
  215. return FALSE;
  216. }
  217. }
  218. /*******************************************************************************
  219. *
  220. * DeleteKey
  221. *
  222. * ENTRY:
  223. *
  224. * EXIT:
  225. *
  226. *
  227. ******************************************************************************/
  228. LONG
  229. DeleteKey()
  230. {
  231. HKEY Handle;
  232. LONG rc;
  233. /*
  234. * Open registry
  235. */
  236. if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  237. REG_CONTROL_TSERVER,
  238. 0,
  239. KEY_ALL_ACCESS,
  240. &Handle ) != ERROR_SUCCESS )
  241. return FALSE;
  242. /*
  243. * Delete flat temp directory key
  244. */
  245. rc = RegDeleteValueW( Handle,
  246. REG_CITRIX_FLATTEMPDIR );
  247. if (rc == ERROR_FILE_NOT_FOUND) {
  248. rc = ERROR_SUCCESS;
  249. }
  250. /*
  251. * Close registry and key handle
  252. */
  253. RegCloseKey( Handle );
  254. return( rc );
  255. }
  256. /*******************************************************************************
  257. *
  258. * AddKey
  259. *
  260. * ENTRY:
  261. *
  262. * EXIT:
  263. *
  264. *
  265. ******************************************************************************/
  266. LONG
  267. AddKey()
  268. {
  269. HKEY Handle;
  270. LONG rc;
  271. DWORD dwDummy;
  272. /*
  273. * Open registry
  274. */
  275. if ( RegCreateKeyEx( HKEY_LOCAL_MACHINE,
  276. REG_CONTROL_TSERVER,
  277. 0,
  278. NULL,
  279. REG_OPTION_NON_VOLATILE,
  280. KEY_ALL_ACCESS,
  281. NULL,
  282. &Handle,
  283. &dwDummy ) != ERROR_SUCCESS )
  284. return FALSE;
  285. /*
  286. * Write registry value
  287. */
  288. rc = RegSetValueExW( Handle,
  289. REG_CITRIX_FLATTEMPDIR,
  290. 0,
  291. REG_SZ,
  292. (PUCHAR)SZENABLE,
  293. sizeof(SZENABLE) );
  294. /*
  295. * Close registry and key handle
  296. */
  297. RegCloseKey( Handle );
  298. return( rc );
  299. }