//---------------------------------------------------------------------------- // // Copyright (c) Intel Corporation, 2009-2013 All Rights Reserved. // // File: Base.cs // // Contents: High Level API definition: Factory class // // Notes: // //---------------------------------------------------------------------------- using Intel.Manageability.Impl; using Intel.Manageability.Exceptions; using System; using System.Management; using System.Security; using static Common.Utils.SecureStringExtensions; namespace Intel.Manageability { /// /// This class creates the "AMT Instance" which is used to perform /// all the actions in the HLAPI. /// public static class AMTInstanceFactory { /// /// This is the default way to create an AMT Instance object, where all /// that is passed is the connection information and the library chooses /// the transport mechanism. /// /// Connection information for the host. /// Returns the new AMT Instance. public static IAMTInstance CreateEX(ConnectionInfoEX info) { AMTInstanceManager amtInstance; try { amtInstance = new AMTInstanceManager(info, false); } catch (Exception e) { if (e.Message.Contains("404")) throw new ManageabilityException("Unable to connect: check if the machine exist " + e.Message, e); else throw new ManageabilityException(e.Message, e); } //if (amtInstance.MajorVersion == 3 && amtInstance.MinorVersion == 0) // throw new ManageabilityException("the HLAPI does not support AMT release 3.0 or earlier releases"); return amtInstance; } // The overloading of the Create function is maintained // for backward compatibility. // The recommended way is to use the ConnectionInfoEX structure. /// /// Note: Create Method with ConnectionInfo has been deprecated. Use CreateEX instead. /// This is the default way to create an AMT Instance object, where all /// that is passed is the connection information and the library chooses /// the transport mechanism. /// /// Connection information for the host. /// Returns the new AMT Instance. [Obsolete("Create method with ConnectionInfo has been deprecated. Use CreateEx instead.", false)] public static IAMTInstance Create(ConnectionInfo info) { return Create(info, ManagedSystem.TRANSPORT_WS_MAN, null); } /// /// Note: Create Method with ConnectionInfo has been deprecated. Use CreateEX instead. /// Create a connection that uses a certificate with a password. /// The default transport is used. /// /// Connection information for the host. /// The password to use with the certificate. /// Returns the new AMT Instance. [Obsolete("Create method with ConnectionInfo class has been deprecated. Use CreateEx instead.", false)] public static IAMTInstance CreateWithCertificatePassword(ConnectionInfo info, string certPassword) { return Create(info, ManagedSystem.TRANSPORT_WS_MAN, certPassword); } /// /// Note: Create Method with ConnectionInfo has been deprecated. Use CreateEX instead. /// Create a connection that uses a specified transport. /// /// Connection information for the host. /// The transport to use. Currently only /// ManagedSystem.TRANSPORT_WS_MAN is supported. /// Returns the new AMT Instance. [Obsolete("Create method with ConnectionInfo class has been deprecated. Use CreateEx instead.", false)] public static IAMTInstance Create(ConnectionInfo info, string transport) { return Create(info, transport, null); } /// /// Note: Create Method with ConnectionInfo has been deprecated. Use CreateEX instead. /// Create a connection using the specified transport and the specified /// certificate password. /// /// Connection information for the host. /// The transport to use. Currently only /// ManagedSystem.TRANSPORT_WS_MAN is supported. /// The password to use with the certificate. /// Returns the new AMT Instance. [Obsolete("Create method with ConnectionInfo class has been deprecated. Use CreateEx instead.", false)] public static IAMTInstance Create(ConnectionInfo info, string transport, string certPassword) { AMTInstanceManager amtInstance; try { using (ManagedSystem ms = new ManagedSystem(info, transport)) { amtInstance = new AMTInstanceManager(ms, certPassword, false); } } catch (Exception e) { if (e.Message.Contains("404")) throw new ManageabilityException("Unable to connect: check if the machine exist " + e.Message, e); else throw new ManageabilityException(e.Message, e); } //if (amtInstance.MajorVersion == 3 && amtInstance.MinorVersion == 0) // throw new ManageabilityException("the HLAPI does not support AMT release 3.0 or earlier releases"); return amtInstance; } /// /// This constructor is used for Host Based Setup configuration. /// /// Returns the new AMT Instance. public static IAMTInstance Create() { // Get the OsAdmin credenials prior to the connection string userName; SecureString password; GetOsAdminCridentials(out userName, out password); ConnectionInfoEX info = new ConnectionInfoEX("127.0.0.1", userName, password, false, string.Empty, ConnectionInfoEX.AuthMethod.Digest, null, null, null, false); //ManagedSystem ms = new ManagedSystem(info, ManagedSystem.TRANSPORT_WS_MAN); return new AMTInstanceManager(info, true); } private static void GetOsAdminCridentials(out string username, out SecureString password) { ManagementClass oob_service = null; try { // Get the OOB_Service object on which the method will be invoked using (oob_service = new ManagementClass(@"ROOT\Intel_ME:OOB_Service")) { // Execute the method ManagementBaseObject outParam = oob_service.InvokeMethod("GetLocalAdminCredentials", null, null); if (outParam != null) { // The return value of the function (Username: $$OsAdmin, Password: random password) username = outParam["Username"].ToString(); password = outParam["Password"].ToString().ConvertToSecureString(); } else { throw new ManageabilityException("OsAdmin credentials not found. Please check that you are running the sample from local and WMI provider is installed."); } } } catch { throw new ManageabilityException("OsAdmin credentials not found. Please check that you are running the sample from local and WMI provider is installed."); } } } }