Ejemplo n.º 1
0
 protected override void OnExecute(FtpCommandContext context)
 {
     context.Channel.Status = FtpSessionStatus.NotLogin;
     context.Channel.User   = null;
     context.Channel.Send("221 Bye-bye.");
     context.Channel.Close();
 }
        protected override void OnExecute(FtpCommandContext context)
        {
            context.Channel.CheckLogin();

            if (string.IsNullOrEmpty(context.Statement.Argument))
            {
                throw new SyntaxException();
            }

            var arg = context.Statement.Argument.ToUpper();

            if (arg == "A")
            {
                context.Channel.TransferMode = FtpTransferMode.Ascii;
                context.Channel.Send("200 ASCII transfer mode active.");
            }
            else if (arg == "I")
            {
                context.Channel.TransferMode = FtpTransferMode.Binary;
                context.Channel.Send("200 Binary transfer mode active.");
            }
            else
            {
                throw new UnknownTransferModeException();
            }
        }
Ejemplo n.º 3
0
        public static void Format(FtpCommandContext context, FileSystemInfo fileInfo, StringBuilder output)
        {
            var isFile = fileInfo is FileInfo;

            //Size
            output.AppendFormat("size={0};", isFile ? ((FileInfo)fileInfo).Length : 0);

            //Permission
            output.AppendFormat("perm={0}{1};",
                                /* Can read */ isFile ? "r" : "el",
                                /* Can write */ isFile ? "adfw" : "fpcm");

            //Type
            output.AppendFormat("type={0};", isFile ? "file" : "dir");

            //Create
            output.AppendFormat("create={0};", FtpDateUtils.FormatFtpDate(fileInfo.CreationTimeUtc));

            //Modify
            output.AppendFormat("modify={0};", FtpDateUtils.FormatFtpDate(fileInfo.LastWriteTimeUtc));

            //File name
            output.Append(DELIM);
            output.Append(fileInfo.Name);

            output.Append(NEWLINE);
        }
Ejemplo n.º 4
0
        protected override void OnExecute(FtpCommandContext context)
        {
            context.Channel.CheckLogin();

            if (string.IsNullOrEmpty(context.Statement.Argument))
            {
                throw new SyntaxException();
            }

            var path      = context.Statement.Argument;
            var localPath = context.Channel.MapVirtualPathToLocalPath(path);

            context.Statement.Result = localPath;

            if (File.Exists(localPath))
            {
                throw new DirectoryNotFoundException(path);
            }

            try
            {
                Directory.CreateDirectory(localPath);
            }
            catch (Exception)
            {
                throw new InternalException("create dir");
            }

            context.Channel.Send("257 Created directory successfully");
        }
Ejemplo n.º 5
0
        public static string Format(FtpCommandContext context, FileSystemInfo fileInfo)
        {
            var output = new StringBuilder();

            Format(context, fileInfo, output);
            return(output.ToString());
        }
Ejemplo n.º 6
0
        protected override object OnExecute(FtpCommandContext context)
        {
            context.Channel.CheckLogin();

            if (string.IsNullOrEmpty(context.Statement.Argument))
            {
                throw new SyntaxException();
            }

            var    args = context.Statement.Argument;
            string message;

            if (args.Equals("UTF8 ON", StringComparison.OrdinalIgnoreCase))
            {
                context.Channel.Encoding = Encoding.UTF8;
                message = "200 UTF enabled mode.";
            }
            else if (args.Equals("UTF8 OFF", StringComparison.OrdinalIgnoreCase))
            {
                context.Channel.Encoding = Encoding.ASCII;
                message = "200 ASCII enabled mode.";
            }
            else
            {
                throw new SyntaxException();
            }

            context.Channel.Send(message);

            return(message);
        }
Ejemplo n.º 7
0
        protected override void OnExecute(FtpCommandContext context)
        {
            string result;
            var    user = context.Server.Configuration.Users[context.Statement.Argument];

            if (user != null)
            {
                context.Channel.UserName = context.Statement.Argument;

                if (string.IsNullOrEmpty(user.Password))
                {
                    context.Channel.User = user;
                    result = "230 User successfully logged in.";
                    context.Statement.Result = true;
                }
                else
                {
                    result = "331 Password required for " + context.Statement.Argument;
                    context.Statement.Result = false;
                }
            }
            else
            {
                result = "331 Password required for " + context.Statement.Argument;
                context.Statement.Result = false;
            }

            context.Channel.Send(result);
        }
Ejemplo n.º 8
0
        protected override object OnExecute(FtpCommandContext context)
        {
            context.Channel.CheckLogin();

            if (string.IsNullOrEmpty(context.Statement.Argument))
            {
                throw new SyntaxException();
            }

            var changeDir = context.Statement.Argument;

            var path = context.Channel.MapVirtualPathToLocalPath(changeDir);

            if (File.Exists(path) || !Directory.Exists(path))
            {
                throw new DirectoryNotFoundException(changeDir);
            }

            context.Statement.Result = path;

            context.Channel.CurrentDir = context.Channel.MapLocalPathToVirtualPath(path);
            var message = string.Format("250 \"{0}\" is current directory.", context.Channel.CurrentDir);

            context.Channel.Send(message);
            return(message);
        }
Ejemplo n.º 9
0
        protected override void OnExecute(FtpCommandContext context)
        {
            if (string.IsNullOrEmpty(context.Statement.Argument))
            {
                var cmd = context.Statement.Argument;
                if (context.Executor.Root.Children.Contains(cmd))
                {
                    context.Channel.Send(string.Format("214 Command {0} is supported by Ftp Server", cmd.ToUpper()));
                }
                else
                {
                    context.Channel.Send(string.Format("502 Command {0} is not recognized or supported by Ftp Server",
                                                       cmd.ToUpper()));
                }
            }
            else
            {
                var cmds = context.Executor.Root.Children.Keys.ToArray();

                var str = new StringBuilder();
                str.Append("214-The following commands are recognized:");

                for (int i = 0; i < cmds.Length; i++)
                {
                    if (i % 8 == 0)
                    {
                        str.Append("\r\n");
                    }

                    str.AppendFormat("    {0}", cmds[i]);
                }

                context.Channel.Send(str.ToString());
            }
        }
Ejemplo n.º 10
0
        protected override object OnExecute(FtpCommandContext context)
        {
            const string MESSAGE = "250 Deleted file successfully.";

            context.Channel.CheckLogin();

            if (string.IsNullOrEmpty(context.Statement.Argument))
            {
                throw new SyntaxException();
            }

            var path      = context.Statement.Argument;
            var localPath = context.Channel.MapVirtualPathToLocalPath(path);

            context.Statement.Result = localPath;

            if (!Directory.Exists(localPath))
            {
                throw new FileNotFoundException(path);
            }

            try
            {
                File.Delete(localPath);
            }
            catch (Exception)
            {
                throw new InternalException("delete file");
            }

            context.Channel.Send(MESSAGE);

            return(MESSAGE);
        }
Ejemplo n.º 11
0
        protected override object OnExecute(FtpCommandContext context)
        {
            context.Channel.CheckLogin();

            if (string.IsNullOrEmpty(context.Statement.Argument))
            {
                throw new SyntaxException();
            }

            var path      = context.Statement.Argument;
            var localPath = context.Channel.MapVirtualPathToLocalPath(path);

            context.Statement.Result = localPath;

            var info = new FileInfo(localPath);

            if (info.Exists)
            {
                var message = string.Format("213 {0}", FtpDateUtils.FormatFtpDate(info.LastWriteTimeUtc));
                context.Channel.Send(message);
                return(message);
            }

            throw new FileNotFoundException(path);
        }
Ejemplo n.º 12
0
        protected override object OnExecute(FtpCommandContext context)
        {
            context.Channel.CheckLogin();

            context.Channel.CreatePasvDataChannel();

            return(true);
        }
Ejemplo n.º 13
0
        private void WriteFileInfo(FtpCommandContext context, FileSystemInfo fileInfo)
        {
            var result = FtpListFileFormater.Format(context, fileInfo);

            var data = context.Channel.Encoding.GetBytes(result);

            context.Channel.DataChannel.Send(data, 0, data.Length);
        }
Ejemplo n.º 14
0
        protected override object OnExecute(FtpCommandContext context)
        {
            const string MESSAGE = "200 NOOP Command Successful.";

            context.Channel.CheckLogin();
            context.Channel.Send(MESSAGE);
            return(MESSAGE);
        }
Ejemplo n.º 15
0
        protected override void OnExecute(FtpCommandContext context)
        {
            context.Channel.CheckLogin();

            context.Channel.Send(string.Format("257 \"{0}\" is current directory.", context.Channel.CurrentDir));

            context.Statement.Result = context.Channel.MapVirtualPathToLocalPath(context.Channel.CurrentDir);
        }
Ejemplo n.º 16
0
        protected override object OnExecute(FtpCommandContext context)
        {
            context.Channel.CheckLogin();

            if (string.IsNullOrEmpty(context.Statement.Argument))
            {
                throw new SyntaxException();
            }

            context.Channel.CheckDataChannel();

            try
            {
                //context.Channel.Status = FtpSessionStatus.Download;

                var    path      = context.Statement.Argument;
                string localPath = context.Channel.MapVirtualPathToLocalPath(path);
                context.Statement.Result = localPath;

                var fileInfo = new FileInfo(localPath);

                if (!fileInfo.Exists)
                {
                    throw new FileNotFoundException(path);
                }

                var message = "150 Open data connection for file transfer.";

                if (context.Channel.DataChannel.SendFile(fileInfo, context.Channel.FileOffset))
                {
                    message = "226 Transfer complete.";
                }
                else
                {
                    message = "426 Connection closed; transfer aborted.";
                }

                context.Channel.Send(message);
                context.Channel.FileOffset = 0;

                return(message);
            }
            catch (FtpException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new InternalException(e.Message);
            }
            finally
            {
                context.Channel.CloseDataChannel();
                //context.Channel.Status = FtpSessionStatus.Wait;
            }
        }
Ejemplo n.º 17
0
        protected override object OnExecute(FtpCommandContext context)
        {
            const string MESSAGE = "215 UNIX emulated by Zongsoft.FtpServer";

            context.Channel.CheckLogin();

            context.Channel.Send(MESSAGE);

            return(MESSAGE);
        }
Ejemplo n.º 18
0
        protected override object OnExecute(FtpCommandContext context)
        {
            const string MESSAGE = "226 Data connection closed.";

            context.Channel.CheckLogin();
            context.Channel.CloseDataChannel();
            context.Channel.Send(MESSAGE);

            return(MESSAGE);
        }
Ejemplo n.º 19
0
        private static char[] GetPermission(FtpCommandContext context, FileSystemInfo fileInfo)
        {
            var perm = ("----------").ToCharArray();

            perm[0] = fileInfo is DirectoryInfo ? 'd' : '-';
            perm[1] = 'r';
            perm[2] = !context.Channel.User.ReadOnly ? 'w' : '-';
            perm[0] = fileInfo is DirectoryInfo ? 'x' : '-';
            return(perm);
        }
Ejemplo n.º 20
0
        protected override object OnExecute(FtpCommandContext context)
        {
            const string MESSAGE = "150 Opening data connection for file transfer.";

            context.Channel.CheckLogin();
            context.Channel.CheckDataChannel();

            var dataChannel = context.Channel.DataChannel;

            try
            {
                context.Channel.Status = FtpSessionStatus.Upload;

                var path = context.Statement.Argument;
                if (string.IsNullOrWhiteSpace(path))
                {
                    throw new SyntaxException();
                }

                var localPath = context.Channel.MapVirtualPathToLocalPath(path);
                context.Statement.Result = localPath;

                context.Channel.UpFileStream    = File.Open(localPath, FileMode.Append, FileAccess.Write, FileShare.Read);
                context.Channel.UpFileLocalPath = localPath;
                context.Channel.UpFileFailed    = false;
                dataChannel.Closed   += DataChannel_Closed;
                dataChannel.Error    += DataChannel_Failed;
                dataChannel.Received += DataChannel_Received;

                dataChannel.Receive();

                context.Channel.Send(MESSAGE);

                return(MESSAGE);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);

                if (context.Channel.UpFileStream != null)
                {
                    context.Channel.UpFileStream.Close();
                    context.Channel.UpFileStream = null;
                }
                context.Channel.CloseDataChannel();

                if (ex is FtpException)
                {
                    throw ex;
                }

                throw new InternalException("store file");
            }
        }
Ejemplo n.º 21
0
        protected override void OnExecute(FtpCommandContext context)
        {
            context.Channel.CheckLogin();

            if (string.IsNullOrEmpty(context.Statement.Argument))
            {
                throw new SyntaxException();
            }

            context.Channel.RenamePath = context.Channel.MapVirtualPathToLocalPath(context.Statement.Argument);
            context.Statement.Result   = context.Channel.RenamePath;
            context.Channel.Send("350 File or directory exists, ready for destination name.");
        }
Ejemplo n.º 22
0
        protected override void OnExecute(FtpCommandContext context)
        {
            context.Channel.CheckLogin();

            long offset;

            if (!long.TryParse(context.Statement.Argument, out offset))
            {
                throw new SyntaxException();
            }

            context.Channel.FileOffset = offset;

            context.Channel.Send(string.Format("350 Restarting at {0}. Send STORE or RETR to initiate transfer.", offset));
        }
Ejemplo n.º 23
0
        protected override void OnExecute(FtpCommandContext context)
        {
            context.Channel.Send(
                @"211-Features:
 MDTM
 SIZE
 PASV
 UTF8
 HELP
 MFMT
 MLST size*;type*;perm*;create*;modify*;
 MLSD
 REST
 OPTS
 NOOP
211 End");
        }
Ejemplo n.º 24
0
        protected override object OnExecute(FtpCommandContext context)
        {
            const string MESSAGE = "250 Rename successful.";

            context.Channel.CheckLogin();

            try
            {
                if (string.IsNullOrEmpty(context.Statement.Argument))
                {
                    throw new SyntaxException();
                }

                if (string.IsNullOrEmpty(context.Channel.RenamePath))
                {
                    throw new BadSeqCommandsException();
                }

                string destPath = context.Channel.MapVirtualPathToLocalPath(context.Statement.Argument);
                context.Statement.Result = destPath;

                try
                {
                    if (Directory.Exists(context.Channel.RenamePath))
                    {
                        Directory.Move(context.Channel.RenamePath, destPath);
                    }
                    else
                    {
                        File.Move(context.Channel.RenamePath, destPath);
                    }
                }
                catch (Exception)
                {
                    throw new InternalException("rename path");
                }

                context.Channel.Send(MESSAGE);
                return(MESSAGE);
            }
            finally
            {
                context.Channel.RenamePath = null;
            }
        }
Ejemplo n.º 25
0
        protected override object OnExecute(FtpCommandContext context)
        {
            context.Channel.CheckLogin();

            var localPath = context.Channel.MapVirtualPathToLocalPath("..");

            context.Statement.Result = localPath;
            var virtualPath = context.Channel.MapLocalPathToVirtualPath(localPath);

            if (Directory.Exists(localPath))
            {
                context.Channel.CurrentDir = virtualPath;
                context.Channel.Send($"250 the '{context.Channel.CurrentDir}' is current directory.");
                return($"250 the '{context.Channel.CurrentDir}' is current directory.");
            }

            throw new DirectoryNotFoundException(virtualPath);
        }
Ejemplo n.º 26
0
        protected override void OnExecute(FtpCommandContext context)
        {
            var user = context.Server.Configuration.Users[context.Channel.UserName];

            if (user != null)
            {
                if (string.Equals(context.Statement.Argument, user.Password, StringComparison.OrdinalIgnoreCase))
                {
                    context.Channel.User = user;
                    context.Channel.Send("230 User successfully logged in.", null);
                    context.Statement.Result = true;
                    return;
                }
            }

            context.Channel.Send("530 Not logged in, user or password incorrect!", null);
            context.Statement.Result = false;
        }
Ejemplo n.º 27
0
        protected override object OnExecute(FtpCommandContext context)
        {
            context.Channel.CheckLogin();

            try
            {
                var path = string.Empty;
                if (!string.IsNullOrWhiteSpace(context.Statement.Argument))
                {
                    path = context.Statement.Argument;
                }

                if (string.IsNullOrWhiteSpace(path))
                {
                    path = context.Channel.CurrentDir;
                }

                var localPath = context.Channel.MapVirtualPathToLocalPath(path);
                context.Statement.Result = localPath;

                FileSystemInfo fileInfo;

                if (File.Exists(localPath))
                {
                    fileInfo = new FileInfo(localPath);
                }
                else if (Directory.Exists(localPath))
                {
                    fileInfo = new DirectoryInfo(localPath);
                }
                else
                {
                    throw new FileNotFoundException(path);
                }

                var message = string.Format("250-Listing {0}\r\n{1}250 END", path, FtpMlstFileFormater.Format(context, fileInfo));
                context.Channel.Send(message);
                return(message);
            }
            catch (IOException e)
            {
                throw new InternalException(e.Message);
            }
        }
Ejemplo n.º 28
0
 public static void Format(FtpCommandContext context, FileSystemInfo fileInfo, StringBuilder output)
 {
     output.Append(GetPermission(context, fileInfo));
     output.Append(DELIM);
     output.Append(DELIM);
     output.Append(DELIM);
     output.Append(1);     //LinkCount
     output.Append(DELIM);
     output.Append("ftp"); //OwnerName
     output.Append(DELIM);
     output.Append("ftp"); //GroupName
     output.Append(DELIM);
     output.Append(GetLength(fileInfo));
     output.Append(DELIM);
     output.Append(GetLastModified(fileInfo));
     output.Append(DELIM);
     output.Append(fileInfo.Name);
     output.Append(NEWLINE);
 }
Ejemplo n.º 29
0
        protected override void OnExecute(FtpCommandContext context)
        {
            context.Channel.CheckLogin();

            var localPath = context.Channel.MapVirtualPathToLocalPath("..");

            context.Statement.Result = localPath;
            var virtualPath = context.Channel.MapLocalPathToVirtualPath(localPath);

            if (Directory.Exists(localPath))
            {
                context.Channel.CurrentDir = virtualPath;
                context.Channel.Send("250 \"" + context.Channel.CurrentDir + "\" is current directory.");
            }
            else
            {
                throw new DirectoryNotFoundException(virtualPath);
            }
        }
Ejemplo n.º 30
0
        protected override object OnExecute(FtpCommandContext context)
        {
            const string MESSAGE = "213 Date/time changed OK.";

            context.Channel.CheckLogin();

            try
            {
                if (string.IsNullOrWhiteSpace(context.Statement.Argument))
                {
                    throw new SyntaxException();
                }

                var arguments = context.Statement.Argument.Split(new[] { ' ' }, 2);
                if (arguments.Length != 2)
                {
                    throw new SyntaxException();
                }

                var dateTime = FtpDateUtils.ParseFtpDate(arguments[0]);
                var filePath = context.Channel.MapVirtualPathToLocalPath(arguments[1]);

                var fileInfo = new FileInfo(filePath);
                if (!fileInfo.Exists)
                {
                    throw new FileNotFoundException(arguments[1]);
                }

                fileInfo.LastWriteTimeUtc = dateTime;

                context.Channel.Send(MESSAGE);

                return(MESSAGE);
            }
            catch (FtpException)
            {
                throw;
            }
            catch (Exception)
            {
                throw new InternalException("");
            }
        }