public static void Cmd_ABOR(Object sender, CommandEventArgs e) { FtpSession lSession = (FtpSession)e.Session; if (lSession.State == FtpState.Transfer && lSession.TransferThread != null) { lSession.TransferThread.Abort(); lSession.TransferThread = null; e.Connection.WriteLine("250 ABOR successful"); } else { e.Connection.WriteLine("503 Bad sequence of commands."); } }
public static void Cmd_NOOP(Object sender, CommandEventArgs e) { e.Connection.WriteLine("200 OK"); }
public static void Cmd_RNFR(Object sender, CommandEventArgs e) { FtpSession lSession = (FtpSession)e.Session; if (lSession.State == FtpState.LoggedIn) { if ((e.AllParameters.Length == 0) || (!ValidFilename(e.AllParameters))) { e.Connection.WriteLine("501 Syntax error in parameters or arguments."); return; } String lNewDir = e.AllParameters; if (lNewDir.StartsWith("/")) { lNewDir = ValidateDirectory(lNewDir); } else { String lTemp = lSession.Directory; if (!lTemp.EndsWith("/")) lTemp = lTemp + "/"; lNewDir = ValidateDirectory(lTemp + lNewDir); } lSession.RenameFrom = lNewDir; e.Connection.WriteLine("350 Ready for destination name."); } else { e.Connection.WriteLine("503 Bad sequence of commands."); } }
public static void Cmd_USER(Object sender, CommandEventArgs e) { FtpSession lSession = (FtpSession)e.Session; if (lSession.State == FtpState.Start || lSession.State == FtpState.PasswordRequired) { if (e.Parameters.Length != 1) { e.Connection.WriteLine("501 Syntax error in parameters or arguments."); } else { lSession.Username = e.Parameters[0]; e.Connection.WriteLine("331 User name okay, need password."); lSession.State = FtpState.PasswordRequired; } } else { e.Connection.WriteLine("503 Bad sequence of commands."); } }
public static void Cmd_PASS(Object sender, CommandEventArgs e) { FtpSession lSession = (FtpSession)e.Session; if (lSession.State == FtpState.PasswordRequired) { if (e.Parameters.Length != 1) { e.Connection.WriteLine("501 Syntax error in parameters or arguments."); } else { FtpUserLoginEventArgs lEventArgs = new FtpUserLoginEventArgs(e.Session, e.Connection, e.Server); lEventArgs.UserName = lSession.Username; lEventArgs.Password = e.Parameters[0]; try { ((FtpServer)e.Server).InvokeOnUserLogin(lEventArgs); } catch (FtpException ex) { e.Connection.WriteLine(ex.ToString()); return; } catch { e.Connection.WriteLine("500 Internal Error"); return; } if (lEventArgs.LoginOk) { if (lEventArgs.NeedAccount) { lSession.State = FtpState.AccountRequired; e.Connection.WriteLine("332 Need account for login."); } else { lSession.State = FtpState.LoggedIn; e.Connection.WriteLine("230 User logged in, proceed."); } } else { lSession.State = FtpState.Start; e.Connection.WriteLine("530 Unable to login"); } } } else { e.Connection.WriteLine("503 Bad sequence of commands."); } }
public static void Cmd_PASV(Object sender, CommandEventArgs e) { FtpSession lSession = (FtpSession)e.Session; if (lSession.State == FtpState.LoggedIn) { if (lSession.ActiveConnection != null) { if (lSession.ActiveConnection.Connected) lSession.ActiveConnection.Close(); lSession.ActiveConnection = null; } if (lSession.PassiveServer == null) { lSession.PassiveServer = new SimpleServer(); lSession.PassiveServer.Binding.Address = ((IPEndPoint)e.Connection.LocalEndPoint).Address; lSession.PassiveServer.Open(); } lSession.Passive = true; Byte[] lAddress; #if FULLFRAMEWORK lAddress = ((IPEndPoint)lSession.PassiveServer.Binding.ListeningSocket.LocalEndPoint).Address.GetAddressBytes(); #endif #if COMPACTFRAMEWORK IPAddress lIPAddress = ((IPEndPoint)lSession.PassiveServer.Binding.ListeningSocket.LocalEndPoint).Address; String[] lIPAddressstr = lIPAddress.ToString().Split(new Char[] {'.'}); lAddress = new Byte[lIPAddressstr.Length]; for (Int32 i = 0; i < lIPAddressstr.Length; i++) lAddress[i] = Byte.Parse(lIPAddressstr[i]); #endif Int32 lPort = ((IPEndPoint)lSession.PassiveServer.Binding.ListeningSocket.LocalEndPoint).Port; e.Connection.WriteLine("227 Entering Passive Mode ({0},{1},{2},{3},{4},{5}).", lAddress[0], lAddress[1], lAddress[2], lAddress[3], unchecked((Byte)(lPort >> 8)), unchecked((Byte)lPort)); } else { e.Connection.WriteLine("503 Bad sequence of commands."); } }
public static void Cmd_SYST(Object sender, CommandEventArgs e) { FtpSession lSession = (FtpSession)e.Session; if (lSession.State == FtpState.LoggedIn) e.Connection.WriteLine("215 " + ((FtpServer)e.Server).SystemType); else e.Connection.WriteLine("503 Bad sequence of commands."); }
public static void Cmd_PWD(Object sender, CommandEventArgs e) { FtpSession lSession = (FtpSession)e.Session; if (lSession.State == FtpState.LoggedIn) { if (e.Parameters.Length != 0) e.Connection.WriteLine("501 Syntax error in parameters or arguments."); else e.Connection.WriteLine(String.Format("257 \"{0}\" is current directory.", lSession.Directory)); } else { e.Connection.WriteLine("503 Bad sequence of commands."); } }
public static void Cmd_CWD(Object sender, CommandEventArgs e) { FtpSession lSession = (FtpSession)e.Session; if (lSession.State == FtpState.LoggedIn) { if (e.Parameters.Length == 0) { e.Connection.WriteLine("501 Syntax error in parameters or arguments."); } else { String lNewDir = e.AllParameters; if (lNewDir.StartsWith("/")) { lNewDir = ValidateDirectory(lNewDir); } else { String lTemp = lSession.Directory; if (!lTemp.EndsWith("/")) lTemp = lTemp + "/"; lNewDir = ValidateDirectory(lTemp + lNewDir); } FtpChangeDirectoryArgs lArgs = new FtpChangeDirectoryArgs(e.Session, e.Connection, e.Server); lArgs.NewDirectory = lNewDir; try { ((FtpServer)e.Server).InvokeOnChangeDirectory(lArgs); } catch (FtpException ex) { e.Connection.WriteLine(ex.ToString()); return; } catch { e.Connection.WriteLine("500 Internal Error"); return; } if (lArgs.ChangeDirOk) { e.Connection.WriteLine("250 CWD command successful, new folder is '{0}'.", lSession.Directory); lSession.Directory = lNewDir; } else { e.Connection.WriteLine("550 Permission Denied."); } } } else { e.Connection.WriteLine("503 Bad sequence of commands."); } }
public static void Cmd_PORT(Object sender, CommandEventArgs e) { FtpSession lSession = (FtpSession)e.Session; if (lSession.State == FtpState.LoggedIn) { if (e.Parameters.Length != 1) { e.Connection.WriteLine("501 Syntax error in parameters or arguments."); } else { String[] lNewPort = e.Parameters[0].Split(new Char[] { ',' }); if (lNewPort.Length != 6) { e.Connection.WriteLine("501 Syntax error in parameters or arguments."); } else { IPAddress lNewIp; Int32 lNewPortw; try { lNewIp = IPAddress.Parse(String.Format("{0}.{1}.{2}.{3}", lNewPort[0], lNewPort[1], lNewPort[2], lNewPort[3])); lNewPortw = Byte.Parse(lNewPort[4]) << 8 | Byte.Parse(lNewPort[5]); lSession.Passive = false; try { lSession.ActiveConnection = Client.Connect(lNewIp, lNewPortw, new Binding()); e.Connection.WriteLine("200 PORT command succesful"); } catch { e.Connection.WriteLine("500 Illegal PORT command "); } } catch { e.Connection.WriteLine("501 Syntax error in parameters or arguments."); } } } } else { e.Connection.WriteLine("503 Bad sequence of commands."); } }
public static void Cmd_QUIT(Object sender, CommandEventArgs e) { e.Connection.WriteLine(((FtpServer)e.Server).QuitReply); e.Connection.Close(); }
public static void Cmd_REST(Object sender, CommandEventArgs e) { FtpSession lSession = (FtpSession)e.Session; if (lSession.State == FtpState.LoggedIn) { if (e.Parameters.Length != 1) { e.Connection.WriteLine("501 Syntax error in parameters or arguments."); } else { try { Int64 lRestPoint = Int64.Parse(e.Parameters[0]); lSession.RestartPoint = lRestPoint; e.Connection.WriteLine(String.Format("350 Restarting at {0}. Send STORE or RETRIEVE to initiate transfer.", lRestPoint)); } catch { e.Connection.WriteLine("501 Syntax error in parameters or arguments."); } } } else { e.Connection.WriteLine("503 Bad sequence of commands."); } }
public static void Cmd_TYPE(Object sender, CommandEventArgs e) { FtpSession lSession = (FtpSession)e.Session; if (lSession.State == FtpState.LoggedIn) { if (e.Parameters.Length != 1) { e.Connection.WriteLine("501 Syntax error in parameters or arguments."); } else { switch (e.Parameters[0]) { case "I": e.Connection.WriteLine("200 Type set to I"); lSession.Image = true; break; case "A": e.Connection.WriteLine("200 Type set to A"); lSession.Image = false; break; default: e.Connection.WriteLine("501 Syntax error in parameters or arguments."); break; } } } else { e.Connection.WriteLine("503 Bad sequence of commands."); } }
public static void Cmd_LIST(Object sender, CommandEventArgs e) { FtpSession lSession = (FtpSession)e.Session; if (lSession.State == FtpState.LoggedIn) { if (lSession.Passive) e.Connection.WriteLine("125 Data connection already open; transfer starting"); if (!ValidateConnection(lSession)) { e.Connection.WriteLine("425 Unable to build data connection"); } else { FtpGetListingArgs lListingArgs = new FtpGetListingArgs(e.Session, e.Connection, e.Server); if (!lSession.Passive) e.Connection.WriteLine("150 Opening data connection"); try { ((FtpServer)e.Server).InvokeOnGetListing(lListingArgs); } catch (FtpException ex) { e.Connection.WriteLine(ex.ToString()); return; } for (Int32 i = 0; i < lListingArgs.Listing.Count; i++) { FtpListingItem lItem = lListingArgs.Listing[i]; lSession.ActiveConnection.WriteLine(lItem.ToString()); } lSession.ActiveConnection.Close(); lSession.ActiveConnection = null; try { e.Connection.WriteLine("226 Transfer complete."); } catch { e.Connection.WriteLine("425 Error while transfering"); } } lSession.RestartPoint = 0; } else { e.Connection.WriteLine("503 Bad sequence of commands."); } }
public static void Cmd_MKD(Object sender, CommandEventArgs e) { FtpSession lSession = (FtpSession)e.Session; if (lSession.State == FtpState.LoggedIn) { if ((e.AllParameters.Length == 0) || (!ValidFilename(e.AllParameters))) { e.Connection.WriteLine("501 Syntax error in parameters or arguments."); return; } String lNewDir = e.AllParameters; if (lNewDir.StartsWith("/")) { lNewDir = ValidateDirectory(lNewDir); } else { String lTemp = lSession.Directory; if (!lTemp.EndsWith("/")) lTemp = lTemp + "/"; lNewDir = ValidateDirectory(lTemp + lNewDir); } FtpFileEventArgs lEventArgs = new FtpFileEventArgs(e.Session, e.Connection, e.Server); lEventArgs.FileName = lNewDir; try { ((FtpServer)e.Server).InvokeOnMakeDirectory(lEventArgs); } catch (FtpException ex) { e.Connection.WriteLine(ex.ToString()); return; } catch { e.Connection.WriteLine("500 Internal Error"); return; } if (lEventArgs.Ok) e.Connection.WriteLine(String.Format("257 \"{0}\" - Directory successfully created", lEventArgs.FileName)); else e.Connection.WriteLine("550 Permission Denied"); } else { e.Connection.WriteLine("503 Bad sequence of commands."); } }
public static void Cmd_CDUP(Object sender, CommandEventArgs e) { FtpSession lSession = (FtpSession)e.Session; if (lSession.State == FtpState.LoggedIn) { if (e.Parameters.Length != 0) { e.Connection.WriteLine("501 Syntax error in parameters or arguments."); } else { String lNewDir = lSession.Directory; if (lNewDir.Length > 2) { Int32 lNewLen = lNewDir.LastIndexOf('/', lNewDir.Length - 2); if (lNewLen <= 0) lNewLen = 1; lNewDir = lNewDir.Substring(0, lNewLen); if (lNewDir.Length == 0) lNewDir = "/"; } FtpChangeDirectoryArgs lEventArgs = new FtpChangeDirectoryArgs(e.Session, e.Connection, e.Server); lEventArgs.NewDirectory = lNewDir; try { ((FtpServer)e.Server).InvokeOnChangeDirectory(lEventArgs); } catch (FtpException ex) { e.Connection.WriteLine(ex.ToString()); return; } catch { e.Connection.WriteLine("500 Internal Error"); return; } if (lEventArgs.ChangeDirOk) { e.Connection.WriteLine("250 CDUP command successful."); lSession.Directory = lNewDir; } else { e.Connection.WriteLine("550 Permission Denied."); } } } else { e.Connection.WriteLine("503 Bad sequence of commands."); } }
public static void Cmd_APPE(Object sender, CommandEventArgs e) { FtpSession lSession = (FtpSession)e.Session; if (lSession.State == FtpState.LoggedIn) { if ((e.AllParameters.Length == 0) || (!ValidFilename(e.AllParameters))) { e.Connection.WriteLine("501 Syntax error in parameters or arguments."); return; } String lNewDir = e.AllParameters; if (lNewDir.StartsWith("/")) { lNewDir = ValidateDirectory(lNewDir); } else { String lTemp = lSession.Directory; if (!lTemp.EndsWith("/")) lTemp = lTemp + "/"; lNewDir = ValidateDirectory(lTemp + lNewDir); } FtpTransferEventArgs lArgs = new FtpTransferEventArgs(e.Session, e.Connection, e.Server); lArgs.FileName = lNewDir; lArgs.Append = true; lArgs.RestartPoint = 0; lSession.RestartPoint = 0; try { ((FtpServer)lArgs.Server).InvokeOnCanStoreFile(lArgs); } catch (FtpException ex) { lArgs.Connection.WriteLine(ex.ToString()); return; } catch { lArgs.Connection.WriteLine("500 Internal Error"); return; } if (lArgs.Ok) { if (lSession.Passive) e.Connection.WriteLine("125 Data connection already open; transfer starting"); if (!ValidateConnection(lSession)) { e.Connection.WriteLine("425 Unable to build data connection"); return; } if (!lSession.Passive) e.Connection.WriteLine("150 Opening data connection"); } else { lArgs.Connection.WriteLine("550 Permission Denied"); return; } lSession.State = FtpState.Transfer; if (lSession.TransferThread != null) { lSession.TransferThread.Abort(); lSession.TransferThread = null; } lSession.TransferThread = new FtpTransferThread(lArgs, true); } else { e.Connection.WriteLine("503 Bad sequence of commands."); } }
protected override void DoWork() { CommandBasedServer lServer = (CommandBasedServer)Owner; CommandBasedSession lSession = lServer.CreateSession(); lSession.RemoteEndPoint = ((IPEndPoint)DataConnection.RemoteEndPoint); lSession.LocalEndPoint = ((IPEndPoint)DataConnection.RemoteEndPoint); try { lServer.InvokeOnClientConnected(new SessionEventArgs(lSession, DataConnection, lServer)); DataConnection.WriteLine(lServer.Greeting); CommandEventArgs lArgs = new CommandEventArgs(lSession, DataConnection, lServer); String lCmdLine; while (DataConnection.Connected) { lCmdLine = DataConnection.ReadLine().Trim(); if (lCmdLine.Length == 0) continue; Int32 tempidx = lCmdLine.IndexOf(' '); if (tempidx == -1) { lArgs.Command = lCmdLine.ToUpper(); lArgs.AllParameters = ""; lArgs.Parameters = new String[0]; } else { lArgs.Command = lCmdLine.Substring(0, tempidx).ToUpper(); lArgs.AllParameters = lCmdLine.Substring(tempidx + 1).Trim(); lArgs.Parameters = lArgs.AllParameters.Split(SPACE); } OnCommandHandler lCommandHandler; lServer.Commands.TryGetValue(lArgs.Command, out lCommandHandler); if (lCommandHandler == null) { DataConnection.WriteLine(String.Format(lServer.UnknownCommand, lArgs.Command)); } else { try { lCommandHandler(lServer, lArgs); } catch (Exception ex) { lServer.HandleCommandException(DataConnection, ex); } } } } catch (Exception) { } lServer.InvokeOnClientDisconnected(new SessionEventArgs(lSession, DataConnection, lServer)); }