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.

282 lines
6.3 KiB

  1. /******************************************************************************
  2. *
  3. * CHGUSR.C
  4. *
  5. * Text utility to change INI file mapping settings
  6. *
  7. * Copyright (c) 1998-1999 Microsoft Corporation
  8. *
  9. *
  10. *******************************************************************************/
  11. #include "precomp.h"
  12. #include <ntddkbd.h>
  13. #include <winsta.h>
  14. #include <syslib.h>
  15. #include <assert.h>
  16. #include <stdlib.h>
  17. #include <time.h>
  18. #include <utilsub.h>
  19. #include <string.h>
  20. #include <malloc.h>
  21. #include <locale.h>
  22. #include "chgusr.h"
  23. #include "winbasep.h"
  24. #include "regapi.h"
  25. #if DBG
  26. ULONG
  27. DbgPrint(
  28. PCH Format,
  29. ...
  30. );
  31. #define DBGPRINT(x) DbgPrint x
  32. #if DBGTRACE
  33. #define TRACE0(x) DbgPrint x
  34. #define TRACE1(x) DbgPrint x
  35. #else
  36. #define TRACE0(x)
  37. #define TRACE1(x)
  38. #endif
  39. #else
  40. #define DBGPRINT(x)
  41. #define TRACE0(x)
  42. #define TRACE1(x)
  43. #endif
  44. WCHAR Arg1[MAX_IDS_LEN+1];
  45. int On_flag = FALSE;
  46. int Off_flag = FALSE;
  47. int Query_flag = FALSE;
  48. int Help_flag = FALSE;
  49. int Install_flag = FALSE;
  50. int Execute_flag = FALSE;
  51. TOKMAPW ptm[] = {
  52. {L" ", TMFLAG_OPTIONAL, TMFORM_STRING, MAX_IDS_LEN, Arg1},
  53. {L"/INIMAPPING:ON", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(int), &On_flag},
  54. {L"/INIMAPPING:OFF", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(int), &Off_flag},
  55. {L"/QUERY", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(int), &Query_flag},
  56. {L"/Q", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(int), &Query_flag},
  57. {L"/INSTALL", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(int), &Install_flag},
  58. {L"/EXECUTE", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(int), &Execute_flag},
  59. {L"/?", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &Help_flag},
  60. {0, 0, 0, 0, 0}
  61. };
  62. BOOL IsRemoteAdminMode( );
  63. BOOL
  64. TestUserForAdmin( VOID );
  65. /*******************************************************************************
  66. *
  67. * main
  68. *
  69. ******************************************************************************/
  70. int __cdecl
  71. main(INT argc, CHAR **argv)
  72. {
  73. WCHAR **argvW;
  74. ULONG rc;
  75. int i;
  76. BOOL Result;
  77. BOOL State;
  78. HANDLE hWin;
  79. setlocale(LC_ALL, ".OCP");
  80. /*
  81. * Massage the command line.
  82. */
  83. argvW = MassageCommandLine((DWORD)argc);
  84. if (argvW == NULL) {
  85. ErrorPrintf(IDS_ERROR_MALLOC);
  86. return(FAILURE);
  87. }
  88. /*
  89. * parse the cmd line without parsing the program name (argc-1, argv+1)
  90. */
  91. rc = ParseCommandLine(argc-1, argvW+1, ptm, 0);
  92. /*
  93. * Check for error from ParseCommandLine
  94. */
  95. if ( Help_flag || (rc && !(rc & PARSE_FLAG_NO_PARMS)) ) {
  96. if ( !Help_flag ) {
  97. // International
  98. ErrorPrintf(IDS_ERROR_INVALID_PARAMETERS);
  99. ErrorPrintf(IDS_HELP_USAGE1);
  100. ErrorPrintf(IDS_HELP_USAGE2);
  101. ErrorPrintf(IDS_HELP_USAGE3);
  102. ErrorPrintf(IDS_HELP_USAGE4);
  103. ErrorPrintf(IDS_HELP_USAGE5);
  104. return(FAILURE);
  105. } else {
  106. Message(IDS_HELP_USAGE1);
  107. Message(IDS_HELP_USAGE2);
  108. Message(IDS_HELP_USAGE3);
  109. Message(IDS_HELP_USAGE4);
  110. Message(IDS_HELP_USAGE5);
  111. return(SUCCESS);
  112. }
  113. }
  114. if(!AreWeRunningTerminalServices())
  115. {
  116. ErrorPrintf(IDS_ERROR_NOT_TS);
  117. return(FAILURE);
  118. }
  119. if( Query_flag ) {
  120. // Show the current state
  121. State = TermsrvAppInstallMode();
  122. if( !State ) {
  123. Message(IDS_EXECUTE);
  124. }
  125. else {
  126. Message(IDS_INSTALL);
  127. }
  128. if( IsRemoteAdminMode( ) )
  129. {
  130. Message( IDS_ERROR_REMOTE_ADMIN );
  131. }
  132. return( !State + 100 ); // Exit code 100 == INSTALL Mode
  133. // Exit Code 101 == EXECUTE Mode
  134. }
  135. /*
  136. * Set the modes necessary to install applications
  137. */
  138. if ( Install_flag ) {
  139. On_flag = FALSE;
  140. Off_flag = TRUE;
  141. }
  142. /*
  143. * Set the modes necessary to run applications
  144. */
  145. if ( Execute_flag ) {
  146. On_flag = TRUE;
  147. Off_flag = FALSE;
  148. }
  149. // Default to Execute mode
  150. State = TRUE;
  151. if( On_flag || Off_flag ) {
  152. if( IsRemoteAdminMode( ) ) {
  153. Message( IDS_ERROR_REMOTE_ADMIN );
  154. return SUCCESS;
  155. }
  156. if( Off_flag ) {
  157. /*
  158. * We only allow admins to turn off execute mode
  159. */
  160. if( !TestUserForAdmin() ) {
  161. ErrorPrintf(IDS_ERROR_ADMIN_ONLY);
  162. return(FAILURE);
  163. }
  164. State = FALSE;
  165. }
  166. rc = SetTermsrvAppInstallMode( (BOOL)(!State) );
  167. if( !rc ) {
  168. // Use function to map error message to string
  169. ErrorPrintf(IDS_ERROR_INI_MAPPING_FAILED,GetLastError());
  170. return(!rc);
  171. } else {
  172. if ( Off_flag )
  173. Message(IDS_READY_INSTALL);
  174. if ( On_flag )
  175. Message(IDS_READY_EXECUTE);
  176. }
  177. }
  178. else {
  179. Message(IDS_HELP_USAGE1);
  180. Message(IDS_HELP_USAGE2);
  181. Message(IDS_HELP_USAGE3);
  182. Message(IDS_HELP_USAGE4);
  183. Message(IDS_HELP_USAGE5);
  184. return(FAILURE);
  185. }
  186. if( IsRemoteAdminMode( ) )
  187. {
  188. Message( IDS_ERROR_REMOTE_ADMIN );
  189. }
  190. return( !rc );
  191. }
  192. BOOL IsRemoteAdminMode( )
  193. {
  194. HKEY hKey;
  195. DWORD dwData = 0;
  196. BOOL fMode = FALSE;
  197. DWORD dwSize = sizeof( DWORD );
  198. DBGPRINT( ( "CHGUSR : IsRemoteAdminMode\n" ) );
  199. if( RegOpenKeyEx( HKEY_LOCAL_MACHINE ,
  200. REG_CONTROL_TSERVER,
  201. 0,
  202. KEY_READ ,
  203. &hKey ) != ERROR_SUCCESS )
  204. {
  205. DBGPRINT( ( "CHGUSR : IsRemoteAdminMode -- RegOpenEx unable to open key\n" ) );
  206. return FALSE;
  207. }
  208. if( RegQueryValueEx( hKey ,
  209. TEXT( "TSAppCompat" ) ,
  210. NULL ,
  211. NULL ,
  212. ( LPBYTE )&dwData ,
  213. &dwSize ) != ERROR_SUCCESS )
  214. {
  215. DBGPRINT( ( "CHGUSR : IsRemoteAdminMode -- RegQueryValueEx failed\n" ) );
  216. fMode = FALSE; // for application server
  217. }
  218. else
  219. {
  220. // dwData = 0 fMode = TRUE remote admin mode
  221. // dwData = 1 fMode = FALSE app server mode
  222. fMode = !( BOOL )dwData;
  223. }
  224. RegCloseKey( hKey );
  225. return fMode;
  226. }