229 lines
9.7 KiB
C#

//----------------------------------------------------------------------------
//
// Copyright © 2009-2014, Intel Corporation. All rights reserved.
//
// File: Program.cs
//
//----------------------------------------------------------------------------
using System;
using UCT.Forms;
using System.Net;
using Intel.Manageability;
using System.Windows.Forms;
using Intel.Manageability.Utils;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Security.Principal;
using System.Management;
using System.Collections;
using System.Collections.Generic;
using UCT.Utils;
using Intel.Management.Wsman;
using Common.Utils;
namespace UCT
{
static class Program
{
//to support command line execution.
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
[DllImport("kernel32.dll")]
static extern bool FreeConsole();
[DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetDefaultDllDirectories(int directoryFlags);
private static CmdLineArguments Params = new CmdLineArguments();
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// set default dll lookup directory to system
SetDefaultDllDirectories(0x00000800); //LOAD_LIBRARY_SEARCH_SYSTEM32
AMT_SW_GUI.MessageManager.InitMessageManager(Properties.Resources.phoneapp_32px);
if (!ProcessOwnerExist())
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
WsmanConnection connectionInfo = null;
try
{
AttachConsole(-1);
//if user provided arguments in the command line - use them for the connection
if (args.Length > 0)
{
connectionInfo = ParseArguments(args);
if (connectionInfo == null)
{
Environment.Exit(0);
}
}
ConnectionSettings credentials;
using (credentials = connectionInfo != null ? new ConnectionSettings(connectionInfo) : new ConnectionSettings())
{
try
{
Application.Run(credentials);
}
catch { }
//if the credentials form ended successfully continue to the
//remote session screen
if (DialogResult.OK == credentials.DialogResult)
{
UserInterface remote = new UserInterface();
credentials.Close();
Application.Run(remote);
}
}
}
catch (CmdLineArguments.Exception e)
{
Console.WriteLine("\n--------------CmdLineArguments.Exception--------------");
Console.WriteLine(e.Message);
Console.WriteLine("\nPress <ENTER> to continue....");
}
catch (DllNotFoundException e)
{
Console.WriteLine("\n--------------DllNotFoudtException--------------");
Console.WriteLine(e.Message);
}
catch (Exception e)
{
AMT_SW_GUI.MessageManager.ShowErrorMessage(e.Message, "UCT Failure");
}
finally
{
LogManager.CloseResource();
FreeConsole();
Environment.Exit(1);
}
}
else
{
AMT_SW_GUI.MessageManager.ShowErrorMessage("Application already running", "Error");
}
}
private static bool ProcessOwnerExist()
{
WindowsIdentity user = WindowsIdentity.GetCurrent();
string[] s = new string[0];
if (user.Name.Contains("\\"))
s = user.Name.Split('\\');
string owner = (s.Length == 0) ? user.Name : s[s.Length - 1];
List<string> processesOwners = new List<string>();
Process[] UCTProcesses = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
foreach (Process process in UCTProcesses)
{
processesOwners.Add(GetProcessOwner(process.Id));
}
if ((processesOwners.FindAll(e => e == owner)).Count <= 1)
{
return false;
}
return true;
}
private static string GetProcessOwner(int processId)
{
string query = "Select * From Win32_Process Where ProcessID = " + processId;
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
ManagementObjectCollection processList = searcher.Get();
foreach (ManagementObject obj in processList)
{
string[] argList = new string[] { string.Empty };
int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));
if (returnVal == 0)
return argList[0];
}
}
return "NO OWNER";
}
private static WsmanConnection ParseArguments(string[] args)
{
string usage = string.Empty;
WsmanConnection con = null;
try
{
// Add command the relevant command line argument options
Params.AddArg(CmdLineArguments.OPT_HOST, true, true, String.Empty); // -host required
Params.AddArg(CmdLineArguments.OPT_USER, true, false, String.Empty); // -user optional
Params.AddArg(CmdLineArguments.OPT_PASS, true, false, String.Empty); // -pass optional
Params.AddArg(CmdLineArguments.OPT_SECURE, false, false, String.Empty); // -tls options
Params.AddArg(CmdLineArguments.OPT_KRB, false, false, String.Empty); // -kerberos options
Params.AddArg(CmdLineArguments.OPT_CERT, true, false, String.Empty); // -certificate options
Params.AddArg(CmdLineArguments.OPT_PROXY, true, false, String.Empty); // -proxy options
Params.AddArg(CmdLineArguments.OPT_PROXY_USER, true, false, String.Empty); // proxy user
Params.AddArg(CmdLineArguments.OPT_PROXY_PASSWORD, true, false, String.Empty); // proxy password
Params.AddArg(CmdLineArguments.OPT_HELP, false, false, String.Empty);
// Creates usage string
string assembly = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
usage = Params.CreateUsage(assembly, false);
if (args[0].ToLower().Equals("-help") || args[0].Equals("/h") || args[0].ToLower().Equals("/help") || args[0].ToLower().Equals("-h"))
{
Console.WriteLine(usage);
Console.WriteLine("\nPress <ENTER> to continue....");
return null;
}
//parse the args
Params.Parse(args);
con = new WsmanConnection();
string address = Params[CmdLineArguments.OPT_HOST];
if (Utilities.CheckAddressFormat(address) == Address.IPV6)
address = "[" + address + "]";
con.Address = Params.Selected(CmdLineArguments.OPT_SECURE)
? "https://" + address + ":16993/wsman"
: "http://" + address + ":16992/wsman";
con.Username = Params.Selected(CmdLineArguments.OPT_USER) ? Params[CmdLineArguments.OPT_USER]: "";
// Convert password to secure string to comply with wsman dll which supports passwords in SecureString
// format only.
con.Password = Params.Selected(CmdLineArguments.OPT_PASS) ? Params[CmdLineArguments.OPT_PASS].ConvertToSecureString() : null;
con.AuthenticationScheme = Params.Selected(CmdLineArguments.OPT_KRB) ? "Negotiate" : "Digest";
if (Params[CmdLineArguments.OPT_PROXY] != null)
{
string proxyAddress = Params[CmdLineArguments.OPT_PROXY];
if (Utilities.CheckAddressFormat(proxyAddress) == Address.IPV6)
proxyAddress = "[" + proxyAddress + "]";
con.Options.ProxyAddress = "http://" + proxyAddress;
if (Params[CmdLineArguments.OPT_PROXY_USER] != null &&
Params[CmdLineArguments.OPT_PROXY_PASSWORD] != null)
{
con.Options.ProxyUser = Params[CmdLineArguments.OPT_PROXY_USER];
con.Options.ProxyPassword = Params[CmdLineArguments.OPT_PROXY_PASSWORD].ConvertToSecureString();
}
}
con.Options.ClientCertificate = Params[CmdLineArguments.OPT_CERT] != null ? Utilities.getCertFromStore(Params[CmdLineArguments.OPT_CERT])[0] : null;
return con;
}
catch (Exception e)
{
con?.Dispose();
throw new CmdLineArguments.Exception(e.Message + "\n" + usage);
}
}
}
}