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.

335 lines
7.4 KiB

  1. //Copyright (c) 1998 - 1999 Microsoft Corporation
  2. /******************************************************************************
  3. *
  4. * NDSPSVR.C
  5. *
  6. * This module contains code for the NDSPSVR utility.
  7. * This utility adds or removes the NDS Preferred Server registry key.
  8. *
  9. * Copyright Citrix Systems Inc. 1996-1997
  10. *
  11. * $Log: $
  12. *
  13. *******************************************************************************/
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <windows.h>
  18. #include <winsta.h>
  19. #include <regapi.h>
  20. #include <assert.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <utilsub.h>
  24. #include <string.h>
  25. #undef SAP_SOCKET
  26. #include <ncp.h>
  27. #include "ndspsvr.h"
  28. #include <printfoa.h>
  29. /*
  30. * Global Data
  31. */
  32. USHORT help_flag = FALSE; // User wants help
  33. USHORT fQuery = FALSE; // query
  34. USHORT fDisable = FALSE; // disable
  35. WCHAR Server[MAX_SERVER_NAME_LENGTH];
  36. TOKMAP ptm[] = {
  37. {L"/q", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &fQuery},
  38. {L"/query", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &fQuery},
  39. {L"/enable", TMFLAG_OPTIONAL, TMFORM_STRING, MAX_SERVER_NAME_LENGTH, Server},
  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. BOOL DeleteKey(VOID);
  49. BOOL AddKey(VOID);
  50. BOOL QueryKey(VOID);
  51. /*******************************************************************************
  52. *
  53. * main
  54. *
  55. ******************************************************************************/
  56. int __cdecl
  57. main(INT argc, CHAR **argv)
  58. {
  59. WCHAR *CmdLine;
  60. WCHAR **argvW;
  61. BOOL rc;
  62. INT i;
  63. /*
  64. * We can't use argv[] because its always ANSI, regardless of UNICODE
  65. */
  66. CmdLine = GetCommandLineW();
  67. /*
  68. * Massage the new command line to look like an argv[] type
  69. * because ParseCommandLine() depends on this format
  70. */
  71. argvW = (WCHAR **)malloc( sizeof(WCHAR *) * (argc+1) );
  72. if(argvW == NULL) {
  73. ErrorPrintf(IDS_ERROR_MALLOC);
  74. return(FAILURE);
  75. }
  76. argvW[0] = wcstok(CmdLine, L" ");
  77. for(i=1; i < argc; i++){
  78. argvW[i] = wcstok(0, L" ");
  79. OEM2ANSIW(argvW[i], wcslen(argvW[i]));
  80. }
  81. argvW[argc] = NULL;
  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. * Enable or disable
  100. */
  101. if ( fDisable ) {
  102. rc = DeleteKey();
  103. }
  104. else if ( Server[0] ) {
  105. rc = AddKey();
  106. }
  107. else if ( fQuery ) {
  108. if ( rc = QueryKey() ) {
  109. if (Server[0])
  110. ErrorPrintf(IDS_NDSPSVR_ENABLED, Server);
  111. else
  112. ErrorPrintf(IDS_NDSPSVR_DISABLED);
  113. }
  114. }
  115. /*
  116. * Query or error ?
  117. */
  118. if ( !rc ) {
  119. ErrorPrintf(IDS_ACCESS_DENIED);
  120. }
  121. return(SUCCESS);
  122. }
  123. /*******************************************************************************
  124. *
  125. * Usage
  126. *
  127. * Output the usage message for this utility.
  128. *
  129. * ENTRY:
  130. * bError (input)
  131. * TRUE if the 'invalid parameter(s)' message should preceed the usage
  132. * message and the output go to stderr; FALSE for no such error
  133. * string and output goes to stdout.
  134. *
  135. * EXIT:
  136. *
  137. *
  138. ******************************************************************************/
  139. void
  140. Usage( BOOLEAN bError )
  141. {
  142. if ( bError ) {
  143. ErrorPrintf(IDS_ERROR_INVALID_PARAMETERS);
  144. }
  145. ErrorPrintf(IDS_HELP_USAGE1);
  146. ErrorPrintf(IDS_HELP_USAGE2);
  147. ErrorPrintf(IDS_HELP_USAGE3);
  148. ErrorPrintf(IDS_HELP_USAGE4);
  149. ErrorPrintf(IDS_HELP_USAGE5);
  150. } /* Usage() */
  151. /*******************************************************************************
  152. *
  153. * QueryKey
  154. *
  155. * ENTRY:
  156. *
  157. * EXIT:
  158. *
  159. *
  160. ******************************************************************************/
  161. BOOL
  162. QueryKey()
  163. {
  164. DWORD dwType = REG_SZ;
  165. DWORD dwSize = MAX_SERVER_NAME_LENGTH * sizeof(WCHAR);
  166. WCHAR szValue[MAX_SERVER_NAME_LENGTH];
  167. HKEY Handle;
  168. LONG rc;
  169. /*
  170. * Open registry
  171. */
  172. if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  173. REG_CONTROL_TSERVER,
  174. 0,
  175. KEY_READ,
  176. &Handle ) != ERROR_SUCCESS )
  177. return FALSE;
  178. /*
  179. * Read registry value
  180. */
  181. rc = RegQueryValueExW( Handle,
  182. REG_CITRIX_NWNDSPREFERREDSERVER,
  183. NULL,
  184. &dwType,
  185. (PUCHAR)&szValue,
  186. &dwSize );
  187. /*
  188. * Close registry and key handle
  189. */
  190. RegCloseKey( Handle );
  191. if ( rc == ERROR_SUCCESS ) {
  192. wcscpy (Server, szValue);
  193. return TRUE;
  194. }
  195. else if ( rc == ERROR_FILE_NOT_FOUND )
  196. return TRUE;
  197. else
  198. return FALSE;
  199. }
  200. /*******************************************************************************
  201. *
  202. * DeleteKey
  203. *
  204. * ENTRY:
  205. *
  206. * EXIT:
  207. *
  208. *
  209. ******************************************************************************/
  210. BOOL
  211. DeleteKey()
  212. {
  213. DWORD dwType = REG_SZ;
  214. DWORD dwSize = MAX_SERVER_NAME_LENGTH;
  215. WCHAR szValue[MAX_SERVER_NAME_LENGTH];
  216. HKEY Handle;
  217. LONG rc;
  218. /*
  219. * Open registry
  220. */
  221. if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  222. REG_CONTROL_TSERVER,
  223. 0,
  224. KEY_ALL_ACCESS,
  225. &Handle ) != ERROR_SUCCESS )
  226. return FALSE;
  227. /*
  228. * Delete Preferred Server key
  229. */
  230. rc = RegDeleteValueW( Handle,
  231. REG_CITRIX_NWNDSPREFERREDSERVER );
  232. /*
  233. * Close registry and key handle
  234. */
  235. RegCloseKey( Handle );
  236. if ( rc != ERROR_SUCCESS )
  237. return FALSE;
  238. else
  239. return TRUE;
  240. }
  241. /*******************************************************************************
  242. *
  243. * AddKey
  244. *
  245. * ENTRY:
  246. *
  247. * EXIT:
  248. *
  249. *
  250. ******************************************************************************/
  251. BOOL
  252. AddKey()
  253. {
  254. HKEY Handle;
  255. LONG rc;
  256. DWORD dwDummy;
  257. DWORD dwSize;
  258. /*
  259. * Open registry
  260. */
  261. if ( RegCreateKeyEx( HKEY_LOCAL_MACHINE,
  262. REG_CONTROL_TSERVER,
  263. 0,
  264. NULL,
  265. REG_OPTION_NON_VOLATILE,
  266. KEY_ALL_ACCESS,
  267. NULL,
  268. &Handle,
  269. &dwDummy ) != ERROR_SUCCESS )
  270. return FALSE;
  271. /*
  272. * Write registry value
  273. */
  274. dwSize = ( (wcslen(Server)+1) * sizeof(WCHAR) );
  275. rc = RegSetValueExW( Handle,
  276. REG_CITRIX_NWNDSPREFERREDSERVER,
  277. 0,
  278. REG_SZ,
  279. (PUCHAR)Server,
  280. dwSize );
  281. /*
  282. * Close registry and key handle
  283. */
  284. RegCloseKey( Handle );
  285. return( rc == ERROR_SUCCESS );
  286. }