FTP.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package de.tudarmstadt.informatik.hostage.protocol;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import de.tudarmstadt.informatik.hostage.wrapper.Packet;
  5. /**
  6. * FTP protocol
  7. * @author Wulf Pfeiffer
  8. */
  9. public class FTP implements Protocol {
  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. public int getPort() {
  21. return 21;
  22. }
  23. public TALK_FIRST whoTalksFirst() {
  24. return TALK_FIRST.SERVER;
  25. }
  26. public List<Packet> processMessage(Packet packet) {
  27. String request = null;
  28. if (packet != null) {
  29. request = packet.toString();
  30. }
  31. List<Packet> response = new ArrayList<Packet>();
  32. switch (state) {
  33. case NONE:
  34. if (request == null) {
  35. state = STATE.OPEN;
  36. response.add(new Packet(c220));
  37. } else {
  38. state = STATE.CLOSED;
  39. response.add(new Packet(c421));
  40. }
  41. break;
  42. case OPEN:
  43. if (request.contains("QUIT")) {
  44. state = STATE.CLOSED;
  45. return null;
  46. } else if (request.equals("USER \r\n")) {
  47. response.add(new Packet(c501));
  48. } else if (request.contains("USER")) {
  49. state = STATE.USER;
  50. response.add(new Packet(c331));
  51. } else {
  52. response.add(new Packet(c332));
  53. }
  54. break;
  55. case USER:
  56. if (request.equals("PASS \r\n")) {
  57. state = STATE.OPEN;
  58. response.add(new Packet(c501));
  59. } else if (request.contains("PASS")) {
  60. state = STATE.LOGGED_IN;
  61. response.add(new Packet(c230));
  62. } else {
  63. state = STATE.CLOSED;
  64. response.add(new Packet(c221));
  65. }
  66. break;
  67. case LOGGED_IN:
  68. if (request != null && !request.contains("QUIT")) {
  69. response.add(new Packet(c500));
  70. } else {
  71. state = STATE.CLOSED;
  72. response.add(new Packet(c221));
  73. }
  74. break;
  75. default:
  76. state = STATE.CLOSED;
  77. response.add(new Packet(c421));
  78. }
  79. return response;
  80. }
  81. //commands
  82. private String c220 = "220 Service ready for new user.";
  83. private String c221 = "221 Service closing control connection.";
  84. private String c230 = "230 User logged in.";
  85. private String c331 = "331 User name ok, need password.";
  86. private String c332 = "332 Need account for login.";
  87. private String c421 = "421 Service not available, closing control connection.";
  88. private String c500 = "500 Syntax error, command unrecognized.";
  89. private String c501 = "501 Syntax error in parameters or arguments";
  90. public boolean isClosed() {
  91. return state == STATE.CLOSED;
  92. }
  93. public boolean isSecure() {
  94. return false;
  95. }
  96. public Class<String> getType() {
  97. return String.class;
  98. }
  99. public String toString() {
  100. return "FTP";
  101. }
  102. }