ServerValidatingClient.cs

// edtFTPnet/PRO Example 2
// 
// Copyright (C) 2004 Enterprise Distributed Technologies Ltd
// 
// www.enterprisedt.com

using System;
using System.IO;
using EnterpriseDT.Util.Debug;
using EnterpriseDT.Net.Ftp;
using EnterpriseDT.Net.Ftp.Pro;

public class ServerValidatingClient 
{
        public static void Main(string[] args)
        {
                // we want remote host, user name and password
                if (args.Length < 4) {
                        System.Console.Out.WriteLine("Usage: ServerValidatingClient remote-host username password filename (hostname-checking)");
                        System.Console.Out.WriteLine("See readme.html for instructions.");
                        System.Environment.Exit(1);
                }

                // extract command-line arguments
                string host = args[0];
                string username = args[1];
                string password = args[2];
                string filename = args[3];
                bool enableHostNameChecking = args.Length<5 || args[4].ToLower().Equals("on");

                // set up logger so that we get some output
                Logger log = Logger.GetLogger(typeof(ServerValidatingClient));
                Logger.CurrentLevel = Level.INFO;

                try 
                {
                        // create client
                        log.Info("Creating FTPS (explicit) client");
                        ProFTPClient ftp = new ProFTPClient();
                        ftp.RemoteHost = host;
                        ftp.ServerCompatibility = ProFTPClient.CompatibilityFlags.DisableDataSSLClosure;
                        
                        // NOTE: The DisableControlSSLClosure & DisableDataSSLClosure flags
                        // are included in this example for the sake of compatibility with
                        // as wide a range of servers as possible. If possible it should be 
                        // avoided as it opens the possibility of truncation attacks (
                        // i.e. attacks where data is compromised through premature 
                        // disconnection).
            
                        if (!enableHostNameChecking) 
                        {
                                // Disable host-name checking (only recommended when testing)
                                log.Info("Disable host-name checking (only recommended when testing)");
                                ftp.ServerValidation = ProFTPClient.ServerValidationType.AutomaticNoNameCheck;
                        }
                        else
                        {
                            log.Info("Host-name checking enabled");
                            ftp.ServerValidation = ProFTPClient.ServerValidationType.Automatic;
                        }

                        // connect to the server
                        log.Info("Connecting to server " + host);
                        ftp.Connect();

                        // switch to SSL on control channel
                        log.Info("Switching to FTPS (explicit mode)");
                        ftp.Auth(ProFTPClient.SecurityMechanism.TLS);

                        // log in
                        log.Info(
                                "Logging in with username="
                                        + username);
                        ftp.Login(username, password);

                        // set up passive ASCII transfers
                        log.Info("Setting up passive, ASCII transfers");
                        ftp.ConnectMode = FTPConnectMode.PASV;
                        ftp.TransferType = FTPTransferType.ASCII;

                        // get directory and display it
                        log.Info("Directory before put:");
                        string[] files = ftp.Dir(".", true);
                        ShowFiles(log, files);

                        // copy file to server
                        log.Info("Putting " + filename + " to server");
                        ftp.Put(filename, Path.GetFileName(filename));

                        // get directory and print it to console
                        log.Info("Directory after put:");
                        files = ftp.Dir(".", true);
                        ShowFiles(log, files);

                        // copy file from server
                        log.Info(
                                "Getting "
                                        + Path.GetFileName(filename)
                                        + " from server and saving as "
                                        + filename
                                        + ".copy");
                        ftp.Get(filename + ".copy", Path.GetFileName(filename));

                        // delete file from server
                        log.Info("Deleting " + Path.GetFileName(filename));
                        ftp.Delete(Path.GetFileName(filename));

                        // get directory and print it to console
                        log.Info("Directory after delete:");
                        files = ftp.Dir("", true);
                        ShowFiles(log, files);

                        // Shut down client
                        log.Info("Quitting client");
                        ftp.Quit();

                        log.Info("Test complete");
                } 
                catch (Exception e)
                {
                        log.Error("Caught exception " + e.GetType().FullName + " " + e.Message, e);
                }
        }

        private static void ShowFiles(Logger log, string[] files) {
                if (files.Length == 0)
                        log.Info("  no files");
                else
                        for (int i = 0; i < files.Length; i++)
                                log.Info("  " + files[i]);
        }
}

Generated on Wed Jan 26 19:05:03 2005 for NonvalidatingClient by  doxygen 1.4.1