Ejemplo n.º 1
0
 /// <summary>
 /// Creates a new instance of FTPUser
 /// </summary>
 /// <param name="name">The name of the user</param>
 /// <param name="pass">The desired password for the user</param>
 public FTPUser(string name, string pass)
 {
     Permissions  = new FTPUserPermission();
     Name         = name;
     Startupdir   = "/";
     SHA1Password = HelperFunctions.SHA1Hash(pass);
 }
Ejemplo n.º 2
0
        void CommandReceived(IAsyncResult arg)
        {
            _activecommand = false;
            int CommandSize = 0;

            try { CommandSize = _clientSocket.EndReceive(arg); }
            catch { }
            if (CommandSize == 0)
            {
                Disconnect();
                //return;
            }

            // Wait for the next command to be sent by the client
            try
            {
                _clientSocket.BeginReceive(_portBuffer, 0, _portBuffer.Length, SocketFlags.None, new AsyncCallback(CommandReceived), null);
            }
            catch
            {
                Disconnect();
                return;
            }

            _lastcmdtime = DateTime.Now;
            string CommandText;

            if (_utf8)
            {
                CommandText = Encoding.UTF8.GetString(_portBuffer, 0, CommandSize).TrimStart(' ');
            }
            else
            {
                CommandText = Encoding.ASCII.GetString(_portBuffer, 0, CommandSize).TrimStart(' ');
            }
            string CmdArguments = null, Command = null;
            int    End = 0;

            if ((End = CommandText.IndexOf(' ')) == -1)
            {
                End = (CommandText = CommandText.Trim()).Length;
            }
            else
            {
                CmdArguments = CommandText.Substring(End).TrimStart(' ');
            }
            Command = CommandText.Substring(0, End).ToUpper();

            if (CmdArguments != null && CmdArguments.EndsWith("\r\n"))
            {
                CmdArguments = CmdArguments.Substring(0, CmdArguments.Length - 2);
            }
            bool CommandExecued = false;

            switch (Command)
            {
            case "USER":
                _activecommand = true;
                if (CmdArguments != null && CmdArguments.Length > 0)
                {
                    SendMessage("331 Password required!");
                    _connuser = CmdArguments;
                }
                CommandExecued = true;
                break;

            case "PASS":
                _activecommand = true;
                if (_connuser == "")
                {
                    SendMessage("503 Invalid User Name");
                    return;
                }
                if (_server.Users[_connuser] != null)
                {
                    if (_server.Users[_connuser].SHA1Password == HelperFunctions.SHA1Hash(CmdArguments))
                    {
                        _isAuthenticated = true;
                        _currentDir      = "/";
                        _currentperms    = _server.Users.GetPermissions(_connuser);
                        if (string.IsNullOrEmpty(_server.Users[_connuser].Startupdir) || _server.Users[_connuser].Startupdir == "/")
                        {
                            _localpath = _server.StartupDir;
                        }
                        else
                        {
                            _localpath = _server.Users[_connuser].Startupdir;
                        }
                        _server.Call_Log(new FTPLogEventArgs(FTPLogEventType.UserConnect, _connuser, true, 230, "none"));
                        SendMessage("230 Authentication Successful");
                    }
                    else
                    {
                        SendMessage("530 Authentication Failed!");
                        _server.Call_Log(new FTPLogEventArgs(FTPLogEventType.UserConnect, _connuser, false, 530, "none"));
                    }
                }
                else
                {
                    SendMessage("530 Authentication Failed!");
                    _server.Call_Log(new FTPLogEventArgs(FTPLogEventType.UserConnect, _connuser, false, 530, "none"));
                }
                CommandExecued = true;
                break;
            }
            if (!CommandExecued)
            {
                if (!_isAuthenticated)
                {
                    SendMessage("530 Access Denied! Authenticate first");
                    return;
                }
                switch (Command.ToUpper())
                {
                case "CWD":
                    _activecommand = true;
                    CWD(CmdArguments);
                    break;

                case "CDUP":
                    _activecommand = true;
                    string[] pathParts = _currentDir.Split('/');
                    if (pathParts.Length > 1)
                    {
                        _currentDir = "";
                        for (int i = 0; i < (pathParts.Length - 2); i++)
                        {
                            _currentDir += pathParts[i] + "/";
                        }
                        if (_currentDir.Length == 0)
                        {
                            _currentDir = "/";
                        }
                    }
                    SendMessage("250 CDUP command successful.");
                    break;

                case "QUIT":
                    _activecommand = true;
                    SendMessage("221 FTP server signing off");
                    Disconnect();
                    break;

                case "PWD":
                    _activecommand = true;
                    SendMessage("257 \"" + _currentDir + "\"");
                    break;

                case "PORT":
                    _activecommand = true;
                    PORT(CmdArguments);     //done
                    break;

                case "PASV":
                    _activecommand = true;
                    PASV(CmdArguments);     //done
                    break;

                case "TYPE":
                    _activecommand = true;
                    TYPE(CmdArguments);     //done
                    break;

                case "SYST":
                    _activecommand = true;
                    SendMessage("215 Windows_NT");
                    break;

                case "NOOP":
                    _activecommand = true;
                    SendMessage("200 OK");
                    break;

                case "RETR":
                    _activecommand = true;
                    RETR(CmdArguments);
                    break;

                case "STOR":
                    _activecommand = true;
                    STOR(CmdArguments, false);
                    break;

                case "APPE":
                    _activecommand = true;
                    APPE(CmdArguments);
                    break;

                case "RNFR":
                    _activecommand = true;
                    RNFR(CmdArguments);
                    break;

                case "RNTO":
                    _activecommand = true;
                    RNTO(CmdArguments);
                    break;

                case "DELE":
                    _activecommand = true;
                    DELE(CmdArguments);
                    break;

                case "RMD":
                    _activecommand = true;
                    RMD(CmdArguments);
                    break;

                case "MKD":
                    _activecommand = true;
                    MKD(CmdArguments);
                    break;

                case "LIST":
                    _activecommand = true;
                    LIST(_currentDir);
                    break;

                case "NLST":
                    _activecommand = true;
                    NLST(CmdArguments);
                    break;

                /*case "CLNT":
                 *  break;*/
                case "MDTM":
                    _activecommand = true;
                    MDTM(CmdArguments);
                    break;

                case "SIZE":
                    _activecommand = true;
                    SIZE(CmdArguments);
                    break;

                case "OPTS":
                    _activecommand = true;
                    OPTS(CmdArguments);
                    break;

                case "REIN":
                    _activecommand = true;
                    REIN(CmdArguments);
                    break;

                case "STOU":
                    _activecommand = true;
                    STOR(CmdArguments, true);
                    break;

                case "ABOR":
                case "SHUTDOWN":
                    if (_datasocket != null && _datasocket.Connected)
                    {
                        _datasocket.Close();
                    }
                    _datasocket = null;
                    GC.Collect();
                    SendMessage("200 Data transfer aborted");
                    _server.Call_Log(new FTPLogEventArgs(FTPLogEventType.Abort, _connuser, true, 200));
                    break;

                case "FEAT":
                    SendMessage("  SIZE");
                    SendMessage("  MTDM");
                    SendMessage("211 Feature list end");
                    _server.Call_Log(new FTPLogEventArgs(FTPLogEventType.FeatureList, _connuser, true, 211));
                    break;

                default:
                    SendMessage("500 Unknown Command.");
                    _server.Call_Log(new FTPLogEventArgs(FTPLogEventType.UnknownCommand, _connuser, true, 500, Command, CmdArguments));
                    break;

                    //	case "STAT":
                    //		break;

                    //	case "HELP":
                    //		break;

                    //	case "REST":
                    //		break;
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Creates a new instance of FTPUser
 /// </summary>
 public FTPUser()
 {
     Permissions = new FTPUserPermission();
     Startupdir  = "/";
 }