184 lines
8.1 KiB
C#
184 lines
8.1 KiB
C#
//----------------------------------------------------------------------------
|
|
//
|
|
// 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
|
|
{
|
|
/// <summary>
|
|
/// This class creates the "AMT Instance" which is used to perform
|
|
/// all the actions in the HLAPI.
|
|
/// </summary>
|
|
public static class AMTInstanceFactory
|
|
{
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
/// <param name="info">Connection information for the host.</param>
|
|
/// <returns>Returns the new AMT Instance.</returns>
|
|
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.
|
|
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
/// <param name="info">Connection information for the host.</param>
|
|
/// <returns>Returns the new AMT Instance.</returns>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
/// <param name="info">Connection information for the host.</param>
|
|
/// <param name="certPassword">The password to use with the certificate.</param>
|
|
/// <returns>Returns the new AMT Instance.</returns>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Note: Create Method with ConnectionInfo has been deprecated. Use CreateEX instead.
|
|
/// Create a connection that uses a specified transport.
|
|
/// </summary>
|
|
/// <param name="info">Connection information for the host.</param>
|
|
/// <param name="transport">The transport to use. Currently only
|
|
/// ManagedSystem.TRANSPORT_WS_MAN is supported.</param>
|
|
/// <returns>Returns the new AMT Instance.</returns>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Note: Create Method with ConnectionInfo has been deprecated. Use CreateEX instead.
|
|
/// Create a connection using the specified transport and the specified
|
|
/// certificate password.
|
|
/// </summary>
|
|
/// <param name="info">Connection information for the host.</param>
|
|
/// <param name="transport">The transport to use. Currently only
|
|
/// ManagedSystem.TRANSPORT_WS_MAN is supported.</param>
|
|
/// <param name="certPassword">The password to use with the certificate.</param>
|
|
/// <returns>Returns the new AMT Instance.</returns>
|
|
[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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// This constructor is used for Host Based Setup configuration.
|
|
/// </summary>
|
|
/// <returns>Returns the new AMT Instance.</returns>
|
|
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.");
|
|
}
|
|
}
|
|
}
|
|
}
|