203 lines
7.9 KiB
C#

//----------------------------------------------------------------------------
//
// Copyright (C) Intel Corporation, 2011 - 2015 All Rights Reserved.
//
// File: UserConsentSample.cs
//
// Contents: Sample code for Intel(R) Active Management Technology
// (Intel® AMT) User Consent.
//
// Notes: This sample application demonstrates various commands of user
// consent flow for Intel AMT 6.1 and above FW versions.
//
//----------------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
using Connection;
using Utils;
namespace UserConsent
{
class UserConsentSample
{
#region CONSTANTS
// Function options
private const string GET_USER_CONSENT_STATUS = "status";
private const string START_USER_CONSENT = "start";
private const string CLOSE_USER_CONSENT = "cancel";
private const string SEND_CONSENT_CODE = "sendcode";
private const string FULL_USER_CONSENT_FLOW = "flow";
private const string SET_MONITOR = "setmonitor";
private const string GET_MONITOR = "getmonitor";
// Function options descriptions
private const string GET_USER_CONSENT_STATUS_DESCRIPTION = "Display the user consent required and state properties.";
private const string START_USER_CONSENT_DESCRIPTION = "Try to start a user consent pop up at the host.";
private const string CLOSE_USER_CONSENT_DESCRIPTION = "Cancel User Consent.";
private const string SEND_CONSENT_CODE_DESCRIPTION = "Send Consent Code.";
private const string FULL_USER_CONSENT_FLOW_DESCRIPTION = "Try to perform a full user consent flow.";
private const string SET_MONITOR_DESCRIPTION = "Change the default monitor.";
private const string GET_MONITOR_DESCRIPTION = "Display the current default monitor.";
// Exit Codes Types
private enum exitCodes
{
EXIT_SUCCESS = 0,
EXIT_FAILURE,
EXIT_USAGE,
EXIT_COMMUNICATION_ERROR,
EXIT_ARGUMENT_ERROR,
}
#endregion CONSTANTS
#region MAIN
[DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetDefaultDllDirectories(int directoryFlags);
/// <summary>
/// Start of Execution from MAIN Function.
/// </summary>
static int Main(string[] args)
{
// set default dll lookup directory to system
SetDefaultDllDirectories(0x00000800); //LOAD_LIBRARY_SEARCH_SYSTEM32
exitCodes exitCode = exitCodes.EXIT_SUCCESS;
UserConsentApi api = null;
#region INIT_COMMAND_LINE_ARGUMENTS
CmdLineArguments Params = new CmdLineArguments();
// Add command line argument options.
Params.init_functions();
// Add options to activate
Params.AddArg(FULL_USER_CONSENT_FLOW, false, false, FULL_USER_CONSENT_FLOW_DESCRIPTION);
Params.AddArg(GET_USER_CONSENT_STATUS, false, false, GET_USER_CONSENT_STATUS_DESCRIPTION);
Params.AddArg(START_USER_CONSENT, false, false, START_USER_CONSENT_DESCRIPTION);
Params.AddArg(SEND_CONSENT_CODE, false, false, SEND_CONSENT_CODE_DESCRIPTION);
Params.AddArg(CLOSE_USER_CONSENT, false, false, CLOSE_USER_CONSENT_DESCRIPTION);
Params.AddArg(GET_MONITOR, false, false, GET_MONITOR_DESCRIPTION);
Params.AddArg(SET_MONITOR, false, false, SET_MONITOR_DESCRIPTION);
#endregion
try
{
string usage = string.Empty;
string assembly = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
// If no params were given, just print the usage.
if (args.Length == 0)
{
Params.MessageDisplay_Color("This feature is supported from Intel AMT FW version 6.1 and above.\n",
ConsoleColor.Yellow);
// Creates usage string which describes how to use.
usage = Params.CreateUsage(assembly, false);
Console.WriteLine(usage);
return (int)exitCodes.EXIT_SUCCESS;
}
try
{
// Verify command line arguments.
Params.Parse(args);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Params.MessageDisplay_Color("This feature is supported from Intel AMT FW version 6.1 and above.\n",
ConsoleColor.Yellow);
usage = Params.CreateUsage(assembly, false);
Console.WriteLine(usage);
return 0;
}
//Create the WSMan Connection Object based on TLS/Non-TLS option.
if (Params.Selected(CmdLineArguments.OPT_SECURE) == false)
{
api = new UserConsentApi(Params[CmdLineArguments.OPT_HOST], Params[CmdLineArguments.OPT_USER],
Params[CmdLineArguments.OPT_PASS], Params.Selected(CmdLineArguments.OPT_KRB),
Params.GetWebProxy(),
Params.Selected(CmdLineArguments.ACCEPT_SELF_SIGNED_CERTIFICATE));
}
else
{
api = new UserConsentApi(Params[CmdLineArguments.OPT_HOST], Params[CmdLineArguments.OPT_USER],
Params[CmdLineArguments.OPT_PASS], Params[CmdLineArguments.OPT_CERT],
Params.Selected(CmdLineArguments.OPT_KRB), Params.GetWebProxy(),
Params.Selected(CmdLineArguments.ACCEPT_SELF_SIGNED_CERTIFICATE));
}
if (UtilitiesMethods.CompareVersions(UtilitiesMethods.GetCoreVersion(api.wsmanClient), "6.1") < 0)
throw new Exception("User consent is not supported on this Intel(r) AMT release.");
//Check for only 1 parameter.
switch (args[0].Trim('-'))
{
// Call Consent Status.
case GET_USER_CONSENT_STATUS:
api.DisplayUserConsentStatus();
break;
// Call Start User Consent.
case START_USER_CONSENT:
api.StartUserConsent();
break;
// Call Send Consent Code.
case SEND_CONSENT_CODE:
api.SendConsentCode();
break;
//Call Close User Consent.
case CLOSE_USER_CONSENT:
api.StopUserConsent();
break;
//Call Set Monitor.
case SET_MONITOR:
api.SetDefaultMonitor();
break;
//Call Set Monitor.
case GET_MONITOR:
api.GetDefaultMonitor();
break;
//Call run full user consent flow.
case FULL_USER_CONSENT_FLOW:
api.RunFullUserConsentFlow();
break;
//Default, check the next arg value.
default:
Console.WriteLine("Invalid Parameter specified");
break;
}
}
catch (Exception e)
{
//Check for the Type of Exception created during execution.
exitCode = (exitCodes)Params.catchType(e, null);
}
finally
{
api?.Dispose();
}
return (int)exitCode;
}
#endregion MAIN
}
}