FTP.java 2.6 KB

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