//----------------------------------------------------------------------------
//
// Copyright (c) Intel Corporation, 2011 - 2015 All Rights Reserved.
//
// File: AgentPresenceSample.cs
//
// Contents: Sample code for an Intel (R) Active Management Technology (Intel® AMT)
// AgentPresence Sample.
//
// Notes: This file demonstrates the usage of Intel® AMT Agent Presence
// using WS-Management interface.
// Agent Presence includes the ability for the Management Sub-System
// to monitor the presence of SW-based agents (e.g. Anti-Virus, etc.)
// running in the context of the OS, and take specific action, if a
// SW-based agent is no longer present.
//
//----------------------------------------------------------------------------
using System;
using Utils;
using Intel.Management.Wsman;
using Common.Utils;
using System.Runtime.InteropServices;
namespace AgentPresence
{
class AgentPresence_Sample
{
#region CONSTANTS
private const ushort MAX_AGENTS = 16;
// Exit Codes Types
private enum exitCodes
{
EXIT_SUCCESS = 0,
EXIT_FAILURE,
EXIT_USAGE,
EXIT_COMMUNICATION_ERROR,
EXIT_ARGUMENT_ERROR,
}
#endregion CONSTANTS
#region PRIVATE_DATA_MEMBERS
// User parameters
private static CmdLineArguments Params = new CmdLineArguments();
#endregion PRIVATE_DATA_MEMBERS
#region PUBLIC_FUNCTIONS
///
/// Cleans the class instances created by the sample.
///
/// The Sample's general class
/// Instance of the created AMT_SystemDefensePolicy class
/// Instance of the created AMT_AgentPresenceInterfacePolicy class
/// Instance of the created AMT_AgentPresenceWatchdog class
public static void CleanAMT(AgentPresence_Api service, IManagedReference policy, IManagedReference iPolicy,
IManagedInstance agent)
{
try
{
Console.WriteLine("Cleaning... ");
if (null != service)
{
if (null != agent)
{
service.DeleteAgent();
}
if (null != iPolicy)
{
service.RemoveAgentPresencePolicy(Params.Selected(CmdLineArguments.OPT_WIRELESS));
}
if (null != policy)
{
service.DeletePolicy();
}
}
Params.MessageDisplay_Color("The Intel AMT was cleaned successfully.", ConsoleColor.Green);
}
catch (Exception e1)
{
Console.WriteLine("{0} Exception caught during cleaning", e1.Message);
}
}
#endregion
#region MAIN
[DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetDefaultDllDirectories(int directoryFlags);
static int Main(string[] args)
{
// set default dll lookup directory to system
SetDefaultDllDirectories(0x00000800); //LOAD_LIBRARY_SEARCH_SYSTEM32
exitCodes exitCode = exitCodes.EXIT_SUCCESS;
AgentPresence_Api api = null;
IManagedInstance agent = null;
IManagedReference policy = null;
IManagedReference iPolicy = null;
#region INIT FUNCTIONS
// Add command line argument options.
Params.init_functions();
#endregion
string usage = String.Empty;
// Creates usage string
string assembly = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
// If no params were given, just print the usage
if (args.Length == 0)
{
usage = Params.CreateUsage(assembly, false, false, true);
Console.WriteLine("\n" + usage);
return (int)exitCodes.EXIT_SUCCESS;
}
try
{
// Verify command line arguments.
Params.Parse(args);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(Params.CreateUsage(assembly, false, false, true));
return 0;
}
try
{
bool verbose = Params.Selected(CmdLineArguments.OPT_VERBOSE);
// Create the WSMan Connection Object based on TLS/Non-TLS option.
// Convert password to secure string to comply with wsman dll which supports passwords in SecureString
// format only.
if (Params.Selected(CmdLineArguments.OPT_SECURE) == false)
{
api = new AgentPresence_Api(Params[CmdLineArguments.OPT_HOST], Params[CmdLineArguments.OPT_USER],
Params[CmdLineArguments.OPT_PASS].ConvertToSecureString(), Params.Selected(CmdLineArguments.OPT_KRB), Params.GetWebProxy(), Params.Selected(CmdLineArguments.ACCEPT_SELF_SIGNED_CERTIFICATE));
}
else
{
api = new AgentPresence_Api(Params[CmdLineArguments.OPT_HOST], Params[CmdLineArguments.OPT_USER],
Params[CmdLineArguments.OPT_PASS].ConvertToSecureString(), Params[CmdLineArguments.OPT_CERT],
Params.Selected(CmdLineArguments.OPT_KRB), Params.GetWebProxy(), Params.Selected(CmdLineArguments.ACCEPT_SELF_SIGNED_CERTIFICATE));
}
// Main flow.
// Queries Agent Presence capabilities.
api.QueryAgentPresenceCapabilities(Params.Selected(CmdLineArguments.OPT_VERBOSE));
int countAgents = api.GetAgentsCount();
// Enumerates the agents to validate that there is a place for one more agent.
if (countAgents < MAX_AGENTS)
{
// Creates a Watchdog Agent.
agent = api.CreateAgentWatchdog();
// Sets an Agent's actions.
api.SetActions();
}
else
{
Params.MessageDisplay_Color("Warning: There are already maximum agents in the Intel(R) AMT platform. Skipping the agent's creation", ConsoleColor.Red);
}
// Gets the sample agent and prints its properties.
api.DisplayAgents(Params.Selected(CmdLineArguments.OPT_VERBOSE));
// Creates a System Defense Policy.
policy = api.CreateSystemDefensePolicy(verbose);
// Creates an AMT_AgentPresenceInterfacePolicy for the LAN interface
// and sets the CB Policy to be activated by the Agent Presence.
iPolicy = api.SetInterfacePolicy(verbose, policy, Params.Selected(CmdLineArguments.OPT_WIRELESS));
}
catch (Exception excep)
{
// Check for the Type of Exception created during execution.
exitCode = (exitCodes)Params.catchType(excep, null);
}
finally
{
// Cleanup
if (exitCodes.EXIT_USAGE != exitCode)
{
CleanAMT(api, policy, iPolicy, agent);
}
api?.Dispose();
}
return (int)exitCode;
}
#endregion MAIN
}
}