pass.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. int valid; /* 1 if selected word is not already used and
  81. does not conflict with other words, 0 otherwise */
  82. int pickedWord = 0; /* Indicate whether or not we've chosen the correct word */
  83. int left = WORDS_CHOSEN; /* # of words that still need to be chosen */
  84. char correctWord[WORD_SIZE]; /* the correct word */
  85. while(left>0){
  86. valid = 1;
  87. /* Choose a random place in bigString */
  88. place = rand()%(BIGSTRING_SIZE-WORD_SIZE);
  89. /* Check of any characters there or around it are A-Z */
  90. for(i=place-1; i<place+WORD_SIZE+1; i++){
  91. if(bigString[i] > 64 && bigString[i] < 91){
  92. valid = 0;
  93. }
  94. }
  95. if(valid){
  96. int wordLoc = rand()%WORD_POOL_SIZE;
  97. /* Check if the word selected has already been selected */
  98. for(i=0;i<=WORDS_CHOSEN-left;i++)
  99. {
  100. if(wordLoc == takenWords[i])
  101. valid = 0;
  102. }
  103. if(valid){
  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. }
  212. }
  213. mvprintw(21,41," ",currentChar[0]);
  214. needsClearingMultiLine = 0;
  215. move(y,x);
  216. }
  217. /* Clear the char array */
  218. for(i=0;i<12;i++)
  219. currentChar[i]=' ';
  220. currentChar[0] = (char) (char)mvinch(y,x);
  221. /* Set the new y and x to origy and origx */
  222. origy = y;
  223. origx = x;
  224. /* Check for bracket tricks */
  225. if((currentChar[0]=='(' || currentChar[0]=='<' || currentChar[0]=='[' || currentChar[0]=='{') && usedBrackets[getCharLoc(y,x)] && bracketTricks<WORDS_CHOSEN){
  226. charStart = x;
  227. bracketLength=0;
  228. while(x!=18 && x!=38){
  229. x++;
  230. endBracket = (char)mvinch(y,x);
  231. bracketLength++;
  232. if((endBracket == ')' && currentChar[0]=='(') ||
  233. (endBracket == '>' && currentChar[0]=='<') ||
  234. (endBracket == ']' && currentChar[0]=='[') ||
  235. (endBracket == '}' && currentChar[0]=='{')){
  236. /* Reprint the bracket trick with highlight */
  237. attron(A_STANDOUT);
  238. charCounter = 0;
  239. while(1){
  240. currentChar[charCounter] = (char)mvinch(y,charStart+charCounter);
  241. mvprintw(y,charStart+charCounter,"%c",currentChar[charCounter]);
  242. if(currentChar[charCounter] == endBracket)
  243. break;
  244. charCounter++;
  245. }
  246. attroff(A_STANDOUT);
  247. /* Print the bracket trick to output */
  248. attron(A_BOLD);
  249. for(i=0;i<=charCounter;i++)
  250. mvprintw(21,41+i,"%c",(int)currentChar[i]);
  251. /* Notify that highlighting will need to be cleared next move */
  252. needsClearing = 1;
  253. }
  254. }
  255. if(!((endBracket == ')' && currentChar[0]=='(') ||
  256. (endBracket == '>' && currentChar[0]=='<') ||
  257. (endBracket == ']' && currentChar[0]=='[') ||
  258. (endBracket == '}' && currentChar[0]=='{'))){
  259. mvprintw(21,41,"%c",currentChar[0]);
  260. }
  261. }
  262. /* Check for letters */
  263. else if(currentChar[0]>64 && currentChar[0]<91){
  264. /* Check for letter behind the current location */
  265. int tempx = x;
  266. int tempy = y;
  267. while(bigString[getCharLoc(tempy,tempx)-1]>64 && bigString[getCharLoc(tempy,tempx)-1]<91){
  268. currentChar[0] = bigString[getCharLoc(tempy,tempx)];
  269. tempx--;
  270. if(tempx==6 || tempx==26){
  271. tempx+=12;
  272. tempy--;
  273. }
  274. }
  275. startx = tempx;
  276. starty = tempy; /* We'll need the location of the first char for cleaning */
  277. /* And start there */
  278. charCounter = 0;
  279. while(bigString[getCharLoc(tempy,tempx)+1]>64 && bigString[getCharLoc(tempy,tempx)+1]<91){
  280. currentChar[charCounter] = bigString[getCharLoc(tempy,tempx)];
  281. charCounter++;
  282. tempx++;
  283. if(tempx==19 || tempx==39){
  284. tempx-=12;
  285. tempy++;
  286. }
  287. }
  288. /* Now currentChar is the String, and charCounter+1 is the length */
  289. wordLength = charCounter+1;
  290. /* Reprint the word with highlight */
  291. tempx = startx;
  292. tempy = starty;
  293. attron(A_STANDOUT);
  294. charCounter = 0;
  295. while(charCounter!=wordLength){
  296. currentChar[charCounter] = (char)mvinch(tempy,tempx);
  297. mvprintw(tempy,tempx,"%c",currentChar[charCounter]);
  298. charCounter++;
  299. tempx++;
  300. if(tempx==19 || tempx==39){
  301. tempx-=12;
  302. tempy++;
  303. }
  304. }
  305. attroff(A_STANDOUT);
  306. /* Print the word to output */
  307. attron(A_BOLD);
  308. for(i=0;i<charCounter;i++)
  309. mvprintw(21,41+i,"%c",(int)currentChar[i]);
  310. /* Notify that highlighting will need to be cleared next move */
  311. needsClearingMultiLine = 1;
  312. }
  313. /* Nothing was found, print current char */
  314. else
  315. mvprintw(21,41,"%c",currentChar[0]);
  316. move(origy,origx);
  317. refresh();
  318. keyPress = getch();
  319. getyx(stdscr,y,x);
  320. if(keyPress=='w'){
  321. if(y>5)
  322. move(y-1,x);
  323. }
  324. if(keyPress=='s'){
  325. if(y<21)
  326. move(y+1,x);
  327. }
  328. if(keyPress=='a'){
  329. if(x>7){
  330. if(x==27)
  331. move(y,18);
  332. else
  333. move(y,x-1);
  334. }
  335. }
  336. if(keyPress=='d'){
  337. if(x<38){
  338. if(x==18)
  339. move(y,27);
  340. else
  341. move(y,x+1);
  342. }
  343. }
  344. if(keyPress==3) /* Ctrl-C */
  345. exit(0);
  346. if(keyPress=='\n'){ /* Enter */
  347. mvprintw(17,40," ");
  348. mvprintw(18,40," ");
  349. mvprintw(19,40," ");
  350. /* If the char is a left bracket */
  351. if(((currentChar[0]=='(') && currentCharContains(currentChar,')')) ||
  352. (currentChar[0]=='<' && currentCharContains(currentChar,'>')) ||
  353. (currentChar[0]=='[' && currentCharContains(currentChar,']')) ||
  354. (currentChar[0]=='{' && currentCharContains(currentChar,'}'))){
  355. /* Set the selected bracket as used */
  356. usedBrackets[getCharLoc(y,x)] = 0;
  357. /* Increment total bracket tricks used */
  358. bracketTricks++;
  359. if(rand()%5==0){
  360. /* 20% chance of allowance replenish */
  361. sprintf(output,"Allowance ");
  362. mvprintw(18,40,">");
  363. for(i=0;i<12;i++){
  364. mvprintw(18,41+i,"%c",output[i]);
  365. }
  366. sprintf(output,"replenished.");
  367. mvprintw(19,40,">");
  368. for(i=0;i<12;i++){
  369. mvprintw(19,41+i,"%c",output[i]);
  370. }
  371. allowances = 4;
  372. }
  373. else{
  374. /* Remove a dud */
  375. int tempx,tempy;
  376. pickagain:do{
  377. if(rand()%2==0)
  378. tempx = (rand()%12)+7;
  379. else
  380. tempx = (rand()%12)+27;
  381. tempy = (rand()%17)+5;
  382. } while(!(bigString[getCharLoc(tempy,tempx)]>64 && bigString[getCharLoc(tempy,tempx)]<91));
  383. while(bigString[getCharLoc(tempy,tempx)-1]>64 && bigString[getCharLoc(tempy,tempx)-1]<91){
  384. tempx--;
  385. if(tempx==6 || tempx==26){
  386. tempx+=12;
  387. tempy--;
  388. }
  389. }
  390. startx = tempx;
  391. starty = tempy;
  392. charCounter = 0;
  393. while(bigString[getCharLoc(tempy,tempx)+1]>64 && bigString[getCharLoc(tempy,tempx)+1]<91){
  394. currentChar[charCounter] = bigString[getCharLoc(tempy,tempx)];
  395. charCounter++;
  396. tempx++;
  397. if(tempx==19 || tempx==39){
  398. tempx-=12;
  399. tempy++;
  400. }
  401. }
  402. /* Check if currentChar = correctWord */
  403. int allCorrect=1;
  404. for(i=0;i<WORD_SIZE;i++){
  405. if(currentChar[i]!=correctWord[i])
  406. allCorrect = 0;
  407. }
  408. if(allCorrect)
  409. goto pickagain;
  410. tempx = startx;
  411. tempy = starty;
  412. while(bigString[getCharLoc(tempy,tempx)]>64 && bigString[getCharLoc(tempy,tempx)]<91){
  413. mvprintw(tempy,tempx,"%c",'.');
  414. bigString[getCharLoc(tempy,tempx)] = '.';
  415. tempx++;
  416. if(tempx==19 || tempx==39){
  417. tempx-=12;
  418. tempy++;
  419. }
  420. }
  421. sprintf(output,"Dud removed.");
  422. mvprintw(19,40,">");
  423. for(i=0;i<12;i++){
  424. mvprintw(19,41+i,"%c",output[i]);
  425. }
  426. }
  427. }
  428. /* Else compare it to the correct word */
  429. else{
  430. int rightLetters = WORD_SIZE;
  431. for(i=0;i<WORD_SIZE; i++){
  432. if(currentChar[i]!=correctWord[i])
  433. rightLetters--;
  434. }
  435. if(rightLetters==WORD_SIZE){
  436. mvprintw(15,40,">");
  437. for(i=0;i<12;i++){
  438. mvprintw(15,41+i,"%c",currentChar[i]);
  439. }
  440. sprintf(output,"Exact match!");
  441. mvprintw(16,40,">");
  442. for(i=0;i<12;i++){
  443. mvprintw(16,41+i,"%c",output[i]);
  444. }
  445. sprintf(output,"Please wait ");
  446. mvprintw(17,40,">");
  447. for(i=0;i<12;i++){
  448. mvprintw(17,41+i,"%c",output[i]);
  449. }
  450. sprintf(output,"while system");
  451. mvprintw(18,40,">");
  452. for(i=0;i<12;i++){
  453. mvprintw(18,41+i,"%c",output[i]);
  454. }
  455. sprintf(output,"is accessed.");
  456. mvprintw(19,40,">");
  457. for(i=0;i<12;i++){
  458. mvprintw(19,41+i,"%c",output[i]);
  459. }
  460. refresh();
  461. SLEEP(3000000);
  462. endwin();
  463. exit(0);
  464. }
  465. else{
  466. mvprintw(17,40,">");
  467. for(i=0;i<12;i++){
  468. mvprintw(17,41+i,"%c",currentChar[i]);
  469. }
  470. sprintf(output,"Entry denied");
  471. mvprintw(18,40,">");
  472. for(i=0;i<12;i++){
  473. mvprintw(18,41+i,"%c",output[i]);
  474. }
  475. sprintf(output,"%d/%d correct.",rightLetters,WORD_SIZE);
  476. mvprintw(19,40,">");
  477. for(i=0;i<12;i++){
  478. mvprintw(19,41+i,"%c",output[i]);
  479. }
  480. allowances--;
  481. }
  482. }
  483. move(y,x);
  484. }
  485. refresh();
  486. }
  487. endwin();
  488. exit(0);
  489. }