Пример #1
0
 /// <summary>
 /// Creates a new instance of an FTPS server.
 /// </summary>
 /// <param name="displayname">The name to display for this FTPS server.</param>
 /// <param name="hostname">The address of this server.</param>
 /// <param name="port">The port to use for this FTPS server. (usually 21 or 990)</param>
 /// <param name="username">The username to connect to this server with.</param>
 /// <param name="password">The password to connect to this server with.</param>
 /// <param name="anonlogin">If true, connect to this server anonymously (without a username and password).</param>
 /// <param name="passivemode">If true, connect in passive mode. If false, connect in active mode.</param>
 /// <param name="sslmode">The mode used for interacting with the FTPS server. Specifically, what connections should be secured and when.</param>
 /// <param name="X509cert">The path of the client X.509 certificate file to use.</param>
 /// <param name="certpass">The password to access the X.509 certificate file.</param>
 public FTPSServer(string displayname, string hostname, int port, string username, string password, bool anonlogin, bool passivemode, ESSLSupportMode sslmode, string X509cert = "", string certpass = "")
 {
     _displayname = displayname;
     _server      = hostname;
     _port        = port;
     _username    = username;
     _password    = password;
     _service     = AccountService.FTPS;
     _type        = AccountType.Server;
     _anon        = anonlogin;
     _passive     = passivemode;
     _sslmode     = sslmode;
     if (X509cert != "")
     {
         if (certpass != "")
         {
             _client = new X509Certificate(X509cert, certpass);
         }
         else
         {
             _client = new X509Certificate(X509cert);
         }
     }
     else
     {
         _client = null;
     }
 }
Пример #2
0
        //private string hostname, username, password;

        public static void UploadFiles(ConfigurationProfile profile, string temppath, string remotedir)
        {
            // Setup session options
            // add support for: scp/sftp protocols, ssh host/private keys, active/passive mode, port, FtpSecure, timeout, ssl cert,

            using (FTPSClient session = new FTPSClient())
            {
                ESSLSupportMode sslSupportMode = ESSLSupportMode.ClearText;
                RemoteCertificateValidationCallback userValidateServerCertificate;
                userValidateServerCertificate = new RemoteCertificateValidationCallback(ValidateServerCertificate);

                // enable encryption if desired
                if (profile.Encryption.IsTrue())
                {
                    sslSupportMode |= ESSLSupportMode.ControlAndDataChannelsRequired | ESSLSupportMode.CredentialsRequired;
                    if (profile.EncryptionImplicit.IsTrue())
                    {
                        // implicit if desired
                        sslSupportMode |= ESSLSupportMode.Implicit;
                    }
                    if (profile.ForceEncryption.IsTrue())
                    {
                        // force encryption if desired
                        userValidateServerCertificate = new RemoteCertificateValidationCallback(delegate { return(true); });
                    }
                }

                session.Connect(profile.Hostname, new System.Net.NetworkCredential(profile.Username, profile.Password), sslSupportMode, userValidateServerCertificate);

                // Upload files
                //TransferOptions transferOptions = new TransferOptions();
                //transferOptions.TransferMode = TransferMode.Binary;

                //TransferOperationResult transferResult;
                //transferResult = session.PutFiles(Path.Combine(temppath, "*"), Common.Parse(remotedir), false, transferOptions);

                try
                {
                    session.SetCurrentDirectory(Common.ParseTemplate(remotedir));
                }
                catch
                {
                    session.MakeDir(Common.ParseTemplate(remotedir));
                }
                session.PutFiles(temppath, Common.ParseTemplate(remotedir), "*", EPatternStyle.Wildcard, false, new FileTransferCallback(TransferCallback));

                // Throw on any error
                //transferResult.Check();

                // Print results
                //foreach (TransferEventArgs transfer in transferResult.Transfers)
                //{
                //    Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
                //}
            }
        }
 public string Connect(string address, NetworkCredential credentials, ESSLSupportMode esslSupportMode)
 {
     _client = new FTPSClient();
     return _client.Connect(address, credentials, esslSupportMode);
 }
Пример #4
0
        /// <summary>
        /// Connects to a FTP server using the provided parameters. 
        /// The default representation tipe is set to Binary.
        /// The text encoding is set to UTF8, if supported by the server via the FEAT command.
        /// </summary>
        /// <param name="hostname"></param>
        /// <param name="port"></param>
        /// <param name="credential"></param>
        /// <param name="sslSupportMode"></param>
        /// <param name="userValidateServerCertificate"></param>
        /// <param name="x509ClientCert"></param>
        /// <param name="sslMinKeyExchangeAlgStrength"></param>
        /// <param name="sslMinCipherAlgStrength"></param>
        /// <param name="sslMinHashAlgStrength"></param>
        /// <param name="timeout">Connection timeout in ms. <c>null</c> can be specifiad to keep the default value of 120s.</param>
        /// <param name="useCtrlEndPointAddressForData"><c>true</c> to use the control channel remote address for data connections instead of the address returned by PASV</param>
        /// <returns>The text of the \"welcome message\" sent by the server.</returns>
        public string Connect(string hostname, int port, NetworkCredential credential, ESSLSupportMode sslSupportMode, 
                            RemoteCertificateValidationCallback userValidateServerCertificate, X509Certificate x509ClientCert, 
                            int sslMinKeyExchangeAlgStrength, int sslMinCipherAlgStrength, int sslMinHashAlgStrength, 
                            int? timeout, bool useCtrlEndPointAddressForData)
        {
            Close();

            // Anonymous authentication
            if (credential == null)
                credential = new NetworkCredential(anonUsername, anonPassword);

            if (timeout != null)
                this.timeout = timeout.Value;

            this.sslClientCert = x509ClientCert;

            this.userValidateServerCertificate = userValidateServerCertificate;

            this.sslMinKeyExchangeAlgStrength = sslMinKeyExchangeAlgStrength;
            this.sslMinCipherAlgStrength = sslMinCipherAlgStrength;
            this.sslMinHashAlgStrength = sslMinHashAlgStrength;

            this.sslSupportRequestedMode = sslSupportMode;
            this.sslSupportCurrentMode = sslSupportMode;

            this.useCtrlEndPointAddressForData = useCtrlEndPointAddressForData;

            sslInfo = null;

            features = null;

            transferMode = ETransferMode.ASCII;
            textEncoding = ETextEncoding.ASCII;

            bannerMessage = null;
            welcomeMessage = null;            

            currDirStack.Clear();

            // Ok, member initialization is done. Start with setting up a control connection
            SetupCtrlConnection(hostname, port, Encoding.ASCII);

            // Used later for SSL/TLS auth
            this.hostname = hostname;

            // Implicit SSL/TLS
            bool isImplicitSsl = (sslSupportMode & ESSLSupportMode.Implicit) == ESSLSupportMode.Implicit;
            if (isImplicitSsl)
                SwitchCtrlToSSLMode();

            // Wait fot server message
            bannerMessage = GetReply().Message;

            // Explicit SSL/TLS
            if (!isImplicitSsl)
                SslControlChannelCheckExplicitEncryptionRequest(sslSupportMode);

            // Login. Note that a password might not be required
            // TODO: check if the welcomeMessage is returned by the USER command in case the PASS command is not required.  
            if(UserCmd(credential.UserName))
                welcomeMessage = PassCmd(credential.Password);

            GetFeaturesFromServer();

            if (IsControlChannelEncrypted && !isImplicitSsl)
            {
                SslDataChannelCheckExplicitEncryptionRequest();

                if ((sslSupportMode & ESSLSupportMode.ControlChannelRequested) != ESSLSupportMode.ControlChannelRequested)
                    SSlCtrlChannelCheckRevertToClearText();
            }

            try
            {
                // This is required by some FTP servers and must precede any OPTS command
                if (CheckFeature("CLNT"))
                    ClntCmd(clntName);

                // Set UTF8 as character encoding, but only if listed among the FEAT features
                if (CheckFeature("UTF8"))
                    SetTextEncoding(ETextEncoding.UTF8);
            }
            catch (Exception ex)
            {
                //TODO: add warning info
            }

            // Default binary transfers
            SetTransferMode(ETransferMode.Binary);

            return welcomeMessage;
        }
Пример #5
0
 /// <summary>
 /// Connects to a FTP server using the provided parameters. 
 /// The default representation tipe is set to Binary.
 /// The text encoding is set to UTF8, if supported by the server via the FEAT command.
 /// </summary>
 /// <param name="hostname"></param>
 /// <param name="port"></param>
 /// <param name="credential"></param>
 /// <param name="sslSupportMode"></param>
 /// <param name="userValidateServerCertificate"></param>
 /// <param name="x509ClientCert"></param>
 /// <param name="sslMinKeyExchangeAlgStrength"></param>
 /// <param name="sslMinCipherAlgStrength"></param>
 /// <param name="sslMinHashAlgStrength"></param>
 /// <param name="timeout">Connection timeout in ms. <c>null</c> can be specifiad to keep the default value of 120s.</param>
 /// <returns>The text of the \"welcome message\" sent by the server.</returns>
 public string Connect(string hostname, int port, NetworkCredential credential, ESSLSupportMode sslSupportMode, 
                     RemoteCertificateValidationCallback userValidateServerCertificate, X509Certificate x509ClientCert, 
                     int sslMinKeyExchangeAlgStrength, int sslMinCipherAlgStrength, int sslMinHashAlgStrength, 
                     int? timeout)
 {
     return Connect(hostname, port, credential, sslSupportMode, userValidateServerCertificate, x509ClientCert,
                    sslMinKeyExchangeAlgStrength, sslMinCipherAlgStrength, sslMinHashAlgStrength, timeout, true);
 }
Пример #6
0
 public string Connect(string hostname, NetworkCredential credential, ESSLSupportMode sslSupportMode, 
                     RemoteCertificateValidationCallback userValidateServerCertificate)
 {
     // Default implicit FTPS port is 990, default standard and explicit FTPS port is 21
     int port = (sslSupportMode & ESSLSupportMode.Implicit) == ESSLSupportMode.Implicit ? 990 : 21;
     return Connect(hostname, port, credential, sslSupportMode, userValidateServerCertificate, null, 0, 0, 0, null);
 }
Пример #7
0
 public string Connect(string hostname, NetworkCredential credential, ESSLSupportMode sslSupportMode)
 {
     return Connect(hostname, credential, sslSupportMode, null);
 }
Пример #8
0
 /// <summary>
 /// Anonymous authentication
 /// </summary>
 /// <param name="hostname"></param>
 /// <returns>The text of the \"welcome message\" sent by the server.</returns>
 public string Connect(string hostname, ESSLSupportMode sslSupportMode)
 {
     return Connect(hostname, null, sslSupportMode);
 }
Пример #9
0
        private void SslControlChannelCheckExplicitEncryptionRequest(ESSLSupportMode sslSupportMode)
        {
            if ((sslSupportMode & ESSLSupportMode.CredentialsRequested) == ESSLSupportMode.CredentialsRequested)
                try
                {
                    AuthCmd(EAuthMechanism.TLS);
                }
                catch (FTPCommandException ex)
                {
                    if ((sslSupportMode & ESSLSupportMode.CredentialsRequired) == ESSLSupportMode.CredentialsRequired)
                        if (ex.ErrorCode == 530 || ex.ErrorCode == 534)
                            throw new FTPSslException("SSL/TLS connection not supported on server", ex);
                        else
                            throw ex;

                    sslSupportCurrentMode = ESSLSupportMode.ClearText;
                }
        }
Пример #10
0
        private void SslDataChannelCheckExplicitEncryptionRequest()
        {
            if ((sslSupportCurrentMode & ESSLSupportMode.DataChannelRequested) == ESSLSupportMode.DataChannelRequested)
            {
                PbszCmd(0);

                try
                {
                    ProtCmd(EProtCode.P);
                }
                catch (FTPCommandException ex)
                {
                    // Note: MS FTP 7.0 returns 536, but RFC 2228 requires 534
                    if ((sslSupportCurrentMode & ESSLSupportMode.DataChannelRequired) == ESSLSupportMode.DataChannelRequired)
                        if (ex.ErrorCode == 534 || ex.ErrorCode == 536)
                            throw new FTPSslException("The server policy denies SSL/TLS", ex);
                        else
                            throw ex;

                    sslSupportCurrentMode ^= ESSLSupportMode.DataChannelRequired;

                    // Data channel transfers will be done in clear text
                    ProtCmd(EProtCode.C);
                }
            }
        }
Пример #11
0
 private void SSlCtrlChannelCheckRevertToClearText()
 {
     // Back to clear text mode, but only if supported by the server
     if (CheckFeature("CCC"))
         CccCmd();
     else
         sslSupportCurrentMode |= ESSLSupportMode.ControlChannelRequested;
 }
 public FtpsVirtualPathProvider(ESSLSupportMode sslMode, string host, int port, string username, string password)
     : base(new FTPSClient(), host, port, username, password)
 {
     this.sslMode = sslMode;
 }