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.

310 lines
8.2 KiB

  1. using System;
  2. using System.Security.Principal;
  3. using System.Threading;
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;
  6. using UDDI;
  7. using UDDI.Replication;
  8. namespace UDDI.Tools
  9. {
  10. [ StructLayout( LayoutKind.Sequential ) ]
  11. internal class SECURITY_ATTRIBUTES
  12. {
  13. public int nLength;
  14. public object lpSecurityDescriptor;
  15. public bool bInheritHandle;
  16. public SECURITY_ATTRIBUTES()
  17. {
  18. nLength = Marshal.SizeOf( typeof( SECURITY_ATTRIBUTES ) );
  19. lpSecurityDescriptor = null;
  20. bInheritHandle = false;
  21. }
  22. }
  23. internal enum SystemErrorCodes
  24. {
  25. ERROR_SUCCESS = 0,
  26. ERROR_ALREADY_EXISTS = 183
  27. }
  28. //
  29. // TODO add more values as we need them
  30. //
  31. internal enum FileHandleValues
  32. {
  33. INVALID_HANDLE_VALUE = -1
  34. }
  35. //
  36. // TODO add more values as we need them
  37. //
  38. internal enum SharedFileProtection : byte
  39. {
  40. PAGE_READONLY = 0x02
  41. }
  42. internal class SharedMemory
  43. {
  44. int hSharedMemory;
  45. const int INVALID_HANDLE_VALUE = -1;
  46. public bool Create( string name )
  47. {
  48. hSharedMemory = -1;
  49. bool success = false;
  50. try
  51. {
  52. SECURITY_ATTRIBUTES securityAttributes = new SECURITY_ATTRIBUTES();
  53. hSharedMemory = CreateFileMapping( ( int )FileHandleValues.INVALID_HANDLE_VALUE,
  54. securityAttributes,
  55. ( int )SharedFileProtection.PAGE_READONLY,
  56. 0,
  57. 1,
  58. name );
  59. if( ( int )SystemErrorCodes.ERROR_SUCCESS == GetLastError() )
  60. {
  61. success = true;
  62. }
  63. }
  64. catch
  65. {
  66. if( -1 != hSharedMemory )
  67. {
  68. CloseHandle( hSharedMemory );
  69. }
  70. }
  71. return success;
  72. }
  73. public void Release()
  74. {
  75. if( -1 != hSharedMemory )
  76. {
  77. CloseHandle( hSharedMemory );
  78. }
  79. }
  80. [DllImport( "user32.dll", CharSet=CharSet.Auto )]
  81. private static extern int MessageBox(int hWnd, String text, String caption, uint type);
  82. [DllImport( "kernel32.dll", SetLastError=true )]
  83. private static extern int CreateFileMapping( int hFile,
  84. SECURITY_ATTRIBUTES lpAttributes,
  85. int flProtect,
  86. int dwMaximumSizeHigh,
  87. int dwMaximumSizeLow,
  88. string lpName );
  89. [DllImport( "kernel32.dll" )]
  90. private static extern bool CloseHandle( int hObject );
  91. [DllImport( "kernel32.dll" )]
  92. private static extern int GetLastError();
  93. }
  94. public class ReplicationUtility
  95. {
  96. public static string OperatorKey = null;
  97. /// ****************************************************************
  98. /// public Main [static]
  99. /// ----------------------------------------------------------------
  100. /// <summary>
  101. /// Program entry point.
  102. /// </summary>
  103. /// ----------------------------------------------------------------
  104. /// <param name="args">
  105. /// Command-line arguments.
  106. /// </param>
  107. /// ****************************************************************
  108. ///
  109. public static void Main( string[] args )
  110. {
  111. //
  112. // Use shared memory to make sure that only 1 instance of this process is running. sharedMemory.Release() MUST
  113. // be called when this process exits in order to free up the shared memory.
  114. //
  115. SharedMemory sharedMemory = new SharedMemory();
  116. try
  117. {
  118. Console.WriteLine( "Microsoft (R) UDDI Replication Utility" );
  119. Console.WriteLine( "Copyright (C) Microsoft Corp. 2002. All rights reserved." );
  120. Console.WriteLine();
  121. if( false == sharedMemory.Create( "UDDI_replication_process" ) )
  122. {
  123. Console.WriteLine( "Only 1 instance of this process can be running." );
  124. System.Environment.Exit( 1 );
  125. }
  126. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  127. WindowsPrincipal principal = new WindowsPrincipal( identity );
  128. Context.User.SetRole( principal );
  129. if( !Context.User.IsAdministrator )
  130. {
  131. Console.WriteLine( "Access denied.\r\n\r\nThis program must be executed by a member of the '"
  132. + Config.GetString( "GroupName.Administrators" ) + "'\r\ngroup. The current user '"
  133. + identity.Name + "' is not a member of this group." );
  134. return;
  135. }
  136. ProcessCommandLine( args );
  137. ConnectionManager.Open( true, false );
  138. try
  139. {
  140. if( null == OperatorKey )
  141. ReplicationHelper.Replicate();
  142. else
  143. ReplicationHelper.ReplicateWithNode( OperatorKey );
  144. }
  145. finally
  146. {
  147. ConnectionManager.Close();
  148. }
  149. }
  150. catch( CommandLineException e )
  151. {
  152. if( null != e.Message && e.Message.Length > 0 )
  153. Console.WriteLine( e.Message );
  154. else
  155. DisplayUsage();
  156. }
  157. catch( Exception e )
  158. {
  159. Console.WriteLine( e.ToString() );
  160. }
  161. finally
  162. {
  163. sharedMemory.Release();
  164. }
  165. }
  166. /// ****************************************************************
  167. /// internal ProcessCommandLine [static]
  168. /// ----------------------------------------------------------------
  169. /// <summary>
  170. /// Parse the command-line.
  171. /// </summary>
  172. /// ----------------------------------------------------------------
  173. /// <param name="args">
  174. /// Command-line arguments.
  175. /// </param>
  176. /// ****************************************************************
  177. internal static void ProcessCommandLine( string[] args )
  178. {
  179. int i = 0;
  180. while( i < args.Length )
  181. {
  182. if( '-' == args[ i ][ 0 ] || '/' == args[ i ][ 0 ] )
  183. {
  184. //
  185. // Process the switch.
  186. //
  187. switch( args[ i ].Substring( 1 ).ToLower() )
  188. {
  189. case "o":
  190. if( i + 1 >= args.Length )
  191. throw new CommandLineException( "Missing required parameter 'operatorkey'" );
  192. i ++;
  193. try
  194. {
  195. OperatorKey = new Guid( args[ i ] ).ToString();
  196. }
  197. catch
  198. {
  199. throw new CommandLineException( "Invalid operator key specified." );
  200. }
  201. break;
  202. case "?":
  203. goto case "help";
  204. case "help":
  205. throw new CommandLineException( "" );
  206. default:
  207. throw new CommandLineException( "Unknown switch '" + args[i] + "'." );
  208. }
  209. }
  210. i ++;
  211. }
  212. }
  213. public static void DisplayUsage()
  214. {
  215. Console.WriteLine( "Syntax:" );
  216. Console.WriteLine( " replicate.exe <switches>" );
  217. Console.WriteLine();
  218. Console.WriteLine( "Switches:" );
  219. Console.WriteLine( " -o <operatorkey> Replicates against the specified" );
  220. Console.WriteLine( " operator only." );
  221. Console.WriteLine( " -? Displays this help message." );
  222. Console.WriteLine();
  223. Console.WriteLine( "Examples:" );
  224. Console.WriteLine( " replicate.exe" );
  225. Console.WriteLine( " replicate.exe -o F6D80408-A206-4b85-B2F4-699EFA13A669" );
  226. Console.WriteLine();
  227. }
  228. }
  229. /// ****************************************************************
  230. /// public class CommandLineException
  231. /// ----------------------------------------------------------------
  232. /// <summary>
  233. /// Exception class for errors encountered while parsing the
  234. /// command-line.
  235. /// </summary>
  236. /// ****************************************************************
  237. ///
  238. public class CommandLineException : ApplicationException
  239. {
  240. /// ************************************************************
  241. /// public CommandLineException [constructor]
  242. /// ------------------------------------------------------------
  243. /// <summary>
  244. /// CommandLineException constructor.
  245. /// </summary>
  246. /// ************************************************************
  247. ///
  248. public CommandLineException()
  249. : base()
  250. {
  251. }
  252. /// ************************************************************
  253. /// public CommandLineException [constructor]
  254. /// ------------------------------------------------------------
  255. /// <summary>
  256. /// CommandLineException constructor.
  257. /// </summary>
  258. /// ------------------------------------------------------------
  259. /// <param name="message">
  260. /// Exception message.
  261. /// </param>
  262. /// ************************************************************
  263. ///
  264. public CommandLineException( string message )
  265. : base( message )
  266. {
  267. }
  268. }
  269. }