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.

320 lines
7.5 KiB

  1. using System;
  2. using System.Globalization;
  3. using System.Security.Cryptography;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using Microsoft.Win32;
  7. using System.Resources;
  8. namespace UDDI.Tools
  9. {
  10. class Resetkey
  11. {
  12. static bool resetnow = false;
  13. static string key;
  14. static string iv;
  15. static DateTime dt = DateTime.Now;
  16. static SqlConnection connection;
  17. static SqlTransaction transaction;
  18. static int Main( string[] args )
  19. {
  20. int rc = 0; // assume success
  21. try
  22. {
  23. //
  24. // Check if CurrentUICulture needs to be overridden
  25. //
  26. UDDI.Localization.SetConsoleUICulture();
  27. DisplayBanner();
  28. //
  29. // Parse the command line
  30. //
  31. if( !ProcessCommandLine( args ) )
  32. {
  33. return 1;
  34. }
  35. //
  36. // Generate key and initialization vector
  37. //
  38. SymmetricAlgorithm sa = SymmetricAlgorithm.Create();
  39. sa.GenerateKey();
  40. key = Convert.ToBase64String( sa.Key );
  41. sa.GenerateIV();
  42. iv = Convert.ToBase64String( sa.IV );
  43. //
  44. // Save config information
  45. //
  46. if( resetnow )
  47. {
  48. ResetKeysNow();
  49. }
  50. else
  51. {
  52. ResetKeysScheduled();
  53. }
  54. }
  55. catch( Exception e )
  56. {
  57. Console.WriteLine( FormatFromResource( "RESETKEY_FAILED" , e.Message ) );
  58. rc = 1;
  59. }
  60. return rc;
  61. }
  62. private static bool ProcessCommandLine( string [] args )
  63. {
  64. bool bOK = false;
  65. if ( args.Length > 0 )
  66. {
  67. for( int i = 0; i < args.Length; i ++ )
  68. {
  69. if( '-' == args[i][0] || '/' == args[i][0] )
  70. {
  71. string option = args[i].Substring( 1 );
  72. if( "help" == option.ToLower() || "?" == option )
  73. {
  74. DisplayUsage();
  75. return false;
  76. }
  77. if( "now" == option.ToLower() )
  78. {
  79. i++; // move to the next arg
  80. resetnow = true;
  81. bOK = true;
  82. }
  83. }
  84. }
  85. }
  86. else
  87. bOK = true;
  88. if( !bOK )
  89. {
  90. DisplayUsage();
  91. return false;
  92. }
  93. return true;
  94. }
  95. static void DisplayBanner()
  96. {
  97. Console.WriteLine( FormatFromResource( "RESETKEY_COPYRIGHT_1" ) );
  98. Console.WriteLine( FormatFromResource( "RESETKEY_COPYRIGHT_2" ) );
  99. Console.WriteLine();
  100. }
  101. static void DisplayUsage()
  102. {
  103. Console.WriteLine( FormatFromResource( "RESETKEY_USAGE_1" ) );
  104. Console.WriteLine( FormatFromResource( "RESETKEY_USAGE_2" ) );
  105. Console.WriteLine( FormatFromResource( "RESETKEY_USAGE_3" ) );
  106. Console.WriteLine();
  107. }
  108. static void OpenConnection()
  109. {
  110. try
  111. {
  112. string connectionectionString = (string) Registry.LocalMachine.OpenSubKey( @"SOFTWARE\Microsoft\UDDI\Database" ).GetValue( "WriterConnectionString" );
  113. connection = new SqlConnection( connectionectionString );
  114. connection.Open();
  115. transaction = connection.BeginTransaction( IsolationLevel.ReadCommitted, "resetkey" );
  116. }
  117. catch
  118. {
  119. throw new Exception( "Unable to connect to the database" );
  120. }
  121. }
  122. static void CloseConnection()
  123. {
  124. transaction.Commit();
  125. connection.Close();
  126. }
  127. static void SaveConfig(string configname, string configvalue)
  128. {
  129. //
  130. // Save configuration info
  131. //
  132. SqlCommand cmd = new SqlCommand( "net_config_save", connection, transaction );
  133. cmd.CommandType = CommandType.StoredProcedure;
  134. cmd.Parameters.Add( new SqlParameter( "@configName", SqlDbType.NVarChar, UDDI.Constants.Lengths.ConfigName ) ).Direction = ParameterDirection.Input;
  135. cmd.Parameters[ "@configName" ].Value = configname;
  136. cmd.Parameters.Add( new SqlParameter( "@configValue", SqlDbType.NVarChar, UDDI.Constants.Lengths.ConfigValue ) ).Direction = ParameterDirection.Input;
  137. cmd.Parameters[ "@configValue" ].Value = configvalue;
  138. cmd.ExecuteNonQuery();
  139. }
  140. static void ResetKeysNow()
  141. {
  142. OpenConnection();
  143. try
  144. {
  145. //
  146. // 739955 - Make sure date is parsed in the same format it was written.
  147. //
  148. UDDILastResetDate.Set( dt );
  149. SaveConfig( "Security.Key", key );
  150. SaveConfig( "Security.IV", iv );
  151. Console.WriteLine( FormatFromResource( "RESETKEY_SUCCEEDED" ) );
  152. }
  153. finally
  154. {
  155. CloseConnection();
  156. }
  157. return;
  158. }
  159. static void ResetKeysScheduled()
  160. {
  161. OpenConnection();
  162. try
  163. {
  164. //
  165. // Get config values
  166. //
  167. SqlCommand cmd = new SqlCommand( "net_config_get", connection, transaction );
  168. SqlDataReader rdr = cmd.ExecuteReader( CommandBehavior.SingleResult );
  169. //
  170. // Iterate through results and populate variables
  171. //
  172. string configname;
  173. string configvalue;
  174. int timeoutdays = 0;
  175. DateTime olddt = DateTime.Now;
  176. int autoreset = 1;
  177. while( rdr.Read() )
  178. {
  179. configname = "";
  180. configvalue = "";
  181. if( !rdr.IsDBNull( 0 ) )
  182. configname = rdr.GetString(0);
  183. if (!rdr.IsDBNull( 1 ))
  184. configvalue = rdr.GetString(1);
  185. //
  186. // TODO: Use ToInt32 here please
  187. //
  188. switch( configname )
  189. {
  190. case "Security.KeyTimeout":
  191. timeoutdays = Convert.ToInt16( configvalue );
  192. break;
  193. case "Security.KeyLastResetDate":
  194. {
  195. //
  196. // 739955 - Make sure date is parsed in the same format it was written.
  197. //
  198. olddt = UDDILastResetDate.Get();
  199. break;
  200. }
  201. case "Security.KeyAutoReset":
  202. autoreset = Convert.ToInt16 ( configvalue );
  203. break;
  204. }
  205. }
  206. rdr.Close();
  207. Console.WriteLine( FormatFromResource( "RESETKEY_EXISTING_SETTINGS" ) );
  208. Console.WriteLine( "Security.KeyAutoReset = " + autoreset.ToString() );
  209. Console.WriteLine( "Security.KeyTimeout = " + timeoutdays.ToString() );
  210. //
  211. // 661537 - Output the date in the correct format for the user.
  212. //
  213. Console.WriteLine( "Security.KeyLastResetDate = " + olddt.ToShortDateString() + " " + olddt.ToShortTimeString() + "\n" );
  214. //
  215. // Check Security.KeyAutoReset
  216. //
  217. if( 1 != autoreset )
  218. {
  219. Console.WriteLine( FormatFromResource( "RESETKEY_AUTO_RESET_1" ) );
  220. Console.WriteLine( FormatFromResource( "RESETKEY_AUTO_RESET_2" ) );
  221. return;
  222. }
  223. //
  224. // Check dates to determine if key has expired
  225. //
  226. DateTime expiration = olddt.AddDays( timeoutdays );
  227. if( dt <= expiration )
  228. {
  229. //
  230. // 661537 - Output the date in the correct format for the user.
  231. //
  232. Console.WriteLine( FormatFromResource( "RESETKEY_KEY_EXPIRE_NOTE_1", expiration.ToShortDateString() + " " + expiration.ToShortTimeString() ) );
  233. Console.WriteLine( FormatFromResource( "RESETKEY_KEY_EXPIRE_NOTE_2" ) );
  234. return;
  235. }
  236. //
  237. // Write config values
  238. //
  239. //
  240. // 739955 - Make sure date is parsed in the same format it was written.
  241. //
  242. UDDILastResetDate.Set( dt );
  243. SaveConfig( "Security.Key", key );
  244. SaveConfig( "Security.IV", iv );
  245. Console.WriteLine( FormatFromResource( "RESETKEY_SUCCEEDED" ) );
  246. }
  247. finally
  248. {
  249. CloseConnection();
  250. }
  251. return;
  252. }
  253. static string FormatFromResource( string resID, params object[] inserts )
  254. {
  255. try
  256. {
  257. string resourceStr = UDDI.Localization.GetString( resID );
  258. if( null != resourceStr )
  259. {
  260. string resultStr = string.Format( resourceStr, inserts );
  261. return resultStr;
  262. }
  263. return "String not specified in the resources: " + resID;
  264. }
  265. catch( Exception e )
  266. {
  267. return "FormatFromResource failed to load the resource string for ID: " + resID + " Reason: " + e.Message;
  268. }
  269. }
  270. }
  271. }