Beispiel #1
0
 /// <summary>
 /// Process FTP commands when the user is not yet logged in.
 /// Mostly handles the login commands USER and PASS.
 /// </summary>
 private void HandleAuth(string verb, string args)
 {
     if (verb == "USER" && args != null)
     {
         if (authHandler.AllowLogin(args, null, password))
         {
             Respond(230, "Login successful.");
             loggedIn = true;
         }
         else
         {
             loggedInUser = args;
             Respond(331, "Password please.");
         }
     }
     else if (verb == "PASS")
     {
         if (loggedInUser != null)
         {
             if (authHandler.AllowLogin(loggedInUser, args, password))
             {
                 Respond(230, "Login successful.");
                 loggedIn = true;
             }
             else
             {
                 loggedInUser = null;
                 Respond(530, "Login failed, please try again.");
             }
         }
         else
         {
             Respond(530, "No USER specified.");
         }
     }
     else
     {
         Respond(530, "Please login first.");
     }
 }
Beispiel #2
0
        /// <summary>
        /// Main method of the session thread.
        /// Reads commands and executes them.
        /// </summary>
        private void Work()
        {
            if (logHandler != null)
            {
                logHandler.NewControlConnection();
            }

            try {
                if (!authHandler.AllowControlConnection())
                {
                    Respond(421, "Control connection refused.");
                    // first flush, then close
                    controlSocket.Shutdown(SocketShutdown.Both);
                    controlSocket.Close();
                    return;
                }

                Respond(220, String.Format("This is mooftpserv v{0}. {1}", LIB_VERSION, GetRandomText(HELLO_TEXT)));

                // allow anonymous login?
                if (authHandler.AllowLogin(null, null))
                {
                    loggedIn = true;
                }

                while (controlSocket.Connected)
                {
                    string verb;
                    string args;
                    if (!ReadCommand(out verb, out args))
                    {
                        if (controlSocket.Connected)
                        {
                            // assume clean disconnect if there are no buffered bytes
                            if (cmdRcvBytes != 0)
                            {
                                Respond(500, "Failed to read command, closing connection.");
                            }
                            controlSocket.Close();
                        }
                        break;
                    }
                    else if (verb.Trim() == "")
                    {
                        // ignore empty lines
                        continue;
                    }

                    try {
                        if (loggedIn)
                        {
                            ProcessCommand(verb, args);
                        }
                        else if (verb == "QUIT")   // QUIT should always be allowed
                        {
                            Respond(221, "Bye.");
                            // first flush, then close
                            controlSocket.Shutdown(SocketShutdown.Both);
                            controlSocket.Close();
                        }
                        else
                        {
                            HandleAuth(verb, args);
                        }
                    } catch (Exception ex) {
                        Respond(500, ex);
                    }
                }
            } catch (Exception) {
                // catch any uncaught stuff, the server should not throw anything
            } finally {
                if (controlSocket.Connected)
                {
                    controlSocket.Close();
                }

                if (logHandler != null)
                {
                    logHandler.ClosedControlConnection();
                }

                threadAlive = false;
            }
        }