Advertisement
Guest User

Untitled

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