Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //sudo apt-get install whois -> Use to install function for mkpasswd
- //C++ Library
- #include <iostream>
- #include <cstring>
- #include <string>
- #include <fstream>
- #include <sstream>
- //C Library
- #include <unistd.h>
- #include <stdio.h>
- #include <termios.h>
- #include <stdlib.h>
- using namespace std; //using standard namespace
- //Functions
- /**Create OR Login User Function Headers**/
- int getch();
- string getpass(const char*, bool);
- void createUser();
- bool validatePass(char[]);
- bool validateShadow(string, string);
- bool checkUser(string);
- bool checkPasswd(string);
- void addSalt(string);
- void addShadow(string, string, int);
- void addPasswd(string, string, int);
- /**File System Function Headers**/
- void SystemMenu(string);
- bool checkFile(string);
- void createFile(string, int, int);
- int getUserLevel(string);
- int getFileLevel(string);
- void readFile(string, int);
- void writeFile(string, int);
- void listFile();
- void saveFile();
- //Main Function
- int main (int argc, char* argv[])
- {
- //Variable
- string username;
- string password;
- //Create files for those computer don't have these files
- system("touch salt.txt");
- system("touch shadow.txt");
- system("touch passwd.txt");
- system("touch Files.store");
- cout<<"\tWelcome to File System"<<endl;
- cout<<"======================================"<<endl;
- if(argc == 1) //If run with ./FileSystem
- {
- cout<<"Login to File System\n======================"<<endl;
- enteruser:
- cout<<"Username : ";
- cin>>username;
- if(checkUser(username) == false && checkPasswd(username) == false) //Check if user name and password does not exist, prompt user to enter again
- {
- cout<<"User Does Not Exist. Please Try Again. "<<endl;
- goto enteruser;
- }
- else //Else ask for password and continue to login
- {
- cin.ignore(); //To reserve a line
- password=getpass("Password: ",true); //getpass("Display", if true = hide password, false = display password)
- if (validateShadow(username, password) == true) //Validate username and password/login
- {
- sleep (5); //Delay
- SystemMenu(username);
- }
- else //If fail to match hash value
- {
- cout<<"Password Incorrect!"<<endl;
- cout<<"Hash Value Does Not Match. \nProgram Terminating......."<<endl;
- }
- }
- }
- else if(strcmp(argv[1], "-i") == 0) //Run with ./FileSystem -i
- {
- cout<<"Username : ";
- cin>>username;
- if(checkUser(username) == false)
- {
- cout<<"User Does Not Exist. Proceed to Create User Account. "<<endl;
- createUser();
- }
- else
- cout<<"User already Exist inside System. \nProgram Terminating......."<<endl;
- }
- }
- //Create User
- void createUser()
- {
- //Variable
- string username;
- string pwd1;
- string pwd2;
- char *passwd;
- cout<<"\tRegister"<<endl;
- cout<<"======================================"<<endl;
- cout<<"Username : ";
- cin>>username;
- cin.ignore(); //use to reserve for a next string input
- tryagain:
- pwd1=getpass("Please enter the password: ",true); //Hide input password by using getpass() function
- //convert string to char
- passwd = new char [pwd1.length()];
- strcpy(passwd, pwd1.c_str());
- if (validatePass(passwd) == true)
- {
- int count=1;
- enter_again:
- if(count<4)
- {
- pwd2=getpass("Enter password again: ",true);
- if(pwd1 == pwd2)
- {
- int clearance;
- cout<<"Successful created"<<endl;
- addSalt(username);
- enterclearance:
- cout<<"User Clearance(0/1/2) : ";
- cin>>clearance;
- if(clearance < 0 ||clearance > 2)
- {
- cout<<"Invalid clearance."<<endl;
- goto enterclearance;
- }
- else
- {
- addShadow(username, pwd1, clearance);
- addPasswd(username, pwd1, clearance);
- }
- }
- else
- {
- cout<<"Password invalid. Please try again.("<<3-count<<")"<<endl;
- count++;
- goto enter_again;
- }
- }
- else
- {
- cout<<"Enter password again."<<endl;
- goto tryagain;
- }
- }
- else
- {
- cout<<"Enter password again."<<endl;
- goto tryagain;
- }
- }
- //Check User, salt file
- bool checkUser(string user)
- {
- string line;
- ifstream inFile("salt.txt");
- if (!inFile.is_open())
- cout<<"Unable to open file!";
- while(getline(inFile, line))
- {
- stringstream ss;
- ss.str(line);
- string str1;
- for(int x = 0;x<2;x++)
- {
- getline(ss, str1, ':');
- if(x==0)
- {
- string name =str1;
- if(name == user)
- {
- return true;
- break;
- }
- }
- }
- }
- return false;
- }
- //Check Password, passwd file
- bool checkPasswd(string user)
- {
- string line;
- ifstream inFile("passwd.txt");
- if (!inFile.is_open())
- cout<<"Unable to open passwd.txt!"<<endl;
- while(getline(inFile, line))
- {
- stringstream ss;
- ss.str(line);
- string str1;
- for(int x = 0;x<2;x++)
- {
- getline(ss, str1, ':');
- if(x==0)
- {
- string name =str1;
- if(name == user)
- {
- return true;
- break;
- }
- }
- }
- }
- return false;
- }
- //Check for validate password
- bool validatePass(char pass[])
- {
- //Password policy: one symbol, an upper case, lower case, digit, min 8 character
- bool size = false, upper = false, alpha = false, digit =false, punct = false;
- if( strlen(pass) == 8) //to ensure password is 8 character
- {
- size = true;
- }
- for (int i = 0; pass[i]; ++i)
- {
- if(isupper(pass[i])) //upper case
- upper = true;
- else if(isalpha(pass[i])) //alphabet
- alpha = true;
- else if(isdigit(pass[i])) //digit
- digit = true;
- else if(ispunct(pass[i]) ) //symbol
- punct = true;
- }
- //Show error message
- if(size == false)
- {
- cout<<"Min 8 character."<<endl;
- }
- if(upper == false)
- {
- cout<<"At least 1 upper case character."<<endl;
- }
- if(alpha == false)
- {
- cout<<"At least 1 letter character."<<endl;
- }
- if(digit == false)
- {
- cout<<"At least 1 digit number."<<endl;
- }
- if(punct == false)
- {
- cout<<"At least 1 symbol character."<<endl;
- }
- if(upper && alpha && digit && punct && size)
- return true;
- else
- return false;
- }
- //Create shadow password
- bool validateShadow(string username, string pass)
- {
- char mkpass_cmd[] = "mkpasswd -m md5 "; //MD5 command
- string line, salt_num, name;
- //get salt password from salt.txt
- ifstream inFile("salt.txt");
- if (!inFile.is_open())
- cout<<"Unable to open file!";
- while(getline(inFile, line))
- {
- stringstream ss;
- ss.str(line);
- string str1;
- for(int x = 0;x<2;x++)
- {
- getline(ss, str1, ':');
- if(x==0)
- {
- name =str1;
- }
- else if (x==1)
- {
- if(name == username)
- {
- salt_num = str1.c_str();
- }
- }
- }
- }
- //convert salt and userpassword to Passhash
- system("touch temp.txt"); //create a temp file to store system output
- char salt[20];
- strcpy(salt, salt_num.c_str());
- //Combine mkpass_cmd with Password
- strcat (mkpass_cmd, pass.c_str());
- //Combine mkpass_cmd with -s
- strcat (mkpass_cmd, " -s ");
- //Combine mkpass_cmd with salt value
- strcat (mkpass_cmd, salt);
- strcat (mkpass_cmd, " >temp.txt");
- system(mkpass_cmd);
- //open temp file and take out the output
- ifstream intemp("temp.txt");
- string hash;
- getline(intemp, hash);
- system("rm temp.txt"); //remove temp file
- system("clear"); //clear screen
- //Output to user interface
- cout<<username<<" Found in salt.txt and passwd.txt. "<<endl;
- cout<<"\nSalt Retrieved: "<<salt_num<<endl;
- cout<<"\nhashing......"<<endl;
- cout<<"\nHash Value: "<<hash<<endl;
- cout<<"\n\n\nLoading........."<<endl;
- //open shadow file
- string salt_hash;
- ifstream inShadow("shadow.txt");
- if (!inShadow.is_open())
- cout<<"Unable to open file!";
- while(getline(inShadow, line))
- {
- stringstream ss;
- ss.str(line);
- string str1;
- for(int x = 0;x<2;x++)
- {
- getline(ss, str1, ':');
- if(x==0)
- {
- name =str1;
- }
- else if (x==1)
- {
- if(name == username)
- {
- salt_hash = str1.c_str();
- }
- }
- }
- }
- //Compare Generated Hash Value with Hash Value inside shadow.txt
- if(salt_hash == hash)
- {
- return true;
- }
- else
- return false;
- }
- //Hide input character(C language), instead of using <conio.h> library
- int getch()
- {
- int ch;
- struct termios t_old, t_new;
- tcgetattr(STDIN_FILENO, &t_old);
- t_new = t_old;
- t_new.c_lflag &= ~(ICANON | ECHO);
- tcsetattr(STDIN_FILENO, TCSANOW, &t_new);
- ch = getchar();
- tcsetattr(STDIN_FILENO, TCSANOW, &t_old);
- return ch;
- }
- //Declaration of a function to hide input character
- string getpass(const char *prompt, bool show_asterisk=true)
- {
- const char BACKSPACE=127;
- const char RETURN=10;
- string password;
- unsigned char ch=0;
- cout <<prompt;
- while((ch=getch())!=RETURN)
- {
- if(ch==BACKSPACE) //when backspace is pressed, delete password
- {
- if(password.length()!=0)
- {
- if(show_asterisk) //\b is hide it to the back
- cout <<"\b \b";
- password.resize(password.length()-1);
- }
- }
- else
- {
- password+=ch;
- if(show_asterisk)
- cout <<'*'; //Show * for every single character instead of real password
- }
- }
- cout <<endl;
- return password;
- }
- //Random 8 digits function
- void addSalt(string username)
- {
- srand(time(NULL));
- int salt = rand() % 90000000 + 10000000; //random 8 digits
- ofstream outfile("salt.txt", ios::app); //outfile
- outfile<<username<<":"<<salt<<endl;
- outfile.close();
- }
- //Write to shadow file
- void addShadow(string username, string pass, int clearance)
- {
- string line;
- string salt_num;
- string name;
- char mkpass_cmd[] = "mkpasswd -m md5 "; //md5 command
- //open salt file
- ifstream inFile("salt.txt");
- if (!inFile.is_open())
- cout<<"Unable to open file!";
- //extract each line from text file
- while(getline(inFile, line))
- {
- stringstream ss;
- ss.str(line);
- string str1;
- for(int x = 0;x<2;x++)
- {
- getline(ss, str1, ':'); //split it out when meet :
- if(x==0)
- {
- name =str1;
- }
- else if (x==1)
- {
- if(name == username)
- {
- salt_num = str1.c_str(); //c_str() is a conversion function from stringstream to string
- }
- }
- }
- }
- system("touch temp.txt"); //create a temp file to store system output
- char salt[20];
- strcpy(salt, salt_num.c_str());
- //Combine mkpass_cmd with Password
- strcat (mkpass_cmd, pass.c_str());
- //Combine mkpass_cmd with -s
- strcat (mkpass_cmd, " -s ");
- //Combine mkpass_cmd with salt value
- strcat (mkpass_cmd, salt);
- strcat (mkpass_cmd, " >temp.txt");
- system(mkpass_cmd);
- //open temp file and take out the output
- ifstream intemp("temp.txt");
- string hash;
- getline(intemp, hash);
- system("rm temp.txt"); //remove temp file
- ofstream outfile("shadow.txt", ios::app);
- outfile<<username<<":"<<hash<<":"<<clearance<<endl;
- outfile.close();
- }
- //Add password
- void addPasswd(string username, string password, int clearance)
- {
- string line, name, salt, shadow;
- //Open file
- ifstream inSalt("salt.txt");
- ifstream inShadow("shadow.txt");
- ofstream outPasswd;
- if (!inShadow.is_open())
- cout << "Unable to open shadow.txt!";
- while (getline(inShadow, line))
- {
- stringstream ss;
- ss.str(line);
- string str1;
- for (int i = 1; i<=2; i++)
- {
- getline(ss, str1, ':');
- if (i == 1)
- {
- name = str1;
- }
- else if (i == 2)
- {
- if (name == username)
- {
- shadow = str1;
- }
- }
- }
- }
- outPasswd.open("passwd.txt", ios::app);
- outPasswd<<username<<":"<<shadow<<":"<<":"<<":"<<username<<":"<<":"<<":"<<endl;
- outPasswd.close();
- inSalt.close();
- inShadow.close();
- outPasswd.close();
- }
- /**File System Functions**/
- void SystemMenu(string username)
- {
- char selection;
- string filename, line;
- int filelevel;
- int userlevel = getUserLevel(username);
- do
- {
- system("clear");
- menu:
- cout<<endl<<endl;
- //Main Menu
- cout<<"\tFile System Main Menu\n ==============================="<<endl;
- cout<<"||\t(C)reate New File\t ||"<<endl;
- cout<<"||\t(R)ead From File\t ||"<<endl;
- cout<<"||\t(W)rite To File\t ||"<<endl;
- cout<<"||\t(L)ist All Files\t ||"<<endl;
- cout<<"||\t(S)ave All Records\t ||"<<endl;
- cout<<"||\t(E)xit File System\t ||"<<endl;
- cout<<" ==============================="<<endl;
- cout<<"Select Action (C/R/W/L/S/E): ";
- cin>>selection;
- //Accept input upper case and lower case
- if (selection == 'C' || selection == 'c')
- {
- system ("clear");
- //Create File with File Security Level
- cout<<"Please Enter the Name and Classification of your File: "<<endl;
- cout<<"File Name: ";
- cin>>filename;
- enterlvl:
- cout<<"Security level(0 or 1 or 2): ";
- cin>>filelevel;
- if (filelevel <0 || filelevel > 2)
- {
- cout<<"Invalid File Security Level. Please Enter Again."<<endl;
- goto enterlvl; //repeat
- }
- else if (checkFile(filename) == true)
- {
- cout<<filename<<" Already Exist in File System. Please Try Again."<<endl;
- goto menu; //repeat
- }
- else
- createFile(filename, filelevel, userlevel); //create new file
- sleep(5);
- }
- if (selection == 'R' || selection == 'r')
- {
- system ("clear");
- //Read or Write File
- cout<<"Please Enter the File Name to Read "<<endl;
- cout<<"File Name: ";
- cin>>filename;
- if (checkFile(filename) == true)
- readFile(filename, userlevel);
- else
- {
- cout<<filename<<" Does Not Exist in File System. "<<endl;
- goto menu; //repeat
- }
- sleep(5);
- }
- if (selection == 'W' || selection == 'w')
- {
- system ("clear");
- //Read or Write File
- cout<<"Please Enter the File Name to Write "<<endl;
- cout<<"File Name: ";
- cin>>filename;
- if (checkFile(filename) == true)
- writeFile(filename, userlevel);
- else
- {
- cout<<filename<<" Does Not Exist in File System. "<<endl;
- goto menu; //repeat
- }
- sleep(5);
- }
- if (selection == 'L' || selection == 'l')
- {
- system ("clear");
- listFile();
- goto menu; //repeat
- }
- if (selection == 'S' || selection == 's')
- {
- system ("clear");
- saveFile();
- goto menu;
- }
- if (selection == 'E' || selection == 'e')
- {
- system ("clear");
- char exit;
- cout<<"Shut Down the File System? (Y/N): ";
- cin>>exit;
- if (exit == 'Y' || exit == 'y')
- {
- cout<<"Listing all Files in Record ..... Before Shut Down\n\n"<<endl;
- listFile();
- break;
- }
- else if (exit == 'N' || exit == 'n')
- goto menu;
- }
- }while (selection != 'E' || selection != 'e');
- }
- //Check all exists files
- bool checkFile(string file)
- {
- string line, filename;
- bool file_exist = false;
- ifstream inFiles("Files.store");
- if (!inFiles.is_open())
- cout<<"Unable to Open File Records"<<endl;
- while (getline(inFiles,line))
- {
- stringstream ss;
- string str1;
- ss.str(line);
- for (int i=1; i<=2; i++)
- {
- getline(ss,str1,':');
- if (i == 1) //1st column of string
- filename = str1; //Set File Name
- if (file == filename)//If file exist inside Files.store
- file_exist = true;
- }
- }
- //Close inFiles
- inFiles.close();
- return file_exist;
- }
- //Create file
- void createFile(string file, int filelvl, int userlvl)
- {
- string line, filename;
- ofstream outFiles;
- if (userlvl >= filelvl)
- {
- outFiles.open("Files.store", ios::app);
- outFiles<<file<<":"<<filelvl<<endl;
- outFiles.close();
- cout<<file<<" have been Successfully Created with Security Level "<<filelvl<<". "<<endl;
- }
- else
- cout<<file<<" Create Denied due to Current User Level. "<<endl;
- cout<<"\n\n\nLoading........."<<endl;
- }
- int getUserLevel(string username)
- {
- int userlvl;
- string line, user;
- ifstream inShadow("shadow.txt");
- if (!inShadow.is_open())
- cout<<"Unable to Open shadow.txt"<<endl;
- while (getline(inShadow,line))
- {
- stringstream ss;
- string str1;
- ss.str(line);
- for (int i=1; i<=3; i++)
- {
- getline(ss,str1,':');
- if (i == 1)
- user = str1;
- if (i == 3) //3rd column of string
- {
- if (user == username)
- userlvl = atoi(str1.c_str()); //Set File Level
- }
- }
- }
- //Close inShadow
- inShadow.close();
- return userlvl;
- }
- int getFileLevel(string file)
- {
- int filelvl;
- string line, filename;
- ifstream inFiles("Files.store");
- if (!inFiles.is_open())
- cout<<"Unable to Open File Records"<<endl;
- while (getline(inFiles,line))
- {
- stringstream ss;
- string str1;
- ss.str(line);
- for (int i=1; i<=2; i++)
- {
- getline(ss,str1,':');
- if (i == 1)
- filename = str1;
- if (i == 2) //2nd column of string
- {
- if (filename == file)
- filelvl = atoi(str1.c_str()); //Set File Level
- }
- }
- }
- //Close inFiles
- inFiles.close();
- return filelvl;
- }
- void readFile(string file, int userlvl)
- {
- int filelvl = getFileLevel(file);
- if (userlvl == filelvl || userlvl > filelvl)
- cout<<"Read Access Granted for "<<file<<endl;
- else
- cout<<"Read Access Denied for "<<file<<endl;
- cout<<"\n\n\nLoading........."<<endl;
- }
- void writeFile(string file, int userlvl)
- {
- int filelvl = getFileLevel(file);
- if (userlvl == filelvl)
- cout<<"Write Access Granted for "<<file<<endl;
- else
- cout<<"Write Access Denied for "<<file<<endl;
- cout<<"\n\n\nLoading........."<<endl;
- }
- void listFile()
- {
- string line;
- ifstream inFiles("Files.store");
- if (!inFiles.is_open())
- cout<<"Unable to Open File Records"<<endl;
- cout<<"File System Records\n-------------------"<<endl;
- while (getline(inFiles,line))
- {
- stringstream ss;
- string str1;
- ss.str(line);
- for (int i=1; i<=2; i++)
- {
- getline(ss,str1,':');
- if (i == 1)
- cout<<str1<<endl;
- }
- }
- //Close inFiles
- inFiles.close();
- }
- void saveFile()
- {
- cout<<"File Save Successful."<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement