/// <summary> /// Get the imap4 server information from /// the configuration file, the host and /// port of the imap4 server is located in /// the default section, this data is used /// to connect to the imap4 server. /// </summary> private void GetImap4ServerHost() { try { // Create a new connection adapter. _connection = new Imap4ConnectionAdapter(); // If no host name set then // use the defualt values. if (String.IsNullOrEmpty(_imap4ServerName)) { // Create a new default host type // an load the values from the configuration // file into the default host type. ProxyImap4ServerDefaultHost defaultHost = (ProxyImap4ServerDefaultHost)System.Configuration.ConfigurationManager.GetSection( "ProxyImap4ServerGroup/ProxyImap4ServerDefaultHost"); // If the port is greater than zero then // assign the port number. if (defaultHost.HostSection.PortAttribute > 0) { _connection.Port = defaultHost.HostSection.PortAttribute; } // Get the imap4 server. _connection.Server = defaultHost.HostSection.HostAttribute; } else { // Create a new host type // an load the values from the configuration // file into the host type. ProxyImap4ServerHosts hosts = (ProxyImap4ServerHosts)System.Configuration.ConfigurationManager.GetSection( "ProxyImap4ServerGroup/ProxyImap4ServerHosts"); // If the port is greater than zero then // assign the port number. if (hosts.HostSection[_imap4ServerName].PortAttribute > 0) { _connection.Port = hosts.HostSection[_imap4ServerName].PortAttribute; } // Get the imap4 server. _connection.Server = hosts.HostSection[_imap4ServerName].HostAttribute; } } catch (Exception e) { // Detect a thread abort exception. if (e is ThreadAbortException) { Thread.ResetAbort(); } base.Write("Imap4TlsProxyServer", "GetListeningPort", e.Message, 246, WriteTo.EventLog, LogType.Error); } }
/// <summary> /// Get the maximum number of clients from the configuration /// file, if no value is specified then default or /// the current value will be used. /// </summary> private void GetMaxNumClients() { try { // If no host name set then // use the defualt values. if (String.IsNullOrEmpty(_hostName)) { // Create a new default host type // an load the values from the configuration // file into the default host type. ProxyImap4ServerDefaultHost defaultHost = (ProxyImap4ServerDefaultHost)System.Configuration.ConfigurationManager.GetSection( "ProxyImap4ServerGroup/ProxyImap4ServerDefaultHost"); // If the port is greater than zero then // assign the port number. if (defaultHost.HostSection.MaxNumClientsAttribute > 0) { _maxNumClients = defaultHost.HostSection.MaxNumClientsAttribute; } } else { // Create a new host type // an load the values from the configuration // file into the host type. ProxyImap4ServerHosts hosts = (ProxyImap4ServerHosts)System.Configuration.ConfigurationManager.GetSection( "ProxyImap4ServerGroup/ProxyImap4ServerHosts"); // If the port is greater than zero then // assign the port number. if (hosts.HostSection[_hostName].MaxNumClientsAttribute > 0) { _maxNumClients = hosts.HostSection[_hostName].MaxNumClientsAttribute; } } // Create a new client array. _client = new Imap4TlsProxyConnection[_maxNumClients]; } catch (Exception e) { // Detect a thread abort exception. if (e is ThreadAbortException) { Thread.ResetAbort(); } base.Write("Imap4TlsProxyServer", "GetMaxNumClients", e.Message, 246, WriteTo.EventLog, LogType.Error); } }
/// <summary> /// Get the server credentials from the certificate store or file. /// </summary> /// <returns>The x509 certificate else null.</returns> private X509Certificate2 GetServerCredentials() { X509Certificate2 certificate = null; try { // Create a new default host type // an load the values from the configuration // file into the default host type. ProxyImap4ServerDefaultHost defaultHost = (ProxyImap4ServerDefaultHost)System.Configuration.ConfigurationManager.GetSection( "ProxyImap4ServerGroup/ProxyImap4ServerDefaultHost"); // Make sure the section is defined. if (defaultHost == null) { throw new Exception("Configuration section ProxyImap4ServerDefaultHost has not been defined."); } // Get the server credetials element. ProxyImap4ServerCredentialsElement serverCredentials = defaultHost.ServerCredentialsSection; if (serverCredentials == null) { throw new Exception("Configuration element ServerCredentials has not been defined."); } // If using the certificate store. if (serverCredentials.UseCertificateStore) { // Get the certificate from the store. ProxyImap4ServerCredentialsStoreElement certificateStore = serverCredentials.CertificateStore; if (certificateStore == null) { throw new Exception("Configuration element CertificateStore has not been defined."); } // Get the certificate refrence details from the certificate store. certificate = X509Certificate2Store.GetCertificate( certificateStore.StoreName, certificateStore.StoreLocation, certificateStore.X509FindType, certificateStore.FindValue, false); } else { // Get the certificate path ProxyImap4ServerCredentialsPathElement certificatePath = serverCredentials.CertificatePath; if (certificatePath == null) { throw new Exception("Configuration element CertificatePath has not been defined."); } // Get the certificate path details and create // the x509 certificate reference. certificate = X509Certificate2Store.GetCertificate(certificatePath.Path, certificatePath.Password); } } catch (Exception e) { // Detect a thread abort exception. if (e is ThreadAbortException) { Thread.ResetAbort(); } base.Write("Imap4TlsProxyConnection", "GetServerCredentials", e.Message, 711, WriteTo.EventLog, LogType.Error); } // Return the certificate. return(certificate); }