/// <summary>
        /// Creates a new ManagementLogic object.
        /// </summary>
        /// <param name="ovpn">parent</param>
        /// <param name="host">host to connect to (e.g. 127.0.0.1)</param>
        /// <param name="port">port to connect to</param>
        /// <param name="logs">LogManager to write the logs to</param>
        /// <param name="receiveOldLogs">Should old log lines be received?</param>
        public ManagementLogic(Connection ovpn,string host,
                               int port,LogManager logs,bool receiveOldLogs)
        {
            m_ovpn           = ovpn;
            m_logs           = logs;
            m_releaselock    = true;
            m_receiveOldLogs = receiveOldLogs;

            // initialize required components
            m_ovpnComm      = new Communicator(host,port,logs);
            m_ovpnMParser   = new ManagementParser(m_ovpnComm,this);
            m_pkcs11details = new List <PKCS11Detail>();

            m_ovpnComm.connectionClosed += new System.EventHandler(m_ovpnComm_connectionClosed);
        }
 private void Dispose(bool disposing)
 {
     if (!disposed)
     {
         if (disposing)
         {
             m_ovpnComm.Dispose();
         }
         m_logs          = null;
         m_ovpnMParser   = null;
         m_pkcs11details = null;
         m_todo          = null;
         m_ovpn          = null;
         disposed        = true;
     }
 }
        private void ProcessSyncEventPkcs11GetKeys(string msg)
        {
            PKCS11Detail d = ManagementParser.getPKCS11ID(msg);

            if (d != null)
            {
                m_pkcs11details.Add(d);

                if (d.Number < m_pkcs11count - 1)
                {
                    m_ovpnComm.send("pkcs11-id-get " + (d.Number + 1));
                }

                else
                {
                    releaseLock();

                    int kid = m_ovpn.getKeyID(m_pkcs11details);

                    if (kid == NeedCardIdEventArgs.Retry)
                    {
                        setLock(WaitState.PKCS11_GET_COUNT);
                        m_ovpnComm.send("pkcs11-id-count");
                    }
                    else if (kid != NeedCardIdEventArgs.None)
                    {
                        m_ovpnComm.send("needstr 'pkcs11-id-request' '" +
                                        m_pkcs11details[kid].Id + "'");
                    }
                }
            }

            // error in parsing
            else
            {
                m_logs.logDebugLine(1,
                                    "Error while parsing pkcs11-id-get: \"" +
                                    msg + "\"");

                releaseLock();
            }
        }
        private void ProcessSyncEventPkcs11GetCount(string msg)
        {
            m_pkcs11count = ManagementParser.getPKCS11IDCount(msg);

            if (m_pkcs11count == -1)
            {
                m_logs.logLine(LogType.Management,
                               "Could not determine the number of pkcs11-ids:\"" +
                               msg + "\"");
                releaseLock();
            }

            else if (m_pkcs11count == 0)
            {
                m_logs.logLine(LogType.Management,
                               "No pkcs11-ids were found");

                int id = m_ovpn.getKeyID(new List <PKCS11Detail>());
                if (id == NeedCardIdEventArgs.Retry)
                {
                    m_ovpnComm.send("pkcs11-id-count");
                }
                else
                {
                    releaseLock();
                }
            }
            else
            {
                m_logs.logLine(LogType.Management,
                               "Got " + m_pkcs11count + " PKCS11-IDs");
                m_pkcs11details.Clear();
                releaseLock();

                setLock(WaitState.PKCS11_GET_KEYS);
                m_ovpnComm.send("pkcs11-id-get 0");
            }
        }
        private void ProcessAsyncEventPassword(AsyncEventDetail aeDetail)
        {
            string pwType = aeDetail.getInfos()[0]; // "Auth" or "Private Key", or ...
            string pwInfo = aeDetail.getInfos()[1]; // "password" or "username/password"
            string pwMsg  = aeDetail.getInfos()[2]; // "Need" or "Verification Failed"

            if (pwMsg.Equals("Need",System.StringComparison.OrdinalIgnoreCase))
            {
                if (pwType.Equals("Auth",
                                  System.StringComparison.OrdinalIgnoreCase) &&
                    pwInfo.Equals("username/password",
                                  System.StringComparison.OrdinalIgnoreCase))
                {
                    // Ask for username/password
                    string[] loginInfo    = m_ovpn.getLoginPass(pwType);
                    bool     sendUserPass = false;
                    if (loginInfo != null)
                    {
                        string username = loginInfo[0];
                        string password = loginInfo[1];
                        if (username != null && pwType.Length > 0 &&
                            password != null && password.Length > 0)
                        {
                            m_ovpnComm.send("username '" + pwType + "' " +
                                            ManagementParser.encodeMsg(username));
                            //wait for processing by OpenVPN or it might not always process the password correctly.
                            m_ovpnComm.processManagementConnectionLine();
                            m_ovpnComm.send("password '" + pwType + "' " +
                                            ManagementParser.encodeMsg(password));
                            sendUserPass = true;
                        }
                    }
                    if (!sendUserPass)
                    {
                        // Send 'bogus' user and pass to keep OpenVPN from quiting on disconnect.. (WORKAROUND)
                        m_ovpnComm.send("username '" + pwType + "' -");
                        m_ovpnComm.send("password '" + pwType + "' -");
                    }
                }
                else
                {
                    string pw = m_ovpn.getPW(pwType);
                    if (pw != null)
                    {
                        if (pw.Length > 0)
                        {
                            m_ovpnComm.send("password '" + pwType + "' " +
                                            ManagementParser.encodeMsg(pw));
                        }
                    }
                }
            }
            else if (pwMsg.Equals("Verification Failed",
                                  System.StringComparison.OrdinalIgnoreCase))
            {
                m_logs.logDebugLine(1,"Authentication Failed said remote server");
            }
            else
            {
                m_logs.logDebugLine(1,"Unknown 'PASSWORD' reply from remote server: " + pwMsg);
            }
        }