コード例 #1
0
        public clsSFTP()
        {
#if (DEBUG)
            m_rec_sftp = (from s in Program.dbData3060.Tblsftp where s.Navn == "TestHD35" select s).First();
            //m_rec_sftp = (from s in Program.dbData3060.Tblsftp where s.Navn == "Test" select s).First();
            //m_rec_sftp = (from s in Program.dbData3060.Tblsftp where s.Navn == "Produktion" select s).First();
#else
            m_rec_sftp = (from s in Program.dbData3060.Tblsftp where s.Navn == "Produktion" select s).First();
#endif

            m_sftp = new SFtp();
            bool success = m_sftp.UnlockComponent("HAFSJOSSH_6pspJCMP1QnW");
            if (!success)
            {
                throw new Exception(m_sftp.LastErrorText);
            }
            m_sftp.ConnectTimeoutMs = 5000;
            m_sftp.IdleTimeoutMs    = 15000;
            success = m_sftp.Connect(m_rec_sftp.Host, int.Parse(m_rec_sftp.Port));
            if (!success)
            {
                throw new Exception(m_sftp.LastErrorText);
            }

            Chilkat.SshKey key = new Chilkat.SshKey();

            string privKey = m_rec_sftp.Certificate;
            if (privKey == null)
            {
                throw new Exception(m_sftp.LastErrorText);
            }

            key.Password = m_rec_sftp.Pincode;
            success      = key.FromOpenSshPrivateKey(privKey);
            if (!success)
            {
                throw new Exception(m_sftp.LastErrorText);
            }

            success = m_sftp.AuthenticatePk(m_rec_sftp.User, key);
            if (!success)
            {
                throw new Exception(m_sftp.LastErrorText);
            }

            //  After authenticating, the SFTP subsystem must be initialized:
            success = m_sftp.InitializeSftp();
            if (!success)
            {
                throw new Exception(m_sftp.LastErrorText);
            }
        }
コード例 #2
0
        internal SftpLogFileInfoChilkat(SftpFileSystem sftFileSystem, Uri fileUri, ILogExpertLogger logger)
        {
            _logger         = logger;
            _sftFileSystem  = sftFileSystem;
            Uri             = fileUri;
            _remoteFileName = Uri.PathAndQuery;

            //  Any string automatically begins a fully-functional 30-day trial.
            bool success;

            success = _sftp.UnlockComponent("PUT_SERIAL_HERE");
            if (success != true)
            {
                _logger.LogError(_sftp.LastErrorText);
                MessageBox.Show(_sftp.LastErrorText);
                return;
            }

            int port = Uri.Port != -1 ? Uri.Port : 22;

            success = _sftp.Connect(Uri.Host, port);
            if (success != true)
            {
                _logger.LogError(_sftp.LastErrorText);
                MessageBox.Show(_sftp.LastErrorText);
                return;
            }

            success = false;
            bool cancelled = false;

            if (_sftFileSystem.ConfigData.UseKeyfile)
            {
                lock (_sshKeyMonitor) // prevent multiple password dialogs when opening multiple files at once
                {
                    while (_sftFileSystem.SshKey == null)
                    {
                        // Load key from file, possibly encrypted by password

                        SshKey sshKey  = new SshKey();
                        string keyText = sshKey.LoadText(_sftFileSystem.ConfigData.KeyFile);

                        PrivateKeyPasswordDialog dlg          = new PrivateKeyPasswordDialog();
                        DialogResult             dialogResult = dlg.ShowDialog();
                        if (dialogResult == DialogResult.Cancel)
                        {
                            cancelled = true;
                            break;
                        }

                        sshKey.Password = dlg.Password;
                        if (_sftFileSystem.ConfigData.KeyType == KeyType.Ssh)
                        {
                            logger.Info("Loading SSH key from " + _sftFileSystem.ConfigData.KeyFile);
                            success = sshKey.FromOpenSshPrivateKey(keyText);
                        }
                        else
                        {
                            logger.Info("Loading Putty key from " + _sftFileSystem.ConfigData.KeyFile);
                            success = sshKey.FromPuttyPrivateKey(keyText);
                        }

                        if (!success)
                        {
                            MessageBox.Show("Loading key file failed");
                        }
                        else
                        {
                            _sftFileSystem.SshKey = sshKey;
                        }
                    }
                }

                if (!cancelled)
                {
                    success = false;
                    Credentials credentials = _sftFileSystem.GetCredentials(Uri, true, true);
                    while (!success)
                    {
                        success = _sftp.AuthenticatePk(credentials.UserName, _sftFileSystem.SshKey);
                        if (!success)
                        {
                            FailedKeyDialog dlg = new FailedKeyDialog();
                            DialogResult    res = dlg.ShowDialog();
                            dlg.Dispose();
                            if (res == DialogResult.Cancel)
                            {
                                return;
                            }

                            if (res == DialogResult.OK)
                            {
                                break; // go to user/pw auth
                            }

                            // retries with disabled cache
                            credentials = _sftFileSystem.GetCredentials(Uri, false, true);
                        }
                    }
                }
            }

            if (!success)
            {
                // username/password auth

                Credentials credentials = _sftFileSystem.GetCredentials(Uri, true, false);
                success = _sftp.AuthenticatePw(credentials.UserName, credentials.Password);
                if (success != true)
                {
                    // first fail -> try again with disabled cache
                    credentials = _sftFileSystem.GetCredentials(Uri, false, false);
                    success     = _sftp.AuthenticatePw(credentials.UserName, credentials.Password);
                    if (success != true)
                    {
                        // 2nd fail -> abort
                        MessageBox.Show("Authentication failed!");
                        //MessageBox.Show(sftp.LastErrorText);
                        return;
                    }
                }
            }

            success = _sftp.InitializeSftp();
            if (success != true)
            {
                _logger.LogError(_sftp.LastErrorText);
                MessageBox.Show(_sftp.LastErrorText);
                return;
            }

            OriginalLength = _lastLength = Length;
        }
コード例 #3
0
ファイル: clsSFTP.cs プロジェクト: hafsjold/snvrepos
        public clsSFTP()
        {
            clsRest   objRest    = new clsRest();
            string    strxmldata = objRest.HttpGet2(clsRest.urlBaseType.data, "sftp/" + Program.sftpName);
            XDocument xdoc       = XDocument.Parse(strxmldata);

            string Status = xdoc.Descendants("Status").First().Value;

            if (Status != "True")
            {
                throw new Exception("Getting sftp-data for " + Program.sftpName + " failed.");
            }

            m_SftpId      = xdoc.Descendants("Id").First().Value;
            m_SftpNavn    = xdoc.Descendants("Navn").First().Value;
            m_Host        = xdoc.Descendants("Host").First().Value;
            m_Port        = xdoc.Descendants("Port").First().Value;
            m_Certificate = xdoc.Descendants("Certificate").First().Value;
            m_Pincode     = xdoc.Descendants("Pincode").First().Value;
            m_User        = xdoc.Descendants("User").First().Value;
            m_Outbound    = xdoc.Descendants("Outbound").First().Value;
            m_Inbound     = xdoc.Descendants("Inbound").First().Value;


            m_sftp = new SFtp();
            bool success = m_sftp.UnlockComponent("HAFSJOSSH_6pspJCMP1QnW");

            if (!success)
            {
                throw new Exception(m_sftp.LastErrorText);
            }
            m_sftp.ConnectTimeoutMs = 60000;
            m_sftp.IdleTimeoutMs    = 55000;
            success = m_sftp.Connect(m_Host, int.Parse(m_Port));
            if (!success)
            {
                throw new Exception(m_sftp.LastErrorText);
            }
            Chilkat.SshKey key = new Chilkat.SshKey();

            string privKey = m_Certificate;

            if (privKey == null)
            {
                throw new Exception(m_sftp.LastErrorText);
            }

            key.Password = m_Pincode;
            success      = key.FromOpenSshPrivateKey(privKey);
            if (!success)
            {
                throw new Exception(m_sftp.LastErrorText);
            }

            success = m_sftp.AuthenticatePk(m_User, key);
            if (!success)
            {
                throw new Exception(m_sftp.LastErrorText);
            }

            //  After authenticating, the SFTP subsystem must be initialized:
            success = m_sftp.InitializeSftp();
            if (!success)
            {
                throw new Exception(m_sftp.LastErrorText);
            }
        }