Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Corral, Jesus J
- CSE7359 Lab 2 - BOF
- 02/26/2010
- */
- #include <stdio.h>
- #include <string.h>
- #define DEBUG 0
- #define DEBUG_INFO {\
- printf("\n");\
- printf("\t+---- DEBUG INFO -----+\n");\
- printf("\t| ACL : %s\n", aclFile);\
- printf("\t| KEY : %s\n", keyFile);\
- printf("\t| usr : %s\n", usr);\
- printf("\t| psw : %s\n", psw);\
- printf("\t| nStr: %s\n", newStr);\
- printf("\t| key : %s\n", key);\
- printf("\t| uPsw: %s\n", usrpsw);\
- printf("\t+---------------------+\n");\
- printf("\n");\
- }\
- #define MAX_BUFFER 8
- #define LINE_BUFFER 32
- #define READ_LINE 255
- #define ENC_MAX_BUFFER (MAX_BUFFER*2)+1
- #define ACLFILENAME "acl.txt"
- #define KEYFILENAME "key.txt"
- #define ALPHA 26
- int find(char str[], char c){
- int i;
- for (i = 0; i < strlen(str) && str[i]; i++)
- if(str[i] == c) return i;
- return -1;
- }
- void concatenate(char newStr[], char str1[], char str2[]){
- strcat(newStr, str1);
- strcat(newStr, ":");
- strcat(newStr, str2);
- }
- void lowercase(char str[]){
- int i;
- for(i = 0; i < strlen(str) && str[i]; i++)
- str[i] = tolower(str[i]);
- }
- void uppercase(char str[]){
- int i;
- for(i = 0; i < strlen(str) && str[i]; i++)
- str[i] = toupper(str[i]);
- }
- void encode(char key[], char str[]){
- lowercase(str);
- int i;
- for(i = 0; i < strlen(str) && str[i]; i++){
- if('a' <= str[i] && str[i] <= 'z'){
- str[i] = find(key, str[i]) + 'a';
- }
- }
- uppercase(str);
- }
- int validate(char str1[], char str2[]){
- if(strlen(str1) != strlen(str2)) return 0;
- int i;
- for(i = 0; i < strlen(str1) && i < strlen(str2) ; i++){
- if(str1[i] != str2[i]) return 0;
- }
- return 1;
- }
- int main(char argc[], int argv[]){
- char aclFile[strlen(ACLFILENAME) + 1];
- char usr [MAX_BUFFER + 1];
- char psw [MAX_BUFFER + 1];
- char buf [LINE_BUFFER];
- char key [ALPHA];
- char line [LINE_BUFFER];
- char usrpsw [ENC_MAX_BUFFER];
- char newStr [ENC_MAX_BUFFER];
- char keyFile[strlen(KEYFILENAME) + 1];
- int authenticated;
- while(1){
- // Init all variables
- strncpy(aclFile, ACLFILENAME, strlen(ACLFILENAME));
- aclFile[strlen(ACLFILENAME)] = 0;
- usr[0] = 0;
- psw[0] = 0;
- buf[0] = 0;
- key[0] = 0;
- usrpsw[0] = 0;
- newStr[0] = 0;
- strncpy(keyFile, KEYFILENAME, strlen(KEYFILENAME));
- keyFile[strlen(KEYFILENAME)] = 0;
- authenticated = 0;
- // Get user name
- printf("username: ");
- gets(buf);
- strncpy(usr, buf, MAX_BUFFER);
- usr[MAX_BUFFER] = 0;
- // Get password
- printf("password: ");
- gets(buf);
- strncpy(psw, buf, MAX_BUFFER);
- psw[MAX_BUFFER] = 0;
- // create new string
- concatenate(newStr, usr, psw);
- #if DEBUG
- DEBUG_INFO;
- #endif
- // open key file and get key.
- FILE * pFile = fopen(keyFile, "r");
- if(pFile != NULL){
- fgets(key, ALPHA, pFile);
- fclose(pFile);
- // open acl file and get encoded usr:psw.
- pFile = fopen(aclFile, "r");
- if(pFile != NULL){
- fgets(line, READ_LINE, pFile);
- strncpy(usrpsw, line, ENC_MAX_BUFFER);
- usrpsw[ENC_MAX_BUFFER] = 0;
- fclose(pFile);
- // encode string
- encode(key, newStr);
- #if DEBUG
- DEBUG_INFO;
- #endif
- // authenticate user
- authenticated = validate(usrpsw, newStr);
- }
- } // end of if open file key.
- if(authenticated){
- printf("Congratulations!!! You hacked my program!!!\n");
- printf("Please enter your name: ");
- pFile = fopen("log.txt", "a");
- if(pFile != NULL){
- gets(buf);
- fprintf(pFile, "%s\n", buf);
- fclose(pFile);
- }
- return 0;
- } else {
- printf("Login failed...\n");
- }
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement