public override bool Connect(SftpOptions options) { if (options.HasError()) { this.error = options.GetError(); return(false); } bool useKey = false; if (SecurityUtils.compareStrings("", options.KeyPath) || SecurityUtils.compareStrings("", options.User) || SecurityUtils.compareStrings("", options.KeyPassword)) { useKey = false; if (SecurityUtils.compareStrings("", options.User) || SecurityUtils.compareStrings("", options.Password)) { this.error.setError("SF001", "Authentication misconfiguration"); return(false); } else { useKey = false; } } else { useKey = true; } if (SecurityUtils.compareStrings("", options.Host)) { this.error.setError("SF003", "Empty host"); return(false); } try { SetupChannelSftp(options, useKey); if (this.channel == null) { return(false); } if (!this.channel.IsConnected) { this.channel.Connect(); } } catch (Exception e) { this.error.setError("SF004", e.Message); return(false); } this.whiteList = options.WhiteList; return(true); }
/******** EXTERNAL OBJECT PUBLIC METHODS - END ********/ private void SetupChannelSftp(SftpOptions options, bool useKey) { List <AuthenticationMethod> method = new List <AuthenticationMethod>(); if (useKey) { PrivateKeyFile keyFile = new PrivateKeyFile(options.KeyPath, options.KeyPassword); method.Add(new PrivateKeyAuthenticationMethod(options.User, keyFile)); } else { method.Add(new PasswordAuthenticationMethod(options.User, options.Password)); } ConnectionInfo con = new ConnectionInfo(options.Host, options.Port, options.User, method.ToArray()); if (options.AllowHostKeyChecking) { if (SecurityUtils.compareStrings("", options.KnownHostsPath)) { this.error.setError("SF009", "Options misconfiguration, known_hosts path is empty but host key checking is true"); return; } checkFingerpint(con, options.KnownHostsPath); if (this.fingerprint) { this.channel = new Renci.SshNet.SftpClient(con); } } else { this.channel = new Renci.SshNet.SftpClient(con); } }