Esempio n. 1
0
        bool IFtpModule.HandleRequest(FtpRequest request, ClientData client, FtpHandler handler, Server server)
        {
            if (!client.Resources.TryGetResource <FtpData>(out var ftpData))
            {
                client.Resources.SetResource(new FtpData());
            }

            if (request.Command == FtpRequestCommands.PASV)
            {
                handler.QueueFtpResponse(server, client, this.EnablePassiveMode(request, ftpData, client));
                return(true);
            }
            else if (request.Command == FtpRequestCommands.PORT)
            {
                handler.QueueFtpResponse(server, client, this.EnableActiveMode(request, ftpData));
                return(true);
            }
            else if (request.Command == FtpRequestCommands.TYPE)
            {
                handler.QueueFtpResponse(server, client, this.HandleTypeArguments(request, ftpData));
                return(true);
            }

            return(false);
        }
Esempio n. 2
0
        bool IFtpModule.HandleRequest(FtpRequest request, ClientData client, FtpHandler handler, Server server)
        {
            if (!client.Resources.TryGetResource <AuthenticationData>(out var authData))
            {
                authData = new AuthenticationData();
                client.Resources.SetResource(authData);
            }

            if (request.Command == FtpRequestCommands.USER)
            {
                authData.Username = request.Arguments[0];
                handler.QueueFtpResponse(server, client, new FtpResponse {
                    StatusCode = FtpResponseCodes.NeedPassword, Message = "Username OK. Need password!"
                });
                return(true);
            }
            else if (request.Command == FtpRequestCommands.PASS)
            {
                if (!string.IsNullOrWhiteSpace(authData.Username))
                {
                    authData.Password = request.Arguments[0];
                    if (this.ValidateAuthentication.Invoke(authData))
                    {
                        handler.QueueFtpResponse(server, client, new FtpResponse {
                            StatusCode = FtpResponseCodes.UserLoggedIn, Message = "User logged in!"
                        });
                        authData.Authenticated = true;
                    }
                    else
                    {
                        handler.QueueFtpResponse(server, client, new FtpResponse {
                            StatusCode = FtpResponseCodes.InvalidUsernameOrPassword, Message = "Invalid username or password!"
                        });
                    }
                }
                else
                {
                    handler.QueueFtpResponse(server, client, new FtpResponse {
                        StatusCode = FtpResponseCodes.NotLoggedIn, Message = "Username not present. Please provide username!"
                    });
                }

                return(true);
            }
            else if (!authData.Authenticated)
            {
                handler.QueueFtpResponse(server, client, new FtpResponse {
                    StatusCode = FtpResponseCodes.NotLoggedIn, Message = "Please log in before issuing other commands!"
                });
                return(true);
            }

            return(false);
        }
Esempio n. 3
0
 bool IFtpModule.HandleRequest(FtpRequest request, ClientData client, FtpHandler handler, Server server)
 {
     handler.QueueFtpResponse(server, client, new FtpResponse {
         StatusCode = FtpResponseCodes.CommandNotImplemented, Message = "Command not implemented"
     });
     return(true);
 }
Esempio n. 4
0
        bool IFtpModule.HandleRequest(FtpRequest request, ClientData client, FtpHandler handler, Server server)
        {
            if (!client.Resources.TryGetResource <FtpData>(out var ftpData))
            {
                client.Resources.SetResource(new FtpData());
            }

            if (request.Command == FtpRequestCommands.STOR)
            {
                if (!ftpData.TransferDetails.ConnectionOpen)
                {
                    handler.QueueFtpResponse(server, client, new FtpResponse {
                        StatusCode = FtpResponseCodes.FileStatusOkay, Message = "Opening data connection"
                    });
                    ftpData.OpenDataConnection();
                }

                handler.QueueFtpResponse(server, client, this.StoreFile(request, ftpData));
                ftpData.CloseDataConnection();
                return(true);
            }
            else if (request.Command == FtpRequestCommands.DELE)
            {
                handler.QueueFtpResponse(server, client, this.RemoveFile(request, ftpData));
                return(true);
            }
            else if (request.Command == FtpRequestCommands.RETR)
            {
                if (!ftpData.TransferDetails.ConnectionOpen)
                {
                    handler.QueueFtpResponse(server, client, new FtpResponse {
                        StatusCode = FtpResponseCodes.FileStatusOkay, Message = "Opening data connection"
                    });
                    ftpData.OpenDataConnection();
                }

                handler.QueueFtpResponse(server, client, this.RetrieveFile(request, ftpData));
                ftpData.CloseDataConnection();
                return(true);
            }

            return(false);
        }
Esempio n. 5
0
        bool IFtpModule.HandleRequest(FtpRequest request, ClientData client, FtpHandler handler, Server server)
        {
            if (request.Command == FtpRequestCommands.QUIT)
            {
                handler.QueueFtpResponse(server, client, new FtpResponse {
                    StatusCode = FtpResponseCodes.ServiceClosingControlConnection, Message = "Service closing control connection"
                });
                return(true);
            }

            return(false);
        }
Esempio n. 6
0
        bool IFtpModule.HandleRequest(FtpRequest request, ClientData client, FtpHandler handler, Server server)
        {
            if (request.Command == FtpRequestCommands.SYST)
            {
                handler.QueueFtpResponse(server, client, new FtpResponse {
                    StatusCode = FtpResponseCodes.NAMESystemType, Message = "MTSC Webserver " + System.Reflection.Assembly.GetEntryAssembly().GetName().Version
                });
                return(true);
            }

            return(false);
        }
Esempio n. 7
0
        bool IFtpModule.HandleRequest(FtpRequest request, ClientData client, FtpHandler handler, Server server)
        {
            if (!Directory.Exists(Path.GetFullPath(this.RootPath)))
            {
                server.Log($"Root path [{Path.GetFullPath(this.RootPath)}] doesn't exist! Creating it now!");
                Directory.CreateDirectory(Path.GetFullPath(this.RootPath));
            }

            if (!client.Resources.TryGetResource <FtpData>(out var ftpData))
            {
                client.Resources.SetResource(new FtpData {
                    CurrentDirectory = Path.GetFullPath(this.RootPath)
                });
            }

            if (string.IsNullOrEmpty(ftpData.CurrentDirectory))
            {
                ftpData.CurrentDirectory = Path.GetFullPath(this.RootPath);
            }

            if (request.Command == FtpRequestCommands.MKD)
            {
                handler.QueueFtpResponse(server, client, this.CreateDirectory(request, ftpData));
                return(true);
            }
            else if (request.Command == FtpRequestCommands.PWD)
            {
                handler.QueueFtpResponse(server, client, this.PrintCurrentDirectory(request, ftpData));
                return(true);
            }
            else if (request.Command == FtpRequestCommands.CWD)
            {
                handler.QueueFtpResponse(server, client, this.ChangeCurrentDirectory(request, ftpData));
                return(true);
            }
            else if (request.Command == FtpRequestCommands.CDUP)
            {
                handler.QueueFtpResponse(server, client, this.ChangeToParentDirectory(request, ftpData));
                return(true);
            }
            else if (request.Command == FtpRequestCommands.RMD)
            {
                handler.QueueFtpResponse(server, client, this.RemoveDirectory(request, ftpData));
                return(true);
            }
            else if (request.Command == FtpRequestCommands.LIST)
            {
                if (!ftpData.TransferDetails.ConnectionOpen)
                {
                    handler.QueueFtpResponse(server, client, new FtpResponse {
                        StatusCode = FtpResponseCodes.FileStatusOkay, Message = "Opening data connection"
                    });
                    ftpData.OpenDataConnection();
                }

                handler.QueueFtpResponse(server, client, this.ListFolder(request, ftpData));
                ftpData.CloseDataConnection();
                return(true);
            }

            return(false);
        }