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.

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