/// <summary> /// Adds an application to manage to the drop down menu. /// </summary> private int AddApplicationToManage(ManagedApplication application) { // ignore files that don't exist. if (application == null) { return(ApplicationToManageCB.SelectedIndex); } try { // ignore duplicates. foreach (ManagedApplication applicationToManage in ApplicationToManageCB.Items) { if (String.Compare(applicationToManage.SourceFile.FullName, application.SourceFile.FullName, StringComparison.OrdinalIgnoreCase) == 0) { return(ApplicationToManageCB.SelectedIndex); } } // add to list. return(ApplicationToManageCB.Items.Add(application)); } // ignore errors. catch (Exception) { return(ApplicationToManageCB.SelectedIndex); } }
/// <summary> /// Adds an application to manage to the drop down menu. /// </summary> private int AddApplicationToManage(FileInfo fileInfo) { // ignore files that don't exist. if (fileInfo == null || !fileInfo.Exists) { return(ApplicationToManageCB.SelectedIndex); } try { ManagedApplication application = ManagedApplication.Load(fileInfo.FullName); if (application == null) { return(ApplicationToManageCB.SelectedIndex); } // add to list. return(AddApplicationToManage(application)); } // ignore errors. catch (Exception) { return(ApplicationToManageCB.SelectedIndex); } }
/// <summary> /// Loads the specified file path. /// </summary> /// <param name="filePath">The file path.</param> /// <returns></returns> public static ManagedApplication Load(string filePath) { using (Stream istrm = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite)) { DataContractSerializer serializer = new DataContractSerializer(typeof(ManagedApplication)); ManagedApplication application = (ManagedApplication)serializer.ReadObject(istrm); application.m_sourceFile = new FileInfo(filePath); if (String.IsNullOrEmpty(application.DisplayName)) { string name = application.m_sourceFile.Name; int index = name.LastIndexOf('.'); if (index > 0) { name = name.Substring(0, index); } application.DisplayName = name; } application.LoadSdkConfigFile(); return(application); } }
private void EditApplicationBTN_Click(object sender, EventArgs e) { try { // get application. ManagedApplication application = ApplicationToManageCB.SelectedItem as ManagedApplication; if (application == null) { return; } // edit application. application = new ManagedApplicationDlg().ShowDialog(application); if (application == null) { return; } // update list. ApplicationToManageCB.Items.RemoveAt(ApplicationToManageCB.SelectedIndex); ApplicationToManageCB.SelectedIndex = ApplicationToManageCB.Items.Add(application); } catch (Exception exception) { GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception); } }
private void ApplicationToManageCB_SelectedIndexChanged(object sender, EventArgs e) { try { // ensure something is selected. if (ApplicationToManageCB.SelectedIndex == -1) { if (ApplicationToManageCB.Items.Count > 0) { ApplicationToManageCB.SelectedIndex = 0; } return; } ServersLV.Items.Clear(); ManagedApplication application = ApplicationToManageCB.SelectedItem as ManagedApplication; if (application == null) { return; } m_configuration = ApplicationConfiguration.Load(new FileInfo(application.ConfigurationPath), ApplicationType.Server, null); UpdateServers(); Utils.UpdateRecentFileList("COM Wrappers", application.ExecutablePath, 16); } catch (Exception exception) { GuiUtils.HandleException(this.Text, System.Reflection.MethodBase.GetCurrentMethod(), exception); } }
/// <summary> /// Loads the applications from disk. /// </summary> public void LoadApplications() { ApplicationToManageCB.Items.Clear(); // add the recent files. foreach (string filePath in Utils.GetRecentFileList(m_groupName)) { AddApplicationToManage(new FileInfo(filePath)); } // load the config files for any OPC applications. string configDir = Utils.GetAbsoluteDirectoryPath("%LocalApplicationData%\\OPC Foundation\\Applications", false, false, true); if (configDir != null) { foreach (FileInfo fileInfo in new DirectoryInfo(configDir).GetFiles("*.xml")) { AddApplicationToManage(fileInfo); } } // add the standard applications. foreach (string fileName in s_StandardApplications) { string filePath = Utils.FindInstalledFile(fileName); if (!String.IsNullOrEmpty(filePath)) { ManagedApplication application = new ManagedApplication(); application.SetExecutableFile(filePath); bool found = false; foreach (ManagedApplication item in ApplicationToManageCB.Items) { if (item.ExecutablePath != null) { if (String.Compare(item.ExecutablePath, application.ExecutablePath, StringComparison.OrdinalIgnoreCase) == 0) { found = true; break; } } } if (!found) { application.Save(configDir + "\\" + application.DisplayName + ".xml"); AddApplicationToManage(application); } } } // select the first item. if (ApplicationToManageCB.Items.Count > 0) { ApplicationToManageCB.SelectedIndex = 0; } }
/// <summary> /// Updates the dialog. /// </summary> private void Update(ManagedApplication application) { m_application = application; Dictionary <string, SecuredObjectAccessRights> read = new Dictionary <string, SecuredObjectAccessRights>(); Dictionary <string, SecuredObjectAccessRights> write = new Dictionary <string, SecuredObjectAccessRights>(); Dictionary <string, SecuredObjectAccessRights> configure = new Dictionary <string, SecuredObjectAccessRights>(); m_executablePath = application.ExecutablePath; if (!String.IsNullOrEmpty(m_executablePath)) { ChangeApplicationPermissionBTN.Enabled = true; GetAccountAccessRights(m_executablePath, SecuredObject.ExecutableFile, read, write, configure); string appConfigFile = m_executablePath + ".config"; if (File.Exists(appConfigFile)) { GetAccountAccessRights(appConfigFile, SecuredObject.DotNetAppConfigFile, read, write, configure); } } m_configurationFile = application.ConfigurationPath; if (!String.IsNullOrEmpty(m_configurationFile)) { ChangeApplicationPermissionBTN.Enabled = true; GetAccountAccessRights(m_configurationFile, SecuredObject.ConfigurationFile, read, write, configure); } if (application.Certificate != null) { m_privateKeyFilePath = application.Certificate.GetPrivateKeyFilePath(); if (!String.IsNullOrEmpty(m_privateKeyFilePath)) { GetAccountAccessRights(m_privateKeyFilePath, SecuredObject.PrivateKey, read, write, configure); } } if (application.TrustList != null) { if (application.TrustList.StoreType == CertificateStoreType.Directory) { m_trustListPath = Utils.GetAbsoluteDirectoryPath(application.TrustList.StorePath, true, false); if (!String.IsNullOrEmpty(m_trustListPath)) { ChangeTrustListPermissionBTN.Enabled = true; GetAccountAccessRights(m_trustListPath, SecuredObject.TrustList, read, write, configure); } } } ConfigureRightsCTRL.Initialize(configure.Values); WriteRightsCTRL.Initialize(write.Values); ReadRightsCTRL.Initialize(read.Values); }
private void NewApplicationBTN_Click(object sender, EventArgs e) { try { // set current directory. if (m_currentDirectory == null) { m_currentDirectory = Utils.GetAbsoluteDirectoryPath("%ProgramFiles%", false, false); } // open file dialog. OpenFileDialog dialog = new OpenFileDialog(); dialog.CheckFileExists = true; dialog.CheckPathExists = true; dialog.DefaultExt = ".exe"; dialog.Filter = "Executable Files (*.exe)|*.exe|All Files (*.*)|*.*"; dialog.Multiselect = false; dialog.ValidateNames = true; dialog.Title = "Select Wrapper Executable File"; dialog.FileName = null; dialog.InitialDirectory = m_currentDirectory; dialog.RestoreDirectory = true; if (dialog.ShowDialog() != DialogResult.OK) { return; } FileInfo executableFile = new FileInfo(dialog.FileName); m_currentDirectory = executableFile.Directory.FullName; ManagedApplication application = new ManagedApplication(); application.SetExecutableFile(executableFile.FullName); for (int ii = 0; ii < ApplicationToManageCB.Items.Count; ii++) { ManagedApplication item = ApplicationToManageCB.Items[ii] as ManagedApplication; if (item != null) { if (String.Compare(item.ExecutablePath, application.ExecutablePath, StringComparison.OrdinalIgnoreCase) == 0) { ApplicationToManageCB.SelectedIndex = ii; return; } } } ApplicationToManageCB.SelectedIndex = ApplicationToManageCB.Items.Add(application); } catch (Exception exception) { GuiUtils.HandleException(this.Text, System.Reflection.MethodBase.GetCurrentMethod(), exception); } }
/// <summary> /// Displays the dialog. /// </summary> public ManagedApplication ShowDialog(ManagedApplication application) { Update(application); if (application == null) { m_application = new ManagedApplication(); ExecutableBTN_Click(this, null); } if (ShowDialog() != DialogResult.OK) { return(null); } return(m_application); }
/// <summary> /// Displays the dialog. /// </summary> public ManagedApplication ShowDialog(ManagedApplication application) { Update(application); if (application == null) { m_application = new ManagedApplication(); ExecutableBTN_Click(this, null); } if (ShowDialog() != DialogResult.OK) { return null; } return m_application; }
/// <summary> /// Displays the dialog. /// </summary> public bool ShowDialog(ManagedApplication application) { m_application = application; if (m_application != null) { ApplicationAccessGrantedCK.Checked = ConfigUtils.CheckFirewallAccess(m_application.ExecutablePath, null); } UpdatePorts(); if (ShowDialog() != DialogResult.OK) { return false; } return true; }
/// <summary> /// Displays the dialog. /// </summary> public bool ShowDialog(ManagedApplication application) { m_application = application; if (m_application != null) { ApplicationAccessGrantedCK.Checked = ConfigUtils.CheckFirewallAccess(m_application.ExecutablePath, null); } UpdatePorts(); if (ShowDialog() != DialogResult.OK) { return(false); } return(true); }
/// <summary> /// Displays the dialog. /// </summary> public bool ShowDialog(ManagedApplication application) { InstructionsTB.Text = "It is not possible to set permissions on trust lists which are Windows certificate stores. " + "It also may not be possible to permissions on private keys stored in a Windows certificate store on some machines. " + "In these cases, the application should be configured to use directory stores."; ChangeApplicationPermissionBTN.Enabled = false; ChangeTrustListPermissionBTN.Enabled = false; Update(application); if (ShowDialog() != DialogResult.OK) { return(false); } return(true); }
/// <summary> /// Displays the dialog. /// </summary> public bool ShowDialog(ManagedApplication application) { InstructionsTB.Text = "It is not possible to set permissions on trust lists which are Windows certificate stores. " + "It also may not be possible to permissions on private keys stored in a Windows certificate store on some machines. " + "In these cases, the application should be configured to use directory stores."; ChangeApplicationPermissionBTN.Enabled = false; ChangeTrustListPermissionBTN.Enabled = false; Update(application); if (ShowDialog() != DialogResult.OK) { return false; } return true; }
private void ApplicationToManageCB_SelectedIndexChanged(object sender, EventArgs e) { try { EditApplicationBTN.Enabled = false; // check if nothing is selected. if (ApplicationToManageCB.SelectedIndex == -1) { if (ApplicationToManageCB.Items.Count > 0) { ApplicationToManageCB.SelectedIndex = 0; } return; } // get the application. ManagedApplication application = ApplicationToManageCB.SelectedItem as ManagedApplication; if (application == null) { return; } EditApplicationBTN.Enabled = true; Utils.UpdateRecentFileList(m_groupName, application.SourceFile.FullName, 16); // raise notification. if (m_ApplicationChanged != null) { m_ApplicationChanged(this, null); } } catch (Exception exception) { GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception); } }
/// <summary> /// Updates the dialog with the configuration. /// </summary> private void Update(ManagedApplication application) { if (application == null) { application = new ManagedApplication(); } m_application = application; SetIsSdkApplication(application.IsSdkCompatible); ApplicationNameTB.Text = application.DisplayName; ExecutableFileTB.Text = application.ExecutablePath; ConfigurationFileTB.Text = application.ConfigurationPath; CertificateTB.Text = null; TrustListTB.Text = null; m_certificate = application.Certificate; m_trustList = application.TrustList; if (m_certificate != null) { X509Certificate2 certificate = m_certificate.Find(); if (certificate != null) { CertificateTB.Text = certificate.Subject; } else { CertificateTB.Text = m_certificate.ToString(); } } if (m_trustList != null) { TrustListTB.Text = m_trustList.ToString(); } }
private void ApplicationToManageCB_DropDown(object sender, EventArgs e) { try { ApplicationToManageCB.Items.Clear(); List <string> applications = Utils.GetRecentFileList("COM Wrappers"); for (int ii = 0; ii < applications.Count; ii++) { if (File.Exists(applications[ii])) { ManagedApplication application = new ManagedApplication(); application.SetExecutableFile(applications[ii]); ApplicationToManageCB.Items.Add(application); } } if (ApplicationToManageCB.Items.Count == 0) { // find the wrapper. string path = Utils.FindInstalledFile("Opc.Ua.ComServerWrapper.exe"); if (path != null) { ManagedApplication application = new ManagedApplication(); application.SetExecutableFile(path); ApplicationToManageCB.Items.Add(application); } } } catch (Exception exception) { GuiUtils.HandleException(this.Text, System.Reflection.MethodBase.GetCurrentMethod(), exception); } }
private void NewApplicationBTN_Click(object sender, EventArgs e) { try { // set current directory. if (m_currentDirectory == null) { m_currentDirectory = Utils.GetAbsoluteDirectoryPath("%ProgramFiles%", false, false); } // open file dialog. OpenFileDialog dialog = new OpenFileDialog(); dialog.CheckFileExists = true; dialog.CheckPathExists = true; dialog.DefaultExt = ".exe"; dialog.Filter = "Executable Files (*.exe)|*.exe|All Files (*.*)|*.*"; dialog.Multiselect = false; dialog.ValidateNames = true; dialog.Title = "Select Wrapper Executable File"; dialog.FileName = null; dialog.InitialDirectory = m_currentDirectory; dialog.RestoreDirectory = true; if (dialog.ShowDialog() != DialogResult.OK) { return; } FileInfo executableFile = new FileInfo(dialog.FileName); m_currentDirectory = executableFile.Directory.FullName; ManagedApplication application = new ManagedApplication(); application.SetExecutableFile(executableFile.FullName); for (int ii = 0; ii < ApplicationToManageCB.Items.Count; ii++) { ManagedApplication item = ApplicationToManageCB.Items[ii] as ManagedApplication; if (item != null) { if (String.Compare(item.ExecutablePath, application.ExecutablePath, StringComparison.OrdinalIgnoreCase) == 0) { ApplicationToManageCB.SelectedIndex = ii; return; } } } ApplicationToManageCB.SelectedIndex = ApplicationToManageCB.Items.Add(application); } catch (Exception exception) { GuiUtils.HandleException(this.Text, System.Reflection.MethodBase.GetCurrentMethod(), exception); } }
/// <summary> /// Updates the dialog. /// </summary> private void Update(ManagedApplication application) { m_application = application; Dictionary<string, SecuredObjectAccessRights> read = new Dictionary<string, SecuredObjectAccessRights>(); Dictionary<string, SecuredObjectAccessRights> write = new Dictionary<string, SecuredObjectAccessRights>(); Dictionary<string, SecuredObjectAccessRights> configure = new Dictionary<string, SecuredObjectAccessRights>(); m_executablePath = application.ExecutablePath; if (!String.IsNullOrEmpty(m_executablePath)) { ChangeApplicationPermissionBTN.Enabled = true; GetAccountAccessRights(m_executablePath, SecuredObject.ExecutableFile, read, write, configure); string appConfigFile = m_executablePath + ".config"; if (File.Exists(appConfigFile)) { GetAccountAccessRights(appConfigFile, SecuredObject.DotNetAppConfigFile, read, write, configure); } } m_configurationFile = application.ConfigurationPath; if (!String.IsNullOrEmpty(m_configurationFile)) { ChangeApplicationPermissionBTN.Enabled = true; GetAccountAccessRights(m_configurationFile, SecuredObject.ConfigurationFile, read, write, configure); } if (application.Certificate != null) { m_privateKeyFilePath = application.Certificate.GetPrivateKeyFilePath(); if (!String.IsNullOrEmpty(m_privateKeyFilePath)) { GetAccountAccessRights(m_privateKeyFilePath, SecuredObject.PrivateKey, read, write, configure); } } if (application.TrustList != null) { if (application.TrustList.StoreType == CertificateStoreType.Directory) { m_trustListPath = Utils.GetAbsoluteDirectoryPath(application.TrustList.StorePath, true, false); if (!String.IsNullOrEmpty(m_trustListPath)) { ChangeTrustListPermissionBTN.Enabled = true; GetAccountAccessRights(m_trustListPath, SecuredObject.TrustList, read, write, configure); } } } ConfigureRightsCTRL.Initialize(configure.Values); WriteRightsCTRL.Initialize(write.Values); ReadRightsCTRL.Initialize(read.Values); }
/// <summary> /// Gets the default store. /// </summary> private CertificateStoreIdentifier GetDefaultStore(ManagedApplication application, bool useApplicationCertificate) { CertificateStoreIdentifier store = m_currentStore; if (m_currentStore != null) { return m_currentStore; } store = new CertificateStoreIdentifier(); if (application != null) { if (useApplicationCertificate && application.Certificate != null && !String.IsNullOrEmpty(application.Certificate.StorePath)) { store.StoreType = application.Certificate.StoreType; store.StorePath = application.Certificate.StorePath; return store; } if (application.TrustList != null && !String.IsNullOrEmpty(application.TrustList.StorePath)) { store.StoreType = application.TrustList.StoreType; store.StorePath = application.TrustList.StorePath; return store; } } store.StoreType = Utils.DefaultStoreType; store.StorePath = Utils.DefaultStorePath; return store; }
/// <summary> /// Setups the trust relationship between two applications. /// </summary> /// <param name="application1">The application1.</param> /// <param name="application2">The application2.</param> private bool SetupTrustRelationship(ManagedApplication application1, ManagedApplication application2) { X509Certificate2 certificate1 = application1.Certificate.Find(); if (certificate1 == null) { MessageBox.Show(application1.ToString() + " does not have a certificate defined.", "Setup Trust Relationship", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } X509Certificate2 certificate2 = application2.Certificate.Find(); if (certificate2 == null) { MessageBox.Show(application2.ToString() + " does not have a certificate defined.", "Setup Trust Relationship", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } if (application1.TrustList != null) { ICertificateStore store = application1.TrustList.OpenStore(); try { if (store.FindByThumbprint(certificate2.Thumbprint) == null) { store.Add(new X509Certificate2(certificate2.RawData)); } } finally { store.Close(); } } if (application2.TrustList != null) { ICertificateStore store = application2.TrustList.OpenStore(); try { if (store.FindByThumbprint(certificate1.Thumbprint) == null) { store.Add(new X509Certificate2(certificate1.RawData)); } } finally { store.Close(); } } return true; }
private void RegisterWithDiscoveryServerBTN_Click(object sender, EventArgs e) { try { const string caption = "Register with Discovery Server"; ManagedApplication application = ManageApplicationSecurityCTRL.GetSelectedApplication(); if (application == null) { return; } application.Reload(); if (application.Certificate == null) { MessageBox.Show(this, "Certificate is not specified.", caption, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } // find discovery server. string path = Utils.FindInstalledFile("Opc.Ua.DiscoveryServer.exe"); if (path == null) { MessageBox.Show("Could not find the discovery server. Please confirm that it is installed.", caption, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } ManagedApplication lds = new ManagedApplication(); lds.SetExecutableFile(path); if (!SetupTrustRelationship(application, lds)) { return; } MessageBox.Show("The Local Discovery Server now trusts the application.", caption, MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception exception) { GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception); } }
/// <summary> /// Adds an application to manage to the drop down menu. /// </summary> private int AddApplicationToManage(ManagedApplication application) { // ignore files that don't exist. if (application == null) { return ApplicationToManageCB.SelectedIndex; } try { // ignore duplicates. foreach (ManagedApplication applicationToManage in ApplicationToManageCB.Items) { if (String.Compare(applicationToManage.SourceFile.FullName, application.SourceFile.FullName, StringComparison.OrdinalIgnoreCase) == 0) { return ApplicationToManageCB.SelectedIndex; } } // add to list. return ApplicationToManageCB.Items.Add(application); } // ignore errors. catch (Exception) { return ApplicationToManageCB.SelectedIndex; } }
/// <summary> /// Loads the applications from disk. /// </summary> public void LoadApplications() { ApplicationToManageCB.Items.Clear(); // add the recent files. foreach (string filePath in Utils.GetRecentFileList(m_groupName)) { AddApplicationToManage(new FileInfo(filePath)); } // load the config files for any OPC applications. string configDir = Utils.GetAbsoluteDirectoryPath("%LocalApplicationData%\\OPC Foundation\\Applications", false, false, true); if (configDir != null) { foreach (FileInfo fileInfo in new DirectoryInfo(configDir).GetFiles("*.xml")) { AddApplicationToManage(fileInfo); } } // add the standard applications. foreach (string fileName in s_StandardApplications) { string filePath = Utils.FindInstalledFile(fileName); if (!String.IsNullOrEmpty(filePath)) { ManagedApplication application = new ManagedApplication(); application.SetExecutableFile(filePath); bool found = false; foreach (ManagedApplication item in ApplicationToManageCB.Items) { if (item.ExecutablePath != null) { if (String.Compare(item.ExecutablePath, application.ExecutablePath, StringComparison.OrdinalIgnoreCase) == 0) { found = true; break; } } } if (!found) { application.Save(configDir + "\\" + application.DisplayName + ".xml"); AddApplicationToManage(application); } } } // select the first item. if (ApplicationToManageCB.Items.Count > 0) { ApplicationToManageCB.SelectedIndex = 0; } }
private void ApplicationToManageCB_DropDown(object sender, EventArgs e) { try { ApplicationToManageCB.Items.Clear(); List<string> applications = Utils.GetRecentFileList("COM Wrappers"); for (int ii = 0; ii < applications.Count; ii++) { if (File.Exists(applications[ii])) { ManagedApplication application = new ManagedApplication(); application.SetExecutableFile(applications[ii]); ApplicationToManageCB.Items.Add(application); } } if (ApplicationToManageCB.Items.Count == 0) { // find the wrapper. string path = Utils.FindInstalledFile("Opc.Ua.ComServerWrapper.exe"); if (path != null) { ManagedApplication application = new ManagedApplication(); application.SetExecutableFile(path); ApplicationToManageCB.Items.Add(application); } } } catch (Exception exception) { GuiUtils.HandleException(this.Text, System.Reflection.MethodBase.GetCurrentMethod(), exception); } }
/// <summary> /// Updates the dialog with the configuration. /// </summary> private void Update(ManagedApplication application) { if (application == null) { application = new ManagedApplication(); } m_application = application; SetIsSdkApplication(application.IsSdkCompatible); ApplicationNameTB.Text = application.DisplayName; ExecutableFileTB.Text = application.ExecutablePath; ConfigurationFileTB.Text = application.ConfigurationPath; CertificateTB.Text = null; TrustListTB.Text = null; m_certificate = application.Certificate; m_trustList = application.TrustList; if (m_certificate != null) { X509Certificate2 certificate = m_certificate.Find(); if (certificate != null) { CertificateTB.Text = certificate.Subject; } else { CertificateTB.Text = m_certificate.ToString(); } } if (m_trustList != null) { TrustListTB.Text = m_trustList.ToString(); } }