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.

270 lines
6.8 KiB

  1. // Copyright (c) 1998-1999 Microsoft Corporation
  2. /*************************************************************************
  3. *
  4. * TSCON.C
  5. *
  6. *************************************************************************/
  7. #include <stdio.h>
  8. #include <windows.h>
  9. // #include <ntddkbd.h>
  10. // #include <ntddmou.h>
  11. #include <winstaw.h>
  12. #include <stdlib.h>
  13. #include <utilsub.h>
  14. #include <string.h>
  15. #include <malloc.h>
  16. #include <locale.h>
  17. #include "tscon.h"
  18. #include "printfoa.h"
  19. HANDLE hServerName = SERVERNAME_CURRENT;
  20. WCHAR ServerName[MAX_IDS_LEN+1];
  21. WINSTATIONNAME Source;
  22. WINSTATIONNAME Destination;
  23. WCHAR Password[ PASSWORD_LENGTH + 1 ];
  24. USHORT help_flag = FALSE;
  25. USHORT v_flag = FALSE;
  26. TOKMAP ptm[] =
  27. {
  28. #define TERM_PARM 0
  29. {TOKEN_SOURCE, TMFLAG_REQUIRED, TMFORM_S_STRING,
  30. WINSTATIONNAME_LENGTH, Source},
  31. /* { TOKEN_SERVER, TMFLAG_OPTIONAL, TMFORM_STRING,
  32. MAX_IDS_LEN, ServerName}, */
  33. {TOKEN_DESTINATION, TMFLAG_OPTIONAL, TMFORM_X_STRING,
  34. WINSTATIONNAME_LENGTH, Destination},
  35. {TOKEN_PASSWORD, TMFLAG_OPTIONAL, TMFORM_X_STRING,
  36. PASSWORD_LENGTH, Password},
  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 TSCON utility.
  51. *
  52. * ENTRY:
  53. * argc - count of the command line arguments.
  54. * argv - vector of strings containing the command line arguments.
  55. *
  56. * EXIT
  57. * Nothing.
  58. *
  59. *************************************************************************/
  60. int __cdecl
  61. main(INT argc, CHAR **argv)
  62. {
  63. BOOLEAN bCurrent = FALSE;
  64. int rc, i;
  65. WCHAR *CmdLine;
  66. WCHAR **argvW, *endptr;
  67. ULONG SourceId, DestId;
  68. setlocale(LC_ALL, ".OCP");
  69. /*
  70. * Massage the command line.
  71. */
  72. argvW = MassageCommandLine((DWORD)argc);
  73. if (argvW == NULL) {
  74. ErrorPrintf(IDS_ERROR_MALLOC);
  75. return(FAILURE);
  76. }
  77. /*
  78. * parse the cmd line without parsing the program name (argc-1, argv+1)
  79. */
  80. rc = ParseCommandLine(argc-1, argvW+1, ptm, 0);
  81. /*
  82. * Check for error from ParseCommandLine
  83. */
  84. if ( help_flag || rc ) {
  85. if ( !help_flag ) {
  86. Usage(TRUE);
  87. return(FAILURE);
  88. } else {
  89. Usage(FALSE);
  90. return(SUCCESS);
  91. }
  92. }
  93. // If SERVER or DEST are not specified, we need to run on TS
  94. // Check if we are running under Terminal Server
  95. if ( ( (!IsTokenPresent(ptm, TOKEN_SERVER) )
  96. || (!IsTokenPresent(ptm, TOKEN_DESTINATION)) )
  97. && (!AreWeRunningTerminalServices()) )
  98. {
  99. ErrorPrintf(IDS_ERROR_NOT_TS);
  100. return(FAILURE);
  101. }
  102. /*
  103. * Open the specified server
  104. */
  105. if( ServerName[0] ) {
  106. hServerName = WinStationOpenServer( ServerName );
  107. if( hServerName == NULL ) {
  108. StringErrorPrintf(IDS_ERROR_SERVER,ServerName);
  109. PutStdErr( GetLastError(), 0 );
  110. return(FAILURE);
  111. }
  112. }
  113. /*
  114. * Validate the source.
  115. */
  116. if ( !IsTokenPresent(ptm, TOKEN_SOURCE) ) {
  117. /*
  118. * No source specified; use current winstation.
  119. */
  120. SourceId = GetCurrentLogonId();
  121. } else if ( !iswdigit(*Source) ) {
  122. /*
  123. * Treat the source string as a WinStation name.
  124. */
  125. if ( !LogonIdFromWinStationName(hServerName, Source, &SourceId) ) {
  126. StringErrorPrintf(IDS_ERROR_WINSTATION_NOT_FOUND, Source);
  127. return(FAILURE);
  128. }
  129. } else {
  130. /*
  131. * Treat the source string as a LogonId.
  132. */
  133. SourceId = wcstoul(Source, &endptr, 10);
  134. if ( *endptr ) {
  135. StringErrorPrintf(IDS_ERROR_INVALID_LOGONID, Source);
  136. return(FAILURE);
  137. }
  138. if ( !WinStationNameFromLogonId(hServerName, SourceId, Source) ) {
  139. ErrorPrintf(IDS_ERROR_LOGONID_NOT_FOUND, SourceId);
  140. return(FAILURE);
  141. }
  142. }
  143. /*
  144. * Validate the destination.
  145. */
  146. if ( !IsTokenPresent(ptm, TOKEN_DESTINATION) ) {
  147. /*
  148. * No destination specified; use current winstation.
  149. */
  150. bCurrent = TRUE;
  151. DestId = GetCurrentLogonId();
  152. if ( !WinStationNameFromLogonId(hServerName, DestId, Destination) ) {
  153. ErrorPrintf(IDS_ERROR_CANT_GET_CURRENT_WINSTATION, GetLastError());
  154. PutStdErr(GetLastError(), 0);
  155. return(FAILURE);
  156. }
  157. } else {
  158. /*
  159. * Validate the destination WinStation name.
  160. */
  161. if ( !LogonIdFromWinStationName(hServerName, Destination, &DestId) ) {
  162. StringErrorPrintf(IDS_ERROR_WINSTATION_NOT_FOUND, Destination);
  163. return(FAILURE);
  164. }
  165. }
  166. /*
  167. * Perform the connect.
  168. */
  169. if ( v_flag )
  170. DwordStringMessage(IDS_WINSTATION_CONNECT, SourceId, Destination);
  171. if ( !WinStationConnect(hServerName, SourceId, DestId, Password, TRUE) ) {
  172. if ( bCurrent )
  173. ErrorPrintf(IDS_ERROR_WINSTATION_CONNECT_CURRENT,
  174. SourceId, GetLastError());
  175. else
  176. ErrorPrintf(IDS_ERROR_WINSTATION_CONNECT,
  177. SourceId, Destination, GetLastError());
  178. PutStdErr(GetLastError(), 0);
  179. return(FAILURE);
  180. }
  181. return(SUCCESS);
  182. } /* main() */
  183. /*******************************************************************************
  184. *
  185. * Usage
  186. *
  187. * Output the usage message for this utility.
  188. *
  189. * ENTRY:
  190. * bError (input)
  191. * TRUE if the 'invalid parameter(s)' message should preceed the usage
  192. * message and the output go to stderr; FALSE for no such error
  193. * string and output goes to stdout.
  194. *
  195. * EXIT:
  196. *
  197. *
  198. ******************************************************************************/
  199. void
  200. Usage( BOOLEAN bError )
  201. {
  202. if ( bError ) {
  203. ErrorPrintf(IDS_ERROR_INVALID_PARAMETERS);
  204. ErrorPrintf(IDS_USAGE_1);
  205. ErrorPrintf(IDS_USAGE_2);
  206. ErrorPrintf(IDS_USAGE_3);
  207. ErrorPrintf(IDS_USAGE_4);
  208. ErrorPrintf(IDS_USAGE_5);
  209. // ErrorPrintf(IDS_USAGE_6);
  210. ErrorPrintf(IDS_USAGE_7);
  211. ErrorPrintf(IDS_USAGE_8);
  212. ErrorPrintf(IDS_USAGE_9);
  213. }
  214. else{
  215. Message(IDS_USAGE_1);
  216. Message(IDS_USAGE_2);
  217. Message(IDS_USAGE_3);
  218. Message(IDS_USAGE_4);
  219. Message(IDS_USAGE_5);
  220. //Message(IDS_USAGE_6);
  221. Message(IDS_USAGE_7);
  222. Message(IDS_USAGE_8);
  223. Message(IDS_USAGE_9);
  224. }
  225. } /* Usage() */