610 lines
23 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Intel.Management.Wsman;
namespace Intel.Management.PSModule.Amt
{
class WirelessService : DriveContainer
{
public WirelessService(DriveItem parent)
: base("Wireless",parent)
{
}
public override void GetChildItems(ChildWriter writer)
{
writer.Add(new Endpoints(this));
writer.Add(new Ports(this));
writer.Add(new Settings(this));
}
public override object GetReturnObject()
{
return new NameValuePairItem(Name, Value);
}
class Settings : SettingsContainer
{
public Settings(DriveItem parent)
: base("Settings", parent)
{
}
public override void GetChildItems(ChildWriter writer)
{
IWsmanConnection conn = ((AmtRootService)GetRoot()).Connection;
_refToSettings = conn.NewReference("AMT_WiFiPortConfigurationService");
_refToSettings.AddSelector("Name", "Intel(r) AMT WiFi Port Configuration Service");
_settingsObj = _refToSettings.Get();
if (HasSetting("localProfileSynchronizationEnabled"))
writer.Add(new ValueMapSetting("localProfileSynchronizationEnabled", ValueList.LocalProfileSynchronizationEnabled ,this));
if (HasSetting("LastConnectedSsidUnderMeControl"))
writer.Add(new DriveEntry("LastConnectedSsidUnderMeControl",
_settingsObj.GetProperty("LastConnectedSsidUnderMeControl").ToString(), this));
}
public override object GetReturnObject()
{
return new NameValuePairItem(Name, Value);
}
}
class Endpoints : DriveContainer
{
public Endpoints(DriveItem parent)
: base("Endpoints", parent)
{
}
public override void GetChildItems(ChildWriter writer)
{
writer.Add(new Endpoint("WiFi Endpoint 0", this));
}
public override object GetReturnObject()
{
return new NameValuePairItem(Name, Value);
}
}
class Endpoint: DriveContainer
{
public Endpoint(string name,DriveItem parent)
: base(name, parent)
{
}
public override void GetChildItems(ChildWriter writer)
{
IWsmanConnection conn = ((AmtRootService)GetRoot()).Connection;
IManagedReference endPointRef = conn.NewReference("CIM_WiFiEndpoint");
endPointRef.AddSelector("Name", "WiFi Endpoint 0");
writer.Add(new Profiles(endPointRef,this));
}
public override object GetReturnObject()
{
return new NameValuePairItem(Name, Value);
}
}
class Ports : DriveContainer
{
public Ports(DriveItem parent)
: base("Ports",parent)
{
}
public override void GetChildItems(ChildWriter writer)
{
writer.Add(new WifiPort("WiFi Port 0",this));
}
public override object GetReturnObject()
{
return new NameValuePairItem(Name, Value);
}
}
class WifiPort : SettingsContainer
{
public WifiPort(string name,DriveItem parent)
: base(name, parent)
{
}
public override void GetChildItems(ChildWriter writer)
{
IWsmanConnection conn = ((AmtRootService)GetRoot()).Connection;
_refToSettings = conn.NewReference("CIM_WiFiPort");
_refToSettings.AddSelector("DeviceID", _name);
_settingsObj = _refToSettings.Get();
if (HasSetting("LinkTechnology"))
writer.Add(new DriveEntry("LinkTechnology",
ValueMap.Create(_settingsObj.GetProperty("LinkTechnology").ToString(),
ValueList.LinkTechnology), this));
if (HasSetting("PortType"))
writer.Add(new DriveEntry("PortType",
ValueMap.Create(_settingsObj.GetProperty("PortType").ToString(),
ValueList.PortType), this));
if (HasSetting("PermanentAddress"))
writer.Add(new DriveEntry("PermanentAddress",
_settingsObj.GetProperty("PermanentAddress").ToString(), this));
_refToSettings = conn.NewReference("CIM_WiFiPort");
_refToSettings.AddSelector("DeviceID", _name);
_refToSettings = conn.NewReference("CIM_WiFiPortCapabilities");
_refToSettings.AddSelector("InstanceID", "Intel(r) AMT:WiFi Port 0 Capabilities");
_settingsObj = _refToSettings.Get();
List<string> list = new List<string>();
IDictionary<string, string> map = ValueList.PortType;
foreach (IWsmanItem item in _settingsObj.GetProperty("SupportedPortTypes"))
{
string mapResult;
if (map.TryGetValue(item.ToString(), out mapResult))
{
list.Add(mapResult);
}
}
writer.Add(new DriveEntry("SupportedPortTypes", list.ToArray(), this));
_refToSettings = conn.NewReference("CIM_WiFiEndpointCapabilities");
_refToSettings.AddSelector("InstanceID", "Intel(r) AMT:WiFi Endpoint Capabilities");
_settingsObj = _refToSettings.Get();
if (HasSetting("ElementNameEditSupported"))
writer.Add(new DriveEntry("ElementNameEditSupported",
bool.Parse(_settingsObj.GetProperty("ElementNameEditSupported").ToString()), this));
list.Clear();
map = WirelessProfile.SupportedAuthenticationMethods;
foreach (IWsmanItem item in _settingsObj.GetProperty("SupportedAuthenticationMethods"))
{
string mapResult;
if (map.TryGetValue(item.ToString(), out mapResult))
{
list.Add(mapResult);
}
}
writer.Add(new DriveEntry("SupportedAuthenticationMethods", list.ToArray(), this));
list.Clear();
map = WirelessProfile.SupportedEncryptionMethods;
foreach (IWsmanItem item in _settingsObj.GetProperty("SupportedEncryptionMethods"))
{
string mapResult;
if (map.TryGetValue(item.ToString(), out mapResult))
{
list.Add(mapResult);
}
}
writer.Add(new DriveEntry("SupportedEncryptionMethods", list.ToArray(), this));
_refToSettings = conn.NewReference("CIM_IEEE8021xCapabilities");
_refToSettings.AddSelector("InstanceID", "Intel(r) AMT:IEEE 802.1x Capabilities");
_settingsObj = _refToSettings.Get();
if (HasSetting("FastRoamingSupported"))
writer.Add(new DriveEntry("FastRoamingSupported",
bool.Parse(_settingsObj.GetProperty("FastRoamingSupported").ToString()), this));
list.Clear();
map = WirelessProfile.IEEESettings.SupportedAuthenticationProtocols;
foreach (IWsmanItem item in _settingsObj.GetProperty("SupportedAuthenticationProtocols"))
{
string mapResult;
if (map.TryGetValue(item.ToString(), out mapResult))
{
list.Add(mapResult);
}
}
writer.Add(new DriveEntry("SupportedAuthenticationProtocols", list.ToArray(), this));
} //end GetChildITems
} //End WifiPort class
class Profiles : DriveContainer
{
IManagedReference _endPointRef;
public Profiles(IManagedReference endpointRef, DriveItem parent)
: base("Profiles", parent)
{
_endPointRef = endpointRef;
}
public override void GetChildItems(ChildWriter writer)
{
IWsmanConnection conn = ((AmtRootService)GetRoot()).Connection;
// Wifi mapping
Dictionary<string, IManagedInstance> wMap = new Dictionary<string, IManagedInstance>();
// IEEE profile mapping
Dictionary<string, IManagedInstance> iMap = new Dictionary<string, IManagedInstance>();
List<IManagedInstance> list = new List<IManagedInstance>();
foreach (IWsmanItem item in conn.ExecQuery("SELECT * FROM CIM_WiFiEndpointSettings"))
{
wMap.Add(item.Object.GetProperty("InstanceID").ToString(), item.Object);
}
//CIM_WiFiEndpoint
foreach (IWsmanItem item in conn.ExecQuery("SELECT * FROM CIM_IEEE8021xSettings"))
{
iMap.Add(item.Object.GetProperty("InstanceID").ToString(), item.Object);
}
//REF CIM_Credential ElementInContext
//ElementProvidingContext
// ManagedElement
foreach (KeyValuePair<string, IManagedInstance> pair in wMap)
{
IManagedInstance wSetting = pair.Value;
IManagedInstance iSetting = null;
if (iMap.ContainsKey(pair.Key))
iSetting = iMap[pair.Key];
WirelessProfile profile = LoadProfile(wSetting,iSetting);
writer.Add(new Profile(profile, this));
}
}
public override DriveItem NewItem(string name, string type, object newItem, DriveProvider provider)
{
Profile profileItem = null;
object baseObj = AmtDriveInfo.GetBaseObject(newItem);
if (baseObj is WirelessProfile)
{
WirelessProfile profile = baseObj as WirelessProfile;
SaveProfile(profile,true);
profileItem=new Profile(profile,this);
}
else
{
throw new System.Management.Automation.PSArgumentException();
}
this.AddChildItem(profileItem);
return profileItem;
}
public void SaveProfile(WirelessProfile profile,bool createNew)
{
IWsmanConnection conn = ((AmtRootService)GetRoot()).Connection;
IManagedReference refToService = conn.NewReference("AMT_WiFiPortConfigurationService");
refToService.AddSelector("Name", "Intel(r) AMT WiFi Port Configuration Service");
IManagedInstance inputObj = null;
if (createNew)
{
inputObj = refToService.CreateMethodInput("AddWiFiSettings");
inputObj.SetProperty("WiFiEndpoint", _endPointRef);
}
else
{
IManagedReference refToProfile = conn.NewReference("CIM_WiFiEndpointSettings");
refToProfile.AddSelector("InstanceID",profile.InstanceID);
inputObj = refToService.CreateMethodInput("UpdateWiFiSettings");
inputObj.SetProperty("WiFiEndpointSettings", refToProfile);
}
IManagedInstance iSettings = null;
IManagedInstance wSettings = conn.NewInstance("CIM_WiFiEndpointSettings");
IManagedReference refToClientCert=null;
IManagedReference refToCaCert =null;
wSettings.SetProperty("ElementName", profile.Name);
wSettings.SetProperty("InstanceID", profile.Name);
wSettings.SetProperty("Priority", profile.Priority.ToString());
wSettings.SetProperty("SSID", profile.SSID);
wSettings.SetProperty("BSSType", ((int)profile.BssType).ToString());
ValueMap encMap=ValueMap.Create("5", WirelessProfile.SupportedEncryptionMethods);
ValueMap authMap = ValueMap.Create("1", WirelessProfile.SupportedAuthenticationMethods);
encMap.Value = encMap.GetValueFromString(profile.EncryptionMethod);
authMap.Value = authMap.GetValueFromString(profile.AuthenticationMethod);
wSettings.SetProperty("EncryptionMethod", encMap.Value.ToString());
wSettings.SetProperty("AuthenticationMethod", authMap.Value.ToString());
if (authMap.Value.Equals("4") || authMap.Value.Equals("6"))
{
if (profile.WPA.PSK!=null && profile.WPA.PSK.Length>0)
{
wSettings.SetProperty("PSKValue",Convert.ToBase64String(profile.WPA.PSK));
}
if (!string.IsNullOrEmpty(profile.WPA.PassPhrase))
{
wSettings.SetProperty("PSKPassPhrase",profile.WPA.PassPhrase);
}
}
//load 8021x
if (authMap.Value.Equals("5") || authMap.Value.Equals("7"))
{
iSettings = conn.NewInstance("CIM_IEEE8021xSettings");
iSettings.SetProperty("InstanceID", profile.Name);
iSettings.SetProperty("ElementName", profile.Name);
ValueMap proMap = ValueMap.Create("1", WirelessProfile.IEEESettings.SupportedAuthenticationProtocols);
proMap.Value = proMap.GetValueFromString(profile.IEEE.AuthenticationProtocol);
iSettings.SetProperty("AuthenticationProtocol",proMap.Value.ToString());
if (profile.IEEE.ClientCredential != null)
{
refToClientCert = TLSService.GetCertificateRef(profile.IEEE.ClientCredential, conn);
}
if (profile.IEEE.CACredential != null)
{
refToCaCert = TLSService.GetCertificateRef(profile.IEEE.CACredential, conn);
}
if (profile.IEEE.ServerCertificateName != null)
{
iSettings.SetProperty("ServerCertificateName", profile.IEEE.ServerCertificateName);
}
if (profile.IEEE.ServerCertificateNameComparison != null)
{
ValueMap compMap = ValueMap.Create("1", WirelessProfile.IEEESettings.SupportedCertificateComparisonMethods);
compMap.Value = compMap.GetValueFromString(profile.IEEE.ServerCertificateNameComparison);
iSettings.SetProperty("ServerCertificateNameComparison", compMap.Value.ToString());
}
if (profile.IEEE.Username != null)
{
iSettings.SetProperty("Username", profile.IEEE.Username);
}
if (profile.IEEE.Password != null)
{
iSettings.SetProperty("Password", profile.IEEE.Password);
}
if (profile.IEEE.Domain != null)
{
iSettings.SetProperty("Domain", profile.IEEE.Domain);
}
if (profile.IEEE.RoamingIdentity != null)
{
iSettings.SetProperty("RoamingIdentity", profile.IEEE.RoamingIdentity);
}
if (profile.IEEE.ProtectedAccessCredential != null)
{
iSettings.SetProperty("ProtectedAccessCredential",
Convert.ToBase64String(profile.IEEE.ProtectedAccessCredential));
}
if (profile.IEEE.PACPassword != null)
{
iSettings.SetProperty("PACPassword",profile.IEEE.PACPassword);
}
if (profile.IEEE.PSK != null)
{
iSettings.SetProperty("PSK", Convert.ToBase64String( profile.IEEE.PSK));
}
}
inputObj.SetProperty("WiFiEndpointSettingsInput", wSettings);
if (iSettings!=null)
{
inputObj.SetProperty("IEEE8021xSettingsInput", iSettings);
if (refToClientCert != null)
inputObj.SetProperty("ClientCredential", refToClientCert);
if (refToCaCert!=null)
inputObj.SetProperty("CACredential", refToCaCert);
}
IManagedInstance resultSettings = null;
IManagedInstance outObj = refToService.InvokeMethod(inputObj);
switch (outObj.GetProperty("ReturnValue").ToString())
{
case "0":
resultSettings=outObj.GetProperty("WiFiEndpointSettings").Ref.Get();
profile.InstanceID = resultSettings.GetProperty("InstanceID").ToString();
//if (outObj.GetProperty("
//resultSettings = outObj.GetProperty("WiFiEndpointSettings").Ref.Get();
break;
case "1":
AmtException.ThrowInvokeError(outObj, "Not Supported");
break;
case "2":
AmtException.ThrowInvokeError(outObj, "Failed");
break;
case "3":
AmtException.ThrowInvokeError(outObj, "Invalid Parameter");
break;
case "4":
AmtException.ThrowInvokeError(outObj, "Invalid Reference");
break;
default:
AmtException.ThrowInvokeError(outObj);
break;
}
}
private WirelessProfile LoadProfile(IManagedInstance settingsObj,IManagedInstance ieeeSettings)
{
//CIM_CredentialContext
//CIM_ConcreteComponent
WirelessProfile result = new WirelessProfile();
result.InstanceID=settingsObj.GetProperty("InstanceID").ToString();
result.SSID=settingsObj.GetProperty("SSID").ToString();
result.Name=settingsObj.GetProperty("ElementName").ToString();
result.Priority = int.Parse(settingsObj.GetProperty("Priority").ToString());
ValueMap encMap = ValueMap.Create("5", WirelessProfile.SupportedEncryptionMethods);
ValueMap authMap = ValueMap.Create("1", WirelessProfile.SupportedAuthenticationMethods);
result.AuthenticationMethod =
authMap.GetStringFromValue(settingsObj.GetProperty("AuthenticationMethod").ToString());
result.EncryptionMethod =
encMap.GetStringFromValue(settingsObj.GetProperty("EncryptionMethod").ToString());
if (ieeeSettings != null)
{
ValueMap proMap = ValueMap.Create("1", WirelessProfile.IEEESettings.SupportedAuthenticationProtocols);
result.IEEE.AuthenticationProtocol=
proMap.GetStringFromValue(ieeeSettings.GetProperty("EncryptionMethod").ToString());
if (HasSetting(ieeeSettings, "ServerCertificateName"))
{
result.IEEE.ServerCertificateName = settingsObj.GetProperty("ServerCertificateName").ToString();
ValueMap cerMap = ValueMap.Create("1", ValueList.CertificateNameComparison);
result.IEEE.ServerCertificateNameComparison =
cerMap.GetStringFromValue(ieeeSettings.GetProperty("ServerCertificateNameComparison").ToString());
}
if (HasSetting(ieeeSettings, "RoamingIdentity"))
{
result.IEEE.RoamingIdentity = settingsObj.GetProperty("RoamingIdentity").ToString();
}
if (HasSetting(ieeeSettings, "Username"))
{
result.IEEE.RoamingIdentity = settingsObj.GetProperty("Username").ToString();
}
if (HasSetting(ieeeSettings, "Domain"))
{
result.IEEE.RoamingIdentity = settingsObj.GetProperty("Domain").ToString();
}
}
return result;
}
private bool HasSetting(IManagedInstance inst, string name)
{
IWsmanItem item = inst.GetProperty(name);
return !item.IsNull;
}
public override object GetReturnObject()
{
return new NameValuePairItem(Name, Value);
}
}
class Profile : DriveItem
{
public Profile(WirelessProfile profile, Profiles parent)
: base(profile.Name, parent)
{
_value = profile;
}
public override void RemoveItem(DriveProvider provider)
{
IWsmanConnection conn = ((AmtRootService)GetRoot()).Connection;
WirelessProfile profile = _value as WirelessProfile;
IManagedReference refToSetting = conn.NewReference("CIM_WiFiEndpointSettings");
refToSetting.AddSelector("InstanceID", profile.InstanceID);
refToSetting.Delete();
_parent.RemoveChildItem(this);
}
public override void SetItem(object values, DriveProvider provider)
{
object baseObject = AmtDriveInfo.GetBaseObject(values);
if (baseObject is WirelessProfile)
{
Profiles profiles = _parent as Profiles;
WirelessProfile changeTo = baseObject as WirelessProfile;
profiles.SaveProfile(changeTo, false);
}
else
{
throw new System.Management.Automation.PSArgumentException();
}
}
public override object GetReturnObject()
{
return new NameValuePairItem(Name, Value);
}
}
} // end Wireless Service
} // end namespace