191 lines
6.8 KiB
C#
191 lines
6.8 KiB
C#
//----------------------------------------------------------------------------
|
|
//
|
|
// Copyright (c) Intel Corporation, 2011 - 2014 All Rights Reserved.
|
|
//
|
|
// File: SecurityAdminSample.cs
|
|
//
|
|
// Contents: Sample code for an Intel(R) Active Management Technology
|
|
// (Intel® AMT) Security Administration.
|
|
//
|
|
// Notes: This file demonstrates the usage of Intel® AMT Security
|
|
// Administration using WS-Management interface.
|
|
// This file contains routines to manipulate Security Administration
|
|
// capabilities of the Intel® AMT by the WS-Management interface.
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
|
|
using System;
|
|
using System.Runtime.InteropServices;
|
|
using Utils;
|
|
|
|
namespace SecurityAdmin
|
|
{
|
|
class SecurityAdminSample
|
|
{
|
|
#region CONSTANTS
|
|
|
|
// Command line flags
|
|
private const string OPT_ADMIN_ACL = "adminacl";
|
|
private const string OPT_USER_ACL = "useracl";
|
|
private const string OPT_VERSION = "version";
|
|
private const string OPT_API_TEST = "api";
|
|
|
|
// Command line descriptions
|
|
private const string DESCRIPTION_ADMIN_ACL = "Manipulate Admin Acl Entry";
|
|
private const string DESCRIPTION_USER_ACL = "Manipulate User Acl Entries";
|
|
private const string DESCRIPTION_VERSION = "Get Core Version";
|
|
private const string DESCRIPTION_API_TEST = "Runs an API test";
|
|
|
|
// 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 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;
|
|
string usage = String.Empty; // Describes how to use.
|
|
SecurityAdminApi api = null;
|
|
|
|
int i = 0;
|
|
int flag = 0;
|
|
|
|
#region INIT_COMMAND_LINE_ARGUMENTS
|
|
|
|
// Add command line argument options.
|
|
Params.init_functions();
|
|
|
|
//Add options to activate
|
|
// Add command line argument options.
|
|
|
|
Params.AddArg(OPT_ADMIN_ACL, false, false, DESCRIPTION_ADMIN_ACL);
|
|
Params.AddArg(OPT_USER_ACL, false, false, DESCRIPTION_USER_ACL);
|
|
Params.AddArg(OPT_API_TEST, false, false, DESCRIPTION_API_TEST);
|
|
Params.AddArg(OPT_VERSION, false, false, DESCRIPTION_VERSION);
|
|
|
|
#endregion
|
|
|
|
try
|
|
{
|
|
// Creates usage string.
|
|
string assembly = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
|
|
usage = string.Empty;
|
|
|
|
// If no params were given, just print the usage.
|
|
if (args.Length == 0)
|
|
{
|
|
usage = Params.CreateUsage(assembly, false);
|
|
Console.WriteLine("\n" + usage);
|
|
return (int)exitCodes.EXIT_SUCCESS;
|
|
}
|
|
|
|
try
|
|
{
|
|
// Verify command line arguments.
|
|
Params.Parse(args);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
usage = Params.CreateUsage(assembly, false);
|
|
Console.WriteLine(usage);
|
|
return 0;
|
|
|
|
}
|
|
bool krb = (Params.Selected(CmdLineArguments.OPT_KRB) == false) ? false : true;
|
|
bool verbose = Params.Selected(CmdLineArguments.OPT_VERBOSE);
|
|
//Create the WSMan Connection Object based on TLS/Non-TLS option.
|
|
if (Params.Selected(CmdLineArguments.OPT_SECURE) == false)
|
|
{
|
|
api = new SecurityAdminApi(Params[CmdLineArguments.OPT_HOST], Params[CmdLineArguments.OPT_USER], Params[CmdLineArguments.OPT_PASS], krb, Params.GetWebProxy(), Params.Selected(CmdLineArguments.ACCEPT_SELF_SIGNED_CERTIFICATE));
|
|
}
|
|
else
|
|
{
|
|
api = new SecurityAdminApi(Params[CmdLineArguments.OPT_HOST], Params[CmdLineArguments.OPT_USER], Params[CmdLineArguments.OPT_PASS], Params[CmdLineArguments.OPT_CERT], krb, Params.GetWebProxy(), Params.Selected(CmdLineArguments.ACCEPT_SELF_SIGNED_CERTIFICATE));//Params[OPT_HOST], Params[OPT_USER], Params[OPT_PASS]);
|
|
}
|
|
|
|
// Main flow.
|
|
//Parse though all the args passed in command line to call the requested function.
|
|
while (i < args.Length)
|
|
{
|
|
switch (args[i])
|
|
{
|
|
//Call RunAPI Test.
|
|
case "-" + OPT_API_TEST:
|
|
api.RunAPITest(verbose);
|
|
flag = 1;
|
|
break;
|
|
|
|
//Call RunAPI Test. AMT 8 and later.
|
|
case "-" + OPT_ADMIN_ACL:
|
|
api.ManipulateAdminSettings(verbose);
|
|
flag = 1;
|
|
break;
|
|
|
|
//Set SingleWake On.
|
|
case "-" + OPT_USER_ACL:
|
|
api.ManipulateUserSettings(verbose);
|
|
flag = 1;
|
|
break;
|
|
|
|
//Set SingleWake On. AMT 8 and later.
|
|
case "-" + OPT_VERSION:
|
|
api.GetCoreVersion(verbose);
|
|
flag = 1;
|
|
break;
|
|
|
|
//Default, check the next arg value.
|
|
default: i++;
|
|
flag = 0;
|
|
break;
|
|
}
|
|
//end switch
|
|
|
|
if (flag == 1)
|
|
break;
|
|
}
|
|
//end while
|
|
}
|
|
//end try
|
|
|
|
catch (Exception e)
|
|
{
|
|
//Check for the Type of Exception created during execution.
|
|
exitCode = (exitCodes)Params.catchType(e, null);
|
|
}
|
|
finally
|
|
{
|
|
api?.Dispose();
|
|
}
|
|
|
|
return (int)exitCode;
|
|
}
|
|
//end MAIN
|
|
|
|
#endregion MAIN
|
|
}
|
|
}
|