Ejemplo n.º 1
0
Archivo: bank.c Proyecto: evanqi/atm
void send_no_user(Bank *bank)
{
    unsigned char *out = (unsigned char *)calloc(10000, sizeof(unsigned char));
    char *response = (char *) calloc(3, sizeof(char));
    response = "nouser";
    do_crypt(bank, (unsigned char *)response, out, 1);
    bank_send(bank, (char*) out, strlen((char *)out));
}
Ejemplo n.º 2
0
Archivo: bank.c Proyecto: yufangfred/hm
void handle_balance_atm(Bank* bank, char* args) {
    char* tmp;
    char send_msg[MAX_LINE_SIZE + 1];
    memset(send_msg, 0, MAX_LINE_SIZE);
    
    tmp = (char*)hash_table_find(bank->user_balance_ht, args);
    printf("balance:%s\n", tmp);
    strcat(send_msg, tmp);
    
    bank_encrypt(bank, send_msg, strlen(send_msg));
    bank_send(bank, send_msg, strlen(send_msg));
}
Ejemplo n.º 3
0
Archivo: bank.c Proyecto: yufangfred/hm
void handle_user_login(Bank* bank, char* args) {
    char send_msg[MAX_LINE_SIZE + 1];
    memset(send_msg, 0, MAX_LINE_SIZE);

    if ((char*)hash_table_find(bank->user_balance_ht, args) == NULL ||
            (char*)hash_table_find(bank->logged_user_ht, args) != NULL)  {
        strcat(send_msg, "NA");
    } else {
        strcat(send_msg, "OK");
        hash_table_add(bank->logged_user_ht, args, "1");
    }

    bank_encrypt(bank, send_msg, strlen(send_msg));
    bank_send(bank, send_msg, strlen(send_msg));
}
Ejemplo n.º 4
0
Archivo: bank.c Proyecto: yufangfred/hm
void handle_user_logout(Bank* bank, char* args) {
    char send_msg[MAX_LINE_SIZE + 1];
    memset(send_msg, 0, MAX_LINE_SIZE);

    if ((char*)hash_table_find(bank->logged_user_ht, args) == NULL)  {
        strcat(send_msg, "No user logged in");
    } else {
        strcat(send_msg, "OK");
        hash_table_del(bank->logged_user_ht, args);
    }

    bank_encrypt(bank, send_msg, strlen(send_msg));
    bank_send(bank, send_msg, strlen(send_msg));
    return;
}
Ejemplo n.º 5
0
Archivo: bank.c Proyecto: yufangfred/hm
void handle_withdraw(Bank* bank, char* args1, char* args2) {
    char* tmp;
    int balance;
    int amt;
    char send_msg[MAX_LINE_SIZE + 1];
    memset(send_msg, 0, MAX_LINE_SIZE);
    
    tmp = (char*)hash_table_find(bank->user_balance_ht, args1);
    sscanf(tmp, "%d", &balance);
    sscanf(args2, "%d", &amt);
    if (balance < amt) {
        strcat(send_msg, "IF");
    } else {
        strcat(send_msg, "AD");
        balance -= amt;
        memset(tmp, 0, 64);
        sprintf(tmp, "%d", balance);
        hash_table_add(bank->user_balance_ht, args1, tmp);
    }

    bank_encrypt(bank, send_msg, strlen(send_msg));
    bank_send(bank, send_msg, strlen(send_msg));
}
Ejemplo n.º 6
0
int main(int argc, char**argv)
{
int n;
char sendline[10000];
char recvline[10000];
char key[32];
memset(key,'\0',32);
unsigned char decrypted[10000];

FILE *file;
//memset(recvline,'\0',10000);
//char user_input[1000];
file=fopen(argv[1],"r");
char *split=strtok(argv[1],".");
split=strtok(NULL,".");
if(file==0 || strcmp(split,"bank")) {
    printf("Error opening ATM initialization file\n");
    return 64;
}

fread(key,sizeof(key),32,file);
key[32]='\0';
//printf("bank file contents: %s\n",key);
HashTable *users = hash_table_create(100);
HashTable *balance = hash_table_create(100);
Bank *bank = bank_create();
//bank->users = list_create();
//bank->usr_key = hash_table_create(100);
//bank->usr_bal = hash_table_create(100);
printf("%s", prompt);
fflush(stdout);

while(1)
{
    memset(decrypted,'\0',10000);
    memset(recvline,'\0',10000);
    //memset(sendline,'\0',10000);
    fd_set fds;
    FD_ZERO(&fds);
    FD_SET(0, &fds);
    FD_SET(bank->sockfd, &fds);
    select(bank->sockfd+1, &fds, NULL, NULL, NULL);

    if(FD_ISSET(0, &fds)) {
        fgets(sendline, 10000,stdin);
        bank_process_local_command(bank, sendline, strlen(sendline),users,balance);
        printf("%s", prompt);
        fflush(stdout);
    } else if(FD_ISSET(bank->sockfd, &fds)) {
        ;
        //int flag = 0;

        n = bank_recv(bank, recvline, 10000);


        //int flag=decrypt(recvline,key,decrypted,n);

        if(!decrypt(recvline,key,decrypted,n)) { //this means that it has not been decrypted correctly so it will return a null packet
            unsigned char encrypted[10000];
            char packet[10000];
            sprintf(packet,"<%s>",NULL);
            int out_size =0;
            encrypt(packet,key,encrypted,&out_size);
            bank_send(bank, encrypted, out_size);
            printf("%s", prompt);
            fflush(stdout);
            continue;
        }

        //printf("%s\n",decrypted);
        char * message=strtok(decrypted,"\n");

        bank_process_remote_command(bank, message, n, users,key,balance);

    }

}
hash_table_free(balance);
hash_table_free(users); //never executes
bank_free(bank);
//fclose(file);

return EXIT_SUCCESS;
}
Ejemplo n.º 7
0
Archivo: bank.c Proyecto: scmilburn/ATM
void bank_process_remote_command(Bank *bank, char *command, size_t len)
{
    // TODO: Implement the bank side of the ATM-bank protocol
    uint8_t opcode = *((uint8_t *) command);
    char errM[] = {0,0};

    if(opcode == 1){
        int namelen;
        char *name;
        Node *user = NULL;
        uint8_t *sendback = malloc(2);

        namelen = *((uint32_t *)(command+1));
        namelen = ntohl(namelen);

        name = malloc(namelen + 1);

        memcpy(name, command+5, namelen);

        name[namelen] = 0;

        user = get_client(bank->clientHead, name);

        sendback[0] = 1;

        if(user == NULL){
            sendback[1] = 0;
        }
        else
            sendback[1] = 1;

        bank_send(bank, sendback, 2);
        free(sendback);
    }
    else{
        uint8_t *mesHash = command + len-32;
        int messlen = len-33;
        if(messlen > 5){
            int namelen;
            char *name;
            uint8_t *data = malloc(messlen);
            Node *user = NULL;
            memcpy(data, command+1, messlen);

            namelen = *((uint32_t *)data);
            namelen = ntohl(namelen);

            name = malloc(namelen + 1);

            memcpy(name, data+4, namelen);

            name[namelen] = 0;

            user = get_client(bank->clientHead, name);

            if(user != NULL){
                EVP_MD_CTX *hashctx = EVP_MD_CTX_create();
                uint8_t thehash[32];
                int hashlen;

                EVP_DigestInit_ex(hashctx, EVP_sha256(), NULL);

                EVP_DigestUpdate(hashctx, user->PIN,4);
                EVP_DigestUpdate(hashctx, data, messlen);
                EVP_DigestFinal_ex(hashctx, thehash, &hashlen);
                EVP_MD_CTX_destroy(hashctx);

                if(compHash(thehash, mesHash) == 1){
                    if(opcode == 2){
                        uint8_t *sendback = malloc(2);
                        uint32_t with = *((uint32_t *)(data+namelen+4));

                        with = ntohl(with);
                        sendback[0] = 2;

                        if(with < user->balance){
                            user->balance = user->balance - with;
                            sendback[1] = 1;
                        }
                        else
                            sendback[1] = 0;

                        bank_send(bank, sendback, 2);
                        free(sendback);
                    }
                    else{
                        if(opcode == 3){
                            uint8_t *sendback = malloc(6);
                            uint32_t bal = htonl(user->balance);
                            sendback[0] = 3;
                            sendback[1] = 1;

                            memcpy(sendback + 2, &bal, 4);

                            bank_send(bank, sendback, 6);
                            free(sendback);
                        }
                        else{
                            bank_send(bank, errM, 2);
                        }
                    }
                }
                else{
                    bank_send(bank, errM, 2);
                }
            }
            else{
                bank_send(bank, errM, 2);
            }


            free(data);
        }
        else{
            bank_send(bank, errM, 2);
        }
    }
}
Ejemplo n.º 8
0
void bank_process_remote_command(Bank *bank, char *command, size_t len)
{
    // TODO: Implement the bank side of the ATM-bank protocol

	/*
	 * The following is a toy example that simply receives a
	 * string from the ATM, prepends "Bank got: " and echoes 
	 * it back to the ATM before printing it to stdout.
	 */

    FILE *fp;
    char decrypted[1024];
    char call[1024];
    strtok(command, "\n");
    // printf("cipher is: %s\n", command);   
    sprintf(call,"echo %s |openssl enc -aes-256-cbc -d -a -pass file:%s -salt",command,bank->symm_key);

    fp =popen(call,"r");
    memset(call,0x00,strlen(call));
    if(fp!=NULL) {
        const char space[] = " ";
        static char begin_session[] = "begin-session";
        static char withdraw[] = "withdraw";
        static char balance[] = "balance";

        fgets(decrypted,1024,fp);
        char *token;
        strtok(decrypted, "\n");
        token = strtok(decrypted, space);

        if (strcmp(token, begin_session) == 0) {
            token = strtok(NULL, space);

            char sendline[1024];
            char response[4];
            if (list_find(bank->users, token) == NULL)
                sprintf(response, "%s", "no");
            else
                sprintf(response, "%s", "yes");

            //To encrypt and make system call to create .card file
            sprintf(call,"echo %s |openssl enc -aes-256-cbc -e -a -pass file:%s -salt -out %s",response, bank->symm_key,"temp.txt");
            system(call);
            memset(call,0x00,strlen(call)); 
            FILE *dec;
            char cipher[1024];

            //open .card file and read in cipher
            dec = fopen("temp.txt", "r");
            fgets(cipher, 1024, (FILE*)dec);            
            strtok(cipher, "\n");
            fclose(dec);
            // printf("send cipher: %s\n", cipher);
            bank_send(bank, cipher, strlen(cipher));
            system("rm temp.txt");
            
        } else if (strcmp(token, withdraw) == 0) {
            char *user_name;
            char *amount;
            user_name = strtok(NULL, space);
            amount = strtok(NULL, space);

            char *ptr;
            unsigned int withdraw_amt = strtoul(amount, &ptr, 10);
            unsigned int *cur_b = list_find(bank->users, user_name);

            char sendline[1024];
            char response[4];

            if (*cur_b >= withdraw_amt) {
                unsigned int new_b = *cur_b - withdraw_amt;
                *cur_b = new_b;
                sprintf(response, "%s", "yes");
            } else {
                sprintf(response, "%s", "no");
            }

            //To encrypt and make system call to create .card file
            sprintf(call,"echo %s |openssl enc -aes-256-cbc -e -a -pass file:%s -salt -out %s",response, bank->symm_key,"temp.txt");
            system(call);
            memset(call,0x00,strlen(call));

            FILE *dec;
            char cipher[1024];

            //open .card file and read in cipher
            dec = fopen("temp.txt", "r");
            fgets(cipher, 1024, (FILE*)dec);            
            strtok(cipher, "\n");
            fclose(dec);
            // printf("cipher is: %s\n", cipher);
            bank_send(bank, cipher, strlen(cipher));
            system("rm temp.txt");
            
        } else if (strcmp(token, balance) == 0){
            char *user_name;
            char response[1024];
            user_name = strtok(NULL, space);
            unsigned int *cur_b = list_find(bank->users, user_name);
            
            sprintf(response, "%u", *cur_b);
            // printf("response is: %s\n", response);
            //To encrypt and make system call to create .card file
            sprintf(call,"echo %s |openssl enc -aes-256-cbc -e -a -pass file:%s -salt -out %s", response, bank->symm_key,"temp.txt");
            system(call);
            memset(call,0x00,strlen(call));
            FILE *dec;
            char cipher[1024];

            //open .card file and read in cipher
            dec = fopen("temp.txt", "r");
            fgets(cipher, 1024, (FILE*)dec);            
            strtok(cipher, "\n");
            fclose(dec);
            // printf("cipher is: %s\n", cipher);
            bank_send(bank, cipher, strlen(cipher));
            system("rm temp.txt");
        }
        memset(decrypted,0x00,strlen(decrypted));
        // if (strcmp())
        // printf("name and amount are: %s",decrypted); 

    }else {
        printf("Error couldn't open .card file");
    }
                            
    pclose(fp);

	/*
    char sendline[4];
    memset(sendline, 0x00, strlen(sendline));
    command[len]=0;
    strtok(command, "\n");
    if (list_find(bank->users, command) == NULL)
        sprintf(sendline, "%s", "no");
    else
        sprintf(sendline, "%s", "yes");
    bank_send(bank, sendline, strlen(sendline));
    // printf("Received the following:\n");
    // fputs(command, stdout);
    */
	
}
Ejemplo n.º 9
0
Archivo: bank.c Proyecto: evanqi/atm
void send_balance(Bank *bank, char *bal)
{
    unsigned char *out = (unsigned char *)calloc(10000, sizeof(unsigned char));
    do_crypt(bank, (unsigned char *)bal, out, 1);
    bank_send(bank, (char*) out, strlen((char *)out));
}