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.

257 lines
6.5 KiB

  1. // Copyright (c) 1998-1999 Microsoft Corporation
  2. /*************************************************************************
  3. *
  4. * TSDISCON.C
  5. *
  6. * This module is the TSDISCON utility code.
  7. *
  8. *
  9. *************************************************************************/
  10. #include <stdio.h>
  11. #include <windows.h>
  12. //#include <ntddkbd.h>
  13. //#include <ntddmou.h>
  14. #include <winstaw.h>
  15. #include <stdlib.h>
  16. #include <utilsub.h>
  17. #include <string.h>
  18. #include <malloc.h>
  19. #include <locale.h>
  20. #include <winnlsp.h>
  21. #include "tsdiscon.h"
  22. #include "printfoa.h"
  23. // max length of the locale string
  24. #define MAX_LOCALE_STRING 64
  25. WINSTATIONNAME WSName;
  26. USHORT help_flag = FALSE;
  27. USHORT v_flag = FALSE;
  28. HANDLE hServerName = SERVERNAME_CURRENT;
  29. WCHAR ServerName[MAX_IDS_LEN+1];
  30. TOKMAP ptm[] =
  31. {
  32. #define TERM_PARM 0
  33. {TOKEN_WS, TMFLAG_OPTIONAL, TMFORM_STRING,
  34. WINSTATIONNAME_LENGTH, WSName},
  35. {TOKEN_SERVER, TMFLAG_OPTIONAL, TMFORM_STRING,
  36. MAX_IDS_LEN, ServerName},
  37. {TOKEN_HELP, TMFLAG_OPTIONAL, TMFORM_BOOLEAN,
  38. sizeof(USHORT), &help_flag},
  39. {TOKEN_VERBOSE, TMFLAG_OPTIONAL, TMFORM_BOOLEAN,
  40. sizeof(USHORT), &v_flag},
  41. {0, 0, 0, 0, 0}
  42. };
  43. /*
  44. * Local function prototypes.
  45. */
  46. void Usage( BOOLEAN bError );
  47. /*************************************************************************
  48. *
  49. * main
  50. * Main function and entry point of the TSDISCON
  51. * utility.
  52. *
  53. * ENTRY:
  54. * argc - count of the command line arguments.
  55. * argv - vector of strings containing the command line arguments.
  56. *
  57. * EXIT
  58. * Nothing.
  59. *
  60. *************************************************************************/
  61. int __cdecl
  62. main(INT argc, CHAR **argv)
  63. {
  64. BOOLEAN bCurrent = FALSE;
  65. int rc, i;
  66. ULONG Error;
  67. WCHAR *CmdLine;
  68. WCHAR **argvW, *endptr;
  69. ULONG LogonId;
  70. WCHAR wszString[MAX_LOCALE_STRING + 1];
  71. setlocale(LC_ALL, ".OCP");
  72. // We don't want LC_CTYPE set the same as the others or else we will see
  73. // garbage output in the localized version, so we need to explicitly
  74. // set it to correct console output code page
  75. _snwprintf(wszString, sizeof(wszString)/sizeof(WCHAR), L".%d", GetConsoleOutputCP());
  76. wszString[sizeof(wszString)/sizeof(WCHAR) - 1] = L'\0';
  77. _wsetlocale(LC_CTYPE, wszString);
  78. SetThreadUILanguage(0);
  79. /*
  80. * Massage the command line.
  81. */
  82. argvW = MassageCommandLine((DWORD)argc);
  83. if (argvW == NULL) {
  84. ErrorPrintf(IDS_ERROR_MALLOC);
  85. return(FAILURE);
  86. }
  87. /*
  88. * parse the cmd line without parsing the program name (argc-1, argv+1)
  89. */
  90. rc = ParseCommandLine(argc-1, argvW+1, ptm, 0);
  91. /*
  92. * Check for error from ParseCommandLine
  93. */
  94. if ( help_flag || (rc && !(rc & PARSE_FLAG_NO_PARMS)) ) {
  95. if ( !help_flag ) {
  96. Usage(TRUE);
  97. return(FAILURE);
  98. } else {
  99. Usage(FALSE);
  100. return(SUCCESS);
  101. }
  102. }
  103. //Check if we are running under Terminal Server
  104. if ((!IsTokenPresent(ptm, TOKEN_SERVER) ) && (!AreWeRunningTerminalServices()))
  105. {
  106. ErrorPrintf(IDS_ERROR_NOT_TS);
  107. return(FAILURE);
  108. }
  109. /*
  110. * Open the specified server
  111. */
  112. if( ServerName[0] ) {
  113. hServerName = WinStationOpenServer( ServerName );
  114. if( hServerName == NULL ) {
  115. StringErrorPrintf(IDS_ERROR_SERVER,ServerName);
  116. PutStdErr( GetLastError(), 0 );
  117. return(FAILURE);
  118. }
  119. }
  120. /*
  121. * Validate input string for WinStation or LogonId.
  122. */
  123. if ( !IsTokenPresent(ptm, TOKEN_WS) ) {
  124. /*
  125. * No string specified; use current WinStation / LogonId.
  126. */
  127. bCurrent = TRUE;
  128. LogonId = GetCurrentLogonId();
  129. if ( !WinStationNameFromLogonId(hServerName, LogonId, WSName) ) {
  130. ErrorPrintf(IDS_ERROR_CANT_GET_CURRENT_WINSTATION, GetLastError());
  131. PutStdErr( GetLastError(), 0 );
  132. return(FAILURE);
  133. }
  134. } else if ( !iswdigit(*WSName) ) {
  135. /*
  136. * Treat the string as a WinStation name.
  137. */
  138. if ( !LogonIdFromWinStationName(hServerName, WSName, &LogonId) ) {
  139. StringErrorPrintf(IDS_ERROR_WINSTATION_NOT_FOUND, WSName);
  140. return(FAILURE);
  141. }
  142. } else {
  143. /*
  144. * Treat the string as a LogonId.
  145. */
  146. LogonId = wcstoul(WSName, &endptr, 10);
  147. if ( *endptr ) {
  148. StringErrorPrintf(IDS_ERROR_INVALID_LOGONID, WSName);
  149. return(FAILURE);
  150. }
  151. if ( !WinStationNameFromLogonId(hServerName, LogonId, WSName) ) {
  152. ErrorPrintf(IDS_ERROR_LOGONID_NOT_FOUND, LogonId);
  153. return(FAILURE);
  154. }
  155. }
  156. /*
  157. * Perform the disconnect.
  158. */
  159. if ( v_flag )
  160. DwordStringMessage(IDS_WINSTATION_DISCONNECT, LogonId, WSName);
  161. if ( !WinStationDisconnect(hServerName, LogonId, TRUE) ) {
  162. if ( bCurrent )
  163. ErrorPrintf(IDS_ERROR_DISCONNECT_CURRENT,
  164. GetLastError());
  165. else
  166. ErrorPrintf(IDS_ERROR_DISCONNECT,
  167. LogonId, WSName, GetLastError());
  168. PutStdErr( GetLastError(), 0 );
  169. return(FAILURE);
  170. }
  171. return(SUCCESS);
  172. } /* main() */
  173. /*******************************************************************************
  174. *
  175. * Usage
  176. *
  177. * Output the usage message for this utility.
  178. *
  179. * ENTRY:
  180. * bError (input)
  181. * TRUE if the 'invalid parameter(s)' message should preceed the usage
  182. * message and the output go to stderr; FALSE for no such error
  183. * string and output goes to stdout.
  184. *
  185. * EXIT:
  186. *
  187. *
  188. ******************************************************************************/
  189. void
  190. Usage( BOOLEAN bError )
  191. {
  192. if ( bError ) {
  193. ErrorPrintf(IDS_ERROR_INVALID_PARAMETERS);
  194. ErrorPrintf(IDS_USAGE_1);
  195. ErrorPrintf(IDS_USAGE_2);
  196. ErrorPrintf(IDS_USAGE_3);
  197. ErrorPrintf(IDS_USAGE_4);
  198. ErrorPrintf(IDS_USAGE_5);
  199. ErrorPrintf(IDS_USAGE_6);
  200. } else {
  201. Message(IDS_USAGE_1);
  202. Message(IDS_USAGE_2);
  203. Message(IDS_USAGE_3);
  204. Message(IDS_USAGE_4);
  205. Message(IDS_USAGE_5);
  206. Message(IDS_USAGE_6);
  207. }
  208. } /* Usage() */