Esempio n. 1
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;
        }
Esempio n. 2
0
 public SftpFileSystem(IFileSystemCallback callback)
 {
     _logger          = callback.GetLogger();
     CredentialsCache = new CredentialCache();
 }