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.

274 lines
7.3 KiB

  1. //Copyright (c) 1998 - 1999 Microsoft Corporation
  2. /******************************************************************************
  3. *
  4. * CNVRTUC
  5. *
  6. * Convert WinFrame User Configuration
  7. *
  8. * Copyright Citrix Systems Inc. 1997
  9. *
  10. * Author: BruceF
  11. *
  12. * $Log: U:\NT\PRIVATE\UTILS\citrix\cnvrtuc\VCS\CNVRTUC.C $
  13. *
  14. * Rev 1.3 May 04 1998 18:03:04 bills
  15. * Fixes for MS bug #2109, OEM->ANSI conversion and moving strings to the rc file.
  16. *
  17. * Rev 1.2 Jun 26 1997 18:17:18 billm
  18. * move to WF40 tree
  19. *
  20. * Rev 1.1 23 Jun 1997 16:17:24 butchd
  21. * update
  22. *
  23. * Rev 1.0 15 Feb 1997 09:51:48 brucef
  24. * Initial revision.
  25. *
  26. *
  27. ******************************************************************************/
  28. #include <nt.h>
  29. #include <ntrtl.h>
  30. #include <nturtl.h>
  31. #include <windows.h>
  32. #include <lm.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <hydra/regapi.h>
  36. #include <hydra/winsta.h>
  37. #include "resource.h"
  38. #include <printfoa.h>
  39. #include <utilsub.h>
  40. #if MAX_COMPUTERNAME_LENGTH > DOMAIN_LENGTH
  41. #define _COMPUTERNAME_LENGTH MAX_COMPUTER_NAME_LENGTH
  42. #else
  43. #define _COMPUTERNAME_LENGTH DOMAIN_LENGTH
  44. #endif
  45. ULONG fAll;
  46. WCHAR UserName[ USERNAME_LENGTH + 1 ];
  47. WCHAR DomainName[ _COMPUTERNAME_LENGTH + 1 ];
  48. BOOLEAN ProcessCommandLine( int argc, char *argv[] );
  49. void Print( int nResourceID, ... );
  50. BOOLEAN
  51. ConvertUserConfiguration(
  52. PWCHAR pUserName,
  53. PWCHAR pDomainName
  54. )
  55. {
  56. HKEY ServerHandle, UCHandle;
  57. PWCHAR ServerName = NULL;
  58. USERCONFIG UserConfig;
  59. LONG Error;
  60. BOOLEAN fFound;
  61. WCHAR UserNameTemp[ USERNAME_LENGTH + 1 ];
  62. ULONG Index;
  63. WCHAR ComputerName[ _COMPUTERNAME_LENGTH + 1 ];
  64. ULONG CNLength = sizeof(ComputerName)/sizeof( WCHAR );
  65. if ( !GetComputerName( ComputerName, &CNLength ) ) {
  66. Error = ERROR_NOT_ENOUGH_MEMORY;
  67. }
  68. if ( !wcscmp( pDomainName, ComputerName ) ) {
  69. ServerName = NULL;
  70. } else {
  71. PWCHAR ServerNameTemp = NULL;
  72. /*
  73. * Get the Server Name of the PDC
  74. */
  75. Error = NetGetDCName( NULL, pDomainName, (LPBYTE *)&ServerNameTemp );
  76. if ( Error != ERROR_SUCCESS ) {
  77. ErrorPrintf(IDS_ERR_GET_PDC, pDomainName, Error );
  78. goto exit;
  79. }
  80. wcscpy( ComputerName, ServerNameTemp );
  81. NetApiBufferFree( ServerNameTemp );
  82. ServerName = ComputerName;
  83. }
  84. #ifdef DEBUG
  85. fprintf( stderr,
  86. "Using server %ws as PDC for Domain %ws\n",
  87. ServerName ? ServerName : ComputerName,
  88. pDomainName );
  89. #endif
  90. /*
  91. * Connect to registry of the PDC.
  92. */
  93. if ( (Error = RegConnectRegistry( ServerName,
  94. HKEY_LOCAL_MACHINE,
  95. &ServerHandle )) != ERROR_SUCCESS ) {
  96. ErrorPrintf(IDS_ERR_CONNECT_REG, Error );
  97. goto exit;
  98. }
  99. /*
  100. * Open the UserConfiguration key
  101. */
  102. if ( (Error = RegOpenKeyEx( ServerHandle, USERCONFIG_REG_NAME, 0,
  103. KEY_READ, &UCHandle )) != ERROR_SUCCESS ) {
  104. ErrorPrintf(IDS_ERR_OPEN_KEY, Error );
  105. goto cleanupregconnect;
  106. }
  107. fFound = FALSE;
  108. for ( Index = 0 ; ; Index++ ) {
  109. ULONG UCLength;
  110. /*
  111. * Enumerate next subkey - which is a user name
  112. */
  113. if ((Error = RegEnumKey( UCHandle, Index, UserNameTemp,
  114. sizeof(UserNameTemp)/sizeof(WCHAR))) != ERROR_SUCCESS ) {
  115. if ( Error != ERROR_NO_MORE_ITEMS ) {
  116. ErrorPrintf(IDS_ERR_ENUM_KEY, Error );
  117. break;
  118. }
  119. Error = ERROR_SUCCESS;
  120. break;
  121. }
  122. /*
  123. * Get the configuration - it may already be in the SAM.
  124. * The Query API is designed to look in the SAM first and then
  125. * the Registry.
  126. */
  127. if ( !pUserName ) {
  128. Print(IDS_CONVERTING, UserNameTemp );
  129. UCLength = sizeof( UserConfig );
  130. Error = RegUserConfigQuery( ServerName,
  131. UserNameTemp,
  132. &UserConfig,
  133. sizeof( UserConfig ),
  134. &UCLength );
  135. if ( Error != ERROR_SUCCESS ) {
  136. Print( IDS_ERR_QUERY_CONFIG, Error );
  137. break;
  138. }
  139. /*
  140. * Store the configuration in the SAM.
  141. */
  142. Error = RegUserConfigSet( ServerName,
  143. UserNameTemp,
  144. &UserConfig,
  145. sizeof(UserConfig) );
  146. if ( Error != ERROR_SUCCESS ) {
  147. Print( IDS_ERR_SET_CONFIG,
  148. Error );
  149. } else {
  150. Print( IDS_COMPLETE );
  151. }
  152. } else if ( !wcscmp( pUserName, UserNameTemp ) ) {
  153. Print( IDS_CONVERTING, UserNameTemp );
  154. fFound = TRUE;
  155. UCLength = sizeof( UserConfig );
  156. Error = RegUserConfigQuery( ServerName,
  157. UserNameTemp,
  158. &UserConfig,
  159. sizeof( UserConfig ),
  160. &UCLength );
  161. if ( Error != ERROR_SUCCESS ) {
  162. Print( IDS_ERR_QUERY_CONFIG2,
  163. Error );
  164. break;
  165. }
  166. /*
  167. * Store the configuration in the SAM.
  168. */
  169. Error = RegUserConfigSet( ServerName,
  170. UserNameTemp,
  171. &UserConfig,
  172. sizeof(UserConfig) );
  173. if ( Error != ERROR_SUCCESS ) {
  174. Print( IDS_ERR_SET_CONFIG,
  175. Error );
  176. } else {
  177. Print(IDS_COMPLETE);
  178. }
  179. break;
  180. }
  181. }
  182. /*
  183. * If a name was given and it wasn't found, then say so.
  184. */
  185. if ( !Error && !fFound && pUserName ) {
  186. ErrorPrintf(IDS_ERR_USER_NOT_FOUND, pUserName );
  187. }
  188. /*
  189. * Close UserConfiguration key
  190. */
  191. RegCloseKey( UCHandle );
  192. cleanupregconnect:
  193. /*
  194. * Close connection to Registry on PDC
  195. */
  196. RegCloseKey( ServerHandle );
  197. exit:
  198. return( Error == ERROR_SUCCESS );
  199. }
  200. __cdecl
  201. main( int argc, char *argv[] )
  202. {
  203. if ( !ProcessCommandLine( argc, argv ) ) {
  204. return( 1 );
  205. }
  206. if ( !ConvertUserConfiguration( fAll ? NULL : UserName,
  207. DomainName ) ) {
  208. return( 1 );
  209. }
  210. }
  211. /*******************************************************************************
  212. *
  213. * print
  214. * Display a message to stdout with variable arguments. Message
  215. * format string comes from the application resources.
  216. *
  217. * ENTRY:
  218. * nResourceID (input)
  219. * Resource ID of the format string to use in the message.
  220. * ... (input)
  221. * Optional additional arguments to be used with format string.
  222. *
  223. * EXIT:
  224. *
  225. ******************************************************************************/
  226. void
  227. Print( int nResourceID, ... )
  228. {
  229. char sz1[256], sz2[512];
  230. va_list args;
  231. va_start( args, nResourceID );
  232. if ( LoadStringA( NULL, nResourceID, sz1, 256 ) ) {
  233. vsprintf( sz2, sz1, args );
  234. printf( sz2 );
  235. }
  236. va_end(args);
  237. }