Exemple #1
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;
        }
Exemple #2
0
 /// <summary>
 /// Copies the protocol information form the given stream.
 /// </summary>
 /// <param name="sslStream"></param>
 private void SetSslInfo(SslStream sslStream)
 {
     sslInfo = new SslInfo()
     {
         SslProtocol = sslStream.SslProtocol,
         CipherAlgorithm = sslStream.CipherAlgorithm,
         CipherStrength = sslStream.CipherStrength,
         HashAlgorithm = sslStream.HashAlgorithm,
         HashStrength = sslStream.HashStrength,
         KeyExchangeAlgorithm = sslStream.KeyExchangeAlgorithm,
         KeyExchangeStrength = sslStream.KeyExchangeStrength
     };
 }