Advertisement
Guest User

Untitled

a guest
Aug 27th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.20 KB | None | 0 0
  1. //sudo apt-get install whois -> Use to install function for mkpasswd
  2.  
  3.  
  4. //C++ Library
  5. #include <iostream>
  6. #include <cstring>
  7. #include <string>
  8. #include <fstream>
  9. #include <sstream>
  10.  
  11. //C Library
  12. #include <unistd.h>
  13. #include <stdio.h>
  14. #include <termios.h>
  15. #include <stdlib.h>
  16. using namespace std; //using standard namespace
  17.  
  18. //Functions
  19. /**Create OR Login User Function Headers**/
  20. int getch();
  21. string getpass(const char*, bool);
  22. void createUser();
  23. bool validatePass(char[]);
  24. bool validateShadow(string, string);
  25. bool checkUser(string);
  26. bool checkPasswd(string);
  27. void addSalt(string);
  28. void addShadow(string, string, int);
  29. void addPasswd(string, string, int);
  30.  
  31. /**File System Function Headers**/
  32. void SystemMenu(string);
  33. bool checkFile(string);
  34. void createFile(string, int, int);
  35. int getUserLevel(string);
  36. int getFileLevel(string);
  37. void readFile(string, int);
  38. void writeFile(string, int);
  39. void listFile();
  40. void saveFile();
  41.  
  42. //Main Function
  43. int main (int argc, char* argv[])
  44. {
  45. //Variable
  46. string username;
  47. string password;
  48.  
  49. //Create files for those computer don't have these files
  50. system("touch salt.txt");
  51. system("touch shadow.txt");
  52. system("touch passwd.txt");
  53. system("touch Files.store");
  54.  
  55. cout<<"\tWelcome to File System"<<endl;
  56. cout<<"======================================"<<endl;
  57. if(argc == 1) //If run with ./FileSystem
  58. {
  59. cout<<"Login to File System\n======================"<<endl;
  60. enteruser:
  61. cout<<"Username : ";
  62. cin>>username;
  63. if(checkUser(username) == false && checkPasswd(username) == false) //Check if user name and password does not exist, prompt user to enter again
  64. {
  65. cout<<"User Does Not Exist. Please Try Again. "<<endl;
  66. goto enteruser;
  67. }
  68. else //Else ask for password and continue to login
  69. {
  70. cin.ignore(); //To reserve a line
  71. password=getpass("Password: ",true); //getpass("Display", if true = hide password, false = display password)
  72.  
  73. if (validateShadow(username, password) == true) //Validate username and password/login
  74. {
  75. sleep (5); //Delay
  76. SystemMenu(username);
  77. }
  78. else //If fail to match hash value
  79. {
  80. cout<<"Password Incorrect!"<<endl;
  81. cout<<"Hash Value Does Not Match. \nProgram Terminating......."<<endl;
  82. }
  83. }
  84.  
  85. }
  86. else if(strcmp(argv[1], "-i") == 0) //Run with ./FileSystem -i
  87. {
  88. cout<<"Username : ";
  89. cin>>username;
  90. if(checkUser(username) == false)
  91. {
  92. cout<<"User Does Not Exist. Proceed to Create User Account. "<<endl;
  93. createUser();
  94. }
  95. else
  96. cout<<"User already Exist inside System. \nProgram Terminating......."<<endl;
  97.  
  98. }
  99. }
  100.  
  101. //Create User
  102. void createUser()
  103. {
  104. //Variable
  105. string username;
  106. string pwd1;
  107. string pwd2;
  108. char *passwd;
  109.  
  110. cout<<"\tRegister"<<endl;
  111. cout<<"======================================"<<endl;
  112. cout<<"Username : ";
  113. cin>>username;
  114. cin.ignore(); //use to reserve for a next string input
  115. tryagain:
  116.  
  117. pwd1=getpass("Please enter the password: ",true); //Hide input password by using getpass() function
  118.  
  119. //convert string to char
  120. passwd = new char [pwd1.length()];
  121. strcpy(passwd, pwd1.c_str());
  122.  
  123. if (validatePass(passwd) == true)
  124. {
  125. int count=1;
  126. enter_again:
  127. if(count<4)
  128. {
  129. pwd2=getpass("Enter password again: ",true);
  130.  
  131. if(pwd1 == pwd2)
  132. {
  133. int clearance;
  134. cout<<"Successful created"<<endl;
  135. addSalt(username);
  136.  
  137. enterclearance:
  138. cout<<"User Clearance(0/1/2) : ";
  139. cin>>clearance;
  140. if(clearance < 0 ||clearance > 2)
  141. {
  142. cout<<"Invalid clearance."<<endl;
  143. goto enterclearance;
  144. }
  145. else
  146. {
  147. addShadow(username, pwd1, clearance);
  148. addPasswd(username, pwd1, clearance);
  149. }
  150. }
  151. else
  152. {
  153. cout<<"Password invalid. Please try again.("<<3-count<<")"<<endl;
  154. count++;
  155. goto enter_again;
  156. }
  157. }
  158. else
  159. {
  160. cout<<"Enter password again."<<endl;
  161. goto tryagain;
  162. }
  163.  
  164. }
  165. else
  166. {
  167. cout<<"Enter password again."<<endl;
  168. goto tryagain;
  169. }
  170. }
  171.  
  172. //Check User, salt file
  173. bool checkUser(string user)
  174. {
  175. string line;
  176. ifstream inFile("salt.txt");
  177. if (!inFile.is_open())
  178. cout<<"Unable to open file!";
  179.  
  180. while(getline(inFile, line))
  181. {
  182. stringstream ss;
  183. ss.str(line);
  184. string str1;
  185. for(int x = 0;x<2;x++)
  186. {
  187. getline(ss, str1, ':');
  188. if(x==0)
  189. {
  190. string name =str1;
  191. if(name == user)
  192. {
  193. return true;
  194. break;
  195. }
  196. }
  197. }
  198. }
  199.  
  200. return false;
  201. }
  202.  
  203. //Check Password, passwd file
  204. bool checkPasswd(string user)
  205. {
  206. string line;
  207. ifstream inFile("passwd.txt");
  208. if (!inFile.is_open())
  209. cout<<"Unable to open passwd.txt!"<<endl;
  210.  
  211. while(getline(inFile, line))
  212. {
  213. stringstream ss;
  214. ss.str(line);
  215. string str1;
  216. for(int x = 0;x<2;x++)
  217. {
  218. getline(ss, str1, ':');
  219. if(x==0)
  220. {
  221. string name =str1;
  222. if(name == user)
  223. {
  224. return true;
  225. break;
  226. }
  227. }
  228. }
  229. }
  230. return false;
  231. }
  232.  
  233. //Check for validate password
  234. bool validatePass(char pass[])
  235. {
  236. //Password policy: one symbol, an upper case, lower case, digit, min 8 character
  237. bool size = false, upper = false, alpha = false, digit =false, punct = false;
  238. if( strlen(pass) == 8) //to ensure password is 8 character
  239. {
  240. size = true;
  241. }
  242.  
  243. for (int i = 0; pass[i]; ++i)
  244. {
  245. if(isupper(pass[i])) //upper case
  246. upper = true;
  247. else if(isalpha(pass[i])) //alphabet
  248. alpha = true;
  249. else if(isdigit(pass[i])) //digit
  250. digit = true;
  251. else if(ispunct(pass[i]) ) //symbol
  252. punct = true;
  253. }
  254.  
  255. //Show error message
  256. if(size == false)
  257. {
  258. cout<<"Min 8 character."<<endl;
  259. }
  260.  
  261. if(upper == false)
  262. {
  263. cout<<"At least 1 upper case character."<<endl;
  264. }
  265.  
  266. if(alpha == false)
  267. {
  268. cout<<"At least 1 letter character."<<endl;
  269. }
  270.  
  271. if(digit == false)
  272. {
  273. cout<<"At least 1 digit number."<<endl;
  274. }
  275.  
  276. if(punct == false)
  277. {
  278. cout<<"At least 1 symbol character."<<endl;
  279. }
  280.  
  281. if(upper && alpha && digit && punct && size)
  282. return true;
  283. else
  284. return false;
  285. }
  286.  
  287. //Create shadow password
  288. bool validateShadow(string username, string pass)
  289. {
  290. char mkpass_cmd[] = "mkpasswd -m md5 "; //MD5 command
  291. string line, salt_num, name;
  292.  
  293.  
  294. //get salt password from salt.txt
  295. ifstream inFile("salt.txt");
  296. if (!inFile.is_open())
  297. cout<<"Unable to open file!";
  298.  
  299. while(getline(inFile, line))
  300. {
  301. stringstream ss;
  302. ss.str(line);
  303. string str1;
  304. for(int x = 0;x<2;x++)
  305. {
  306. getline(ss, str1, ':');
  307. if(x==0)
  308. {
  309. name =str1;
  310. }
  311. else if (x==1)
  312. {
  313. if(name == username)
  314. {
  315. salt_num = str1.c_str();
  316. }
  317. }
  318. }
  319. }
  320.  
  321. //convert salt and userpassword to Passhash
  322. system("touch temp.txt"); //create a temp file to store system output
  323. char salt[20];
  324. strcpy(salt, salt_num.c_str());
  325. //Combine mkpass_cmd with Password
  326. strcat (mkpass_cmd, pass.c_str());
  327. //Combine mkpass_cmd with -s
  328. strcat (mkpass_cmd, " -s ");
  329. //Combine mkpass_cmd with salt value
  330. strcat (mkpass_cmd, salt);
  331. strcat (mkpass_cmd, " >temp.txt");
  332. system(mkpass_cmd);
  333.  
  334. //open temp file and take out the output
  335. ifstream intemp("temp.txt");
  336. string hash;
  337. getline(intemp, hash);
  338. system("rm temp.txt"); //remove temp file
  339.  
  340. system("clear"); //clear screen
  341.  
  342. //Output to user interface
  343. cout<<username<<" Found in salt.txt and passwd.txt. "<<endl;
  344. cout<<"\nSalt Retrieved: "<<salt_num<<endl;
  345. cout<<"\nhashing......"<<endl;
  346. cout<<"\nHash Value: "<<hash<<endl;
  347. cout<<"\n\n\nLoading........."<<endl;
  348. //open shadow file
  349. string salt_hash;
  350. ifstream inShadow("shadow.txt");
  351. if (!inShadow.is_open())
  352. cout<<"Unable to open file!";
  353.  
  354. while(getline(inShadow, line))
  355. {
  356. stringstream ss;
  357. ss.str(line);
  358. string str1;
  359. for(int x = 0;x<2;x++)
  360. {
  361. getline(ss, str1, ':');
  362. if(x==0)
  363. {
  364. name =str1;
  365. }
  366. else if (x==1)
  367. {
  368. if(name == username)
  369. {
  370. salt_hash = str1.c_str();
  371. }
  372. }
  373. }
  374. }
  375.  
  376. //Compare Generated Hash Value with Hash Value inside shadow.txt
  377. if(salt_hash == hash)
  378. {
  379. return true;
  380. }
  381. else
  382. return false;
  383.  
  384. }
  385.  
  386. //Hide input character(C language), instead of using <conio.h> library
  387. int getch()
  388. {
  389. int ch;
  390. struct termios t_old, t_new;
  391.  
  392. tcgetattr(STDIN_FILENO, &t_old);
  393. t_new = t_old;
  394. t_new.c_lflag &= ~(ICANON | ECHO);
  395. tcsetattr(STDIN_FILENO, TCSANOW, &t_new);
  396.  
  397. ch = getchar();
  398.  
  399. tcsetattr(STDIN_FILENO, TCSANOW, &t_old);
  400. return ch;
  401. }
  402.  
  403. //Declaration of a function to hide input character
  404. string getpass(const char *prompt, bool show_asterisk=true)
  405. {
  406. const char BACKSPACE=127;
  407. const char RETURN=10;
  408.  
  409. string password;
  410. unsigned char ch=0;
  411.  
  412. cout <<prompt;
  413.  
  414. while((ch=getch())!=RETURN)
  415. {
  416. if(ch==BACKSPACE) //when backspace is pressed, delete password
  417. {
  418. if(password.length()!=0)
  419. {
  420. if(show_asterisk) //\b is hide it to the back
  421. cout <<"\b \b";
  422. password.resize(password.length()-1);
  423. }
  424. }
  425. else
  426. {
  427. password+=ch;
  428. if(show_asterisk)
  429. cout <<'*'; //Show * for every single character instead of real password
  430. }
  431. }
  432. cout <<endl;
  433. return password;
  434. }
  435.  
  436. //Random 8 digits function
  437. void addSalt(string username)
  438. {
  439. srand(time(NULL));
  440. int salt = rand() % 90000000 + 10000000; //random 8 digits
  441. ofstream outfile("salt.txt", ios::app); //outfile
  442. outfile<<username<<":"<<salt<<endl;
  443. outfile.close();
  444. }
  445.  
  446. //Write to shadow file
  447. void addShadow(string username, string pass, int clearance)
  448. {
  449. string line;
  450. string salt_num;
  451. string name;
  452. char mkpass_cmd[] = "mkpasswd -m md5 "; //md5 command
  453. //open salt file
  454. ifstream inFile("salt.txt");
  455. if (!inFile.is_open())
  456. cout<<"Unable to open file!";
  457.  
  458. //extract each line from text file
  459. while(getline(inFile, line))
  460. {
  461. stringstream ss;
  462. ss.str(line);
  463. string str1;
  464. for(int x = 0;x<2;x++)
  465. {
  466. getline(ss, str1, ':'); //split it out when meet :
  467. if(x==0)
  468. {
  469. name =str1;
  470. }
  471. else if (x==1)
  472. {
  473. if(name == username)
  474. {
  475. salt_num = str1.c_str(); //c_str() is a conversion function from stringstream to string
  476. }
  477. }
  478. }
  479. }
  480. system("touch temp.txt"); //create a temp file to store system output
  481. char salt[20];
  482. strcpy(salt, salt_num.c_str());
  483. //Combine mkpass_cmd with Password
  484. strcat (mkpass_cmd, pass.c_str());
  485. //Combine mkpass_cmd with -s
  486. strcat (mkpass_cmd, " -s ");
  487. //Combine mkpass_cmd with salt value
  488. strcat (mkpass_cmd, salt);
  489. strcat (mkpass_cmd, " >temp.txt");
  490. system(mkpass_cmd);
  491.  
  492. //open temp file and take out the output
  493. ifstream intemp("temp.txt");
  494. string hash;
  495. getline(intemp, hash);
  496. system("rm temp.txt"); //remove temp file
  497.  
  498.  
  499. ofstream outfile("shadow.txt", ios::app);
  500. outfile<<username<<":"<<hash<<":"<<clearance<<endl;
  501. outfile.close();
  502. }
  503.  
  504. //Add password
  505. void addPasswd(string username, string password, int clearance)
  506. {
  507. string line, name, salt, shadow;
  508.  
  509. //Open file
  510. ifstream inSalt("salt.txt");
  511. ifstream inShadow("shadow.txt");
  512. ofstream outPasswd;
  513.  
  514. if (!inShadow.is_open())
  515. cout << "Unable to open shadow.txt!";
  516.  
  517. while (getline(inShadow, line))
  518. {
  519. stringstream ss;
  520. ss.str(line);
  521. string str1;
  522. for (int i = 1; i<=2; i++)
  523. {
  524. getline(ss, str1, ':');
  525. if (i == 1)
  526. {
  527. name = str1;
  528. }
  529. else if (i == 2)
  530. {
  531. if (name == username)
  532. {
  533. shadow = str1;
  534. }
  535. }
  536. }
  537. }
  538.  
  539. outPasswd.open("passwd.txt", ios::app);
  540. outPasswd<<username<<":"<<shadow<<":"<<":"<<":"<<username<<":"<<":"<<":"<<endl;
  541. outPasswd.close();
  542.  
  543. inSalt.close();
  544. inShadow.close();
  545. outPasswd.close();
  546.  
  547.  
  548. }
  549.  
  550. /**File System Functions**/
  551. void SystemMenu(string username)
  552. {
  553. char selection;
  554. string filename, line;
  555. int filelevel;
  556.  
  557. int userlevel = getUserLevel(username);
  558. do
  559. {
  560. system("clear");
  561. menu:
  562. cout<<endl<<endl;
  563.  
  564. //Main Menu
  565. cout<<"\tFile System Main Menu\n ==============================="<<endl;
  566. cout<<"||\t(C)reate New File\t ||"<<endl;
  567. cout<<"||\t(R)ead From File\t ||"<<endl;
  568. cout<<"||\t(W)rite To File\t ||"<<endl;
  569. cout<<"||\t(L)ist All Files\t ||"<<endl;
  570. cout<<"||\t(S)ave All Records\t ||"<<endl;
  571. cout<<"||\t(E)xit File System\t ||"<<endl;
  572. cout<<" ==============================="<<endl;
  573. cout<<"Select Action (C/R/W/L/S/E): ";
  574. cin>>selection;
  575.  
  576. //Accept input upper case and lower case
  577. if (selection == 'C' || selection == 'c')
  578. {
  579. system ("clear");
  580. //Create File with File Security Level
  581. cout<<"Please Enter the Name and Classification of your File: "<<endl;
  582. cout<<"File Name: ";
  583. cin>>filename;
  584. enterlvl:
  585. cout<<"Security level(0 or 1 or 2): ";
  586. cin>>filelevel;
  587.  
  588. if (filelevel <0 || filelevel > 2)
  589. {
  590. cout<<"Invalid File Security Level. Please Enter Again."<<endl;
  591. goto enterlvl; //repeat
  592. }
  593. else if (checkFile(filename) == true)
  594. {
  595. cout<<filename<<" Already Exist in File System. Please Try Again."<<endl;
  596. goto menu; //repeat
  597. }
  598. else
  599. createFile(filename, filelevel, userlevel); //create new file
  600.  
  601. sleep(5);
  602. }
  603. if (selection == 'R' || selection == 'r')
  604. {
  605. system ("clear");
  606. //Read or Write File
  607. cout<<"Please Enter the File Name to Read "<<endl;
  608. cout<<"File Name: ";
  609. cin>>filename;
  610. if (checkFile(filename) == true)
  611. readFile(filename, userlevel);
  612. else
  613. {
  614. cout<<filename<<" Does Not Exist in File System. "<<endl;
  615. goto menu; //repeat
  616. }
  617.  
  618. sleep(5);
  619. }
  620. if (selection == 'W' || selection == 'w')
  621. {
  622. system ("clear");
  623. //Read or Write File
  624. cout<<"Please Enter the File Name to Write "<<endl;
  625. cout<<"File Name: ";
  626. cin>>filename;
  627. if (checkFile(filename) == true)
  628. writeFile(filename, userlevel);
  629. else
  630. {
  631. cout<<filename<<" Does Not Exist in File System. "<<endl;
  632. goto menu; //repeat
  633. }
  634.  
  635. sleep(5);
  636. }
  637. if (selection == 'L' || selection == 'l')
  638. {
  639. system ("clear");
  640. listFile();
  641. goto menu; //repeat
  642. }
  643. if (selection == 'S' || selection == 's')
  644. {
  645. system ("clear");
  646. saveFile();
  647. goto menu;
  648. }
  649. if (selection == 'E' || selection == 'e')
  650. {
  651. system ("clear");
  652. char exit;
  653. cout<<"Shut Down the File System? (Y/N): ";
  654. cin>>exit;
  655. if (exit == 'Y' || exit == 'y')
  656. {
  657. cout<<"Listing all Files in Record ..... Before Shut Down\n\n"<<endl;
  658. listFile();
  659. break;
  660. }
  661. else if (exit == 'N' || exit == 'n')
  662. goto menu;
  663. }
  664.  
  665. }while (selection != 'E' || selection != 'e');
  666. }
  667.  
  668. //Check all exists files
  669. bool checkFile(string file)
  670. {
  671. string line, filename;
  672. bool file_exist = false;
  673. ifstream inFiles("Files.store");
  674.  
  675. if (!inFiles.is_open())
  676. cout<<"Unable to Open File Records"<<endl;
  677.  
  678. while (getline(inFiles,line))
  679. {
  680. stringstream ss;
  681. string str1;
  682. ss.str(line);
  683. for (int i=1; i<=2; i++)
  684. {
  685. getline(ss,str1,':');
  686. if (i == 1) //1st column of string
  687. filename = str1; //Set File Name
  688.  
  689. if (file == filename)//If file exist inside Files.store
  690. file_exist = true;
  691. }
  692. }
  693. //Close inFiles
  694. inFiles.close();
  695. return file_exist;
  696. }
  697.  
  698. //Create file
  699. void createFile(string file, int filelvl, int userlvl)
  700. {
  701. string line, filename;
  702.  
  703. ofstream outFiles;
  704.  
  705. if (userlvl >= filelvl)
  706. {
  707. outFiles.open("Files.store", ios::app);
  708. outFiles<<file<<":"<<filelvl<<endl;
  709. outFiles.close();
  710. cout<<file<<" have been Successfully Created with Security Level "<<filelvl<<". "<<endl;
  711. }
  712. else
  713. cout<<file<<" Create Denied due to Current User Level. "<<endl;
  714.  
  715. cout<<"\n\n\nLoading........."<<endl;
  716. }
  717.  
  718. int getUserLevel(string username)
  719. {
  720. int userlvl;
  721. string line, user;
  722. ifstream inShadow("shadow.txt");
  723.  
  724. if (!inShadow.is_open())
  725. cout<<"Unable to Open shadow.txt"<<endl;
  726.  
  727. while (getline(inShadow,line))
  728. {
  729. stringstream ss;
  730. string str1;
  731. ss.str(line);
  732. for (int i=1; i<=3; i++)
  733. {
  734. getline(ss,str1,':');
  735. if (i == 1)
  736. user = str1;
  737. if (i == 3) //3rd column of string
  738. {
  739. if (user == username)
  740. userlvl = atoi(str1.c_str()); //Set File Level
  741. }
  742. }
  743. }
  744. //Close inShadow
  745. inShadow.close();
  746. return userlvl;
  747. }
  748.  
  749. int getFileLevel(string file)
  750. {
  751. int filelvl;
  752. string line, filename;
  753. ifstream inFiles("Files.store");
  754.  
  755. if (!inFiles.is_open())
  756. cout<<"Unable to Open File Records"<<endl;
  757.  
  758. while (getline(inFiles,line))
  759. {
  760. stringstream ss;
  761. string str1;
  762. ss.str(line);
  763. for (int i=1; i<=2; i++)
  764. {
  765. getline(ss,str1,':');
  766. if (i == 1)
  767. filename = str1;
  768. if (i == 2) //2nd column of string
  769. {
  770. if (filename == file)
  771. filelvl = atoi(str1.c_str()); //Set File Level
  772. }
  773. }
  774. }
  775. //Close inFiles
  776. inFiles.close();
  777. return filelvl;
  778. }
  779.  
  780. void readFile(string file, int userlvl)
  781. {
  782. int filelvl = getFileLevel(file);
  783.  
  784. if (userlvl == filelvl || userlvl > filelvl)
  785. cout<<"Read Access Granted for "<<file<<endl;
  786. else
  787. cout<<"Read Access Denied for "<<file<<endl;
  788.  
  789. cout<<"\n\n\nLoading........."<<endl;
  790. }
  791.  
  792. void writeFile(string file, int userlvl)
  793. {
  794. int filelvl = getFileLevel(file);
  795.  
  796. if (userlvl == filelvl)
  797. cout<<"Write Access Granted for "<<file<<endl;
  798. else
  799. cout<<"Write Access Denied for "<<file<<endl;
  800.  
  801. cout<<"\n\n\nLoading........."<<endl;
  802. }
  803.  
  804. void listFile()
  805. {
  806. string line;
  807. ifstream inFiles("Files.store");
  808.  
  809. if (!inFiles.is_open())
  810. cout<<"Unable to Open File Records"<<endl;
  811.  
  812. cout<<"File System Records\n-------------------"<<endl;
  813. while (getline(inFiles,line))
  814. {
  815. stringstream ss;
  816. string str1;
  817. ss.str(line);
  818. for (int i=1; i<=2; i++)
  819. {
  820. getline(ss,str1,':');
  821. if (i == 1)
  822. cout<<str1<<endl;
  823. }
  824. }
  825. //Close inFiles
  826. inFiles.close();
  827. }
  828.  
  829. void saveFile()
  830. {
  831. cout<<"File Save Successful."<<endl;
  832. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement