FTP.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package de.tudarmstadt.informatik.hostage.protocol;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.regex.Pattern;
  5. public final class FTP implements Protocol<String> {
  6. private static enum STATE {
  7. NONE, OPEN, CLOSED, USER, LOGGED_IN
  8. };
  9. private STATE state = STATE.NONE;
  10. @Override
  11. public int getPort() {
  12. return 21;
  13. }
  14. @Override
  15. public TALK_FIRST whoTalksFirst() {
  16. return TALK_FIRST.SERVER;
  17. }
  18. @Override
  19. public List<String> processMessage(String message) {
  20. List<String> response = new ArrayList<String>();
  21. switch (state) {
  22. case NONE:
  23. if (message == null) {
  24. state = STATE.OPEN;
  25. response.add("220 Service ready for new user.");
  26. } else {
  27. state = STATE.CLOSED;
  28. response.add("421 Service not available, closing control connection.");
  29. }
  30. break;
  31. case OPEN:
  32. if (Pattern.matches("^QUIT\\s?", message)) {
  33. state = STATE.CLOSED;
  34. return null;
  35. } else if (Pattern.matches("^USER (\\w)+$", message)) {
  36. state = STATE.USER;
  37. response.add("331 User name ok, need password.");
  38. } else if (message != null && Pattern.matches("^USER\\s?", message)) {
  39. response.add("530 Not logged in.");
  40. } else if (message != null && Pattern.matches("^USER.*", message)) {
  41. response.add("501 Syntax error in parameters or arguments");
  42. } else {
  43. response.add("332 Need account for login.");
  44. }
  45. break;
  46. case USER:
  47. if (Pattern.matches("^PASS (\\S)+$", message)) {
  48. state = STATE.LOGGED_IN;
  49. response.add("230 User logged in.");
  50. } else if (Pattern.matches("^PASS.*", message)) {
  51. state = STATE.OPEN;
  52. response.add("501 Syntax error in parameters or arguments");
  53. } else {
  54. state = STATE.CLOSED;
  55. response.add("221 Service closing control connection.");
  56. }
  57. break;
  58. case LOGGED_IN:
  59. if (Pattern.matches("^QUIT\\s?", message)) {
  60. state = STATE.CLOSED;
  61. response.add("221 Service closing control connection.");
  62. } else if (message != null) {
  63. response.add("502 Command not implemented.");
  64. } else {
  65. state = STATE.CLOSED;
  66. response.add("221 Service closing control connection.");
  67. }
  68. break;
  69. default:
  70. state = STATE.CLOSED;
  71. response.add("421 Service not available, closing control connection.");
  72. }
  73. return response;
  74. }
  75. @Override
  76. public boolean isClosed() {
  77. return (state == STATE.CLOSED);
  78. }
  79. @Override
  80. public boolean isSecure() {
  81. return false;
  82. }
  83. @Override
  84. public Class<String> getType() {
  85. return String.class;
  86. }
  87. @Override
  88. public String toString() {
  89. return "FTP";
  90. }
  91. }