Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.nio.file.StandardCopyOption;
- import java.util.ArrayList;
- import java.util.StringTokenizer;
- public class FTP2server {
- final static String CRLF = "\r\n";
- static int retrCount = 0;
- static boolean user = false;
- static boolean needPass = false;
- static boolean loggedIn = false;
- static boolean pass = false;
- static String type = "";
- static boolean syst = false;
- static String hostaddress = "";
- static int portnumber = 0;
- static boolean needRetr = false;
- static boolean validRetr = false;
- // Program-Class overview: The purpose of FTP1Server is to determine if a received command is a valid FTP command.
- // My approach was to first read the input from the console and then parse the string in order to check for validity.
- public static void main(String[] args) throws IOException { // input is saved and passed to "parse"
- BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); // declare and instantiate a BufferedReader
- ArrayList<String> inputList = new ArrayList<String>(); // declare and instantiate an ArrayList
- String input = ""; // declare and instantiate input
- int i = 0;
- i = b.read();
- while (i != -1) { // read input from the console and compile it into inputList
- input += (char) i; // scan characters into input string
- if ((char) i == '\r') { // check for the end of input
- i = b.read();
- if ((char) i == '\n') {
- input += (char) i;
- inputList.add(input);
- input = "";
- i = b.read();
- }
- else {
- inputList.add(input);
- input = "";
- }
- } else if ((char) i == '\n') {
- inputList.add(input);
- input = "";
- i = b.read();
- } else {
- i = b.read();
- }
- }
- System.out.printf("220 COMP 431 FTP server ready.%s", CRLF);
- for (int j = 0; j < inputList.size(); j++) { // loop through the inputList in order to pass each input to "parse"
- input = inputList.get(j);
- System.out.print(input);
- parse(input);
- }
- }
- static void parse(String inputline) throws IOException { // takes input from the console and checks each string for valid FTP characteristics
- String token = ""; // The current token
- StringTokenizer tokenizedLine = new StringTokenizer(inputline, " \n\r", true); // declare and instantiate a
- // StringTokenizer with delimiters
- if (tokenizedLine.hasMoreTokens()) { // check if string has more tokens
- token = tokenizedLine.nextToken(); // Get the token
- if (token.equalsIgnoreCase("user")) { // see if the string representing the token matches "user"
- if (tokenizedLine.hasMoreTokens()) { // check if string has more tokens
- token = tokenizedLine.nextToken();
- if (!token.equals(" ")) { // make sure there is a space between "user" and the username
- System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
- return;
- }
- while (token.equals(" ")) { // loop through the spaces between "user" and the username
- token = tokenizedLine.nextToken();
- }
- String username = "";
- while (tokenizedLine.hasMoreTokens() && !token.equals("\r")) { // store the username from the remaining tokens
- username += token; // add token to username variable
- token = tokenizedLine.nextToken();
- }
- char[] characters = username.toCharArray(); // convert the username to a character array
- for (int x = 0; x < characters.length; x++) { // loop through each character of the username
- if ((int) characters[x] <= 127 && (int) characters[x] >= 0) {
- // check if character is one of the 128 ASCII characters
- continue;
- } else { // the username characters were invalid
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- }
- if (token.equals("\r")) { // check for <CR>
- if(tokenizedLine.hasMoreTokens()) { // check if token has more tokens
- if (tokenizedLine.nextToken().equals("\n")) { // check for <LF>
- if (username.length() > 0) { // make sure there was a username entered
- checkOrder("user");
- return;
- } else { // no username was entered
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else { // there was no <LF>
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else { // there was no <LF>
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else { // there was no <CR>
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- }
- else { // there were no more tokens after the command
- System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
- return;
- }
- }
- else if (token.equalsIgnoreCase("pass")) { // see if the string representing the token matches "pass"
- if (tokenizedLine.hasMoreTokens()) { // check if token has more tokens
- token = tokenizedLine.nextToken();
- if (!token.equals(" ")) { // make sure there is a space between "pass" and the password
- System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
- return;
- }
- while (token.equals(" ")) { // loop through the spaces between "pass" and the password
- token = tokenizedLine.nextToken();
- }
- String password = "";
- while (tokenizedLine.hasMoreTokens() && !token.equals("\r")) { //store the password from the remaining tokens
- password += token;
- token = tokenizedLine.nextToken();
- }
- char[] characters = password.toCharArray(); // create a character array of the password
- for (int x = 0; x < characters.length; x++) { // loop through every character of the password
- if ((int) characters[x] <= 127 && (int) characters[x] >= 0) {
- // check that every character is one of the 128 ASCII characters
- continue;
- } else { // the password characters were invalid
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- }
- if (token.equals("\r")) { // check for <CR>
- if(tokenizedLine.hasMoreTokens()) { // check if token has more tokens
- if (tokenizedLine.nextToken().equals("\n")) { // check for <LF>
- if (password.length() > 0) { // make sure there was a password entered
- checkOrder("pass");
- return;
- } else { // no password was entered
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else { // there was no <LF>
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else { // there was no <LF>
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else { // there was no <CR>
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else { // there were no more tokens after the command
- System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
- return;
- }
- }
- else if (token.equalsIgnoreCase("type")) { // see if the string representing the token matches "type"
- token = tokenizedLine.nextToken();
- if (!token.equals(" ")) { // make sure there is a space between "type" and the character chosen
- System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
- return;
- }
- while (token.equals(" ")) { // loop through the spaces between type and the character
- token = tokenizedLine.nextToken();
- }
- if (tokenizedLine.hasMoreTokens()) { // check if there are more tokens
- if (token.equals("A") || token.equals("I")) { // check that those tokens are either 'A' or 'I'
- if (token.equals("A")) {
- type = "a";
- }
- if (token.equals("I")) {
- type = "i";
- }
- if (tokenizedLine.hasMoreTokens()) { // check if there are more tokens
- token = tokenizedLine.nextToken();
- if (token.equals("\r")) { // check for <CR>
- if(tokenizedLine.hasMoreTokens()) { // check if token has more tokens
- if (tokenizedLine.nextToken().equals("\n")) { // check for <LF>
- checkOrder("type");
- return;
- } else { // there was no <LF>
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else { // there was no <LF>
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else { // there was no <CR>
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else { // there were no more tokens after the command
- System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
- return;
- }
- } else { // the type-code was invalid
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- }
- } else { // there were no more tokens after the command
- System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
- return;
- }
- }
- else if (token.equalsIgnoreCase("noop") || token.equalsIgnoreCase("syst") || token.equalsIgnoreCase("quit")) {
- // see if the string representing the token matches "noop", "syst", or "quit"
- if (tokenizedLine.hasMoreTokens() && tokenizedLine.nextToken().equals("\r")) { // checks if next token is <CR>
- if (tokenizedLine.hasMoreTokens() && tokenizedLine.nextToken().equals("\n")) { // check if next token is <LF>
- if (token.equalsIgnoreCase("noop")) {
- checkOrder("noop");
- } else if (token.equalsIgnoreCase("syst")) {
- checkOrder("syst");
- } else {
- checkOrder("quit");
- }
- return;
- } else { // there was no <LF>
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else { // there was no <CR>
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- }
- else if (token.equalsIgnoreCase("port")) {
- if (tokenizedLine.hasMoreTokens()) { // check if string has more tokens
- token = tokenizedLine.nextToken();
- if (!token.equals(" ")) { // make sure there is a space between "user" and the pathname
- System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
- return;
- }
- while (token.equals(" ")) { // loop through the spaces between "user" and the pathname
- token = tokenizedLine.nextToken();
- }
- String hostport = "";
- while (tokenizedLine.hasMoreTokens() && !token.equals("\r")) { // store the pathname from the remaining tokens
- hostport += token; // add token to pathname variable
- token = tokenizedLine.nextToken();
- }
- char[] hostChar = hostport.toCharArray();
- for (int i = 0; i < hostChar.length; i++) {
- if ((int) hostChar[i] <= 255 && (int) hostChar[i] > 0) {
- continue;
- } else {
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- }
- String[] parts = hostport.split(",",6);
- if (parts.length == 6) {
- String ha1 = parts[0];
- String ha2 = parts[1];
- String ha3 = parts[2];
- String ha4 = parts[3];
- int pa1 = Integer.parseInt(parts[4]);
- int pa2 = Integer.parseInt(parts[5]);
- if (token.equals("\r")) { // check for <CR>
- if(tokenizedLine.hasMoreTokens()) { // check if token has more tokens
- if (tokenizedLine.nextToken().equals("\n")) { // check for <LF>
- processPort(ha1,ha2,ha3,ha4,pa1,pa2);
- checkOrder("port");
- return;
- } else { // no pathname was entered
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else { // there was no <LF>
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else { // there was no <CR>
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else {
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else {
- System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
- return;
- }
- }
- else if (token.equalsIgnoreCase("retr")) {
- if (tokenizedLine.hasMoreTokens()) { // check if string has more tokens
- token = tokenizedLine.nextToken();
- if (!token.equals(" ")) { // make sure there is a space between "user" and the pathname
- System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
- return;
- }
- while (token.equals(" ")) { // loop through the spaces between "user" and the pathname
- token = tokenizedLine.nextToken();
- }
- while (token.charAt(0) == '/' || token.charAt(0) == '\\') {
- token = token.substring(1, token.length());
- }
- String pathname = "";
- while (tokenizedLine.hasMoreTokens() && !token.equals("\r")) { // store the pathname from the remaining tokens
- pathname += token; // add token to pathname variable
- token = tokenizedLine.nextToken();
- }
- char[] characters = pathname.toCharArray(); // convert the pathname to a character array
- for (int x = 0; x < characters.length; x++) { // loop through each character of the pathname
- if ((int) characters[x] <= 127 && (int) characters[x] >= 0) {
- // check if character is one of the 128 ASCII characters
- continue;
- } else { // the pathname characters were invalid
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- }
- if (token.equals("\r")) { // check for <CR>
- if(tokenizedLine.hasMoreTokens()) { // check if token has more tokens
- if (tokenizedLine.nextToken().equals("\n")) { // check for <LF>
- if (pathname.length() > 0) { // make sure there was a pathname entered
- retrCount++;
- processRetr(pathname);
- checkOrder("retr");
- return;
- } else { // no pathname was entered
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else { // there was no <LF>
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else { // there was no <LF>
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- } else { // there was no <CR>
- System.out.printf("501 Syntax error in parameter.%s", CRLF);
- return;
- }
- }
- else { // there were no more tokens after the command
- System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
- return;
- }
- }
- else { // the command entered did not match "user", "pass", "type", "syst", "noop", or "quit"
- System.out.printf("500 Syntax error, command unrecognized.%s", CRLF);
- return;
- }
- }
- }
- public static void checkOrder(String command) {
- if (command.equals("user")) {
- if (user == false) {
- user = true;
- needPass = true;
- System.out.printf("331 Guest access OK, send password.%s", CRLF);
- return;
- } else {
- System.out.printf("503 Bad sequence of commands.%s", CRLF);
- return;
- }
- } else if (command.equals("pass")) {
- if (user && needPass) {
- needPass = false;
- loggedIn = true;
- System.out.printf("230 Guest login OK.%s", CRLF);
- return;
- } else {
- System.out.printf("503 Bad sequence of commands.%s", CRLF);
- return;
- }
- } else if (command.equals("type")) {
- if (needPass) {
- System.out.printf("503 Bad sequence of commands.%s", CRLF);
- return;
- } else if (!loggedIn) {
- System.out.printf("530 Not logged in.%s", CRLF);
- return;
- } else {
- if (type.equalsIgnoreCase("a")) {
- System.out.printf("200 Type set to A.%s", CRLF);
- return;
- } else {
- System.out.printf("200 Type set to I.%s", CRLF);
- return;
- }
- }
- } else if (command.equals("noop")) {
- if (needPass) {
- System.out.printf("503 Bad sequence of commands.%s", CRLF);
- return;
- } else if (!loggedIn) {
- System.out.printf("530 Not logged in.%s", CRLF);
- return;
- } else {
- System.out.printf("200 Command OK.%s", CRLF);
- return;
- }
- } else if (command.equals("syst")) {
- if (needPass) {
- System.out.printf("503 Bad sequence of commands.%s", CRLF);
- return;
- } else if (!loggedIn) {
- System.out.printf("530 Not logged in.%s", CRLF);
- return;
- } else {
- System.out.printf("215 UNIX Type: L8.%s", CRLF);
- return;
- }
- } else if (command.equals("port")) {
- if (needPass) {
- System.out.printf("503 Bad sequence of commands.%s", CRLF);
- return;
- } else if (!loggedIn) {
- System.out.printf("530 Not logged in.%s", CRLF);
- return;
- } else {
- needRetr = true;
- System.out.printf("200 Port command successful (" + hostaddress + portnumber + ").%s", CRLF);
- return;
- }
- } else if (command.equals("retr")) {
- if (needPass) {
- System.out.printf("503 Bad sequence of commands.%s", CRLF);
- return;
- } else if (!loggedIn) {
- System.out.printf("530 Not logged in.%s", CRLF);
- return;
- } else if (needRetr) {
- if (validRetr) {
- System.out.printf("250 Requested file action completed.%s", CRLF);
- validRetr = false;
- needRetr = false;
- return;
- } else {
- return;
- }
- } else {
- System.out.printf("503 Bad sequence of commands.%s", CRLF);
- return;
- }
- } else {
- System.out.printf("200 Command OK.%s", CRLF);
- System.exit(1);
- }
- }
- public static void processPort(String ha1, String ha2, String ha3, String ha4, int pa1, int pa2) {
- hostaddress = ha1 + "." + ha2 + "." + ha3 + "." + ha4 + ",";
- portnumber = (pa1 * 256) + pa2;
- }
- public static void processRetr(String pathname) throws IOException {
- String filename = "retr_files/file" + retrCount;
- FileInputStream in = null;
- FileOutputStream out = null;
- File pathnameFile = new File(pathname);
- if (needRetr) {
- if (pathnameFile.exists()) {
- System.out.printf("150 File status okay.%s", CRLF);
- validRetr = true;
- }
- }
- try {
- in = new FileInputStream(pathname);
- out = new FileOutputStream(filename);
- int x;
- while ((x = in.read()) != -1) {
- out.write(x);
- }
- if (in != null) {
- in.close();
- }
- if (out != null) {
- out.close();
- }
- } catch (IOException e) {
- System.out.printf("550 File not found or access denied.%s", CRLF);
- validRetr = false;
- return;
- }
- }
- public static void fail(Exception e, String msg) { // handle exceptions
- System.err.println(msg + ": " + e);
- System.exit(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement