Ejemplo n.º 1
0
int main(int argc, char** argv)
{
    //checking
    //int argc = 5;
    //char* argv[] = {"logread", "-K", "secret", "-R", "/root/Desktop/.hidden/bibifi/log1"};
    //char* argv[] = {"logread", "-B", "/root/Documents/NetBeansProjects/bibifi-final/dist/Debug/GNU-Linux-x86/hello"};
    if (argc < 5)
        normalExit(); // parameter number if too low



    //normal mode
    int sflag = 0, rflag = 0, tflag = 0, iflag = 0;
    char* token = NULL, * employeename = NULL, *guestname = NULL, *logfilename = NULL;

    int switchflag = -1;
    //reading options
    while ((switchflag = getopt(argc, argv, "K:SRTIE:G:")) != -1)
    {
        switch (switchflag)
        {
            case 'K':
                if (!checkToken(optarg)) normalExit();
                token = optarg;
                break;
            case 'S':
                sflag = true;
                break;
            case 'R':
                rflag = true;
                break;
            case 'T':
                tflag = true;
                break;
            case 'I':
                iflag = true;
                break;
            case 'E':
                if (!checkName(optarg)) normalExit();
                employeename = optarg;
                break;
            case 'G':
                if (!checkName(optarg)) normalExit();
                guestname = optarg;
                break;
            default:
                normalExit();
        }
    }
    if (argc - optind != 1)
        normalExit();
    else
    {
        if (!checkLogName(argv[optind])) normalExit();
        logfilename = argv[optind];
    }

    //here to check program syntax sanity
    int count_true = 0;
    if (sflag) count_true++;
    if (iflag) count_true++;
    if (rflag) count_true++;
    if (tflag) count_true++;

    if (token == NULL) normalExit();
    if (count_true != 1) normalExit();
    if (employeename != NULL && guestname != NULL && rflag) normalExit();
    if (employeename == NULL && guestname == NULL && rflag) normalExit();
    if (sflag && (employeename != NULL || guestname != NULL)) normalExit();
    if (tflag || iflag)
    {
        printf("unimplemented");
        exit(0);
    }
    //checking if file exists
    //getting the resolved path
    execution_argv[0] = logfilename;
    if (executeCommand("file_exists.sh", 1, execution_argv) == 0) // file found
    {
        //decrypt file
        execution_argv[0] = logfilename;
        execution_argv[1] = token;
        if (executeCommand("decrypt.sh", 2, execution_argv) != 0)
            securityExit();

        //reading from the file time, type, where, name
        FILE* input = fopen(logfilename, "r");
        if (input == NULL) normalExitWithEncryption(logfilename, token);
        int log_count = fill_log_array(input);
        fclose(input);


        if (sflag)
        {
            char ** emplist = (char **) malloc(sizeof (char*) * 1000);
            char ** guestlist = (char **) malloc(sizeof (char*) * 1000);
            int i, emp_count = 0, guest_count = 0;
            for (i = 0; i < 1000; i++)
            {
                emplist[i] = (char*) malloc(sizeof (char) * 100);
                guestlist[i] = (char*) malloc(sizeof (char) * 100);
            }
            emp_count = get_employee_names_list(emplist, log_count);
            guest_count = get_guest_names_list(guestlist, log_count);

            if (emp_count == 0)
                printf("\n");
            else
            {
                for (i = 0; i < emp_count; i++)
                {
                    if (i == emp_count - 1)
                        printf("%s\n", emplist[i]);
                    else
                        printf("%s,", emplist[i]);
                }
            }

            if (guest_count == 0)
                printf("\n");
            else
            {
                for (i = 0; i < guest_count; i++)
                {
                    if (i == guest_count - 1)
                        printf("%s\n", guestlist[i]);
                    else
                        printf("%s,", guestlist[i]);
                }
            }
            int* room_ids = (int*) malloc(sizeof (int) * 100);
            int room_count = get_room_list(room_ids, log_count);
            int j;
            for (i = 0; i < room_count; i++)
            {
                int who_count = who_is_in_room(guestlist, room_ids[i], log_count);
                if (who_count != 0)
                    printf("%d: ", room_ids[i]);

                for (j = 0; j < who_count; j++)
                {
                    if (j == who_count - 1) printf("%s\n", guestlist[j]);
                    else printf("%s,", guestlist[j]);
                }
            }
        }

        if (rflag)
        {
            char *some_name;
            int some_type;
            some_type = EMPLOYEETYPE, some_name = employeename;
            if (employeename == NULL)
                some_type = GUESTTYPE, some_name = guestname;

            int* room_ids = (int*) malloc(sizeof (int) * 100);
            int room_count = get_r_request(room_ids, some_name, some_type, log_count);
            int index;
            for (index = 0; index < room_count; index++)
            {
                if (index == room_count - 1)
                    printf("%d", room_ids[index]);
                else
                    printf("%d,", room_ids[index]);
            }
            printf("\n");
        }


        //encrypt file
        execution_argv[0] = logfilename;
        execution_argv[1] = token;
        if (executeCommand("encrypt.sh", 2, execution_argv) != 0)
            normalExitWithEncryption(logfilename, token);

    } else
        normalExit();

    return (EXIT_SUCCESS);
}
Ejemplo n.º 2
0
/*
 *Main thread for each client.  Receives all messages
 *and passes the data off to the correct function.  Receives
 *a pointer to the file descriptor for the socket the thread
 *should listen on
 */
void *client_receive(void *ptr) {
   int client = *(int *) ptr;
   int received;
   int logged_in = 0;
   packet in_pkt, *client_message_ptr = &in_pkt;

   while (1) {
      received = recv(client, &in_pkt, sizeof(packet), 0);
      if (received) {
         debugPacket(client_message_ptr);

         // Responses to not logged in clients
         if (!logged_in) {
            if(in_pkt.options == REGISTER) {
               logged_in = register_user(&in_pkt, client);
            }
            else if(in_pkt.options == LOGIN) {
               logged_in = login(&in_pkt, client);
            }
            else if(in_pkt.options == EXIT) {
               close(client);
               return NULL;
            }
            else {
               sendError("Not logged in.", client);
            }
         }

         // Responses to logged in clients
         else if (logged_in) {
            // Handle option messages for logged in client
            if (in_pkt.options < 1000) {
               if(in_pkt.options == REGISTER) { 
                  sendError("You may not register while logged in.", client);
               }
               else if(in_pkt.options == SETPASS) {
                  set_pass(&in_pkt, client);
               }
               else if(in_pkt.options == SETNAME) {
                  set_name(&in_pkt, client);
               }
               else if(in_pkt.options == LOGIN) {
                  sendError("Already logged in.", client);
               }
               else if(in_pkt.options == EXIT) {
                  exit_client(&in_pkt, client);
                  return NULL;
               }
               else if(in_pkt.options == INVITE) {
                  invite(&in_pkt, client);
               }
               else if(in_pkt.options == JOIN) {
                  join(&in_pkt, client);
               }
               else if(in_pkt.options == LEAVE) {
                  leave(&in_pkt, client);
               }
               else if(in_pkt.options == GETALLUSERS) {
                  get_active_users(client);
               }
               else if(in_pkt.options == GETUSERS) {
                  get_room_users(&in_pkt, client);
               }
               else if(in_pkt.options == GETUSER) {
                  user_lookup(&in_pkt, client);
               }
               else if(in_pkt.options == GETROOMS) {
                  get_room_list(client);
               }
               else if(in_pkt.options == GETMOTD) {
                  sendMOTD(client);
               }
               else if(in_pkt.options == 0) {
                  printf("%s --- Error:%s Abrupt disconnect on logged in client.\n", RED, NORMAL);
                  exit_client(&in_pkt, client);
                  return NULL;
               }
               else {
                  printf("%s --- Error:%s Unknown message received from client.\n", RED, NORMAL);
	       }
            }
            // Handle conversation message for logged in client
            else {
               // Will be treated as a message packet, safe to santize entire buffer
               sanitizeInput((void *)&in_pkt.buf, 0);
               send_message(&in_pkt, client);
            }
         }

         memset(&in_pkt, 0, sizeof(packet));
      }
   }
   return NULL;
}