FTP.java 2.6 KB

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