pass.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. #define _BSD_SOURCE /* for unistd.h */
  2. #ifdef _WIN32
  3. # include <Windows.h>
  4. # include <curses.h>
  5. # define SLEEP(delay) Sleep(delay/1000)
  6. // Unix builds require ncurses.h for the Ncurses library.
  7. // Unix also requires unistd.h for usleep(microseconds).
  8. // usleep/1000 = Sleep
  9. #else
  10. # include <ncurses.h>
  11. # include <unistd.h>
  12. # define SLEEP(delay) usleep(delay)
  13. #endif
  14. #include <time.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include "pass.h"
  18. #include "print.h"
  19. #include "wordParse.h"
  20. #include "intro.h"
  21. #define OFFSET_LEFT 0
  22. #define OFFSET_RIGHT 20
  23. #define BIGSTRING_SIZE 408
  24. static int currentCharContains(char arr[],char c){
  25. int i;
  26. for(i=0; i<12; i++)
  27. if(arr[i]==c)
  28. return 1;
  29. return 0;
  30. }
  31. static int getCharLoc(int y, int x){
  32. /* Left side */
  33. if(x<19)
  34. return 12*(y-5)+(x-7);
  35. /* Right side */
  36. else
  37. return 12*(y-5)+(x-27+204);
  38. }
  39. void pass(){
  40. /* Start a new screen where nodelay is false */
  41. erase();
  42. endwin();
  43. initscr();
  44. noecho();
  45. refresh();
  46. attron(A_BOLD);
  47. nodelay(stdscr, 0);
  48. if(has_colors() == 1){
  49. // Colors
  50. start_color();
  51. init_pair(1,COLOR_GREEN,COLOR_BLACK);
  52. attron(COLOR_PAIR(1));
  53. }
  54. /* Intro text */
  55. char prompt[] = "ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL";
  56. passPrint(prompt,strlen(prompt),0);
  57. char prompt2[] = "ENTER PASSWORD NOW";
  58. passPrint(prompt2, strlen(prompt2), 1);
  59. char prompt3[] = "4 ATTEMPT(S) LEFT: * * * *";
  60. passPrint(prompt3, strlen(prompt3), 3);
  61. /* Generate the hex values on the left sides */
  62. int arbHex;
  63. arbHex = (rand() % 200) + 63744;
  64. /* Generate the string to hold the bracket tricks and words */
  65. char bigString [BIGSTRING_SIZE];
  66. char randValues[] = "!@#$%^*()_-=+\\|/[]{}?\"\':;,.<>";
  67. int i;
  68. for(i=0; i<BIGSTRING_SIZE; i++){
  69. /* Fill bigString with random values */
  70. bigString[i] = randValues[rand()%29];
  71. }
  72. char ** wordArr = getWordArr();
  73. int WORD_POOL_SIZE = getNumWords();
  74. int WORD_SIZE = getWordLength();
  75. int WORDS_CHOSEN = getWordsToChoose();
  76. /* Place a word in the string total times, making sure it doesn't
  77. overwrite another word or get placed right next to it */
  78. int place; /* Current place for checking and word insertion*/
  79. int takenWords[WORDS_CHOSEN]; /* Words already placed in bigString */
  80. for(int i=0; i<WORDS_CHOSEN; i++)
  81. takenWords[i] = 0;
  82. int valid; /* 1 if selected word is not already used and
  83. does not conflict with other words, 0 otherwise */
  84. int pickedWord = 0; /* Indicate whether or not we've chosen the correct word */
  85. int left = WORDS_CHOSEN; /* # of words that still need to be chosen */
  86. char correctWord[WORD_SIZE]; /* the correct word */
  87. while(left>0){
  88. valid = 1;
  89. /* Choose a random place in bigString */
  90. place = rand()%(BIGSTRING_SIZE-WORD_SIZE);
  91. /* Check of any characters there or around it are A-Z */
  92. for(i=place-1; i<place+WORD_SIZE+1; i++){
  93. if(bigString[i] > 64 && bigString[i] < 91){
  94. valid = 0;
  95. break;
  96. }
  97. }
  98. if(valid){
  99. int wordLoc = rand()%WORD_POOL_SIZE;
  100. if(takenWords[wordLoc])
  101. valid=0;
  102. if(valid){
  103. takenWords[wordLoc] = 1;
  104. /* Add the word to bigString */
  105. for(i=place; i<place+WORD_SIZE; i++){
  106. bigString[i] = *(*(wordArr+wordLoc)+(i-place));
  107. /* If this is the first word chosen, it is the correct word. */
  108. if(!pickedWord)
  109. correctWord[i-place] = *(*(wordArr+wordLoc)+(i-place));
  110. }
  111. pickedWord = 1;
  112. left--;
  113. }
  114. }
  115. }
  116. /* Create and fill an array to keep track of which brackets were used */
  117. int usedBrackets[BIGSTRING_SIZE];
  118. for(i=0; i<BIGSTRING_SIZE; i++){
  119. usedBrackets[i] = 1;
  120. }
  121. /* Print the hex and the filled bigString */
  122. char temp[12];
  123. int current = 0;
  124. for(i=5; i<22; i++){
  125. /* Print left side */
  126. for(int j=0; j<12; j++){
  127. temp[j] = bigString[j+current];
  128. }
  129. printChoices(arbHex,temp,i, OFFSET_LEFT);
  130. current = current + 12;
  131. arbHex = arbHex + 12;
  132. }
  133. for(i=5; i<22; i++){
  134. /* Print right side */
  135. for(int j=0; j<12; j++){
  136. temp[j] = bigString[j+current];
  137. }
  138. printChoices(arbHex,temp,i, OFFSET_RIGHT);
  139. current = current + 12;
  140. arbHex = arbHex + 12;
  141. }
  142. mvprintw(21,40,"%c",'>');
  143. move(5,7);
  144. char currentChar[12]; /* Max length currentChar could be (total possible length of a bracket trick) */
  145. currentChar[0] = (char)mvinch(5,7);
  146. // TODO Clear any key presses that may have occurred during this loading sequence
  147. int y,x,origy,origx,starty,startx; /* values that keep track of current yx locations, and original ones */
  148. int wordLength; /* How long a word is */
  149. int charStart; /* where character counting starts for brackets */
  150. char keyPress; /* key pressed by user */
  151. int charCounter; /* counts currentChar - used for incrementing currentChar to print or change it */
  152. int bracketLength; /* length of a bracket trick */
  153. char endBracket; /* the end bracket that corresponds to currentChar[0]; */
  154. int bracketTricks=0; /* Total number of bracket tricks used */
  155. int needsClearing = 0; /* Whether or not highlights need to be purged */
  156. int needsClearingMultiLine = 0; /* Whether or not a multi line highlight needs to be purged */
  157. char output[12]; /* Used for side terminal output */
  158. int allowances = 4;
  159. while(1){
  160. getyx(stdscr,y,x);
  161. /* Get allowances left */
  162. mvprintw(1,0," ");
  163. mvprintw(3,0," ");
  164. switch(allowances){
  165. case 1: mvprintw(3,0,"1 ATTEMPT(S) LEFT: *");
  166. attron(A_BLINK);
  167. mvprintw(1,0,"!!! WARNING: LOCKOUT IMNINENT !!!");
  168. attroff(A_BLINK);
  169. attron(A_BOLD);
  170. break;
  171. case 2: mvprintw(3,0,"2 ATTEMPT(S) LEFT: * *");
  172. mvprintw(1,0,"ENTER PASSWORD NOW");
  173. break;
  174. case 3: mvprintw(3,0,"3 ATTEMPT(S) LEFT: * * *");
  175. mvprintw(1,0,"ENTER PASSWORD NOW");
  176. break;
  177. case 4: mvprintw(3,0,"4 ATTEMPT(S) LEFT: * * * *");
  178. mvprintw(1,0,"ENTER PASSWORD NOW");
  179. break;
  180. case 0: clear();
  181. mvprintw(10,20,"TERMINAL LOCKED");
  182. mvprintw(12,12,"PLEASE CONTACT AN ADMINISTRATOR");
  183. refresh();
  184. getch();
  185. exit(0);
  186. }
  187. refresh();
  188. move(y,x);
  189. /* Check if highlights need to be purged */
  190. if(needsClearing){
  191. charCounter = 0;
  192. while(charCounter!=bracketLength+1){
  193. currentChar[charCounter] = (char)mvinch(origy,charStart+charCounter);
  194. mvprintw(origy,charStart+charCounter,"%c",(int)currentChar[charCounter]);
  195. charCounter++;
  196. }
  197. mvprintw(21,41," ",currentChar[0]);
  198. needsClearing = 0;
  199. move(y,origx);
  200. }
  201. if(needsClearingMultiLine){
  202. charCounter = 0;
  203. while(charCounter!=wordLength){
  204. currentChar[charCounter] = (char)mvinch(starty,startx);
  205. mvprintw(starty,startx,"%c",currentChar[charCounter]);
  206. charCounter++;
  207. startx++;
  208. if(startx==19 || startx==39){
  209. startx-=12;
  210. starty++;
  211. if(starty == 22) {
  212. starty = 5;
  213. startx+=20;
  214. }
  215. }
  216. }
  217. mvprintw(21,41," ",currentChar[0]);
  218. needsClearingMultiLine = 0;
  219. move(y,x);
  220. }
  221. /* Clear the char array */
  222. for(i=0;i<12;i++)
  223. currentChar[i]=' ';
  224. currentChar[0] = (char) (char)mvinch(y,x);
  225. /* Set the new y and x to origy and origx */
  226. origy = y;
  227. origx = x;
  228. /* Check for bracket tricks */
  229. if((currentChar[0]=='(' || currentChar[0]=='<' || currentChar[0]=='[' || currentChar[0]=='{') && usedBrackets[getCharLoc(y,x)] && bracketTricks<WORDS_CHOSEN){
  230. charStart = x;
  231. bracketLength=0;
  232. while(x!=18 && x!=38){
  233. x++;
  234. endBracket = (char)mvinch(y,x);
  235. bracketLength++;
  236. if((endBracket == ')' && currentChar[0]=='(') ||
  237. (endBracket == '>' && currentChar[0]=='<') ||
  238. (endBracket == ']' && currentChar[0]=='[') ||
  239. (endBracket == '}' && currentChar[0]=='{')){
  240. /* Reprint the bracket trick with highlight */
  241. attron(A_STANDOUT);
  242. charCounter = 0;
  243. while(1){
  244. currentChar[charCounter] = (char)mvinch(y,charStart+charCounter);
  245. mvprintw(y,charStart+charCounter,"%c",currentChar[charCounter]);
  246. if(currentChar[charCounter] == endBracket)
  247. break;
  248. charCounter++;
  249. }
  250. attroff(A_STANDOUT);
  251. /* Print the bracket trick to output */
  252. attron(A_BOLD);
  253. for(i=0;i<=charCounter;i++)
  254. mvprintw(21,41+i,"%c",(int)currentChar[i]);
  255. /* Notify that highlighting will need to be cleared next move */
  256. needsClearing = 1;
  257. }
  258. }
  259. if(!((endBracket == ')' && currentChar[0]=='(') ||
  260. (endBracket == '>' && currentChar[0]=='<') ||
  261. (endBracket == ']' && currentChar[0]=='[') ||
  262. (endBracket == '}' && currentChar[0]=='{'))){
  263. mvprintw(21,41,"%c",currentChar[0]);
  264. }
  265. }
  266. /* Check for letters */
  267. else if(currentChar[0]>64 && currentChar[0]<91){
  268. /* Check for letter behind the current location */
  269. int tempx = x;
  270. int tempy = y;
  271. while(bigString[getCharLoc(tempy,tempx)-1]>64 && bigString[getCharLoc(tempy,tempx)-1]<91){
  272. currentChar[0] = bigString[getCharLoc(tempy,tempx)];
  273. tempx--;
  274. if(tempx==6 || tempx==26){
  275. tempx+=12;
  276. tempy--;
  277. if(tempy == 4){
  278. tempy = 21;
  279. tempx-=20;
  280. }
  281. }
  282. }
  283. startx = tempx;
  284. starty = tempy; /* We'll need the location of the first char for cleaning */
  285. /* And start there */
  286. charCounter = 0;
  287. while(bigString[getCharLoc(tempy,tempx)+1]>64 && bigString[getCharLoc(tempy,tempx)+1]<91){
  288. currentChar[charCounter] = bigString[getCharLoc(tempy,tempx)];
  289. charCounter++;
  290. tempx++;
  291. if(tempx==19 || tempx==39){
  292. tempx-=12;
  293. tempy++;
  294. if(tempy == 22) {
  295. tempy = 5;
  296. tempx+=20;
  297. }
  298. }
  299. }
  300. /* Now currentChar is the String, and charCounter+1 is the length */
  301. wordLength = charCounter+1;
  302. /* Reprint the word with highlight */
  303. tempx = startx;
  304. tempy = starty;
  305. attron(A_STANDOUT);
  306. charCounter = 0;
  307. while(charCounter!=wordLength){
  308. currentChar[charCounter] = (char)mvinch(tempy,tempx);
  309. mvprintw(tempy,tempx,"%c",currentChar[charCounter]);
  310. charCounter++;
  311. tempx++;
  312. if(tempx==19 || tempx==39){
  313. tempx-=12;
  314. tempy++;
  315. if(tempy == 22) {
  316. tempy = 5;
  317. tempx+=20;
  318. }
  319. }
  320. }
  321. attroff(A_STANDOUT);
  322. /* Print the word to output */
  323. attron(A_BOLD);
  324. for(i=0;i<charCounter;i++)
  325. mvprintw(21,41+i,"%c",(int)currentChar[i]);
  326. /* Notify that highlighting will need to be cleared next move */
  327. needsClearingMultiLine = 1;
  328. }
  329. /* Nothing was found, print current char */
  330. else
  331. mvprintw(21,41,"%c",currentChar[0]);
  332. move(origy,origx);
  333. refresh();
  334. keyPress = getch();
  335. getyx(stdscr,y,x);
  336. if(keyPress=='w'){
  337. if(y>5)
  338. move(y-1,x);
  339. }
  340. if(keyPress=='s'){
  341. if(y<21)
  342. move(y+1,x);
  343. }
  344. if(keyPress=='a'){
  345. if(x>7){
  346. if(x==27)
  347. move(y,18);
  348. else
  349. move(y,x-1);
  350. }
  351. }
  352. if(keyPress=='d'){
  353. if(x<38){
  354. if(x==18)
  355. move(y,27);
  356. else
  357. move(y,x+1);
  358. }
  359. }
  360. if(keyPress==3) /* Ctrl-C */
  361. exit(0);
  362. if(keyPress=='\n'){ /* Enter */
  363. mvprintw(17,40," ");
  364. mvprintw(18,40," ");
  365. mvprintw(19,40," ");
  366. /* If the char is a left bracket */
  367. if(((currentChar[0]=='(') && currentCharContains(currentChar,')')) ||
  368. (currentChar[0]=='<' && currentCharContains(currentChar,'>')) ||
  369. (currentChar[0]=='[' && currentCharContains(currentChar,']')) ||
  370. (currentChar[0]=='{' && currentCharContains(currentChar,'}'))){
  371. /* Set the selected bracket as used */
  372. usedBrackets[getCharLoc(y,x)] = 0;
  373. /* Increment total bracket tricks used */
  374. bracketTricks++;
  375. if(rand()%5==0){
  376. /* 20% chance of allowance replenish */
  377. sprintf(output,"Allowance ");
  378. mvprintw(18,40,">");
  379. for(i=0;i<12;i++){
  380. mvprintw(18,41+i,"%c",output[i]);
  381. }
  382. sprintf(output,"replenished.");
  383. mvprintw(19,40,">");
  384. for(i=0;i<12;i++){
  385. mvprintw(19,41+i,"%c",output[i]);
  386. }
  387. allowances = 4;
  388. }
  389. else{
  390. /* Remove a dud */
  391. int tempx,tempy;
  392. pickagain:do{
  393. if(rand()%2==0)
  394. tempx = (rand()%12)+7;
  395. else
  396. tempx = (rand()%12)+27;
  397. tempy = (rand()%17)+5;
  398. } while(!(bigString[getCharLoc(tempy,tempx)]>64 && bigString[getCharLoc(tempy,tempx)]<91));
  399. while(bigString[getCharLoc(tempy,tempx)-1]>64 && bigString[getCharLoc(tempy,tempx)-1]<91){
  400. tempx--;
  401. if(tempx==6 || tempx==26){
  402. tempx+=12;
  403. tempy--;
  404. }
  405. }
  406. startx = tempx;
  407. starty = tempy;
  408. charCounter = 0;
  409. while(bigString[getCharLoc(tempy,tempx)+1]>64 && bigString[getCharLoc(tempy,tempx)+1]<91){
  410. currentChar[charCounter] = bigString[getCharLoc(tempy,tempx)];
  411. charCounter++;
  412. tempx++;
  413. if(tempx==19 || tempx==39){
  414. tempx-=12;
  415. tempy++;
  416. }
  417. }
  418. /* Check if currentChar = correctWord */
  419. int allCorrect=1;
  420. for(i=0;i<WORD_SIZE;i++){
  421. if(currentChar[i]!=correctWord[i])
  422. allCorrect = 0;
  423. }
  424. if(allCorrect)
  425. goto pickagain;
  426. tempx = startx;
  427. tempy = starty;
  428. while(bigString[getCharLoc(tempy,tempx)]>64 && bigString[getCharLoc(tempy,tempx)]<91){
  429. mvprintw(tempy,tempx,"%c",'.');
  430. bigString[getCharLoc(tempy,tempx)] = '.';
  431. tempx++;
  432. if(tempx==19 || tempx==39){
  433. tempx-=12;
  434. tempy++;
  435. }
  436. }
  437. sprintf(output,"Dud removed.");
  438. mvprintw(19,40,">");
  439. for(i=0;i<12;i++){
  440. mvprintw(19,41+i,"%c",output[i]);
  441. }
  442. }
  443. }
  444. /* Else compare it to the correct word */
  445. else{
  446. int rightLetters = WORD_SIZE;
  447. for(i=0;i<WORD_SIZE; i++){
  448. if(currentChar[i]!=correctWord[i])
  449. rightLetters--;
  450. }
  451. if(rightLetters==WORD_SIZE){
  452. mvprintw(15,40,">");
  453. for(i=0;i<12;i++){
  454. mvprintw(15,41+i,"%c",currentChar[i]);
  455. }
  456. sprintf(output,"Exact match!");
  457. mvprintw(16,40,">");
  458. for(i=0;i<12;i++){
  459. mvprintw(16,41+i,"%c",output[i]);
  460. }
  461. sprintf(output,"Please wait ");
  462. mvprintw(17,40,">");
  463. for(i=0;i<12;i++){
  464. mvprintw(17,41+i,"%c",output[i]);
  465. }
  466. sprintf(output,"while system");
  467. mvprintw(18,40,">");
  468. for(i=0;i<12;i++){
  469. mvprintw(18,41+i,"%c",output[i]);
  470. }
  471. sprintf(output,"is accessed.");
  472. mvprintw(19,40,">");
  473. for(i=0;i<12;i++){
  474. mvprintw(19,41+i,"%c",output[i]);
  475. }
  476. refresh();
  477. SLEEP(3000000);
  478. endwin();
  479. exit(0);
  480. }
  481. else{
  482. mvprintw(17,40,">");
  483. for(i=0;i<12;i++){
  484. mvprintw(17,41+i,"%c",currentChar[i]);
  485. }
  486. sprintf(output,"Entry denied");
  487. mvprintw(18,40,">");
  488. for(i=0;i<12;i++){
  489. mvprintw(18,41+i,"%c",output[i]);
  490. }
  491. sprintf(output,"%d/%d correct.",rightLetters,WORD_SIZE);
  492. mvprintw(19,40,">");
  493. for(i=0;i<12;i++){
  494. mvprintw(19,41+i,"%c",output[i]);
  495. }
  496. allowances--;
  497. }
  498. }
  499. move(y,x);
  500. }
  501. refresh();
  502. }
  503. endwin();
  504. exit(0);
  505. }