316 lines
8.8 KiB
C++

//------------------------------------------------------------------
//
// Copyright (C) 2010 Intel Corporation
//
// File: HostBasedSetupSample.cpp
//
//------------------------------------------------------------------
#include "HostBasedSetupSample.h"
#include "HostBasedSetupTypedFlow.h"
#include "HostBasedSetupUntypedFlow.h"
#include "CommonDefinitions.h"
#include "Options.h"
#include "PTHICommand.h"
#include "CimOpenWsmanClient.h"
#include <iostream>
using namespace std;
using namespace Intel::Manageability::Exceptions;
/*
* This is a main entry point
*/
int main(UINT32 argc, char *argv[])
{
PT_STATUS status = PT_STATUS_SUCCESS;
HOST_BASED_SETUP_PARAMS params;
if (1 == argc)
{
// Running the sample with no parameters displays the usage
DisplayUsage();
return PT_STATUS_SUCCESS;
}
if (false == Parse(argc, argv, &params))
{
DisplayUsage();
return PTSDK_STATUS_INVALID_PARAM;
}
if (params.Disable && !params.Force)
{
cout << endl << "Since this operation is irreversible (once client control mode is disabled it " << endl;
cout << "cannot be enabled), one must use the " << CMD_FORCE << " option as well to actually disable " << endl;
cout << "the client control mode." <<endl;
return PT_STATUS_SUCCESS;
}
try
{
if (params.Discovery)
{
cout << endl << "Calling function Discovery..." << endl;
DISCOVERY_PARAMS discoveryParams;
Discovery(params.WMI, discoveryParams);
cout << endl << "Function Discovery ended successfully" << endl;
if (discoveryParams.hostBasedSupported)
{
cout << endl << "Machine supports Host Based Setup Service" << endl;
cout << endl << "Current Control Mode: " << (int)discoveryParams.currentControlMode;
cout << " (" << GetControlModeString(discoveryParams.currentControlMode) << ")" << endl;
cout << endl << "Allowed Control Modes: ";
unsigned int allowedControlModesSize = discoveryParams.allowedControlModes.size();
for (unsigned int i=0 ; i < allowedControlModesSize ; i++)
{
unsigned char controlMode = discoveryParams.allowedControlModes.at(i);
cout << (int)controlMode;
cout << " (" << GetControlModeString(controlMode) << ")";
(i < (allowedControlModesSize-1)) ? cout << ", " : cout << endl;
}
}
else
{
cout << endl << "Machine does not supports Host Based Setup Service." << endl;
}
}
else if (params.Setup)
{
cout << endl << "Calling function Setup..." << endl;
Setup(params.WMI, params.NewAdminPassword, params.AdapterName);
cout << endl << "Function Setup ended successfully" << endl;
}
else if (params.Disable)
{
cout << endl << "Calling function Disable..." << endl;
Disable(params.WMI);
cout << endl << "Function Disable ended successfully" << endl;
}
else if (params.Unprovision)
{
cout << endl << "Calling function UnprovisionClient..." << endl;
UnprovisionClient(params.WMI);
cout << endl << "Function UnprovisionClient ended successfully" << endl;
}
}
catch (HostBasedSetupException& exp)
{
status = exp.GetStatus();
if (AMT_STATUS_SUCCESS != status)
{
cerr << endl << "Error - HostBasedSetupException: " << endl;
cerr << exp.what() << endl;
cerr << "Status code: " << status << endl;
}
else
{
cout << endl << exp.what() << endl;
}
}
catch (CimException& exp)
{
cerr << endl << "Error - CimException: " << endl;
cerr << exp.what() << endl;
status = PTSDK_STATUS_NETWORK_ERROR;
}
catch (ConversionException &exp)
{
cerr << endl << "Error - ConversionException: " << endl;
cerr << exp.what() << endl;
status = PTSDK_STATUS_NETWORK_ERROR;
}
catch (WSManException &exp)
{
cerr << endl << "Error - WSManException: " << endl;
cerr << exp.what() << endl;
status = PTSDK_STATUS_NETWORK_ERROR;
}
catch (HTTPException &exp)
{
cerr << endl << "Error - HTTPException: " << endl;
cerr << exp.what() << endl;
cerr << "Make sure that the Intel(R) ME Interface driver and the Intel(R) LMS" << endl;
cerr << "were installed before running the application," << endl;
cerr << "and the application is running with Administrator privileges.(for more details view SDK documentation)" << endl;
status = PTSDK_STATUS_NETWORK_ERROR;
}
catch (const exception &exp)
{
cerr << endl << "Error - std::exception: " << endl;
cerr << exp.what() << endl;
status = PTSDK_STATUS_NETWORK_ERROR;
}
return status;
}
/*
* Description: Calls to Parse user arguments.
* Arguments:
* argc - number of arguments
* argv - pointer to the arguments
* params - host based setup parameters structure
* Return values:
* bool - true on successful parsing, false otherwise
*/
bool Parse(int argc, char* argv[], HOST_BASED_SETUP_PARAMS* params)
{
if(NULL == params|| NULL == argv)
{
return false;
}
params->Discovery = false;
params->Setup = false;
params->Disable = false;
params->NewAdminPassword = "";
params->AdapterName = "";
params->Force = false;
params->Unprovision = false;
params->WMI = false;
Options options;
const Options::FormatNode format[] =
{{CMD_DISCOVERY, false}, {CMD_SETUP, false}, {CMD_NEWPASS, true}, {CMD_ADAPTERNAME, true}, {CMD_DISABLE, false},
{CMD_FORCE, false}, {CMD_UNPROVISION, false}, {CMD_WMI, false}};
if (!options.Parse(argc, const_cast<const char**>(argv), format,
sizeof(format)/sizeof(Options::FormatNode)))
{
cout << endl << "Error: Invalid parameter" << endl;
return false;
}
if(options.OptionExists(CMD_DISCOVERY))
{
params->Discovery = true;
}
if(options.OptionExists(CMD_SETUP))
{
if (params->Discovery)
{
cout << endl << "Error: Cannot specify more than one option" << endl;
return false;
}
params->Setup = true;
if (options.OptionExists(CMD_NEWPASS))
{
auto res = options.GetOption(CMD_NEWPASS);
params->NewAdminPassword = (res != NULL) ? res : "";
if (params->NewAdminPassword != "" && (params->NewAdminPassword.length() < 8 || params->NewAdminPassword.length() > 32)) {
cout << endl << "Error: Password must contain between 8 to 32 characters." << endl;
return false;
}
}
else
{
cout << endl << "Error: Must specify " << CMD_NEWPASS << " with the " << CMD_SETUP << " option" << endl;
return false;
}
if (options.OptionExists(CMD_ADAPTERNAME))
{
auto res = options.GetOption(CMD_ADAPTERNAME);
params->AdapterName = (res != NULL) ? res : "";
}
}
if(options.OptionExists(CMD_DISABLE))
{
if (params->Setup || params->Discovery)
{
cout << endl << "Error: Cannot specify more than one option" << endl;
return false;
}
params->Disable = true;
params->Force = options.OptionExists(CMD_FORCE);
}
if(options.OptionExists(CMD_UNPROVISION))
{
if (params->Disable || params->Setup || params->Discovery)
{
cout << endl << "Error: Cannot specify more than one option" << endl;
return false;
}
params->Unprovision = true;
}
if (!params->Discovery && !params->Setup && !params->Disable && !params->Unprovision)
{
cout << endl << "Error: Must specify a valid option" << endl;
return false;
}
if(options.OptionExists(CMD_WMI))
{
params->WMI = true;
}
return true;
}
/*
* Description: Displays the usage.
*/
VOID DisplayUsage()
{
cout << endl << "Usage:" << endl;
cout << "HostBasedSetup.exe < -" << CMD_DISCOVERY << " | -" << CMD_SETUP << " -" << CMD_NEWPASS << " -" << CMD_ADAPTERNAME;
cout << " | -" << CMD_DISABLE << " -" << CMD_FORCE << " | -"<< CMD_UNPROVISION << " >";
cout << " [-" << CMD_WMI << "]" << endl;
cout << endl << "Where options are: " << endl << endl;
cout << " " << CMD_DISCOVERY;
cout << " - Perform discovery test on the Intel(R) AMT device." << endl;
cout << endl;
cout << " " << CMD_SETUP;
cout << " - Perform setup of an Intel(R) AMT device in client control mode." << endl;
cout << endl;
cout << " " << CMD_DISABLE;
cout << " - Disable the client control mode of an Intel(R) AMT device." << endl;
cout << " Since this operation is irreversible (once client control mode " << endl;
cout << " is disabled it cannot be enabled), one must use the " << CMD_FORCE << endl;
cout << " option as well to actually disable the client control mode." <<endl;
cout << endl;
cout << " " << CMD_UNPROVISION;
cout << " - Unprovision an Intel(R) AMT device in client control mode." << endl;
cout << endl;
cout << " " << CMD_WMI;
cout << " - Execute the command over WMI (instead of the default MEI)." << endl;
}
/*
* Description: Return a string representing the given control mode value.
* Arguments:
* controlMode - The control mode value
* Return value:
* String representing the given control mode value
*/
string GetControlModeString(unsigned char controlMode)
{
if (0 == controlMode)
{
return "Not provisioned";
}
if (1 == controlMode)
{
return "Client";
}
if (2 == controlMode)
{
return "Admin";
}
return "Unsupported";
}