Ejemplo n.º 1
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.º 2
0
int main(int argc, char**argv)
{
  int n;
  char sendline[1000];
  char recvline[1000];
  char *bankPath;
  FILE *bankFile;
  char *second;
  int filenamelen = 0;
  Bank *bank = bank_create();


  if(argc == 1) {
    printf("Usage: Bank <filename>\n");
    return 62; 
  } //user fails to provide exactly one argument

  second = argv[1];

  filenamelen = strlen(second);

  bankPath = malloc(1 + filenamelen + 5);

  strncpy(bankPath, second, strlen(second));

  bankPath[filenamelen] = '.';
  bankPath[filenamelen+1] = 'b';
  bankPath[filenamelen+2] = 'a';
  bankPath[filenamelen+3] = 'n';
  bankPath[filenamelen+4] = 'k';
  bankPath[filenamelen+5] = 0;

  bankFile = fopen(bankPath, "r");

  if(bankFile == NULL){
    printf("Error opening bank initialization file\n");
    return 64;
  }

  fclose(bankFile);

   printf("%s", prompt);
   fflush(stdout);

   while(1)
   {
       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));
           printf("%s", prompt);
           fflush(stdout);
       }
       else if(FD_ISSET(bank->sockfd, &fds))
       {
           n = bank_recv(bank, recvline, 10000);
           bank_process_remote_command(bank, recvline, n);
       }
   }

   return EXIT_SUCCESS;
}
Ejemplo n.º 3
0
int main(int argc, char**argv)
{
    char* filename = argv[1];
    FILE* fp;
    fp = fopen(filename,"r");
    if(fp == NULL){
        puts("ERROR OPENING FILE");
        return 64;
    }
    int n;
    char sendline[1000];
    char recvline[1000];

    Bank *bank = bank_create();
    bank->bank_pub_key = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
    if(bank->bank_pub_key == NULL)
    {
        printf("\n%s\n", "Error Reading bank public key");
        RSA_free(bank->bank_pub_key);
        return 0;
    }
    bank->bank_priv_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
    if(bank->bank_priv_key == NULL)
    {
        printf("\n%s\n", "Error Reading bank private key");
        RSA_free(bank->bank_priv_key);
        return 0;
    }
    bank->atm_pub_key = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
    if(bank->atm_pub_key == NULL)
    {
        printf("\n%s\n", "Error Reading atm public key");
        RSA_free(bank->atm_pub_key);
        return 0;
    }

    fclose(fp);

    printf("%s", prompt);
    fflush(stdout);

    while(1)
    {
       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));
           printf("%s", prompt);
           fflush(stdout);
       }
       else if(FD_ISSET(bank->sockfd, &fds))
       {
           n = bank_recv(bank, recvline, 10000);
           bank_process_remote_command(bank, recvline, n);
       }
    }
    RSA_free(bank->bank_priv_key);
    return EXIT_SUCCESS;
}
Ejemplo n.º 4
0
int main(int argc, char**argv)
{
   int n;
   char sendline[1000];
   char recvline[1000];

   if (argc != 2) {
     printf("Error opening bank initialization file\n");
     return 64;
   }

   char *token;
   char argv_cp[1024];
   strcpy(argv_cp, argv[1]);
   strtok(argv_cp, ".");
   token = strtok(NULL, ".");
   if (strcmp(token, "bank") != 0) {
    printf("Error opening bank initialization file\n");
    return 64;
   }

   Bank *bank = bank_create();

   //Check for command line arguements
   FILE *init_file;
   strcpy(bank->symm_key, argv[1]);
   init_file = fopen(argv[1],"r");
    
   if (init_file == NULL) {
     printf("Error opening bank initialization file\n");
     return 64;
   } 

   fclose(init_file);
   printf("%s", prompt);
   fflush(stdout);

   while(1)
   {
       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));
           printf("%s", prompt);
           fflush(stdout);
       }
       else if(FD_ISSET(bank->sockfd, &fds))
       {
           n = bank_recv(bank, recvline, 10000);
           bank_process_remote_command(bank, recvline, n);
       }
   }

   return EXIT_SUCCESS;
}