Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.84 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.nio.file.Files;
  8. import java.nio.file.Paths;
  9. import java.nio.file.StandardCopyOption;
  10. import java.util.ArrayList;
  11. import java.util.StringTokenizer;
  12.  
  13. public class FTP2server {
  14.  
  15. final static String CRLF = "\r\n";
  16. static int retrCount = 0;
  17. static boolean user = false;
  18. static boolean needPass = false;
  19. static boolean loggedIn = false;
  20. static boolean pass = false;
  21. static String type = "";
  22. static boolean syst = false;
  23. static String hostaddress = "";
  24. static int portnumber = 0;
  25. static boolean needRetr = false;
  26. static boolean validRetr = false;
  27.  
  28. // Program-Class overview: The purpose of FTP1Server is to determine if a received command is a valid FTP command.
  29. // My approach was to first read the input from the console and then parse the string in order to check for validity.
  30.  
  31. public static void main(String[] args) throws IOException { // input is saved and passed to "parse"
  32.  
  33. BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); // declare and instantiate a BufferedReader
  34. ArrayList<String> inputList = new ArrayList<String>(); // declare and instantiate an ArrayList
  35. String input = ""; // declare and instantiate input
  36. int i = 0;
  37. i = b.read();
  38.  
  39. while (i != -1) { // read input from the console and compile it into inputList
  40. input += (char) i; // scan characters into input string
  41. if ((char) i == '\r') { // check for the end of input
  42. i = b.read();
  43. if ((char) i == '\n') {
  44. input += (char) i;
  45. inputList.add(input);
  46. input = "";
  47. i = b.read();
  48. }
  49. else {
  50. inputList.add(input);
  51. input = "";
  52. }
  53. } else if ((char) i == '\n') {
  54. inputList.add(input);
  55. input = "";
  56. i = b.read();
  57. } else {
  58. i = b.read();
  59. }
  60. }
  61.  
  62. System.out.printf("220 COMP 431 FTP server ready.%s", CRLF);
  63.  
  64. for (int j = 0; j < inputList.size(); j++) { // loop through the inputList in order to pass each input to "parse"
  65. input = inputList.get(j);
  66. System.out.print(input);
  67. parse(input);
  68. }
  69. }
  70.  
  71. static void parse(String inputline) throws IOException { // takes input from the console and checks each string for valid FTP characteristics
  72.  
  73. String token = ""; // The current token
  74.  
  75. StringTokenizer tokenizedLine = new StringTokenizer(inputline, " \n\r", true); // declare and instantiate a
  76. // StringTokenizer with delimiters
  77.  
  78. if (tokenizedLine.hasMoreTokens()) { // check if string has more tokens
  79. token = tokenizedLine.nextToken(); // Get the token
  80.  
  81. if (token.equalsIgnoreCase("user")) { // see if the string representing the token matches "user"
  82. if (tokenizedLine.hasMoreTokens()) { // check if string has more tokens
  83. token = tokenizedLine.nextToken();
  84.  
  85. if (!token.equals(" ")) { // make sure there is a space between "user" and the username
  86. System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
  87. return;
  88. }
  89.  
  90. while (token.equals(" ")) { // loop through the spaces between "user" and the username
  91. token = tokenizedLine.nextToken();
  92. }
  93.  
  94. String username = "";
  95.  
  96. while (tokenizedLine.hasMoreTokens() && !token.equals("\r")) { // store the username from the remaining tokens
  97. username += token; // add token to username variable
  98. token = tokenizedLine.nextToken();
  99. }
  100.  
  101. char[] characters = username.toCharArray(); // convert the username to a character array
  102. for (int x = 0; x < characters.length; x++) { // loop through each character of the username
  103. if ((int) characters[x] <= 127 && (int) characters[x] >= 0) {
  104. // check if character is one of the 128 ASCII characters
  105. continue;
  106. } else { // the username characters were invalid
  107. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  108. return;
  109. }
  110. }
  111.  
  112. if (token.equals("\r")) { // check for <CR>
  113. if(tokenizedLine.hasMoreTokens()) { // check if token has more tokens
  114. if (tokenizedLine.nextToken().equals("\n")) { // check for <LF>
  115. if (username.length() > 0) { // make sure there was a username entered
  116. checkOrder("user");
  117. return;
  118. } else { // no username was entered
  119. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  120. return;
  121. }
  122. } else { // there was no <LF>
  123. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  124. return;
  125. }
  126. } else { // there was no <LF>
  127. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  128. return;
  129. }
  130. } else { // there was no <CR>
  131. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  132. return;
  133. }
  134. }
  135.  
  136. else { // there were no more tokens after the command
  137. System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
  138. return;
  139. }
  140. }
  141.  
  142. else if (token.equalsIgnoreCase("pass")) { // see if the string representing the token matches "pass"
  143. if (tokenizedLine.hasMoreTokens()) { // check if token has more tokens
  144. token = tokenizedLine.nextToken();
  145.  
  146. if (!token.equals(" ")) { // make sure there is a space between "pass" and the password
  147. System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
  148. return;
  149. }
  150.  
  151. while (token.equals(" ")) { // loop through the spaces between "pass" and the password
  152. token = tokenizedLine.nextToken();
  153. }
  154.  
  155. String password = "";
  156.  
  157. while (tokenizedLine.hasMoreTokens() && !token.equals("\r")) { //store the password from the remaining tokens
  158. password += token;
  159. token = tokenizedLine.nextToken();
  160. }
  161.  
  162. char[] characters = password.toCharArray(); // create a character array of the password
  163. for (int x = 0; x < characters.length; x++) { // loop through every character of the password
  164. if ((int) characters[x] <= 127 && (int) characters[x] >= 0) {
  165. // check that every character is one of the 128 ASCII characters
  166. continue;
  167. } else { // the password characters were invalid
  168. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  169. return;
  170. }
  171. }
  172.  
  173. if (token.equals("\r")) { // check for <CR>
  174. if(tokenizedLine.hasMoreTokens()) { // check if token has more tokens
  175. if (tokenizedLine.nextToken().equals("\n")) { // check for <LF>
  176. if (password.length() > 0) { // make sure there was a password entered
  177. checkOrder("pass");
  178. return;
  179. } else { // no password was entered
  180. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  181. return;
  182. }
  183. } else { // there was no <LF>
  184. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  185. return;
  186. }
  187. } else { // there was no <LF>
  188. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  189. return;
  190. }
  191. } else { // there was no <CR>
  192. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  193. return;
  194. }
  195. } else { // there were no more tokens after the command
  196. System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
  197. return;
  198. }
  199. }
  200.  
  201. else if (token.equalsIgnoreCase("type")) { // see if the string representing the token matches "type"
  202. token = tokenizedLine.nextToken();
  203.  
  204. if (!token.equals(" ")) { // make sure there is a space between "type" and the character chosen
  205. System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
  206. return;
  207. }
  208.  
  209. while (token.equals(" ")) { // loop through the spaces between type and the character
  210. token = tokenizedLine.nextToken();
  211. }
  212.  
  213. if (tokenizedLine.hasMoreTokens()) { // check if there are more tokens
  214. if (token.equals("A") || token.equals("I")) { // check that those tokens are either 'A' or 'I'
  215. if (token.equals("A")) {
  216. type = "a";
  217. }
  218. if (token.equals("I")) {
  219. type = "i";
  220. }
  221. if (tokenizedLine.hasMoreTokens()) { // check if there are more tokens
  222. token = tokenizedLine.nextToken();
  223. if (token.equals("\r")) { // check for <CR>
  224. if(tokenizedLine.hasMoreTokens()) { // check if token has more tokens
  225. if (tokenizedLine.nextToken().equals("\n")) { // check for <LF>
  226. checkOrder("type");
  227. return;
  228. } else { // there was no <LF>
  229. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  230. return;
  231. }
  232. } else { // there was no <LF>
  233. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  234. return;
  235. }
  236. } else { // there was no <CR>
  237. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  238. return;
  239. }
  240. } else { // there were no more tokens after the command
  241. System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
  242. return;
  243. }
  244. } else { // the type-code was invalid
  245. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  246. }
  247. } else { // there were no more tokens after the command
  248. System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
  249. return;
  250. }
  251. }
  252.  
  253. else if (token.equalsIgnoreCase("noop") || token.equalsIgnoreCase("syst") || token.equalsIgnoreCase("quit")) {
  254. // see if the string representing the token matches "noop", "syst", or "quit"
  255. if (tokenizedLine.hasMoreTokens() && tokenizedLine.nextToken().equals("\r")) { // checks if next token is <CR>
  256. if (tokenizedLine.hasMoreTokens() && tokenizedLine.nextToken().equals("\n")) { // check if next token is <LF>
  257. if (token.equalsIgnoreCase("noop")) {
  258. checkOrder("noop");
  259. } else if (token.equalsIgnoreCase("syst")) {
  260. checkOrder("syst");
  261. } else {
  262. checkOrder("quit");
  263. }
  264. return;
  265. } else { // there was no <LF>
  266. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  267. return;
  268. }
  269. } else { // there was no <CR>
  270. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  271. return;
  272. }
  273. }
  274.  
  275. else if (token.equalsIgnoreCase("port")) {
  276. if (tokenizedLine.hasMoreTokens()) { // check if string has more tokens
  277. token = tokenizedLine.nextToken();
  278.  
  279. if (!token.equals(" ")) { // make sure there is a space between "user" and the pathname
  280. System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
  281. return;
  282. }
  283.  
  284. while (token.equals(" ")) { // loop through the spaces between "user" and the pathname
  285. token = tokenizedLine.nextToken();
  286. }
  287.  
  288. String hostport = "";
  289.  
  290. while (tokenizedLine.hasMoreTokens() && !token.equals("\r")) { // store the pathname from the remaining tokens
  291. hostport += token; // add token to pathname variable
  292. token = tokenizedLine.nextToken();
  293. }
  294.  
  295. char[] hostChar = hostport.toCharArray();
  296.  
  297. for (int i = 0; i < hostChar.length; i++) {
  298. if ((int) hostChar[i] <= 255 && (int) hostChar[i] > 0) {
  299. continue;
  300. } else {
  301. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  302. return;
  303. }
  304. }
  305.  
  306. String[] parts = hostport.split(",",6);
  307.  
  308. if (parts.length == 6) {
  309.  
  310. String ha1 = parts[0];
  311. String ha2 = parts[1];
  312. String ha3 = parts[2];
  313. String ha4 = parts[3];
  314. int pa1 = Integer.parseInt(parts[4]);
  315. int pa2 = Integer.parseInt(parts[5]);
  316.  
  317. if (token.equals("\r")) { // check for <CR>
  318. if(tokenizedLine.hasMoreTokens()) { // check if token has more tokens
  319. if (tokenizedLine.nextToken().equals("\n")) { // check for <LF>
  320. processPort(ha1,ha2,ha3,ha4,pa1,pa2);
  321. checkOrder("port");
  322. return;
  323. } else { // no pathname was entered
  324. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  325. return;
  326. }
  327. } else { // there was no <LF>
  328. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  329. return;
  330. }
  331. } else { // there was no <CR>
  332. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  333. return;
  334. }
  335. } else {
  336. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  337. return;
  338. }
  339. } else {
  340. System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
  341. return;
  342. }
  343. }
  344.  
  345. else if (token.equalsIgnoreCase("retr")) {
  346. if (tokenizedLine.hasMoreTokens()) { // check if string has more tokens
  347. token = tokenizedLine.nextToken();
  348.  
  349. if (!token.equals(" ")) { // make sure there is a space between "user" and the pathname
  350. System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
  351. return;
  352. }
  353.  
  354. while (token.equals(" ")) { // loop through the spaces between "user" and the pathname
  355. token = tokenizedLine.nextToken();
  356. }
  357.  
  358. while (token.charAt(0) == '/' || token.charAt(0) == '\\') {
  359. token = token.substring(1, token.length());
  360. }
  361.  
  362. String pathname = "";
  363.  
  364. while (tokenizedLine.hasMoreTokens() && !token.equals("\r")) { // store the pathname from the remaining tokens
  365. pathname += token; // add token to pathname variable
  366. token = tokenizedLine.nextToken();
  367. }
  368.  
  369. char[] characters = pathname.toCharArray(); // convert the pathname to a character array
  370. for (int x = 0; x < characters.length; x++) { // loop through each character of the pathname
  371. if ((int) characters[x] <= 127 && (int) characters[x] >= 0) {
  372. // check if character is one of the 128 ASCII characters
  373. continue;
  374. } else { // the pathname characters were invalid
  375. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  376. return;
  377. }
  378. }
  379.  
  380. if (token.equals("\r")) { // check for <CR>
  381. if(tokenizedLine.hasMoreTokens()) { // check if token has more tokens
  382. if (tokenizedLine.nextToken().equals("\n")) { // check for <LF>
  383. if (pathname.length() > 0) { // make sure there was a pathname entered
  384. retrCount++;
  385. processRetr(pathname);
  386. checkOrder("retr");
  387. return;
  388. } else { // no pathname was entered
  389. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  390. return;
  391. }
  392. } else { // there was no <LF>
  393. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  394. return;
  395. }
  396. } else { // there was no <LF>
  397. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  398. return;
  399. }
  400. } else { // there was no <CR>
  401. System.out.printf("501 Syntax error in parameter.%s", CRLF);
  402. return;
  403. }
  404. }
  405.  
  406. else { // there were no more tokens after the command
  407. System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
  408. return;
  409. }
  410. }
  411.  
  412. else { // the command entered did not match "user", "pass", "type", "syst", "noop", or "quit"
  413. System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
  414. return;
  415. }
  416. }
  417. }
  418.  
  419. public static void checkOrder(String command) {
  420. if (command.equals("user")) {
  421. if (user == false) {
  422. user = true;
  423. needPass = true;
  424. System.out.printf("331 Guest access OK, send password.%s", CRLF);
  425. return;
  426. } else {
  427. System.out.printf("503 Bad sequence of commands.%s", CRLF);
  428. return;
  429. }
  430. } else if (command.equals("pass")) {
  431. if (user && needPass) {
  432. needPass = false;
  433. loggedIn = true;
  434. System.out.printf("230 Guest login OK.%s", CRLF);
  435. return;
  436. } else {
  437. System.out.printf("503 Bad sequence of commands.%s", CRLF);
  438. return;
  439. }
  440. } else if (command.equals("type")) {
  441. if (needPass) {
  442. System.out.printf("503 Bad sequence of commands.%s", CRLF);
  443. return;
  444. } else if (!loggedIn) {
  445. System.out.printf("530 Not logged in.%s", CRLF);
  446. return;
  447. } else {
  448. if (type.equalsIgnoreCase("a")) {
  449. System.out.printf("200 Type set to A.%s", CRLF);
  450. return;
  451. } else {
  452. System.out.printf("200 Type set to I.%s", CRLF);
  453. return;
  454. }
  455. }
  456. } else if (command.equals("noop")) {
  457. if (needPass) {
  458. System.out.printf("503 Bad sequence of commands.%s", CRLF);
  459. return;
  460. } else if (!loggedIn) {
  461. System.out.printf("530 Not logged in.%s", CRLF);
  462. return;
  463. } else {
  464. System.out.printf("200 Command OK.%s", CRLF);
  465. return;
  466. }
  467. } else if (command.equals("syst")) {
  468. if (needPass) {
  469. System.out.printf("503 Bad sequence of commands.%s", CRLF);
  470. return;
  471. } else if (!loggedIn) {
  472. System.out.printf("530 Not logged in.%s", CRLF);
  473. return;
  474. } else {
  475. System.out.printf("215 UNIX Type: L8.%s", CRLF);
  476. return;
  477. }
  478. } else if (command.equals("port")) {
  479. if (needPass) {
  480. System.out.printf("503 Bad sequence of commands.%s", CRLF);
  481. return;
  482. } else if (!loggedIn) {
  483. System.out.printf("530 Not logged in.%s", CRLF);
  484. return;
  485. } else {
  486. needRetr = true;
  487. System.out.printf("200 Port command successful (" + hostaddress + portnumber + ").%s", CRLF);
  488. return;
  489. }
  490. } else if (command.equals("retr")) {
  491. if (needPass) {
  492. System.out.printf("503 Bad sequence of commands.%s", CRLF);
  493. return;
  494. } else if (!loggedIn) {
  495. System.out.printf("530 Not logged in.%s", CRLF);
  496. return;
  497. } else if (needRetr) {
  498. if (validRetr) {
  499. System.out.printf("250 Requested file action completed.%s", CRLF);
  500. validRetr = false;
  501. needRetr = false;
  502. return;
  503. } else {
  504. return;
  505. }
  506. } else {
  507. System.out.printf("503 Bad sequence of commands.%s", CRLF);
  508. return;
  509. }
  510. } else {
  511. System.out.printf("200 Command OK.%s", CRLF);
  512. System.exit(1);
  513. }
  514. }
  515.  
  516. public static void processPort(String ha1, String ha2, String ha3, String ha4, int pa1, int pa2) {
  517. hostaddress = ha1 + "." + ha2 + "." + ha3 + "." + ha4 + ",";
  518. portnumber = (pa1 * 256) + pa2;
  519. }
  520.  
  521. public static void processRetr(String pathname) throws IOException {
  522. String filename = "retr_files/file" + retrCount;
  523. FileInputStream in = null;
  524. FileOutputStream out = null;
  525.  
  526. File pathnameFile = new File(pathname);
  527. if (needRetr) {
  528. if (pathnameFile.exists()) {
  529. System.out.printf("150 File status okay.%s", CRLF);
  530. validRetr = true;
  531. }
  532. }
  533.  
  534. try {
  535. in = new FileInputStream(pathname);
  536. out = new FileOutputStream(filename);
  537.  
  538. int x;
  539.  
  540. while ((x = in.read()) != -1) {
  541. out.write(x);
  542. }
  543.  
  544. if (in != null) {
  545. in.close();
  546. }
  547.  
  548. if (out != null) {
  549. out.close();
  550. }
  551. } catch (IOException e) {
  552. System.out.printf("550 File not found or access denied.%s", CRLF);
  553. validRetr = false;
  554. return;
  555. }
  556. }
  557.  
  558.  
  559. public static void fail(Exception e, String msg) { // handle exceptions
  560. System.err.println(msg + ": " + e);
  561. System.exit(1);
  562. }
  563. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement